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 "GrDistanceFieldTextureEffect.h"
9 #include "gl/GrGLEffect.h"
10 #include "gl/GrGLSL.h"
11 #include "gl/GrGLTexture.h"
12 #include "gl/GrGLVertexEffect.h"
13 #include "GrTBackendEffectFactory.h"
14 #include "GrTexture.h"
15
16 // The distance field is constructed as unsigned char values, so that the zero value is at 128.
17 // Hence our zero threshold is 128/255.
18 #define THRESHOLD "0.50196078431"
19
20 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect {
21 public:
GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory & factory,const GrDrawEffect & drawEffect)22 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
23 : INHERITED (factory) {}
24
emitCode(GrGLFullShaderBuilder * builder,const GrDrawEffect & drawEffect,EffectKey key,const char * outputColor,const char * inputColor,const TransformedCoordsArray &,const TextureSamplerArray & samplers)25 virtual void emitCode(GrGLFullShaderBuilder* builder,
26 const GrDrawEffect& drawEffect,
27 EffectKey key,
28 const char* outputColor,
29 const char* inputColor,
30 const TransformedCoordsArray&,
31 const TextureSamplerArray& samplers) SK_OVERRIDE {
32 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs());
33
34 SkString fsCoordName;
35 const char* vsVaryingName;
36 const char* fsVaryingNamePtr;
37 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr);
38 fsCoordName = fsVaryingNamePtr;
39
40 const char* attrName =
41 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
42 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
43
44 builder->fsCodeAppend("\tvec4 texColor = ");
45 builder->fsAppendTextureLookup(samplers[0],
46 fsCoordName.c_str(),
47 kVec2f_GrSLType);
48 builder->fsCodeAppend(";\n");
49 builder->fsCodeAppend("\tfloat distance = texColor.r;\n");
50 // this gives us a smooth step across approximately one fragment
51 // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2)
52 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(vec2(dFdx(distance), dFdy(distance)));\n");
53 builder->fsCodeAppend("\tfloat val = smoothstep("THRESHOLD"-afwidth, "THRESHOLD"+afwidth, distance);\n");
54
55 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
56 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str());
57 }
58
setData(const GrGLUniformManager & uman,const GrDrawEffect & drawEffect)59 virtual void setData(const GrGLUniformManager& uman,
60 const GrDrawEffect& drawEffect) SK_OVERRIDE {}
61
62 private:
63 typedef GrGLVertexEffect INHERITED;
64 };
65
66 ///////////////////////////////////////////////////////////////////////////////
67
GrDistanceFieldTextureEffect(GrTexture * texture,const GrTextureParams & params)68 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
69 const GrTextureParams& params)
70 : fTextureAccess(texture, params) {
71 this->addTextureAccess(&fTextureAccess);
72 this->addVertexAttrib(kVec2f_GrSLType);
73 }
74
onIsEqual(const GrEffect & other) const75 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
76 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureEffect>(other);
77 return fTextureAccess == cte.fTextureAccess;
78 }
79
getConstantColorComponents(GrColor * color,uint32_t * validFlags) const80 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color,
81 uint32_t* validFlags) const {
82 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
83 GrPixelConfigIsOpaque(this->texture(0)->config())) {
84 *validFlags = kA_GrColorComponentFlag;
85 } else {
86 *validFlags = 0;
87 }
88 }
89
getFactory() const90 const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const {
91 return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance();
92 }
93
94 ///////////////////////////////////////////////////////////////////////////////
95
96 GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect);
97
TestCreate(SkRandom * random,GrContext *,const GrDrawTargetCaps &,GrTexture * textures[])98 GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random,
99 GrContext*,
100 const GrDrawTargetCaps&,
101 GrTexture* textures[]) {
102 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
103 GrEffectUnitTest::kAlphaTextureIdx;
104 static const SkShader::TileMode kTileModes[] = {
105 SkShader::kClamp_TileMode,
106 SkShader::kRepeat_TileMode,
107 SkShader::kMirror_TileMode,
108 };
109 SkShader::TileMode tileModes[] = {
110 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
111 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
112 };
113 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
114 GrTextureParams::kNone_FilterMode);
115
116 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params);
117 }
118