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