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