blob: b60a6c961d24977e6e3857df10585cda0b1bcf61 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com5c3d1472011-02-22 19:12:23 +00008#include "SkClipStack.h"
9#include "SkPath.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000010#include "SkThread.h"
11
reed@google.com5c3d1472011-02-22 19:12:23 +000012#include <new>
13
robertphillips@google.com607fe072012-07-24 13:54:00 +000014
robertphillips@google.com46f93502012-08-07 15:38:08 +000015// 0-2 are reserved for invalid, empty & wide-open
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000016static const int32_t kFirstUnreservedGenID = 3;
17int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +000018
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000019SkClipStack::Element::Element(const Element& that) {
20 switch (that.getType()) {
21 case kEmpty_Type:
22 fPath.reset();
23 break;
24 case kRect_Type: // Rect uses rrect
25 case kRRect_Type:
26 fPath.reset();
27 fRRect = that.fRRect;
28 break;
29 case kPath_Type:
30 fPath.set(that.getPath());
31 break;
32 }
33
34 fSaveCount = that.fSaveCount;
35 fOp = that.fOp;
36 fType = that.fType;
37 fDoAA = that.fDoAA;
38 fFiniteBoundType = that.fFiniteBoundType;
39 fFiniteBound = that.fFiniteBound;
40 fIsIntersectionOfRects = that.fIsIntersectionOfRects;
41 fGenID = that.fGenID;
42}
43
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000044bool SkClipStack::Element::operator== (const Element& element) const {
45 if (this == &element) {
46 return true;
47 }
48 if (fOp != element.fOp ||
49 fType != element.fType ||
50 fDoAA != element.fDoAA ||
51 fSaveCount != element.fSaveCount) {
52 return false;
53 }
54 switch (fType) {
55 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000056 return this->getPath() == element.getPath();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000057 case kRRect_Type:
58 return fRRect == element.fRRect;
59 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +000060 return this->getRect() == element.getRect();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000061 case kEmpty_Type:
62 return true;
63 default:
64 SkDEBUGFAIL("Unexpected type.");
65 return false;
66 }
67}
68
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000069void SkClipStack::Element::invertShapeFillType() {
70 switch (fType) {
71 case kRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000072 fPath.init();
73 fPath.get()->addRect(this->getRect());
74 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000075 fType = kPath_Type;
76 break;
77 case kRRect_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000078 fPath.init();
79 fPath.get()->addRRect(fRRect);
80 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000081 fType = kPath_Type;
82 break;
83 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +000084 fPath.get()->toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000085 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000086 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000087 // Should this set to an empty, inverse filled path?
88 break;
89 }
90}
91
92void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
93 bool doAA) {
94 if (!path.isInverseFillType()) {
95 if (SkPath::kNone_PathAsRect != path.asRect()) {
96 this->initRect(saveCount, path.getBounds(), op, doAA);
97 return;
98 }
99 SkRect ovalRect;
100 if (path.isOval(&ovalRect)) {
101 SkRRect rrect;
102 rrect.setOval(ovalRect);
103 this->initRRect(saveCount, rrect, op, doAA);
104 return;
105 }
106 }
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000107 fPath.set(path);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000108 fType = kPath_Type;
109 this->initCommon(saveCount, op, doAA);
110}
111
112void SkClipStack::Element::asPath(SkPath* path) const {
113 switch (fType) {
114 case kEmpty_Type:
115 path->reset();
116 break;
117 case kRect_Type:
118 path->reset();
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000119 path->addRect(this->getRect());
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000120 break;
121 case kRRect_Type:
122 path->reset();
123 path->addRRect(fRRect);
124 break;
125 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000126 *path = *fPath.get();
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000127 break;
128 }
129}
130
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000131void SkClipStack::Element::setEmpty() {
132 fType = kEmpty_Type;
133 fFiniteBound.setEmpty();
134 fFiniteBoundType = kNormal_BoundsType;
135 fIsIntersectionOfRects = false;
commit-bot@chromium.org9cb671a2014-02-16 14:45:45 +0000136 fRRect.setEmpty();
137 fPath.reset();
138 fGenID = kEmptyGenID;
139 SkDEBUGCODE(this->checkEmpty();)
140}
141
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000142void SkClipStack::Element::checkEmpty() const {
143 SkASSERT(fFiniteBound.isEmpty());
144 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
145 SkASSERT(!fIsIntersectionOfRects);
146 SkASSERT(kEmptyGenID == fGenID);
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000147 SkASSERT(!fPath.isValid());
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000148}
reed@google.com5c3d1472011-02-22 19:12:23 +0000149
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000150bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
151 if (kEmpty_Type == fType &&
152 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
153 return true;
154 }
155 // Only clips within the same save/restore frame (as captured by
156 // the save count) can be merged
157 return fSaveCount == saveCount &&
158 SkRegion::kIntersect_Op == op &&
159 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
160}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000161
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000162bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
163 SkASSERT(kRect_Type == fType);
164
165 if (fDoAA == newAA) {
166 // if the AA setting is the same there is no issue
167 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000168 }
169
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000170 if (!SkRect::Intersects(this->getRect(), newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000171 // The calling code will correctly set the result to the empty clip
172 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000173 }
174
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000175 if (this->getRect().contains(newR)) {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000176 // if the new rect carves out a portion of the old one there is no
177 // issue
178 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000179 }
180
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000181 // So either the two overlap in some complex manner or newR contains oldR.
182 // In the first, case the edges will require different AA. In the second,
183 // the AA setting that would be carried forward is incorrect (e.g., oldR
184 // is AA while newR is BW but since newR contains oldR, oldR will be
185 // drawn BW) since the new AA setting will predominate.
186 return false;
187}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000188
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000189// a mirror of combineBoundsRevDiff
190void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
191 switch (combination) {
192 case kInvPrev_InvCur_FillCombo:
193 // In this case the only pixels that can remain set
194 // are inside the current clip rect since the extensions
195 // to infinity of both clips cancel out and whatever
196 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000197 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000198 break;
199 case kInvPrev_Cur_FillCombo:
200 // In this case the current op is finite so the only pixels
201 // that aren't set are whatever isn't set in the previous
202 // clip and whatever this clip carves out
203 fFiniteBound.join(prevFinite);
204 fFiniteBoundType = kInsideOut_BoundsType;
205 break;
206 case kPrev_InvCur_FillCombo:
207 // In this case everything outside of this clip's bound
208 // is erased, so the only pixels that can remain set
209 // occur w/in the intersection of the two finite bounds
210 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000211 this->setEmpty();
212 } else {
213 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000214 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000215 break;
216 case kPrev_Cur_FillCombo:
217 // The most conservative result bound is that of the
218 // prior clip. This could be wildly incorrect if the
219 // second clip either exactly matches the first clip
220 // (which should yield the empty set) or reduces the
221 // size of the prior bound (e.g., if the second clip
222 // exactly matched the bottom half of the prior clip).
223 // We ignore these two possibilities.
224 fFiniteBound = prevFinite;
225 break;
226 default:
227 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
228 break;
229 }
230}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000231
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000232void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000233
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000234 switch (combination) {
235 case kInvPrev_Cur_FillCombo: // fall through
236 case kPrev_InvCur_FillCombo:
237 // With only one of the clips inverted the result will always
238 // extend to infinity. The only pixels that may be un-writeable
239 // lie within the union of the two finite bounds
240 fFiniteBound.join(prevFinite);
241 fFiniteBoundType = kInsideOut_BoundsType;
242 break;
243 case kInvPrev_InvCur_FillCombo:
244 // The only pixels that can survive are within the
245 // union of the two bounding boxes since the extensions
246 // to infinity of both clips cancel out
247 // fall through!
248 case kPrev_Cur_FillCombo:
249 // The most conservative bound for xor is the
250 // union of the two bounds. If the two clips exactly overlapped
251 // the xor could yield the empty set. Similarly the xor
252 // could reduce the size of the original clip's bound (e.g.,
253 // if the second clip exactly matched the bottom half of the
254 // first clip). We ignore these two cases.
255 fFiniteBound.join(prevFinite);
256 fFiniteBoundType = kNormal_BoundsType;
257 break;
258 default:
259 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
260 break;
261 }
262}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000263
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000264// a mirror of combineBoundsIntersection
265void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
266
267 switch (combination) {
268 case kInvPrev_InvCur_FillCombo:
269 if (!fFiniteBound.intersect(prevFinite)) {
270 fFiniteBound.setEmpty();
271 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000272 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000273 fFiniteBoundType = kInsideOut_BoundsType;
274 break;
275 case kInvPrev_Cur_FillCombo:
276 // The only pixels that won't be drawable are inside
277 // the prior clip's finite bound
278 fFiniteBound = prevFinite;
279 fFiniteBoundType = kInsideOut_BoundsType;
280 break;
281 case kPrev_InvCur_FillCombo:
282 // The only pixels that won't be drawable are inside
283 // this clip's finite bound
284 break;
285 case kPrev_Cur_FillCombo:
286 fFiniteBound.join(prevFinite);
287 break;
288 default:
289 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
290 break;
291 }
292}
293
294// a mirror of combineBoundsUnion
295void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
296
297 switch (combination) {
298 case kInvPrev_InvCur_FillCombo:
299 // The only pixels that aren't writable in this case
300 // occur in the union of the two finite bounds
301 fFiniteBound.join(prevFinite);
302 fFiniteBoundType = kInsideOut_BoundsType;
303 break;
304 case kInvPrev_Cur_FillCombo:
305 // In this case the only pixels that will remain writeable
306 // are within the current clip
307 break;
308 case kPrev_InvCur_FillCombo:
309 // In this case the only pixels that will remain writeable
310 // are with the previous clip
311 fFiniteBound = prevFinite;
312 fFiniteBoundType = kNormal_BoundsType;
313 break;
314 case kPrev_Cur_FillCombo:
315 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000316 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000317 }
318 break;
319 default:
320 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
321 break;
322 }
323}
324
325// a mirror of combineBoundsDiff
326void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
327
328 switch (combination) {
329 case kInvPrev_InvCur_FillCombo:
330 // The only pixels that can survive are in the
331 // previous bound since the extensions to infinity in
332 // both clips cancel out
333 fFiniteBound = prevFinite;
334 fFiniteBoundType = kNormal_BoundsType;
335 break;
336 case kInvPrev_Cur_FillCombo:
337 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000338 this->setEmpty();
339 } else {
340 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000341 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000342 break;
343 case kPrev_InvCur_FillCombo:
344 fFiniteBound.join(prevFinite);
345 fFiniteBoundType = kInsideOut_BoundsType;
346 break;
347 case kPrev_Cur_FillCombo:
348 // Fall through - as with the kDifference_Op case, the
349 // most conservative result bound is the bound of the
350 // current clip. The prior clip could reduce the size of this
351 // bound (as in the kDifference_Op case) but we are ignoring
352 // those cases.
353 break;
354 default:
355 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
356 break;
357 }
358}
359
360void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
361 // We set this first here but we may overwrite it later if we determine that the clip is
362 // either wide-open or empty.
363 fGenID = GetNextGenID();
364
365 // First, optimistically update the current Element's bound information
366 // with the current clip's bound
367 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000368 switch (fType) {
369 case kRect_Type:
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000370 fFiniteBound = this->getRect();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000371 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000372
373 if (SkRegion::kReplace_Op == fOp ||
374 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
375 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000376 prior->rectRectIntersectAllowed(this->getRect(), fDoAA))) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000377 fIsIntersectionOfRects = true;
378 }
379 break;
380 case kRRect_Type:
381 fFiniteBound = fRRect.getBounds();
382 fFiniteBoundType = kNormal_BoundsType;
383 break;
384 case kPath_Type:
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000385 fFiniteBound = fPath.get()->getBounds();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000386
commit-bot@chromium.org6f954b92014-02-27 17:39:46 +0000387 if (fPath.get()->isInverseFillType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000388 fFiniteBoundType = kInsideOut_BoundsType;
389 } else {
390 fFiniteBoundType = kNormal_BoundsType;
391 }
392 break;
393 case kEmpty_Type:
394 SkDEBUGFAIL("We shouldn't get here with an empty element.");
395 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000397
398 if (!fDoAA) {
399 // Here we mimic a non-anti-aliased scanline system. If there is
400 // no anti-aliasing we can integerize the bounding box to exclude
401 // fractional parts that won't be rendered.
402 // Note: the left edge is handled slightly differently below. We
403 // are a bit more generous in the rounding since we don't want to
404 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000405 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
406 SkScalarRoundToScalar(fFiniteBound.fTop),
407 SkScalarRoundToScalar(fFiniteBound.fRight),
408 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000409 }
410
411 // Now determine the previous Element's bound information taking into
412 // account that there may be no previous clip
413 SkRect prevFinite;
414 SkClipStack::BoundsType prevType;
415
416 if (NULL == prior) {
417 // no prior clip means the entire plane is writable
418 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
419 prevType = kInsideOut_BoundsType;
420 } else {
421 prevFinite = prior->fFiniteBound;
422 prevType = prior->fFiniteBoundType;
423 }
424
425 FillCombo combination = kPrev_Cur_FillCombo;
426 if (kInsideOut_BoundsType == fFiniteBoundType) {
427 combination = (FillCombo) (combination | 0x01);
428 }
429 if (kInsideOut_BoundsType == prevType) {
430 combination = (FillCombo) (combination | 0x02);
431 }
432
433 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
434 kInvPrev_Cur_FillCombo == combination ||
435 kPrev_InvCur_FillCombo == combination ||
436 kPrev_Cur_FillCombo == combination);
437
438 // Now integrate with clip with the prior clips
439 switch (fOp) {
440 case SkRegion::kDifference_Op:
441 this->combineBoundsDiff(combination, prevFinite);
442 break;
443 case SkRegion::kXOR_Op:
444 this->combineBoundsXOR(combination, prevFinite);
445 break;
446 case SkRegion::kUnion_Op:
447 this->combineBoundsUnion(combination, prevFinite);
448 break;
449 case SkRegion::kIntersect_Op:
450 this->combineBoundsIntersection(combination, prevFinite);
451 break;
452 case SkRegion::kReverseDifference_Op:
453 this->combineBoundsRevDiff(combination, prevFinite);
454 break;
455 case SkRegion::kReplace_Op:
456 // Replace just ignores everything prior
457 // The current clip's bound information is already filled in
458 // so nothing to do
459 break;
460 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000461 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000462 SkASSERT(0);
463 break;
464 }
465}
reed@google.com5c3d1472011-02-22 19:12:23 +0000466
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000467// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000468// the deque. As such it needs to balance allocating too much memory vs.
469// incurring allocation/deallocation thrashing. It should roughly correspond to
470// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000471static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000472
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000473SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000474 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000475 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000476}
477
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000478SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000479 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000480 *this = b;
481}
482
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000483SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000484 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000485 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000486 if (!r.isEmpty()) {
487 this->clipDevRect(r, SkRegion::kReplace_Op, false);
488 }
489}
490
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000491SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000492 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000493 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000494 if (!r.isEmpty()) {
495 SkRect temp;
496 temp.set(r);
497 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
498 }
499}
500
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000501SkClipStack::~SkClipStack() {
502 reset();
503}
504
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000505SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
506 if (this == &b) {
507 return *this;
508 }
509 reset();
510
511 fSaveCount = b.fSaveCount;
512 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000513 for (const Element* element = (const Element*)recIter.next();
514 element != NULL;
515 element = (const Element*)recIter.next()) {
516 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000517 }
518
519 return *this;
520}
521
522bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000523 if (this->getTopmostGenID() == b.getTopmostGenID()) {
524 return true;
525 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000526 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000527 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000528 return false;
529 }
530 SkDeque::F2BIter myIter(fDeque);
531 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000532 const Element* myElement = (const Element*)myIter.next();
533 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000534
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000535 while (myElement != NULL && bElement != NULL) {
536 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000537 return false;
538 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000539 myElement = (const Element*)myIter.next();
540 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000541 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000542 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000543}
544
reed@google.com5c3d1472011-02-22 19:12:23 +0000545void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000546 // We used a placement new for each object in fDeque, so we're responsible
547 // for calling the destructor on each of them as well.
548 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000549 Element* element = (Element*)fDeque.back();
550 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000551 fDeque.pop_back();
552 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000553
554 fSaveCount = 0;
555}
556
557void SkClipStack::save() {
558 fSaveCount += 1;
559}
560
561void SkClipStack::restore() {
562 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000563 restoreTo(fSaveCount);
564}
565
566void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000567 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000568 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000569 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000570 break;
571 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000572 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000573 fDeque.pop_back();
574 }
575}
576
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000577void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000578 BoundsType* boundType,
579 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000580 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000581
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000582 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000583
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000584 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000585 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000586 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000587 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000588 if (NULL != isIntersectionOfRects) {
589 *isIntersectionOfRects = false;
590 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000591 return;
592 }
593
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000594 *canvFiniteBound = element->fFiniteBound;
595 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000596 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000597 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000598 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000599}
600
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000601bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
602 SkASSERT(NULL != rect);
603
604 SkRect bounds;
605 SkClipStack::BoundsType bt;
606 this->getBounds(&bounds, &bt);
607 if (bt == SkClipStack::kInsideOut_BoundsType) {
608 if (bounds.contains(*rect)) {
609 return false;
610 } else {
611 // If rect's x values are both within bound's x range we
612 // could clip here. Same for y. But we don't bother to check.
613 return true;
614 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000615 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000616 return rect->intersect(bounds);
617 }
618}
619
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000620bool SkClipStack::quickContains(const SkRect& rect) const {
621
622 Iter iter(*this, Iter::kTop_IterStart);
623 const Element* element = iter.prev();
624 while (element != NULL) {
625 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
626 return false;
627 if (element->isInverseFilled()) {
628 // Part of 'rect' could be trimmed off by the inverse-filled clip element
629 if (SkRect::Intersects(element->getBounds(), rect)) {
630 return false;
631 }
632 } else {
633 if (!element->contains(rect)) {
634 return false;
635 }
636 }
637 if (SkRegion::kReplace_Op == element->getOp()) {
638 break;
639 }
640 element = iter.prev();
641 }
642 return true;
643}
644
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000645void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000646 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000647 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000648 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000649
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000650 if (NULL != prior) {
651 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
652 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000653 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000654 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000655 return;
656 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000657 if (Element::kRect_Type == element.getType()) {
658 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000659 SkRect isectRect;
660 if (!isectRect.intersect(prior->getRect(), element.getRect())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000661 prior->setEmpty();
662 return;
663 }
664
commit-bot@chromium.org032a52f2014-02-21 20:09:13 +0000665 prior->fRRect.setRect(isectRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000666 prior->fDoAA = element.isAA();
667 Element* priorPrior = (Element*) iter.prev();
668 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000669 return;
670 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000671 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000672 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000673 // fallthrough
674 default:
675 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
676 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000677 return;
678 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000679 break;
680 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000681 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000682 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000683 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000684 }
685 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000686 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
687 newElement->updateBoundAndGenID(prior);
688}
689
690void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
691 Element element(fSaveCount, rrect, op, doAA);
692 this->pushElement(element);
693}
694
695void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
696 Element element(fSaveCount, rect, op, doAA);
697 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000698}
699
reed@google.com00177082011-10-12 14:34:30 +0000700void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000701 Element element(fSaveCount, path, op, doAA);
702 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000703}
704
reed@google.com0557d9e2012-08-16 15:59:59 +0000705void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000706 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000707
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000708 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000709 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000710 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000711 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000712
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000713 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000714}
715
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000716bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000717 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000718}
719
reed@google.com5c3d1472011-02-22 19:12:23 +0000720///////////////////////////////////////////////////////////////////////////////
721
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000722SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000723}
724
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000725SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
726 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000727 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000728}
729
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000730const SkClipStack::Element* SkClipStack::Iter::next() {
731 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000732}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000733
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000734const SkClipStack::Element* SkClipStack::Iter::prev() {
735 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000736}
737
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000738const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000739
740 if (NULL == fStack) {
741 return NULL;
742 }
743
744 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
745
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000746 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000747
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000748 for (element = (const SkClipStack::Element*) fIter.prev();
749 NULL != element;
750 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000751
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000752 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000753 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000754 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000755 // return, the iterator is actually pointing at (and will
756 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000757 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000758 // step so we get the expected result.
759 if (NULL == fIter.next()) {
760 // The reverse iterator has run off the front of the deque
761 // (i.e., the "op" clip is the first clip) and can't
762 // recover. Reset the iterator to start at the front.
763 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
764 }
765 break;
766 }
767 }
768
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000769 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000770 // There were no "op" clips
771 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
772 }
773
774 return this->next();
775}
776
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000777void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000778 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000779 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000780}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000781
782// helper method
783void SkClipStack::getConservativeBounds(int offsetX,
784 int offsetY,
785 int maxWidth,
786 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000787 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000788 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000789 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000790
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000791 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000792 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000793
794 SkRect temp;
795 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000796
robertphillips@google.com7b112892012-07-31 15:18:21 +0000797 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000798 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000799 if (SkClipStack::kInsideOut_BoundsType == boundType) {
800 return;
801 }
802
robertphillips@google.com7b112892012-07-31 15:18:21 +0000803 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000804 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
805
robertphillips@google.com7b112892012-07-31 15:18:21 +0000806 if (!devBounds->intersect(temp)) {
807 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000808 }
809}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000810
robertphillips@google.com46f93502012-08-07 15:38:08 +0000811int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000812 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000813 return sk_atomic_inc(&gGenID);
814}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000815
816int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000817 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000818 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000819 }
820
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000821 const Element* back = static_cast<const Element*>(fDeque.back());
822 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
823 return kWideOpenGenID;
824 }
825
826 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000827}