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