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 this->drawPath(SkPath::Oval(oval), paint, true);
357 }
358
drawRRect(const SkRRect & rrect,const SkPaint & paint)359 void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
360 LOOP_TILER( drawRRect(rrect, paint), Bounder(rrect.getBounds(), paint))
361 }
362
drawPath(const SkPath & path,const SkPaint & paint,bool pathIsMutable)363 void SkBitmapDevice::drawPath(const SkPath& path,
364 const SkPaint& paint,
365 bool pathIsMutable) {
366 const SkRect* bounds = nullptr;
367 if (SkDrawTiler::NeedsTiling(this) && !path.isInverseFillType()) {
368 bounds = &path.getBounds();
369 }
370 SkDrawTiler tiler(this, bounds ? Bounder(*bounds, paint).bounds() : nullptr);
371 if (tiler.needsTiling()) {
372 pathIsMutable = false;
373 }
374 while (const SkDraw* draw = tiler.next()) {
375 draw->drawPath(path, paint, nullptr, pathIsMutable);
376 }
377 }
378
drawBitmap(const SkBitmap & bitmap,const SkMatrix & matrix,const SkRect * dstOrNull,const SkSamplingOptions & sampling,const SkPaint & paint)379 void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
380 const SkRect* dstOrNull, const SkSamplingOptions& sampling,
381 const SkPaint& paint) {
382 const SkRect* bounds = dstOrNull;
383 SkRect storage;
384 if (!bounds && SkDrawTiler::NeedsTiling(this)) {
385 matrix.mapRect(&storage, SkRect::MakeIWH(bitmap.width(), bitmap.height()));
386 Bounder b(storage, paint);
387 if (b.hasBounds()) {
388 storage = *b.bounds();
389 bounds = &storage;
390 }
391 }
392 LOOP_TILER(drawBitmap(bitmap, matrix, dstOrNull, sampling, paint), bounds)
393 }
394
CanApplyDstMatrixAsCTM(const SkMatrix & m,const SkPaint & paint)395 static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
396 if (!paint.getMaskFilter()) {
397 return true;
398 }
399
400 // Some mask filters parameters (sigma) depend on the CTM/scale.
401 return m.getType() <= SkMatrix::kTranslate_Mask;
402 }
403
drawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)404 void SkBitmapDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
405 const SkSamplingOptions& sampling, const SkPaint& paint,
406 SkCanvas::SrcRectConstraint constraint) {
407 SkASSERT(dst.isFinite());
408 SkASSERT(dst.isSorted());
409
410 SkBitmap bitmap;
411 // TODO: Elevate direct context requirement to public API and remove cheat.
412 auto dContext = as_IB(image)->directContext();
413 if (!as_IB(image)->getROPixels(dContext, &bitmap)) {
414 return;
415 }
416
417 SkRect bitmapBounds, tmpSrc, tmpDst;
418 SkBitmap tmpBitmap;
419
420 bitmapBounds.setIWH(bitmap.width(), bitmap.height());
421
422 // Compute matrix from the two rectangles
423 if (src) {
424 tmpSrc = *src;
425 } else {
426 tmpSrc = bitmapBounds;
427 }
428 SkMatrix matrix = SkMatrix::RectToRect(tmpSrc, dst);
429
430 const SkRect* dstPtr = &dst;
431 const SkBitmap* bitmapPtr = &bitmap;
432
433 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
434 // needed (if the src was clipped). No check needed if src==null.
435 bool srcIsSubset = false;
436 if (src) {
437 if (!bitmapBounds.contains(*src)) {
438 if (!tmpSrc.intersect(bitmapBounds)) {
439 return; // nothing to draw
440 }
441 // recompute dst, based on the smaller tmpSrc
442 matrix.mapRect(&tmpDst, tmpSrc);
443 if (!tmpDst.isFinite()) {
444 return;
445 }
446 dstPtr = &tmpDst;
447 }
448 srcIsSubset = !tmpSrc.contains(bitmapBounds);
449 }
450
451 if (srcIsSubset &&
452 SkCanvas::kFast_SrcRectConstraint == constraint &&
453 sampling != SkSamplingOptions()) {
454 // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know
455 // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap,
456 // but we must use a shader w/ dst bounds (which can access all of the bitmap needed).
457 goto USE_SHADER;
458 }
459
460 if (srcIsSubset) {
461 // since we may need to clamp to the borders of the src rect within
462 // the bitmap, we extract a subset.
463 const SkIRect srcIR = tmpSrc.roundOut();
464 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
465 return;
466 }
467 bitmapPtr = &tmpBitmap;
468
469 // Since we did an extract, we need to adjust the matrix accordingly
470 SkScalar dx = 0, dy = 0;
471 if (srcIR.fLeft > 0) {
472 dx = SkIntToScalar(srcIR.fLeft);
473 }
474 if (srcIR.fTop > 0) {
475 dy = SkIntToScalar(srcIR.fTop);
476 }
477 if (dx || dy) {
478 matrix.preTranslate(dx, dy);
479 }
480
481 #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
482 SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
483 SkIntToScalar(bitmapPtr->width()),
484 SkIntToScalar(bitmapPtr->height()));
485 #else
486 SkRect extractedBitmapBounds;
487 extractedBitmapBounds.setIWH(bitmapPtr->width(), bitmapPtr->height());
488 #endif
489 if (extractedBitmapBounds == tmpSrc) {
490 // no fractional part in src, we can just call drawBitmap
491 goto USE_DRAWBITMAP;
492 }
493 } else {
494 USE_DRAWBITMAP:
495 // We can go faster by just calling drawBitmap, which will concat the
496 // matrix with the CTM, and try to call drawSprite if it can. If not,
497 // it will make a shader and call drawRect, as we do below.
498 if (CanApplyDstMatrixAsCTM(matrix, paint)) {
499 this->drawBitmap(*bitmapPtr, matrix, dstPtr, sampling, paint);
500 return;
501 }
502 }
503
504 USE_SHADER:
505
506 // construct a shader, so we can call drawRect with the dst
507 auto s = SkMakeBitmapShaderForPaint(paint, *bitmapPtr, SkTileMode::kClamp, SkTileMode::kClamp,
508 sampling, &matrix, kNever_SkCopyPixelsMode);
509 if (!s) {
510 return;
511 }
512
513 SkPaint paintWithShader(paint);
514 paintWithShader.setStyle(SkPaint::kFill_Style);
515 paintWithShader.setShader(std::move(s));
516
517 // Call ourself, in case the subclass wanted to share this setup code
518 // but handle the drawRect code themselves.
519 this->drawRect(*dstPtr, paintWithShader);
520 }
521
onDrawGlyphRunList(SkCanvas * canvas,const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)522 void SkBitmapDevice::onDrawGlyphRunList(SkCanvas* canvas,
523 const sktext::GlyphRunList& glyphRunList,
524 const SkPaint& paint) {
525 SkASSERT(!glyphRunList.hasRSXForm());
526 LOOP_TILER( drawGlyphRunList(canvas, &fGlyphPainter, glyphRunList, paint), nullptr )
527 }
528
drawVertices(const SkVertices * vertices,sk_sp<SkBlender> blender,const SkPaint & paint,bool skipColorXform)529 void SkBitmapDevice::drawVertices(const SkVertices* vertices,
530 sk_sp<SkBlender> blender,
531 const SkPaint& paint,
532 bool skipColorXform) {
533 #ifdef SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER
534 if (!paint.getShader()) {
535 blender = SkBlender::Mode(SkBlendMode::kDst);
536 }
537 #endif
538 BDDraw(this).drawVertices(vertices, std::move(blender), paint, skipColorXform);
539 }
540
drawMesh(const SkMesh &,sk_sp<SkBlender>,const SkPaint &)541 void SkBitmapDevice::drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) {
542 // TODO: Implement, maybe with a subclass of BitmapDevice that has SkSL support.
543 }
544
drawAtlas(const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,sk_sp<SkBlender> blender,const SkPaint & paint)545 void SkBitmapDevice::drawAtlas(const SkRSXform xform[],
546 const SkRect tex[],
547 const SkColor colors[],
548 int count,
549 sk_sp<SkBlender> blender,
550 const SkPaint& paint) {
551 // set this to true for performance comparisons with the old drawVertices way
552 if ((false)) {
553 this->SkDevice::drawAtlas(xform, tex, colors, count, std::move(blender), paint);
554 return;
555 }
556 BDDraw(this).drawAtlas(xform, tex, colors, count, std::move(blender), paint);
557 }
558
559 ///////////////////////////////////////////////////////////////////////////////
560
drawSpecial(SkSpecialImage * src,const SkMatrix & localToDevice,const SkSamplingOptions & sampling,const SkPaint & paint,SkCanvas::SrcRectConstraint)561 void SkBitmapDevice::drawSpecial(SkSpecialImage* src,
562 const SkMatrix& localToDevice,
563 const SkSamplingOptions& sampling,
564 const SkPaint& paint,
565 SkCanvas::SrcRectConstraint) {
566 SkASSERT(!paint.getImageFilter());
567 SkASSERT(!paint.getMaskFilter());
568 SkASSERT(!src->isGaneshBacked());
569 SkASSERT(!src->isGraphiteBacked());
570
571 SkBitmap resultBM;
572 if (SkSpecialImages::AsBitmap(src, &resultBM)) {
573 SkDraw draw;
574 if (!this->accessPixels(&draw.fDst)) {
575 return; // no pixels to draw to so skip it
576 }
577 draw.fCTM = &localToDevice;
578 draw.fRC = &fRCStack.rc();
579 draw.drawBitmap(resultBM, SkMatrix::I(), nullptr, sampling, paint);
580 }
581 }
makeSpecial(const SkBitmap & bitmap)582 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
583 return SkSpecialImages::MakeFromRaster(bitmap.bounds(), bitmap, this->surfaceProps());
584 }
585
makeSpecial(const SkImage * image)586 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
587 return SkSpecialImages::MakeFromRaster(SkIRect::MakeWH(image->width(), image->height()),
588 image->makeNonTextureImage(),
589 this->surfaceProps());
590 }
591
snapSpecial(const SkIRect & bounds,bool forceCopy)592 sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial(const SkIRect& bounds, bool forceCopy) {
593 if (forceCopy) {
594 return SkSpecialImages::CopyFromRaster(bounds, fBitmap, this->surfaceProps());
595 } else {
596 return SkSpecialImages::MakeFromRaster(bounds, fBitmap, this->surfaceProps());
597 }
598 }
599
600 ///////////////////////////////////////////////////////////////////////////////
601
makeSurface(const SkImageInfo & info,const SkSurfaceProps & props)602 sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
603 return SkSurfaces::Raster(info, &props);
604 }
605
606 ///////////////////////////////////////////////////////////////////////////////////////////////////
607
pushClipStack()608 void SkBitmapDevice::pushClipStack() {
609 fRCStack.save();
610 }
611
popClipStack()612 void SkBitmapDevice::popClipStack() {
613 fRCStack.restore();
614 }
615
clipRect(const SkRect & rect,SkClipOp op,bool aa)616 void SkBitmapDevice::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
617 fRCStack.clipRect(this->localToDevice(), rect, op, aa);
618 }
619
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)620 void SkBitmapDevice::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
621 fRCStack.clipRRect(this->localToDevice(), rrect, op, aa);
622 }
623
clipPath(const SkPath & path,SkClipOp op,bool aa)624 void SkBitmapDevice::clipPath(const SkPath& path, SkClipOp op, bool aa) {
625 fRCStack.clipPath(this->localToDevice(), path, op, aa);
626 }
627
onClipShader(sk_sp<SkShader> sh)628 void SkBitmapDevice::onClipShader(sk_sp<SkShader> sh) {
629 fRCStack.clipShader(std::move(sh));
630 }
631
clipRegion(const SkRegion & rgn,SkClipOp op)632 void SkBitmapDevice::clipRegion(const SkRegion& rgn, SkClipOp op) {
633 SkIPoint origin = this->getOrigin();
634 SkRegion tmp;
635 const SkRegion* ptr = &rgn;
636 if (origin.fX | origin.fY) {
637 // translate from "global/canvas" coordinates to relative to this device
638 rgn.translate(-origin.fX, -origin.fY, &tmp);
639 ptr = &tmp;
640 }
641 fRCStack.clipRegion(*ptr, op);
642 }
643
replaceClip(const SkIRect & rect)644 void SkBitmapDevice::replaceClip(const SkIRect& rect) {
645 // Transform from "global/canvas" coordinates to relative to this device
646 SkRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect));
647 fRCStack.replaceClip(deviceRect.round());
648 }
649
isClipWideOpen() const650 bool SkBitmapDevice::isClipWideOpen() const {
651 const SkRasterClip& rc = fRCStack.rc();
652 // If we're AA, we can't be wide-open (we would represent that as BW)
653 return rc.isBW() && rc.bwRgn().isRect() &&
654 rc.bwRgn().getBounds() == SkIRect{0, 0, this->width(), this->height()};
655 }
656
isClipEmpty() const657 bool SkBitmapDevice::isClipEmpty() const {
658 return fRCStack.rc().isEmpty();
659 }
660
isClipRect() const661 bool SkBitmapDevice::isClipRect() const {
662 const SkRasterClip& rc = fRCStack.rc();
663 return !rc.isEmpty() && rc.isRect() && !SkToBool(rc.clipShader());
664 }
665
isClipAntiAliased() const666 bool SkBitmapDevice::isClipAntiAliased() const {
667 const SkRasterClip& rc = fRCStack.rc();
668 return !rc.isEmpty() && rc.isAA();
669 }
670
android_utils_clipAsRgn(SkRegion * rgn) const671 void SkBitmapDevice::android_utils_clipAsRgn(SkRegion* rgn) const {
672 const SkRasterClip& rc = fRCStack.rc();
673 if (rc.isAA()) {
674 rgn->setRect( rc.getBounds());
675 } else {
676 *rgn = rc.bwRgn();
677 }
678 }
679
devClipBounds() const680 SkIRect SkBitmapDevice::devClipBounds() const {
681 return fRCStack.rc().getBounds();
682 }
683