• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/private/SkSafe32.h"
13 #include "src/core/SkFuzzLogging.h"
14 #include "src/core/SkImageFilterCache.h"
15 #include "src/core/SkImageFilter_Base.h"
16 #include "src/core/SkLocalMatrixImageFilter.h"
17 #include "src/core/SkMatrixImageFilter.h"
18 #include "src/core/SkReadBuffer.h"
19 #include "src/core/SkSpecialImage.h"
20 #include "src/core/SkSpecialSurface.h"
21 #include "src/core/SkValidationUtils.h"
22 #include "src/core/SkWriteBuffer.h"
23 #if SK_SUPPORT_GPU
24 #include "include/gpu/GrRecordingContext.h"
25 #include "src/gpu/GrColorSpaceXform.h"
26 #include "src/gpu/GrDirectContextPriv.h"
27 #include "src/gpu/GrRecordingContextPriv.h"
28 #include "src/gpu/GrSurfaceDrawContext.h"
29 #include "src/gpu/GrTextureProxy.h"
30 #include "src/gpu/SkGr.h"
31 #endif
32 #include <atomic>
33 
34 ///////////////////////////////////////////////////////////////////////////////////////////////////
35 // SkImageFilter - A number of the public APIs on SkImageFilter downcast to SkImageFilter_Base
36 // in order to perform their actual work.
37 ///////////////////////////////////////////////////////////////////////////////////////////////////
38 
39 /**
40  *  Returns the number of inputs this filter will accept (some inputs can
41  *  be NULL).
42  */
countInputs() const43 int SkImageFilter::countInputs() const { return as_IFB(this)->fInputs.count(); }
44 
45 /**
46  *  Returns the input filter at a given index, or NULL if no input is
47  *  connected.  The indices used are filter-specific.
48  */
getInput(int i) const49 const SkImageFilter* SkImageFilter::getInput(int i) const {
50     SkASSERT(i < this->countInputs());
51     return as_IFB(this)->fInputs[i].get();
52 }
53 
isColorFilterNode(SkColorFilter ** filterPtr) const54 bool SkImageFilter::isColorFilterNode(SkColorFilter** filterPtr) const {
55     return as_IFB(this)->onIsColorFilterNode(filterPtr);
56 }
57 
filterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection direction,const SkIRect * inputRect) const58 SkIRect SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm,
59                                     MapDirection direction, const SkIRect* inputRect) const {
60     // The old filterBounds() function uses SkIRects that are defined in layer space so, while
61     // we still are supporting it, bypass SkIF_B's new public filter bounds functions and go right
62     // to the internal layer-space calculations.
63     skif::Mapping mapping(SkMatrix::I(), ctm);
64     if (kReverse_MapDirection == direction) {
65         skif::LayerSpace<SkIRect> targetOutput(src);
66         if (as_IFB(this)->cropRectIsSet()) {
67            skif::LayerSpace<SkIRect> outputCrop = mapping.paramToLayer(
68                     skif::ParameterSpace<SkRect>(as_IFB(this)->getCropRect().rect())).roundOut();
69             // Just intersect directly; unlike the forward-mapping case, since we start with the
70             // external target output, there's no need to embiggen due to affecting trans. black
71             if (!targetOutput.intersect(outputCrop)) {
72                 // Nothing would be output by the filter, so return empty rect
73                 return SkIRect::MakeEmpty();
74             }
75         }
76         skif::LayerSpace<SkIRect> content(inputRect ? *inputRect : src);
77         return SkIRect(as_IFB(this)->onGetInputLayerBounds(mapping, targetOutput, content));
78     } else {
79         SkASSERT(!inputRect);
80         skif::LayerSpace<SkIRect> content(src);
81         skif::LayerSpace<SkIRect> output = as_IFB(this)->onGetOutputLayerBounds(mapping, content);
82         // Manually apply the crop rect for now, until cropping is performed by a dedicated SkIF.
83         SkIRect dst;
84         as_IFB(this)->getCropRect().applyTo(
85                 SkIRect(output), ctm, as_IFB(this)->affectsTransparentBlack(), &dst);
86         return dst;
87     }
88 }
89 
computeFastBounds(const SkRect & src) const90 SkRect SkImageFilter::computeFastBounds(const SkRect& src) const {
91     if (0 == this->countInputs()) {
92         return src;
93     }
94     SkRect combinedBounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
95     for (int i = 1; i < this->countInputs(); i++) {
96         const SkImageFilter* input = this->getInput(i);
97         if (input) {
98             combinedBounds.join(input->computeFastBounds(src));
99         } else {
100             combinedBounds.join(src);
101         }
102     }
103     return combinedBounds;
104 }
105 
canComputeFastBounds() const106 bool SkImageFilter::canComputeFastBounds() const {
107     if (as_IFB(this)->affectsTransparentBlack()) {
108         return false;
109     }
110     for (int i = 0; i < this->countInputs(); i++) {
111         const SkImageFilter* input = this->getInput(i);
112         if (input && !input->canComputeFastBounds()) {
113             return false;
114         }
115     }
116     return true;
117 }
118 
asAColorFilter(SkColorFilter ** filterPtr) const119 bool SkImageFilter::asAColorFilter(SkColorFilter** filterPtr) const {
120     SkASSERT(nullptr != filterPtr);
121     if (!this->isColorFilterNode(filterPtr)) {
122         return false;
123     }
124     if (nullptr != this->getInput(0) || as_CFB(*filterPtr)->affectsTransparentBlack()) {
125         (*filterPtr)->unref();
126         return false;
127     }
128     return true;
129 }
130 
makeWithLocalMatrix(const SkMatrix & matrix) const131 sk_sp<SkImageFilter> SkImageFilter::makeWithLocalMatrix(const SkMatrix& matrix) const {
132     return SkLocalMatrixImageFilter::Make(matrix, this->refMe());
133 }
134 
135 ///////////////////////////////////////////////////////////////////////////////////////////////////
136 // SkImageFilter_Base
137 ///////////////////////////////////////////////////////////////////////////////////////////////////
138 
139 SK_USE_FLUENT_IMAGE_FILTER_TYPES
140 
next_image_filter_unique_id()141 static int32_t next_image_filter_unique_id() {
142     static std::atomic<int32_t> nextID{1};
143 
144     int32_t id;
145     do {
146         id = nextID.fetch_add(1, std::memory_order_relaxed);
147     } while (id == 0);
148     return id;
149 }
150 
SkImageFilter_Base(sk_sp<SkImageFilter> const * inputs,int inputCount,const SkRect * cropRect)151 SkImageFilter_Base::SkImageFilter_Base(sk_sp<SkImageFilter> const* inputs,
152                                        int inputCount, const SkRect* cropRect)
153         : fUsesSrcInput(false)
154         , fCropRect(cropRect)
155         , fUniqueID(next_image_filter_unique_id()) {
156     fInputs.reset(inputCount);
157 
158     for (int i = 0; i < inputCount; ++i) {
159         if (!inputs[i] || as_IFB(inputs[i])->fUsesSrcInput) {
160             fUsesSrcInput = true;
161         }
162         fInputs[i] = inputs[i];
163     }
164 }
165 
~SkImageFilter_Base()166 SkImageFilter_Base::~SkImageFilter_Base() {
167     SkImageFilterCache::Get()->purgeByImageFilter(this);
168 }
169 
unflatten(SkReadBuffer & buffer,int expectedCount)170 bool SkImageFilter_Base::Common::unflatten(SkReadBuffer& buffer, int expectedCount) {
171     const int count = buffer.readInt();
172     if (!buffer.validate(count >= 0)) {
173         return false;
174     }
175     if (!buffer.validate(expectedCount < 0 || count == expectedCount)) {
176         return false;
177     }
178 
179 #if defined(SK_BUILD_FOR_FUZZER)
180     if (count > 4) {
181         return false;
182     }
183 #endif
184 
185     SkASSERT(fInputs.empty());
186     for (int i = 0; i < count; i++) {
187         fInputs.push_back(buffer.readBool() ? buffer.readImageFilter() : nullptr);
188         if (!buffer.isValid()) {
189             return false;
190         }
191     }
192     SkRect rect;
193     buffer.readRect(&rect);
194     if (!buffer.isValid() || !buffer.validate(SkIsValidRect(rect))) {
195         return false;
196     }
197 
198     uint32_t flags = buffer.readUInt();
199     if (!buffer.isValid() ||
200         !buffer.validate(flags == 0x0 || flags == CropRect::kHasAll_CropEdge)) {
201         return false;
202     }
203     fCropRect = CropRect(flags ? &rect : nullptr);
204     return buffer.isValid();
205 }
206 
flatten(SkWriteBuffer & buffer) const207 void SkImageFilter_Base::flatten(SkWriteBuffer& buffer) const {
208     buffer.writeInt(fInputs.count());
209     for (int i = 0; i < fInputs.count(); i++) {
210         const SkImageFilter* input = this->getInput(i);
211         buffer.writeBool(input != nullptr);
212         if (input != nullptr) {
213             buffer.writeFlattenable(input);
214         }
215     }
216     buffer.writeRect(fCropRect.rect());
217     buffer.writeUInt(fCropRect.flags());
218 }
219 
filterImage(const skif::Context & context) const220 skif::FilterResult<For::kOutput> SkImageFilter_Base::filterImage(const skif::Context& context) const {
221     // TODO (michaelludwig) - Old filters have an implicit assumption that the source image
222     // (originally passed separately) has an origin of (0, 0). SkComposeImageFilter makes an effort
223     // to ensure that remains the case. Once everyone uses the new type systems for bounds, non
224     // (0, 0) source origins will be easy to support.
225     SkASSERT(context.source().layerOrigin().x() == 0 && context.source().layerOrigin().y() == 0);
226 
227     skif::FilterResult<For::kOutput> result;
228     if (!context.isValid()) {
229         return result;
230     }
231 
232     uint32_t srcGenID = fUsesSrcInput ? context.sourceImage()->uniqueID() : 0;
233     const SkIRect srcSubset = fUsesSrcInput ? context.sourceImage()->subset()
234                                             : SkIRect::MakeWH(0, 0);
235 
236     SkImageFilterCacheKey key(fUniqueID, context.mapping().layerMatrix(), context.clipBounds(),
237                               srcGenID, srcSubset);
238     if (context.cache() && context.cache()->get(key, &result)) {
239         return result;
240     }
241 
242     result = this->onFilterImage(context);
243 
244     if (context.gpuBacked()) {
245         SkASSERT(!result.image() || result.image()->isTextureBacked());
246     }
247 
248     if (context.cache()) {
249         context.cache()->set(key, this, result);
250     }
251 
252     return result;
253 }
254 
getInputBounds(const skif::Mapping & mapping,const skif::DeviceSpace<SkIRect> & desiredOutput,const skif::ParameterSpace<SkRect> * knownContentBounds) const255 skif::LayerSpace<SkIRect> SkImageFilter_Base::getInputBounds(
256         const skif::Mapping& mapping, const skif::DeviceSpace<SkIRect>& desiredOutput,
257         const skif::ParameterSpace<SkRect>* knownContentBounds) const {
258     // Map both the device-space desired coverage area and the known content bounds to layer space
259     skif::LayerSpace<SkIRect> desiredBounds = mapping.deviceToLayer(desiredOutput);
260 
261     // TODO (michaelludwig) - To be removed once cropping is its own filter, since then an output
262     // crop would automatically adjust the required input of its child filter in this same way.
263     if (this->cropRectIsSet()) {
264         skif::LayerSpace<SkIRect> outputCrop =
265                 mapping.paramToLayer(skif::ParameterSpace<SkRect>(fCropRect.rect())).roundOut();
266         if (!desiredBounds.intersect(outputCrop)) {
267             // Nothing would be output by the filter, so return empty rect
268             return skif::LayerSpace<SkIRect>(SkIRect::MakeEmpty());
269         }
270     }
271 
272     // If we have no known content bounds use the desired coverage area, because that is the most
273     // conservative possibility.
274     skif::LayerSpace<SkIRect> contentBounds =
275             knownContentBounds ? mapping.paramToLayer(*knownContentBounds).roundOut()
276                                : desiredBounds;
277 
278     // Process the layer-space desired output with the filter DAG to determine required input
279     skif::LayerSpace<SkIRect> requiredInput = this->onGetInputLayerBounds(
280             mapping, desiredBounds, contentBounds);
281     // If we know what's actually going to be drawn into the layer, and we don't change transparent
282     // black, then we can further restrict the layer to what the known content is
283     if (knownContentBounds && !this->affectsTransparentBlack()) {
284         if (!requiredInput.intersect(contentBounds)) {
285             // Nothing would be output by the filter, so return empty rect
286             return skif::LayerSpace<SkIRect>(SkIRect::MakeEmpty());
287         }
288     }
289     return requiredInput;
290 }
291 
getOutputBounds(const skif::Mapping & mapping,const skif::ParameterSpace<SkRect> & contentBounds) const292 skif::DeviceSpace<SkIRect> SkImageFilter_Base::getOutputBounds(
293         const skif::Mapping& mapping, const skif::ParameterSpace<SkRect>& contentBounds) const {
294     // Map the input content into the layer space where filtering will occur
295     skif::LayerSpace<SkRect> layerContent = mapping.paramToLayer(contentBounds);
296     // Determine the filter DAGs output bounds in layer space
297     skif::LayerSpace<SkIRect> filterOutput = this->onGetOutputLayerBounds(
298             mapping, layerContent.roundOut());
299     // FIXME (michaelludwig) - To be removed once cropping is isolated, but remain consistent with
300     // old filterBounds(kForward) behavior.
301     SkIRect dst;
302     as_IFB(this)->getCropRect().applyTo(
303             SkIRect(filterOutput), mapping.layerMatrix(),
304             as_IFB(this)->affectsTransparentBlack(), &dst);
305 
306     // Map all the way to device space
307     return mapping.layerToDevice(skif::LayerSpace<SkIRect>(dst));
308 }
309 
310 // TODO (michaelludwig) - Default to using the old onFilterImage, as filters are updated one by one.
311 // Once the old function is gone, this onFilterImage() will be made a pure virtual.
onFilterImage(const skif::Context & context) const312 skif::FilterResult<For::kOutput> SkImageFilter_Base::onFilterImage(const skif::Context& context) const {
313     SkIPoint origin;
314     auto image = this->onFilterImage(context, &origin);
315     return skif::FilterResult<For::kOutput>(std::move(image), skif::LayerSpace<SkIPoint>(origin));
316 }
317 
canHandleComplexCTM() const318 bool SkImageFilter_Base::canHandleComplexCTM() const {
319     // CropRects need to apply in the source coordinate system, but are not aware of complex CTMs
320     // when performing clipping. For a simple fix, any filter with a crop rect set cannot support
321     // complex CTMs until that's updated.
322     if (this->cropRectIsSet() || !this->onCanHandleComplexCTM()) {
323         return false;
324     }
325     const int count = this->countInputs();
326     for (int i = 0; i < count; ++i) {
327         const SkImageFilter_Base* input = as_IFB(this->getInput(i));
328         if (input && !input->canHandleComplexCTM()) {
329             return false;
330         }
331     }
332     return true;
333 }
334 
applyTo(const SkIRect & imageBounds,const SkMatrix & ctm,bool embiggen,SkIRect * cropped) const335 void SkImageFilter_Base::CropRect::applyTo(const SkIRect& imageBounds, const SkMatrix& ctm,
336                                            bool embiggen, SkIRect* cropped) const {
337     *cropped = imageBounds;
338     if (fFlags) {
339         SkRect devCropR;
340         ctm.mapRect(&devCropR, fRect);
341         SkIRect devICropR = devCropR.roundOut();
342 
343         // Compute the left/top first, in case we need to modify the right/bottom for a missing edge
344         if (fFlags & kHasLeft_CropEdge) {
345             if (embiggen || devICropR.fLeft > cropped->fLeft) {
346                 cropped->fLeft = devICropR.fLeft;
347             }
348         } else {
349             devICropR.fRight = Sk32_sat_add(cropped->fLeft, devICropR.width());
350         }
351         if (fFlags & kHasTop_CropEdge) {
352             if (embiggen || devICropR.fTop > cropped->fTop) {
353                 cropped->fTop = devICropR.fTop;
354             }
355         } else {
356             devICropR.fBottom = Sk32_sat_add(cropped->fTop, devICropR.height());
357         }
358         if (fFlags & kHasWidth_CropEdge) {
359             if (embiggen || devICropR.fRight < cropped->fRight) {
360                 cropped->fRight = devICropR.fRight;
361             }
362         }
363         if (fFlags & kHasHeight_CropEdge) {
364             if (embiggen || devICropR.fBottom < cropped->fBottom) {
365                 cropped->fBottom = devICropR.fBottom;
366             }
367         }
368     }
369 }
370 
applyCropRect(const Context & ctx,const SkIRect & srcBounds,SkIRect * dstBounds) const371 bool SkImageFilter_Base::applyCropRect(const Context& ctx, const SkIRect& srcBounds,
372                                        SkIRect* dstBounds) const {
373     SkIRect tmpDst = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection, nullptr);
374     fCropRect.applyTo(tmpDst, ctx.ctm(), this->affectsTransparentBlack(), dstBounds);
375     // Intersect against the clip bounds, in case the crop rect has
376     // grown the bounds beyond the original clip. This can happen for
377     // example in tiling, where the clip is much smaller than the filtered
378     // primitive. If we didn't do this, we would be processing the filter
379     // at the full crop rect size in every tile.
380     return dstBounds->intersect(ctx.clipBounds());
381 }
382 
383 // Return a larger (newWidth x newHeight) copy of 'src' with black padding
384 // around it.
pad_image(SkSpecialImage * src,const SkImageFilter_Base::Context & ctx,int newWidth,int newHeight,int offX,int offY)385 static sk_sp<SkSpecialImage> pad_image(SkSpecialImage* src, const SkImageFilter_Base::Context& ctx,
386                                        int newWidth, int newHeight, int offX, int offY) {
387     // We would like to operate in the source's color space (so that we return an "identical"
388     // image, other than the padding. To achieve that, we'd create a new context using
389     // src->getColorSpace() to replace ctx.colorSpace().
390 
391     // That fails in at least two ways. For formats that are texturable but not renderable (like
392     // F16 on some ES implementations), we can't create a surface to do the work. For sRGB, images
393     // may be tagged with an sRGB color space (which leads to an sRGB config in makeSurface). But
394     // the actual config of that sRGB image on a device with no sRGB support is non-sRGB.
395     //
396     // Rather than try to special case these situations, we execute the image padding in the
397     // destination color space. This should not affect the output of the DAG in (almost) any case,
398     // because the result of this call is going to be used as an input, where it would have been
399     // switched to the destination space anyway. The one exception would be a filter that expected
400     // to consume unclamped F16 data, but the padded version of the image is pre-clamped to 8888.
401     // We can revisit this logic if that ever becomes an actual problem.
402     sk_sp<SkSpecialSurface> surf(ctx.makeSurface(SkISize::Make(newWidth, newHeight)));
403     if (!surf) {
404         return nullptr;
405     }
406 
407     SkCanvas* canvas = surf->getCanvas();
408     SkASSERT(canvas);
409 
410     canvas->clear(0x0);
411 
412     src->draw(canvas, offX, offY);
413 
414     return surf->makeImageSnapshot();
415 }
416 
applyCropRectAndPad(const Context & ctx,SkSpecialImage * src,SkIPoint * srcOffset,SkIRect * bounds) const417 sk_sp<SkSpecialImage> SkImageFilter_Base::applyCropRectAndPad(const Context& ctx,
418                                                               SkSpecialImage* src,
419                                                               SkIPoint* srcOffset,
420                                                               SkIRect* bounds) const {
421     const SkIRect srcBounds = SkIRect::MakeXYWH(srcOffset->x(), srcOffset->y(),
422                                                 src->width(), src->height());
423 
424     if (!this->applyCropRect(ctx, srcBounds, bounds)) {
425         return nullptr;
426     }
427 
428     if (srcBounds.contains(*bounds)) {
429         return sk_sp<SkSpecialImage>(SkRef(src));
430     } else {
431         sk_sp<SkSpecialImage> img(pad_image(src, ctx, bounds->width(), bounds->height(),
432                                             Sk32_sat_sub(srcOffset->x(), bounds->x()),
433                                             Sk32_sat_sub(srcOffset->y(), bounds->y())));
434         *srcOffset = SkIPoint::Make(bounds->x(), bounds->y());
435         return img;
436     }
437 }
438 
439 // NOTE: The new onGetOutputLayerBounds() and onGetInputLayerBounds() default to calling into the
440 // deprecated onFilterBounds and onFilterNodeBounds. While these functions are not tagged, they do
441 // match the documented default behavior for the new bounds functions.
onFilterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const442 SkIRect SkImageFilter_Base::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
443                                            MapDirection dir, const SkIRect* inputRect) const {
444     if (this->countInputs() < 1) {
445         return src;
446     }
447 
448     SkIRect totalBounds;
449     for (int i = 0; i < this->countInputs(); ++i) {
450         const SkImageFilter* filter = this->getInput(i);
451         SkIRect rect = filter ? filter->filterBounds(src, ctm, dir, inputRect) : src;
452         if (0 == i) {
453             totalBounds = rect;
454         } else {
455             totalBounds.join(rect);
456         }
457     }
458 
459     return totalBounds;
460 }
461 
onFilterNodeBounds(const SkIRect & src,const SkMatrix &,MapDirection,const SkIRect *) const462 SkIRect SkImageFilter_Base::onFilterNodeBounds(const SkIRect& src, const SkMatrix&,
463                                                MapDirection, const SkIRect*) const {
464     return src;
465 }
466 
visitInputLayerBounds(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & desiredOutput,const skif::LayerSpace<SkIRect> & contentBounds) const467 skif::LayerSpace<SkIRect> SkImageFilter_Base::visitInputLayerBounds(
468         const skif::Mapping& mapping, const skif::LayerSpace<SkIRect>& desiredOutput,
469         const skif::LayerSpace<SkIRect>& contentBounds) const {
470     if (this->countInputs() < 1) {
471         // TODO (michaelludwig) - if a filter doesn't have any inputs, it doesn't need any
472         // implicit source image, so arguably we could return an empty rect here. 'desiredOutput' is
473         // consistent with original behavior, so empty bounds may have unintended side effects
474         // but should be explored later.
475         return desiredOutput;
476     }
477 
478     skif::LayerSpace<SkIRect> netInput;
479     for (int i = 0; i < this->countInputs(); ++i) {
480         const SkImageFilter* filter = this->getInput(i);
481         // The required input for this input filter, or 'targetOutput' if the filter is null and
482         // the source image is used (so must be sized to cover 'targetOutput').
483         skif::LayerSpace<SkIRect> requiredInput =
484                 filter ? as_IFB(filter)->onGetInputLayerBounds(mapping, desiredOutput,
485                                                                contentBounds)
486                        : desiredOutput;
487         // Accumulate with all other filters
488         if (i == 0) {
489             netInput = requiredInput;
490         } else {
491             netInput.join(requiredInput);
492         }
493     }
494     return netInput;
495 }
496 
visitOutputLayerBounds(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & contentBounds) const497 skif::LayerSpace<SkIRect> SkImageFilter_Base::visitOutputLayerBounds(
498         const skif::Mapping& mapping, const skif::LayerSpace<SkIRect>& contentBounds) const {
499     if (this->countInputs() < 1) {
500         // TODO (michaelludwig) - if a filter doesn't have any inputs, it presumably is determining
501         // its output size from something other than the implicit source contentBounds, in which
502         // case it shouldn't be calling this helper function, so explore adding an unreachable test
503         return contentBounds;
504     }
505 
506     skif::LayerSpace<SkIRect> netOutput;
507     for (int i = 0; i < this->countInputs(); ++i) {
508         const SkImageFilter* filter = this->getInput(i);
509         // The output for just this input filter, or 'contentBounds' if the filter is null and
510         // the source image is used (i.e. the identity filter applied to the source).
511         skif::LayerSpace<SkIRect> output =
512                 filter ? as_IFB(filter)->onGetOutputLayerBounds(mapping, contentBounds)
513                        : contentBounds;
514         // Accumulate with all other filters
515         if (i == 0) {
516             netOutput = output;
517         } else {
518             netOutput.join(output);
519         }
520     }
521     return netOutput;
522 }
523 
onGetInputLayerBounds(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & desiredOutput,const skif::LayerSpace<SkIRect> & contentBounds,VisitChildren recurse) const524 skif::LayerSpace<SkIRect> SkImageFilter_Base::onGetInputLayerBounds(
525         const skif::Mapping& mapping, const skif::LayerSpace<SkIRect>& desiredOutput,
526         const skif::LayerSpace<SkIRect>& contentBounds, VisitChildren recurse) const {
527     // Call old functions for now since they may have been overridden by a subclass that's not been
528     // updated yet; normally this would just default to visitInputLayerBounds()
529     SkIRect content = SkIRect(contentBounds);
530     SkIRect input = this->onFilterNodeBounds(SkIRect(desiredOutput), mapping.layerMatrix(),
531                                              kReverse_MapDirection, &content);
532     if (recurse == VisitChildren::kYes) {
533         SkIRect aggregate = this->onFilterBounds(input, mapping.layerMatrix(),
534                                                  kReverse_MapDirection, &input);
535         return skif::LayerSpace<SkIRect>(aggregate);
536     } else {
537         return skif::LayerSpace<SkIRect>(input);
538     }
539 }
540 
onGetOutputLayerBounds(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & contentBounds) const541 skif::LayerSpace<SkIRect> SkImageFilter_Base::onGetOutputLayerBounds(
542         const skif::Mapping& mapping, const skif::LayerSpace<SkIRect>& contentBounds) const {
543     // Call old functions for now; normally this would default to visitOutputLayerBounds()
544     SkIRect aggregate = this->onFilterBounds(SkIRect(contentBounds), mapping.layerMatrix(),
545                                              kForward_MapDirection, nullptr);
546     SkIRect output = this->onFilterNodeBounds(aggregate, mapping.layerMatrix(),
547                                               kForward_MapDirection, nullptr);
548     return skif::LayerSpace<SkIRect>(output);
549 }
550 
551 template<skif::Usage kU>
filterInput(int index,const skif::Context & ctx) const552 skif::FilterResult<kU> SkImageFilter_Base::filterInput(int index, const skif::Context& ctx) const {
553     SkASSERT(kU != skif::Usage::kInput0 || index == 0);
554     SkASSERT(kU != skif::Usage::kInput1 || index == 1);
555 
556     const SkImageFilter* input = this->getInput(index);
557     if (!input) {
558         // Convert from the generic kInput of the source image to kU
559         return static_cast<skif::FilterResult<kU>>(ctx.source());
560     }
561 
562     skif::FilterResult<For::kOutput> result = as_IFB(input)->filterImage(this->mapContext(ctx));
563     SkASSERT(!result.image() || ctx.gpuBacked() == result.image()->isTextureBacked());
564 
565     // Map the output result of the input image filter to the input usage requested for this filter
566     return static_cast<skif::FilterResult<kU>>(std::move(result));
567 }
568 // Instantiate filterInput() for kInput, kInput0, and kInput1. This does not provide a definition
569 // for kOutput, which should never be used anyways, and this way the linker will fail for us then.
570 template skif::FilterResult<For::kInput> SkImageFilter_Base::filterInput(int, const skif::Context&) const;
571 template skif::FilterResult<For::kInput0> SkImageFilter_Base::filterInput(int, const skif::Context&) const;
572 template skif::FilterResult<For::kInput1> SkImageFilter_Base::filterInput(int, const skif::Context&) const;
573 
mapContext(const Context & ctx) const574 SkImageFilter_Base::Context SkImageFilter_Base::mapContext(const Context& ctx) const {
575     // We don't recurse through the child input filters because that happens automatically
576     // as part of the filterImage() evaluation. In this case, we want the bounds for the
577     // edge from this node to its children, without the effects of the child filters.
578     skif::LayerSpace<SkIRect> childOutput = this->onGetInputLayerBounds(
579             ctx.mapping(), ctx.desiredOutput(), ctx.desiredOutput(), VisitChildren::kNo);
580     return ctx.withNewDesiredOutput(childOutput);
581 }
582 
583 #if SK_SUPPORT_GPU
DrawWithFP(GrRecordingContext * context,std::unique_ptr<GrFragmentProcessor> fp,const SkIRect & bounds,SkColorType colorType,const SkColorSpace * colorSpace,const SkSurfaceProps & surfaceProps,GrProtected isProtected)584 sk_sp<SkSpecialImage> SkImageFilter_Base::DrawWithFP(GrRecordingContext* context,
585                                                      std::unique_ptr<GrFragmentProcessor> fp,
586                                                      const SkIRect& bounds,
587                                                      SkColorType colorType,
588                                                      const SkColorSpace* colorSpace,
589                                                      const SkSurfaceProps& surfaceProps,
590                                                      GrProtected isProtected) {
591     GrImageInfo info(SkColorTypeToGrColorType(colorType),
592                      kPremul_SkAlphaType,
593                      sk_ref_sp(colorSpace),
594                      bounds.size());
595 
596     auto surfaceFillContext = GrSurfaceFillContext::Make(context,
597                                                          info,
598                                                          SkBackingFit::kApprox,
599                                                          1,
600                                                          GrMipmapped::kNo,
601                                                          isProtected,
602                                                          kBottomLeft_GrSurfaceOrigin);
603     if (!surfaceFillContext) {
604         return nullptr;
605     }
606 
607     SkIRect dstIRect = SkIRect::MakeWH(bounds.width(), bounds.height());
608     SkRect srcRect = SkRect::Make(bounds);
609     surfaceFillContext->fillRectToRectWithFP(srcRect, dstIRect, std::move(fp));
610 
611     return SkSpecialImage::MakeDeferredFromGpu(context,
612                                                dstIRect,
613                                                kNeedNewImageUniqueID_SpecialImage,
614                                                surfaceFillContext->readSurfaceView(),
615                                                surfaceFillContext->colorInfo().colorType(),
616                                                surfaceFillContext->colorInfo().refColorSpace(),
617                                                surfaceProps);
618 }
619 
ImageToColorSpace(SkSpecialImage * src,SkColorType colorType,SkColorSpace * colorSpace,const SkSurfaceProps & surfaceProps)620 sk_sp<SkSpecialImage> SkImageFilter_Base::ImageToColorSpace(SkSpecialImage* src,
621                                                             SkColorType colorType,
622                                                             SkColorSpace* colorSpace,
623                                                             const SkSurfaceProps& surfaceProps) {
624     // There are several conditions that determine if we actually need to convert the source to the
625     // destination's color space. Rather than duplicate that logic here, just try to make an xform
626     // object. If that produces something, then both are tagged, and the source is in a different
627     // gamut than the dest. There is some overhead to making the xform, but those are cached, and
628     // if we get one back, that means we're about to use it during the conversion anyway.
629     auto colorSpaceXform = GrColorSpaceXform::Make(src->getColorSpace(),  src->alphaType(),
630                                                    colorSpace, kPremul_SkAlphaType);
631 
632     if (!colorSpaceXform) {
633         // No xform needed, just return the original image
634         return sk_ref_sp(src);
635     }
636 
637     sk_sp<SkSpecialSurface> surf(src->makeSurface(colorType, colorSpace,
638                                                   SkISize::Make(src->width(), src->height()),
639                                                   kPremul_SkAlphaType, surfaceProps));
640     if (!surf) {
641         return sk_ref_sp(src);
642     }
643 
644     SkCanvas* canvas = surf->getCanvas();
645     SkASSERT(canvas);
646     SkPaint p;
647     p.setBlendMode(SkBlendMode::kSrc);
648     src->draw(canvas, 0, 0, SkSamplingOptions(), &p);
649     return surf->makeImageSnapshot();
650 }
651 #endif
652 
653 // In repeat mode, when we are going to sample off one edge of the srcBounds we require the
654 // opposite side be preserved.
DetermineRepeatedSrcBound(const SkIRect & srcBounds,const SkIVector & filterOffset,const SkISize & filterSize,const SkIRect & originalSrcBounds)655 SkIRect SkImageFilter_Base::DetermineRepeatedSrcBound(const SkIRect& srcBounds,
656                                                       const SkIVector& filterOffset,
657                                                       const SkISize& filterSize,
658                                                       const SkIRect& originalSrcBounds) {
659     SkIRect tmp = srcBounds;
660     tmp.adjust(-filterOffset.fX, -filterOffset.fY,
661                filterSize.fWidth - filterOffset.fX, filterSize.fHeight - filterOffset.fY);
662 
663     if (tmp.fLeft < originalSrcBounds.fLeft || tmp.fRight > originalSrcBounds.fRight) {
664         tmp.fLeft = originalSrcBounds.fLeft;
665         tmp.fRight = originalSrcBounds.fRight;
666     }
667     if (tmp.fTop < originalSrcBounds.fTop || tmp.fBottom > originalSrcBounds.fBottom) {
668         tmp.fTop = originalSrcBounds.fTop;
669         tmp.fBottom = originalSrcBounds.fBottom;
670     }
671 
672     return tmp;
673 }
674 
PurgeCache()675 void SkImageFilter_Base::PurgeCache() {
676     SkImageFilterCache::Get()->purge();
677 }
678 
apply_ctm_to_filter(sk_sp<SkImageFilter> input,const SkMatrix & ctm,SkMatrix * remainder)679 static sk_sp<SkImageFilter> apply_ctm_to_filter(sk_sp<SkImageFilter> input, const SkMatrix& ctm,
680                                                 SkMatrix* remainder) {
681     if (ctm.isScaleTranslate() || as_IFB(input)->canHandleComplexCTM()) {
682         // The filter supports the CTM, so leave it as-is and 'remainder' stores the whole CTM
683         *remainder = ctm;
684         return input;
685     }
686 
687     // We have a complex CTM and a filter that can't support them, so it needs to use the matrix
688     // transform filter that resamples the image contents. Decompose the simple portion of the ctm
689     // into 'remainder'
690     SkMatrix ctmToEmbed;
691     SkSize scale;
692     if (ctm.decomposeScale(&scale, &ctmToEmbed)) {
693         // decomposeScale splits ctm into scale * ctmToEmbed, so bake ctmToEmbed into DAG
694         // with a matrix filter and return scale as the remaining matrix for the real CTM.
695         remainder->setScale(scale.fWidth, scale.fHeight);
696 
697         // ctmToEmbed is passed to SkMatrixImageFilter, which performs its transforms as if it were
698         // a pre-transformation before applying the image-filter context's CTM. In this case, we
699         // need ctmToEmbed to be a post-transformation (i.e. after the scale matrix since
700         // decomposeScale produces ctm = ctmToEmbed * scale). Giving scale^-1 * ctmToEmbed * scale
701         // to the matrix filter achieves this effect.
702         // TODO (michaelludwig) - When the original root node of a filter can be drawn directly to a
703         // device using ctmToEmbed, this abuse of SkMatrixImageFilter can go away.
704         ctmToEmbed.preScale(scale.fWidth, scale.fHeight);
705         ctmToEmbed.postScale(1.f / scale.fWidth, 1.f / scale.fHeight);
706     } else {
707         // Unable to decompose
708         // FIXME Ideally we'd embed the entire CTM as part of the matrix image filter, but
709         // the device <-> src bounds calculations for filters are very brittle under perspective,
710         // and can easily run into precision issues (wrong bounds that clip), or performance issues
711         // (producing large source-space images where 80% of the image is compressed into a few
712         // device pixels). A longer term solution for perspective-space image filtering is needed
713         // see skbug.com/9074
714         if (ctm.hasPerspective()) {
715                 *remainder = ctm;
716             return input;
717         }
718 
719         ctmToEmbed = ctm;
720         remainder->setIdentity();
721     }
722 
723     return SkMatrixImageFilter::Make(ctmToEmbed, SkSamplingOptions(SkFilterMode::kLinear), input);
724 }
725 
applyCTM(const SkMatrix & ctm,SkMatrix * remainder) const726 sk_sp<SkImageFilter> SkImageFilter_Base::applyCTM(const SkMatrix& ctm, SkMatrix* remainder) const {
727     return apply_ctm_to_filter(this->refMe(), ctm, remainder);
728 }
729