Debian Codegraph @

src/core/SkClipStack.h

raw
1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkClipStack_DEFINED
9 #define SkClipStack_DEFINED
10
11 #include "include/core/SkClipOp.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkShader.h"
18 #include "include/private/base/SkAssert.h"
19 #include "include/private/base/SkDebug.h"
20 #include "include/private/base/SkDeque.h"
21
22 #include <cstddef>
23 #include <cstdint>
24 #include <optional>
25 #include <utility>
26
27 // Because a single save/restore state can have multiple clips, this class
28 // stores the stack depth (fSaveCount) and clips (fDeque) separately.
29 // Each clip in fDeque stores the stack state to which it belongs
30 // (i.e., the fSaveCount in force when it was added). Restores are thus
31 // implemented by removing clips from fDeque that have an fSaveCount larger
32 // then the freshly decremented count.
33 class SkClipStack {
34 public:
35 enum BoundsType {
36 // The bounding box contains all the pixels that can be written to
37 kNormal_BoundsType,
38 // The bounding box contains all the pixels that cannot be written to.
39 // The real bound extends out to infinity and all the pixels outside
40 // of the bound can be written to. Note that some of the pixels inside
41 // the bound may also be writeable but all pixels that cannot be
42 // written to are guaranteed to be inside.
43 kInsideOut_BoundsType
44 };
45
46 /**
47 * An element of the clip stack. It represents a shape combined with the prevoius clip using a
48 * set operator. Each element can be antialiased or not.
49 */
50 class Element {
51 public:
52 /** This indicates the shape type of the clip element in device space. */
53 enum class DeviceSpaceType {
54 //!< This element makes the clip empty (regardless of previous elements).
55 kEmpty,
56 //!< This element combines a device space rect with the current clip.
57 kRect,
58 //!< This element combines a device space round-rect with the current clip.
59 kRRect,
60 //!< This element combines a device space path with the current clip.
61 kPath,
62 //!< This element does not have geometry, but applies a shader to the clip
63 kShader,
64
65 kLastType = kShader
66 };
67 static const int kTypeCnt = (int)DeviceSpaceType::kLastType + 1;
68
69 Element() {
70 this->initCommon(0, SkClipOp::kIntersect, false);
71 this->setEmpty();
72 }
73
74 Element(const Element&);
75
76 Element(const SkRect& rect, const SkMatrix& m, SkClipOp op, bool doAA) {
77 this->initRect(0, rect, m, op, doAA);
78 }
79
80 Element(const SkRRect& rrect, const SkMatrix& m, SkClipOp op, bool doAA) {
81 this->initRRect(0, rrect, m, op, doAA);
82 }
83
84 Element(const SkPath& path, const SkMatrix& m, SkClipOp op, bool doAA) {
85 this->initPath(0, path, m, op, doAA);
86 }
87
88 explicit Element(sk_sp<SkShader> shader) { this->initShader(0, std::move(shader)); }
89
90 Element(const SkRect& rect, bool doAA) {
91 this->initReplaceRect(0, rect, doAA);
92 }
93
94 ~Element();
95
96 bool operator== (const Element& element) const;
97 bool operator!= (const Element& element) const { return !(*this == element); }
98
99 //!< Call to get the type of the clip element.
100 DeviceSpaceType getDeviceSpaceType() const { return fDeviceSpaceType; }
101
102 //!< Call to get the save count associated with this clip element.
103 int getSaveCount() const { return fSaveCount; }
104
105 //!< Call if getDeviceSpaceType() is kPath to get the path.
106 const SkPath& getDeviceSpacePath() const {
107 SkASSERT(DeviceSpaceType::kPath == fDeviceSpaceType);
108 return *fDeviceSpacePath;
109 }
110
111 //!< Call if getDeviceSpaceType() is kRRect to get the round-rect.
112 const SkRRect& getDeviceSpaceRRect() const {
113 SkASSERT(DeviceSpaceType::kRRect == fDeviceSpaceType);
114 return fDeviceSpaceRRect;
115 }
116
117 //!< Call if getDeviceSpaceType() is kRect to get the rect.
118 const SkRect& getDeviceSpaceRect() const {
119 SkASSERT(DeviceSpaceType::kRect == fDeviceSpaceType &&
120 (fDeviceSpaceRRect.isRect() || fDeviceSpaceRRect.isEmpty()));
121 return fDeviceSpaceRRect.getBounds();
122 }
123
124 //!<Call if getDeviceSpaceType() is kShader to get a reference to the clip shader.
125 sk_sp<SkShader> refShader() const {
126 return fShader;
127 }
128 const SkShader* getShader() const {
129 return fShader.get();
130 }
131
132 //!< Call if getDeviceSpaceType() is not kEmpty to get the set operation used to combine
133 //!< this element.
134 SkClipOp getOp() const { return fOp; }
135 // Augments getOps()'s behavior by requiring a clip reset before the op is applied.
136 bool isReplaceOp() const { return fIsReplace; }
137
138 //!< Call to get the element as a path, regardless of its type.
139 SkPath asDeviceSpacePath() const;
140
141 //!< Call if getType() is not kPath to get the element as a round rect.
142 const SkRRect& asDeviceSpaceRRect() const {
143 SkASSERT(DeviceSpaceType::kPath != fDeviceSpaceType);
144 return fDeviceSpaceRRect;
145 }
146
147 /** If getType() is not kEmpty this indicates whether the clip shape should be anti-aliased
148 when it is rasterized. */
149 bool isAA() const { return fDoAA; }
150
151 /** The GenID can be used by clip stack clients to cache representations of the clip. The
152 ID corresponds to the set of clip elements up to and including this element within the
153 stack not to the element itself. That is the same clip path in different stacks will
154 have a different ID since the elements produce different clip result in the context of
155 their stacks. */
156 uint32_t getGenID() const { SkASSERT(kInvalidGenID != fGenID); return fGenID; }
157
158 /**
159 * Gets the bounds of the clip element, either the rect or path bounds. (Whether the shape
160 * is inverse filled is not considered.)
161 */
162 const SkRect& getBounds() const;
163
164 /**
165 * Conservatively checks whether the clip shape contains the rect/rrect. (Whether the shape
166 * is inverse filled is not considered.)
167 */
168 bool contains(const SkRect& rect) const;
169 bool contains(const SkRRect& rrect) const;
170
171 /**
172 * Is the clip shape inverse filled.
173 */
174 bool isInverseFilled() const {
175 return DeviceSpaceType::kPath == fDeviceSpaceType &&
176 fDeviceSpacePath->isInverseFillType();
177 }
178
179 #ifdef SK_DEBUG
180 /**
181 * Dumps the element to SkDebugf. This is intended for Skia development debugging
182 * Don't rely on the existence of this function or the formatting of its output.
183 */
184 void dump() const;
185 #endif
186
187 private:
188 friend class SkClipStack;
189
190 std::optional<SkPath> fDeviceSpacePath;
191 SkRRect fDeviceSpaceRRect;
192 sk_sp<SkShader> fShader;
193 int fSaveCount; // save count of stack when this element was added.
194 SkClipOp fOp;
195 DeviceSpaceType fDeviceSpaceType;
196 bool fDoAA;
197 bool fIsReplace;
198
199 /* fFiniteBoundType and fFiniteBound are used to incrementally update the clip stack's
200 bound. When fFiniteBoundType is kNormal_BoundsType, fFiniteBound represents the
201 conservative bounding box of the pixels that aren't clipped (i.e., any pixels that can be
202 drawn to are inside the bound). When fFiniteBoundType is kInsideOut_BoundsType (which
203 occurs when a clip is inverse filled), fFiniteBound represents the conservative bounding
204 box of the pixels that _are_ clipped (i.e., any pixels that cannot be drawn to are inside
205 the bound). When fFiniteBoundType is kInsideOut_BoundsType the actual bound is the
206 infinite plane. This behavior of fFiniteBoundType and fFiniteBound is required so that we
207 can capture the cancelling out of the extensions to infinity when two inverse filled
208 clips are Booleaned together. */
209 SkClipStack::BoundsType fFiniteBoundType;
210 SkRect fFiniteBound;
211
212 // When element is applied to the previous elements in the stack is the result known to be
213 // equivalent to a single rect intersection? IIOW, is the clip effectively a rectangle.
214 bool fIsIntersectionOfRects;
215
216 uint32_t fGenID;
217 explicit Element(int saveCount) {
218 this->initCommon(saveCount, SkClipOp::kIntersect, false);
219 this->setEmpty();
220 }
221
222 Element(int saveCount, const SkRRect& rrect, const SkMatrix& m, SkClipOp op, bool doAA) {
223 this->initRRect(saveCount, rrect, m, op, doAA);
224 }
225
226 Element(int saveCount, const SkRect& rect, const SkMatrix& m, SkClipOp op, bool doAA) {
227 this->initRect(saveCount, rect, m, op, doAA);
228 }
229
230 Element(int saveCount, const SkPath& path, const SkMatrix& m, SkClipOp op, bool doAA) {
231 this->initPath(saveCount, path, m, op, doAA);
232 }
233
234 Element(int saveCount, sk_sp<SkShader> shader) {
235 this->initShader(saveCount, std::move(shader));
236 }
237
238 Element(int saveCount, const SkRect& rect, bool doAA) {
239 this->initReplaceRect(saveCount, rect, doAA);
240 }
241
242 void initCommon(int saveCount, SkClipOp op, bool doAA);
243 void initRect(int saveCount, const SkRect&, const SkMatrix&, SkClipOp, bool doAA);
244 void initRRect(int saveCount, const SkRRect&, const SkMatrix&, SkClipOp, bool doAA);
245 void initPath(int saveCount, const SkPath&, const SkMatrix&, SkClipOp, bool doAA);
246 void initAsPath(int saveCount, const SkPath&, const SkMatrix&, SkClipOp, bool doAA);
247 void initShader(int saveCount, sk_sp<SkShader>);
248 void initReplaceRect(int saveCount, const SkRect&, bool doAA);
249
250 void setEmpty();
251
252 // All Element methods below are only used within SkClipStack.cpp
253 inline void checkEmpty() const;
254 inline bool canBeIntersectedInPlace(int saveCount, SkClipOp op) const;
255 /* This method checks to see if two rect clips can be safely merged into one. The issue here
256 is that to be strictly correct all the edges of the resulting rect must have the same
257 anti-aliasing. */
258 bool rectRectIntersectAllowed(const SkRect& newR, bool newAA) const;
259 /** Determines possible finite bounds for the Element given the previous element of the
260 stack */
261 void updateBoundAndGenID(const Element* prior);
262 // The different combination of fill & inverse fill when combining bounding boxes
263 enum FillCombo {
264 kPrev_Cur_FillCombo,
265 kPrev_InvCur_FillCombo,
266 kInvPrev_Cur_FillCombo,
267 kInvPrev_InvCur_FillCombo
268 };
269 // per-set operation functions used by updateBoundAndGenID().
270 inline void combineBoundsDiff(FillCombo combination, const SkRect& prevFinite);
271 inline void combineBoundsIntersection(int combination, const SkRect& prevFinite);
272 };
273
274 SkClipStack();
275 SkClipStack(void* storage, size_t size);
276 SkClipStack(const SkClipStack& b);
277 ~SkClipStack();
278
279 SkClipStack& operator=(const SkClipStack& b);
280 bool operator==(const SkClipStack& b) const;
281 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
282
283 void reset();
284
285 int getSaveCount() const { return fSaveCount; }
286 void save();
287 void restore();
288
289 class AutoRestore {
290 public:
291 AutoRestore(SkClipStack* cs, bool doSave)
292 : fCS(cs), fSaveCount(cs->getSaveCount())
293 {
294 if (doSave) {
295 fCS->save();
296 }
297 }
298 ~AutoRestore() {
299 SkASSERT(fCS->getSaveCount() >= fSaveCount); // no underflow
300 while (fCS->getSaveCount() > fSaveCount) {
301 fCS->restore();
302 }
303 }
304
305 private:
306 SkClipStack* fCS;
307 const int fSaveCount;
308 };
309
310 /**
311 * getBounds places the current finite bound in its first parameter. In its
312 * second, it indicates which kind of bound is being returned. If
313 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
314 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
315 * encloses all the un-writeable pixels and the true/normal bound is the
316 * infinite plane. isIntersectionOfRects is an optional parameter
317 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
318 */
319 void getBounds(SkRect* canvFiniteBound,
320 BoundsType* boundType,
321 bool* isIntersectionOfRects = nullptr) const;
322
323 SkRect bounds(const SkIRect& deviceBounds) const;
324 bool isEmpty(const SkIRect& deviceBounds) const;
325
326 /**
327 * Returns true if the input (r)rect in device space is entirely contained
328 * by the clip. A return value of false does not guarantee that the (r)rect
329 * is not contained by the clip.
330 */
331 bool quickContains(const SkRect& devRect) const {
332 return this->isWideOpen() || this->internalQuickContains(devRect);
333 }
334
335 bool quickContains(const SkRRect& devRRect) const {
336 return this->isWideOpen() || this->internalQuickContains(devRRect);
337 }
338
339 void clipDevRect(const SkIRect& ir, SkClipOp op) {
340 SkRect r;
341 r.set(ir);
342 this->clipRect(r, SkMatrix::I(), op, false);
343 }
344 void clipRect(const SkRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
345 void clipRRect(const SkRRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
346 void clipPath(const SkPath&, const SkMatrix& matrix, SkClipOp, bool doAA);
347 void clipShader(sk_sp<SkShader>);
348 // An optimized version of clipDevRect(emptyRect, kIntersect, ...)
349 void clipEmpty();
350
351 void replaceClip(const SkRect& devRect, bool doAA);
352
353 /**
354 * isWideOpen returns true if the clip state corresponds to the infinite
355 * plane (i.e., draws are not limited at all)
356 */
357 bool isWideOpen() const { return this->getTopmostGenID() == kWideOpenGenID; }
358
359 /**
360 * This method quickly and conservatively determines whether the entire stack is equivalent to
361 * intersection with a rrect given a bounds, where the rrect must not contain the entire bounds.
362 *
363 * @param bounds A bounds on what will be drawn through the clip. The clip only need be
364 * equivalent to a intersection with a rrect for draws within the bounds. The
365 * returned rrect must intersect the bounds but need not be contained by the
366 * bounds.
367 * @param rrect If return is true rrect will contain the rrect equivalent to the stack.
368 * @param aa If return is true aa will indicate whether the equivalent rrect clip is
369 * antialiased.
370 * @return true if the stack is equivalent to a single rrect intersect clip, false otherwise.
371 */
372 bool isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const;
373
374 /**
375 * The generation ID has three reserved values to indicate special
376 * (potentially ignorable) cases
377 */
378 static const uint32_t kInvalidGenID = 0; //!< Invalid id that is never returned by
379 //!< SkClipStack. Useful when caching clips
380 //!< based on GenID.
381 static const uint32_t kEmptyGenID = 1; // no pixels writeable
382 static const uint32_t kWideOpenGenID = 2; // all pixels writeable
383
384 uint32_t getTopmostGenID() const;
385
386 #ifdef SK_DEBUG
387 /**
388 * Dumps the contents of the clip stack to SkDebugf. This is intended for Skia development
389 * debugging. Don't rely on the existence of this function or the formatting of its output.
390 */
391 void dump() const;
392 #endif
393
394 public:
395 class Iter {
396 public:
397 enum IterStart {
398 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
399 kTop_IterStart = SkDeque::Iter::kBack_IterStart
400 };
401
402 /**
403 * Creates an uninitialized iterator. Must be reset()
404 */
405 Iter();
406
407 Iter(const SkClipStack& stack, IterStart startLoc);
408
409 /**
410 * Return the clip element for this iterator. If next()/prev() returns NULL, then the
411 * iterator is done.
412 */
413 const Element* next();
414 const Element* prev();
415
416 /**
417 * Moves the iterator to the topmost element with the specified RegionOp and returns that
418 * element. If no clip element with that op is found, the first element is returned.
419 */
420 const Element* skipToTopmost(SkClipOp op);
421
422 /**
423 * Restarts the iterator on a clip stack.
424 */
425 void reset(const SkClipStack& stack, IterStart startLoc);
426
427 private:
428 const SkClipStack* fStack;
429 SkDeque::Iter fIter;
430 };
431
432 /**
433 * The B2TIter iterates from the bottom of the stack to the top.
434 * It inherits privately from Iter to prevent access to reverse iteration.
435 */
436 class B2TIter : private Iter {
437 public:
438 B2TIter() {}
439
440 /**
441 * Wrap Iter's 2 parameter ctor to force initialization to the
442 * beginning of the deque/bottom of the stack
443 */
444 B2TIter(const SkClipStack& stack)
445 : INHERITED(stack, kBottom_IterStart) {
446 }
447
448 using Iter::next;
449
450 /**
451 * Wrap Iter::reset to force initialization to the
452 * beginning of the deque/bottom of the stack
453 */
454 void reset(const SkClipStack& stack) {
455 this->INHERITED::reset(stack, kBottom_IterStart);
456 }
457
458 private:
459
460 using INHERITED = Iter;
461 };
462
463 /**
464 * GetConservativeBounds returns a conservative bound of the current clip.
465 * Since this could be the infinite plane (if inverse fills were involved) the
466 * maxWidth and maxHeight parameters can be used to limit the returned bound
467 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
468 * allow the caller to offset the returned bound to account for translated
469 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
470 * the translation (+offsetX, +offsetY) is applied before the clamp to the
471 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
472 * isIntersectionOfRects is an optional parameter that is true when
473 * 'devBounds' is the result of an intersection of rects. In this case
474 * 'devBounds' is the exact answer/clip.
475 */
476 void getConservativeBounds(int offsetX,
477 int offsetY,
478 int maxWidth,
479 int maxHeight,
480 SkRect* devBounds,
481 bool* isIntersectionOfRects = nullptr) const;
482
483 private:
484 friend class Iter;
485
486 SkDeque fDeque;
487 int fSaveCount;
488
489 bool internalQuickContains(const SkRect& devRect) const;
490 bool internalQuickContains(const SkRRect& devRRect) const;
491
492 /**
493 * Helper for clipDevPath, etc.
494 */
495 void pushElement(const Element& element);
496
497 /**
498 * Restore the stack back to the specified save count.
499 */
500 void restoreTo(int saveCount);
501
502 /**
503 * Return the next unique generation ID.
504 */
505 static uint32_t GetNextGenID();
506 };
507
508 #endif