| 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 |  | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 9 | #include "GrBicubicEffect.h" | 
|  | 10 |  | 
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 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 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | class GrGLBicubicEffect : public GrGLFragmentProcessor { | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 23 | public: | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 24 | GrGLBicubicEffect(const GrBackendProcessorFactory& factory, | 
|  | 25 | const GrProcessor&); | 
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 26 |  | 
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 27 | virtual void emitCode(GrGLFPBuilder*, | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | const GrFragmentProcessor&, | 
|  | 29 | const GrProcessorKey&, | 
| 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 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 35 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 36 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 37 | static inline void GenKey(const GrProcessor& effect, const GrGLCaps&, | 
|  | 38 | GrProcessorKeyBuilder* b) { | 
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 39 | const GrTextureDomain& domain = effect.cast<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: | 
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 44 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 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 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 50 | typedef GrGLFragmentProcessor INHERITED; | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 51 | }; | 
|  | 52 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendProcessorFactory& factory, const GrProcessor&) | 
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 54 | : INHERITED(factory) { | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 57 | void GrGLBicubicEffect::emitCode(GrGLFPBuilder* builder, | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | const GrFragmentProcessor& effect, | 
|  | 59 | const GrProcessorKey& 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) { | 
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 64 | const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain(); | 
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 65 |  | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 66 | fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 67 | kMat44f_GrSLType, "Coefficients"); | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 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 | }; | 
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 84 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 85 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); | 
|  | 86 | fsBuilder->emitFunction(kVec4f_GrSLType, | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 87 | "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); | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 94 | fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc); | 
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 95 | // 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. | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 99 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 103 | 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.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 107 | SkString sampleVar; | 
|  | 108 | sampleVar.printf("rowColors[%d]", x); | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 109 | fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]); | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 110 | } | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 111 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 112 | } | 
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 113 | SkString bicubicColor; | 
|  | 114 | bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff); | 
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 115 | fsBuilder->codeAppendf("\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] | 116 | } | 
|  | 117 |  | 
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 118 | void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman, | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 119 | const GrProcessor& processor) { | 
|  | 120 | const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>(); | 
|  | 121 | const GrTexture& texture = *processor.texture(0); | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 122 | float imageIncrement[2]; | 
|  | 123 | imageIncrement[0] = 1.0f / texture.width(); | 
|  | 124 | imageIncrement[1] = 1.0f / texture.height(); | 
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 125 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 126 | pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients()); | 
|  | 127 | fDomain.setData(pdman, bicubicEffect.domain(), texture.origin()); | 
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | static 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
|  | 139 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 140 | const SkScalar coefficients[16], | 
|  | 141 | const SkMatrix &matrix, | 
| commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 142 | const SkShader::TileMode tileModes[2]) | 
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 143 | : 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 |  | 
|  | 148 | GrBicubicEffect::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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | GrBicubicEffect::~GrBicubicEffect() { | 
|  | 159 | } | 
|  | 160 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 161 | const GrBackendFragmentProcessorFactory& GrBicubicEffect::getFactory() const { | 
|  | 162 | return GrTBackendFragmentProcessorFactory<GrBicubicEffect>::getInstance(); | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame^] | 165 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const { | 
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 166 | const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>(); | 
| humper@google.com | d1af237 | 2013-09-04 20:32:58 +0000 | [diff] [blame] | 167 | return this->textureAccess(0) == s.textureAccess(0) && | 
| bsalomon | 838f62d | 2014-08-05 07:15:57 -0700 | [diff] [blame] | 168 | !memcmp(fCoefficients, s.coefficients(), 16) && | 
|  | 169 | fDomain == s.fDomain; | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 172 | void GrBicubicEffect::onComputeInvariantOutput(InvariantOutput* inout) const { | 
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 173 | // FIXME: Perhaps we can do better. | 
| egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 174 | inout->mulByUnknownAlpha(); | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 178 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 179 | GrFragmentProcessor* GrBicubicEffect::TestCreate(SkRandom* random, | 
|  | 180 | GrContext* context, | 
|  | 181 | const GrDrawTargetCaps&, | 
|  | 182 | GrTexture* textures[]) { | 
|  | 183 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : | 
|  | 184 | GrProcessorUnitTest::kAlphaTextureIdx; | 
| humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 185 | SkScalar coefficients[16]; | 
|  | 186 | for (int i = 0; i < 16; i++) { | 
|  | 187 | coefficients[i] = random->nextSScalar1(); | 
|  | 188 | } | 
|  | 189 | return GrBicubicEffect::Create(textures[texIdx], coefficients); | 
|  | 190 | } | 
| commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 191 |  | 
|  | 192 | ////////////////////////////////////////////////////////////////////////////// | 
|  | 193 |  | 
|  | 194 | bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, | 
|  | 195 | GrTextureParams::FilterMode* filterMode) { | 
|  | 196 | if (matrix.isIdentity()) { | 
|  | 197 | *filterMode = GrTextureParams::kNone_FilterMode; | 
|  | 198 | return false; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | SkScalar scales[2]; | 
|  | 202 | if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) { | 
|  | 203 | // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped | 
|  | 204 | // entirely, | 
|  | 205 | *filterMode = GrTextureParams::kMipMap_FilterMode; | 
|  | 206 | return false; | 
|  | 207 | } | 
|  | 208 | // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling. | 
|  | 209 | if (scales[1] == SK_Scalar1) { | 
|  | 210 | if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) && | 
|  | 211 | SkScalarIsInt(matrix.getTranslateY())) { | 
|  | 212 | *filterMode = GrTextureParams::kNone_FilterMode; | 
|  | 213 | } else { | 
|  | 214 | // Use bilerp to handle rotation or fractional translation. | 
|  | 215 | *filterMode = GrTextureParams::kBilerp_FilterMode; | 
|  | 216 | } | 
|  | 217 | return false; | 
|  | 218 | } | 
|  | 219 | // When we use the bicubic filtering effect each sample is read from the texture using | 
|  | 220 | // nearest neighbor sampling. | 
|  | 221 | *filterMode = GrTextureParams::kNone_FilterMode; | 
|  | 222 | return true; | 
|  | 223 | } |