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