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