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