blob: 0019077804967ddf354fdb4f2f634eee013a99d0 [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:
cdaltonb893a4c2016-03-17 12:56:11 -070024 fRRect.setEmpty();
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000025 fPath.reset();
26 break;
27 case kRect_Type: // Rect uses rrect
28 case kRRect_Type:
29 fPath.reset();
30 fRRect = that.fRRect;
31 break;
32 case kPath_Type:
33 fPath.set(that.getPath());
34 break;
35 }
36
37 fSaveCount = that.fSaveCount;
38 fOp = that.fOp;
39 fType = that.fType;
40 fDoAA = that.fDoAA;
41 fFiniteBoundType = that.fFiniteBoundType;
42 fFiniteBound = that.fFiniteBound;
43 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
44 fGenID = that.fGenID;
45}
46
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000047bool SkClipStack::Element::operator== (const Element& element) const {
48 if (this == &element) {
49 return true;
50 }
51 if (fOp != element.fOp ||
52 fType != element.fType ||
53 fDoAA != element.fDoAA ||
54 fSaveCount != element.fSaveCount) {
55 return false;
56 }
57 switch (fType) {
58 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000059 return this->getPath() == element.getPath();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000060 case kRRect_Type:
61 return fRRect == element.fRRect;
62 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000063 return this->getRect() == element.getRect();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000064 case kEmpty_Type:
65 return true;
66 default:
67 SkDEBUGFAIL("Unexpected type.");
68 return false;
69 }
70}
71
fmalitac3b589a2014-06-05 12:40:07 -070072void SkClipStack::Element::replay(SkCanvasClipVisitor* visitor) const {
73 static const SkRect kEmptyRect = { 0, 0, 0, 0 };
74
75 switch (fType) {
76 case kPath_Type:
77 visitor->clipPath(this->getPath(), this->getOp(), this->isAA());
78 break;
79 case kRRect_Type:
80 visitor->clipRRect(this->getRRect(), this->getOp(), this->isAA());
81 break;
82 case kRect_Type:
83 visitor->clipRect(this->getRect(), this->getOp(), this->isAA());
84 break;
85 case kEmpty_Type:
reed73603f32016-09-20 08:42:38 -070086 visitor->clipRect(kEmptyRect, SkCanvas::kIntersect_Op, false);
fmalitac3b589a2014-06-05 12:40:07 -070087 break;
88 }
89}
90
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000091void SkClipStack::Element::invertShapeFillType() {
92 switch (fType) {
93 case kRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000094 fPath.init();
95 fPath.get()->addRect(this->getRect());
96 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000097 fType = kPath_Type;
98 break;
99 case kRRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000100 fPath.init();
101 fPath.get()->addRRect(fRRect);
102 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000103 fType = kPath_Type;
104 break;
105 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000106 fPath.get()->toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000107 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000108 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000109 // Should this set to an empty, inverse filled path?
110 break;
111 }
112}
113
reed73603f32016-09-20 08:42:38 -0700114void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkCanvas::ClipOp op,
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000115 bool doAA) {
116 if (!path.isInverseFillType()) {
robertphillips2b6ab612015-01-05 12:22:14 -0800117 SkRect r;
118 if (path.isRect(&r)) {
119 this->initRect(saveCount, r, op, doAA);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000120 return;
121 }
122 SkRect ovalRect;
123 if (path.isOval(&ovalRect)) {
124 SkRRect rrect;
125 rrect.setOval(ovalRect);
126 this->initRRect(saveCount, rrect, op, doAA);
127 return;
128 }
129 }
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000130 fPath.set(path);
jvanverth0deb2d92014-10-24 12:41:32 -0700131 fPath.get()->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000132 fType = kPath_Type;
133 this->initCommon(saveCount, op, doAA);
134}
135
136void SkClipStack::Element::asPath(SkPath* path) const {
137 switch (fType) {
138 case kEmpty_Type:
139 path->reset();
robertphillips4d8762f8b2015-10-26 14:37:03 -0700140 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000141 break;
142 case kRect_Type:
143 path->reset();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000144 path->addRect(this->getRect());
robertphillips4d8762f8b2015-10-26 14:37:03 -0700145 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000146 break;
147 case kRRect_Type:
148 path->reset();
149 path->addRRect(fRRect);
robertphillips4d8762f8b2015-10-26 14:37:03 -0700150 path->setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000151 break;
152 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000153 *path = *fPath.get();
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000154 break;
155 }
jvanverth0deb2d92014-10-24 12:41:32 -0700156 path->setIsVolatile(true);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000157}
158
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000159void SkClipStack::Element::setEmpty() {
160 fType = kEmpty_Type;
161 fFiniteBound.setEmpty();
162 fFiniteBoundType = kNormal_BoundsType;
163 fIsIntersectionOfRects = false;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000164 fRRect.setEmpty();
165 fPath.reset();
166 fGenID = kEmptyGenID;
167 SkDEBUGCODE(this->checkEmpty();)
168}
169
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000170void SkClipStack::Element::checkEmpty() const {
171 SkASSERT(fFiniteBound.isEmpty());
172 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
173 SkASSERT(!fIsIntersectionOfRects);
174 SkASSERT(kEmptyGenID == fGenID);
cdaltonb893a4c2016-03-17 12:56:11 -0700175 SkASSERT(fRRect.isEmpty());
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000176 SkASSERT(!fPath.isValid());
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000177}
reed@google.com5c3d1472011-02-22 19:12:23 +0000178
reed73603f32016-09-20 08:42:38 -0700179bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkCanvas::ClipOp op) const {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000180 if (kEmpty_Type == fType &&
reed73603f32016-09-20 08:42:38 -0700181 (SkCanvas::kDifference_Op == op || SkCanvas::kIntersect_Op == op)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000182 return true;
183 }
184 // Only clips within the same save/restore frame (as captured by
185 // the save count) can be merged
186 return fSaveCount == saveCount &&
reed73603f32016-09-20 08:42:38 -0700187 SkCanvas::kIntersect_Op == op &&
188 (SkCanvas::kIntersect_Op == fOp || SkCanvas::kReplace_Op == fOp);
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000189}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000190
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000191bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
192 SkASSERT(kRect_Type == fType);
193
194 if (fDoAA == newAA) {
195 // if the AA setting is the same there is no issue
196 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000197 }
198
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000199 if (!SkRect::Intersects(this->getRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000200 // The calling code will correctly set the result to the empty clip
201 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000202 }
203
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000204 if (this->getRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000205 // if the new rect carves out a portion of the old one there is no
206 // issue
207 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000208 }
209
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000210 // So either the two overlap in some complex manner or newR contains oldR.
211 // In the first, case the edges will require different AA. In the second,
212 // the AA setting that would be carried forward is incorrect (e.g., oldR
213 // is AA while newR is BW but since newR contains oldR, oldR will be
214 // drawn BW) since the new AA setting will predominate.
215 return false;
216}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000217
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000218// a mirror of combineBoundsRevDiff
219void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
220 switch (combination) {
221 case kInvPrev_InvCur_FillCombo:
222 // In this case the only pixels that can remain set
223 // are inside the current clip rect since the extensions
224 // to infinity of both clips cancel out and whatever
225 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000226 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000227 break;
228 case kInvPrev_Cur_FillCombo:
229 // In this case the current op is finite so the only pixels
230 // that aren't set are whatever isn't set in the previous
231 // clip and whatever this clip carves out
232 fFiniteBound.join(prevFinite);
233 fFiniteBoundType = kInsideOut_BoundsType;
234 break;
235 case kPrev_InvCur_FillCombo:
236 // In this case everything outside of this clip's bound
237 // is erased, so the only pixels that can remain set
238 // occur w/in the intersection of the two finite bounds
239 if (!fFiniteBound.intersect(prevFinite)) {
csmartdaltond50e2402016-07-22 08:39:06 -0700240 fFiniteBound.setEmpty();
241 fGenID = kEmptyGenID;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000242 }
csmartdaltond50e2402016-07-22 08:39:06 -0700243 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000244 break;
245 case kPrev_Cur_FillCombo:
246 // The most conservative result bound is that of the
247 // prior clip. This could be wildly incorrect if the
248 // second clip either exactly matches the first clip
249 // (which should yield the empty set) or reduces the
250 // size of the prior bound (e.g., if the second clip
251 // exactly matched the bottom half of the prior clip).
252 // We ignore these two possibilities.
253 fFiniteBound = prevFinite;
254 break;
255 default:
256 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
257 break;
258 }
259}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000260
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000261void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000263 switch (combination) {
264 case kInvPrev_Cur_FillCombo: // fall through
265 case kPrev_InvCur_FillCombo:
266 // With only one of the clips inverted the result will always
267 // extend to infinity. The only pixels that may be un-writeable
268 // lie within the union of the two finite bounds
269 fFiniteBound.join(prevFinite);
270 fFiniteBoundType = kInsideOut_BoundsType;
271 break;
272 case kInvPrev_InvCur_FillCombo:
273 // The only pixels that can survive are within the
274 // union of the two bounding boxes since the extensions
275 // to infinity of both clips cancel out
276 // fall through!
277 case kPrev_Cur_FillCombo:
278 // The most conservative bound for xor is the
279 // union of the two bounds. If the two clips exactly overlapped
280 // the xor could yield the empty set. Similarly the xor
281 // could reduce the size of the original clip's bound (e.g.,
282 // if the second clip exactly matched the bottom half of the
283 // first clip). We ignore these two cases.
284 fFiniteBound.join(prevFinite);
285 fFiniteBoundType = kNormal_BoundsType;
286 break;
287 default:
288 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
289 break;
290 }
291}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000292
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000293// a mirror of combineBoundsIntersection
294void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
295
296 switch (combination) {
297 case kInvPrev_InvCur_FillCombo:
298 if (!fFiniteBound.intersect(prevFinite)) {
299 fFiniteBound.setEmpty();
300 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000301 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000302 fFiniteBoundType = kInsideOut_BoundsType;
303 break;
304 case kInvPrev_Cur_FillCombo:
305 // The only pixels that won't be drawable are inside
306 // the prior clip's finite bound
307 fFiniteBound = prevFinite;
308 fFiniteBoundType = kInsideOut_BoundsType;
309 break;
310 case kPrev_InvCur_FillCombo:
311 // The only pixels that won't be drawable are inside
312 // this clip's finite bound
313 break;
314 case kPrev_Cur_FillCombo:
315 fFiniteBound.join(prevFinite);
316 break;
317 default:
318 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
319 break;
320 }
321}
322
323// a mirror of combineBoundsUnion
324void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
325
326 switch (combination) {
327 case kInvPrev_InvCur_FillCombo:
328 // The only pixels that aren't writable in this case
329 // occur in the union of the two finite bounds
330 fFiniteBound.join(prevFinite);
331 fFiniteBoundType = kInsideOut_BoundsType;
332 break;
333 case kInvPrev_Cur_FillCombo:
334 // In this case the only pixels that will remain writeable
335 // are within the current clip
336 break;
337 case kPrev_InvCur_FillCombo:
338 // In this case the only pixels that will remain writeable
339 // are with the previous clip
340 fFiniteBound = prevFinite;
341 fFiniteBoundType = kNormal_BoundsType;
342 break;
343 case kPrev_Cur_FillCombo:
344 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000345 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000346 }
347 break;
348 default:
349 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
350 break;
351 }
352}
353
354// a mirror of combineBoundsDiff
355void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
356
357 switch (combination) {
358 case kInvPrev_InvCur_FillCombo:
359 // The only pixels that can survive are in the
360 // previous bound since the extensions to infinity in
361 // both clips cancel out
362 fFiniteBound = prevFinite;
363 fFiniteBoundType = kNormal_BoundsType;
364 break;
365 case kInvPrev_Cur_FillCombo:
366 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000367 this->setEmpty();
368 } else {
369 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000370 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000371 break;
372 case kPrev_InvCur_FillCombo:
373 fFiniteBound.join(prevFinite);
374 fFiniteBoundType = kInsideOut_BoundsType;
375 break;
376 case kPrev_Cur_FillCombo:
377 // Fall through - as with the kDifference_Op case, the
378 // most conservative result bound is the bound of the
379 // current clip. The prior clip could reduce the size of this
380 // bound (as in the kDifference_Op case) but we are ignoring
381 // those cases.
382 break;
383 default:
384 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
385 break;
386 }
387}
388
389void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
390 // We set this first here but we may overwrite it later if we determine that the clip is
391 // either wide-open or empty.
392 fGenID = GetNextGenID();
393
394 // First, optimistically update the current Element's bound information
395 // with the current clip's bound
396 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000397 switch (fType) {
398 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000399 fFiniteBound = this->getRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000400 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000401
reed73603f32016-09-20 08:42:38 -0700402 if (SkCanvas::kReplace_Op == fOp ||
403 (SkCanvas::kIntersect_Op == fOp && nullptr == prior) ||
404 (SkCanvas::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000405 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000406 fIsIntersectionOfRects = true;
407 }
408 break;
409 case kRRect_Type:
410 fFiniteBound = fRRect.getBounds();
411 fFiniteBoundType = kNormal_BoundsType;
412 break;
413 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000414 fFiniteBound = fPath.get()->getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000415
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000416 if (fPath.get()->isInverseFillType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000417 fFiniteBoundType = kInsideOut_BoundsType;
418 } else {
419 fFiniteBoundType = kNormal_BoundsType;
420 }
421 break;
422 case kEmpty_Type:
423 SkDEBUGFAIL("We shouldn't get here with an empty element.");
424 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000425 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000426
robertphillipsff420212015-03-30 08:09:58 -0700427 if (!fDoAA) {
428 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
429 SkScalarRoundToScalar(fFiniteBound.fTop),
430 SkScalarRoundToScalar(fFiniteBound.fRight),
431 SkScalarRoundToScalar(fFiniteBound.fBottom));
432 }
433
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000434 // Now determine the previous Element's bound information taking into
435 // account that there may be no previous clip
436 SkRect prevFinite;
437 SkClipStack::BoundsType prevType;
438
halcanary96fcdcc2015-08-27 07:41:13 -0700439 if (nullptr == prior) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000440 // no prior clip means the entire plane is writable
441 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
442 prevType = kInsideOut_BoundsType;
443 } else {
444 prevFinite = prior->fFiniteBound;
445 prevType = prior->fFiniteBoundType;
446 }
447
448 FillCombo combination = kPrev_Cur_FillCombo;
449 if (kInsideOut_BoundsType == fFiniteBoundType) {
450 combination = (FillCombo) (combination | 0x01);
451 }
452 if (kInsideOut_BoundsType == prevType) {
453 combination = (FillCombo) (combination | 0x02);
454 }
455
456 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
457 kInvPrev_Cur_FillCombo == combination ||
458 kPrev_InvCur_FillCombo == combination ||
459 kPrev_Cur_FillCombo == combination);
460
461 // Now integrate with clip with the prior clips
462 switch (fOp) {
reed73603f32016-09-20 08:42:38 -0700463 case SkCanvas::kDifference_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000464 this->combineBoundsDiff(combination, prevFinite);
465 break;
reed73603f32016-09-20 08:42:38 -0700466 case SkCanvas::kXOR_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000467 this->combineBoundsXOR(combination, prevFinite);
468 break;
reed73603f32016-09-20 08:42:38 -0700469 case SkCanvas::kUnion_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000470 this->combineBoundsUnion(combination, prevFinite);
471 break;
reed73603f32016-09-20 08:42:38 -0700472 case SkCanvas::kIntersect_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000473 this->combineBoundsIntersection(combination, prevFinite);
474 break;
reed73603f32016-09-20 08:42:38 -0700475 case SkCanvas::kReverseDifference_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000476 this->combineBoundsRevDiff(combination, prevFinite);
477 break;
reed73603f32016-09-20 08:42:38 -0700478 case SkCanvas::kReplace_Op:
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000479 // Replace just ignores everything prior
480 // The current clip's bound information is already filled in
481 // so nothing to do
482 break;
483 default:
reed73603f32016-09-20 08:42:38 -0700484 SkDebugf("SkCanvas::ClipOp error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000485 SkASSERT(0);
486 break;
487 }
488}
reed@google.com5c3d1472011-02-22 19:12:23 +0000489
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000490// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000491// the deque. As such it needs to balance allocating too much memory vs.
492// incurring allocation/deallocation thrashing. It should roughly correspond to
493// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000494static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000495
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000496SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000497 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000498 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000499}
500
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000501SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000502 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000503 *this = b;
504}
505
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000506SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000507 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000508 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000509 if (!r.isEmpty()) {
reed73603f32016-09-20 08:42:38 -0700510 this->clipDevRect(r, SkCanvas::kReplace_Op, false);
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000511 }
512}
513
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000514SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000515 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000516 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000517 if (!r.isEmpty()) {
518 SkRect temp;
519 temp.set(r);
reed73603f32016-09-20 08:42:38 -0700520 this->clipDevRect(temp, SkCanvas::kReplace_Op, false);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000521 }
522}
523
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000524SkClipStack::~SkClipStack() {
525 reset();
526}
527
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000528SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
529 if (this == &b) {
530 return *this;
531 }
532 reset();
533
534 fSaveCount = b.fSaveCount;
535 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000536 for (const Element* element = (const Element*)recIter.next();
halcanary96fcdcc2015-08-27 07:41:13 -0700537 element != nullptr;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000538 element = (const Element*)recIter.next()) {
539 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000540 }
541
542 return *this;
543}
544
545bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000546 if (this->getTopmostGenID() == b.getTopmostGenID()) {
547 return true;
548 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000549 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000550 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000551 return false;
552 }
553 SkDeque::F2BIter myIter(fDeque);
554 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000555 const Element* myElement = (const Element*)myIter.next();
556 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000557
halcanary96fcdcc2015-08-27 07:41:13 -0700558 while (myElement != nullptr && bElement != nullptr) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000559 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000560 return false;
561 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000562 myElement = (const Element*)myIter.next();
563 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000564 }
halcanary96fcdcc2015-08-27 07:41:13 -0700565 return myElement == nullptr && bElement == nullptr;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000566}
567
reed@google.com5c3d1472011-02-22 19:12:23 +0000568void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000569 // We used a placement new for each object in fDeque, so we're responsible
570 // for calling the destructor on each of them as well.
571 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000572 Element* element = (Element*)fDeque.back();
573 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000574 fDeque.pop_back();
575 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000576
577 fSaveCount = 0;
578}
579
580void SkClipStack::save() {
581 fSaveCount += 1;
582}
583
584void SkClipStack::restore() {
585 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000586 restoreTo(fSaveCount);
587}
588
589void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000590 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000591 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000592 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000593 break;
594 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000595 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000596 fDeque.pop_back();
597 }
598}
599
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000600void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000601 BoundsType* boundType,
602 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700603 SkASSERT(canvFiniteBound && boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000604
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000605 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000606
halcanary96fcdcc2015-08-27 07:41:13 -0700607 if (nullptr == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000608 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000609 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000610 *boundType = kInsideOut_BoundsType;
bsalomon49f085d2014-09-05 13:34:00 -0700611 if (isIntersectionOfRects) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000612 *isIntersectionOfRects = false;
613 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000614 return;
615 }
616
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000617 *canvFiniteBound = element->fFiniteBound;
618 *boundType = element->fFiniteBoundType;
bsalomon49f085d2014-09-05 13:34:00 -0700619 if (isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000620 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000621 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000622}
623
reed4d2cce42016-08-22 13:03:47 -0700624bool SkClipStack::internalQuickContains(const SkRect& rect) const {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000625
626 Iter iter(*this, Iter::kTop_IterStart);
627 const Element* element = iter.prev();
halcanary96fcdcc2015-08-27 07:41:13 -0700628 while (element != nullptr) {
reed73603f32016-09-20 08:42:38 -0700629 if (SkCanvas::kIntersect_Op != element->getOp() && SkCanvas::kReplace_Op != element->getOp())
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000630 return false;
631 if (element->isInverseFilled()) {
632 // Part of 'rect' could be trimmed off by the inverse-filled clip element
633 if (SkRect::Intersects(element->getBounds(), rect)) {
634 return false;
635 }
636 } else {
637 if (!element->contains(rect)) {
638 return false;
639 }
640 }
reed73603f32016-09-20 08:42:38 -0700641 if (SkCanvas::kReplace_Op == element->getOp()) {
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000642 break;
643 }
644 element = iter.prev();
645 }
646 return true;
647}
648
reed4d2cce42016-08-22 13:03:47 -0700649bool SkClipStack::internalQuickContains(const SkRRect& rrect) const {
bsalomon7f0d9f32016-08-15 14:49:10 -0700650
651 Iter iter(*this, Iter::kTop_IterStart);
652 const Element* element = iter.prev();
653 while (element != nullptr) {
reed73603f32016-09-20 08:42:38 -0700654 if (SkCanvas::kIntersect_Op != element->getOp() && SkCanvas::kReplace_Op != element->getOp())
bsalomon7f0d9f32016-08-15 14:49:10 -0700655 return false;
656 if (element->isInverseFilled()) {
657 // Part of 'rrect' could be trimmed off by the inverse-filled clip element
658 if (SkRect::Intersects(element->getBounds(), rrect.getBounds())) {
659 return false;
660 }
661 } else {
662 if (!element->contains(rrect)) {
663 return false;
664 }
665 }
reed73603f32016-09-20 08:42:38 -0700666 if (SkCanvas::kReplace_Op == element->getOp()) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700667 break;
668 }
669 element = iter.prev();
670 }
671 return true;
672}
673
fmalita1a481fe2015-02-04 07:39:34 -0800674bool SkClipStack::asPath(SkPath *path) const {
675 bool isAA = false;
676
677 path->reset();
678 path->setFillType(SkPath::kInverseEvenOdd_FillType);
679
680 SkClipStack::Iter iter(*this, SkClipStack::Iter::kBottom_IterStart);
681 while (const SkClipStack::Element* element = iter.next()) {
682 SkPath operand;
683 if (element->getType() != SkClipStack::Element::kEmpty_Type) {
684 element->asPath(&operand);
685 }
686
reed73603f32016-09-20 08:42:38 -0700687 SkCanvas::ClipOp elementOp = element->getOp();
688 if (elementOp == SkCanvas::kReplace_Op) {
fmalita1a481fe2015-02-04 07:39:34 -0800689 *path = operand;
690 } else {
691 Op(*path, operand, (SkPathOp)elementOp, path);
692 }
693
694 // if the prev and curr clips disagree about aa -vs- not, favor the aa request.
695 // perhaps we need an API change to avoid this sort of mixed-signals about
696 // clipping.
697 isAA = (isAA || element->isAA());
698 }
699
700 return isAA;
701}
702
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000703void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000704 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000705 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000706 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000707
bsalomon49f085d2014-09-05 13:34:00 -0700708 if (prior) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000709 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
710 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000711 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000712 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000713 return;
714 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000715 if (Element::kRect_Type == element.getType()) {
716 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000717 SkRect isectRect;
718 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000719 prior->setEmpty();
720 return;
721 }
722
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000723 prior->fRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000724 prior->fDoAA = element.isAA();
725 Element* priorPrior = (Element*) iter.prev();
726 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000727 return;
728 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000729 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000730 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000731 // fallthrough
732 default:
733 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
734 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000735 return;
736 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000737 break;
738 }
reed73603f32016-09-20 08:42:38 -0700739 } else if (SkCanvas::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000740 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000741 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000742 }
743 }
halcanary385fe4d2015-08-26 13:07:48 -0700744 Element* newElement = new (fDeque.push_back()) Element(element);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000745 newElement->updateBoundAndGenID(prior);
746}
747
reed73603f32016-09-20 08:42:38 -0700748void SkClipStack::clipDevRRect(const SkRRect& rrect, SkCanvas::ClipOp op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000749 Element element(fSaveCount, rrect, op, doAA);
750 this->pushElement(element);
751}
752
reed73603f32016-09-20 08:42:38 -0700753void SkClipStack::clipDevRect(const SkRect& rect, SkCanvas::ClipOp op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000754 Element element(fSaveCount, rect, op, doAA);
755 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000756}
757
reed73603f32016-09-20 08:42:38 -0700758void SkClipStack::clipDevPath(const SkPath& path, SkCanvas::ClipOp op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000759 Element element(fSaveCount, path, op, doAA);
760 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000761}
762
reed@google.com0557d9e2012-08-16 15:59:59 +0000763void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000764 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000765
reed73603f32016-09-20 08:42:38 -0700766 if (element && element->canBeIntersectedInPlace(fSaveCount, SkCanvas::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000767 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000768 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000769 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000770
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000771 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000772}
773
reed@google.com5c3d1472011-02-22 19:12:23 +0000774///////////////////////////////////////////////////////////////////////////////
775
halcanary96fcdcc2015-08-27 07:41:13 -0700776SkClipStack::Iter::Iter() : fStack(nullptr) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000777}
778
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000779SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
780 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000781 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000782}
783
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000784const SkClipStack::Element* SkClipStack::Iter::next() {
785 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000786}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000787
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000788const SkClipStack::Element* SkClipStack::Iter::prev() {
789 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000790}
791
reed73603f32016-09-20 08:42:38 -0700792const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkCanvas::ClipOp op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000793
halcanary96fcdcc2015-08-27 07:41:13 -0700794 if (nullptr == fStack) {
795 return nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000796 }
797
798 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
799
halcanary96fcdcc2015-08-27 07:41:13 -0700800 const SkClipStack::Element* element = nullptr;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000801
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000802 for (element = (const SkClipStack::Element*) fIter.prev();
bsalomon49f085d2014-09-05 13:34:00 -0700803 element;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000804 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000805
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000806 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000807 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000808 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000809 // return, the iterator is actually pointing at (and will
810 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000811 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000812 // step so we get the expected result.
halcanary96fcdcc2015-08-27 07:41:13 -0700813 if (nullptr == fIter.next()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000814 // The reverse iterator has run off the front of the deque
815 // (i.e., the "op" clip is the first clip) and can't
816 // recover. Reset the iterator to start at the front.
817 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
818 }
819 break;
820 }
821 }
822
halcanary96fcdcc2015-08-27 07:41:13 -0700823 if (nullptr == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000824 // There were no "op" clips
825 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
826 }
827
828 return this->next();
829}
830
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000831void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000832 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000833 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000834}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000835
836// helper method
837void SkClipStack::getConservativeBounds(int offsetX,
838 int offsetY,
839 int maxWidth,
840 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000841 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000842 bool* isIntersectionOfRects) const {
bsalomon49f085d2014-09-05 13:34:00 -0700843 SkASSERT(devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000844
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000845 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000846 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000847
848 SkRect temp;
849 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000850
robertphillips@google.com7b112892012-07-31 15:18:21 +0000851 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000852 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000853 if (SkClipStack::kInsideOut_BoundsType == boundType) {
854 return;
855 }
856
robertphillips@google.com7b112892012-07-31 15:18:21 +0000857 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000858 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
859
robertphillips@google.com7b112892012-07-31 15:18:21 +0000860 if (!devBounds->intersect(temp)) {
861 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000862 }
863}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000864
bsalomoncb31e512016-08-26 10:48:19 -0700865bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const {
866 // We limit to 5 elements. This means the back element will be bounds checked at most 4 times if
867 // it is an rrect.
868 int cnt = fDeque.count();
869 if (!cnt || cnt > 5) {
870 return false;
871 }
872 const Element* back = static_cast<const Element*>(fDeque.back());
873 if (back->getType() != SkClipStack::Element::kRect_Type &&
874 back->getType() != SkClipStack::Element::kRRect_Type) {
875 return false;
876 }
reed73603f32016-09-20 08:42:38 -0700877 if (back->getOp() == SkCanvas::kReplace_Op) {
bsalomoncb31e512016-08-26 10:48:19 -0700878 *rrect = back->asRRect();
879 *aa = back->isAA();
880 return true;
881 }
882
reed73603f32016-09-20 08:42:38 -0700883 if (back->getOp() == SkCanvas::kIntersect_Op) {
bsalomoncb31e512016-08-26 10:48:19 -0700884 SkRect backBounds;
885 if (!backBounds.intersect(bounds, back->asRRect().rect())) {
886 return false;
887 }
888 if (cnt > 1) {
889 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
890 SkAssertResult(static_cast<const Element*>(iter.prev()) == back);
891 while (const Element* prior = (const Element*)iter.prev()) {
reed73603f32016-09-20 08:42:38 -0700892 if ((prior->getOp() != SkCanvas::kIntersect_Op &&
893 prior->getOp() != SkCanvas::kReplace_Op) ||
bsalomoncb31e512016-08-26 10:48:19 -0700894 !prior->contains(backBounds)) {
895 return false;
896 }
reed73603f32016-09-20 08:42:38 -0700897 if (prior->getOp() == SkCanvas::kReplace_Op) {
bsalomoncb31e512016-08-26 10:48:19 -0700898 break;
899 }
900 }
901 }
902 *rrect = back->asRRect();
903 *aa = back->isAA();
904 return true;
905 }
906 return false;
907}
908
robertphillips@google.com46f93502012-08-07 15:38:08 +0000909int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000910 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000911 return sk_atomic_inc(&gGenID);
912}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000913
914int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000915 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000916 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000917 }
918
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000919 const Element* back = static_cast<const Element*>(fDeque.back());
920 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
921 return kWideOpenGenID;
922 }
923
924 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000925}
bsalomonb6b02522014-06-09 07:59:06 -0700926
djsollenefe46d22016-04-29 06:41:35 -0700927#ifdef SK_DEBUG
bsalomonb6b02522014-06-09 07:59:06 -0700928void SkClipStack::Element::dump() const {
929 static const char* kTypeStrings[] = {
930 "empty",
931 "rect",
932 "rrect",
933 "path"
934 };
bungeman99fe8222015-08-20 07:57:51 -0700935 static_assert(0 == kEmpty_Type, "type_str");
936 static_assert(1 == kRect_Type, "type_str");
937 static_assert(2 == kRRect_Type, "type_str");
938 static_assert(3 == kPath_Type, "type_str");
939 static_assert(SK_ARRAY_COUNT(kTypeStrings) == kTypeCnt, "type_str");
bsalomonb6b02522014-06-09 07:59:06 -0700940
941 static const char* kOpStrings[] = {
942 "difference",
943 "intersect",
944 "union",
945 "xor",
946 "reverse-difference",
947 "replace",
948 };
reed73603f32016-09-20 08:42:38 -0700949 static_assert(0 == SkCanvas::kDifference_Op, "op_str");
950 static_assert(1 == SkCanvas::kIntersect_Op, "op_str");
951 static_assert(2 == SkCanvas::kUnion_Op, "op_str");
952 static_assert(3 == SkCanvas::kXOR_Op, "op_str");
953 static_assert(4 == SkCanvas::kReverseDifference_Op, "op_str");
954 static_assert(5 == SkCanvas::kReplace_Op, "op_str");
bungeman99fe8222015-08-20 07:57:51 -0700955 static_assert(SK_ARRAY_COUNT(kOpStrings) == SkRegion::kOpCnt, "op_str");
bsalomonb6b02522014-06-09 07:59:06 -0700956
957 SkDebugf("Type: %s, Op: %s, AA: %s, Save Count: %d\n", kTypeStrings[fType],
958 kOpStrings[fOp], (fDoAA ? "yes" : "no"), fSaveCount);
959 switch (fType) {
960 case kEmpty_Type:
961 SkDebugf("\n");
962 break;
963 case kRect_Type:
964 this->getRect().dump();
965 SkDebugf("\n");
966 break;
967 case kRRect_Type:
968 this->getRRect().dump();
969 SkDebugf("\n");
970 break;
971 case kPath_Type:
halcanary96fcdcc2015-08-27 07:41:13 -0700972 this->getPath().dump(nullptr, true, false);
bsalomonb6b02522014-06-09 07:59:06 -0700973 break;
974 }
975}
976
977void SkClipStack::dump() const {
978 B2TIter iter(*this);
979 const Element* e;
980 while ((e = iter.next())) {
981 e->dump();
982 SkDebugf("\n");
983 }
984}
985#endif