blob: 5585ea026c253ca853fd96816ad07fe5134088ff [file] [log] [blame]
bsalomon848faf02014-07-11 10:01:02 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrBicubicEffect.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -04009
Brian Salomond0d033a2020-02-18 16:59:28 -050010#include "src/core/SkMatrixPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000011#include "src/gpu/GrTexture.h"
Michael Ludwigb5bd1b32020-06-26 10:23:23 -040012#include "src/gpu/effects/GrMatrixEffect.h"
Brian Salomond0d033a2020-02-18 16:59:28 -050013#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
16#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Brian Salomonf990b6a2020-07-13 10:44:22 -040017#include <cmath>
bsalomon848faf02014-07-11 10:01:02 -070018
Brian Salomon3176e862021-08-09 11:23:04 -040019class GrBicubicEffect::Impl : public GrFragmentProcessor::ProgramImpl {
humper@google.com3aad3b02013-09-04 19:23:53 +000020public:
Mike Reed3867c702020-09-01 13:28:10 -040021 Impl() : fKernel{-1, -1} {}
robertphillips9cdb9922016-02-03 12:25:40 -080022 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000023
Brian Salomon2e66ade2020-07-21 16:27:29 -040024protected:
25 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
26
humper@google.com3aad3b02013-09-04 19:23:53 +000027private:
Mike Reed3867c702020-09-01 13:28:10 -040028 SkImage::CubicResampler fKernel;
Brian Salomon2e66ade2020-07-21 16:27:29 -040029 UniformHandle fCoefficientUni;
Brian Salomon3176e862021-08-09 11:23:04 -040030 using INHERITED = ProgramImpl;
humper@google.com3aad3b02013-09-04 19:23:53 +000031};
32
Brian Salomond0d033a2020-02-18 16:59:28 -050033void GrBicubicEffect::Impl::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070034 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000035
cdalton85285412016-02-18 12:37:07 -080036 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osmana3fa2ef2017-01-06 16:13:06 +000037
Brian Salomon2e66ade2020-07-21 16:27:29 -040038 const char* coeffs;
39 fCoefficientUni = args.fUniformHandler->addUniform(&args.fFp, kFragment_GrShaderFlag,
40 kHalf4x4_GrSLType, "coefficients", &coeffs);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040041 // We determine our fractional offset (f) within the texel. We then snap coord to a texel
42 // center. The snap prevents cases where the starting coords are near a texel boundary and
43 // offsets with imperfect precision would cause us to skip/double hit a texel.
44 // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is
45 // assumed the child processor represents something akin to a nearest neighbor sampled texture.
Brian Salomond0d033a2020-02-18 16:59:28 -050046 if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) {
Michael Ludwige88320b2020-06-24 09:04:56 -040047 fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);", args.fSampleCoord);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040048 fragBuilder->codeAppend("half2 f = half2(fract(coord));");
49 fragBuilder->codeAppend("coord += 0.5 - f;");
Brian Salomon2e66ade2020-07-21 16:27:29 -040050 fragBuilder->codeAppendf("half4 wx = %s * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);",
51 coeffs);
52 fragBuilder->codeAppendf("half4 wy = %s * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);",
53 coeffs);
Brian Salomona86fc7a2019-05-28 20:42:58 -040054 fragBuilder->codeAppend("half4 rowColors[4];");
55 for (int y = 0; y < 4; ++y) {
56 for (int x = 0; x < 4; ++x) {
57 SkString coord;
Brian Salomon5c4c61e2020-03-25 14:26:01 -040058 coord.printf("coord + float2(%d, %d)", x - 1, y - 1);
Brian Salomond0d033a2020-02-18 16:59:28 -050059 auto childStr =
60 this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size()));
61 fragBuilder->codeAppendf("rowColors[%d] = %s;", x, childStr.c_str());
Brian Salomona86fc7a2019-05-28 20:42:58 -040062 }
63 fragBuilder->codeAppendf(
64 "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + "
65 "wx.w * rowColors[3];",
66 y);
67 }
68 fragBuilder->codeAppend(
69 "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;");
70 } else {
Brian Salomon5c4c61e2020-03-25 14:26:01 -040071 const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y";
Michael Ludwige88320b2020-06-24 09:04:56 -040072 fragBuilder->codeAppendf("float coord = %s.%s - 0.5;", args.fSampleCoord, d);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040073 fragBuilder->codeAppend("half f = half(fract(coord));");
74 fragBuilder->codeAppend("coord += 0.5 - f;");
75 fragBuilder->codeAppend("half f2 = f * f;");
Brian Salomon2e66ade2020-07-21 16:27:29 -040076 fragBuilder->codeAppendf("half4 w = %s * half4(1.0, f, f2, f2 * f);", coeffs);
Brian Salomona86fc7a2019-05-28 20:42:58 -040077 fragBuilder->codeAppend("half4 c[4];");
78 for (int i = 0; i < 4; ++i) {
humper@google.com3aad3b02013-09-04 19:23:53 +000079 SkString coord;
Brian Salomon5c4c61e2020-03-25 14:26:01 -040080 if (bicubicEffect.fDirection == Direction::kX) {
Michael Ludwige88320b2020-06-24 09:04:56 -040081 coord.printf("float2(coord + %d, %s.y)", i - 1, args.fSampleCoord);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040082 } else {
Michael Ludwige88320b2020-06-24 09:04:56 -040083 coord.printf("float2(%s.x, coord + %d)", args.fSampleCoord, i - 1);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040084 }
Brian Salomond0d033a2020-02-18 16:59:28 -050085 auto childStr = this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size()));
86 fragBuilder->codeAppendf("c[%d] = %s;", i, childStr.c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +000087 }
Brian Salomona86fc7a2019-05-28 20:42:58 -040088 fragBuilder->codeAppend(
89 "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;");
humper@google.com3aad3b02013-09-04 19:23:53 +000090 }
Brian Osmane22dba82019-03-13 10:22:28 -040091 // Bicubic can send colors out of range, so clamp to get them back in (source) gamut.
92 // The kind of clamp we have to do depends on the alpha type.
Brian Salomond0d033a2020-02-18 16:59:28 -050093 switch (bicubicEffect.fClamp) {
94 case Clamp::kUnpremul:
95 fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);");
96 break;
97 case Clamp::kPremul:
98 fragBuilder->codeAppend(
99 "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));");
100 break;
Brian Osmane22dba82019-03-13 10:22:28 -0400101 }
John Stiles4bdc1212020-12-14 18:04:13 -0500102 fragBuilder->codeAppendf("return bicubicColor;");
humper@google.com3aad3b02013-09-04 19:23:53 +0000103}
104
Mike Reed3867c702020-09-01 13:28:10 -0400105#include "src/shaders/SkImageShader.h"
106
Brian Salomon2e66ade2020-07-21 16:27:29 -0400107void GrBicubicEffect::Impl::onSetData(const GrGLSLProgramDataManager& pdm,
108 const GrFragmentProcessor& fp) {
109 auto& bicubicEffect = fp.cast<GrBicubicEffect>();
Brian Salomon2e66ade2020-07-21 16:27:29 -0400110
Mike Reed3867c702020-09-01 13:28:10 -0400111 if (fKernel.B != bicubicEffect.fKernel.B || fKernel.C != bicubicEffect.fKernel.C) {
112 fKernel = bicubicEffect.fKernel;
113 pdm.setSkM44(fCoefficientUni, SkImageShader::CubicResamplerMatrix(fKernel.B, fKernel.C));
Brian Salomon2e66ade2020-07-21 16:27:29 -0400114 }
115}
116
Brian Salomond0d033a2020-02-18 16:59:28 -0500117std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
118 SkAlphaType alphaType,
119 const SkMatrix& matrix,
Mike Reed3867c702020-09-01 13:28:10 -0400120 SkImage::CubicResampler kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500121 Direction direction) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400122 auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I());
Brian Salomond0d033a2020-02-18 16:59:28 -0500123 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400124 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
125 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500126}
127
128std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
129 SkAlphaType alphaType,
130 const SkMatrix& matrix,
131 const GrSamplerState::WrapMode wrapX,
132 const GrSamplerState::WrapMode wrapY,
Mike Reed3867c702020-09-01 13:28:10 -0400133 SkImage::CubicResampler kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500134 Direction direction,
135 const GrCaps& caps) {
136 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
137 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400138 fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps);
Brian Salomond0d033a2020-02-18 16:59:28 -0500139 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400140 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
141 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500142}
143
144std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset(
145 GrSurfaceProxyView view,
146 SkAlphaType alphaType,
147 const SkMatrix& matrix,
148 const GrSamplerState::WrapMode wrapX,
149 const GrSamplerState::WrapMode wrapY,
150 const SkRect& subset,
Mike Reed3867c702020-09-01 13:28:10 -0400151 SkImage::CubicResampler kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500152 Direction direction,
153 const GrCaps& caps) {
154 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
155 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400156 fp = GrTextureEffect::MakeSubset(
157 std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps);
Brian Salomond0d033a2020-02-18 16:59:28 -0500158 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400159 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
160 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400161}
162
Brian Salomonf990b6a2020-07-13 10:44:22 -0400163std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset(
164 GrSurfaceProxyView view,
165 SkAlphaType alphaType,
166 const SkMatrix& matrix,
167 const GrSamplerState::WrapMode wrapX,
168 const GrSamplerState::WrapMode wrapY,
169 const SkRect& subset,
170 const SkRect& domain,
Mike Reed3867c702020-09-01 13:28:10 -0400171 SkImage::CubicResampler kernel,
Brian Salomonf990b6a2020-07-13 10:44:22 -0400172 Direction direction,
173 const GrCaps& caps) {
174 auto lowerBound = [](float x) { return std::floor(x - 1.5f) + 0.5f; };
175 auto upperBound = [](float x) { return std::floor(x + 1.5f) - 0.5f; };
176 SkRect expandedDomain {
177 lowerBound(domain.fLeft) ,
178 upperBound(domain.fRight) ,
179 lowerBound(domain.fTop) ,
180 upperBound(domain.fBottom)
181 };
182 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
183 std::unique_ptr<GrFragmentProcessor> fp;
184 fp = GrTextureEffect::MakeSubset(
185 std::move(view), alphaType, SkMatrix::I(), sampler, subset, expandedDomain, caps);
186 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
187 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
188 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
189}
190
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400191std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
192 SkAlphaType alphaType,
193 const SkMatrix& matrix,
Mike Reed3867c702020-09-01 13:28:10 -0400194 SkImage::CubicResampler kernel,
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400195 Direction direction) {
196 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400197 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
198 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500199}
200
201GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp,
Mike Reed3867c702020-09-01 13:28:10 -0400202 SkImage::CubicResampler kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500203 Direction direction,
204 Clamp clamp)
John Stilesde240ac2020-08-07 17:52:42 +0000205 : INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get()))
Brian Salomon1af72d12020-06-25 10:47:26 -0400206 , fKernel(kernel)
Brian Salomond0d033a2020-02-18 16:59:28 -0500207 , fDirection(direction)
208 , fClamp(clamp) {
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400209 this->setUsesSampleCoordsDirectly();
Brian Osman1298bc42020-06-30 13:39:35 -0400210 this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit());
Robert Phillips40fd7c92017-01-30 08:06:27 -0500211}
212
Brian Salomon3f6f9652017-07-28 07:34:05 -0400213GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
John Stiles307f8f52021-08-09 15:36:59 -0400214 : INHERITED(that)
Brian Salomon1af72d12020-06-25 10:47:26 -0400215 , fKernel(that.fKernel)
Brian Salomond0d033a2020-02-18 16:59:28 -0500216 , fDirection(that.fDirection)
John Stiles307f8f52021-08-09 15:36:59 -0400217 , fClamp(that.fClamp) {}
humper@google.com3aad3b02013-09-04 19:23:53 +0000218
Brian Salomon13b28732021-08-06 15:33:58 -0400219void GrBicubicEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
Brian Salomon2e66ade2020-07-21 16:27:29 -0400220 uint32_t key = (static_cast<uint32_t>(fDirection) << 0) | (static_cast<uint32_t>(fClamp) << 2);
Brian Salomond0d033a2020-02-18 16:59:28 -0500221 b->add32(key);
joshualitteb2a6762014-12-04 11:35:33 -0800222}
223
Brian Salomon3176e862021-08-09 11:23:04 -0400224std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrBicubicEffect::onMakeProgramImpl() const {
Brian Salomon18ab2032021-02-23 10:07:05 -0500225 return std::make_unique<Impl>();
226}
humper@google.com3aad3b02013-09-04 19:23:53 +0000227
Brian Salomond0d033a2020-02-18 16:59:28 -0500228bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const {
229 const auto& that = other.cast<GrBicubicEffect>();
Brian Salomonc3622f42020-12-21 14:41:48 -0500230 return fDirection == that.fDirection &&
231 fClamp == that.fClamp &&
232 fKernel.B == that.fKernel.B &&
233 fKernel.C == that.fKernel.C;
humper@google.com3aad3b02013-09-04 19:23:53 +0000234}
235
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400236SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const {
237 return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input);
238}
239
joshualittb0a8a372014-09-23 09:50:21 -0700240GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000241
Hal Canary6f6961e2017-01-31 13:50:44 -0500242#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400243std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Brian Salomona86fc7a2019-05-28 20:42:58 -0400244 Direction direction = Direction::kX;
245 switch (d->fRandom->nextULessThan(3)) {
246 case 0:
247 direction = Direction::kX;
248 break;
249 case 1:
250 direction = Direction::kY;
251 break;
252 case 2:
253 direction = Direction::kXY;
254 break;
255 }
Mike Reed3867c702020-09-01 13:28:10 -0400256 auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::gMitchell
257 : GrBicubicEffect::gCatmullRom;
Brian Salomond0d033a2020-02-18 16:59:28 -0500258 auto m = GrTest::TestMatrix(d->fRandom);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400259 switch (d->fRandom->nextULessThan(3)) {
260 case 0: {
261 auto [view, ct, at] = d->randomView();
262 GrSamplerState::WrapMode wm[2];
263 GrTest::TestWrapModes(d->fRandom, wm);
Greg Danielba51d682020-02-05 13:12:13 -0500264
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400265 if (d->fRandom->nextBool()) {
266 SkRect subset;
267 subset.fLeft = d->fRandom->nextSScalar1() * view.width();
268 subset.fTop = d->fRandom->nextSScalar1() * view.height();
269 subset.fRight = d->fRandom->nextSScalar1() * view.width();
270 subset.fBottom = d->fRandom->nextSScalar1() * view.height();
271 subset.sort();
Brian Salomon1af72d12020-06-25 10:47:26 -0400272 return MakeSubset(std::move(view),
273 at,
274 m,
275 wm[0],
276 wm[1],
277 subset,
278 kernel,
279 direction,
280 *d->caps());
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400281 }
Brian Salomon1af72d12020-06-25 10:47:26 -0400282 return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps());
Brian Salomond0d033a2020-02-18 16:59:28 -0500283 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400284 case 1: {
285 auto [view, ct, at] = d->randomView();
Brian Salomon1af72d12020-06-25 10:47:26 -0400286 return Make(std::move(view), at, m, kernel, direction);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400287 }
288 default: {
289 SkAlphaType at;
290 do {
291 at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1));
Brian Osman609f1592020-07-01 15:14:39 -0400292 } while (at == kUnknown_SkAlphaType);
293 return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400294 }
Brian Salomond0d033a2020-02-18 16:59:28 -0500295 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000296}
Hal Canary6f6961e2017-01-31 13:50:44 -0500297#endif