blob: 17eb6f931d31f04d344c27ecc0228fe7a100ce0a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com5c3d1472011-02-22 19:12:23 +00008#include "SkClipStack.h"
9#include "SkPath.h"
robertphillips@google.com46f93502012-08-07 15:38:08 +000010#include "SkThread.h"
11
reed@google.com5c3d1472011-02-22 19:12:23 +000012#include <new>
13
robertphillips@google.com607fe072012-07-24 13:54:00 +000014
robertphillips@google.com46f93502012-08-07 15:38:08 +000015// 0-2 are reserved for invalid, empty & wide-open
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000016static const int32_t kFirstUnreservedGenID = 3;
17int32_t SkClipStack::gGenID = kFirstUnreservedGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +000018
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000019bool SkClipStack::Element::operator== (const Element& element) const {
20 if (this == &element) {
21 return true;
22 }
23 if (fOp != element.fOp ||
24 fType != element.fType ||
25 fDoAA != element.fDoAA ||
26 fSaveCount != element.fSaveCount) {
27 return false;
28 }
29 switch (fType) {
30 case kPath_Type:
31 return fPath == element.fPath;
32 case kRRect_Type:
33 return fRRect == element.fRRect;
34 case kRect_Type:
35 return fRect == element.fRect;
36 case kEmpty_Type:
37 return true;
38 default:
39 SkDEBUGFAIL("Unexpected type.");
40 return false;
41 }
42}
43
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000044void SkClipStack::Element::invertShapeFillType() {
45 switch (fType) {
46 case kRect_Type:
47 fPath.reset();
48 fPath.addRect(fRect);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000049 fPath.setFillType(SkPath::kInverseEvenOdd_FillType);
50 fType = kPath_Type;
51 break;
52 case kRRect_Type:
53 fPath.reset();
54 fPath.addRRect(fRRect);
55 fPath.setFillType(SkPath::kInverseEvenOdd_FillType);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000056 fType = kPath_Type;
57 break;
58 case kPath_Type:
59 fPath.toggleInverseFillType();
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000060 break;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000061 case kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000062 // Should this set to an empty, inverse filled path?
63 break;
64 }
65}
66
67void SkClipStack::Element::initPath(int saveCount, const SkPath& path, SkRegion::Op op,
68 bool doAA) {
69 if (!path.isInverseFillType()) {
70 if (SkPath::kNone_PathAsRect != path.asRect()) {
71 this->initRect(saveCount, path.getBounds(), op, doAA);
72 return;
73 }
74 SkRect ovalRect;
75 if (path.isOval(&ovalRect)) {
76 SkRRect rrect;
77 rrect.setOval(ovalRect);
78 this->initRRect(saveCount, rrect, op, doAA);
79 return;
80 }
81 }
82 fPath = path;
83 fType = kPath_Type;
84 this->initCommon(saveCount, op, doAA);
85}
86
87void SkClipStack::Element::asPath(SkPath* path) const {
88 switch (fType) {
89 case kEmpty_Type:
90 path->reset();
91 break;
92 case kRect_Type:
93 path->reset();
94 path->addRect(fRect);
95 break;
96 case kRRect_Type:
97 path->reset();
98 path->addRRect(fRRect);
99 break;
100 case kPath_Type:
101 *path = fPath;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000102 break;
103 }
104}
105
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000106void SkClipStack::Element::checkEmpty() const {
107 SkASSERT(fFiniteBound.isEmpty());
108 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
109 SkASSERT(!fIsIntersectionOfRects);
110 SkASSERT(kEmptyGenID == fGenID);
111 SkASSERT(fPath.isEmpty());
112}
reed@google.com5c3d1472011-02-22 19:12:23 +0000113
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000114bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
115 if (kEmpty_Type == fType &&
116 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
117 return true;
118 }
119 // Only clips within the same save/restore frame (as captured by
120 // the save count) can be merged
121 return fSaveCount == saveCount &&
122 SkRegion::kIntersect_Op == op &&
123 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
124}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000125
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000126bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
127 SkASSERT(kRect_Type == fType);
128
129 if (fDoAA == newAA) {
130 // if the AA setting is the same there is no issue
131 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000132 }
133
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000134 if (!SkRect::Intersects(fRect, newR)) {
135 // The calling code will correctly set the result to the empty clip
136 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000137 }
138
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000139 if (fRect.contains(newR)) {
140 // if the new rect carves out a portion of the old one there is no
141 // issue
142 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000143 }
144
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000145 // So either the two overlap in some complex manner or newR contains oldR.
146 // In the first, case the edges will require different AA. In the second,
147 // the AA setting that would be carried forward is incorrect (e.g., oldR
148 // is AA while newR is BW but since newR contains oldR, oldR will be
149 // drawn BW) since the new AA setting will predominate.
150 return false;
151}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000152
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000153// a mirror of combineBoundsRevDiff
154void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
155 switch (combination) {
156 case kInvPrev_InvCur_FillCombo:
157 // In this case the only pixels that can remain set
158 // are inside the current clip rect since the extensions
159 // to infinity of both clips cancel out and whatever
160 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +0000161 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000162 break;
163 case kInvPrev_Cur_FillCombo:
164 // In this case the current op is finite so the only pixels
165 // that aren't set are whatever isn't set in the previous
166 // clip and whatever this clip carves out
167 fFiniteBound.join(prevFinite);
168 fFiniteBoundType = kInsideOut_BoundsType;
169 break;
170 case kPrev_InvCur_FillCombo:
171 // In this case everything outside of this clip's bound
172 // is erased, so the only pixels that can remain set
173 // occur w/in the intersection of the two finite bounds
174 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000175 this->setEmpty();
176 } else {
177 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000178 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000179 break;
180 case kPrev_Cur_FillCombo:
181 // The most conservative result bound is that of the
182 // prior clip. This could be wildly incorrect if the
183 // second clip either exactly matches the first clip
184 // (which should yield the empty set) or reduces the
185 // size of the prior bound (e.g., if the second clip
186 // exactly matched the bottom half of the prior clip).
187 // We ignore these two possibilities.
188 fFiniteBound = prevFinite;
189 break;
190 default:
191 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
192 break;
193 }
194}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000195
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000196void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000198 switch (combination) {
199 case kInvPrev_Cur_FillCombo: // fall through
200 case kPrev_InvCur_FillCombo:
201 // With only one of the clips inverted the result will always
202 // extend to infinity. The only pixels that may be un-writeable
203 // lie within the union of the two finite bounds
204 fFiniteBound.join(prevFinite);
205 fFiniteBoundType = kInsideOut_BoundsType;
206 break;
207 case kInvPrev_InvCur_FillCombo:
208 // The only pixels that can survive are within the
209 // union of the two bounding boxes since the extensions
210 // to infinity of both clips cancel out
211 // fall through!
212 case kPrev_Cur_FillCombo:
213 // The most conservative bound for xor is the
214 // union of the two bounds. If the two clips exactly overlapped
215 // the xor could yield the empty set. Similarly the xor
216 // could reduce the size of the original clip's bound (e.g.,
217 // if the second clip exactly matched the bottom half of the
218 // first clip). We ignore these two cases.
219 fFiniteBound.join(prevFinite);
220 fFiniteBoundType = kNormal_BoundsType;
221 break;
222 default:
223 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
224 break;
225 }
226}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000227
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000228// a mirror of combineBoundsIntersection
229void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
230
231 switch (combination) {
232 case kInvPrev_InvCur_FillCombo:
233 if (!fFiniteBound.intersect(prevFinite)) {
234 fFiniteBound.setEmpty();
235 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000236 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000237 fFiniteBoundType = kInsideOut_BoundsType;
238 break;
239 case kInvPrev_Cur_FillCombo:
240 // The only pixels that won't be drawable are inside
241 // the prior clip's finite bound
242 fFiniteBound = prevFinite;
243 fFiniteBoundType = kInsideOut_BoundsType;
244 break;
245 case kPrev_InvCur_FillCombo:
246 // The only pixels that won't be drawable are inside
247 // this clip's finite bound
248 break;
249 case kPrev_Cur_FillCombo:
250 fFiniteBound.join(prevFinite);
251 break;
252 default:
253 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
254 break;
255 }
256}
257
258// a mirror of combineBoundsUnion
259void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
260
261 switch (combination) {
262 case kInvPrev_InvCur_FillCombo:
263 // The only pixels that aren't writable in this case
264 // occur in the union of the two finite bounds
265 fFiniteBound.join(prevFinite);
266 fFiniteBoundType = kInsideOut_BoundsType;
267 break;
268 case kInvPrev_Cur_FillCombo:
269 // In this case the only pixels that will remain writeable
270 // are within the current clip
271 break;
272 case kPrev_InvCur_FillCombo:
273 // In this case the only pixels that will remain writeable
274 // are with the previous clip
275 fFiniteBound = prevFinite;
276 fFiniteBoundType = kNormal_BoundsType;
277 break;
278 case kPrev_Cur_FillCombo:
279 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000280 this->setEmpty();
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000281 }
282 break;
283 default:
284 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
285 break;
286 }
287}
288
289// a mirror of combineBoundsDiff
290void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
291
292 switch (combination) {
293 case kInvPrev_InvCur_FillCombo:
294 // The only pixels that can survive are in the
295 // previous bound since the extensions to infinity in
296 // both clips cancel out
297 fFiniteBound = prevFinite;
298 fFiniteBoundType = kNormal_BoundsType;
299 break;
300 case kInvPrev_Cur_FillCombo:
301 if (!fFiniteBound.intersect(prevFinite)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000302 this->setEmpty();
303 } else {
304 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000305 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000306 break;
307 case kPrev_InvCur_FillCombo:
308 fFiniteBound.join(prevFinite);
309 fFiniteBoundType = kInsideOut_BoundsType;
310 break;
311 case kPrev_Cur_FillCombo:
312 // Fall through - as with the kDifference_Op case, the
313 // most conservative result bound is the bound of the
314 // current clip. The prior clip could reduce the size of this
315 // bound (as in the kDifference_Op case) but we are ignoring
316 // those cases.
317 break;
318 default:
319 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
320 break;
321 }
322}
323
324void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
325 // We set this first here but we may overwrite it later if we determine that the clip is
326 // either wide-open or empty.
327 fGenID = GetNextGenID();
328
329 // First, optimistically update the current Element's bound information
330 // with the current clip's bound
331 fIsIntersectionOfRects = false;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000332 switch (fType) {
333 case kRect_Type:
334 fFiniteBound = fRect;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000335 fFiniteBoundType = kNormal_BoundsType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000336
337 if (SkRegion::kReplace_Op == fOp ||
338 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
339 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
340 prior->rectRectIntersectAllowed(fRect, fDoAA))) {
341 fIsIntersectionOfRects = true;
342 }
343 break;
344 case kRRect_Type:
345 fFiniteBound = fRRect.getBounds();
346 fFiniteBoundType = kNormal_BoundsType;
347 break;
348 case kPath_Type:
349 fFiniteBound = fPath.getBounds();
350
351 if (fPath.isInverseFillType()) {
352 fFiniteBoundType = kInsideOut_BoundsType;
353 } else {
354 fFiniteBoundType = kNormal_BoundsType;
355 }
356 break;
357 case kEmpty_Type:
358 SkDEBUGFAIL("We shouldn't get here with an empty element.");
359 break;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000360 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000361
362 if (!fDoAA) {
363 // Here we mimic a non-anti-aliased scanline system. If there is
364 // no anti-aliasing we can integerize the bounding box to exclude
365 // fractional parts that won't be rendered.
366 // Note: the left edge is handled slightly differently below. We
367 // are a bit more generous in the rounding since we don't want to
368 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000369 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
370 SkScalarRoundToScalar(fFiniteBound.fTop),
371 SkScalarRoundToScalar(fFiniteBound.fRight),
372 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000373 }
374
375 // Now determine the previous Element's bound information taking into
376 // account that there may be no previous clip
377 SkRect prevFinite;
378 SkClipStack::BoundsType prevType;
379
380 if (NULL == prior) {
381 // no prior clip means the entire plane is writable
382 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
383 prevType = kInsideOut_BoundsType;
384 } else {
385 prevFinite = prior->fFiniteBound;
386 prevType = prior->fFiniteBoundType;
387 }
388
389 FillCombo combination = kPrev_Cur_FillCombo;
390 if (kInsideOut_BoundsType == fFiniteBoundType) {
391 combination = (FillCombo) (combination | 0x01);
392 }
393 if (kInsideOut_BoundsType == prevType) {
394 combination = (FillCombo) (combination | 0x02);
395 }
396
397 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
398 kInvPrev_Cur_FillCombo == combination ||
399 kPrev_InvCur_FillCombo == combination ||
400 kPrev_Cur_FillCombo == combination);
401
402 // Now integrate with clip with the prior clips
403 switch (fOp) {
404 case SkRegion::kDifference_Op:
405 this->combineBoundsDiff(combination, prevFinite);
406 break;
407 case SkRegion::kXOR_Op:
408 this->combineBoundsXOR(combination, prevFinite);
409 break;
410 case SkRegion::kUnion_Op:
411 this->combineBoundsUnion(combination, prevFinite);
412 break;
413 case SkRegion::kIntersect_Op:
414 this->combineBoundsIntersection(combination, prevFinite);
415 break;
416 case SkRegion::kReverseDifference_Op:
417 this->combineBoundsRevDiff(combination, prevFinite);
418 break;
419 case SkRegion::kReplace_Op:
420 // Replace just ignores everything prior
421 // The current clip's bound information is already filled in
422 // so nothing to do
423 break;
424 default:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000425 SkDebugf("SkRegion::Op error\n");
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000426 SkASSERT(0);
427 break;
428 }
429}
reed@google.com5c3d1472011-02-22 19:12:23 +0000430
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000431// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000432// the deque. As such it needs to balance allocating too much memory vs.
433// incurring allocation/deallocation thrashing. It should roughly correspond to
434// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000435static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000436
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000438 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000439 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000440}
441
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000442SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000443 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000444 *this = b;
445}
446
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000447SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000448 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000449 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000450 if (!r.isEmpty()) {
451 this->clipDevRect(r, SkRegion::kReplace_Op, false);
452 }
453}
454
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000455SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000456 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000457 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000458 if (!r.isEmpty()) {
459 SkRect temp;
460 temp.set(r);
461 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
462 }
463}
464
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000465SkClipStack::~SkClipStack() {
466 reset();
467}
468
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000469SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
470 if (this == &b) {
471 return *this;
472 }
473 reset();
474
475 fSaveCount = b.fSaveCount;
476 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000477 for (const Element* element = (const Element*)recIter.next();
478 element != NULL;
479 element = (const Element*)recIter.next()) {
480 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000481 }
482
483 return *this;
484}
485
486bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000487 if (this->getTopmostGenID() == b.getTopmostGenID()) {
488 return true;
489 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000490 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000491 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000492 return false;
493 }
494 SkDeque::F2BIter myIter(fDeque);
495 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000496 const Element* myElement = (const Element*)myIter.next();
497 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000498
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000499 while (myElement != NULL && bElement != NULL) {
500 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000501 return false;
502 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000503 myElement = (const Element*)myIter.next();
504 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000505 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000506 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000507}
508
reed@google.com5c3d1472011-02-22 19:12:23 +0000509void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000510 // We used a placement new for each object in fDeque, so we're responsible
511 // for calling the destructor on each of them as well.
512 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000513 Element* element = (Element*)fDeque.back();
514 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000515 fDeque.pop_back();
516 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000517
518 fSaveCount = 0;
519}
520
521void SkClipStack::save() {
522 fSaveCount += 1;
523}
524
525void SkClipStack::restore() {
526 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000527 restoreTo(fSaveCount);
528}
529
530void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000531 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000532 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000533 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000534 break;
535 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000536 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000537 fDeque.pop_back();
538 }
539}
540
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000541void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000542 BoundsType* boundType,
543 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000544 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000545
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000546 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000547
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000548 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000549 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000550 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000551 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000552 if (NULL != isIntersectionOfRects) {
553 *isIntersectionOfRects = false;
554 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000555 return;
556 }
557
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000558 *canvFiniteBound = element->fFiniteBound;
559 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000560 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000561 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000562 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000563}
564
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000565bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
566 SkASSERT(NULL != rect);
567
568 SkRect bounds;
569 SkClipStack::BoundsType bt;
570 this->getBounds(&bounds, &bt);
571 if (bt == SkClipStack::kInsideOut_BoundsType) {
572 if (bounds.contains(*rect)) {
573 return false;
574 } else {
575 // If rect's x values are both within bound's x range we
576 // could clip here. Same for y. But we don't bother to check.
577 return true;
578 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000579 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000580 return rect->intersect(bounds);
581 }
582}
583
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000584bool SkClipStack::quickContains(const SkRect& rect) const {
585
586 Iter iter(*this, Iter::kTop_IterStart);
587 const Element* element = iter.prev();
588 while (element != NULL) {
589 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
590 return false;
591 if (element->isInverseFilled()) {
592 // Part of 'rect' could be trimmed off by the inverse-filled clip element
593 if (SkRect::Intersects(element->getBounds(), rect)) {
594 return false;
595 }
596 } else {
597 if (!element->contains(rect)) {
598 return false;
599 }
600 }
601 if (SkRegion::kReplace_Op == element->getOp()) {
602 break;
603 }
604 element = iter.prev();
605 }
606 return true;
607}
608
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000609void SkClipStack::pushElement(const Element& element) {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000610 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000611 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000612 Element* prior = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000613
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000614 if (NULL != prior) {
615 if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) {
616 switch (prior->fType) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000617 case Element::kEmpty_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000618 SkDEBUGCODE(prior->checkEmpty();)
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000619 return;
620 case Element::kRect_Type:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000621 if (Element::kRect_Type == element.getType()) {
622 if (prior->rectRectIntersectAllowed(element.getRect(), element.isAA())) {
623 if (!prior->fRect.intersect(element.getRect())) {
624 prior->setEmpty();
625 return;
626 }
627
628 prior->fDoAA = element.isAA();
629 Element* priorPrior = (Element*) iter.prev();
630 prior->updateBoundAndGenID(priorPrior);
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000631 return;
632 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000633 break;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000634 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000635 // fallthrough
636 default:
637 if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) {
638 prior->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000639 return;
640 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000641 break;
642 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000643 } else if (SkRegion::kReplace_Op == element.getOp()) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000644 this->restoreTo(fSaveCount - 1);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000645 prior = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000646 }
647 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000648 Element* newElement = SkNEW_PLACEMENT_ARGS(fDeque.push_back(), Element, (element));
649 newElement->updateBoundAndGenID(prior);
650}
651
652void SkClipStack::clipDevRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
653 Element element(fSaveCount, rrect, op, doAA);
654 this->pushElement(element);
655}
656
657void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
658 Element element(fSaveCount, rect, op, doAA);
659 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000660}
661
reed@google.com00177082011-10-12 14:34:30 +0000662void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000663 Element element(fSaveCount, path, op, doAA);
664 this->pushElement(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000665}
666
reed@google.com0557d9e2012-08-16 15:59:59 +0000667void SkClipStack::clipEmpty() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000668 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000669
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000670 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000671 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000672 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000673 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000675 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000676}
677
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000678bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000679 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000680}
681
reed@google.com5c3d1472011-02-22 19:12:23 +0000682///////////////////////////////////////////////////////////////////////////////
683
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000684SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000685}
686
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000687SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
688 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000689 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000690}
691
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000692const SkClipStack::Element* SkClipStack::Iter::next() {
693 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000694}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000695
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000696const SkClipStack::Element* SkClipStack::Iter::prev() {
697 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000698}
699
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000700const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000701
702 if (NULL == fStack) {
703 return NULL;
704 }
705
706 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
707
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000708 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000709
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000710 for (element = (const SkClipStack::Element*) fIter.prev();
711 NULL != element;
712 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000713
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000714 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000715 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000716 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000717 // return, the iterator is actually pointing at (and will
718 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000720 // step so we get the expected result.
721 if (NULL == fIter.next()) {
722 // The reverse iterator has run off the front of the deque
723 // (i.e., the "op" clip is the first clip) and can't
724 // recover. Reset the iterator to start at the front.
725 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
726 }
727 break;
728 }
729 }
730
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000731 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000732 // There were no "op" clips
733 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
734 }
735
736 return this->next();
737}
738
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000739void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000740 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000741 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000742}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000743
744// helper method
745void SkClipStack::getConservativeBounds(int offsetX,
746 int offsetY,
747 int maxWidth,
748 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000749 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000750 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000751 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000752
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000753 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000754 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000755
756 SkRect temp;
757 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000758
robertphillips@google.com7b112892012-07-31 15:18:21 +0000759 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000760 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000761 if (SkClipStack::kInsideOut_BoundsType == boundType) {
762 return;
763 }
764
robertphillips@google.com7b112892012-07-31 15:18:21 +0000765 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000766 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
767
robertphillips@google.com7b112892012-07-31 15:18:21 +0000768 if (!devBounds->intersect(temp)) {
769 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000770 }
771}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000772
robertphillips@google.com46f93502012-08-07 15:38:08 +0000773int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000774 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000775 return sk_atomic_inc(&gGenID);
776}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000777
778int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000779 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000780 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000781 }
782
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000783 const Element* back = static_cast<const Element*>(fDeque.back());
784 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
785 return kWideOpenGenID;
786 }
787
788 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000789}