Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrBicubicEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 Google Inc.
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
9
12#include "include/core/SkRect.h"
16#include "src/base/SkRandom.h"
18#include "src/gpu/KeyBuilder.h"
27#include "src/sksl/SkSLString.h"
28
29#include <cmath>
30#include <cstdint>
31#include <string>
32#include <utility>
33
35public:
36 void emitCode(EmitArgs&) override;
37
38private:
39 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
40
41 SkCubicResampler fKernel = {-1, -1};
42 UniformHandle fCoefficientUni;
43};
44
46 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
47
48 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
49
50 const char* coeffs;
51 fCoefficientUni = args.fUniformHandler->addUniform(&args.fFp, kFragment_GrShaderFlag,
52 SkSLType::kHalf4x4, "coefficients", &coeffs);
53 // We determine our fractional offset (f) within the texel. We then snap coord to a texel
54 // center. The snap prevents cases where the starting coords are near a texel boundary and
55 // offsets with imperfect precision would cause us to skip/double hit a texel.
56 // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is
57 // assumed the child processor represents something akin to a nearest neighbor sampled texture.
58 if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) {
59 fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);", args.fSampleCoord);
60 fragBuilder->codeAppend("half2 f = half2(fract(coord));");
61 fragBuilder->codeAppend("coord += 0.5 - f;");
62 fragBuilder->codeAppendf("half4 wx = %s * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);",
63 coeffs);
64 fragBuilder->codeAppendf("half4 wy = %s * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);",
65 coeffs);
66 fragBuilder->codeAppend("half4 rowColors[4];");
67 for (int y = 0; y < 4; ++y) {
68 for (int x = 0; x < 4; ++x) {
69 auto coord = SkSL::String::printf("coord + float2(%d, %d)", x - 1, y - 1);
70 auto childStr = this->invokeChild(0, args, coord);
71 fragBuilder->codeAppendf("rowColors[%d] = %s;", x, childStr.c_str());
72 }
73 fragBuilder->codeAppendf(
74 "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + "
75 "wx.w * rowColors[3];",
76 y);
77 }
78 fragBuilder->codeAppend(
79 "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;");
80 } else {
81 const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y";
82 fragBuilder->codeAppendf("float coord = %s.%s - 0.5;", args.fSampleCoord, d);
83 fragBuilder->codeAppend("half f = half(fract(coord));");
84 fragBuilder->codeAppend("coord += 0.5 - f;");
85 fragBuilder->codeAppend("half f2 = f * f;");
86 fragBuilder->codeAppendf("half4 w = %s * half4(1.0, f, f2, f2 * f);", coeffs);
87 fragBuilder->codeAppend("half4 c[4];");
88 for (int i = 0; i < 4; ++i) {
89 std::string coord;
90 if (bicubicEffect.fDirection == Direction::kX) {
91 coord = SkSL::String::printf("float2(coord + %d, %s.y)", i - 1, args.fSampleCoord);
92 } else {
93 coord = SkSL::String::printf("float2(%s.x, coord + %d)", args.fSampleCoord, i - 1);
94 }
95 auto childStr = this->invokeChild(0, args, coord);
96 fragBuilder->codeAppendf("c[%d] = %s;", i, childStr.c_str());
97 }
98 fragBuilder->codeAppend(
99 "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;");
100 }
101 // Bicubic can send colors out of range, so clamp to get them back in (source) gamut.
102 // The kind of clamp we have to do depends on the alpha type.
103 switch (bicubicEffect.fClamp) {
104 case Clamp::kUnpremul:
105 fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);");
106 break;
107 case Clamp::kPremul:
108 fragBuilder->codeAppend("bicubicColor.a = saturate(bicubicColor.a);");
109 fragBuilder->codeAppend(
110 "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));");
111 break;
112 }
113 fragBuilder->codeAppendf("return bicubicColor;");
114}
115
117 const GrFragmentProcessor& fp) {
118 auto& bicubicEffect = fp.cast<GrBicubicEffect>();
119
120 if (fKernel.B != bicubicEffect.fKernel.B || fKernel.C != bicubicEffect.fKernel.C) {
121 fKernel = bicubicEffect.fKernel;
122 pdm.setSkM44(fCoefficientUni, SkImageShader::CubicResamplerMatrix(fKernel.B, fKernel.C));
123 }
124}
125
126std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
127 SkAlphaType alphaType,
128 const SkMatrix& matrix,
129 SkCubicResampler kernel,
130 Direction direction) {
131 auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I());
132 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
133 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
134 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
135}
136
137std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
138 SkAlphaType alphaType,
139 const SkMatrix& matrix,
140 const GrSamplerState::WrapMode wrapX,
141 const GrSamplerState::WrapMode wrapY,
142 SkCubicResampler kernel,
143 Direction direction,
144 const GrCaps& caps) {
145 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
146 std::unique_ptr<GrFragmentProcessor> fp;
147 fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps);
148 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
149 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
150 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
151}
152
153std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset(
155 SkAlphaType alphaType,
156 const SkMatrix& matrix,
157 const GrSamplerState::WrapMode wrapX,
158 const GrSamplerState::WrapMode wrapY,
159 const SkRect& subset,
160 SkCubicResampler kernel,
161 Direction direction,
162 const GrCaps& caps) {
163 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
164 std::unique_ptr<GrFragmentProcessor> fp;
166 std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps);
167 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
168 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
169 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
170}
171
172std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset(
174 SkAlphaType alphaType,
175 const SkMatrix& matrix,
176 const GrSamplerState::WrapMode wrapX,
177 const GrSamplerState::WrapMode wrapY,
178 const SkRect& subset,
179 const SkRect& domain,
180 SkCubicResampler kernel,
181 Direction direction,
182 const GrCaps& caps) {
183 auto lowerBound = [](float x) { return std::floor(x - 1.5f) + 0.5f; };
184 auto upperBound = [](float x) { return std::floor(x + 1.5f) - 0.5f; };
185 SkRect expandedDomain {
186 lowerBound(domain.fLeft) ,
187 upperBound(domain.fRight) ,
188 lowerBound(domain.fTop) ,
189 upperBound(domain.fBottom)
190 };
191 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
192 std::unique_ptr<GrFragmentProcessor> fp;
194 std::move(view), alphaType, SkMatrix::I(), sampler, subset, expandedDomain, caps);
195 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
196 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
197 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
198}
199
200std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
201 SkAlphaType alphaType,
202 const SkMatrix& matrix,
203 SkCubicResampler kernel,
204 Direction direction) {
205 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
206 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
207 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
208}
209
210GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp,
211 SkCubicResampler kernel,
212 Direction direction,
213 Clamp clamp)
215 , fKernel(kernel)
216 , fDirection(direction)
217 , fClamp(clamp) {
219 this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit());
220}
221
222GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
223 : INHERITED(that)
224 , fKernel(that.fKernel)
225 , fDirection(that.fDirection)
226 , fClamp(that.fClamp) {}
227
229 uint32_t key = (static_cast<uint32_t>(fDirection) << 0) | (static_cast<uint32_t>(fClamp) << 2);
230 b->add32(key);
231}
232
233std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrBicubicEffect::onMakeProgramImpl() const {
234 return std::make_unique<Impl>();
235}
236
238 const auto& that = other.cast<GrBicubicEffect>();
239 return fDirection == that.fDirection &&
240 fClamp == that.fClamp &&
241 fKernel.B == that.fKernel.B &&
242 fKernel.C == that.fKernel.C;
243}
244
248
250
251#if defined(GR_TEST_UTILS)
252std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
253 Direction direction = Direction::kX;
254 switch (d->fRandom->nextULessThan(3)) {
255 case 0:
256 direction = Direction::kX;
257 break;
258 case 1:
259 direction = Direction::kY;
260 break;
261 case 2:
262 direction = Direction::kXY;
263 break;
264 }
265 auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::gMitchell
267 auto m = GrTest::TestMatrix(d->fRandom);
268 switch (d->fRandom->nextULessThan(3)) {
269 case 0: {
270 auto [view, ct, at] = d->randomView();
272 GrTest::TestWrapModes(d->fRandom, wm);
273
274 if (d->fRandom->nextBool()) {
275 SkRect subset;
276 subset.fLeft = d->fRandom->nextSScalar1() * view.width();
277 subset.fTop = d->fRandom->nextSScalar1() * view.height();
278 subset.fRight = d->fRandom->nextSScalar1() * view.width();
279 subset.fBottom = d->fRandom->nextSScalar1() * view.height();
280 subset.sort();
281 return MakeSubset(std::move(view),
282 at,
283 m,
284 wm[0],
285 wm[1],
286 subset,
287 kernel,
288 direction,
289 *d->caps());
290 }
291 return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps());
292 }
293 case 1: {
294 auto [view, ct, at] = d->randomView();
295 return Make(std::move(view), at, m, kernel, direction);
296 }
297 default: {
298 SkAlphaType at;
299 do {
300 at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1));
301 } while (at == kUnknown_SkAlphaType);
302 return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction);
303 }
304 }
305}
306#endif
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
@ kFragment_GrShaderFlag
SkAlphaType
Definition SkAlphaType.h:26
@ kUnknown_SkAlphaType
uninitialized
Definition SkAlphaType.h:27
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kLastEnum_SkAlphaType
last valid value
Definition SkAlphaType.h:31
static unsigned clamp(SkFixed fx, int max)
#define INHERITED(method,...)
void onSetData(const GrGLSLProgramDataManager &, const GrFragmentProcessor &) override
void emitCode(EmitArgs &) override
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView view, SkAlphaType, const SkMatrix &, SkCubicResampler, Direction)
void onAddToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const override
static constexpr SkCubicResampler gCatmullRom
static std::unique_ptr< GrFragmentProcessor > MakeSubset(GrSurfaceProxyView view, SkAlphaType, const SkMatrix &, const GrSamplerState::WrapMode wrapX, const GrSamplerState::WrapMode wrapY, const SkRect &subset, SkCubicResampler, Direction, const GrCaps &)
bool onIsEqual(const GrFragmentProcessor &) const override
static constexpr SkCubicResampler gMitchell
std::unique_ptr< ProgramImpl > onMakeProgramImpl() const override
SkPMColor4f constantOutputForConstantInput(const SkPMColor4f &) const override
GrGLSLUniformHandler::UniformHandle UniformHandle
SkString invokeChild(int childIndex, EmitArgs &parentArgs, std::string_view skslCoords={})
static OptimizationFlags ProcessorOptimizationFlags(const GrFragmentProcessor *fp)
GrFragmentProcessor * childProcessor(int index)
void registerChild(std::unique_ptr< GrFragmentProcessor > child, SkSL::SampleUsage sampleUsage=SkSL::SampleUsage::PassThrough())
static SkPMColor4f ConstantOutputForConstantInput(const GrFragmentProcessor *fp, const SkPMColor4f &input)
void setSkM44(UniformHandle, const SkM44 &) const
static std::unique_ptr< GrFragmentProcessor > Make(const SkMatrix &matrix, std::unique_ptr< GrFragmentProcessor > child)
const T & cast() const
@ kGrBicubicEffect_ClassID
Definition GrProcessor.h:49
static std::unique_ptr< GrFragmentProcessor > MakeSubset(GrSurfaceProxyView, SkAlphaType, const SkMatrix &, GrSamplerState, const SkRect &subset, const GrCaps &caps, const float border[4]=kDefaultBorder, bool alwaysUseShaderTileMode=false)
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
static SkM44 CubicResamplerMatrix(float B, float C)
static const SkMatrix & I()
static SampleUsage Explicit()
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
double y
double x
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
void sort()
Definition SkRect.h:1313
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15