bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame^] | 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 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 8 | #include "GrBicubicEffect.h" |
| 9 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame^] | 10 | #include "gl/GrGLShaderBuilder.h" |
| 11 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 12 | #define DS(x) SkDoubleToScalar(x) |
| 13 | |
| 14 | const 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 | |
| 22 | class GrGLBicubicEffect : public GrGLEffect { |
| 23 | public: |
| 24 | GrGLBicubicEffect(const GrBackendEffectFactory& factory, |
| 25 | const GrDrawEffect&); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 26 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 27 | virtual void emitCode(GrGLShaderBuilder*, |
| 28 | const GrDrawEffect&, |
| 29 | EffectKey, |
| 30 | const char* outputColor, |
| 31 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 32 | const TransformedCoordsArray&, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 33 | const TextureSamplerArray&) SK_OVERRIDE; |
| 34 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 35 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 36 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 37 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 42 | private: |
| 43 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 44 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 45 | UniformHandle fCoefficientsUni; |
| 46 | UniformHandle fImageIncrementUni; |
| 47 | GrTextureDomain::GLDomain fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 48 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 49 | typedef GrGLEffect INHERITED; |
| 50 | }; |
| 51 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 52 | GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 53 | : INHERITED(factory) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 57 | const GrDrawEffect& drawEffect, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 58 | EffectKey key, |
| 59 | const char* outputColor, |
| 60 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 61 | const TransformedCoordsArray& coords, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 62 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 63 | const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 65 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 66 | 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.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 92 | 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.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 100 | builder->fsCodeAppend("\tvec4 rowColors[4];\n"); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 101 | 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.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 105 | SkString sampleVar; |
| 106 | sampleVar.printf("rowColors[%d]", x); |
| 107 | fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 109 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 110 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 111 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 114 | } |
| 115 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 116 | void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, |
| 117 | const GrDrawEffect& drawEffect) { |
| 118 | const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 119 | const GrTexture& texture = *effect.texture(0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 120 | float imageIncrement[2]; |
| 121 | imageIncrement[0] = 1.0f / texture.width(); |
| 122 | imageIncrement[1] = 1.0f / texture.height(); |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 123 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 124 | uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 125 | fDomain.setData(uman, effect.domain(), texture.origin()); |
| 126 | } |
| 127 | |
| 128 | static 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 138 | const SkScalar coefficients[16], |
| 139 | const SkMatrix &matrix, |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 140 | const SkShader::TileMode tileModes[2]) |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 141 | : 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 | |
| 146 | GrBicubicEffect::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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | GrBicubicEffect::~GrBicubicEffect() { |
| 157 | } |
| 158 | |
| 159 | const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { |
| 160 | return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); |
| 161 | } |
| 162 | |
| 163 | bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { |
| 164 | const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase); |
humper@google.com | d1af237 | 2013-09-04 20:32:58 +0000 | [diff] [blame] | 165 | return this->textureAccess(0) == s.textureAccess(0) && |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 166 | !memcmp(fCoefficients, s.coefficients(), 16); |
| 167 | } |
| 168 | |
| 169 | void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 170 | // FIXME: Perhaps we can do better. |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 171 | *validFlags = 0; |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | GR_DEFINE_EFFECT_TEST(GrBicubicEffect); |
| 176 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 177 | GrEffect* GrBicubicEffect::TestCreate(SkRandom* random, |
| 178 | GrContext* context, |
| 179 | const GrDrawTargetCaps&, |
| 180 | GrTexture* textures[]) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 181 | 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.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 189 | |
| 190 | ////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | bool 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 | } |