blob: c60400e383f74a0f5920a4282e94e1f4c1a5a7f1 [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 Phillips40fd7c92017-01-30 08:06:27 -050010#include "GrProxyMove.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050011#include "GrTextureProxy.h"
brianosman54f30c12016-07-18 10:53:52 -070012#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "../private/GrGLSL.h"
bsalomon848faf02014-07-11 10:01:02 -070017
egdaniel64c47282015-11-13 06:54:19 -080018class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000019public:
robertphillips9cdb9922016-02-03 12:25:40 -080020 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000021
Brian Salomon94efbf52016-11-29 13:43:05 -050022 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070023 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070024 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
25 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
brianosman77320db2016-09-07 08:09:10 -070026 b->add32(GrColorSpaceXform::XformKey(bicubicEffect.colorSpaceXform()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000027 }
28
wangyixb1daa862015-08-18 11:29:31 -070029protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040030 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070031
humper@google.com3aad3b02013-09-04 19:23:53 +000032private:
egdaniel018fb622015-10-28 07:26:40 -070033 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000034
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000035 UniformHandle fImageIncrementUni;
Brian Osmanc624d9d2017-03-08 11:42:02 -050036 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000037 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000038
egdaniel64c47282015-11-13 06:54:19 -080039 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000040};
41
wangyix7c157a92015-07-22 15:08:53 -070042void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070043 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000044
egdaniel7ea439b2015-12-03 09:20:44 -080045 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -080046 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080047 kVec2f_GrSLType, kDefault_GrSLPrecision,
48 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000049
egdaniel7ea439b2015-12-03 09:20:44 -080050 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000051
Brian Osmanc624d9d2017-03-08 11:42:02 -050052 fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform());
brianosman54f30c12016-07-18 10:53:52 -070053
cdalton85285412016-02-18 12:37:07 -080054 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -070055 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000056
57 /*
58 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
59 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
60 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
61 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
62 *
63 * The authors define a family of cubic filters with two free parameters (B and C):
64 *
65 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
66 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
67 * { 0 otherwise
68 *
69 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
70 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
71 * source of the specific weights below.
72 *
73 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
74 */
75 fragBuilder->codeAppend("mat4 kMitchellCoefficients = mat4("
76 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
77 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
78 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
79 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
80 fragBuilder->codeAppendf("vec2 coord = %s - %s * vec2(0.5);", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000081 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
82 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
83 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
84 // double hit a texel.
Brian Osmana3fa2ef2017-01-06 16:13:06 +000085 fragBuilder->codeAppendf("coord /= %s;", imgInc);
86 fragBuilder->codeAppend("vec2 f = fract(coord);");
87 fragBuilder->codeAppendf("coord = (coord - f + vec2(0.5)) * %s;", imgInc);
88 fragBuilder->codeAppend("vec4 wx = kMitchellCoefficients * vec4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
89 fragBuilder->codeAppend("vec4 wy = kMitchellCoefficients * vec4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
90 fragBuilder->codeAppend("vec4 rowColors[4];");
humper@google.com3aad3b02013-09-04 19:23:53 +000091 for (int y = 0; y < 4; ++y) {
92 for (int x = 0; x < 4; ++x) {
93 SkString coord;
94 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000095 SkString sampleVar;
96 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -080097 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080098 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050099 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -0700100 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -0800101 sampleVar.c_str(),
102 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700103 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000104 }
egdaniel4ca2e602015-11-18 08:01:26 -0800105 fragBuilder->codeAppendf(
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000106 "vec4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + wx.w * rowColors[3];",
107 y);
humper@google.com3aad3b02013-09-04 19:23:53 +0000108 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000109 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
Brian Osmanc624d9d2017-03-08 11:42:02 -0500110 if (fColorSpaceHelper.isValid()) {
brianosman77320db2016-09-07 08:09:10 -0700111 SkString xformedColor;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500112 fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper);
brianosman77320db2016-09-07 08:09:10 -0700113 bicubicColor.swap(xformedColor);
brianosman54f30c12016-07-18 10:53:52 -0700114 }
Ethan Nicholas421fe442017-05-04 13:10:59 +0000115 fragBuilder->codeAppendf("%s = %s;",
116 args.fOutputColor, (GrGLSLExpr4(bicubicColor.c_str()) *
117 GrGLSLExpr4(args.fInputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000118}
119
egdaniel018fb622015-10-28 07:26:40 -0700120void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400121 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700122 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Brian Salomondb4183d2016-11-17 12:48:40 -0500123 GrTexture* texture = processor.textureSampler(0).texture();
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 Phillipse98234f2017-01-09 14:23:59 -0500128 fDomain.setData(pdman, bicubicEffect.domain(), texture);
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 Phillips296b1cc2017-03-15 10:42:12 -0400134GrBicubicEffect::GrBicubicEffect(GrResourceProvider* resourceProvider, sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500135 sk_sp<GrColorSpaceXform> colorSpaceXform,
136 const SkMatrix &matrix,
137 const SkShader::TileMode tileModes[2])
Robert Phillips296b1cc2017-03-15 10:42:12 -0400138 : INHERITED{resourceProvider,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500139 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
Robert Phillips296b1cc2017-03-15 10:42:12 -0400148GrBicubicEffect::GrBicubicEffect(GrResourceProvider* resourceProvider, sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500149 sk_sp<GrColorSpaceXform> colorSpaceXform,
150 const SkMatrix &matrix,
151 const SkRect& domain)
Robert Phillips296b1cc2017-03-15 10:42:12 -0400152 : INHERITED(resourceProvider, ModulationFlags(proxy->config()), proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500153 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
humper@google.com3aad3b02013-09-04 19:23:53 +0000159GrBicubicEffect::~GrBicubicEffect() {
160}
161
Brian Salomon94efbf52016-11-29 13:43:05 -0500162void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800163 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800164 GrGLBicubicEffect::GenKey(*this, caps, b);
165}
166
egdaniel57d3b032015-11-13 11:57:27 -0800167GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800168 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000169}
170
bsalomon0e08fc12014-10-15 08:19:04 -0700171bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700172 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Osman08575272016-12-21 15:26:37 -0500173 return fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000174}
175
joshualittb0a8a372014-09-23 09:50:21 -0700176GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000177
Hal Canary6f6961e2017-01-31 13:50:44 -0500178#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700179sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500180 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
181 : GrProcessorUnitTest::kAlphaTextureIdx;
182 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Brian Osman08575272016-12-21 15:26:37 -0500183 static const SkShader::TileMode kClampClamp[] =
184 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
Robert Phillips296b1cc2017-03-15 10:42:12 -0400185 return GrBicubicEffect::Make(d->resourceProvider(),
186 d->textureProxy(texIdx), std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500187 SkMatrix::I(), kClampClamp);
humper@google.com3aad3b02013-09-04 19:23:53 +0000188}
Hal Canary6f6961e2017-01-31 13:50:44 -0500189#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000190
191//////////////////////////////////////////////////////////////////////////////
192
193bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500194 GrSamplerParams::FilterMode* filterMode) {
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000195 if (matrix.isIdentity()) {
Brian Salomon514baff2016-11-17 15:17:07 -0500196 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000197 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,
Brian Salomon514baff2016-11-17 15:17:07 -0500204 *filterMode = GrSamplerParams::kMipMap_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000205 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())) {
Brian Salomon514baff2016-11-17 15:17:07 -0500211 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000212 } else {
213 // Use bilerp to handle rotation or fractional translation.
Brian Salomon514baff2016-11-17 15:17:07 -0500214 *filterMode = GrSamplerParams::kBilerp_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000215 }
216 return false;
217 }
218 // When we use the bicubic filtering effect each sample is read from the texture using
219 // nearest neighbor sampling.
Brian Salomon514baff2016-11-17 15:17:07 -0500220 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000221 return true;
222}