Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "SkClipStack.h"
9 #include "SkPath.h"
10 #include "SkThread.h"
12 #include <new>
15 // 0-2 are reserved for invalid, empty & wide-open
16 static const int32_t kFirstUnreservedGenID = 3;
17 int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
19 SkClipStack::Element::Element(const Element& that) {
20 switch (that.getType()) {
21 case kEmpty_Type:
22 fPath.reset();
23 break;
24 case kRect_Type: // Rect uses rrect
25 case kRRect_Type:
26 fPath.reset();
27 fRRect = that.fRRect;
28 break;
29 case kPath_Type:
30 fPath.set(that.getPath());
31 break;
32 }
34 fSaveCount = that.fSaveCount;
35 fOp = that.fOp;
36 fType = that.fType;
37 fDoAA = that.fDoAA;
38 fFiniteBoundType = that.fFiniteBoundType;
39 fFiniteBound = that.fFiniteBound;
40 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
41 fGenID = that.fGenID;
42 }
44 bool SkClipStack::Element::operator== (const Element& element) const {
45 if (this == &element) {
46 return true;
47 }
48 if (fOp != element.fOp ||
49 fType != element.fType ||
50 fDoAA != element.fDoAA ||
51 fSaveCount != element.fSaveCount) {
52 return false;
53 }
54 switch (fType) {
55 case kPath_Type:
56 return this->getPath() == element.getPath();
57 case kRRect_Type:
58 return fRRect == element.fRRect;
59 case kRect_Type:
60 return this->getRect() == element.getRect();
61 case kEmpty_Type:
62 return true;
63 default:
64 SkDEBUGFAIL("Unexpected type.");
65 return false;
66 }
67 }
69 void SkClipStack::Element::invertShapeFillType() {
70 switch (fType) {
71 case kRect_Type:
72 fPath.init();
73 fPath.get()->addRect(this->getRect());
74 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
75 fType = kPath_Type;
76 break;
77 case kRRect_Type:
78 fPath.init();
79 fPath.get()->addRRect(fRRect);
80 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
81 fType = kPath_Type;
82 break;
83 case kPath_Type:
84 fPath.get()->toggleInverseFillType();
85 break;
86 case kEmpty_Type:
87 // Should this set to an empty, inverse filled path?
88 break;
89 }
90 }
92 void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
93 bool doAA) {
94 if (!path.isInverseFillType()) {
95 if (SkPath::kNone_PathAsRect != path.asRect()) {
96 this->initRect(saveCount, path.getBounds(), op, doAA);
97 return;
98 }
99 SkRect ovalRect;
100 if (path.isOval(&ovalRect)) {
101 SkRRect rrect;
102 rrect.setOval(ovalRect);
103 this->initRRect(saveCount, rrect, op, doAA);
104 return;
105 }
106 }
107 fPath.set(path);
108 fType = kPath_Type;
109 this->initCommon(saveCount, op, doAA);
110 }
112 void SkClipStack::Element::asPath(SkPath* path) const {
113 switch (fType) {
114 case kEmpty_Type:
115 path->reset();
116 break;
117 case kRect_Type:
118 path->reset();
119 path->addRect(this->getRect());
120 break;
121 case kRRect_Type:
122 path->reset();
123 path->addRRect(fRRect);
124 break;
125 case kPath_Type:
126 *path = *fPath.get();
127 break;
128 }
129 }
131 void SkClipStack::Element::setEmpty() {
132 fType = kEmpty_Type;
133 fFiniteBound.setEmpty();
134 fFiniteBoundType = kNormal_BoundsType;
135 fIsIntersectionOfRects = false;
136 fRRect.setEmpty();
137 fPath.reset();
138 fGenID = kEmptyGenID;
139 SkDEBUGCODE(this->checkEmpty();)
140 }
142 void SkClipStack::Element::checkEmpty() const {
143 SkASSERT(fFiniteBound.isEmpty());
144 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
145 SkASSERT(!fIsIntersectionOfRects);
146 SkASSERT(kEmptyGenID == fGenID);
147 SkASSERT(!fPath.isValid());
148 }
150 bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
151 if (kEmpty_Type == fType &&
152 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
153 return true;
154 }
155 // Only clips within the same save/restore frame (as captured by
156 // the save count) can be merged
157 return fSaveCount == saveCount &&
158 SkRegion::kIntersect_Op == op &&
159 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
160 }
162 bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
163 SkASSERT(kRect_Type == fType);
165 if (fDoAA == newAA) {
166 // if the AA setting is the same there is no issue
167 return true;
168 }
170 if (!SkRect::Intersects(this->getRect(), newR)) {
171 // The calling code will correctly set the result to the empty clip
172 return true;
173 }
175 if (this->getRect().contains(newR)) {
176 // if the new rect carves out a portion of the old one there is no
177 // issue
178 return true;
179 }
181 // So either the two overlap in some complex manner or newR contains oldR.
182 // In the first, case the edges will require different AA. In the second,
183 // the AA setting that would be carried forward is incorrect (e.g., oldR
184 // is AA while newR is BW but since newR contains oldR, oldR will be
185 // drawn BW) since the new AA setting will predominate.
186 return false;
187 }
189 // a mirror of combineBoundsRevDiff
190 void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
191 switch (combination) {
192 case kInvPrev_InvCur_FillCombo:
193 // In this case the only pixels that can remain set
194 // are inside the current clip rect since the extensions
195 // to infinity of both clips cancel out and whatever
196 // is outside of the current clip is removed
197 fFiniteBoundType = kNormal_BoundsType;
198 break;
199 case kInvPrev_Cur_FillCombo:
200 // In this case the current op is finite so the only pixels
201 // that aren't set are whatever isn't set in the previous
202 // clip and whatever this clip carves out
203 fFiniteBound.join(prevFinite);
204 fFiniteBoundType = kInsideOut_BoundsType;
205 break;
206 case kPrev_InvCur_FillCombo:
207 // In this case everything outside of this clip's bound
208 // is erased, so the only pixels that can remain set
209 // occur w/in the intersection of the two finite bounds
210 if (!fFiniteBound.intersect(prevFinite)) {
211 this->setEmpty();
212 } else {
213 fFiniteBoundType = kNormal_BoundsType;
214 }
215 break;
216 case kPrev_Cur_FillCombo:
217 // The most conservative result bound is that of the
218 // prior clip. This could be wildly incorrect if the
219 // second clip either exactly matches the first clip
220 // (which should yield the empty set) or reduces the
221 // size of the prior bound (e.g., if the second clip
222 // exactly matched the bottom half of the prior clip).
223 // We ignore these two possibilities.
224 fFiniteBound = prevFinite;
225 break;
226 default:
227 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
228 break;
229 }
230 }
232 void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
234 switch (combination) {
235 case kInvPrev_Cur_FillCombo: // fall through
236 case kPrev_InvCur_FillCombo:
237 // With only one of the clips inverted the result will always
238 // extend to infinity. The only pixels that may be un-writeable
239 // lie within the union of the two finite bounds
240 fFiniteBound.join(prevFinite);
241 fFiniteBoundType = kInsideOut_BoundsType;
242 break;
243 case kInvPrev_InvCur_FillCombo:
244 // The only pixels that can survive are within the
245 // union of the two bounding boxes since the extensions
246 // to infinity of both clips cancel out
247 // fall through!
248 case kPrev_Cur_FillCombo:
249 // The most conservative bound for xor is the
250 // union of the two bounds. If the two clips exactly overlapped
251 // the xor could yield the empty set. Similarly the xor
252 // could reduce the size of the original clip's bound (e.g.,
253 // if the second clip exactly matched the bottom half of the
254 // first clip). We ignore these two cases.
255 fFiniteBound.join(prevFinite);
256 fFiniteBoundType = kNormal_BoundsType;
257 break;
258 default:
259 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
260 break;
261 }
262 }
264 // a mirror of combineBoundsIntersection
265 void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
267 switch (combination) {
268 case kInvPrev_InvCur_FillCombo:
269 if (!fFiniteBound.intersect(prevFinite)) {
270 fFiniteBound.setEmpty();
271 fGenID = kWideOpenGenID;
272 }
273 fFiniteBoundType = kInsideOut_BoundsType;
274 break;
275 case kInvPrev_Cur_FillCombo:
276 // The only pixels that won't be drawable are inside
277 // the prior clip's finite bound
278 fFiniteBound = prevFinite;
279 fFiniteBoundType = kInsideOut_BoundsType;
280 break;
281 case kPrev_InvCur_FillCombo:
282 // The only pixels that won't be drawable are inside
283 // this clip's finite bound
284 break;
285 case kPrev_Cur_FillCombo:
286 fFiniteBound.join(prevFinite);
287 break;
288 default:
289 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
290 break;
291 }
292 }
294 // a mirror of combineBoundsUnion
295 void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
297 switch (combination) {
298 case kInvPrev_InvCur_FillCombo:
299 // The only pixels that aren't writable in this case
300 // occur in the union of the two finite bounds
301 fFiniteBound.join(prevFinite);
302 fFiniteBoundType = kInsideOut_BoundsType;
303 break;
304 case kInvPrev_Cur_FillCombo:
305 // In this case the only pixels that will remain writeable
306 // are within the current clip
307 break;
308 case kPrev_InvCur_FillCombo:
309 // In this case the only pixels that will remain writeable
310 // are with the previous clip
311 fFiniteBound = prevFinite;
312 fFiniteBoundType = kNormal_BoundsType;
313 break;
314 case kPrev_Cur_FillCombo:
315 if (!fFiniteBound.intersect(prevFinite)) {
316 this->setEmpty();
317 }
318 break;
319 default:
320 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
321 break;
322 }
323 }
325 // a mirror of combineBoundsDiff
326 void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
328 switch (combination) {
329 case kInvPrev_InvCur_FillCombo:
330 // The only pixels that can survive are in the
331 // previous bound since the extensions to infinity in
332 // both clips cancel out
333 fFiniteBound = prevFinite;
334 fFiniteBoundType = kNormal_BoundsType;
335 break;
336 case kInvPrev_Cur_FillCombo:
337 if (!fFiniteBound.intersect(prevFinite)) {
338 this->setEmpty();
339 } else {
340 fFiniteBoundType = kNormal_BoundsType;
341 }
342 break;
343 case kPrev_InvCur_FillCombo:
344 fFiniteBound.join(prevFinite);
345 fFiniteBoundType = kInsideOut_BoundsType;
346 break;
347 case kPrev_Cur_FillCombo:
348 // Fall through - as with the kDifference_Op case, the
349 // most conservative result bound is the bound of the
350 // current clip. The prior clip could reduce the size of this
351 // bound (as in the kDifference_Op case) but we are ignoring
352 // those cases.
353 break;
354 default:
355 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
356 break;
357 }
358 }
360 void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
361 // We set this first here but we may overwrite it later if we determine that the clip is
362 // either wide-open or empty.
363 fGenID = GetNextGenID();
365 // First, optimistically update the current Element's bound information
366 // with the current clip's bound
367 fIsIntersectionOfRects = false;
368 switch (fType) {
369 case kRect_Type:
370 fFiniteBound = this->getRect();
371 fFiniteBoundType = kNormal_BoundsType;
373 if (SkRegion::kReplace_Op == fOp ||
374 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
375 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
376 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
377 fIsIntersectionOfRects = true;
378 }
379 break;
380 case kRRect_Type:
381 fFiniteBound = fRRect.getBounds();
382 fFiniteBoundType = kNormal_BoundsType;
383 break;
384 case kPath_Type:
385 fFiniteBound = fPath.get()->getBounds();
387 if (fPath.get()->isInverseFillType()) {
388 fFiniteBoundType = kInsideOut_BoundsType;
389 } else {
390 fFiniteBoundType = kNormal_BoundsType;
391 }
392 break;
393 case kEmpty_Type:
394 SkDEBUGFAIL("We shouldn't get here with an empty element.");
395 break;
396 }
398 if (!fDoAA) {
399 // Here we mimic a non-anti-aliased scanline system. If there is
400 // no anti-aliasing we can integerize the bounding box to exclude
401 // fractional parts that won't be rendered.
402 // Note: the left edge is handled slightly differently below. We
403 // are a bit more generous in the rounding since we don't want to
404 // risk missing the left pixels when fLeft is very close to .5
405 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
406 SkScalarRoundToScalar(fFiniteBound.fTop),
407 SkScalarRoundToScalar(fFiniteBound.fRight),
408 SkScalarRoundToScalar(fFiniteBound.fBottom));
409 }
411 // Now determine the previous Element's bound information taking into
412 // account that there may be no previous clip
413 SkRect prevFinite;
414 SkClipStack::BoundsType prevType;
416 if (NULL == prior) {
417 // no prior clip means the entire plane is writable
418 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
419 prevType = kInsideOut_BoundsType;
420 } else {
421 prevFinite = prior->fFiniteBound;
422 prevType = prior->fFiniteBoundType;
423 }
425 FillCombo combination = kPrev_Cur_FillCombo;
426 if (kInsideOut_BoundsType == fFiniteBoundType) {
427 combination = (FillCombo) (combination | 0x01);
428 }
429 if (kInsideOut_BoundsType == prevType) {
430 combination = (FillCombo) (combination | 0x02);
431 }
433 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
434 kInvPrev_Cur_FillCombo == combination ||
435 kPrev_InvCur_FillCombo == combination ||
436 kPrev_Cur_FillCombo == combination);
438 // Now integrate with clip with the prior clips
439 switch (fOp) {
440 case SkRegion::kDifference_Op:
441 this->combineBoundsDiff(combination, prevFinite);
442 break;
443 case SkRegion::kXOR_Op:
444 this->combineBoundsXOR(combination, prevFinite);
445 break;
446 case SkRegion::kUnion_Op:
447 this->combineBoundsUnion(combination, prevFinite);
448 break;
449 case SkRegion::kIntersect_Op:
450 this->combineBoundsIntersection(combination, prevFinite);
451 break;
452 case SkRegion::kReverseDifference_Op:
453 this->combineBoundsRevDiff(combination, prevFinite);
454 break;
455 case SkRegion::kReplace_Op:
456 // Replace just ignores everything prior
457 // The current clip's bound information is already filled in
458 // so nothing to do
459 break;
460 default:
461 SkDebugf("SkRegion::Op error\n");
462 SkASSERT(0);
463 break;
464 }
465 }
467 // This constant determines how many Element's are allocated together as a block in
468 // the deque. As such it needs to balance allocating too much memory vs.
469 // incurring allocation/deallocation thrashing. It should roughly correspond to
470 // the deepest save/restore stack we expect to see.
471 static const int kDefaultElementAllocCnt = 8;
473 SkClipStack::SkClipStack()
474 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
475 , fSaveCount(0) {
476 }
478 SkClipStack::SkClipStack(const SkClipStack& b)
479 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
480 *this = b;
481 }
483 SkClipStack::SkClipStack(const SkRect& r)
484 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
485 , fSaveCount(0) {
486 if (!r.isEmpty()) {
487 this->clipDevRect(r, SkRegion::kReplace_Op, false);
488 }
489 }
491 SkClipStack::SkClipStack(const SkIRect& r)
492 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
493 , fSaveCount(0) {
494 if (!r.isEmpty()) {
495 SkRect temp;
496 temp.set(r);
497 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
498 }
499 }
501 SkClipStack::~SkClipStack() {
502 reset();
503 }
505 SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
506 if (this == &b) {
507 return *this;
508 }
509 reset();
511 fSaveCount = b.fSaveCount;
512 SkDeque::F2BIter recIter(b.fDeque);
513 for (const Element* element = (const Element*)recIter.next();
514 element != NULL;
515 element = (const Element*)recIter.next()) {
516 new (fDeque.push_back()) Element(*element);
517 }
519 return *this;
520 }
522 bool SkClipStack::operator==(const SkClipStack& b) const {
523 if (this->getTopmostGenID() == b.getTopmostGenID()) {
524 return true;
525 }
526 if (fSaveCount != b.fSaveCount ||
527 fDeque.count() != b.fDeque.count()) {
528 return false;
529 }
530 SkDeque::F2BIter myIter(fDeque);
531 SkDeque::F2BIter bIter(b.fDeque);
532 const Element* myElement = (const Element*)myIter.next();
533 const Element* bElement = (const Element*)bIter.next();
535 while (myElement != NULL && bElement != NULL) {
536 if (*myElement != *bElement) {
537 return false;
538 }
539 myElement = (const Element*)myIter.next();
540 bElement = (const Element*)bIter.next();
541 }
542 return myElement == NULL && bElement == NULL;
543 }
545 void SkClipStack::reset() {
546 // We used a placement new for each object in fDeque, so we're responsible
547 // for calling the destructor on each of them as well.
548 while (!fDeque.empty()) {
549 Element* element = (Element*)fDeque.back();
550 element->~Element();
551 fDeque.pop_back();
552 }
554 fSaveCount = 0;
555 }
557 void SkClipStack::save() {
558 fSaveCount += 1;
559 }
561 void SkClipStack::restore() {
562 fSaveCount -= 1;
563 restoreTo(fSaveCount);
564 }
566 void SkClipStack::restoreTo(int saveCount) {
567 while (!fDeque.empty()) {
568 Element* element = (Element*)fDeque.back();
569 if (element->fSaveCount <= saveCount) {
570 break;
571 }
572 element->~Element();
573 fDeque.pop_back();
574 }
575 }
577 void SkClipStack::getBounds(SkRect* canvFiniteBound,
578 BoundsType* boundType,
579 bool* isIntersectionOfRects) const {
580 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
582 Element* element = (Element*)fDeque.back();
584 if (NULL == element) {
585 // the clip is wide open - the infinite plane w/ no pixels un-writeable
586 canvFiniteBound->setEmpty();
587 *boundType = kInsideOut_BoundsType;
588 if (NULL != isIntersectionOfRects) {
589 *isIntersectionOfRects = false;
590 }
591 return;
592 }
594 *canvFiniteBound = element->fFiniteBound;
595 *boundType = element->fFiniteBoundType;
596 if (NULL != isIntersectionOfRects) {
597 *isIntersectionOfRects = element->fIsIntersectionOfRects;
598 }
599 }
601 bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
602 SkASSERT(NULL != rect);
604 SkRect bounds;
605 SkClipStack::BoundsType bt;
606 this->getBounds(&bounds, &bt);
607 if (bt == SkClipStack::kInsideOut_BoundsType) {
608 if (bounds.contains(*rect)) {
609 return false;
610 } else {
611 // If rect's x values are both within bound's x range we
612 // could clip here. Same for y. But we don't bother to check.
613 return true;
614 }
615 } else {
616 return rect->intersect(bounds);
617 }
618 }
620 bool SkClipStack::quickContains(const SkRect& rect) const {
622 Iter iter(*this, Iter::kTop_IterStart);
623 const Element* element = iter.prev();
624 while (element != NULL) {
625 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
626 return false;
627 if (element->isInverseFilled()) {
628 // Part of 'rect' could be trimmed off by the inverse-filled clip element
629 if (SkRect::Intersects(element->getBounds(), rect)) {
630 return false;
631 }
632 } else {
633 if (!element->contains(rect)) {
634 return false;
635 }
636 }
637 if (SkRegion::kReplace_Op == element->getOp()) {
638 break;
639 }
640 element = iter.prev();
641 }
642 return true;
643 }
645 void SkClipStack::pushElement(const Element& element) {
646 // Use reverse iterator instead of back because Rect path may need previous
647 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
648 Element* prior = (Element*) iter.prev();
650 if (NULL != prior) {
651 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
652 switch (prior->fType) {
653 case Element::kEmpty_Type:
654 SkDEBUGCODE(prior->checkEmpty();)
655 return;
656 case Element::kRect_Type:
657 if (Element::kRect_Type == element.getType()) {
658 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
659 SkRect isectRect;
660 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
661 prior->setEmpty();
662 return;
663 }
665 prior->fRRect.setRect(isectRect);
666 prior->fDoAA = element.isAA();
667 Element* priorPrior = (Element*) iter.prev();
668 prior->updateBoundAndGenID(priorPrior);
669 return;
670 }
671 break;
672 }
673 // fallthrough
674 default:
675 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
676 prior->setEmpty();
677 return;
678 }
679 break;
680 }
681 } else if (SkRegion::kReplace_Op == element.getOp()) {
682 this->restoreTo(fSaveCount - 1);
683 prior = (Element*) fDeque.back();
684 }
685 }
686 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
687 newElement->updateBoundAndGenID(prior);
688 }
690 void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
691 Element element(fSaveCount, rrect, op, doAA);
692 this->pushElement(element);
693 }
695 void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
696 Element element(fSaveCount, rect, op, doAA);
697 this->pushElement(element);
698 }
700 void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
701 Element element(fSaveCount, path, op, doAA);
702 this->pushElement(element);
703 }
705 void SkClipStack::clipEmpty() {
706 Element* element = (Element*) fDeque.back();
708 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
709 element->setEmpty();
710 }
711 new (fDeque.push_back()) Element(fSaveCount);
713 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
714 }
716 bool SkClipStack::isWideOpen() const {
717 return this->getTopmostGenID() == kWideOpenGenID;
718 }
720 ///////////////////////////////////////////////////////////////////////////////
722 SkClipStack::Iter::Iter() : fStack(NULL) {
723 }
725 SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
726 : fStack(&stack) {
727 this->reset(stack, startLoc);
728 }
730 const SkClipStack::Element* SkClipStack::Iter::next() {
731 return (const SkClipStack::Element*)fIter.next();
732 }
734 const SkClipStack::Element* SkClipStack::Iter::prev() {
735 return (const SkClipStack::Element*)fIter.prev();
736 }
738 const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
740 if (NULL == fStack) {
741 return NULL;
742 }
744 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
746 const SkClipStack::Element* element = NULL;
748 for (element = (const SkClipStack::Element*) fIter.prev();
749 NULL != element;
750 element = (const SkClipStack::Element*) fIter.prev()) {
752 if (op == element->fOp) {
753 // The Deque's iterator is actually one pace ahead of the
754 // returned value. So while "element" is the element we want to
755 // return, the iterator is actually pointing at (and will
756 // return on the next "next" or "prev" call) the element
757 // in front of it in the deque. Bump the iterator forward a
758 // step so we get the expected result.
759 if (NULL == fIter.next()) {
760 // The reverse iterator has run off the front of the deque
761 // (i.e., the "op" clip is the first clip) and can't
762 // recover. Reset the iterator to start at the front.
763 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
764 }
765 break;
766 }
767 }
769 if (NULL == element) {
770 // There were no "op" clips
771 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
772 }
774 return this->next();
775 }
777 void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
778 fStack = &stack;
779 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
780 }
782 // helper method
783 void SkClipStack::getConservativeBounds(int offsetX,
784 int offsetY,
785 int maxWidth,
786 int maxHeight,
787 SkRect* devBounds,
788 bool* isIntersectionOfRects) const {
789 SkASSERT(NULL != devBounds);
791 devBounds->setLTRB(0, 0,
792 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
794 SkRect temp;
795 SkClipStack::BoundsType boundType;
797 // temp starts off in canvas space here
798 this->getBounds(&temp, &boundType, isIntersectionOfRects);
799 if (SkClipStack::kInsideOut_BoundsType == boundType) {
800 return;
801 }
803 // but is converted to device space here
804 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
806 if (!devBounds->intersect(temp)) {
807 devBounds->setEmpty();
808 }
809 }
811 int32_t SkClipStack::GetNextGenID() {
812 // TODO: handle overflow.
813 return sk_atomic_inc(&gGenID);
814 }
816 int32_t SkClipStack::getTopmostGenID() const {
817 if (fDeque.empty()) {
818 return kWideOpenGenID;
819 }
821 const Element* back = static_cast<const Element*>(fDeque.back());
822 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
823 return kWideOpenGenID;
824 }
826 return back->getGenID();
827 }