blob: 118efaf68c02b883d5252f8f39ed62eb5000be8d [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"
9
bsalomon848faf02014-07-11 10:01:02 -070010#include "gl/GrGLShaderBuilder.h"
11
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,
25 const GrDrawEffect&);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026
humper@google.com3aad3b02013-09-04 19:23:53 +000027 virtual void emitCode(GrGLShaderBuilder*,
28 const GrDrawEffect&,
29 EffectKey,
30 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
humper@google.com3aad3b02013-09-04 19:23:53 +000035 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
36
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000037 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
38 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
39 return GrTextureDomain::GLDomain::DomainKey(domain);
40 }
41
humper@google.com3aad3b02013-09-04 19:23:53 +000042private:
43 typedef GrGLUniformManager::UniformHandle UniformHandle;
44
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000045 UniformHandle fCoefficientsUni;
46 UniformHandle fImageIncrementUni;
47 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000048
humper@google.com3aad3b02013-09-04 19:23:53 +000049 typedef GrGLEffect INHERITED;
50};
51
bsalomon@google.com77af6802013-10-02 13:04:56 +000052GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
53 : INHERITED(factory) {
humper@google.com3aad3b02013-09-04 19:23:53 +000054}
55
56void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000057 const GrDrawEffect& drawEffect,
humper@google.com3aad3b02013-09-04 19:23:53 +000058 EffectKey key,
59 const char* outputColor,
60 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000061 const TransformedCoordsArray& coords,
humper@google.com3aad3b02013-09-04 19:23:53 +000062 const TextureSamplerArray& samplers) {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000063 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000064
bsalomon@google.com77af6802013-10-02 13:04:56 +000065 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
humper@google.com3aad3b02013-09-04 19:23:53 +000066 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
67 kMat44f_GrSLType, "Coefficients");
68 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
69 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 };
84 builder->fsEmitFunction(kVec4f_GrSLType,
85 "cubicBlend",
86 SK_ARRAY_COUNT(gCubicBlendArgs),
87 gCubicBlendArgs,
88 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
89 "\tvec4 c = coefficients * ts;\n"
90 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
91 &cubicBlendName);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000092 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
93 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
94 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
95 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
96 // double hit a texel.
97 builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc);
98 builder->fsCodeAppend("\tvec2 f = fract(coord);\n");
99 builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000100 builder->fsCodeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +0000101 for (int y = 0; y < 4; ++y) {
102 for (int x = 0; x < 4; ++x) {
103 SkString coord;
104 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000105 SkString sampleVar;
106 sampleVar.printf("rowColors[%d]", x);
107 fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000108 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000109 builder->fsCodeAppendf("\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 +0000110 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000111 SkString bicubicColor;
112 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
113 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000114}
115
humper@google.com3aad3b02013-09-04 19:23:53 +0000116void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
117 const GrDrawEffect& drawEffect) {
118 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000119 const GrTexture& texture = *effect.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000120 float imageIncrement[2];
121 imageIncrement[0] = 1.0f / texture.width();
122 imageIncrement[1] = 1.0f / texture.height();
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000123 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
humper@google.com3aad3b02013-09-04 19:23:53 +0000124 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000125 fDomain.setData(uman, effect.domain(), texture.origin());
126}
127
128static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
129 const SkScalar src[16]) {
130 for (int y = 0; y < 4; y++) {
131 for (int x = 0; x < 4; x++) {
132 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
133 }
134 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000135}
136
137GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000138 const SkScalar coefficients[16],
139 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000140 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000141 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
142 , fDomain(GrTextureDomain::IgnoredDomain()) {
143 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
144}
145
146GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
147 const SkScalar coefficients[16],
148 const SkMatrix &matrix,
149 const SkRect& domain)
150 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
151 GrTextureParams::kNone_FilterMode))
152 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
153 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000154}
155
156GrBicubicEffect::~GrBicubicEffect() {
157}
158
159const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
160 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
161}
162
163bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
164 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
humper@google.comd1af2372013-09-04 20:32:58 +0000165 return this->textureAccess(0) == s.textureAccess(0) &&
humper@google.com3aad3b02013-09-04 19:23:53 +0000166 !memcmp(fCoefficients, s.coefficients(), 16);
167}
168
169void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000170 // FIXME: Perhaps we can do better.
humper@google.com3aad3b02013-09-04 19:23:53 +0000171 *validFlags = 0;
172 return;
173}
174
175GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
176
bsalomon83d081a2014-07-08 09:56:10 -0700177GrEffect* GrBicubicEffect::TestCreate(SkRandom* random,
178 GrContext* context,
179 const GrDrawTargetCaps&,
180 GrTexture* textures[]) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000181 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
182 GrEffectUnitTest::kAlphaTextureIdx;
183 SkScalar coefficients[16];
184 for (int i = 0; i < 16; i++) {
185 coefficients[i] = random->nextSScalar1();
186 }
187 return GrBicubicEffect::Create(textures[texIdx], coefficients);
188}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000189
190//////////////////////////////////////////////////////////////////////////////
191
192bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
193 GrTextureParams::FilterMode* filterMode) {
194 if (matrix.isIdentity()) {
195 *filterMode = GrTextureParams::kNone_FilterMode;
196 return false;
197 }
198
199 SkScalar scales[2];
200 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
201 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
202 // entirely,
203 *filterMode = GrTextureParams::kMipMap_FilterMode;
204 return false;
205 }
206 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
207 if (scales[1] == SK_Scalar1) {
208 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
209 SkScalarIsInt(matrix.getTranslateY())) {
210 *filterMode = GrTextureParams::kNone_FilterMode;
211 } else {
212 // Use bilerp to handle rotation or fractional translation.
213 *filterMode = GrTextureParams::kBilerp_FilterMode;
214 }
215 return false;
216 }
217 // When we use the bicubic filtering effect each sample is read from the texture using
218 // nearest neighbor sampling.
219 *filterMode = GrTextureParams::kNone_FilterMode;
220 return true;
221}