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