1 /*
2 * Copyright 2012 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/SkImageFilter.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkRect.h"
12 #include "include/effects/SkComposeImageFilter.h"
13 #include "include/private/SkSafe32.h"
14 #include "src/core/SkFuzzLogging.h"
15 #include "src/core/SkImageFilterCache.h"
16 #include "src/core/SkImageFilter_Base.h"
17 #include "src/core/SkLocalMatrixImageFilter.h"
18 #include "src/core/SkMatrixImageFilter.h"
19 #include "src/core/SkReadBuffer.h"
20 #include "src/core/SkSpecialImage.h"
21 #include "src/core/SkSpecialSurface.h"
22 #include "src/core/SkValidationUtils.h"
23 #include "src/core/SkWriteBuffer.h"
24 #if SK_SUPPORT_GPU
25 #include "include/gpu/GrContext.h"
26 #include "include/private/GrRecordingContext.h"
27 #include "src/gpu/GrColorSpaceXform.h"
28 #include "src/gpu/GrContextPriv.h"
29 #include "src/gpu/GrFixedClip.h"
30 #include "src/gpu/GrRecordingContextPriv.h"
31 #include "src/gpu/GrRenderTargetContext.h"
32 #include "src/gpu/GrTextureProxy.h"
33 #include "src/gpu/SkGr.h"
34 #endif
35 #include <atomic>
36
37 ///////////////////////////////////////////////////////////////////////////////////////////////////
38 // SkImageFilter - A number of the public APIs on SkImageFilter downcast to SkImageFilter_Base
39 // in order to perform their actual work.
40 ///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 /**
43 * Returns the number of inputs this filter will accept (some inputs can
44 * be NULL).
45 */
countInputs() const46 int SkImageFilter::countInputs() const { return as_IFB(this)->fInputs.count(); }
47
48 /**
49 * Returns the input filter at a given index, or NULL if no input is
50 * connected. The indices used are filter-specific.
51 */
getInput(int i) const52 const SkImageFilter* SkImageFilter::getInput(int i) const {
53 SkASSERT(i < this->countInputs());
54 return as_IFB(this)->fInputs[i].get();
55 }
56
isColorFilterNode(SkColorFilter ** filterPtr) const57 bool SkImageFilter::isColorFilterNode(SkColorFilter** filterPtr) const {
58 return as_IFB(this)->onIsColorFilterNode(filterPtr);
59 }
60
filterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection direction,const SkIRect * inputRect) const61 SkIRect SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm,
62 MapDirection direction, const SkIRect* inputRect) const {
63 if (kReverse_MapDirection == direction) {
64 SkIRect bounds = as_IFB(this)->onFilterNodeBounds(src, ctm, direction, inputRect);
65 return as_IFB(this)->onFilterBounds(bounds, ctm, direction, &bounds);
66 } else {
67 SkASSERT(!inputRect);
68 SkIRect bounds = as_IFB(this)->onFilterBounds(src, ctm, direction, nullptr);
69 bounds = as_IFB(this)->onFilterNodeBounds(bounds, ctm, direction, nullptr);
70 SkIRect dst;
71 as_IFB(this)->getCropRect().applyTo(
72 bounds, ctm, as_IFB(this)->affectsTransparentBlack(), &dst);
73 return dst;
74 }
75 }
76
computeFastBounds(const SkRect & src) const77 SkRect SkImageFilter::computeFastBounds(const SkRect& src) const {
78 if (0 == this->countInputs()) {
79 return src;
80 }
81 SkRect combinedBounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
82 for (int i = 1; i < this->countInputs(); i++) {
83 const SkImageFilter* input = this->getInput(i);
84 if (input) {
85 combinedBounds.join(input->computeFastBounds(src));
86 } else {
87 combinedBounds.join(src);
88 }
89 }
90 return combinedBounds;
91 }
92
canComputeFastBounds() const93 bool SkImageFilter::canComputeFastBounds() const {
94 if (as_IFB(this)->affectsTransparentBlack()) {
95 return false;
96 }
97 for (int i = 0; i < this->countInputs(); i++) {
98 const SkImageFilter* input = this->getInput(i);
99 if (input && !input->canComputeFastBounds()) {
100 return false;
101 }
102 }
103 return true;
104 }
105
asAColorFilter(SkColorFilter ** filterPtr) const106 bool SkImageFilter::asAColorFilter(SkColorFilter** filterPtr) const {
107 SkASSERT(nullptr != filterPtr);
108 if (!this->isColorFilterNode(filterPtr)) {
109 return false;
110 }
111 if (nullptr != this->getInput(0) || (*filterPtr)->affectsTransparentBlack()) {
112 (*filterPtr)->unref();
113 return false;
114 }
115 return true;
116 }
117
MakeMatrixFilter(const SkMatrix & matrix,SkFilterQuality filterQuality,sk_sp<SkImageFilter> input)118 sk_sp<SkImageFilter> SkImageFilter::MakeMatrixFilter(const SkMatrix& matrix,
119 SkFilterQuality filterQuality,
120 sk_sp<SkImageFilter> input) {
121 return SkMatrixImageFilter::Make(matrix, filterQuality, std::move(input));
122 }
123
makeWithLocalMatrix(const SkMatrix & matrix) const124 sk_sp<SkImageFilter> SkImageFilter::makeWithLocalMatrix(const SkMatrix& matrix) const {
125 return SkLocalMatrixImageFilter::Make(matrix, this->refMe());
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////////////////////////
129 // SkImageFilter_Base
130 ///////////////////////////////////////////////////////////////////////////////////////////////////
131
next_image_filter_unique_id()132 static int32_t next_image_filter_unique_id() {
133 static std::atomic<int32_t> nextID{1};
134
135 int32_t id;
136 do {
137 id = nextID++;
138 } while (id == 0);
139 return id;
140 }
141
SkImageFilter_Base(sk_sp<SkImageFilter> const * inputs,int inputCount,const CropRect * cropRect)142 SkImageFilter_Base::SkImageFilter_Base(sk_sp<SkImageFilter> const* inputs,
143 int inputCount, const CropRect* cropRect)
144 : fUsesSrcInput(false)
145 , fUniqueID(next_image_filter_unique_id()) {
146 fCropRect = cropRect ? *cropRect : CropRect(SkRect(), 0x0);
147
148 fInputs.reset(inputCount);
149
150 for (int i = 0; i < inputCount; ++i) {
151 if (!inputs[i] || as_IFB(inputs[i])->fUsesSrcInput) {
152 fUsesSrcInput = true;
153 }
154 fInputs[i] = inputs[i];
155 }
156 }
157
~SkImageFilter_Base()158 SkImageFilter_Base::~SkImageFilter_Base() {
159 SkImageFilterCache::Get()->purgeByImageFilter(this);
160 }
161
unflatten(SkReadBuffer & buffer,int expectedCount)162 bool SkImageFilter_Base::Common::unflatten(SkReadBuffer& buffer, int expectedCount) {
163 const int count = buffer.readInt();
164 if (!buffer.validate(count >= 0)) {
165 return false;
166 }
167 if (!buffer.validate(expectedCount < 0 || count == expectedCount)) {
168 return false;
169 }
170
171 SkASSERT(fInputs.empty());
172 for (int i = 0; i < count; i++) {
173 fInputs.push_back(buffer.readBool() ? buffer.readImageFilter() : nullptr);
174 if (!buffer.isValid()) {
175 return false;
176 }
177 }
178 SkRect rect;
179 buffer.readRect(&rect);
180 if (!buffer.isValid() || !buffer.validate(SkIsValidRect(rect))) {
181 return false;
182 }
183
184 uint32_t flags = buffer.readUInt();
185 fCropRect = CropRect(rect, flags);
186 return buffer.isValid();
187 }
188
flatten(SkWriteBuffer & buffer) const189 void SkImageFilter_Base::flatten(SkWriteBuffer& buffer) const {
190 buffer.writeInt(fInputs.count());
191 for (int i = 0; i < fInputs.count(); i++) {
192 const SkImageFilter* input = this->getInput(i);
193 buffer.writeBool(input != nullptr);
194 if (input != nullptr) {
195 buffer.writeFlattenable(input);
196 }
197 }
198 buffer.writeRect(fCropRect.rect());
199 buffer.writeUInt(fCropRect.flags());
200 }
201
filterImage(const Context & context,SkIPoint * offset) const202 sk_sp<SkSpecialImage> SkImageFilter_Base::filterImage(const Context& context,
203 SkIPoint* offset) const {
204 SkASSERT(offset);
205 if (!context.isValid()) {
206 return nullptr;
207 }
208
209 uint32_t srcGenID = fUsesSrcInput ? context.sourceImage()->uniqueID() : 0;
210 const SkIRect srcSubset = fUsesSrcInput ? context.sourceImage()->subset()
211 : SkIRect::MakeWH(0, 0);
212 SkImageFilterCacheKey key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID, srcSubset);
213 if (context.cache()) {
214 sk_sp<SkSpecialImage> result = context.cache()->get(key, offset);
215 if (result) {
216 return result;
217 }
218 }
219
220 sk_sp<SkSpecialImage> result(this->onFilterImage(context, offset));
221
222 #if SK_SUPPORT_GPU
223 if (context.gpuBacked() && result && !result->isTextureBacked()) {
224 // Keep the result on the GPU - this is still required for some
225 // image filters that don't support GPU in all cases
226 result = result->makeTextureImage(context.getContext());
227 }
228 #endif
229
230 if (result && context.cache()) {
231 context.cache()->set(key, result.get(), *offset, this);
232 }
233
234 return result;
235 }
236
canHandleComplexCTM() const237 bool SkImageFilter_Base::canHandleComplexCTM() const {
238 // CropRects need to apply in the source coordinate system, but are not aware of complex CTMs
239 // when performing clipping. For a simple fix, any filter with a crop rect set cannot support
240 // complex CTMs until that's updated.
241 if (this->cropRectIsSet() || !this->onCanHandleComplexCTM()) {
242 return false;
243 }
244 const int count = this->countInputs();
245 for (int i = 0; i < count; ++i) {
246 const SkImageFilter_Base* input = as_IFB(this->getInput(i));
247 if (input && !input->canHandleComplexCTM()) {
248 return false;
249 }
250 }
251 return true;
252 }
253
applyTo(const SkIRect & imageBounds,const SkMatrix & ctm,bool embiggen,SkIRect * cropped) const254 void SkImageFilter::CropRect::applyTo(const SkIRect& imageBounds, const SkMatrix& ctm,
255 bool embiggen, SkIRect* cropped) const {
256 *cropped = imageBounds;
257 if (fFlags) {
258 SkRect devCropR;
259 ctm.mapRect(&devCropR, fRect);
260 SkIRect devICropR = devCropR.roundOut();
261
262 // Compute the left/top first, in case we need to modify the right/bottom for a missing edge
263 if (fFlags & kHasLeft_CropEdge) {
264 if (embiggen || devICropR.fLeft > cropped->fLeft) {
265 cropped->fLeft = devICropR.fLeft;
266 }
267 } else {
268 devICropR.fRight = Sk32_sat_add(cropped->fLeft, devICropR.width());
269 }
270 if (fFlags & kHasTop_CropEdge) {
271 if (embiggen || devICropR.fTop > cropped->fTop) {
272 cropped->fTop = devICropR.fTop;
273 }
274 } else {
275 devICropR.fBottom = Sk32_sat_add(cropped->fTop, devICropR.height());
276 }
277 if (fFlags & kHasWidth_CropEdge) {
278 if (embiggen || devICropR.fRight < cropped->fRight) {
279 cropped->fRight = devICropR.fRight;
280 }
281 }
282 if (fFlags & kHasHeight_CropEdge) {
283 if (embiggen || devICropR.fBottom < cropped->fBottom) {
284 cropped->fBottom = devICropR.fBottom;
285 }
286 }
287 }
288 }
289
applyCropRect(const Context & ctx,const SkIRect & srcBounds,SkIRect * dstBounds) const290 bool SkImageFilter_Base::applyCropRect(const Context& ctx, const SkIRect& srcBounds,
291 SkIRect* dstBounds) const {
292 SkIRect tmpDst = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection, nullptr);
293 fCropRect.applyTo(tmpDst, ctx.ctm(), this->affectsTransparentBlack(), dstBounds);
294 // Intersect against the clip bounds, in case the crop rect has
295 // grown the bounds beyond the original clip. This can happen for
296 // example in tiling, where the clip is much smaller than the filtered
297 // primitive. If we didn't do this, we would be processing the filter
298 // at the full crop rect size in every tile.
299 return dstBounds->intersect(ctx.clipBounds());
300 }
301
302 // Return a larger (newWidth x newHeight) copy of 'src' with black padding
303 // around it.
pad_image(SkSpecialImage * src,const SkImageFilter_Base::Context & ctx,int newWidth,int newHeight,int offX,int offY)304 static sk_sp<SkSpecialImage> pad_image(SkSpecialImage* src, const SkImageFilter_Base::Context& ctx,
305 int newWidth, int newHeight, int offX, int offY) {
306 // We would like to operate in the source's color space (so that we return an "identical"
307 // image, other than the padding. To achieve that, we'd create a new context using
308 // src->getColorSpace() to replace ctx.colorSpace().
309
310 // That fails in at least two ways. For formats that are texturable but not renderable (like
311 // F16 on some ES implementations), we can't create a surface to do the work. For sRGB, images
312 // may be tagged with an sRGB color space (which leads to an sRGB config in makeSurface). But
313 // the actual config of that sRGB image on a device with no sRGB support is non-sRGB.
314 //
315 // Rather than try to special case these situations, we execute the image padding in the
316 // destination color space. This should not affect the output of the DAG in (almost) any case,
317 // because the result of this call is going to be used as an input, where it would have been
318 // switched to the destination space anyway. The one exception would be a filter that expected
319 // to consume unclamped F16 data, but the padded version of the image is pre-clamped to 8888.
320 // We can revisit this logic if that ever becomes an actual problem.
321 sk_sp<SkSpecialSurface> surf(ctx.makeSurface(SkISize::Make(newWidth, newHeight)));
322 if (!surf) {
323 return nullptr;
324 }
325
326 SkCanvas* canvas = surf->getCanvas();
327 SkASSERT(canvas);
328
329 canvas->clear(0x0);
330
331 src->draw(canvas, offX, offY, nullptr);
332
333 return surf->makeImageSnapshot();
334 }
335
applyCropRectAndPad(const Context & ctx,SkSpecialImage * src,SkIPoint * srcOffset,SkIRect * bounds) const336 sk_sp<SkSpecialImage> SkImageFilter_Base::applyCropRectAndPad(const Context& ctx,
337 SkSpecialImage* src,
338 SkIPoint* srcOffset,
339 SkIRect* bounds) const {
340 const SkIRect srcBounds = SkIRect::MakeXYWH(srcOffset->x(), srcOffset->y(),
341 src->width(), src->height());
342
343 if (!this->applyCropRect(ctx, srcBounds, bounds)) {
344 return nullptr;
345 }
346
347 if (srcBounds.contains(*bounds)) {
348 return sk_sp<SkSpecialImage>(SkRef(src));
349 } else {
350 sk_sp<SkSpecialImage> img(pad_image(src, ctx, bounds->width(), bounds->height(),
351 Sk32_sat_sub(srcOffset->x(), bounds->x()),
352 Sk32_sat_sub(srcOffset->y(), bounds->y())));
353 *srcOffset = SkIPoint::Make(bounds->x(), bounds->y());
354 return img;
355 }
356 }
357
onFilterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const358 SkIRect SkImageFilter_Base::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
359 MapDirection dir, const SkIRect* inputRect) const {
360 if (this->countInputs() < 1) {
361 return src;
362 }
363
364 SkIRect totalBounds;
365 for (int i = 0; i < this->countInputs(); ++i) {
366 const SkImageFilter* filter = this->getInput(i);
367 SkIRect rect = filter ? filter->filterBounds(src, ctm, dir, inputRect) : src;
368 if (0 == i) {
369 totalBounds = rect;
370 } else {
371 totalBounds.join(rect);
372 }
373 }
374
375 return totalBounds;
376 }
377
onFilterNodeBounds(const SkIRect & src,const SkMatrix &,MapDirection,const SkIRect *) const378 SkIRect SkImageFilter_Base::onFilterNodeBounds(const SkIRect& src, const SkMatrix&,
379 MapDirection, const SkIRect*) const {
380 return src;
381 }
382
filterInput(int index,const Context & ctx,SkIPoint * offset) const383 sk_sp<SkSpecialImage> SkImageFilter_Base::filterInput(int index,
384 const Context& ctx,
385 SkIPoint* offset) const {
386 const SkImageFilter* input = this->getInput(index);
387 if (!input) {
388 return sk_ref_sp(ctx.sourceImage());
389 }
390
391 sk_sp<SkSpecialImage> result(as_IFB(input)->filterImage(this->mapContext(ctx), offset));
392
393 SkASSERT(!result || ctx.gpuBacked() == result->isTextureBacked());
394
395 return result;
396 }
397
mapContext(const Context & ctx) const398 SkImageFilter_Base::Context SkImageFilter_Base::mapContext(const Context& ctx) const {
399 SkIRect clipBounds = this->onFilterNodeBounds(ctx.clipBounds(), ctx.ctm(),
400 MapDirection::kReverse_MapDirection,
401 &ctx.clipBounds());
402 return ctx.withNewClipBounds(clipBounds);
403 }
404
405 #if SK_SUPPORT_GPU
DrawWithFP(GrRecordingContext * context,std::unique_ptr<GrFragmentProcessor> fp,const SkIRect & bounds,SkColorType colorType,const SkColorSpace * colorSpace,GrProtected isProtected)406 sk_sp<SkSpecialImage> SkImageFilter_Base::DrawWithFP(GrRecordingContext* context,
407 std::unique_ptr<GrFragmentProcessor> fp,
408 const SkIRect& bounds,
409 SkColorType colorType,
410 const SkColorSpace* colorSpace,
411 GrProtected isProtected) {
412 GrPaint paint;
413 paint.addColorFragmentProcessor(std::move(fp));
414 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
415
416 sk_sp<GrRenderTargetContext> renderTargetContext(
417 context->priv().makeDeferredRenderTargetContext(
418 SkBackingFit::kApprox,
419 bounds.width(),
420 bounds.height(),
421 SkColorTypeToGrColorType(colorType),
422 sk_ref_sp(colorSpace),
423 1,
424 GrMipMapped::kNo,
425 kBottomLeft_GrSurfaceOrigin,
426 nullptr,
427 SkBudgeted::kYes,
428 isProtected));
429 if (!renderTargetContext) {
430 return nullptr;
431 }
432
433 SkIRect dstIRect = SkIRect::MakeWH(bounds.width(), bounds.height());
434 SkRect srcRect = SkRect::Make(bounds);
435 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
436 GrFixedClip clip(dstIRect);
437 renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
438 srcRect);
439
440 return SkSpecialImage::MakeDeferredFromGpu(
441 context, dstIRect, kNeedNewImageUniqueID_SpecialImage,
442 renderTargetContext->asTextureProxyRef(),
443 renderTargetContext->colorSpaceInfo().refColorSpace());
444 }
445
ImageToColorSpace(SkSpecialImage * src,SkColorType colorType,SkColorSpace * colorSpace)446 sk_sp<SkSpecialImage> SkImageFilter_Base::ImageToColorSpace(SkSpecialImage* src,
447 SkColorType colorType,
448 SkColorSpace* colorSpace) {
449 // There are several conditions that determine if we actually need to convert the source to the
450 // destination's color space. Rather than duplicate that logic here, just try to make an xform
451 // object. If that produces something, then both are tagged, and the source is in a different
452 // gamut than the dest. There is some overhead to making the xform, but those are cached, and
453 // if we get one back, that means we're about to use it during the conversion anyway.
454 auto colorSpaceXform = GrColorSpaceXform::Make(src->getColorSpace(), src->alphaType(),
455 colorSpace, kPremul_SkAlphaType);
456
457 if (!colorSpaceXform) {
458 // No xform needed, just return the original image
459 return sk_ref_sp(src);
460 }
461
462 sk_sp<SkSpecialSurface> surf(src->makeSurface(colorType, colorSpace,
463 SkISize::Make(src->width(), src->height())));
464 if (!surf) {
465 return sk_ref_sp(src);
466 }
467
468 SkCanvas* canvas = surf->getCanvas();
469 SkASSERT(canvas);
470 SkPaint p;
471 p.setBlendMode(SkBlendMode::kSrc);
472 src->draw(canvas, 0, 0, &p);
473 return surf->makeImageSnapshot();
474 }
475 #endif
476
477 // In repeat mode, when we are going to sample off one edge of the srcBounds we require the
478 // opposite side be preserved.
DetermineRepeatedSrcBound(const SkIRect & srcBounds,const SkIVector & filterOffset,const SkISize & filterSize,const SkIRect & originalSrcBounds)479 SkIRect SkImageFilter_Base::DetermineRepeatedSrcBound(const SkIRect& srcBounds,
480 const SkIVector& filterOffset,
481 const SkISize& filterSize,
482 const SkIRect& originalSrcBounds) {
483 SkIRect tmp = srcBounds;
484 tmp.adjust(-filterOffset.fX, -filterOffset.fY,
485 filterSize.fWidth - filterOffset.fX, filterSize.fHeight - filterOffset.fY);
486
487 if (tmp.fLeft < originalSrcBounds.fLeft || tmp.fRight > originalSrcBounds.fRight) {
488 tmp.fLeft = originalSrcBounds.fLeft;
489 tmp.fRight = originalSrcBounds.fRight;
490 }
491 if (tmp.fTop < originalSrcBounds.fTop || tmp.fBottom > originalSrcBounds.fBottom) {
492 tmp.fTop = originalSrcBounds.fTop;
493 tmp.fBottom = originalSrcBounds.fBottom;
494 }
495
496 return tmp;
497 }
498
PurgeCache()499 void SkImageFilter_Base::PurgeCache() {
500 SkImageFilterCache::Get()->purge();
501 }
502
apply_ctm_to_filter(sk_sp<SkImageFilter> input,const SkMatrix & ctm,SkMatrix * remainder,bool asBackdrop)503 static sk_sp<SkImageFilter> apply_ctm_to_filter(sk_sp<SkImageFilter> input, const SkMatrix& ctm,
504 SkMatrix* remainder, bool asBackdrop) {
505 if (ctm.isScaleTranslate() || as_IFB(input)->canHandleComplexCTM()) {
506 // The filter supports the CTM, so leave it as-is and 'remainder' stores the whole CTM
507 *remainder = ctm;
508 return input;
509 }
510
511 // We have a complex CTM and a filter that can't support them, so it needs to use the matrix
512 // transform filter that resamples the image contents. Decompose the simple portion of the ctm
513 // into 'remainder'
514 SkMatrix ctmToEmbed;
515 SkSize scale;
516 if (ctm.decomposeScale(&scale, &ctmToEmbed)) {
517 // decomposeScale splits ctm into scale * ctmToEmbed, so bake ctmToEmbed into DAG
518 // with a matrix filter and return scale as the remaining matrix for the real CTM.
519 remainder->setScale(scale.fWidth, scale.fHeight);
520
521 // ctmToEmbed is passed to SkMatrixImageFilter, which performs its transforms as if it were
522 // a pre-transformation before applying the image-filter context's CTM. In this case, we
523 // need ctmToEmbed to be a post-transformation (i.e. after the scale matrix since
524 // decomposeScale produces ctm = ctmToEmbed * scale). Giving scale^-1 * ctmToEmbed * scale
525 // to the matrix filter achieves this effect.
526 // TODO (michaelludwig) - When the original root node of a filter can be drawn directly to a
527 // device using ctmToEmbed, this abuse of SkMatrixImageFilter can go away.
528 ctmToEmbed.preScale(scale.fWidth, scale.fHeight);
529 ctmToEmbed.postScale(1.f / scale.fWidth, 1.f / scale.fHeight);
530 } else {
531 // Unable to decompose
532 // FIXME Ideally we'd embed the entire CTM as part of the matrix image filter, but
533 // the device <-> src bounds calculations for filters are very brittle under perspective,
534 // and can easily run into precision issues (wrong bounds that clip), or performance issues
535 // (producing large source-space images where 80% of the image is compressed into a few
536 // device pixels). A longer term solution for perspective-space image filtering is needed
537 // see skbug.com/9074
538 if (ctm.hasPerspective()) {
539 *remainder = ctm;
540 return input;
541 }
542
543 ctmToEmbed = ctm;
544 remainder->setIdentity();
545 }
546
547 if (asBackdrop) {
548 // In the backdrop case we also have to transform the existing device-space buffer content
549 // into the source coordinate space prior to the filtering. Non-backdrop filter inputs are
550 // already in the source space because of how the layer is drawn by SkCanvas.
551 SkMatrix invEmbed;
552 if (ctmToEmbed.invert(&invEmbed)) {
553 input = SkComposeImageFilter::Make(std::move(input),
554 SkMatrixImageFilter::Make(invEmbed, kLow_SkFilterQuality, nullptr));
555 }
556 }
557 return SkMatrixImageFilter::Make(ctmToEmbed, kLow_SkFilterQuality, input);
558 }
559
applyCTM(const SkMatrix & ctm,SkMatrix * remainder) const560 sk_sp<SkImageFilter> SkImageFilter_Base::applyCTM(const SkMatrix& ctm, SkMatrix* remainder) const {
561 return apply_ctm_to_filter(this->refMe(), ctm, remainder, false);
562 }
563
applyCTMForBackdrop(const SkMatrix & ctm,SkMatrix * remainder) const564 sk_sp<SkImageFilter> SkImageFilter_Base::applyCTMForBackdrop(const SkMatrix& ctm,
565 SkMatrix* remainder) const {
566 return apply_ctm_to_filter(this->refMe(), ctm, remainder, true);
567 }
568