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