blob: c74c5cec8048d31362d11ff00f33f018471cebe0 [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;
egdaniel4ca2e602015-11-18 08:01:26 -080038 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)
cdalton5e58cee2016-02-11 12:49:47 -080052 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080053 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);
jvanverth7023a002016-02-22 11:25:32 -080081 GrGLSLVertToFrag uv(kVec2f_GrSLType);
jvanverthcf371bb2016-03-10 11:10:43 -080082 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
83 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070084 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070085 bool isGammaCorrect =
86 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverth7023a002016-02-22 11:25:32 -080087 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
88 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070089
jvanverthcf371bb2016-03-10 11:10:43 -080090 // compute numbers to be hardcoded to convert texture coordinates from float to int
joshualitt922c8b12015-08-07 09:55:23 -070091 SkASSERT(dfTexEffect.numTextures() == 1);
92 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070093 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070094
jvanverth7023a002016-02-22 11:25:32 -080095 GrGLSLVertToFrag st(kVec2f_GrSLType);
96 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
97 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
98 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -080099 dfTexEffect.inTextureCoords()->fName);
halcanary9d524f22016-03-29 09:03:52 -0700100
jvanverthfdf7ccc2015-01-27 08:19:33 -0800101 // Use highp to work around aliasing issues
cdalton1f50acf2016-04-11 11:30:50 -0700102 fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800103 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 = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700106 fragBuilder->appendTextureLookup(args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -0800107 "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;");
jvanverthcf371bb2016-03-10 11:10:43 -0800118 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700119 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700120 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800121 // 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 +0000122
jvanverth354eba52015-03-16 11:32:49 -0700123 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700124#ifdef SK_VULKAN
125 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
126 st.fsIn());
127#else
128 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800129 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
130 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700131#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800132 } else if (isSimilarity) {
133 // For similarity transform, we adjust the effect of the transformation on the distance
134 // by using the length of the gradient of the texture coordinates. We use st coordinates
135 // to ensure we're mapping 1:1 from texel space to pixel space.
136 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
137
138 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700139#ifdef SK_VULKAN
140 fragBuilder->codeAppendf("float st_grad_len = length(dFdx(%s));", st.fsIn());
141#else
142 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800143 fragBuilder->codeAppendf("float st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700144#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800145 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700146 } else {
147 // For general transforms, to determine the amount of correction we multiply a unit
148 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
149 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800150 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700151 // the length of the gradient may be 0, so we need to check for this
152 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800153 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
154 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
155 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
156 fragBuilder->codeAppend("} else {");
157 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
158 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700159
egdaniel4ca2e602015-11-18 08:01:26 -0800160 fragBuilder->codeAppendf("vec2 Jdx = dFdx(%s);", st.fsIn());
161 fragBuilder->codeAppendf("vec2 Jdy = dFdy(%s);", st.fsIn());
162 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
163 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000164
165 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800166 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000167 }
brianosman0586f5c2016-04-12 12:48:21 -0700168
169 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
170 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
171 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700172 if (isGammaCorrect) {
brianosman0586f5c2016-04-12 12:48:21 -0700173 fragBuilder->codeAppend(
174 "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);");
175 } else {
176 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
177 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000178
egdaniel4ca2e602015-11-18 08:01:26 -0800179 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000180 }
181
egdaniel018fb622015-10-28 07:26:40 -0700182 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
jvanverth2d2a68c2014-06-10 06:42:56 -0700183#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth502286d2015-04-08 12:37:51 -0700184 const GrDistanceFieldA8TextGeoProc& dfTexEffect = proc.cast<GrDistanceFieldA8TextGeoProc>();
jvanverth21deace2015-04-01 12:43:48 -0700185 float distanceAdjust = dfTexEffect.getDistanceAdjust();
186 if (distanceAdjust != fDistanceAdjust) {
187 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
188 fDistanceAdjust = distanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700189 }
190#endif
joshualitte578a952015-05-14 10:09:13 -0700191 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700192
193 if (!dfa8gp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfa8gp.viewMatrix())) {
194 fViewMatrix = dfa8gp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700195 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800196 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700197 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
198 }
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000199 }
200
robertphillips46d36f02015-01-18 08:14:14 -0800201 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700202 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700203 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700204 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800205 uint32_t key = dfTexEffect.getFlags();
joshualitt53f26aa2015-12-10 07:29:54 -0800206 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700207 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800208 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700209
210 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
211 SkASSERT(gp.numTextures() == 1);
212 GrTexture* atlas = gp.textureAccess(0).getTexture();
213 SkASSERT(atlas);
214 b->add32(atlas->width());
215 b->add32(atlas->height());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000216 }
217
jvanverth@google.comd830d132013-11-11 20:54:09 +0000218private:
joshualitt5559ca22015-05-21 15:50:36 -0700219 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700220 UniformHandle fViewMatrixUniform;
mtklein50282b42015-01-22 07:59:52 -0800221#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700222 float fDistanceAdjust;
223 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800224#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000225
egdaniele659a582015-11-13 09:55:43 -0800226 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000227};
228
229///////////////////////////////////////////////////////////////////////////////
230
jvanverth502286d2015-04-08 12:37:51 -0700231GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800232 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800233 GrTexture* texture,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000234 const GrTextureParams& params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700235#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700236 float distanceAdjust,
jvanverth2d2a68c2014-06-10 06:42:56 -0700237#endif
joshualittb8c241a2015-05-19 08:23:30 -0700238 uint32_t flags,
239 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700240 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700241 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800242 , fTextureAccess(texture, params)
jvanverth2d2a68c2014-06-10 06:42:56 -0700243#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700244 , fDistanceAdjust(distanceAdjust)
jvanverth2d2a68c2014-06-10 06:42:56 -0700245#endif
joshualitt249af152014-09-15 11:41:13 -0700246 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700247 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700248 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700249 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700250 this->initClassID<GrDistanceFieldA8TextGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700251 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
252 kHigh_GrSLPrecision);
253 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
254 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType,
255 kHigh_GrSLPrecision);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000256 this->addTextureAccess(&fTextureAccess);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000257}
258
egdaniel57d3b032015-11-13 11:57:27 -0800259void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
260 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700261 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800262}
263
egdaniel57d3b032015-11-13 11:57:27 -0800264GrGLSLPrimitiveProcessor* GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700265 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000266}
267
268///////////////////////////////////////////////////////////////////////////////
269
jvanverth502286d2015-04-08 12:37:51 -0700270GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000271
bungeman06ca8ec2016-06-09 08:01:03 -0700272sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700273 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
274 GrProcessorUnitTest::kAlphaTextureIdx;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000275 static const SkShader::TileMode kTileModes[] = {
276 SkShader::kClamp_TileMode,
277 SkShader::kRepeat_TileMode,
278 SkShader::kMirror_TileMode,
279 };
280 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700281 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
282 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverth@google.comd830d132013-11-11 20:54:09 +0000283 };
joshualitt0067ff52015-07-08 14:26:19 -0700284 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
jvanverth@google.comd830d132013-11-11 20:54:09 +0000285 GrTextureParams::kNone_FilterMode);
286
jvanverthcf371bb2016-03-10 11:10:43 -0800287 uint32_t flags = 0;
288 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
289 if (flags & kSimilarity_DistanceFieldEffectFlag) {
290 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
291 }
292
bungeman06ca8ec2016-06-09 08:01:03 -0700293 return GrDistanceFieldA8TextGeoProc::Make(GrRandomColor(d->fRandom),
294 GrTest::TestMatrix(d->fRandom),
295 d->fTextures[texIdx], params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700296#ifdef SK_GAMMA_APPLY_TO_A8
bungeman06ca8ec2016-06-09 08:01:03 -0700297 d->fRandom->nextF(),
jvanverth2d2a68c2014-06-10 06:42:56 -0700298#endif
bungeman06ca8ec2016-06-09 08:01:03 -0700299 flags,
300 d->fRandom->nextBool());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000301}
302
303///////////////////////////////////////////////////////////////////////////////
304
egdaniele659a582015-11-13 09:55:43 -0800305class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700306public:
joshualitt465283c2015-09-11 08:19:35 -0700307 GrGLDistanceFieldPathGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -0700308 : fViewMatrix(SkMatrix::InvalidMatrix())
joshualitt5559ca22015-05-21 15:50:36 -0700309 , fTextureSize(SkISize::Make(-1, -1)) {}
jvanverthfa38a302014-10-06 05:59:05 -0700310
mtklein36352bf2015-03-25 18:17:31 -0700311 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700312 const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700313
cdalton85285412016-02-18 12:37:07 -0800314 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800315 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800316 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverthfa38a302014-10-06 05:59:05 -0700317
egdaniel4ca2e602015-11-18 08:01:26 -0800318 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800319 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800320 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800321
322 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800323 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -0800324
egdaniel8dcdedc2015-11-11 06:27:20 -0800325 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800326 varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
jvanverthfa38a302014-10-06 05:59:05 -0700327
joshualitt9b989322014-12-15 14:16:27 -0800328 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700329 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800330 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700331 }
egdaniel4ca2e602015-11-18 08:01:26 -0800332 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName);
reede4ef1ca2015-02-17 18:38:38 -0800333
joshualittabb52a12015-01-13 15:02:10 -0800334 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800335 this->setupPosition(vertBuilder,
336 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800337 gpArgs,
338 dfTexEffect.inPosition()->fName,
339 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700340 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800341
342 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800343 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800344 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800345 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800346 gpArgs->fPositionVar,
347 dfTexEffect.inPosition()->fName,
348 args.fTransformsIn,
349 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800350
halcanary96fcdcc2015-08-27 07:41:13 -0700351 const char* textureSizeUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800352 fTextureSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800353 kVec2f_GrSLType, kDefault_GrSLPrecision,
354 "TextureSize", &textureSizeUniName);
reede4ef1ca2015-02-17 18:38:38 -0800355
jvanverthfdf7ccc2015-01-27 08:19:33 -0800356 // Use highp to work around aliasing issues
cdalton1f50acf2016-04-11 11:30:50 -0700357 fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800358 fragBuilder->codeAppendf("vec2 uv = %s;", v.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800359
egdaniel4ca2e602015-11-18 08:01:26 -0800360 fragBuilder->codeAppend("float texColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700361 fragBuilder->appendTextureLookup(args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -0800362 "uv",
363 kVec2f_GrSLType);
364 fragBuilder->codeAppend(".r;");
365 fragBuilder->codeAppend("float distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800366 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700367
cdalton1f50acf2016-04-11 11:30:50 -0700368 fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800369 fragBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName);
370 fragBuilder->codeAppend("float afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800371 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
372 kUniformScale_DistanceFieldEffectMask;
373 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700374 bool isGammaCorrect =
375 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800376 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700377 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700378 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800379 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700380
jvanverthfa38a302014-10-06 05:59:05 -0700381 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700382#ifdef SK_VULKAN
383 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(st.x));");
384#else
385 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800386 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(st.y));");
jvanverthe499adf2016-07-20 12:22:14 -0700387#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800388 } else if (isSimilarity) {
389 // For similarity transform, we adjust the effect of the transformation on the distance
390 // by using the length of the gradient of the texture coordinates. We use st coordinates
391 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800392
393 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700394#ifdef SK_VULKAN
395 fragBuilder->codeAppend("float st_grad_len = length(dFdx(st));");
396#else
397 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800398 fragBuilder->codeAppend("float st_grad_len = length(dFdy(st));");
jvanverthe499adf2016-07-20 12:22:14 -0700399#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800400 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700401 } else {
jvanverth354eba52015-03-16 11:32:49 -0700402 // For general transforms, to determine the amount of correction we multiply a unit
403 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
404 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800405 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700406 // the length of the gradient may be 0, so we need to check for this
407 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800408 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
409 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
410 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
411 fragBuilder->codeAppend("} else {");
412 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
413 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700414
egdaniel4ca2e602015-11-18 08:01:26 -0800415 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
416 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
417 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
418 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700419
420 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800421 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700422 }
brianosman0e3c5542016-04-13 13:56:21 -0700423 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
424 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
425 // mapped linearly to coverage, so use a linear step:
426 if (isGammaCorrect) {
427 fragBuilder->codeAppend(
428 "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);");
429 } else {
430 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
431 }
jvanverthfa38a302014-10-06 05:59:05 -0700432
egdaniel4ca2e602015-11-18 08:01:26 -0800433 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700434 }
435
egdaniel018fb622015-10-28 07:26:40 -0700436 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
reede4ef1ca2015-02-17 18:38:38 -0800437 SkASSERT(fTextureSizeUni.isValid());
jvanverthfa38a302014-10-06 05:59:05 -0700438
joshualitt87f48d92014-12-04 10:41:40 -0800439 GrTexture* texture = proc.texture(0);
halcanary9d524f22016-03-29 09:03:52 -0700440 if (texture->width() != fTextureSize.width() ||
jvanverthfa38a302014-10-06 05:59:05 -0700441 texture->height() != fTextureSize.height()) {
442 fTextureSize = SkISize::Make(texture->width(), texture->height());
reede4ef1ca2015-02-17 18:38:38 -0800443 pdman.set2f(fTextureSizeUni,
444 SkIntToScalar(fTextureSize.width()),
445 SkIntToScalar(fTextureSize.height()));
jvanverthfa38a302014-10-06 05:59:05 -0700446 }
joshualitt9b989322014-12-15 14:16:27 -0800447
joshualitte578a952015-05-14 10:09:13 -0700448 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700449
450 if (!dfpgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfpgp.viewMatrix())) {
451 fViewMatrix = dfpgp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700452 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800453 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700454 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
455 }
jvanverthfa38a302014-10-06 05:59:05 -0700456 }
457
robertphillips46d36f02015-01-18 08:14:14 -0800458 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700459 const GrGLSLCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700460 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700461 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700462
joshualitt8fc6c2d2014-12-22 15:27:05 -0800463 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700464 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700465 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800466 b->add32(key);
jvanverthfa38a302014-10-06 05:59:05 -0700467 }
468
469private:
reede4ef1ca2015-02-17 18:38:38 -0800470 UniformHandle fTextureSizeUni;
joshualitt5559ca22015-05-21 15:50:36 -0700471 UniformHandle fViewMatrixUniform;
472 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800473 SkISize fTextureSize;
jvanverthfa38a302014-10-06 05:59:05 -0700474
egdaniele659a582015-11-13 09:55:43 -0800475 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700476};
477
478///////////////////////////////////////////////////////////////////////////////
479
jvanverth502286d2015-04-08 12:37:51 -0700480GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(
joshualitt2e3b3e32014-12-09 13:31:14 -0800481 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800482 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800483 GrTexture* texture,
484 const GrTextureParams& params,
joshualittb8c241a2015-05-19 08:23:30 -0700485 uint32_t flags,
486 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700487 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700488 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800489 , fTextureAccess(texture, params)
jvanverthfa38a302014-10-06 05:59:05 -0700490 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700491 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700492 , fUsesLocalCoords(usesLocalCoords) {
jvanverthfa38a302014-10-06 05:59:05 -0700493 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700494 this->initClassID<GrDistanceFieldPathGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700495 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
496 kHigh_GrSLPrecision);
497 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
498 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2f_GrVertexAttribType);
jvanverthfa38a302014-10-06 05:59:05 -0700499 this->addTextureAccess(&fTextureAccess);
500}
501
egdaniel57d3b032015-11-13 11:57:27 -0800502void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
503 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700504 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800505}
506
egdaniel57d3b032015-11-13 11:57:27 -0800507GrGLSLPrimitiveProcessor* GrDistanceFieldPathGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700508 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700509}
510
511///////////////////////////////////////////////////////////////////////////////
512
jvanverth502286d2015-04-08 12:37:51 -0700513GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700514
bungeman06ca8ec2016-06-09 08:01:03 -0700515sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700516 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
517 : GrProcessorUnitTest::kAlphaTextureIdx;
jvanverthfa38a302014-10-06 05:59:05 -0700518 static const SkShader::TileMode kTileModes[] = {
519 SkShader::kClamp_TileMode,
520 SkShader::kRepeat_TileMode,
521 SkShader::kMirror_TileMode,
522 };
523 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700524 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
525 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverthfa38a302014-10-06 05:59:05 -0700526 };
joshualitt0067ff52015-07-08 14:26:19 -0700527 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode
528 : GrTextureParams::kNone_FilterMode);
jvanverthfa38a302014-10-06 05:59:05 -0700529
jvanverthcf371bb2016-03-10 11:10:43 -0800530 uint32_t flags = 0;
531 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
532 if (flags & kSimilarity_DistanceFieldEffectFlag) {
533 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
534 }
535
bungeman06ca8ec2016-06-09 08:01:03 -0700536 return GrDistanceFieldPathGeoProc::Make(GrRandomColor(d->fRandom),
537 GrTest::TestMatrix(d->fRandom),
538 d->fTextures[texIdx],
539 params,
540 flags,
541 d->fRandom->nextBool());
jvanverthfa38a302014-10-06 05:59:05 -0700542}
543
544///////////////////////////////////////////////////////////////////////////////
545
egdaniele659a582015-11-13 09:55:43 -0800546class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000547public:
joshualitt465283c2015-09-11 08:19:35 -0700548 GrGLDistanceFieldLCDTextGeoProc()
joshualitt53f26aa2015-12-10 07:29:54 -0800549 : fViewMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700550 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700551 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000552
mtklein36352bf2015-03-25 18:17:31 -0700553 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700554 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
555 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000556
egdaniel4ca2e602015-11-18 08:01:26 -0800557 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800558 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800559 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800560
561 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800562 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800563
cdalton85285412016-02-18 12:37:07 -0800564 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800565
joshualitt9b989322014-12-15 14:16:27 -0800566 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700567 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800568 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700569 }
joshualitt9b989322014-12-15 14:16:27 -0800570
joshualittabb52a12015-01-13 15:02:10 -0800571 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800572 this->setupPosition(vertBuilder,
573 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800574 gpArgs,
575 dfTexEffect.inPosition()->fName,
576 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700577 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800578
joshualittabb52a12015-01-13 15:02:10 -0800579 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800580 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800581 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800582 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800583 gpArgs->fPositionVar,
584 dfTexEffect.inPosition()->fName,
585 args.fTransformsIn,
586 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800587
jvanverthbb4a1cf2015-04-07 09:06:00 -0700588 // set up varyings
jvanverthcf371bb2016-03-10 11:10:43 -0800589 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
590 kUniformScale_DistanceFieldEffectMask;
591 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700592 bool isGammaCorrect =
593 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
egdaniel8dcdedc2015-11-11 06:27:20 -0800594 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -0800595 GrGLSLVertToFrag uv(kVec2f_GrSLType);
596 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
597 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700598
jvanverthcf371bb2016-03-10 11:10:43 -0800599 // compute numbers to be hardcoded to convert texture coordinates from float to int
joshualitt922c8b12015-08-07 09:55:23 -0700600 SkASSERT(dfTexEffect.numTextures() == 1);
601 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -0700602 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -0700603
jvanverth7023a002016-02-22 11:25:32 -0800604 GrGLSLVertToFrag st(kVec2f_GrSLType);
605 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
606 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
607 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -0800608 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700609
610 // add frag shader code
joshualitt30ba4362014-08-21 20:18:45 -0700611
egdaniel4ca2e602015-11-18 08:01:26 -0800612 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800613 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt30ba4362014-08-21 20:18:45 -0700614
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000615 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800616 // Use highp to work around aliasing issues
cdalton1f50acf2016-04-11 11:30:50 -0700617 fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800618 fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn());
cdalton1f50acf2016-04-11 11:30:50 -0700619 fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
joshualitt922c8b12015-08-07 09:55:23 -0700620
621 SkScalar lcdDelta = 1.0f / (3.0f * atlas->width());
jvanverth5a105ff2015-02-18 11:36:35 -0800622 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
egdaniel4ca2e602015-11-18 08:01:26 -0800623 fragBuilder->codeAppendf("float delta = -%.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800624 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800625 fragBuilder->codeAppendf("float delta = %.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800626 }
jvanverth78f07182014-07-30 06:17:59 -0700627 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700628#ifdef SK_VULKAN
629 fragBuilder->codeAppendf("float st_grad_len = abs(dFdx(%s.x));", st.fsIn());
630#else
631 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800632 fragBuilder->codeAppendf("float st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700633#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800634 fragBuilder->codeAppend("vec2 offset = vec2(st_grad_len*delta, 0.0);");
635 } else if (isSimilarity) {
636 // For a similarity matrix with rotation, the gradient will not be aligned
637 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700638#ifdef SK_VULKAN
639 fragBuilder->codeAppendf("vec2 st_grad = dFdx(%s);", st.fsIn());
640 fragBuilder->codeAppend("vec2 offset = delta*st_grad;");
641#else
jvanverthcf371bb2016-03-10 11:10:43 -0800642 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
643 // get the gradient in the x direction.
644 fragBuilder->codeAppendf("vec2 st_grad = dFdy(%s);", st.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800645 fragBuilder->codeAppend("vec2 offset = delta*vec2(st_grad.y, -st_grad.x);");
jvanverthe499adf2016-07-20 12:22:14 -0700646#endif
647 fragBuilder->codeAppend("float st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000648 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800649 fragBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700650
egdaniel4ca2e602015-11-18 08:01:26 -0800651 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
652 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
653 fragBuilder->codeAppend("vec2 offset = delta*Jdx;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000654 }
655
656 // green is distance to uv center
egdaniel4ca2e602015-11-18 08:01:26 -0800657 fragBuilder->codeAppend("\tvec4 texColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700658 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800659 fragBuilder->codeAppend(";\n");
660 fragBuilder->codeAppend("\tvec3 distance;\n");
661 fragBuilder->codeAppend("\tdistance.y = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000662 // red is distance to left offset
egdaniel4ca2e602015-11-18 08:01:26 -0800663 fragBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n");
664 fragBuilder->codeAppend("\ttexColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700665 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800666 fragBuilder->codeAppend(";\n");
667 fragBuilder->codeAppend("\tdistance.x = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000668 // blue is distance to right offset
egdaniel4ca2e602015-11-18 08:01:26 -0800669 fragBuilder->codeAppend("\tuv_adjusted = uv + offset;\n");
670 fragBuilder->codeAppend("\ttexColor = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700671 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -0800672 fragBuilder->codeAppend(";\n");
673 fragBuilder->codeAppend("\tdistance.z = texColor.r;\n");
jvanverth2d2a68c2014-06-10 06:42:56 -0700674
egdaniel4ca2e602015-11-18 08:01:26 -0800675 fragBuilder->codeAppend("\tdistance = "
jvanverthada68ef2014-11-03 14:00:24 -0800676 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700677
jvanverth21deace2015-04-01 12:43:48 -0700678 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700679 const char* distanceAdjustUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800680 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800681 kVec3f_GrSLType, kDefault_GrSLPrecision,
682 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800683 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700684
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000685 // To be strictly correct, we should compute the anti-aliasing factor separately
686 // for each color component. However, this is only important when using perspective
687 // transformations, and even then using a single factor seems like a reasonable
688 // trade-off between quality and speed.
egdaniel4ca2e602015-11-18 08:01:26 -0800689 fragBuilder->codeAppend("float afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800690 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700691 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
692 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800693 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
694 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700695
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000696 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800697 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000698 } else {
jvanverth354eba52015-03-16 11:32:49 -0700699 // For general transforms, to determine the amount of correction we multiply a unit
700 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
701 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800702 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700703 // the length of the gradient may be 0, so we need to check for this
704 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800705 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
706 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
707 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
708 fragBuilder->codeAppend("} else {");
709 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
710 fragBuilder->codeAppend("}");
711 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
712 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000713
714 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800715 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000716 }
717
brianosman0586f5c2016-04-12 12:48:21 -0700718 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
719 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
720 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700721 if (isGammaCorrect) {
brianosman0586f5c2016-04-12 12:48:21 -0700722 fragBuilder->codeAppend("vec4 val = "
bsalomon32d1e952016-09-15 07:29:52 -0700723 "vec4(clamp(distance + vec3(afwidth) / vec3(2.0 * afwidth), 0.0, 1.0), 1.0);");
brianosman0586f5c2016-04-12 12:48:21 -0700724 } else {
725 fragBuilder->codeAppend(
726 "vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);");
727 }
728
egdaniel27b63352015-09-15 13:13:50 -0700729 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -0800730 fragBuilder->codeAppend("val.a = max(max(val.r, val.g), val.b);");
jvanverth2d2a68c2014-06-10 06:42:56 -0700731
egdaniel4ca2e602015-11-18 08:01:26 -0800732 fragBuilder->codeAppendf("%s = val;", args.fOutputCoverage);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000733 }
734
egdaniel018fb622015-10-28 07:26:40 -0700735 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700736 const GrPrimitiveProcessor& processor) override {
jvanverth21deace2015-04-01 12:43:48 -0700737 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000738
joshualitt5559ca22015-05-21 15:50:36 -0700739 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
740 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700741 if (wa != fDistanceAdjust) {
742 pdman.set3f(fDistanceAdjustUni,
743 wa.fR,
744 wa.fG,
745 wa.fB);
746 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700747 }
joshualitt9b989322014-12-15 14:16:27 -0800748
joshualitt5559ca22015-05-21 15:50:36 -0700749 if (!dflcd.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dflcd.viewMatrix())) {
750 fViewMatrix = dflcd.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700751 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800752 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700753 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
754 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000755 }
756
robertphillips46d36f02015-01-18 08:14:14 -0800757 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700758 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700759 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700760 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000761
joshualitt8fc6c2d2014-12-22 15:27:05 -0800762 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700763 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700764 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800765 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700766
767 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
768 SkASSERT(gp.numTextures() == 1);
769 GrTexture* atlas = gp.textureAccess(0).getTexture();
770 SkASSERT(atlas);
771 b->add32(atlas->width());
772 b->add32(atlas->height());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000773 }
774
775private:
joshualitt5559ca22015-05-21 15:50:36 -0700776 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700777 UniformHandle fViewMatrixUniform;
jvanverth21deace2015-04-01 12:43:48 -0700778 UniformHandle fColorUniform;
jvanverth502286d2015-04-08 12:37:51 -0700779 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
jvanverth21deace2015-04-01 12:43:48 -0700780 UniformHandle fDistanceAdjustUni;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000781
egdaniele659a582015-11-13 09:55:43 -0800782 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000783};
784
785///////////////////////////////////////////////////////////////////////////////
786
jvanverth502286d2015-04-08 12:37:51 -0700787GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(
joshualitt8059eb92014-12-29 15:10:07 -0800788 GrColor color, const SkMatrix& viewMatrix,
jvanverth2d2a68c2014-06-10 06:42:56 -0700789 GrTexture* texture, const GrTextureParams& params,
jvanverth21deace2015-04-01 12:43:48 -0700790 DistanceAdjust distanceAdjust,
joshualittb8c241a2015-05-19 08:23:30 -0700791 uint32_t flags, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700792 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700793 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800794 , fTextureAccess(texture, params)
jvanverth21deace2015-04-01 12:43:48 -0700795 , fDistanceAdjust(distanceAdjust)
joshualittb8c241a2015-05-19 08:23:30 -0700796 , fFlags(flags & kLCD_DistanceFieldEffectMask)
797 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700798 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
jvanverth502286d2015-04-08 12:37:51 -0700799 this->initClassID<GrDistanceFieldLCDTextGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700800 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
801 kHigh_GrSLPrecision);
802 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
803 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType,
804 kHigh_GrSLPrecision);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000805 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000806}
807
egdaniel57d3b032015-11-13 11:57:27 -0800808void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
809 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700810 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800811}
812
egdaniel57d3b032015-11-13 11:57:27 -0800813GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700814 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000815}
816
817///////////////////////////////////////////////////////////////////////////////
818
jvanverth502286d2015-04-08 12:37:51 -0700819GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000820
bungeman06ca8ec2016-06-09 08:01:03 -0700821sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700822 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
823 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000824 static const SkShader::TileMode kTileModes[] = {
825 SkShader::kClamp_TileMode,
826 SkShader::kRepeat_TileMode,
827 SkShader::kMirror_TileMode,
828 };
829 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700830 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
831 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000832 };
joshualitt0067ff52015-07-08 14:26:19 -0700833 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000834 GrTextureParams::kNone_FilterMode);
jvanverth21deace2015-04-01 12:43:48 -0700835 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700836 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800837 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
838 if (flags & kSimilarity_DistanceFieldEffectFlag) {
839 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
840 }
joshualitt0067ff52015-07-08 14:26:19 -0700841 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
bungeman06ca8ec2016-06-09 08:01:03 -0700842 return GrDistanceFieldLCDTextGeoProc::Make(GrRandomColor(d->fRandom),
843 GrTest::TestMatrix(d->fRandom),
844 d->fTextures[texIdx], params,
845 wa,
846 flags,
847 d->fRandom->nextBool());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000848}