blob: 27d482ae2e407206421d9689750a070e57d780f1 [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
humper@google.com3aad3b02013-09-04 19:23:53 +00009#include "GrBicubicEffect.h"
10
bsalomon848faf02014-07-11 10:01:02 -070011
humper@google.com3aad3b02013-09-04 19:23:53 +000012#define DS(x) SkDoubleToScalar(x)
13
14const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = {
15 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
16 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
17 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
18 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
19};
20
21
22class GrGLBicubicEffect : public GrGLEffect {
23public:
24 GrGLBicubicEffect(const GrBackendEffectFactory& factory,
joshualitt08da4f22014-09-16 07:17:28 -070025 const GrDrawEffect&);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026
joshualitt30ba4362014-08-21 20:18:45 -070027 virtual void emitCode(GrGLProgramBuilder*,
joshualitt08da4f22014-09-16 07:17:28 -070028 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -070029 const GrEffectKey&,
humper@google.com3aad3b02013-09-04 19:23:53 +000030 const char* outputColor,
31 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000032 const TransformedCoordsArray&,
humper@google.com3aad3b02013-09-04 19:23:53 +000033 const TextureSamplerArray&) SK_OVERRIDE;
34
joshualitt08da4f22014-09-16 07:17:28 -070035 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
humper@google.com3aad3b02013-09-04 19:23:53 +000036
joshualitt08da4f22014-09-16 07:17:28 -070037 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -070038 GrEffectKeyBuilder* b) {
joshualitt08da4f22014-09-16 07:17:28 -070039 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
bsalomon63e99f72014-07-21 08:03:14 -070040 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000041 }
42
humper@google.com3aad3b02013-09-04 19:23:53 +000043private:
kkinnunen7510b222014-07-30 00:04:16 -070044 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000045
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000046 UniformHandle fCoefficientsUni;
47 UniformHandle fImageIncrementUni;
48 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000049
humper@google.com3aad3b02013-09-04 19:23:53 +000050 typedef GrGLEffect INHERITED;
51};
52
joshualitt08da4f22014-09-16 07:17:28 -070053GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
bsalomon@google.com77af6802013-10-02 13:04:56 +000054 : INHERITED(factory) {
humper@google.com3aad3b02013-09-04 19:23:53 +000055}
56
joshualitt30ba4362014-08-21 20:18:45 -070057void GrGLBicubicEffect::emitCode(GrGLProgramBuilder* builder,
joshualitt08da4f22014-09-16 07:17:28 -070058 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -070059 const GrEffectKey& key,
humper@google.com3aad3b02013-09-04 19:23:53 +000060 const char* outputColor,
61 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000062 const TransformedCoordsArray& coords,
humper@google.com3aad3b02013-09-04 19:23:53 +000063 const TextureSamplerArray& samplers) {
joshualitt08da4f22014-09-16 07:17:28 -070064 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000065
joshualitt30ba4362014-08-21 20:18:45 -070066 fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000067 kMat44f_GrSLType, "Coefficients");
joshualitt30ba4362014-08-21 20:18:45 -070068 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000069 kVec2f_GrSLType, "ImageIncrement");
70
71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
72 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
73
74 SkString cubicBlendName;
75
76 static const GrGLShaderVar gCubicBlendArgs[] = {
77 GrGLShaderVar("coefficients", kMat44f_GrSLType),
78 GrGLShaderVar("t", kFloat_GrSLType),
79 GrGLShaderVar("c0", kVec4f_GrSLType),
80 GrGLShaderVar("c1", kVec4f_GrSLType),
81 GrGLShaderVar("c2", kVec4f_GrSLType),
82 GrGLShaderVar("c3", kVec4f_GrSLType),
83 };
joshualitt30ba4362014-08-21 20:18:45 -070084 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
85 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
86 fsBuilder->emitFunction(kVec4f_GrSLType,
humper@google.com3aad3b02013-09-04 19:23:53 +000087 "cubicBlend",
88 SK_ARRAY_COUNT(gCubicBlendArgs),
89 gCubicBlendArgs,
90 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
91 "\tvec4 c = coefficients * ts;\n"
92 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
93 &cubicBlendName);
joshualitt30ba4362014-08-21 20:18:45 -070094 fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000095 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
96 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
97 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
98 // double hit a texel.
joshualitt30ba4362014-08-21 20:18:45 -070099 fsBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
100 fsBuilder->codeAppend("\tvec2 f = fract(coord);\n");
101 fsBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
102 fsBuilder->codeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 for (int y = 0; y < 4; ++y) {
104 for (int x = 0; x < 4; ++x) {
105 SkString coord;
106 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000107 SkString sampleVar;
108 sampleVar.printf("rowColors[%d]", x);
joshualitt30ba4362014-08-21 20:18:45 -0700109 fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000110 }
joshualitt30ba4362014-08-21 20:18:45 -0700111 fsBuilder->codeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff);
humper@google.com3aad3b02013-09-04 19:23:53 +0000112 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000113 SkString bicubicColor;
114 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
joshualitt30ba4362014-08-21 20:18:45 -0700115 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000116}
117
kkinnunen7510b222014-07-30 00:04:16 -0700118void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt08da4f22014-09-16 07:17:28 -0700119 const GrDrawEffect& drawEffect) {
120 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000121 const GrTexture& texture = *effect.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000122 float imageIncrement[2];
123 imageIncrement[0] = 1.0f / texture.width();
124 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700125 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt08da4f22014-09-16 07:17:28 -0700126 pdman.setMatrix4f(fCoefficientsUni, effect.coefficients());
127 fDomain.setData(pdman, effect.domain(), texture.origin());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000128}
129
130static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
131 const SkScalar src[16]) {
132 for (int y = 0; y < 4; y++) {
133 for (int x = 0; x < 4; x++) {
134 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
135 }
136 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000137}
138
139GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000140 const SkScalar coefficients[16],
141 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000142 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000143 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
144 , fDomain(GrTextureDomain::IgnoredDomain()) {
145 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
146}
147
148GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
149 const SkScalar coefficients[16],
150 const SkMatrix &matrix,
151 const SkRect& domain)
152 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
153 GrTextureParams::kNone_FilterMode))
154 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
155 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000156}
157
158GrBicubicEffect::~GrBicubicEffect() {
159}
160
161const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
162 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
163}
164
165bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
joshualitt08da4f22014-09-16 07:17:28 -0700166 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
humper@google.comd1af2372013-09-04 20:32:58 +0000167 return this->textureAccess(0) == s.textureAccess(0) &&
bsalomon838f62d2014-08-05 07:15:57 -0700168 !memcmp(fCoefficients, s.coefficients(), 16) &&
169 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000170}
171
172void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000173 // FIXME: Perhaps we can do better.
humper@google.com3aad3b02013-09-04 19:23:53 +0000174 *validFlags = 0;
175 return;
176}
177
178GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
179
bsalomon83d081a2014-07-08 09:56:10 -0700180GrEffect* GrBicubicEffect::TestCreate(SkRandom* random,
181 GrContext* context,
182 const GrDrawTargetCaps&,
183 GrTexture* textures[]) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000184 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
185 GrEffectUnitTest::kAlphaTextureIdx;
186 SkScalar coefficients[16];
187 for (int i = 0; i < 16; i++) {
188 coefficients[i] = random->nextSScalar1();
189 }
190 return GrBicubicEffect::Create(textures[texIdx], coefficients);
191}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000192
193//////////////////////////////////////////////////////////////////////////////
194
195bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
196 GrTextureParams::FilterMode* filterMode) {
197 if (matrix.isIdentity()) {
198 *filterMode = GrTextureParams::kNone_FilterMode;
199 return false;
200 }
201
202 SkScalar scales[2];
203 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
204 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
205 // entirely,
206 *filterMode = GrTextureParams::kMipMap_FilterMode;
207 return false;
208 }
209 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
210 if (scales[1] == SK_Scalar1) {
211 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
212 SkScalarIsInt(matrix.getTranslateY())) {
213 *filterMode = GrTextureParams::kNone_FilterMode;
214 } else {
215 // Use bilerp to handle rotation or fractional translation.
216 *filterMode = GrTextureParams::kBilerp_FilterMode;
217 }
218 return false;
219 }
220 // When we use the bicubic filtering effect each sample is read from the texture using
221 // nearest neighbor sampling.
222 *filterMode = GrTextureParams::kNone_FilterMode;
223 return true;
224}