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