blob: d087592b173c3fbaaacc5441075b283929a26813 [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
8#include "SkCanvas.h"
reed@google.com5c3d1472011-02-22 19:12:23 +00009#include "SkClipStack.h"
10#include "SkPath.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000011#include "SkThread.h"
12
reed@google.com5c3d1472011-02-22 19:12:23 +000013#include <new>
14
robertphillips@google.com607fe072012-07-24 13:54:00 +000015
robertphillips@google.com46f93502012-08-07 15:38:08 +000016// 0-2 are reserved for invalid, empty & wide-open
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000017static const int32_t kFirstUnreservedGenID = 3;
18int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +000019
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000020SkClipStack::Element::Element(const Element& that) {
21 switch (that.getType()) {
22 case kEmpty_Type:
23 fPath.reset();
24 break;
25 case kRect_Type: // Rect uses rrect
26 case kRRect_Type:
27 fPath.reset();
28 fRRect = that.fRRect;
29 break;
30 case kPath_Type:
31 fPath.set(that.getPath());
32 break;
33 }
34
35 fSaveCount = that.fSaveCount;
36 fOp = that.fOp;
37 fType = that.fType;
38 fDoAA = that.fDoAA;
39 fFiniteBoundType = that.fFiniteBoundType;
40 fFiniteBound = that.fFiniteBound;
41 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
42 fGenID = that.fGenID;
43}
44
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000045bool SkClipStack::Element::operator== (const Element& element) const {
46 if (this == &element) {
47 return true;
48 }
49 if (fOp != element.fOp ||
50 fType != element.fType ||
51 fDoAA != element.fDoAA ||
52 fSaveCount != element.fSaveCount) {
53 return false;
54 }
55 switch (fType) {
56 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000057 return this->getPath() == element.getPath();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000058 case kRRect_Type:
59 return fRRect == element.fRRect;
60 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000061 return this->getRect() == element.getRect();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000062 case kEmpty_Type:
63 return true;
64 default:
65 SkDEBUGFAIL("Unexpected type.");
66 return false;
67 }
68}
69
fmalitac3b589a2014-06-05 12:40:07 -070070void SkClipStack::Element::replay(SkCanvasClipVisitor* visitor) const {
71 static const SkRect kEmptyRect = { 0, 0, 0, 0 };
72
73 switch (fType) {
74 case kPath_Type:
75 visitor->clipPath(this->getPath(), this->getOp(), this->isAA());
76 break;
77 case kRRect_Type:
78 visitor->clipRRect(this->getRRect(), this->getOp(), this->isAA());
79 break;
80 case kRect_Type:
81 visitor->clipRect(this->getRect(), this->getOp(), this->isAA());
82 break;
83 case kEmpty_Type:
84 visitor->clipRect(kEmptyRect, SkRegion::kIntersect_Op, false);
85 break;
86 }
87}
88
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000089void SkClipStack::Element::invertShapeFillType() {
90 switch (fType) {
91 case kRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000092 fPath.init();
93 fPath.get()->addRect(this->getRect());
94 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000095 fType = kPath_Type;
96 break;
97 case kRRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000098 fPath.init();
99 fPath.get()->addRRect(fRRect);
100 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000101 fType = kPath_Type;
102 break;
103 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000104 fPath.get()->toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000105 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000106 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000107 // Should this set to an empty, inverse filled path?
108 break;
109 }
110}
111
112void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
113 bool doAA) {
114 if (!path.isInverseFillType()) {
115 if (SkPath::kNone_PathAsRect != path.asRect()) {
116 this->initRect(saveCount, path.getBounds(), op, doAA);
117 return;
118 }
119 SkRect ovalRect;
120 if (path.isOval(&ovalRect)) {
121 SkRRect rrect;
122 rrect.setOval(ovalRect);
123 this->initRRect(saveCount, rrect, op, doAA);
124 return;
125 }
126 }
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000127 fPath.set(path);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000128 fType = kPath_Type;
129 this->initCommon(saveCount, op, doAA);
130}
131
132void SkClipStack::Element::asPath(SkPath* path) const {
133 switch (fType) {
134 case kEmpty_Type:
135 path->reset();
136 break;
137 case kRect_Type:
138 path->reset();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000139 path->addRect(this->getRect());
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000140 break;
141 case kRRect_Type:
142 path->reset();
143 path->addRRect(fRRect);
144 break;
145 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000146 *path = *fPath.get();
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000147 break;
148 }
149}
150
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000151void SkClipStack::Element::setEmpty() {
152 fType = kEmpty_Type;
153 fFiniteBound.setEmpty();
154 fFiniteBoundType = kNormal_BoundsType;
155 fIsIntersectionOfRects = false;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000156 fRRect.setEmpty();
157 fPath.reset();
158 fGenID = kEmptyGenID;
159 SkDEBUGCODE(this->checkEmpty();)
160}
161
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000162void SkClipStack::Element::checkEmpty() const {
163 SkASSERT(fFiniteBound.isEmpty());
164 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
165 SkASSERT(!fIsIntersectionOfRects);
166 SkASSERT(kEmptyGenID == fGenID);
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000167 SkASSERT(!fPath.isValid());
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000168}
reed@google.com5c3d1472011-02-22 19:12:23 +0000169
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000170bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
171 if (kEmpty_Type == fType &&
172 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
173 return true;
174 }
175 // Only clips within the same save/restore frame (as captured by
176 // the save count) can be merged
177 return fSaveCount == saveCount &&
178 SkRegion::kIntersect_Op == op &&
179 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
180}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000181
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000182bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
183 SkASSERT(kRect_Type == fType);
184
185 if (fDoAA == newAA) {
186 // if the AA setting is the same there is no issue
187 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000188 }
189
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000190 if (!SkRect::Intersects(this->getRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000191 // The calling code will correctly set the result to the empty clip
192 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000193 }
194
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000195 if (this->getRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000196 // if the new rect carves out a portion of the old one there is no
197 // issue
198 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000199 }
200
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000201 // So either the two overlap in some complex manner or newR contains oldR.
202 // In the first, case the edges will require different AA. In the second,
203 // the AA setting that would be carried forward is incorrect (e.g., oldR
204 // is AA while newR is BW but since newR contains oldR, oldR will be
205 // drawn BW) since the new AA setting will predominate.
206 return false;
207}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000208
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000209// a mirror of combineBoundsRevDiff
210void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
211 switch (combination) {
212 case kInvPrev_InvCur_FillCombo:
213 // In this case the only pixels that can remain set
214 // are inside the current clip rect since the extensions
215 // to infinity of both clips cancel out and whatever
216 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000217 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000218 break;
219 case kInvPrev_Cur_FillCombo:
220 // In this case the current op is finite so the only pixels
221 // that aren't set are whatever isn't set in the previous
222 // clip and whatever this clip carves out
223 fFiniteBound.join(prevFinite);
224 fFiniteBoundType = kInsideOut_BoundsType;
225 break;
226 case kPrev_InvCur_FillCombo:
227 // In this case everything outside of this clip's bound
228 // is erased, so the only pixels that can remain set
229 // occur w/in the intersection of the two finite bounds
230 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000231 this->setEmpty();
232 } else {
233 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000234 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000235 break;
236 case kPrev_Cur_FillCombo:
237 // The most conservative result bound is that of the
238 // prior clip. This could be wildly incorrect if the
239 // second clip either exactly matches the first clip
240 // (which should yield the empty set) or reduces the
241 // size of the prior bound (e.g., if the second clip
242 // exactly matched the bottom half of the prior clip).
243 // We ignore these two possibilities.
244 fFiniteBound = prevFinite;
245 break;
246 default:
247 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
248 break;
249 }
250}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000251
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000252void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000253
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000254 switch (combination) {
255 case kInvPrev_Cur_FillCombo: // fall through
256 case kPrev_InvCur_FillCombo:
257 // With only one of the clips inverted the result will always
258 // extend to infinity. The only pixels that may be un-writeable
259 // lie within the union of the two finite bounds
260 fFiniteBound.join(prevFinite);
261 fFiniteBoundType = kInsideOut_BoundsType;
262 break;
263 case kInvPrev_InvCur_FillCombo:
264 // The only pixels that can survive are within the
265 // union of the two bounding boxes since the extensions
266 // to infinity of both clips cancel out
267 // fall through!
268 case kPrev_Cur_FillCombo:
269 // The most conservative bound for xor is the
270 // union of the two bounds. If the two clips exactly overlapped
271 // the xor could yield the empty set. Similarly the xor
272 // could reduce the size of the original clip's bound (e.g.,
273 // if the second clip exactly matched the bottom half of the
274 // first clip). We ignore these two cases.
275 fFiniteBound.join(prevFinite);
276 fFiniteBoundType = kNormal_BoundsType;
277 break;
278 default:
279 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
280 break;
281 }
282}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000283
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000284// a mirror of combineBoundsIntersection
285void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
286
287 switch (combination) {
288 case kInvPrev_InvCur_FillCombo:
289 if (!fFiniteBound.intersect(prevFinite)) {
290 fFiniteBound.setEmpty();
291 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000292 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000293 fFiniteBoundType = kInsideOut_BoundsType;
294 break;
295 case kInvPrev_Cur_FillCombo:
296 // The only pixels that won't be drawable are inside
297 // the prior clip's finite bound
298 fFiniteBound = prevFinite;
299 fFiniteBoundType = kInsideOut_BoundsType;
300 break;
301 case kPrev_InvCur_FillCombo:
302 // The only pixels that won't be drawable are inside
303 // this clip's finite bound
304 break;
305 case kPrev_Cur_FillCombo:
306 fFiniteBound.join(prevFinite);
307 break;
308 default:
309 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
310 break;
311 }
312}
313
314// a mirror of combineBoundsUnion
315void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
316
317 switch (combination) {
318 case kInvPrev_InvCur_FillCombo:
319 // The only pixels that aren't writable in this case
320 // occur in the union of the two finite bounds
321 fFiniteBound.join(prevFinite);
322 fFiniteBoundType = kInsideOut_BoundsType;
323 break;
324 case kInvPrev_Cur_FillCombo:
325 // In this case the only pixels that will remain writeable
326 // are within the current clip
327 break;
328 case kPrev_InvCur_FillCombo:
329 // In this case the only pixels that will remain writeable
330 // are with the previous clip
331 fFiniteBound = prevFinite;
332 fFiniteBoundType = kNormal_BoundsType;
333 break;
334 case kPrev_Cur_FillCombo:
335 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000336 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000337 }
338 break;
339 default:
340 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
341 break;
342 }
343}
344
345// a mirror of combineBoundsDiff
346void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
347
348 switch (combination) {
349 case kInvPrev_InvCur_FillCombo:
350 // The only pixels that can survive are in the
351 // previous bound since the extensions to infinity in
352 // both clips cancel out
353 fFiniteBound = prevFinite;
354 fFiniteBoundType = kNormal_BoundsType;
355 break;
356 case kInvPrev_Cur_FillCombo:
357 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000358 this->setEmpty();
359 } else {
360 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000361 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000362 break;
363 case kPrev_InvCur_FillCombo:
364 fFiniteBound.join(prevFinite);
365 fFiniteBoundType = kInsideOut_BoundsType;
366 break;
367 case kPrev_Cur_FillCombo:
368 // Fall through - as with the kDifference_Op case, the
369 // most conservative result bound is the bound of the
370 // current clip. The prior clip could reduce the size of this
371 // bound (as in the kDifference_Op case) but we are ignoring
372 // those cases.
373 break;
374 default:
375 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
376 break;
377 }
378}
379
380void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
381 // We set this first here but we may overwrite it later if we determine that the clip is
382 // either wide-open or empty.
383 fGenID = GetNextGenID();
384
385 // First, optimistically update the current Element's bound information
386 // with the current clip's bound
387 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000388 switch (fType) {
389 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000390 fFiniteBound = this->getRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000391 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000392
393 if (SkRegion::kReplace_Op == fOp ||
394 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
395 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000396 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000397 fIsIntersectionOfRects = true;
398 }
399 break;
400 case kRRect_Type:
401 fFiniteBound = fRRect.getBounds();
402 fFiniteBoundType = kNormal_BoundsType;
403 break;
404 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000405 fFiniteBound = fPath.get()->getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000406
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000407 if (fPath.get()->isInverseFillType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000408 fFiniteBoundType = kInsideOut_BoundsType;
409 } else {
410 fFiniteBoundType = kNormal_BoundsType;
411 }
412 break;
413 case kEmpty_Type:
414 SkDEBUGFAIL("We shouldn't get here with an empty element.");
415 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000416 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000417
418 if (!fDoAA) {
419 // Here we mimic a non-anti-aliased scanline system. If there is
420 // no anti-aliasing we can integerize the bounding box to exclude
421 // fractional parts that won't be rendered.
422 // Note: the left edge is handled slightly differently below. We
423 // are a bit more generous in the rounding since we don't want to
424 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000425 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
426 SkScalarRoundToScalar(fFiniteBound.fTop),
427 SkScalarRoundToScalar(fFiniteBound.fRight),
428 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000429 }
430
431 // Now determine the previous Element's bound information taking into
432 // account that there may be no previous clip
433 SkRect prevFinite;
434 SkClipStack::BoundsType prevType;
435
436 if (NULL == prior) {
437 // no prior clip means the entire plane is writable
438 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
439 prevType = kInsideOut_BoundsType;
440 } else {
441 prevFinite = prior->fFiniteBound;
442 prevType = prior->fFiniteBoundType;
443 }
444
445 FillCombo combination = kPrev_Cur_FillCombo;
446 if (kInsideOut_BoundsType == fFiniteBoundType) {
447 combination = (FillCombo) (combination | 0x01);
448 }
449 if (kInsideOut_BoundsType == prevType) {
450 combination = (FillCombo) (combination | 0x02);
451 }
452
453 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
454 kInvPrev_Cur_FillCombo == combination ||
455 kPrev_InvCur_FillCombo == combination ||
456 kPrev_Cur_FillCombo == combination);
457
458 // Now integrate with clip with the prior clips
459 switch (fOp) {
460 case SkRegion::kDifference_Op:
461 this->combineBoundsDiff(combination, prevFinite);
462 break;
463 case SkRegion::kXOR_Op:
464 this->combineBoundsXOR(combination, prevFinite);
465 break;
466 case SkRegion::kUnion_Op:
467 this->combineBoundsUnion(combination, prevFinite);
468 break;
469 case SkRegion::kIntersect_Op:
470 this->combineBoundsIntersection(combination, prevFinite);
471 break;
472 case SkRegion::kReverseDifference_Op:
473 this->combineBoundsRevDiff(combination, prevFinite);
474 break;
475 case SkRegion::kReplace_Op:
476 // Replace just ignores everything prior
477 // The current clip's bound information is already filled in
478 // so nothing to do
479 break;
480 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000481 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000482 SkASSERT(0);
483 break;
484 }
485}
reed@google.com5c3d1472011-02-22 19:12:23 +0000486
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000487// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000488// the deque. As such it needs to balance allocating too much memory vs.
489// incurring allocation/deallocation thrashing. It should roughly correspond to
490// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000491static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000492
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000493SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000494 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000495 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000496}
497
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000498SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000499 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000500 *this = b;
501}
502
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000503SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000504 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000505 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000506 if (!r.isEmpty()) {
507 this->clipDevRect(r, SkRegion::kReplace_Op, false);
508 }
509}
510
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000512 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000513 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000514 if (!r.isEmpty()) {
515 SkRect temp;
516 temp.set(r);
517 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
518 }
519}
520
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000521SkClipStack::~SkClipStack() {
522 reset();
523}
524
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000525SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
526 if (this == &b) {
527 return *this;
528 }
529 reset();
530
531 fSaveCount = b.fSaveCount;
532 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000533 for (const Element* element = (const Element*)recIter.next();
534 element != NULL;
535 element = (const Element*)recIter.next()) {
536 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000537 }
538
539 return *this;
540}
541
542bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000543 if (this->getTopmostGenID() == b.getTopmostGenID()) {
544 return true;
545 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000546 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000547 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000548 return false;
549 }
550 SkDeque::F2BIter myIter(fDeque);
551 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000552 const Element* myElement = (const Element*)myIter.next();
553 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000554
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000555 while (myElement != NULL && bElement != NULL) {
556 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000557 return false;
558 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000559 myElement = (const Element*)myIter.next();
560 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000561 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000562 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000563}
564
reed@google.com5c3d1472011-02-22 19:12:23 +0000565void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000566 // We used a placement new for each object in fDeque, so we're responsible
567 // for calling the destructor on each of them as well.
568 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000569 Element* element = (Element*)fDeque.back();
570 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000571 fDeque.pop_back();
572 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000573
574 fSaveCount = 0;
575}
576
577void SkClipStack::save() {
578 fSaveCount += 1;
579}
580
581void SkClipStack::restore() {
582 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000583 restoreTo(fSaveCount);
584}
585
586void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000587 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000588 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000589 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000590 break;
591 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000592 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000593 fDeque.pop_back();
594 }
595}
596
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000597void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000598 BoundsType* boundType,
599 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000600 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000601
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000602 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000603
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000604 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000605 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000606 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000607 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000608 if (NULL != isIntersectionOfRects) {
609 *isIntersectionOfRects = false;
610 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000611 return;
612 }
613
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000614 *canvFiniteBound = element->fFiniteBound;
615 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000616 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000617 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000618 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000619}
620
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000621bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
622 SkASSERT(NULL != rect);
623
624 SkRect bounds;
625 SkClipStack::BoundsType bt;
626 this->getBounds(&bounds, &bt);
627 if (bt == SkClipStack::kInsideOut_BoundsType) {
628 if (bounds.contains(*rect)) {
629 return false;
630 } else {
631 // If rect's x values are both within bound's x range we
632 // could clip here. Same for y. But we don't bother to check.
633 return true;
634 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000635 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000636 return rect->intersect(bounds);
637 }
638}
639
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000640bool SkClipStack::quickContains(const SkRect& rect) const {
641
642 Iter iter(*this, Iter::kTop_IterStart);
643 const Element* element = iter.prev();
644 while (element != NULL) {
645 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
646 return false;
647 if (element->isInverseFilled()) {
648 // Part of 'rect' could be trimmed off by the inverse-filled clip element
649 if (SkRect::Intersects(element->getBounds(), rect)) {
650 return false;
651 }
652 } else {
653 if (!element->contains(rect)) {
654 return false;
655 }
656 }
657 if (SkRegion::kReplace_Op == element->getOp()) {
658 break;
659 }
660 element = iter.prev();
661 }
662 return true;
663}
664
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000665void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000666 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000667 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000668 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000669
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000670 if (NULL != prior) {
671 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
672 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000673 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000674 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000675 return;
676 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000677 if (Element::kRect_Type == element.getType()) {
678 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000679 SkRect isectRect;
680 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000681 prior->setEmpty();
682 return;
683 }
684
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000685 prior->fRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000686 prior->fDoAA = element.isAA();
687 Element* priorPrior = (Element*) iter.prev();
688 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000689 return;
690 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000691 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000692 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000693 // fallthrough
694 default:
695 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
696 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000697 return;
698 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000699 break;
700 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000701 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000702 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000703 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000704 }
705 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000706 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
707 newElement->updateBoundAndGenID(prior);
708}
709
710void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
711 Element element(fSaveCount, rrect, op, doAA);
712 this->pushElement(element);
713}
714
715void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
716 Element element(fSaveCount, rect, op, doAA);
717 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000718}
719
reed@google.com00177082011-10-12 14:34:30 +0000720void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000721 Element element(fSaveCount, path, op, doAA);
722 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000723}
724
reed@google.com0557d9e2012-08-16 15:59:59 +0000725void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000726 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000727
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000728 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000729 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000730 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000731 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000732
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000733 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000734}
735
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000736bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000737 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000738}
739
reed@google.com5c3d1472011-02-22 19:12:23 +0000740///////////////////////////////////////////////////////////////////////////////
741
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000742SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000743}
744
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000745SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
746 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000747 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000748}
749
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000750const SkClipStack::Element* SkClipStack::Iter::next() {
751 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000752}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000753
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000754const SkClipStack::Element* SkClipStack::Iter::prev() {
755 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000756}
757
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000758const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000759
760 if (NULL == fStack) {
761 return NULL;
762 }
763
764 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
765
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000766 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000767
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000768 for (element = (const SkClipStack::Element*) fIter.prev();
769 NULL != element;
770 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000771
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000772 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000773 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000774 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000775 // return, the iterator is actually pointing at (and will
776 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000777 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000778 // step so we get the expected result.
779 if (NULL == fIter.next()) {
780 // The reverse iterator has run off the front of the deque
781 // (i.e., the "op" clip is the first clip) and can't
782 // recover. Reset the iterator to start at the front.
783 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
784 }
785 break;
786 }
787 }
788
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000789 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000790 // There were no "op" clips
791 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
792 }
793
794 return this->next();
795}
796
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000797void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000798 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000799 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000800}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000801
802// helper method
803void SkClipStack::getConservativeBounds(int offsetX,
804 int offsetY,
805 int maxWidth,
806 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000807 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000808 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000809 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000810
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000811 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000812 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000813
814 SkRect temp;
815 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000816
robertphillips@google.com7b112892012-07-31 15:18:21 +0000817 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000818 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000819 if (SkClipStack::kInsideOut_BoundsType == boundType) {
820 return;
821 }
822
robertphillips@google.com7b112892012-07-31 15:18:21 +0000823 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000824 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
825
robertphillips@google.com7b112892012-07-31 15:18:21 +0000826 if (!devBounds->intersect(temp)) {
827 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000828 }
829}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000830
robertphillips@google.com46f93502012-08-07 15:38:08 +0000831int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000832 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000833 return sk_atomic_inc(&gGenID);
834}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000835
836int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000837 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000838 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000839 }
840
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000841 const Element* back = static_cast<const Element*>(fDeque.back());
842 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
843 return kWideOpenGenID;
844 }
845
846 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000847}