reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 1 | /* |
| 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" |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 9 | #include "SkPath.h" |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 10 | |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 11 | SkRasterClip::SkRasterClip(const SkRasterClip& src) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 12 | AUTO_RASTERCLIP_VALIDATE(src); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 13 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 14 | fForceConservativeRects = src.fForceConservativeRects; |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 15 | fIsBW = src.fIsBW; |
| 16 | if (fIsBW) { |
| 17 | fBW = src.fBW; |
| 18 | } else { |
| 19 | fAA = src.fAA; |
| 20 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 21 | |
| 22 | fIsEmpty = src.isEmpty(); |
| 23 | fIsRect = src.isRect(); |
| 24 | SkDEBUGCODE(this->validate();) |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 25 | } |
| 26 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame^] | 27 | SkRasterClip::SkRasterClip(const SkRegion& rgn) : fBW(rgn) { |
| 28 | fForceConservativeRects = false; |
| 29 | fIsBW = true; |
| 30 | fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
| 31 | fIsRect = !fIsEmpty; |
| 32 | SkDEBUGCODE(this->validate();) |
| 33 | } |
| 34 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 35 | SkRasterClip::SkRasterClip(const SkIRect& bounds, bool forceConservativeRects) : fBW(bounds) { |
| 36 | fForceConservativeRects = forceConservativeRects; |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 37 | fIsBW = true; |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 38 | fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
| 39 | fIsRect = !fIsEmpty; |
| 40 | SkDEBUGCODE(this->validate();) |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 41 | } |
| 42 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 43 | SkRasterClip::SkRasterClip(bool forceConservativeRects) { |
| 44 | fForceConservativeRects = forceConservativeRects; |
| 45 | fIsBW = true; |
| 46 | fIsEmpty = true; |
| 47 | fIsRect = false; |
| 48 | SkDEBUGCODE(this->validate();) |
| 49 | } |
| 50 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 51 | SkRasterClip::~SkRasterClip() { |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 52 | SkDEBUGCODE(this->validate();) |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame^] | 55 | bool SkRasterClip::operator==(const SkRasterClip& other) const { |
| 56 | // This impl doesn't care if fForceConservativeRects is the same in both, only the current state |
| 57 | |
| 58 | if (fIsBW != other.fIsBW) { |
| 59 | return false; |
| 60 | } |
| 61 | bool isEqual = fIsBW ? fBW == other.fBW : fAA == other.fAA; |
| 62 | #ifdef SK_DEBUG |
| 63 | if (isEqual) { |
| 64 | SkASSERT(fIsEmpty == other.fIsEmpty); |
| 65 | SkASSERT(fIsRect == other.fIsRect); |
| 66 | } |
| 67 | #endif |
| 68 | return isEqual; |
| 69 | } |
| 70 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 71 | bool SkRasterClip::isComplex() const { |
| 72 | return fIsBW ? fBW.isComplex() : !fAA.isEmpty(); |
| 73 | } |
| 74 | |
| 75 | const SkIRect& SkRasterClip::getBounds() const { |
| 76 | return fIsBW ? fBW.getBounds() : fAA.getBounds(); |
| 77 | } |
| 78 | |
| 79 | bool SkRasterClip::setEmpty() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 80 | AUTO_RASTERCLIP_VALIDATE(*this); |
| 81 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 82 | fIsBW = true; |
| 83 | fBW.setEmpty(); |
| 84 | fAA.setEmpty(); |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 85 | fIsEmpty = true; |
| 86 | fIsRect = false; |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 90 | bool SkRasterClip::setRect(const SkIRect& rect) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 91 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 92 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 93 | fIsBW = true; |
| 94 | fAA.setEmpty(); |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 95 | fIsRect = fBW.setRect(rect); |
| 96 | fIsEmpty = !fIsRect; |
| 97 | return fIsRect; |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 100 | ///////////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | bool SkRasterClip::setConservativeRect(const SkRect& r, const SkIRect& clipR, bool isInverse) { |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 103 | SkRegion::Op op; |
| 104 | if (isInverse) { |
| 105 | op = SkRegion::kDifference_Op; |
| 106 | } else { |
| 107 | op = SkRegion::kIntersect_Op; |
| 108 | } |
| 109 | fBW.setRect(clipR); |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 110 | fBW.op(r.roundOut(), op); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 111 | return this->updateCacheAndReturnNonEmpty(); |
| 112 | } |
| 113 | |
| 114 | ///////////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | enum MutateResult { |
| 117 | kDoNothing_MutateResult, |
| 118 | kReplaceClippedAgainstGlobalBounds_MutateResult, |
| 119 | kContinue_MutateResult, |
| 120 | }; |
| 121 | |
| 122 | static MutateResult mutate_conservative_op(SkRegion::Op* op, bool inverseFilled) { |
| 123 | if (inverseFilled) { |
| 124 | switch (*op) { |
| 125 | case SkRegion::kIntersect_Op: |
| 126 | case SkRegion::kDifference_Op: |
| 127 | // These ops can only shrink the current clip. So leaving |
| 128 | // the clip unchanged conservatively respects the contract. |
| 129 | return kDoNothing_MutateResult; |
| 130 | case SkRegion::kUnion_Op: |
| 131 | case SkRegion::kReplace_Op: |
| 132 | case SkRegion::kReverseDifference_Op: |
| 133 | case SkRegion::kXOR_Op: { |
| 134 | // These ops can grow the current clip up to the extents of |
| 135 | // the input clip, which is inverse filled, so we just set |
| 136 | // the current clip to the device bounds. |
| 137 | *op = SkRegion::kReplace_Op; |
| 138 | return kReplaceClippedAgainstGlobalBounds_MutateResult; |
| 139 | } |
| 140 | } |
| 141 | } else { |
| 142 | // Not inverse filled |
| 143 | switch (*op) { |
| 144 | case SkRegion::kIntersect_Op: |
| 145 | case SkRegion::kUnion_Op: |
| 146 | case SkRegion::kReplace_Op: |
| 147 | return kContinue_MutateResult; |
| 148 | case SkRegion::kDifference_Op: |
| 149 | // Difference can only shrink the current clip. |
| 150 | // Leaving clip unchanged conservatively fullfills the contract. |
| 151 | return kDoNothing_MutateResult; |
| 152 | case SkRegion::kReverseDifference_Op: |
| 153 | // To reverse, we swap in the bounds with a replace op. |
| 154 | // As with difference, leave it unchanged. |
| 155 | *op = SkRegion::kReplace_Op; |
| 156 | return kContinue_MutateResult; |
| 157 | case SkRegion::kXOR_Op: |
| 158 | // Be conservative, based on (A XOR B) always included in (A union B), |
| 159 | // which is always included in (bounds(A) union bounds(B)) |
| 160 | *op = SkRegion::kUnion_Op; |
| 161 | return kContinue_MutateResult; |
| 162 | } |
| 163 | } |
| 164 | SkFAIL("should not get here"); |
| 165 | return kDoNothing_MutateResult; |
| 166 | } |
| 167 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 168 | bool SkRasterClip::setPath(const SkPath& path, const SkRegion& clip, bool doAA) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 169 | AUTO_RASTERCLIP_VALIDATE(*this); |
| 170 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 171 | if (fForceConservativeRects) { |
| 172 | return this->setConservativeRect(path.getBounds(), clip.getBounds(), path.isInverseFillType()); |
| 173 | } |
| 174 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 175 | if (this->isBW() && !doAA) { |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 176 | (void)fBW.setPath(path, clip); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 177 | } else { |
reed@google.com | 897fc41 | 2012-02-16 17:11:25 +0000 | [diff] [blame] | 178 | // TODO: since we are going to over-write fAA completely (aren't we?) |
| 179 | // we should just clear our BW data (if any) and set fIsAA=true |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 180 | if (this->isBW()) { |
| 181 | this->convertToAA(); |
| 182 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 183 | (void)fAA.setPath(path, &clip, doAA); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 184 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 185 | return this->updateCacheAndReturnNonEmpty(); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 188 | bool SkRasterClip::op(const SkRRect& rrect, const SkIRect& bounds, SkRegion::Op op, bool doAA) { |
robertphillips | 125f19a | 2015-11-23 09:00:05 -0800 | [diff] [blame] | 189 | if (fForceConservativeRects) { |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 190 | return this->op(rrect.getBounds(), bounds, op, doAA); |
robertphillips | 125f19a | 2015-11-23 09:00:05 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | SkPath path; |
| 194 | path.addRRect(rrect); |
| 195 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 196 | return this->op(path, bounds, op, doAA); |
robertphillips | 125f19a | 2015-11-23 09:00:05 -0800 | [diff] [blame] | 197 | } |
| 198 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 199 | bool SkRasterClip::op(const SkPath& path, const SkIRect& bounds, SkRegion::Op op, bool doAA) { |
robertphillips | 125f19a | 2015-11-23 09:00:05 -0800 | [diff] [blame] | 200 | AUTO_RASTERCLIP_VALIDATE(*this); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 201 | |
| 202 | if (fForceConservativeRects) { |
| 203 | SkIRect ir; |
| 204 | switch (mutate_conservative_op(&op, path.isInverseFillType())) { |
| 205 | case kDoNothing_MutateResult: |
| 206 | return !this->isEmpty(); |
| 207 | case kReplaceClippedAgainstGlobalBounds_MutateResult: |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 208 | ir = bounds; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 209 | break; |
| 210 | case kContinue_MutateResult: |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 211 | ir = path.getBounds().roundOut(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | return this->op(ir, op); |
| 215 | } |
| 216 | |
robertphillips | 125f19a | 2015-11-23 09:00:05 -0800 | [diff] [blame] | 217 | // base is used to limit the size (and therefore memory allocation) of the |
| 218 | // region that results from scan converting devPath. |
| 219 | SkRegion base; |
| 220 | |
reed | d64c948 | 2014-09-05 17:37:38 -0700 | [diff] [blame] | 221 | if (SkRegion::kIntersect_Op == op) { |
| 222 | // since we are intersect, we can do better (tighter) with currRgn's |
| 223 | // bounds, than just using the device. However, if currRgn is complex, |
| 224 | // our region blitter may hork, so we do that case in two steps. |
| 225 | if (this->isRect()) { |
| 226 | // FIXME: we should also be able to do this when this->isBW(), |
| 227 | // but relaxing the test above triggers GM asserts in |
| 228 | // SkRgnBuilder::blitH(). We need to investigate what's going on. |
| 229 | return this->setPath(path, this->bwRgn(), doAA); |
| 230 | } else { |
| 231 | base.setRect(this->getBounds()); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 232 | SkRasterClip clip(fForceConservativeRects); |
reed | d64c948 | 2014-09-05 17:37:38 -0700 | [diff] [blame] | 233 | clip.setPath(path, base, doAA); |
| 234 | return this->op(clip, op); |
| 235 | } |
| 236 | } else { |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 237 | base.setRect(bounds); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 238 | |
reed | d64c948 | 2014-09-05 17:37:38 -0700 | [diff] [blame] | 239 | if (SkRegion::kReplace_Op == op) { |
| 240 | return this->setPath(path, base, doAA); |
| 241 | } else { |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 242 | SkRasterClip clip(fForceConservativeRects); |
reed | d64c948 | 2014-09-05 17:37:38 -0700 | [diff] [blame] | 243 | clip.setPath(path, base, doAA); |
| 244 | return this->op(clip, op); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 249 | bool SkRasterClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) { |
| 250 | SkRegion tmp; |
| 251 | tmp.setRect(clip); |
| 252 | return this->setPath(path, tmp, doAA); |
| 253 | } |
| 254 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 255 | bool SkRasterClip::op(const SkIRect& rect, SkRegion::Op op) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 256 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 257 | |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 258 | fIsBW ? fBW.op(rect, op) : fAA.op(rect, op); |
| 259 | return this->updateCacheAndReturnNonEmpty(); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 260 | } |
| 261 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 262 | bool SkRasterClip::op(const SkRegion& rgn, SkRegion::Op op) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 263 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 264 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 265 | if (fIsBW) { |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 266 | (void)fBW.op(rgn, op); |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 267 | } else { |
| 268 | SkAAClip tmp; |
| 269 | tmp.setRegion(rgn); |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 270 | (void)fAA.op(tmp, op); |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 271 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 272 | return this->updateCacheAndReturnNonEmpty(); |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 273 | } |
| 274 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 275 | bool SkRasterClip::op(const SkRasterClip& clip, SkRegion::Op op) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 276 | AUTO_RASTERCLIP_VALIDATE(*this); |
| 277 | clip.validate(); |
| 278 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 279 | if (this->isBW() && clip.isBW()) { |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 280 | (void)fBW.op(clip.fBW, op); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 281 | } else { |
| 282 | SkAAClip tmp; |
| 283 | const SkAAClip* other; |
| 284 | |
| 285 | if (this->isBW()) { |
| 286 | this->convertToAA(); |
| 287 | } |
| 288 | if (clip.isBW()) { |
| 289 | tmp.setRegion(clip.bwRgn()); |
| 290 | other = &tmp; |
| 291 | } else { |
| 292 | other = &clip.aaRgn(); |
| 293 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 294 | (void)fAA.op(*other, op); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 295 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 296 | return this->updateCacheAndReturnNonEmpty(); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 297 | } |
| 298 | |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 299 | /** |
| 300 | * Our antialiasing currently has a granularity of 1/4 of a pixel along each |
| 301 | * axis. Thus we can treat an axis coordinate as an integer if it differs |
| 302 | * from its nearest int by < half of that value (1.8 in this case). |
| 303 | */ |
| 304 | static bool nearly_integral(SkScalar x) { |
| 305 | static const SkScalar domain = SK_Scalar1 / 4; |
| 306 | static const SkScalar halfDomain = domain / 2; |
| 307 | |
| 308 | x += halfDomain; |
| 309 | return x - SkScalarFloorToScalar(x) < domain; |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 312 | bool SkRasterClip::op(const SkRect& r, const SkIRect& bounds, SkRegion::Op op, bool doAA) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 313 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 314 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 315 | if (fForceConservativeRects) { |
| 316 | SkIRect ir; |
| 317 | switch (mutate_conservative_op(&op, false)) { |
| 318 | case kDoNothing_MutateResult: |
| 319 | return !this->isEmpty(); |
| 320 | case kReplaceClippedAgainstGlobalBounds_MutateResult: |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 321 | ir = bounds; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 322 | break; |
| 323 | case kContinue_MutateResult: |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 324 | ir = r.roundOut(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | return this->op(ir, op); |
| 328 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 329 | |
reed@google.com | 420f74f | 2012-05-11 18:46:43 +0000 | [diff] [blame] | 330 | if (fIsBW && doAA) { |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 331 | // check that the rect really needs aa, or is it close enought to |
| 332 | // integer boundaries that we can just treat it as a BW rect? |
| 333 | if (nearly_integral(r.fLeft) && nearly_integral(r.fTop) && |
| 334 | nearly_integral(r.fRight) && nearly_integral(r.fBottom)) { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 335 | doAA = false; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (fIsBW && !doAA) { |
| 340 | SkIRect ir; |
| 341 | r.round(&ir); |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 342 | (void)fBW.op(ir, op); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 343 | } else { |
| 344 | if (fIsBW) { |
| 345 | this->convertToAA(); |
| 346 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 347 | (void)fAA.op(r, op, doAA); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 348 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 349 | return this->updateCacheAndReturnNonEmpty(); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 350 | } |
| 351 | |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 352 | void SkRasterClip::translate(int dx, int dy, SkRasterClip* dst) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 353 | if (nullptr == dst) { |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 354 | return; |
| 355 | } |
| 356 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 357 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 358 | |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 359 | if (this->isEmpty()) { |
| 360 | dst->setEmpty(); |
| 361 | return; |
| 362 | } |
| 363 | if (0 == (dx | dy)) { |
| 364 | *dst = *this; |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | dst->fIsBW = fIsBW; |
| 369 | if (fIsBW) { |
| 370 | fBW.translate(dx, dy, &dst->fBW); |
| 371 | dst->fAA.setEmpty(); |
| 372 | } else { |
| 373 | fAA.translate(dx, dy, &dst->fAA); |
| 374 | dst->fBW.setEmpty(); |
| 375 | } |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 376 | dst->updateCacheAndReturnNonEmpty(); |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 377 | } |
| 378 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 379 | bool SkRasterClip::quickContains(const SkIRect& ir) const { |
| 380 | return fIsBW ? fBW.quickContains(ir) : fAA.quickContains(ir); |
| 381 | } |
| 382 | |
reed@google.com | 34f7e47 | 2011-10-13 15:11:59 +0000 | [diff] [blame] | 383 | /////////////////////////////////////////////////////////////////////////////// |
| 384 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 385 | const SkRegion& SkRasterClip::forceGetBW() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 386 | AUTO_RASTERCLIP_VALIDATE(*this); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 387 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame] | 388 | if (!fIsBW) { |
| 389 | fBW.setRect(fAA.getBounds()); |
| 390 | } |
| 391 | return fBW; |
| 392 | } |
| 393 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 394 | void SkRasterClip::convertToAA() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 395 | AUTO_RASTERCLIP_VALIDATE(*this); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 396 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 397 | SkASSERT(!fForceConservativeRects); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 398 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 399 | SkASSERT(fIsBW); |
| 400 | fAA.setRegion(fBW); |
| 401 | fIsBW = false; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 402 | |
reed | 202ab2a | 2014-08-07 11:48:10 -0700 | [diff] [blame] | 403 | // since we are being explicitly asked to convert-to-aa, we pass false so we don't "optimize" |
| 404 | // ourselves back to BW. |
| 405 | (void)this->updateCacheAndReturnNonEmpty(false); |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 408 | #ifdef SK_DEBUG |
| 409 | void SkRasterClip::validate() const { |
| 410 | // can't ever assert that fBW is empty, since we may have called forceGetBW |
| 411 | if (fIsBW) { |
| 412 | SkASSERT(fAA.isEmpty()); |
| 413 | } |
| 414 | |
| 415 | fBW.validate(); |
| 416 | fAA.validate(); |
reed@google.com | a1c6ff4 | 2012-05-11 14:36:57 +0000 | [diff] [blame] | 417 | |
| 418 | SkASSERT(this->computeIsEmpty() == fIsEmpty); |
| 419 | SkASSERT(this->computeIsRect() == fIsRect); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | |
| 423 | /////////////////////////////////////////////////////////////////////////////// |
| 424 | |
| 425 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 426 | SkDEBUGCODE(fClipRgn = nullptr;) |
| 427 | SkDEBUGCODE(fBlitter = nullptr;) |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkRasterClip& clip, |
| 431 | SkBlitter* blitter) { |
| 432 | this->init(clip, blitter); |
| 433 | } |
| 434 | |
| 435 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkAAClip* aaclip, |
| 436 | SkBlitter* blitter) { |
| 437 | SkASSERT(blitter); |
| 438 | SkASSERT(aaclip); |
| 439 | fBWRgn.setRect(aaclip->getBounds()); |
| 440 | fAABlitter.init(blitter, aaclip); |
| 441 | // now our return values |
| 442 | fClipRgn = &fBWRgn; |
| 443 | fBlitter = &fAABlitter; |
| 444 | } |
| 445 | |
| 446 | void SkAAClipBlitterWrapper::init(const SkRasterClip& clip, SkBlitter* blitter) { |
| 447 | SkASSERT(blitter); |
| 448 | if (clip.isBW()) { |
| 449 | fClipRgn = &clip.bwRgn(); |
| 450 | fBlitter = blitter; |
| 451 | } else { |
| 452 | const SkAAClip& aaclip = clip.aaRgn(); |
| 453 | fBWRgn.setRect(aaclip.getBounds()); |
| 454 | fAABlitter.init(blitter, &aaclip); |
| 455 | // now our return values |
| 456 | fClipRgn = &fBWRgn; |
| 457 | fBlitter = &fAABlitter; |
| 458 | } |
| 459 | } |