blob: b77a8f438b90b7aa7e64c298831b37f1ad022865 [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>();
cdalton85285412016-02-18 12:37:07 -080037 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverth@google.comd830d132013-11-11 20:54:09 +000038
egdaniel4ca2e602015-11-18 08:01:26 -080039 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080040 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080041 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080042
43 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080044 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080045
jvanverth21deace2015-04-01 12:43:48 -070046#ifdef SK_GAMMA_APPLY_TO_A8
47 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070048 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070049 // width, height, 1/(3*width)
cdalton5e58cee2016-02-11 12:49:47 -080050 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080051 kFloat_GrSLType, kDefault_GrSLPrecision,
52 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070053#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000054
joshualitt9b989322014-12-15 14:16:27 -080055 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070056 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -080057 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070058 }
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000059
joshualittabb52a12015-01-13 15:02:10 -080060 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080061 this->setupPosition(vertBuilder,
62 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080063 gpArgs,
64 dfTexEffect.inPosition()->fName,
65 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -070066 &fViewMatrixUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080067
joshualittabb52a12015-01-13 15:02:10 -080068 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080069 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080070 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080071 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080072 gpArgs->fPositionVar,
73 dfTexEffect.inPosition()->fName,
bsalomona624bf32016-09-20 09:12:47 -070074 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -080075
jvanverthbb4a1cf2015-04-07 09:06:00 -070076 // add varyings
egdaniel8dcdedc2015-11-11 06:27:20 -080077 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -080078 GrGLSLVertToFrag uv(kVec2f_GrSLType);
jvanverthcf371bb2016-03-10 11:10:43 -080079 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
80 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070081 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070082 bool isGammaCorrect =
83 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverth7023a002016-02-22 11:25:32 -080084 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
85 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070086
jvanverthcf371bb2016-03-10 11:10:43 -080087 // compute numbers to be hardcoded to convert texture coordinates from float to int
Brian Salomon0bbecb22016-11-17 11:38:22 -050088 SkASSERT(dfTexEffect.numTextureSamplers() == 1);
Brian Salomondb4183d2016-11-17 12:48:40 -050089 GrTexture* atlas = dfTexEffect.textureSampler(0).texture();
joshualitt7375d6b2015-08-07 13:36:44 -070090 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070091
jvanverth7023a002016-02-22 11:25:32 -080092 GrGLSLVertToFrag st(kVec2f_GrSLType);
93 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
94 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
95 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -080096 dfTexEffect.inTextureCoords()->fName);
halcanary9d524f22016-03-29 09:03:52 -070097
jvanverthfdf7ccc2015-01-27 08:19:33 -080098 // Use highp to work around aliasing issues
Ethan Nicholas1fc83b12016-11-22 09:31:35 -050099 fragBuilder->codeAppendf("highp vec2 uv = %s;\n", uv.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800100
egdaniel4ca2e602015-11-18 08:01:26 -0800101 fragBuilder->codeAppend("\tfloat texColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700102 fragBuilder->appendTextureLookup(args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -0800103 "uv",
104 kVec2f_GrSLType);
105 fragBuilder->codeAppend(".r;\n");
106 fragBuilder->codeAppend("\tfloat distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800107 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -0700108#ifdef SK_GAMMA_APPLY_TO_A8
109 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700111#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000112
egdaniel4ca2e602015-11-18 08:01:26 -0800113 fragBuilder->codeAppend("float afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800114 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700115 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700116 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800117 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000118
jvanverth354eba52015-03-16 11:32:49 -0700119 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700120#ifdef SK_VULKAN
121 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
122 st.fsIn());
123#else
124 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800125 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
126 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700127#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800128 } else if (isSimilarity) {
129 // For similarity transform, we adjust the effect of the transformation on the distance
130 // by using the length of the gradient of the texture coordinates. We use st coordinates
131 // to ensure we're mapping 1:1 from texel space to pixel space.
132 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
133
134 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700135#ifdef SK_VULKAN
136 fragBuilder->codeAppendf("float st_grad_len = length(dFdx(%s));", st.fsIn());
137#else
138 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800139 fragBuilder->codeAppendf("float st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700140#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800141 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700142 } else {
143 // For general transforms, to determine the amount of correction we multiply a unit
144 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
145 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800146 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700147 // the length of the gradient may be 0, so we need to check for this
148 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800149 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
150 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
151 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
152 fragBuilder->codeAppend("} else {");
153 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
154 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700155
egdaniel4ca2e602015-11-18 08:01:26 -0800156 fragBuilder->codeAppendf("vec2 Jdx = dFdx(%s);", st.fsIn());
157 fragBuilder->codeAppendf("vec2 Jdy = dFdy(%s);", st.fsIn());
158 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
159 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000160
161 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800162 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000163 }
brianosman0586f5c2016-04-12 12:48:21 -0700164
165 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
166 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
167 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700168 if (isGammaCorrect) {
brianosman0586f5c2016-04-12 12:48:21 -0700169 fragBuilder->codeAppend(
170 "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);");
171 } else {
172 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
173 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000174
egdaniel4ca2e602015-11-18 08:01:26 -0800175 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000176 }
177
bsalomona624bf32016-09-20 09:12:47 -0700178 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
179 FPCoordTransformIter&& transformIter) override {
jvanverth2d2a68c2014-06-10 06:42:56 -0700180#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth502286d2015-04-08 12:37:51 -0700181 const GrDistanceFieldA8TextGeoProc& dfTexEffect = proc.cast<GrDistanceFieldA8TextGeoProc>();
jvanverth21deace2015-04-01 12:43:48 -0700182 float distanceAdjust = dfTexEffect.getDistanceAdjust();
183 if (distanceAdjust != fDistanceAdjust) {
184 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
185 fDistanceAdjust = distanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700186 }
187#endif
joshualitte578a952015-05-14 10:09:13 -0700188 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700189
190 if (!dfa8gp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfa8gp.viewMatrix())) {
191 fViewMatrix = dfa8gp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700192 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800193 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700194 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
195 }
bsalomona624bf32016-09-20 09:12:47 -0700196 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000197 }
198
robertphillips46d36f02015-01-18 08:14:14 -0800199 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700200 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700201 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700202 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800203 uint32_t key = dfTexEffect.getFlags();
joshualitt53f26aa2015-12-10 07:29:54 -0800204 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700205 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800206 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700207
208 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
Brian Salomon0bbecb22016-11-17 11:38:22 -0500209 SkASSERT(gp.numTextureSamplers() == 1);
Brian Salomondb4183d2016-11-17 12:48:40 -0500210 GrTexture* atlas = gp.textureSampler(0).texture();
joshualitt922c8b12015-08-07 09:55:23 -0700211 SkASSERT(atlas);
212 b->add32(atlas->width());
213 b->add32(atlas->height());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000214 }
215
jvanverth@google.comd830d132013-11-11 20:54:09 +0000216private:
joshualitt5559ca22015-05-21 15:50:36 -0700217 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700218 UniformHandle fViewMatrixUniform;
mtklein50282b42015-01-22 07:59:52 -0800219#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700220 float fDistanceAdjust;
221 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800222#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000223
egdaniele659a582015-11-13 09:55:43 -0800224 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000225};
226
227///////////////////////////////////////////////////////////////////////////////
228
jvanverth502286d2015-04-08 12:37:51 -0700229GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800230 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800231 GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500232 const GrSamplerParams& params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700233#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700234 float distanceAdjust,
jvanverth2d2a68c2014-06-10 06:42:56 -0700235#endif
joshualittb8c241a2015-05-19 08:23:30 -0700236 uint32_t flags,
237 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700238 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700239 , fViewMatrix(viewMatrix)
Brian Salomon0bbecb22016-11-17 11:38:22 -0500240 , fTextureSampler(texture, params)
jvanverth2d2a68c2014-06-10 06:42:56 -0700241#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700242 , fDistanceAdjust(distanceAdjust)
jvanverth2d2a68c2014-06-10 06:42:56 -0700243#endif
joshualitt249af152014-09-15 11:41:13 -0700244 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700245 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700246 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700247 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700248 this->initClassID<GrDistanceFieldA8TextGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700249 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
250 kHigh_GrSLPrecision);
251 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
252 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType,
253 kHigh_GrSLPrecision);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500254 this->addTextureSampler(&fTextureSampler);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000255}
256
egdaniel57d3b032015-11-13 11:57:27 -0800257void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
258 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700259 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800260}
261
egdaniel57d3b032015-11-13 11:57:27 -0800262GrGLSLPrimitiveProcessor* GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700263 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000264}
265
266///////////////////////////////////////////////////////////////////////////////
267
jvanverth502286d2015-04-08 12:37:51 -0700268GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000269
bungeman06ca8ec2016-06-09 08:01:03 -0700270sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700271 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
272 GrProcessorUnitTest::kAlphaTextureIdx;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000273 static const SkShader::TileMode kTileModes[] = {
274 SkShader::kClamp_TileMode,
275 SkShader::kRepeat_TileMode,
276 SkShader::kMirror_TileMode,
277 };
278 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700279 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
280 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverth@google.comd830d132013-11-11 20:54:09 +0000281 };
Brian Salomon514baff2016-11-17 15:17:07 -0500282 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
283 : GrSamplerParams::kNone_FilterMode);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000284
jvanverthcf371bb2016-03-10 11:10:43 -0800285 uint32_t flags = 0;
286 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
287 if (flags & kSimilarity_DistanceFieldEffectFlag) {
288 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
289 }
290
bungeman06ca8ec2016-06-09 08:01:03 -0700291 return GrDistanceFieldA8TextGeoProc::Make(GrRandomColor(d->fRandom),
292 GrTest::TestMatrix(d->fRandom),
293 d->fTextures[texIdx], params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700294#ifdef SK_GAMMA_APPLY_TO_A8
bungeman06ca8ec2016-06-09 08:01:03 -0700295 d->fRandom->nextF(),
jvanverth2d2a68c2014-06-10 06:42:56 -0700296#endif
bungeman06ca8ec2016-06-09 08:01:03 -0700297 flags,
298 d->fRandom->nextBool());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000299}
300
301///////////////////////////////////////////////////////////////////////////////
302
egdaniele659a582015-11-13 09:55:43 -0800303class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700304public:
joshualitt465283c2015-09-11 08:19:35 -0700305 GrGLDistanceFieldPathGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -0700306 : fViewMatrix(SkMatrix::InvalidMatrix())
joshualitt5559ca22015-05-21 15:50:36 -0700307 , fTextureSize(SkISize::Make(-1, -1)) {}
jvanverthfa38a302014-10-06 05:59:05 -0700308
mtklein36352bf2015-03-25 18:17:31 -0700309 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700310 const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700311
cdalton85285412016-02-18 12:37:07 -0800312 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700313
egdaniel4ca2e602015-11-18 08:01:26 -0800314 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800315 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800316 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800317
318 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800319 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -0800320
egdaniel8dcdedc2015-11-11 06:27:20 -0800321 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800322 varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
jvanverthfa38a302014-10-06 05:59:05 -0700323
joshualitt9b989322014-12-15 14:16:27 -0800324 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700325 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800326 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700327 }
egdaniel4ca2e602015-11-18 08:01:26 -0800328 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName);
reede4ef1ca2015-02-17 18:38:38 -0800329
joshualittabb52a12015-01-13 15:02:10 -0800330 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800331 this->setupPosition(vertBuilder,
332 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800333 gpArgs,
334 dfTexEffect.inPosition()->fName,
335 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700336 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800337
338 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800339 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800340 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800341 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800342 gpArgs->fPositionVar,
343 dfTexEffect.inPosition()->fName,
bsalomona624bf32016-09-20 09:12:47 -0700344 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -0800345
halcanary96fcdcc2015-08-27 07:41:13 -0700346 const char* textureSizeUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800347 fTextureSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800348 kVec2f_GrSLType, kDefault_GrSLPrecision,
349 "TextureSize", &textureSizeUniName);
reede4ef1ca2015-02-17 18:38:38 -0800350
jvanverthfdf7ccc2015-01-27 08:19:33 -0800351 // Use highp to work around aliasing issues
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500352 fragBuilder->codeAppendf("highp vec2 uv = %s;", v.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800353
egdaniel4ca2e602015-11-18 08:01:26 -0800354 fragBuilder->codeAppend("float texColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700355 fragBuilder->appendTextureLookup(args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -0800356 "uv",
357 kVec2f_GrSLType);
358 fragBuilder->codeAppend(".r;");
359 fragBuilder->codeAppend("float distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800360 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700361
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500362 fragBuilder->codeAppendf("highp vec2 st = uv*%s;", textureSizeUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800363 fragBuilder->codeAppend("float afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800364 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
365 kUniformScale_DistanceFieldEffectMask;
366 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700367 bool isGammaCorrect =
368 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800369 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700370 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700371 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800372 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700373
jvanverthfa38a302014-10-06 05:59:05 -0700374 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700375#ifdef SK_VULKAN
376 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(st.x));");
377#else
378 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800379 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(st.y));");
jvanverthe499adf2016-07-20 12:22:14 -0700380#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800381 } else if (isSimilarity) {
382 // For similarity transform, we adjust the effect of the transformation on the distance
383 // by using the length of the gradient of the texture coordinates. We use st coordinates
384 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800385
386 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700387#ifdef SK_VULKAN
388 fragBuilder->codeAppend("float st_grad_len = length(dFdx(st));");
389#else
390 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800391 fragBuilder->codeAppend("float st_grad_len = length(dFdy(st));");
jvanverthe499adf2016-07-20 12:22:14 -0700392#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800393 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700394 } else {
jvanverth354eba52015-03-16 11:32:49 -0700395 // For general transforms, to determine the amount of correction we multiply a unit
396 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
397 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800398 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700399 // the length of the gradient may be 0, so we need to check for this
400 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800401 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
402 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
403 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
404 fragBuilder->codeAppend("} else {");
405 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
406 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700407
egdaniel4ca2e602015-11-18 08:01:26 -0800408 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
409 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
410 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
411 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700412
413 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800414 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700415 }
brianosman0e3c5542016-04-13 13:56:21 -0700416 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
417 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
418 // mapped linearly to coverage, so use a linear step:
419 if (isGammaCorrect) {
420 fragBuilder->codeAppend(
421 "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);");
422 } else {
423 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
424 }
jvanverthfa38a302014-10-06 05:59:05 -0700425
egdaniel4ca2e602015-11-18 08:01:26 -0800426 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700427 }
428
bsalomona624bf32016-09-20 09:12:47 -0700429 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
430 FPCoordTransformIter&& transformIter) override {
reede4ef1ca2015-02-17 18:38:38 -0800431 SkASSERT(fTextureSizeUni.isValid());
jvanverthfa38a302014-10-06 05:59:05 -0700432
Brian Salomondb4183d2016-11-17 12:48:40 -0500433 GrTexture* texture = proc.textureSampler(0).texture();
halcanary9d524f22016-03-29 09:03:52 -0700434 if (texture->width() != fTextureSize.width() ||
jvanverthfa38a302014-10-06 05:59:05 -0700435 texture->height() != fTextureSize.height()) {
436 fTextureSize = SkISize::Make(texture->width(), texture->height());
reede4ef1ca2015-02-17 18:38:38 -0800437 pdman.set2f(fTextureSizeUni,
438 SkIntToScalar(fTextureSize.width()),
439 SkIntToScalar(fTextureSize.height()));
jvanverthfa38a302014-10-06 05:59:05 -0700440 }
joshualitt9b989322014-12-15 14:16:27 -0800441
joshualitte578a952015-05-14 10:09:13 -0700442 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700443
444 if (!dfpgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfpgp.viewMatrix())) {
445 fViewMatrix = dfpgp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700446 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800447 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700448 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
449 }
bsalomona624bf32016-09-20 09:12:47 -0700450 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
jvanverthfa38a302014-10-06 05:59:05 -0700451 }
452
robertphillips46d36f02015-01-18 08:14:14 -0800453 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700454 const GrGLSLCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700455 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700456 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700457
joshualitt8fc6c2d2014-12-22 15:27:05 -0800458 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700459 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700460 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800461 b->add32(key);
jvanverthfa38a302014-10-06 05:59:05 -0700462 }
463
464private:
reede4ef1ca2015-02-17 18:38:38 -0800465 UniformHandle fTextureSizeUni;
joshualitt5559ca22015-05-21 15:50:36 -0700466 UniformHandle fViewMatrixUniform;
467 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800468 SkISize fTextureSize;
jvanverthfa38a302014-10-06 05:59:05 -0700469
egdaniele659a582015-11-13 09:55:43 -0800470 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700471};
472
473///////////////////////////////////////////////////////////////////////////////
474
jvanverth502286d2015-04-08 12:37:51 -0700475GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(
joshualitt2e3b3e32014-12-09 13:31:14 -0800476 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800477 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800478 GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500479 const GrSamplerParams& params,
joshualittb8c241a2015-05-19 08:23:30 -0700480 uint32_t flags,
481 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700482 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700483 , fViewMatrix(viewMatrix)
Brian Salomon0bbecb22016-11-17 11:38:22 -0500484 , fTextureSampler(texture, params)
jvanverthfa38a302014-10-06 05:59:05 -0700485 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700486 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700487 , fUsesLocalCoords(usesLocalCoords) {
jvanverthfa38a302014-10-06 05:59:05 -0700488 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700489 this->initClassID<GrDistanceFieldPathGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700490 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
491 kHigh_GrSLPrecision);
492 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
493 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2f_GrVertexAttribType);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500494 this->addTextureSampler(&fTextureSampler);
jvanverthfa38a302014-10-06 05:59:05 -0700495}
496
egdaniel57d3b032015-11-13 11:57:27 -0800497void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
498 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700499 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800500}
501
egdaniel57d3b032015-11-13 11:57:27 -0800502GrGLSLPrimitiveProcessor* GrDistanceFieldPathGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700503 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700504}
505
506///////////////////////////////////////////////////////////////////////////////
507
jvanverth502286d2015-04-08 12:37:51 -0700508GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700509
bungeman06ca8ec2016-06-09 08:01:03 -0700510sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700511 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
512 : GrProcessorUnitTest::kAlphaTextureIdx;
jvanverthfa38a302014-10-06 05:59:05 -0700513 static const SkShader::TileMode kTileModes[] = {
514 SkShader::kClamp_TileMode,
515 SkShader::kRepeat_TileMode,
516 SkShader::kMirror_TileMode,
517 };
518 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700519 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
520 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverthfa38a302014-10-06 05:59:05 -0700521 };
Brian Salomon514baff2016-11-17 15:17:07 -0500522 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
523 : GrSamplerParams::kNone_FilterMode);
jvanverthfa38a302014-10-06 05:59:05 -0700524
jvanverthcf371bb2016-03-10 11:10:43 -0800525 uint32_t flags = 0;
526 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
527 if (flags & kSimilarity_DistanceFieldEffectFlag) {
528 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
529 }
530
bungeman06ca8ec2016-06-09 08:01:03 -0700531 return GrDistanceFieldPathGeoProc::Make(GrRandomColor(d->fRandom),
532 GrTest::TestMatrix(d->fRandom),
533 d->fTextures[texIdx],
534 params,
535 flags,
536 d->fRandom->nextBool());
jvanverthfa38a302014-10-06 05:59:05 -0700537}
538
539///////////////////////////////////////////////////////////////////////////////
540
egdaniele659a582015-11-13 09:55:43 -0800541class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000542public:
joshualitt465283c2015-09-11 08:19:35 -0700543 GrGLDistanceFieldLCDTextGeoProc()
joshualitt53f26aa2015-12-10 07:29:54 -0800544 : fViewMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700545 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700546 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000547
mtklein36352bf2015-03-25 18:17:31 -0700548 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700549 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
550 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000551
egdaniel4ca2e602015-11-18 08:01:26 -0800552 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800553 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800554 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800555
556 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800557 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800558
cdalton85285412016-02-18 12:37:07 -0800559 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800560
joshualitt9b989322014-12-15 14:16:27 -0800561 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700562 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800563 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700564 }
joshualitt9b989322014-12-15 14:16:27 -0800565
joshualittabb52a12015-01-13 15:02:10 -0800566 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800567 this->setupPosition(vertBuilder,
568 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800569 gpArgs,
570 dfTexEffect.inPosition()->fName,
571 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700572 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800573
joshualittabb52a12015-01-13 15:02:10 -0800574 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800575 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800576 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800577 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800578 gpArgs->fPositionVar,
579 dfTexEffect.inPosition()->fName,
bsalomona624bf32016-09-20 09:12:47 -0700580 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800581
jvanverthbb4a1cf2015-04-07 09:06:00 -0700582 // set up varyings
jvanverthcf371bb2016-03-10 11:10:43 -0800583 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
584 kUniformScale_DistanceFieldEffectMask;
585 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700586 bool isGammaCorrect =
587 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
egdaniel8dcdedc2015-11-11 06:27:20 -0800588 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -0800589 GrGLSLVertToFrag uv(kVec2f_GrSLType);
590 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
591 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700592
jvanverthcf371bb2016-03-10 11:10:43 -0800593 // compute numbers to be hardcoded to convert texture coordinates from float to int
Brian Salomon0bbecb22016-11-17 11:38:22 -0500594 SkASSERT(dfTexEffect.numTextureSamplers() == 1);
Brian Salomondb4183d2016-11-17 12:48:40 -0500595 GrTexture* atlas = dfTexEffect.textureSampler(0).texture();
joshualitt7375d6b2015-08-07 13:36:44 -0700596 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -0700597
jvanverth7023a002016-02-22 11:25:32 -0800598 GrGLSLVertToFrag st(kVec2f_GrSLType);
599 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
600 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
601 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -0800602 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700603
604 // add frag shader code
joshualitt30ba4362014-08-21 20:18:45 -0700605
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000606 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800607 // Use highp to work around aliasing issues
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500608 fragBuilder->codeAppendf("highp vec2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700609
610 SkScalar lcdDelta = 1.0f / (3.0f * atlas->width());
jvanverth5a105ff2015-02-18 11:36:35 -0800611 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500612 fragBuilder->codeAppendf("highp float delta = -%.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800613 } else {
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500614 fragBuilder->codeAppendf("highp float delta = %.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800615 }
jvanverth78f07182014-07-30 06:17:59 -0700616 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700617#ifdef SK_VULKAN
618 fragBuilder->codeAppendf("float st_grad_len = abs(dFdx(%s.x));", st.fsIn());
619#else
620 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800621 fragBuilder->codeAppendf("float st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700622#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800623 fragBuilder->codeAppend("vec2 offset = vec2(st_grad_len*delta, 0.0);");
624 } else if (isSimilarity) {
625 // For a similarity matrix with rotation, the gradient will not be aligned
626 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700627#ifdef SK_VULKAN
628 fragBuilder->codeAppendf("vec2 st_grad = dFdx(%s);", st.fsIn());
629 fragBuilder->codeAppend("vec2 offset = delta*st_grad;");
630#else
jvanverthcf371bb2016-03-10 11:10:43 -0800631 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
632 // get the gradient in the x direction.
633 fragBuilder->codeAppendf("vec2 st_grad = dFdy(%s);", st.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800634 fragBuilder->codeAppend("vec2 offset = delta*vec2(st_grad.y, -st_grad.x);");
jvanverthe499adf2016-07-20 12:22:14 -0700635#endif
636 fragBuilder->codeAppend("float st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000637 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800638 fragBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700639
egdaniel4ca2e602015-11-18 08:01:26 -0800640 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
641 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
642 fragBuilder->codeAppend("vec2 offset = delta*Jdx;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000643 }
644
645 // green is distance to uv center
egdaniel4ca2e602015-11-18 08:01:26 -0800646 fragBuilder->codeAppend("\tvec4 texColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700647 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800648 fragBuilder->codeAppend(";\n");
649 fragBuilder->codeAppend("\tvec3 distance;\n");
650 fragBuilder->codeAppend("\tdistance.y = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000651 // red is distance to left offset
egdaniel4ca2e602015-11-18 08:01:26 -0800652 fragBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n");
653 fragBuilder->codeAppend("\ttexColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700654 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800655 fragBuilder->codeAppend(";\n");
656 fragBuilder->codeAppend("\tdistance.x = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000657 // blue is distance to right offset
egdaniel4ca2e602015-11-18 08:01:26 -0800658 fragBuilder->codeAppend("\tuv_adjusted = uv + offset;\n");
659 fragBuilder->codeAppend("\ttexColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700660 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800661 fragBuilder->codeAppend(";\n");
662 fragBuilder->codeAppend("\tdistance.z = texColor.r;\n");
jvanverth2d2a68c2014-06-10 06:42:56 -0700663
egdaniel4ca2e602015-11-18 08:01:26 -0800664 fragBuilder->codeAppend("\tdistance = "
jvanverthada68ef2014-11-03 14:00:24 -0800665 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700666
jvanverth21deace2015-04-01 12:43:48 -0700667 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700668 const char* distanceAdjustUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800669 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800670 kVec3f_GrSLType, kDefault_GrSLPrecision,
671 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800672 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700673
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000674 // To be strictly correct, we should compute the anti-aliasing factor separately
675 // for each color component. However, this is only important when using perspective
676 // transformations, and even then using a single factor seems like a reasonable
677 // trade-off between quality and speed.
egdaniel4ca2e602015-11-18 08:01:26 -0800678 fragBuilder->codeAppend("float afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800679 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700680 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
681 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800682 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
683 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700684
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000685 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800686 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000687 } else {
jvanverth354eba52015-03-16 11:32:49 -0700688 // For general transforms, to determine the amount of correction we multiply a unit
689 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
690 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800691 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700692 // the length of the gradient may be 0, so we need to check for this
693 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800694 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
695 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
696 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
697 fragBuilder->codeAppend("} else {");
698 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
699 fragBuilder->codeAppend("}");
700 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
701 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000702
703 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800704 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000705 }
706
brianosman0586f5c2016-04-12 12:48:21 -0700707 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
708 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
709 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700710 if (isGammaCorrect) {
brianosman0586f5c2016-04-12 12:48:21 -0700711 fragBuilder->codeAppend("vec4 val = "
bsalomon32d1e952016-09-15 07:29:52 -0700712 "vec4(clamp(distance + vec3(afwidth) / vec3(2.0 * afwidth), 0.0, 1.0), 1.0);");
brianosman0586f5c2016-04-12 12:48:21 -0700713 } else {
714 fragBuilder->codeAppend(
715 "vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);");
716 }
717
egdaniel27b63352015-09-15 13:13:50 -0700718 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -0800719 fragBuilder->codeAppend("val.a = max(max(val.r, val.g), val.b);");
jvanverth2d2a68c2014-06-10 06:42:56 -0700720
egdaniel4ca2e602015-11-18 08:01:26 -0800721 fragBuilder->codeAppendf("%s = val;", args.fOutputCoverage);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000722 }
723
bsalomona624bf32016-09-20 09:12:47 -0700724 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
725 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700726 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000727
joshualitt5559ca22015-05-21 15:50:36 -0700728 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
729 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700730 if (wa != fDistanceAdjust) {
731 pdman.set3f(fDistanceAdjustUni,
732 wa.fR,
733 wa.fG,
734 wa.fB);
735 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700736 }
joshualitt9b989322014-12-15 14:16:27 -0800737
joshualitt5559ca22015-05-21 15:50:36 -0700738 if (!dflcd.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dflcd.viewMatrix())) {
739 fViewMatrix = dflcd.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700740 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800741 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700742 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
743 }
bsalomona624bf32016-09-20 09:12:47 -0700744 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000745 }
746
robertphillips46d36f02015-01-18 08:14:14 -0800747 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700748 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700749 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700750 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000751
joshualitt8fc6c2d2014-12-22 15:27:05 -0800752 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700753 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700754 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800755 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700756
757 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
Brian Salomon0bbecb22016-11-17 11:38:22 -0500758 SkASSERT(gp.numTextureSamplers() == 1);
Brian Salomondb4183d2016-11-17 12:48:40 -0500759 GrTexture* atlas = gp.textureSampler(0).texture();
joshualitt922c8b12015-08-07 09:55:23 -0700760 SkASSERT(atlas);
761 b->add32(atlas->width());
762 b->add32(atlas->height());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000763 }
764
765private:
joshualitt5559ca22015-05-21 15:50:36 -0700766 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700767 UniformHandle fViewMatrixUniform;
jvanverth21deace2015-04-01 12:43:48 -0700768 UniformHandle fColorUniform;
jvanverth502286d2015-04-08 12:37:51 -0700769 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
jvanverth21deace2015-04-01 12:43:48 -0700770 UniformHandle fDistanceAdjustUni;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000771
egdaniele659a582015-11-13 09:55:43 -0800772 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000773};
774
775///////////////////////////////////////////////////////////////////////////////
776
jvanverth502286d2015-04-08 12:37:51 -0700777GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(
joshualitt8059eb92014-12-29 15:10:07 -0800778 GrColor color, const SkMatrix& viewMatrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500779 GrTexture* texture, const GrSamplerParams& params,
jvanverth21deace2015-04-01 12:43:48 -0700780 DistanceAdjust distanceAdjust,
joshualittb8c241a2015-05-19 08:23:30 -0700781 uint32_t flags, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700782 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700783 , fViewMatrix(viewMatrix)
Brian Salomon0bbecb22016-11-17 11:38:22 -0500784 , fTextureSampler(texture, params)
jvanverth21deace2015-04-01 12:43:48 -0700785 , fDistanceAdjust(distanceAdjust)
joshualittb8c241a2015-05-19 08:23:30 -0700786 , fFlags(flags & kLCD_DistanceFieldEffectMask)
787 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700788 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
jvanverth502286d2015-04-08 12:37:51 -0700789 this->initClassID<GrDistanceFieldLCDTextGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700790 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
791 kHigh_GrSLPrecision);
792 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
793 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType,
794 kHigh_GrSLPrecision);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500795 this->addTextureSampler(&fTextureSampler);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000796}
797
egdaniel57d3b032015-11-13 11:57:27 -0800798void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
799 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700800 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800801}
802
egdaniel57d3b032015-11-13 11:57:27 -0800803GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700804 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000805}
806
807///////////////////////////////////////////////////////////////////////////////
808
jvanverth502286d2015-04-08 12:37:51 -0700809GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000810
bungeman06ca8ec2016-06-09 08:01:03 -0700811sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700812 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
813 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000814 static const SkShader::TileMode kTileModes[] = {
815 SkShader::kClamp_TileMode,
816 SkShader::kRepeat_TileMode,
817 SkShader::kMirror_TileMode,
818 };
819 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700820 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
821 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000822 };
Brian Salomon514baff2016-11-17 15:17:07 -0500823 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
824 : GrSamplerParams::kNone_FilterMode);
jvanverth21deace2015-04-01 12:43:48 -0700825 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700826 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800827 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
828 if (flags & kSimilarity_DistanceFieldEffectFlag) {
829 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
830 }
joshualitt0067ff52015-07-08 14:26:19 -0700831 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
bungeman06ca8ec2016-06-09 08:01:03 -0700832 return GrDistanceFieldLCDTextGeoProc::Make(GrRandomColor(d->fRandom),
833 GrTest::TestMatrix(d->fRandom),
834 d->fTextures[texIdx], params,
835 wa,
836 flags,
837 d->fRandom->nextBool());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000838}