• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkImageFilter.h"
9 #include "include/core/SkMatrix.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/core/SkRasterHandleAllocator.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkSurface.h"
16 #include "include/core/SkVertices.h"
17 #include "src/core/SkBitmapDevice.h"
18 #include "src/core/SkDraw.h"
19 #include "src/core/SkGlyphRun.h"
20 #include "src/core/SkImageFilterCache.h"
21 #include "src/core/SkImageFilter_Base.h"
22 #include "src/core/SkRasterClip.h"
23 #include "src/core/SkSpecialImage.h"
24 #include "src/core/SkStrikeCache.h"
25 #include "src/core/SkTLazy.h"
26 
27 struct Bounder {
28     SkRect  fBounds;
29     bool    fHasBounds;
30 
BounderBounder31     Bounder(const SkRect& r, const SkPaint& paint) {
32         if ((fHasBounds = paint.canComputeFastBounds())) {
33             fBounds = paint.computeFastBounds(r, &fBounds);
34         }
35     }
36 
hasBoundsBounder37     bool hasBounds() const { return fHasBounds; }
boundsBounder38     const SkRect* bounds() const { return fHasBounds ? &fBounds : nullptr; }
operator const SkRect*Bounder39     operator const SkRect* () const { return this->bounds(); }
40 };
41 
42 class SkDrawTiler {
43     enum {
44         // 8K is 1 too big, since 8K << supersample == 32768 which is too big for SkFixed
45         kMaxDim = 8192 - 1
46     };
47 
48     SkBitmapDevice* fDevice;
49     SkPixmap        fRootPixmap;
50     SkIRect         fSrcBounds;
51 
52     // Used for tiling and non-tiling
53     SkDraw          fDraw;
54 
55     // fCurr... are only used if fNeedTiling
56     SkMatrix        fTileMatrix;
57     SkRasterClip    fTileRC;
58     SkIPoint        fOrigin;
59 
60     bool            fDone, fNeedsTiling;
61 
62 public:
NeedsTiling(SkBitmapDevice * dev)63     static bool NeedsTiling(SkBitmapDevice* dev) {
64         return dev->width() > kMaxDim || dev->height() > kMaxDim;
65     }
66 
SkDrawTiler(SkBitmapDevice * dev,const SkRect * bounds)67     SkDrawTiler(SkBitmapDevice* dev, const SkRect* bounds) : fDevice(dev) {
68         fDone = false;
69 
70         // we need fDst to be set, and if we're actually drawing, to dirty the genID
71         if (!dev->accessPixels(&fRootPixmap)) {
72             // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
73             fRootPixmap.reset(dev->imageInfo(), nullptr, 0);
74         }
75 
76         // do a quick check, so we don't even have to process "bounds" if there is no need
77         const SkIRect clipR = dev->fRCStack.rc().getBounds();
78         fNeedsTiling = clipR.right() > kMaxDim || clipR.bottom() > kMaxDim;
79         if (fNeedsTiling) {
80             if (bounds) {
81                 // Make sure we round first, and then intersect. We can't rely on promoting the
82                 // clipR to floats (and then intersecting with devBounds) since promoting
83                 // int --> float can make the float larger than the int.
84                 // rounding(out) first runs the risk of clamping if the float is larger an intmax
85                 // but our roundOut() is saturating, which is fine for this use case
86                 //
87                 // e.g. the older version of this code did this:
88                 //    devBounds = mapRect(bounds);
89                 //    if (devBounds.intersect(SkRect::Make(clipR))) {
90                 //        fSrcBounds = devBounds.roundOut();
91                 // The problem being that the promotion of clipR to SkRect was unreliable
92                 //
93                 fSrcBounds = dev->localToDevice().mapRect(*bounds).roundOut();
94                 if (fSrcBounds.intersect(clipR)) {
95                     // Check again, now that we have computed srcbounds.
96                     fNeedsTiling = fSrcBounds.right() > kMaxDim || fSrcBounds.bottom() > kMaxDim;
97                 } else {
98                     fNeedsTiling = false;
99                     fDone = true;
100                 }
101             } else {
102                 fSrcBounds = clipR;
103             }
104         }
105 
106         if (fNeedsTiling) {
107             // fDraw.fDst is reset each time in setupTileDraw()
108             fDraw.fMatrix = &fTileMatrix;
109             fDraw.fRC = &fTileRC;
110             // we'll step/increase it before using it
111             fOrigin.set(fSrcBounds.fLeft - kMaxDim, fSrcBounds.fTop);
112         } else {
113             // don't reference fSrcBounds, as it may not have been set
114             fDraw.fDst = fRootPixmap;
115             fDraw.fMatrix = &dev->localToDevice();
116             fDraw.fRC = &dev->fRCStack.rc();
117             fOrigin.set(0, 0);
118 
119             fDraw.fCoverage = dev->accessCoverage();
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         fTileMatrix = fDevice->localToDevice();
168         fTileMatrix.postTranslate(SkIntToScalar(-fOrigin.x()), SkIntToScalar(-fOrigin.y()));
169         fDevice->fRCStack.rc().translate(-fOrigin.x(), -fOrigin.y(), &fTileRC);
170         fTileRC.op(SkIRect::MakeWH(fDraw.fDst.width(), fDraw.fDst.height()),
171                    SkRegion::kIntersect_Op);
172     }
173 };
174 
175 // Passing a bounds allows the tiler to only visit the dst-tiles that might intersect the
176 // drawing. If null is passed, the tiler has to visit everywhere. The bounds is expected to be
177 // in local coordinates, as the tiler itself will transform that into device coordinates.
178 //
179 #define LOOP_TILER(code, boundsPtr)                         \
180     SkDrawTiler priv_tiler(this, boundsPtr);                \
181     while (const SkDraw* priv_draw = priv_tiler.next()) {   \
182         priv_draw->code;                                    \
183     }
184 
185 // Helper to create an SkDraw from a device
186 class SkBitmapDevice::BDDraw : public SkDraw {
187 public:
BDDraw(SkBitmapDevice * dev)188     BDDraw(SkBitmapDevice* dev) {
189         // we need fDst to be set, and if we're actually drawing, to dirty the genID
190         if (!dev->accessPixels(&fDst)) {
191             // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
192             fDst.reset(dev->imageInfo(), nullptr, 0);
193         }
194         fMatrix = &dev->localToDevice();
195         fRC = &dev->fRCStack.rc();
196         fCoverage = dev->accessCoverage();
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(SkSurfaceProps::kLegacyFontHost_InitType))
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(SkSurfaceProps::kLegacyFontHost_InitType));
227 }
228 
SkBitmapDevice(const SkBitmap & bitmap,const SkSurfaceProps & surfaceProps,SkRasterHandleAllocator::Handle hndl,const SkBitmap * coverage)229 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
230                                SkRasterHandleAllocator::Handle hndl, const SkBitmap* coverage)
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     if (coverage) {
242         SkASSERT(coverage->width() == bitmap.width());
243         SkASSERT(coverage->height() == bitmap.height());
244         fCoverage = std::make_unique<SkBitmap>(*coverage);
245     }
246 }
247 
Create(const SkImageInfo & origInfo,const SkSurfaceProps & surfaceProps,bool trackCoverage,SkRasterHandleAllocator * allocator)248 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
249                                        const SkSurfaceProps& surfaceProps,
250                                        bool trackCoverage,
251                                        SkRasterHandleAllocator* allocator) {
252     SkAlphaType newAT = origInfo.alphaType();
253     if (!valid_for_bitmap_device(origInfo, &newAT)) {
254         return nullptr;
255     }
256 
257     SkRasterHandleAllocator::Handle hndl = nullptr;
258     const SkImageInfo info = origInfo.makeAlphaType(newAT);
259     SkBitmap bitmap;
260 
261     if (kUnknown_SkColorType == info.colorType()) {
262         if (!bitmap.setInfo(info)) {
263             return nullptr;
264         }
265     } else if (allocator) {
266         hndl = allocator->allocBitmap(info, &bitmap);
267         if (!hndl) {
268             return nullptr;
269         }
270     } else if (info.isOpaque()) {
271         // If this bitmap is opaque, we don't have any sensible default color,
272         // so we just return uninitialized pixels.
273         if (!bitmap.tryAllocPixels(info)) {
274             return nullptr;
275         }
276     } else {
277         // This bitmap has transparency, so we'll zero the pixels (to transparent).
278         // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT).
279         if (!bitmap.tryAllocPixelsFlags(info, SkBitmap::kZeroPixels_AllocFlag)) {
280             return nullptr;
281         }
282     }
283 
284     SkBitmap coverage;
285     if (trackCoverage) {
286         SkImageInfo ci =
287                 SkImageInfo::Make(info.dimensions(), kAlpha_8_SkColorType, kPremul_SkAlphaType);
288         if (!coverage.tryAllocPixelsFlags(ci, SkBitmap::kZeroPixels_AllocFlag)) {
289             return nullptr;
290         }
291     }
292 
293     return new SkBitmapDevice(bitmap, surfaceProps, hndl, trackCoverage ? &coverage : nullptr);
294 }
295 
replaceBitmapBackendForRasterSurface(const SkBitmap & bm)296 void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) {
297     SkASSERT(bm.width() == fBitmap.width());
298     SkASSERT(bm.height() == fBitmap.height());
299     fBitmap = bm;   // intent is to use bm's pixelRef (and rowbytes/config)
300     this->privateResize(fBitmap.info().width(), fBitmap.info().height());
301 }
302 
onCreateDevice(const CreateInfo & cinfo,const SkPaint * layerPaint)303 SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
304     const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
305 
306     // Need to force L32 for now if we have an image filter.
307     // If filters ever support other colortypes, e.g. F16, we can modify this check.
308     SkImageInfo info = cinfo.fInfo;
309     if (layerPaint && layerPaint->getImageFilter()) {
310         // TODO: can we query the imagefilter, to see if it can handle floats (so we don't always
311         //       use N32 when the layer itself was float)?
312         info = info.makeColorType(kN32_SkColorType);
313     }
314 
315     return SkBitmapDevice::Create(info, surfaceProps, cinfo.fTrackCoverage, cinfo.fAllocator);
316 }
317 
onAccessPixels(SkPixmap * pmap)318 bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
319     if (this->onPeekPixels(pmap)) {
320         fBitmap.notifyPixelsChanged();
321         return true;
322     }
323     return false;
324 }
325 
onPeekPixels(SkPixmap * pmap)326 bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
327     const SkImageInfo info = fBitmap.info();
328     if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
329         pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes());
330         return true;
331     }
332     return false;
333 }
334 
onWritePixels(const SkPixmap & pm,int x,int y)335 bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
336     // since we don't stop creating un-pixeled devices yet, check for no pixels here
337     if (nullptr == fBitmap.getPixels()) {
338         return false;
339     }
340 
341     if (fBitmap.writePixels(pm, x, y)) {
342         fBitmap.notifyPixelsChanged();
343         return true;
344     }
345     return false;
346 }
347 
onReadPixels(const SkPixmap & pm,int x,int y)348 bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
349     return fBitmap.readPixels(pm, x, y);
350 }
351 
352 ///////////////////////////////////////////////////////////////////////////////
353 
drawPaint(const SkPaint & paint)354 void SkBitmapDevice::drawPaint(const SkPaint& paint) {
355     BDDraw(this).drawPaint(paint);
356 }
357 
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)358 void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
359                                 const SkPoint pts[], const SkPaint& paint) {
360     LOOP_TILER( drawPoints(mode, count, pts, paint, nullptr), nullptr)
361 }
362 
drawRect(const SkRect & r,const SkPaint & paint)363 void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) {
364     LOOP_TILER( drawRect(r, paint), Bounder(r, paint))
365 }
366 
drawOval(const SkRect & oval,const SkPaint & paint)367 void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
368     SkPath path;
369     path.addOval(oval);
370     // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
371     // required to override drawOval.
372     this->drawPath(path, paint, true);
373 }
374 
drawRRect(const SkRRect & rrect,const SkPaint & paint)375 void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
376 #ifdef SK_IGNORE_BLURRED_RRECT_OPT
377     SkPath  path;
378 
379     path.addRRect(rrect);
380     // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
381     // required to override drawRRect.
382     this->drawPath(path, paint, true);
383 #else
384     LOOP_TILER( drawRRect(rrect, paint), Bounder(rrect.getBounds(), paint))
385 #endif
386 }
387 
drawPath(const SkPath & path,const SkPaint & paint,bool pathIsMutable)388 void SkBitmapDevice::drawPath(const SkPath& path,
389                               const SkPaint& paint,
390                               bool pathIsMutable) {
391     const SkRect* bounds = nullptr;
392     if (SkDrawTiler::NeedsTiling(this) && !path.isInverseFillType()) {
393         bounds = &path.getBounds();
394     }
395     SkDrawTiler tiler(this, bounds ? Bounder(*bounds, paint).bounds() : nullptr);
396     if (tiler.needsTiling()) {
397         pathIsMutable = false;
398     }
399     while (const SkDraw* draw = tiler.next()) {
400         draw->drawPath(path, paint, nullptr, pathIsMutable);
401     }
402 }
403 
drawBitmap(const SkBitmap & bitmap,const SkMatrix & matrix,const SkRect * dstOrNull,const SkPaint & paint)404 void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
405                                 const SkRect* dstOrNull, const SkPaint& paint) {
406     const SkRect* bounds = dstOrNull;
407     SkRect storage;
408     if (!bounds && SkDrawTiler::NeedsTiling(this)) {
409         matrix.mapRect(&storage, SkRect::MakeIWH(bitmap.width(), bitmap.height()));
410         Bounder b(storage, paint);
411         if (b.hasBounds()) {
412             storage = *b.bounds();
413             bounds = &storage;
414         }
415     }
416     LOOP_TILER(drawBitmap(bitmap, matrix, dstOrNull, paint), bounds)
417 }
418 
CanApplyDstMatrixAsCTM(const SkMatrix & m,const SkPaint & paint)419 static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
420     if (!paint.getMaskFilter()) {
421         return true;
422     }
423 
424     // Some mask filters parameters (sigma) depend on the CTM/scale.
425     return m.getType() <= SkMatrix::kTranslate_Mask;
426 }
427 
drawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)428 void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap,
429                                     const SkRect* src, const SkRect& dst,
430                                     const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
431     SkASSERT(dst.isFinite());
432     SkASSERT(dst.isSorted());
433 
434     SkMatrix    matrix;
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     matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
447 
448     LogDrawScaleFactor(this->localToDevice(), matrix, paint.getFilterQuality());
449 
450     const SkRect* dstPtr = &dst;
451     const SkBitmap* bitmapPtr = &bitmap;
452 
453     // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
454     // needed (if the src was clipped). No check needed if src==null.
455     if (src) {
456         if (!bitmapBounds.contains(*src)) {
457             if (!tmpSrc.intersect(bitmapBounds)) {
458                 return; // nothing to draw
459             }
460             // recompute dst, based on the smaller tmpSrc
461             matrix.mapRect(&tmpDst, tmpSrc);
462             if (!tmpDst.isFinite()) {
463                 return;
464             }
465             dstPtr = &tmpDst;
466         }
467     }
468 
469     if (src && !src->contains(bitmapBounds) &&
470         SkCanvas::kFast_SrcRectConstraint == constraint &&
471         paint.getFilterQuality() != kNone_SkFilterQuality) {
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 (src) {
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 
487         // Since we did an extract, we need to adjust the matrix accordingly
488         SkScalar dx = 0, dy = 0;
489         if (srcIR.fLeft > 0) {
490             dx = SkIntToScalar(srcIR.fLeft);
491         }
492         if (srcIR.fTop > 0) {
493             dy = SkIntToScalar(srcIR.fTop);
494         }
495         if (dx || dy) {
496             matrix.preTranslate(dx, dy);
497         }
498 
499 #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
500         SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
501                                                         SkIntToScalar(bitmapPtr->width()),
502                                                         SkIntToScalar(bitmapPtr->height()));
503 #else
504         SkRect extractedBitmapBounds;
505         extractedBitmapBounds.setIWH(bitmapPtr->width(), bitmapPtr->height());
506 #endif
507         if (extractedBitmapBounds == tmpSrc) {
508             // no fractional part in src, we can just call drawBitmap
509             goto USE_DRAWBITMAP;
510         }
511     } else {
512         USE_DRAWBITMAP:
513         // We can go faster by just calling drawBitmap, which will concat the
514         // matrix with the CTM, and try to call drawSprite if it can. If not,
515         // it will make a shader and call drawRect, as we do below.
516         if (CanApplyDstMatrixAsCTM(matrix, paint)) {
517             this->drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
518             return;
519         }
520     }
521 
522     USE_SHADER:
523 
524     // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
525     // Since the shader need only live for our stack-frame, pass in a custom allocator. This
526     // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap
527     // if its mutable, since that precaution is not needed (give the short lifetime of the shader).
528 
529     // construct a shader, so we can call drawRect with the dst
530     auto s = SkMakeBitmapShaderForPaint(paint, *bitmapPtr, SkTileMode::kClamp,
531                                         SkTileMode::kClamp, &matrix, kNever_SkCopyPixelsMode);
532     if (!s) {
533         return;
534     }
535 
536     SkPaint paintWithShader(paint);
537     paintWithShader.setStyle(SkPaint::kFill_Style);
538     paintWithShader.setShader(std::move(s));
539 
540     // Call ourself, in case the subclass wanted to share this setup code
541     // but handle the drawRect code themselves.
542     this->drawRect(*dstPtr, paintWithShader);
543 }
544 
drawSprite(const SkBitmap & bitmap,int x,int y,const SkPaint & paint)545 void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
546     BDDraw(this).drawSprite(bitmap, x, y, paint);
547 }
548 
drawGlyphRunList(const SkGlyphRunList & glyphRunList)549 void SkBitmapDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
550     LOOP_TILER( drawGlyphRunList(glyphRunList, &fGlyphPainter), nullptr )
551 }
552 
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const SkPaint & paint)553 void SkBitmapDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
554                                   int boneCount, SkBlendMode bmode, const SkPaint& paint) {
555     BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
556                               vertices->texCoords(), vertices->colors(), vertices->boneIndices(),
557                               vertices->boneWeights(), bmode, vertices->indices(),
558                               vertices->indexCount(), paint, bones, boneCount);
559 }
560 
drawDevice(SkBaseDevice * device,int x,int y,const SkPaint & origPaint)561 void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& origPaint) {
562     SkASSERT(!origPaint.getImageFilter());
563 
564     // todo: can we unify with similar adjustment in SkGpuDevice?
565     SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
566     if (paint->getMaskFilter()) {
567         paint.writable()->setMaskFilter(
568                 paint->getMaskFilter()->makeWithMatrix(this->localToDevice()));
569     }
570 
571     // hack to test coverage
572     SkBitmapDevice* src = static_cast<SkBitmapDevice*>(device);
573     if (src->fCoverage) {
574         SkDraw draw;
575         draw.fDst = fBitmap.pixmap();
576         draw.fMatrix = &SkMatrix::I();
577         draw.fRC = &fRCStack.rc();
578         SkPaint paint(origPaint);
579         paint.setShader(src->fBitmap.makeShader());
580         draw.drawBitmap(*src->fCoverage.get(),
581                         SkMatrix::MakeTrans(SkIntToScalar(x),SkIntToScalar(y)), nullptr, paint);
582     } else {
583         this->drawSprite(src->fBitmap, x, y, *paint);
584     }
585 }
586 
drawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkPaint & paint)587 void SkBitmapDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
588                                const SkRect tex[], const SkColor colors[], int count,
589                                SkBlendMode mode, const SkPaint& paint) {
590     // set this to true for performance comparisons with the old drawVertices way
591     if (false) {
592         this->INHERITED::drawAtlas(atlas, xform, tex, colors, count, mode, paint);
593         return;
594     }
595     BDDraw(this).drawAtlas(atlas, xform, tex, colors, count, mode, paint);
596 }
597 
598 ///////////////////////////////////////////////////////////////////////////////
599 
600 namespace {
601 
602 class SkAutoDeviceClipRestore {
603 public:
SkAutoDeviceClipRestore(SkBaseDevice * device,const SkIRect & clip)604     SkAutoDeviceClipRestore(SkBaseDevice* device, const SkIRect& clip)
605         : fDevice(device)
606         , fPrevLocalToDevice(device->localToDevice()) {
607         fDevice->save();
608         fDevice->setLocalToDevice(SkMatrix::I());
609         fDevice->clipRect(SkRect::Make(clip), SkClipOp::kIntersect, false);
610         fDevice->setLocalToDevice(fPrevLocalToDevice);
611     }
612 
~SkAutoDeviceClipRestore()613     ~SkAutoDeviceClipRestore() {
614         fDevice->restoreLocal(fPrevLocalToDevice);
615     }
616 
617 private:
618     SkBaseDevice*  fDevice;
619     const SkMatrix fPrevLocalToDevice;
620 };
621 
622 }  // anonymous ns
623 
drawSpecial(SkSpecialImage * src,int x,int y,const SkPaint & origPaint,SkImage * clipImage,const SkMatrix & clipMatrix)624 void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint,
625                                  SkImage* clipImage, const SkMatrix& clipMatrix) {
626     SkASSERT(!src->isTextureBacked());
627 
628     sk_sp<SkSpecialImage> filteredImage;
629     SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
630 
631     if (SkImageFilter* filter = paint->getImageFilter()) {
632         SkIPoint offset = SkIPoint::Make(0, 0);
633         const SkMatrix matrix = SkMatrix::Concat(
634             SkMatrix::MakeTrans(SkIntToScalar(-x), SkIntToScalar(-y)), this->localToDevice());
635         const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y);
636         sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
637         SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), fBitmap.colorType(),
638                                         fBitmap.colorSpace(), src);
639 
640         filteredImage = as_IFB(filter)->filterImage(ctx).imageAndOffset(&offset);
641         if (!filteredImage) {
642             return;
643         }
644 
645         src = filteredImage.get();
646         paint.writable()->setImageFilter(nullptr);
647         x += offset.x();
648         y += offset.y();
649     }
650 
651     if (paint->getMaskFilter()) {
652         paint.writable()->setMaskFilter(
653                 paint->getMaskFilter()->makeWithMatrix(this->localToDevice()));
654     }
655 
656     if (!clipImage) {
657         SkBitmap resultBM;
658         if (src->getROPixels(&resultBM)) {
659             this->drawSprite(resultBM, x, y, *paint);
660         }
661         return;
662     }
663 
664     // Clip image case.
665     sk_sp<SkImage> srcImage(src->asImage());
666     if (!srcImage) {
667         return;
668     }
669 
670     const SkMatrix totalMatrix = SkMatrix::Concat(this->localToDevice(), clipMatrix);
671     SkRect clipBounds;
672     totalMatrix.mapRect(&clipBounds, SkRect::Make(clipImage->bounds()));
673     const SkIRect srcBounds = srcImage->bounds().makeOffset(x, y);
674 
675     SkIRect maskBounds = fRCStack.rc().getBounds();
676     if (!maskBounds.intersect(clipBounds.roundOut()) || !maskBounds.intersect(srcBounds)) {
677         return;
678     }
679 
680     sk_sp<SkImage> mask;
681     SkMatrix maskMatrix, shaderMatrix;
682     SkTLazy<SkAutoDeviceClipRestore> autoClipRestore;
683 
684     SkMatrix totalInverse;
685     if (clipImage->isAlphaOnly() && totalMatrix.invert(&totalInverse)) {
686         // If the mask is already in A8 format, we can draw it directly
687         // (while compensating in the shader matrix).
688         mask = sk_ref_sp(clipImage);
689         maskMatrix = totalMatrix;
690         shaderMatrix = SkMatrix::Concat(totalInverse, SkMatrix::MakeTrans(x, y));
691 
692         // If the mask is not fully contained within the src layer, we must clip.
693         if (!srcBounds.contains(clipBounds)) {
694             autoClipRestore.init(this, srcBounds);
695         }
696 
697         maskBounds.offsetTo(0, 0);
698     } else {
699         // Otherwise, we convert the mask to A8 explicitly.
700         sk_sp<SkSurface> surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(maskBounds.width(),
701                                                                           maskBounds.height()));
702         SkCanvas* canvas = surf->getCanvas();
703         canvas->translate(-maskBounds.x(), -maskBounds.y());
704         canvas->concat(totalMatrix);
705         canvas->drawImage(clipImage, 0, 0);
706 
707         mask = surf->makeImageSnapshot();
708         maskMatrix = SkMatrix::I();
709         shaderMatrix = SkMatrix::MakeTrans(x - maskBounds.x(), y - maskBounds.y());
710     }
711 
712     SkAutoDeviceTransformRestore adr(this, maskMatrix);
713     paint.writable()->setShader(srcImage->makeShader(&shaderMatrix));
714     this->drawImageRect(mask.get(), nullptr,
715                         SkRect::MakeXYWH(maskBounds.x(), maskBounds.y(),
716                                          mask->width(), mask->height()),
717                         *paint, SkCanvas::kFast_SrcRectConstraint);
718 }
719 
makeSpecial(const SkBitmap & bitmap)720 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
721     return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
722 }
723 
makeSpecial(const SkImage * image)724 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
725     return SkSpecialImage::MakeFromImage(nullptr, SkIRect::MakeWH(image->width(), image->height()),
726                                          image->makeNonTextureImage());
727 }
728 
snapSpecial(const SkIRect & bounds,bool forceCopy)729 sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial(const SkIRect& bounds, bool forceCopy) {
730     if (forceCopy) {
731         return SkSpecialImage::CopyFromRaster(bounds, fBitmap, &this->surfaceProps());
732     } else {
733         return SkSpecialImage::MakeFromRaster(bounds, fBitmap);
734     }
735 }
736 
737 ///////////////////////////////////////////////////////////////////////////////
738 
makeSurface(const SkImageInfo & info,const SkSurfaceProps & props)739 sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
740     return SkSurface::MakeRaster(info, &props);
741 }
742 
getImageFilterCache()743 SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
744     SkImageFilterCache* cache = SkImageFilterCache::Get();
745     cache->ref();
746     return cache;
747 }
748 
749 ///////////////////////////////////////////////////////////////////////////////////////////////////
750 
onSave()751 void SkBitmapDevice::onSave() {
752     fRCStack.save();
753 }
754 
onRestore()755 void SkBitmapDevice::onRestore() {
756     fRCStack.restore();
757 }
758 
onClipRect(const SkRect & rect,SkClipOp op,bool aa)759 void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
760     fRCStack.clipRect(this->localToDevice(), rect, op, aa);
761 }
762 
onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)763 void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
764     fRCStack.clipRRect(this->localToDevice(), rrect, op, aa);
765 }
766 
onClipPath(const SkPath & path,SkClipOp op,bool aa)767 void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
768     fRCStack.clipPath(this->localToDevice(), path, op, aa);
769 }
770 
onClipRegion(const SkRegion & rgn,SkClipOp op)771 void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
772     SkIPoint origin = this->getOrigin();
773     SkRegion tmp;
774     const SkRegion* ptr = &rgn;
775     if (origin.fX | origin.fY) {
776         // translate from "global/canvas" coordinates to relative to this device
777         rgn.translate(-origin.fX, -origin.fY, &tmp);
778         ptr = &tmp;
779     }
780     fRCStack.clipRegion(*ptr, op);
781 }
782 
onSetDeviceClipRestriction(SkIRect * mutableClipRestriction)783 void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {
784     fRCStack.setDeviceClipRestriction(mutableClipRestriction);
785     if (!mutableClipRestriction->isEmpty()) {
786         SkRegion rgn(*mutableClipRestriction);
787         fRCStack.clipRegion(rgn, SkClipOp::kIntersect);
788     }
789 }
790 
onClipIsWideOpen() const791 bool SkBitmapDevice::onClipIsWideOpen() const {
792     const SkRasterClip& rc = fRCStack.rc();
793     // If we're AA, we can't be wide-open (we would represent that as BW)
794     return rc.isBW() && rc.bwRgn().isRect() &&
795            rc.bwRgn().getBounds() == SkIRect{0, 0, this->width(), this->height()};
796 }
797 
onClipIsAA() const798 bool SkBitmapDevice::onClipIsAA() const {
799     const SkRasterClip& rc = fRCStack.rc();
800     return !rc.isEmpty() && rc.isAA();
801 }
802 
onAsRgnClip(SkRegion * rgn) const803 void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const {
804     const SkRasterClip& rc = fRCStack.rc();
805     if (rc.isAA()) {
806         rgn->setRect(rc.getBounds());
807     } else {
808         *rgn = rc.bwRgn();
809     }
810 }
811 
validateDevBounds(const SkIRect & drawClipBounds)812 void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) {
813 #ifdef SK_DEBUG
814     const SkIRect& stackBounds = fRCStack.rc().getBounds();
815     SkASSERT(drawClipBounds == stackBounds);
816 #endif
817 }
818 
onGetClipType() const819 SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const {
820     const SkRasterClip& rc = fRCStack.rc();
821     if (rc.isEmpty()) {
822         return ClipType::kEmpty;
823     } else if (rc.isRect()) {
824         return ClipType::kRect;
825     } else {
826         return ClipType::kComplex;
827     }
828 }
829