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