• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "include/gpu/GrTexture.h"
9 #include "src/core/SkDistanceFieldGen.h"
10 #include "src/gpu/GrCaps.h"
11 #include "src/gpu/GrShaderCaps.h"
12 #include "src/gpu/effects/GrAtlasedShaderHelpers.h"
13 #include "src/gpu/effects/GrDistanceFieldGeoProc.h"
14 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
17 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
18 #include "src/gpu/glsl/GrGLSLUtil.h"
19 #include "src/gpu/glsl/GrGLSLVarying.h"
20 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
21 
22 // Assuming a radius of a little less than the diagonal of the fragment
23 #define SK_DistanceFieldAAFactor     "0.65"
24 
25 class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
26 public:
27     GrGLDistanceFieldA8TextGeoProc() = default;
28 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)29     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
30         const GrDistanceFieldA8TextGeoProc& dfTexEffect =
31                 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
32         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
33 
34         GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
35         GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
36         GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
37 
38         // emit attributes
39         varyingHandler->emitAttributes(dfTexEffect);
40 
41         const char* atlasSizeInvName;
42         fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
43                                                           kFloat2_GrSLType,
44                                                           "AtlasSizeInv",
45                                                           &atlasSizeInvName);
46 #ifdef SK_GAMMA_APPLY_TO_A8
47         // adjust based on gamma
48         const char* distanceAdjustUniName = nullptr;
49         // width, height, 1/(3*width)
50         fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
51                                                         "DistanceAdjust", &distanceAdjustUniName);
52 #endif
53 
54         // Setup pass through color
55         varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
56 
57         // Setup position
58         gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
59 
60         // emit transforms
61         this->emitTransforms(vertBuilder,
62                              varyingHandler,
63                              uniformHandler,
64                              dfTexEffect.inPosition().asShaderVar(),
65                              dfTexEffect.localMatrix(),
66                              args.fFPCoordTransformHandler);
67 
68         // add varyings
69         GrGLSLVarying uv(kFloat2_GrSLType);
70         GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
71         GrGLSLVarying texIdx(texIdxType);
72         GrGLSLVarying st(kFloat2_GrSLType);
73         append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
74                                  &texIdx, &st);
75 
76         bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
77                               kUniformScale_DistanceFieldEffectMask;
78         bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
79         bool isGammaCorrect =
80             SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
81         bool isAliased =
82             SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
83 
84         // Use highp to work around aliasing issues
85         fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
86         fragBuilder->codeAppend("half4 texColor;");
87         append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
88                                    texIdx, "uv", "texColor");
89 
90         fragBuilder->codeAppend("half distance = "
91                       SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
92 #ifdef SK_GAMMA_APPLY_TO_A8
93         // adjust width based on gamma
94         fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
95 #endif
96 
97         fragBuilder->codeAppend("half afwidth;");
98         if (isUniformScale) {
99             // For uniform scale, we adjust for the effect of the transformation on the distance
100             // by using the length of the gradient of the t coordinate in the y direction.
101             // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
102 
103             // this gives us a smooth step across approximately one fragment
104 #ifdef SK_VULKAN
105             fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
106                                     "*half(dFdx(%s.x)));", st.fsIn());
107 #else
108             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
109             fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
110                                      "*half(dFdy(%s.y)));", st.fsIn());
111 #endif
112         } else if (isSimilarity) {
113             // For similarity transform, we adjust the effect of the transformation on the distance
114             // by using the length of the gradient of the texture coordinates. We use st coordinates
115             // to ensure we're mapping 1:1 from texel space to pixel space.
116             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
117 
118             // this gives us a smooth step across approximately one fragment
119 #ifdef SK_VULKAN
120             fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdx(%s)));", st.fsIn());
121 #else
122             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
123             fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdy(%s)));", st.fsIn());
124 #endif
125             fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
126         } else {
127             // For general transforms, to determine the amount of correction we multiply a unit
128             // vector pointing along the SDF gradient direction by the Jacobian of the st coords
129             // (which is the inverse transform for this fragment) and take the length of the result.
130             fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
131                                                                    "dFdy(distance)));");
132             // the length of the gradient may be 0, so we need to check for this
133             // this also compensates for the Adreno, which likes to drop tiles on division by 0
134             fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
135             fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
136             fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
137             fragBuilder->codeAppend("} else {");
138             fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
139             fragBuilder->codeAppend("}");
140 
141             fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
142             fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
143             fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
144             fragBuilder->codeAppend("                 dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
145 
146             // this gives us a smooth step across approximately one fragment
147             fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
148         }
149 
150         if (isAliased) {
151             fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
152         } else if (isGammaCorrect) {
153             // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
154             // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
155             // distance mapped linearly to coverage, so use a linear step:
156             fragBuilder->codeAppend(
157                 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
158         } else {
159             fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
160         }
161 
162         fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
163     }
164 
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor & proc,FPCoordTransformIter && transformIter)165     void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
166                  FPCoordTransformIter&& transformIter) override {
167         const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
168 
169 #ifdef SK_GAMMA_APPLY_TO_A8
170         float distanceAdjust = dfa8gp.getDistanceAdjust();
171         if (distanceAdjust != fDistanceAdjust) {
172             fDistanceAdjust = distanceAdjust;
173             pdman.set1f(fDistanceAdjustUni, distanceAdjust);
174         }
175 #endif
176 
177         const SkISize& atlasSize = dfa8gp.atlasSize();
178         SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
179 
180         if (fAtlasSize != atlasSize) {
181             pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
182             fAtlasSize = atlasSize;
183         }
184         this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
185     }
186 
GenKey(const GrGeometryProcessor & gp,const GrShaderCaps &,GrProcessorKeyBuilder * b)187     static inline void GenKey(const GrGeometryProcessor& gp,
188                               const GrShaderCaps&,
189                               GrProcessorKeyBuilder* b) {
190         const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
191         uint32_t key = dfTexEffect.getFlags();
192         b->add32(key);
193         b->add32(dfTexEffect.numTextureSamplers());
194     }
195 
196 private:
197 #ifdef SK_GAMMA_APPLY_TO_A8
198     float fDistanceAdjust = -1.f;
199     UniformHandle fDistanceAdjustUni;
200 #endif
201     SkISize fAtlasSize = {0, 0};
202     UniformHandle fAtlasSizeInvUniform;
203 
204     typedef GrGLSLGeometryProcessor INHERITED;
205 };
206 
207 ///////////////////////////////////////////////////////////////////////////////
208 
GrDistanceFieldA8TextGeoProc(const GrShaderCaps & caps,const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params,float distanceAdjust,uint32_t flags,const SkMatrix & localMatrix)209 GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
210                                                            const sk_sp<GrTextureProxy>* proxies,
211                                                            int numProxies,
212                                                            const GrSamplerState& params,
213 #ifdef SK_GAMMA_APPLY_TO_A8
214                                                            float distanceAdjust,
215 #endif
216                                                            uint32_t flags,
217                                                            const SkMatrix& localMatrix)
218         : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
219         , fLocalMatrix(localMatrix)
220         , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
221 #ifdef SK_GAMMA_APPLY_TO_A8
222         , fDistanceAdjust(distanceAdjust)
223 #endif
224 {
225     SkASSERT(numProxies <= kMaxTextures);
226     SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
227 
228     if (flags & kPerspective_DistanceFieldEffectFlag) {
229         fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
230     } else {
231         fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
232     }
233     fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
234     fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
235                         caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
236     this->setVertexAttributes(&fInPosition, 3);
237 
238     if (numProxies) {
239         fAtlasSize = proxies[0]->isize();
240     }
241     for (int i = 0; i < numProxies; ++i) {
242         SkASSERT(proxies[i]);
243         SkASSERT(proxies[i]->isize() == fAtlasSize);
244         fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
245     }
246     this->setTextureSamplerCnt(numProxies);
247 }
248 
addNewProxies(const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params)249 void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
250                                                  int numProxies,
251                                                  const GrSamplerState& params) {
252     SkASSERT(numProxies <= kMaxTextures);
253 
254     if (!fTextureSamplers[0].isInitialized()) {
255         fAtlasSize = proxies[0]->isize();
256     }
257 
258     for (int i = 0; i < numProxies; ++i) {
259         SkASSERT(proxies[i]);
260         SkASSERT(proxies[i]->isize() == fAtlasSize);
261         if (!fTextureSamplers[i].isInitialized()) {
262             fTextureSamplers[i].reset(proxies[i]->textureType(), params,
263                                       proxies[i]->textureSwizzle());
264         }
265     }
266     this->setTextureSamplerCnt(numProxies);
267 }
268 
getGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const269 void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
270                                                        GrProcessorKeyBuilder* b) const {
271     GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
272 }
273 
274 GrGLSLPrimitiveProcessor*
createGLSLInstance(const GrShaderCaps &) const275 GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
276     return new GrGLDistanceFieldA8TextGeoProc();
277 }
278 
279 ///////////////////////////////////////////////////////////////////////////////
280 
281 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
282 
283 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)284 sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
285     int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
286                                         : GrProcessorUnitTest::kAlphaTextureIdx;
287     sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
288         d->textureProxy(texIdx),
289         nullptr,
290         nullptr,
291         nullptr
292     };
293 
294     GrSamplerState::WrapMode wrapModes[2];
295     GrTest::TestWrapModes(d->fRandom, wrapModes);
296     GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
297                                                    ? GrSamplerState::Filter::kBilerp
298                                                    : GrSamplerState::Filter::kNearest);
299 
300     uint32_t flags = 0;
301     flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
302     if (flags & kSimilarity_DistanceFieldEffectFlag) {
303         flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
304     }
305     SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
306 #ifdef SK_GAMMA_APPLY_TO_A8
307     float lum = d->fRandom->nextF();
308 #endif
309     return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
310                                               proxies, 1,
311                                               samplerState,
312 #ifdef SK_GAMMA_APPLY_TO_A8
313                                               lum,
314 #endif
315                                               flags, localMatrix);
316 }
317 #endif
318 
319 ///////////////////////////////////////////////////////////////////////////////
320 
321 class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
322 public:
GrGLDistanceFieldPathGeoProc()323     GrGLDistanceFieldPathGeoProc()
324             : fMatrix(SkMatrix::InvalidMatrix())
325             , fAtlasSize({0,0}) {
326     }
327 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)328     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
329         const GrDistanceFieldPathGeoProc& dfPathEffect =
330                 args.fGP.cast<GrDistanceFieldPathGeoProc>();
331 
332         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
333 
334         GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
335         GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
336         GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
337 
338         // emit attributes
339         varyingHandler->emitAttributes(dfPathEffect);
340 
341         const char* atlasSizeInvName;
342         fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
343                                                           kFloat2_GrSLType,
344                                                           "AtlasSizeInv",
345                                                           &atlasSizeInvName);
346 
347         GrGLSLVarying uv(kFloat2_GrSLType);
348         GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
349         GrGLSLVarying texIdx(texIdxType);
350         GrGLSLVarying st(kFloat2_GrSLType);
351         append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
352                                  &texIdx, &st);
353 
354         // setup pass through color
355         varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
356 
357         if (dfPathEffect.matrix().hasPerspective()) {
358             // Setup position
359             this->writeOutputPosition(vertBuilder,
360                                       uniformHandler,
361                                       gpArgs,
362                                       dfPathEffect.inPosition().name(),
363                                       dfPathEffect.matrix(),
364                                       &fMatrixUniform);
365 
366             // emit transforms
367             this->emitTransforms(vertBuilder,
368                                  varyingHandler,
369                                  uniformHandler,
370                                  dfPathEffect.inPosition().asShaderVar(),
371                                  args.fFPCoordTransformHandler);
372         } else {
373             // Setup position
374             this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
375 
376             // emit transforms
377             this->emitTransforms(vertBuilder,
378                                  varyingHandler,
379                                  uniformHandler,
380                                  dfPathEffect.inPosition().asShaderVar(),
381                                  dfPathEffect.matrix(),
382                                  args.fFPCoordTransformHandler);
383         }
384 
385         // Use highp to work around aliasing issues
386         fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
387         fragBuilder->codeAppend("half4 texColor;");
388         append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
389                                    "texColor");
390 
391         fragBuilder->codeAppend("half distance = "
392             SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
393 
394         fragBuilder->codeAppend("half afwidth;");
395         bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
396                               kUniformScale_DistanceFieldEffectMask;
397         bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
398         bool isGammaCorrect =
399                 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
400         if (isUniformScale) {
401             // For uniform scale, we adjust for the effect of the transformation on the distance
402             // by using the length of the gradient of the t coordinate in the y direction.
403             // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
404 
405             // this gives us a smooth step across approximately one fragment
406 #ifdef SK_VULKAN
407             fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
408                                      "*half(dFdx(%s.x)));", st.fsIn());
409 #else
410             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
411             fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
412                                      "*half(dFdy(%s.y)));", st.fsIn());
413 #endif
414         } else if (isSimilarity) {
415             // For similarity transform, we adjust the effect of the transformation on the distance
416             // by using the length of the gradient of the texture coordinates. We use st coordinates
417             // to ensure we're mapping 1:1 from texel space to pixel space.
418 
419             // this gives us a smooth step across approximately one fragment
420 #ifdef SK_VULKAN
421             fragBuilder->codeAppendf("half st_grad_len = half(length(dFdx(%s)));", st.fsIn());
422 #else
423             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
424             fragBuilder->codeAppendf("half st_grad_len = half(length(dFdy(%s)));", st.fsIn());
425 #endif
426             fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
427         } else {
428             // For general transforms, to determine the amount of correction we multiply a unit
429             // vector pointing along the SDF gradient direction by the Jacobian of the st coords
430             // (which is the inverse transform for this fragment) and take the length of the result.
431             fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
432                                                             "dFdy(distance));");
433             // the length of the gradient may be 0, so we need to check for this
434             // this also compensates for the Adreno, which likes to drop tiles on division by 0
435             fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
436             fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
437             fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
438             fragBuilder->codeAppend("} else {");
439             fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
440             fragBuilder->codeAppend("}");
441 
442             fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
443             fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
444             fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
445             fragBuilder->codeAppend("                   dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
446 
447             // this gives us a smooth step across approximately one fragment
448             fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
449         }
450         // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
451         // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
452         // mapped linearly to coverage, so use a linear step:
453         if (isGammaCorrect) {
454             fragBuilder->codeAppend(
455                 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
456         } else {
457             fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
458         }
459 
460         fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
461     }
462 
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor & proc,FPCoordTransformIter && transformIter)463     void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
464                  FPCoordTransformIter&& transformIter) override {
465 
466         const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
467 
468         if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
469             fMatrix = dfpgp.matrix();
470             float matrix[3 * 3];
471             GrGLSLGetMatrix<3>(matrix, fMatrix);
472             pdman.setMatrix3f(fMatrixUniform, matrix);
473         }
474 
475         const SkISize& atlasSize = dfpgp.atlasSize();
476         SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
477         if (fAtlasSize != atlasSize) {
478             pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
479             fAtlasSize = atlasSize;
480         }
481 
482         if (dfpgp.matrix().hasPerspective()) {
483             this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
484         } else {
485             this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
486         }
487     }
488 
GenKey(const GrGeometryProcessor & gp,const GrShaderCaps &,GrProcessorKeyBuilder * b)489     static inline void GenKey(const GrGeometryProcessor& gp,
490                               const GrShaderCaps&,
491                               GrProcessorKeyBuilder* b) {
492         const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
493 
494         uint32_t key = dfTexEffect.getFlags();
495         key |= ComputePosKey(dfTexEffect.matrix()) << 16;
496         b->add32(key);
497         b->add32(dfTexEffect.matrix().hasPerspective());
498         b->add32(dfTexEffect.numTextureSamplers());
499     }
500 
501 private:
502     SkMatrix      fMatrix;        // view matrix if perspective, local matrix otherwise
503     UniformHandle fMatrixUniform;
504 
505     SkISize       fAtlasSize;
506     UniformHandle fAtlasSizeInvUniform;
507 
508     typedef GrGLSLGeometryProcessor INHERITED;
509 };
510 
511 ///////////////////////////////////////////////////////////////////////////////
512 
GrDistanceFieldPathGeoProc(const GrShaderCaps & caps,const SkMatrix & matrix,bool wideColor,const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params,uint32_t flags)513 GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
514                                                        const SkMatrix& matrix,
515                                                        bool wideColor,
516                                                        const sk_sp<GrTextureProxy>* proxies,
517                                                        int numProxies,
518                                                        const GrSamplerState& params,
519                                                        uint32_t flags)
520         : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
521         , fMatrix(matrix)
522         , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
523     SkASSERT(numProxies <= kMaxTextures);
524     SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
525 
526     fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
527     fInColor = MakeColorAttribute("inColor", wideColor);
528     fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
529                         caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
530     this->setVertexAttributes(&fInPosition, 3);
531 
532     if (numProxies) {
533         fAtlasSize = proxies[0]->isize();
534     }
535 
536     for (int i = 0; i < numProxies; ++i) {
537         SkASSERT(proxies[i]);
538         SkASSERT(proxies[i]->isize() == fAtlasSize);
539         fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
540     }
541     this->setTextureSamplerCnt(numProxies);
542 }
543 
addNewProxies(const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params)544 void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
545                                                int numProxies,
546                                                const GrSamplerState& params) {
547     SkASSERT(numProxies <= kMaxTextures);
548 
549     if (!fTextureSamplers[0].isInitialized()) {
550         fAtlasSize = proxies[0]->isize();
551     }
552 
553     for (int i = 0; i < numProxies; ++i) {
554         SkASSERT(proxies[i]);
555         SkASSERT(proxies[i]->isize() == fAtlasSize);
556 
557         if (!fTextureSamplers[i].isInitialized()) {
558             fTextureSamplers[i].reset(proxies[i]->textureType(), params,
559                                       proxies[i]->textureSwizzle());
560         }
561     }
562     this->setTextureSamplerCnt(numProxies);
563 }
564 
getGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const565 void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
566                                                      GrProcessorKeyBuilder* b) const {
567     GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
568 }
569 
570 GrGLSLPrimitiveProcessor*
createGLSLInstance(const GrShaderCaps &) const571 GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
572     return new GrGLDistanceFieldPathGeoProc();
573 }
574 
575 ///////////////////////////////////////////////////////////////////////////////
576 
577 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
578 
579 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)580 sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
581     int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
582                                         : GrProcessorUnitTest::kAlphaTextureIdx;
583     sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
584         d->textureProxy(texIdx),
585         nullptr,
586         nullptr,
587         nullptr
588     };
589 
590     GrSamplerState::WrapMode wrapModes[2];
591     GrTest::TestWrapModes(d->fRandom, wrapModes);
592     GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
593                                                    ? GrSamplerState::Filter::kBilerp
594                                                    : GrSamplerState::Filter::kNearest);
595 
596     uint32_t flags = 0;
597     flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
598     if (flags & kSimilarity_DistanceFieldEffectFlag) {
599         flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
600     }
601 
602     return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
603                                             GrTest::TestMatrix(d->fRandom),
604                                             d->fRandom->nextBool(),
605                                             proxies, 1,
606                                             samplerState,
607                                             flags);
608 }
609 #endif
610 
611 ///////////////////////////////////////////////////////////////////////////////
612 
613 class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
614 public:
GrGLDistanceFieldLCDTextGeoProc()615     GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
616         fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
617     }
618 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)619     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
620         const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
621                 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
622 
623         GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
624         GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
625         GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
626 
627         // emit attributes
628         varyingHandler->emitAttributes(dfTexEffect);
629 
630         const char* atlasSizeInvName;
631         fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
632                                                           kFloat2_GrSLType,
633                                                           "AtlasSizeInv",
634                                                           &atlasSizeInvName);
635 
636         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
637 
638         // setup pass through color
639         varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
640 
641         // Setup position
642         gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
643 
644         // emit transforms
645         this->emitTransforms(vertBuilder,
646                              varyingHandler,
647                              uniformHandler,
648                              dfTexEffect.inPosition().asShaderVar(),
649                              dfTexEffect.localMatrix(),
650                              args.fFPCoordTransformHandler);
651 
652         // set up varyings
653         GrGLSLVarying uv(kFloat2_GrSLType);
654         GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
655         GrGLSLVarying texIdx(texIdxType);
656         GrGLSLVarying st(kFloat2_GrSLType);
657         append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
658                                  &texIdx, &st);
659 
660         GrGLSLVarying delta(kFloat_GrSLType);
661         varyingHandler->addVarying("Delta", &delta);
662         if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
663             vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
664         } else {
665             vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
666         }
667 
668         // add frag shader code
669         bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
670                               kUniformScale_DistanceFieldEffectMask;
671         bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
672         bool isGammaCorrect =
673             SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
674 
675         // create LCD offset adjusted by inverse of transform
676         // Use highp to work around aliasing issues
677         fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
678 
679         if (isUniformScale) {
680 #ifdef SK_VULKAN
681             fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
682 #else
683             // We use the y gradient because there is a bug in the Mali 400 in the x direction.
684             fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
685 #endif
686             fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
687                                      delta.fsIn());
688         } else if (isSimilarity) {
689             // For a similarity matrix with rotation, the gradient will not be aligned
690             // with the texel coordinate axes, so we need to calculate it.
691 #ifdef SK_VULKAN
692             fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
693             fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
694 #else
695             // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
696             // get the gradient in the x direction.
697             fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
698             fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
699                                      delta.fsIn());
700 #endif
701             fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
702         } else {
703             fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
704 
705             fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
706             fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
707             fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
708         }
709 
710         // sample the texture by index
711         fragBuilder->codeAppend("half4 texColor;");
712         append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
713                                    texIdx, "uv", "texColor");
714 
715         // green is distance to uv center
716         fragBuilder->codeAppend("half3 distance;");
717         fragBuilder->codeAppend("distance.y = texColor.r;");
718         // red is distance to left offset
719         fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
720         append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
721                                    texIdx, "uv_adjusted", "texColor");
722         fragBuilder->codeAppend("distance.x = texColor.r;");
723         // blue is distance to right offset
724         fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
725         append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
726                                    texIdx, "uv_adjusted", "texColor");
727         fragBuilder->codeAppend("distance.z = texColor.r;");
728 
729         fragBuilder->codeAppend("distance = "
730            "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
731 
732         // adjust width based on gamma
733         const char* distanceAdjustUniName = nullptr;
734         fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
735                                                         "DistanceAdjust", &distanceAdjustUniName);
736         fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
737 
738         // To be strictly correct, we should compute the anti-aliasing factor separately
739         // for each color component. However, this is only important when using perspective
740         // transformations, and even then using a single factor seems like a reasonable
741         // trade-off between quality and speed.
742         fragBuilder->codeAppend("half afwidth;");
743         if (isSimilarity) {
744             // For similarity transform (uniform scale-only is a subset of this), we adjust for the
745             // effect of the transformation on the distance by using the length of the gradient of
746             // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
747             // space to pixel space.
748 
749             // this gives us a smooth step across approximately one fragment
750             fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
751         } else {
752             // For general transforms, to determine the amount of correction we multiply a unit
753             // vector pointing along the SDF gradient direction by the Jacobian of the st coords
754             // (which is the inverse transform for this fragment) and take the length of the result.
755             fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
756                                                             "half(dFdy(distance.r)));");
757             // the length of the gradient may be 0, so we need to check for this
758             // this also compensates for the Adreno, which likes to drop tiles on division by 0
759             fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
760             fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
761             fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
762             fragBuilder->codeAppend("} else {");
763             fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
764             fragBuilder->codeAppend("}");
765             fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
766             fragBuilder->codeAppend("                 dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
767 
768             // this gives us a smooth step across approximately one fragment
769             fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
770         }
771 
772         // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
773         // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
774         // mapped linearly to coverage, so use a linear step:
775         if (isGammaCorrect) {
776             fragBuilder->codeAppendf("%s = "
777                 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
778                 args.fOutputCoverage);
779         } else {
780             fragBuilder->codeAppendf(
781                 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
782                 args.fOutputCoverage);
783         }
784     }
785 
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor & processor,FPCoordTransformIter && transformIter)786     void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
787                  FPCoordTransformIter&& transformIter) override {
788         SkASSERT(fDistanceAdjustUni.isValid());
789 
790         const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
791         GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
792         if (wa != fDistanceAdjust) {
793             pdman.set3f(fDistanceAdjustUni,
794                         wa.fR,
795                         wa.fG,
796                         wa.fB);
797             fDistanceAdjust = wa;
798         }
799 
800         const SkISize& atlasSize = dflcd.atlasSize();
801         SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
802         if (fAtlasSize != atlasSize) {
803             pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
804             fAtlasSize = atlasSize;
805         }
806         this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
807     }
808 
GenKey(const GrGeometryProcessor & gp,const GrShaderCaps &,GrProcessorKeyBuilder * b)809     static inline void GenKey(const GrGeometryProcessor& gp,
810                               const GrShaderCaps&,
811                               GrProcessorKeyBuilder* b) {
812         const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
813 
814         uint32_t key = dfTexEffect.getFlags();
815         b->add32(key);
816         b->add32(dfTexEffect.numTextureSamplers());
817     }
818 
819 private:
820     GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
821     UniformHandle                                 fDistanceAdjustUni;
822 
823     SkISize                                       fAtlasSize;
824     UniformHandle                                 fAtlasSizeInvUniform;
825 
826     typedef GrGLSLGeometryProcessor INHERITED;
827 };
828 
829 ///////////////////////////////////////////////////////////////////////////////
830 
GrDistanceFieldLCDTextGeoProc(const GrShaderCaps & caps,const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params,DistanceAdjust distanceAdjust,uint32_t flags,const SkMatrix & localMatrix)831 GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
832                                                              const sk_sp<GrTextureProxy>* proxies,
833                                                              int numProxies,
834                                                              const GrSamplerState& params,
835                                                              DistanceAdjust distanceAdjust,
836                                                              uint32_t flags,
837                                                              const SkMatrix& localMatrix)
838         : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
839         , fLocalMatrix(localMatrix)
840         , fDistanceAdjust(distanceAdjust)
841         , fFlags(flags & kLCD_DistanceFieldEffectMask) {
842     SkASSERT(numProxies <= kMaxTextures);
843     SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
844 
845     if (fFlags & kPerspective_DistanceFieldEffectFlag) {
846         fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
847     } else {
848         fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
849     }
850     fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
851     fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
852                         caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
853     this->setVertexAttributes(&fInPosition, 3);
854 
855     if (numProxies) {
856         fAtlasSize = proxies[0]->isize();
857     }
858 
859     for (int i = 0; i < numProxies; ++i) {
860         SkASSERT(proxies[i]);
861         SkASSERT(proxies[i]->isize() == fAtlasSize);
862         fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
863     }
864     this->setTextureSamplerCnt(numProxies);
865 }
866 
addNewProxies(const sk_sp<GrTextureProxy> * proxies,int numProxies,const GrSamplerState & params)867 void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
868                                                   int numProxies,
869                                                   const GrSamplerState& params) {
870     SkASSERT(numProxies <= kMaxTextures);
871 
872     if (!fTextureSamplers[0].isInitialized()) {
873         fAtlasSize = proxies[0]->isize();
874     }
875 
876     for (int i = 0; i < numProxies; ++i) {
877         SkASSERT(proxies[i]);
878         SkASSERT(proxies[i]->isize() == fAtlasSize);
879 
880         if (!fTextureSamplers[i].isInitialized()) {
881             fTextureSamplers[i].reset(proxies[i]->textureType(), params,
882                                       proxies[i]->textureSwizzle());
883         }
884     }
885     this->setTextureSamplerCnt(numProxies);
886 }
887 
getGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const888 void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
889                                                         GrProcessorKeyBuilder* b) const {
890     GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
891 }
892 
createGLSLInstance(const GrShaderCaps &) const893 GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
894     return new GrGLDistanceFieldLCDTextGeoProc();
895 }
896 
897 ///////////////////////////////////////////////////////////////////////////////
898 
899 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
900 
901 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)902 sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
903     int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
904                                           GrProcessorUnitTest::kAlphaTextureIdx;
905     sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
906         d->textureProxy(texIdx),
907         nullptr,
908         nullptr,
909         nullptr
910     };
911 
912     GrSamplerState::WrapMode wrapModes[2];
913     GrTest::TestWrapModes(d->fRandom, wrapModes);
914     GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
915                                                    ? GrSamplerState::Filter::kBilerp
916                                                    : GrSamplerState::Filter::kNearest);
917     DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
918     uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
919     flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
920     if (flags & kSimilarity_DistanceFieldEffectFlag) {
921         flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
922     }
923     flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
924     SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
925     return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
926                                                wa, flags, localMatrix);
927 }
928 #endif
929