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