blob: d4f6a5298589c8df25d9e033f03924247f2178a2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com5c3d1472011-02-22 19:12:23 +00008#ifndef SkClipStack_DEFINED
9#define SkClipStack_DEFINED
10
11#include "SkDeque.h"
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000012#include "SkPath.h"
13#include "SkRect.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000014#include "SkRegion.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000015#include "SkTDArray.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000016
reed@google.com5c3d1472011-02-22 19:12:23 +000017
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000018// Because a single save/restore state can have multiple clips, this class
19// stores the stack depth (fSaveCount) and clips (fDeque) separately.
20// Each clip in fDeque stores the stack state to which it belongs
21// (i.e., the fSaveCount in force when it was added). Restores are thus
22// implemented by removing clips from fDeque that have an fSaveCount larger
23// then the freshly decremented count.
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000024class SK_API SkClipStack {
reed@google.com5c3d1472011-02-22 19:12:23 +000025public:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000026 enum BoundsType {
27 // The bounding box contains all the pixels that can be written to
28 kNormal_BoundsType,
29 // The bounding box contains all the pixels that cannot be written to.
30 // The real bound extends out to infinity and all the pixels outside
31 // of the bound can be written to. Note that some of the pixels inside
32 // the bound may also be writeable but all pixels that cannot be
33 // written to are guaranteed to be inside.
34 kInsideOut_BoundsType
35 };
36
37 class Element {
38 public:
39 enum Type {
40 //!< This element makes the clip empty (regardless of previous elements).
41 kEmpty_Type,
42 //!< This element combines a rect with the current clip using a set operation
43 kRect_Type,
44 //!< This element combines a path with the current clip using a set operation
45 kPath_Type,
46 };
47
48 Element() {
49 this->initCommon(0, SkRegion::kReplace_Op, false);
50 this->setEmpty();
51 }
52
53 Element(const SkRect& rect, SkRegion::Op op, bool doAA) {
54 this->initRect(0, rect, op, doAA);
55 }
56
57 Element(const SkPath& path, SkRegion::Op op, bool doAA) {
58 this->initPath(0, path, op, doAA);
59 }
60
61 //!< Call to get the type of the clip element.
62 Type getType() const { return fType; }
63
64 //!< Call if getType() is kPath to get the path.
65 const SkPath& getPath() const { return fPath; }
66
67 //!< Call if getType() is kRect to get the rect.
68 const SkRect& getRect() const { return fRect; }
69
70 //!< Call if getType() is not kEmpty to get the set operation used to combine this element.
71 SkRegion::Op getOp() const { return fOp; }
72
73 /** If getType() is not kEmpty this indicates whether the clip shape should be anti-aliased
74 when it is rasterized. */
75 bool isAA() const { return fDoAA; }
76
77 /** The GenID can be used by clip stack clients to cache representations of the clip. The
78 ID corresponds to the set of clip elements up to and including this element within the
79 stack not to the element itself. That is the same clip path in different stacks will
80 have a different ID since the elements produce different clip result in the context of
81 their stacks. */
82 int32_t getGenID() const { return fGenID; }
83
84 private:
85 friend class SkClipStack;
86
87 SkPath fPath;
88 SkRect fRect;
89 int fSaveCount; // save count of stack when this element was added.
90 SkRegion::Op fOp;
91 Type fType;
92 bool fDoAA;
93
94 /* fFiniteBoundType and fFiniteBound are used to incrementally update the clip stack's
95 bound. When fFiniteBoundType is kNormal_BoundsType, fFiniteBound represents the
96 conservative bounding box of the pixels that aren't clipped (i.e., any pixels that can be
97 drawn to are inside the bound). When fFiniteBoundType is kInsideOut_BoundsType (which
98 occurs when a clip is inverse filled), fFiniteBound represents the conservative bounding
99 box of the pixels that _are_ clipped (i.e., any pixels that cannot be drawn to are inside
100 the bound). When fFiniteBoundType is kInsideOut_BoundsType the actual bound is the
101 infinite plane. This behavior of fFiniteBoundType and fFiniteBound is required so that we
102 can capture the cancelling out of the extensions to infinity when two inverse filled
103 clips are Booleaned together. */
104 SkClipStack::BoundsType fFiniteBoundType;
105 SkRect fFiniteBound;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000106
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000107 // When element is applied to the previous elements in the stack is the result known to be
108 // equivalent to a single rect intersection? IIOW, is the clip effectively a rectangle.
109 bool fIsIntersectionOfRects;
110
111 int fGenID;
112
113 Element(int saveCount) {
114 this->initCommon(saveCount, SkRegion::kReplace_Op, false);
115 this->setEmpty();
116 }
117
118 Element(int saveCount, const SkRect& rect, SkRegion::Op op, bool doAA) {
119 this->initRect(saveCount, rect, op, doAA);
120 }
121
122 Element(int saveCount, const SkPath& path, SkRegion::Op op, bool doAA) {
123 this->initPath(saveCount, path, op, doAA);
124 }
125
126 void initCommon(int saveCount, SkRegion::Op op, bool doAA) {
127 fSaveCount = saveCount;
128 fOp = op;
129 fDoAA = doAA;
130 // A default of inside-out and empty bounds means the bounds are effectively void as it
131 // indicates that nothing is known to be outside the clip.
132 fFiniteBoundType = kInsideOut_BoundsType;
133 fFiniteBound.setEmpty();
134 fIsIntersectionOfRects = false;
135 fGenID = kInvalidGenID;
136 }
137
138 void initRect(int saveCount, const SkRect& rect, SkRegion::Op op, bool doAA) {
139 fRect = rect;
140 fType = kRect_Type;
141 this->initCommon(saveCount, op, doAA);
142 }
143
144 void initPath(int saveCount, const SkPath& path, SkRegion::Op op, bool doAA) {
145 fPath = path;
146 fType = kPath_Type;
147 this->initCommon(saveCount, op, doAA);
148 }
149
150 void setEmpty() {
151 fType = kEmpty_Type;
152 fFiniteBound.setEmpty();
153 fFiniteBoundType = kNormal_BoundsType;
154 fIsIntersectionOfRects = false;
155 fRect.setEmpty();
156 fPath.reset();
157 fGenID = kEmptyGenID;
158 }
159
160 // All Element methods below are only used within SkClipStack.cpp
161 inline void checkEmpty() const;
162 inline bool operator==(const Element& b) const;
163 inline bool operator!=(const Element& b) const;
164 inline bool canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const;
165 /* This method checks to see if two rect clips can be safely merged into one. The issue here
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000166 is that to be strictly correct all the edges of the resulting rect must have the same
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000167 anti-aliasing. */
168 bool rectRectIntersectAllowed(const SkRect& newR, bool newAA) const;
169 /** Determines possible finite bounds for the Element given the previous element of the
170 stack */
171 void updateBoundAndGenID(const Element* prior);
172 // The different combination of fill & inverse fill when combining bounding boxes
173 enum FillCombo {
174 kPrev_Cur_FillCombo,
175 kPrev_InvCur_FillCombo,
176 kInvPrev_Cur_FillCombo,
177 kInvPrev_InvCur_FillCombo
178 };
179 // per-set operation functions used by updateBoundAndGenID().
180 inline void combineBoundsDiff(FillCombo combination, const SkRect& prevFinite);
181 inline void combineBoundsXOR(int combination, const SkRect& prevFinite);
182 inline void combineBoundsUnion(int combination, const SkRect& prevFinite);
183 inline void combineBoundsIntersection(int combination, const SkRect& prevFinite);
184 inline void combineBoundsRevDiff(int combination, const SkRect& prevFinite);
185 };
186
reed@google.com5c3d1472011-02-22 19:12:23 +0000187 SkClipStack();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000188 SkClipStack(const SkClipStack& b);
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000189 explicit SkClipStack(const SkRect& r);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000190 explicit SkClipStack(const SkIRect& r);
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000191 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +0000192
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000193 SkClipStack& operator=(const SkClipStack& b);
194 bool operator==(const SkClipStack& b) const;
195 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
196
reed@google.com5c3d1472011-02-22 19:12:23 +0000197 void reset();
198
199 int getSaveCount() const { return fSaveCount; }
200 void save();
201 void restore();
202
robertphillips@google.com607fe072012-07-24 13:54:00 +0000203 /**
204 * getBounds places the current finite bound in its first parameter. In its
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000205 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com7b112892012-07-31 15:18:21 +0000206 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000207 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
robertphillips@google.com607fe072012-07-24 13:54:00 +0000208 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000209 * infinite plane. isIntersectionOfRects is an optional parameter
robertphillips@google.com7b112892012-07-31 15:18:21 +0000210 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000211 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000212 void getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000213 BoundsType* boundType,
214 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000215
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000216 /**
217 * Takes an input rect in device space and conservatively clips it to the
218 * clip-stack. If false is returned then the rect does not intersect the
219 * clip and is unmodified.
220 */
221 bool intersectRectWithClip(SkRect* devRect) const;
222
reed@google.com115d9312012-05-16 18:50:40 +0000223 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000224 SkRect r;
225 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +0000226 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +0000227 }
reed@google.com00177082011-10-12 14:34:30 +0000228 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
229 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com0557d9e2012-08-16 15:59:59 +0000230 // An optimized version of clipDevRect(emptyRect, kIntersect, ...)
231 void clipEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000232
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000233 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000234 * isWideOpen returns true if the clip state corresponds to the infinite
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000235 * plane (i.e., draws are not limited at all)
236 */
237 bool isWideOpen() const;
238
robertphillips@google.com46f93502012-08-07 15:38:08 +0000239 /**
240 * Add a callback function that will be called whenever a clip state
241 * is no longer viable. This will occur whenever restore
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000242 * is called or when a clipDevRect or clipDevPath call updates the
robertphillips@google.com46f93502012-08-07 15:38:08 +0000243 * clip within an existing save/restore state. Each clip state is
244 * represented by a unique generation ID.
245 */
246 typedef void (*PFPurgeClipCB)(int genID, void* data);
247 void addPurgeClipCallback(PFPurgeClipCB callback, void* data) const;
248
249 /**
250 * Remove a callback added earlier via addPurgeClipCallback
251 */
252 void removePurgeClipCallback(PFPurgeClipCB callback, void* data) const;
253
254 /**
255 * The generation ID has three reserved values to indicate special
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000256 * (potentially ignorable) cases
robertphillips@google.com46f93502012-08-07 15:38:08 +0000257 */
258 static const int32_t kInvalidGenID = 0;
259 static const int32_t kEmptyGenID = 1; // no pixels writeable
260 static const int32_t kWideOpenGenID = 2; // all pixels writeable
261
robertphillips@google.com73e71022012-08-09 18:10:49 +0000262 int32_t getTopmostGenID() const;
263
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000264public:
265 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +0000266 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000267 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000268 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
269 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000270 };
271
bsalomon@google.comd302f142011-03-03 13:54:13 +0000272 /**
273 * Creates an uninitialized iterator. Must be reset()
274 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000275 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000276
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000277 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000278
279 struct Clip {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000280 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
bsalomon@google.com51a62862012-11-26 21:19:43 +0000281 fDoAA(false), fGenID(kInvalidGenID) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000282 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000283 friend bool operator!=(const Clip& a, const Clip& b);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000284 /**
bsalomon@google.com51a62862012-11-26 21:19:43 +0000285 * Gets the bounds of the clip element, either the rect or path bounds. (Whether the
286 * shape is inverse filled is not considered)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000287 */
288 const SkRect& getBounds() const;
289
bsalomon@google.com51a62862012-11-26 21:19:43 +0000290 /**
291 * Conservatively checks whether the clip shape (rect/path) contains the rect param.
292 * (Whether the shape is inverse filled is not considered)
293 */
294 bool contains(const SkRect&) const;
295
296 /**
297 * Is the clip shape inverse filled.
298 */
299 bool isInverseFilled() const;
300
reed@google.com5c3d1472011-02-22 19:12:23 +0000301 const SkRect* fRect; // if non-null, this is a rect clip
302 const SkPath* fPath; // if non-null, this is a path clip
303 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +0000304 bool fDoAA;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000305 int32_t fGenID;
reed@google.com5c3d1472011-02-22 19:12:23 +0000306 };
307
308 /**
309 * Return the clip for this element in the iterator. If next() returns
310 * NULL, then the iterator is done. The type of clip is determined by
311 * the pointers fRect and fPath:
312 *
313 * fRect==NULL fPath!=NULL path clip
314 * fRect!=NULL fPath==NULL rect clip
315 * fRect==NULL fPath==NULL empty clip
316 */
317 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000318 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000319
bsalomon@google.comd302f142011-03-03 13:54:13 +0000320 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321 * Moves the iterator to the topmost clip with the specified RegionOp
322 * and returns that clip. If no clip with that op is found,
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000323 * returns NULL.
324 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000325 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000326
327 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000328 * Restarts the iterator on a clip stack.
329 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000330 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000331
reed@google.com5c3d1472011-02-22 19:12:23 +0000332 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000333 const SkClipStack* fStack;
334 Clip fClip;
335 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000336 /**
337 * updateClip updates fClip to the current state of fIter. It unifies
338 * functionality needed by both next() and prev().
339 */
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000340 const Clip* updateClip(const SkClipStack::Element* element);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000341 };
342
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000344 * The B2TIter iterates from the bottom of the stack to the top.
345 * It inherits privately from Iter to prevent access to reverse iteration.
346 */
347 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000348 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000349 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000350
351 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000352 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000353 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000354 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000355 B2TIter(const SkClipStack& stack)
robertphillips@google.com80214e22012-07-20 15:33:18 +0000356 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000357 }
358
359 using Iter::Clip;
360 using Iter::next;
361
362 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000363 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000364 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000365 */
366 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000367 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000368 }
369
370 private:
371
372 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000373 };
374
robertphillips@google.com607fe072012-07-24 13:54:00 +0000375 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000376 * GetConservativeBounds returns a conservative bound of the current clip.
377 * Since this could be the infinite plane (if inverse fills were involved) the
378 * maxWidth and maxHeight parameters can be used to limit the returned bound
robertphillips@google.com607fe072012-07-24 13:54:00 +0000379 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000380 * allow the caller to offset the returned bound to account for translated
robertphillips@google.com607fe072012-07-24 13:54:00 +0000381 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000382 * the translation (+offsetX, +offsetY) is applied before the clamp to the
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000384 * isIntersectionOfRects is an optional parameter that is true when
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000385 * 'devBounds' is the result of an intersection of rects. In this case
386 * 'devBounds' is the exact answer/clip.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387 */
388 void getConservativeBounds(int offsetX,
389 int offsetY,
390 int maxWidth,
391 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000393 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394
reed@google.com5c3d1472011-02-22 19:12:23 +0000395private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000396 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000397
398 SkDeque fDeque;
399 int fSaveCount;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000400
401 // Generation ID for the clip stack. This is incremented for each
402 // clipDevRect and clipDevPath call. 0 is reserved to indicate an
403 // invalid ID.
404 static int32_t gGenID;
405
406 struct ClipCallbackData {
407 PFPurgeClipCB fCallback;
408 void* fData;
409
410 friend bool operator==(const ClipCallbackData& a,
411 const ClipCallbackData& b) {
412 return a.fCallback == b.fCallback && a.fData == b.fData;
413 }
414 };
415
416 mutable SkTDArray<ClipCallbackData> fCallbackData;
417
418 /**
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000419 * Invoke all the purge callbacks passing in element's generation ID.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000420 */
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000421 void purgeClip(Element* element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000422
423 /**
424 * Return the next unique generation ID.
425 */
426 static int32_t GetNextGenID();
reed@google.com5c3d1472011-02-22 19:12:23 +0000427};
428
429#endif
430