1 /*
2 * Copyright 2014 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/SkFlattenable.h"
9 #include "include/core/SkImageFilter.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSamplingOptions.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypes.h"
17 #include "include/effects/SkImageFilters.h"
18 #include "src/core/SkImageFilterTypes.h"
19 #include "src/core/SkImageFilter_Base.h"
20 #include "src/core/SkPicturePriv.h"
21 #include "src/core/SkReadBuffer.h"
22 #include "src/core/SkSamplingPriv.h"
23 #include "src/core/SkWriteBuffer.h"
24
25 #include <optional>
26 #include <utility>
27
28 struct SkISize;
29
30 namespace {
31
32 class SkMatrixTransformImageFilter final : public SkImageFilter_Base {
33 public:
34 // TODO(michaelludwig): Update this to use SkM44.
SkMatrixTransformImageFilter(const SkMatrix & transform,const SkSamplingOptions & sampling,sk_sp<SkImageFilter> input)35 SkMatrixTransformImageFilter(const SkMatrix& transform,
36 const SkSamplingOptions& sampling,
37 sk_sp<SkImageFilter> input)
38 : SkImageFilter_Base(&input, 1)
39 , fTransform(transform)
40 , fSampling(sampling) {
41 // Pre-cache so future calls to fTransform.getType() are threadsafe.
42 (void) static_cast<const SkMatrix&>(fTransform).getType();
43 }
44
45 SkRect computeFastBounds(const SkRect&) const override;
46
47 protected:
48 void flatten(SkWriteBuffer&) const override;
49
50 private:
51 friend void ::SkRegisterMatrixTransformImageFilterFlattenable();
52 SK_FLATTENABLE_HOOKS(SkMatrixTransformImageFilter)
53 static sk_sp<SkFlattenable> LegacyOffsetCreateProc(SkReadBuffer& buffer);
54
onGetCTMCapability() const55 MatrixCapability onGetCTMCapability() const override { return MatrixCapability::kComplex; }
56
57 skif::FilterResult onFilterImage(const skif::Context& context) const override;
58
59 skif::LayerSpace<SkIRect> onGetInputLayerBounds(
60 const skif::Mapping& mapping,
61 const skif::LayerSpace<SkIRect>& desiredOutput,
62 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override;
63
64 std::optional<skif::LayerSpace<SkIRect>> onGetOutputLayerBounds(
65 const skif::Mapping& mapping,
66 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override;
67
68 skif::LayerSpace<SkIRect> requiredInput(const skif::Mapping& mapping,
69 const skif::LayerSpace<SkIRect>& desiredOutput) const;
70
71 skif::ParameterSpace<SkMatrix> fTransform;
72 SkSamplingOptions fSampling;
73 };
74
75 } // namespace
76
MatrixTransform(const SkMatrix & transform,const SkSamplingOptions & sampling,sk_sp<SkImageFilter> input)77 sk_sp<SkImageFilter> SkImageFilters::MatrixTransform(const SkMatrix& transform,
78 const SkSamplingOptions& sampling,
79 sk_sp<SkImageFilter> input) {
80 return sk_sp<SkImageFilter>(new SkMatrixTransformImageFilter(transform,
81 sampling,
82 std::move(input)));
83 }
84
Offset(SkScalar dx,SkScalar dy,sk_sp<SkImageFilter> input,const CropRect & cropRect)85 sk_sp<SkImageFilter> SkImageFilters::Offset(SkScalar dx, SkScalar dy,
86 sk_sp<SkImageFilter> input,
87 const CropRect& cropRect) {
88 // The legacy ::Offset() implementation rounded its offset vector to layer-space pixels, which
89 // is roughly equivalent to using nearest-neighbor sampling with the translation matrix.
90 sk_sp<SkImageFilter> offset = SkImageFilters::MatrixTransform(
91 SkMatrix::Translate(dx, dy),
92 SkFilterMode::kNearest,
93 std::move(input));
94 // The legacy 'cropRect' applies only to the output of the offset filter.
95 if (cropRect) {
96 offset = SkImageFilters::Crop(*cropRect, std::move(offset));
97 }
98 return offset;
99 }
100
SkRegisterMatrixTransformImageFilterFlattenable()101 void SkRegisterMatrixTransformImageFilterFlattenable() {
102 SK_REGISTER_FLATTENABLE(SkMatrixTransformImageFilter);
103 // TODO(michaelludwig): Remove after grace period for SKPs to stop using old name
104 SkFlattenable::Register("SkMatrixImageFilter", SkMatrixTransformImageFilter::CreateProc);
105 // TODO(michaelludwig): Remove after grace period for SKPs to stop using old serialization
106 SkFlattenable::Register("SkOffsetImageFilter",
107 SkMatrixTransformImageFilter::LegacyOffsetCreateProc);
108 SkFlattenable::Register("SkOffsetImageFilterImpl",
109 SkMatrixTransformImageFilter::LegacyOffsetCreateProc);
110 }
111
LegacyOffsetCreateProc(SkReadBuffer & buffer)112 sk_sp<SkFlattenable> SkMatrixTransformImageFilter::LegacyOffsetCreateProc(SkReadBuffer& buffer) {
113 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
114 SkPoint offset;
115 buffer.readPoint(&offset);
116 return SkImageFilters::Offset(offset.x(), offset.y(), common.getInput(0), common.cropRect());
117 }
118
CreateProc(SkReadBuffer & buffer)119 sk_sp<SkFlattenable> SkMatrixTransformImageFilter::CreateProc(SkReadBuffer& buffer) {
120 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
121 SkMatrix matrix;
122 buffer.readMatrix(&matrix);
123
124 auto sampling = [&]() {
125 if (buffer.isVersionLT(SkPicturePriv::kMatrixImageFilterSampling_Version)) {
126 return SkSamplingPriv::FromFQ(buffer.read32LE(kLast_SkLegacyFQ), kLinear_SkMediumAs);
127 } else {
128 return buffer.readSampling();
129 }
130 }();
131 return SkImageFilters::MatrixTransform(matrix, sampling, common.getInput(0));
132 }
133
flatten(SkWriteBuffer & buffer) const134 void SkMatrixTransformImageFilter::flatten(SkWriteBuffer& buffer) const {
135 this->SkImageFilter_Base::flatten(buffer);
136 buffer.writeMatrix(SkMatrix(fTransform));
137 buffer.writeSampling(fSampling);
138 }
139
140 ///////////////////////////////////////////////////////////////////////////////////////////////////
141
onFilterImage(const skif::Context & context) const142 skif::FilterResult SkMatrixTransformImageFilter::onFilterImage(const skif::Context& context) const {
143 skif::LayerSpace<SkIRect> requiredInput =
144 this->requiredInput(context.mapping(), context.desiredOutput());
145 skif::FilterResult childOutput =
146 this->getChildOutput(0, context.withNewDesiredOutput(requiredInput));
147
148 skif::LayerSpace<SkMatrix> transform = context.mapping().paramToLayer(fTransform);
149 return childOutput.applyTransform(context, transform, fSampling);
150 }
151
computeFastBounds(const SkRect & src) const152 SkRect SkMatrixTransformImageFilter::computeFastBounds(const SkRect& src) const {
153 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
154 return static_cast<const SkMatrix&>(fTransform).mapRect(bounds);
155 }
156
requiredInput(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & desiredOutput) const157 skif::LayerSpace<SkIRect> SkMatrixTransformImageFilter::requiredInput(
158 const skif::Mapping& mapping,
159 const skif::LayerSpace<SkIRect>& desiredOutput) const {
160 // The required input for this filter to cover 'desiredOutput' is the smallest rectangle such
161 // that after being transformed by the layer-space adjusted 'fTransform', it contains the output
162 skif::LayerSpace<SkIRect> requiredInput;
163 if (!mapping.paramToLayer(fTransform).inverseMapRect(desiredOutput, &requiredInput)) {
164 return skif::LayerSpace<SkIRect>::Empty();
165 }
166
167 // Additionally if there is any filtering beyond nearest neighbor, we request an extra buffer of
168 // pixels so that the content is available to the bilerp/bicubic kernel.
169 if (fSampling != SkSamplingOptions()) {
170 requiredInput.outset(skif::LayerSpace<SkISize>({1, 1}));
171 }
172 return requiredInput;
173 }
174
175
onGetInputLayerBounds(const skif::Mapping & mapping,const skif::LayerSpace<SkIRect> & desiredOutput,std::optional<skif::LayerSpace<SkIRect>> contentBounds) const176 skif::LayerSpace<SkIRect> SkMatrixTransformImageFilter::onGetInputLayerBounds(
177 const skif::Mapping& mapping,
178 const skif::LayerSpace<SkIRect>& desiredOutput,
179 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const {
180 // Our required input is the desired output for our child image filter.
181 skif::LayerSpace<SkIRect> requiredInput = this->requiredInput(mapping, desiredOutput);
182 return this->getChildInputLayerBounds(0, mapping, requiredInput, contentBounds);
183 }
184
onGetOutputLayerBounds(const skif::Mapping & mapping,std::optional<skif::LayerSpace<SkIRect>> contentBounds) const185 std::optional<skif::LayerSpace<SkIRect>> SkMatrixTransformImageFilter::onGetOutputLayerBounds(
186 const skif::Mapping& mapping,
187 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const {
188 // The output of this filter is the transformed bounds of its child's output.
189 auto childOutput = this->getChildOutputLayerBounds(0, mapping, contentBounds);
190 if (childOutput) {
191 return mapping.paramToLayer(fTransform).mapRect(*childOutput);
192 } else {
193 return skif::LayerSpace<SkIRect>::Unbounded();
194 }
195 }
196