1 /*
2  * Copyright 2019 Google LLC
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 #ifndef SkImageFilterTypes_DEFINED
9 #define SkImageFilterTypes_DEFINED
10 
11 #include "src/core/SkSpecialImage.h"
12 #include "src/core/SkSpecialSurface.h"
13 
14 class GrRecordingContext;
15 class SkImageFilter;
16 class SkImageFilterCache;
17 class SkSpecialSurface;
18 class SkSurfaceProps;
19 
20 // The skif (SKI[mage]F[ilter]) namespace contains types that are used for filter implementations.
21 // The defined types come in two groups: users of internal Skia types, and templates to help with
22 // readability. Image filters cannot be implemented without access to key internal types, such as
23 // SkSpecialImage. It is possible to avoid the use of the readability templates, although they are
24 // strongly encouraged.
25 namespace skif {
26 
27 // skif::IVector and skif::Vector represent plain-old-data types for storing direction vectors, so
28 // that the coordinate-space templating system defined below can have a separate type id for
29 // directions vs. points, and specialize appropriately. As such, all operations with direction
30 // vectors are defined on the LayerSpace specialization, since that is the intended point of use.
31 struct IVector {
32     int32_t fX;
33     int32_t fY;
34 
35     IVector() = default;
IVectorIVector36     IVector(int32_t x, int32_t y) : fX(x), fY(y) {}
IVectorIVector37     explicit IVector(const SkIVector& v) : fX(v.fX), fY(v.fY) {}
38 };
39 
40 struct Vector {
41     SkScalar fX;
42     SkScalar fY;
43 
44     Vector() = default;
VectorVector45     Vector(SkScalar x, SkScalar y) : fX(x), fY(y) {}
VectorVector46     explicit Vector(const SkVector& v) : fX(v.fX), fY(v.fY) {}
47 };
48 
49 ///////////////////////////////////////////////////////////////////////////////////////////////////
50 // Coordinate Space Tagging
51 // - In order to enforce correct coordinate spaces in image filter implementations and use,
52 //   geometry is wrapped by templated structs to declare in the type system what coordinate space
53 //   the coordinates are defined in.
54 // - Currently there is ParameterSpace and DeviceSpace that are data-only wrappers around
55 //   coordinates, and the primary LayerSpace that provides all operative functionality for image
56 //   filters. It is intended that all logic about image bounds and access be conducted in the shared
57 //   layer space.
58 // - The LayerSpace struct has type-safe specializations for SkIRect, SkRect, SkIPoint, SkPoint,
59 //   skif::IVector (to distinguish SkIVector from SkIPoint), skif::Vector, SkISize, and SkSize.
60 // - A Mapping object provides type safe coordinate conversions between these spaces, and
61 //   automatically does the "right thing" for each geometric type.
62 ///////////////////////////////////////////////////////////////////////////////////////////////////
63 
64 // ParameterSpace is a data-only wrapper around Skia's geometric types such as SkIPoint, and SkRect.
65 // Parameter space is the same as the local coordinate space of an SkShader, or the coordinates
66 // passed into SkCanvas::drawX calls, but "local" is avoided due to the alliteration with layer
67 // space. SkImageFilters are defined in terms of ParameterSpace<T> geometry and must use the Mapping
68 // on Context to transform the parameters into LayerSpace to evaluate the filter in the shared
69 // coordinate space of the entire filter DAG.
70 //
71 // A value of ParameterSpace<SkIRect> implies that its wrapped SkIRect is defined in the local
72 // parameter space.
73 template<typename T>
74 class ParameterSpace {
75 public:
ParameterSpace(const T & data)76     explicit ParameterSpace(const T& data) : fData(data) {}
ParameterSpace(T && data)77     explicit ParameterSpace(T&& data) : fData(std::move(data)) {}
78 
79     explicit operator const T&() const { return fData; }
80 
81 private:
82     T fData;
83 };
84 
85 // DeviceSpace is a data-only wrapper around Skia's geometric types. It is similar to
86 // 'ParameterSpace' except that it is used to represent geometry that has been transformed or
87 // defined in the root device space (i.e. the final pixels of drawn content). Much of what SkCanvas
88 // tracks, such as its clip bounds are defined in this space and DeviceSpace provides a
89 // type-enforced mechanism for the canvas to pass that information into the image filtering system,
90 // using the Mapping of the filtering context.
91 template<typename T>
92 class DeviceSpace {
93 public:
DeviceSpace(const T & data)94     explicit DeviceSpace(const T& data) : fData(data) {}
DeviceSpace(T && data)95     explicit DeviceSpace(T&& data) : fData(std::move(data)) {}
96 
97     explicit operator const T&() const { return fData; }
98 
99 private:
100     T fData;
101 };
102 
103 // LayerSpace is a geometric wrapper that specifies the geometry is defined in the shared layer
104 // space where image filters are evaluated. For a given Context (and its Mapping), the image filter
105 // DAG operates in the same coordinate space. This space may be different from the local coordinate
106 // space that defined the image filter parameters (such as blur sigma), and it may be different
107 // from the total CTM of the SkCanvas.
108 //
109 // To encourage correct filter use and implementation, the bulk of filter logic should be performed
110 // in layer space (e.g. determining what portion of an input image to read, or what the output
111 // region is). LayerSpace specializations for the six common Skia math types (Sk[I]Rect, Sk[I]Point,
112 // and Sk[I]Size), and skif::[I]Vector (to allow vectors to be specialized separately from points))
113 // are provided that mimic their APIs but preserve the coordinate space and enforce type semantics.
114 template<typename T>
115 class LayerSpace {};
116 
117 // Layer-space specialization for integerized direction vectors.
118 template<>
119 class LayerSpace<IVector> {
120 public:
121     LayerSpace() = default;
LayerSpace(const IVector & geometry)122     explicit LayerSpace(const IVector& geometry) : fData(geometry) {}
LayerSpace(IVector && geometry)123     explicit LayerSpace(IVector&& geometry) : fData(std::move(geometry)) {}
124     explicit operator const IVector&() const { return fData; }
125 
SkIVector()126     explicit operator SkIVector() const { return SkIVector::Make(fData.fX, fData.fY); }
127 
x()128     int32_t x() const { return fData.fX; }
y()129     int32_t y() const { return fData.fY; }
130 
131     LayerSpace<IVector> operator-() const { return LayerSpace<IVector>({-fData.fX, -fData.fY}); }
132 
133     LayerSpace<IVector> operator+(const LayerSpace<IVector>& v) const {
134         LayerSpace<IVector> sum = *this;
135         sum += v;
136         return sum;
137     }
138     LayerSpace<IVector> operator-(const LayerSpace<IVector>& v) const {
139         LayerSpace<IVector> diff = *this;
140         diff -= v;
141         return diff;
142     }
143 
144     void operator+=(const LayerSpace<IVector>& v) {
145         fData.fX += v.fData.fX;
146         fData.fY += v.fData.fY;
147     }
148     void operator-=(const LayerSpace<IVector>& v) {
149         fData.fX -= v.fData.fX;
150         fData.fY -= v.fData.fY;
151     }
152 
153 private:
154     IVector fData;
155 };
156 
157 // Layer-space specialization for floating point direction vectors.
158 template<>
159 class LayerSpace<Vector> {
160 public:
161     LayerSpace() = default;
LayerSpace(const Vector & geometry)162     explicit LayerSpace(const Vector& geometry) : fData(geometry) {}
LayerSpace(Vector && geometry)163     explicit LayerSpace(Vector&& geometry) : fData(std::move(geometry)) {}
164     explicit operator const Vector&() const { return fData; }
165 
SkVector()166     explicit operator SkVector() const { return SkVector::Make(fData.fX, fData.fY); }
167 
x()168     SkScalar x() const { return fData.fX; }
y()169     SkScalar y() const { return fData.fY; }
170 
length()171     SkScalar length() const { return SkVector::Length(fData.fX, fData.fY); }
172 
173     LayerSpace<Vector> operator-() const { return LayerSpace<Vector>({-fData.fX, -fData.fY}); }
174 
175     LayerSpace<Vector> operator*(SkScalar s) const {
176         LayerSpace<Vector> scaled = *this;
177         scaled *= s;
178         return scaled;
179     }
180 
181     LayerSpace<Vector> operator+(const LayerSpace<Vector>& v) const {
182         LayerSpace<Vector> sum = *this;
183         sum += v;
184         return sum;
185     }
186     LayerSpace<Vector> operator-(const LayerSpace<Vector>& v) const {
187         LayerSpace<Vector> diff = *this;
188         diff -= v;
189         return diff;
190     }
191 
192     void operator*=(SkScalar s) {
193         fData.fX *= s;
194         fData.fY *= s;
195     }
196     void operator+=(const LayerSpace<Vector>& v) {
197         fData.fX += v.fData.fX;
198         fData.fY += v.fData.fY;
199     }
200     void operator-=(const LayerSpace<Vector>& v) {
201         fData.fX -= v.fData.fX;
202         fData.fY -= v.fData.fY;
203     }
204 
205     friend LayerSpace<Vector> operator*(SkScalar s, const LayerSpace<Vector>& b) {
206         return b * s;
207     }
208 
209 private:
210     Vector fData;
211 };
212 
213 // Layer-space specialization for integer 2D coordinates (treated as positions, not directions).
214 template<>
215 class LayerSpace<SkIPoint> {
216 public:
217     LayerSpace() = default;
LayerSpace(const SkIPoint & geometry)218     explicit LayerSpace(const SkIPoint& geometry)  : fData(geometry) {}
LayerSpace(SkIPoint && geometry)219     explicit LayerSpace(SkIPoint&& geometry) : fData(std::move(geometry)) {}
220     explicit operator const SkIPoint&() const { return fData; }
221 
222     // Parrot the SkIPoint API while preserving coordinate space.
x()223     int32_t x() const { return fData.fX; }
y()224     int32_t y() const { return fData.fY; }
225 
226     // Offsetting by direction vectors produce more points
227     LayerSpace<SkIPoint> operator+(const LayerSpace<IVector>& v) {
228         return LayerSpace<SkIPoint>(fData + SkIVector(v));
229     }
230     LayerSpace<SkIPoint> operator-(const LayerSpace<IVector>& v) {
231         return LayerSpace<SkIPoint>(fData - SkIVector(v));
232     }
233 
234     void operator+=(const LayerSpace<IVector>& v) {
235         fData += SkIVector(v);
236     }
237     void operator-=(const LayerSpace<IVector>& v) {
238         fData -= SkIVector(v);
239     }
240 
241     // Subtracting another point makes a direction between them
242     LayerSpace<IVector> operator-(const LayerSpace<SkIPoint>& p) {
243         return LayerSpace<IVector>(IVector(fData - p.fData));
244     }
245 
246 private:
247     SkIPoint fData;
248 };
249 
250 // Layer-space specialization for floating point 2D coordinates (treated as positions)
251 template<>
252 class LayerSpace<SkPoint> {
253 public:
254     LayerSpace() = default;
LayerSpace(const SkPoint & geometry)255     explicit LayerSpace(const SkPoint& geometry) : fData(geometry) {}
LayerSpace(SkPoint && geometry)256     explicit LayerSpace(SkPoint&& geometry) : fData(std::move(geometry)) {}
257     explicit operator const SkPoint&() const { return fData; }
258 
259     // Parrot the SkPoint API while preserving coordinate space.
x()260     SkScalar x() const { return fData.fX; }
y()261     SkScalar y() const { return fData.fY; }
262 
distanceToOrigin()263     SkScalar distanceToOrigin() const { return fData.distanceToOrigin(); }
264 
265     // Offsetting by direction vectors produce more points
266     LayerSpace<SkPoint> operator+(const LayerSpace<Vector>& v) {
267         return LayerSpace<SkPoint>(fData + SkVector(v));
268     }
269     LayerSpace<SkPoint> operator-(const LayerSpace<Vector>& v) {
270         return LayerSpace<SkPoint>(fData - SkVector(v));
271     }
272 
273     void operator+=(const LayerSpace<Vector>& v) {
274         fData += SkVector(v);
275     }
276     void operator-=(const LayerSpace<Vector>& v) {
277         fData -= SkVector(v);
278     }
279 
280     // Subtracting another point makes a direction between them
281     LayerSpace<Vector> operator-(const LayerSpace<SkPoint>& p) {
282         return LayerSpace<Vector>(Vector(fData - p.fData));
283     }
284 
285 private:
286     SkPoint fData;
287 };
288 
289 // Layer-space specialization for integer dimensions
290 template<>
291 class LayerSpace<SkISize> {
292 public:
293     LayerSpace() = default;
LayerSpace(const SkISize & geometry)294     explicit LayerSpace(const SkISize& geometry) : fData(geometry) {}
LayerSpace(SkISize && geometry)295     explicit LayerSpace(SkISize&& geometry) : fData(std::move(geometry)) {}
296     explicit operator const SkISize&() const { return fData; }
297 
width()298     int32_t width() const { return fData.width(); }
height()299     int32_t height() const { return fData.height(); }
300 
isEmpty()301     bool isEmpty() const { return fData.isEmpty(); }
302 
303 private:
304     SkISize fData;
305 };
306 
307 // Layer-space specialization for floating point dimensions
308 template<>
309 class LayerSpace<SkSize> {
310 public:
311     LayerSpace() = default;
LayerSpace(const SkSize & geometry)312     explicit LayerSpace(const SkSize& geometry) : fData(geometry) {}
LayerSpace(SkSize && geometry)313     explicit LayerSpace(SkSize&& geometry) : fData(std::move(geometry)) {}
314     explicit operator const SkSize&() const { return fData; }
315 
width()316     SkScalar width() const { return fData.width(); }
height()317     SkScalar height() const { return fData.height(); }
318 
isEmpty()319     bool isEmpty() const { return fData.isEmpty(); }
isZero()320     bool isZero() const { return fData.isZero(); }
321 
round()322     LayerSpace<SkISize> round() const { return LayerSpace<SkISize>(fData.toRound()); }
ceil()323     LayerSpace<SkISize> ceil() const { return LayerSpace<SkISize>(fData.toCeil()); }
floor()324     LayerSpace<SkISize> floor() const { return LayerSpace<SkISize>(fData.toFloor()); }
325 
326 private:
327     SkSize fData;
328 };
329 
330 // Layer-space specialization for axis-aligned integer bounding boxes.
331 template<>
332 class LayerSpace<SkIRect> {
333 public:
334     LayerSpace() = default;
LayerSpace(const SkIRect & geometry)335     explicit LayerSpace(const SkIRect& geometry) : fData(geometry) {}
LayerSpace(SkIRect && geometry)336     explicit LayerSpace(SkIRect&& geometry) : fData(std::move(geometry)) {}
337     explicit operator const SkIRect&() const { return fData; }
338 
339     // Parrot the SkIRect API while preserving coord space
left()340     int32_t left() const { return fData.fLeft; }
top()341     int32_t top() const { return fData.fTop; }
right()342     int32_t right() const { return fData.fRight; }
bottom()343     int32_t bottom() const { return fData.fBottom; }
344 
width()345     int32_t width() const { return fData.width(); }
height()346     int32_t height() const { return fData.height(); }
347 
topLeft()348     LayerSpace<SkIPoint> topLeft() const { return LayerSpace<SkIPoint>(fData.topLeft()); }
size()349     LayerSpace<SkISize> size() const { return LayerSpace<SkISize>(fData.size()); }
350 
intersect(const LayerSpace<SkIRect> & r)351     bool intersect(const LayerSpace<SkIRect>& r) { return fData.intersect(r.fData); }
join(const LayerSpace<SkIRect> & r)352     void join(const LayerSpace<SkIRect>& r) { fData.join(r.fData); }
offset(const LayerSpace<IVector> & v)353     void offset(const LayerSpace<IVector>& v) { fData.offset(SkIVector(v)); }
outset(const LayerSpace<SkISize> & delta)354     void outset(const LayerSpace<SkISize>& delta) { fData.outset(delta.width(), delta.height()); }
355 
356 private:
357     SkIRect fData;
358 };
359 
360 // Layer-space specialization for axis-aligned float bounding boxes.
361 template<>
362 class LayerSpace<SkRect> {
363 public:
364     LayerSpace() = default;
LayerSpace(const SkRect & geometry)365     explicit LayerSpace(const SkRect& geometry) : fData(geometry) {}
LayerSpace(SkRect && geometry)366     explicit LayerSpace(SkRect&& geometry) : fData(std::move(geometry)) {}
367     explicit operator const SkRect&() const { return fData; }
368 
369     // Parrot the SkRect API while preserving coord space and usage
left()370     SkScalar left() const { return fData.fLeft; }
top()371     SkScalar top() const { return fData.fTop; }
right()372     SkScalar right() const { return fData.fRight; }
bottom()373     SkScalar bottom() const { return fData.fBottom; }
374 
width()375     SkScalar width() const { return fData.width(); }
height()376     SkScalar height() const { return fData.height(); }
377 
topLeft()378     LayerSpace<SkPoint> topLeft() const {
379         return LayerSpace<SkPoint>(SkPoint::Make(fData.fLeft, fData.fTop));
380     }
size()381     LayerSpace<SkSize> size() const {
382         return LayerSpace<SkSize>(SkSize::Make(fData.width(), fData.height()));
383     }
roundOut()384     LayerSpace<SkIRect> roundOut() const { return LayerSpace<SkIRect>(fData.roundOut()); }
385 
intersect(const LayerSpace<SkRect> & r)386     bool intersect(const LayerSpace<SkRect>& r) { return fData.intersect(r.fData); }
join(const LayerSpace<SkRect> & r)387     void join(const LayerSpace<SkRect>& r) { fData.join(r.fData); }
offset(const LayerSpace<Vector> & v)388     void offset(const LayerSpace<Vector>& v) { fData.offset(SkVector(v)); }
outset(const LayerSpace<SkSize> & delta)389     void outset(const LayerSpace<SkSize>& delta) { fData.outset(delta.width(), delta.height()); }
390 
391 private:
392     SkRect fData;
393 };
394 
395 // Mapping is the primary definition of the shared layer space used when evaluating an image filter
396 // DAG. It encapsulates any needed decomposition of the total CTM into the parameter-to-layer matrix
397 // (that filters use to map their parameters to the layer space), and the layer-to-device matrix
398 // (that canvas uses to map the output layer-space image into its root device space). Mapping
399 // defines functions to transform ParameterSpace and DeviceSpace types to and from their LayerSpace
400 // variants, which can then be used and reasoned about by SkImageFilter implementations.
401 class Mapping {
402 public:
403     // This constructor allows the decomposition to be explicitly provided
Mapping(const SkMatrix & layerToDev,const SkMatrix & paramToLayer)404     Mapping(const SkMatrix& layerToDev, const SkMatrix& paramToLayer)
405             : fLayerToDevMatrix(layerToDev)
406             , fParamToLayerMatrix(paramToLayer) {}
407 
408     // Make the default decomposition Mapping, given the total CTM and the root image filter.
409     static Mapping Make(const SkMatrix& ctm, const SkImageFilter* filter);
410 
411     // Return a new Mapping object whose parameter-to-layer matrix is equal to this->layerMatrix() *
412     // local, but both share the same layer-to-device matrix.
concatLocal(const SkMatrix & local)413     Mapping concatLocal(const SkMatrix& local) const {
414         return Mapping(fLayerToDevMatrix, SkMatrix::Concat(fParamToLayerMatrix, local));
415     }
416 
deviceMatrix()417     const SkMatrix& deviceMatrix() const { return fLayerToDevMatrix; }
layerMatrix()418     const SkMatrix& layerMatrix() const { return fParamToLayerMatrix; }
totalMatrix()419     SkMatrix totalMatrix() const {
420         return SkMatrix::Concat(fLayerToDevMatrix, fParamToLayerMatrix);
421     }
422 
423     template<typename T>
paramToLayer(const ParameterSpace<T> & paramGeometry)424     LayerSpace<T> paramToLayer(const ParameterSpace<T>& paramGeometry) const {
425         return LayerSpace<T>(map(static_cast<const T&>(paramGeometry), fParamToLayerMatrix));
426     }
427 
428     template<typename T>
deviceToLayer(const DeviceSpace<T> & devGeometry)429     LayerSpace<T> deviceToLayer(const DeviceSpace<T>& devGeometry) const {
430         // The mapping from device space to layer space is defined by the inverse of the
431         // layer-to-device matrix
432         SkMatrix devToLayerMatrix;
433         if (!fLayerToDevMatrix.invert(&devToLayerMatrix)) {
434             // Punt and just pass through the geometry unmodified...
435             return LayerSpace<T>(static_cast<const T&>(devGeometry));
436         } else {
437             return LayerSpace<T>(map(static_cast<const T&>(devGeometry), devToLayerMatrix));
438         }
439     }
440 
441     template<typename T>
layerToDevice(const LayerSpace<T> & layerGeometry)442     DeviceSpace<T> layerToDevice(const LayerSpace<T>& layerGeometry) const {
443         return DeviceSpace<T>(map(static_cast<const T&>(layerGeometry), fLayerToDevMatrix));
444     }
445 
446 private:
447     // The image filter process decomposes the total CTM into layerToDev * paramToLayer and uses the
448     // param-to-layer matrix to define the layer-space coordinate system. Depending on how it's
449     // decomposed, either the layer matrix or the device matrix could be the identity matrix (but
450     // sometimes neither).
451     SkMatrix fLayerToDevMatrix;
452     SkMatrix fParamToLayerMatrix;
453 
454     // Actual geometric mapping operations that work on coordinates and matrices w/o the type
455     // safety of the coordinate space wrappers (hence these are private).
456     template<typename T>
457     static T map(const T& geom, const SkMatrix& matrix);
458 };
459 
460 // Usage is a template tag to improve the readability of filter implementations. It is attached to
461 // images and geometry to group a collection of related variables and ensure that moving from one
462 // use case to another is explicit.
463 // NOTE: This can be aliased as 'For' when using the fluent type names.
464 // TODO (michaelludwig) - If the primary motivation for Usage--enforcing layer to image space
465 // transformations safely when multiple images are involved--can be handled entirely by helper
466 // functions on FilterResult, then Usage can go away and FilterResult will not need to be templated
467 enum class Usage {
468     // Designates the semantic purpose of the bounds, coordinate, or image as being an input
469     // to the image filter calculations. When this usage is used, it denotes a generic input,
470     // such as the current input in a dynamic loop, or some aggregate of all inputs. Because
471     // most image filters consume 1 or 2 filters only, the related kInput0 and kInput1 are
472     // also defined.
473     kInput,
474     // A more specific version of kInput, this marks the tagged variable as attached to the
475     // image filter of SkImageFilter_Base::getInput(0).
476     kInput0,
477     // A more specific version of kInput, this marks the tagged variable as attached to the
478     // image filter of SkImageFilter_Base::getInput(1).
479     kInput1,
480     // Designates the purpose of the bounds, coordinate, or image as being the output of the
481     // current image filter calculation. There is only ever one output for an image filter.
482     kOutput,
483 };
484 
485 // Convenience macros to add 'using' declarations that rename the above enums to provide a more
486 // fluent and readable API. This should only be used in a private or local scope to prevent leakage
487 // of the names. Use the IN_CLASS variant at the start of a class declaration in those scenarios.
488 // These macros enable the following simpler type names:
489 //   skif::Image<skif::Usage::kInput> -> Image<For::kInput>
490 #define SK_USE_FLUENT_IMAGE_FILTER_TYPES \
491     using For = skif::Usage;
492 
493 #define SK_USE_FLUENT_IMAGE_FILTER_TYPES_IN_CLASS \
494     protected: SK_USE_FLUENT_IMAGE_FILTER_TYPES public:
495 
496 // Wraps an SkSpecialImage and tags it with a corresponding usage, either as generic input (e.g. the
497 // source image), or a specific input image from a filter's connected inputs. It also includes the
498 // origin of the image in the layer space. This origin is used to draw the image in the correct
499 // location. The 'layerBounds' rectangle of the filtered image is the layer-space bounding box of
500 // the image. It has its top left corner at 'origin' and has the same dimensions as the underlying
501 // special image subset. Transforming 'layerBounds' by the Context's layer matrix and painting it
502 // with the subset rectangle will display the filtered results in the appropriate device-space
503 // region.
504 //
505 // When filter implementations are processing intermediate FilterResult results, it can be assumed
506 // that all FilterResult' layerBounds are in the same layer coordinate space defined by the shared
507 // skif::Context.
508 //
509 // NOTE: This is named FilterResult since most instances will represent the output of an image
510 // filter (even if that is then cast to be the input to the next filter). The main exception is the
511 // source input used when an input filter is null, but from a data-standpoint it is the same since
512 // it is equivalent to the result of an identity filter.
513 template<Usage kU>
514 class FilterResult {
515 public:
FilterResult()516     FilterResult() : fImage(nullptr), fOrigin(SkIPoint::Make(0, 0)) {}
517 
FilterResult(sk_sp<SkSpecialImage> image,const LayerSpace<SkIPoint> & origin)518     FilterResult(sk_sp<SkSpecialImage> image, const LayerSpace<SkIPoint>& origin)
519             : fImage(std::move(image))
520             , fOrigin(origin) {}
521 
522     // Allow explicit moves/copies in order to cast from one use type to another, except kInput0
523     // and kInput1 can only be cast to kOutput (e.g. as part of a noop image filter).
524     template<Usage kI>
FilterResult(FilterResult<kI> && image)525     explicit FilterResult(FilterResult<kI>&& image)
526             : fImage(std::move(image.fImage))
527             , fOrigin(image.fOrigin) {
528         static_assert((kU != Usage::kInput) || (kI != Usage::kInput0 && kI != Usage::kInput1),
529                       "kInput0 and kInput1 cannot be moved to more generic kInput usage.");
530         static_assert((kU != Usage::kInput0 && kU != Usage::kInput1) ||
531                       (kI == kU || kI == Usage::kInput || kI == Usage::kOutput),
532                       "Can only move to specific input from the generic kInput or kOutput usage.");
533     }
534 
535     template<Usage kI>
FilterResult(const FilterResult<kI> & image)536     explicit FilterResult(const FilterResult<kI>& image)
537             : fImage(image.fImage)
538             , fOrigin(image.fOrigin) {
539         static_assert((kU != Usage::kInput) || (kI != Usage::kInput0 && kI != Usage::kInput1),
540                       "kInput0 and kInput1 cannot be copied to more generic kInput usage.");
541         static_assert((kU != Usage::kInput0 && kU != Usage::kInput1) ||
542                       (kI == kU || kI == Usage::kInput || kI == Usage::kOutput),
543                       "Can only copy to specific input from the generic kInput usage.");
544     }
545 
image()546     const SkSpecialImage* image() const { return fImage.get(); }
refImage()547     sk_sp<SkSpecialImage> refImage() const { return fImage; }
548 
549     // Get the layer-space bounds of the result. This will have the same dimensions as the
550     // image and its top left corner will be 'origin()'.
layerBounds()551     LayerSpace<SkIRect> layerBounds() const {
552         return LayerSpace<SkIRect>(SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(),
553                                                      fImage->width(), fImage->height()));
554     }
555 
556     // Get the layer-space coordinate of this image's top left pixel.
layerOrigin()557     const LayerSpace<SkIPoint>& layerOrigin() const { return fOrigin; }
558 
559     // Extract image and origin, safely when the image is null.
560     // TODO (michaelludwig) - This is intended for convenience until all call sites of
561     // SkImageFilter_Base::filterImage() have been updated to work in the new type system
562     // (which comes later as SkDevice, SkCanvas, etc. need to be modified, and coordinate space
563     // tagging needs to be added).
imageAndOffset(SkIPoint * offset)564     sk_sp<SkSpecialImage> imageAndOffset(SkIPoint* offset) const {
565         if (fImage) {
566             *offset = SkIPoint(fOrigin);
567             return fImage;
568         } else {
569             *offset = {0, 0};
570             return nullptr;
571         }
572     }
573 
574 private:
575     // Allow all FilterResult templates access to each others members
576     template<Usage kO>
577     friend class FilterResult;
578 
579     sk_sp<SkSpecialImage> fImage;
580     LayerSpace<SkIPoint> fOrigin;
581 };
582 
583 // The context contains all necessary information to describe how the image filter should be
584 // computed (i.e. the current layer matrix and clip), and the color information of the output of a
585 // filter DAG. For now, this is just the color space (of the original requesting device). This is
586 // used when constructing intermediate rendering surfaces, so that we ensure we land in a surface
587 // that's similar/compatible to the final consumer of the DAG's output.
588 class Context {
589 public:
590     SK_USE_FLUENT_IMAGE_FILTER_TYPES_IN_CLASS
591 
592     // Creates a context with the given layer matrix and destination clip, reading from 'source'
593     // with an origin of (0,0).
Context(const SkMatrix & layerMatrix,const SkIRect & clipBounds,SkImageFilterCache * cache,SkColorType colorType,SkColorSpace * colorSpace,const SkSpecialImage * source)594     Context(const SkMatrix& layerMatrix, const SkIRect& clipBounds, SkImageFilterCache* cache,
595             SkColorType colorType, SkColorSpace* colorSpace, const SkSpecialImage* source)
596         : fMapping(SkMatrix::I(), layerMatrix)
597         , fDesiredOutput(clipBounds)
598         , fCache(cache)
599         , fColorType(colorType)
600         , fColorSpace(colorSpace)
601         , fSource(sk_ref_sp(source), LayerSpace<SkIPoint>({0, 0})) {}
602 
Context(const Mapping & mapping,const LayerSpace<SkIRect> & desiredOutput,SkImageFilterCache * cache,SkColorType colorType,SkColorSpace * colorSpace,const FilterResult<For::kInput> & source)603     Context(const Mapping& mapping, const LayerSpace<SkIRect>& desiredOutput,
604             SkImageFilterCache* cache, SkColorType colorType, SkColorSpace* colorSpace,
605             const FilterResult<For::kInput>& source)
606         : fMapping(mapping)
607         , fDesiredOutput(desiredOutput)
608         , fCache(cache)
609         , fColorType(colorType)
610         , fColorSpace(colorSpace)
611         , fSource(source) {}
612 
613     // The mapping that defines the transformation from local parameter space of the filters to the
614     // layer space where the image filters are evaluated, as well as the remaining transformation
615     // from the layer space to the final device space. The layer space defined by the returned
616     // Mapping may be the same as the root device space, or be an intermediate space that is
617     // supported by the image filter DAG (depending on what it returns from canHandleComplexCTM()).
618     // If a node returns false from canHandleComplexCTM(), the layer matrix of the mapping will be
619     // at most a scale + translate, and the remaining matrix will be appropriately set to transform
620     // the layer space to the final device space (applied by the SkCanvas when filtering is
621     // finished).
mapping()622     const Mapping& mapping() const { return fMapping; }
623     // DEPRECATED: Use mapping() and its coordinate-space types instead
ctm()624     const SkMatrix& ctm() const { return fMapping.layerMatrix(); }
625     // The bounds, in the layer space, that the filtered image will be clipped to. The output
626     // from filterImage() must cover these clip bounds, except in areas where it will just be
627     // transparent black, in which case a smaller output image can be returned.
desiredOutput()628     const LayerSpace<SkIRect>& desiredOutput() const { return fDesiredOutput; }
629     // DEPRECATED: Use desiredOutput() instead
clipBounds()630     const SkIRect& clipBounds() const { return static_cast<const SkIRect&>(fDesiredOutput); }
631     // The cache to use when recursing through the filter DAG, in order to avoid repeated
632     // calculations of the same image.
cache()633     SkImageFilterCache* cache() const { return fCache; }
634     // The output device's color type, which can be used for intermediate images to be
635     // compatible with the eventual target of the filtered result.
colorType()636     SkColorType colorType() const { return fColorType; }
637 #if SK_SUPPORT_GPU
grColorType()638     GrColorType grColorType() const { return SkColorTypeToGrColorType(fColorType); }
639 #endif
640     // The output device's color space, so intermediate images can match, and so filtering can
641     // be performed in the destination color space.
colorSpace()642     SkColorSpace* colorSpace() const { return fColorSpace; }
refColorSpace()643     sk_sp<SkColorSpace> refColorSpace() const { return sk_ref_sp(fColorSpace); }
644     // The default surface properties to use when making transient surfaces during filtering.
surfaceProps()645     const SkSurfaceProps* surfaceProps() const { return &fSource.image()->props(); }
646 
647     // This is the image to use whenever an expected input filter has been set to null. In the
648     // majority of cases, this is the original source image for the image filter DAG so it comes
649     // from the SkDevice that holds either the saveLayer or the temporary rendered result. The
650     // exception is composing two image filters (via SkImageFilters::Compose), which must use
651     // the output of the inner DAG as the "source" for the outer DAG.
source()652     const FilterResult<For::kInput>& source() const { return fSource; }
653     // DEPRECATED: Use source() instead to get both the image and its origin.
sourceImage()654     const SkSpecialImage* sourceImage() const { return fSource.image(); }
655 
656     // True if image filtering should occur on the GPU if possible.
gpuBacked()657     bool gpuBacked() const { return fSource.image()->isTextureBacked(); }
658     // The recording context to use when computing the filter with the GPU.
getContext()659     GrRecordingContext* getContext() const { return fSource.image()->getContext(); }
660 
661     /**
662      *  Since a context can be built directly, its constructor has no chance to "return null" if
663      *  it's given invalid or unsupported inputs. Call this to know of the the context can be
664      *  used.
665      *
666      *  The SkImageFilterCache Key, for example, requires a finite ctm (no infinities or NaN),
667      *  so that test is part of isValid.
668      */
isValid()669     bool isValid() const { return fSource.image() != nullptr && fMapping.layerMatrix().isFinite(); }
670 
671     // Create a surface of the given size, that matches the context's color type and color space
672     // as closely as possible, and uses the same backend of the device that produced the source
673     // image.
674     sk_sp<SkSpecialSurface> makeSurface(const SkISize& size,
675                                         const SkSurfaceProps* props = nullptr) const {
676         return fSource.image()->makeSurface(fColorType, fColorSpace, size,
677                                             kPremul_SkAlphaType, props);
678     }
679 
680     // Create a new context that matches this context, but with an overridden layer space.
withNewMapping(const Mapping & mapping)681     Context withNewMapping(const Mapping& mapping) const {
682         return Context(mapping, fDesiredOutput, fCache, fColorType, fColorSpace, fSource);
683     }
684     // Create a new context that matches this context, but with an overridden desired output rect.
withNewDesiredOutput(const LayerSpace<SkIRect> & desiredOutput)685     Context withNewDesiredOutput(const LayerSpace<SkIRect>& desiredOutput) const {
686         return Context(fMapping, desiredOutput, fCache, fColorType, fColorSpace, fSource);
687     }
688 
689 private:
690     Mapping                   fMapping;
691     LayerSpace<SkIRect>       fDesiredOutput;
692     SkImageFilterCache*       fCache;
693     SkColorType               fColorType;
694     // The pointed-to object is owned by the device controlling the filter process, and our lifetime
695     // is bounded by the device, so this can be a bare pointer.
696     SkColorSpace*             fColorSpace;
697     FilterResult<For::kInput> fSource;
698 };
699 
700 } // end namespace skif
701 
702 #endif // SkImageFilterTypes_DEFINED
703