blob: 7a63a62dec62a973bd44d38664b82732880361ff [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
2 * Copyright 2013 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
jvanverth8ed3b9a2015-04-09 08:00:49 -07008#include "GrDistanceFieldGeoProc.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
jvanverth21deace2015-04-01 12:43:48 -070011
joshualitteb2a6762014-12-04 11:35:33 -080012#include "SkDistanceFieldGen.h"
jvanverth21deace2015-04-01 12:43:48 -070013
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080015#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080019#include "glsl/GrGLSLVarying.h"
20#include "glsl/GrGLSLVertexShaderBuilder.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000021
jvanverth21deace2015-04-01 12:43:48 -070022// Assuming a radius of a little less than the diagonal of the fragment
jvanverth24ba0082015-03-19 11:34:13 -070023#define SK_DistanceFieldAAFactor "0.65"
jvanverth2d2a68c2014-06-10 06:42:56 -070024
egdaniele659a582015-11-13 09:55:43 -080025class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000026public:
joshualitt465283c2015-09-11 08:19:35 -070027 GrGLDistanceFieldA8TextGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -070028 : fViewMatrix(SkMatrix::InvalidMatrix())
jvanverth9564ce62014-09-16 05:45:19 -070029#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -070030 , fDistanceAdjust(-1.0f)
jvanverth9564ce62014-09-16 05:45:19 -070031#endif
32 {}
jvanverth@google.comd830d132013-11-11 20:54:09 +000033
mtklein36352bf2015-03-25 18:17:31 -070034 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070035 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
36 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
egdaniel4ca2e602015-11-18 08:01:26 -080037 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
38 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -080039 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverth@google.comd830d132013-11-11 20:54:09 +000040
egdaniel4ca2e602015-11-18 08:01:26 -080041 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080042 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080043 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080044
45 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080046 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080047
jvanverth21deace2015-04-01 12:43:48 -070048#ifdef SK_GAMMA_APPLY_TO_A8
49 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070050 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070051 // width, height, 1/(3*width)
egdaniel7ea439b2015-12-03 09:20:44 -080052 fDistanceAdjustUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
53 kFloat_GrSLType, kDefault_GrSLPrecision,
54 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070055#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000056
joshualitt9b989322014-12-15 14:16:27 -080057 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070058 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -080059 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070060 }
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000061
joshualittabb52a12015-01-13 15:02:10 -080062 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080063 this->setupPosition(vertBuilder,
64 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080065 gpArgs,
66 dfTexEffect.inPosition()->fName,
67 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -070068 &fViewMatrixUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080069
joshualittabb52a12015-01-13 15:02:10 -080070 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080071 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080072 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080073 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080074 gpArgs->fPositionVar,
75 dfTexEffect.inPosition()->fName,
76 args.fTransformsIn,
77 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -080078
jvanverthbb4a1cf2015-04-07 09:06:00 -070079 // add varyings
egdaniel8dcdedc2015-11-11 06:27:20 -080080 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
81 GrGLSLVertToFrag st(kVec2f_GrSLType);
jvanverthbb4a1cf2015-04-07 09:06:00 -070082 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
egdaniel0eafe792015-11-20 14:01:22 -080083 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -080084 vertBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070085
joshualitt922c8b12015-08-07 09:55:23 -070086 // compute numbers to be hardcoded to convert texture coordinates from int to float
87 SkASSERT(dfTexEffect.numTextures() == 1);
88 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070089 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070090 SkScalar recipWidth = 1.0f / atlas->width();
91 SkScalar recipHeight = 1.0f / atlas->height();
92
egdaniel8dcdedc2015-11-11 06:27:20 -080093 GrGLSLVertToFrag uv(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -080094 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -080095 vertBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", uv.vsOut(),
96 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
97 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
98 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070099
jvanverthfdf7ccc2015-01-27 08:19:33 -0800100 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -0800101 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800102 kHigh_GrSLPrecision));
103 fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800104
egdaniel4ca2e602015-11-18 08:01:26 -0800105 fragBuilder->codeAppend("\tfloat texColor = ");
106 fragBuilder->appendTextureLookup(args.fSamplers[0],
107 "uv",
108 kVec2f_GrSLType);
109 fragBuilder->codeAppend(".r;\n");
110 fragBuilder->codeAppend("\tfloat distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800111 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -0700112#ifdef SK_GAMMA_APPLY_TO_A8
113 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -0800114 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700115#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000116
egdaniel4ca2e602015-11-18 08:01:26 -0800117 fragBuilder->codeAppend("float afwidth;");
jvanverthbb4a1cf2015-04-07 09:06:00 -0700118 if (isSimilarity) {
jvanverth354eba52015-03-16 11:32:49 -0700119 // For uniform scale, we adjust for the effect of the transformation on the distance
120 // by using the length of the gradient of the texture coordinates. We use st coordinates
jvanverth221360a2015-04-15 12:31:22 -0700121 // to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000122
jvanverth354eba52015-03-16 11:32:49 -0700123 // this gives us a smooth step across approximately one fragment
jvanverth221360a2015-04-15 12:31:22 -0700124 // we use y to work around a Mali400 bug in the x direction
egdaniel4ca2e602015-11-18 08:01:26 -0800125 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
126 st.fsIn());
jvanverth354eba52015-03-16 11:32:49 -0700127 } else {
128 // For general transforms, to determine the amount of correction we multiply a unit
129 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
130 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800131 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700132 // the length of the gradient may be 0, so we need to check for this
133 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800134 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
135 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
136 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
137 fragBuilder->codeAppend("} else {");
138 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
139 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700140
egdaniel4ca2e602015-11-18 08:01:26 -0800141 fragBuilder->codeAppendf("vec2 Jdx = dFdx(%s);", st.fsIn());
142 fragBuilder->codeAppendf("vec2 Jdy = dFdy(%s);", st.fsIn());
143 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
144 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000145
146 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800147 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000148 }
egdaniel4ca2e602015-11-18 08:01:26 -0800149 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
jvanverth@google.comd830d132013-11-11 20:54:09 +0000150
egdaniel4ca2e602015-11-18 08:01:26 -0800151 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000152 }
153
egdaniel018fb622015-10-28 07:26:40 -0700154 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
jvanverth2d2a68c2014-06-10 06:42:56 -0700155#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth502286d2015-04-08 12:37:51 -0700156 const GrDistanceFieldA8TextGeoProc& dfTexEffect = proc.cast<GrDistanceFieldA8TextGeoProc>();
jvanverth21deace2015-04-01 12:43:48 -0700157 float distanceAdjust = dfTexEffect.getDistanceAdjust();
158 if (distanceAdjust != fDistanceAdjust) {
159 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
160 fDistanceAdjust = distanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700161 }
162#endif
joshualitte578a952015-05-14 10:09:13 -0700163 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700164
165 if (!dfa8gp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfa8gp.viewMatrix())) {
166 fViewMatrix = dfa8gp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700167 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800168 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700169 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
170 }
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000171 }
172
robertphillips46d36f02015-01-18 08:14:14 -0800173 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700174 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700175 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700176 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800177 uint32_t key = dfTexEffect.getFlags();
joshualitt53f26aa2015-12-10 07:29:54 -0800178 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700179 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800180 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700181
182 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
183 SkASSERT(gp.numTextures() == 1);
184 GrTexture* atlas = gp.textureAccess(0).getTexture();
185 SkASSERT(atlas);
186 b->add32(atlas->width());
187 b->add32(atlas->height());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000188 }
189
jvanverth@google.comd830d132013-11-11 20:54:09 +0000190private:
joshualitt5559ca22015-05-21 15:50:36 -0700191 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700192 UniformHandle fViewMatrixUniform;
mtklein50282b42015-01-22 07:59:52 -0800193#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700194 float fDistanceAdjust;
195 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800196#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000197
egdaniele659a582015-11-13 09:55:43 -0800198 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000199};
200
201///////////////////////////////////////////////////////////////////////////////
202
jvanverth502286d2015-04-08 12:37:51 -0700203GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800204 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800205 GrTexture* texture,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000206 const GrTextureParams& params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700207#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700208 float distanceAdjust,
jvanverth2d2a68c2014-06-10 06:42:56 -0700209#endif
joshualittb8c241a2015-05-19 08:23:30 -0700210 uint32_t flags,
211 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700212 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700213 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800214 , fTextureAccess(texture, params)
jvanverth2d2a68c2014-06-10 06:42:56 -0700215#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700216 , fDistanceAdjust(distanceAdjust)
jvanverth2d2a68c2014-06-10 06:42:56 -0700217#endif
joshualitt249af152014-09-15 11:41:13 -0700218 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700219 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700220 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700221 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700222 this->initClassID<GrDistanceFieldA8TextGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700223 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
224 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800225 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800226 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800227 kVec2s_GrVertexAttribType));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000228 this->addTextureAccess(&fTextureAccess);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000229}
230
egdaniel57d3b032015-11-13 11:57:27 -0800231void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
232 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700233 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800234}
235
egdaniel57d3b032015-11-13 11:57:27 -0800236GrGLSLPrimitiveProcessor* GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700237 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000238}
239
240///////////////////////////////////////////////////////////////////////////////
241
jvanverth502286d2015-04-08 12:37:51 -0700242GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000243
bsalomonc21b09e2015-08-28 18:46:56 -0700244const GrGeometryProcessor* GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700245 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
246 GrProcessorUnitTest::kAlphaTextureIdx;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000247 static const SkShader::TileMode kTileModes[] = {
248 SkShader::kClamp_TileMode,
249 SkShader::kRepeat_TileMode,
250 SkShader::kMirror_TileMode,
251 };
252 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700253 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
254 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverth@google.comd830d132013-11-11 20:54:09 +0000255 };
joshualitt0067ff52015-07-08 14:26:19 -0700256 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
jvanverth@google.comd830d132013-11-11 20:54:09 +0000257 GrTextureParams::kNone_FilterMode);
258
joshualitt0067ff52015-07-08 14:26:19 -0700259 return GrDistanceFieldA8TextGeoProc::Create(GrRandomColor(d->fRandom),
260 GrTest::TestMatrix(d->fRandom),
261 d->fTextures[texIdx], params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700262#ifdef SK_GAMMA_APPLY_TO_A8
joshualitt0067ff52015-07-08 14:26:19 -0700263 d->fRandom->nextF(),
jvanverth2d2a68c2014-06-10 06:42:56 -0700264#endif
joshualitt0067ff52015-07-08 14:26:19 -0700265 d->fRandom->nextBool() ?
joshualittb8c241a2015-05-19 08:23:30 -0700266 kSimilarity_DistanceFieldEffectFlag : 0,
joshualitt0067ff52015-07-08 14:26:19 -0700267 d->fRandom->nextBool());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000268}
269
270///////////////////////////////////////////////////////////////////////////////
271
egdaniele659a582015-11-13 09:55:43 -0800272class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700273public:
joshualitt465283c2015-09-11 08:19:35 -0700274 GrGLDistanceFieldPathGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -0700275 : fViewMatrix(SkMatrix::InvalidMatrix())
joshualitt5559ca22015-05-21 15:50:36 -0700276 , fTextureSize(SkISize::Make(-1, -1)) {}
jvanverthfa38a302014-10-06 05:59:05 -0700277
mtklein36352bf2015-03-25 18:17:31 -0700278 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700279 const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700280
egdaniel4ca2e602015-11-18 08:01:26 -0800281 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
282 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800283 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverthfa38a302014-10-06 05:59:05 -0700284
egdaniel4ca2e602015-11-18 08:01:26 -0800285 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800286 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800287 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800288
289 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800290 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -0800291
egdaniel8dcdedc2015-11-11 06:27:20 -0800292 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800293 varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
jvanverthfa38a302014-10-06 05:59:05 -0700294
joshualitt9b989322014-12-15 14:16:27 -0800295 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700296 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800297 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700298 }
egdaniel4ca2e602015-11-18 08:01:26 -0800299 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName);
reede4ef1ca2015-02-17 18:38:38 -0800300
joshualittabb52a12015-01-13 15:02:10 -0800301 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800302 this->setupPosition(vertBuilder,
303 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800304 gpArgs,
305 dfTexEffect.inPosition()->fName,
306 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700307 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800308
309 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800310 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800311 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800312 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800313 gpArgs->fPositionVar,
314 dfTexEffect.inPosition()->fName,
315 args.fTransformsIn,
316 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800317
halcanary96fcdcc2015-08-27 07:41:13 -0700318 const char* textureSizeUniName = nullptr;
egdaniel7ea439b2015-12-03 09:20:44 -0800319 fTextureSizeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
320 kVec2f_GrSLType, kDefault_GrSLPrecision,
321 "TextureSize", &textureSizeUniName);
reede4ef1ca2015-02-17 18:38:38 -0800322
jvanverthfdf7ccc2015-01-27 08:19:33 -0800323 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -0800324 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800325 kHigh_GrSLPrecision));
326 fragBuilder->codeAppendf("vec2 uv = %s;", v.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800327
egdaniel4ca2e602015-11-18 08:01:26 -0800328 fragBuilder->codeAppend("float texColor = ");
329 fragBuilder->appendTextureLookup(args.fSamplers[0],
330 "uv",
331 kVec2f_GrSLType);
332 fragBuilder->codeAppend(".r;");
333 fragBuilder->codeAppend("float distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800334 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700335
egdaniela2e3e0f2015-11-19 07:23:45 -0800336 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800337 kHigh_GrSLPrecision));
338 fragBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName);
339 fragBuilder->codeAppend("float afwidth;");
jvanverthfa38a302014-10-06 05:59:05 -0700340 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) {
jvanverth354eba52015-03-16 11:32:49 -0700341 // For uniform scale, we adjust for the effect of the transformation on the distance
342 // by using the length of the gradient of the texture coordinates. We use st coordinates
343 // to ensure we're mapping 1:1 from texel space to pixel space.
344
jvanverthfa38a302014-10-06 05:59:05 -0700345 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800346 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(st.y));");
jvanverthfa38a302014-10-06 05:59:05 -0700347 } else {
jvanverth354eba52015-03-16 11:32:49 -0700348 // For general transforms, to determine the amount of correction we multiply a unit
349 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
350 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800351 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700352 // the length of the gradient may be 0, so we need to check for this
353 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800354 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
355 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
356 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
357 fragBuilder->codeAppend("} else {");
358 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
359 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700360
egdaniel4ca2e602015-11-18 08:01:26 -0800361 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
362 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
363 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
364 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700365
366 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800367 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700368 }
egdaniel4ca2e602015-11-18 08:01:26 -0800369 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
jvanverthfa38a302014-10-06 05:59:05 -0700370
egdaniel4ca2e602015-11-18 08:01:26 -0800371 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700372 }
373
egdaniel018fb622015-10-28 07:26:40 -0700374 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
reede4ef1ca2015-02-17 18:38:38 -0800375 SkASSERT(fTextureSizeUni.isValid());
jvanverthfa38a302014-10-06 05:59:05 -0700376
joshualitt87f48d92014-12-04 10:41:40 -0800377 GrTexture* texture = proc.texture(0);
jvanverthfa38a302014-10-06 05:59:05 -0700378 if (texture->width() != fTextureSize.width() ||
379 texture->height() != fTextureSize.height()) {
380 fTextureSize = SkISize::Make(texture->width(), texture->height());
reede4ef1ca2015-02-17 18:38:38 -0800381 pdman.set2f(fTextureSizeUni,
382 SkIntToScalar(fTextureSize.width()),
383 SkIntToScalar(fTextureSize.height()));
jvanverthfa38a302014-10-06 05:59:05 -0700384 }
joshualitt9b989322014-12-15 14:16:27 -0800385
joshualitte578a952015-05-14 10:09:13 -0700386 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700387
388 if (!dfpgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfpgp.viewMatrix())) {
389 fViewMatrix = dfpgp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700390 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800391 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700392 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
393 }
jvanverthfa38a302014-10-06 05:59:05 -0700394 }
395
robertphillips46d36f02015-01-18 08:14:14 -0800396 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700397 const GrGLSLCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700398 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700399 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700400
joshualitt8fc6c2d2014-12-22 15:27:05 -0800401 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700402 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700403 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800404 b->add32(key);
jvanverthfa38a302014-10-06 05:59:05 -0700405 }
406
407private:
reede4ef1ca2015-02-17 18:38:38 -0800408 UniformHandle fTextureSizeUni;
joshualitt5559ca22015-05-21 15:50:36 -0700409 UniformHandle fViewMatrixUniform;
410 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800411 SkISize fTextureSize;
jvanverthfa38a302014-10-06 05:59:05 -0700412
egdaniele659a582015-11-13 09:55:43 -0800413 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700414};
415
416///////////////////////////////////////////////////////////////////////////////
417
jvanverth502286d2015-04-08 12:37:51 -0700418GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(
joshualitt2e3b3e32014-12-09 13:31:14 -0800419 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800420 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800421 GrTexture* texture,
422 const GrTextureParams& params,
joshualittb8c241a2015-05-19 08:23:30 -0700423 uint32_t flags,
424 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700425 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700426 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800427 , fTextureAccess(texture, params)
jvanverthfa38a302014-10-06 05:59:05 -0700428 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700429 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700430 , fUsesLocalCoords(usesLocalCoords) {
jvanverthfa38a302014-10-06 05:59:05 -0700431 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700432 this->initClassID<GrDistanceFieldPathGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700433 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
434 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800435 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800436 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
joshualittb8c241a2015-05-19 08:23:30 -0700437 kVec2f_GrVertexAttribType));
jvanverthfa38a302014-10-06 05:59:05 -0700438 this->addTextureAccess(&fTextureAccess);
439}
440
egdaniel57d3b032015-11-13 11:57:27 -0800441void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
442 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700443 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800444}
445
egdaniel57d3b032015-11-13 11:57:27 -0800446GrGLSLPrimitiveProcessor* GrDistanceFieldPathGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700447 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700448}
449
450///////////////////////////////////////////////////////////////////////////////
451
jvanverth502286d2015-04-08 12:37:51 -0700452GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700453
bsalomonc21b09e2015-08-28 18:46:56 -0700454const GrGeometryProcessor* GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700455 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
456 : GrProcessorUnitTest::kAlphaTextureIdx;
jvanverthfa38a302014-10-06 05:59:05 -0700457 static const SkShader::TileMode kTileModes[] = {
458 SkShader::kClamp_TileMode,
459 SkShader::kRepeat_TileMode,
460 SkShader::kMirror_TileMode,
461 };
462 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700463 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
464 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverthfa38a302014-10-06 05:59:05 -0700465 };
joshualitt0067ff52015-07-08 14:26:19 -0700466 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode
467 : GrTextureParams::kNone_FilterMode);
jvanverthfa38a302014-10-06 05:59:05 -0700468
joshualitt0067ff52015-07-08 14:26:19 -0700469 return GrDistanceFieldPathGeoProc::Create(GrRandomColor(d->fRandom),
470 GrTest::TestMatrix(d->fRandom),
471 d->fTextures[texIdx],
jvanverth502286d2015-04-08 12:37:51 -0700472 params,
joshualitt0067ff52015-07-08 14:26:19 -0700473 d->fRandom->nextBool() ?
joshualittb8c241a2015-05-19 08:23:30 -0700474 kSimilarity_DistanceFieldEffectFlag : 0,
joshualitt0067ff52015-07-08 14:26:19 -0700475 d->fRandom->nextBool());
jvanverthfa38a302014-10-06 05:59:05 -0700476}
477
478///////////////////////////////////////////////////////////////////////////////
479
egdaniele659a582015-11-13 09:55:43 -0800480class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000481public:
joshualitt465283c2015-09-11 08:19:35 -0700482 GrGLDistanceFieldLCDTextGeoProc()
joshualitt53f26aa2015-12-10 07:29:54 -0800483 : fViewMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700484 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700485 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000486
mtklein36352bf2015-03-25 18:17:31 -0700487 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700488 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
489 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000490
egdaniel4ca2e602015-11-18 08:01:26 -0800491 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800492 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800493 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800494
495 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800496 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800497
498 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800499
joshualitt9b989322014-12-15 14:16:27 -0800500 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700501 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800502 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700503 }
joshualitt9b989322014-12-15 14:16:27 -0800504
joshualittabb52a12015-01-13 15:02:10 -0800505 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800506 this->setupPosition(vertBuilder,
507 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800508 gpArgs,
509 dfTexEffect.inPosition()->fName,
510 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700511 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800512
joshualittabb52a12015-01-13 15:02:10 -0800513 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800514 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800515 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800516 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800517 gpArgs->fPositionVar,
518 dfTexEffect.inPosition()->fName,
519 args.fTransformsIn,
520 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800521
jvanverthbb4a1cf2015-04-07 09:06:00 -0700522 // set up varyings
523 bool isUniformScale = SkToBool(dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask);
egdaniel8dcdedc2015-11-11 06:27:20 -0800524 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
525 GrGLSLVertToFrag st(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800526 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800527 vertBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700528
joshualitt922c8b12015-08-07 09:55:23 -0700529 // compute numbers to be hardcoded to convert texture coordinates from int to float
530 SkASSERT(dfTexEffect.numTextures() == 1);
531 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -0700532 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -0700533 SkScalar recipWidth = 1.0f / atlas->width();
534 SkScalar recipHeight = 1.0f / atlas->height();
535
egdaniel8dcdedc2015-11-11 06:27:20 -0800536 GrGLSLVertToFrag uv(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800537 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800538 vertBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", uv.vsOut(),
539 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
540 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
541 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700542
543 // add frag shader code
joshualitt30ba4362014-08-21 20:18:45 -0700544
egdaniel4ca2e602015-11-18 08:01:26 -0800545 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800546 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt30ba4362014-08-21 20:18:45 -0700547
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000548 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800549 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -0800550 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800551 kHigh_GrSLPrecision));
552 fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn());
egdaniela2e3e0f2015-11-19 07:23:45 -0800553 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800554 kHigh_GrSLPrecision));
joshualitt922c8b12015-08-07 09:55:23 -0700555
556 SkScalar lcdDelta = 1.0f / (3.0f * atlas->width());
jvanverth5a105ff2015-02-18 11:36:35 -0800557 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
egdaniel4ca2e602015-11-18 08:01:26 -0800558 fragBuilder->codeAppendf("float delta = -%.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800559 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800560 fragBuilder->codeAppendf("float delta = %.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800561 }
jvanverth78f07182014-07-30 06:17:59 -0700562 if (isUniformScale) {
egdaniel4ca2e602015-11-18 08:01:26 -0800563 fragBuilder->codeAppendf("float dy = abs(dFdy(%s.y));", st.fsIn());
564 fragBuilder->codeAppend("vec2 offset = vec2(dy*delta, 0.0);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000565 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800566 fragBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700567
egdaniel4ca2e602015-11-18 08:01:26 -0800568 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
569 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
570 fragBuilder->codeAppend("vec2 offset = delta*Jdx;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000571 }
572
573 // green is distance to uv center
egdaniel4ca2e602015-11-18 08:01:26 -0800574 fragBuilder->codeAppend("\tvec4 texColor = ");
575 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType);
576 fragBuilder->codeAppend(";\n");
577 fragBuilder->codeAppend("\tvec3 distance;\n");
578 fragBuilder->codeAppend("\tdistance.y = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000579 // red is distance to left offset
egdaniel4ca2e602015-11-18 08:01:26 -0800580 fragBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n");
581 fragBuilder->codeAppend("\ttexColor = ");
582 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType);
583 fragBuilder->codeAppend(";\n");
584 fragBuilder->codeAppend("\tdistance.x = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000585 // blue is distance to right offset
egdaniel4ca2e602015-11-18 08:01:26 -0800586 fragBuilder->codeAppend("\tuv_adjusted = uv + offset;\n");
587 fragBuilder->codeAppend("\ttexColor = ");
588 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType);
589 fragBuilder->codeAppend(";\n");
590 fragBuilder->codeAppend("\tdistance.z = texColor.r;\n");
jvanverth2d2a68c2014-06-10 06:42:56 -0700591
egdaniel4ca2e602015-11-18 08:01:26 -0800592 fragBuilder->codeAppend("\tdistance = "
jvanverthada68ef2014-11-03 14:00:24 -0800593 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700594
jvanverth21deace2015-04-01 12:43:48 -0700595 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700596 const char* distanceAdjustUniName = nullptr;
egdaniel7ea439b2015-12-03 09:20:44 -0800597 fDistanceAdjustUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
598 kVec3f_GrSLType, kDefault_GrSLPrecision,
599 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800600 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700601
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000602 // To be strictly correct, we should compute the anti-aliasing factor separately
603 // for each color component. However, this is only important when using perspective
604 // transformations, and even then using a single factor seems like a reasonable
605 // trade-off between quality and speed.
egdaniel4ca2e602015-11-18 08:01:26 -0800606 fragBuilder->codeAppend("float afwidth;");
jvanverth78f07182014-07-30 06:17:59 -0700607 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700608 // For uniform scale, we adjust for the effect of the transformation on the distance
609 // by using the length of the gradient of the texture coordinates. We use st coordinates
610 // to ensure we're mapping 1:1 from texel space to pixel space.
611
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000612 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800613 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*dy;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000614 } else {
jvanverth354eba52015-03-16 11:32:49 -0700615 // For general transforms, to determine the amount of correction we multiply a unit
616 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
617 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800618 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700619 // the length of the gradient may be 0, so we need to check for this
620 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800621 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
622 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
623 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
624 fragBuilder->codeAppend("} else {");
625 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
626 fragBuilder->codeAppend("}");
627 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
628 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000629
630 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800631 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000632 }
633
egdaniel4ca2e602015-11-18 08:01:26 -0800634 fragBuilder->codeAppend(
jvanverth21deace2015-04-01 12:43:48 -0700635 "vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);");
egdaniel27b63352015-09-15 13:13:50 -0700636 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -0800637 fragBuilder->codeAppend("val.a = max(max(val.r, val.g), val.b);");
jvanverth2d2a68c2014-06-10 06:42:56 -0700638
egdaniel4ca2e602015-11-18 08:01:26 -0800639 fragBuilder->codeAppendf("%s = val;", args.fOutputCoverage);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000640 }
641
egdaniel018fb622015-10-28 07:26:40 -0700642 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700643 const GrPrimitiveProcessor& processor) override {
jvanverth21deace2015-04-01 12:43:48 -0700644 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000645
joshualitt5559ca22015-05-21 15:50:36 -0700646 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
647 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700648 if (wa != fDistanceAdjust) {
649 pdman.set3f(fDistanceAdjustUni,
650 wa.fR,
651 wa.fG,
652 wa.fB);
653 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700654 }
joshualitt9b989322014-12-15 14:16:27 -0800655
joshualitt5559ca22015-05-21 15:50:36 -0700656 if (!dflcd.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dflcd.viewMatrix())) {
657 fViewMatrix = dflcd.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700658 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800659 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700660 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
661 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000662 }
663
robertphillips46d36f02015-01-18 08:14:14 -0800664 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700665 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700666 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700667 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000668
joshualitt8fc6c2d2014-12-22 15:27:05 -0800669 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700670 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700671 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800672 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700673
674 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
675 SkASSERT(gp.numTextures() == 1);
676 GrTexture* atlas = gp.textureAccess(0).getTexture();
677 SkASSERT(atlas);
678 b->add32(atlas->width());
679 b->add32(atlas->height());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000680 }
681
682private:
joshualitt5559ca22015-05-21 15:50:36 -0700683 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700684 UniformHandle fViewMatrixUniform;
jvanverth21deace2015-04-01 12:43:48 -0700685 UniformHandle fColorUniform;
jvanverth502286d2015-04-08 12:37:51 -0700686 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
jvanverth21deace2015-04-01 12:43:48 -0700687 UniformHandle fDistanceAdjustUni;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000688
egdaniele659a582015-11-13 09:55:43 -0800689 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000690};
691
692///////////////////////////////////////////////////////////////////////////////
693
jvanverth502286d2015-04-08 12:37:51 -0700694GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(
joshualitt8059eb92014-12-29 15:10:07 -0800695 GrColor color, const SkMatrix& viewMatrix,
jvanverth2d2a68c2014-06-10 06:42:56 -0700696 GrTexture* texture, const GrTextureParams& params,
jvanverth21deace2015-04-01 12:43:48 -0700697 DistanceAdjust distanceAdjust,
joshualittb8c241a2015-05-19 08:23:30 -0700698 uint32_t flags, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700699 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700700 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800701 , fTextureAccess(texture, params)
jvanverth21deace2015-04-01 12:43:48 -0700702 , fDistanceAdjust(distanceAdjust)
joshualittb8c241a2015-05-19 08:23:30 -0700703 , fFlags(flags & kLCD_DistanceFieldEffectMask)
704 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700705 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
jvanverth502286d2015-04-08 12:37:51 -0700706 this->initClassID<GrDistanceFieldLCDTextGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700707 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
708 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800709 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800710 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
joshualittb8c241a2015-05-19 08:23:30 -0700711 kVec2s_GrVertexAttribType));
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000712 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000713}
714
egdaniel57d3b032015-11-13 11:57:27 -0800715void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
716 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700717 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800718}
719
egdaniel57d3b032015-11-13 11:57:27 -0800720GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700721 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000722}
723
724///////////////////////////////////////////////////////////////////////////////
725
jvanverth502286d2015-04-08 12:37:51 -0700726GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000727
bsalomonc21b09e2015-08-28 18:46:56 -0700728const GrGeometryProcessor* GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700729 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
730 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000731 static const SkShader::TileMode kTileModes[] = {
732 SkShader::kClamp_TileMode,
733 SkShader::kRepeat_TileMode,
734 SkShader::kMirror_TileMode,
735 };
736 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700737 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
738 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000739 };
joshualitt0067ff52015-07-08 14:26:19 -0700740 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000741 GrTextureParams::kNone_FilterMode);
jvanverth21deace2015-04-01 12:43:48 -0700742 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700743 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
joshualitt0067ff52015-07-08 14:26:19 -0700744 flags |= d->fRandom->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0;
745 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
746 return GrDistanceFieldLCDTextGeoProc::Create(GrRandomColor(d->fRandom),
747 GrTest::TestMatrix(d->fRandom),
748 d->fTextures[texIdx], params,
jvanverth502286d2015-04-08 12:37:51 -0700749 wa,
joshualittb8c241a2015-05-19 08:23:30 -0700750 flags,
joshualitt0067ff52015-07-08 14:26:19 -0700751 d->fRandom->nextBool());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000752}