GrBicubicEffect.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include "src/gpu/effects/GrBicubicEffect.h"
  8. #include "include/gpu/GrTexture.h"
  9. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  10. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  11. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  12. class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
  13. public:
  14. void emitCode(EmitArgs&) override;
  15. static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
  16. GrProcessorKeyBuilder* b) {
  17. const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
  18. b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
  19. uint32_t bidir = bicubicEffect.direction() == GrBicubicEffect::Direction::kXY ? 1 : 0;
  20. b->add32(bidir | (bicubicEffect.alphaType() << 1));
  21. }
  22. protected:
  23. void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
  24. private:
  25. typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
  26. UniformHandle fDimensions;
  27. GrTextureDomain::GLDomain fDomain;
  28. typedef GrGLSLFragmentProcessor INHERITED;
  29. };
  30. void GrGLBicubicEffect::emitCode(EmitArgs& args) {
  31. const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
  32. GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
  33. fDimensions = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "Dimensions");
  34. const char* dims = uniformHandler->getUniformCStr(fDimensions);
  35. GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
  36. SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
  37. /*
  38. * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
  39. * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
  40. * ACM DL: http://dl.acm.org/citation.cfm?id=378514
  41. * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
  42. *
  43. * The authors define a family of cubic filters with two free parameters (B and C):
  44. *
  45. * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
  46. * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
  47. * { 0 otherwise
  48. *
  49. * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
  50. * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
  51. * source of the specific weights below.
  52. *
  53. * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
  54. */
  55. fragBuilder->codeAppend("half4x4 kMitchellCoefficients = half4x4("
  56. " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
  57. "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
  58. "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
  59. "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
  60. fragBuilder->codeAppendf("float2 coord = %s - %s.xy * float2(0.5);", coords2D.c_str(), dims);
  61. // We unnormalize the coord in order to determine our fractional offset (f) within the texel
  62. // We then snap coord to a texel center and renormalize. The snap prevents cases where the
  63. // starting coords are near a texel boundary and accumulations of dims would cause us to skip/
  64. // double hit a texel.
  65. fragBuilder->codeAppendf("half2 f = half2(fract(coord * %s.zw));", dims);
  66. fragBuilder->codeAppendf("coord = coord + (half2(0.5) - f) * %s.xy;", dims);
  67. if (bicubicEffect.direction() == GrBicubicEffect::Direction::kXY) {
  68. fragBuilder->codeAppend(
  69. "half4 wx = kMitchellCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
  70. fragBuilder->codeAppend(
  71. "half4 wy = kMitchellCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
  72. fragBuilder->codeAppend("half4 rowColors[4];");
  73. for (int y = 0; y < 4; ++y) {
  74. for (int x = 0; x < 4; ++x) {
  75. SkString coord;
  76. coord.printf("coord + %s.xy * float2(%d, %d)", dims, x - 1, y - 1);
  77. SkString sampleVar;
  78. sampleVar.printf("rowColors[%d]", x);
  79. fDomain.sampleTexture(fragBuilder,
  80. args.fUniformHandler,
  81. args.fShaderCaps,
  82. bicubicEffect.domain(),
  83. sampleVar.c_str(),
  84. coord,
  85. args.fTexSamplers[0]);
  86. }
  87. fragBuilder->codeAppendf(
  88. "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + "
  89. "wx.w * rowColors[3];",
  90. y);
  91. }
  92. fragBuilder->codeAppend(
  93. "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;");
  94. } else {
  95. // One of the dims.xy values will be zero. So v here selects the nonzero value of f.
  96. fragBuilder->codeAppend("half v = f.x + f.y;");
  97. fragBuilder->codeAppend("half v2 = v * v;");
  98. fragBuilder->codeAppend("half4 w = kMitchellCoefficients * half4(1.0, v, v2, v2 * v);");
  99. fragBuilder->codeAppend("half4 c[4];");
  100. for (int i = 0; i < 4; ++i) {
  101. SkString coord;
  102. coord.printf("coord + %s.xy * half(%d)", dims, i - 1);
  103. SkString samplerVar;
  104. samplerVar.printf("c[%d]", i);
  105. // With added complexity we could apply the domain once in X or Y depending on
  106. // direction rather than for each of the four lookups, but then we might not be
  107. // be able to share code for Direction::kX and ::kY.
  108. fDomain.sampleTexture(fragBuilder,
  109. args.fUniformHandler,
  110. args.fShaderCaps,
  111. bicubicEffect.domain(),
  112. samplerVar.c_str(),
  113. coord,
  114. args.fTexSamplers[0]);
  115. }
  116. fragBuilder->codeAppend(
  117. "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;");
  118. }
  119. // Bicubic can send colors out of range, so clamp to get them back in (source) gamut.
  120. // The kind of clamp we have to do depends on the alpha type.
  121. if (kPremul_SkAlphaType == bicubicEffect.alphaType()) {
  122. fragBuilder->codeAppend("bicubicColor.a = saturate(bicubicColor.a);");
  123. fragBuilder->codeAppend(
  124. "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));");
  125. } else {
  126. fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);");
  127. }
  128. fragBuilder->codeAppendf("%s = bicubicColor * %s;", args.fOutputColor, args.fInputColor);
  129. }
  130. void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
  131. const GrFragmentProcessor& processor) {
  132. const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
  133. GrTextureProxy* proxy = processor.textureSampler(0).proxy();
  134. GrTexture* texture = proxy->peekTexture();
  135. float dims[4] = {0, 0, 0, 0};
  136. if (bicubicEffect.direction() != GrBicubicEffect::Direction::kY) {
  137. dims[0] = 1.0f / texture->width();
  138. dims[2] = texture->width();
  139. }
  140. if (bicubicEffect.direction() != GrBicubicEffect::Direction::kX) {
  141. dims[1] = 1.0f / texture->height();
  142. dims[3] = texture->height();
  143. }
  144. pdman.set4fv(fDimensions, 1, dims);
  145. fDomain.setData(pdman, bicubicEffect.domain(), proxy,
  146. processor.textureSampler(0).samplerState());
  147. }
  148. GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, const SkMatrix& matrix,
  149. const SkRect& domain, const GrSamplerState::WrapMode wrapModes[2],
  150. GrTextureDomain::Mode modeX, GrTextureDomain::Mode modeY,
  151. Direction direction, SkAlphaType alphaType)
  152. : INHERITED{kGrBicubicEffect_ClassID,
  153. ModulateForSamplerOptFlags(
  154. proxy->config(),
  155. GrTextureDomain::IsDecalSampled(wrapModes, modeX, modeY))}
  156. , fCoordTransform(matrix, proxy.get())
  157. , fDomain(proxy.get(), domain, modeX, modeY)
  158. , fTextureSampler(std::move(proxy),
  159. GrSamplerState(wrapModes, GrSamplerState::Filter::kNearest))
  160. , fAlphaType(alphaType)
  161. , fDirection(direction) {
  162. this->addCoordTransform(&fCoordTransform);
  163. this->setTextureSamplerCnt(1);
  164. }
  165. GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
  166. : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags())
  167. , fCoordTransform(that.fCoordTransform)
  168. , fDomain(that.fDomain)
  169. , fTextureSampler(that.fTextureSampler)
  170. , fAlphaType(that.fAlphaType)
  171. , fDirection(that.fDirection) {
  172. this->addCoordTransform(&fCoordTransform);
  173. this->setTextureSamplerCnt(1);
  174. }
  175. void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
  176. GrProcessorKeyBuilder* b) const {
  177. GrGLBicubicEffect::GenKey(*this, caps, b);
  178. }
  179. GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
  180. return new GrGLBicubicEffect;
  181. }
  182. bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
  183. const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
  184. return fDomain == s.fDomain && fDirection == s.fDirection && fAlphaType == s.fAlphaType;
  185. }
  186. GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
  187. #if GR_TEST_UTILS
  188. std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
  189. int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
  190. : GrProcessorUnitTest::kAlphaTextureIdx;
  191. static const GrSamplerState::WrapMode kClampClamp[] = {GrSamplerState::WrapMode::kClamp,
  192. GrSamplerState::WrapMode::kClamp};
  193. SkAlphaType alphaType = d->fRandom->nextBool() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  194. Direction direction = Direction::kX;
  195. switch (d->fRandom->nextULessThan(3)) {
  196. case 0:
  197. direction = Direction::kX;
  198. break;
  199. case 1:
  200. direction = Direction::kY;
  201. break;
  202. case 2:
  203. direction = Direction::kXY;
  204. break;
  205. }
  206. return GrBicubicEffect::Make(d->textureProxy(texIdx), SkMatrix::I(), kClampClamp, direction,
  207. alphaType);
  208. }
  209. #endif
  210. //////////////////////////////////////////////////////////////////////////////
  211. bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) {
  212. if (matrix.isIdentity()) {
  213. *filterMode = GrSamplerState::Filter::kNearest;
  214. return false;
  215. }
  216. SkScalar scales[2];
  217. if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
  218. // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
  219. // entirely,
  220. *filterMode = GrSamplerState::Filter::kMipMap;
  221. return false;
  222. }
  223. // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
  224. if (scales[1] == SK_Scalar1) {
  225. if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
  226. SkScalarIsInt(matrix.getTranslateY())) {
  227. *filterMode = GrSamplerState::Filter::kNearest;
  228. } else {
  229. // Use bilerp to handle rotation or fractional translation.
  230. *filterMode = GrSamplerState::Filter::kBilerp;
  231. }
  232. return false;
  233. }
  234. // When we use the bicubic filtering effect each sample is read from the texture using
  235. // nearest neighbor sampling.
  236. *filterMode = GrSamplerState::Filter::kNearest;
  237. return true;
  238. }