Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: src/gpu/effects/GrDistanceFieldTextureEffect.cpp

Issue 205343008: Distance field fixes for Android (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix int->float conversion issues Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/effects/GrDistanceFieldTextureEffect.h ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrDistanceFieldTextureEffect.h" 8 #include "GrDistanceFieldTextureEffect.h"
9 #include "gl/GrGLEffect.h" 9 #include "gl/GrGLEffect.h"
10 #include "gl/GrGLSL.h" 10 #include "gl/GrGLSL.h"
11 #include "gl/GrGLTexture.h" 11 #include "gl/GrGLTexture.h"
12 #include "gl/GrGLVertexEffect.h" 12 #include "gl/GrGLVertexEffect.h"
13 #include "GrTBackendEffectFactory.h" 13 #include "GrTBackendEffectFactory.h"
14 #include "GrTexture.h" 14 #include "GrTexture.h"
15 15
16 // The distance field is constructed as unsigned char values, so that the zero v alue is at 128, 16 // The distance field is constructed as unsigned char values, so that the zero v alue is at 128,
17 // and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero t hreshold is 128/255. 17 // and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero t hreshold is 128/255.
18 #define MULTIPLIER "7.96875" 18 #define MULTIPLIER "7.96875"
19 #define THRESHOLD "0.50196078431" 19 #define THRESHOLD "0.50196078431"
20 20
21 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { 21 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect {
22 public: 22 public:
23 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, 23 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory,
24 const GrDrawEffect& drawEffect) 24 const GrDrawEffect& drawEffect)
25 : INHERITED (factory) 25 : INHERITED (factory)
26 , fTextureSize(SkSize::Make(-1.f,-1.f)) {} 26 , fTextureSize(SkISize::Make(-1,-1)) {}
27 27
28 virtual void emitCode(GrGLFullShaderBuilder* builder, 28 virtual void emitCode(GrGLFullShaderBuilder* builder,
29 const GrDrawEffect& drawEffect, 29 const GrDrawEffect& drawEffect,
30 EffectKey key, 30 EffectKey key,
31 const char* outputColor, 31 const char* outputColor,
32 const char* inputColor, 32 const char* inputColor,
33 const TransformedCoordsArray&, 33 const TransformedCoordsArray&,
34 const TextureSamplerArray& samplers) SK_OVERRIDE { 34 const TextureSamplerArray& samplers) SK_OVERRIDE {
35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV ertexAttribs()); 35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV ertexAttribs());
36 36
37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva tives_GLSLFeature)); 37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva tives_GLSLFeature));
38 const GrDistanceFieldTextureEffect& dfTexEffect =
39 drawEffect.castEffect<GrDistanceFi eldTextureEffect>();
38 40
39 SkString fsCoordName; 41 SkString fsCoordName;
40 const char* vsCoordName; 42 const char* vsCoordName;
41 const char* fsCoordNamePtr; 43 const char* fsCoordNamePtr;
42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr); 44 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr);
43 fsCoordName = fsCoordNamePtr; 45 fsCoordName = fsCoordNamePtr;
44 46
45 const char* attrName0 = 47 const char* attrName0 =
46 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str(); 48 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str();
47 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); 49 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0);
48 50
49 const char* textureSizeUniName = NULL; 51 const char* textureSizeUniName = NULL;
50 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib ility, 52 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib ility,
51 kVec2f_GrSLType, "TextureSize", 53 kVec2f_GrSLType, "TextureSize",
52 &textureSizeUniName); 54 &textureSizeUniName);
53 55
54 builder->fsCodeAppend("\tvec4 texColor = "); 56 builder->fsCodeAppend("\tvec4 texColor = ");
55 builder->fsAppendTextureLookup(samplers[0], 57 builder->fsAppendTextureLookup(samplers[0],
56 fsCoordName.c_str(), 58 fsCoordName.c_str(),
57 kVec2f_GrSLType); 59 kVec2f_GrSLType);
58 builder->fsCodeAppend(";\n"); 60 builder->fsCodeAppend(";\n");
59 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); 61 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n");
60 62
61 // we adjust for the effect of the transformation on the distance by usi ng 63 // we adjust for the effect of the transformation on the distance by usi ng
62 // the length of the gradient of the texture coordinates. We use st coor dinates 64 // the length of the gradient of the texture coordinates. We use st coor dinates
63 // to ensure we're mapping 1:1 from texel space to pixel space. 65 // to ensure we're mapping 1:1 from texel space to pixel space.
64 builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), text ureSizeUniName); 66 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str());
65 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); 67 builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName);
66 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); 68 builder->fsCodeAppend("\tfloat afwidth;\n");
67 builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n"); 69 if (dfTexEffect.isUniformScale()) {
68 builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jd y.x,\n"); 70 // this gives us a smooth step across approximately one fragment
69 builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jd y.y);\n"); 71 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2)
72 builder->fsCodeAppend("\tafwidth = 0.7071*dFdx(st.x);\n");
73 } else {
74 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n");
75 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n");
70 76
71 // this gives us a smooth step across approximately one fragment 77 builder->fsCodeAppend("\tvec2 uv_grad;\n");
72 // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) 78 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) {
73 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n"); 79 // this is to compensate for the Adreno, which likes to drop til es on division by 0
80 builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n");
81 builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n");
82 builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n");
83 builder->fsCodeAppend("\t} else {\n");
84 builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n" );
85 builder->fsCodeAppend("\t}\n");
86 } else {
87 builder->fsCodeAppend("\tuv_grad = normalize(uv);\n");
88 }
89 builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad. y*Jdy.x,\n");
90 builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad. y*Jdy.y);\n");
91
92 // this gives us a smooth step across approximately one fragment
93 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2)
94 builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n");
95 }
96
74 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista nce);\n"); 97 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista nce);\n");
75 98
76 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, 99 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
77 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val") ).c_str()); 100 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val") ).c_str());
78 } 101 }
79 102
80 virtual void setData(const GrGLUniformManager& uman, 103 virtual void setData(const GrGLUniformManager& uman,
81 const GrDrawEffect& drawEffect) SK_OVERRIDE { 104 const GrDrawEffect& drawEffect) SK_OVERRIDE {
82 SkASSERT(fTextureSizeUni.isValid()); 105 SkASSERT(fTextureSizeUni.isValid());
83 const GrDistanceFieldTextureEffect& distanceFieldEffect = 106
107 GrTexture* texture = drawEffect.effect()->get()->texture(0);
108 if (texture->width() != fTextureSize.width() ||
109 texture->height() != fTextureSize.height()) {
110 fTextureSize = SkISize::Make(texture->width(), texture->height());
111 uman.set2f(fTextureSizeUni,
112 SkIntToScalar(fTextureSize.width()),
113 SkIntToScalar(fTextureSize.height()));
114 }
115 }
116
117 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) {
118 const GrDistanceFieldTextureEffect& dfTexEffect =
84 drawEffect.castEffect<GrDistanceFi eldTextureEffect>(); 119 drawEffect.castEffect<GrDistanceFi eldTextureEffect>();
85 if (distanceFieldEffect.getSize().width() != fTextureSize.width() || 120
86 distanceFieldEffect.getSize().height() != fTextureSize.height()) { 121 return dfTexEffect.isUniformScale() ? 0x1 : 0x0;
87 fTextureSize = distanceFieldEffect.getSize();
88 uman.set2f(fTextureSizeUni,
89 distanceFieldEffect.getSize().width(),
90 distanceFieldEffect.getSize().height());
91 }
92 } 122 }
93 123
94 private: 124 private:
95 GrGLUniformManager::UniformHandle fTextureSizeUni; 125 GrGLUniformManager::UniformHandle fTextureSizeUni;
96 SkSize fTextureSize; 126 SkISize fTextureSize;
97 127
98 typedef GrGLVertexEffect INHERITED; 128 typedef GrGLVertexEffect INHERITED;
99 }; 129 };
100 130
101 /////////////////////////////////////////////////////////////////////////////// 131 ///////////////////////////////////////////////////////////////////////////////
102 132
103 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, 133 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
104 const GrTextureParams & params, 134 const GrTextureParams & params,
105 const SkISize& size) 135 bool uniformScale)
106 : fTextureAccess(texture, params) 136 : fTextureAccess(texture, params)
107 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height( )))) { 137 , fUniformScale(uniformScale) {
108 this->addTextureAccess(&fTextureAccess); 138 this->addTextureAccess(&fTextureAccess);
109 this->addVertexAttrib(kVec2f_GrSLType); 139 this->addVertexAttrib(kVec2f_GrSLType);
110 } 140 }
111 141
112 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { 142 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
113 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other); 143 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other);
114 return fTextureAccess == cte.fTextureAccess; 144 return fTextureAccess == cte.fTextureAccess;
115 } 145 }
116 146
117 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, 147 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color,
(...skipping 24 matching lines...) Expand all
142 SkShader::kClamp_TileMode, 172 SkShader::kClamp_TileMode,
143 SkShader::kRepeat_TileMode, 173 SkShader::kRepeat_TileMode,
144 SkShader::kMirror_TileMode, 174 SkShader::kMirror_TileMode,
145 }; 175 };
146 SkShader::TileMode tileModes[] = { 176 SkShader::TileMode tileModes[] = {
147 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 177 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
148 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 178 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
149 }; 179 };
150 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : 180 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode :
151 GrTextureParams::kNon e_FilterMode); 181 GrTextureParams::kNon e_FilterMode);
152 SkISize size = SkISize::Make(1024, 2048);
153 182
154 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size); 183 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, random ->nextBool());
155 } 184 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrDistanceFieldTextureEffect.h ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698