blob: 35197c1ad9bdd3371df62a7d7492ac3dc2fedaad [file] [log] [blame]
reed@google.comd1e3c5f2011-10-10 19:36:25 +00001/*
2 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkRasterClip.h"
reedd9544982014-09-09 18:46:22 -07009#include "SkPath.h"
reed@google.comd1e3c5f2011-10-10 19:36:25 +000010
reed@google.com34f7e472011-10-13 15:11:59 +000011SkRasterClip::SkRasterClip(const SkRasterClip& src) {
reed@google.com045e62d2011-10-24 12:19:46 +000012 AUTO_RASTERCLIP_VALIDATE(src);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000013
reedd9544982014-09-09 18:46:22 -070014 fForceConservativeRects = src.fForceConservativeRects;
reed@google.com34f7e472011-10-13 15:11:59 +000015 fIsBW = src.fIsBW;
16 if (fIsBW) {
17 fBW = src.fBW;
18 } else {
19 fAA = src.fAA;
20 }
reed@google.coma1c6ff42012-05-11 14:36:57 +000021
22 fIsEmpty = src.isEmpty();
23 fIsRect = src.isRect();
24 SkDEBUGCODE(this->validate();)
reed@google.com34f7e472011-10-13 15:11:59 +000025}
26
reedd9544982014-09-09 18:46:22 -070027SkRasterClip::SkRasterClip(const SkIRect& bounds, bool forceConservativeRects) : fBW(bounds) {
28 fForceConservativeRects = forceConservativeRects;
reed@google.comba16da92011-10-11 13:15:03 +000029 fIsBW = true;
reed@google.coma1c6ff42012-05-11 14:36:57 +000030 fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute
31 fIsRect = !fIsEmpty;
32 SkDEBUGCODE(this->validate();)
reed@google.comba16da92011-10-11 13:15:03 +000033}
34
reedd9544982014-09-09 18:46:22 -070035SkRasterClip::SkRasterClip(bool forceConservativeRects) {
36 fForceConservativeRects = forceConservativeRects;
37 fIsBW = true;
38 fIsEmpty = true;
39 fIsRect = false;
40 SkDEBUGCODE(this->validate();)
41}
42
reed@google.com045e62d2011-10-24 12:19:46 +000043SkRasterClip::~SkRasterClip() {
reed@google.coma1c6ff42012-05-11 14:36:57 +000044 SkDEBUGCODE(this->validate();)
reed@google.comd1e3c5f2011-10-10 19:36:25 +000045}
46
47bool SkRasterClip::isComplex() const {
48 return fIsBW ? fBW.isComplex() : !fAA.isEmpty();
49}
50
51const SkIRect& SkRasterClip::getBounds() const {
52 return fIsBW ? fBW.getBounds() : fAA.getBounds();
53}
54
55bool SkRasterClip::setEmpty() {
reed@google.com045e62d2011-10-24 12:19:46 +000056 AUTO_RASTERCLIP_VALIDATE(*this);
57
reed@google.comd1e3c5f2011-10-10 19:36:25 +000058 fIsBW = true;
59 fBW.setEmpty();
60 fAA.setEmpty();
reed@google.coma1c6ff42012-05-11 14:36:57 +000061 fIsEmpty = true;
62 fIsRect = false;
reed@google.comd1e3c5f2011-10-10 19:36:25 +000063 return false;
64}
65
reed@google.comba16da92011-10-11 13:15:03 +000066bool SkRasterClip::setRect(const SkIRect& rect) {
reed@google.com045e62d2011-10-24 12:19:46 +000067 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
reed@google.comd1e3c5f2011-10-10 19:36:25 +000069 fIsBW = true;
70 fAA.setEmpty();
reed@google.coma1c6ff42012-05-11 14:36:57 +000071 fIsRect = fBW.setRect(rect);
72 fIsEmpty = !fIsRect;
73 return fIsRect;
reed@google.comd1e3c5f2011-10-10 19:36:25 +000074}
75
reedd9544982014-09-09 18:46:22 -070076/////////////////////////////////////////////////////////////////////////////////////
77
78bool SkRasterClip::setConservativeRect(const SkRect& r, const SkIRect& clipR, bool isInverse) {
reedd9544982014-09-09 18:46:22 -070079 SkRegion::Op op;
80 if (isInverse) {
81 op = SkRegion::kDifference_Op;
82 } else {
83 op = SkRegion::kIntersect_Op;
84 }
85 fBW.setRect(clipR);
reedb07a94f2014-11-19 05:03:18 -080086 fBW.op(r.roundOut(), op);
reedd9544982014-09-09 18:46:22 -070087 return this->updateCacheAndReturnNonEmpty();
88}
89
90/////////////////////////////////////////////////////////////////////////////////////
91
92enum MutateResult {
93 kDoNothing_MutateResult,
94 kReplaceClippedAgainstGlobalBounds_MutateResult,
95 kContinue_MutateResult,
96};
97
98static MutateResult mutate_conservative_op(SkRegion::Op* op, bool inverseFilled) {
99 if (inverseFilled) {
100 switch (*op) {
101 case SkRegion::kIntersect_Op:
102 case SkRegion::kDifference_Op:
103 // These ops can only shrink the current clip. So leaving
104 // the clip unchanged conservatively respects the contract.
105 return kDoNothing_MutateResult;
106 case SkRegion::kUnion_Op:
107 case SkRegion::kReplace_Op:
108 case SkRegion::kReverseDifference_Op:
109 case SkRegion::kXOR_Op: {
110 // These ops can grow the current clip up to the extents of
111 // the input clip, which is inverse filled, so we just set
112 // the current clip to the device bounds.
113 *op = SkRegion::kReplace_Op;
114 return kReplaceClippedAgainstGlobalBounds_MutateResult;
115 }
116 }
117 } else {
118 // Not inverse filled
119 switch (*op) {
120 case SkRegion::kIntersect_Op:
121 case SkRegion::kUnion_Op:
122 case SkRegion::kReplace_Op:
123 return kContinue_MutateResult;
124 case SkRegion::kDifference_Op:
125 // Difference can only shrink the current clip.
126 // Leaving clip unchanged conservatively fullfills the contract.
127 return kDoNothing_MutateResult;
128 case SkRegion::kReverseDifference_Op:
129 // To reverse, we swap in the bounds with a replace op.
130 // As with difference, leave it unchanged.
131 *op = SkRegion::kReplace_Op;
132 return kContinue_MutateResult;
133 case SkRegion::kXOR_Op:
134 // Be conservative, based on (A XOR B) always included in (A union B),
135 // which is always included in (bounds(A) union bounds(B))
136 *op = SkRegion::kUnion_Op;
137 return kContinue_MutateResult;
138 }
139 }
140 SkFAIL("should not get here");
141 return kDoNothing_MutateResult;
142}
143
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000144bool SkRasterClip::setPath(const SkPath& path, const SkRegion& clip, bool doAA) {
reed@google.com045e62d2011-10-24 12:19:46 +0000145 AUTO_RASTERCLIP_VALIDATE(*this);
146
reedd9544982014-09-09 18:46:22 -0700147 if (fForceConservativeRects) {
148 return this->setConservativeRect(path.getBounds(), clip.getBounds(), path.isInverseFillType());
149 }
150
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000151 if (this->isBW() && !doAA) {
reed@google.coma1c6ff42012-05-11 14:36:57 +0000152 (void)fBW.setPath(path, clip);
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000153 } else {
reed@google.com897fc412012-02-16 17:11:25 +0000154 // TODO: since we are going to over-write fAA completely (aren't we?)
155 // we should just clear our BW data (if any) and set fIsAA=true
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000156 if (this->isBW()) {
157 this->convertToAA();
158 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000159 (void)fAA.setPath(path, &clip, doAA);
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000160 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000161 return this->updateCacheAndReturnNonEmpty();
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000162}
163
reedd64c9482014-09-05 17:37:38 -0700164bool SkRasterClip::op(const SkPath& path, const SkISize& size, SkRegion::Op op, bool doAA) {
165 // base is used to limit the size (and therefore memory allocation) of the
166 // region that results from scan converting devPath.
167 SkRegion base;
reedd9544982014-09-09 18:46:22 -0700168
169 if (fForceConservativeRects) {
170 SkIRect ir;
171 switch (mutate_conservative_op(&op, path.isInverseFillType())) {
172 case kDoNothing_MutateResult:
173 return !this->isEmpty();
174 case kReplaceClippedAgainstGlobalBounds_MutateResult:
175 ir = SkIRect::MakeSize(size);
176 break;
177 case kContinue_MutateResult:
reedb07a94f2014-11-19 05:03:18 -0800178 ir = path.getBounds().roundOut();
reedd9544982014-09-09 18:46:22 -0700179 break;
180 }
181 return this->op(ir, op);
182 }
183
reedd64c9482014-09-05 17:37:38 -0700184 if (SkRegion::kIntersect_Op == op) {
185 // since we are intersect, we can do better (tighter) with currRgn's
186 // bounds, than just using the device. However, if currRgn is complex,
187 // our region blitter may hork, so we do that case in two steps.
188 if (this->isRect()) {
189 // FIXME: we should also be able to do this when this->isBW(),
190 // but relaxing the test above triggers GM asserts in
191 // SkRgnBuilder::blitH(). We need to investigate what's going on.
192 return this->setPath(path, this->bwRgn(), doAA);
193 } else {
194 base.setRect(this->getBounds());
reedd9544982014-09-09 18:46:22 -0700195 SkRasterClip clip(fForceConservativeRects);
reedd64c9482014-09-05 17:37:38 -0700196 clip.setPath(path, base, doAA);
197 return this->op(clip, op);
198 }
199 } else {
200 base.setRect(0, 0, size.width(), size.height());
201
202 if (SkRegion::kReplace_Op == op) {
203 return this->setPath(path, base, doAA);
204 } else {
reedd9544982014-09-09 18:46:22 -0700205 SkRasterClip clip(fForceConservativeRects);
reedd64c9482014-09-05 17:37:38 -0700206 clip.setPath(path, base, doAA);
207 return this->op(clip, op);
208 }
209 }
210}
211
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000212bool SkRasterClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) {
213 SkRegion tmp;
214 tmp.setRect(clip);
215 return this->setPath(path, tmp, doAA);
216}
217
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000218bool SkRasterClip::op(const SkIRect& rect, SkRegion::Op op) {
reed@google.com045e62d2011-10-24 12:19:46 +0000219 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000220
reed@google.coma1c6ff42012-05-11 14:36:57 +0000221 fIsBW ? fBW.op(rect, op) : fAA.op(rect, op);
222 return this->updateCacheAndReturnNonEmpty();
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000223}
224
reed@google.comba16da92011-10-11 13:15:03 +0000225bool SkRasterClip::op(const SkRegion& rgn, SkRegion::Op op) {
reed@google.com045e62d2011-10-24 12:19:46 +0000226 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227
reed@google.comba16da92011-10-11 13:15:03 +0000228 if (fIsBW) {
reed@google.coma1c6ff42012-05-11 14:36:57 +0000229 (void)fBW.op(rgn, op);
reed@google.comba16da92011-10-11 13:15:03 +0000230 } else {
231 SkAAClip tmp;
232 tmp.setRegion(rgn);
reed@google.coma1c6ff42012-05-11 14:36:57 +0000233 (void)fAA.op(tmp, op);
reed@google.comba16da92011-10-11 13:15:03 +0000234 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000235 return this->updateCacheAndReturnNonEmpty();
reed@google.comba16da92011-10-11 13:15:03 +0000236}
237
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000238bool SkRasterClip::op(const SkRasterClip& clip, SkRegion::Op op) {
reed@google.com045e62d2011-10-24 12:19:46 +0000239 AUTO_RASTERCLIP_VALIDATE(*this);
240 clip.validate();
241
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000242 if (this->isBW() && clip.isBW()) {
reed@google.coma1c6ff42012-05-11 14:36:57 +0000243 (void)fBW.op(clip.fBW, op);
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000244 } else {
245 SkAAClip tmp;
246 const SkAAClip* other;
247
248 if (this->isBW()) {
249 this->convertToAA();
250 }
251 if (clip.isBW()) {
252 tmp.setRegion(clip.bwRgn());
253 other = &tmp;
254 } else {
255 other = &clip.aaRgn();
256 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000257 (void)fAA.op(*other, op);
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000258 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000259 return this->updateCacheAndReturnNonEmpty();
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000260}
261
reed@google.com18c464b2012-05-11 20:57:25 +0000262/**
263 * Our antialiasing currently has a granularity of 1/4 of a pixel along each
264 * axis. Thus we can treat an axis coordinate as an integer if it differs
265 * from its nearest int by < half of that value (1.8 in this case).
266 */
267static bool nearly_integral(SkScalar x) {
268 static const SkScalar domain = SK_Scalar1 / 4;
269 static const SkScalar halfDomain = domain / 2;
270
271 x += halfDomain;
272 return x - SkScalarFloorToScalar(x) < domain;
reed@google.com00177082011-10-12 14:34:30 +0000273}
274
reedd9544982014-09-09 18:46:22 -0700275bool SkRasterClip::op(const SkRect& r, const SkISize& size, SkRegion::Op op, bool doAA) {
reed@google.com045e62d2011-10-24 12:19:46 +0000276 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000277
reedd9544982014-09-09 18:46:22 -0700278 if (fForceConservativeRects) {
279 SkIRect ir;
280 switch (mutate_conservative_op(&op, false)) {
281 case kDoNothing_MutateResult:
282 return !this->isEmpty();
283 case kReplaceClippedAgainstGlobalBounds_MutateResult:
284 ir = SkIRect::MakeSize(size);
285 break;
286 case kContinue_MutateResult:
reedb07a94f2014-11-19 05:03:18 -0800287 ir = r.roundOut();
reedd9544982014-09-09 18:46:22 -0700288 break;
289 }
290 return this->op(ir, op);
291 }
292
reed@google.com420f74f2012-05-11 18:46:43 +0000293 if (fIsBW && doAA) {
reed@google.com18c464b2012-05-11 20:57:25 +0000294 // check that the rect really needs aa, or is it close enought to
295 // integer boundaries that we can just treat it as a BW rect?
296 if (nearly_integral(r.fLeft) && nearly_integral(r.fTop) &&
297 nearly_integral(r.fRight) && nearly_integral(r.fBottom)) {
reed@google.com00177082011-10-12 14:34:30 +0000298 doAA = false;
299 }
300 }
301
302 if (fIsBW && !doAA) {
303 SkIRect ir;
304 r.round(&ir);
reed@google.coma1c6ff42012-05-11 14:36:57 +0000305 (void)fBW.op(ir, op);
reed@google.com00177082011-10-12 14:34:30 +0000306 } else {
307 if (fIsBW) {
308 this->convertToAA();
309 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000310 (void)fAA.op(r, op, doAA);
reed@google.com00177082011-10-12 14:34:30 +0000311 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000312 return this->updateCacheAndReturnNonEmpty();
reed@google.com00177082011-10-12 14:34:30 +0000313}
314
reed@google.com34f7e472011-10-13 15:11:59 +0000315void SkRasterClip::translate(int dx, int dy, SkRasterClip* dst) const {
316 if (NULL == dst) {
317 return;
318 }
319
reed@google.com045e62d2011-10-24 12:19:46 +0000320 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321
reed@google.com34f7e472011-10-13 15:11:59 +0000322 if (this->isEmpty()) {
323 dst->setEmpty();
324 return;
325 }
326 if (0 == (dx | dy)) {
327 *dst = *this;
328 return;
329 }
330
331 dst->fIsBW = fIsBW;
332 if (fIsBW) {
333 fBW.translate(dx, dy, &dst->fBW);
334 dst->fAA.setEmpty();
335 } else {
336 fAA.translate(dx, dy, &dst->fAA);
337 dst->fBW.setEmpty();
338 }
reed@google.coma1c6ff42012-05-11 14:36:57 +0000339 dst->updateCacheAndReturnNonEmpty();
reed@google.com34f7e472011-10-13 15:11:59 +0000340}
341
reed@google.com045e62d2011-10-24 12:19:46 +0000342bool SkRasterClip::quickContains(const SkIRect& ir) const {
343 return fIsBW ? fBW.quickContains(ir) : fAA.quickContains(ir);
344}
345
reed@google.com34f7e472011-10-13 15:11:59 +0000346///////////////////////////////////////////////////////////////////////////////
347
reed@google.comba16da92011-10-11 13:15:03 +0000348const SkRegion& SkRasterClip::forceGetBW() {
reed@google.com045e62d2011-10-24 12:19:46 +0000349 AUTO_RASTERCLIP_VALIDATE(*this);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000350
reed@google.comba16da92011-10-11 13:15:03 +0000351 if (!fIsBW) {
352 fBW.setRect(fAA.getBounds());
353 }
354 return fBW;
355}
356
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000357void SkRasterClip::convertToAA() {
reed@google.com045e62d2011-10-24 12:19:46 +0000358 AUTO_RASTERCLIP_VALIDATE(*this);
reedd9544982014-09-09 18:46:22 -0700359
360 SkASSERT(!fForceConservativeRects);
361
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000362 SkASSERT(fIsBW);
363 fAA.setRegion(fBW);
364 fIsBW = false;
reed202ab2a2014-08-07 11:48:10 -0700365
366 // since we are being explicitly asked to convert-to-aa, we pass false so we don't "optimize"
367 // ourselves back to BW.
368 (void)this->updateCacheAndReturnNonEmpty(false);
reed@google.comd1e3c5f2011-10-10 19:36:25 +0000369}
370
reed@google.com045e62d2011-10-24 12:19:46 +0000371#ifdef SK_DEBUG
372void SkRasterClip::validate() const {
373 // can't ever assert that fBW is empty, since we may have called forceGetBW
374 if (fIsBW) {
375 SkASSERT(fAA.isEmpty());
376 }
377
378 fBW.validate();
379 fAA.validate();
reed@google.coma1c6ff42012-05-11 14:36:57 +0000380
381 SkASSERT(this->computeIsEmpty() == fIsEmpty);
382 SkASSERT(this->computeIsRect() == fIsRect);
reed@google.com045e62d2011-10-24 12:19:46 +0000383}
384#endif
385
386///////////////////////////////////////////////////////////////////////////////
387
388SkAAClipBlitterWrapper::SkAAClipBlitterWrapper() {
389 SkDEBUGCODE(fClipRgn = NULL;)
390 SkDEBUGCODE(fBlitter = NULL;)
391}
392
393SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkRasterClip& clip,
394 SkBlitter* blitter) {
395 this->init(clip, blitter);
396}
397
398SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkAAClip* aaclip,
399 SkBlitter* blitter) {
400 SkASSERT(blitter);
401 SkASSERT(aaclip);
402 fBWRgn.setRect(aaclip->getBounds());
403 fAABlitter.init(blitter, aaclip);
404 // now our return values
405 fClipRgn = &fBWRgn;
406 fBlitter = &fAABlitter;
407}
408
409void SkAAClipBlitterWrapper::init(const SkRasterClip& clip, SkBlitter* blitter) {
410 SkASSERT(blitter);
411 if (clip.isBW()) {
412 fClipRgn = &clip.bwRgn();
413 fBlitter = blitter;
414 } else {
415 const SkAAClip& aaclip = clip.aaRgn();
416 fBWRgn.setRect(aaclip.getBounds());
417 fAABlitter.init(blitter, &aaclip);
418 // now our return values
419 fClipRgn = &fBWRgn;
420 fBlitter = &fAABlitter;
421 }
422}