| 1 | /* |
| 2 | * Copyright 2013 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 "include/core/SkImageFilter.h" |
| 9 | #include "include/core/SkMatrix.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkPixmap.h" |
| 13 | #include "include/core/SkRasterHandleAllocator.h" |
| 14 | #include "include/core/SkShader.h" |
| 15 | #include "include/core/SkSurface.h" |
| 16 | #include "include/core/SkVertices.h" |
| 17 | #include "src/core/SkBitmapDevice.h" |
| 18 | #include "src/core/SkDraw.h" |
| 19 | #include "src/core/SkGlyphRun.h" |
| 20 | #include "src/core/SkImageFilterCache.h" |
| 21 | #include "src/core/SkImageFilter_Base.h" |
| 22 | #include "src/core/SkRasterClip.h" |
| 23 | #include "src/core/SkSpecialImage.h" |
| 24 | #include "src/core/SkStrikeCache.h" |
| 25 | #include "src/core/SkTLazy.h" |
| 26 | #include "src/image/SkImage_Base.h" |
| 27 | |
| 28 | struct Bounder { |
| 29 | SkRect fBounds; |
| 30 | bool fHasBounds; |
| 31 | |
| 32 | Bounder(const SkRect& r, const SkPaint& paint) { |
| 33 | if ((fHasBounds = paint.canComputeFastBounds())) { |
| 34 | fBounds = paint.computeFastBounds(r, &fBounds); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bool hasBounds() const { return fHasBounds; } |
| 39 | const SkRect* bounds() const { return fHasBounds ? &fBounds : nullptr; } |
| 40 | operator const SkRect* () const { return this->bounds(); } |
| 41 | }; |
| 42 | |
| 43 | class SkDrawTiler { |
| 44 | enum { |
| 45 | // 8K is 1 too big, since 8K << supersample == 32768 which is too big for SkFixed |
| 46 | kMaxDim = 8192 - 1 |
| 47 | }; |
| 48 | |
| 49 | SkBitmapDevice* fDevice; |
| 50 | SkPixmap fRootPixmap; |
| 51 | SkIRect fSrcBounds; |
| 52 | |
| 53 | // Used for tiling and non-tiling |
| 54 | SkDraw fDraw; |
| 55 | |
| 56 | // fCurr... are only used if fNeedTiling |
| 57 | SkTLazy<SkPostTranslateMatrixProvider> fTileMatrixProvider; |
| 58 | SkRasterClip fTileRC; |
| 59 | SkIPoint fOrigin; |
| 60 | |
| 61 | bool fDone, fNeedsTiling; |
| 62 | |
| 63 | public: |
| 64 | static bool NeedsTiling(SkBitmapDevice* dev) { |
| 65 | return dev->width() > kMaxDim || dev->height() > kMaxDim; |
| 66 | } |
| 67 | |
| 68 | SkDrawTiler(SkBitmapDevice* dev, const SkRect* bounds) : fDevice(dev) { |
| 69 | fDone = false; |
| 70 | |
| 71 | // we need fDst to be set, and if we're actually drawing, to dirty the genID |
| 72 | if (!dev->accessPixels(&fRootPixmap)) { |
| 73 | // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels |
| 74 | fRootPixmap.reset(dev->imageInfo(), nullptr, 0); |
| 75 | } |
| 76 | |
| 77 | // do a quick check, so we don't even have to process "bounds" if there is no need |
| 78 | const SkIRect clipR = dev->fRCStack.rc().getBounds(); |
| 79 | fNeedsTiling = clipR.right() > kMaxDim || clipR.bottom() > kMaxDim; |
| 80 | if (fNeedsTiling) { |
| 81 | if (bounds) { |
| 82 | // Make sure we round first, and then intersect. We can't rely on promoting the |
| 83 | // clipR to floats (and then intersecting with devBounds) since promoting |
| 84 | // int --> float can make the float larger than the int. |
| 85 | // rounding(out) first runs the risk of clamping if the float is larger an intmax |
| 86 | // but our roundOut() is saturating, which is fine for this use case |
| 87 | // |
| 88 | // e.g. the older version of this code did this: |
| 89 | // devBounds = mapRect(bounds); |
| 90 | // if (devBounds.intersect(SkRect::Make(clipR))) { |
| 91 | // fSrcBounds = devBounds.roundOut(); |
| 92 | // The problem being that the promotion of clipR to SkRect was unreliable |
| 93 | // |
| 94 | fSrcBounds = dev->localToDevice().mapRect(*bounds).roundOut(); |
| 95 | if (fSrcBounds.intersect(clipR)) { |
| 96 | // Check again, now that we have computed srcbounds. |
| 97 | fNeedsTiling = fSrcBounds.right() > kMaxDim || fSrcBounds.bottom() > kMaxDim; |
| 98 | } else { |
| 99 | fNeedsTiling = false; |
| 100 | fDone = true; |
| 101 | } |
| 102 | } else { |
| 103 | fSrcBounds = clipR; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (fNeedsTiling) { |
| 108 | // fDraw.fDst and fMatrixProvider are reset each time in setupTileDraw() |
| 109 | fDraw.fRC = &fTileRC; |
| 110 | // we'll step/increase it before using it |
| 111 | fOrigin.set(fSrcBounds.fLeft - kMaxDim, fSrcBounds.fTop); |
| 112 | } else { |
| 113 | // don't reference fSrcBounds, as it may not have been set |
| 114 | fDraw.fDst = fRootPixmap; |
| 115 | fDraw.fMatrixProvider = dev; |
| 116 | fDraw.fRC = &dev->fRCStack.rc(); |
| 117 | fOrigin.set(0, 0); |
| 118 | |
| 119 | fDraw.fCoverage = dev->accessCoverage(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | bool needsTiling() const { return fNeedsTiling; } |
| 124 | |
| 125 | const SkDraw* next() { |
| 126 | if (fDone) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | if (fNeedsTiling) { |
| 130 | do { |
| 131 | this->stepAndSetupTileDraw(); // might set the clip to empty and fDone to true |
| 132 | } while (!fDone && fTileRC.isEmpty()); |
| 133 | // if we exit the loop and we're still empty, we're (past) done |
| 134 | if (fTileRC.isEmpty()) { |
| 135 | SkASSERT(fDone); |
| 136 | return nullptr; |
| 137 | } |
| 138 | SkASSERT(!fTileRC.isEmpty()); |
| 139 | } else { |
| 140 | fDone = true; // only draw untiled once |
| 141 | } |
| 142 | return &fDraw; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | void stepAndSetupTileDraw() { |
| 147 | SkASSERT(!fDone); |
| 148 | SkASSERT(fNeedsTiling); |
| 149 | |
| 150 | // We do fRootPixmap.width() - kMaxDim instead of fOrigin.fX + kMaxDim to avoid overflow. |
| 151 | if (fOrigin.fX >= fSrcBounds.fRight - kMaxDim) { // too far |
| 152 | fOrigin.fX = fSrcBounds.fLeft; |
| 153 | fOrigin.fY += kMaxDim; |
| 154 | } else { |
| 155 | fOrigin.fX += kMaxDim; |
| 156 | } |
| 157 | // fDone = next origin will be invalid. |
| 158 | fDone = fOrigin.fX >= fSrcBounds.fRight - kMaxDim && |
| 159 | fOrigin.fY >= fSrcBounds.fBottom - kMaxDim; |
| 160 | |
| 161 | SkIRect bounds = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), kMaxDim, kMaxDim); |
| 162 | SkASSERT(!bounds.isEmpty()); |
| 163 | bool success = fRootPixmap.extractSubset(&fDraw.fDst, bounds); |
| 164 | SkASSERT_RELEASE(success); |
| 165 | // now don't use bounds, since fDst has the clipped dimensions. |
| 166 | |
| 167 | fDraw.fMatrixProvider = fTileMatrixProvider.init(fDevice->asMatrixProvider(), |
| 168 | SkIntToScalar(-fOrigin.x()), |
| 169 | SkIntToScalar(-fOrigin.y())); |
| 170 | fDevice->fRCStack.rc().translate(-fOrigin.x(), -fOrigin.y(), &fTileRC); |
| 171 | fTileRC.op(SkIRect::MakeWH(fDraw.fDst.width(), fDraw.fDst.height()), |
| 172 | SkRegion::kIntersect_Op); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | // Passing a bounds allows the tiler to only visit the dst-tiles that might intersect the |
| 177 | // drawing. If null is passed, the tiler has to visit everywhere. The bounds is expected to be |
| 178 | // in local coordinates, as the tiler itself will transform that into device coordinates. |
| 179 | // |
| 180 | #define LOOP_TILER(code, boundsPtr) \ |
| 181 | SkDrawTiler priv_tiler(this, boundsPtr); \ |
| 182 | while (const SkDraw* priv_draw = priv_tiler.next()) { \ |
| 183 | priv_draw->code; \ |
| 184 | } |
| 185 | |
| 186 | // Helper to create an SkDraw from a device |
| 187 | class SkBitmapDevice::BDDraw : public SkDraw { |
| 188 | public: |
| 189 | BDDraw(SkBitmapDevice* dev) { |
| 190 | // we need fDst to be set, and if we're actually drawing, to dirty the genID |
| 191 | if (!dev->accessPixels(&fDst)) { |
| 192 | // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels |
| 193 | fDst.reset(dev->imageInfo(), nullptr, 0); |
| 194 | } |
| 195 | fMatrixProvider = dev; |
| 196 | fRC = &dev->fRCStack.rc(); |
| 197 | fCoverage = dev->accessCoverage(); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | static bool valid_for_bitmap_device(const SkImageInfo& info, |
| 202 | SkAlphaType* newAlphaType) { |
| 203 | if (info.width() < 0 || info.height() < 0 || kUnknown_SkColorType == info.colorType()) { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | if (newAlphaType) { |
| 208 | *newAlphaType = SkColorTypeIsAlwaysOpaque(info.colorType()) ? kOpaque_SkAlphaType |
| 209 | : info.alphaType(); |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap) |
| 216 | : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)) |
| 217 | , fBitmap(bitmap) |
| 218 | , fRCStack(bitmap.width(), bitmap.height()) |
| 219 | , fGlyphPainter(this->surfaceProps(), |
| 220 | bitmap.colorType(), |
| 221 | bitmap.colorSpace(), |
| 222 | SkStrikeCache::GlobalStrikeCache()) { |
| 223 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
| 224 | } |
| 225 | |
| 226 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) { |
| 227 | return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
| 228 | } |
| 229 | |
| 230 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps, |
| 231 | SkRasterHandleAllocator::Handle hndl, const SkBitmap* coverage) |
| 232 | : INHERITED(bitmap.info(), surfaceProps) |
| 233 | , fBitmap(bitmap) |
| 234 | , fRasterHandle(hndl) |
| 235 | , fRCStack(bitmap.width(), bitmap.height()) |
| 236 | , fGlyphPainter(this->surfaceProps(), |
| 237 | bitmap.colorType(), |
| 238 | bitmap.colorSpace(), |
| 239 | SkStrikeCache::GlobalStrikeCache()) { |
| 240 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
| 241 | |
| 242 | if (coverage) { |
| 243 | SkASSERT(coverage->width() == bitmap.width()); |
| 244 | SkASSERT(coverage->height() == bitmap.height()); |
| 245 | fCoverage = std::make_unique<SkBitmap>(*coverage); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, |
| 250 | const SkSurfaceProps& surfaceProps, |
| 251 | bool trackCoverage, |
| 252 | SkRasterHandleAllocator* allocator) { |
| 253 | SkAlphaType newAT = origInfo.alphaType(); |
| 254 | if (!valid_for_bitmap_device(origInfo, &newAT)) { |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | SkRasterHandleAllocator::Handle hndl = nullptr; |
| 259 | const SkImageInfo info = origInfo.makeAlphaType(newAT); |
| 260 | SkBitmap bitmap; |
| 261 | |
| 262 | if (kUnknown_SkColorType == info.colorType()) { |
| 263 | if (!bitmap.setInfo(info)) { |
| 264 | return nullptr; |
| 265 | } |
| 266 | } else if (allocator) { |
| 267 | hndl = allocator->allocBitmap(info, &bitmap); |
| 268 | if (!hndl) { |
| 269 | return nullptr; |
| 270 | } |
| 271 | } else if (info.isOpaque()) { |
| 272 | // If this bitmap is opaque, we don't have any sensible default color, |
| 273 | // so we just return uninitialized pixels. |
| 274 | if (!bitmap.tryAllocPixels(info)) { |
| 275 | return nullptr; |
| 276 | } |
| 277 | } else { |
| 278 | // This bitmap has transparency, so we'll zero the pixels (to transparent). |
| 279 | // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). |
| 280 | if (!bitmap.tryAllocPixelsFlags(info, SkBitmap::kZeroPixels_AllocFlag)) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | SkBitmap coverage; |
| 286 | if (trackCoverage) { |
| 287 | SkImageInfo ci = |
| 288 | SkImageInfo::Make(info.dimensions(), kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 289 | if (!coverage.tryAllocPixelsFlags(ci, SkBitmap::kZeroPixels_AllocFlag)) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return new SkBitmapDevice(bitmap, surfaceProps, hndl, trackCoverage ? &coverage : nullptr); |
| 295 | } |
| 296 | |
| 297 | void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) { |
| 298 | SkASSERT(bm.width() == fBitmap.width()); |
| 299 | SkASSERT(bm.height() == fBitmap.height()); |
| 300 | fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config) |
| 301 | this->privateResize(fBitmap.info().width(), fBitmap.info().height()); |
| 302 | } |
| 303 | |
| 304 | SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { |
| 305 | const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
| 306 | |
| 307 | // Need to force L32 for now if we have an image filter. |
| 308 | // If filters ever support other colortypes, e.g. F16, we can modify this check. |
| 309 | SkImageInfo info = cinfo.fInfo; |
| 310 | if (layerPaint && layerPaint->getImageFilter()) { |
| 311 | // TODO: can we query the imagefilter, to see if it can handle floats (so we don't always |
| 312 | // use N32 when the layer itself was float)? |
| 313 | info = info.makeColorType(kN32_SkColorType); |
| 314 | } |
| 315 | |
| 316 | return SkBitmapDevice::Create(info, surfaceProps, cinfo.fTrackCoverage, cinfo.fAllocator); |
| 317 | } |
| 318 | |
| 319 | bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) { |
| 320 | if (this->onPeekPixels(pmap)) { |
| 321 | fBitmap.notifyPixelsChanged(); |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) { |
| 328 | const SkImageInfo info = fBitmap.info(); |
| 329 | if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) { |
| 330 | pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes()); |
| 331 | return true; |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) { |
| 337 | // since we don't stop creating un-pixeled devices yet, check for no pixels here |
| 338 | if (nullptr == fBitmap.getPixels()) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (fBitmap.writePixels(pm, x, y)) { |
| 343 | fBitmap.notifyPixelsChanged(); |
| 344 | return true; |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) { |
| 350 | return fBitmap.readPixels(pm, x, y); |
| 351 | } |
| 352 | |
| 353 | /////////////////////////////////////////////////////////////////////////////// |
| 354 | |
| 355 | void SkBitmapDevice::drawPaint(const SkPaint& paint) { |
| 356 | BDDraw(this).drawPaint(paint); |
| 357 | } |
| 358 | |
| 359 | void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count, |
| 360 | const SkPoint pts[], const SkPaint& paint) { |
| 361 | LOOP_TILER( drawPoints(mode, count, pts, paint, nullptr), nullptr) |
| 362 | } |
| 363 | |
| 364 | void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) { |
| 365 | LOOP_TILER( drawRect(r, paint), Bounder(r, paint)) |
| 366 | } |
| 367 | |
| 368 | void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 369 | SkPath path; |
| 370 | path.addOval(oval); |
| 371 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 372 | // required to override drawOval. |
| 373 | this->drawPath(path, paint, true); |
| 374 | } |
| 375 | |
| 376 | void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| 377 | #ifdef SK_IGNORE_BLURRED_RRECT_OPT |
| 378 | SkPath path; |
| 379 | |
| 380 | path.addRRect(rrect); |
| 381 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 382 | // required to override drawRRect. |
| 383 | this->drawPath(path, paint, true); |
| 384 | #else |
| 385 | LOOP_TILER( drawRRect(rrect, paint), Bounder(rrect.getBounds(), paint)) |
| 386 | #endif |
| 387 | } |
| 388 | |
| 389 | void SkBitmapDevice::drawPath(const SkPath& path, |
| 390 | const SkPaint& paint, |
| 391 | bool pathIsMutable) { |
| 392 | const SkRect* bounds = nullptr; |
| 393 | if (SkDrawTiler::NeedsTiling(this) && !path.isInverseFillType()) { |
| 394 | bounds = &path.getBounds(); |
| 395 | } |
| 396 | SkDrawTiler tiler(this, bounds ? Bounder(*bounds, paint).bounds() : nullptr); |
| 397 | if (tiler.needsTiling()) { |
| 398 | pathIsMutable = false; |
| 399 | } |
| 400 | while (const SkDraw* draw = tiler.next()) { |
| 401 | draw->drawPath(path, paint, nullptr, pathIsMutable); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 406 | const SkRect* dstOrNull, const SkPaint& paint) { |
| 407 | const SkRect* bounds = dstOrNull; |
| 408 | SkRect storage; |
| 409 | if (!bounds && SkDrawTiler::NeedsTiling(this)) { |
| 410 | matrix.mapRect(&storage, SkRect::MakeIWH(bitmap.width(), bitmap.height())); |
| 411 | Bounder b(storage, paint); |
| 412 | if (b.hasBounds()) { |
| 413 | storage = *b.bounds(); |
| 414 | bounds = &storage; |
| 415 | } |
| 416 | } |
| 417 | LOOP_TILER(drawBitmap(bitmap, matrix, dstOrNull, paint), bounds) |
| 418 | } |
| 419 | |
| 420 | static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) { |
| 421 | if (!paint.getMaskFilter()) { |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | // Some mask filters parameters (sigma) depend on the CTM/scale. |
| 426 | return m.getType() <= SkMatrix::kTranslate_Mask; |
| 427 | } |
| 428 | |
| 429 | void SkBitmapDevice::drawImageRect(const SkImage* image, |
| 430 | const SkRect* src, const SkRect& dst, |
| 431 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
| 432 | SkASSERT(dst.isFinite()); |
| 433 | SkASSERT(dst.isSorted()); |
| 434 | |
| 435 | SkBitmap bitmap; |
| 436 | if (!as_IB(image)->getROPixels(&bitmap)) { |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | SkMatrix matrix; |
| 441 | SkRect bitmapBounds, tmpSrc, tmpDst; |
| 442 | SkBitmap tmpBitmap; |
| 443 | |
| 444 | bitmapBounds.setIWH(bitmap.width(), bitmap.height()); |
| 445 | |
| 446 | // Compute matrix from the two rectangles |
| 447 | if (src) { |
| 448 | tmpSrc = *src; |
| 449 | } else { |
| 450 | tmpSrc = bitmapBounds; |
| 451 | } |
| 452 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 453 | |
| 454 | LogDrawScaleFactor(this->localToDevice(), matrix, paint.getFilterQuality()); |
| 455 | |
| 456 | const SkRect* dstPtr = &dst; |
| 457 | const SkBitmap* bitmapPtr = &bitmap; |
| 458 | |
| 459 | // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if |
| 460 | // needed (if the src was clipped). No check needed if src==null. |
| 461 | if (src) { |
| 462 | if (!bitmapBounds.contains(*src)) { |
| 463 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 464 | return; // nothing to draw |
| 465 | } |
| 466 | // recompute dst, based on the smaller tmpSrc |
| 467 | matrix.mapRect(&tmpDst, tmpSrc); |
| 468 | if (!tmpDst.isFinite()) { |
| 469 | return; |
| 470 | } |
| 471 | dstPtr = &tmpDst; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (src && !src->contains(bitmapBounds) && |
| 476 | SkCanvas::kFast_SrcRectConstraint == constraint && |
| 477 | paint.getFilterQuality() != kNone_SkFilterQuality) { |
| 478 | // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know |
| 479 | // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap, |
| 480 | // but we must use a shader w/ dst bounds (which can access all of the bitmap needed). |
| 481 | goto USE_SHADER; |
| 482 | } |
| 483 | |
| 484 | if (src) { |
| 485 | // since we may need to clamp to the borders of the src rect within |
| 486 | // the bitmap, we extract a subset. |
| 487 | const SkIRect srcIR = tmpSrc.roundOut(); |
| 488 | if (!bitmap.extractSubset(&tmpBitmap, srcIR)) { |
| 489 | return; |
| 490 | } |
| 491 | bitmapPtr = &tmpBitmap; |
| 492 | |
| 493 | // Since we did an extract, we need to adjust the matrix accordingly |
| 494 | SkScalar dx = 0, dy = 0; |
| 495 | if (srcIR.fLeft > 0) { |
| 496 | dx = SkIntToScalar(srcIR.fLeft); |
| 497 | } |
| 498 | if (srcIR.fTop > 0) { |
| 499 | dy = SkIntToScalar(srcIR.fTop); |
| 500 | } |
| 501 | if (dx || dy) { |
| 502 | matrix.preTranslate(dx, dy); |
| 503 | } |
| 504 | |
| 505 | #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET |
| 506 | SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy, |
| 507 | SkIntToScalar(bitmapPtr->width()), |
| 508 | SkIntToScalar(bitmapPtr->height())); |
| 509 | #else |
| 510 | SkRect ; |
| 511 | extractedBitmapBounds.setIWH(bitmapPtr->width(), bitmapPtr->height()); |
| 512 | #endif |
| 513 | if (extractedBitmapBounds == tmpSrc) { |
| 514 | // no fractional part in src, we can just call drawBitmap |
| 515 | goto USE_DRAWBITMAP; |
| 516 | } |
| 517 | } else { |
| 518 | USE_DRAWBITMAP: |
| 519 | // We can go faster by just calling drawBitmap, which will concat the |
| 520 | // matrix with the CTM, and try to call drawSprite if it can. If not, |
| 521 | // it will make a shader and call drawRect, as we do below. |
| 522 | if (CanApplyDstMatrixAsCTM(matrix, paint)) { |
| 523 | this->drawBitmap(*bitmapPtr, matrix, dstPtr, paint); |
| 524 | return; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | USE_SHADER: |
| 529 | |
| 530 | // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps. |
| 531 | // Since the shader need only live for our stack-frame, pass in a custom allocator. This |
| 532 | // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap |
| 533 | // if its mutable, since that precaution is not needed (give the short lifetime of the shader). |
| 534 | |
| 535 | // construct a shader, so we can call drawRect with the dst |
| 536 | auto s = SkMakeBitmapShaderForPaint(paint, *bitmapPtr, SkTileMode::kClamp, |
| 537 | SkTileMode::kClamp, &matrix, kNever_SkCopyPixelsMode); |
| 538 | if (!s) { |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | SkPaint paintWithShader(paint); |
| 543 | paintWithShader.setStyle(SkPaint::kFill_Style); |
| 544 | paintWithShader.setShader(std::move(s)); |
| 545 | |
| 546 | // Call ourself, in case the subclass wanted to share this setup code |
| 547 | // but handle the drawRect code themselves. |
| 548 | this->drawRect(*dstPtr, paintWithShader); |
| 549 | } |
| 550 | |
| 551 | void SkBitmapDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) { |
| 552 | LOOP_TILER( drawGlyphRunList(glyphRunList, &fGlyphPainter), nullptr ) |
| 553 | } |
| 554 | |
| 555 | void SkBitmapDevice::drawVertices(const SkVertices* vertices, SkBlendMode bmode, |
| 556 | const SkPaint& paint) { |
| 557 | BDDraw(this).drawVertices(vertices, bmode, paint); |
| 558 | } |
| 559 | |
| 560 | void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& origPaint) { |
| 561 | SkASSERT(!origPaint.getImageFilter()); |
| 562 | SkASSERT(!origPaint.getMaskFilter()); |
| 563 | |
| 564 | // todo: can we unify with similar adjustment in SkGpuDevice? |
| 565 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 566 | |
| 567 | // hack to test coverage |
| 568 | SkBitmapDevice* src = static_cast<SkBitmapDevice*>(device); |
| 569 | if (src->fCoverage) { |
| 570 | SkDraw draw; |
| 571 | SkSimpleMatrixProvider matrixProvider(SkMatrix::I()); |
| 572 | draw.fDst = fBitmap.pixmap(); |
| 573 | draw.fMatrixProvider = &matrixProvider; |
| 574 | draw.fRC = &fRCStack.rc(); |
| 575 | paint.writable()->setShader(src->fBitmap.makeShader()); |
| 576 | draw.drawBitmap(*src->fCoverage.get(), |
| 577 | SkMatrix::Translate(SkIntToScalar(x),SkIntToScalar(y)), nullptr, *paint); |
| 578 | } else { |
| 579 | BDDraw(this).drawSprite(src->fBitmap, x, y, *paint); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void SkBitmapDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[], |
| 584 | const SkRect tex[], const SkColor colors[], int count, |
| 585 | SkBlendMode mode, const SkPaint& paint) { |
| 586 | // set this to true for performance comparisons with the old drawVertices way |
| 587 | if (false) { |
| 588 | this->INHERITED::drawAtlas(atlas, xform, tex, colors, count, mode, paint); |
| 589 | return; |
| 590 | } |
| 591 | BDDraw(this).drawAtlas(atlas, xform, tex, colors, count, mode, paint); |
| 592 | } |
| 593 | |
| 594 | /////////////////////////////////////////////////////////////////////////////// |
| 595 | |
| 596 | void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint) { |
| 597 | SkASSERT(!src->isTextureBacked()); |
| 598 | SkASSERT(!origPaint.getMaskFilter()); |
| 599 | |
| 600 | sk_sp<SkSpecialImage> filteredImage; |
| 601 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 602 | |
| 603 | if (SkImageFilter* filter = paint->getImageFilter()) { |
| 604 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 605 | const SkMatrix matrix = SkMatrix::Concat( |
| 606 | SkMatrix::Translate(SkIntToScalar(-x), SkIntToScalar(-y)), this->localToDevice()); |
| 607 | const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y); |
| 608 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
| 609 | SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), fBitmap.colorType(), |
| 610 | fBitmap.colorSpace(), src); |
| 611 | |
| 612 | filteredImage = as_IFB(filter)->filterImage(ctx).imageAndOffset(&offset); |
| 613 | if (!filteredImage) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | src = filteredImage.get(); |
| 618 | paint.writable()->setImageFilter(nullptr); |
| 619 | x += offset.x(); |
| 620 | y += offset.y(); |
| 621 | } |
| 622 | |
| 623 | SkBitmap resultBM; |
| 624 | if (src->getROPixels(&resultBM)) { |
| 625 | BDDraw(this).drawSprite(resultBM, x, y, *paint); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) { |
| 630 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 631 | } |
| 632 | |
| 633 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) { |
| 634 | return SkSpecialImage::MakeFromImage(nullptr, SkIRect::MakeWH(image->width(), image->height()), |
| 635 | image->makeNonTextureImage()); |
| 636 | } |
| 637 | |
| 638 | sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial(const SkIRect& bounds, bool forceCopy) { |
| 639 | if (forceCopy) { |
| 640 | return SkSpecialImage::CopyFromRaster(bounds, fBitmap, &this->surfaceProps()); |
| 641 | } else { |
| 642 | return SkSpecialImage::MakeFromRaster(bounds, fBitmap); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /////////////////////////////////////////////////////////////////////////////// |
| 647 | |
| 648 | sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 649 | return SkSurface::MakeRaster(info, &props); |
| 650 | } |
| 651 | |
| 652 | SkImageFilterCache* SkBitmapDevice::getImageFilterCache() { |
| 653 | SkImageFilterCache* cache = SkImageFilterCache::Get(); |
| 654 | cache->ref(); |
| 655 | return cache; |
| 656 | } |
| 657 | |
| 658 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 659 | |
| 660 | void SkBitmapDevice::onSave() { |
| 661 | fRCStack.save(); |
| 662 | } |
| 663 | |
| 664 | void SkBitmapDevice::onRestore() { |
| 665 | fRCStack.restore(); |
| 666 | } |
| 667 | |
| 668 | void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) { |
| 669 | fRCStack.clipRect(this->localToDevice(), rect, op, aa); |
| 670 | } |
| 671 | |
| 672 | void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) { |
| 673 | fRCStack.clipRRect(this->localToDevice(), rrect, op, aa); |
| 674 | } |
| 675 | |
| 676 | void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) { |
| 677 | fRCStack.clipPath(this->localToDevice(), path, op, aa); |
| 678 | } |
| 679 | |
| 680 | void SkBitmapDevice::onClipShader(sk_sp<SkShader> sh) { |
| 681 | fRCStack.clipShader(std::move(sh)); |
| 682 | } |
| 683 | |
| 684 | void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
| 685 | SkIPoint origin = this->getOrigin(); |
| 686 | SkRegion tmp; |
| 687 | const SkRegion* ptr = &rgn; |
| 688 | if (origin.fX | origin.fY) { |
| 689 | // translate from "global/canvas" coordinates to relative to this device |
| 690 | rgn.translate(-origin.fX, -origin.fY, &tmp); |
| 691 | ptr = &tmp; |
| 692 | } |
| 693 | fRCStack.clipRegion(*ptr, op); |
| 694 | } |
| 695 | |
| 696 | void SkBitmapDevice::onReplaceClip(const SkIRect& rect) { |
| 697 | // Transform from "global/canvas" coordinates to relative to this device |
| 698 | SkIRect deviceRect = this->globalToDevice().mapRect(SkRect::Make(rect)).round(); |
| 699 | fRCStack.replaceClip(deviceRect); |
| 700 | } |
| 701 | |
| 702 | void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) { |
| 703 | fRCStack.setDeviceClipRestriction(mutableClipRestriction); |
| 704 | if (!mutableClipRestriction->isEmpty()) { |
| 705 | SkRegion rgn(*mutableClipRestriction); |
| 706 | fRCStack.clipRegion(rgn, SkClipOp::kIntersect); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | bool SkBitmapDevice::onClipIsWideOpen() const { |
| 711 | const SkRasterClip& rc = fRCStack.rc(); |
| 712 | // If we're AA, we can't be wide-open (we would represent that as BW) |
| 713 | return rc.isBW() && rc.bwRgn().isRect() && |
| 714 | rc.bwRgn().getBounds() == SkIRect{0, 0, this->width(), this->height()}; |
| 715 | } |
| 716 | |
| 717 | bool SkBitmapDevice::onClipIsAA() const { |
| 718 | const SkRasterClip& rc = fRCStack.rc(); |
| 719 | return !rc.isEmpty() && rc.isAA(); |
| 720 | } |
| 721 | |
| 722 | void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const { |
| 723 | const SkRasterClip& rc = fRCStack.rc(); |
| 724 | if (rc.isAA()) { |
| 725 | rgn->setRect(rc.getBounds()); |
| 726 | } else { |
| 727 | *rgn = rc.bwRgn(); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) { |
| 732 | #ifdef SK_DEBUG |
| 733 | const SkIRect& stackBounds = fRCStack.rc().getBounds(); |
| 734 | SkASSERT(drawClipBounds == stackBounds); |
| 735 | #endif |
| 736 | } |
| 737 | |
| 738 | SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const { |
| 739 | const SkRasterClip& rc = fRCStack.rc(); |
| 740 | if (rc.isEmpty()) { |
| 741 | return ClipType::kEmpty; |
| 742 | } else if (rc.isRect()) { |
| 743 | return ClipType::kRect; |
| 744 | } else { |
| 745 | return ClipType::kComplex; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | SkIRect SkBitmapDevice::onDevClipBounds() const { |
| 750 | return fRCStack.rc().getBounds(); |
| 751 | } |
| 752 | |