blob: e5016977c9ed5cfbd43744b35a4db5682d1458d6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.com5c3d1472011-02-22 19:12:23 +00008#include "SkClipStack.h"
9#include "SkPath.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000010#include "SkThread.h"
11
reed@google.com5c3d1472011-02-22 19:12:23 +000012#include <new>
13
robertphillips@google.com607fe072012-07-24 13:54:00 +000014
robertphillips@google.com46f93502012-08-07 15:38:08 +000015// 0-2 are reserved for invalid, empty & wide-open
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000016static const int32_t kFirstUnreservedGenID = 3;
17int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +000018
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000019bool SkClipStack::Element::operator== (const Element& element) const {
20 if (this == &element) {
21 return true;
22 }
23 if (fOp != element.fOp ||
24 fType != element.fType ||
25 fDoAA != element.fDoAA ||
26 fSaveCount != element.fSaveCount) {
27 return false;
28 }
29 switch (fType) {
30 case kPath_Type:
31 return fPath == element.fPath;
32 case kRRect_Type:
33 return fRRect == element.fRRect;
34 case kRect_Type:
35 return fRect == element.fRect;
36 case kEmpty_Type:
37 return true;
38 default:
39 SkDEBUGFAIL("Unexpected type.");
40 return false;
41 }
42}
43
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000044void SkClipStack::Element::invertShapeFillType() {
45 switch (fType) {
46 case kRect_Type:
47 fPath.reset();
48 fPath.addRect(fRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000049 fPath.setFillType(SkPath::kInverseEvenOdd_FillType);
50 fType = kPath_Type;
51 break;
52 case kRRect_Type:
53 fPath.reset();
54 fPath.addRRect(fRRect);
55 fPath.setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000056 fType = kPath_Type;
57 break;
58 case kPath_Type:
59 fPath.toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000060 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000061 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000062 // Should this set to an empty, inverse filled path?
63 break;
64 }
65}
66
67void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
68 bool doAA) {
69 if (!path.isInverseFillType()) {
70 if (SkPath::kNone_PathAsRect != path.asRect()) {
71 this->initRect(saveCount, path.getBounds(), op, doAA);
72 return;
73 }
74 SkRect ovalRect;
75 if (path.isOval(&ovalRect)) {
76 SkRRect rrect;
77 rrect.setOval(ovalRect);
78 this->initRRect(saveCount, rrect, op, doAA);
79 return;
80 }
81 }
82 fPath = path;
83 fType = kPath_Type;
84 this->initCommon(saveCount, op, doAA);
85}
86
87void SkClipStack::Element::asPath(SkPath* path) const {
88 switch (fType) {
89 case kEmpty_Type:
90 path->reset();
91 break;
92 case kRect_Type:
93 path->reset();
94 path->addRect(fRect);
95 break;
96 case kRRect_Type:
97 path->reset();
98 path->addRRect(fRRect);
99 break;
100 case kPath_Type:
101 *path = fPath;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000102 break;
103 }
104}
105
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000106void SkClipStack::Element::setEmpty() {
107 fType = kEmpty_Type;
108 fFiniteBound.setEmpty();
109 fFiniteBoundType = kNormal_BoundsType;
110 fIsIntersectionOfRects = false;
111 fRect.setEmpty();
112 fRRect.setEmpty();
113 fPath.reset();
114 fGenID = kEmptyGenID;
115 SkDEBUGCODE(this->checkEmpty();)
116}
117
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000118void SkClipStack::Element::checkEmpty() const {
119 SkASSERT(fFiniteBound.isEmpty());
120 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
121 SkASSERT(!fIsIntersectionOfRects);
122 SkASSERT(kEmptyGenID == fGenID);
123 SkASSERT(fPath.isEmpty());
124}
reed@google.com5c3d1472011-02-22 19:12:23 +0000125
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000126bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
127 if (kEmpty_Type == fType &&
128 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
129 return true;
130 }
131 // Only clips within the same save/restore frame (as captured by
132 // the save count) can be merged
133 return fSaveCount == saveCount &&
134 SkRegion::kIntersect_Op == op &&
135 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
136}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000137
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000138bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
139 SkASSERT(kRect_Type == fType);
140
141 if (fDoAA == newAA) {
142 // if the AA setting is the same there is no issue
143 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000144 }
145
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000146 if (!SkRect::Intersects(fRect, newR)) {
147 // The calling code will correctly set the result to the empty clip
148 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000149 }
150
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000151 if (fRect.contains(newR)) {
152 // if the new rect carves out a portion of the old one there is no
153 // issue
154 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000155 }
156
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000157 // So either the two overlap in some complex manner or newR contains oldR.
158 // In the first, case the edges will require different AA. In the second,
159 // the AA setting that would be carried forward is incorrect (e.g., oldR
160 // is AA while newR is BW but since newR contains oldR, oldR will be
161 // drawn BW) since the new AA setting will predominate.
162 return false;
163}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000164
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000165// a mirror of combineBoundsRevDiff
166void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
167 switch (combination) {
168 case kInvPrev_InvCur_FillCombo:
169 // In this case the only pixels that can remain set
170 // are inside the current clip rect since the extensions
171 // to infinity of both clips cancel out and whatever
172 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000173 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000174 break;
175 case kInvPrev_Cur_FillCombo:
176 // In this case the current op is finite so the only pixels
177 // that aren't set are whatever isn't set in the previous
178 // clip and whatever this clip carves out
179 fFiniteBound.join(prevFinite);
180 fFiniteBoundType = kInsideOut_BoundsType;
181 break;
182 case kPrev_InvCur_FillCombo:
183 // In this case everything outside of this clip's bound
184 // is erased, so the only pixels that can remain set
185 // occur w/in the intersection of the two finite bounds
186 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000187 this->setEmpty();
188 } else {
189 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000190 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000191 break;
192 case kPrev_Cur_FillCombo:
193 // The most conservative result bound is that of the
194 // prior clip. This could be wildly incorrect if the
195 // second clip either exactly matches the first clip
196 // (which should yield the empty set) or reduces the
197 // size of the prior bound (e.g., if the second clip
198 // exactly matched the bottom half of the prior clip).
199 // We ignore these two possibilities.
200 fFiniteBound = prevFinite;
201 break;
202 default:
203 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
204 break;
205 }
206}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000207
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000208void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000210 switch (combination) {
211 case kInvPrev_Cur_FillCombo: // fall through
212 case kPrev_InvCur_FillCombo:
213 // With only one of the clips inverted the result will always
214 // extend to infinity. The only pixels that may be un-writeable
215 // lie within the union of the two finite bounds
216 fFiniteBound.join(prevFinite);
217 fFiniteBoundType = kInsideOut_BoundsType;
218 break;
219 case kInvPrev_InvCur_FillCombo:
220 // The only pixels that can survive are within the
221 // union of the two bounding boxes since the extensions
222 // to infinity of both clips cancel out
223 // fall through!
224 case kPrev_Cur_FillCombo:
225 // The most conservative bound for xor is the
226 // union of the two bounds. If the two clips exactly overlapped
227 // the xor could yield the empty set. Similarly the xor
228 // could reduce the size of the original clip's bound (e.g.,
229 // if the second clip exactly matched the bottom half of the
230 // first clip). We ignore these two cases.
231 fFiniteBound.join(prevFinite);
232 fFiniteBoundType = kNormal_BoundsType;
233 break;
234 default:
235 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
236 break;
237 }
238}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000239
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000240// a mirror of combineBoundsIntersection
241void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
242
243 switch (combination) {
244 case kInvPrev_InvCur_FillCombo:
245 if (!fFiniteBound.intersect(prevFinite)) {
246 fFiniteBound.setEmpty();
247 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000248 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000249 fFiniteBoundType = kInsideOut_BoundsType;
250 break;
251 case kInvPrev_Cur_FillCombo:
252 // The only pixels that won't be drawable are inside
253 // the prior clip's finite bound
254 fFiniteBound = prevFinite;
255 fFiniteBoundType = kInsideOut_BoundsType;
256 break;
257 case kPrev_InvCur_FillCombo:
258 // The only pixels that won't be drawable are inside
259 // this clip's finite bound
260 break;
261 case kPrev_Cur_FillCombo:
262 fFiniteBound.join(prevFinite);
263 break;
264 default:
265 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
266 break;
267 }
268}
269
270// a mirror of combineBoundsUnion
271void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
272
273 switch (combination) {
274 case kInvPrev_InvCur_FillCombo:
275 // The only pixels that aren't writable in this case
276 // occur in the union of the two finite bounds
277 fFiniteBound.join(prevFinite);
278 fFiniteBoundType = kInsideOut_BoundsType;
279 break;
280 case kInvPrev_Cur_FillCombo:
281 // In this case the only pixels that will remain writeable
282 // are within the current clip
283 break;
284 case kPrev_InvCur_FillCombo:
285 // In this case the only pixels that will remain writeable
286 // are with the previous clip
287 fFiniteBound = prevFinite;
288 fFiniteBoundType = kNormal_BoundsType;
289 break;
290 case kPrev_Cur_FillCombo:
291 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000292 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000293 }
294 break;
295 default:
296 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
297 break;
298 }
299}
300
301// a mirror of combineBoundsDiff
302void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
303
304 switch (combination) {
305 case kInvPrev_InvCur_FillCombo:
306 // The only pixels that can survive are in the
307 // previous bound since the extensions to infinity in
308 // both clips cancel out
309 fFiniteBound = prevFinite;
310 fFiniteBoundType = kNormal_BoundsType;
311 break;
312 case kInvPrev_Cur_FillCombo:
313 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000314 this->setEmpty();
315 } else {
316 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000317 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000318 break;
319 case kPrev_InvCur_FillCombo:
320 fFiniteBound.join(prevFinite);
321 fFiniteBoundType = kInsideOut_BoundsType;
322 break;
323 case kPrev_Cur_FillCombo:
324 // Fall through - as with the kDifference_Op case, the
325 // most conservative result bound is the bound of the
326 // current clip. The prior clip could reduce the size of this
327 // bound (as in the kDifference_Op case) but we are ignoring
328 // those cases.
329 break;
330 default:
331 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
332 break;
333 }
334}
335
336void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
337 // We set this first here but we may overwrite it later if we determine that the clip is
338 // either wide-open or empty.
339 fGenID = GetNextGenID();
340
341 // First, optimistically update the current Element's bound information
342 // with the current clip's bound
343 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000344 switch (fType) {
345 case kRect_Type:
346 fFiniteBound = fRect;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000347 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000348
349 if (SkRegion::kReplace_Op == fOp ||
350 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
351 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
352 prior->rectRectIntersectAllowed(fRect, fDoAA))) {
353 fIsIntersectionOfRects = true;
354 }
355 break;
356 case kRRect_Type:
357 fFiniteBound = fRRect.getBounds();
358 fFiniteBoundType = kNormal_BoundsType;
359 break;
360 case kPath_Type:
361 fFiniteBound = fPath.getBounds();
362
363 if (fPath.isInverseFillType()) {
364 fFiniteBoundType = kInsideOut_BoundsType;
365 } else {
366 fFiniteBoundType = kNormal_BoundsType;
367 }
368 break;
369 case kEmpty_Type:
370 SkDEBUGFAIL("We shouldn't get here with an empty element.");
371 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000372 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000373
374 if (!fDoAA) {
375 // Here we mimic a non-anti-aliased scanline system. If there is
376 // no anti-aliasing we can integerize the bounding box to exclude
377 // fractional parts that won't be rendered.
378 // Note: the left edge is handled slightly differently below. We
379 // are a bit more generous in the rounding since we don't want to
380 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000381 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
382 SkScalarRoundToScalar(fFiniteBound.fTop),
383 SkScalarRoundToScalar(fFiniteBound.fRight),
384 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000385 }
386
387 // Now determine the previous Element's bound information taking into
388 // account that there may be no previous clip
389 SkRect prevFinite;
390 SkClipStack::BoundsType prevType;
391
392 if (NULL == prior) {
393 // no prior clip means the entire plane is writable
394 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
395 prevType = kInsideOut_BoundsType;
396 } else {
397 prevFinite = prior->fFiniteBound;
398 prevType = prior->fFiniteBoundType;
399 }
400
401 FillCombo combination = kPrev_Cur_FillCombo;
402 if (kInsideOut_BoundsType == fFiniteBoundType) {
403 combination = (FillCombo) (combination | 0x01);
404 }
405 if (kInsideOut_BoundsType == prevType) {
406 combination = (FillCombo) (combination | 0x02);
407 }
408
409 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
410 kInvPrev_Cur_FillCombo == combination ||
411 kPrev_InvCur_FillCombo == combination ||
412 kPrev_Cur_FillCombo == combination);
413
414 // Now integrate with clip with the prior clips
415 switch (fOp) {
416 case SkRegion::kDifference_Op:
417 this->combineBoundsDiff(combination, prevFinite);
418 break;
419 case SkRegion::kXOR_Op:
420 this->combineBoundsXOR(combination, prevFinite);
421 break;
422 case SkRegion::kUnion_Op:
423 this->combineBoundsUnion(combination, prevFinite);
424 break;
425 case SkRegion::kIntersect_Op:
426 this->combineBoundsIntersection(combination, prevFinite);
427 break;
428 case SkRegion::kReverseDifference_Op:
429 this->combineBoundsRevDiff(combination, prevFinite);
430 break;
431 case SkRegion::kReplace_Op:
432 // Replace just ignores everything prior
433 // The current clip's bound information is already filled in
434 // so nothing to do
435 break;
436 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000437 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000438 SkASSERT(0);
439 break;
440 }
441}
reed@google.com5c3d1472011-02-22 19:12:23 +0000442
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000443// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000444// the deque. As such it needs to balance allocating too much memory vs.
445// incurring allocation/deallocation thrashing. It should roughly correspond to
446// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000447static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000448
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000449SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000450 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000451 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000452}
453
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000454SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000455 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000456 *this = b;
457}
458
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000459SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000460 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000461 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000462 if (!r.isEmpty()) {
463 this->clipDevRect(r, SkRegion::kReplace_Op, false);
464 }
465}
466
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000467SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000468 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000469 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000470 if (!r.isEmpty()) {
471 SkRect temp;
472 temp.set(r);
473 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
474 }
475}
476
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000477SkClipStack::~SkClipStack() {
478 reset();
479}
480
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000481SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
482 if (this == &b) {
483 return *this;
484 }
485 reset();
486
487 fSaveCount = b.fSaveCount;
488 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000489 for (const Element* element = (const Element*)recIter.next();
490 element != NULL;
491 element = (const Element*)recIter.next()) {
492 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000493 }
494
495 return *this;
496}
497
498bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000499 if (this->getTopmostGenID() == b.getTopmostGenID()) {
500 return true;
501 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000502 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000503 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000504 return false;
505 }
506 SkDeque::F2BIter myIter(fDeque);
507 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000508 const Element* myElement = (const Element*)myIter.next();
509 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000510
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000511 while (myElement != NULL && bElement != NULL) {
512 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000513 return false;
514 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000515 myElement = (const Element*)myIter.next();
516 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000517 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000518 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000519}
520
reed@google.com5c3d1472011-02-22 19:12:23 +0000521void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000522 // We used a placement new for each object in fDeque, so we're responsible
523 // for calling the destructor on each of them as well.
524 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000525 Element* element = (Element*)fDeque.back();
526 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000527 fDeque.pop_back();
528 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000529
530 fSaveCount = 0;
531}
532
533void SkClipStack::save() {
534 fSaveCount += 1;
535}
536
537void SkClipStack::restore() {
538 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000539 restoreTo(fSaveCount);
540}
541
542void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000543 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000544 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000545 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000546 break;
547 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000548 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000549 fDeque.pop_back();
550 }
551}
552
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000553void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000554 BoundsType* boundType,
555 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000556 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000557
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000558 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000559
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000560 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000561 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000562 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000563 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000564 if (NULL != isIntersectionOfRects) {
565 *isIntersectionOfRects = false;
566 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000567 return;
568 }
569
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000570 *canvFiniteBound = element->fFiniteBound;
571 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000572 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000573 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000574 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000575}
576
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000577bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
578 SkASSERT(NULL != rect);
579
580 SkRect bounds;
581 SkClipStack::BoundsType bt;
582 this->getBounds(&bounds, &bt);
583 if (bt == SkClipStack::kInsideOut_BoundsType) {
584 if (bounds.contains(*rect)) {
585 return false;
586 } else {
587 // If rect's x values are both within bound's x range we
588 // could clip here. Same for y. But we don't bother to check.
589 return true;
590 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000591 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000592 return rect->intersect(bounds);
593 }
594}
595
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000596bool SkClipStack::quickContains(const SkRect& rect) const {
597
598 Iter iter(*this, Iter::kTop_IterStart);
599 const Element* element = iter.prev();
600 while (element != NULL) {
601 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
602 return false;
603 if (element->isInverseFilled()) {
604 // Part of 'rect' could be trimmed off by the inverse-filled clip element
605 if (SkRect::Intersects(element->getBounds(), rect)) {
606 return false;
607 }
608 } else {
609 if (!element->contains(rect)) {
610 return false;
611 }
612 }
613 if (SkRegion::kReplace_Op == element->getOp()) {
614 break;
615 }
616 element = iter.prev();
617 }
618 return true;
619}
620
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000621void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000622 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000623 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000624 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000625
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000626 if (NULL != prior) {
627 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
628 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000629 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000630 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000631 return;
632 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000633 if (Element::kRect_Type == element.getType()) {
634 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
635 if (!prior->fRect.intersect(element.getRect())) {
636 prior->setEmpty();
637 return;
638 }
639
640 prior->fDoAA = element.isAA();
641 Element* priorPrior = (Element*) iter.prev();
642 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000643 return;
644 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000645 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000646 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000647 // fallthrough
648 default:
649 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
650 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000651 return;
652 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000653 break;
654 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000655 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000656 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000657 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000658 }
659 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000660 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
661 newElement->updateBoundAndGenID(prior);
662}
663
664void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
665 Element element(fSaveCount, rrect, op, doAA);
666 this->pushElement(element);
667}
668
669void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
670 Element element(fSaveCount, rect, op, doAA);
671 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000672}
673
reed@google.com00177082011-10-12 14:34:30 +0000674void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000675 Element element(fSaveCount, path, op, doAA);
676 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000677}
678
reed@google.com0557d9e2012-08-16 15:59:59 +0000679void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000680 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000681
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000682 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000683 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000684 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000685 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000687 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000688}
689
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000690bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000691 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000692}
693
reed@google.com5c3d1472011-02-22 19:12:23 +0000694///////////////////////////////////////////////////////////////////////////////
695
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000696SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000697}
698
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000699SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
700 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000701 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000702}
703
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000704const SkClipStack::Element* SkClipStack::Iter::next() {
705 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000706}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000707
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000708const SkClipStack::Element* SkClipStack::Iter::prev() {
709 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000710}
711
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000712const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000713
714 if (NULL == fStack) {
715 return NULL;
716 }
717
718 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
719
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000720 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000721
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000722 for (element = (const SkClipStack::Element*) fIter.prev();
723 NULL != element;
724 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000725
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000726 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000727 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000728 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000729 // return, the iterator is actually pointing at (and will
730 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000731 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000732 // step so we get the expected result.
733 if (NULL == fIter.next()) {
734 // The reverse iterator has run off the front of the deque
735 // (i.e., the "op" clip is the first clip) and can't
736 // recover. Reset the iterator to start at the front.
737 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
738 }
739 break;
740 }
741 }
742
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000743 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000744 // There were no "op" clips
745 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
746 }
747
748 return this->next();
749}
750
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000751void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000752 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000753 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000754}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000755
756// helper method
757void SkClipStack::getConservativeBounds(int offsetX,
758 int offsetY,
759 int maxWidth,
760 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000761 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000762 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000763 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000764
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000765 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000766 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000767
768 SkRect temp;
769 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000770
robertphillips@google.com7b112892012-07-31 15:18:21 +0000771 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000772 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000773 if (SkClipStack::kInsideOut_BoundsType == boundType) {
774 return;
775 }
776
robertphillips@google.com7b112892012-07-31 15:18:21 +0000777 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000778 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
779
robertphillips@google.com7b112892012-07-31 15:18:21 +0000780 if (!devBounds->intersect(temp)) {
781 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000782 }
783}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000784
robertphillips@google.com46f93502012-08-07 15:38:08 +0000785int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000786 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000787 return sk_atomic_inc(&gGenID);
788}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000789
790int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000791 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000792 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000793 }
794
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000795 const Element* back = static_cast<const Element*>(fDeque.back());
796 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
797 return kWideOpenGenID;
798 }
799
800 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000801}