GrBicubicEffect.cpp revision 467022b1861033d968195687da15270c208279ff
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 19class GrGLBicubicEffect : public GrGLSLFragmentProcessor { 20public: 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 30protected: 31 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; 32 33private: 34 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; 35 36 UniformHandle fImageIncrementUni; 37 GrGLSLColorSpaceXformHelper fColorSpaceHelper; 38 GrTextureDomain::GLDomain fDomain; 39 40 typedef GrGLSLFragmentProcessor INHERITED; 41}; 42 43void 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 120void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman, 121 const GrFragmentProcessor& processor) { 122 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>(); 123 GrSurfaceProxy* proxy = processor.textureSampler(0).proxy(); 124 GrTexture* texture = proxy->priv().peekTexture(); 125 126 float imageIncrement[2]; 127 imageIncrement[0] = 1.0f / texture->width(); 128 imageIncrement[1] = 1.0f / texture->height(); 129 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); 130 fDomain.setData(pdman, bicubicEffect.domain(), proxy); 131 if (SkToBool(bicubicEffect.colorSpaceXform())) { 132 fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform()); 133 } 134} 135 136GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, 137 sk_sp<GrColorSpaceXform> colorSpaceXform, 138 const SkMatrix &matrix, 139 const SkShader::TileMode tileModes[2]) 140 : INHERITED{ModulationFlags(proxy->config()), 141 GR_PROXY_MOVE(proxy), 142 std::move(colorSpaceXform), 143 matrix, 144 GrSamplerParams(tileModes, GrSamplerParams::kNone_FilterMode)} 145 , fDomain(GrTextureDomain::IgnoredDomain()) { 146 this->initClassID<GrBicubicEffect>(); 147} 148 149GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, 150 sk_sp<GrColorSpaceXform> colorSpaceXform, 151 const SkMatrix &matrix, 152 const SkRect& domain) 153 : INHERITED(ModulationFlags(proxy->config()), proxy, 154 std::move(colorSpaceXform), matrix, 155 GrSamplerParams(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode)) 156 , fDomain(proxy.get(), domain, GrTextureDomain::kClamp_Mode) { 157 this->initClassID<GrBicubicEffect>(); 158} 159 160GrBicubicEffect::~GrBicubicEffect() { 161} 162 163void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 164 GrProcessorKeyBuilder* b) const { 165 GrGLBicubicEffect::GenKey(*this, caps, b); 166} 167 168GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { 169 return new GrGLBicubicEffect; 170} 171 172bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const { 173 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>(); 174 return fDomain == s.fDomain; 175} 176 177GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); 178 179#if GR_TEST_UTILS 180sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { 181 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx 182 : GrProcessorUnitTest::kAlphaTextureIdx; 183 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom); 184 static const SkShader::TileMode kClampClamp[] = 185 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; 186 return GrBicubicEffect::Make(d->textureProxy(texIdx), std::move(colorSpaceXform), 187 SkMatrix::I(), kClampClamp); 188} 189#endif 190 191////////////////////////////////////////////////////////////////////////////// 192 193bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, 194 GrSamplerParams::FilterMode* filterMode) { 195 if (matrix.isIdentity()) { 196 *filterMode = GrSamplerParams::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 = GrSamplerParams::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 = GrSamplerParams::kNone_FilterMode; 212 } else { 213 // Use bilerp to handle rotation or fractional translation. 214 *filterMode = GrSamplerParams::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 = GrSamplerParams::kNone_FilterMode; 221 return true; 222} 223