• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ganesh/effects/GrBicubicEffect.h"
9 
10 #include "src/core/SkMatrixPriv.h"
11 #include "src/gpu/KeyBuilder.h"
12 #include "src/gpu/ganesh/GrTexture.h"
13 #include "src/gpu/ganesh/effects/GrMatrixEffect.h"
14 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
17 #include "src/gpu/ganesh/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     SkCubicResampler 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("bicubicColor.a = saturate(bicubicColor.a);");
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,SkCubicResampler kernel,Direction direction)114 std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
115                                                            SkAlphaType alphaType,
116                                                            const SkMatrix& matrix,
117                                                            SkCubicResampler 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,SkCubicResampler 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                                                            SkCubicResampler 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,SkCubicResampler 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         SkCubicResampler 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,SkCubicResampler 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         SkCubicResampler 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,SkCubicResampler kernel,Direction direction)188 std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
189                                                            SkAlphaType alphaType,
190                                                            const SkMatrix& matrix,
191                                                            SkCubicResampler 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,SkCubicResampler kernel,Direction direction,Clamp clamp)198 GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp,
199                                  SkCubicResampler 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 
onAddToKey(const GrShaderCaps & caps,skgpu::KeyBuilder * b) const216 void GrBicubicEffect::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const {
217     uint32_t key = (static_cast<uint32_t>(fDirection) << 0) | (static_cast<uint32_t>(fClamp) << 2);
218     b->add32(key);
219 }
220 
onMakeProgramImpl() const221 std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrBicubicEffect::onMakeProgramImpl() const {
222     return std::make_unique<Impl>();
223 }
224 
onIsEqual(const GrFragmentProcessor & other) const225 bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const {
226     const auto& that = other.cast<GrBicubicEffect>();
227     return fDirection == that.fDirection &&
228            fClamp == that.fClamp         &&
229            fKernel.B == that.fKernel.B   &&
230            fKernel.C == that.fKernel.C;
231 }
232 
constantOutputForConstantInput(const SkPMColor4f & input) const233 SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const {
234     return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input);
235 }
236 
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect)237 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect)
238 
239 #if GR_TEST_UTILS
240 std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
241     Direction direction = Direction::kX;
242     switch (d->fRandom->nextULessThan(3)) {
243         case 0:
244             direction = Direction::kX;
245             break;
246         case 1:
247             direction = Direction::kY;
248             break;
249         case 2:
250             direction = Direction::kXY;
251             break;
252     }
253     auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::gMitchell
254                                          : GrBicubicEffect::gCatmullRom;
255     auto m = GrTest::TestMatrix(d->fRandom);
256     switch (d->fRandom->nextULessThan(3)) {
257         case 0: {
258             auto [view, ct, at] = d->randomView();
259             GrSamplerState::WrapMode wm[2];
260             GrTest::TestWrapModes(d->fRandom, wm);
261 
262             if (d->fRandom->nextBool()) {
263                 SkRect subset;
264                 subset.fLeft = d->fRandom->nextSScalar1() * view.width();
265                 subset.fTop = d->fRandom->nextSScalar1() * view.height();
266                 subset.fRight = d->fRandom->nextSScalar1() * view.width();
267                 subset.fBottom = d->fRandom->nextSScalar1() * view.height();
268                 subset.sort();
269                 return MakeSubset(std::move(view),
270                                   at,
271                                   m,
272                                   wm[0],
273                                   wm[1],
274                                   subset,
275                                   kernel,
276                                   direction,
277                                   *d->caps());
278             }
279             return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps());
280         }
281         case 1: {
282             auto [view, ct, at] = d->randomView();
283             return Make(std::move(view), at, m, kernel, direction);
284         }
285         default: {
286             SkAlphaType at;
287             do {
288                 at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1));
289             } while (at == kUnknown_SkAlphaType);
290             return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction);
291         }
292     }
293 }
294 #endif
295