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