| 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 "src/core/SkRasterClip.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkClipOp.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "src/core/SkRegionPriv.h" |
| 17 |
|
| 18 |
class SkBlitter; |
| 19 |
|
| 20 |
SkRasterClip::SkRasterClip(const SkRasterClip& that) |
| 21 |
: fIsBW(that.fIsBW) |
| 22 |
, fIsEmpty(that.fIsEmpty) |
| 23 |
, fIsRect(that.fIsRect) |
| 24 |
, fShader(that.fShader) |
| 25 |
{ |
| 26 |
AUTO_RASTERCLIP_VALIDATE(that); |
| 27 |
|
| 28 |
if (fIsBW) { |
| 29 |
fBW = that.fBW; |
| 30 |
} else { |
| 31 |
fAA = that.fAA; |
| 32 |
} |
| 33 |
|
| 34 |
SkDEBUGCODE(this->validate();) |
| 35 |
} |
| 36 |
|
| 37 |
SkRasterClip& SkRasterClip::operator=(const SkRasterClip& that) { |
| 38 |
AUTO_RASTERCLIP_VALIDATE(that); |
| 39 |
|
| 40 |
fIsBW = that.fIsBW; |
| 41 |
if (fIsBW) { |
| 42 |
fBW = that.fBW; |
| 43 |
} else { |
| 44 |
fAA = that.fAA; |
| 45 |
} |
| 46 |
|
| 47 |
fIsEmpty = that.isEmpty(); |
| 48 |
fIsRect = that.isRect(); |
| 49 |
fShader = that.fShader; |
| 50 |
SkDEBUGCODE(this->validate();) |
| 51 |
return *this; |
| 52 |
} |
| 53 |
|
| 54 |
SkRasterClip::SkRasterClip(const SkRegion& rgn) : fBW(rgn) { |
| 55 |
fIsBW = true; |
| 56 |
fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
| 57 |
fIsRect = !fIsEmpty; |
| 58 |
SkDEBUGCODE(this->validate();) |
| 59 |
} |
| 60 |
|
| 61 |
SkRasterClip::SkRasterClip(const SkIRect& bounds) : fBW(bounds) { |
| 62 |
fIsBW = true; |
| 63 |
fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
| 64 |
fIsRect = !fIsEmpty; |
| 65 |
SkDEBUGCODE(this->validate();) |
| 66 |
} |
| 67 |
|
| 68 |
SkRasterClip::SkRasterClip() { |
| 69 |
fIsBW = true; |
| 70 |
fIsEmpty = true; |
| 71 |
fIsRect = false; |
| 72 |
SkDEBUGCODE(this->validate();) |
| 73 |
} |
| 74 |
|
| 75 |
SkRasterClip::SkRasterClip(const SkPath& path, const SkIRect& bounds, bool doAA) { |
| 76 |
if (doAA) { |
| 77 |
fIsBW = false; |
| 78 |
fAA.setPath(path, bounds, true); |
| 79 |
} else { |
| 80 |
fIsBW = true; |
| 81 |
fBW.setPath(path, SkRegion(bounds)); |
| 82 |
} |
| 83 |
fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
| 84 |
fIsRect = this->computeIsRect(); |
| 85 |
SkDEBUGCODE(this->validate();) |
| 86 |
} |
| 87 |
|
| 88 |
SkRasterClip::~SkRasterClip() { |
| 89 |
SkDEBUGCODE(this->validate();) |
| 90 |
} |
| 91 |
|
| 92 |
bool SkRasterClip::setEmpty() { |
| 93 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 94 |
|
| 95 |
fIsBW = true; |
| 96 |
fBW.setEmpty(); |
| 97 |
fAA.setEmpty(); |
| 98 |
fIsEmpty = true; |
| 99 |
fIsRect = false; |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
bool SkRasterClip::setRect(const SkIRect& rect) { |
| 104 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 105 |
|
| 106 |
fIsBW = true; |
| 107 |
fAA.setEmpty(); |
| 108 |
fIsRect = fBW.setRect(rect); |
| 109 |
fIsEmpty = !fIsRect; |
| 110 |
return fIsRect; |
| 111 |
} |
| 112 |
|
| 113 |
///////////////////////////////////////////////////////////////////////////////////// |
| 114 |
|
| 115 |
bool SkRasterClip::op(const SkIRect& rect, SkClipOp op) { |
| 116 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 117 |
|
| 118 |
if (fIsBW) { |
| 119 |
fBW.op(rect, (SkRegion::Op) op); |
| 120 |
} else { |
| 121 |
fAA.op(rect, op); |
| 122 |
} |
| 123 |
return this->updateCacheAndReturnNonEmpty(); |
| 124 |
} |
| 125 |
|
| 126 |
bool SkRasterClip::op(const SkRegion& rgn, SkClipOp op) { |
| 127 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 128 |
|
| 129 |
if (fIsBW) { |
| 130 |
(void)fBW.op(rgn, (SkRegion::Op) op); |
| 131 |
} else { |
| 132 |
SkAAClip tmp; |
| 133 |
tmp.setRegion(rgn); |
| 134 |
(void)fAA.op(tmp, op); |
| 135 |
} |
| 136 |
return this->updateCacheAndReturnNonEmpty(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Our antialiasing currently has a granularity of 1/4 of a pixel along each |
| 141 |
* axis. Thus we can treat an axis coordinate as an integer if it differs |
| 142 |
* from its nearest int by < half of that value (1/8 in this case). |
| 143 |
*/ |
| 144 |
static bool nearly_integral(SkScalar x) { |
| 145 |
static const SkScalar domain = SK_Scalar1 / 4; |
| 146 |
static const SkScalar halfDomain = domain / 2; |
| 147 |
|
| 148 |
x += halfDomain; |
| 149 |
return x - SkScalarFloorToScalar(x) < domain; |
| 150 |
} |
| 151 |
|
| 152 |
bool SkRasterClip::op(const SkRect& localRect, const SkMatrix& matrix, SkClipOp op, bool doAA) { |
| 153 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 154 |
|
| 155 |
const bool isScaleTrans = matrix.isScaleTranslate(); |
| 156 |
if (!isScaleTrans) { |
| 157 |
return this->op(SkPath::Rect(localRect), matrix, op, doAA); |
| 158 |
} |
| 159 |
|
| 160 |
SkRect devRect = matrix.mapRect(localRect); |
| 161 |
if (fIsBW && doAA) { |
| 162 |
// check that the rect really needs aa, or is it close enought to |
| 163 |
// integer boundaries that we can just treat it as a BW rect? |
| 164 |
if (nearly_integral(devRect.fLeft) && nearly_integral(devRect.fTop) && |
| 165 |
nearly_integral(devRect.fRight) && nearly_integral(devRect.fBottom)) { |
| 166 |
doAA = false; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (fIsBW && !doAA) { |
| 171 |
(void)fBW.op(devRect.round(), (SkRegion::Op) op); |
| 172 |
} else { |
| 173 |
if (fIsBW) { |
| 174 |
this->convertToAA(); |
| 175 |
} |
| 176 |
(void)fAA.op(devRect, op, doAA); |
| 177 |
} |
| 178 |
return this->updateCacheAndReturnNonEmpty(); |
| 179 |
} |
| 180 |
|
| 181 |
bool SkRasterClip::op(const SkRRect& rrect, const SkMatrix& matrix, SkClipOp op, bool doAA) { |
| 182 |
return this->op(SkPath::RRect(rrect), matrix, op, doAA); |
| 183 |
} |
| 184 |
|
| 185 |
bool SkRasterClip::op(const SkPath& path, const SkMatrix& matrix, SkClipOp op, bool doAA) { |
| 186 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 187 |
|
| 188 |
SkPath devPath = path.makeTransform(matrix); |
| 189 |
|
| 190 |
// Since op is either intersect or difference, the clip is always shrinking; that means we can |
| 191 |
// always use our current bounds as the limiting factor for region/aaclip operations. |
| 192 |
if (this->isRect() && op == SkClipOp::kIntersect) { |
| 193 |
// However, in the relatively common case of intersecting a new path with a rectangular |
| 194 |
// clip, it's faster to convert the path into a region/aa-mask in place than evaluate the |
| 195 |
// actual intersection. See skbug.com/40043482 |
| 196 |
if (doAA && fIsBW) { |
| 197 |
this->convertToAA(); |
| 198 |
} |
| 199 |
if (fIsBW) { |
| 200 |
fBW.setPath(devPath, SkRegion(this->getBounds())); |
| 201 |
} else { |
| 202 |
fAA.setPath(devPath, this->getBounds(), doAA); |
| 203 |
} |
| 204 |
return this->updateCacheAndReturnNonEmpty(); |
| 205 |
} else { |
| 206 |
return this->op(SkRasterClip(devPath, this->getBounds(), doAA), op); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
bool SkRasterClip::op(sk_sp<SkShader> sh) { |
| 211 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 212 |
|
| 213 |
if (!fShader) { |
| 214 |
fShader = sh; |
| 215 |
} else { |
| 216 |
fShader = SkShaders::Blend(SkBlendMode::kSrcIn, sh, fShader); |
| 217 |
} |
| 218 |
return !this->isEmpty(); |
| 219 |
} |
| 220 |
|
| 221 |
bool SkRasterClip::op(const SkRasterClip& clip, SkClipOp op) { |
| 222 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 223 |
clip.validate(); |
| 224 |
|
| 225 |
if (this->isBW() && clip.isBW()) { |
| 226 |
(void)fBW.op(clip.fBW, (SkRegion::Op) op); |
| 227 |
} else { |
| 228 |
SkAAClip tmp; |
| 229 |
const SkAAClip* other; |
| 230 |
|
| 231 |
if (this->isBW()) { |
| 232 |
this->convertToAA(); |
| 233 |
} |
| 234 |
if (clip.isBW()) { |
| 235 |
tmp.setRegion(clip.bwRgn()); |
| 236 |
other = &tmp; |
| 237 |
} else { |
| 238 |
other = &clip.aaRgn(); |
| 239 |
} |
| 240 |
(void)fAA.op(*other, op); |
| 241 |
} |
| 242 |
return this->updateCacheAndReturnNonEmpty(); |
| 243 |
} |
| 244 |
|
| 245 |
void SkRasterClip::translate(int dx, int dy, SkRasterClip* dst) const { |
| 246 |
if (nullptr == dst) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 251 |
|
| 252 |
if (this->isEmpty()) { |
| 253 |
dst->setEmpty(); |
| 254 |
return; |
| 255 |
} |
| 256 |
if (0 == (dx | dy)) { |
| 257 |
*dst = *this; |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
dst->fIsBW = fIsBW; |
| 262 |
if (fIsBW) { |
| 263 |
fBW.translate(dx, dy, &dst->fBW); |
| 264 |
dst->fAA.setEmpty(); |
| 265 |
} else { |
| 266 |
fAA.translate(dx, dy, &dst->fAA); |
| 267 |
dst->fBW.setEmpty(); |
| 268 |
} |
| 269 |
dst->updateCacheAndReturnNonEmpty(); |
| 270 |
} |
| 271 |
|
| 272 |
void SkRasterClip::convertToAA() { |
| 273 |
AUTO_RASTERCLIP_VALIDATE(*this); |
| 274 |
|
| 275 |
SkASSERT(fIsBW); |
| 276 |
fAA.setRegion(fBW); |
| 277 |
fIsBW = false; |
| 278 |
|
| 279 |
// since we are being explicitly asked to convert-to-aa, we pass false so we don't "optimize" |
| 280 |
// ourselves back to BW. |
| 281 |
(void)this->updateCacheAndReturnNonEmpty(false); |
| 282 |
} |
| 283 |
|
| 284 |
#ifdef SK_DEBUG |
| 285 |
void SkRasterClip::validate() const { |
| 286 |
// can't ever assert that fBW is empty, since we may have called forceGetBW |
| 287 |
if (fIsBW) { |
| 288 |
SkASSERT(fAA.isEmpty()); |
| 289 |
} |
| 290 |
|
| 291 |
SkRegionPriv::Validate(fBW); |
| 292 |
fAA.validate(); |
| 293 |
|
| 294 |
SkASSERT(this->computeIsEmpty() == fIsEmpty); |
| 295 |
SkASSERT(this->computeIsRect() == fIsRect); |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
/////////////////////////////////////////////////////////////////////////////// |
| 300 |
|
| 301 |
SkAAClipBlitterWrapper::SkAAClipBlitterWrapper() { |
| 302 |
SkDEBUGCODE(fClipRgn = nullptr;) |
| 303 |
SkDEBUGCODE(fBlitter = nullptr;) |
| 304 |
} |
| 305 |
|
| 306 |
SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkRasterClip& clip, |
| 307 |
SkBlitter* blitter) { |
| 308 |
this->init(clip, blitter); |
| 309 |
} |
| 310 |
|
| 311 |
SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkAAClip* aaclip, |
| 312 |
SkBlitter* blitter) { |
| 313 |
SkASSERT(blitter); |
| 314 |
SkASSERT(aaclip); |
| 315 |
fBWRgn.setRect(aaclip->getBounds()); |
| 316 |
fAABlitter.init(blitter, aaclip); |
| 317 |
// now our return values |
| 318 |
fClipRgn = &fBWRgn; |
| 319 |
fBlitter = &fAABlitter; |
| 320 |
} |
| 321 |
|
| 322 |
void SkAAClipBlitterWrapper::init(const SkRasterClip& clip, SkBlitter* blitter) { |
| 323 |
SkASSERT(blitter); |
| 324 |
if (clip.isBW()) { |
| 325 |
fClipRgn = &clip.bwRgn(); |
| 326 |
fBlitter = blitter; |
| 327 |
} else { |
| 328 |
const SkAAClip& aaclip = clip.aaRgn(); |
| 329 |
fBWRgn.setRect(aaclip.getBounds()); |
| 330 |
fAABlitter.init(blitter, &aaclip); |
| 331 |
// now our return values |
| 332 |
fClipRgn = &fBWRgn; |
| 333 |
fBlitter = &fAABlitter; |
| 334 |
} |
| 335 |
} |