blob: 28fcd9e1dcbdbaae1d7da1c96121fe6ef0eede88 [file] [log] [blame]
bsalomon848faf02014-07-11 10:01:02 -07001/*
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.com3aad3b02013-09-04 19:23:53 +00008#include "GrBicubicEffect.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -04009
Robert Phillips646e4292017-06-13 12:44:56 -040010#include "GrTexture.h"
brianosman54f30c12016-07-18 10:53:52 -070011#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "../private/GrGLSL.h"
bsalomon848faf02014-07-11 10:01:02 -070016
egdaniel64c47282015-11-13 06:54:19 -080017class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000018public:
robertphillips9cdb9922016-02-03 12:25:40 -080019 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000020
Brian Salomon94efbf52016-11-29 13:43:05 -050021 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070022 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070023 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
24 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
brianosman77320db2016-09-07 08:09:10 -070025 b->add32(GrColorSpaceXform::XformKey(bicubicEffect.colorSpaceXform()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026 }
27
wangyixb1daa862015-08-18 11:29:31 -070028protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040029 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070030
humper@google.com3aad3b02013-09-04 19:23:53 +000031private:
egdaniel018fb622015-10-28 07:26:40 -070032 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000033
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000034 UniformHandle fImageIncrementUni;
Brian Osmanc624d9d2017-03-08 11:42:02 -050035 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000036 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000037
egdaniel64c47282015-11-13 06:54:19 -080038 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000039};
40
wangyix7c157a92015-07-22 15:08:53 -070041void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070042 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000043
egdaniel7ea439b2015-12-03 09:20:44 -080044 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -080045 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080046 kVec2f_GrSLType, kDefault_GrSLPrecision,
47 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000048
egdaniel7ea439b2015-12-03 09:20:44 -080049 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000050
Brian Osmanc624d9d2017-03-08 11:42:02 -050051 fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform());
brianosman54f30c12016-07-18 10:53:52 -070052
cdalton85285412016-02-18 12:37:07 -080053 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -070054 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000055
56 /*
57 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
58 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
59 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
60 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
61 *
62 * The authors define a family of cubic filters with two free parameters (B and C):
63 *
64 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
65 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
66 * { 0 otherwise
67 *
68 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
69 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
70 * source of the specific weights below.
71 *
72 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
73 */
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040074 fragBuilder->codeAppend("float4x4 kMitchellCoefficients = float4x4("
Brian Osmana3fa2ef2017-01-06 16:13:06 +000075 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
76 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
77 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
78 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040079 fragBuilder->codeAppendf("float2 coord = %s - %s * float2(0.5);", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000080 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
81 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
82 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
83 // double hit a texel.
Brian Osmana3fa2ef2017-01-06 16:13:06 +000084 fragBuilder->codeAppendf("coord /= %s;", imgInc);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040085 fragBuilder->codeAppend("float2 f = fract(coord);");
86 fragBuilder->codeAppendf("coord = (coord - f + float2(0.5)) * %s;", imgInc);
87 fragBuilder->codeAppend("float4 wx = kMitchellCoefficients * float4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
88 fragBuilder->codeAppend("float4 wy = kMitchellCoefficients * float4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
89 fragBuilder->codeAppend("float4 rowColors[4];");
humper@google.com3aad3b02013-09-04 19:23:53 +000090 for (int y = 0; y < 4; ++y) {
91 for (int x = 0; x < 4; ++x) {
92 SkString coord;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040093 coord.printf("coord + %s * float2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000094 SkString sampleVar;
95 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -080096 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080097 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050098 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -070099 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -0800100 sampleVar.c_str(),
101 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700102 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 }
egdaniel4ca2e602015-11-18 08:01:26 -0800104 fragBuilder->codeAppendf(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400105 "float4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + wx.w * rowColors[3];",
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000106 y);
humper@google.com3aad3b02013-09-04 19:23:53 +0000107 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000108 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
Brian Osmanc624d9d2017-03-08 11:42:02 -0500109 if (fColorSpaceHelper.isValid()) {
brianosman77320db2016-09-07 08:09:10 -0700110 SkString xformedColor;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500111 fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper);
brianosman77320db2016-09-07 08:09:10 -0700112 bicubicColor.swap(xformedColor);
brianosman54f30c12016-07-18 10:53:52 -0700113 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400114 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputColor, bicubicColor.c_str(),
115 args.fInputColor);
humper@google.com3aad3b02013-09-04 19:23:53 +0000116}
117
egdaniel018fb622015-10-28 07:26:40 -0700118void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400119 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700120 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Robert Phillipsc686ce32017-07-21 14:12:29 -0400121 GrSurfaceProxy* proxy = processor.textureSampler(0).proxy();
122 GrTexture* texture = proxy->priv().peekTexture();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400123
humper@google.com3aad3b02013-09-04 19:23:53 +0000124 float imageIncrement[2];
Brian Salomon0bbecb22016-11-17 11:38:22 -0500125 imageIncrement[0] = 1.0f / texture->width();
126 imageIncrement[1] = 1.0f / texture->height();
kkinnunen7510b222014-07-30 00:04:16 -0700127 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
Robert Phillipsc686ce32017-07-21 14:12:29 -0400128 fDomain.setData(pdman, bicubicEffect.domain(), proxy);
brianosman54f30c12016-07-18 10:53:52 -0700129 if (SkToBool(bicubicEffect.colorSpaceXform())) {
Brian Osmanc624d9d2017-03-08 11:42:02 -0500130 fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform());
brianosman54f30c12016-07-18 10:53:52 -0700131 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000132}
133
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400134GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500135 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400136 const SkMatrix& matrix,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500137 const SkShader::TileMode tileModes[2])
Brian Salomon6cd51b52017-07-26 19:07:15 -0400138 : INHERITED{ModulateByConfigOptimizationFlags(proxy->config())}
139 , fCoordTransform(matrix, proxy.get())
140 , fDomain(GrTextureDomain::IgnoredDomain())
141 , fTextureSampler(std::move(proxy),
142 GrSamplerParams(tileModes, GrSamplerParams::kNone_FilterMode))
143 , fColorSpaceXform(std::move(colorSpaceXform)) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500144 this->initClassID<GrBicubicEffect>();
Brian Salomon6cd51b52017-07-26 19:07:15 -0400145 this->addCoordTransform(&fCoordTransform);
146 this->addTextureSampler(&fTextureSampler);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500147}
148
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400149GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500150 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400151 const SkMatrix& matrix,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500152 const SkRect& domain)
Brian Salomon6cd51b52017-07-26 19:07:15 -0400153 : INHERITED(ModulateByConfigOptimizationFlags(proxy->config()))
154 , fCoordTransform(matrix, proxy.get())
155 , fDomain(proxy.get(), domain, GrTextureDomain::kClamp_Mode)
156 , fTextureSampler(std::move(proxy))
157 , fColorSpaceXform(std::move(colorSpaceXform)) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500158 this->initClassID<GrBicubicEffect>();
Brian Salomon6cd51b52017-07-26 19:07:15 -0400159 this->addCoordTransform(&fCoordTransform);
160 this->addTextureSampler(&fTextureSampler);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500161}
162
Brian Salomon3f6f9652017-07-28 07:34:05 -0400163GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
164 : INHERITED(that.optimizationFlags())
165 , fCoordTransform(that.fCoordTransform)
166 , fDomain(that.fDomain)
167 , fTextureSampler(that.fTextureSampler)
168 , fColorSpaceXform(that.fColorSpaceXform) {
169 this->initClassID<GrBicubicEffect>();
170 this->addCoordTransform(&fCoordTransform);
171 this->addTextureSampler(&fTextureSampler);
humper@google.com3aad3b02013-09-04 19:23:53 +0000172}
173
Brian Salomon94efbf52016-11-29 13:43:05 -0500174void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800175 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800176 GrGLBicubicEffect::GenKey(*this, caps, b);
177}
178
egdaniel57d3b032015-11-13 11:57:27 -0800179GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800180 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000181}
182
bsalomon0e08fc12014-10-15 08:19:04 -0700183bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700184 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Osman08575272016-12-21 15:26:37 -0500185 return fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000186}
187
joshualittb0a8a372014-09-23 09:50:21 -0700188GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000189
Hal Canary6f6961e2017-01-31 13:50:44 -0500190#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700191sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500192 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
193 : GrProcessorUnitTest::kAlphaTextureIdx;
194 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Brian Osman08575272016-12-21 15:26:37 -0500195 static const SkShader::TileMode kClampClamp[] =
196 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400197 return GrBicubicEffect::Make(d->textureProxy(texIdx), std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500198 SkMatrix::I(), kClampClamp);
humper@google.com3aad3b02013-09-04 19:23:53 +0000199}
Hal Canary6f6961e2017-01-31 13:50:44 -0500200#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000201
202//////////////////////////////////////////////////////////////////////////////
203
204bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500205 GrSamplerParams::FilterMode* filterMode) {
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000206 if (matrix.isIdentity()) {
Brian Salomon514baff2016-11-17 15:17:07 -0500207 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000208 return false;
209 }
210
211 SkScalar scales[2];
212 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
213 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
214 // entirely,
Brian Salomon514baff2016-11-17 15:17:07 -0500215 *filterMode = GrSamplerParams::kMipMap_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000216 return false;
217 }
218 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
219 if (scales[1] == SK_Scalar1) {
220 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
221 SkScalarIsInt(matrix.getTranslateY())) {
Brian Salomon514baff2016-11-17 15:17:07 -0500222 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000223 } else {
224 // Use bilerp to handle rotation or fractional translation.
Brian Salomon514baff2016-11-17 15:17:07 -0500225 *filterMode = GrSamplerParams::kBilerp_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000226 }
227 return false;
228 }
229 // When we use the bicubic filtering effect each sample is read from the texture using
230 // nearest neighbor sampling.
Brian Salomon514baff2016-11-17 15:17:07 -0500231 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000232 return true;
233}