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" |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
| 13 | #include "SkRect.h" |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 14 | #include "SkRRect.h" |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 16 | #include "SkTDArray.h" |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 17 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 19 | // Because a single save/restore state can have multiple clips, this class |
| 20 | // stores the stack depth (fSaveCount) and clips (fDeque) separately. |
| 21 | // Each clip in fDeque stores the stack state to which it belongs |
| 22 | // (i.e., the fSaveCount in force when it was added). Restores are thus |
| 23 | // implemented by removing clips from fDeque that have an fSaveCount larger |
| 24 | // then the freshly decremented count. |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 25 | class SK_API SkClipStack { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 26 | public: |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 27 | enum BoundsType { |
| 28 | // The bounding box contains all the pixels that can be written to |
| 29 | kNormal_BoundsType, |
| 30 | // The bounding box contains all the pixels that cannot be written to. |
| 31 | // The real bound extends out to infinity and all the pixels outside |
| 32 | // of the bound can be written to. Note that some of the pixels inside |
| 33 | // the bound may also be writeable but all pixels that cannot be |
| 34 | // written to are guaranteed to be inside. |
| 35 | kInsideOut_BoundsType |
| 36 | }; |
| 37 | |
| 38 | class Element { |
| 39 | public: |
| 40 | enum Type { |
| 41 | //!< This element makes the clip empty (regardless of previous elements). |
| 42 | kEmpty_Type, |
| 43 | //!< This element combines a rect with the current clip using a set operation |
| 44 | kRect_Type, |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 45 | //!< This element combines a round-rect with the current clip using a set operation |
| 46 | kRRect_Type, |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 47 | //!< This element combines a path with the current clip using a set operation |
| 48 | kPath_Type, |
| 49 | }; |
| 50 | |
| 51 | Element() { |
| 52 | this->initCommon(0, SkRegion::kReplace_Op, false); |
| 53 | this->setEmpty(); |
| 54 | } |
| 55 | |
| 56 | Element(const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 57 | this->initRect(0, rect, op, doAA); |
| 58 | } |
| 59 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 60 | Element(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 61 | this->initRRect(0, rrect, op, doAA); |
| 62 | } |
| 63 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 64 | Element(const SkPath& path, SkRegion::Op op, bool doAA) { |
| 65 | this->initPath(0, path, op, doAA); |
| 66 | } |
| 67 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 68 | bool operator== (const Element& element) const; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 69 | bool operator!= (const Element& element) const { return !(*this == element); } |
| 70 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 71 | //!< Call to get the type of the clip element. |
| 72 | Type getType() const { return fType; } |
| 73 | |
| 74 | //!< Call if getType() is kPath to get the path. |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 75 | const SkPath& getPath() const { SkASSERT(kPath_Type == fType); return fPath; } |
| 76 | |
| 77 | //!< Call if getType() is kRRect to get the round-rect. |
| 78 | const SkRRect& getRRect() const { SkASSERT(kRRect_Type == fType); return fRRect; } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 79 | |
| 80 | //!< Call if getType() is kRect to get the rect. |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 81 | const SkRect& getRect() const { SkASSERT(kRect_Type == fType); return fRect; } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 82 | |
| 83 | //!< Call if getType() is not kEmpty to get the set operation used to combine this element. |
| 84 | SkRegion::Op getOp() const { return fOp; } |
| 85 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 86 | //!< Call to get the element as a path, regardless of its type. |
| 87 | void asPath(SkPath* path) const; |
| 88 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 89 | /** If getType() is not kEmpty this indicates whether the clip shape should be anti-aliased |
| 90 | when it is rasterized. */ |
| 91 | bool isAA() const { return fDoAA; } |
| 92 | |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 93 | //!< Inverts the fill of the clip shape. Note that a kEmpty element remains kEmpty. |
| 94 | void invertShapeFillType(); |
| 95 | |
| 96 | //!< Sets the set operation represented by the element. |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 97 | void setOp(SkRegion::Op op) { fOp = op; } |
| 98 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 99 | /** The GenID can be used by clip stack clients to cache representations of the clip. The |
| 100 | ID corresponds to the set of clip elements up to and including this element within the |
| 101 | stack not to the element itself. That is the same clip path in different stacks will |
| 102 | have a different ID since the elements produce different clip result in the context of |
| 103 | their stacks. */ |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 104 | int32_t getGenID() const { SkASSERT(kInvalidGenID != fGenID); return fGenID; } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 106 | /** |
| 107 | * Gets the bounds of the clip element, either the rect or path bounds. (Whether the shape |
| 108 | * is inverse filled is not considered.) |
| 109 | */ |
| 110 | const SkRect& getBounds() const { |
| 111 | static const SkRect kEmpty = { 0, 0, 0, 0 }; |
| 112 | switch (fType) { |
| 113 | case kRect_Type: |
| 114 | return fRect; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 115 | case kRRect_Type: |
| 116 | return fRRect.getBounds(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 117 | case kPath_Type: |
| 118 | return fPath.getBounds(); |
| 119 | case kEmpty_Type: |
| 120 | return kEmpty; |
| 121 | default: |
| 122 | SkDEBUGFAIL("Unexpected type."); |
| 123 | return kEmpty; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Conservatively checks whether the clip shape contains the rect param. (Whether the shape |
| 129 | * is inverse filled is not considered.) |
| 130 | */ |
| 131 | bool contains(const SkRect& rect) const { |
| 132 | switch (fType) { |
| 133 | case kRect_Type: |
| 134 | return fRect.contains(rect); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 135 | case kRRect_Type: |
| 136 | return fRRect.contains(rect); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 137 | case kPath_Type: |
| 138 | return fPath.conservativelyContainsRect(rect); |
| 139 | case kEmpty_Type: |
| 140 | return false; |
| 141 | default: |
| 142 | SkDEBUGFAIL("Unexpected type."); |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Is the clip shape inverse filled. |
| 149 | */ |
| 150 | bool isInverseFilled() const { |
| 151 | return kPath_Type == fType && fPath.isInverseFillType(); |
| 152 | } |
| 153 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 154 | private: |
| 155 | friend class SkClipStack; |
| 156 | |
| 157 | SkPath fPath; |
| 158 | SkRect fRect; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 159 | SkRRect fRRect; |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 160 | int fSaveCount; // save count of stack when this element was added. |
| 161 | SkRegion::Op fOp; |
| 162 | Type fType; |
| 163 | bool fDoAA; |
| 164 | |
| 165 | /* fFiniteBoundType and fFiniteBound are used to incrementally update the clip stack's |
| 166 | bound. When fFiniteBoundType is kNormal_BoundsType, fFiniteBound represents the |
| 167 | conservative bounding box of the pixels that aren't clipped (i.e., any pixels that can be |
| 168 | drawn to are inside the bound). When fFiniteBoundType is kInsideOut_BoundsType (which |
| 169 | occurs when a clip is inverse filled), fFiniteBound represents the conservative bounding |
| 170 | box of the pixels that _are_ clipped (i.e., any pixels that cannot be drawn to are inside |
| 171 | the bound). When fFiniteBoundType is kInsideOut_BoundsType the actual bound is the |
| 172 | infinite plane. This behavior of fFiniteBoundType and fFiniteBound is required so that we |
| 173 | can capture the cancelling out of the extensions to infinity when two inverse filled |
| 174 | clips are Booleaned together. */ |
| 175 | SkClipStack::BoundsType fFiniteBoundType; |
| 176 | SkRect fFiniteBound; |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 178 | // When element is applied to the previous elements in the stack is the result known to be |
| 179 | // equivalent to a single rect intersection? IIOW, is the clip effectively a rectangle. |
| 180 | bool fIsIntersectionOfRects; |
| 181 | |
| 182 | int fGenID; |
| 183 | |
| 184 | Element(int saveCount) { |
| 185 | this->initCommon(saveCount, SkRegion::kReplace_Op, false); |
| 186 | this->setEmpty(); |
| 187 | } |
| 188 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 189 | Element(int saveCount, const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 190 | this->initRRect(saveCount, rrect, op, doAA); |
| 191 | } |
| 192 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 193 | Element(int saveCount, const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 194 | this->initRect(saveCount, rect, op, doAA); |
| 195 | } |
| 196 | |
| 197 | Element(int saveCount, const SkPath& path, SkRegion::Op op, bool doAA) { |
| 198 | this->initPath(saveCount, path, op, doAA); |
| 199 | } |
| 200 | |
| 201 | void initCommon(int saveCount, SkRegion::Op op, bool doAA) { |
| 202 | fSaveCount = saveCount; |
| 203 | fOp = op; |
| 204 | fDoAA = doAA; |
| 205 | // A default of inside-out and empty bounds means the bounds are effectively void as it |
| 206 | // indicates that nothing is known to be outside the clip. |
| 207 | fFiniteBoundType = kInsideOut_BoundsType; |
| 208 | fFiniteBound.setEmpty(); |
| 209 | fIsIntersectionOfRects = false; |
| 210 | fGenID = kInvalidGenID; |
| 211 | } |
| 212 | |
| 213 | void initRect(int saveCount, const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 214 | fRect = rect; |
| 215 | fType = kRect_Type; |
| 216 | this->initCommon(saveCount, op, doAA); |
| 217 | } |
| 218 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 219 | void initRRect(int saveCount, const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 220 | if (rrect.isRect()) { |
| 221 | fRect = rrect.getBounds(); |
| 222 | fType = kRect_Type; |
| 223 | } else { |
| 224 | fRRect = rrect; |
| 225 | fType = kRRect_Type; |
| 226 | } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 227 | this->initCommon(saveCount, op, doAA); |
| 228 | } |
| 229 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 230 | void initPath(int saveCount, const SkPath& path, SkRegion::Op op, bool doAA); |
| 231 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 232 | void setEmpty() { |
| 233 | fType = kEmpty_Type; |
| 234 | fFiniteBound.setEmpty(); |
| 235 | fFiniteBoundType = kNormal_BoundsType; |
| 236 | fIsIntersectionOfRects = false; |
| 237 | fRect.setEmpty(); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 238 | fRRect.setEmpty(); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 239 | fPath.reset(); |
| 240 | fGenID = kEmptyGenID; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 241 | SkDEBUGCODE(this->checkEmpty();) |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // All Element methods below are only used within SkClipStack.cpp |
| 245 | inline void checkEmpty() const; |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 246 | inline bool canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const; |
| 247 | /* This method checks to see if two rect clips can be safely merged into one. The issue here |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 248 | is that to be strictly correct all the edges of the resulting rect must have the same |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 249 | anti-aliasing. */ |
| 250 | bool rectRectIntersectAllowed(const SkRect& newR, bool newAA) const; |
| 251 | /** Determines possible finite bounds for the Element given the previous element of the |
| 252 | stack */ |
| 253 | void updateBoundAndGenID(const Element* prior); |
| 254 | // The different combination of fill & inverse fill when combining bounding boxes |
| 255 | enum FillCombo { |
| 256 | kPrev_Cur_FillCombo, |
| 257 | kPrev_InvCur_FillCombo, |
| 258 | kInvPrev_Cur_FillCombo, |
| 259 | kInvPrev_InvCur_FillCombo |
| 260 | }; |
| 261 | // per-set operation functions used by updateBoundAndGenID(). |
| 262 | inline void combineBoundsDiff(FillCombo combination, const SkRect& prevFinite); |
| 263 | inline void combineBoundsXOR(int combination, const SkRect& prevFinite); |
| 264 | inline void combineBoundsUnion(int combination, const SkRect& prevFinite); |
| 265 | inline void combineBoundsIntersection(int combination, const SkRect& prevFinite); |
| 266 | inline void combineBoundsRevDiff(int combination, const SkRect& prevFinite); |
| 267 | }; |
| 268 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 269 | SkClipStack(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 270 | SkClipStack(const SkClipStack& b); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 271 | explicit SkClipStack(const SkRect& r); |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 272 | explicit SkClipStack(const SkIRect& r); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 273 | ~SkClipStack(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 274 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 275 | SkClipStack& operator=(const SkClipStack& b); |
| 276 | bool operator==(const SkClipStack& b) const; |
| 277 | bool operator!=(const SkClipStack& b) const { return !(*this == b); } |
| 278 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 279 | void reset(); |
| 280 | |
| 281 | int getSaveCount() const { return fSaveCount; } |
| 282 | void save(); |
| 283 | void restore(); |
| 284 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 285 | /** |
| 286 | * 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] | 287 | * second, it indicates which kind of bound is being returned. If |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 288 | * 'canvFiniteBound' is a normal bounding box then it encloses all writeable |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 289 | * pixels. If 'canvFiniteBound' is an inside out bounding box then it |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 290 | * 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] | 291 | * infinite plane. isIntersectionOfRects is an optional parameter |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 292 | * that is true if 'canvFiniteBound' resulted from an intersection of rects. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 293 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 294 | void getBounds(SkRect* canvFiniteBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 295 | BoundsType* boundType, |
| 296 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 297 | |
bsalomon@google.com | 3ab43d5 | 2012-10-11 19:39:09 +0000 | [diff] [blame] | 298 | /** |
| 299 | * Takes an input rect in device space and conservatively clips it to the |
| 300 | * clip-stack. If false is returned then the rect does not intersect the |
| 301 | * clip and is unmodified. |
| 302 | */ |
| 303 | bool intersectRectWithClip(SkRect* devRect) const; |
| 304 | |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 305 | /** |
| 306 | * Returns true if the input rect in device space is entirely contained |
| 307 | * by the clip. A return value of false does not guarantee that the rect |
| 308 | * is not contained by the clip. |
| 309 | */ |
| 310 | bool quickContains(const SkRect& devRect) const; |
| 311 | |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 312 | void clipDevRect(const SkIRect& ir, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 313 | SkRect r; |
| 314 | r.set(ir); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 315 | this->clipDevRect(r, op, false); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 316 | } |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 317 | void clipDevRect(const SkRect&, SkRegion::Op, bool doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 318 | void clipDevRRect(const SkRRect&, SkRegion::Op, bool doAA); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 319 | void clipDevPath(const SkPath&, SkRegion::Op, bool doAA); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 320 | // An optimized version of clipDevRect(emptyRect, kIntersect, ...) |
| 321 | void clipEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 322 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 323 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 324 | * isWideOpen returns true if the clip state corresponds to the infinite |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 325 | * plane (i.e., draws are not limited at all) |
| 326 | */ |
| 327 | bool isWideOpen() const; |
| 328 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 329 | /** |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 330 | * The generation ID has three reserved values to indicate special |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame] | 331 | * (potentially ignorable) cases |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 332 | */ |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 333 | static const int32_t kInvalidGenID = 0; //!< Invalid id that is never returned by |
| 334 | //!< SkClipStack. Useful when caching clips |
| 335 | //!< based on GenID. |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 336 | static const int32_t kEmptyGenID = 1; // no pixels writeable |
| 337 | static const int32_t kWideOpenGenID = 2; // all pixels writeable |
| 338 | |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 339 | int32_t getTopmostGenID() const; |
| 340 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 341 | public: |
| 342 | class Iter { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 343 | public: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 344 | enum IterStart { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 345 | kBottom_IterStart = SkDeque::Iter::kFront_IterStart, |
| 346 | kTop_IterStart = SkDeque::Iter::kBack_IterStart |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 349 | /** |
| 350 | * Creates an uninitialized iterator. Must be reset() |
| 351 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 352 | Iter(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 353 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 354 | Iter(const SkClipStack& stack, IterStart startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 355 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 356 | /** |
| 357 | * Return the clip element for this iterator. If next()/prev() returns NULL, then the |
| 358 | * iterator is done. |
| 359 | */ |
| 360 | const Element* next(); |
| 361 | const Element* prev(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 362 | |
| 363 | /** |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 364 | * Moves the iterator to the topmost element with the specified RegionOp and returns that |
| 365 | * element. If no clip element with that op is found, the first element is returned. |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 366 | */ |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 367 | const Element* skipToTopmost(SkRegion::Op op); |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 368 | |
| 369 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 370 | * Restarts the iterator on a clip stack. |
| 371 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 372 | void reset(const SkClipStack& stack, IterStart startLoc); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 373 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 374 | private: |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 375 | const SkClipStack* fStack; |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 376 | SkDeque::Iter fIter; |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 379 | /** |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 380 | * The B2TIter iterates from the bottom of the stack to the top. |
| 381 | * It inherits privately from Iter to prevent access to reverse iteration. |
| 382 | */ |
| 383 | class B2TIter : private Iter { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 384 | public: |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 385 | B2TIter() {} |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 386 | |
| 387 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 388 | * Wrap Iter's 2 parameter ctor to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 389 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 390 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 391 | B2TIter(const SkClipStack& stack) |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 392 | : INHERITED(stack, kBottom_IterStart) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 393 | } |
| 394 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 395 | using Iter::next; |
| 396 | |
| 397 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 398 | * Wrap Iter::reset to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 399 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 400 | */ |
| 401 | void reset(const SkClipStack& stack) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 402 | this->INHERITED::reset(stack, kBottom_IterStart); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | private: |
| 406 | |
| 407 | typedef Iter INHERITED; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 410 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 411 | * GetConservativeBounds returns a conservative bound of the current clip. |
| 412 | * Since this could be the infinite plane (if inverse fills were involved) the |
| 413 | * 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] | 414 | * to the expected drawing area. Similarly, the offsetX and offsetY parameters |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 415 | * 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] | 416 | * 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] | 417 | * the translation (+offsetX, +offsetY) is applied before the clamp to the |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 418 | * maximum rectangle: [0,maxWidth) x [0,maxHeight). |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 419 | * isIntersectionOfRects is an optional parameter that is true when |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 420 | * 'devBounds' is the result of an intersection of rects. In this case |
| 421 | * 'devBounds' is the exact answer/clip. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 422 | */ |
| 423 | void getConservativeBounds(int offsetX, |
| 424 | int offsetY, |
| 425 | int maxWidth, |
| 426 | int maxHeight, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 427 | SkRect* devBounds, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 428 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 429 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 430 | private: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 431 | friend class Iter; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 432 | |
| 433 | SkDeque fDeque; |
| 434 | int fSaveCount; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 435 | |
| 436 | // Generation ID for the clip stack. This is incremented for each |
| 437 | // clipDevRect and clipDevPath call. 0 is reserved to indicate an |
| 438 | // invalid ID. |
| 439 | static int32_t gGenID; |
| 440 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 441 | /** |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame^] | 442 | * Helper for clipDevPath, etc. |
| 443 | */ |
| 444 | void pushElement(const Element& element); |
| 445 | |
| 446 | /** |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 447 | * Restore the stack back to the specified save count. |
| 448 | */ |
| 449 | void restoreTo(int saveCount); |
| 450 | |
| 451 | /** |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 452 | * Return the next unique generation ID. |
| 453 | */ |
| 454 | static int32_t GetNextGenID(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | #endif |