blob: d86761c7c9524dca0cff6c0db5e98d84db3dc0d0 [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::operator==(const Element& b) const {
28 if (fSaveCount != b.fSaveCount ||
29 fOp != b.fOp ||
30 fType != b.fType ||
31 fDoAA != b.fDoAA) {
robertphillips@google.com08eacc12012-08-02 12:49:00 +000032 return false;
33 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000034 switch (fType) {
35 case kEmpty_Type:
36 return true;
37 case kRect_Type:
38 return fRect == b.fRect;
39 case kPath_Type:
40 return fPath == b.fPath;
41 }
42 return false; // Silence the compiler.
43}
robertphillips@google.com08eacc12012-08-02 12:49:00 +000044
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000045bool SkClipStack::Element::operator!=(const Element& b) const {
46 return !(*this == b);
47}
robertphillips@google.com08eacc12012-08-02 12:49:00 +000048
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000049bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const {
50 if (kEmpty_Type == fType &&
51 (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) {
52 return true;
53 }
54 // Only clips within the same save/restore frame (as captured by
55 // the save count) can be merged
56 return fSaveCount == saveCount &&
57 SkRegion::kIntersect_Op == op &&
58 (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp);
59}
robertphillips@google.com607fe072012-07-24 13:54:00 +000060
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000061bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const {
62 SkASSERT(kRect_Type == fType);
63
64 if (fDoAA == newAA) {
65 // if the AA setting is the same there is no issue
66 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000067 }
68
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000069 if (!SkRect::Intersects(fRect, newR)) {
70 // The calling code will correctly set the result to the empty clip
71 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000072 }
73
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000074 if (fRect.contains(newR)) {
75 // if the new rect carves out a portion of the old one there is no
76 // issue
77 return true;
robertphillips@google.com607fe072012-07-24 13:54:00 +000078 }
79
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000080 // So either the two overlap in some complex manner or newR contains oldR.
81 // In the first, case the edges will require different AA. In the second,
82 // the AA setting that would be carried forward is incorrect (e.g., oldR
83 // is AA while newR is BW but since newR contains oldR, oldR will be
84 // drawn BW) since the new AA setting will predominate.
85 return false;
86}
robertphillips@google.com607fe072012-07-24 13:54:00 +000087
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000088// a mirror of combineBoundsRevDiff
89void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) {
90 switch (combination) {
91 case kInvPrev_InvCur_FillCombo:
92 // In this case the only pixels that can remain set
93 // are inside the current clip rect since the extensions
94 // to infinity of both clips cancel out and whatever
95 // is outside of the current clip is removed
robertphillips@google.com607fe072012-07-24 13:54:00 +000096 fFiniteBoundType = kNormal_BoundsType;
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +000097 break;
98 case kInvPrev_Cur_FillCombo:
99 // In this case the current op is finite so the only pixels
100 // that aren't set are whatever isn't set in the previous
101 // clip and whatever this clip carves out
102 fFiniteBound.join(prevFinite);
103 fFiniteBoundType = kInsideOut_BoundsType;
104 break;
105 case kPrev_InvCur_FillCombo:
106 // In this case everything outside of this clip's bound
107 // is erased, so the only pixels that can remain set
108 // occur w/in the intersection of the two finite bounds
109 if (!fFiniteBound.intersect(prevFinite)) {
110 fFiniteBound.setEmpty();
111 fGenID = kEmptyGenID;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000112 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000113 fFiniteBoundType = kNormal_BoundsType;
114 break;
115 case kPrev_Cur_FillCombo:
116 // The most conservative result bound is that of the
117 // prior clip. This could be wildly incorrect if the
118 // second clip either exactly matches the first clip
119 // (which should yield the empty set) or reduces the
120 // size of the prior bound (e.g., if the second clip
121 // exactly matched the bottom half of the prior clip).
122 // We ignore these two possibilities.
123 fFiniteBound = prevFinite;
124 break;
125 default:
126 SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination");
127 break;
128 }
129}
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000130
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000131void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000133 switch (combination) {
134 case kInvPrev_Cur_FillCombo: // fall through
135 case kPrev_InvCur_FillCombo:
136 // With only one of the clips inverted the result will always
137 // extend to infinity. The only pixels that may be un-writeable
138 // lie within the union of the two finite bounds
139 fFiniteBound.join(prevFinite);
140 fFiniteBoundType = kInsideOut_BoundsType;
141 break;
142 case kInvPrev_InvCur_FillCombo:
143 // The only pixels that can survive are within the
144 // union of the two bounding boxes since the extensions
145 // to infinity of both clips cancel out
146 // fall through!
147 case kPrev_Cur_FillCombo:
148 // The most conservative bound for xor is the
149 // union of the two bounds. If the two clips exactly overlapped
150 // the xor could yield the empty set. Similarly the xor
151 // could reduce the size of the original clip's bound (e.g.,
152 // if the second clip exactly matched the bottom half of the
153 // first clip). We ignore these two cases.
154 fFiniteBound.join(prevFinite);
155 fFiniteBoundType = kNormal_BoundsType;
156 break;
157 default:
158 SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination");
159 break;
160 }
161}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000162
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000163// a mirror of combineBoundsIntersection
164void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) {
165
166 switch (combination) {
167 case kInvPrev_InvCur_FillCombo:
168 if (!fFiniteBound.intersect(prevFinite)) {
169 fFiniteBound.setEmpty();
170 fGenID = kWideOpenGenID;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000171 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000172 fFiniteBoundType = kInsideOut_BoundsType;
173 break;
174 case kInvPrev_Cur_FillCombo:
175 // The only pixels that won't be drawable are inside
176 // the prior clip's finite bound
177 fFiniteBound = prevFinite;
178 fFiniteBoundType = kInsideOut_BoundsType;
179 break;
180 case kPrev_InvCur_FillCombo:
181 // The only pixels that won't be drawable are inside
182 // this clip's finite bound
183 break;
184 case kPrev_Cur_FillCombo:
185 fFiniteBound.join(prevFinite);
186 break;
187 default:
188 SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination");
189 break;
190 }
191}
192
193// a mirror of combineBoundsUnion
194void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) {
195
196 switch (combination) {
197 case kInvPrev_InvCur_FillCombo:
198 // The only pixels that aren't writable in this case
199 // occur in the union of the two finite bounds
200 fFiniteBound.join(prevFinite);
201 fFiniteBoundType = kInsideOut_BoundsType;
202 break;
203 case kInvPrev_Cur_FillCombo:
204 // In this case the only pixels that will remain writeable
205 // are within the current clip
206 break;
207 case kPrev_InvCur_FillCombo:
208 // In this case the only pixels that will remain writeable
209 // are with the previous clip
210 fFiniteBound = prevFinite;
211 fFiniteBoundType = kNormal_BoundsType;
212 break;
213 case kPrev_Cur_FillCombo:
214 if (!fFiniteBound.intersect(prevFinite)) {
215 fFiniteBound.setEmpty();
216 fGenID = kEmptyGenID;
217 }
218 break;
219 default:
220 SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination");
221 break;
222 }
223}
224
225// a mirror of combineBoundsDiff
226void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) {
227
228 switch (combination) {
229 case kInvPrev_InvCur_FillCombo:
230 // The only pixels that can survive are in the
231 // previous bound since the extensions to infinity in
232 // both clips cancel out
233 fFiniteBound = prevFinite;
234 fFiniteBoundType = kNormal_BoundsType;
235 break;
236 case kInvPrev_Cur_FillCombo:
237 if (!fFiniteBound.intersect(prevFinite)) {
238 fFiniteBound.setEmpty();
239 fGenID = kEmptyGenID;
240 }
241 fFiniteBoundType = kNormal_BoundsType;
242 break;
243 case kPrev_InvCur_FillCombo:
244 fFiniteBound.join(prevFinite);
245 fFiniteBoundType = kInsideOut_BoundsType;
246 break;
247 case kPrev_Cur_FillCombo:
248 // Fall through - as with the kDifference_Op case, the
249 // most conservative result bound is the bound of the
250 // current clip. The prior clip could reduce the size of this
251 // bound (as in the kDifference_Op case) but we are ignoring
252 // those cases.
253 break;
254 default:
255 SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination");
256 break;
257 }
258}
259
260void SkClipStack::Element::updateBoundAndGenID(const Element* prior) {
261 // We set this first here but we may overwrite it later if we determine that the clip is
262 // either wide-open or empty.
263 fGenID = GetNextGenID();
264
265 // First, optimistically update the current Element's bound information
266 // with the current clip's bound
267 fIsIntersectionOfRects = false;
268 if (kRect_Type == fType) {
269 fFiniteBound = fRect;
270 fFiniteBoundType = kNormal_BoundsType;
271
272 if (SkRegion::kReplace_Op == fOp ||
273 (SkRegion::kIntersect_Op == fOp && NULL == prior) ||
274 (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects &&
275 prior->rectRectIntersectAllowed(fRect, fDoAA))) {
276 fIsIntersectionOfRects = true;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000277 }
278
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000279 } else {
280 SkASSERT(kPath_Type == fType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000281
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000282 fFiniteBound = fPath.getBounds();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000283
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000284 if (fPath.isInverseFillType()) {
285 fFiniteBoundType = kInsideOut_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000286 } else {
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000287 fFiniteBoundType = kNormal_BoundsType;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000288 }
289 }
bsalomon@google.com8a98e3b2012-11-29 21:05:13 +0000290
291 if (!fDoAA) {
292 // Here we mimic a non-anti-aliased scanline system. If there is
293 // no anti-aliasing we can integerize the bounding box to exclude
294 // fractional parts that won't be rendered.
295 // Note: the left edge is handled slightly differently below. We
296 // are a bit more generous in the rounding since we don't want to
297 // risk missing the left pixels when fLeft is very close to .5
298 fFiniteBound.set(SkIntToScalar(SkScalarFloorToInt(fFiniteBound.fLeft+0.45f)),
299 SkIntToScalar(SkScalarRound(fFiniteBound.fTop)),
300 SkIntToScalar(SkScalarRound(fFiniteBound.fRight)),
301 SkIntToScalar(SkScalarRound(fFiniteBound.fBottom)));
302 }
303
304 // Now determine the previous Element's bound information taking into
305 // account that there may be no previous clip
306 SkRect prevFinite;
307 SkClipStack::BoundsType prevType;
308
309 if (NULL == prior) {
310 // no prior clip means the entire plane is writable
311 prevFinite.setEmpty(); // there are no pixels that cannot be drawn to
312 prevType = kInsideOut_BoundsType;
313 } else {
314 prevFinite = prior->fFiniteBound;
315 prevType = prior->fFiniteBoundType;
316 }
317
318 FillCombo combination = kPrev_Cur_FillCombo;
319 if (kInsideOut_BoundsType == fFiniteBoundType) {
320 combination = (FillCombo) (combination | 0x01);
321 }
322 if (kInsideOut_BoundsType == prevType) {
323 combination = (FillCombo) (combination | 0x02);
324 }
325
326 SkASSERT(kInvPrev_InvCur_FillCombo == combination ||
327 kInvPrev_Cur_FillCombo == combination ||
328 kPrev_InvCur_FillCombo == combination ||
329 kPrev_Cur_FillCombo == combination);
330
331 // Now integrate with clip with the prior clips
332 switch (fOp) {
333 case SkRegion::kDifference_Op:
334 this->combineBoundsDiff(combination, prevFinite);
335 break;
336 case SkRegion::kXOR_Op:
337 this->combineBoundsXOR(combination, prevFinite);
338 break;
339 case SkRegion::kUnion_Op:
340 this->combineBoundsUnion(combination, prevFinite);
341 break;
342 case SkRegion::kIntersect_Op:
343 this->combineBoundsIntersection(combination, prevFinite);
344 break;
345 case SkRegion::kReverseDifference_Op:
346 this->combineBoundsRevDiff(combination, prevFinite);
347 break;
348 case SkRegion::kReplace_Op:
349 // Replace just ignores everything prior
350 // The current clip's bound information is already filled in
351 // so nothing to do
352 break;
353 default:
354 SkDebugf("SkRegion::Op error/n");
355 SkASSERT(0);
356 break;
357 }
358}
reed@google.com5c3d1472011-02-22 19:12:23 +0000359
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000360// This constant determines how many Element's are allocated together as a block in
robertphillips@google.comf9a90842012-08-17 14:25:43 +0000361// the deque. As such it needs to balance allocating too much memory vs.
362// incurring allocation/deallocation thrashing. It should roughly correspond to
363// the deepest save/restore stack we expect to see.
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000364static const int kDefaultElementAllocCnt = 8;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000365
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000366SkClipStack::SkClipStack()
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000367 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000368 , fSaveCount(0) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000369}
370
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000371SkClipStack::SkClipStack(const SkClipStack& b)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000372 : fDeque(sizeof(Element), kDefaultElementAllocCnt) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000373 *this = b;
374}
375
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000376SkClipStack::SkClipStack(const SkRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000377 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000378 , fSaveCount(0) {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000379 if (!r.isEmpty()) {
380 this->clipDevRect(r, SkRegion::kReplace_Op, false);
381 }
382}
383
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000384SkClipStack::SkClipStack(const SkIRect& r)
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000385 : fDeque(sizeof(Element), kDefaultElementAllocCnt)
robertphillips@google.com46f93502012-08-07 15:38:08 +0000386 , fSaveCount(0) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000387 if (!r.isEmpty()) {
388 SkRect temp;
389 temp.set(r);
390 this->clipDevRect(temp, SkRegion::kReplace_Op, false);
391 }
392}
393
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000394SkClipStack::~SkClipStack() {
395 reset();
396}
397
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000398SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
399 if (this == &b) {
400 return *this;
401 }
402 reset();
403
404 fSaveCount = b.fSaveCount;
405 SkDeque::F2BIter recIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000406 for (const Element* element = (const Element*)recIter.next();
407 element != NULL;
408 element = (const Element*)recIter.next()) {
409 new (fDeque.push_back()) Element(*element);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000410 }
411
412 return *this;
413}
414
415bool SkClipStack::operator==(const SkClipStack& b) const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000416 if (fSaveCount != b.fSaveCount ||
robertphillips@google.com46f93502012-08-07 15:38:08 +0000417 fDeque.count() != b.fDeque.count()) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000418 return false;
419 }
420 SkDeque::F2BIter myIter(fDeque);
421 SkDeque::F2BIter bIter(b.fDeque);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000422 const Element* myElement = (const Element*)myIter.next();
423 const Element* bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000424
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000425 while (myElement != NULL && bElement != NULL) {
426 if (*myElement != *bElement) {
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000427 return false;
428 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000429 myElement = (const Element*)myIter.next();
430 bElement = (const Element*)bIter.next();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000431 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000432 return myElement == NULL && bElement == NULL;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000433}
434
reed@google.com5c3d1472011-02-22 19:12:23 +0000435void SkClipStack::reset() {
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000436 // We used a placement new for each object in fDeque, so we're responsible
437 // for calling the destructor on each of them as well.
438 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000439 Element* element = (Element*)fDeque.back();
440 element->~Element();
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000441 fDeque.pop_back();
442 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000443
444 fSaveCount = 0;
445}
446
447void SkClipStack::save() {
448 fSaveCount += 1;
449}
450
451void SkClipStack::restore() {
452 fSaveCount -= 1;
453 while (!fDeque.empty()) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000454 Element* element = (Element*)fDeque.back();
455 if (element->fSaveCount <= fSaveCount) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000456 break;
457 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000458 this->purgeClip(element);
459 element->~Element();
reed@google.com5c3d1472011-02-22 19:12:23 +0000460 fDeque.pop_back();
461 }
462}
463
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000464void SkClipStack::getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000465 BoundsType* boundType,
466 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000467 SkASSERT(NULL != canvFiniteBound && NULL != boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000468
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000469 Element* element = (Element*)fDeque.back();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000470
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000471 if (NULL == element) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000472 // the clip is wide open - the infinite plane w/ no pixels un-writeable
robertphillips@google.com7b112892012-07-31 15:18:21 +0000473 canvFiniteBound->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000474 *boundType = kInsideOut_BoundsType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000475 if (NULL != isIntersectionOfRects) {
476 *isIntersectionOfRects = false;
477 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000478 return;
479 }
480
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000481 *canvFiniteBound = element->fFiniteBound;
482 *boundType = element->fFiniteBoundType;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000483 if (NULL != isIntersectionOfRects) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000484 *isIntersectionOfRects = element->fIsIntersectionOfRects;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000485 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000486}
487
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000488bool SkClipStack::intersectRectWithClip(SkRect* rect) const {
489 SkASSERT(NULL != rect);
490
491 SkRect bounds;
492 SkClipStack::BoundsType bt;
493 this->getBounds(&bounds, &bt);
494 if (bt == SkClipStack::kInsideOut_BoundsType) {
495 if (bounds.contains(*rect)) {
496 return false;
497 } else {
498 // If rect's x values are both within bound's x range we
499 // could clip here. Same for y. But we don't bother to check.
500 return true;
501 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000502 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000503 return rect->intersect(bounds);
504 }
505}
506
reed@google.com00177082011-10-12 14:34:30 +0000507void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000508
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000509 // Use reverse iterator instead of back because Rect path may need previous
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000510 SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000511 Element* element = (Element*) iter.prev();
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000512
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000513 if (element && element->canBeIntersectedInPlace(fSaveCount, op)) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000514 switch (element->fType) {
515 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000516 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000517 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000518 case Element::kRect_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000519 if (element->rectRectIntersectAllowed(rect, doAA)) {
520 this->purgeClip(element);
521 if (!element->fRect.intersect(rect)) {
522 element->setEmpty();
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000523 return;
524 }
525
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000526 element->fDoAA = doAA;
527 Element* prev = (Element*) iter.prev();
528 element->updateBoundAndGenID(prev);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000529 return;
reed@google.com5c3d1472011-02-22 19:12:23 +0000530 }
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000531 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000532 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000533 if (!SkRect::Intersects(element->fPath.getBounds(), rect)) {
534 this->purgeClip(element);
535 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000536 return;
537 }
538 break;
539 }
540 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000541 new (fDeque.push_back()) Element(fSaveCount, rect, op, doAA);
542 ((Element*) fDeque.back())->updateBoundAndGenID(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000543
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000544 if (element && element->fSaveCount == fSaveCount) {
545 this->purgeClip(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000546 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000547}
548
reed@google.com00177082011-10-12 14:34:30 +0000549void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) {
tomhudson@google.com4c433722012-03-09 16:48:20 +0000550 SkRect alt;
551 if (path.isRect(&alt)) {
552 return this->clipDevRect(alt, op, doAA);
553 }
robertphillips@google.com46f93502012-08-07 15:38:08 +0000554
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000555 Element* element = (Element*)fDeque.back();
556 if (element && element->canBeIntersectedInPlace(fSaveCount, op)) {
reed@google.com5c3d1472011-02-22 19:12:23 +0000557 const SkRect& pathBounds = path.getBounds();
bsalomon@google.com417bc132012-11-29 19:16:32 +0000558 switch (element->fType) {
559 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000560 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000561 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000562 case Element::kRect_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000563 if (!SkRect::Intersects(element->fRect, pathBounds)) {
564 this->purgeClip(element);
565 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000566 return;
567 }
568 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000569 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000570 if (!SkRect::Intersects(element->fPath.getBounds(), pathBounds)) {
571 this->purgeClip(element);
572 element->setEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000573 return;
574 }
575 break;
576 }
577 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000578 new (fDeque.push_back()) Element(fSaveCount, path, op, doAA);
579 ((Element*) fDeque.back())->updateBoundAndGenID(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000580
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000581 if (element && element->fSaveCount == fSaveCount) {
582 this->purgeClip(element);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000583 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000584}
585
reed@google.com0557d9e2012-08-16 15:59:59 +0000586void SkClipStack::clipEmpty() {
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000587
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000588 Element* element = (Element*) fDeque.back();
robertphillips@google.com63ae1cf2012-08-17 13:53:05 +0000589
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000590 if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000591 switch (element->fType) {
592 case Element::kEmpty_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000593 element->checkEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000594 return;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000595 case Element::kRect_Type:
596 case Element::kPath_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000597 this->purgeClip(element);
598 element->setEmpty();
reed@google.com0557d9e2012-08-16 15:59:59 +0000599 return;
600 }
601 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000602 new (fDeque.push_back()) Element(fSaveCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000603
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000604 if (element && element->fSaveCount == fSaveCount) {
605 this->purgeClip(element);
reed@google.com0557d9e2012-08-16 15:59:59 +0000606 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000607 ((Element*)fDeque.back())->fGenID = kEmptyGenID;
reed@google.com0557d9e2012-08-16 15:59:59 +0000608}
609
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000610bool SkClipStack::isWideOpen() const {
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000611 if (0 == fDeque.count()) {
612 return true;
613 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000614
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000615 const Element* back = (const Element*) fDeque.back();
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000616 return kWideOpenGenID == back->fGenID ||
617 (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty());
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000618}
619
reed@google.com5c3d1472011-02-22 19:12:23 +0000620///////////////////////////////////////////////////////////////////////////////
621
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000622SkClipStack::Iter::Iter() : fStack(NULL) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000623}
624
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000625bool operator==(const SkClipStack::Iter::Clip& a,
626 const SkClipStack::Iter::Clip& b) {
reed@google.com00177082011-10-12 14:34:30 +0000627 return a.fOp == b.fOp && a.fDoAA == b.fDoAA &&
vandebo@chromium.orge03c6522011-06-21 20:45:51 +0000628 ((a.fRect == NULL && b.fRect == NULL) ||
629 (a.fRect != NULL && b.fRect != NULL && *a.fRect == *b.fRect)) &&
630 ((a.fPath == NULL && b.fPath == NULL) ||
631 (a.fPath != NULL && b.fPath != NULL && *a.fPath == *b.fPath));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000632}
633
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000634bool operator!=(const SkClipStack::Iter::Clip& a,
635 const SkClipStack::Iter::Clip& b) {
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000636 return !(a == b);
637}
638
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000639const SkRect& SkClipStack::Iter::Clip::getBounds() const {
640 if (NULL != fRect) {
641 return *fRect;
642 } else if (NULL != fPath) {
643 return fPath->getBounds();
644 } else {
645 static const SkRect kEmpty = {0, 0, 0, 0};
646 return kEmpty;
647 }
648}
649
bsalomon@google.com51a62862012-11-26 21:19:43 +0000650bool SkClipStack::Iter::Clip::contains(const SkRect& rect) const {
651 if (NULL != fRect) {
652 return fRect->contains(rect);
653 } else if (NULL != fPath) {
654 return fPath->conservativelyContainsRect(rect);
655 } else {
656 return false;
657 }
658}
659
660bool SkClipStack::Iter::Clip::isInverseFilled() const {
661 return NULL != fPath && fPath->isInverseFillType();
662}
663
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000664SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc)
665 : fStack(&stack) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000666 this->reset(stack, startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000667}
668
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000669const SkClipStack::Iter::Clip* SkClipStack::Iter::updateClip(
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000670 const SkClipStack::Element* element) {
bsalomon@google.com417bc132012-11-29 19:16:32 +0000671 switch (element->fType) {
672 case SkClipStack::Element::kEmpty_Type:
reed@google.com5c3d1472011-02-22 19:12:23 +0000673 fClip.fRect = NULL;
674 fClip.fPath = NULL;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000675 element->checkEmpty();
reed@google.com5c3d1472011-02-22 19:12:23 +0000676 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000677 case SkClipStack::Element::kRect_Type:
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000678 fClip.fRect = &element->fRect;
reed@google.com5c3d1472011-02-22 19:12:23 +0000679 fClip.fPath = NULL;
680 break;
bsalomon@google.com417bc132012-11-29 19:16:32 +0000681 case SkClipStack::Element::kPath_Type:
reed@google.com5c3d1472011-02-22 19:12:23 +0000682 fClip.fRect = NULL;
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000683 fClip.fPath = &element->fPath;
reed@google.com5c3d1472011-02-22 19:12:23 +0000684 break;
685 }
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000686 fClip.fOp = element->fOp;
687 fClip.fDoAA = element->fDoAA;
688 fClip.fGenID = element->fGenID;
reed@google.com5c3d1472011-02-22 19:12:23 +0000689 return &fClip;
690}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000691
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000692const SkClipStack::Iter::Clip* SkClipStack::Iter::next() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000693 const SkClipStack::Element* element = (const SkClipStack::Element*)fIter.next();
694 if (NULL == element) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000695 return NULL;
696 }
697
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000698 return this->updateClip(element);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000699}
700
701const SkClipStack::Iter::Clip* SkClipStack::Iter::prev() {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000702 const SkClipStack::Element* element = (const SkClipStack::Element*)fIter.prev();
703 if (NULL == element) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000704 return NULL;
705 }
706
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000707 return this->updateClip(element);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000708}
709
robertphillips@google.com80214e22012-07-20 15:33:18 +0000710const SkClipStack::Iter::Clip* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000711
712 if (NULL == fStack) {
713 return NULL;
714 }
715
716 fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart);
717
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000718 const SkClipStack::Element* element = NULL;
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000719
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000720 for (element = (const SkClipStack::Element*) fIter.prev();
721 NULL != element;
722 element = (const SkClipStack::Element*) fIter.prev()) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000723
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000724 if (op == element->fOp) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000725 // The Deque's iterator is actually one pace ahead of the
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000726 // returned value. So while "element" is the element we want to
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000727 // return, the iterator is actually pointing at (and will
728 // return on the next "next" or "prev" call) the element
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729 // in front of it in the deque. Bump the iterator forward a
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000730 // step so we get the expected result.
731 if (NULL == fIter.next()) {
732 // The reverse iterator has run off the front of the deque
733 // (i.e., the "op" clip is the first clip) and can't
734 // recover. Reset the iterator to start at the front.
735 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
736 }
737 break;
738 }
739 }
740
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000741 if (NULL == element) {
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000742 // There were no "op" clips
743 fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart);
744 }
745
746 return this->next();
747}
748
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000749const SkClipStack::Iter::Clip* SkClipStack::Iter::nextCombined() {
750 const Clip* clip;
751
752 if (NULL != (clip = this->next()) &&
753 SkRegion::kIntersect_Op == clip->fOp &&
754 NULL != clip->fRect) {
755 fCombinedRect = *clip->fRect;
756 bool doAA = clip->fDoAA;
757
758 while(NULL != (clip = this->next()) &&
759 SkRegion::kIntersect_Op == clip->fOp &&
760 NULL != clip->fRect) { // backup if non-null
761 /**
762 * If the AA settings don't match on consecutive rects we can still continue if
763 * either contains the other. Otherwise, we must stop.
764 */
765 if (doAA != clip->fDoAA) {
766 if (fCombinedRect.contains(*clip->fRect)) {
767 fCombinedRect = *clip->fRect;
768 doAA = clip->fDoAA;
769 } else if (!clip->fRect->contains(fCombinedRect)) {
770 break;
771 }
772 } else if (!fCombinedRect.intersect(*fClip.fRect)) {
773 fCombinedRect.setEmpty();
774 clip = NULL; // prevents unnecessary rewind below.
775 break;
776 }
777 }
778 // If we got here and clip is non-NULL then we got an element that we weren't able to
779 // combine. We need to backup one to ensure that the callers next next() call returns it.
780 if (NULL != clip) {
781 // If next() above returned the last element then due to Iter's internal workings prev()
782 // will return NULL. In that case we reset to the last element.
783 if (NULL == this->prev()) {
784 this->reset(*fStack, SkClipStack::Iter::kTop_IterStart);
785 }
786 }
787
788 // Must do this last because it is overwritten in the above backup.
789 fClip.fRect = &fCombinedRect;
790 fClip.fPath = NULL;
791 fClip.fOp = SkRegion::kIntersect_Op;
792 fClip.fDoAA = doAA;
793 fClip.fGenID = kInvalidGenID;
794 return &fClip;
795 } else {
796 return clip;
797 }
798}
799
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000800void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000801 fStack = &stack;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000802 fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000803}
robertphillips@google.com607fe072012-07-24 13:54:00 +0000804
805// helper method
806void SkClipStack::getConservativeBounds(int offsetX,
807 int offsetY,
808 int maxWidth,
809 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000810 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000811 bool* isIntersectionOfRects) const {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000812 SkASSERT(NULL != devBounds);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000813
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000814 devBounds->setLTRB(0, 0,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000815 SkIntToScalar(maxWidth), SkIntToScalar(maxHeight));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000816
817 SkRect temp;
818 SkClipStack::BoundsType boundType;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000819
robertphillips@google.com7b112892012-07-31 15:18:21 +0000820 // temp starts off in canvas space here
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000821 this->getBounds(&temp, &boundType, isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000822 if (SkClipStack::kInsideOut_BoundsType == boundType) {
823 return;
824 }
825
robertphillips@google.com7b112892012-07-31 15:18:21 +0000826 // but is converted to device space here
robertphillips@google.com607fe072012-07-24 13:54:00 +0000827 temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY));
828
robertphillips@google.com7b112892012-07-31 15:18:21 +0000829 if (!devBounds->intersect(temp)) {
830 devBounds->setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000831 }
832}
robertphillips@google.com46f93502012-08-07 15:38:08 +0000833
834void SkClipStack::addPurgeClipCallback(PFPurgeClipCB callback, void* data) const {
835 ClipCallbackData temp = { callback, data };
836 fCallbackData.append(1, &temp);
837}
838
839void SkClipStack::removePurgeClipCallback(PFPurgeClipCB callback, void* data) const {
840 ClipCallbackData temp = { callback, data };
841 int index = fCallbackData.find(temp);
842 if (index >= 0) {
843 fCallbackData.removeShuffle(index);
844 }
845}
846
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000847// The clip state represented by 'element' will never be used again. Purge it.
848void SkClipStack::purgeClip(Element* element) {
849 SkASSERT(NULL != element);
850 if (element->fGenID >= 0 && element->fGenID < kFirstUnreservedGenID) {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000851 return;
852 }
robertphillips@google.com46f93502012-08-07 15:38:08 +0000853
854 for (int i = 0; i < fCallbackData.count(); ++i) {
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000855 (*fCallbackData[i].fCallback)(element->fGenID, fCallbackData[i].fData);
robertphillips@google.com46f93502012-08-07 15:38:08 +0000856 }
857
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000858 // Invalidate element's gen ID so handlers can detect already handled records
859 element->fGenID = kInvalidGenID;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000860}
861
862int32_t SkClipStack::GetNextGenID() {
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000863 // TODO: handle overflow.
robertphillips@google.com46f93502012-08-07 15:38:08 +0000864 return sk_atomic_inc(&gGenID);
865}
robertphillips@google.com73e71022012-08-09 18:10:49 +0000866
867int32_t SkClipStack::getTopmostGenID() const {
868
869 if (fDeque.empty()) {
870 return kInvalidGenID;
871 }
872
bsalomon@google.com9128edc2012-11-29 18:58:19 +0000873 Element* element = (Element*)fDeque.back();
874 return element->fGenID;
robertphillips@google.com73e71022012-08-09 18:10:49 +0000875}