epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 8 | #ifndef SkClipStack_DEFINED |
| 9 | #define SkClipStack_DEFINED |
| 10 | |
| 11 | #include "SkDeque.h" |
| 12 | #include "SkRegion.h" |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | 57788b5 | 2011-02-22 21:00:31 +0000 | [diff] [blame] | 15 | struct SkRect; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 16 | class SkPath; |
| 17 | |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 18 | // 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.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 24 | class SK_API SkClipStack { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 25 | public: |
| 26 | SkClipStack(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 27 | SkClipStack(const SkClipStack& b); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 28 | explicit SkClipStack(const SkRect& r); |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 29 | explicit SkClipStack(const SkIRect& r); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 30 | ~SkClipStack(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 31 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 32 | SkClipStack& operator=(const SkClipStack& b); |
| 33 | bool operator==(const SkClipStack& b) const; |
| 34 | bool operator!=(const SkClipStack& b) const { return !(*this == b); } |
| 35 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 36 | void reset(); |
| 37 | |
| 38 | int getSaveCount() const { return fSaveCount; } |
| 39 | void save(); |
| 40 | void restore(); |
| 41 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 42 | enum BoundsType { |
| 43 | // The bounding box contains all the pixels that can be written to |
| 44 | kNormal_BoundsType, |
| 45 | // The bounding box contains all the pixels that cannot be written to. |
| 46 | // The real bound extends out to infinity and all the pixels outside |
| 47 | // of the bound can be written to. Note that some of the pixels inside |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | // the bound may also be writeable but all pixels that cannot be |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 49 | // written to are guaranteed to be inside. |
| 50 | kInsideOut_BoundsType |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * getBounds places the current finite bound in its first parameter. In its |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 55 | * second, it indicates which kind of bound is being returned. If |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 56 | * 'canvFiniteBound' is a normal bounding box then it encloses all writeable |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 57 | * pixels. If 'canvFiniteBound' is an inside out bounding box then it |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 58 | * encloses all the un-writeable pixels and the true/normal bound is the |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 59 | * infinite plane. isIntersectionOfRects is an optional parameter |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 60 | * that is true if 'canvFiniteBound' resulted from an intersection of rects. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 61 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 62 | void getBounds(SkRect* canvFiniteBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 63 | BoundsType* boundType, |
| 64 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 3ab43d5 | 2012-10-11 19:39:09 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Takes an input rect in device space and conservatively clips it to the |
| 68 | * clip-stack. If false is returned then the rect does not intersect the |
| 69 | * clip and is unmodified. |
| 70 | */ |
| 71 | bool intersectRectWithClip(SkRect* devRect) const; |
| 72 | |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 73 | void clipDevRect(const SkIRect& ir, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 74 | SkRect r; |
| 75 | r.set(ir); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 76 | this->clipDevRect(r, op, false); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 77 | } |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 78 | void clipDevRect(const SkRect&, SkRegion::Op, bool doAA); |
| 79 | void clipDevPath(const SkPath&, SkRegion::Op, bool doAA); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 80 | // An optimized version of clipDevRect(emptyRect, kIntersect, ...) |
| 81 | void clipEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 82 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 83 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | * isWideOpen returns true if the clip state corresponds to the infinite |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 85 | * plane (i.e., draws are not limited at all) |
| 86 | */ |
| 87 | bool isWideOpen() const; |
| 88 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Add a callback function that will be called whenever a clip state |
| 91 | * is no longer viable. This will occur whenever restore |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 92 | * is called or when a clipDevRect or clipDevPath call updates the |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 93 | * clip within an existing save/restore state. Each clip state is |
| 94 | * represented by a unique generation ID. |
| 95 | */ |
| 96 | typedef void (*PFPurgeClipCB)(int genID, void* data); |
| 97 | void addPurgeClipCallback(PFPurgeClipCB callback, void* data) const; |
| 98 | |
| 99 | /** |
| 100 | * Remove a callback added earlier via addPurgeClipCallback |
| 101 | */ |
| 102 | void removePurgeClipCallback(PFPurgeClipCB callback, void* data) const; |
| 103 | |
| 104 | /** |
| 105 | * The generation ID has three reserved values to indicate special |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame^] | 106 | * (potentially ignorable) cases |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 107 | */ |
| 108 | static const int32_t kInvalidGenID = 0; |
| 109 | static const int32_t kEmptyGenID = 1; // no pixels writeable |
| 110 | static const int32_t kWideOpenGenID = 2; // all pixels writeable |
| 111 | |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 112 | int32_t getTopmostGenID() const; |
| 113 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 114 | private: |
| 115 | struct Rec; |
| 116 | |
| 117 | public: |
| 118 | class Iter { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 119 | public: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 120 | enum IterStart { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 121 | kBottom_IterStart = SkDeque::Iter::kFront_IterStart, |
| 122 | kTop_IterStart = SkDeque::Iter::kBack_IterStart |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Creates an uninitialized iterator. Must be reset() |
| 127 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 128 | Iter(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 129 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 130 | Iter(const SkClipStack& stack, IterStart startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 131 | |
| 132 | struct Clip { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op), |
robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 134 | fDoAA(false) {} |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 135 | friend bool operator==(const Clip& a, const Clip& b); |
vandebo@chromium.org | 8887ede | 2011-05-25 01:27:52 +0000 | [diff] [blame] | 136 | friend bool operator!=(const Clip& a, const Clip& b); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 137 | /** |
| 138 | * Gets the bounds of the clip element, either the rect or path bounds. |
| 139 | */ |
| 140 | const SkRect& getBounds() const; |
| 141 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 142 | const SkRect* fRect; // if non-null, this is a rect clip |
| 143 | const SkPath* fPath; // if non-null, this is a path clip |
| 144 | SkRegion::Op fOp; |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 145 | bool fDoAA; |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 146 | int32_t fGenID; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Return the clip for this element in the iterator. If next() returns |
| 151 | * NULL, then the iterator is done. The type of clip is determined by |
| 152 | * the pointers fRect and fPath: |
| 153 | * |
| 154 | * fRect==NULL fPath!=NULL path clip |
| 155 | * fRect!=NULL fPath==NULL rect clip |
| 156 | * fRect==NULL fPath==NULL empty clip |
| 157 | */ |
| 158 | const Clip* next(); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 159 | const Clip* prev(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 161 | /** |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame^] | 162 | * This is a variant of next() that greedily attempts to combine elements when possible. |
| 163 | * Currently it attempts to combine intersecting rectangles, though it may do more in the |
| 164 | * future. The returned Clip may not refer to a single element in the stack, so its |
| 165 | * generation ID may be invalid. |
| 166 | */ |
| 167 | const Clip* nextCombined(); |
| 168 | |
| 169 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 170 | * Moves the iterator to the topmost clip with the specified RegionOp |
| 171 | * and returns that clip. If no clip with that op is found, |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 172 | * returns NULL. |
| 173 | */ |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 174 | const Clip* skipToTopmost(SkRegion::Op op); |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 175 | |
| 176 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 177 | * Restarts the iterator on a clip stack. |
| 178 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 179 | void reset(const SkClipStack& stack, IterStart startLoc); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 180 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 181 | private: |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 182 | const SkClipStack* fStack; |
| 183 | Clip fClip; |
| 184 | SkDeque::Iter fIter; |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame^] | 185 | SkRect fCombinedRect; // used for nextCombined() |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 186 | /** |
| 187 | * updateClip updates fClip to the current state of fIter. It unifies |
| 188 | * functionality needed by both next() and prev(). |
| 189 | */ |
| 190 | const Clip* updateClip(const SkClipStack::Rec* rec); |
| 191 | }; |
| 192 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 193 | /** |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 194 | * The B2TIter iterates from the bottom of the stack to the top. |
| 195 | * It inherits privately from Iter to prevent access to reverse iteration. |
| 196 | */ |
| 197 | class B2TIter : private Iter { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 198 | public: |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 199 | B2TIter() {} |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 200 | |
| 201 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 202 | * Wrap Iter's 2 parameter ctor to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 203 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 204 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 205 | B2TIter(const SkClipStack& stack) |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 206 | : INHERITED(stack, kBottom_IterStart) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | using Iter::Clip; |
| 210 | using Iter::next; |
| 211 | |
| 212 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 213 | * Wrap Iter::reset to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 214 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 215 | */ |
| 216 | void reset(const SkClipStack& stack) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 217 | this->INHERITED::reset(stack, kBottom_IterStart); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private: |
| 221 | |
| 222 | typedef Iter INHERITED; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 225 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 226 | * GetConservativeBounds returns a conservative bound of the current clip. |
| 227 | * Since this could be the infinite plane (if inverse fills were involved) the |
| 228 | * maxWidth and maxHeight parameters can be used to limit the returned bound |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 229 | * to the expected drawing area. Similarly, the offsetX and offsetY parameters |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 230 | * allow the caller to offset the returned bound to account for translated |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 231 | * drawing areas (i.e., those resulting from a saveLayer). For finite bounds, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 232 | * the translation (+offsetX, +offsetY) is applied before the clamp to the |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 233 | * maximum rectangle: [0,maxWidth) x [0,maxHeight). |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 234 | * isIntersectionOfRects is an optional parameter that is true when |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 235 | * 'devBounds' is the result of an intersection of rects. In this case |
| 236 | * 'devBounds' is the exact answer/clip. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 237 | */ |
| 238 | void getConservativeBounds(int offsetX, |
| 239 | int offsetY, |
| 240 | int maxWidth, |
| 241 | int maxHeight, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 242 | SkRect* devBounds, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 243 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 244 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 245 | private: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 246 | friend class Iter; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 247 | |
| 248 | SkDeque fDeque; |
| 249 | int fSaveCount; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 250 | |
| 251 | // Generation ID for the clip stack. This is incremented for each |
| 252 | // clipDevRect and clipDevPath call. 0 is reserved to indicate an |
| 253 | // invalid ID. |
| 254 | static int32_t gGenID; |
| 255 | |
| 256 | struct ClipCallbackData { |
| 257 | PFPurgeClipCB fCallback; |
| 258 | void* fData; |
| 259 | |
| 260 | friend bool operator==(const ClipCallbackData& a, |
| 261 | const ClipCallbackData& b) { |
| 262 | return a.fCallback == b.fCallback && a.fData == b.fData; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | mutable SkTDArray<ClipCallbackData> fCallbackData; |
| 267 | |
| 268 | /** |
| 269 | * Invoke all the purge callbacks passing in rec's generation ID. |
| 270 | */ |
| 271 | void purgeClip(Rec* rec); |
| 272 | |
| 273 | /** |
| 274 | * Return the next unique generation ID. |
| 275 | */ |
| 276 | static int32_t GetNextGenID(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | #endif |
| 280 | |