blob: 7e67ddf2b40fe0fc2566bb1a2413f0f9b70fab29 [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:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000035 return this->getRect() == element.getRect();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000036 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();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000048 fPath.addRect(this->getRect());
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();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000094 path->addRect(this->getRect());
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000095 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;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000111 fRRect.setEmpty();
112 fPath.reset();
113 fGenID = kEmptyGenID;
114 SkDEBUGCODE(this->checkEmpty();)
115}
116
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000117void SkClipStack::Element::checkEmpty() const {
118 SkASSERT(fFiniteBound.isEmpty());
119 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
120 SkASSERT(!fIsIntersectionOfRects);
121 SkASSERT(kEmptyGenID == fGenID);
122 SkASSERT(fPath.isEmpty());
123}
reed@google.com5c3d1472011-02-22 19:12:23 +0000124
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000125bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
126 if (kEmpty_Type == fType &&
127 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
128 return true;
129 }
130 // Only clips within the same save/restore frame (as captured by
131 // the save count) can be merged
132 return fSaveCount == saveCount &&
133 SkRegion::kIntersect_Op == op &&
134 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
135}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000136
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000137bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
138 SkASSERT(kRect_Type == fType);
139
140 if (fDoAA == newAA) {
141 // if the AA setting is the same there is no issue
142 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000143 }
144
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000145 if (!SkRect::Intersects(this->getRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000146 // The calling code will correctly set the result to the empty clip
147 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000148 }
149
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000150 if (this->getRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000151 // if the new rect carves out a portion of the old one there is no
152 // issue
153 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000154 }
155
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000156 // So either the two overlap in some complex manner or newR contains oldR.
157 // In the first, case the edges will require different AA. In the second,
158 // the AA setting that would be carried forward is incorrect (e.g., oldR
159 // is AA while newR is BW but since newR contains oldR, oldR will be
160 // drawn BW) since the new AA setting will predominate.
161 return false;
162}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000163
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000164// a mirror of combineBoundsRevDiff
165void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
166 switch (combination) {
167 case kInvPrev_InvCur_FillCombo:
168 // In this case the only pixels that can remain set
169 // are inside the current clip rect since the extensions
170 // to infinity of both clips cancel out and whatever
171 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000172 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000173 break;
174 case kInvPrev_Cur_FillCombo:
175 // In this case the current op is finite so the only pixels
176 // that aren't set are whatever isn't set in the previous
177 // clip and whatever this clip carves out
178 fFiniteBound.join(prevFinite);
179 fFiniteBoundType = kInsideOut_BoundsType;
180 break;
181 case kPrev_InvCur_FillCombo:
182 // In this case everything outside of this clip's bound
183 // is erased, so the only pixels that can remain set
184 // occur w/in the intersection of the two finite bounds
185 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000186 this->setEmpty();
187 } else {
188 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000189 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000190 break;
191 case kPrev_Cur_FillCombo:
192 // The most conservative result bound is that of the
193 // prior clip. This could be wildly incorrect if the
194 // second clip either exactly matches the first clip
195 // (which should yield the empty set) or reduces the
196 // size of the prior bound (e.g., if the second clip
197 // exactly matched the bottom half of the prior clip).
198 // We ignore these two possibilities.
199 fFiniteBound = prevFinite;
200 break;
201 default:
202 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
203 break;
204 }
205}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000206
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000207void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000209 switch (combination) {
210 case kInvPrev_Cur_FillCombo: // fall through
211 case kPrev_InvCur_FillCombo:
212 // With only one of the clips inverted the result will always
213 // extend to infinity. The only pixels that may be un-writeable
214 // lie within the union of the two finite bounds
215 fFiniteBound.join(prevFinite);
216 fFiniteBoundType = kInsideOut_BoundsType;
217 break;
218 case kInvPrev_InvCur_FillCombo:
219 // The only pixels that can survive are within the
220 // union of the two bounding boxes since the extensions
221 // to infinity of both clips cancel out
222 // fall through!
223 case kPrev_Cur_FillCombo:
224 // The most conservative bound for xor is the
225 // union of the two bounds. If the two clips exactly overlapped
226 // the xor could yield the empty set. Similarly the xor
227 // could reduce the size of the original clip's bound (e.g.,
228 // if the second clip exactly matched the bottom half of the
229 // first clip). We ignore these two cases.
230 fFiniteBound.join(prevFinite);
231 fFiniteBoundType = kNormal_BoundsType;
232 break;
233 default:
234 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
235 break;
236 }
237}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000238
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000239// a mirror of combineBoundsIntersection
240void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
241
242 switch (combination) {
243 case kInvPrev_InvCur_FillCombo:
244 if (!fFiniteBound.intersect(prevFinite)) {
245 fFiniteBound.setEmpty();
246 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000247 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000248 fFiniteBoundType = kInsideOut_BoundsType;
249 break;
250 case kInvPrev_Cur_FillCombo:
251 // The only pixels that won't be drawable are inside
252 // the prior clip's finite bound
253 fFiniteBound = prevFinite;
254 fFiniteBoundType = kInsideOut_BoundsType;
255 break;
256 case kPrev_InvCur_FillCombo:
257 // The only pixels that won't be drawable are inside
258 // this clip's finite bound
259 break;
260 case kPrev_Cur_FillCombo:
261 fFiniteBound.join(prevFinite);
262 break;
263 default:
264 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
265 break;
266 }
267}
268
269// a mirror of combineBoundsUnion
270void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
271
272 switch (combination) {
273 case kInvPrev_InvCur_FillCombo:
274 // The only pixels that aren't writable in this case
275 // occur in the union of the two finite bounds
276 fFiniteBound.join(prevFinite);
277 fFiniteBoundType = kInsideOut_BoundsType;
278 break;
279 case kInvPrev_Cur_FillCombo:
280 // In this case the only pixels that will remain writeable
281 // are within the current clip
282 break;
283 case kPrev_InvCur_FillCombo:
284 // In this case the only pixels that will remain writeable
285 // are with the previous clip
286 fFiniteBound = prevFinite;
287 fFiniteBoundType = kNormal_BoundsType;
288 break;
289 case kPrev_Cur_FillCombo:
290 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000291 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000292 }
293 break;
294 default:
295 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
296 break;
297 }
298}
299
300// a mirror of combineBoundsDiff
301void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
302
303 switch (combination) {
304 case kInvPrev_InvCur_FillCombo:
305 // The only pixels that can survive are in the
306 // previous bound since the extensions to infinity in
307 // both clips cancel out
308 fFiniteBound = prevFinite;
309 fFiniteBoundType = kNormal_BoundsType;
310 break;
311 case kInvPrev_Cur_FillCombo:
312 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000313 this->setEmpty();
314 } else {
315 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000316 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000317 break;
318 case kPrev_InvCur_FillCombo:
319 fFiniteBound.join(prevFinite);
320 fFiniteBoundType = kInsideOut_BoundsType;
321 break;
322 case kPrev_Cur_FillCombo:
323 // Fall through - as with the kDifference_Op case, the
324 // most conservative result bound is the bound of the
325 // current clip. The prior clip could reduce the size of this
326 // bound (as in the kDifference_Op case) but we are ignoring
327 // those cases.
328 break;
329 default:
330 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
331 break;
332 }
333}
334
335void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
336 // We set this first here but we may overwrite it later if we determine that the clip is
337 // either wide-open or empty.
338 fGenID = GetNextGenID();
339
340 // First, optimistically update the current Element's bound information
341 // with the current clip's bound
342 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000343 switch (fType) {
344 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000345 fFiniteBound = this->getRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000346 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000347
348 if (SkRegion::kReplace_Op == fOp ||
349 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
350 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000351 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000352 fIsIntersectionOfRects = true;
353 }
354 break;
355 case kRRect_Type:
356 fFiniteBound = fRRect.getBounds();
357 fFiniteBoundType = kNormal_BoundsType;
358 break;
359 case kPath_Type:
360 fFiniteBound = fPath.getBounds();
361
362 if (fPath.isInverseFillType()) {
363 fFiniteBoundType = kInsideOut_BoundsType;
364 } else {
365 fFiniteBoundType = kNormal_BoundsType;
366 }
367 break;
368 case kEmpty_Type:
369 SkDEBUGFAIL("We shouldn't get here with an empty element.");
370 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000371 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000372
373 if (!fDoAA) {
374 // Here we mimic a non-anti-aliased scanline system. If there is
375 // no anti-aliasing we can integerize the bounding box to exclude
376 // fractional parts that won't be rendered.
377 // Note: the left edge is handled slightly differently below. We
378 // are a bit more generous in the rounding since we don't want to
379 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000380 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
381 SkScalarRoundToScalar(fFiniteBound.fTop),
382 SkScalarRoundToScalar(fFiniteBound.fRight),
383 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000384 }
385
386 // Now determine the previous Element's bound information taking into
387 // account that there may be no previous clip
388 SkRect prevFinite;
389 SkClipStack::BoundsType prevType;
390
391 if (NULL == prior) {
392 // no prior clip means the entire plane is writable
393 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
394 prevType = kInsideOut_BoundsType;
395 } else {
396 prevFinite = prior->fFiniteBound;
397 prevType = prior->fFiniteBoundType;
398 }
399
400 FillCombo combination = kPrev_Cur_FillCombo;
401 if (kInsideOut_BoundsType == fFiniteBoundType) {
402 combination = (FillCombo) (combination | 0x01);
403 }
404 if (kInsideOut_BoundsType == prevType) {
405 combination = (FillCombo) (combination | 0x02);
406 }
407
408 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
409 kInvPrev_Cur_FillCombo == combination ||
410 kPrev_InvCur_FillCombo == combination ||
411 kPrev_Cur_FillCombo == combination);
412
413 // Now integrate with clip with the prior clips
414 switch (fOp) {
415 case SkRegion::kDifference_Op:
416 this->combineBoundsDiff(combination, prevFinite);
417 break;
418 case SkRegion::kXOR_Op:
419 this->combineBoundsXOR(combination, prevFinite);
420 break;
421 case SkRegion::kUnion_Op:
422 this->combineBoundsUnion(combination, prevFinite);
423 break;
424 case SkRegion::kIntersect_Op:
425 this->combineBoundsIntersection(combination, prevFinite);
426 break;
427 case SkRegion::kReverseDifference_Op:
428 this->combineBoundsRevDiff(combination, prevFinite);
429 break;
430 case SkRegion::kReplace_Op:
431 // Replace just ignores everything prior
432 // The current clip's bound information is already filled in
433 // so nothing to do
434 break;
435 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000436 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000437 SkASSERT(0);
438 break;
439 }
440}
reed@google.com5c3d1472011-02-22 19:12:23 +0000441
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000442// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000443// the deque. As such it needs to balance allocating too much memory vs.
444// incurring allocation/deallocation thrashing. It should roughly correspond to
445// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000446static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000447
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000448SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000449 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000450 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000451}
452
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000453SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000454 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000455 *this = b;
456}
457
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000458SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000459 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000460 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000461 if (!r.isEmpty()) {
462 this->clipDevRect(r, SkRegion::kReplace_Op, false);
463 }
464}
465
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000466SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000467 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000468 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000469 if (!r.isEmpty()) {
470 SkRect temp;
471 temp.set(r);
472 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
473 }
474}
475
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000476SkClipStack::~SkClipStack() {
477 reset();
478}
479
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000480SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
481 if (this == &b) {
482 return *this;
483 }
484 reset();
485
486 fSaveCount = b.fSaveCount;
487 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000488 for (const Element* element = (const Element*)recIter.next();
489 element != NULL;
490 element = (const Element*)recIter.next()) {
491 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000492 }
493
494 return *this;
495}
496
497bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000498 if (this->getTopmostGenID() == b.getTopmostGenID()) {
499 return true;
500 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000501 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000502 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000503 return false;
504 }
505 SkDeque::F2BIter myIter(fDeque);
506 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000507 const Element* myElement = (const Element*)myIter.next();
508 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000509
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000510 while (myElement != NULL && bElement != NULL) {
511 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000512 return false;
513 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000514 myElement = (const Element*)myIter.next();
515 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000516 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000517 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000518}
519
reed@google.com5c3d1472011-02-22 19:12:23 +0000520void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000521 // We used a placement new for each object in fDeque, so we're responsible
522 // for calling the destructor on each of them as well.
523 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000524 Element* element = (Element*)fDeque.back();
525 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000526 fDeque.pop_back();
527 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000528
529 fSaveCount = 0;
530}
531
532void SkClipStack::save() {
533 fSaveCount += 1;
534}
535
536void SkClipStack::restore() {
537 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000538 restoreTo(fSaveCount);
539}
540
541void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000542 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000543 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000544 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000545 break;
546 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000547 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000548 fDeque.pop_back();
549 }
550}
551
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000552void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000553 BoundsType* boundType,
554 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000555 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000556
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000557 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000558
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000559 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000560 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000561 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000562 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000563 if (NULL != isIntersectionOfRects) {
564 *isIntersectionOfRects = false;
565 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000566 return;
567 }
568
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000569 *canvFiniteBound = element->fFiniteBound;
570 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000571 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000572 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000573 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000574}
575
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000576bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
577 SkASSERT(NULL != rect);
578
579 SkRect bounds;
580 SkClipStack::BoundsType bt;
581 this->getBounds(&bounds, &bt);
582 if (bt == SkClipStack::kInsideOut_BoundsType) {
583 if (bounds.contains(*rect)) {
584 return false;
585 } else {
586 // If rect's x values are both within bound's x range we
587 // could clip here. Same for y. But we don't bother to check.
588 return true;
589 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000590 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000591 return rect->intersect(bounds);
592 }
593}
594
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000595bool SkClipStack::quickContains(const SkRect& rect) const {
596
597 Iter iter(*this, Iter::kTop_IterStart);
598 const Element* element = iter.prev();
599 while (element != NULL) {
600 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
601 return false;
602 if (element->isInverseFilled()) {
603 // Part of 'rect' could be trimmed off by the inverse-filled clip element
604 if (SkRect::Intersects(element->getBounds(), rect)) {
605 return false;
606 }
607 } else {
608 if (!element->contains(rect)) {
609 return false;
610 }
611 }
612 if (SkRegion::kReplace_Op == element->getOp()) {
613 break;
614 }
615 element = iter.prev();
616 }
617 return true;
618}
619
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000620void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000621 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000622 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000623 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000624
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000625 if (NULL != prior) {
626 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
627 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000628 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000629 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000630 return;
631 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000632 if (Element::kRect_Type == element.getType()) {
633 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000634 SkRect isectRect;
635 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000636 prior->setEmpty();
637 return;
638 }
639
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000640 prior->fRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000641 prior->fDoAA = element.isAA();
642 Element* priorPrior = (Element*) iter.prev();
643 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000644 return;
645 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000646 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000647 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000648 // fallthrough
649 default:
650 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
651 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000652 return;
653 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000654 break;
655 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000656 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000657 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000658 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000659 }
660 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000661 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
662 newElement->updateBoundAndGenID(prior);
663}
664
665void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
666 Element element(fSaveCount, rrect, op, doAA);
667 this->pushElement(element);
668}
669
670void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
671 Element element(fSaveCount, rect, op, doAA);
672 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000673}
674
reed@google.com00177082011-10-12 14:34:30 +0000675void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000676 Element element(fSaveCount, path, op, doAA);
677 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000678}
679
reed@google.com0557d9e2012-08-16 15:59:59 +0000680void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000681 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000682
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000683 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000684 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000685 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000686 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000688 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000689}
690
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000691bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000692 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000693}
694
reed@google.com5c3d1472011-02-22 19:12:23 +0000695///////////////////////////////////////////////////////////////////////////////
696
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000697SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000698}
699
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000700SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
701 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000702 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000703}
704
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000705const SkClipStack::Element* SkClipStack::Iter::next() {
706 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000707}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000708
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000709const SkClipStack::Element* SkClipStack::Iter::prev() {
710 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000711}
712
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000713const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000714
715 if (NULL == fStack) {
716 return NULL;
717 }
718
719 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
720
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000721 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000722
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000723 for (element = (const SkClipStack::Element*) fIter.prev();
724 NULL != element;
725 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000726
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000727 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000728 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000729 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000730 // return, the iterator is actually pointing at (and will
731 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000732 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000733 // step so we get the expected result.
734 if (NULL == fIter.next()) {
735 // The reverse iterator has run off the front of the deque
736 // (i.e., the "op" clip is the first clip) and can't
737 // recover. Reset the iterator to start at the front.
738 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
739 }
740 break;
741 }
742 }
743
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000744 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000745 // There were no "op" clips
746 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
747 }
748
749 return this->next();
750}
751
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000752void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000753 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000754 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000755}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000756
757// helper method
758void SkClipStack::getConservativeBounds(int offsetX,
759 int offsetY,
760 int maxWidth,
761 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000762 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000763 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000764 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000765
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000766 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000767 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000768
769 SkRect temp;
770 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000771
robertphillips@google.com7b112892012-07-31 15:18:21 +0000772 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000773 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000774 if (SkClipStack::kInsideOut_BoundsType == boundType) {
775 return;
776 }
777
robertphillips@google.com7b112892012-07-31 15:18:21 +0000778 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000779 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
780
robertphillips@google.com7b112892012-07-31 15:18:21 +0000781 if (!devBounds->intersect(temp)) {
782 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000783 }
784}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000785
robertphillips@google.com46f93502012-08-07 15:38:08 +0000786int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000787 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000788 return sk_atomic_inc(&gGenID);
789}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000790
791int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000792 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000793 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000794 }
795
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000796 const Element* back = static_cast<const Element*>(fDeque.back());
797 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
798 return kWideOpenGenID;
799 }
800
801 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000802}