blob: c66a219b629bbe7c467eafa946e2487fe067afbe [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
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000019void SkClipStack::Element::invertShapeFillType() {
20 switch (fType) {
21 case kRect_Type:
22 fPath.reset();
23 fPath.addRect(fRect);
24 fPath.setFillType(SkPath::kInverseWinding_FillType);
25 fType = kPath_Type;
26 break;
27 case kPath_Type:
28 fPath.toggleInverseFillType();
29 case kEmpty_Type:
30 break;
31 }
32}
33
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000034void SkClipStack::Element::checkEmpty() const {
35 SkASSERT(fFiniteBound.isEmpty());
36 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
37 SkASSERT(!fIsIntersectionOfRects);
38 SkASSERT(kEmptyGenID == fGenID);
39 SkASSERT(fPath.isEmpty());
40}
reed@google.com5c3d1472011-02-22 19:12:23 +000041
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000042bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
43 if (kEmpty_Type == fType &&
44 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
45 return true;
46 }
47 // Only clips within the same save/restore frame (as captured by
48 // the save count) can be merged
49 return fSaveCount == saveCount &&
50 SkRegion::kIntersect_Op == op &&
51 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
52}
robertphillips@google.com607fe072012-07-24 13:54:00 +000053
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000054bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
55 SkASSERT(kRect_Type == fType);
56
57 if (fDoAA == newAA) {
58 // if the AA setting is the same there is no issue
59 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000060 }
61
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000062 if (!SkRect::Intersects(fRect, newR)) {
63 // The calling code will correctly set the result to the empty clip
64 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000065 }
66
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000067 if (fRect.contains(newR)) {
68 // if the new rect carves out a portion of the old one there is no
69 // issue
70 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000071 }
72
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000073 // So either the two overlap in some complex manner or newR contains oldR.
74 // In the first, case the edges will require different AA. In the second,
75 // the AA setting that would be carried forward is incorrect (e.g., oldR
76 // is AA while newR is BW but since newR contains oldR, oldR will be
77 // drawn BW) since the new AA setting will predominate.
78 return false;
79}
robertphillips@google.com607fe072012-07-24 13:54:00 +000080
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000081// a mirror of combineBoundsRevDiff
82void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
83 switch (combination) {
84 case kInvPrev_InvCur_FillCombo:
85 // In this case the only pixels that can remain set
86 // are inside the current clip rect since the extensions
87 // to infinity of both clips cancel out and whatever
88 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +000089 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000090 break;
91 case kInvPrev_Cur_FillCombo:
92 // In this case the current op is finite so the only pixels
93 // that aren't set are whatever isn't set in the previous
94 // clip and whatever this clip carves out
95 fFiniteBound.join(prevFinite);
96 fFiniteBoundType = kInsideOut_BoundsType;
97 break;
98 case kPrev_InvCur_FillCombo:
99 // In this case everything outside of this clip's bound
100 // is erased, so the only pixels that can remain set
101 // occur w/in the intersection of the two finite bounds
102 if (!fFiniteBound.intersect(prevFinite)) {
103 fFiniteBound.setEmpty();
104 fGenID = kEmptyGenID;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000105 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000106 fFiniteBoundType = kNormal_BoundsType;
107 break;
108 case kPrev_Cur_FillCombo:
109 // The most conservative result bound is that of the
110 // prior clip. This could be wildly incorrect if the
111 // second clip either exactly matches the first clip
112 // (which should yield the empty set) or reduces the
113 // size of the prior bound (e.g., if the second clip
114 // exactly matched the bottom half of the prior clip).
115 // We ignore these two possibilities.
116 fFiniteBound = prevFinite;
117 break;
118 default:
119 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
120 break;
121 }
122}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000123
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000124void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000126 switch (combination) {
127 case kInvPrev_Cur_FillCombo: // fall through
128 case kPrev_InvCur_FillCombo:
129 // With only one of the clips inverted the result will always
130 // extend to infinity. The only pixels that may be un-writeable
131 // lie within the union of the two finite bounds
132 fFiniteBound.join(prevFinite);
133 fFiniteBoundType = kInsideOut_BoundsType;
134 break;
135 case kInvPrev_InvCur_FillCombo:
136 // The only pixels that can survive are within the
137 // union of the two bounding boxes since the extensions
138 // to infinity of both clips cancel out
139 // fall through!
140 case kPrev_Cur_FillCombo:
141 // The most conservative bound for xor is the
142 // union of the two bounds. If the two clips exactly overlapped
143 // the xor could yield the empty set. Similarly the xor
144 // could reduce the size of the original clip's bound (e.g.,
145 // if the second clip exactly matched the bottom half of the
146 // first clip). We ignore these two cases.
147 fFiniteBound.join(prevFinite);
148 fFiniteBoundType = kNormal_BoundsType;
149 break;
150 default:
151 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
152 break;
153 }
154}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000155
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000156// a mirror of combineBoundsIntersection
157void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
158
159 switch (combination) {
160 case kInvPrev_InvCur_FillCombo:
161 if (!fFiniteBound.intersect(prevFinite)) {
162 fFiniteBound.setEmpty();
163 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000164 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000165 fFiniteBoundType = kInsideOut_BoundsType;
166 break;
167 case kInvPrev_Cur_FillCombo:
168 // The only pixels that won't be drawable are inside
169 // the prior clip's finite bound
170 fFiniteBound = prevFinite;
171 fFiniteBoundType = kInsideOut_BoundsType;
172 break;
173 case kPrev_InvCur_FillCombo:
174 // The only pixels that won't be drawable are inside
175 // this clip's finite bound
176 break;
177 case kPrev_Cur_FillCombo:
178 fFiniteBound.join(prevFinite);
179 break;
180 default:
181 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
182 break;
183 }
184}
185
186// a mirror of combineBoundsUnion
187void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
188
189 switch (combination) {
190 case kInvPrev_InvCur_FillCombo:
191 // The only pixels that aren't writable in this case
192 // occur in the union of the two finite bounds
193 fFiniteBound.join(prevFinite);
194 fFiniteBoundType = kInsideOut_BoundsType;
195 break;
196 case kInvPrev_Cur_FillCombo:
197 // In this case the only pixels that will remain writeable
198 // are within the current clip
199 break;
200 case kPrev_InvCur_FillCombo:
201 // In this case the only pixels that will remain writeable
202 // are with the previous clip
203 fFiniteBound = prevFinite;
204 fFiniteBoundType = kNormal_BoundsType;
205 break;
206 case kPrev_Cur_FillCombo:
207 if (!fFiniteBound.intersect(prevFinite)) {
208 fFiniteBound.setEmpty();
209 fGenID = kEmptyGenID;
210 }
211 break;
212 default:
213 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
214 break;
215 }
216}
217
218// a mirror of combineBoundsDiff
219void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
220
221 switch (combination) {
222 case kInvPrev_InvCur_FillCombo:
223 // The only pixels that can survive are in the
224 // previous bound since the extensions to infinity in
225 // both clips cancel out
226 fFiniteBound = prevFinite;
227 fFiniteBoundType = kNormal_BoundsType;
228 break;
229 case kInvPrev_Cur_FillCombo:
230 if (!fFiniteBound.intersect(prevFinite)) {
231 fFiniteBound.setEmpty();
232 fGenID = kEmptyGenID;
233 }
234 fFiniteBoundType = kNormal_BoundsType;
235 break;
236 case kPrev_InvCur_FillCombo:
237 fFiniteBound.join(prevFinite);
238 fFiniteBoundType = kInsideOut_BoundsType;
239 break;
240 case kPrev_Cur_FillCombo:
241 // Fall through - as with the kDifference_Op case, the
242 // most conservative result bound is the bound of the
243 // current clip. The prior clip could reduce the size of this
244 // bound (as in the kDifference_Op case) but we are ignoring
245 // those cases.
246 break;
247 default:
248 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
249 break;
250 }
251}
252
253void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
254 // We set this first here but we may overwrite it later if we determine that the clip is
255 // either wide-open or empty.
256 fGenID = GetNextGenID();
257
258 // First, optimistically update the current Element's bound information
259 // with the current clip's bound
260 fIsIntersectionOfRects = false;
261 if (kRect_Type == fType) {
262 fFiniteBound = fRect;
263 fFiniteBoundType = kNormal_BoundsType;
264
265 if (SkRegion::kReplace_Op == fOp ||
266 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
267 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
268 prior->rectRectIntersectAllowed(fRect, fDoAA))) {
269 fIsIntersectionOfRects = true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000270 }
271
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000272 } else {
273 SkASSERT(kPath_Type == fType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000274
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000275 fFiniteBound = fPath.getBounds();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000276
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000277 if (fPath.isInverseFillType()) {
278 fFiniteBoundType = kInsideOut_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000279 } else {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000280 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000281 }
282 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000283
284 if (!fDoAA) {
285 // Here we mimic a non-anti-aliased scanline system. If there is
286 // no anti-aliasing we can integerize the bounding box to exclude
287 // fractional parts that won't be rendered.
288 // Note: the left edge is handled slightly differently below. We
289 // are a bit more generous in the rounding since we don't want to
290 // risk missing the left pixels when fLeft is very close to .5
reed@google.come1ca7052013-12-17 19:22:07 +0000291 fFiniteBound.set(SkScalarFloorToScalar(fFiniteBound.fLeft+0.45f),
292 SkScalarRoundToScalar(fFiniteBound.fTop),
293 SkScalarRoundToScalar(fFiniteBound.fRight),
294 SkScalarRoundToScalar(fFiniteBound.fBottom));
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000295 }
296
297 // Now determine the previous Element's bound information taking into
298 // account that there may be no previous clip
299 SkRect prevFinite;
300 SkClipStack::BoundsType prevType;
301
302 if (NULL == prior) {
303 // no prior clip means the entire plane is writable
304 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
305 prevType = kInsideOut_BoundsType;
306 } else {
307 prevFinite = prior->fFiniteBound;
308 prevType = prior->fFiniteBoundType;
309 }
310
311 FillCombo combination = kPrev_Cur_FillCombo;
312 if (kInsideOut_BoundsType == fFiniteBoundType) {
313 combination = (FillCombo) (combination | 0x01);
314 }
315 if (kInsideOut_BoundsType == prevType) {
316 combination = (FillCombo) (combination | 0x02);
317 }
318
319 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
320 kInvPrev_Cur_FillCombo == combination ||
321 kPrev_InvCur_FillCombo == combination ||
322 kPrev_Cur_FillCombo == combination);
323
324 // Now integrate with clip with the prior clips
325 switch (fOp) {
326 case SkRegion::kDifference_Op:
327 this->combineBoundsDiff(combination, prevFinite);
328 break;
329 case SkRegion::kXOR_Op:
330 this->combineBoundsXOR(combination, prevFinite);
331 break;
332 case SkRegion::kUnion_Op:
333 this->combineBoundsUnion(combination, prevFinite);
334 break;
335 case SkRegion::kIntersect_Op:
336 this->combineBoundsIntersection(combination, prevFinite);
337 break;
338 case SkRegion::kReverseDifference_Op:
339 this->combineBoundsRevDiff(combination, prevFinite);
340 break;
341 case SkRegion::kReplace_Op:
342 // Replace just ignores everything prior
343 // The current clip's bound information is already filled in
344 // so nothing to do
345 break;
346 default:
347 SkDebugf("SkRegion::Op error/n");
348 SkASSERT(0);
349 break;
350 }
351}
reed@google.com5c3d1472011-02-22 19:12:23 +0000352
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000353// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000354// the deque. As such it needs to balance allocating too much memory vs.
355// incurring allocation/deallocation thrashing. It should roughly correspond to
356// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000357static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000358
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000359SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000360 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000361 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000362}
363
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000364SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000365 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000366 *this = b;
367}
368
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000369SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000370 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000371 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000372 if (!r.isEmpty()) {
373 this->clipDevRect(r, SkRegion::kReplace_Op, false);
374 }
375}
376
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000377SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000378 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000379 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000380 if (!r.isEmpty()) {
381 SkRect temp;
382 temp.set(r);
383 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
384 }
385}
386
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000387SkClipStack::~SkClipStack() {
388 reset();
389}
390
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000391SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
392 if (this == &b) {
393 return *this;
394 }
395 reset();
396
397 fSaveCount = b.fSaveCount;
398 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000399 for (const Element* element = (const Element*)recIter.next();
400 element != NULL;
401 element = (const Element*)recIter.next()) {
402 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000403 }
404
405 return *this;
406}
407
408bool SkClipStack::operator==(const SkClipStack& b) const {
commit-bot@chromium.org86b39f32014-01-06 16:54:20 +0000409 if (this->getTopmostGenID() == b.getTopmostGenID()) {
410 return true;
411 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000412 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000413 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000414 return false;
415 }
416 SkDeque::F2BIter myIter(fDeque);
417 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000418 const Element* myElement = (const Element*)myIter.next();
419 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000420
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000421 while (myElement != NULL && bElement != NULL) {
422 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000423 return false;
424 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000425 myElement = (const Element*)myIter.next();
426 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000427 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000428 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000429}
430
reed@google.com5c3d1472011-02-22 19:12:23 +0000431void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000432 // We used a placement new for each object in fDeque, so we're responsible
433 // for calling the destructor on each of them as well.
434 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000435 Element* element = (Element*)fDeque.back();
436 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000437 fDeque.pop_back();
438 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000439
440 fSaveCount = 0;
441}
442
443void SkClipStack::save() {
444 fSaveCount += 1;
445}
446
447void SkClipStack::restore() {
448 fSaveCount -= 1;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000449 restoreTo(fSaveCount);
450}
451
452void SkClipStack::restoreTo(int saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000453 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000454 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000455 if (element->fSaveCount <= saveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000456 break;
457 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000458 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000459 fDeque.pop_back();
460 }
461}
462
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000463void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000464 BoundsType* boundType,
465 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000466 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000467
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000468 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000469
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000470 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000471 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000472 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000473 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000474 if (NULL != isIntersectionOfRects) {
475 *isIntersectionOfRects = false;
476 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000477 return;
478 }
479
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000480 *canvFiniteBound = element->fFiniteBound;
481 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000482 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000483 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000484 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000485}
486
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000487bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
488 SkASSERT(NULL != rect);
489
490 SkRect bounds;
491 SkClipStack::BoundsType bt;
492 this->getBounds(&bounds, &bt);
493 if (bt == SkClipStack::kInsideOut_BoundsType) {
494 if (bounds.contains(*rect)) {
495 return false;
496 } else {
497 // If rect's x values are both within bound's x range we
498 // could clip here. Same for y. But we don't bother to check.
499 return true;
500 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000501 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000502 return rect->intersect(bounds);
503 }
504}
505
junov@chromium.org8cdf0f52012-12-12 17:58:15 +0000506bool SkClipStack::quickContains(const SkRect& rect) const {
507
508 Iter iter(*this, Iter::kTop_IterStart);
509 const Element* element = iter.prev();
510 while (element != NULL) {
511 if (SkRegion::kIntersect_Op != element->getOp() && SkRegion::kReplace_Op != element->getOp())
512 return false;
513 if (element->isInverseFilled()) {
514 // Part of 'rect' could be trimmed off by the inverse-filled clip element
515 if (SkRect::Intersects(element->getBounds(), rect)) {
516 return false;
517 }
518 } else {
519 if (!element->contains(rect)) {
520 return false;
521 }
522 }
523 if (SkRegion::kReplace_Op == element->getOp()) {
524 break;
525 }
526 element = iter.prev();
527 }
528 return true;
529}
530
reed@google.com00177082011-10-12 14:34:30 +0000531void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000532
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000533 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000534 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000535 Element* element = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000536
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000537 if (NULL != element) {
538 if (element->canBeIntersectedInPlace(fSaveCount, op)) {
539 switch (element->fType) {
540 case Element::kEmpty_Type:
541 element->checkEmpty();
542 return;
543 case Element::kRect_Type:
544 if (element->rectRectIntersectAllowed(rect, doAA)) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000545 if (!element->fRect.intersect(rect)) {
546 element->setEmpty();
547 return;
548 }
549
550 element->fDoAA = doAA;
551 Element* prev = (Element*) iter.prev();
552 element->updateBoundAndGenID(prev);
553 return;
554 }
555 break;
556 case Element::kPath_Type:
557 if (!SkRect::Intersects(element->fPath.getBounds(), rect)) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000558 element->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000559 return;
560 }
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000561 break;
562 }
563 } else if (SkRegion::kReplace_Op == op) {
564 this->restoreTo(fSaveCount - 1);
565 element = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000566 }
567 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000568 new (fDeque.push_back()) Element(fSaveCount, rect, op, doAA);
569 ((Element*) fDeque.back())->updateBoundAndGenID(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000570}
571
reed@google.com00177082011-10-12 14:34:30 +0000572void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
tomhudson@google.com4c433722012-03-09 16:48:20 +0000573 SkRect alt;
junov@chromium.orgedf32d52012-12-10 14:57:54 +0000574 if (path.isRect(&alt) && !path.isInverseFillType()) {
tomhudson@google.com4c433722012-03-09 16:48:20 +0000575 return this->clipDevRect(alt, op, doAA);
576 }
robertphillips@google.com46f93502012-08-07 15:38:08 +0000577
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000578 Element* element = (Element*)fDeque.back();
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000579 if (NULL != element) {
580 if (element->canBeIntersectedInPlace(fSaveCount, op)) {
581 const SkRect& pathBounds = path.getBounds();
582 switch (element->fType) {
583 case Element::kEmpty_Type:
584 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000585 return;
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000586 case Element::kRect_Type:
587 if (!SkRect::Intersects(element->fRect, pathBounds)) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000588 element->setEmpty();
589 return;
590 }
591 break;
592 case Element::kPath_Type:
593 if (!SkRect::Intersects(element->fPath.getBounds(), pathBounds)) {
commit-bot@chromium.org6fbe54c2013-06-11 11:01:48 +0000594 element->setEmpty();
595 return;
596 }
597 break;
598 }
599 } else if (SkRegion::kReplace_Op == op) {
600 this->restoreTo(fSaveCount - 1);
601 element = (Element*) fDeque.back();
reed@google.com5c3d1472011-02-22 19:12:23 +0000602 }
603 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000604 new (fDeque.push_back()) Element(fSaveCount, path, op, doAA);
605 ((Element*) fDeque.back())->updateBoundAndGenID(element);
reed@google.com5c3d1472011-02-22 19:12:23 +0000606}
607
reed@google.com0557d9e2012-08-16 15:59:59 +0000608void SkClipStack::clipEmpty() {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000609
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000610 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000611
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000612 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000613 switch (element->fType) {
614 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000615 element->checkEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000616 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000617 case Element::kRect_Type:
618 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000619 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000620 return;
621 }
622 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000623 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000624
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000625 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000626}
627
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000628bool SkClipStack::isWideOpen() const {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000629 return this->getTopmostGenID() == kWideOpenGenID;
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000630}
631
reed@google.com5c3d1472011-02-22 19:12:23 +0000632///////////////////////////////////////////////////////////////////////////////
633
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000634SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000635}
636
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000637SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
638 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000639 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000640}
641
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000642const SkClipStack::Element* SkClipStack::Iter::next() {
643 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000644}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000645
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000646const SkClipStack::Element* SkClipStack::Iter::prev() {
647 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000648}
649
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000650const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000651
652 if (NULL == fStack) {
653 return NULL;
654 }
655
656 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
657
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000658 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000659
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000660 for (element = (const SkClipStack::Element*) fIter.prev();
661 NULL != element;
662 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000663
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000664 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000665 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000666 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000667 // return, the iterator is actually pointing at (and will
668 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000669 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000670 // step so we get the expected result.
671 if (NULL == fIter.next()) {
672 // The reverse iterator has run off the front of the deque
673 // (i.e., the "op" clip is the first clip) and can't
674 // recover. Reset the iterator to start at the front.
675 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
676 }
677 break;
678 }
679 }
680
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000681 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000682 // There were no "op" clips
683 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
684 }
685
686 return this->next();
687}
688
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000689void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000690 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000691 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000692}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000693
694// helper method
695void SkClipStack::getConservativeBounds(int offsetX,
696 int offsetY,
697 int maxWidth,
698 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000699 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000700 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000701 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000702
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000703 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000704 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000705
706 SkRect temp;
707 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000708
robertphillips@google.com7b112892012-07-31 15:18:21 +0000709 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000710 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000711 if (SkClipStack::kInsideOut_BoundsType == boundType) {
712 return;
713 }
714
robertphillips@google.com7b112892012-07-31 15:18:21 +0000715 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000716 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
717
robertphillips@google.com7b112892012-07-31 15:18:21 +0000718 if (!devBounds->intersect(temp)) {
719 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000720 }
721}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000722
robertphillips@google.com46f93502012-08-07 15:38:08 +0000723int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000724 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000725 return sk_atomic_inc(&gGenID);
726}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000727
728int32_t SkClipStack::getTopmostGenID() const {
robertphillips@google.com73e71022012-08-09 18:10:49 +0000729 if (fDeque.empty()) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000730 return kWideOpenGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000731 }
732
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000733 const Element* back = static_cast<const Element*>(fDeque.back());
734 if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()) {
735 return kWideOpenGenID;
736 }
737
738 return back->getGenID();
robertphillips@google.com73e71022012-08-09 18:10:49 +0000739}