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