Debian Codegraph @

src/core/SkClipStack.cpp

raw
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 */
7
8 #include "src/core/SkClipStack.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkDebug.h"
15 #include "src/core/SkRectPriv.h"
16
17 #include <array>
18 #include <atomic>
19 #include <new>
20
21 SkClipStack::Element::Element(const Element& that) {
22 switch (that.getDeviceSpaceType()) {
23 case DeviceSpaceType::kEmpty:
24 fDeviceSpaceRRect.setEmpty();
25 fDeviceSpacePath.reset();
26 fShader.reset();
27 break;
28 case DeviceSpaceType::kRect: // Rect uses rrect
29 case DeviceSpaceType::kRRect:
30 fDeviceSpacePath.reset();
31 fShader.reset();
32 fDeviceSpaceRRect = that.fDeviceSpaceRRect;
33 break;
34 case DeviceSpaceType::kPath:
35 fShader.reset();
36 fDeviceSpacePath = that.getDeviceSpacePath();
37 break;
38 case DeviceSpaceType::kShader:
39 fDeviceSpacePath.reset();
40 fShader = that.fShader;
41 break;
42 }
43
44 fSaveCount = that.fSaveCount;
45 fOp = that.fOp;
46 fDeviceSpaceType = that.fDeviceSpaceType;
47 fDoAA = that.fDoAA;
48 fIsReplace = that.fIsReplace;
49 fFiniteBoundType = that.fFiniteBoundType;
50 fFiniteBound = that.fFiniteBound;
51 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
52 fGenID = that.fGenID;
53 }
54
55 SkClipStack::Element::~Element() = default;
56
57 bool SkClipStack::Element::operator== (const Element& element) const {
58 if (this == &element) {
59 return true;
60 }
61 if (fOp != element.fOp || fDeviceSpaceType != element.fDeviceSpaceType ||
62 fDoAA != element.fDoAA || fIsReplace != element.fIsReplace ||
63 fSaveCount != element.fSaveCount) {
64 return false;
65 }
66 switch (fDeviceSpaceType) {
67 case DeviceSpaceType::kShader:
68 return this->getShader() == element.getShader();
69 case DeviceSpaceType::kPath:
70 return this->getDeviceSpacePath() == element.getDeviceSpacePath();
71 case DeviceSpaceType::kRRect:
72 return fDeviceSpaceRRect == element.fDeviceSpaceRRect;
73 case DeviceSpaceType::kRect:
74 return this->getDeviceSpaceRect() == element.getDeviceSpaceRect();
75 case DeviceSpaceType::kEmpty:
76 return true;
77 default:
78 SkDEBUGFAIL("Unexpected type.");
79 return false;
80 }
81 }
82
83 const SkRect& SkClipStack::Element::getBounds() const {
84 static const SkRect kEmpty = {0, 0, 0, 0};
85 static const SkRect kInfinite = SkRectPriv::MakeLargeS32();
86 switch (fDeviceSpaceType) {
87 case DeviceSpaceType::kRect: // fallthrough
88 case DeviceSpaceType::kRRect:
89 return fDeviceSpaceRRect.getBounds();
90 case DeviceSpaceType::kPath:
91 return fDeviceSpacePath->getBounds();
92 case DeviceSpaceType::kShader:
93 // Shaders have infinite bounds since any pixel could have clipped or full coverage
94 // (which is different from wide-open, where every pixel has 1.0 coverage, or empty
95 // where every pixel has 0.0 coverage).
96 return kInfinite;
97 case DeviceSpaceType::kEmpty:
98 return kEmpty;
99 default:
100 SkDEBUGFAIL("Unexpected type.");
101 return kEmpty;
102 }
103 }
104
105 bool SkClipStack::Element::contains(const SkRect& rect) const {
106 switch (fDeviceSpaceType) {
107 case DeviceSpaceType::kRect:
108 return this->getDeviceSpaceRect().contains(rect);
109 case DeviceSpaceType::kRRect:
110 return fDeviceSpaceRRect.contains(rect);
111 case DeviceSpaceType::kPath:
112 return fDeviceSpacePath->conservativelyContainsRect(rect);
113 case DeviceSpaceType::kEmpty:
114 case DeviceSpaceType::kShader:
115 return false;
116 default:
117 SkDEBUGFAIL("Unexpected type.");
118 return false;
119 }
120 }
121
122 bool SkClipStack::Element::contains(const SkRRect& rrect) const {
123 switch (fDeviceSpaceType) {
124 case DeviceSpaceType::kRect:
125 return this->getDeviceSpaceRect().contains(rrect.getBounds());
126 case DeviceSpaceType::kRRect:
127 // We don't currently have a generalized rrect-rrect containment.
128 return fDeviceSpaceRRect.contains(rrect.getBounds()) || rrect == fDeviceSpaceRRect;
129 case DeviceSpaceType::kPath:
130 return fDeviceSpacePath->conservativelyContainsRect(rrect.getBounds());
131 case DeviceSpaceType::kEmpty:
132 case DeviceSpaceType::kShader:
133 return false;
134 default:
135 SkDEBUGFAIL("Unexpected type.");
136 return false;
137 }
138 }
139
140 void SkClipStack::Element::initCommon(int saveCount, SkClipOp op, bool doAA) {
141 fSaveCount = saveCount;
142 fOp = op;
143 fDoAA = doAA;
144 fIsReplace = false;
145 // A default of inside-out and empty bounds means the bounds are effectively void as it
146 // indicates that nothing is known to be outside the clip.
147 fFiniteBoundType = kInsideOut_BoundsType;
148 fFiniteBound.setEmpty();
149 fIsIntersectionOfRects = false;
150 fGenID = kInvalidGenID;
151 }
152
153 void SkClipStack::Element::initRect(int saveCount, const SkRect& rect, const SkMatrix& m,
154 SkClipOp op, bool doAA) {
155 if (m.rectStaysRect()) {
156 SkRect devRect;
157 m.mapRect(&devRect, rect);
158 fDeviceSpaceRRect.setRect(devRect);
159 fDeviceSpaceType = DeviceSpaceType::kRect;
160 this->initCommon(saveCount, op, doAA);
161 return;
162 }
163 this->initAsPath(saveCount, SkPath::Rect(rect), m, op, doAA);
164 }
165
166 void SkClipStack::Element::initRRect(int saveCount, const SkRRect& rrect, const SkMatrix& m,
167 SkClipOp op, bool doAA) {
168 if (auto rr = rrect.transform(m)) {
169 fDeviceSpaceRRect = *rr;
170 SkRRect::Type type = fDeviceSpaceRRect.getType();
171 if (SkRRect::kRect_Type == type || SkRRect::kEmpty_Type == type) {
172 fDeviceSpaceType = DeviceSpaceType::kRect;
173 } else {
174 fDeviceSpaceType = DeviceSpaceType::kRRect;
175 }
176 this->initCommon(saveCount, op, doAA);
177 return;
178 }
179 this->initAsPath(saveCount, SkPath::RRect(rrect), m, op, doAA);
180 }
181
182 void SkClipStack::Element::initPath(int saveCount, const SkPath& path, const SkMatrix& m,
183 SkClipOp op, bool doAA) {
184 if (!path.isInverseFillType()) {
185 SkRect r;
186 if (path.isRect(&r)) {
187 this->initRect(saveCount, r, m, op, doAA);
188 return;
189 }
190 SkRect ovalRect;
191 if (path.isOval(&ovalRect)) {
192 SkRRect rrect;
193 rrect.setOval(ovalRect);
194 this->initRRect(saveCount, rrect, m, op, doAA);
195 return;
196 }
197 }
198 this->initAsPath(saveCount, path, m, op, doAA);
199 }
200
201 void SkClipStack::Element::initAsPath(int saveCount, const SkPath& path, const SkMatrix& m,
202 SkClipOp op, bool doAA) {
203 SkPathBuilder builder(path);
204 builder.transform(m);
205 builder.setIsVolatile(true);
206 fDeviceSpacePath = builder.detach();
207
208 fDeviceSpaceType = DeviceSpaceType::kPath;
209 this->initCommon(saveCount, op, doAA);
210 }
211
212 void SkClipStack::Element::initShader(int saveCount, sk_sp<SkShader> shader) {
213 SkASSERT(shader);
214 fDeviceSpaceType = DeviceSpaceType::kShader;
215 fShader = std::move(shader);
216 this->initCommon(saveCount, SkClipOp::kIntersect, false);
217 }
218
219 void SkClipStack::Element::initReplaceRect(int saveCount, const SkRect& rect, bool doAA) {
220 fDeviceSpaceRRect.setRect(rect);
221 fDeviceSpaceType = DeviceSpaceType::kRect;
222 this->initCommon(saveCount, SkClipOp::kIntersect, doAA);
223 fIsReplace = true;
224 }
225
226 SkPath SkClipStack::Element::asDeviceSpacePath() const {
227 SkPathBuilder builder;
228 switch (fDeviceSpaceType) {
229 case DeviceSpaceType::kEmpty:
230 break;
231 case DeviceSpaceType::kRect:
232 builder.addRect(this->getDeviceSpaceRect());
233 break;
234 case DeviceSpaceType::kRRect:
235 builder.addRRect(fDeviceSpaceRRect);
236 break;
237 case DeviceSpaceType::kPath:
238 return *fDeviceSpacePath;
239 case DeviceSpaceType::kShader:
240 builder.addRect(SkRectPriv::MakeLargeS32());
241 break;
242 }
243 return builder.detach();
244 }
245
246 void SkClipStack::Element::setEmpty() {
247 fDeviceSpaceType = DeviceSpaceType::kEmpty;
248 fFiniteBound.setEmpty();
249 fFiniteBoundType = kNormal_BoundsType;
250 fIsIntersectionOfRects = false;
251 fDeviceSpaceRRect.setEmpty();
252 fDeviceSpacePath.reset();
253 fShader.reset();
254 fGenID = kEmptyGenID;
255 SkDEBUGCODE(this->checkEmpty();)
256 }
257
258 void SkClipStack::Element::checkEmpty() const {
259 SkASSERT(fFiniteBound.isEmpty());
260 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
261 SkASSERT(!fIsIntersectionOfRects);
262 SkASSERT(kEmptyGenID == fGenID);
263 SkASSERT(fDeviceSpaceRRect.isEmpty());
264 SkASSERT(!fDeviceSpacePath.has_value());
265 SkASSERT(!fShader);
266 }
267
268 bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkClipOp op) const {
269 if (DeviceSpaceType::kEmpty == fDeviceSpaceType &&
270 (SkClipOp::kDifference == op || SkClipOp::kIntersect == op)) {
271 return true;
272 }
273 // Only clips within the same save/restore frame (as captured by
274 // the save count) can be merged
275 return fSaveCount == saveCount &&
276 SkClipOp::kIntersect == op &&
277 (SkClipOp::kIntersect == fOp || this->isReplaceOp());
278 }
279
280 bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
281 SkASSERT(DeviceSpaceType::kRect == fDeviceSpaceType);
282
283 if (fDoAA == newAA) {
284 // if the AA setting is the same there is no issue
285 return true;
286 }
287
288 if (!SkRect::Intersects(this->getDeviceSpaceRect(), newR)) {
289 // The calling code will correctly set the result to the empty clip
290 return true;
291 }
292
293 if (this->getDeviceSpaceRect().contains(newR)) {
294 // if the new rect carves out a portion of the old one there is no
295 // issue
296 return true;
297 }
298
299 // So either the two overlap in some complex manner or newR contains oldR.
300 // In the first, case the edges will require different AA. In the second,
301 // the AA setting that would be carried forward is incorrect (e.g., oldR
302 // is AA while newR is BW but since newR contains oldR, oldR will be
303 // drawn BW) since the new AA setting will predominate.
304 return false;
305 }
306
307 // a mirror of combineBoundsRevDiff
308 void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
309 switch (combination) {
310 case kInvPrev_InvCur_FillCombo:
311 // In this case the only pixels that can remain set
312 // are inside the current clip rect since the extensions
313 // to infinity of both clips cancel out and whatever
314 // is outside of the current clip is removed
315 fFiniteBoundType = kNormal_BoundsType;
316 break;
317 case kInvPrev_Cur_FillCombo:
318 // In this case the current op is finite so the only pixels
319 // that aren't set are whatever isn't set in the previous
320 // clip and whatever this clip carves out
321 fFiniteBound.join(prevFinite);
322 fFiniteBoundType = kInsideOut_BoundsType;
323 break;
324 case kPrev_InvCur_FillCombo:
325 // In this case everything outside of this clip's bound
326 // is erased, so the only pixels that can remain set
327 // occur w/in the intersection of the two finite bounds
328 if (!fFiniteBound.intersect(prevFinite)) {
329 fFiniteBound.setEmpty();
330 fGenID = kEmptyGenID;
331 }
332 fFiniteBoundType = kNormal_BoundsType;
333 break;
334 case kPrev_Cur_FillCombo:
335 // The most conservative result bound is that of the
336 // prior clip. This could be wildly incorrect if the
337 // second clip either exactly matches the first clip
338 // (which should yield the empty set) or reduces the
339 // size of the prior bound (e.g., if the second clip
340 // exactly matched the bottom half of the prior clip).
341 // We ignore these two possibilities.
342 fFiniteBound = prevFinite;
343 break;
344 default:
345 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
346 break;
347 }
348 }
349
350 // a mirror of combineBoundsUnion
351 void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
352
353 switch (combination) {
354 case kInvPrev_InvCur_FillCombo:
355 // The only pixels that aren't writable in this case
356 // occur in the union of the two finite bounds
357 fFiniteBound.join(prevFinite);
358 fFiniteBoundType = kInsideOut_BoundsType;
359 break;
360 case kInvPrev_Cur_FillCombo:
361 // In this case the only pixels that will remain writeable
362 // are within the current clip
363 break;
364 case kPrev_InvCur_FillCombo:
365 // In this case the only pixels that will remain writeable
366 // are with the previous clip
367 fFiniteBound = prevFinite;
368 fFiniteBoundType = kNormal_BoundsType;
369 break;
370 case kPrev_Cur_FillCombo:
371 if (!fFiniteBound.intersect(prevFinite)) {
372 this->setEmpty();
373 }
374 break;
375 default:
376 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
377 break;
378 }
379 }
380
381 void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
382 // We set this first here but we may overwrite it later if we determine that the clip is
383 // either wide-open or empty.
384 fGenID = GetNextGenID();
385
386 // First, optimistically update the current Element's bound information
387 // with the current clip's bound
388 fIsIntersectionOfRects = false;
389 switch (fDeviceSpaceType) {
390 case DeviceSpaceType::kRect:
391 fFiniteBound = this->getDeviceSpaceRect();
392 fFiniteBoundType = kNormal_BoundsType;
393
394 if (this->isReplaceOp() ||
395 (SkClipOp::kIntersect == fOp && nullptr == prior) ||
396 (SkClipOp::kIntersect == fOp && prior->fIsIntersectionOfRects &&
397 prior->rectRectIntersectAllowed(this->getDeviceSpaceRect(), fDoAA))) {
398 fIsIntersectionOfRects = true;
399 }
400 break;
401 case DeviceSpaceType::kRRect:
402 fFiniteBound = fDeviceSpaceRRect.getBounds();
403 fFiniteBoundType = kNormal_BoundsType;
404 break;
405 case DeviceSpaceType::kPath:
406 fFiniteBound = fDeviceSpacePath->getBounds();
407
408 if (fDeviceSpacePath->isInverseFillType()) {
409 fFiniteBoundType = kInsideOut_BoundsType;
410 } else {
411 fFiniteBoundType = kNormal_BoundsType;
412 }
413 break;
414 case DeviceSpaceType::kShader:
415 // A shader is infinite. We don't act as wide-open here (which is an empty bounds with
416 // the inside out type). This is because when the bounds is empty and inside-out, we
417 // know there's full coverage everywhere. With a shader, there's *unknown* coverage
418 // everywhere.
419 fFiniteBound = SkRectPriv::MakeLargeS32();
420 fFiniteBoundType = kNormal_BoundsType;
421 break;
422 case DeviceSpaceType::kEmpty:
423 SkDEBUGFAIL("We shouldn't get here with an empty element.");
424 break;
425 }
426
427 // Now determine the previous Element's bound information taking into
428 // account that there may be no previous clip
429 SkRect prevFinite;
430 SkClipStack::BoundsType prevType;
431
432 if (nullptr == prior) {
433 // no prior clip means the entire plane is writable
434 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
435 prevType = kInsideOut_BoundsType;
436 } else {
437 prevFinite = prior->fFiniteBound;
438 prevType = prior->fFiniteBoundType;
439 }
440
441 FillCombo combination = kPrev_Cur_FillCombo;
442 if (kInsideOut_BoundsType == fFiniteBoundType) {
443 combination = (FillCombo) (combination | 0x01);
444 }
445 if (kInsideOut_BoundsType == prevType) {
446 combination = (FillCombo) (combination | 0x02);
447 }
448
449 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
450 kInvPrev_Cur_FillCombo == combination ||
451 kPrev_InvCur_FillCombo == combination ||
452 kPrev_Cur_FillCombo == combination);
453
454 // Now integrate with clip with the prior clips
455 if (!this->isReplaceOp()) {
456 switch (fOp) {
457 case SkClipOp::kDifference:
458 this->combineBoundsDiff(combination, prevFinite);
459 break;
460 case SkClipOp::kIntersect:
461 this->combineBoundsIntersection(combination, prevFinite);
462 break;
463 default:
464 SkDebugf("SkClipOp error\n");
465 SkASSERT(0);
466 break;
467 }
468 } // else Replace just ignores everything prior and should already have filled in bounds.
469 }
470
471 // This constant determines how many Element's are allocated together as a block in
472 // the deque. As such it needs to balance allocating too much memory vs.
473 // incurring allocation/deallocation thrashing. It should roughly correspond to
474 // the deepest save/restore stack we expect to see.
475 static const int kDefaultElementAllocCnt = 8;
476
477 SkClipStack::SkClipStack()
478 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
479 , fSaveCount(0) {
480 }
481
482 SkClipStack::SkClipStack(void* storage, size_t size)
483 : fDeque(sizeof(Element), storage, size, kDefaultElementAllocCnt)
484 , fSaveCount(0) {
485 }
486
487 SkClipStack::SkClipStack(const SkClipStack& b)
488 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
489 *this = b;
490 }
491
492 SkClipStack::~SkClipStack() {
493 reset();
494 }
495
496 SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
497 if (this == &b) {
498 return *this;
499 }
500 reset();
501
502 fSaveCount = b.fSaveCount;
503 SkDeque::F2BIter recIter(b.fDeque);
504 for (const Element* element = (const Element*)recIter.next();
505 element != nullptr;
506 element = (const Element*)recIter.next()) {
507 new (fDeque.push_back()) Element(*element);
508 }
509
510 return *this;
511 }
512
513 bool SkClipStack::operator==(const SkClipStack& b) const {
514 if (this->getTopmostGenID() == b.getTopmostGenID()) {
515 return true;
516 }
517 if (fSaveCount != b.fSaveCount ||
518 fDeque.count() != b.fDeque.count()) {
519 return false;
520 }
521 SkDeque::F2BIter myIter(fDeque);
522 SkDeque::F2BIter bIter(b.fDeque);
523 const Element* myElement = (const Element*)myIter.next();
524 const Element* bElement = (const Element*)bIter.next();
525
526 while (myElement != nullptr && bElement != nullptr) {
527 if (*myElement != *bElement) {
528 return false;
529 }
530 myElement = (const Element*)myIter.next();
531 bElement = (const Element*)bIter.next();
532 }
533 return myElement == nullptr && bElement == nullptr;
534 }
535
536 void SkClipStack::reset() {
537 // We used a placement new for each object in fDeque, so we're responsible
538 // for calling the destructor on each of them as well.
539 while (!fDeque.empty()) {
540 Element* element = (Element*)fDeque.back();
541 element->~Element();
542 fDeque.pop_back();
543 }
544
545 fSaveCount = 0;
546 }
547
548 void SkClipStack::save() {
549 fSaveCount += 1;
550 }
551
552 void SkClipStack::restore() {
553 fSaveCount -= 1;
554 restoreTo(fSaveCount);
555 }
556
557 void SkClipStack::restoreTo(int saveCount) {
558 while (!fDeque.empty()) {
559 Element* element = (Element*)fDeque.back();
560 if (element->fSaveCount <= saveCount) {
561 break;
562 }
563 element->~Element();
564 fDeque.pop_back();
565 }
566 }
567
568 SkRect SkClipStack::bounds(const SkIRect& deviceBounds) const {
569 // TODO: optimize this.
570 SkRect r;
571 SkClipStack::BoundsType bounds;
572 this->getBounds(&r, &bounds);
573 if (bounds == SkClipStack::kInsideOut_BoundsType) {
574 return SkRect::Make(deviceBounds);
575 }
576 return r.intersect(SkRect::Make(deviceBounds)) ? r : SkRect::MakeEmpty();
577 }
578
579 // TODO: optimize this.
580 bool SkClipStack::isEmpty(const SkIRect& r) const { return this->bounds(r).isEmpty(); }
581
582 void SkClipStack::getBounds(SkRect* canvFiniteBound,
583 BoundsType* boundType,
584 bool* isIntersectionOfRects) const {
585 SkASSERT(canvFiniteBound && boundType);
586
587 const Element* element = (const Element*)fDeque.back();
588
589 if (nullptr == element) {
590 // the clip is wide open - the infinite plane w/ no pixels un-writeable
591 canvFiniteBound->setEmpty();
592 *boundType = kInsideOut_BoundsType;
593 if (isIntersectionOfRects) {
594 *isIntersectionOfRects = false;
595 }
596 return;
597 }
598
599 *canvFiniteBound = element->fFiniteBound;
600 *boundType = element->fFiniteBoundType;
601 if (isIntersectionOfRects) {
602 *isIntersectionOfRects = element->fIsIntersectionOfRects;
603 }
604 }
605
606 bool SkClipStack::internalQuickContains(const SkRect& rect) const {
607 Iter iter(*this, Iter::kTop_IterStart);
608 const Element* element = iter.prev();
609 while (element != nullptr) {
610 // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference.
611 if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) {
612 return false;
613 }
614 if (element->isInverseFilled()) {
615 // Part of 'rect' could be trimmed off by the inverse-filled clip element
616 if (SkRect::Intersects(element->getBounds(), rect)) {
617 return false;
618 }
619 } else {
620 if (!element->contains(rect)) {
621 return false;
622 }
623 }
624 if (element->isReplaceOp()) {
625 break;
626 }
627 element = iter.prev();
628 }
629 return true;
630 }
631
632 bool SkClipStack::internalQuickContains(const SkRRect& rrect) const {
633 Iter iter(*this, Iter::kTop_IterStart);
634 const Element* element = iter.prev();
635 while (element != nullptr) {
636 // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference.
637 if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) {
638 return false;
639 }
640 if (element->isInverseFilled()) {
641 // Part of 'rrect' could be trimmed off by the inverse-filled clip element
642 if (SkRect::Intersects(element->getBounds(), rrect.getBounds())) {
643 return false;
644 }
645 } else {
646 if (!element->contains(rrect)) {
647 return false;
648 }
649 }
650 if (element->isReplaceOp()) {
651 break;
652 }
653 element = iter.prev();
654 }
655 return true;
656 }
657
658 void SkClipStack::pushElement(const Element& element) {
659 // Use reverse iterator instead of back because Rect path may need previous
660 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
661 Element* prior = (Element*) iter.prev();
662
663 if (prior) {
664 if (element.isReplaceOp()) {
665 this->restoreTo(fSaveCount - 1);
666 prior = (Element*) fDeque.back();
667 } else if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
668 switch (prior->fDeviceSpaceType) {
669 case Element::DeviceSpaceType::kEmpty:
670 SkDEBUGCODE(prior->checkEmpty();)
671 return;
672 case Element::DeviceSpaceType::kShader:
673 if (Element::DeviceSpaceType::kShader == element.getDeviceSpaceType()) {
674 prior->fShader = SkShaders::Blend(SkBlendMode::kSrcIn,
675 element.fShader, prior->fShader);
676 Element* priorPrior = (Element*) iter.prev();
677 prior->updateBoundAndGenID(priorPrior);
678 return;
679 }
680 break;
681 case Element::DeviceSpaceType::kRect:
682 if (Element::DeviceSpaceType::kRect == element.getDeviceSpaceType()) {
683 if (prior->rectRectIntersectAllowed(element.getDeviceSpaceRect(),
684 element.isAA())) {
685 SkRect isectRect;
686 if (!isectRect.intersect(prior->getDeviceSpaceRect(),
687 element.getDeviceSpaceRect())) {
688 prior->setEmpty();
689 return;
690 }
691
692 prior->fDeviceSpaceRRect.setRect(isectRect);
693 prior->fDoAA = element.isAA();
694 Element* priorPrior = (Element*) iter.prev();
695 prior->updateBoundAndGenID(priorPrior);
696 return;
697 }
698 break;
699 }
700 [[fallthrough]];
701 default:
702 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
703 prior->setEmpty();
704 return;
705 }
706 break;
707 }
708 }
709 }
710 Element* newElement = new (fDeque.push_back()) Element(element);
711 newElement->updateBoundAndGenID(prior);
712 }
713
714 void SkClipStack::clipRRect(const SkRRect& rrect, const SkMatrix& matrix, SkClipOp op, bool doAA) {
715 Element element(fSaveCount, rrect, matrix, op, doAA);
716 this->pushElement(element);
717 }
718
719 void SkClipStack::clipRect(const SkRect& rect, const SkMatrix& matrix, SkClipOp op, bool doAA) {
720 Element element(fSaveCount, rect, matrix, op, doAA);
721 this->pushElement(element);
722 }
723
724 void SkClipStack::clipPath(const SkPath& path, const SkMatrix& matrix, SkClipOp op,
725 bool doAA) {
726 Element element(fSaveCount, path, matrix, op, doAA);
727 this->pushElement(element);
728 }
729
730 void SkClipStack::clipShader(sk_sp<SkShader> shader) {
731 Element element(fSaveCount, std::move(shader));
732 this->pushElement(element);
733 }
734
735 void SkClipStack::replaceClip(const SkRect& rect, bool doAA) {
736 Element element(fSaveCount, rect, doAA);
737 this->pushElement(element);
738 }
739
740 void SkClipStack::clipEmpty() {
741 Element* element = (Element*) fDeque.back();
742
743 if (element && element->canBeIntersectedInPlace(fSaveCount, SkClipOp::kIntersect)) {
744 element->setEmpty();
745 }
746 new (fDeque.push_back()) Element(fSaveCount);
747
748 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
749 }
750
751 ///////////////////////////////////////////////////////////////////////////////
752
753 SkClipStack::Iter::Iter() : fStack(nullptr) {
754 }
755
756 SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
757 : fStack(&stack) {
758 this->reset(stack, startLoc);
759 }
760
761 const SkClipStack::Element* SkClipStack::Iter::next() {
762 return (const SkClipStack::Element*)fIter.next();
763 }
764
765 const SkClipStack::Element* SkClipStack::Iter::prev() {
766 return (const SkClipStack::Element*)fIter.prev();
767 }
768
769 const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkClipOp op) {
770 if (nullptr == fStack) {
771 return nullptr;
772 }
773
774 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
775
776 const SkClipStack::Element* element = nullptr;
777
778 for (element = (const SkClipStack::Element*) fIter.prev();
779 element;
780 element = (const SkClipStack::Element*) fIter.prev()) {
781
782 if (op == element->fOp) {
783 // The Deque's iterator is actually one pace ahead of the
784 // returned value. So while "element" is the element we want to
785 // return, the iterator is actually pointing at (and will
786 // return on the next "next" or "prev" call) the element
787 // in front of it in the deque. Bump the iterator forward a
788 // step so we get the expected result.
789 if (nullptr == fIter.next()) {
790 // The reverse iterator has run off the front of the deque
791 // (i.e., the "op" clip is the first clip) and can't
792 // recover. Reset the iterator to start at the front.
793 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
794 }
795 break;
796 }
797 }
798
799 if (nullptr == element) {
800 // There were no "op" clips
801 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
802 }
803
804 return this->next();
805 }
806
807 void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
808 fStack = &stack;
809 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
810 }
811
812 // helper method
813 void SkClipStack::getConservativeBounds(int offsetX,
814 int offsetY,
815 int maxWidth,
816 int maxHeight,
817 SkRect* devBounds,
818 bool* isIntersectionOfRects) const {
819 SkASSERT(devBounds);
820
821 devBounds->setLTRB(0, 0,
822 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
823
824 SkRect temp;
825 SkClipStack::BoundsType boundType;
826
827 // temp starts off in canvas space here
828 this->getBounds(&temp, &boundType, isIntersectionOfRects);
829 if (SkClipStack::kInsideOut_BoundsType == boundType) {
830 return;
831 }
832
833 // but is converted to device space here
834 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
835
836 if (!devBounds->intersect(temp)) {
837 devBounds->setEmpty();
838 }
839 }
840
841 bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const {
842 const Element* back = static_cast<const Element*>(fDeque.back());
843 if (!back) {
844 // TODO: return bounds?
845 return false;
846 }
847 // First check if the entire stack is known to be a rect by the top element.
848 if (back->fIsIntersectionOfRects && back->fFiniteBoundType == BoundsType::kNormal_BoundsType) {
849 rrect->setRect(back->fFiniteBound);
850 *aa = back->isAA();
851 return true;
852 }
853
854 if (back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRect &&
855 back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRRect) {
856 return false;
857 }
858 if (back->isReplaceOp()) {
859 *rrect = back->asDeviceSpaceRRect();
860 *aa = back->isAA();
861 return true;
862 }
863
864 if (back->getOp() == SkClipOp::kIntersect) {
865 SkRect backBounds;
866 if (!backBounds.intersect(bounds, back->asDeviceSpaceRRect().rect())) {
867 return false;
868 }
869 // We limit to 17 elements. This means the back element will be bounds checked at most 16
870 // times if it is an rrect.
871 int cnt = fDeque.count();
872 if (cnt > 17) {
873 return false;
874 }
875 if (cnt > 1) {
876 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
877 SkAssertResult(static_cast<const Element*>(iter.prev()) == back);
878 while (const Element* prior = (const Element*)iter.prev()) {
879 // TODO: Once expanding clip ops are removed, this is equiv. to op == kDifference
880 if ((prior->getOp() != SkClipOp::kIntersect && !prior->isReplaceOp()) ||
881 !prior->contains(backBounds)) {
882 return false;
883 }
884 if (prior->isReplaceOp()) {
885 break;
886 }
887 }
888 }
889 *rrect = back->asDeviceSpaceRRect();
890 *aa = back->isAA();
891 return true;
892 }
893 return false;
894 }
895
896 uint32_t SkClipStack::GetNextGenID() {
897 // 0-2 are reserved for invalid, empty & wide-open
898 static const uint32_t kFirstUnreservedGenID = 3;
899 static std::atomic<uint32_t> nextID{kFirstUnreservedGenID};
900
901 uint32_t id;
902 do {
903 id = nextID.fetch_add(1, std::memory_order_relaxed);
904 } while (id < kFirstUnreservedGenID);
905 return id;
906 }
907
908 uint32_t SkClipStack::getTopmostGenID() const {
909 if (fDeque.empty()) {
910 return kWideOpenGenID;
911 }
912
913 const Element* back = static_cast<const Element*>(fDeque.back());
914 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty() &&
915 Element::DeviceSpaceType::kShader != back->fDeviceSpaceType) {
916 return kWideOpenGenID;
917 }
918
919 return back->getGenID();
920 }
921
922 #ifdef SK_DEBUG
923 void SkClipStack::Element::dump() const {
924 static const char* kTypeStrings[] = {
925 "empty",
926 "rect",
927 "rrect",
928 "path",
929 "shader"
930 };
931 static_assert(0 == static_cast<int>(DeviceSpaceType::kEmpty), "enum mismatch");
932 static_assert(1 == static_cast<int>(DeviceSpaceType::kRect), "enum mismatch");
933 static_assert(2 == static_cast<int>(DeviceSpaceType::kRRect), "enum mismatch");
934 static_assert(3 == static_cast<int>(DeviceSpaceType::kPath), "enum mismatch");
935 static_assert(4 == static_cast<int>(DeviceSpaceType::kShader), "enum mismatch");
936 static_assert(std::size(kTypeStrings) == kTypeCnt, "enum mismatch");
937
938 const char* opName = this->isReplaceOp() ? "replace" :
939 (fOp == SkClipOp::kDifference ? "difference" : "intersect");
940 SkDebugf("Type: %s, Op: %s, AA: %s, Save Count: %d\n", kTypeStrings[(int)fDeviceSpaceType],
941 opName, (fDoAA ? "yes" : "no"), fSaveCount);
942 switch (fDeviceSpaceType) {
943 case DeviceSpaceType::kEmpty:
944 SkDebugf("\n");
945 break;
946 case DeviceSpaceType::kRect:
947 this->getDeviceSpaceRect().dump();
948 SkDebugf("\n");
949 break;
950 case DeviceSpaceType::kRRect:
951 this->getDeviceSpaceRRect().dump();
952 SkDebugf("\n");
953 break;
954 case DeviceSpaceType::kPath:
955 this->getDeviceSpacePath().dump(nullptr, false);
956 break;
957 case DeviceSpaceType::kShader:
958 // SkShaders don't provide much introspection that's worth while.
959 break;
960 }
961 }
962
963 void SkClipStack::dump() const {
964 B2TIter iter(*this);
965 const Element* e;
966 while ((e = iter.next())) {
967 e->dump();
968 SkDebugf("\n");
969 }
970 }
971 #endif