blob: db42f356698a6b09fd74fb510d09fa6f978024f9 [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
humper@google.com3aad3b02013-09-04 19:23:53 +00008#include "GrBicubicEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
brianosman54f30c12016-07-18 10:53:52 -070010#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLUniformHandler.h"
bsalomon848faf02014-07-11 10:01:02 -070014
humper@google.com3aad3b02013-09-04 19:23:53 +000015#define DS(x) SkDoubleToScalar(x)
16
17const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = {
18 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
19 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
20 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
21 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
22};
23
24
egdaniel64c47282015-11-13 06:54:19 -080025class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000026public:
robertphillips9cdb9922016-02-03 12:25:40 -080027 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000028
jvanverthcfc18862015-04-28 08:48:20 -070029 static inline void GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070030 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070031 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
32 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
33 b->add32(SkToInt(SkToBool(bicubicEffect.colorSpaceXform())));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000034 }
35
wangyixb1daa862015-08-18 11:29:31 -070036protected:
egdaniel018fb622015-10-28 07:26:40 -070037 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070038
humper@google.com3aad3b02013-09-04 19:23:53 +000039private:
egdaniel018fb622015-10-28 07:26:40 -070040 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000041
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000042 UniformHandle fCoefficientsUni;
43 UniformHandle fImageIncrementUni;
brianosman54f30c12016-07-18 10:53:52 -070044 UniformHandle fColorSpaceXformUni;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000045 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000046
egdaniel64c47282015-11-13 06:54:19 -080047 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000048};
49
wangyix7c157a92015-07-22 15:08:53 -070050void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070051 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000052
egdaniel7ea439b2015-12-03 09:20:44 -080053 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -080054 fCoefficientsUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080055 kMat44f_GrSLType, kDefault_GrSLPrecision,
56 "Coefficients");
cdalton5e58cee2016-02-11 12:49:47 -080057 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080058 kVec2f_GrSLType, kDefault_GrSLPrecision,
59 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000060
egdaniel7ea439b2015-12-03 09:20:44 -080061 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
62 const char* coeff = uniformHandler->getUniformCStr(fCoefficientsUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000063
brianosman54f30c12016-07-18 10:53:52 -070064 GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, bicubicEffect.colorSpaceXform(),
65 &fColorSpaceXformUni);
66
humper@google.com3aad3b02013-09-04 19:23:53 +000067 SkString cubicBlendName;
68
egdaniel0d3f0612015-10-21 10:45:48 -070069 static const GrGLSLShaderVar gCubicBlendArgs[] = {
70 GrGLSLShaderVar("coefficients", kMat44f_GrSLType),
71 GrGLSLShaderVar("t", kFloat_GrSLType),
72 GrGLSLShaderVar("c0", kVec4f_GrSLType),
73 GrGLSLShaderVar("c1", kVec4f_GrSLType),
74 GrGLSLShaderVar("c2", kVec4f_GrSLType),
75 GrGLSLShaderVar("c3", kVec4f_GrSLType),
humper@google.com3aad3b02013-09-04 19:23:53 +000076 };
cdalton85285412016-02-18 12:37:07 -080077 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080078 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
79 fragBuilder->emitFunction(kVec4f_GrSLType,
80 "cubicBlend",
81 SK_ARRAY_COUNT(gCubicBlendArgs),
82 gCubicBlendArgs,
83 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
84 "\tvec4 c = coefficients * ts;\n"
85 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
86 &cubicBlendName);
87 fragBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000088 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
89 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
90 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
91 // double hit a texel.
egdaniel4ca2e602015-11-18 08:01:26 -080092 fragBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
93 fragBuilder->codeAppend("\tvec2 f = fract(coord);\n");
94 fragBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
95 fragBuilder->codeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +000096 for (int y = 0; y < 4; ++y) {
97 for (int x = 0; x < 4; ++x) {
98 SkString coord;
99 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000100 SkString sampleVar;
101 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -0800102 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800103 args.fUniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -0800104 args.fGLSLCaps,
brianosman54f30c12016-07-18 10:53:52 -0700105 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -0800106 sampleVar.c_str(),
107 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700108 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000109 }
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf(
111 "\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n",
112 y, cubicBlendName.c_str(), coeff);
humper@google.com3aad3b02013-09-04 19:23:53 +0000113 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000114 SkString bicubicColor;
115 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
brianosman54f30c12016-07-18 10:53:52 -0700116 if (colorSpaceHelper.getXformMatrix()) {
117 bicubicColor.appendf(" * %s", colorSpaceHelper.getXformMatrix());
118 }
egdaniel4ca2e602015-11-18 08:01:26 -0800119 fragBuilder->codeAppendf("\t%s = %s;\n",
120 args.fOutputColor, (GrGLSLExpr4(bicubicColor.c_str()) *
121 GrGLSLExpr4(args.fInputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000122}
123
egdaniel018fb622015-10-28 07:26:40 -0700124void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
125 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700126 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
127 const GrTexture& texture = *processor.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000128 float imageIncrement[2];
129 imageIncrement[0] = 1.0f / texture.width();
130 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700131 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt49586be2014-09-16 08:21:41 -0700132 pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients());
133 fDomain.setData(pdman, bicubicEffect.domain(), texture.origin());
brianosman54f30c12016-07-18 10:53:52 -0700134 if (SkToBool(bicubicEffect.colorSpaceXform())) {
135 float xformMatrix[16];
136 bicubicEffect.colorSpaceXform()->srcToDst().asColMajorf(xformMatrix);
137 pdman.setMatrix4f(fColorSpaceXformUni, xformMatrix);
138 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000139}
140
141static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
142 const SkScalar src[16]) {
143 for (int y = 0; y < 4; y++) {
144 for (int x = 0; x < 4; x++) {
145 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
146 }
147 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000148}
149
bsalomon4a339522015-10-06 08:40:50 -0700150GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700151 sk_sp<GrColorSpaceXform> colorSpaceXform,
humper@google.com3aad3b02013-09-04 19:23:53 +0000152 const SkScalar coefficients[16],
153 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000154 const SkShader::TileMode tileModes[2])
brianosman54f30c12016-07-18 10:53:52 -0700155 : INHERITED(texture, nullptr, matrix,
156 GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
157 , fDomain(GrTextureDomain::IgnoredDomain())
158 , fColorSpaceXform(std::move(colorSpaceXform)) {
joshualitteb2a6762014-12-04 11:35:33 -0800159 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000160 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
161}
162
bsalomon4a339522015-10-06 08:40:50 -0700163GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700164 sk_sp<GrColorSpaceXform> colorSpaceXform,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000165 const SkScalar coefficients[16],
166 const SkMatrix &matrix,
167 const SkRect& domain)
brianosman54f30c12016-07-18 10:53:52 -0700168 : INHERITED(texture, nullptr, matrix,
joshualitt5f10b5c2015-07-09 10:24:35 -0700169 GrTextureParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode))
brianosman54f30c12016-07-18 10:53:52 -0700170 , fDomain(domain, GrTextureDomain::kClamp_Mode)
171 , fColorSpaceXform(std::move(colorSpaceXform)) {
joshualitteb2a6762014-12-04 11:35:33 -0800172 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000173 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000174}
175
176GrBicubicEffect::~GrBicubicEffect() {
177}
178
egdaniel57d3b032015-11-13 11:57:27 -0800179void GrBicubicEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
180 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800181 GrGLBicubicEffect::GenKey(*this, caps, b);
182}
183
egdaniel57d3b032015-11-13 11:57:27 -0800184GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800185 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000186}
187
bsalomon0e08fc12014-10-15 08:19:04 -0700188bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700189 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700190 return !memcmp(fCoefficients, s.coefficients(), 16) &&
bsalomon838f62d2014-08-05 07:15:57 -0700191 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000192}
193
egdaniel605dd0f2014-11-12 08:35:25 -0800194void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000195 // FIXME: Perhaps we can do better.
joshualitt56995b52014-12-11 15:44:02 -0800196 inout->mulByUnknownSingleComponent();
humper@google.com3aad3b02013-09-04 19:23:53 +0000197}
198
joshualittb0a8a372014-09-23 09:50:21 -0700199GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000200
bungeman06ca8ec2016-06-09 08:01:03 -0700201sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700202 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
203 GrProcessorUnitTest::kAlphaTextureIdx;
humper@google.com3aad3b02013-09-04 19:23:53 +0000204 SkScalar coefficients[16];
205 for (int i = 0; i < 16; i++) {
joshualitt0067ff52015-07-08 14:26:19 -0700206 coefficients[i] = d->fRandom->nextSScalar1();
humper@google.com3aad3b02013-09-04 19:23:53 +0000207 }
brianosman54f30c12016-07-18 10:53:52 -0700208 return GrBicubicEffect::Make(d->fTextures[texIdx], nullptr, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000209}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000210
211//////////////////////////////////////////////////////////////////////////////
212
213bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
214 GrTextureParams::FilterMode* filterMode) {
215 if (matrix.isIdentity()) {
216 *filterMode = GrTextureParams::kNone_FilterMode;
217 return false;
218 }
219
220 SkScalar scales[2];
221 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
222 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
223 // entirely,
224 *filterMode = GrTextureParams::kMipMap_FilterMode;
225 return false;
226 }
227 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
228 if (scales[1] == SK_Scalar1) {
229 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
230 SkScalarIsInt(matrix.getTranslateY())) {
231 *filterMode = GrTextureParams::kNone_FilterMode;
232 } else {
233 // Use bilerp to handle rotation or fractional translation.
234 *filterMode = GrTextureParams::kBilerp_FilterMode;
235 }
236 return false;
237 }
238 // When we use the bicubic filtering effect each sample is read from the texture using
239 // nearest neighbor sampling.
240 *filterMode = GrTextureParams::kNone_FilterMode;
241 return true;
242}