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 "SkBitmapDevice.h"
9 #include "SkDraw.h"
10 #include "SkGlyphRun.h"
11 #include "SkImageFilter.h"
12 #include "SkImageFilterCache.h"
13 #include "SkMakeUnique.h"
14 #include "SkMatrix.h"
15 #include "SkPaint.h"
16 #include "SkPath.h"
17 #include "SkPixmap.h"
18 #include "SkRasterClip.h"
19 #include "SkRasterHandleAllocator.h"
20 #include "SkShader.h"
21 #include "SkSpecialImage.h"
22 #include "SkStrikeCache.h"
23 #include "SkSurface.h"
24 #include "SkTLazy.h"
25 #include "SkVertices.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->ctm().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->ctm();
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->ctm();
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->ctm();
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 = skstd::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 = SkImageInfo::Make(info.width(), info.height(), kAlpha_8_SkColorType,
287                                            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 *)303 SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
304     const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
305     return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fTrackCoverage,
306                                   cinfo.fAllocator);
307 }
308 
onAccessPixels(SkPixmap * pmap)309 bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
310     if (this->onPeekPixels(pmap)) {
311         fBitmap.notifyPixelsChanged();
312         return true;
313     }
314     return false;
315 }
316 
onPeekPixels(SkPixmap * pmap)317 bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
318     const SkImageInfo info = fBitmap.info();
319     if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
320         pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes());
321         return true;
322     }
323     return false;
324 }
325 
onWritePixels(const SkPixmap & pm,int x,int y)326 bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
327     // since we don't stop creating un-pixeled devices yet, check for no pixels here
328     if (nullptr == fBitmap.getPixels()) {
329         return false;
330     }
331 
332     if (fBitmap.writePixels(pm, x, y)) {
333         fBitmap.notifyPixelsChanged();
334         return true;
335     }
336     return false;
337 }
338 
onReadPixels(const SkPixmap & pm,int x,int y)339 bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
340     return fBitmap.readPixels(pm, x, y);
341 }
342 
343 ///////////////////////////////////////////////////////////////////////////////
344 
drawPaint(const SkPaint & paint)345 void SkBitmapDevice::drawPaint(const SkPaint& paint) {
346     BDDraw(this).drawPaint(paint);
347 }
348 
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)349 void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
350                                 const SkPoint pts[], const SkPaint& paint) {
351     LOOP_TILER( drawPoints(mode, count, pts, paint, nullptr), nullptr)
352 }
353 
drawRect(const SkRect & r,const SkPaint & paint)354 void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) {
355     LOOP_TILER( drawRect(r, paint), Bounder(r, paint))
356 }
357 
drawOval(const SkRect & oval,const SkPaint & paint)358 void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
359     SkPath path;
360     path.addOval(oval);
361     // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
362     // required to override drawOval.
363     this->drawPath(path, paint, true);
364 }
365 
drawRRect(const SkRRect & rrect,const SkPaint & paint)366 void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
367 #ifdef SK_IGNORE_BLURRED_RRECT_OPT
368     SkPath  path;
369 
370     path.addRRect(rrect);
371     // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
372     // required to override drawRRect.
373     this->drawPath(path, paint, true);
374 #else
375     LOOP_TILER( drawRRect(rrect, paint), Bounder(rrect.getBounds(), paint))
376 #endif
377 }
378 
drawPath(const SkPath & path,const SkPaint & paint,bool pathIsMutable)379 void SkBitmapDevice::drawPath(const SkPath& path,
380                               const SkPaint& paint,
381                               bool pathIsMutable) {
382     const SkRect* bounds = nullptr;
383     if (SkDrawTiler::NeedsTiling(this) && !path.isInverseFillType()) {
384         bounds = &path.getBounds();
385     }
386     SkDrawTiler tiler(this, bounds ? Bounder(*bounds, paint).bounds() : nullptr);
387     if (tiler.needsTiling()) {
388         pathIsMutable = false;
389     }
390     while (const SkDraw* draw = tiler.next()) {
391         draw->drawPath(path, paint, nullptr, pathIsMutable);
392     }
393 }
394 
drawBitmap(const SkBitmap & bitmap,const SkMatrix & matrix,const SkRect * dstOrNull,const SkPaint & paint)395 void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
396                                 const SkRect* dstOrNull, const SkPaint& paint) {
397     const SkRect* bounds = dstOrNull;
398     SkRect storage;
399     if (!bounds && SkDrawTiler::NeedsTiling(this)) {
400         matrix.mapRect(&storage, SkRect::MakeIWH(bitmap.width(), bitmap.height()));
401         Bounder b(storage, paint);
402         if (b.hasBounds()) {
403             storage = *b.bounds();
404             bounds = &storage;
405         }
406     }
407     LOOP_TILER(drawBitmap(bitmap, matrix, dstOrNull, paint), bounds)
408 }
409 
CanApplyDstMatrixAsCTM(const SkMatrix & m,const SkPaint & paint)410 static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
411     if (!paint.getMaskFilter()) {
412         return true;
413     }
414 
415     // Some mask filters parameters (sigma) depend on the CTM/scale.
416     return m.getType() <= SkMatrix::kTranslate_Mask;
417 }
418 
drawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)419 void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap,
420                                     const SkRect* src, const SkRect& dst,
421                                     const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
422     SkASSERT(dst.isFinite());
423     SkASSERT(dst.isSorted());
424 
425     SkMatrix    matrix;
426     SkRect      bitmapBounds, tmpSrc, tmpDst;
427     SkBitmap    tmpBitmap;
428 
429     bitmapBounds.isetWH(bitmap.width(), bitmap.height());
430 
431     // Compute matrix from the two rectangles
432     if (src) {
433         tmpSrc = *src;
434     } else {
435         tmpSrc = bitmapBounds;
436     }
437     matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
438 
439     LogDrawScaleFactor(this->ctm(), matrix, paint.getFilterQuality());
440 
441     const SkRect* dstPtr = &dst;
442     const SkBitmap* bitmapPtr = &bitmap;
443 
444     // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
445     // needed (if the src was clipped). No check needed if src==null.
446     if (src) {
447         if (!bitmapBounds.contains(*src)) {
448             if (!tmpSrc.intersect(bitmapBounds)) {
449                 return; // nothing to draw
450             }
451             // recompute dst, based on the smaller tmpSrc
452             matrix.mapRect(&tmpDst, tmpSrc);
453             if (!tmpDst.isFinite()) {
454                 return;
455             }
456             dstPtr = &tmpDst;
457         }
458     }
459 
460     if (src && !src->contains(bitmapBounds) &&
461         SkCanvas::kFast_SrcRectConstraint == constraint &&
462         paint.getFilterQuality() != kNone_SkFilterQuality) {
463         // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know
464         // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap,
465         // but we must use a shader w/ dst bounds (which can access all of the bitmap needed).
466         goto USE_SHADER;
467     }
468 
469     if (src) {
470         // since we may need to clamp to the borders of the src rect within
471         // the bitmap, we extract a subset.
472         const SkIRect srcIR = tmpSrc.roundOut();
473         if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
474             return;
475         }
476         bitmapPtr = &tmpBitmap;
477 
478         // Since we did an extract, we need to adjust the matrix accordingly
479         SkScalar dx = 0, dy = 0;
480         if (srcIR.fLeft > 0) {
481             dx = SkIntToScalar(srcIR.fLeft);
482         }
483         if (srcIR.fTop > 0) {
484             dy = SkIntToScalar(srcIR.fTop);
485         }
486         if (dx || dy) {
487             matrix.preTranslate(dx, dy);
488         }
489 
490 #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
491         SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
492                                                         SkIntToScalar(bitmapPtr->width()),
493                                                         SkIntToScalar(bitmapPtr->height()));
494 #else
495         SkRect extractedBitmapBounds;
496         extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height());
497 #endif
498         if (extractedBitmapBounds == tmpSrc) {
499             // no fractional part in src, we can just call drawBitmap
500             goto USE_DRAWBITMAP;
501         }
502     } else {
503         USE_DRAWBITMAP:
504         // We can go faster by just calling drawBitmap, which will concat the
505         // matrix with the CTM, and try to call drawSprite if it can. If not,
506         // it will make a shader and call drawRect, as we do below.
507         if (CanApplyDstMatrixAsCTM(matrix, paint)) {
508             this->drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
509             return;
510         }
511     }
512 
513     USE_SHADER:
514 
515     // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
516     // Since the shader need only live for our stack-frame, pass in a custom allocator. This
517     // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap
518     // if its mutable, since that precaution is not needed (give the short lifetime of the shader).
519 
520     // construct a shader, so we can call drawRect with the dst
521     auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
522                                 &matrix, kNever_SkCopyPixelsMode);
523     if (!s) {
524         return;
525     }
526 
527     SkPaint paintWithShader(paint);
528     paintWithShader.setStyle(SkPaint::kFill_Style);
529     paintWithShader.setShader(s);
530 
531     // Call ourself, in case the subclass wanted to share this setup code
532     // but handle the drawRect code themselves.
533     this->drawRect(*dstPtr, paintWithShader);
534 }
535 
drawSprite(const SkBitmap & bitmap,int x,int y,const SkPaint & paint)536 void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
537     BDDraw(this).drawSprite(bitmap, x, y, paint);
538 }
539 
drawGlyphRunList(const SkGlyphRunList & glyphRunList)540 void SkBitmapDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
541     LOOP_TILER( drawGlyphRunList(glyphRunList, &fGlyphPainter), nullptr )
542 }
543 
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const SkPaint & paint)544 void SkBitmapDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
545                                   int boneCount, SkBlendMode bmode, const SkPaint& paint) {
546     BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
547                               vertices->texCoords(), vertices->colors(), vertices->boneIndices(),
548                               vertices->boneWeights(), bmode, vertices->indices(),
549                               vertices->indexCount(), paint, bones, boneCount);
550 }
551 
drawDevice(SkBaseDevice * device,int x,int y,const SkPaint & origPaint)552 void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& origPaint) {
553     SkASSERT(!origPaint.getImageFilter());
554 
555     // todo: can we unify with similar adjustment in SkGpuDevice?
556     SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
557     if (paint->getMaskFilter()) {
558         paint.writable()->setMaskFilter(paint->getMaskFilter()->makeWithMatrix(this->ctm()));
559     }
560 
561     // hack to test coverage
562     SkBitmapDevice* src = static_cast<SkBitmapDevice*>(device);
563     if (src->fCoverage) {
564         SkDraw draw;
565         draw.fDst = fBitmap.pixmap();
566         draw.fMatrix = &SkMatrix::I();
567         draw.fRC = &fRCStack.rc();
568         SkPaint paint(origPaint);
569         paint.setShader(SkShader::MakeBitmapShader(src->fBitmap, SkShader::kClamp_TileMode,
570                                                    SkShader::kClamp_TileMode, nullptr));
571         draw.drawBitmap(*src->fCoverage.get(),
572                         SkMatrix::MakeTrans(SkIntToScalar(x),SkIntToScalar(y)), nullptr, paint);
573     } else {
574         this->drawSprite(src->fBitmap, x, y, *paint);
575     }
576 }
577 
578 ///////////////////////////////////////////////////////////////////////////////
579 
580 namespace {
581 
582 class SkAutoDeviceClipRestore {
583 public:
SkAutoDeviceClipRestore(SkBaseDevice * device,const SkIRect & clip)584     SkAutoDeviceClipRestore(SkBaseDevice* device, const SkIRect& clip)
585         : fDevice(device)
586         , fPrevCTM(device->ctm()) {
587         fDevice->save();
588         fDevice->setCTM(SkMatrix::I());
589         fDevice->clipRect(SkRect::Make(clip), SkClipOp::kIntersect, false);
590         fDevice->setCTM(fPrevCTM);
591     }
592 
~SkAutoDeviceClipRestore()593     ~SkAutoDeviceClipRestore() {
594         fDevice->restore(fPrevCTM);
595     }
596 
597 private:
598     SkBaseDevice*  fDevice;
599     const SkMatrix fPrevCTM;
600 };
601 
602 }  // anonymous ns
603 
drawSpecial(SkSpecialImage * src,int x,int y,const SkPaint & origPaint,SkImage * clipImage,const SkMatrix & clipMatrix)604 void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint,
605                                  SkImage* clipImage, const SkMatrix& clipMatrix) {
606     SkASSERT(!src->isTextureBacked());
607 
608     sk_sp<SkSpecialImage> filteredImage;
609     SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
610 
611     if (SkImageFilter* filter = paint->getImageFilter()) {
612         SkIPoint offset = SkIPoint::Make(0, 0);
613         const SkMatrix matrix = SkMatrix::Concat(
614             SkMatrix::MakeTrans(SkIntToScalar(-x), SkIntToScalar(-y)), this->ctm());
615         const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y);
616         sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
617         SkImageFilter::OutputProperties outputProperties(fBitmap.colorType(), fBitmap.colorSpace());
618         SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
619 
620         filteredImage = filter->filterImage(src, ctx, &offset);
621         if (!filteredImage) {
622             return;
623         }
624 
625         src = filteredImage.get();
626         paint.writable()->setImageFilter(nullptr);
627         x += offset.x();
628         y += offset.y();
629     }
630 
631     if (paint->getMaskFilter()) {
632         paint.writable()->setMaskFilter(paint->getMaskFilter()->makeWithMatrix(this->ctm()));
633     }
634 
635     if (!clipImage) {
636         SkBitmap resultBM;
637         if (src->getROPixels(&resultBM)) {
638             this->drawSprite(resultBM, x, y, *paint);
639         }
640         return;
641     }
642 
643     // Clip image case.
644     sk_sp<SkImage> srcImage(src->asImage());
645     if (!srcImage) {
646         return;
647     }
648 
649     const SkMatrix totalMatrix = SkMatrix::Concat(this->ctm(), clipMatrix);
650     SkRect clipBounds;
651     totalMatrix.mapRect(&clipBounds, SkRect::Make(clipImage->bounds()));
652     const SkIRect srcBounds = srcImage->bounds().makeOffset(x, y);
653 
654     SkIRect maskBounds = fRCStack.rc().getBounds();
655     if (!maskBounds.intersect(clipBounds.roundOut()) || !maskBounds.intersect(srcBounds)) {
656         return;
657     }
658 
659     sk_sp<SkImage> mask;
660     SkMatrix maskMatrix, shaderMatrix;
661     SkTLazy<SkAutoDeviceClipRestore> autoClipRestore;
662 
663     SkMatrix totalInverse;
664     if (clipImage->isAlphaOnly() && totalMatrix.invert(&totalInverse)) {
665         // If the mask is already in A8 format, we can draw it directly
666         // (while compensating in the shader matrix).
667         mask = sk_ref_sp(clipImage);
668         maskMatrix = totalMatrix;
669         shaderMatrix = SkMatrix::Concat(totalInverse, SkMatrix::MakeTrans(x, y));
670 
671         // If the mask is not fully contained within the src layer, we must clip.
672         if (!srcBounds.contains(clipBounds)) {
673             autoClipRestore.init(this, srcBounds);
674         }
675 
676         maskBounds.offsetTo(0, 0);
677     } else {
678         // Otherwise, we convert the mask to A8 explicitly.
679         sk_sp<SkSurface> surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(maskBounds.width(),
680                                                                           maskBounds.height()));
681         SkCanvas* canvas = surf->getCanvas();
682         canvas->translate(-maskBounds.x(), -maskBounds.y());
683         canvas->concat(totalMatrix);
684         canvas->drawImage(clipImage, 0, 0);
685 
686         mask = surf->makeImageSnapshot();
687         maskMatrix = SkMatrix::I();
688         shaderMatrix = SkMatrix::MakeTrans(x - maskBounds.x(), y - maskBounds.y());
689     }
690 
691     SkAutoDeviceCTMRestore adctmr(this, maskMatrix);
692     paint.writable()->setShader(srcImage->makeShader(&shaderMatrix));
693     this->drawImageRect(mask.get(), nullptr,
694                         SkRect::MakeXYWH(maskBounds.x(), maskBounds.y(),
695                                          mask->width(), mask->height()),
696                         *paint, SkCanvas::kFast_SrcRectConstraint);
697 }
698 
makeSpecial(const SkBitmap & bitmap)699 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
700     return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
701 }
702 
makeSpecial(const SkImage * image)703 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
704     return SkSpecialImage::MakeFromImage(nullptr, SkIRect::MakeWH(image->width(), image->height()),
705                                          image->makeNonTextureImage());
706 }
707 
snapSpecial()708 sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() {
709     return this->makeSpecial(fBitmap);
710 }
711 
snapBackImage(const SkIRect & bounds)712 sk_sp<SkSpecialImage> SkBitmapDevice::snapBackImage(const SkIRect& bounds) {
713     return SkSpecialImage::CopyFromRaster(bounds, fBitmap, &this->surfaceProps());
714 }
715 
716 ///////////////////////////////////////////////////////////////////////////////
717 
makeSurface(const SkImageInfo & info,const SkSurfaceProps & props)718 sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
719     return SkSurface::MakeRaster(info, &props);
720 }
721 
getImageFilterCache()722 SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
723     SkImageFilterCache* cache = SkImageFilterCache::Get();
724     cache->ref();
725     return cache;
726 }
727 
728 ///////////////////////////////////////////////////////////////////////////////////////////////////
729 
onSave()730 void SkBitmapDevice::onSave() {
731     fRCStack.save();
732 }
733 
onRestore()734 void SkBitmapDevice::onRestore() {
735     fRCStack.restore();
736 }
737 
onClipRect(const SkRect & rect,SkClipOp op,bool aa)738 void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
739     fRCStack.clipRect(this->ctm(), rect, op, aa);
740 }
741 
onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)742 void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
743     fRCStack.clipRRect(this->ctm(), rrect, op, aa);
744 }
745 
onClipPath(const SkPath & path,SkClipOp op,bool aa)746 void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
747     fRCStack.clipPath(this->ctm(), path, op, aa);
748 }
749 
onClipRegion(const SkRegion & rgn,SkClipOp op)750 void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
751     SkIPoint origin = this->getOrigin();
752     SkRegion tmp;
753     const SkRegion* ptr = &rgn;
754     if (origin.fX | origin.fY) {
755         // translate from "global/canvas" coordinates to relative to this device
756         rgn.translate(-origin.fX, -origin.fY, &tmp);
757         ptr = &tmp;
758     }
759     fRCStack.clipRegion(*ptr, op);
760 }
761 
onSetDeviceClipRestriction(SkIRect * mutableClipRestriction)762 void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {
763     fRCStack.setDeviceClipRestriction(mutableClipRestriction);
764     if (!mutableClipRestriction->isEmpty()) {
765         SkRegion rgn(*mutableClipRestriction);
766         fRCStack.clipRegion(rgn, SkClipOp::kIntersect);
767     }
768 }
769 
onClipIsAA() const770 bool SkBitmapDevice::onClipIsAA() const {
771     const SkRasterClip& rc = fRCStack.rc();
772     return !rc.isEmpty() && rc.isAA();
773 }
774 
onAsRgnClip(SkRegion * rgn) const775 void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const {
776     const SkRasterClip& rc = fRCStack.rc();
777     if (rc.isAA()) {
778         rgn->setRect(rc.getBounds());
779     } else {
780         *rgn = rc.bwRgn();
781     }
782 }
783 
validateDevBounds(const SkIRect & drawClipBounds)784 void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) {
785 #ifdef SK_DEBUG
786     const SkIRect& stackBounds = fRCStack.rc().getBounds();
787     SkASSERT(drawClipBounds == stackBounds);
788 #endif
789 }
790 
onGetClipType() const791 SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const {
792     const SkRasterClip& rc = fRCStack.rc();
793     if (rc.isEmpty()) {
794         return kEmpty_ClipType;
795     } else if (rc.isRect()) {
796         return kRect_ClipType;
797     } else {
798         return kComplex_ClipType;
799     }
800 }
801