blob: af0d1a7dfc56ca553517ef4ba00e9e4614a1fe7f [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 */
fmalitac3b589a2014-06-05 12:40:07 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkClipStack.h"
Michael Ludwig4e221bd2020-06-05 11:29:36 -040011#include "src/core/SkRectPriv.h"
12#include "src/shaders/SkShaderBase.h"
13
Mike Klein0ec1c572018-12-04 11:52:51 -050014#include <atomic>
reed@google.com5c3d1472011-02-22 19:12:23 +000015#include <new>
16
Robert Phillips427966a2018-12-20 17:20:43 -050017#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrProxyProvider.h"
Robert Phillips427966a2018-12-20 17:20:43 -050019#endif
20
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000021SkClipStack::Element::Element(const Element& that) {
Brian Salomonf3b46e52017-08-30 11:37:57 -040022 switch (that.getDeviceSpaceType()) {
23 case DeviceSpaceType::kEmpty:
24 fDeviceSpaceRRect.setEmpty();
25 fDeviceSpacePath.reset();
Michael Ludwig4e221bd2020-06-05 11:29:36 -040026 fShader.reset();
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000027 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -040028 case DeviceSpaceType::kRect: // Rect uses rrect
29 case DeviceSpaceType::kRRect:
30 fDeviceSpacePath.reset();
Michael Ludwig4e221bd2020-06-05 11:29:36 -040031 fShader.reset();
Brian Salomonf3b46e52017-08-30 11:37:57 -040032 fDeviceSpaceRRect = that.fDeviceSpaceRRect;
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000033 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -040034 case DeviceSpaceType::kPath:
Michael Ludwig4e221bd2020-06-05 11:29:36 -040035 fShader.reset();
Brian Salomonf3b46e52017-08-30 11:37:57 -040036 fDeviceSpacePath.set(that.getDeviceSpacePath());
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000037 break;
Michael Ludwig4e221bd2020-06-05 11:29:36 -040038 case DeviceSpaceType::kShader:
39 fDeviceSpacePath.reset();
40 fShader = that.fShader;
41 break;
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000042 }
43
44 fSaveCount = that.fSaveCount;
45 fOp = that.fOp;
Brian Salomonf3b46e52017-08-30 11:37:57 -040046 fDeviceSpaceType = that.fDeviceSpaceType;
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000047 fDoAA = that.fDoAA;
Michael Ludwigd59d3e12021-08-05 16:39:18 -040048 fIsReplace = that.fIsReplace;
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000049 fFiniteBoundType = that.fFiniteBoundType;
50 fFiniteBound = that.fFiniteBound;
51 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
52 fGenID = that.fGenID;
53}
54
Robert Phillips427966a2018-12-20 17:20:43 -050055SkClipStack::Element::~Element() {
56#if SK_SUPPORT_GPU
57 for (int i = 0; i < fKeysToInvalidate.count(); ++i) {
58 fProxyProvider->processInvalidUniqueKey(fKeysToInvalidate[i], nullptr,
59 GrProxyProvider::InvalidateGPUResource::kYes);
60 }
61#endif
62}
63
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000064bool SkClipStack::Element::operator== (const Element& element) const {
65 if (this == &element) {
66 return true;
67 }
Brian Salomonf3b46e52017-08-30 11:37:57 -040068 if (fOp != element.fOp || fDeviceSpaceType != element.fDeviceSpaceType ||
Michael Ludwigd59d3e12021-08-05 16:39:18 -040069 fDoAA != element.fDoAA || fIsReplace != element.fIsReplace ||
70 fSaveCount != element.fSaveCount) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000071 return false;
72 }
Brian Salomonf3b46e52017-08-30 11:37:57 -040073 switch (fDeviceSpaceType) {
Michael Ludwig4e221bd2020-06-05 11:29:36 -040074 case DeviceSpaceType::kShader:
75 return this->getShader() == element.getShader();
Brian Salomonf3b46e52017-08-30 11:37:57 -040076 case DeviceSpaceType::kPath:
77 return this->getDeviceSpacePath() == element.getDeviceSpacePath();
78 case DeviceSpaceType::kRRect:
79 return fDeviceSpaceRRect == element.fDeviceSpaceRRect;
80 case DeviceSpaceType::kRect:
81 return this->getDeviceSpaceRect() == element.getDeviceSpaceRect();
82 case DeviceSpaceType::kEmpty:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000083 return true;
84 default:
85 SkDEBUGFAIL("Unexpected type.");
86 return false;
87 }
88}
89
Brian Salomon5c727962017-08-21 12:37:36 -040090const SkRect& SkClipStack::Element::getBounds() const {
91 static const SkRect kEmpty = {0, 0, 0, 0};
Michael Ludwig4e221bd2020-06-05 11:29:36 -040092 static const SkRect kInfinite = SkRectPriv::MakeLargeS32();
Brian Salomonf3b46e52017-08-30 11:37:57 -040093 switch (fDeviceSpaceType) {
94 case DeviceSpaceType::kRect: // fallthrough
95 case DeviceSpaceType::kRRect:
96 return fDeviceSpaceRRect.getBounds();
97 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -040098 return fDeviceSpacePath->getBounds();
Michael Ludwig4e221bd2020-06-05 11:29:36 -040099 case DeviceSpaceType::kShader:
100 // Shaders have infinite bounds since any pixel could have clipped or full coverage
101 // (which is different from wide-open, where every pixel has 1.0 coverage, or empty
102 // where every pixel has 0.0 coverage).
103 return kInfinite;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400104 case DeviceSpaceType::kEmpty:
Brian Salomon5c727962017-08-21 12:37:36 -0400105 return kEmpty;
106 default:
107 SkDEBUGFAIL("Unexpected type.");
108 return kEmpty;
109 }
110}
111
112bool SkClipStack::Element::contains(const SkRect& rect) const {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400113 switch (fDeviceSpaceType) {
114 case DeviceSpaceType::kRect:
115 return this->getDeviceSpaceRect().contains(rect);
116 case DeviceSpaceType::kRRect:
117 return fDeviceSpaceRRect.contains(rect);
118 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -0400119 return fDeviceSpacePath->conservativelyContainsRect(rect);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400120 case DeviceSpaceType::kEmpty:
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400121 case DeviceSpaceType::kShader:
Brian Salomon5c727962017-08-21 12:37:36 -0400122 return false;
123 default:
124 SkDEBUGFAIL("Unexpected type.");
125 return false;
126 }
127}
128
129bool SkClipStack::Element::contains(const SkRRect& rrect) const {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400130 switch (fDeviceSpaceType) {
131 case DeviceSpaceType::kRect:
132 return this->getDeviceSpaceRect().contains(rrect.getBounds());
133 case DeviceSpaceType::kRRect:
Brian Salomon5c727962017-08-21 12:37:36 -0400134 // We don't currently have a generalized rrect-rrect containment.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400135 return fDeviceSpaceRRect.contains(rrect.getBounds()) || rrect == fDeviceSpaceRRect;
136 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -0400137 return fDeviceSpacePath->conservativelyContainsRect(rrect.getBounds());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400138 case DeviceSpaceType::kEmpty:
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400139 case DeviceSpaceType::kShader:
Brian Salomon5c727962017-08-21 12:37:36 -0400140 return false;
141 default:
142 SkDEBUGFAIL("Unexpected type.");
143 return false;
144 }
145}
146
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000147void SkClipStack::Element::invertShapeFillType() {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400148 switch (fDeviceSpaceType) {
149 case DeviceSpaceType::kRect:
150 fDeviceSpacePath.init();
John Stilesa008b0f2020-08-16 08:48:02 -0400151 fDeviceSpacePath->addRect(this->getDeviceSpaceRect());
152 fDeviceSpacePath->setFillType(SkPathFillType::kInverseEvenOdd);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400153 fDeviceSpaceType = DeviceSpaceType::kPath;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000154 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400155 case DeviceSpaceType::kRRect:
156 fDeviceSpacePath.init();
John Stilesa008b0f2020-08-16 08:48:02 -0400157 fDeviceSpacePath->addRRect(fDeviceSpaceRRect);
158 fDeviceSpacePath->setFillType(SkPathFillType::kInverseEvenOdd);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400159 fDeviceSpaceType = DeviceSpaceType::kPath;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000160 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400161 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -0400162 fDeviceSpacePath->toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000163 break;
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400164 case DeviceSpaceType::kShader:
165 fShader = as_SB(fShader)->makeInvertAlpha();
166 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400167 case DeviceSpaceType::kEmpty:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000168 // Should this set to an empty, inverse filled path?
169 break;
170 }
171}
172
Brian Salomon5c727962017-08-21 12:37:36 -0400173void SkClipStack::Element::initCommon(int saveCount, SkClipOp op, bool doAA) {
174 fSaveCount = saveCount;
175 fOp = op;
176 fDoAA = doAA;
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400177 fIsReplace = false;
Brian Salomon5c727962017-08-21 12:37:36 -0400178 // A default of inside-out and empty bounds means the bounds are effectively void as it
179 // indicates that nothing is known to be outside the clip.
180 fFiniteBoundType = kInsideOut_BoundsType;
181 fFiniteBound.setEmpty();
182 fIsIntersectionOfRects = false;
183 fGenID = kInvalidGenID;
184}
185
Brian Salomon2e866342017-08-28 12:38:58 -0400186void SkClipStack::Element::initRect(int saveCount, const SkRect& rect, const SkMatrix& m,
187 SkClipOp op, bool doAA) {
188 if (m.rectStaysRect()) {
189 SkRect devRect;
190 m.mapRect(&devRect, rect);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400191 fDeviceSpaceRRect.setRect(devRect);
192 fDeviceSpaceType = DeviceSpaceType::kRect;
Brian Salomon2e866342017-08-28 12:38:58 -0400193 this->initCommon(saveCount, op, doAA);
194 return;
Brian Salomon5c727962017-08-21 12:37:36 -0400195 }
Brian Salomon2e866342017-08-28 12:38:58 -0400196 SkPath path;
197 path.addRect(rect);
198 path.setIsVolatile(true);
199 this->initAsPath(saveCount, path, m, op, doAA);
Brian Salomon5c727962017-08-21 12:37:36 -0400200}
201
Brian Salomon2e866342017-08-28 12:38:58 -0400202void SkClipStack::Element::initRRect(int saveCount, const SkRRect& rrect, const SkMatrix& m,
203 SkClipOp op, bool doAA) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400204 if (rrect.transform(m, &fDeviceSpaceRRect)) {
205 SkRRect::Type type = fDeviceSpaceRRect.getType();
Brian Salomon2e866342017-08-28 12:38:58 -0400206 if (SkRRect::kRect_Type == type || SkRRect::kEmpty_Type == type) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400207 fDeviceSpaceType = DeviceSpaceType::kRect;
Brian Salomon2e866342017-08-28 12:38:58 -0400208 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400209 fDeviceSpaceType = DeviceSpaceType::kRRect;
Brian Salomon2e866342017-08-28 12:38:58 -0400210 }
211 this->initCommon(saveCount, op, doAA);
212 return;
213 }
214 SkPath path;
215 path.addRRect(rrect);
216 path.setIsVolatile(true);
217 this->initAsPath(saveCount, path, m, op, doAA);
218}
219
220void SkClipStack::Element::initPath(int saveCount, const SkPath& path, const SkMatrix& m,
221 SkClipOp op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000222 if (!path.isInverseFillType()) {
robertphillips2b6ab612015-01-05 12:22:14 -0800223 SkRect r;
224 if (path.isRect(&r)) {
Brian Salomon2e866342017-08-28 12:38:58 -0400225 this->initRect(saveCount, r, m, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000226 return;
227 }
228 SkRect ovalRect;
229 if (path.isOval(&ovalRect)) {
230 SkRRect rrect;
231 rrect.setOval(ovalRect);
Brian Salomon2e866342017-08-28 12:38:58 -0400232 this->initRRect(saveCount, rrect, m, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000233 return;
234 }
235 }
Brian Salomon2e866342017-08-28 12:38:58 -0400236 this->initAsPath(saveCount, path, m, op, doAA);
237}
238
239void SkClipStack::Element::initAsPath(int saveCount, const SkPath& path, const SkMatrix& m,
240 SkClipOp op, bool doAA) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400241 path.transform(m, fDeviceSpacePath.init());
John Stilesa008b0f2020-08-16 08:48:02 -0400242 fDeviceSpacePath->setIsVolatile(true);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400243 fDeviceSpaceType = DeviceSpaceType::kPath;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000244 this->initCommon(saveCount, op, doAA);
245}
246
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400247void SkClipStack::Element::initShader(int saveCount, sk_sp<SkShader> shader) {
248 SkASSERT(shader);
249 fDeviceSpaceType = DeviceSpaceType::kShader;
250 fShader = std::move(shader);
251 this->initCommon(saveCount, SkClipOp::kIntersect, false);
252}
253
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400254void SkClipStack::Element::initReplaceRect(int saveCount, const SkRect& rect, bool doAA) {
255 fDeviceSpaceRRect.setRect(rect);
256 fDeviceSpaceType = DeviceSpaceType::kRect;
257 this->initCommon(saveCount, SkClipOp::kIntersect, doAA);
258 fIsReplace = true;
259}
260
Brian Salomonf3b46e52017-08-30 11:37:57 -0400261void SkClipStack::Element::asDeviceSpacePath(SkPath* path) const {
262 switch (fDeviceSpaceType) {
263 case DeviceSpaceType::kEmpty:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000264 path->reset();
265 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400266 case DeviceSpaceType::kRect:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000267 path->reset();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400268 path->addRect(this->getDeviceSpaceRect());
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000269 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400270 case DeviceSpaceType::kRRect:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000271 path->reset();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400272 path->addRRect(fDeviceSpaceRRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000273 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400274 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -0400275 *path = *fDeviceSpacePath;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000276 break;
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400277 case DeviceSpaceType::kShader:
278 path->reset();
279 path->addRect(SkRectPriv::MakeLargeS32());
280 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000281 }
jvanverth0deb2d92014-10-24 12:41:32 -0700282 path->setIsVolatile(true);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000283}
284
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000285void SkClipStack::Element::setEmpty() {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400286 fDeviceSpaceType = DeviceSpaceType::kEmpty;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000287 fFiniteBound.setEmpty();
288 fFiniteBoundType = kNormal_BoundsType;
289 fIsIntersectionOfRects = false;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400290 fDeviceSpaceRRect.setEmpty();
291 fDeviceSpacePath.reset();
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400292 fShader.reset();
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000293 fGenID = kEmptyGenID;
294 SkDEBUGCODE(this->checkEmpty();)
295}
296
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000297void SkClipStack::Element::checkEmpty() const {
298 SkASSERT(fFiniteBound.isEmpty());
299 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
300 SkASSERT(!fIsIntersectionOfRects);
301 SkASSERT(kEmptyGenID == fGenID);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400302 SkASSERT(fDeviceSpaceRRect.isEmpty());
303 SkASSERT(!fDeviceSpacePath.isValid());
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400304 SkASSERT(!fShader);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000305}
reed@google.com5c3d1472011-02-22 19:12:23 +0000306
Mike Reedc1f77742016-12-09 09:00:50 -0500307bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkClipOp op) const {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400308 if (DeviceSpaceType::kEmpty == fDeviceSpaceType &&
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400309 (SkClipOp::kDifference == op || SkClipOp::kIntersect == op)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000310 return true;
311 }
312 // Only clips within the same save/restore frame (as captured by
313 // the save count) can be merged
314 return fSaveCount == saveCount &&
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400315 SkClipOp::kIntersect == op &&
316 (SkClipOp::kIntersect == fOp || this->isReplaceOp());
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000317}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000318
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000319bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400320 SkASSERT(DeviceSpaceType::kRect == fDeviceSpaceType);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000321
322 if (fDoAA == newAA) {
323 // if the AA setting is the same there is no issue
324 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000325 }
326
Brian Salomonf3b46e52017-08-30 11:37:57 -0400327 if (!SkRect::Intersects(this->getDeviceSpaceRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000328 // The calling code will correctly set the result to the empty clip
329 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000330 }
331
Brian Salomonf3b46e52017-08-30 11:37:57 -0400332 if (this->getDeviceSpaceRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000333 // if the new rect carves out a portion of the old one there is no
334 // issue
335 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000336 }
337
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000338 // So either the two overlap in some complex manner or newR contains oldR.
339 // In the first, case the edges will require different AA. In the second,
340 // the AA setting that would be carried forward is incorrect (e.g., oldR
341 // is AA while newR is BW but since newR contains oldR, oldR will be
342 // drawn BW) since the new AA setting will predominate.
343 return false;
344}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000345
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000346// a mirror of combineBoundsRevDiff
347void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
348 switch (combination) {
349 case kInvPrev_InvCur_FillCombo:
350 // In this case the only pixels that can remain set
351 // are inside the current clip rect since the extensions
352 // to infinity of both clips cancel out and whatever
353 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000354 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000355 break;
356 case kInvPrev_Cur_FillCombo:
357 // In this case the current op is finite so the only pixels
358 // that aren't set are whatever isn't set in the previous
359 // clip and whatever this clip carves out
360 fFiniteBound.join(prevFinite);
361 fFiniteBoundType = kInsideOut_BoundsType;
362 break;
363 case kPrev_InvCur_FillCombo:
364 // In this case everything outside of this clip's bound
365 // is erased, so the only pixels that can remain set
366 // occur w/in the intersection of the two finite bounds
367 if (!fFiniteBound.intersect(prevFinite)) {
csmartdaltond50e2402016-07-22 08:39:06 -0700368 fFiniteBound.setEmpty();
369 fGenID = kEmptyGenID;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000370 }
csmartdaltond50e2402016-07-22 08:39:06 -0700371 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000372 break;
373 case kPrev_Cur_FillCombo:
374 // The most conservative result bound is that of the
375 // prior clip. This could be wildly incorrect if the
376 // second clip either exactly matches the first clip
377 // (which should yield the empty set) or reduces the
378 // size of the prior bound (e.g., if the second clip
379 // exactly matched the bottom half of the prior clip).
380 // We ignore these two possibilities.
381 fFiniteBound = prevFinite;
382 break;
383 default:
384 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
385 break;
386 }
387}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000388
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000389// a mirror of combineBoundsUnion
390void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
391
392 switch (combination) {
393 case kInvPrev_InvCur_FillCombo:
394 // The only pixels that aren't writable in this case
395 // occur in the union of the two finite bounds
396 fFiniteBound.join(prevFinite);
397 fFiniteBoundType = kInsideOut_BoundsType;
398 break;
399 case kInvPrev_Cur_FillCombo:
400 // In this case the only pixels that will remain writeable
401 // are within the current clip
402 break;
403 case kPrev_InvCur_FillCombo:
404 // In this case the only pixels that will remain writeable
405 // are with the previous clip
406 fFiniteBound = prevFinite;
407 fFiniteBoundType = kNormal_BoundsType;
408 break;
409 case kPrev_Cur_FillCombo:
410 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000411 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000412 }
413 break;
414 default:
415 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
416 break;
417 }
418}
419
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000420void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
421 // We set this first here but we may overwrite it later if we determine that the clip is
422 // either wide-open or empty.
423 fGenID = GetNextGenID();
424
425 // First, optimistically update the current Element's bound information
426 // with the current clip's bound
427 fIsIntersectionOfRects = false;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400428 switch (fDeviceSpaceType) {
429 case DeviceSpaceType::kRect:
430 fFiniteBound = this->getDeviceSpaceRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000431 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000432
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400433 if (this->isReplaceOp() ||
434 (SkClipOp::kIntersect == fOp && nullptr == prior) ||
435 (SkClipOp::kIntersect == fOp && prior->fIsIntersectionOfRects &&
Brian Salomonf3b46e52017-08-30 11:37:57 -0400436 prior->rectRectIntersectAllowed(this->getDeviceSpaceRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000437 fIsIntersectionOfRects = true;
438 }
439 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400440 case DeviceSpaceType::kRRect:
441 fFiniteBound = fDeviceSpaceRRect.getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000442 fFiniteBoundType = kNormal_BoundsType;
443 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400444 case DeviceSpaceType::kPath:
John Stilesa008b0f2020-08-16 08:48:02 -0400445 fFiniteBound = fDeviceSpacePath->getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000446
John Stilesa008b0f2020-08-16 08:48:02 -0400447 if (fDeviceSpacePath->isInverseFillType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000448 fFiniteBoundType = kInsideOut_BoundsType;
449 } else {
450 fFiniteBoundType = kNormal_BoundsType;
451 }
452 break;
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400453 case DeviceSpaceType::kShader:
454 // A shader is infinite. We don't act as wide-open here (which is an empty bounds with
455 // the inside out type). This is because when the bounds is empty and inside-out, we
456 // know there's full coverage everywhere. With a shader, there's *unknown* coverage
457 // everywhere.
458 fFiniteBound = SkRectPriv::MakeLargeS32();
459 fFiniteBoundType = kNormal_BoundsType;
460 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400461 case DeviceSpaceType::kEmpty:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000462 SkDEBUGFAIL("We shouldn't get here with an empty element.");
463 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000464 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000465
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000466 // Now determine the previous Element's bound information taking into
467 // account that there may be no previous clip
468 SkRect prevFinite;
469 SkClipStack::BoundsType prevType;
470
halcanary96fcdcc2015-08-27 07:41:13 -0700471 if (nullptr == prior) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000472 // no prior clip means the entire plane is writable
473 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
474 prevType = kInsideOut_BoundsType;
475 } else {
476 prevFinite = prior->fFiniteBound;
477 prevType = prior->fFiniteBoundType;
478 }
479
480 FillCombo combination = kPrev_Cur_FillCombo;
481 if (kInsideOut_BoundsType == fFiniteBoundType) {
482 combination = (FillCombo) (combination | 0x01);
483 }
484 if (kInsideOut_BoundsType == prevType) {
485 combination = (FillCombo) (combination | 0x02);
486 }
487
488 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
489 kInvPrev_Cur_FillCombo == combination ||
490 kPrev_InvCur_FillCombo == combination ||
491 kPrev_Cur_FillCombo == combination);
492
493 // Now integrate with clip with the prior clips
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400494 if (!this->isReplaceOp()) {
495 switch (fOp) {
496 case SkClipOp::kDifference:
497 this->combineBoundsDiff(combination, prevFinite);
498 break;
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400499 case SkClipOp::kIntersect:
500 this->combineBoundsIntersection(combination, prevFinite);
501 break;
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400502 default:
503 SkDebugf("SkClipOp error\n");
504 SkASSERT(0);
505 break;
506 }
507 } // else Replace just ignores everything prior and should already have filled in bounds.
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000508}
reed@google.com5c3d1472011-02-22 19:12:23 +0000509
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000510// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000511// the deque. As such it needs to balance allocating too much memory vs.
512// incurring allocation/deallocation thrashing. It should roughly correspond to
513// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000514static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000515
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000516SkClipStack::SkClipStack()
Mike Reed6e1e27b2017-03-10 02:51:59 +0000517 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000518 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000519}
520
Mike Reedd37f22b2017-03-09 23:56:25 -0500521SkClipStack::SkClipStack(void* storage, size_t size)
522 : fDeque(sizeof(Element), storage, size, kDefaultElementAllocCnt)
523 , fSaveCount(0) {
524}
525
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000526SkClipStack::SkClipStack(const SkClipStack& b)
Mike Reed6e1e27b2017-03-10 02:51:59 +0000527 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000528 *this = b;
529}
530
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000531SkClipStack::~SkClipStack() {
532 reset();
533}
534
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000535SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
536 if (this == &b) {
537 return *this;
538 }
539 reset();
540
541 fSaveCount = b.fSaveCount;
542 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000543 for (const Element* element = (const Element*)recIter.next();
halcanary96fcdcc2015-08-27 07:41:13 -0700544 element != nullptr;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000545 element = (const Element*)recIter.next()) {
546 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000547 }
548
549 return *this;
550}
551
552bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000553 if (this->getTopmostGenID() == b.getTopmostGenID()) {
554 return true;
555 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000556 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000557 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000558 return false;
559 }
560 SkDeque::F2BIter myIter(fDeque);
561 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000562 const Element* myElement = (const Element*)myIter.next();
563 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000564
halcanary96fcdcc2015-08-27 07:41:13 -0700565 while (myElement != nullptr && bElement != nullptr) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000566 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000567 return false;
568 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000569 myElement = (const Element*)myIter.next();
570 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000571 }
halcanary96fcdcc2015-08-27 07:41:13 -0700572 return myElement == nullptr && bElement == nullptr;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000573}
574
reed@google.com5c3d1472011-02-22 19:12:23 +0000575void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000576 // We used a placement new for each object in fDeque, so we're responsible
577 // for calling the destructor on each of them as well.
578 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000579 Element* element = (Element*)fDeque.back();
580 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000581 fDeque.pop_back();
582 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000583
584 fSaveCount = 0;
585}
586
587void SkClipStack::save() {
588 fSaveCount += 1;
589}
590
591void SkClipStack::restore() {
592 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000593 restoreTo(fSaveCount);
594}
595
596void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000597 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000598 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000599 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000600 break;
601 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000602 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000603 fDeque.pop_back();
604 }
605}
606
Hal Canaryf3ee34f2017-02-07 16:58:28 -0500607SkRect SkClipStack::bounds(const SkIRect& deviceBounds) const {
608 // TODO: optimize this.
609 SkRect r;
610 SkClipStack::BoundsType bounds;
611 this->getBounds(&r, &bounds);
612 if (bounds == SkClipStack::kInsideOut_BoundsType) {
613 return SkRect::Make(deviceBounds);
614 }
615 return r.intersect(SkRect::Make(deviceBounds)) ? r : SkRect::MakeEmpty();
616}
617
618// TODO: optimize this.
619bool SkClipStack::isEmpty(const SkIRect& r) const { return this->bounds(r).isEmpty(); }
620
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000621void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000622 BoundsType* boundType,
623 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700624 SkASSERT(canvFiniteBound && boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000625
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000626 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000627
halcanary96fcdcc2015-08-27 07:41:13 -0700628 if (nullptr == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000629 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000630 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000631 *boundType = kInsideOut_BoundsType;
bsalomon49f085d2014-09-05 13:34:00 -0700632 if (isIntersectionOfRects) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000633 *isIntersectionOfRects = false;
634 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000635 return;
636 }
637
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000638 *canvFiniteBound = element->fFiniteBound;
639 *boundType = element->fFiniteBoundType;
bsalomon49f085d2014-09-05 13:34:00 -0700640 if (isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000641 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000642 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000643}
644
reed4d2cce42016-08-22 13:03:47 -0700645bool SkClipStack::internalQuickContains(const SkRect& rect) const {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000646 Iter iter(*this, Iter::kTop_IterStart);
647 const Element* element = iter.prev();
halcanary96fcdcc2015-08-27 07:41:13 -0700648 while (element != nullptr) {
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400649 // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference.
650 if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000651 return false;
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400652 }
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000653 if (element->isInverseFilled()) {
654 // Part of 'rect' could be trimmed off by the inverse-filled clip element
655 if (SkRect::Intersects(element->getBounds(), rect)) {
656 return false;
657 }
658 } else {
659 if (!element->contains(rect)) {
660 return false;
661 }
662 }
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400663 if (element->isReplaceOp()) {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000664 break;
665 }
666 element = iter.prev();
667 }
668 return true;
669}
670
reed4d2cce42016-08-22 13:03:47 -0700671bool SkClipStack::internalQuickContains(const SkRRect& rrect) const {
bsalomon7f0d9f32016-08-15 14:49:10 -0700672 Iter iter(*this, Iter::kTop_IterStart);
673 const Element* element = iter.prev();
674 while (element != nullptr) {
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400675 // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference.
676 if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700677 return false;
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400678 }
bsalomon7f0d9f32016-08-15 14:49:10 -0700679 if (element->isInverseFilled()) {
680 // Part of 'rrect' could be trimmed off by the inverse-filled clip element
681 if (SkRect::Intersects(element->getBounds(), rrect.getBounds())) {
682 return false;
683 }
684 } else {
685 if (!element->contains(rrect)) {
686 return false;
687 }
688 }
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400689 if (element->isReplaceOp()) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700690 break;
691 }
692 element = iter.prev();
693 }
694 return true;
695}
696
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000697void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000698 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000699 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000700 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000701
bsalomon49f085d2014-09-05 13:34:00 -0700702 if (prior) {
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400703 if (element.isReplaceOp()) {
704 this->restoreTo(fSaveCount - 1);
705 prior = (Element*) fDeque.back();
706 } else if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400707 switch (prior->fDeviceSpaceType) {
708 case Element::DeviceSpaceType::kEmpty:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000709 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000710 return;
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400711 case Element::DeviceSpaceType::kShader:
712 if (Element::DeviceSpaceType::kShader == element.getDeviceSpaceType()) {
713 prior->fShader = SkShaders::Blend(SkBlendMode::kSrcIn,
714 element.fShader, prior->fShader);
715 Element* priorPrior = (Element*) iter.prev();
716 prior->updateBoundAndGenID(priorPrior);
717 return;
718 }
719 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400720 case Element::DeviceSpaceType::kRect:
721 if (Element::DeviceSpaceType::kRect == element.getDeviceSpaceType()) {
722 if (prior->rectRectIntersectAllowed(element.getDeviceSpaceRect(),
723 element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000724 SkRect isectRect;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400725 if (!isectRect.intersect(prior->getDeviceSpaceRect(),
726 element.getDeviceSpaceRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000727 prior->setEmpty();
728 return;
729 }
730
Brian Salomonf3b46e52017-08-30 11:37:57 -0400731 prior->fDeviceSpaceRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000732 prior->fDoAA = element.isAA();
733 Element* priorPrior = (Element*) iter.prev();
734 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000735 return;
736 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000737 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000738 }
John Stiles30212b72020-06-11 17:55:07 -0400739 [[fallthrough]];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000740 default:
741 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
742 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000743 return;
744 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000745 break;
746 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000747 }
748 }
halcanary385fe4d2015-08-26 13:07:48 -0700749 Element* newElement = new (fDeque.push_back()) Element(element);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000750 newElement->updateBoundAndGenID(prior);
751}
752
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400753void SkClipStack::clipRRect(const SkRRect& rrect, const SkMatrix& matrix, SkClipOp op, bool doAA) {
Brian Salomon2e866342017-08-28 12:38:58 -0400754 Element element(fSaveCount, rrect, matrix, op, doAA);
755 this->pushElement(element);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000756}
757
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400758void SkClipStack::clipRect(const SkRect& rect, const SkMatrix& matrix, SkClipOp op, bool doAA) {
Brian Salomon2e866342017-08-28 12:38:58 -0400759 Element element(fSaveCount, rect, matrix, op, doAA);
760 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000761}
762
Mike Reedc1f77742016-12-09 09:00:50 -0500763void SkClipStack::clipPath(const SkPath& path, const SkMatrix& matrix, SkClipOp op,
Brian Salomona3b45d42016-10-03 11:36:16 -0400764 bool doAA) {
Brian Salomon2e866342017-08-28 12:38:58 -0400765 Element element(fSaveCount, path, matrix, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000766 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000767}
768
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400769void SkClipStack::clipShader(sk_sp<SkShader> shader) {
770 Element element(fSaveCount, std::move(shader));
771 this->pushElement(element);
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400772}
773
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400774void SkClipStack::replaceClip(const SkRect& rect, bool doAA) {
775 Element element(fSaveCount, rect, doAA);
776 this->pushElement(element);
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400777}
778
reed@google.com0557d9e2012-08-16 15:59:59 +0000779void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000780 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000781
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400782 if (element && element->canBeIntersectedInPlace(fSaveCount, SkClipOp::kIntersect)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000783 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000784 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000785 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000786
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000787 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000788}
789
reed@google.com5c3d1472011-02-22 19:12:23 +0000790///////////////////////////////////////////////////////////////////////////////
791
halcanary96fcdcc2015-08-27 07:41:13 -0700792SkClipStack::Iter::Iter() : fStack(nullptr) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000793}
794
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000795SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
796 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000797 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000798}
799
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000800const SkClipStack::Element* SkClipStack::Iter::next() {
801 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000802}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000803
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000804const SkClipStack::Element* SkClipStack::Iter::prev() {
805 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000806}
807
Mike Reedc1f77742016-12-09 09:00:50 -0500808const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkClipOp op) {
halcanary96fcdcc2015-08-27 07:41:13 -0700809 if (nullptr == fStack) {
810 return nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000811 }
812
813 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
814
halcanary96fcdcc2015-08-27 07:41:13 -0700815 const SkClipStack::Element* element = nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000816
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000817 for (element = (const SkClipStack::Element*) fIter.prev();
bsalomon49f085d2014-09-05 13:34:00 -0700818 element;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000819 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000820
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000821 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000822 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000823 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000824 // return, the iterator is actually pointing at (and will
825 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000826 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000827 // step so we get the expected result.
halcanary96fcdcc2015-08-27 07:41:13 -0700828 if (nullptr == fIter.next()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000829 // The reverse iterator has run off the front of the deque
830 // (i.e., the "op" clip is the first clip) and can't
831 // recover. Reset the iterator to start at the front.
832 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
833 }
834 break;
835 }
836 }
837
halcanary96fcdcc2015-08-27 07:41:13 -0700838 if (nullptr == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000839 // There were no "op" clips
840 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
841 }
842
843 return this->next();
844}
845
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000846void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000847 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000848 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000849}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000850
851// helper method
852void SkClipStack::getConservativeBounds(int offsetX,
853 int offsetY,
854 int maxWidth,
855 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000856 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000857 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700858 SkASSERT(devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000859
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000860 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000861 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000862
863 SkRect temp;
864 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000865
robertphillips@google.com7b112892012-07-31 15:18:21 +0000866 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000867 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000868 if (SkClipStack::kInsideOut_BoundsType == boundType) {
869 return;
870 }
871
robertphillips@google.com7b112892012-07-31 15:18:21 +0000872 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000873 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
874
robertphillips@google.com7b112892012-07-31 15:18:21 +0000875 if (!devBounds->intersect(temp)) {
876 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000877 }
878}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000879
bsalomoncb31e512016-08-26 10:48:19 -0700880bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const {
Brian Salomon1c0b05a2019-04-19 15:37:28 -0400881 const Element* back = static_cast<const Element*>(fDeque.back());
882 if (!back) {
883 // TODO: return bounds?
Brian Salomon3bfba2a2018-01-23 15:12:07 -0500884 return false;
885 }
Brian Salomon1c0b05a2019-04-19 15:37:28 -0400886 // First check if the entire stack is known to be a rect by the top element.
887 if (back->fIsIntersectionOfRects && back->fFiniteBoundType == BoundsType::kNormal_BoundsType) {
888 rrect->setRect(back->fFiniteBound);
889 *aa = back->isAA();
890 return true;
891 }
892
Brian Salomonf3b46e52017-08-30 11:37:57 -0400893 if (back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRect &&
894 back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRRect) {
bsalomoncb31e512016-08-26 10:48:19 -0700895 return false;
896 }
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400897 if (back->isReplaceOp()) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400898 *rrect = back->asDeviceSpaceRRect();
bsalomoncb31e512016-08-26 10:48:19 -0700899 *aa = back->isAA();
900 return true;
901 }
902
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400903 if (back->getOp() == SkClipOp::kIntersect) {
bsalomoncb31e512016-08-26 10:48:19 -0700904 SkRect backBounds;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400905 if (!backBounds.intersect(bounds, back->asDeviceSpaceRRect().rect())) {
bsalomoncb31e512016-08-26 10:48:19 -0700906 return false;
907 }
Brian Salomon3061d272019-04-22 11:59:48 -0400908 // We limit to 17 elements. This means the back element will be bounds checked at most 16
Brian Salomon1c0b05a2019-04-19 15:37:28 -0400909 // times if it is an rrect.
910 int cnt = fDeque.count();
Brian Salomon3061d272019-04-22 11:59:48 -0400911 if (cnt > 17) {
Brian Salomon1c0b05a2019-04-19 15:37:28 -0400912 return false;
913 }
bsalomoncb31e512016-08-26 10:48:19 -0700914 if (cnt > 1) {
915 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
916 SkAssertResult(static_cast<const Element*>(iter.prev()) == back);
917 while (const Element* prior = (const Element*)iter.prev()) {
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400918 // TODO: Once expanding clip ops are removed, this is equiv. to op == kDifference
919 if ((prior->getOp() != SkClipOp::kIntersect && !prior->isReplaceOp()) ||
bsalomoncb31e512016-08-26 10:48:19 -0700920 !prior->contains(backBounds)) {
921 return false;
922 }
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400923 if (prior->isReplaceOp()) {
bsalomoncb31e512016-08-26 10:48:19 -0700924 break;
925 }
926 }
927 }
Brian Salomonf3b46e52017-08-30 11:37:57 -0400928 *rrect = back->asDeviceSpaceRRect();
bsalomoncb31e512016-08-26 10:48:19 -0700929 *aa = back->isAA();
930 return true;
931 }
932 return false;
933}
934
Robert Phillips806be2d2017-06-28 15:23:59 -0400935uint32_t SkClipStack::GetNextGenID() {
Mike Klein0ec1c572018-12-04 11:52:51 -0500936 // 0-2 are reserved for invalid, empty & wide-open
937 static const uint32_t kFirstUnreservedGenID = 3;
938 static std::atomic<uint32_t> nextID{kFirstUnreservedGenID};
939
Robert Phillips806be2d2017-06-28 15:23:59 -0400940 uint32_t id;
941 do {
Adlai Holler4888cda2020-11-06 16:37:37 -0500942 id = nextID.fetch_add(1, std::memory_order_relaxed);
Robert Phillips806be2d2017-06-28 15:23:59 -0400943 } while (id < kFirstUnreservedGenID);
944 return id;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000945}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000946
Robert Phillips806be2d2017-06-28 15:23:59 -0400947uint32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000948 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000949 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000950 }
951
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000952 const Element* back = static_cast<const Element*>(fDeque.back());
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400953 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty() &&
954 Element::DeviceSpaceType::kShader != back->fDeviceSpaceType) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000955 return kWideOpenGenID;
956 }
957
958 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000959}
bsalomonb6b02522014-06-09 07:59:06 -0700960
djsollenefe46d22016-04-29 06:41:35 -0700961#ifdef SK_DEBUG
bsalomonb6b02522014-06-09 07:59:06 -0700962void SkClipStack::Element::dump() const {
963 static const char* kTypeStrings[] = {
964 "empty",
965 "rect",
966 "rrect",
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400967 "path",
968 "shader"
bsalomonb6b02522014-06-09 07:59:06 -0700969 };
Brian Salomonf3b46e52017-08-30 11:37:57 -0400970 static_assert(0 == static_cast<int>(DeviceSpaceType::kEmpty), "enum mismatch");
971 static_assert(1 == static_cast<int>(DeviceSpaceType::kRect), "enum mismatch");
972 static_assert(2 == static_cast<int>(DeviceSpaceType::kRRect), "enum mismatch");
973 static_assert(3 == static_cast<int>(DeviceSpaceType::kPath), "enum mismatch");
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400974 static_assert(4 == static_cast<int>(DeviceSpaceType::kShader), "enum mismatch");
Brian Salomonf3b46e52017-08-30 11:37:57 -0400975 static_assert(SK_ARRAY_COUNT(kTypeStrings) == kTypeCnt, "enum mismatch");
bsalomonb6b02522014-06-09 07:59:06 -0700976
Michael Ludwiga0438e62021-08-06 11:19:26 -0400977 const char* opName = this->isReplaceOp() ? "replace" :
978 (fOp == SkClipOp::kDifference ? "difference" : "intersect");
Brian Salomonf3b46e52017-08-30 11:37:57 -0400979 SkDebugf("Type: %s, Op: %s, AA: %s, Save Count: %d\n", kTypeStrings[(int)fDeviceSpaceType],
Michael Ludwigd59d3e12021-08-05 16:39:18 -0400980 opName, (fDoAA ? "yes" : "no"), fSaveCount);
Brian Salomonf3b46e52017-08-30 11:37:57 -0400981 switch (fDeviceSpaceType) {
982 case DeviceSpaceType::kEmpty:
bsalomonb6b02522014-06-09 07:59:06 -0700983 SkDebugf("\n");
984 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400985 case DeviceSpaceType::kRect:
986 this->getDeviceSpaceRect().dump();
bsalomonb6b02522014-06-09 07:59:06 -0700987 SkDebugf("\n");
988 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400989 case DeviceSpaceType::kRRect:
990 this->getDeviceSpaceRRect().dump();
bsalomonb6b02522014-06-09 07:59:06 -0700991 SkDebugf("\n");
992 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400993 case DeviceSpaceType::kPath:
Mike Reededb22ec2020-12-06 14:08:41 -0500994 this->getDeviceSpacePath().dump(nullptr, false);
bsalomonb6b02522014-06-09 07:59:06 -0700995 break;
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400996 case DeviceSpaceType::kShader:
997 // SkShaders don't provide much introspection that's worth while.
998 break;
bsalomonb6b02522014-06-09 07:59:06 -0700999 }
1000}
1001
1002void SkClipStack::dump() const {
1003 B2TIter iter(*this);
1004 const Element* e;
1005 while ((e = iter.next())) {
1006 e->dump();
1007 SkDebugf("\n");
1008 }
1009}
1010#endif