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&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 29 | const GrEffectKey&, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 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 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 37 | static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
| 38 | GrEffectKeyBuilder* b) { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 39 | const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 40 | b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 41 | } |
| 42 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 43 | private: |
| 44 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 45 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 46 | UniformHandle fCoefficientsUni; |
| 47 | UniformHandle fImageIncrementUni; |
| 48 | GrTextureDomain::GLDomain fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 49 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 50 | typedef GrGLEffect INHERITED; |
| 51 | }; |
| 52 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 53 | GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 54 | : INHERITED(factory) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 58 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 59 | const GrEffectKey& key, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 60 | const char* outputColor, |
| 61 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 62 | const TransformedCoordsArray& coords, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 63 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 64 | const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 66 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 67 | fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 68 | kMat44f_GrSLType, "Coefficients"); |
| 69 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 70 | kVec2f_GrSLType, "ImageIncrement"); |
| 71 | |
| 72 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 73 | const char* coeff = builder->getUniformCStr(fCoefficientsUni); |
| 74 | |
| 75 | SkString cubicBlendName; |
| 76 | |
| 77 | static const GrGLShaderVar gCubicBlendArgs[] = { |
| 78 | GrGLShaderVar("coefficients", kMat44f_GrSLType), |
| 79 | GrGLShaderVar("t", kFloat_GrSLType), |
| 80 | GrGLShaderVar("c0", kVec4f_GrSLType), |
| 81 | GrGLShaderVar("c1", kVec4f_GrSLType), |
| 82 | GrGLShaderVar("c2", kVec4f_GrSLType), |
| 83 | GrGLShaderVar("c3", kVec4f_GrSLType), |
| 84 | }; |
| 85 | builder->fsEmitFunction(kVec4f_GrSLType, |
| 86 | "cubicBlend", |
| 87 | SK_ARRAY_COUNT(gCubicBlendArgs), |
| 88 | gCubicBlendArgs, |
| 89 | "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" |
| 90 | "\tvec4 c = coefficients * ts;\n" |
| 91 | "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", |
| 92 | &cubicBlendName); |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 93 | builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc); |
| 94 | // We unnormalize the coord in order to determine our fractional offset (f) within the texel |
| 95 | // We then snap coord to a texel center and renormalize. The snap prevents cases where the |
| 96 | // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/ |
| 97 | // double hit a texel. |
| 98 | builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc); |
| 99 | builder->fsCodeAppend("\tvec2 f = fract(coord);\n"); |
| 100 | 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] | 101 | builder->fsCodeAppend("\tvec4 rowColors[4];\n"); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 102 | for (int y = 0; y < 4; ++y) { |
| 103 | for (int x = 0; x < 4; ++x) { |
| 104 | SkString coord; |
| 105 | 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] | 106 | SkString sampleVar; |
| 107 | sampleVar.printf("rowColors[%d]", x); |
| 108 | fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 109 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 110 | 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] | 111 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 112 | SkString bicubicColor; |
| 113 | bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff); |
| 114 | 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] | 115 | } |
| 116 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 117 | void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, |
| 118 | const GrDrawEffect& drawEffect) { |
| 119 | const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 120 | const GrTexture& texture = *effect.texture(0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 121 | float imageIncrement[2]; |
| 122 | imageIncrement[0] = 1.0f / texture.width(); |
| 123 | imageIncrement[1] = 1.0f / texture.height(); |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 124 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 125 | uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 126 | fDomain.setData(uman, effect.domain(), texture.origin()); |
| 127 | } |
| 128 | |
| 129 | static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16], |
| 130 | const SkScalar src[16]) { |
| 131 | for (int y = 0; y < 4; y++) { |
| 132 | for (int x = 0; x < 4; x++) { |
| 133 | dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]); |
| 134 | } |
| 135 | } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 139 | const SkScalar coefficients[16], |
| 140 | const SkMatrix &matrix, |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 141 | const SkShader::TileMode tileModes[2]) |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 142 | : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode)) |
| 143 | , fDomain(GrTextureDomain::IgnoredDomain()) { |
| 144 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
| 145 | } |
| 146 | |
| 147 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
| 148 | const SkScalar coefficients[16], |
| 149 | const SkMatrix &matrix, |
| 150 | const SkRect& domain) |
| 151 | : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode, |
| 152 | GrTextureParams::kNone_FilterMode)) |
| 153 | , fDomain(domain, GrTextureDomain::kClamp_Mode) { |
| 154 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GrBicubicEffect::~GrBicubicEffect() { |
| 158 | } |
| 159 | |
| 160 | const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { |
| 161 | return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); |
| 162 | } |
| 163 | |
| 164 | bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { |
| 165 | const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase); |
humper@google.com | d1af237 | 2013-09-04 20:32:58 +0000 | [diff] [blame] | 166 | return this->textureAccess(0) == s.textureAccess(0) && |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 167 | !memcmp(fCoefficients, s.coefficients(), 16); |
| 168 | } |
| 169 | |
| 170 | void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 171 | // FIXME: Perhaps we can do better. |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 172 | *validFlags = 0; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | GR_DEFINE_EFFECT_TEST(GrBicubicEffect); |
| 177 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 178 | GrEffect* GrBicubicEffect::TestCreate(SkRandom* random, |
| 179 | GrContext* context, |
| 180 | const GrDrawTargetCaps&, |
| 181 | GrTexture* textures[]) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 182 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 183 | GrEffectUnitTest::kAlphaTextureIdx; |
| 184 | SkScalar coefficients[16]; |
| 185 | for (int i = 0; i < 16; i++) { |
| 186 | coefficients[i] = random->nextSScalar1(); |
| 187 | } |
| 188 | return GrBicubicEffect::Create(textures[texIdx], coefficients); |
| 189 | } |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 190 | |
| 191 | ////////////////////////////////////////////////////////////////////////////// |
| 192 | |
| 193 | bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, |
| 194 | GrTextureParams::FilterMode* filterMode) { |
| 195 | if (matrix.isIdentity()) { |
| 196 | *filterMode = GrTextureParams::kNone_FilterMode; |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | SkScalar scales[2]; |
| 201 | if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) { |
| 202 | // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped |
| 203 | // entirely, |
| 204 | *filterMode = GrTextureParams::kMipMap_FilterMode; |
| 205 | return false; |
| 206 | } |
| 207 | // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling. |
| 208 | if (scales[1] == SK_Scalar1) { |
| 209 | if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) && |
| 210 | SkScalarIsInt(matrix.getTranslateY())) { |
| 211 | *filterMode = GrTextureParams::kNone_FilterMode; |
| 212 | } else { |
| 213 | // Use bilerp to handle rotation or fractional translation. |
| 214 | *filterMode = GrTextureParams::kBilerp_FilterMode; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | // When we use the bicubic filtering effect each sample is read from the texture using |
| 219 | // nearest neighbor sampling. |
| 220 | *filterMode = GrTextureParams::kNone_FilterMode; |
| 221 | return true; |
| 222 | } |