blob: 107c20f116b774e5c1f88e9906846f35291ab819 [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.com8a98e3b2012-11-29 21:05:13 +000019void SkClipStack::Element::checkEmpty() const {
20 SkASSERT(fFiniteBound.isEmpty());
21 SkASSERT(kNormal_BoundsType == fFiniteBoundType);
22 SkASSERT(!fIsIntersectionOfRects);
23 SkASSERT(kEmptyGenID == fGenID);
24 SkASSERT(fPath.isEmpty());
25}
reed@google.com5c3d1472011-02-22 19:12:23 +000026
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000027bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
28 if (kEmpty_Type == fType &&
29 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
30 return true;
31 }
32 // Only clips within the same save/restore frame (as captured by
33 // the save count) can be merged
34 return fSaveCount == saveCount &&
35 SkRegion::kIntersect_Op == op &&
36 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
37}
robertphillips@google.com607fe072012-07-24 13:54:00 +000038
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000039bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
40 SkASSERT(kRect_Type == fType);
41
42 if (fDoAA == newAA) {
43 // if the AA setting is the same there is no issue
44 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000045 }
46
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000047 if (!SkRect::Intersects(fRect, newR)) {
48 // The calling code will correctly set the result to the empty clip
49 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000050 }
51
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000052 if (fRect.contains(newR)) {
53 // if the new rect carves out a portion of the old one there is no
54 // issue
55 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000056 }
57
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000058 // So either the two overlap in some complex manner or newR contains oldR.
59 // In the first, case the edges will require different AA. In the second,
60 // the AA setting that would be carried forward is incorrect (e.g., oldR
61 // is AA while newR is BW but since newR contains oldR, oldR will be
62 // drawn BW) since the new AA setting will predominate.
63 return false;
64}
robertphillips@google.com607fe072012-07-24 13:54:00 +000065
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000066// a mirror of combineBoundsRevDiff
67void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
68 switch (combination) {
69 case kInvPrev_InvCur_FillCombo:
70 // In this case the only pixels that can remain set
71 // are inside the current clip rect since the extensions
72 // to infinity of both clips cancel out and whatever
73 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +000074 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000075 break;
76 case kInvPrev_Cur_FillCombo:
77 // In this case the current op is finite so the only pixels
78 // that aren't set are whatever isn't set in the previous
79 // clip and whatever this clip carves out
80 fFiniteBound.join(prevFinite);
81 fFiniteBoundType = kInsideOut_BoundsType;
82 break;
83 case kPrev_InvCur_FillCombo:
84 // In this case everything outside of this clip's bound
85 // is erased, so the only pixels that can remain set
86 // occur w/in the intersection of the two finite bounds
87 if (!fFiniteBound.intersect(prevFinite)) {
88 fFiniteBound.setEmpty();
89 fGenID = kEmptyGenID;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000090 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000091 fFiniteBoundType = kNormal_BoundsType;
92 break;
93 case kPrev_Cur_FillCombo:
94 // The most conservative result bound is that of the
95 // prior clip. This could be wildly incorrect if the
96 // second clip either exactly matches the first clip
97 // (which should yield the empty set) or reduces the
98 // size of the prior bound (e.g., if the second clip
99 // exactly matched the bottom half of the prior clip).
100 // We ignore these two possibilities.
101 fFiniteBound = prevFinite;
102 break;
103 default:
104 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
105 break;
106 }
107}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000108
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000109void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000110
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000111 switch (combination) {
112 case kInvPrev_Cur_FillCombo: // fall through
113 case kPrev_InvCur_FillCombo:
114 // With only one of the clips inverted the result will always
115 // extend to infinity. The only pixels that may be un-writeable
116 // lie within the union of the two finite bounds
117 fFiniteBound.join(prevFinite);
118 fFiniteBoundType = kInsideOut_BoundsType;
119 break;
120 case kInvPrev_InvCur_FillCombo:
121 // The only pixels that can survive are within the
122 // union of the two bounding boxes since the extensions
123 // to infinity of both clips cancel out
124 // fall through!
125 case kPrev_Cur_FillCombo:
126 // The most conservative bound for xor is the
127 // union of the two bounds. If the two clips exactly overlapped
128 // the xor could yield the empty set. Similarly the xor
129 // could reduce the size of the original clip's bound (e.g.,
130 // if the second clip exactly matched the bottom half of the
131 // first clip). We ignore these two cases.
132 fFiniteBound.join(prevFinite);
133 fFiniteBoundType = kNormal_BoundsType;
134 break;
135 default:
136 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
137 break;
138 }
139}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000140
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000141// a mirror of combineBoundsIntersection
142void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
143
144 switch (combination) {
145 case kInvPrev_InvCur_FillCombo:
146 if (!fFiniteBound.intersect(prevFinite)) {
147 fFiniteBound.setEmpty();
148 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000149 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000150 fFiniteBoundType = kInsideOut_BoundsType;
151 break;
152 case kInvPrev_Cur_FillCombo:
153 // The only pixels that won't be drawable are inside
154 // the prior clip's finite bound
155 fFiniteBound = prevFinite;
156 fFiniteBoundType = kInsideOut_BoundsType;
157 break;
158 case kPrev_InvCur_FillCombo:
159 // The only pixels that won't be drawable are inside
160 // this clip's finite bound
161 break;
162 case kPrev_Cur_FillCombo:
163 fFiniteBound.join(prevFinite);
164 break;
165 default:
166 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
167 break;
168 }
169}
170
171// a mirror of combineBoundsUnion
172void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
173
174 switch (combination) {
175 case kInvPrev_InvCur_FillCombo:
176 // The only pixels that aren't writable in this case
177 // occur in the union of the two finite bounds
178 fFiniteBound.join(prevFinite);
179 fFiniteBoundType = kInsideOut_BoundsType;
180 break;
181 case kInvPrev_Cur_FillCombo:
182 // In this case the only pixels that will remain writeable
183 // are within the current clip
184 break;
185 case kPrev_InvCur_FillCombo:
186 // In this case the only pixels that will remain writeable
187 // are with the previous clip
188 fFiniteBound = prevFinite;
189 fFiniteBoundType = kNormal_BoundsType;
190 break;
191 case kPrev_Cur_FillCombo:
192 if (!fFiniteBound.intersect(prevFinite)) {
193 fFiniteBound.setEmpty();
194 fGenID = kEmptyGenID;
195 }
196 break;
197 default:
198 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
199 break;
200 }
201}
202
203// a mirror of combineBoundsDiff
204void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
205
206 switch (combination) {
207 case kInvPrev_InvCur_FillCombo:
208 // The only pixels that can survive are in the
209 // previous bound since the extensions to infinity in
210 // both clips cancel out
211 fFiniteBound = prevFinite;
212 fFiniteBoundType = kNormal_BoundsType;
213 break;
214 case kInvPrev_Cur_FillCombo:
215 if (!fFiniteBound.intersect(prevFinite)) {
216 fFiniteBound.setEmpty();
217 fGenID = kEmptyGenID;
218 }
219 fFiniteBoundType = kNormal_BoundsType;
220 break;
221 case kPrev_InvCur_FillCombo:
222 fFiniteBound.join(prevFinite);
223 fFiniteBoundType = kInsideOut_BoundsType;
224 break;
225 case kPrev_Cur_FillCombo:
226 // Fall through - as with the kDifference_Op case, the
227 // most conservative result bound is the bound of the
228 // current clip. The prior clip could reduce the size of this
229 // bound (as in the kDifference_Op case) but we are ignoring
230 // those cases.
231 break;
232 default:
233 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
234 break;
235 }
236}
237
238void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
239 // We set this first here but we may overwrite it later if we determine that the clip is
240 // either wide-open or empty.
241 fGenID = GetNextGenID();
242
243 // First, optimistically update the current Element's bound information
244 // with the current clip's bound
245 fIsIntersectionOfRects = false;
246 if (kRect_Type == fType) {
247 fFiniteBound = fRect;
248 fFiniteBoundType = kNormal_BoundsType;
249
250 if (SkRegion::kReplace_Op == fOp ||
251 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
252 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
253 prior->rectRectIntersectAllowed(fRect, fDoAA))) {
254 fIsIntersectionOfRects = true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000255 }
256
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000257 } else {
258 SkASSERT(kPath_Type == fType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000259
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000260 fFiniteBound = fPath.getBounds();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000261
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000262 if (fPath.isInverseFillType()) {
263 fFiniteBoundType = kInsideOut_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000264 } else {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000265 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000266 }
267 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000268
269 if (!fDoAA) {
270 // Here we mimic a non-anti-aliased scanline system. If there is
271 // no anti-aliasing we can integerize the bounding box to exclude
272 // fractional parts that won't be rendered.
273 // Note: the left edge is handled slightly differently below. We
274 // are a bit more generous in the rounding since we don't want to
275 // risk missing the left pixels when fLeft is very close to .5
276 fFiniteBound.set(SkIntToScalar(SkScalarFloorToInt(fFiniteBound.fLeft+0.45f)),
277 SkIntToScalar(SkScalarRound(fFiniteBound.fTop)),
278 SkIntToScalar(SkScalarRound(fFiniteBound.fRight)),
279 SkIntToScalar(SkScalarRound(fFiniteBound.fBottom)));
280 }
281
282 // Now determine the previous Element's bound information taking into
283 // account that there may be no previous clip
284 SkRect prevFinite;
285 SkClipStack::BoundsType prevType;
286
287 if (NULL == prior) {
288 // no prior clip means the entire plane is writable
289 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
290 prevType = kInsideOut_BoundsType;
291 } else {
292 prevFinite = prior->fFiniteBound;
293 prevType = prior->fFiniteBoundType;
294 }
295
296 FillCombo combination = kPrev_Cur_FillCombo;
297 if (kInsideOut_BoundsType == fFiniteBoundType) {
298 combination = (FillCombo) (combination | 0x01);
299 }
300 if (kInsideOut_BoundsType == prevType) {
301 combination = (FillCombo) (combination | 0x02);
302 }
303
304 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
305 kInvPrev_Cur_FillCombo == combination ||
306 kPrev_InvCur_FillCombo == combination ||
307 kPrev_Cur_FillCombo == combination);
308
309 // Now integrate with clip with the prior clips
310 switch (fOp) {
311 case SkRegion::kDifference_Op:
312 this->combineBoundsDiff(combination, prevFinite);
313 break;
314 case SkRegion::kXOR_Op:
315 this->combineBoundsXOR(combination, prevFinite);
316 break;
317 case SkRegion::kUnion_Op:
318 this->combineBoundsUnion(combination, prevFinite);
319 break;
320 case SkRegion::kIntersect_Op:
321 this->combineBoundsIntersection(combination, prevFinite);
322 break;
323 case SkRegion::kReverseDifference_Op:
324 this->combineBoundsRevDiff(combination, prevFinite);
325 break;
326 case SkRegion::kReplace_Op:
327 // Replace just ignores everything prior
328 // The current clip's bound information is already filled in
329 // so nothing to do
330 break;
331 default:
332 SkDebugf("SkRegion::Op error/n");
333 SkASSERT(0);
334 break;
335 }
336}
reed@google.com5c3d1472011-02-22 19:12:23 +0000337
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000338// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000339// the deque. As such it needs to balance allocating too much memory vs.
340// incurring allocation/deallocation thrashing. It should roughly correspond to
341// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000342static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000343
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000344SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000345 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000346 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000347}
348
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000349SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000350 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000351 *this = b;
352}
353
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000354SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000355 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000356 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000357 if (!r.isEmpty()) {
358 this->clipDevRect(r, SkRegion::kReplace_Op, false);
359 }
360}
361
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000362SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000363 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000364 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000365 if (!r.isEmpty()) {
366 SkRect temp;
367 temp.set(r);
368 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
369 }
370}
371
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000372SkClipStack::~SkClipStack() {
373 reset();
374}
375
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000376SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
377 if (this == &b) {
378 return *this;
379 }
380 reset();
381
382 fSaveCount = b.fSaveCount;
383 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000384 for (const Element* element = (const Element*)recIter.next();
385 element != NULL;
386 element = (const Element*)recIter.next()) {
387 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000388 }
389
390 return *this;
391}
392
393bool SkClipStack::operator==(const SkClipStack& b) const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000394 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000395 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000396 return false;
397 }
398 SkDeque::F2BIter myIter(fDeque);
399 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000400 const Element* myElement = (const Element*)myIter.next();
401 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000402
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000403 while (myElement != NULL && bElement != NULL) {
404 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000405 return false;
406 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000407 myElement = (const Element*)myIter.next();
408 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000409 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000410 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000411}
412
reed@google.com5c3d1472011-02-22 19:12:23 +0000413void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000414 // We used a placement new for each object in fDeque, so we're responsible
415 // for calling the destructor on each of them as well.
416 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000417 Element* element = (Element*)fDeque.back();
418 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000419 fDeque.pop_back();
420 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000421
422 fSaveCount = 0;
423}
424
425void SkClipStack::save() {
426 fSaveCount += 1;
427}
428
429void SkClipStack::restore() {
430 fSaveCount -= 1;
431 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000432 Element* element = (Element*)fDeque.back();
433 if (element->fSaveCount <= fSaveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000434 break;
435 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000436 this->purgeClip(element);
437 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000438 fDeque.pop_back();
439 }
440}
441
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000442void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000443 BoundsType* boundType,
444 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000445 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000446
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000447 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000448
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000449 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000450 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000451 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000452 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000453 if (NULL != isIntersectionOfRects) {
454 *isIntersectionOfRects = false;
455 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000456 return;
457 }
458
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000459 *canvFiniteBound = element->fFiniteBound;
460 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000461 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000462 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000463 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000464}
465
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000466bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
467 SkASSERT(NULL != rect);
468
469 SkRect bounds;
470 SkClipStack::BoundsType bt;
471 this->getBounds(&bounds, &bt);
472 if (bt == SkClipStack::kInsideOut_BoundsType) {
473 if (bounds.contains(*rect)) {
474 return false;
475 } else {
476 // If rect's x values are both within bound's x range we
477 // could clip here. Same for y. But we don't bother to check.
478 return true;
479 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000480 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000481 return rect->intersect(bounds);
482 }
483}
484
reed@google.com00177082011-10-12 14:34:30 +0000485void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000486
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000487 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000488 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000489 Element* element = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000490
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000491 if (element && element->canBeIntersectedInPlace(fSaveCount, op)) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000492 switch (element->fType) {
493 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000494 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000495 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000496 case Element::kRect_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000497 if (element->rectRectIntersectAllowed(rect, doAA)) {
498 this->purgeClip(element);
499 if (!element->fRect.intersect(rect)) {
500 element->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000501 return;
502 }
503
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000504 element->fDoAA = doAA;
505 Element* prev = (Element*) iter.prev();
506 element->updateBoundAndGenID(prev);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000507 return;
reed@google.com5c3d1472011-02-22 19:12:23 +0000508 }
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000509 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000510 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000511 if (!SkRect::Intersects(element->fPath.getBounds(), rect)) {
512 this->purgeClip(element);
513 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000514 return;
515 }
516 break;
517 }
518 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000519 new (fDeque.push_back()) Element(fSaveCount, rect, op, doAA);
520 ((Element*) fDeque.back())->updateBoundAndGenID(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000521
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000522 if (element && element->fSaveCount == fSaveCount) {
523 this->purgeClip(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000524 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000525}
526
reed@google.com00177082011-10-12 14:34:30 +0000527void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
tomhudson@google.com4c433722012-03-09 16:48:20 +0000528 SkRect alt;
529 if (path.isRect(&alt)) {
530 return this->clipDevRect(alt, op, doAA);
531 }
robertphillips@google.com46f93502012-08-07 15:38:08 +0000532
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000533 Element* element = (Element*)fDeque.back();
534 if (element && element->canBeIntersectedInPlace(fSaveCount, op)) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000535 const SkRect& pathBounds = path.getBounds();
bsalomon@google.com417bc132012-11-29 19:16:32 +0000536 switch (element->fType) {
537 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000538 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000539 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000540 case Element::kRect_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000541 if (!SkRect::Intersects(element->fRect, pathBounds)) {
542 this->purgeClip(element);
543 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000544 return;
545 }
546 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000547 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000548 if (!SkRect::Intersects(element->fPath.getBounds(), pathBounds)) {
549 this->purgeClip(element);
550 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000551 return;
552 }
553 break;
554 }
555 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000556 new (fDeque.push_back()) Element(fSaveCount, path, op, doAA);
557 ((Element*) fDeque.back())->updateBoundAndGenID(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000558
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000559 if (element && element->fSaveCount == fSaveCount) {
560 this->purgeClip(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000561 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000562}
563
reed@google.com0557d9e2012-08-16 15:59:59 +0000564void SkClipStack::clipEmpty() {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000565
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000566 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000567
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000568 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000569 switch (element->fType) {
570 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000571 element->checkEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000572 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000573 case Element::kRect_Type:
574 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000575 this->purgeClip(element);
576 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000577 return;
578 }
579 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000580 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000581
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000582 if (element && element->fSaveCount == fSaveCount) {
583 this->purgeClip(element);
reed@google.com0557d9e2012-08-16 15:59:59 +0000584 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000585 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000586}
587
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000588bool SkClipStack::isWideOpen() const {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000589 if (0 == fDeque.count()) {
590 return true;
591 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000592
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000593 const Element* back = (const Element*) fDeque.back();
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000594 return kWideOpenGenID == back->fGenID ||
595 (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty());
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000596}
597
reed@google.com5c3d1472011-02-22 19:12:23 +0000598///////////////////////////////////////////////////////////////////////////////
599
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000600SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000601}
602
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000603SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
604 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000605 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000606}
607
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000608const SkClipStack::Element* SkClipStack::Iter::next() {
609 return (const SkClipStack::Element*)fIter.next();
reed@google.com5c3d1472011-02-22 19:12:23 +0000610}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000611
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000612const SkClipStack::Element* SkClipStack::Iter::prev() {
613 return (const SkClipStack::Element*)fIter.prev();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000614}
615
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000616const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000617
618 if (NULL == fStack) {
619 return NULL;
620 }
621
622 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
623
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000624 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000625
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000626 for (element = (const SkClipStack::Element*) fIter.prev();
627 NULL != element;
628 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000629
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000630 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000631 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000632 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000633 // return, the iterator is actually pointing at (and will
634 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000635 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000636 // step so we get the expected result.
637 if (NULL == fIter.next()) {
638 // The reverse iterator has run off the front of the deque
639 // (i.e., the "op" clip is the first clip) and can't
640 // recover. Reset the iterator to start at the front.
641 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
642 }
643 break;
644 }
645 }
646
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000647 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000648 // There were no "op" clips
649 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
650 }
651
652 return this->next();
653}
654
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000655void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000656 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000657 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000658}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000659
660// helper method
661void SkClipStack::getConservativeBounds(int offsetX,
662 int offsetY,
663 int maxWidth,
664 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000665 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000666 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000667 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000668
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000669 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000670 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000671
672 SkRect temp;
673 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674
robertphillips@google.com7b112892012-07-31 15:18:21 +0000675 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000676 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000677 if (SkClipStack::kInsideOut_BoundsType == boundType) {
678 return;
679 }
680
robertphillips@google.com7b112892012-07-31 15:18:21 +0000681 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000682 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
683
robertphillips@google.com7b112892012-07-31 15:18:21 +0000684 if (!devBounds->intersect(temp)) {
685 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000686 }
687}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000688
689void SkClipStack::addPurgeClipCallback(PFPurgeClipCB callback, void* data) const {
690 ClipCallbackData temp = { callback, data };
691 fCallbackData.append(1, &temp);
692}
693
694void SkClipStack::removePurgeClipCallback(PFPurgeClipCB callback, void* data) const {
695 ClipCallbackData temp = { callback, data };
696 int index = fCallbackData.find(temp);
697 if (index >= 0) {
698 fCallbackData.removeShuffle(index);
699 }
700}
701
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000702// The clip state represented by 'element' will never be used again. Purge it.
703void SkClipStack::purgeClip(Element* element) {
704 SkASSERT(NULL != element);
705 if (element->fGenID >= 0 && element->fGenID < kFirstUnreservedGenID) {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000706 return;
707 }
robertphillips@google.com46f93502012-08-07 15:38:08 +0000708
709 for (int i = 0; i < fCallbackData.count(); ++i) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000710 (*fCallbackData[i].fCallback)(element->fGenID, fCallbackData[i].fData);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000711 }
712
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000713 // Invalidate element's gen ID so handlers can detect already handled records
714 element->fGenID = kInvalidGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000715}
716
717int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000718 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000719 return sk_atomic_inc(&gGenID);
720}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000721
722int32_t SkClipStack::getTopmostGenID() const {
723
724 if (fDeque.empty()) {
725 return kInvalidGenID;
726 }
727
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000728 Element* element = (Element*)fDeque.back();
729 return element->fGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000730}