• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 The Android Open Source Project
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/SkCanvas.h"
9 
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkBlendMode.h"
13 #include "include/core/SkBlender.h"
14 #include "include/core/SkColorFilter.h"
15 #include "include/core/SkColorSpace.h"
16 #include "include/core/SkColorType.h"
17 #include "include/core/SkData.h"
18 #include "include/core/SkImage.h"
19 #include "include/core/SkImageFilter.h"
20 #include "include/core/SkMaskFilter.h"
21 #include "include/core/SkMesh.h"
22 #include "include/core/SkPath.h"
23 #include "include/core/SkPathEffect.h"
24 #include "include/core/SkPicture.h"
25 #include "include/core/SkPixmap.h"
26 #include "include/core/SkRRect.h"
27 #include "include/core/SkRSXform.h"
28 #include "include/core/SkRasterHandleAllocator.h"
29 #include "include/core/SkRegion.h"
30 #include "include/core/SkShader.h"
31 #include "include/core/SkSurface.h"
32 #include "include/core/SkTextBlob.h"
33 #include "include/core/SkTileMode.h"
34 #include "include/core/SkTypes.h"
35 #include "include/core/SkVertices.h"
36 #include "include/private/base/SkDebug.h"
37 #include "include/private/base/SkSafe32.h"
38 #include "include/private/base/SkSpan_impl.h"
39 #include "include/private/base/SkTPin.h"
40 #include "include/private/base/SkTemplates.h"
41 #include "include/private/base/SkTo.h"
42 #include "include/utils/SkNoDrawCanvas.h"
43 #include "src/base/SkMSAN.h"
44 #include "src/core/SkBitmapDevice.h"
45 #include "src/core/SkCanvasPriv.h"
46 #include "src/core/SkColorFilterBase.h"
47 #include "src/core/SkDevice.h"
48 #include "src/core/SkImageFilterTypes.h"
49 #include "src/core/SkImageFilter_Base.h"
50 #include "src/core/SkLatticeIter.h"
51 #include "src/core/SkMatrixPriv.h"
52 #include "src/core/SkMatrixUtils.h"
53 #include "src/core/SkPaintPriv.h"
54 #include "src/core/SkSpecialImage.h"
55 #include "src/core/SkSurfacePriv.h"
56 #include "src/core/SkTextBlobPriv.h"
57 #include "src/core/SkTraceEvent.h"
58 #include "src/core/SkVerticesPriv.h"
59 #include "src/image/SkSurface_Base.h"
60 #include "src/text/GlyphRun.h"
61 #include "src/utils/SkPatchUtils.h"
62 
63 #include <algorithm>
64 #include <atomic>
65 #include <functional>
66 #include <memory>
67 #include <new>
68 #include <optional>
69 #include <tuple>
70 #include <utility>
71 #include <vector>
72 
73 #if defined(SK_GANESH)
74 #include "include/gpu/GrDirectContext.h"
75 #include "include/gpu/GrRecordingContext.h"
76 #include "src/gpu/ganesh/Device_v1.h"
77 #include "src/utils/SkTestCanvas.h"
78 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
79 #   include "src/gpu/ganesh/GrRenderTarget.h"
80 #   include "src/gpu/ganesh/GrRenderTargetProxy.h"
81 #endif
82 #endif
83 
84 #if defined(SK_GRAPHITE)
85 #include "src/gpu/graphite/Device.h"
86 #endif
87 
88 #if (defined(SK_GANESH) || defined(SK_GRAPHITE))
89 #include "include/private/chromium/SkChromeRemoteGlyphCache.h"
90 #include "include/private/chromium/Slug.h"
91 #endif
92 
93 #define RETURN_ON_NULL(ptr)     do { if (nullptr == (ptr)) return; } while (0)
94 #define RETURN_ON_FALSE(pred)   do { if (!(pred)) return; } while (0)
95 
96 // This is a test: static_assert with no message is a c++17 feature,
97 // and std::max() is constexpr only since the c++14 stdlib.
98 static_assert(std::max(3,4) == 4);
99 
100 using Slug = sktext::gpu::Slug;
101 
102 ///////////////////////////////////////////////////////////////////////////////////////////////////
103 
104 /*
105  *  Return true if the drawing this rect would hit every pixels in the canvas.
106  *
107  *  Returns false if
108  *  - rect does not contain the canvas' bounds
109  *  - paint is not fill
110  *  - paint would blur or otherwise change the coverage of the rect
111  */
wouldOverwriteEntireSurface(const SkRect * rect,const SkPaint * paint,ShaderOverrideOpacity overrideOpacity) const112 bool SkCanvas::wouldOverwriteEntireSurface(const SkRect* rect, const SkPaint* paint,
113                                            ShaderOverrideOpacity overrideOpacity) const {
114     static_assert((int)SkPaintPriv::kNone_ShaderOverrideOpacity ==
115                   (int)kNone_ShaderOverrideOpacity,
116                   "need_matching_enums0");
117     static_assert((int)SkPaintPriv::kOpaque_ShaderOverrideOpacity ==
118                   (int)kOpaque_ShaderOverrideOpacity,
119                   "need_matching_enums1");
120     static_assert((int)SkPaintPriv::kNotOpaque_ShaderOverrideOpacity ==
121                   (int)kNotOpaque_ShaderOverrideOpacity,
122                   "need_matching_enums2");
123 
124     const SkISize size = this->getBaseLayerSize();
125     const SkRect bounds = SkRect::MakeIWH(size.width(), size.height());
126 
127     // if we're clipped at all, we can't overwrite the entire surface
128     {
129         const SkBaseDevice* base = this->baseDevice();
130         const SkBaseDevice* top = this->topDevice();
131         if (base != top) {
132             return false;   // we're in a saveLayer, so conservatively don't assume we'll overwrite
133         }
134         if (!base->clipIsWideOpen()) {
135             return false;
136         }
137     }
138 
139     if (rect) {
140         if (!this->getTotalMatrix().isScaleTranslate()) {
141             return false; // conservative
142         }
143 
144         SkRect devRect;
145         this->getTotalMatrix().mapRectScaleTranslate(&devRect, *rect);
146         if (!devRect.contains(bounds)) {
147             return false;
148         }
149     }
150 
151     if (paint) {
152         SkPaint::Style paintStyle = paint->getStyle();
153         if (!(paintStyle == SkPaint::kFill_Style ||
154               paintStyle == SkPaint::kStrokeAndFill_Style)) {
155             return false;
156         }
157         if (paint->getMaskFilter() || paint->getPathEffect() || paint->getImageFilter()) {
158             return false; // conservative
159         }
160     }
161     return SkPaintPriv::Overwrites(paint, (SkPaintPriv::ShaderOverrideOpacity)overrideOpacity);
162 }
163 
164 ///////////////////////////////////////////////////////////////////////////////////////////////////
165 
166 // experimental for faster tiled drawing...
167 //#define SK_TRACE_SAVERESTORE
168 
169 #ifdef SK_TRACE_SAVERESTORE
170     static int gLayerCounter;
inc_layer()171     static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); }
dec_layer()172     static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); }
173 
174     static int gRecCounter;
inc_rec()175     static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); }
dec_rec()176     static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); }
177 
178     static int gCanvasCounter;
inc_canvas()179     static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); }
dec_canvas()180     static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); }
181 #else
182     #define inc_layer()
183     #define dec_layer()
184     #define inc_rec()
185     #define dec_rec()
186     #define inc_canvas()
187     #define dec_canvas()
188 #endif
189 
predrawNotify(bool willOverwritesEntireSurface)190 bool SkCanvas::predrawNotify(bool willOverwritesEntireSurface) {
191     if (fSurfaceBase) {
192         if (!fSurfaceBase->aboutToDraw(willOverwritesEntireSurface
193                                        ? SkSurface::kDiscard_ContentChangeMode
194                                        : SkSurface::kRetain_ContentChangeMode)) {
195             return false;
196         }
197     }
198     return true;
199 }
200 
predrawNotify(const SkRect * rect,const SkPaint * paint,ShaderOverrideOpacity overrideOpacity)201 bool SkCanvas::predrawNotify(const SkRect* rect, const SkPaint* paint,
202                              ShaderOverrideOpacity overrideOpacity) {
203     if (fSurfaceBase) {
204         SkSurface::ContentChangeMode mode = SkSurface::kRetain_ContentChangeMode;
205         // Since willOverwriteAllPixels() may not be complete free to call, we only do so if
206         // there is an outstanding snapshot, since w/o that, there will be no copy-on-write
207         // and therefore we don't care which mode we're in.
208         //
209         if (fSurfaceBase->outstandingImageSnapshot()) {
210             if (this->wouldOverwriteEntireSurface(rect, paint, overrideOpacity)) {
211                 mode = SkSurface::kDiscard_ContentChangeMode;
212             }
213         }
214         if (!fSurfaceBase->aboutToDraw(mode)) {
215             return false;
216         }
217     }
218     return true;
219 }
220 
221 ///////////////////////////////////////////////////////////////////////////////
222 
Layer(sk_sp<SkBaseDevice> device,sk_sp<SkImageFilter> imageFilter,const SkPaint & paint)223 SkCanvas::Layer::Layer(sk_sp<SkBaseDevice> device,
224                        sk_sp<SkImageFilter> imageFilter,
225                        const SkPaint& paint)
226         : fDevice(std::move(device))
227         , fImageFilter(std::move(imageFilter))
228         , fPaint(paint)
229         , fDiscard(false) {
230     SkASSERT(fDevice);
231     // Any image filter should have been pulled out and stored in 'imageFilter' so that 'paint'
232     // can be used as-is to draw the result of the filter to the dst device.
233     SkASSERT(!fPaint.getImageFilter());
234 }
235 
BackImage(sk_sp<SkSpecialImage> img,SkIPoint loc)236 SkCanvas::BackImage::BackImage(sk_sp<SkSpecialImage> img, SkIPoint loc)
237                                :fImage(img), fLoc(loc) {}
238 SkCanvas::BackImage::BackImage(const BackImage&) = default;
239 SkCanvas::BackImage::BackImage(BackImage&&) = default;
240 SkCanvas::BackImage& SkCanvas::BackImage::operator=(const BackImage&) = default;
241 SkCanvas::BackImage::~BackImage() = default;
242 
MCRec(SkBaseDevice * device)243 SkCanvas::MCRec::MCRec(SkBaseDevice* device) : fDevice(device) {
244     SkASSERT(fDevice);
245     inc_rec();
246 }
247 
MCRec(const MCRec * prev)248 SkCanvas::MCRec::MCRec(const MCRec* prev) : fDevice(prev->fDevice), fMatrix(prev->fMatrix) {
249     SkASSERT(fDevice);
250     inc_rec();
251 }
252 
~MCRec()253 SkCanvas::MCRec::~MCRec() { dec_rec(); }
254 
newLayer(sk_sp<SkBaseDevice> layerDevice,sk_sp<SkImageFilter> filter,const SkPaint & restorePaint)255 void SkCanvas::MCRec::newLayer(sk_sp<SkBaseDevice> layerDevice,
256                                sk_sp<SkImageFilter> filter,
257                                const SkPaint& restorePaint) {
258     SkASSERT(!fBackImage);
259     fLayer = std::make_unique<Layer>(std::move(layerDevice), std::move(filter), restorePaint);
260     fDevice = fLayer->fDevice.get();
261 }
262 
reset(SkBaseDevice * device)263 void SkCanvas::MCRec::reset(SkBaseDevice* device) {
264     SkASSERT(!fLayer);
265     SkASSERT(device);
266     SkASSERT(fDeferredSaveCount == 0);
267     fDevice = device;
268     fMatrix.setIdentity();
269 }
270 
271 class SkCanvas::AutoUpdateQRBounds {
272 public:
AutoUpdateQRBounds(SkCanvas * canvas)273     explicit AutoUpdateQRBounds(SkCanvas* canvas) : fCanvas(canvas) {
274         // pre-condition, fQuickRejectBounds and other state should be valid before anything
275         // modifies the device's clip.
276         fCanvas->validateClip();
277     }
~AutoUpdateQRBounds()278     ~AutoUpdateQRBounds() {
279         fCanvas->fQuickRejectBounds = fCanvas->computeDeviceClipBounds();
280         // post-condition, we should remain valid after re-computing the bounds
281         fCanvas->validateClip();
282     }
283 
284 private:
285     SkCanvas* fCanvas;
286 
287     AutoUpdateQRBounds(AutoUpdateQRBounds&&) = delete;
288     AutoUpdateQRBounds(const AutoUpdateQRBounds&) = delete;
289     AutoUpdateQRBounds& operator=(AutoUpdateQRBounds&&) = delete;
290     AutoUpdateQRBounds& operator=(const AutoUpdateQRBounds&) = delete;
291 };
292 
293 /////////////////////////////////////////////////////////////////////////////
294 // Attempts to convert an image filter to its equivalent color filter, which if possible, modifies
295 // the paint to compose the image filter's color filter into the paint's color filter slot.
296 // Returns true if the paint has been modified.
297 // Requires the paint to have an image filter and the copy-on-write be initialized.
image_to_color_filter(SkPaint * paint)298 static bool image_to_color_filter(SkPaint* paint) {
299     SkASSERT(SkToBool(paint) && paint->getImageFilter());
300 
301     SkColorFilter* imgCFPtr;
302     if (!paint->getImageFilter()->asAColorFilter(&imgCFPtr)) {
303         return false;
304     }
305     sk_sp<SkColorFilter> imgCF(imgCFPtr);
306 
307     SkColorFilter* paintCF = paint->getColorFilter();
308     if (paintCF) {
309         // The paint has both a colorfilter(paintCF) and an imagefilter-that-is-a-colorfilter(imgCF)
310         // and we need to combine them into a single colorfilter.
311         imgCF = imgCF->makeComposed(sk_ref_sp(paintCF));
312     }
313 
314     paint->setColorFilter(std::move(imgCF));
315     paint->setImageFilter(nullptr);
316     return true;
317 }
318 
319 /**
320  *  We implement ImageFilters for a given draw by creating a layer, then applying the
321  *  imagefilter to the pixels of that layer (its backing surface/image), and then
322  *  we call restore() to xfer that layer to the main canvas.
323  *
324  *  1. SaveLayer (with a paint containing the current imagefilter and xfermode)
325  *  2. Generate the src pixels:
326  *      Remove the imagefilter and the xfermode from the paint that we (AutoDrawLooper)
327  *      return (fPaint). We then draw the primitive (using srcover) into a cleared
328  *      buffer/surface.
329  *  3. Restore the layer created in #1
330  *      The imagefilter is passed the buffer/surface from the layer (now filled with the
331  *      src pixels of the primitive). It returns a new "filtered" buffer, which we
332  *      draw onto the previous layer using the xfermode from the original paint.
333  */
334 class AutoLayerForImageFilter {
335 public:
336     // "rawBounds" is the original bounds of the primitive about to be drawn, unmodified by the
337     // paint. It's used to determine the size of the offscreen layer for filters.
338     // If null, the clip will be used instead.
339     //
340     // Draw functions should use layer->paint() instead of the passed-in paint.
AutoLayerForImageFilter(SkCanvas * canvas,const SkPaint & paint,const SkRect * rawBounds=nullptr)341     AutoLayerForImageFilter(SkCanvas* canvas,
342                             const SkPaint& paint,
343                             const SkRect* rawBounds = nullptr)
344             : fPaint(paint)
345             , fCanvas(canvas)
346             , fTempLayerForImageFilter(false) {
347         SkDEBUGCODE(fSaveCount = canvas->getSaveCount();)
348 
349         if (fPaint.getImageFilter() && !image_to_color_filter(&fPaint)) {
350             // The draw paint has an image filter that couldn't be simplified to an equivalent
351             // color filter, so we have to inject an automatic saveLayer().
352             SkPaint restorePaint;
353             restorePaint.setImageFilter(fPaint.refImageFilter());
354             restorePaint.setBlender(fPaint.refBlender());
355 
356             // Remove the restorePaint fields from our "working" paint
357             fPaint.setImageFilter(nullptr);
358             fPaint.setBlendMode(SkBlendMode::kSrcOver);
359 
360             SkRect storage;
361             if (rawBounds && fPaint.canComputeFastBounds()) {
362                 // Make rawBounds include all paint outsets except for those due to image filters.
363                 // At this point, fPaint's image filter has been moved to 'restorePaint'.
364                 SkASSERT(!fPaint.getImageFilter());
365                 rawBounds = &fPaint.computeFastBounds(*rawBounds, &storage);
366             }
367 
368             canvas->fSaveCount += 1;
369             (void)canvas->internalSaveLayer(SkCanvas::SaveLayerRec(rawBounds, &restorePaint),
370                                             SkCanvas::kFullLayer_SaveLayerStrategy);
371             fTempLayerForImageFilter = true;
372         }
373     }
374 
375     AutoLayerForImageFilter(const AutoLayerForImageFilter&) = delete;
376     AutoLayerForImageFilter& operator=(const AutoLayerForImageFilter&) = delete;
377     AutoLayerForImageFilter(AutoLayerForImageFilter&&) = default;
378     AutoLayerForImageFilter& operator=(AutoLayerForImageFilter&&) = default;
379 
~AutoLayerForImageFilter()380     ~AutoLayerForImageFilter() {
381         if (fTempLayerForImageFilter) {
382             fCanvas->fSaveCount -= 1;
383             fCanvas->internalRestore();
384         }
385         SkASSERT(fCanvas->getSaveCount() == fSaveCount);
386     }
387 
paint() const388     const SkPaint& paint() const { return fPaint; }
389 
390 private:
391     SkPaint         fPaint;
392     SkCanvas*       fCanvas;
393     bool            fTempLayerForImageFilter;
394 
395     SkDEBUGCODE(int fSaveCount;)
396 };
397 
aboutToDraw(SkCanvas * canvas,const SkPaint & paint,const SkRect * rawBounds,CheckForOverwrite checkOverwrite,ShaderOverrideOpacity overrideOpacity)398 std::optional<AutoLayerForImageFilter> SkCanvas::aboutToDraw(
399     SkCanvas* canvas,
400     const SkPaint& paint,
401     const SkRect* rawBounds,
402     CheckForOverwrite checkOverwrite,
403     ShaderOverrideOpacity overrideOpacity)
404 {
405     if (checkOverwrite == CheckForOverwrite::kYes) {
406         if (!this->predrawNotify(rawBounds, &paint, overrideOpacity)) {
407             return std::nullopt;
408         }
409     } else {
410         if (!this->predrawNotify()) {
411             return std::nullopt;
412         }
413     }
414     return std::optional<AutoLayerForImageFilter>(std::in_place, canvas, paint, rawBounds);
415 }
416 
417 ////////////////////////////////////////////////////////////////////////////
418 
resetForNextPicture(const SkIRect & bounds)419 void SkCanvas::resetForNextPicture(const SkIRect& bounds) {
420     this->restoreToCount(1);
421 
422     // We're peering through a lot of structs here.  Only at this scope do we
423     // know that the device is a SkNoPixelsDevice.
424     SkASSERT(fBaseDevice->isNoPixelsDevice());
425     static_cast<SkNoPixelsDevice*>(fBaseDevice.get())->resetForNextPicture(bounds);
426     fMCRec->reset(fBaseDevice.get());
427     fQuickRejectBounds = this->computeDeviceClipBounds();
428 }
429 
init(sk_sp<SkBaseDevice> device)430 void SkCanvas::init(sk_sp<SkBaseDevice> device) {
431     // SkCanvas.h declares internal storage for the hidden struct MCRec, and this
432     // assert ensure it's sufficient. <= is used because the struct has pointer fields, so the
433     // declared size is an upper bound across architectures. When the size is smaller, more stack
434     static_assert(sizeof(MCRec) <= kMCRecSize);
435 
436     if (!device) {
437         device = sk_make_sp<SkNoPixelsDevice>(SkIRect::MakeEmpty(), fProps);
438     }
439 
440     // From this point on, SkCanvas will always have a device
441     SkASSERT(device);
442 
443     fSaveCount = 1;
444     fMCRec = new (fMCStack.push_back()) MCRec(device.get());
445 
446     // The root device and the canvas should always have the same pixel geometry
447     SkASSERT(fProps.pixelGeometry() == device->surfaceProps().pixelGeometry());
448 
449     fSurfaceBase = nullptr;
450     fBaseDevice = std::move(device);
451     fScratchGlyphRunBuilder = std::make_unique<sktext::GlyphRunBuilder>();
452     fQuickRejectBounds = this->computeDeviceClipBounds();
453 }
454 
SkCanvas()455 SkCanvas::SkCanvas() : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) {
456     inc_canvas();
457     this->init(nullptr);
458 }
459 
SkCanvas(int width,int height,const SkSurfaceProps * props)460 SkCanvas::SkCanvas(int width, int height, const SkSurfaceProps* props)
461         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
462         , fProps(SkSurfacePropsCopyOrDefault(props)) {
463     inc_canvas();
464     this->init(sk_make_sp<SkNoPixelsDevice>(
465             SkIRect::MakeWH(std::max(width, 0), std::max(height, 0)), fProps));
466 }
467 
SkCanvas(const SkIRect & bounds)468 SkCanvas::SkCanvas(const SkIRect& bounds)
469         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) {
470     inc_canvas();
471 
472     SkIRect r = bounds.isEmpty() ? SkIRect::MakeEmpty() : bounds;
473     this->init(sk_make_sp<SkNoPixelsDevice>(r, fProps));
474 }
475 
SkCanvas(sk_sp<SkBaseDevice> device)476 SkCanvas::SkCanvas(sk_sp<SkBaseDevice> device)
477         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
478         , fProps(device->surfaceProps()) {
479     inc_canvas();
480 
481     this->init(device);
482 }
483 
SkCanvas(const SkBitmap & bitmap,const SkSurfaceProps & props)484 SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props)
485         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)), fProps(props) {
486     inc_canvas();
487 
488     sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps));
489     this->init(device);
490 }
491 
SkCanvas(const SkBitmap & bitmap,std::unique_ptr<SkRasterHandleAllocator> alloc,SkRasterHandleAllocator::Handle hndl,const SkSurfaceProps * props)492 SkCanvas::SkCanvas(const SkBitmap& bitmap,
493                    std::unique_ptr<SkRasterHandleAllocator> alloc,
494                    SkRasterHandleAllocator::Handle hndl,
495                    const SkSurfaceProps* props)
496         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
497         , fProps(SkSurfacePropsCopyOrDefault(props))
498         , fAllocator(std::move(alloc)) {
499     inc_canvas();
500 
501     sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps, hndl));
502     this->init(device);
503 }
504 
SkCanvas(const SkBitmap & bitmap)505 SkCanvas::SkCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap, nullptr, nullptr, nullptr) {}
506 
507 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
SkCanvas(const SkBitmap & bitmap,ColorBehavior)508 SkCanvas::SkCanvas(const SkBitmap& bitmap, ColorBehavior)
509         : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) {
510     inc_canvas();
511 
512     SkBitmap tmp(bitmap);
513     *const_cast<SkImageInfo*>(&tmp.info()) = tmp.info().makeColorSpace(nullptr);
514     sk_sp<SkBaseDevice> device(new SkBitmapDevice(tmp, fProps));
515     this->init(device);
516 }
517 #endif
518 
~SkCanvas()519 SkCanvas::~SkCanvas() {
520     // Mark all pending layers to be discarded during restore (rather than drawn)
521     SkDeque::Iter iter(fMCStack, SkDeque::Iter::kFront_IterStart);
522     for (;;) {
523         MCRec* rec = (MCRec*)iter.next();
524         if (!rec) {
525             break;
526         }
527         if (rec->fLayer) {
528             rec->fLayer->fDiscard = true;
529         }
530     }
531 
532     // free up the contents of our deque
533     this->restoreToCount(1);    // restore everything but the last
534     this->internalRestore();    // restore the last, since we're going away
535 
536     dec_canvas();
537 }
538 
539 ///////////////////////////////////////////////////////////////////////////////
540 
flush()541 void SkCanvas::flush() {
542     this->onFlush();
543 }
544 
onFlush()545 void SkCanvas::onFlush() {
546 #if defined(SK_GANESH)
547     auto dContext = GrAsDirectContext(this->recordingContext());
548 
549     if (dContext) {
550         dContext->flushAndSubmit();
551     }
552 #endif
553 }
554 
getSurface() const555 SkSurface* SkCanvas::getSurface() const {
556     return fSurfaceBase;
557 }
558 
getBaseLayerSize() const559 SkISize SkCanvas::getBaseLayerSize() const {
560     return this->baseDevice()->imageInfo().dimensions();
561 }
562 
topDevice() const563 SkBaseDevice* SkCanvas::topDevice() const {
564     SkASSERT(fMCRec->fDevice);
565     return fMCRec->fDevice;
566 }
567 
readPixels(const SkPixmap & pm,int x,int y)568 bool SkCanvas::readPixels(const SkPixmap& pm, int x, int y) {
569     return pm.addr() && this->baseDevice()->readPixels(pm, x, y);
570 }
571 
readPixels(const SkImageInfo & dstInfo,void * dstP,size_t rowBytes,int x,int y)572 bool SkCanvas::readPixels(const SkImageInfo& dstInfo, void* dstP, size_t rowBytes, int x, int y) {
573     return this->readPixels({ dstInfo, dstP, rowBytes}, x, y);
574 }
575 
readPixels(const SkBitmap & bm,int x,int y)576 bool SkCanvas::readPixels(const SkBitmap& bm, int x, int y) {
577     SkPixmap pm;
578     return bm.peekPixels(&pm) && this->readPixels(pm, x, y);
579 }
580 
writePixels(const SkBitmap & bitmap,int x,int y)581 bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) {
582     SkPixmap pm;
583     if (bitmap.peekPixels(&pm)) {
584         return this->writePixels(pm.info(), pm.addr(), pm.rowBytes(), x, y);
585     }
586     return false;
587 }
588 
writePixels(const SkImageInfo & srcInfo,const void * pixels,size_t rowBytes,int x,int y)589 bool SkCanvas::writePixels(const SkImageInfo& srcInfo, const void* pixels, size_t rowBytes,
590                            int x, int y) {
591     SkBaseDevice* device = this->baseDevice();
592 
593     // This check gives us an early out and prevents generation ID churn on the surface.
594     // This is purely optional: it is a subset of the checks performed by SkWritePixelsRec.
595     SkIRect srcRect = SkIRect::MakeXYWH(x, y, srcInfo.width(), srcInfo.height());
596     if (!srcRect.intersect({0, 0, device->width(), device->height()})) {
597         return false;
598     }
599 
600     // Tell our owning surface to bump its generation ID.
601     const bool completeOverwrite = srcRect.size() == device->imageInfo().dimensions();
602     if (!this->predrawNotify(completeOverwrite)) {
603         return false;
604     }
605 
606     // This can still fail, most notably in the case of a invalid color type or alpha type
607     // conversion.  We could pull those checks into this function and avoid the unnecessary
608     // generation ID bump.  But then we would be performing those checks twice, since they
609     // are also necessary at the bitmap/pixmap entry points.
610     return device->writePixels({srcInfo, pixels, rowBytes}, x, y);
611 }
612 
613 //////////////////////////////////////////////////////////////////////////////
614 
checkForDeferredSave()615 void SkCanvas::checkForDeferredSave() {
616     if (fMCRec->fDeferredSaveCount > 0) {
617         this->doSave();
618     }
619 }
620 
getSaveCount() const621 int SkCanvas::getSaveCount() const {
622 #ifdef SK_DEBUG
623     int count = 0;
624     SkDeque::Iter iter(fMCStack, SkDeque::Iter::kFront_IterStart);
625     for (;;) {
626         const MCRec* rec = (const MCRec*)iter.next();
627         if (!rec) {
628             break;
629         }
630         count += 1 + rec->fDeferredSaveCount;
631     }
632     SkASSERT(count == fSaveCount);
633 #endif
634     return fSaveCount;
635 }
636 
save()637 int SkCanvas::save() {
638     fSaveCount += 1;
639     fMCRec->fDeferredSaveCount += 1;
640     return this->getSaveCount() - 1;  // return our prev value
641 }
642 
doSave()643 void SkCanvas::doSave() {
644     this->willSave();
645 
646     SkASSERT(fMCRec->fDeferredSaveCount > 0);
647     fMCRec->fDeferredSaveCount -= 1;
648     this->internalSave();
649 }
650 
restore()651 void SkCanvas::restore() {
652     if (fMCRec->fDeferredSaveCount > 0) {
653         SkASSERT(fSaveCount > 1);
654         fSaveCount -= 1;
655         fMCRec->fDeferredSaveCount -= 1;
656     } else {
657         // check for underflow
658         if (fMCStack.count() > 1) {
659             this->willRestore();
660             SkASSERT(fSaveCount > 1);
661             fSaveCount -= 1;
662             this->internalRestore();
663             this->didRestore();
664         }
665     }
666 }
667 
restoreToCount(int count)668 void SkCanvas::restoreToCount(int count) {
669     // safety check
670     if (count < 1) {
671         count = 1;
672     }
673 
674     int n = this->getSaveCount() - count;
675     for (int i = 0; i < n; ++i) {
676         this->restore();
677     }
678 }
679 
internalSave()680 void SkCanvas::internalSave() {
681     fMCRec = new (fMCStack.push_back()) MCRec(fMCRec);
682 
683     this->topDevice()->save();
684 }
685 
saveLayer(const SkRect * bounds,const SkPaint * paint)686 int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) {
687     return this->saveLayer(SaveLayerRec(bounds, paint, 0));
688 }
689 
saveLayer(const SaveLayerRec & rec)690 int SkCanvas::saveLayer(const SaveLayerRec& rec) {
691     TRACE_EVENT0("skia", TRACE_FUNC);
692     if (rec.fPaint && rec.fPaint->nothingToDraw()) {
693         // no need for the layer (or any of the draws until the matching restore()
694         this->save();
695         this->clipRect({0,0,0,0});
696     } else {
697         SaveLayerStrategy strategy = this->getSaveLayerStrategy(rec);
698         fSaveCount += 1;
699         this->internalSaveLayer(rec, strategy);
700     }
701     return this->getSaveCount() - 1;
702 }
703 
only_axis_aligned_saveBehind(const SkRect * bounds)704 int SkCanvas::only_axis_aligned_saveBehind(const SkRect* bounds) {
705     if (bounds && !this->getLocalClipBounds().intersects(*bounds)) {
706         // Assuming clips never expand, if the request bounds is outside of the current clip
707         // there is no need to copy/restore the area, so just devolve back to a regular save.
708         this->save();
709     } else {
710         bool doTheWork = this->onDoSaveBehind(bounds);
711         fSaveCount += 1;
712         this->internalSave();
713         if (doTheWork) {
714             this->internalSaveBehind(bounds);
715         }
716     }
717     return this->getSaveCount() - 1;
718 }
719 
720 // In our current design/features, we should never have a layer (src) in a different colorspace
721 // than its parent (dst), so we assert that here. This is called out from other asserts, in case
722 // we add some feature in the future to allow a given layer/imagefilter to operate in a specific
723 // colorspace.
check_drawdevice_colorspaces(SkColorSpace * src,SkColorSpace * dst)724 static void check_drawdevice_colorspaces(SkColorSpace* src, SkColorSpace* dst) {
725     SkASSERT(src == dst);
726 }
727 
728 // Helper function to compute the center reference point used for scale decomposition under
729 // non-linear transformations.
compute_decomposition_center(const SkMatrix & dstToLocal,const skif::ParameterSpace<SkRect> * contentBounds,const skif::DeviceSpace<SkIRect> & targetOutput)730 static skif::ParameterSpace<SkPoint> compute_decomposition_center(
731         const SkMatrix& dstToLocal,
732         const skif::ParameterSpace<SkRect>* contentBounds,
733         const skif::DeviceSpace<SkIRect>& targetOutput) {
734     // Will use the inverse and center of the device bounds if the content bounds aren't provided.
735     SkRect rect = contentBounds ? SkRect(*contentBounds) : SkRect::Make(SkIRect(targetOutput));
736     SkPoint center = {rect.centerX(), rect.centerY()};
737     if (!contentBounds) {
738         // Theoretically, the inverse transform could put center's homogeneous coord behind W = 0,
739         // but that case is handled automatically in Mapping::decomposeCTM later.
740         dstToLocal.mapPoints(&center, 1);
741     }
742 
743     return skif::ParameterSpace<SkPoint>(center);
744 }
745 
746 // Compute suitable transformations and layer bounds for a new layer that will be used as the source
747 // input into 'filter'  before being drawn into 'dst' via the returned skif::Mapping.
748 // Null filters are permitted and act as the identity. The returned mapping will be compatible with
749 // the image filter.
750 //
751 // Returns an empty rect if the layer wouldn't draw anything after filtering.
get_layer_mapping_and_bounds(const SkImageFilter * filter,const SkMatrix & localToDst,const skif::DeviceSpace<SkIRect> & targetOutput,const skif::ParameterSpace<SkRect> * contentBounds=nullptr,bool mustCoverDst=true,SkScalar scaleFactor=1.0f)752 static std::pair<skif::Mapping, skif::LayerSpace<SkIRect>> get_layer_mapping_and_bounds(
753         const SkImageFilter* filter,
754         const SkMatrix& localToDst,
755         const skif::DeviceSpace<SkIRect>& targetOutput,
756         const skif::ParameterSpace<SkRect>* contentBounds = nullptr,
757         bool mustCoverDst = true,
758         SkScalar scaleFactor = 1.0f) {
759     auto failedMapping = []() {
760         return std::make_pair<skif::Mapping, skif::LayerSpace<SkIRect>>(
761                 {}, skif::LayerSpace<SkIRect>::Empty());
762     };
763 
764     SkMatrix dstToLocal;
765     if (!localToDst.isFinite() ||
766         !localToDst.invert(&dstToLocal)) {
767         return failedMapping();
768     }
769 
770     skif::ParameterSpace<SkPoint> center =
771             compute_decomposition_center(dstToLocal, contentBounds, targetOutput);
772     // *after* possibly getting a representative point from the provided content bounds, it might
773     // be necessary to discard the bounds for subsequent layer calculations.
774     if (mustCoverDst) {
775         contentBounds = nullptr;
776     }
777 
778     // Determine initial mapping and a reasonable maximum dimension to prevent layer-to-device
779     // transforms with perspective and skew from triggering excessive buffer allocations.
780     skif::Mapping mapping;
781     if (!mapping.decomposeCTM(localToDst, filter, center)) {
782         return failedMapping();
783     }
784     // Push scale factor into layer matrix and device matrix (net no change, but the layer will have
785     // its resolution adjusted in comparison to the final device).
786     if (scaleFactor != 1.0f &&
787         !mapping.adjustLayerSpace(SkMatrix::Scale(scaleFactor, scaleFactor))) {
788         return failedMapping();
789     }
790 
791     // Perspective and skew could exceed this since mapping.deviceToLayer(targetOutput) is
792     // theoretically unbounded under those conditions. Under a 45 degree rotation, a layer needs to
793     // be 2X larger per side of the prior device in order to fully cover it. We use the max of that
794     // and 2048 for a reasonable upper limit (this allows small layers under extreme transforms to
795     // use more relative resolution than a larger layer).
796     static const int kMinDimThreshold = 2048;
797     int maxLayerDim = std::max(Sk64_pin_to_s32(2 * std::max(SkIRect(targetOutput).width64(),
798                                                             SkIRect(targetOutput).height64())),
799                                kMinDimThreshold);
800 
801     skif::LayerSpace<SkIRect> layerBounds;
802     if (filter) {
803         layerBounds = as_IFB(filter)->getInputBounds(mapping, targetOutput, contentBounds);
804         // When a filter is involved, the layer size may be larger than the default maxLayerDim due
805         // to required inputs for filters (e.g. a displacement map with a large radius).
806         if (layerBounds.width() > maxLayerDim || layerBounds.height() > maxLayerDim) {
807             skif::Mapping idealMapping{mapping.layerMatrix()};
808             auto idealLayerBounds = as_IFB(filter)->getInputBounds(idealMapping, targetOutput,
809                                                                    contentBounds);
810             maxLayerDim = std::max(std::max(idealLayerBounds.width(), idealLayerBounds.height()),
811                                    maxLayerDim);
812         }
813     } else {
814         layerBounds = mapping.deviceToLayer(targetOutput);
815         if (contentBounds) {
816             // For better or for worse, user bounds currently act as a hard clip on the layer's
817             // extent (i.e., they implement the CSS filter-effects 'filter region' feature).
818             skif::LayerSpace<SkIRect> knownBounds = mapping.paramToLayer(*contentBounds).roundOut();
819             if (!layerBounds.intersect(knownBounds)) {
820                 return failedMapping();
821             }
822         }
823     }
824 
825     if (layerBounds.width() > maxLayerDim || layerBounds.height() > maxLayerDim) {
826         skif::LayerSpace<SkIRect> newLayerBounds(
827                 SkIRect::MakeWH(std::min(layerBounds.width(), maxLayerDim),
828                                 std::min(layerBounds.height(), maxLayerDim)));
829         SkMatrix adjust = SkMatrix::MakeRectToRect(SkRect::Make(SkIRect(layerBounds)),
830                                                    SkRect::Make(SkIRect(newLayerBounds)),
831                                                    SkMatrix::kFill_ScaleToFit);
832         if (!mapping.adjustLayerSpace(adjust)) {
833             return failedMapping();
834         } else {
835             layerBounds = newLayerBounds;
836         }
837     }
838 
839     return {mapping, layerBounds};
840 }
841 
842 // Ideally image filters operate in the dst color type, but if there is insufficient alpha bits
843 // we move some bits from color channels into the alpha channel since that can greatly improve
844 // the quality of blurs and other filters.
image_filter_color_type(SkImageInfo dstInfo)845 static SkColorType image_filter_color_type(SkImageInfo dstInfo) {
846     if (dstInfo.bytesPerPixel() <= 4 &&
847         dstInfo.colorType() != kRGBA_8888_SkColorType &&
848         dstInfo.colorType() != kBGRA_8888_SkColorType) {
849         // "Upgrade" A8, G8, 565, 4444, 1010102, 101010x, and 888x to 8888
850         return kN32_SkColorType;
851     } else {
852         return dstInfo.colorType();
853     }
854 }
855 
draw_layer_as_sprite(const SkMatrix & matrix,const SkISize & size)856 static bool draw_layer_as_sprite(const SkMatrix& matrix, const SkISize& size) {
857     // Assume anti-aliasing and highest valid filter mode (linear) for drawing layers and image
858     // filters. If the layer can be drawn as a sprite, these can be downgraded.
859     SkPaint paint;
860     paint.setAntiAlias(true);
861     SkSamplingOptions sampling{SkFilterMode::kLinear};
862     return SkTreatAsSprite(matrix, size, sampling, paint.isAntiAlias());
863 }
864 
internalDrawDeviceWithFilter(SkBaseDevice * src,SkBaseDevice * dst,const SkImageFilter * filter,const SkPaint & paint,DeviceCompatibleWithFilter compat,SkScalar scaleFactor)865 void SkCanvas::internalDrawDeviceWithFilter(SkBaseDevice* src,
866                                             SkBaseDevice* dst,
867                                             const SkImageFilter* filter,
868                                             const SkPaint& paint,
869                                             DeviceCompatibleWithFilter compat,
870                                             SkScalar scaleFactor) {
871     check_drawdevice_colorspaces(dst->imageInfo().colorSpace(),
872                                  src->imageInfo().colorSpace());
873     sk_sp<SkColorSpace> filterColorSpace = dst->imageInfo().refColorSpace(); // == src.refColorSpace
874 
875     // 'filterColorType' ends up being the actual color type of the layer, so image filtering is
876     // effectively done in the layer's format. We get there in a roundabout way due to handling both
877     // regular and backdrop filters:
878     //  - For regular filters, 'src' is the layer and 'dst' is the parent device. But the layer
879     //    was constructed with a color type equal to image_filter_color_type(dst), so this matches
880     //    the layer.
881     //  - For backdrop filters, 'src' is the parent device and 'dst' is the layer, which was already
882     //    constructed as image_filter_color_type(src). Calling image_filter_color_type twice does
883     //    not change the color type, so it remains the color type of the layer.
884     const SkColorType filterColorType = image_filter_color_type(dst->imageInfo());
885 
886     // 'filter' sees the src device's buffer as the implicit input image, and processes the image
887     // in this device space (referred to as the "layer" space). However, the filter
888     // parameters need to respect the current matrix, which is not necessarily the local matrix that
889     // was set on 'src' (e.g. because we've popped src off the stack already).
890     // TODO (michaelludwig): Stay in SkM44 once skif::Mapping supports SkM44 instead of SkMatrix.
891     SkMatrix localToSrc = (src->globalToDevice() * fMCRec->fMatrix).asM33();
892     SkISize srcDims = src->imageInfo().dimensions();
893 
894     // Whether or not we need to make a transformed tmp image from 'src', and what that transform is
895     bool needsIntermediateImage = false;
896     SkMatrix srcToIntermediate;
897 
898     skif::Mapping mapping;
899     skif::LayerSpace<SkIRect> requiredInput;
900     if (compat == DeviceCompatibleWithFilter::kYes) {
901         // Just use the relative transform from src to dst and the src's whole image, since
902         // internalSaveLayer should have already determined what was necessary. We explicitly
903         // construct the inverse (dst->src) to avoid the case where src's and dst's coord transforms
904         // were individually invertible by SkM44::invert() but their product is considered not
905         // invertible by SkMatrix::invert(). When this happens the matrices are already poorly
906         // conditioned so getRelativeTransform() gives us something reasonable.
907         SkASSERT(scaleFactor == 1.0f);
908         mapping = skif::Mapping(src->getRelativeTransform(*dst),
909                                 dst->getRelativeTransform(*src),
910                                 localToSrc);
911         requiredInput = skif::LayerSpace<SkIRect>(SkIRect::MakeSize(srcDims));
912         SkASSERT(!requiredInput.isEmpty());
913     } else {
914         // Compute the image filter mapping by decomposing the local->device matrix of dst and
915         // re-determining the required input.
916         std::tie(mapping, requiredInput) = get_layer_mapping_and_bounds(
917                 filter, dst->localToDevice(), skif::DeviceSpace<SkIRect>(dst->devClipBounds()),
918                 nullptr, true, SkTPin(scaleFactor, 0.f, 1.f));
919         if (requiredInput.isEmpty()) {
920             return;
921         }
922 
923         // The above mapping transforms from local to dst's device space, where the layer space
924         // represents the intermediate buffer. Now we need to determine the transform from src to
925         // intermediate to prepare the input to the filter.
926         if (!localToSrc.invert(&srcToIntermediate)) {
927             return;
928         }
929         srcToIntermediate.postConcat(mapping.layerMatrix());
930         if (draw_layer_as_sprite(srcToIntermediate, srcDims)) {
931             // src differs from intermediate by just an integer translation, so it can be applied
932             // automatically when taking a subset of src if we update the mapping.
933             skif::LayerSpace<SkIPoint> srcOrigin({(int) srcToIntermediate.getTranslateX(),
934                                                   (int) srcToIntermediate.getTranslateY()});
935             mapping.applyOrigin(srcOrigin);
936             requiredInput.offset(-srcOrigin);
937         } else {
938             // The contents of 'src' will be drawn to an intermediate buffer using srcToIntermediate
939             // and that buffer will be the input to the image filter.
940             needsIntermediateImage = true;
941         }
942     }
943 
944     sk_sp<SkSpecialImage> filterInput;
945     if (!needsIntermediateImage) {
946         // The src device can be snapped directly
947         skif::LayerSpace<SkIRect> srcSubset(SkIRect::MakeSize(srcDims));
948         if (srcSubset.intersect(requiredInput)) {
949             filterInput = src->snapSpecial(SkIRect(srcSubset));
950 
951             // TODO: For now image filter input images need to have a (0,0) origin. The required
952             // input's top left has been baked into srcSubset so we use that as the image origin.
953             mapping.applyOrigin(srcSubset.topLeft());
954         }
955     } else {
956         // We need to produce a temporary image that is equivalent to 'src' but transformed to
957         // a coordinate space compatible with the image filter
958         SkASSERT(compat == DeviceCompatibleWithFilter::kUnknown);
959         SkRect srcRect;
960         if (!SkMatrixPriv::InverseMapRect(srcToIntermediate, &srcRect,
961                                           SkRect::Make(SkIRect(requiredInput)))) {
962             return;
963         }
964 
965         if (!srcRect.intersect(SkRect::Make(srcDims))) {
966             return;
967         }
968         SkIRect srcSubset = skif::RoundOut(srcRect);
969 
970         if (srcToIntermediate.isScaleTranslate()) {
971             // The transform is from srcRect to requiredInput, but srcRect may have been reduced
972             // to the src dimensions, so map srcSubset back to the intermediate space to get the
973             // appropriate scaled dimensions for snapScaledSpecial.
974             skif::LayerSpace<SkIRect> requiredSubset(
975                     skif::RoundOut(srcToIntermediate.mapRect(srcRect)));
976             filterInput = src->snapSpecialScaled(srcSubset,
977                                                  {requiredSubset.width(), requiredSubset.height()});
978             if (filterInput) {
979                 // TODO: Like the non-intermediate case, we need to apply the image origin
980                 mapping.applyOrigin(requiredSubset.topLeft());
981             } // else fall through and apply transform using a draw
982         }
983 
984         if (!filterInput) {
985             // Either a complex transform or the scaled copy failed so do a copy-as-draw fallback.
986             sk_sp<SkSpecialImage> srcImage = src->snapSpecial(srcSubset);
987             if (!srcImage) {
988                 return;
989             }
990             // Make a new surface and draw 'srcImage' into it with the srcToIntermediate transform
991             // to produce the final input image for the filter
992             SkBaseDevice::CreateInfo info(SkImageInfo::Make(requiredInput.width(),
993                                                             requiredInput.height(),
994                                                             filterColorType,
995                                                             kPremul_SkAlphaType,
996                                                             filterColorSpace),
997                                           SkPixelGeometry::kUnknown_SkPixelGeometry,
998                                           SkBaseDevice::TileUsage::kNever_TileUsage,
999                                           fAllocator.get());
1000             sk_sp<SkBaseDevice> intermediateDevice(src->onCreateDevice(info, &paint));
1001             if (!intermediateDevice) {
1002                 return;
1003             }
1004             intermediateDevice->setOrigin(SkM44(srcToIntermediate),
1005                                           requiredInput.left(), requiredInput.top());
1006 
1007             // We use drawPaint to fill the entire device with the src input + clamp tiling, which
1008             // extends the backdrop's edge pixels to the parts of 'requiredInput' that map offscreen
1009             // Without this, the intermediateDevice would contain transparent pixels that may then
1010             // infect blurs and other filters with large kernels.
1011             SkPaint imageFill;
1012             imageFill.setShader(srcImage->asShader(SkTileMode::kClamp,
1013                                                    SkSamplingOptions{SkFilterMode::kLinear},
1014                                                    SkMatrix::Translate(srcSubset.topLeft())));
1015             intermediateDevice->drawPaint(imageFill);
1016             filterInput = intermediateDevice->snapSpecial();
1017 
1018             // TODO: Like the non-intermediate case, we need to apply the image origin.
1019             mapping.applyOrigin(requiredInput.topLeft());
1020         }
1021     }
1022 
1023     if (filterInput) {
1024         const bool use_nn =
1025                 draw_layer_as_sprite(mapping.layerToDevice(), filterInput->subset().size());
1026         SkSamplingOptions sampling{use_nn ? SkFilterMode::kNearest : SkFilterMode::kLinear};
1027         if (filter) {
1028             dst->drawFilteredImage(mapping, filterInput.get(), filterColorType, filter,
1029                                    sampling, paint);
1030         } else {
1031             dst->drawSpecial(filterInput.get(), mapping.layerToDevice(), sampling, paint);
1032         }
1033     }
1034 }
1035 
1036 // This is similar to image_to_color_filter used by AutoLayerForImageFilter, but with key changes:
1037 //  - image_to_color_filter requires the entire image filter DAG to be represented as a color filter
1038 //    that does not affect transparent black (SkImageFilter::asAColorFilter)
1039 //  - when that is met, the image filter's CF is composed around any CF that was on the draw's paint
1040 //    since for a draw, the color filtering happens before any image filtering
1041 //  - optimize_layer_filter only applies to the last node and does not care about transparent black
1042 //    since a layer is being made regardless (SkImageFilter::isColorFilterNode)
1043 //  - any extracted CF is composed inside the restore paint's CF because image filters are evaluated
1044 //    before the color filter of a restore paint for layers.
1045 //
1046 // Assumes that 'filter', and thus its inputs, will remain owned by the caller. Modifies 'paint'
1047 // to have the updated color filter and returns the image filter to evaluate on restore.
1048 // TODO(michaelludwig): skbug.com/12083, once this guard goes away, the coversDevice arg can go away
optimize_layer_filter(const SkImageFilter * filter,SkPaint * paint,bool * coversDevice=nullptr)1049 static const SkImageFilter* optimize_layer_filter(const SkImageFilter* filter, SkPaint* paint,
1050                                                   bool* coversDevice=nullptr) {
1051     SkASSERT(paint);
1052     SkColorFilter* cf;
1053     if (filter && filter->isColorFilterNode(&cf)) {
1054         sk_sp<SkColorFilter> inner(cf);
1055         if (paint->getAlphaf() < 1.f) {
1056             // The paint's alpha is applied after the image filter but before the paint's color
1057             // filter. If there is transparency, we have to apply it between the two filters.
1058             // FIXME: The Blend CF should allow composing directly at construction.
1059             inner = SkColorFilters::Compose(
1060                     SkColorFilters::Blend(/*src*/paint->getColor4f(), nullptr, SkBlendMode::kDstIn),
1061                                           /*dst*/std::move(inner));
1062             paint->setAlphaf(1.f);
1063         }
1064 
1065         // Check if the once-wrapped color filter affects transparent black *before* we combine
1066         // it with any original color filter on the paint.
1067         if (coversDevice) {
1068 #if defined(SK_LEGACY_LAYER_BOUNDS_EXPANSION)
1069             *coversDevice = as_CFB(inner)->affectsTransparentBlack();
1070 #else
1071             *coversDevice = false;
1072 #endif
1073         }
1074 
1075         paint->setColorFilter(SkColorFilters::Compose(paint->refColorFilter(), std::move(inner)));
1076         SkASSERT(filter->countInputs() == 1);
1077         return filter->getInput(0);
1078     } else {
1079         if (coversDevice) {
1080             *coversDevice = false;
1081         }
1082         return filter;
1083     }
1084 }
1085 
1086 // If there is a backdrop filter, or if the restore paint has a color filter or blend mode that
1087 // affects transparent black, then the new layer must be sized such that it covers the entire device
1088 // clip bounds of the prior device (otherwise edges of the temporary layer would be visible).
1089 // See skbug.com/8783
must_cover_prior_device(const SkImageFilter * backdrop,const SkPaint & restorePaint)1090 static bool must_cover_prior_device(const SkImageFilter* backdrop,
1091                                     const SkPaint& restorePaint) {
1092 #if defined(SK_LEGACY_LAYER_BOUNDS_EXPANSION)
1093     return SkToBool(backdrop);
1094 #else
1095     const SkColorFilter* cf = restorePaint.getColorFilter();
1096     if (backdrop || (cf && as_CFB(cf)->affectsTransparentBlack())) {
1097         // Backdrop image filters always affect the entire (clip-limited) layer. A color filter
1098         // affecting transparent black will colorize pixels that are outside the drawn bounds hint.
1099         return true;
1100     }
1101     // A custom blender is assumed to modify transparent black; some fixed blend modes also modify
1102     // transparent black and the whole layer must be used for the same reason as color filters.
1103     if (auto blendMode = restorePaint.asBlendMode()) {
1104         SkBlendModeCoeff src, dst;
1105         if (SkBlendMode_AsCoeff(*blendMode, &src, &dst)) {
1106             // If the source is (0,0,0,0), then dst is preserved as long as its coefficient
1107             // evaluates to 1.0. This is true for kOne, kISA, and kISC. Anything else means the
1108             // blend mode affects transparent black.
1109             return dst != SkBlendModeCoeff::kOne &&
1110                    dst != SkBlendModeCoeff::kISA &&
1111                    dst != SkBlendModeCoeff::kISC;
1112         } else {
1113             // else an advanced blend mode, which preserve transparent black
1114             return false;
1115         }
1116     } else {
1117         // Blenders that aren't blend modes are assumed to modify transparent black.
1118         return true;
1119     }
1120 #endif
1121 }
1122 
internalSaveLayer(const SaveLayerRec & rec,SaveLayerStrategy strategy)1123 void SkCanvas::internalSaveLayer(const SaveLayerRec& rec, SaveLayerStrategy strategy) {
1124     TRACE_EVENT0("skia", TRACE_FUNC);
1125     // Do this before we create the layer. We don't call the public save() since that would invoke a
1126     // possibly overridden virtual.
1127     this->internalSave();
1128 
1129     if (this->isClipEmpty()) {
1130         // Early out if the layer wouldn't draw anything
1131         return;
1132     }
1133 
1134     // Build up the paint for restoring the layer, taking only the pieces of rec.fPaint that are
1135     // relevant. Filtering is automatically chosen in internalDrawDeviceWithFilter based on the
1136     // device's coordinate space.
1137     SkPaint restorePaint(rec.fPaint ? *rec.fPaint : SkPaint());
1138     restorePaint.setMaskFilter(nullptr);  // mask filters are ignored for saved layers
1139     restorePaint.setImageFilter(nullptr); // the image filter is held separately
1140     // Smooth non-axis-aligned layer edges; this automatically downgrades to non-AA for aligned
1141     // layer restores. This is done to match legacy behavior where the post-applied MatrixTransform
1142     // bilerp also smoothed cropped edges. See skbug.com/11252
1143     restorePaint.setAntiAlias(true);
1144 
1145     bool optimizedCFAffectsTransparent;
1146     const SkImageFilter* filter = optimize_layer_filter(
1147             rec.fPaint ? rec.fPaint->getImageFilter() : nullptr, &restorePaint,
1148             &optimizedCFAffectsTransparent);
1149 
1150 #if !defined(SK_LEGACY_LAYER_BOUNDS_EXPANSION)
1151     SkASSERT(!optimizedCFAffectsTransparent); // shouldn't be needed by new code
1152 #endif
1153 
1154     // Size the new layer relative to the prior device, which may already be aligned for filters.
1155     SkBaseDevice* priorDevice = this->topDevice();
1156     skif::Mapping newLayerMapping;
1157     skif::LayerSpace<SkIRect> layerBounds;
1158     std::tie(newLayerMapping, layerBounds) = get_layer_mapping_and_bounds(
1159             filter, priorDevice->localToDevice(),
1160             skif::DeviceSpace<SkIRect>(priorDevice->devClipBounds()),
1161             skif::ParameterSpace<SkRect>::Optional(rec.fBounds),
1162             must_cover_prior_device(rec.fBackdrop, restorePaint) || optimizedCFAffectsTransparent);
1163 
1164     auto abortLayer = [this]() {
1165         // The filtered content would not draw anything, or the new device space has an invalid
1166         // coordinate system, in which case we mark the current top device as empty so that nothing
1167         // draws until the canvas is restored past this saveLayer.
1168         AutoUpdateQRBounds aqr(this);
1169         this->topDevice()->clipRect(SkRect::MakeEmpty(), SkClipOp::kIntersect, /* aa */ false);
1170     };
1171 
1172     if (layerBounds.isEmpty()) {
1173         abortLayer();
1174         return;
1175     }
1176 
1177     sk_sp<SkBaseDevice> newDevice;
1178     if (strategy == kFullLayer_SaveLayerStrategy) {
1179         SkASSERT(!layerBounds.isEmpty());
1180 
1181         SkColorType layerColorType = SkToBool(rec.fSaveLayerFlags & kF16ColorType)
1182                 ? kRGBA_F16_SkColorType
1183                 : image_filter_color_type(priorDevice->imageInfo());
1184         SkImageInfo info = SkImageInfo::Make(layerBounds.width(), layerBounds.height(),
1185                                              layerColorType, kPremul_SkAlphaType,
1186                                              priorDevice->imageInfo().refColorSpace());
1187 
1188         SkPixelGeometry geo = rec.fSaveLayerFlags & kPreserveLCDText_SaveLayerFlag
1189                                       ? fProps.pixelGeometry()
1190                                       : kUnknown_SkPixelGeometry;
1191         const auto createInfo = SkBaseDevice::CreateInfo(info, geo, SkBaseDevice::kNever_TileUsage,
1192                                                          fAllocator.get());
1193         // Use the original paint as a hint so that it includes the image filter
1194         newDevice.reset(priorDevice->onCreateDevice(createInfo, rec.fPaint));
1195     }
1196 
1197     bool initBackdrop = (rec.fSaveLayerFlags & kInitWithPrevious_SaveLayerFlag) || rec.fBackdrop;
1198     if (!newDevice) {
1199         // Either we weren't meant to allocate a full layer, or the full layer creation failed.
1200         // Using an explicit NoPixelsDevice lets us reflect what the layer state would have been
1201         // on success (or kFull_LayerStrategy) while squashing draw calls that target something that
1202         // doesn't exist.
1203         newDevice = sk_make_sp<SkNoPixelsDevice>(SkIRect::MakeWH(layerBounds.width(),
1204                                                                  layerBounds.height()),
1205                                                  fProps, this->imageInfo().refColorSpace());
1206         initBackdrop = false;
1207     }
1208 
1209     // Configure device to match determined mapping for any image filters.
1210     // The setDeviceCoordinateSystem applies the prior device's global transform since
1211     // 'newLayerMapping' only defines the transforms between the two devices and it must be updated
1212     // to the global coordinate system.
1213     newDevice->setDeviceCoordinateSystem(
1214             priorDevice->deviceToGlobal() * SkM44(newLayerMapping.layerToDevice()),
1215             SkM44(newLayerMapping.deviceToLayer()) * priorDevice->globalToDevice(),
1216             SkM44(newLayerMapping.layerMatrix()),
1217             layerBounds.left(),
1218             layerBounds.top());
1219 
1220     if (initBackdrop) {
1221         SkPaint backdropPaint;
1222         const SkImageFilter* backdropFilter = optimize_layer_filter(rec.fBackdrop, &backdropPaint);
1223         // The new device was constructed to be compatible with 'filter', not necessarily
1224         // 'rec.fBackdrop', so allow DrawDeviceWithFilter to transform the prior device contents
1225         // if necessary to evaluate the backdrop filter. If no filters are involved, then the
1226         // devices differ by integer translations and are always compatible.
1227         bool scaleBackdrop = rec.fExperimentalBackdropScale != 1.0f;
1228         auto compat = (filter || backdropFilter || scaleBackdrop)
1229                 ? DeviceCompatibleWithFilter::kUnknown : DeviceCompatibleWithFilter::kYes;
1230         this->internalDrawDeviceWithFilter(priorDevice,     // src
1231                                            newDevice.get(), // dst
1232                                            backdropFilter,
1233                                            backdropPaint,
1234                                            compat,
1235                                            rec.fExperimentalBackdropScale);
1236     }
1237 
1238     fMCRec->newLayer(std::move(newDevice), sk_ref_sp(filter), restorePaint);
1239     fQuickRejectBounds = this->computeDeviceClipBounds();
1240 }
1241 
saveLayerAlphaf(const SkRect * bounds,float alpha)1242 int SkCanvas::saveLayerAlphaf(const SkRect* bounds, float alpha) {
1243     if (alpha >= 1.0f) {
1244         return this->saveLayer(bounds, nullptr);
1245     } else {
1246         SkPaint tmpPaint;
1247         tmpPaint.setAlphaf(alpha);
1248         return this->saveLayer(bounds, &tmpPaint);
1249     }
1250 }
1251 
internalSaveBehind(const SkRect * localBounds)1252 void SkCanvas::internalSaveBehind(const SkRect* localBounds) {
1253     SkBaseDevice* device = this->topDevice();
1254 
1255     // Map the local bounds into the top device's coordinate space (this is not
1256     // necessarily the full global CTM transform).
1257     SkIRect devBounds;
1258     if (localBounds) {
1259         SkRect tmp;
1260         device->localToDevice().mapRect(&tmp, *localBounds);
1261         if (!devBounds.intersect(tmp.round(), device->devClipBounds())) {
1262             devBounds.setEmpty();
1263         }
1264     } else {
1265         devBounds = device->devClipBounds();
1266     }
1267     if (devBounds.isEmpty()) {
1268         return;
1269     }
1270 
1271     // This is getting the special image from the current device, which is then drawn into (both by
1272     // a client, and the drawClippedToSaveBehind below). Since this is not saving a layer, with its
1273     // own device, we need to explicitly copy the back image contents so that its original content
1274     // is available when we splat it back later during restore.
1275     auto backImage = device->snapSpecial(devBounds, /* forceCopy= */ true);
1276     if (!backImage) {
1277         return;
1278     }
1279 
1280     // we really need the save, so we can wack the fMCRec
1281     this->checkForDeferredSave();
1282 
1283     fMCRec->fBackImage =
1284             std::make_unique<BackImage>(BackImage{std::move(backImage), devBounds.topLeft()});
1285 
1286     SkPaint paint;
1287     paint.setBlendMode(SkBlendMode::kClear);
1288     this->drawClippedToSaveBehind(paint);
1289 }
1290 
internalRestore()1291 void SkCanvas::internalRestore() {
1292     SkASSERT(!fMCStack.empty());
1293 
1294     // now detach these from fMCRec so we can pop(). Gets freed after its drawn
1295     std::unique_ptr<Layer> layer = std::move(fMCRec->fLayer);
1296     std::unique_ptr<BackImage> backImage = std::move(fMCRec->fBackImage);
1297 
1298     // now do the normal restore()
1299     fMCRec->~MCRec();       // balanced in save()
1300     fMCStack.pop_back();
1301     fMCRec = (MCRec*) fMCStack.back();
1302 
1303     if (!fMCRec) {
1304         // This was the last record, restored during the destruction of the SkCanvas
1305         return;
1306     }
1307 
1308     this->topDevice()->restore(fMCRec->fMatrix);
1309 
1310     if (backImage) {
1311         SkPaint paint;
1312         paint.setBlendMode(SkBlendMode::kDstOver);
1313         this->topDevice()->drawSpecial(backImage->fImage.get(),
1314                                        SkMatrix::Translate(backImage->fLoc),
1315                                        SkSamplingOptions(),
1316                                        paint);
1317     }
1318 
1319     // Draw the layer's device contents into the now-current older device. We can't call public
1320     // draw functions since we don't want to record them.
1321     if (layer && !layer->fDevice->isNoPixelsDevice() && !layer->fDiscard) {
1322         layer->fDevice->setImmutable();
1323 
1324         // Don't go through AutoLayerForImageFilter since device draws are so closely tied to
1325         // internalSaveLayer and internalRestore.
1326         if (this->predrawNotify()) {
1327             SkBaseDevice* dstDev = this->topDevice();
1328             if (layer->fImageFilter) {
1329                 this->internalDrawDeviceWithFilter(layer->fDevice.get(), // src
1330                                                    dstDev,               // dst
1331                                                    layer->fImageFilter.get(),
1332                                                    layer->fPaint,
1333                                                    DeviceCompatibleWithFilter::kYes);
1334             } else {
1335                 // NOTE: We don't just call internalDrawDeviceWithFilter with a null filter
1336                 // because we want to take advantage of overridden drawDevice functions for
1337                 // document-based devices.
1338                 SkSamplingOptions sampling;
1339                 dstDev->drawDevice(layer->fDevice.get(), sampling, layer->fPaint);
1340             }
1341         }
1342     }
1343 
1344     // Reset the clip restriction if the restore went past the save point that had added it.
1345     if (this->getSaveCount() < fClipRestrictionSaveCount) {
1346         fClipRestrictionRect.setEmpty();
1347         fClipRestrictionSaveCount = -1;
1348     }
1349     // Update the quick-reject bounds in case the restore changed the top device or the
1350     // removed save record had included modifications to the clip stack.
1351     fQuickRejectBounds = this->computeDeviceClipBounds();
1352     this->validateClip();
1353 }
1354 
makeSurface(const SkImageInfo & info,const SkSurfaceProps * props)1355 sk_sp<SkSurface> SkCanvas::makeSurface(const SkImageInfo& info, const SkSurfaceProps* props) {
1356     if (nullptr == props) {
1357         props = &fProps;
1358     }
1359     return this->onNewSurface(info, *props);
1360 }
1361 
onNewSurface(const SkImageInfo & info,const SkSurfaceProps & props)1362 sk_sp<SkSurface> SkCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1363     return this->baseDevice()->makeSurface(info, props);
1364 }
1365 
imageInfo() const1366 SkImageInfo SkCanvas::imageInfo() const {
1367     return this->onImageInfo();
1368 }
1369 
onImageInfo() const1370 SkImageInfo SkCanvas::onImageInfo() const {
1371     return this->baseDevice()->imageInfo();
1372 }
1373 
getProps(SkSurfaceProps * props) const1374 bool SkCanvas::getProps(SkSurfaceProps* props) const {
1375     return this->onGetProps(props, /*top=*/false);
1376 }
1377 
getBaseProps() const1378 SkSurfaceProps SkCanvas::getBaseProps() const {
1379     SkSurfaceProps props;
1380     this->onGetProps(&props, /*top=*/false);
1381     return props;
1382 }
1383 
getTopProps() const1384 SkSurfaceProps SkCanvas::getTopProps() const {
1385     SkSurfaceProps props;
1386     this->onGetProps(&props, /*top=*/true);
1387     return props;
1388 }
1389 
onGetProps(SkSurfaceProps * props,bool top) const1390 bool SkCanvas::onGetProps(SkSurfaceProps* props, bool top) const {
1391     if (props) {
1392         *props = top ? topDevice()->surfaceProps() : fProps;
1393     }
1394     return true;
1395 }
1396 
peekPixels(SkPixmap * pmap)1397 bool SkCanvas::peekPixels(SkPixmap* pmap) {
1398     return this->onPeekPixels(pmap);
1399 }
1400 
onPeekPixels(SkPixmap * pmap)1401 bool SkCanvas::onPeekPixels(SkPixmap* pmap) {
1402     return this->baseDevice()->peekPixels(pmap);
1403 }
1404 
accessTopLayerPixels(SkImageInfo * info,size_t * rowBytes,SkIPoint * origin)1405 void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) {
1406     SkPixmap pmap;
1407     if (!this->onAccessTopLayerPixels(&pmap)) {
1408         return nullptr;
1409     }
1410     if (info) {
1411         *info = pmap.info();
1412     }
1413     if (rowBytes) {
1414         *rowBytes = pmap.rowBytes();
1415     }
1416     if (origin) {
1417         // If the caller requested the origin, they presumably are expecting the returned pixels to
1418         // be axis-aligned with the root canvas. If the top level device isn't axis aligned, that's
1419         // not the case. Until we update accessTopLayerPixels() to accept a coord space matrix
1420         // instead of an origin, just don't expose the pixels in that case. Note that this means
1421         // that layers with complex coordinate spaces can still report their pixels if the caller
1422         // does not ask for the origin (e.g. just to dump its output to a file, etc).
1423         if (this->topDevice()->isPixelAlignedToGlobal()) {
1424             *origin = this->topDevice()->getOrigin();
1425         } else {
1426             return nullptr;
1427         }
1428     }
1429     return pmap.writable_addr();
1430 }
1431 
onAccessTopLayerPixels(SkPixmap * pmap)1432 bool SkCanvas::onAccessTopLayerPixels(SkPixmap* pmap) {
1433     return this->topDevice()->accessPixels(pmap);
1434 }
1435 
1436 /////////////////////////////////////////////////////////////////////////////
1437 
translate(SkScalar dx,SkScalar dy)1438 void SkCanvas::translate(SkScalar dx, SkScalar dy) {
1439     if (dx || dy) {
1440         this->checkForDeferredSave();
1441         fMCRec->fMatrix.preTranslate(dx, dy);
1442 
1443         this->topDevice()->setGlobalCTM(fMCRec->fMatrix);
1444 
1445         this->didTranslate(dx,dy);
1446     }
1447 }
1448 
scale(SkScalar sx,SkScalar sy)1449 void SkCanvas::scale(SkScalar sx, SkScalar sy) {
1450     if (sx != 1 || sy != 1) {
1451         this->checkForDeferredSave();
1452         fMCRec->fMatrix.preScale(sx, sy);
1453 
1454         this->topDevice()->setGlobalCTM(fMCRec->fMatrix);
1455 
1456         this->didScale(sx, sy);
1457     }
1458 }
1459 
rotate(SkScalar degrees)1460 void SkCanvas::rotate(SkScalar degrees) {
1461     SkMatrix m;
1462     m.setRotate(degrees);
1463     this->concat(m);
1464 }
1465 
rotate(SkScalar degrees,SkScalar px,SkScalar py)1466 void SkCanvas::rotate(SkScalar degrees, SkScalar px, SkScalar py) {
1467     SkMatrix m;
1468     m.setRotate(degrees, px, py);
1469     this->concat(m);
1470 }
1471 
skew(SkScalar sx,SkScalar sy)1472 void SkCanvas::skew(SkScalar sx, SkScalar sy) {
1473     SkMatrix m;
1474     m.setSkew(sx, sy);
1475     this->concat(m);
1476 }
1477 
concat(const SkMatrix & matrix)1478 void SkCanvas::concat(const SkMatrix& matrix) {
1479     if (matrix.isIdentity()) {
1480         return;
1481     }
1482     this->concat(SkM44(matrix));
1483 }
1484 
internalConcat44(const SkM44 & m)1485 void SkCanvas::internalConcat44(const SkM44& m) {
1486     this->checkForDeferredSave();
1487 
1488     fMCRec->fMatrix.preConcat(m);
1489 
1490     this->topDevice()->setGlobalCTM(fMCRec->fMatrix);
1491 }
1492 
concat(const SkM44 & m)1493 void SkCanvas::concat(const SkM44& m) {
1494     this->internalConcat44(m);
1495     // notify subclasses
1496     this->didConcat44(m);
1497 }
1498 
internalSetMatrix(const SkM44 & m)1499 void SkCanvas::internalSetMatrix(const SkM44& m) {
1500     fMCRec->fMatrix = m;
1501 
1502     this->topDevice()->setGlobalCTM(fMCRec->fMatrix);
1503 }
1504 
setMatrix(const SkMatrix & matrix)1505 void SkCanvas::setMatrix(const SkMatrix& matrix) {
1506     this->setMatrix(SkM44(matrix));
1507 }
1508 
setMatrix(const SkM44 & m)1509 void SkCanvas::setMatrix(const SkM44& m) {
1510     this->checkForDeferredSave();
1511     this->internalSetMatrix(m);
1512     this->didSetM44(m);
1513 }
1514 
resetMatrix()1515 void SkCanvas::resetMatrix() {
1516     this->setMatrix(SkM44());
1517 }
1518 
1519 //////////////////////////////////////////////////////////////////////////////
1520 
clipRect(const SkRect & rect,SkClipOp op,bool doAA)1521 void SkCanvas::clipRect(const SkRect& rect, SkClipOp op, bool doAA) {
1522     if (!rect.isFinite()) {
1523         return;
1524     }
1525     this->checkForDeferredSave();
1526     ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
1527     this->onClipRect(rect.makeSorted(), op, edgeStyle);
1528 }
1529 
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)1530 void SkCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
1531     SkASSERT(rect.isSorted());
1532     const bool isAA = kSoft_ClipEdgeStyle == edgeStyle;
1533 
1534     AutoUpdateQRBounds aqr(this);
1535     this->topDevice()->clipRect(rect, op, isAA);
1536 }
1537 
androidFramework_setDeviceClipRestriction(const SkIRect & rect)1538 void SkCanvas::androidFramework_setDeviceClipRestriction(const SkIRect& rect) {
1539     // The device clip restriction is a surface-space rectangular intersection that cannot be
1540     // drawn outside of. The rectangle is remembered so that subsequent resetClip calls still
1541     // respect the restriction. Other than clip resetting, all clip operations restrict the set
1542     // of renderable pixels, so once set, the restriction will be respected until the canvas
1543     // save stack is restored past the point this function was invoked. Unfortunately, the current
1544     // implementation relies on the clip stack of the underyling SkDevices, which leads to some
1545     // awkward behavioral interactions (see skbug.com/12252).
1546     //
1547     // Namely, a canvas restore() could undo the clip restriction's rect, and if
1548     // setDeviceClipRestriction were called at a nested save level, there's no way to undo just the
1549     // prior restriction and re-apply the new one. It also only makes sense to apply to the base
1550     // device; any other device for a saved layer will be clipped back to the base device during its
1551     // matched restore. As such, we:
1552     // - Remember the save count that added the clip restriction and reset the rect to empty when
1553     //   we've restored past that point to keep our state in sync with the device's clip stack.
1554     // - We assert that we're on the base device when this is invoked.
1555     // - We assert that setDeviceClipRestriction() is only called when there was no prior
1556     //   restriction (cannot re-restrict, and prior state must have been reset by restoring the
1557     //   canvas state).
1558     // - Historically, the empty rect would reset the clip restriction but it only could do so
1559     //   partially since the device's clips wasn't adjusted. Resetting is now handled
1560     //   automatically via SkCanvas::restore(), so empty input rects are skipped.
1561     SkASSERT(this->topDevice() == this->baseDevice()); // shouldn't be in a nested layer
1562     // and shouldn't already have a restriction
1563     SkASSERT(fClipRestrictionSaveCount < 0 && fClipRestrictionRect.isEmpty());
1564 
1565     if (fClipRestrictionSaveCount < 0 && !rect.isEmpty()) {
1566         fClipRestrictionRect = rect;
1567         fClipRestrictionSaveCount = this->getSaveCount();
1568 
1569         // A non-empty clip restriction immediately applies an intersection op (ignoring the ctm).
1570         // so we have to resolve the save.
1571         this->checkForDeferredSave();
1572         AutoUpdateQRBounds aqr(this);
1573         // Use clipRegion() since that operates in canvas-space, whereas clipRect() would apply the
1574         // device's current transform first.
1575         this->topDevice()->clipRegion(SkRegion(rect), SkClipOp::kIntersect);
1576     }
1577 }
1578 
internal_private_resetClip()1579 void SkCanvas::internal_private_resetClip() {
1580     this->checkForDeferredSave();
1581     this->onResetClip();
1582 }
1583 
onResetClip()1584 void SkCanvas::onResetClip() {
1585     SkIRect deviceRestriction = this->topDevice()->imageInfo().bounds();
1586     if (fClipRestrictionSaveCount >= 0 && this->topDevice() == this->baseDevice()) {
1587         // Respect the device clip restriction when resetting the clip if we're on the base device.
1588         // If we're not on the base device, then the "reset" applies to the top device's clip stack,
1589         // and the clip restriction will be respected automatically during a restore of the layer.
1590         if (!deviceRestriction.intersect(fClipRestrictionRect)) {
1591             deviceRestriction = SkIRect::MakeEmpty();
1592         }
1593     }
1594 
1595     AutoUpdateQRBounds aqr(this);
1596     this->topDevice()->replaceClip(deviceRestriction);
1597 }
1598 
clipRRect(const SkRRect & rrect,SkClipOp op,bool doAA)1599 void SkCanvas::clipRRect(const SkRRect& rrect, SkClipOp op, bool doAA) {
1600     this->checkForDeferredSave();
1601     ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
1602     if (rrect.isRect()) {
1603         this->onClipRect(rrect.getBounds(), op, edgeStyle);
1604     } else {
1605         this->onClipRRect(rrect, op, edgeStyle);
1606     }
1607 }
1608 
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)1609 void SkCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
1610     bool isAA = kSoft_ClipEdgeStyle == edgeStyle;
1611 
1612     AutoUpdateQRBounds aqr(this);
1613     this->topDevice()->clipRRect(rrect, op, isAA);
1614 }
1615 
clipPath(const SkPath & path,SkClipOp op,bool doAA)1616 void SkCanvas::clipPath(const SkPath& path, SkClipOp op, bool doAA) {
1617     this->checkForDeferredSave();
1618     ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
1619 
1620     if (!path.isInverseFillType() && fMCRec->fMatrix.asM33().rectStaysRect()) {
1621         SkRect r;
1622         if (path.isRect(&r)) {
1623             this->onClipRect(r, op, edgeStyle);
1624             return;
1625         }
1626         SkRRect rrect;
1627         if (path.isOval(&r)) {
1628             rrect.setOval(r);
1629             this->onClipRRect(rrect, op, edgeStyle);
1630             return;
1631         }
1632         if (path.isRRect(&rrect)) {
1633             this->onClipRRect(rrect, op, edgeStyle);
1634             return;
1635         }
1636     }
1637 
1638     this->onClipPath(path, op, edgeStyle);
1639 }
1640 
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)1641 void SkCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
1642     bool isAA = kSoft_ClipEdgeStyle == edgeStyle;
1643 
1644     AutoUpdateQRBounds aqr(this);
1645     this->topDevice()->clipPath(path, op, isAA);
1646 }
1647 
clipShader(sk_sp<SkShader> sh,SkClipOp op)1648 void SkCanvas::clipShader(sk_sp<SkShader> sh, SkClipOp op) {
1649     if (sh) {
1650         if (sh->isOpaque()) {
1651             if (op == SkClipOp::kIntersect) {
1652                 // we don't occlude anything, so skip this call
1653             } else {
1654                 SkASSERT(op == SkClipOp::kDifference);
1655                 // we occlude everything, so set the clip to empty
1656                 this->clipRect({0,0,0,0});
1657             }
1658         } else {
1659             this->checkForDeferredSave();
1660             this->onClipShader(std::move(sh), op);
1661         }
1662     }
1663 }
1664 
onClipShader(sk_sp<SkShader> sh,SkClipOp op)1665 void SkCanvas::onClipShader(sk_sp<SkShader> sh, SkClipOp op) {
1666     AutoUpdateQRBounds aqr(this);
1667     this->topDevice()->clipShader(sh, op);
1668 }
1669 
clipRegion(const SkRegion & rgn,SkClipOp op)1670 void SkCanvas::clipRegion(const SkRegion& rgn, SkClipOp op) {
1671     this->checkForDeferredSave();
1672     this->onClipRegion(rgn, op);
1673 }
1674 
onClipRegion(const SkRegion & rgn,SkClipOp op)1675 void SkCanvas::onClipRegion(const SkRegion& rgn, SkClipOp op) {
1676     AutoUpdateQRBounds aqr(this);
1677     this->topDevice()->clipRegion(rgn, op);
1678 }
1679 
validateClip() const1680 void SkCanvas::validateClip() const {
1681 #ifdef SK_DEBUG
1682     SkRect tmp = this->computeDeviceClipBounds();
1683     if (this->isClipEmpty()) {
1684         SkASSERT(fQuickRejectBounds.isEmpty());
1685     } else {
1686         SkASSERT(tmp == fQuickRejectBounds);
1687     }
1688 #endif
1689 }
1690 
androidFramework_isClipAA() const1691 bool SkCanvas::androidFramework_isClipAA() const {
1692     return this->topDevice()->onClipIsAA();
1693 }
1694 
temporary_internal_getRgnClip(SkRegion * rgn)1695 void SkCanvas::temporary_internal_getRgnClip(SkRegion* rgn) {
1696     rgn->setEmpty();
1697     SkBaseDevice* device = this->topDevice();
1698     if (device && device->isPixelAlignedToGlobal()) {
1699         device->onAsRgnClip(rgn);
1700         SkIPoint origin = device->getOrigin();
1701         if (origin.x() | origin.y()) {
1702             rgn->translate(origin.x(), origin.y());
1703         }
1704     }
1705 }
1706 
1707 ///////////////////////////////////////////////////////////////////////////////
1708 
isClipEmpty() const1709 bool SkCanvas::isClipEmpty() const {
1710     return this->topDevice()->onGetClipType() == SkBaseDevice::ClipType::kEmpty;
1711 }
1712 
isClipRect() const1713 bool SkCanvas::isClipRect() const {
1714     return this->topDevice()->onGetClipType() == SkBaseDevice::ClipType::kRect;
1715 }
1716 
quickReject(const SkRect & src) const1717 bool SkCanvas::quickReject(const SkRect& src) const {
1718 #ifdef SK_DEBUG
1719     // Verify that fQuickRejectBounds are set properly.
1720     this->validateClip();
1721 #endif
1722 
1723     SkRect devRect = SkMatrixPriv::MapRect(fMCRec->fMatrix, src);
1724     return !devRect.isFinite() || !devRect.intersects(fQuickRejectBounds);
1725 }
1726 
quickReject(const SkPath & path) const1727 bool SkCanvas::quickReject(const SkPath& path) const {
1728     return path.isEmpty() || this->quickReject(path.getBounds());
1729 }
1730 
internalQuickReject(const SkRect & bounds,const SkPaint & paint,const SkMatrix * matrix)1731 bool SkCanvas::internalQuickReject(const SkRect& bounds, const SkPaint& paint,
1732                                    const SkMatrix* matrix) {
1733     if (!bounds.isFinite() || paint.nothingToDraw()) {
1734         return true;
1735     }
1736 
1737     if (paint.canComputeFastBounds()) {
1738         SkRect tmp = matrix ? matrix->mapRect(bounds) : bounds;
1739         return this->quickReject(paint.computeFastBounds(tmp, &tmp));
1740     }
1741 
1742     return false;
1743 }
1744 
1745 
getLocalClipBounds() const1746 SkRect SkCanvas::getLocalClipBounds() const {
1747     SkIRect ibounds = this->getDeviceClipBounds();
1748     if (ibounds.isEmpty()) {
1749         return SkRect::MakeEmpty();
1750     }
1751 
1752     SkMatrix inverse;
1753     // if we can't invert the CTM, we can't return local clip bounds
1754     if (!fMCRec->fMatrix.asM33().invert(&inverse)) {
1755         return SkRect::MakeEmpty();
1756     }
1757 
1758     SkRect bounds;
1759     // adjust it outwards in case we are antialiasing
1760     const int margin = 1;
1761 
1762     SkRect r = SkRect::Make(ibounds.makeOutset(margin, margin));
1763     inverse.mapRect(&bounds, r);
1764     return bounds;
1765 }
1766 
getDeviceClipBounds() const1767 SkIRect SkCanvas::getDeviceClipBounds() const {
1768     return this->computeDeviceClipBounds(/*outsetForAA=*/false).roundOut();
1769 }
1770 
computeDeviceClipBounds(bool outsetForAA) const1771 SkRect SkCanvas::computeDeviceClipBounds(bool outsetForAA) const {
1772     const SkBaseDevice* dev = this->topDevice();
1773     if (dev->onGetClipType() == SkBaseDevice::ClipType::kEmpty) {
1774         return SkRect::MakeEmpty();
1775     } else {
1776         SkRect devClipBounds =
1777                 SkMatrixPriv::MapRect(dev->deviceToGlobal(), SkRect::Make(dev->devClipBounds()));
1778         if (outsetForAA) {
1779             // Expand bounds out by 1 in case we are anti-aliasing.  We store the
1780             // bounds as floats to enable a faster quick reject implementation.
1781             devClipBounds.outset(1.f, 1.f);
1782         }
1783         return devClipBounds;
1784     }
1785 }
1786 
1787 ///////////////////////////////////////////////////////////////////////
1788 
getTotalMatrix() const1789 SkMatrix SkCanvas::getTotalMatrix() const {
1790     return fMCRec->fMatrix.asM33();
1791 }
1792 
getLocalToDevice() const1793 SkM44 SkCanvas::getLocalToDevice() const {
1794     return fMCRec->fMatrix;
1795 }
1796 
1797 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_GANESH)
1798 
topLayerBounds() const1799 SkIRect SkCanvas::topLayerBounds() const {
1800     return this->topDevice()->getGlobalBounds();
1801 }
1802 
topLayerBackendRenderTarget() const1803 GrBackendRenderTarget SkCanvas::topLayerBackendRenderTarget() const {
1804     auto proxy = SkCanvasPriv::TopDeviceTargetProxy(const_cast<SkCanvas*>(this));
1805     if (!proxy) {
1806         return {};
1807     }
1808     const GrRenderTarget* renderTarget = proxy->peekRenderTarget();
1809     return renderTarget ? renderTarget->getBackendRenderTarget() : GrBackendRenderTarget();
1810 }
1811 #endif
1812 
recordingContext()1813 GrRecordingContext* SkCanvas::recordingContext() {
1814 #if defined(SK_GANESH)
1815     if (auto gpuDevice = this->topDevice()->asGaneshDevice()) {
1816         return gpuDevice->recordingContext();
1817     }
1818 #endif
1819 
1820     return nullptr;
1821 }
1822 
recorder()1823 skgpu::graphite::Recorder* SkCanvas::recorder() {
1824 #if defined(SK_GRAPHITE)
1825     if (auto graphiteDevice = this->topDevice()->asGraphiteDevice()) {
1826         return graphiteDevice->recorder();
1827     }
1828 #endif
1829 
1830     return nullptr;
1831 }
1832 
1833 
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)1834 void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner,
1835                           const SkPaint& paint) {
1836     TRACE_EVENT0("skia", TRACE_FUNC);
1837     if (outer.isEmpty()) {
1838         return;
1839     }
1840     if (inner.isEmpty()) {
1841         this->drawRRect(outer, paint);
1842         return;
1843     }
1844 
1845     // We don't have this method (yet), but technically this is what we should
1846     // be able to return ...
1847     // if (!outer.contains(inner))) {
1848     //
1849     // For now at least check for containment of bounds
1850     if (!outer.getBounds().contains(inner.getBounds())) {
1851         return;
1852     }
1853 
1854     this->onDrawDRRect(outer, inner, paint);
1855 }
1856 
drawPaint(const SkPaint & paint)1857 void SkCanvas::drawPaint(const SkPaint& paint) {
1858     TRACE_EVENT0("skia", TRACE_FUNC);
1859     this->onDrawPaint(paint);
1860 }
1861 
drawRect(const SkRect & r,const SkPaint & paint)1862 void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
1863     TRACE_EVENT0("skia", TRACE_FUNC);
1864     // To avoid redundant logic in our culling code and various backends, we always sort rects
1865     // before passing them along.
1866     this->onDrawRect(r.makeSorted(), paint);
1867 }
1868 
drawClippedToSaveBehind(const SkPaint & paint)1869 void SkCanvas::drawClippedToSaveBehind(const SkPaint& paint) {
1870     TRACE_EVENT0("skia", TRACE_FUNC);
1871     this->onDrawBehind(paint);
1872 }
1873 
drawRegion(const SkRegion & region,const SkPaint & paint)1874 void SkCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
1875     TRACE_EVENT0("skia", TRACE_FUNC);
1876     if (region.isEmpty()) {
1877         return;
1878     }
1879 
1880     if (region.isRect()) {
1881         return this->drawIRect(region.getBounds(), paint);
1882     }
1883 
1884     this->onDrawRegion(region, paint);
1885 }
1886 
drawOval(const SkRect & r,const SkPaint & paint)1887 void SkCanvas::drawOval(const SkRect& r, const SkPaint& paint) {
1888     TRACE_EVENT0("skia", TRACE_FUNC);
1889     // To avoid redundant logic in our culling code and various backends, we always sort rects
1890     // before passing them along.
1891     this->onDrawOval(r.makeSorted(), paint);
1892 }
1893 
drawRRect(const SkRRect & rrect,const SkPaint & paint)1894 void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
1895     TRACE_EVENT0("skia", TRACE_FUNC);
1896     this->onDrawRRect(rrect, paint);
1897 }
1898 
drawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)1899 void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) {
1900     TRACE_EVENT0("skia", TRACE_FUNC);
1901     this->onDrawPoints(mode, count, pts, paint);
1902 }
1903 
drawVertices(const sk_sp<SkVertices> & vertices,SkBlendMode mode,const SkPaint & paint)1904 void SkCanvas::drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode,
1905                             const SkPaint& paint) {
1906     this->drawVertices(vertices.get(), mode, paint);
1907 }
1908 
drawVertices(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)1909 void SkCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
1910     TRACE_EVENT0("skia", TRACE_FUNC);
1911     RETURN_ON_NULL(vertices);
1912 
1913     // We expect fans to be converted to triangles when building or deserializing SkVertices.
1914     SkASSERT(vertices->priv().mode() != SkVertices::kTriangleFan_VertexMode);
1915 
1916 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1917     // Preserve legacy behavior for Android: ignore the SkShader if there are no texCoords present
1918     if (paint.getShader() && !vertices->priv().hasTexCoords()) {
1919         SkPaint noShaderPaint(paint);
1920         noShaderPaint.setShader(nullptr);
1921         this->onDrawVerticesObject(vertices, mode, noShaderPaint);
1922         return;
1923     }
1924 #endif
1925     this->onDrawVerticesObject(vertices, mode, paint);
1926 }
1927 
1928 #ifdef SK_ENABLE_SKSL
drawMesh(const SkMesh & mesh,sk_sp<SkBlender> blender,const SkPaint & paint)1929 void SkCanvas::drawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
1930     TRACE_EVENT0("skia", TRACE_FUNC);
1931     RETURN_ON_FALSE(mesh.isValid());
1932     if (!blender) {
1933         blender = SkBlender::Mode(SkBlendMode::kModulate);
1934     }
1935     this->onDrawMesh(mesh, std::move(blender), paint);
1936 }
1937 #endif
1938 
drawPath(const SkPath & path,const SkPaint & paint)1939 void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
1940     TRACE_EVENT0("skia", TRACE_FUNC);
1941     this->onDrawPath(path, paint);
1942 }
1943 
1944 // Returns true if the rect can be "filled" : non-empty and finite
fillable(const SkRect & r)1945 static bool fillable(const SkRect& r) {
1946     SkScalar w = r.width();
1947     SkScalar h = r.height();
1948     return SkScalarIsFinite(w) && w > 0 && SkScalarIsFinite(h) && h > 0;
1949 }
1950 
clean_paint_for_lattice(const SkPaint * paint)1951 static SkPaint clean_paint_for_lattice(const SkPaint* paint) {
1952     SkPaint cleaned;
1953     if (paint) {
1954         cleaned = *paint;
1955         cleaned.setMaskFilter(nullptr);
1956         cleaned.setAntiAlias(false);
1957     }
1958     return cleaned;
1959 }
1960 
drawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)1961 void SkCanvas::drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
1962                              SkFilterMode filter, const SkPaint* paint) {
1963     RETURN_ON_NULL(image);
1964 
1965     const int xdivs[] = {center.fLeft, center.fRight};
1966     const int ydivs[] = {center.fTop, center.fBottom};
1967 
1968     Lattice lat;
1969     lat.fXDivs = xdivs;
1970     lat.fYDivs = ydivs;
1971     lat.fRectTypes = nullptr;
1972     lat.fXCount = lat.fYCount = 2;
1973     lat.fBounds = nullptr;
1974     lat.fColors = nullptr;
1975     this->drawImageLattice(image, lat, dst, filter, paint);
1976 }
1977 
drawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)1978 void SkCanvas::drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
1979                                 SkFilterMode filter, const SkPaint* paint) {
1980     TRACE_EVENT0("skia", TRACE_FUNC);
1981     RETURN_ON_NULL(image);
1982     if (dst.isEmpty()) {
1983         return;
1984     }
1985 
1986     SkIRect bounds;
1987     Lattice latticePlusBounds = lattice;
1988     if (!latticePlusBounds.fBounds) {
1989         bounds = SkIRect::MakeWH(image->width(), image->height());
1990         latticePlusBounds.fBounds = &bounds;
1991     }
1992 
1993     SkPaint latticePaint = clean_paint_for_lattice(paint);
1994     if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
1995         this->onDrawImageLattice2(image, latticePlusBounds, dst, filter, &latticePaint);
1996     } else {
1997         this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), dst,
1998                             SkSamplingOptions(filter), &latticePaint, kStrict_SrcRectConstraint);
1999     }
2000 }
2001 
drawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)2002 void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
2003                          const SkColor colors[], int count, SkBlendMode mode,
2004                          const SkSamplingOptions& sampling, const SkRect* cull,
2005                          const SkPaint* paint) {
2006     TRACE_EVENT0("skia", TRACE_FUNC);
2007     RETURN_ON_NULL(atlas);
2008     if (count <= 0) {
2009         return;
2010     }
2011     SkASSERT(atlas);
2012     SkASSERT(tex);
2013     this->onDrawAtlas2(atlas, xform, tex, colors, count, mode, sampling, cull, paint);
2014 }
2015 
drawAnnotation(const SkRect & rect,const char key[],SkData * value)2016 void SkCanvas::drawAnnotation(const SkRect& rect, const char key[], SkData* value) {
2017     TRACE_EVENT0("skia", TRACE_FUNC);
2018     if (key) {
2019         this->onDrawAnnotation(rect, key, value);
2020     }
2021 }
2022 
private_draw_shadow_rec(const SkPath & path,const SkDrawShadowRec & rec)2023 void SkCanvas::private_draw_shadow_rec(const SkPath& path, const SkDrawShadowRec& rec) {
2024     TRACE_EVENT0("skia", TRACE_FUNC);
2025     this->onDrawShadowRec(path, rec);
2026 }
2027 
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)2028 void SkCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
2029     // We don't test quickReject because the shadow outsets the path's bounds.
2030     // TODO(michaelludwig): Is it worth calling SkDrawShadowMetrics::GetLocalBounds here?
2031     if (!this->predrawNotify()) {
2032         return;
2033     }
2034     this->topDevice()->drawShadow(path, rec);
2035 }
2036 
experimental_DrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aaFlags,const SkColor4f & color,SkBlendMode mode)2037 void SkCanvas::experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
2038                                            QuadAAFlags aaFlags, const SkColor4f& color,
2039                                            SkBlendMode mode) {
2040     TRACE_EVENT0("skia", TRACE_FUNC);
2041     // Make sure the rect is sorted before passing it along
2042     this->onDrawEdgeAAQuad(rect.makeSorted(), clip, aaFlags, color, mode);
2043 }
2044 
experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[],int cnt,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)2045 void SkCanvas::experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt,
2046                                                const SkPoint dstClips[],
2047                                                const SkMatrix preViewMatrices[],
2048                                                const SkSamplingOptions& sampling,
2049                                                const SkPaint* paint,
2050                                                SrcRectConstraint constraint) {
2051     TRACE_EVENT0("skia", TRACE_FUNC);
2052     this->onDrawEdgeAAImageSet2(imageSet, cnt, dstClips, preViewMatrices, sampling, paint,
2053                                 constraint);
2054 }
2055 
2056 //////////////////////////////////////////////////////////////////////////////
2057 //  These are the virtual drawing methods
2058 //////////////////////////////////////////////////////////////////////////////
2059 
onDiscard()2060 void SkCanvas::onDiscard() {
2061     if (fSurfaceBase) {
2062         sk_ignore_unused_variable(fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode));
2063     }
2064 }
2065 
onDrawPaint(const SkPaint & paint)2066 void SkCanvas::onDrawPaint(const SkPaint& paint) {
2067     this->internalDrawPaint(paint);
2068 }
2069 
internalDrawPaint(const SkPaint & paint)2070 void SkCanvas::internalDrawPaint(const SkPaint& paint) {
2071     // drawPaint does not call internalQuickReject() because computing its geometry is not free
2072     // (see getLocalClipBounds(), and the two conditions below are sufficient.
2073     if (paint.nothingToDraw() || this->isClipEmpty()) {
2074         return;
2075     }
2076 
2077     auto layer = this->aboutToDraw(this, paint, nullptr, CheckForOverwrite::kYes);
2078     if (layer) {
2079         this->topDevice()->drawPaint(layer->paint());
2080     }
2081 }
2082 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)2083 void SkCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
2084                             const SkPaint& paint) {
2085     if ((long)count <= 0 || paint.nothingToDraw()) {
2086         return;
2087     }
2088     SkASSERT(pts != nullptr);
2089 
2090     SkRect bounds;
2091     // Compute bounds from points (common for drawing a single line)
2092     if (count == 2) {
2093         bounds.set(pts[0], pts[1]);
2094     } else {
2095         bounds.setBounds(pts, SkToInt(count));
2096     }
2097 
2098     // Enforce paint style matches implicit behavior of drawPoints
2099     SkPaint strokePaint = paint;
2100     strokePaint.setStyle(SkPaint::kStroke_Style);
2101     if (this->internalQuickReject(bounds, strokePaint)) {
2102         return;
2103     }
2104 
2105     auto layer = this->aboutToDraw(this, strokePaint, &bounds);
2106     if (layer) {
2107         this->topDevice()->drawPoints(mode, count, pts, layer->paint());
2108     }
2109 }
2110 
onDrawRect(const SkRect & r,const SkPaint & paint)2111 void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
2112     SkASSERT(r.isSorted());
2113     if (this->internalQuickReject(r, paint)) {
2114         return;
2115     }
2116 
2117     auto layer = this->aboutToDraw(this, paint, &r, CheckForOverwrite::kYes);
2118     if (layer) {
2119         this->topDevice()->drawRect(r, layer->paint());
2120     }
2121 }
2122 
onDrawRegion(const SkRegion & region,const SkPaint & paint)2123 void SkCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
2124     const SkRect bounds = SkRect::Make(region.getBounds());
2125     if (this->internalQuickReject(bounds, paint)) {
2126         return;
2127     }
2128 
2129     auto layer = this->aboutToDraw(this, paint, &bounds);
2130     if (layer) {
2131         this->topDevice()->drawRegion(region, layer->paint());
2132     }
2133 }
2134 
onDrawBehind(const SkPaint & paint)2135 void SkCanvas::onDrawBehind(const SkPaint& paint) {
2136     SkBaseDevice* dev = this->topDevice();
2137     if (!dev) {
2138         return;
2139     }
2140 
2141     SkIRect bounds;
2142     SkDeque::Iter iter(fMCStack, SkDeque::Iter::kBack_IterStart);
2143     for (;;) {
2144         const MCRec* rec = (const MCRec*)iter.prev();
2145         if (!rec) {
2146             return; // no backimages, so nothing to draw
2147         }
2148         if (rec->fBackImage) {
2149             // drawBehind should only have been called when the saveBehind record is active;
2150             // if this fails, it means a real saveLayer was made w/o being restored first.
2151             SkASSERT(dev == rec->fDevice);
2152             bounds = SkIRect::MakeXYWH(rec->fBackImage->fLoc.fX, rec->fBackImage->fLoc.fY,
2153                                        rec->fBackImage->fImage->width(),
2154                                        rec->fBackImage->fImage->height());
2155             break;
2156         }
2157     }
2158 
2159     // The backimage location (and thus bounds) were defined in the device's space, so mark it
2160     // as a clip. We use a clip instead of just drawing a rect in case the paint has an image
2161     // filter on it (which is applied before any auto-layer so the filter is clipped).
2162     dev->save();
2163     {
2164         // We also have to temporarily whack the device matrix since clipRegion is affected by the
2165         // global-to-device matrix and clipRect is affected by the local-to-device.
2166         SkAutoDeviceTransformRestore adtr(dev, SkMatrix::I());
2167         dev->clipRect(SkRect::Make(bounds), SkClipOp::kIntersect, /* aa */ false);
2168         // ~adtr will reset the local-to-device matrix so that drawPaint() shades correctly.
2169     }
2170 
2171     auto layer = this->aboutToDraw(this, paint);
2172     if (layer) {
2173         this->topDevice()->drawPaint(layer->paint());
2174     }
2175 
2176     dev->restore(fMCRec->fMatrix);
2177 }
2178 
onDrawOval(const SkRect & oval,const SkPaint & paint)2179 void SkCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
2180     SkASSERT(oval.isSorted());
2181     if (this->internalQuickReject(oval, paint)) {
2182         return;
2183     }
2184 
2185     auto layer = this->aboutToDraw(this, paint, &oval);
2186     if (layer) {
2187         this->topDevice()->drawOval(oval, layer->paint());
2188     }
2189 }
2190 
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)2191 void SkCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle,
2192                          SkScalar sweepAngle, bool useCenter,
2193                          const SkPaint& paint) {
2194     SkASSERT(oval.isSorted());
2195     if (this->internalQuickReject(oval, paint)) {
2196         return;
2197     }
2198 
2199     auto layer = this->aboutToDraw(this, paint, &oval);
2200     if (layer) {
2201         this->topDevice()->drawArc(oval, startAngle, sweepAngle, useCenter, layer->paint());
2202     }
2203 }
2204 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)2205 void SkCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
2206     const SkRect& bounds = rrect.getBounds();
2207 
2208     // Delegating to simpler draw operations
2209     if (rrect.isRect()) {
2210         // call the non-virtual version
2211         this->SkCanvas::drawRect(bounds, paint);
2212         return;
2213     } else if (rrect.isOval()) {
2214         // call the non-virtual version
2215         this->SkCanvas::drawOval(bounds, paint);
2216         return;
2217     }
2218 
2219     if (this->internalQuickReject(bounds, paint)) {
2220         return;
2221     }
2222 
2223     auto layer = this->aboutToDraw(this, paint, &bounds);
2224     if (layer) {
2225         this->topDevice()->drawRRect(rrect, layer->paint());
2226     }
2227 }
2228 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)2229 void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
2230     const SkRect& bounds = outer.getBounds();
2231     if (this->internalQuickReject(bounds, paint)) {
2232         return;
2233     }
2234 
2235     auto layer = this->aboutToDraw(this, paint, &bounds);
2236     if (layer) {
2237         this->topDevice()->drawDRRect(outer, inner, layer->paint());
2238     }
2239 }
2240 
onDrawPath(const SkPath & path,const SkPaint & paint)2241 void SkCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
2242     if (!path.isFinite()) {
2243         return;
2244     }
2245 
2246     const SkRect& pathBounds = path.getBounds();
2247     if (!path.isInverseFillType() && this->internalQuickReject(pathBounds, paint)) {
2248         return;
2249     }
2250     if (path.isInverseFillType() && pathBounds.width() <= 0 && pathBounds.height() <= 0) {
2251         this->internalDrawPaint(paint);
2252         return;
2253     }
2254 
2255     auto layer = this->aboutToDraw(this, paint, path.isInverseFillType() ? nullptr : &pathBounds);
2256     if (layer) {
2257         this->topDevice()->drawPath(path, layer->paint());
2258     }
2259 }
2260 
canDrawBitmapAsSprite(SkScalar x,SkScalar y,int w,int h,const SkSamplingOptions & sampling,const SkPaint & paint)2261 bool SkCanvas::canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h,
2262                                      const SkSamplingOptions& sampling, const SkPaint& paint) {
2263     if (!paint.getImageFilter()) {
2264         return false;
2265     }
2266 
2267     const SkMatrix& ctm = this->getTotalMatrix();
2268     if (!SkTreatAsSprite(ctm, SkISize::Make(w, h), sampling, paint.isAntiAlias())) {
2269         return false;
2270     }
2271 
2272     // The other paint effects need to be applied before the image filter, but the sprite draw
2273     // applies the filter explicitly first.
2274     if (paint.getAlphaf() < 1.f || paint.getColorFilter() || paint.getMaskFilter()) {
2275         return false;
2276     }
2277     // Currently we can only use the filterSprite code if we are clipped to the bitmap's bounds.
2278     // Once we can filter and the filter will return a result larger than itself, we should be
2279     // able to remove this constraint.
2280     // skbug.com/4526
2281     //
2282     SkPoint pt;
2283     ctm.mapXY(x, y, &pt);
2284     SkIRect ir = SkIRect::MakeXYWH(SkScalarRoundToInt(pt.x()), SkScalarRoundToInt(pt.y()), w, h);
2285     // quick bounds have been outset by 1px compared to overall device bounds, so this makes the
2286     // contains check equivalent to between ir and device bounds
2287     ir.outset(1, 1);
2288     return ir.contains(fQuickRejectBounds);
2289 }
2290 
2291 // Clean-up the paint to match the drawing semantics for drawImage et al. (skbug.com/7804).
clean_paint_for_drawImage(const SkPaint * paint)2292 static SkPaint clean_paint_for_drawImage(const SkPaint* paint) {
2293     SkPaint cleaned;
2294     if (paint) {
2295         cleaned = *paint;
2296         cleaned.setStyle(SkPaint::kFill_Style);
2297         cleaned.setPathEffect(nullptr);
2298     }
2299     return cleaned;
2300 }
2301 
2302 // drawVertices fills triangles and ignores mask filter and path effect,
2303 // so canonicalize the paint before checking quick reject.
clean_paint_for_drawVertices(SkPaint paint)2304 static SkPaint clean_paint_for_drawVertices(SkPaint paint) {
2305     paint.setStyle(SkPaint::kFill_Style);
2306     paint.setMaskFilter(nullptr);
2307     paint.setPathEffect(nullptr);
2308     return paint;
2309 }
2310 
onDrawImage2(const SkImage * image,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)2311 void SkCanvas::onDrawImage2(const SkImage* image, SkScalar x, SkScalar y,
2312                             const SkSamplingOptions& sampling, const SkPaint* paint) {
2313     SkPaint realPaint = clean_paint_for_drawImage(paint);
2314 
2315     SkRect bounds = SkRect::MakeXYWH(x, y, image->width(), image->height());
2316     if (this->internalQuickReject(bounds, realPaint)) {
2317         return;
2318     }
2319 
2320     if (realPaint.getImageFilter() &&
2321         this->canDrawBitmapAsSprite(x, y, image->width(), image->height(), sampling, realPaint) &&
2322         !image_to_color_filter(&realPaint)) {
2323         // Evaluate the image filter directly on the input image and then draw the result, instead
2324         // of first drawing the image to a temporary layer and filtering.
2325         SkBaseDevice* device = this->topDevice();
2326         sk_sp<SkSpecialImage> special;
2327         if ((special = device->makeSpecial(image))) {
2328             sk_sp<SkImageFilter> filter = realPaint.refImageFilter();
2329             realPaint.setImageFilter(nullptr);
2330 
2331             // TODO(michaelludwig) - Many filters could probably be evaluated like this even if the
2332             // CTM is not translate-only; the post-transformation of the filtered image by the CTM
2333             // will probably look just as good and not require an extra layer.
2334             // TODO(michaelludwig) - Once image filter implementations can support source images
2335             // with non-(0,0) origins, we can just mark the origin as (x,y) instead of doing a
2336             // pre-concat here.
2337             SkMatrix layerToDevice = device->localToDevice();
2338             layerToDevice.preTranslate(x, y);
2339 
2340             SkMatrix deviceToLayer;
2341             if (!layerToDevice.invert(&deviceToLayer)) {
2342                 return; // bad ctm, draw nothing
2343             }
2344 
2345             skif::Mapping mapping(layerToDevice, deviceToLayer, SkMatrix::Translate(-x, -y));
2346 
2347             if (this->predrawNotify()) {
2348                 // While we are skipping an initial layer, evaluate the rest of the image filter
2349                 // pipeline in the same color format as we would have if there was a layer.
2350                 const auto filterColorType = image_filter_color_type(device->imageInfo());
2351                 device->drawFilteredImage(mapping, special.get(), filterColorType, filter.get(),
2352                                           sampling,realPaint);
2353             }
2354             return;
2355         } // else fall through to regular drawing path
2356     }
2357 
2358     auto layer = this->aboutToDraw(this, realPaint, &bounds);
2359     if (layer) {
2360         this->topDevice()->drawImageRect(image, nullptr, bounds, sampling,
2361                                          layer->paint(), kFast_SrcRectConstraint);
2362     }
2363 }
2364 
clean_sampling_for_constraint(const SkSamplingOptions & sampling,SkCanvas::SrcRectConstraint constraint)2365 static SkSamplingOptions clean_sampling_for_constraint(
2366         const SkSamplingOptions& sampling,
2367         SkCanvas::SrcRectConstraint constraint) {
2368     if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
2369         if (sampling.mipmap != SkMipmapMode::kNone) {
2370             return SkSamplingOptions(sampling.filter);
2371         }
2372         if (sampling.isAniso()) {
2373             return SkSamplingOptions(SkFilterMode::kLinear);
2374         }
2375     }
2376     return sampling;
2377 }
2378 
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)2379 void SkCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
2380                                 const SkSamplingOptions& sampling, const SkPaint* paint,
2381                                 SrcRectConstraint constraint) {
2382     SkPaint realPaint = clean_paint_for_drawImage(paint);
2383     SkSamplingOptions realSampling = clean_sampling_for_constraint(sampling, constraint);
2384 
2385     if (this->internalQuickReject(dst, realPaint)) {
2386         return;
2387     }
2388 
2389     auto layer = this->aboutToDraw(this, realPaint, &dst, CheckForOverwrite::kYes,
2390                                    image->isOpaque() ? kOpaque_ShaderOverrideOpacity
2391                                                      : kNotOpaque_ShaderOverrideOpacity);
2392     if (layer) {
2393         this->topDevice()->drawImageRect(image, &src, dst, realSampling, layer->paint(), constraint);
2394     }
2395 }
2396 
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)2397 void SkCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice, const SkRect& dst,
2398                                    SkFilterMode filter, const SkPaint* paint) {
2399     SkPaint realPaint = clean_paint_for_drawImage(paint);
2400 
2401     if (this->internalQuickReject(dst, realPaint)) {
2402         return;
2403     }
2404 
2405     auto layer = this->aboutToDraw(this, realPaint, &dst);
2406     if (layer) {
2407         this->topDevice()->drawImageLattice(image, lattice, dst, filter, layer->paint());
2408     }
2409 }
2410 
drawImage(const SkImage * image,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)2411 void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y,
2412                          const SkSamplingOptions& sampling, const SkPaint* paint) {
2413     TRACE_EVENT0("skia", TRACE_FUNC);
2414     RETURN_ON_NULL(image);
2415     this->onDrawImage2(image, x, y, sampling, paint);
2416 }
2417 
drawImageRect(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)2418 void SkCanvas::drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
2419                              const SkSamplingOptions& sampling, const SkPaint* paint,
2420                              SrcRectConstraint constraint) {
2421     RETURN_ON_NULL(image);
2422     if (!fillable(dst) || !fillable(src)) {
2423         return;
2424     }
2425     this->onDrawImageRect2(image, src, dst, sampling, paint, constraint);
2426 }
2427 
drawImageRect(const SkImage * image,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint)2428 void SkCanvas::drawImageRect(const SkImage* image, const SkRect& dst,
2429                              const SkSamplingOptions& sampling, const SkPaint* paint) {
2430     RETURN_ON_NULL(image);
2431     this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), dst, sampling,
2432                         paint, kFast_SrcRectConstraint);
2433 }
2434 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)2435 void SkCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
2436                               const SkPaint& paint) {
2437     auto glyphRunList = fScratchGlyphRunBuilder->blobToGlyphRunList(*blob, {x, y});
2438     this->onDrawGlyphRunList(glyphRunList, paint);
2439 }
2440 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)2441 void SkCanvas::onDrawGlyphRunList(const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
2442     SkRect bounds = glyphRunList.sourceBoundsWithOrigin();
2443     if (this->internalQuickReject(bounds, paint)) {
2444         return;
2445     }
2446     auto layer = this->aboutToDraw(this, paint, &bounds);
2447     if (layer) {
2448         this->topDevice()->drawGlyphRunList(this, glyphRunList, paint, layer->paint());
2449     }
2450 }
2451 
2452 #if (defined(SK_GANESH) || defined(SK_GRAPHITE))
convertBlobToSlug(const SkTextBlob & blob,SkPoint origin,const SkPaint & paint)2453 sk_sp<Slug> SkCanvas::convertBlobToSlug(
2454         const SkTextBlob& blob, SkPoint origin, const SkPaint& paint) {
2455     TRACE_EVENT0("skia", TRACE_FUNC);
2456     auto glyphRunList = fScratchGlyphRunBuilder->blobToGlyphRunList(blob, origin);
2457     return this->onConvertGlyphRunListToSlug(glyphRunList, paint);
2458 }
2459 
2460 sk_sp<Slug>
onConvertGlyphRunListToSlug(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)2461 SkCanvas::onConvertGlyphRunListToSlug(
2462         const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
2463     SkRect bounds = glyphRunList.sourceBoundsWithOrigin();
2464     if (bounds.isEmpty() || !bounds.isFinite() || paint.nothingToDraw()) {
2465         return nullptr;
2466     }
2467     auto layer = this->aboutToDraw(this, paint, &bounds);
2468     if (layer) {
2469         return this->topDevice()->convertGlyphRunListToSlug(glyphRunList, paint, layer->paint());
2470     }
2471     return nullptr;
2472 }
2473 
drawSlug(const Slug * slug)2474 void SkCanvas::drawSlug(const Slug* slug) {
2475     TRACE_EVENT0("skia", TRACE_FUNC);
2476     if (slug) {
2477         this->onDrawSlug(slug);
2478     }
2479 }
2480 
onDrawSlug(const Slug * slug)2481 void SkCanvas::onDrawSlug(const Slug* slug) {
2482     SkRect bounds = slug->sourceBoundsWithOrigin();
2483     if (this->internalQuickReject(bounds, slug->initialPaint())) {
2484         return;
2485     }
2486 
2487     auto layer = this->aboutToDraw(this, slug->initialPaint(), &bounds);
2488     if (layer) {
2489         this->topDevice()->drawSlug(this, slug, layer->paint());
2490     }
2491 }
2492 #endif
2493 
2494 // These call the (virtual) onDraw... method
drawSimpleText(const void * text,size_t byteLength,SkTextEncoding encoding,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)2495 void SkCanvas::drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding,
2496                               SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) {
2497     TRACE_EVENT0("skia", TRACE_FUNC);
2498     if (byteLength) {
2499         sk_msan_assert_initialized(text, SkTAddOffset<const void>(text, byteLength));
2500         const sktext::GlyphRunList& glyphRunList =
2501             fScratchGlyphRunBuilder->textToGlyphRunList(
2502                     font, paint, text, byteLength, {x, y}, encoding);
2503         if (!glyphRunList.empty()) {
2504             this->onDrawGlyphRunList(glyphRunList, paint);
2505         }
2506     }
2507 }
2508 
drawGlyphs(int count,const SkGlyphID * glyphs,const SkPoint * positions,const uint32_t * clusters,int textByteCount,const char * utf8text,SkPoint origin,const SkFont & font,const SkPaint & paint)2509 void SkCanvas::drawGlyphs(int count, const SkGlyphID* glyphs, const SkPoint* positions,
2510                           const uint32_t* clusters, int textByteCount, const char* utf8text,
2511                           SkPoint origin, const SkFont& font, const SkPaint& paint) {
2512     if (count <= 0) { return; }
2513 
2514     sktext::GlyphRun glyphRun {
2515             font,
2516             SkSpan(positions, count),
2517             SkSpan(glyphs, count),
2518             SkSpan(utf8text, textByteCount),
2519             SkSpan(clusters, count),
2520             SkSpan<SkVector>()
2521     };
2522 
2523     sktext::GlyphRunList glyphRunList = fScratchGlyphRunBuilder->makeGlyphRunList(
2524             glyphRun, paint, origin);
2525     this->onDrawGlyphRunList(glyphRunList, paint);
2526 }
2527 
drawGlyphs(int count,const SkGlyphID glyphs[],const SkPoint positions[],SkPoint origin,const SkFont & font,const SkPaint & paint)2528 void SkCanvas::drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[],
2529                           SkPoint origin, const SkFont& font, const SkPaint& paint) {
2530     if (count <= 0) { return; }
2531 
2532     sktext::GlyphRun glyphRun {
2533         font,
2534         SkSpan(positions, count),
2535         SkSpan(glyphs, count),
2536         SkSpan<const char>(),
2537         SkSpan<const uint32_t>(),
2538         SkSpan<SkVector>()
2539     };
2540 
2541     sktext::GlyphRunList glyphRunList = fScratchGlyphRunBuilder->makeGlyphRunList(
2542             glyphRun, paint, origin);
2543     this->onDrawGlyphRunList(glyphRunList, paint);
2544 }
2545 
drawGlyphs(int count,const SkGlyphID glyphs[],const SkRSXform xforms[],SkPoint origin,const SkFont & font,const SkPaint & paint)2546 void SkCanvas::drawGlyphs(int count, const SkGlyphID glyphs[], const SkRSXform xforms[],
2547                           SkPoint origin, const SkFont& font, const SkPaint& paint) {
2548     if (count <= 0) { return; }
2549 
2550     auto [positions, rotateScales] =
2551             fScratchGlyphRunBuilder->convertRSXForm(SkSpan(xforms, count));
2552 
2553     sktext::GlyphRun glyphRun {
2554             font,
2555             positions,
2556             SkSpan(glyphs, count),
2557             SkSpan<const char>(),
2558             SkSpan<const uint32_t>(),
2559             rotateScales
2560     };
2561     sktext::GlyphRunList glyphRunList = fScratchGlyphRunBuilder->makeGlyphRunList(
2562             glyphRun, paint, origin);
2563     this->onDrawGlyphRunList(glyphRunList, paint);
2564 }
2565 
2566 #if defined(SK_GANESH) && GR_TEST_UTILS
2567 bool gSkBlobAsSlugTesting = false;
2568 #endif
2569 
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)2570 void SkCanvas::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
2571                             const SkPaint& paint) {
2572     TRACE_EVENT0("skia", TRACE_FUNC);
2573     RETURN_ON_NULL(blob);
2574     RETURN_ON_FALSE(blob->bounds().makeOffset(x, y).isFinite());
2575 
2576     // Overflow if more than 2^21 glyphs stopping a buffer overflow latter in the stack.
2577     // See chromium:1080481
2578     // TODO: can consider unrolling a few at a time if this limit becomes a problem.
2579     int totalGlyphCount = 0;
2580     constexpr int kMaxGlyphCount = 1 << 21;
2581     SkTextBlob::Iter i(*blob);
2582     SkTextBlob::Iter::Run r;
2583     while (i.next(&r)) {
2584         int glyphsLeft = kMaxGlyphCount - totalGlyphCount;
2585         RETURN_ON_FALSE(r.fGlyphCount <= glyphsLeft);
2586         totalGlyphCount += r.fGlyphCount;
2587     }
2588 
2589 #if defined(SK_GANESH) && GR_TEST_UTILS
2590     // Draw using text blob normally or if the blob has RSX form because slugs can't convert that
2591     // form.
2592     if (!gSkBlobAsSlugTesting ||
2593         this->topDevice()->asGaneshDevice() == nullptr ||
2594         SkTextBlobPriv::HasRSXForm(*blob))
2595 #endif
2596     {
2597         this->onDrawTextBlob(blob, x, y, paint);
2598     }
2599 #if defined(SK_GANESH) && GR_TEST_UTILS
2600     else {
2601         auto slug = Slug::ConvertBlob(this, *blob, {x, y}, paint);
2602         slug->draw(this);
2603     }
2604 #endif
2605 }
2606 
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)2607 void SkCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
2608                                     const SkPaint& paint) {
2609     SkPaint simplePaint = clean_paint_for_drawVertices(paint);
2610 
2611     const SkRect& bounds = vertices->bounds();
2612     if (this->internalQuickReject(bounds, simplePaint)) {
2613         return;
2614     }
2615 
2616     auto layer = this->aboutToDraw(this, simplePaint, &bounds);
2617     if (layer) {
2618         this->topDevice()->drawVertices(vertices, SkBlender::Mode(bmode), layer->paint());
2619     }
2620 }
2621 
2622 #ifdef SK_ENABLE_SKSL
onDrawMesh(const SkMesh & mesh,sk_sp<SkBlender> blender,const SkPaint & paint)2623 void SkCanvas::onDrawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
2624     SkPaint simplePaint = clean_paint_for_drawVertices(paint);
2625 
2626     if (this->internalQuickReject(mesh.bounds(), simplePaint)) {
2627         return;
2628     }
2629 
2630     auto layer = this->aboutToDraw(this, simplePaint, nullptr);
2631     if (layer) {
2632         this->topDevice()->drawMesh(mesh, std::move(blender), paint);
2633     }
2634 }
2635 #endif
2636 
drawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)2637 void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4],
2638                          const SkPoint texCoords[4], SkBlendMode bmode,
2639                          const SkPaint& paint) {
2640     TRACE_EVENT0("skia", TRACE_FUNC);
2641     if (nullptr == cubics) {
2642         return;
2643     }
2644 
2645     this->onDrawPatch(cubics, colors, texCoords, bmode, paint);
2646 }
2647 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)2648 void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
2649                            const SkPoint texCoords[4], SkBlendMode bmode,
2650                            const SkPaint& paint) {
2651     // drawPatch has the same behavior restrictions as drawVertices
2652     SkPaint simplePaint = clean_paint_for_drawVertices(paint);
2653 
2654     // Since a patch is always within the convex hull of the control points, we discard it when its
2655     // bounding rectangle is completely outside the current clip.
2656     SkRect bounds;
2657     bounds.setBounds(cubics, SkPatchUtils::kNumCtrlPts);
2658     if (this->internalQuickReject(bounds, simplePaint)) {
2659         return;
2660     }
2661 
2662     auto layer = this->aboutToDraw(this, simplePaint, &bounds);
2663     if (layer) {
2664         this->topDevice()->drawPatch(cubics, colors, texCoords, SkBlender::Mode(bmode),
2665                                      layer->paint());
2666     }
2667 }
2668 
drawDrawable(SkDrawable * dr,SkScalar x,SkScalar y)2669 void SkCanvas::drawDrawable(SkDrawable* dr, SkScalar x, SkScalar y) {
2670 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
2671     TRACE_EVENT0("skia", TRACE_FUNC);
2672 #endif
2673     RETURN_ON_NULL(dr);
2674     if (x || y) {
2675         SkMatrix matrix = SkMatrix::Translate(x, y);
2676         this->onDrawDrawable(dr, &matrix);
2677     } else {
2678         this->onDrawDrawable(dr, nullptr);
2679     }
2680 }
2681 
drawDrawable(SkDrawable * dr,const SkMatrix * matrix)2682 void SkCanvas::drawDrawable(SkDrawable* dr, const SkMatrix* matrix) {
2683 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
2684     TRACE_EVENT0("skia", TRACE_FUNC);
2685 #endif
2686     RETURN_ON_NULL(dr);
2687     if (matrix && matrix->isIdentity()) {
2688         matrix = nullptr;
2689     }
2690     this->onDrawDrawable(dr, matrix);
2691 }
2692 
onDrawDrawable(SkDrawable * dr,const SkMatrix * matrix)2693 void SkCanvas::onDrawDrawable(SkDrawable* dr, const SkMatrix* matrix) {
2694     // drawable bounds are no longer reliable (e.g. android displaylist)
2695     // so don't use them for quick-reject
2696     if (this->predrawNotify()) {
2697         this->topDevice()->drawDrawable(this, dr, matrix);
2698     }
2699 }
2700 
onDrawAtlas2(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode bmode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)2701 void SkCanvas::onDrawAtlas2(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
2702                             const SkColor colors[], int count, SkBlendMode bmode,
2703                             const SkSamplingOptions& sampling, const SkRect* cull,
2704                             const SkPaint* paint) {
2705     // drawAtlas is a combination of drawVertices and drawImage...
2706     SkPaint realPaint = clean_paint_for_drawVertices(clean_paint_for_drawImage(paint));
2707     realPaint.setShader(atlas->makeShader(sampling));
2708 
2709     if (cull && this->internalQuickReject(*cull, realPaint)) {
2710         return;
2711     }
2712 
2713     auto layer = this->aboutToDraw(this, realPaint);
2714     if (layer) {
2715         this->topDevice()->drawAtlas(xform, tex, colors, count, SkBlender::Mode(bmode),
2716                                      layer->paint());
2717     }
2718 }
2719 
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)2720 void SkCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
2721     SkASSERT(key);
2722 
2723     if (this->predrawNotify()) {
2724         this->topDevice()->drawAnnotation(rect, key, value);
2725     }
2726 }
2727 
onDrawEdgeAAQuad(const SkRect & r,const SkPoint clip[4],QuadAAFlags edgeAA,const SkColor4f & color,SkBlendMode mode)2728 void SkCanvas::onDrawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], QuadAAFlags edgeAA,
2729                                 const SkColor4f& color, SkBlendMode mode) {
2730     SkASSERT(r.isSorted());
2731 
2732     SkPaint paint{color};
2733     paint.setBlendMode(mode);
2734     if (this->internalQuickReject(r, paint)) {
2735         return;
2736     }
2737 
2738     if (this->predrawNotify()) {
2739         this->topDevice()->drawEdgeAAQuad(r, clip, edgeAA, color, mode);
2740     }
2741 }
2742 
onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)2743 void SkCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count,
2744                                      const SkPoint dstClips[], const SkMatrix preViewMatrices[],
2745                                      const SkSamplingOptions& sampling, const SkPaint* paint,
2746                                      SrcRectConstraint constraint) {
2747     if (count <= 0) {
2748         // Nothing to draw
2749         return;
2750     }
2751 
2752     SkPaint realPaint = clean_paint_for_drawImage(paint);
2753     SkSamplingOptions realSampling = clean_sampling_for_constraint(sampling, constraint);
2754 
2755     // We could calculate the set's dstRect union to always check quickReject(), but we can't reject
2756     // individual entries and Chromium's occlusion culling already makes it likely that at least one
2757     // entry will be visible. So, we only calculate the draw bounds when it's trivial (count == 1),
2758     // or we need it for the autolooper (since it greatly improves image filter perf).
2759     bool needsAutoLayer = SkToBool(realPaint.getImageFilter());
2760     bool setBoundsValid = count == 1 || needsAutoLayer;
2761     SkRect setBounds = imageSet[0].fDstRect;
2762     if (imageSet[0].fMatrixIndex >= 0) {
2763         // Account for the per-entry transform that is applied prior to the CTM when drawing
2764         preViewMatrices[imageSet[0].fMatrixIndex].mapRect(&setBounds);
2765     }
2766     if (needsAutoLayer) {
2767         for (int i = 1; i < count; ++i) {
2768             SkRect entryBounds = imageSet[i].fDstRect;
2769             if (imageSet[i].fMatrixIndex >= 0) {
2770                 preViewMatrices[imageSet[i].fMatrixIndex].mapRect(&entryBounds);
2771             }
2772             setBounds.joinPossiblyEmptyRect(entryBounds);
2773         }
2774     }
2775 
2776     // If we happen to have the draw bounds, though, might as well check quickReject().
2777     if (setBoundsValid && this->internalQuickReject(setBounds, realPaint)) {
2778         return;
2779     }
2780 
2781     auto layer = this->aboutToDraw(this, realPaint, setBoundsValid ? &setBounds : nullptr);
2782     if (layer) {
2783         this->topDevice()->drawEdgeAAImageSet(imageSet, count, dstClips, preViewMatrices,
2784                                               realSampling, layer->paint(), constraint);
2785     }
2786 }
2787 
2788 //////////////////////////////////////////////////////////////////////////////
2789 // These methods are NOT virtual, and therefore must call back into virtual
2790 // methods, rather than actually drawing themselves.
2791 //////////////////////////////////////////////////////////////////////////////
2792 
drawColor(const SkColor4f & c,SkBlendMode mode)2793 void SkCanvas::drawColor(const SkColor4f& c, SkBlendMode mode) {
2794     SkPaint paint;
2795     paint.setColor(c);
2796     paint.setBlendMode(mode);
2797     this->drawPaint(paint);
2798 }
2799 
drawPoint(SkScalar x,SkScalar y,const SkPaint & paint)2800 void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) {
2801     const SkPoint pt = { x, y };
2802     this->drawPoints(kPoints_PointMode, 1, &pt, paint);
2803 }
2804 
drawLine(SkScalar x0,SkScalar y0,SkScalar x1,SkScalar y1,const SkPaint & paint)2805 void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint) {
2806     SkPoint pts[2];
2807     pts[0].set(x0, y0);
2808     pts[1].set(x1, y1);
2809     this->drawPoints(kLines_PointMode, 2, pts, paint);
2810 }
2811 
drawCircle(SkScalar cx,SkScalar cy,SkScalar radius,const SkPaint & paint)2812 void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint) {
2813     if (radius < 0) {
2814         radius = 0;
2815     }
2816 
2817     SkRect  r;
2818     r.setLTRB(cx - radius, cy - radius, cx + radius, cy + radius);
2819     this->drawOval(r, paint);
2820 }
2821 
drawRoundRect(const SkRect & r,SkScalar rx,SkScalar ry,const SkPaint & paint)2822 void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry,
2823                              const SkPaint& paint) {
2824     if (rx > 0 && ry > 0) {
2825         SkRRect rrect;
2826         rrect.setRectXY(r, rx, ry);
2827         this->drawRRect(rrect, paint);
2828     } else {
2829         this->drawRect(r, paint);
2830     }
2831 }
2832 
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)2833 void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle,
2834                        SkScalar sweepAngle, bool useCenter,
2835                        const SkPaint& paint) {
2836     TRACE_EVENT0("skia", TRACE_FUNC);
2837     if (oval.isEmpty() || !sweepAngle) {
2838         return;
2839     }
2840     this->onDrawArc(oval, startAngle, sweepAngle, useCenter, paint);
2841 }
2842 
2843 ///////////////////////////////////////////////////////////////////////////////
2844 #ifdef SK_DISABLE_SKPICTURE
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)2845 void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) {}
2846 
2847 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)2848 void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
2849                              const SkPaint* paint) {}
2850 #else
2851 
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)2852 void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) {
2853     TRACE_EVENT0("skia", TRACE_FUNC);
2854     RETURN_ON_NULL(picture);
2855 
2856     if (matrix && matrix->isIdentity()) {
2857         matrix = nullptr;
2858     }
2859     if (picture->approximateOpCount() <= kMaxPictureOpsToUnrollInsteadOfRef) {
2860         SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
2861         picture->playback(this);
2862     } else {
2863         this->onDrawPicture(picture, matrix, paint);
2864     }
2865 }
2866 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)2867 void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
2868                              const SkPaint* paint) {
2869     if (this->internalQuickReject(picture->cullRect(), paint ? *paint : SkPaint{}, matrix)) {
2870         return;
2871     }
2872 
2873     SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
2874     picture->playback(this);
2875 }
2876 #endif
2877 
2878 ///////////////////////////////////////////////////////////////////////////////
2879 
2880 SkCanvas::ImageSetEntry::ImageSetEntry() = default;
2881 SkCanvas::ImageSetEntry::~ImageSetEntry() = default;
2882 SkCanvas::ImageSetEntry::ImageSetEntry(const ImageSetEntry&) = default;
2883 SkCanvas::ImageSetEntry& SkCanvas::ImageSetEntry::operator=(const ImageSetEntry&) = default;
2884 
ImageSetEntry(sk_sp<const SkImage> image,const SkRect & srcRect,const SkRect & dstRect,int matrixIndex,float alpha,unsigned aaFlags,bool hasClip)2885 SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect,
2886                                        const SkRect& dstRect, int matrixIndex, float alpha,
2887                                        unsigned aaFlags, bool hasClip)
2888                 : fImage(std::move(image))
2889                 , fSrcRect(srcRect)
2890                 , fDstRect(dstRect)
2891                 , fMatrixIndex(matrixIndex)
2892                 , fAlpha(alpha)
2893                 , fAAFlags(aaFlags)
2894                 , fHasClip(hasClip) {}
2895 
ImageSetEntry(sk_sp<const SkImage> image,const SkRect & srcRect,const SkRect & dstRect,float alpha,unsigned aaFlags)2896 SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect,
2897                                        const SkRect& dstRect, float alpha, unsigned aaFlags)
2898                 : fImage(std::move(image))
2899                 , fSrcRect(srcRect)
2900                 , fDstRect(dstRect)
2901                 , fAlpha(alpha)
2902                 , fAAFlags(aaFlags) {}
2903 
2904 ///////////////////////////////////////////////////////////////////////////////
2905 
MakeRasterDirect(const SkImageInfo & info,void * pixels,size_t rowBytes,const SkSurfaceProps * props)2906 std::unique_ptr<SkCanvas> SkCanvas::MakeRasterDirect(const SkImageInfo& info, void* pixels,
2907                                                      size_t rowBytes, const SkSurfaceProps* props) {
2908     if (!SkSurfaceValidateRasterInfo(info, rowBytes)) {
2909         return nullptr;
2910     }
2911 
2912     SkBitmap bitmap;
2913     if (!bitmap.installPixels(info, pixels, rowBytes)) {
2914         return nullptr;
2915     }
2916 
2917     return props ?
2918         std::make_unique<SkCanvas>(bitmap, *props) :
2919         std::make_unique<SkCanvas>(bitmap);
2920 }
2921 
2922 ///////////////////////////////////////////////////////////////////////////////
2923 
SkNoDrawCanvas(int width,int height)2924 SkNoDrawCanvas::SkNoDrawCanvas(int width, int height)
2925     : INHERITED(SkIRect::MakeWH(width, height)) {}
2926 
SkNoDrawCanvas(const SkIRect & bounds)2927 SkNoDrawCanvas::SkNoDrawCanvas(const SkIRect& bounds)
2928     : INHERITED(bounds) {}
2929 
SkNoDrawCanvas(sk_sp<SkBaseDevice> device)2930 SkNoDrawCanvas::SkNoDrawCanvas(sk_sp<SkBaseDevice> device)
2931     : INHERITED(device) {}
2932 
getSaveLayerStrategy(const SaveLayerRec & rec)2933 SkCanvas::SaveLayerStrategy SkNoDrawCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
2934     (void)this->INHERITED::getSaveLayerStrategy(rec);
2935     return kNoLayer_SaveLayerStrategy;
2936 }
2937 
onDoSaveBehind(const SkRect *)2938 bool SkNoDrawCanvas::onDoSaveBehind(const SkRect*) {
2939     return false;
2940 }
2941 
2942 ///////////////////////////////////////////////////////////////////////////////
2943 
2944 static_assert((int)SkRegion::kDifference_Op == (int)SkClipOp::kDifference, "");
2945 static_assert((int)SkRegion::kIntersect_Op  == (int)SkClipOp::kIntersect, "");
2946 
2947 ///////////////////////////////////////////////////////////////////////////////////////////////////
2948 
accessTopRasterHandle() const2949 SkRasterHandleAllocator::Handle SkCanvas::accessTopRasterHandle() const {
2950     const SkBaseDevice* dev = this->topDevice();
2951     if (fAllocator) {
2952         SkRasterHandleAllocator::Handle handle = dev->getRasterHandle();
2953         SkIRect clip = dev->devClipBounds();
2954         if (!clip.intersect({0, 0, dev->width(), dev->height()})) {
2955             clip.setEmpty();
2956         }
2957 
2958         fAllocator->updateHandle(handle, dev->localToDevice(), clip);
2959         return handle;
2960     }
2961     return nullptr;
2962 }
2963 
install(SkBitmap * bm,const SkImageInfo & info,const SkRasterHandleAllocator::Rec & rec)2964 static bool install(SkBitmap* bm, const SkImageInfo& info,
2965                     const SkRasterHandleAllocator::Rec& rec) {
2966     return bm->installPixels(info, rec.fPixels, rec.fRowBytes, rec.fReleaseProc, rec.fReleaseCtx);
2967 }
2968 
allocBitmap(const SkImageInfo & info,SkBitmap * bm)2969 SkRasterHandleAllocator::Handle SkRasterHandleAllocator::allocBitmap(const SkImageInfo& info,
2970                                                                      SkBitmap* bm) {
2971     SkRasterHandleAllocator::Rec rec;
2972     if (!this->allocHandle(info, &rec) || !install(bm, info, rec)) {
2973         return nullptr;
2974     }
2975     return rec.fHandle;
2976 }
2977 
2978 std::unique_ptr<SkCanvas>
MakeCanvas(std::unique_ptr<SkRasterHandleAllocator> alloc,const SkImageInfo & info,const Rec * rec,const SkSurfaceProps * props)2979 SkRasterHandleAllocator::MakeCanvas(std::unique_ptr<SkRasterHandleAllocator> alloc,
2980                                     const SkImageInfo& info, const Rec* rec,
2981                                     const SkSurfaceProps* props) {
2982     if (!alloc || !SkSurfaceValidateRasterInfo(info, rec ? rec->fRowBytes : kIgnoreRowBytesValue)) {
2983         return nullptr;
2984     }
2985 
2986     SkBitmap bm;
2987     Handle hndl;
2988 
2989     if (rec) {
2990         hndl = install(&bm, info, *rec) ? rec->fHandle : nullptr;
2991     } else {
2992         hndl = alloc->allocBitmap(info, &bm);
2993     }
2994     return hndl ? std::unique_ptr<SkCanvas>(new SkCanvas(bm, std::move(alloc), hndl, props))
2995                 : nullptr;
2996 }
2997 
2998 ////////////////////////////////////////////////////////////////////////////////////////////////////
2999 #if defined(SK_GANESH) && GR_TEST_UTILS
SkTestCanvas(SkCanvas * canvas)3000 SkTestCanvas<SkSlugTestKey>::SkTestCanvas(SkCanvas* canvas)
3001         : SkCanvas(sk_ref_sp(canvas->baseDevice())) {}
3002 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)3003 void SkTestCanvas<SkSlugTestKey>::onDrawGlyphRunList(
3004         const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
3005     SkRect bounds = glyphRunList.sourceBoundsWithOrigin();
3006     if (this->internalQuickReject(bounds, paint)) {
3007         return;
3008     }
3009     auto layer = this->aboutToDraw(this, paint, &bounds);
3010     if (layer) {
3011         if (glyphRunList.hasRSXForm()) {
3012             this->SkCanvas::onDrawGlyphRunList(glyphRunList, layer->paint());
3013         } else {
3014             auto slug = this->onConvertGlyphRunListToSlug(glyphRunList, layer->paint());
3015             this->drawSlug(slug.get());
3016         }
3017     }
3018 }
3019 
SkTestCanvas(SkCanvas * canvas)3020 SkTestCanvas<SkSerializeSlugTestKey>::SkTestCanvas(SkCanvas* canvas)
3021         : SkCanvas(sk_ref_sp(canvas->baseDevice())) {}
3022 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)3023 void SkTestCanvas<SkSerializeSlugTestKey>::onDrawGlyphRunList(
3024         const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
3025     SkRect bounds = glyphRunList.sourceBoundsWithOrigin();
3026     if (this->internalQuickReject(bounds, paint)) {
3027         return;
3028     }
3029     auto layer = this->aboutToDraw(this, paint, &bounds);
3030     if (layer) {
3031         if (glyphRunList.hasRSXForm()) {
3032             this->SkCanvas::onDrawGlyphRunList(glyphRunList, layer->paint());
3033         } else {
3034             sk_sp<SkData> bytes;
3035             {
3036                 auto slug = this->onConvertGlyphRunListToSlug(glyphRunList, layer->paint());
3037                 if (slug != nullptr) {
3038                     bytes = slug->serialize();
3039                 }
3040             }
3041             {
3042                 if (bytes != nullptr) {
3043                     auto slug = Slug::Deserialize(bytes->data(), bytes->size());
3044                     this->drawSlug(slug.get());
3045                 }
3046             }
3047         }
3048     }
3049 }
3050 
3051 // A do nothing handle manager for the remote strike server.
3052 class ServerHandleManager : public SkStrikeServer::DiscardableHandleManager {
3053 public:
createHandle()3054     SkDiscardableHandleId createHandle() override {
3055         return 0;
3056     }
3057 
lockHandle(SkDiscardableHandleId id)3058     bool lockHandle(SkDiscardableHandleId id) override {
3059         return true;
3060     }
3061 
isHandleDeleted(SkDiscardableHandleId id)3062     bool isHandleDeleted(SkDiscardableHandleId id) override {
3063         return false;
3064     }
3065 };
3066 
3067 // Lock the strikes into the cache for the length of the test. This handler is tied to the lifetime
3068 // of the canvas used to render the entire test.
3069 class ClientHandleManager : public SkStrikeClient::DiscardableHandleManager {
3070 public:
deleteHandle(SkDiscardableHandleId id)3071     bool deleteHandle(SkDiscardableHandleId id) override {
3072         return fIsLocked;
3073     }
3074 
assertHandleValid(SkDiscardableHandleId id)3075     void assertHandleValid(SkDiscardableHandleId id) override {
3076         DiscardableHandleManager::assertHandleValid(id);
3077     }
3078 
notifyCacheMiss(SkStrikeClient::CacheMissType type,int fontSize)3079     void notifyCacheMiss(SkStrikeClient::CacheMissType type, int fontSize) override {
3080 
3081     }
3082 
notifyReadFailure(const ReadFailureData & data)3083     void notifyReadFailure(const ReadFailureData& data) override {
3084         DiscardableHandleManager::notifyReadFailure(data);
3085     }
3086 
unlock()3087     void unlock() {
3088         fIsLocked = true;
3089     }
3090 
3091 private:
3092     bool fIsLocked{false};
3093 };
3094 
SkTestCanvas(SkCanvas * canvas)3095 SkTestCanvas<SkRemoteSlugTestKey>::SkTestCanvas(SkCanvas* canvas)
3096         : SkCanvas(sk_ref_sp(canvas->baseDevice()))
3097         , fServerHandleManager(new ServerHandleManager{})
3098         , fClientHandleManager(new ClientHandleManager{})
3099         , fStrikeServer(fServerHandleManager.get())
3100         , fStrikeClient(fClientHandleManager) {}
3101 
3102 // Allow the strikes to be freed from the strike cache after the test has been drawn.
~SkTestCanvas()3103 SkTestCanvas<SkRemoteSlugTestKey>::~SkTestCanvas() {
3104     static_cast<ClientHandleManager*>(fClientHandleManager.get())->unlock();
3105 }
3106 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)3107 void SkTestCanvas<SkRemoteSlugTestKey>::onDrawGlyphRunList(
3108         const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
3109     SkRect bounds = glyphRunList.sourceBoundsWithOrigin();
3110     if (this->internalQuickReject(bounds, paint)) {
3111         return;
3112     }
3113     auto layer = this->aboutToDraw(this, paint, &bounds);
3114     if (layer) {
3115         if (glyphRunList.hasRSXForm()) {
3116             this->SkCanvas::onDrawGlyphRunList(glyphRunList, layer->paint());
3117         } else {
3118             sk_sp<SkData> slugBytes;
3119             std::vector<uint8_t> glyphBytes;
3120             {
3121                 auto analysisCanvas = fStrikeServer.makeAnalysisCanvas(
3122                         this->topDevice()->width(),
3123                         this->topDevice()->height(),
3124                         this->fProps,
3125                         this->topDevice()->imageInfo().refColorSpace(),
3126                         // TODO: Where should we get this value from?
3127                         /*DFTSupport=*/ true);
3128 
3129                 // TODO: Move the analysis canvas processing up to the via to handle a whole
3130                 //  document at a time. This is not the correct way to handle the CTM; it doesn't
3131                 //  work for layers.
3132                 analysisCanvas->setMatrix(this->getLocalToDevice());
3133                 auto slug = analysisCanvas->onConvertGlyphRunListToSlug(glyphRunList,
3134                                                                         layer->paint());
3135                 if (slug != nullptr) {
3136                     slugBytes = slug->serialize();
3137                 }
3138                 fStrikeServer.writeStrikeData(&glyphBytes);
3139             }
3140             {
3141                 if (!glyphBytes.empty()) {
3142                     fStrikeClient.readStrikeData(glyphBytes.data(), glyphBytes.size());
3143                 }
3144                 if (slugBytes != nullptr) {
3145                     auto slug = Slug::Deserialize(
3146                             slugBytes->data(), slugBytes->size(), &fStrikeClient);
3147                     this->drawSlug(slug.get());
3148                 }
3149             }
3150         }
3151     }
3152 }
3153 #endif
3154 
3155 ////////////////////////////////////////////////////////////////////////////////////////////////////
3156