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 8 #include "GrBicubicEffect.h" 9 10 #include "GrProxyMove.h" 11 #include "GrTexture.h" 12 #include "GrTextureProxy.h" 13 #include "glsl/GrGLSLColorSpaceXformHelper.h" 14 #include "glsl/GrGLSLFragmentShaderBuilder.h" 15 #include "glsl/GrGLSLProgramDataManager.h" 16 #include "glsl/GrGLSLUniformHandler.h" 17 #include "../private/GrGLSL.h" 18 19 class GrGLBicubicEffect : public GrGLSLFragmentProcessor { 20 public: 21 void emitCode(EmitArgs&) override; 22 23 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&, 24 GrProcessorKeyBuilder* b) { 25 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>(); 26 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain())); 27 b->add32(GrColorSpaceXform::XformKey(bicubicEffect.colorSpaceXform())); 28 } 29 30 protected: 31 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; 32 33 private: 34 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; 35 36 UniformHandle fImageIncrementUni; 37 GrGLSLColorSpaceXformHelper fColorSpaceHelper; 38 GrTextureDomain::GLDomain fDomain; 39 40 typedef GrGLSLFragmentProcessor INHERITED; 41 }; 42 43 void GrGLBicubicEffect::emitCode(EmitArgs& args) { 44 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>(); 45 46 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 47 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, 48 kVec2f_GrSLType, kDefault_GrSLPrecision, 49 "ImageIncrement"); 50 51 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); 52 53 fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform()); 54 55 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 56 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); 57 58 /* 59 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer 60 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988). 61 * ACM DL: http://dl.acm.org/citation.cfm?id=378514 62 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf 63 * 64 * The authors define a family of cubic filters with two free parameters (B and C): 65 * 66 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1 67 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2 68 * { 0 otherwise 69 * 70 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their 71 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the 72 * source of the specific weights below. 73 * 74 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation). 75 */ 76 fragBuilder->codeAppend("mat4 kMitchellCoefficients = mat4(" 77 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0," 78 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0," 79 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0," 80 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);"); 81 fragBuilder->codeAppendf("vec2 coord = %s - %s * vec2(0.5);", coords2D.c_str(), imgInc); 82 // We unnormalize the coord in order to determine our fractional offset (f) within the texel 83 // We then snap coord to a texel center and renormalize. The snap prevents cases where the 84 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/ 85 // double hit a texel. 86 fragBuilder->codeAppendf("coord /= %s;", imgInc); 87 fragBuilder->codeAppend("vec2 f = fract(coord);"); 88 fragBuilder->codeAppendf("coord = (coord - f + vec2(0.5)) * %s;", imgInc); 89 fragBuilder->codeAppend("vec4 wx = kMitchellCoefficients * vec4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);"); 90 fragBuilder->codeAppend("vec4 wy = kMitchellCoefficients * vec4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);"); 91 fragBuilder->codeAppend("vec4 rowColors[4];"); 92 for (int y = 0; y < 4; ++y) { 93 for (int x = 0; x < 4; ++x) { 94 SkString coord; 95 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); 96 SkString sampleVar; 97 sampleVar.printf("rowColors[%d]", x); 98 fDomain.sampleTexture(fragBuilder, 99 args.fUniformHandler, 100 args.fShaderCaps, 101 bicubicEffect.domain(), 102 sampleVar.c_str(), 103 coord, 104 args.fTexSamplers[0]); 105 } 106 fragBuilder->codeAppendf( 107 "vec4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + wx.w * rowColors[3];", 108 y); 109 } 110 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)"); 111 if (fColorSpaceHelper.isValid()) { 112 SkString xformedColor; 113 fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper); 114 bicubicColor.swap(xformedColor); 115 } 116 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputColor, bicubicColor.c_str(), 117 args.fInputColor); 118 } 119 120 void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman, 121 const GrFragmentProcessor& processor) { 122 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>(); 123 GrTexture* texture = processor.textureSampler(0).peekTexture(); 124 125 float imageIncrement[2]; 126 imageIncrement[0] = 1.0f / texture->width(); 127 imageIncrement[1] = 1.0f / texture->height(); 128 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); 129 fDomain.setData(pdman, bicubicEffect.domain(), texture); 130 if (SkToBool(bicubicEffect.colorSpaceXform())) { 131 fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform()); 132 } 133 } 134 135 GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, 136 sk_sp<GrColorSpaceXform> colorSpaceXform, 137 const SkMatrix &matrix, 138 const SkShader::TileMode tileModes[2]) 139 : INHERITED{ModulationFlags(proxy->config()), 140 GR_PROXY_MOVE(proxy), 141 std::move(colorSpaceXform), 142 matrix, 143 GrSamplerParams(tileModes, GrSamplerParams::kNone_FilterMode)} 144 , fDomain(GrTextureDomain::IgnoredDomain()) { 145 this->initClassID<GrBicubicEffect>(); 146 } 147 148 GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, 149 sk_sp<GrColorSpaceXform> colorSpaceXform, 150 const SkMatrix &matrix, 151 const SkRect& domain) 152 : INHERITED(ModulationFlags(proxy->config()), proxy, 153 std::move(colorSpaceXform), matrix, 154 GrSamplerParams(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode)) 155 , fDomain(proxy.get(), domain, GrTextureDomain::kClamp_Mode) { 156 this->initClassID<GrBicubicEffect>(); 157 } 158 159 GrBicubicEffect::~GrBicubicEffect() { 160 } 161 162 void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 163 GrProcessorKeyBuilder* b) const { 164 GrGLBicubicEffect::GenKey(*this, caps, b); 165 } 166 167 GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { 168 return new GrGLBicubicEffect; 169 } 170 171 bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const { 172 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>(); 173 return fDomain == s.fDomain; 174 } 175 176 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); 177 178 #if GR_TEST_UTILS 179 sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { 180 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx 181 : GrProcessorUnitTest::kAlphaTextureIdx; 182 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom); 183 static const SkShader::TileMode kClampClamp[] = 184 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; 185 return GrBicubicEffect::Make(d->textureProxy(texIdx), std::move(colorSpaceXform), 186 SkMatrix::I(), kClampClamp); 187 } 188 #endif 189 190 ////////////////////////////////////////////////////////////////////////////// 191 192 bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, 193 GrSamplerParams::FilterMode* filterMode) { 194 if (matrix.isIdentity()) { 195 *filterMode = GrSamplerParams::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 = GrSamplerParams::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 = GrSamplerParams::kNone_FilterMode; 211 } else { 212 // Use bilerp to handle rotation or fractional translation. 213 *filterMode = GrSamplerParams::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 = GrSamplerParams::kNone_FilterMode; 220 return true; 221 } 222