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