blob: 87288d8ead9afa96a0615e3d7b5252cbb63a4402 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed73603f32016-09-20 08:42:38 -07007
reed@google.com5c3d1472011-02-22 19:12:23 +00008#ifndef SkClipStack_DEFINED
9#define SkClipStack_DEFINED
10
Brian Salomon19f0ed52017-01-06 13:54:58 -050011#include "../private/SkMessageBus.h"
reed73603f32016-09-20 08:42:38 -070012#include "SkCanvas.h"
Mike Reed14113bc2017-05-10 14:13:20 -040013#include "SkClipOpPriv.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000014#include "SkDeque.h"
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000015#include "SkPath.h"
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000016#include "SkRRect.h"
Brian Salomon19f0ed52017-01-06 13:54:58 -050017#include "SkRect.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000018#include "SkRegion.h"
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000019#include "SkTLazy.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000020
Brian Salomon19f0ed52017-01-06 13:54:58 -050021#if SK_SUPPORT_GPU
Robert Phillips427966a2018-12-20 17:20:43 -050022class GrProxyProvider;
23
Brian Salomon19f0ed52017-01-06 13:54:58 -050024#include "GrResourceKey.h"
25#endif
26
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000027// Because a single save/restore state can have multiple clips, this class
28// stores the stack depth (fSaveCount) and clips (fDeque) separately.
29// Each clip in fDeque stores the stack state to which it belongs
30// (i.e., the fSaveCount in force when it was added). Restores are thus
31// implemented by removing clips from fDeque that have an fSaveCount larger
32// then the freshly decremented count.
Mike Reed7ba4d712017-03-10 00:21:52 -050033class SkClipStack {
reed@google.com5c3d1472011-02-22 19:12:23 +000034public:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000035 enum BoundsType {
36 // The bounding box contains all the pixels that can be written to
37 kNormal_BoundsType,
38 // The bounding box contains all the pixels that cannot be written to.
39 // The real bound extends out to infinity and all the pixels outside
40 // of the bound can be written to. Note that some of the pixels inside
41 // the bound may also be writeable but all pixels that cannot be
42 // written to are guaranteed to be inside.
43 kInsideOut_BoundsType
44 };
45
Brian Salomonf3b46e52017-08-30 11:37:57 -040046 /**
47 * An element of the clip stack. It represents a shape combined with the prevoius clip using a
48 * set operator. Each element can be antialiased or not.
49 */
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000050 class Element {
51 public:
Brian Salomonf3b46e52017-08-30 11:37:57 -040052 /** This indicates the shape type of the clip element in device space. */
53 enum class DeviceSpaceType {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000054 //!< This element makes the clip empty (regardless of previous elements).
Brian Salomonf3b46e52017-08-30 11:37:57 -040055 kEmpty,
56 //!< This element combines a device space rect with the current clip.
57 kRect,
58 //!< This element combines a device space round-rect with the current clip.
59 kRRect,
60 //!< This element combines a device space path with the current clip.
61 kPath,
bsalomonb6b02522014-06-09 07:59:06 -070062
Brian Salomonf3b46e52017-08-30 11:37:57 -040063 kLastType = kPath
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000064 };
Brian Salomonf3b46e52017-08-30 11:37:57 -040065 static const int kTypeCnt = (int)DeviceSpaceType::kLastType + 1;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000066
67 Element() {
Mike Reed14113bc2017-05-10 14:13:20 -040068 this->initCommon(0, kReplace_SkClipOp, false);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000069 this->setEmpty();
70 }
71
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000072 Element(const Element&);
73
Brian Salomon2e866342017-08-28 12:38:58 -040074 Element(const SkRect& rect, const SkMatrix& m, SkClipOp op, bool doAA) {
75 this->initRect(0, rect, m, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000076 }
77
Brian Salomon2e866342017-08-28 12:38:58 -040078 Element(const SkRRect& rrect, const SkMatrix& m, SkClipOp op, bool doAA) {
79 this->initRRect(0, rrect, m, op, doAA);
80 }
81
82 Element(const SkPath& path, const SkMatrix& m, SkClipOp op, bool doAA) {
83 this->initPath(0, path, m, op, doAA);
84 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000085
Robert Phillips427966a2018-12-20 17:20:43 -050086 ~Element();
Brian Salomon19f0ed52017-01-06 13:54:58 -050087
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000088 bool operator== (const Element& element) const;
bsalomon@google.com8182fa02012-12-04 14:06:06 +000089 bool operator!= (const Element& element) const { return !(*this == element); }
90
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000091 //!< Call to get the type of the clip element.
Brian Salomonf3b46e52017-08-30 11:37:57 -040092 DeviceSpaceType getDeviceSpaceType() const { return fDeviceSpaceType; }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000093
fmalitac3b589a2014-06-05 12:40:07 -070094 //!< Call to get the save count associated with this clip element.
95 int getSaveCount() const { return fSaveCount; }
96
Brian Salomonf3b46e52017-08-30 11:37:57 -040097 //!< Call if getDeviceSpaceType() is kPath to get the path.
98 const SkPath& getDeviceSpacePath() const {
99 SkASSERT(DeviceSpaceType::kPath == fDeviceSpaceType);
100 return *fDeviceSpacePath.get();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000101 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000102
Brian Salomonf3b46e52017-08-30 11:37:57 -0400103 //!< Call if getDeviceSpaceType() is kRRect to get the round-rect.
104 const SkRRect& getDeviceSpaceRRect() const {
105 SkASSERT(DeviceSpaceType::kRRect == fDeviceSpaceType);
106 return fDeviceSpaceRRect;
107 }
108
109 //!< Call if getDeviceSpaceType() is kRect to get the rect.
110 const SkRect& getDeviceSpaceRect() const {
111 SkASSERT(DeviceSpaceType::kRect == fDeviceSpaceType &&
112 (fDeviceSpaceRRect.isRect() || fDeviceSpaceRRect.isEmpty()));
113 return fDeviceSpaceRRect.getBounds();
114 }
115
116 //!< Call if getDeviceSpaceType() is not kEmpty to get the set operation used to combine
117 //!< this element.
Mike Reedc1f77742016-12-09 09:00:50 -0500118 SkClipOp getOp() const { return fOp; }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000119
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000120 //!< Call to get the element as a path, regardless of its type.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400121 void asDeviceSpacePath(SkPath* path) const;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000122
cdaltonb893a4c2016-03-17 12:56:11 -0700123 //!< Call if getType() is not kPath to get the element as a round rect.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400124 const SkRRect& asDeviceSpaceRRect() const {
125 SkASSERT(DeviceSpaceType::kPath != fDeviceSpaceType);
126 return fDeviceSpaceRRect;
127 }
cdaltonb893a4c2016-03-17 12:56:11 -0700128
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000129 /** If getType() is not kEmpty this indicates whether the clip shape should be anti-aliased
130 when it is rasterized. */
131 bool isAA() const { return fDoAA; }
132
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000133 //!< Inverts the fill of the clip shape. Note that a kEmpty element remains kEmpty.
134 void invertShapeFillType();
135
136 //!< Sets the set operation represented by the element.
Mike Reedc1f77742016-12-09 09:00:50 -0500137 void setOp(SkClipOp op) { fOp = op; }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000138
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000139 /** The GenID can be used by clip stack clients to cache representations of the clip. The
140 ID corresponds to the set of clip elements up to and including this element within the
141 stack not to the element itself. That is the same clip path in different stacks will
142 have a different ID since the elements produce different clip result in the context of
143 their stacks. */
Robert Phillips806be2d2017-06-28 15:23:59 -0400144 uint32_t getGenID() const { SkASSERT(kInvalidGenID != fGenID); return fGenID; }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000145
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000146 /**
147 * Gets the bounds of the clip element, either the rect or path bounds. (Whether the shape
148 * is inverse filled is not considered.)
149 */
Brian Salomon5c727962017-08-21 12:37:36 -0400150 const SkRect& getBounds() const;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000151
152 /**
Brian Salomon5c727962017-08-21 12:37:36 -0400153 * Conservatively checks whether the clip shape contains the rect/rrect. (Whether the shape
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000154 * is inverse filled is not considered.)
155 */
Brian Salomon5c727962017-08-21 12:37:36 -0400156 bool contains(const SkRect& rect) const;
157 bool contains(const SkRRect& rrect) const;
bsalomon7f0d9f32016-08-15 14:49:10 -0700158
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000159 /**
160 * Is the clip shape inverse filled.
161 */
162 bool isInverseFilled() const {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400163 return DeviceSpaceType::kPath == fDeviceSpaceType &&
164 fDeviceSpacePath.get()->isInverseFillType();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000165 }
166
djsollenefe46d22016-04-29 06:41:35 -0700167#ifdef SK_DEBUG
bsalomonb6b02522014-06-09 07:59:06 -0700168 /**
169 * Dumps the element to SkDebugf. This is intended for Skia development debugging
170 * Don't rely on the existence of this function or the formatting of its output.
171 */
172 void dump() const;
173#endif
174
Brian Salomon19f0ed52017-01-06 13:54:58 -0500175#if SK_SUPPORT_GPU
176 /**
177 * This is used to purge any GPU resource cache items that become unreachable when
178 * the element is destroyed because their key is based on this element's gen ID.
179 */
Robert Phillips427966a2018-12-20 17:20:43 -0500180 void addResourceInvalidationMessage(GrProxyProvider* proxyProvider,
181 const GrUniqueKey& key) const {
182 SkASSERT(proxyProvider);
183
184 if (!fProxyProvider) {
185 fProxyProvider = proxyProvider;
186 }
187 SkASSERT(fProxyProvider == proxyProvider);
188
189 fKeysToInvalidate.push_back(key);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500190 }
191#endif
192
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000193 private:
194 friend class SkClipStack;
195
Brian Salomonf3b46e52017-08-30 11:37:57 -0400196 SkTLazy<SkPath> fDeviceSpacePath;
197 SkRRect fDeviceSpaceRRect;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500198 int fSaveCount; // save count of stack when this element was added.
199 SkClipOp fOp;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400200 DeviceSpaceType fDeviceSpaceType;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500201 bool fDoAA;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000202
203 /* fFiniteBoundType and fFiniteBound are used to incrementally update the clip stack's
204 bound. When fFiniteBoundType is kNormal_BoundsType, fFiniteBound represents the
205 conservative bounding box of the pixels that aren't clipped (i.e., any pixels that can be
206 drawn to are inside the bound). When fFiniteBoundType is kInsideOut_BoundsType (which
207 occurs when a clip is inverse filled), fFiniteBound represents the conservative bounding
208 box of the pixels that _are_ clipped (i.e., any pixels that cannot be drawn to are inside
209 the bound). When fFiniteBoundType is kInsideOut_BoundsType the actual bound is the
210 infinite plane. This behavior of fFiniteBoundType and fFiniteBound is required so that we
211 can capture the cancelling out of the extensions to infinity when two inverse filled
212 clips are Booleaned together. */
213 SkClipStack::BoundsType fFiniteBoundType;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500214 SkRect fFiniteBound;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000215
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000216 // When element is applied to the previous elements in the stack is the result known to be
217 // equivalent to a single rect intersection? IIOW, is the clip effectively a rectangle.
Brian Salomon19f0ed52017-01-06 13:54:58 -0500218 bool fIsIntersectionOfRects;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000219
Robert Phillips806be2d2017-06-28 15:23:59 -0400220 uint32_t fGenID;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500221#if SK_SUPPORT_GPU
Robert Phillips427966a2018-12-20 17:20:43 -0500222 mutable GrProxyProvider* fProxyProvider = nullptr;
223 mutable SkTArray<GrUniqueKey> fKeysToInvalidate;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500224#endif
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000225 Element(int saveCount) {
Mike Reed14113bc2017-05-10 14:13:20 -0400226 this->initCommon(saveCount, kReplace_SkClipOp, false);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000227 this->setEmpty();
228 }
229
Brian Salomon2e866342017-08-28 12:38:58 -0400230 Element(int saveCount, const SkRRect& rrect, const SkMatrix& m, SkClipOp op, bool doAA) {
231 this->initRRect(saveCount, rrect, m, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000232 }
233
Brian Salomon2e866342017-08-28 12:38:58 -0400234 Element(int saveCount, const SkRect& rect, const SkMatrix& m, SkClipOp op, bool doAA) {
235 this->initRect(saveCount, rect, m, op, doAA);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000236 }
237
Brian Salomon2e866342017-08-28 12:38:58 -0400238 Element(int saveCount, const SkPath& path, const SkMatrix& m, SkClipOp op, bool doAA) {
239 this->initPath(saveCount, path, m, op, doAA);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000240 }
241
Brian Salomon5c727962017-08-21 12:37:36 -0400242 void initCommon(int saveCount, SkClipOp op, bool doAA);
Brian Salomon2e866342017-08-28 12:38:58 -0400243 void initRect(int saveCount, const SkRect&, const SkMatrix&, SkClipOp, bool doAA);
244 void initRRect(int saveCount, const SkRRect&, const SkMatrix&, SkClipOp, bool doAA);
245 void initPath(int saveCount, const SkPath&, const SkMatrix&, SkClipOp, bool doAA);
246 void initAsPath(int saveCount, const SkPath&, const SkMatrix&, SkClipOp, bool doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000247
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000248 void setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000249
250 // All Element methods below are only used within SkClipStack.cpp
251 inline void checkEmpty() const;
Mike Reedc1f77742016-12-09 09:00:50 -0500252 inline bool canBeIntersectedInPlace(int saveCount, SkClipOp op) const;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000253 /* This method checks to see if two rect clips can be safely merged into one. The issue here
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000254 is that to be strictly correct all the edges of the resulting rect must have the same
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000255 anti-aliasing. */
256 bool rectRectIntersectAllowed(const SkRect& newR, bool newAA) const;
257 /** Determines possible finite bounds for the Element given the previous element of the
258 stack */
259 void updateBoundAndGenID(const Element* prior);
260 // The different combination of fill & inverse fill when combining bounding boxes
261 enum FillCombo {
262 kPrev_Cur_FillCombo,
263 kPrev_InvCur_FillCombo,
264 kInvPrev_Cur_FillCombo,
265 kInvPrev_InvCur_FillCombo
266 };
267 // per-set operation functions used by updateBoundAndGenID().
268 inline void combineBoundsDiff(FillCombo combination, const SkRect& prevFinite);
269 inline void combineBoundsXOR(int combination, const SkRect& prevFinite);
270 inline void combineBoundsUnion(int combination, const SkRect& prevFinite);
271 inline void combineBoundsIntersection(int combination, const SkRect& prevFinite);
272 inline void combineBoundsRevDiff(int combination, const SkRect& prevFinite);
273 };
274
reed@google.com5c3d1472011-02-22 19:12:23 +0000275 SkClipStack();
Mike Reedd37f22b2017-03-09 23:56:25 -0500276 SkClipStack(void* storage, size_t size);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000277 SkClipStack(const SkClipStack& b);
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000278 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +0000279
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000280 SkClipStack& operator=(const SkClipStack& b);
281 bool operator==(const SkClipStack& b) const;
282 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
283
reed@google.com5c3d1472011-02-22 19:12:23 +0000284 void reset();
285
286 int getSaveCount() const { return fSaveCount; }
287 void save();
288 void restore();
289
Mike Reedf880b682017-03-10 11:30:44 -0500290 class AutoRestore {
291 public:
292 AutoRestore(SkClipStack* cs, bool doSave)
293 : fCS(cs), fSaveCount(cs->getSaveCount())
294 {
295 if (doSave) {
296 fCS->save();
297 }
298 }
299 ~AutoRestore() {
300 SkASSERT(fCS->getSaveCount() >= fSaveCount); // no underflow
301 while (fCS->getSaveCount() > fSaveCount) {
302 fCS->restore();
303 }
304 }
305
306 private:
307 SkClipStack* fCS;
308 const int fSaveCount;
309 };
310
robertphillips@google.com607fe072012-07-24 13:54:00 +0000311 /**
312 * getBounds places the current finite bound in its first parameter. In its
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000313 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com7b112892012-07-31 15:18:21 +0000314 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000315 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
robertphillips@google.com607fe072012-07-24 13:54:00 +0000316 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000317 * infinite plane. isIntersectionOfRects is an optional parameter
robertphillips@google.com7b112892012-07-31 15:18:21 +0000318 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000319 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320 void getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000321 BoundsType* boundType,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400322 bool* isIntersectionOfRects = nullptr) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000323
Hal Canaryf3ee34f2017-02-07 16:58:28 -0500324 SkRect bounds(const SkIRect& deviceBounds) const;
325 bool isEmpty(const SkIRect& deviceBounds) const;
326
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000327 /**
bsalomon7f0d9f32016-08-15 14:49:10 -0700328 * Returns true if the input (r)rect in device space is entirely contained
329 * by the clip. A return value of false does not guarantee that the (r)rect
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000330 * is not contained by the clip.
331 */
reed4d2cce42016-08-22 13:03:47 -0700332 bool quickContains(const SkRect& devRect) const {
333 return this->isWideOpen() || this->internalQuickContains(devRect);
334 }
335
336 bool quickContains(const SkRRect& devRRect) const {
337 return this->isWideOpen() || this->internalQuickContains(devRRect);
338 }
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000339
fmalita1a481fe2015-02-04 07:39:34 -0800340 /**
341 * Flattens the clip stack into a single SkPath. Returns true if any of
342 * the clip stack components requires anti-aliasing.
343 */
344 bool asPath(SkPath* path) const;
345
Mike Reedc1f77742016-12-09 09:00:50 -0500346 void clipDevRect(const SkIRect& ir, SkClipOp op) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000347 SkRect r;
348 r.set(ir);
Brian Salomona3b45d42016-10-03 11:36:16 -0400349 this->clipRect(r, SkMatrix::I(), op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +0000350 }
Mike Reedc1f77742016-12-09 09:00:50 -0500351 void clipRect(const SkRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
352 void clipRRect(const SkRRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
353 void clipPath(const SkPath&, const SkMatrix& matrix, SkClipOp, bool doAA);
reed@google.com0557d9e2012-08-16 15:59:59 +0000354 // An optimized version of clipDevRect(emptyRect, kIntersect, ...)
355 void clipEmpty();
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500356 void setDeviceClipRestriction(const SkIRect& rect) {
Mike Reed4ec2b712016-12-13 09:13:31 -0500357 fClipRestrictionRect = SkRect::Make(rect);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500358 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000359
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000360 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000361 * isWideOpen returns true if the clip state corresponds to the infinite
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000362 * plane (i.e., draws are not limited at all)
363 */
reed4d2cce42016-08-22 13:03:47 -0700364 bool isWideOpen() const { return this->getTopmostGenID() == kWideOpenGenID; }
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000365
robertphillips@google.com46f93502012-08-07 15:38:08 +0000366 /**
bsalomoncb31e512016-08-26 10:48:19 -0700367 * 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.com46f93502012-08-07 15:38:08 +0000382 * The generation ID has three reserved values to indicate special
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000383 * (potentially ignorable) cases
robertphillips@google.com46f93502012-08-07 15:38:08 +0000384 */
Robert Phillips806be2d2017-06-28 15:23:59 -0400385 static const uint32_t kInvalidGenID = 0; //!< Invalid id that is never returned by
386 //!< SkClipStack. Useful when caching clips
387 //!< based on GenID.
388 static const uint32_t kEmptyGenID = 1; // no pixels writeable
389 static const uint32_t kWideOpenGenID = 2; // all pixels writeable
robertphillips@google.com46f93502012-08-07 15:38:08 +0000390
Robert Phillips806be2d2017-06-28 15:23:59 -0400391 uint32_t getTopmostGenID() const;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000392
djsollenefe46d22016-04-29 06:41:35 -0700393#ifdef SK_DEBUG
bsalomonb6b02522014-06-09 07:59:06 -0700394 /**
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.com52cb2c72012-07-16 18:52:29 +0000401public:
402 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +0000403 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000404 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000405 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
406 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000407 };
408
bsalomon@google.comd302f142011-03-03 13:54:13 +0000409 /**
410 * Creates an uninitialized iterator. Must be reset()
411 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000412 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000413
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000414 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000415
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000416 /**
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.com5c3d1472011-02-22 19:12:23 +0000422
423 /**
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000424 * 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.com5c3d1472011-02-22 19:12:23 +0000426 */
Mike Reedc1f77742016-12-09 09:00:50 -0500427 const Element* skipToTopmost(SkClipOp op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000428
429 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000430 * Restarts the iterator on a clip stack.
431 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000432 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000433
reed@google.com5c3d1472011-02-22 19:12:23 +0000434 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000435 const SkClipStack* fStack;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000436 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000437 };
438
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000439 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000440 * 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.com52cb2c72012-07-16 18:52:29 +0000444 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000445 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000446
447 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000448 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000449 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000450 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000451 B2TIter(const SkClipStack& stack)
robertphillips@google.com80214e22012-07-20 15:33:18 +0000452 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000453 }
454
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000455 using Iter::next;
456
457 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000458 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000459 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000460 */
461 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000462 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000463 }
464
465 private:
466
467 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000468 };
469
robertphillips@google.com607fe072012-07-24 13:54:00 +0000470 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000471 * 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.com607fe072012-07-24 13:54:00 +0000474 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000475 * allow the caller to offset the returned bound to account for translated
robertphillips@google.com607fe072012-07-24 13:54:00 +0000476 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000477 * the translation (+offsetX, +offsetY) is applied before the clamp to the
robertphillips@google.com607fe072012-07-24 13:54:00 +0000478 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000479 * isIntersectionOfRects is an optional parameter that is true when
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000480 * 'devBounds' is the result of an intersection of rects. In this case
481 * 'devBounds' is the exact answer/clip.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000482 */
483 void getConservativeBounds(int offsetX,
484 int offsetY,
485 int maxWidth,
486 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000487 SkRect* devBounds,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400488 bool* isIntersectionOfRects = nullptr) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000489
reed@google.com5c3d1472011-02-22 19:12:23 +0000490private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000491 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000492
493 SkDeque fDeque;
494 int fSaveCount;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000495
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500496 SkRect fClipRestrictionRect = SkRect::MakeEmpty();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000497
reed4d2cce42016-08-22 13:03:47 -0700498 bool internalQuickContains(const SkRect& devRect) const;
499 bool internalQuickContains(const SkRRect& devRRect) const;
500
robertphillips@google.com46f93502012-08-07 15:38:08 +0000501 /**
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000502 * Helper for clipDevPath, etc.
503 */
504 void pushElement(const Element& element);
505
506 /**
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000507 * Restore the stack back to the specified save count.
508 */
509 void restoreTo(int saveCount);
510
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500511 inline bool hasClipRestriction(SkClipOp op) {
Mike Reed14113bc2017-05-10 14:13:20 -0400512 return op >= kUnion_SkClipOp && !fClipRestrictionRect.isEmpty();
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500513 }
514
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000515 /**
robertphillips@google.com46f93502012-08-07 15:38:08 +0000516 * Return the next unique generation ID.
517 */
Robert Phillips806be2d2017-06-28 15:23:59 -0400518 static uint32_t GetNextGenID();
reed@google.com5c3d1472011-02-22 19:12:23 +0000519};
520
521#endif
Robert Phillips427966a2018-12-20 17:20:43 -0500522