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