blob: 4227eeac68fc8a7b2101422c084966db8414a0d9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
fmalitac3b589a2014-06-05 12:40:07 -07007
herbb906daf2015-09-29 09:37:59 -07008#include "SkAtomics.h"
fmalitac3b589a2014-06-05 12:40:07 -07009#include "SkCanvas.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000010#include "SkClipStack.h"
11#include "SkPath.h"
fmalita1a481fe2015-02-04 07:39:34 -080012#include "SkPathOps.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000013
reed@google.com5c3d1472011-02-22 19:12:23 +000014#include <new>
15
robertphillips@google.com607fe072012-07-24 13:54:00 +000016
robertphillips@google.com46f93502012-08-07 15:38:08 +000017// 0-2 are reserved for invalid, empty & wide-open
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000018static const int32_t kFirstUnreservedGenID = 3;
19int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +000020
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000021SkClipStack::Element::Element(const Element& that) {
22 switch (that.getType()) {
23 case kEmpty_Type:
24 fPath.reset();
25 break;
26 case kRect_Type: // Rect uses rrect
27 case kRRect_Type:
28 fPath.reset();
29 fRRect = that.fRRect;
30 break;
31 case kPath_Type:
32 fPath.set(that.getPath());
33 break;
34 }
35
36 fSaveCount = that.fSaveCount;
37 fOp = that.fOp;
38 fType = that.fType;
39 fDoAA = that.fDoAA;
40 fFiniteBoundType = that.fFiniteBoundType;
41 fFiniteBound = that.fFiniteBound;
42 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
43 fGenID = that.fGenID;
44}
45
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000046bool SkClipStack::Element::operator== (const Element& element) const {
47 if (this == &element) {
48 return true;
49 }
50 if (fOp != element.fOp ||
51 fType != element.fType ||
52 fDoAA != element.fDoAA ||
53 fSaveCount != element.fSaveCount) {
54 return false;
55 }
56 switch (fType) {
57 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000058 return this->getPath() == element.getPath();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000059 case kRRect_Type:
60 return fRRect == element.fRRect;
61 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000062 return this->getRect() == element.getRect();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000063 case kEmpty_Type:
64 return true;
65 default:
66 SkDEBUGFAIL("Unexpected type.");
67 return false;
68 }
69}
70
fmalitac3b589a2014-06-05 12:40:07 -070071void SkClipStack::Element::replay(SkCanvasClipVisitor* visitor) const {
72 static const SkRect kEmptyRect = { 0, 0, 0, 0 };
73
74 switch (fType) {
75 case kPath_Type:
76 visitor->clipPath(this->getPath(), this->getOp(), this->isAA());
77 break;
78 case kRRect_Type:
79 visitor->clipRRect(this->getRRect(), this->getOp(), this->isAA());
80 break;
81 case kRect_Type:
82 visitor->clipRect(this->getRect(), this->getOp(), this->isAA());
83 break;
84 case kEmpty_Type:
85 visitor->clipRect(kEmptyRect, SkRegion::kIntersect_Op, false);
86 break;
87 }
88}
89
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000090void SkClipStack::Element::invertShapeFillType() {
91 switch (fType) {
92 case kRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000093 fPath.init();
94 fPath.get()->addRect(this->getRect());
95 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000096 fType = kPath_Type;
97 break;
98 case kRRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000099 fPath.init();
100 fPath.get()->addRRect(fRRect);
101 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000102 fType = kPath_Type;
103 break;
104 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000105 fPath.get()->toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000106 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000107 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000108 // Should this set to an empty, inverse filled path?
109 break;
110 }
111}
112
113void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
114 bool doAA) {
115 if (!path.isInverseFillType()) {
robertphillips2b6ab612015-01-05 12:22:14 -0800116 SkRect r;
117 if (path.isRect(&r)) {
118 this->initRect(saveCount, r, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000119 return;
120 }
121 SkRect ovalRect;
122 if (path.isOval(&ovalRect)) {
123 SkRRect rrect;
124 rrect.setOval(ovalRect);
125 this->initRRect(saveCount, rrect, op, doAA);
126 return;
127 }
128 }
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000129 fPath.set(path);
jvanverth0deb2d92014-10-24 12:41:32 -0700130 fPath.get()->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000131 fType = kPath_Type;
132 this->initCommon(saveCount, op, doAA);
133}
134
135void SkClipStack::Element::asPath(SkPath* path) const {
136 switch (fType) {
137 case kEmpty_Type:
138 path->reset();
robertphillips4d8762f8b2015-10-26 14:37:03 -0700139 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000140 break;
141 case kRect_Type:
142 path->reset();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000143 path->addRect(this->getRect());
robertphillips4d8762f8b2015-10-26 14:37:03 -0700144 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000145 break;
146 case kRRect_Type:
147 path->reset();
148 path->addRRect(fRRect);
robertphillips4d8762f8b2015-10-26 14:37:03 -0700149 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000150 break;
151 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000152 *path = *fPath.get();
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000153 break;
154 }
jvanverth0deb2d92014-10-24 12:41:32 -0700155 path->setIsVolatile(true);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000156}
157
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000158void SkClipStack::Element::setEmpty() {
159 fType = kEmpty_Type;
160 fFiniteBound.setEmpty();
161 fFiniteBoundType = kNormal_BoundsType;
162 fIsIntersectionOfRects = false;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000163 fRRect.setEmpty();
164 fPath.reset();
165 fGenID = kEmptyGenID;
166 SkDEBUGCODE(this->checkEmpty();)
167}
168
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000169void SkClipStack::Element::checkEmpty() const {
170 SkASSERT(fFiniteBound.isEmpty());
171 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
172 SkASSERT(!fIsIntersectionOfRects);
173 SkASSERT(kEmptyGenID == fGenID);
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000174 SkASSERT(!fPath.isValid());
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000175}
reed@google.com5c3d1472011-02-22 19:12:23 +0000176
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000177bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
178 if (kEmpty_Type == fType &&
179 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
180 return true;
181 }
182 // Only clips within the same save/restore frame (as captured by
183 // the save count) can be merged
184 return fSaveCount == saveCount &&
185 SkRegion::kIntersect_Op == op &&
186 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
187}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000188
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000189bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
190 SkASSERT(kRect_Type == fType);
191
192 if (fDoAA == newAA) {
193 // if the AA setting is the same there is no issue
194 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000195 }
196
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000197 if (!SkRect::Intersects(this->getRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000198 // The calling code will correctly set the result to the empty clip
199 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000200 }
201
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000202 if (this->getRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000203 // if the new rect carves out a portion of the old one there is no
204 // issue
205 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000206 }
207
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000208 // So either the two overlap in some complex manner or newR contains oldR.
209 // In the first, case the edges will require different AA. In the second,
210 // the AA setting that would be carried forward is incorrect (e.g., oldR
211 // is AA while newR is BW but since newR contains oldR, oldR will be
212 // drawn BW) since the new AA setting will predominate.
213 return false;
214}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000215
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000216// a mirror of combineBoundsRevDiff
217void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
218 switch (combination) {
219 case kInvPrev_InvCur_FillCombo:
220 // In this case the only pixels that can remain set
221 // are inside the current clip rect since the extensions
222 // to infinity of both clips cancel out and whatever
223 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000224 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000225 break;
226 case kInvPrev_Cur_FillCombo:
227 // In this case the current op is finite so the only pixels
228 // that aren't set are whatever isn't set in the previous
229 // clip and whatever this clip carves out
230 fFiniteBound.join(prevFinite);
231 fFiniteBoundType = kInsideOut_BoundsType;
232 break;
233 case kPrev_InvCur_FillCombo:
234 // In this case everything outside of this clip's bound
235 // is erased, so the only pixels that can remain set
236 // occur w/in the intersection of the two finite bounds
237 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000238 this->setEmpty();
239 } else {
240 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000241 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000242 break;
243 case kPrev_Cur_FillCombo:
244 // The most conservative result bound is that of the
245 // prior clip. This could be wildly incorrect if the
246 // second clip either exactly matches the first clip
247 // (which should yield the empty set) or reduces the
248 // size of the prior bound (e.g., if the second clip
249 // exactly matched the bottom half of the prior clip).
250 // We ignore these two possibilities.
251 fFiniteBound = prevFinite;
252 break;
253 default:
254 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
255 break;
256 }
257}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000258
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000259void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000261 switch (combination) {
262 case kInvPrev_Cur_FillCombo: // fall through
263 case kPrev_InvCur_FillCombo:
264 // With only one of the clips inverted the result will always
265 // extend to infinity. The only pixels that may be un-writeable
266 // lie within the union of the two finite bounds
267 fFiniteBound.join(prevFinite);
268 fFiniteBoundType = kInsideOut_BoundsType;
269 break;
270 case kInvPrev_InvCur_FillCombo:
271 // The only pixels that can survive are within the
272 // union of the two bounding boxes since the extensions
273 // to infinity of both clips cancel out
274 // fall through!
275 case kPrev_Cur_FillCombo:
276 // The most conservative bound for xor is the
277 // union of the two bounds. If the two clips exactly overlapped
278 // the xor could yield the empty set. Similarly the xor
279 // could reduce the size of the original clip's bound (e.g.,
280 // if the second clip exactly matched the bottom half of the
281 // first clip). We ignore these two cases.
282 fFiniteBound.join(prevFinite);
283 fFiniteBoundType = kNormal_BoundsType;
284 break;
285 default:
286 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
287 break;
288 }
289}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000290
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000291// a mirror of combineBoundsIntersection
292void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
293
294 switch (combination) {
295 case kInvPrev_InvCur_FillCombo:
296 if (!fFiniteBound.intersect(prevFinite)) {
297 fFiniteBound.setEmpty();
298 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000299 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000300 fFiniteBoundType = kInsideOut_BoundsType;
301 break;
302 case kInvPrev_Cur_FillCombo:
303 // The only pixels that won't be drawable are inside
304 // the prior clip's finite bound
305 fFiniteBound = prevFinite;
306 fFiniteBoundType = kInsideOut_BoundsType;
307 break;
308 case kPrev_InvCur_FillCombo:
309 // The only pixels that won't be drawable are inside
310 // this clip's finite bound
311 break;
312 case kPrev_Cur_FillCombo:
313 fFiniteBound.join(prevFinite);
314 break;
315 default:
316 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
317 break;
318 }
319}
320
321// a mirror of combineBoundsUnion
322void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
323
324 switch (combination) {
325 case kInvPrev_InvCur_FillCombo:
326 // The only pixels that aren't writable in this case
327 // occur in the union of the two finite bounds
328 fFiniteBound.join(prevFinite);
329 fFiniteBoundType = kInsideOut_BoundsType;
330 break;
331 case kInvPrev_Cur_FillCombo:
332 // In this case the only pixels that will remain writeable
333 // are within the current clip
334 break;
335 case kPrev_InvCur_FillCombo:
336 // In this case the only pixels that will remain writeable
337 // are with the previous clip
338 fFiniteBound = prevFinite;
339 fFiniteBoundType = kNormal_BoundsType;
340 break;
341 case kPrev_Cur_FillCombo:
342 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000343 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000344 }
345 break;
346 default:
347 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
348 break;
349 }
350}
351
352// a mirror of combineBoundsDiff
353void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
354
355 switch (combination) {
356 case kInvPrev_InvCur_FillCombo:
357 // The only pixels that can survive are in the
358 // previous bound since the extensions to infinity in
359 // both clips cancel out
360 fFiniteBound = prevFinite;
361 fFiniteBoundType = kNormal_BoundsType;
362 break;
363 case kInvPrev_Cur_FillCombo:
364 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000365 this->setEmpty();
366 } else {
367 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000368 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000369 break;
370 case kPrev_InvCur_FillCombo:
371 fFiniteBound.join(prevFinite);
372 fFiniteBoundType = kInsideOut_BoundsType;
373 break;
374 case kPrev_Cur_FillCombo:
375 // Fall through - as with the kDifference_Op case, the
376 // most conservative result bound is the bound of the
377 // current clip. The prior clip could reduce the size of this
378 // bound (as in the kDifference_Op case) but we are ignoring
379 // those cases.
380 break;
381 default:
382 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
383 break;
384 }
385}
386
387void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
388 // We set this first here but we may overwrite it later if we determine that the clip is
389 // either wide-open or empty.
390 fGenID = GetNextGenID();
391
392 // First, optimistically update the current Element's bound information
393 // with the current clip's bound
394 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000395 switch (fType) {
396 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000397 fFiniteBound = this->getRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000398 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000399
400 if (SkRegion::kReplace_Op == fOp ||
halcanary96fcdcc2015-08-27 07:41:13 -0700401 (SkRegion::kIntersect_Op == fOp && nullptr == prior) ||
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000402 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000403 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000404 fIsIntersectionOfRects = true;
405 }
406 break;
407 case kRRect_Type:
408 fFiniteBound = fRRect.getBounds();
409 fFiniteBoundType = kNormal_BoundsType;
410 break;
411 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000412 fFiniteBound = fPath.get()->getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000413
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000414 if (fPath.get()->isInverseFillType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000415 fFiniteBoundType = kInsideOut_BoundsType;
416 } else {
417 fFiniteBoundType = kNormal_BoundsType;
418 }
419 break;
420 case kEmpty_Type:
421 SkDEBUGFAIL("We shouldn't get here with an empty element.");
422 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000423 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000424
robertphillipsff420212015-03-30 08:09:58 -0700425 if (!fDoAA) {
426 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
427 SkScalarRoundToScalar(fFiniteBound.fTop),
428 SkScalarRoundToScalar(fFiniteBound.fRight),
429 SkScalarRoundToScalar(fFiniteBound.fBottom));
430 }
431
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000432 // Now determine the previous Element's bound information taking into
433 // account that there may be no previous clip
434 SkRect prevFinite;
435 SkClipStack::BoundsType prevType;
436
halcanary96fcdcc2015-08-27 07:41:13 -0700437 if (nullptr == prior) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000438 // no prior clip means the entire plane is writable
439 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
440 prevType = kInsideOut_BoundsType;
441 } else {
442 prevFinite = prior->fFiniteBound;
443 prevType = prior->fFiniteBoundType;
444 }
445
446 FillCombo combination = kPrev_Cur_FillCombo;
447 if (kInsideOut_BoundsType == fFiniteBoundType) {
448 combination = (FillCombo) (combination | 0x01);
449 }
450 if (kInsideOut_BoundsType == prevType) {
451 combination = (FillCombo) (combination | 0x02);
452 }
453
454 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
455 kInvPrev_Cur_FillCombo == combination ||
456 kPrev_InvCur_FillCombo == combination ||
457 kPrev_Cur_FillCombo == combination);
458
459 // Now integrate with clip with the prior clips
460 switch (fOp) {
461 case SkRegion::kDifference_Op:
462 this->combineBoundsDiff(combination, prevFinite);
463 break;
464 case SkRegion::kXOR_Op:
465 this->combineBoundsXOR(combination, prevFinite);
466 break;
467 case SkRegion::kUnion_Op:
468 this->combineBoundsUnion(combination, prevFinite);
469 break;
470 case SkRegion::kIntersect_Op:
471 this->combineBoundsIntersection(combination, prevFinite);
472 break;
473 case SkRegion::kReverseDifference_Op:
474 this->combineBoundsRevDiff(combination, prevFinite);
475 break;
476 case SkRegion::kReplace_Op:
477 // Replace just ignores everything prior
478 // The current clip's bound information is already filled in
479 // so nothing to do
480 break;
481 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000482 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000483 SkASSERT(0);
484 break;
485 }
486}
reed@google.com5c3d1472011-02-22 19:12:23 +0000487
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000488// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000489// the deque. As such it needs to balance allocating too much memory vs.
490// incurring allocation/deallocation thrashing. It should roughly correspond to
491// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000492static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000493
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000494SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000495 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000496 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000497}
498
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000499SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000500 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000501 *this = b;
502}
503
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000504SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000505 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000506 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000507 if (!r.isEmpty()) {
508 this->clipDevRect(r, SkRegion::kReplace_Op, false);
509 }
510}
511
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000512SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000513 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000514 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000515 if (!r.isEmpty()) {
516 SkRect temp;
517 temp.set(r);
518 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
519 }
520}
521
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000522SkClipStack::~SkClipStack() {
523 reset();
524}
525
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000526SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
527 if (this == &b) {
528 return *this;
529 }
530 reset();
531
532 fSaveCount = b.fSaveCount;
533 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000534 for (const Element* element = (const Element*)recIter.next();
halcanary96fcdcc2015-08-27 07:41:13 -0700535 element != nullptr;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000536 element = (const Element*)recIter.next()) {
537 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000538 }
539
540 return *this;
541}
542
543bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000544 if (this->getTopmostGenID() == b.getTopmostGenID()) {
545 return true;
546 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000547 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000548 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000549 return false;
550 }
551 SkDeque::F2BIter myIter(fDeque);
552 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000553 const Element* myElement = (const Element*)myIter.next();
554 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000555
halcanary96fcdcc2015-08-27 07:41:13 -0700556 while (myElement != nullptr && bElement != nullptr) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000557 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000558 return false;
559 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000560 myElement = (const Element*)myIter.next();
561 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000562 }
halcanary96fcdcc2015-08-27 07:41:13 -0700563 return myElement == nullptr && bElement == nullptr;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000564}
565
reed@google.com5c3d1472011-02-22 19:12:23 +0000566void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000567 // We used a placement new for each object in fDeque, so we're responsible
568 // for calling the destructor on each of them as well.
569 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000570 Element* element = (Element*)fDeque.back();
571 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000572 fDeque.pop_back();
573 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000574
575 fSaveCount = 0;
576}
577
578void SkClipStack::save() {
579 fSaveCount += 1;
580}
581
582void SkClipStack::restore() {
583 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000584 restoreTo(fSaveCount);
585}
586
587void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000588 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000589 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000590 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000591 break;
592 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000593 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000594 fDeque.pop_back();
595 }
596}
597
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000598void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000599 BoundsType* boundType,
600 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700601 SkASSERT(canvFiniteBound && boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000602
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000603 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000604
halcanary96fcdcc2015-08-27 07:41:13 -0700605 if (nullptr == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000606 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000607 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000608 *boundType = kInsideOut_BoundsType;
bsalomon49f085d2014-09-05 13:34:00 -0700609 if (isIntersectionOfRects) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000610 *isIntersectionOfRects = false;
611 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000612 return;
613 }
614
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000615 *canvFiniteBound = element->fFiniteBound;
616 *boundType = element->fFiniteBoundType;
bsalomon49f085d2014-09-05 13:34:00 -0700617 if (isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000618 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000619 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000620}
621
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000622bool SkClipStack::quickContains(const SkRect& rect) const {
623
624 Iter iter(*this, Iter::kTop_IterStart);
625 const Element* element = iter.prev();
halcanary96fcdcc2015-08-27 07:41:13 -0700626 while (element != nullptr) {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000627 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
628 return false;
629 if (element->isInverseFilled()) {
630 // Part of 'rect' could be trimmed off by the inverse-filled clip element
631 if (SkRect::Intersects(element->getBounds(), rect)) {
632 return false;
633 }
634 } else {
635 if (!element->contains(rect)) {
636 return false;
637 }
638 }
639 if (SkRegion::kReplace_Op == element->getOp()) {
640 break;
641 }
642 element = iter.prev();
643 }
644 return true;
645}
646
fmalita1a481fe2015-02-04 07:39:34 -0800647bool SkClipStack::asPath(SkPath *path) const {
648 bool isAA = false;
649
650 path->reset();
651 path->setFillType(SkPath::kInverseEvenOdd_FillType);
652
653 SkClipStack::Iter iter(*this, SkClipStack::Iter::kBottom_IterStart);
654 while (const SkClipStack::Element* element = iter.next()) {
655 SkPath operand;
656 if (element->getType() != SkClipStack::Element::kEmpty_Type) {
657 element->asPath(&operand);
658 }
659
660 SkRegion::Op elementOp = element->getOp();
661 if (elementOp == SkRegion::kReplace_Op) {
662 *path = operand;
663 } else {
664 Op(*path, operand, (SkPathOp)elementOp, path);
665 }
666
667 // if the prev and curr clips disagree about aa -vs- not, favor the aa request.
668 // perhaps we need an API change to avoid this sort of mixed-signals about
669 // clipping.
670 isAA = (isAA || element->isAA());
671 }
672
673 return isAA;
674}
675
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000676void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000677 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000678 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000679 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000680
bsalomon49f085d2014-09-05 13:34:00 -0700681 if (prior) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000682 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
683 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000684 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000685 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000686 return;
687 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000688 if (Element::kRect_Type == element.getType()) {
689 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000690 SkRect isectRect;
691 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000692 prior->setEmpty();
693 return;
694 }
695
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000696 prior->fRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000697 prior->fDoAA = element.isAA();
698 Element* priorPrior = (Element*) iter.prev();
699 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000700 return;
701 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000702 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000703 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000704 // fallthrough
705 default:
706 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
707 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000708 return;
709 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000710 break;
711 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000712 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000713 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000714 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000715 }
716 }
halcanary385fe4d2015-08-26 13:07:48 -0700717 Element* newElement = new (fDeque.push_back()) Element(element);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000718 newElement->updateBoundAndGenID(prior);
719}
720
721void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
722 Element element(fSaveCount, rrect, op, doAA);
723 this->pushElement(element);
724}
725
726void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
727 Element element(fSaveCount, rect, op, doAA);
728 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000729}
730
reed@google.com00177082011-10-12 14:34:30 +0000731void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000732 Element element(fSaveCount, path, op, doAA);
733 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000734}
735
reed@google.com0557d9e2012-08-16 15:59:59 +0000736void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000737 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000738
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000739 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000740 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000741 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000742 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000743
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000744 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000745}
746
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000747bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000748 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000749}
750
reed@google.com5c3d1472011-02-22 19:12:23 +0000751///////////////////////////////////////////////////////////////////////////////
752
halcanary96fcdcc2015-08-27 07:41:13 -0700753SkClipStack::Iter::Iter() : fStack(nullptr) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000754}
755
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000756SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
757 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000758 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000759}
760
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000761const SkClipStack::Element* SkClipStack::Iter::next() {
762 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000763}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000764
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000765const SkClipStack::Element* SkClipStack::Iter::prev() {
766 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000767}
768
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000769const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000770
halcanary96fcdcc2015-08-27 07:41:13 -0700771 if (nullptr == fStack) {
772 return nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000773 }
774
775 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
776
halcanary96fcdcc2015-08-27 07:41:13 -0700777 const SkClipStack::Element* element = nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000778
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000779 for (element = (const SkClipStack::Element*) fIter.prev();
bsalomon49f085d2014-09-05 13:34:00 -0700780 element;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000781 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000782
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000783 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000784 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000785 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000786 // return, the iterator is actually pointing at (and will
787 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000788 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000789 // step so we get the expected result.
halcanary96fcdcc2015-08-27 07:41:13 -0700790 if (nullptr == fIter.next()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000791 // The reverse iterator has run off the front of the deque
792 // (i.e., the "op" clip is the first clip) and can't
793 // recover. Reset the iterator to start at the front.
794 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
795 }
796 break;
797 }
798 }
799
halcanary96fcdcc2015-08-27 07:41:13 -0700800 if (nullptr == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000801 // There were no "op" clips
802 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
803 }
804
805 return this->next();
806}
807
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000808void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000809 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000810 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000811}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000812
813// helper method
814void SkClipStack::getConservativeBounds(int offsetX,
815 int offsetY,
816 int maxWidth,
817 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000818 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000819 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700820 SkASSERT(devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000821
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000822 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000823 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000824
825 SkRect temp;
826 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000827
robertphillips@google.com7b112892012-07-31 15:18:21 +0000828 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000829 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000830 if (SkClipStack::kInsideOut_BoundsType == boundType) {
831 return;
832 }
833
robertphillips@google.com7b112892012-07-31 15:18:21 +0000834 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000835 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
836
robertphillips@google.com7b112892012-07-31 15:18:21 +0000837 if (!devBounds->intersect(temp)) {
838 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000839 }
840}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000841
robertphillips@google.com46f93502012-08-07 15:38:08 +0000842int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000843 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000844 return sk_atomic_inc(&gGenID);
845}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000846
847int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000848 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000849 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000850 }
851
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000852 const Element* back = static_cast<const Element*>(fDeque.back());
853 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
854 return kWideOpenGenID;
855 }
856
857 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000858}
bsalomonb6b02522014-06-09 07:59:06 -0700859
860#ifdef SK_DEVELOPER
861void SkClipStack::Element::dump() const {
862 static const char* kTypeStrings[] = {
863 "empty",
864 "rect",
865 "rrect",
866 "path"
867 };
bungeman99fe8222015-08-20 07:57:51 -0700868 static_assert(0 == kEmpty_Type, "type_str");
869 static_assert(1 == kRect_Type, "type_str");
870 static_assert(2 == kRRect_Type, "type_str");
871 static_assert(3 == kPath_Type, "type_str");
872 static_assert(SK_ARRAY_COUNT(kTypeStrings) == kTypeCnt, "type_str");
bsalomonb6b02522014-06-09 07:59:06 -0700873
874 static const char* kOpStrings[] = {
875 "difference",
876 "intersect",
877 "union",
878 "xor",
879 "reverse-difference",
880 "replace",
881 };
bungeman99fe8222015-08-20 07:57:51 -0700882 static_assert(0 == SkRegion::kDifference_Op, "op_str");
883 static_assert(1 == SkRegion::kIntersect_Op, "op_str");
884 static_assert(2 == SkRegion::kUnion_Op, "op_str");
885 static_assert(3 == SkRegion::kXOR_Op, "op_str");
886 static_assert(4 == SkRegion::kReverseDifference_Op, "op_str");
887 static_assert(5 == SkRegion::kReplace_Op, "op_str");
888 static_assert(SK_ARRAY_COUNT(kOpStrings) == SkRegion::kOpCnt, "op_str");
bsalomonb6b02522014-06-09 07:59:06 -0700889
890 SkDebugf("Type: %s, Op: %s, AA: %s, Save Count: %d\n", kTypeStrings[fType],
891 kOpStrings[fOp], (fDoAA ? "yes" : "no"), fSaveCount);
892 switch (fType) {
893 case kEmpty_Type:
894 SkDebugf("\n");
895 break;
896 case kRect_Type:
897 this->getRect().dump();
898 SkDebugf("\n");
899 break;
900 case kRRect_Type:
901 this->getRRect().dump();
902 SkDebugf("\n");
903 break;
904 case kPath_Type:
halcanary96fcdcc2015-08-27 07:41:13 -0700905 this->getPath().dump(nullptr, true, false);
bsalomonb6b02522014-06-09 07:59:06 -0700906 break;
907 }
908}
909
910void SkClipStack::dump() const {
911 B2TIter iter(*this);
912 const Element* e;
913 while ((e = iter.next())) {
914 e->dump();
915 SkDebugf("\n");
916 }
917}
918#endif