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

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

Issue 219243012: Add LCD support for distance field text (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase to master 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') | no next file » | 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"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 kVec2f_GrSLType); 59 kVec2f_GrSLType);
60 builder->fsCodeAppend(";\n"); 60 builder->fsCodeAppend(";\n");
61 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); 61 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n");
62 62
63 // 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
64 // 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
65 // 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.
66 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); 66 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str());
67 builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); 67 builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName);
68 builder->fsCodeAppend("\tfloat afwidth;\n"); 68 builder->fsCodeAppend("\tfloat afwidth;\n");
69 if (dfTexEffect.isUniformScale()) { 69 if (dfTexEffect.isSimilarity()) {
70 // this gives us a smooth step across approximately one fragment 70 // this gives us a smooth step across approximately one fragment
71 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2) 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"); 72 builder->fsCodeAppend("\tafwidth = 0.7071*dFdx(st.x);\n");
73 } else { 73 } else {
74 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); 74 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n");
75 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); 75 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n");
76 76
77 builder->fsCodeAppend("\tvec2 uv_grad;\n"); 77 builder->fsCodeAppend("\tvec2 uv_grad;\n");
78 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) { 78 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) {
79 // this is to compensate for the Adreno, which likes to drop til es on division by 0 79 // this is to compensate for the Adreno, which likes to drop til es on division by 0
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 uman.set2f(fTextureSizeUni, 111 uman.set2f(fTextureSizeUni,
112 SkIntToScalar(fTextureSize.width()), 112 SkIntToScalar(fTextureSize.width()),
113 SkIntToScalar(fTextureSize.height())); 113 SkIntToScalar(fTextureSize.height()));
114 } 114 }
115 } 115 }
116 116
117 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) { 117 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) {
118 const GrDistanceFieldTextureEffect& dfTexEffect = 118 const GrDistanceFieldTextureEffect& dfTexEffect =
119 drawEffect.castEffect<GrDistanceFi eldTextureEffect>(); 119 drawEffect.castEffect<GrDistanceFi eldTextureEffect>();
120 120
121 return dfTexEffect.isUniformScale() ? 0x1 : 0x0; 121 return dfTexEffect.isSimilarity() ? 0x1 : 0x0;
122 } 122 }
123 123
124 private: 124 private:
125 GrGLUniformManager::UniformHandle fTextureSizeUni; 125 GrGLUniformManager::UniformHandle fTextureSizeUni;
126 SkISize fTextureSize; 126 SkISize fTextureSize;
127 127
128 typedef GrGLVertexEffect INHERITED; 128 typedef GrGLVertexEffect INHERITED;
129 }; 129 };
130 130
131 /////////////////////////////////////////////////////////////////////////////// 131 ///////////////////////////////////////////////////////////////////////////////
132 132
133 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, 133 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
134 const GrTextureParams & params, 134 const GrTextureParams & params,
135 bool uniformScale) 135 bool similarity)
136 : fTextureAccess(texture, params) 136 : fTextureAccess(texture, params)
137 , fUniformScale(uniformScale) { 137 , fIsSimilarity(similarity) {
138 this->addTextureAccess(&fTextureAccess); 138 this->addTextureAccess(&fTextureAccess);
139 this->addVertexAttrib(kVec2f_GrSLType); 139 this->addVertexAttrib(kVec2f_GrSLType);
140 } 140 }
141 141
142 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { 142 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
143 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other); 143 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other);
144 return fTextureAccess == cte.fTextureAccess; 144 return fTextureAccess == cte.fTextureAccess;
145 } 145 }
146 146
147 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, 147 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color,
(...skipping 25 matching lines...) Expand all
173 SkShader::kRepeat_TileMode, 173 SkShader::kRepeat_TileMode,
174 SkShader::kMirror_TileMode, 174 SkShader::kMirror_TileMode,
175 }; 175 };
176 SkShader::TileMode tileModes[] = { 176 SkShader::TileMode tileModes[] = {
177 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 177 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
178 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 178 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
179 }; 179 };
180 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : 180 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode :
181 GrTextureParams::kNon e_FilterMode); 181 GrTextureParams::kNon e_FilterMode);
182 182
183 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, random ->nextBool()); 183 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params,
184 } 184 random->nextBool());
185 }
186
187 ///////////////////////////////////////////////////////////////////////////////
188
189 class GrGLDistanceFieldLCDTextureEffect : public GrGLVertexEffect {
190 public:
191 GrGLDistanceFieldLCDTextureEffect(const GrBackendEffectFactory& factory,
192 const GrDrawEffect& drawEffect)
193 : INHERITED (factory)
194 , fTextureSize(SkISize::Make(-1,-1)) {}
195
196 virtual void emitCode(GrGLFullShaderBuilder* builder,
197 const GrDrawEffect& drawEffect,
198 EffectKey key,
199 const char* outputColor,
200 const char* inputColor,
201 const TransformedCoordsArray&,
202 const TextureSamplerArray& samplers) SK_OVERRIDE {
203 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>().n umVertexAttribs());
204
205 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva tives_GLSLFeature));
206 const GrDistanceFieldLCDTextureEffect& dfTexEffect =
207 drawEffect.castEffect<GrDistanceField LCDTextureEffect>();
208
209 SkString fsCoordName;
210 const char* vsCoordName;
211 const char* fsCoordNamePtr;
212 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr);
213 fsCoordName = fsCoordNamePtr;
214
215 const char* attrName0 =
216 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])- >c_str();
217 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0);
218
219 const char* textureSizeUniName = NULL;
220 // width, height, 1/(3*width)
221 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib ility,
222 kVec3f_GrSLType, "TextureSize",
223 &textureSizeUniName);
224
225 // create LCD offset adjusted by inverse of transform
226 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str());
227 builder->fsCodeAppendf("\tvec2 st = uv*%s.xy;\n", textureSizeUniName);
228 if (dfTexEffect.isUniformScale()) {
229 builder->fsCodeAppend("\tfloat dx = dFdx(st.x);\n");
230 builder->fsCodeAppendf("\tvec2 offset = vec2(dx*%s.z, 0.0);\n", text ureSizeUniName);
231 } else {
232 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n");
233 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n");
234 builder->fsCodeAppendf("\tvec2 offset = %s.z*Jdx;\n", textureSizeUni Name);
235 }
236
237 // green is distance to uv center
238 builder->fsCodeAppend("\tvec4 texColor = ");
239 builder->fsAppendTextureLookup(samplers[0], "uv", kVec2f_GrSLType);
240 builder->fsCodeAppend(";\n");
241 builder->fsCodeAppend("\tvec3 distance;\n");
242 builder->fsCodeAppend("\tdistance.y = " MULTIPLIER "*(texColor.r - " THR ESHOLD ");\n");
243 // red is distance to left offset
244 builder->fsCodeAppend("\tvec2 uv_adjusted = uv - offset;\n");
245 builder->fsCodeAppend("\ttexColor = ");
246 builder->fsAppendTextureLookup(samplers[0], "uv_adjusted", kVec2f_GrSLTy pe);
247 builder->fsCodeAppend(";\n");
248 builder->fsCodeAppend("\tdistance.x = " MULTIPLIER "*(texColor.r - " THR ESHOLD ");\n");
249 // blue is distance to right offset
250 builder->fsCodeAppend("\tuv_adjusted = uv + offset;\n");
251 builder->fsCodeAppend("\ttexColor = ");
252 builder->fsAppendTextureLookup(samplers[0], "uv_adjusted", kVec2f_GrSLTy pe);
253 builder->fsCodeAppend(";\n");
254 builder->fsCodeAppend("\tdistance.z = " MULTIPLIER "*(texColor.r - " THR ESHOLD ");\n");
255
256 // we adjust for the effect of the transformation on the distance by usi ng
257 // the length of the gradient of the texture coordinates. We use st coor dinates
258 // to ensure we're mapping 1:1 from texel space to pixel space.
259
260 // To be strictly correct, we should compute the anti-aliasing factor se parately
261 // for each color component. However, this is only important when using perspective
262 // transformations, and even then using a single factor seems like a rea sonable
263 // trade-off between quality and speed.
264 builder->fsCodeAppend("\tfloat afwidth;\n");
265 if (dfTexEffect.isUniformScale()) {
266 // this gives us a smooth step across approximately one fragment
267 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2)
268 builder->fsCodeAppend("\tafwidth = 0.7071*dx;\n");
269 } else {
270 builder->fsCodeAppend("\tvec2 uv_grad;\n");
271 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) {
272 // this is to compensate for the Adreno, which likes to drop til es on division by 0
273 builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n");
274 builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n");
275 builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n");
276 builder->fsCodeAppend("\t} else {\n");
277 builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n" );
278 builder->fsCodeAppend("\t}\n");
279 } else {
280 builder->fsCodeAppend("\tuv_grad = normalize(uv);\n");
281 }
282 builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad. y*Jdy.x,\n");
283 builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad. y*Jdy.y);\n");
284
285 // this gives us a smooth step across approximately one fragment
286 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2)
287 builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n");
288 }
289
290 builder->fsCodeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3 (afwidth), distance), 1.0);\n");
291
292 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
293 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("val")).c_ str());
294 }
295
296 virtual void setData(const GrGLUniformManager& uman,
297 const GrDrawEffect& drawEffect) SK_OVERRIDE {
298 SkASSERT(fTextureSizeUni.isValid());
299
300 GrTexture* texture = drawEffect.effect()->get()->texture(0);
301 if (texture->width() != fTextureSize.width() ||
302 texture->height() != fTextureSize.height()) {
303 const GrDistanceFieldLCDTextureEffect& dfTexEffect =
304 drawEffect.castEffect<GrDistanceField LCDTextureEffect>();
305 fTextureSize = SkISize::Make(texture->width(), texture->height());
306 float delta = 1.0f/(3.0f*texture->width());
307 if (dfTexEffect.useBGR()) {
308 delta = -delta;
309 }
310 uman.set3f(fTextureSizeUni,
311 SkIntToScalar(fTextureSize.width()),
312 SkIntToScalar(fTextureSize.height()),
313 delta);
314 }
315 }
316
317 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) {
318 const GrDistanceFieldLCDTextureEffect& dfTexEffect =
319 drawEffect.castEffect<GrDistanceField LCDTextureEffect>();
320
321 int uniformScale = dfTexEffect.isUniformScale() ? 0x01 : 0x00;
322 int useBGR = dfTexEffect.useBGR() ? 0x10 : 0x00;
323 return uniformScale | useBGR;
324 }
325
326 private:
327 GrGLUniformManager::UniformHandle fTextureSizeUni;
328 SkISize fTextureSize;
329
330 typedef GrGLVertexEffect INHERITED;
331 };
332
333 ///////////////////////////////////////////////////////////////////////////////
334
335 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect(GrTexture* text ure,
336 const GrTexture Params& params,
337 bool uniformSca le,
338 bool useBGR)
339 : fTextureAccess(texture, params)
340 , fUniformScale(uniformScale)
341 , fUseBGR(useBGR) {
342 this->addTextureAccess(&fTextureAccess);
343 this->addVertexAttrib(kVec2f_GrSLType);
344 }
345
346 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrEffect& other) const {
347 const GrDistanceFieldLCDTextureEffect& cte = CastEffect<GrDistanceFieldLCDTe xtureEffect>(other);
348 return fTextureAccess == cte.fTextureAccess;
349 }
350
351 void GrDistanceFieldLCDTextureEffect::getConstantColorComponents(GrColor* color,
352 uint32_t* valid Flags) const {
353 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) &&
354 GrPixelConfigIsOpaque(this->texture(0)->config())) {
355 *validFlags = kA_GrColorComponentFlag;
356 } else {
357 *validFlags = 0;
358 }
359 }
360
361 const GrBackendEffectFactory& GrDistanceFieldLCDTextureEffect::getFactory() cons t {
362 return GrTBackendEffectFactory<GrDistanceFieldLCDTextureEffect>::getInstance ();
363 }
364
365 ///////////////////////////////////////////////////////////////////////////////
366
367 GR_DEFINE_EFFECT_TEST(GrDistanceFieldLCDTextureEffect);
368
369 GrEffectRef* GrDistanceFieldLCDTextureEffect::TestCreate(SkRandom* random,
370 GrContext*,
371 const GrDrawTargetCaps& ,
372 GrTexture* textures[]) {
373 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
374 GrEffectUnitTest::kAlphaTextureIdx;
375 static const SkShader::TileMode kTileModes[] = {
376 SkShader::kClamp_TileMode,
377 SkShader::kRepeat_TileMode,
378 SkShader::kMirror_TileMode,
379 };
380 SkShader::TileMode tileModes[] = {
381 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
382 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
383 };
384 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode :
385 GrTextureParams::kNone_FilterMode);
386
387 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params,
388 random->nextBool(), random->n extBool());
389 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrDistanceFieldTextureEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698