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