OLD | NEW |
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 "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
9 #include "GrDistanceFieldTextureEffect.h" | 9 #include "GrDistanceFieldTextureEffect.h" |
10 #include "gl/GrGLEffect.h" | 10 #include "gl/GrGLEffect.h" |
11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
12 #include "gl/GrGLTexture.h" | 12 #include "gl/GrGLTexture.h" |
13 #include "gl/GrGLVertexEffect.h" | 13 #include "gl/GrGLGeometryProcessor.h" |
14 #include "GrTBackendEffectFactory.h" | 14 #include "GrTBackendEffectFactory.h" |
15 #include "GrTexture.h" | 15 #include "GrTexture.h" |
16 | 16 |
17 #include "SkDistanceFieldGen.h" | 17 #include "SkDistanceFieldGen.h" |
18 | 18 |
19 // To get optical sizes people don't complain about when we blit correctly, | 19 // To get optical sizes people don't complain about when we blit correctly, |
20 // we need to slightly bold each glyph. On the Mac, we need a larger bold value. | 20 // we need to slightly bold each glyph. On the Mac, we need a larger bold value. |
21 #if defined(SK_BUILD_FOR_MAC) | 21 #if defined(SK_BUILD_FOR_MAC) |
22 #define SK_DistanceFieldLCDFactor "0.33" | 22 #define SK_DistanceFieldLCDFactor "0.33" |
23 #define SK_DistanceFieldNonLCDFactor "0.25" | 23 #define SK_DistanceFieldNonLCDFactor "0.25" |
24 #else | 24 #else |
25 #define SK_DistanceFieldLCDFactor "0.05" | 25 #define SK_DistanceFieldLCDFactor "0.05" |
26 #define SK_DistanceFieldNonLCDFactor "0.05" | 26 #define SK_DistanceFieldNonLCDFactor "0.05" |
27 #endif | 27 #endif |
28 | 28 |
29 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 | 29 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 |
30 #define SK_DistanceFieldAAFactor "0.7071" | 30 #define SK_DistanceFieldAAFactor "0.7071" |
31 | 31 |
32 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { | 32 |
| 33 const GrShaderVar kAttrTextureCoords("aTextureCoords", |
| 34 kVec2f_GrSLType, |
| 35 GrShaderVar::kAttribute_TypeModifier); |
| 36 |
| 37 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { |
33 public: | 38 public: |
34 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, | 39 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, |
35 const GrDrawEffect& drawEffect) | 40 const GrDrawEffect& drawEffect) |
36 : INHERITED (factory) | 41 : INHERITED (factory) |
37 , fTextureSize(SkISize::Make(-1,-1)) {} | 42 , fTextureSize(SkISize::Make(-1,-1)) {} |
38 | 43 |
39 virtual void emitCode(GrGLFullProgramBuilder* builder, | 44 virtual void emitCode(GrGLFullProgramBuilder* builder, |
40 const GrDrawEffect& drawEffect, | 45 const GrDrawEffect& drawEffect, |
41 const GrEffectKey& key, | 46 const GrEffectKey& key, |
42 const char* outputColor, | 47 const char* outputColor, |
43 const char* inputColor, | 48 const char* inputColor, |
44 const TransformedCoordsArray&, | 49 const TransformedCoordsArray&, |
45 const TextureSamplerArray& samplers) SK_OVERRIDE { | 50 const TextureSamplerArray& samplers) SK_OVERRIDE { |
46 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV
ertexAttribs()); | 51 SkASSERT(1 == |
| 52 drawEffect.castEffect<GrDistanceFieldTextureEffect>().getVertexA
ttribs().count()); |
47 | 53 |
48 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); | 54 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); |
49 SkAssertResult(fsBuilder->enableFeature( | 55 SkAssertResult(fsBuilder->enableFeature( |
50 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 56 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
51 const GrDistanceFieldTextureEffect& dfTexEffect = | 57 const GrDistanceFieldTextureEffect& dfTexEffect = |
52 drawEffect.castEffect<GrDistanceFi
eldTextureEffect>(); | 58 drawEffect.castEffect<GrDistanceFi
eldTextureEffect>(); |
53 | 59 |
54 SkString fsCoordName; | 60 SkString fsCoordName; |
55 const char* vsCoordName; | 61 const char* vsCoordName; |
56 const char* fsCoordNamePtr; | 62 const char* fsCoordNamePtr; |
57 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); | 63 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); |
58 fsCoordName = fsCoordNamePtr; | 64 fsCoordName = fsCoordNamePtr; |
59 | 65 |
60 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); | 66 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
61 const SkString* attr0Name = | 67 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, kAttrTextureCoords.c
_str()); |
62 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices(
)[0]); | |
63 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str()); | |
64 | 68 |
65 const char* textureSizeUniName = NULL; | 69 const char* textureSizeUniName = NULL; |
66 fTextureSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visi
bility, | 70 fTextureSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visi
bility, |
67 kVec2f_GrSLType, "TextureSize", | 71 kVec2f_GrSLType, "TextureSize", |
68 &textureSizeUniName); | 72 &textureSizeUniName); |
69 | 73 |
70 fsBuilder->codeAppend("\tvec4 texColor = "); | 74 fsBuilder->codeAppend("\tvec4 texColor = "); |
71 fsBuilder->appendTextureLookup(samplers[0], | 75 fsBuilder->appendTextureLookup(samplers[0], |
72 fsCoordName.c_str(), | 76 fsCoordName.c_str(), |
73 kVec2f_GrSLType); | 77 kVec2f_GrSLType); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 162 |
159 b->add32(dfTexEffect.getFlags()); | 163 b->add32(dfTexEffect.getFlags()); |
160 } | 164 } |
161 | 165 |
162 private: | 166 private: |
163 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | 167 GrGLProgramDataManager::UniformHandle fTextureSizeUni; |
164 SkISize fTextureSize; | 168 SkISize fTextureSize; |
165 GrGLProgramDataManager::UniformHandle fLuminanceUni; | 169 GrGLProgramDataManager::UniformHandle fLuminanceUni; |
166 float fLuminance; | 170 float fLuminance; |
167 | 171 |
168 typedef GrGLVertexEffect INHERITED; | 172 typedef GrGLGeometryProcessor INHERITED; |
169 }; | 173 }; |
170 | 174 |
171 /////////////////////////////////////////////////////////////////////////////// | 175 /////////////////////////////////////////////////////////////////////////////// |
172 | 176 |
173 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, | 177 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
174 const GrTextureParams
& params, | 178 const GrTextureParams
& params, |
175 #ifdef SK_GAMMA_APPLY_TO_A8 | 179 #ifdef SK_GAMMA_APPLY_TO_A8 |
176 GrTexture* gamma, | 180 GrTexture* gamma, |
177 const GrTextureParams
& gammaParams, | 181 const GrTextureParams
& gammaParams, |
178 float luminance, | 182 float luminance, |
179 #endif | 183 #endif |
180 uint32_t flags) | 184 uint32_t flags) |
181 : fTextureAccess(texture, params) | 185 : fTextureAccess(texture, params) |
182 #ifdef SK_GAMMA_APPLY_TO_A8 | 186 #ifdef SK_GAMMA_APPLY_TO_A8 |
183 , fGammaTextureAccess(gamma, gammaParams) | 187 , fGammaTextureAccess(gamma, gammaParams) |
184 , fLuminance(luminance) | 188 , fLuminance(luminance) |
185 #endif | 189 #endif |
186 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) { | 190 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) { |
187 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); | 191 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
188 this->addTextureAccess(&fTextureAccess); | 192 this->addTextureAccess(&fTextureAccess); |
189 #ifdef SK_GAMMA_APPLY_TO_A8 | 193 #ifdef SK_GAMMA_APPLY_TO_A8 |
190 this->addTextureAccess(&fGammaTextureAccess); | 194 this->addTextureAccess(&fGammaTextureAccess); |
191 #endif | 195 #endif |
192 this->addVertexAttrib(kVec2f_GrSLType); | 196 this->addVertexAttrib(kAttrTextureCoords); |
193 } | 197 } |
194 | 198 |
195 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { | 199 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
196 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE
ffect>(other); | 200 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE
ffect>(other); |
197 return fTextureAccess == cte.fTextureAccess && | 201 return fTextureAccess == cte.fTextureAccess && |
198 #ifdef SK_GAMMA_APPLY_TO_A8 | 202 #ifdef SK_GAMMA_APPLY_TO_A8 |
199 fGammaTextureAccess == cte.fGammaTextureAccess && | 203 fGammaTextureAccess == cte.fGammaTextureAccess && |
200 fLuminance == cte.fLuminance && | 204 fLuminance == cte.fLuminance && |
201 #endif | 205 #endif |
202 fFlags == cte.fFlags; | 206 fFlags == cte.fFlags; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 #ifdef SK_GAMMA_APPLY_TO_A8 | 254 #ifdef SK_GAMMA_APPLY_TO_A8 |
251 textures[texIdx2], params2, | 255 textures[texIdx2], params2, |
252 random->nextF(), | 256 random->nextF(), |
253 #endif | 257 #endif |
254 random->nextBool() ? | 258 random->nextBool() ? |
255 kSimilarity_DistanceFieldEff
ectFlag : 0); | 259 kSimilarity_DistanceFieldEff
ectFlag : 0); |
256 } | 260 } |
257 | 261 |
258 /////////////////////////////////////////////////////////////////////////////// | 262 /////////////////////////////////////////////////////////////////////////////// |
259 | 263 |
260 class GrGLDistanceFieldLCDTextureEffect : public GrGLVertexEffect { | 264 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
261 public: | 265 public: |
262 GrGLDistanceFieldLCDTextureEffect(const GrBackendEffectFactory& factory, | 266 GrGLDistanceFieldLCDTextureEffect(const GrBackendEffectFactory& factory, |
263 const GrDrawEffect& drawEffect) | 267 const GrDrawEffect& drawEffect) |
264 : INHERITED (factory) | 268 : INHERITED (factory) |
265 , fTextureSize(SkISize::Make(-1,-1)) {} | 269 , fTextureSize(SkISize::Make(-1,-1)) {} |
266 | 270 |
267 virtual void emitCode(GrGLFullProgramBuilder* builder, | 271 virtual void emitCode(GrGLFullProgramBuilder* builder, |
268 const GrDrawEffect& drawEffect, | 272 const GrDrawEffect& drawEffect, |
269 const GrEffectKey& key, | 273 const GrEffectKey& key, |
270 const char* outputColor, | 274 const char* outputColor, |
271 const char* inputColor, | 275 const char* inputColor, |
272 const TransformedCoordsArray&, | 276 const TransformedCoordsArray&, |
273 const TextureSamplerArray& samplers) SK_OVERRIDE { | 277 const TextureSamplerArray& samplers) SK_OVERRIDE { |
274 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>().n
umVertexAttribs()); | 278 SkASSERT(1 == |
| 279 drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>() |
| 280 .getVertexAttribs().count()); |
275 | 281 |
276 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 282 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
277 drawEffect.castEffect<GrDistanceField
LCDTextureEffect>(); | 283 drawEffect.castEffect<GrDistanceField
LCDTextureEffect>(); |
278 | 284 |
279 SkString fsCoordName; | 285 SkString fsCoordName; |
280 const char* vsCoordName; | 286 const char* vsCoordName; |
281 const char* fsCoordNamePtr; | 287 const char* fsCoordNamePtr; |
282 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); | 288 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); |
283 fsCoordName = fsCoordNamePtr; | 289 fsCoordName = fsCoordNamePtr; |
284 | 290 |
285 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); | 291 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
286 const SkString* attr0Name = | 292 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, kAttrTextureCoords.c
_str()); |
287 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices(
)[0]); | |
288 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str()); | |
289 | 293 |
290 const char* textureSizeUniName = NULL; | 294 const char* textureSizeUniName = NULL; |
291 // width, height, 1/(3*width) | 295 // width, height, 1/(3*width) |
292 fTextureSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visi
bility, | 296 fTextureSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visi
bility, |
293 kVec3f_GrSLType, "TextureSize", | 297 kVec3f_GrSLType, "TextureSize", |
294 &textureSizeUniName); | 298 &textureSizeUniName); |
295 | 299 |
296 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); | 300 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); |
297 | 301 |
298 SkAssertResult(fsBuilder->enableFeature( | 302 SkAssertResult(fsBuilder->enableFeature( |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 440 |
437 b->add32(dfTexEffect.getFlags()); | 441 b->add32(dfTexEffect.getFlags()); |
438 } | 442 } |
439 | 443 |
440 private: | 444 private: |
441 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | 445 GrGLProgramDataManager::UniformHandle fTextureSizeUni; |
442 SkISize fTextureSize; | 446 SkISize fTextureSize; |
443 GrGLProgramDataManager::UniformHandle fTextColorUni; | 447 GrGLProgramDataManager::UniformHandle fTextColorUni; |
444 SkColor fTextColor; | 448 SkColor fTextColor; |
445 | 449 |
446 typedef GrGLVertexEffect INHERITED; | 450 typedef GrGLGeometryProcessor INHERITED; |
447 }; | 451 }; |
448 | 452 |
449 /////////////////////////////////////////////////////////////////////////////// | 453 /////////////////////////////////////////////////////////////////////////////// |
450 | 454 |
451 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( | 455 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( |
452 GrTexture* texture, const GrTe
xtureParams& params, | 456 GrTexture* texture, const GrTe
xtureParams& params, |
453 GrTexture* gamma, const GrText
ureParams& gParams, | 457 GrTexture* gamma, const GrText
ureParams& gParams, |
454 SkColor textColor, | 458 SkColor textColor, |
455 uint32_t flags) | 459 uint32_t flags) |
456 : fTextureAccess(texture, params) | 460 : fTextureAccess(texture, params) |
457 , fGammaTextureAccess(gamma, gParams) | 461 , fGammaTextureAccess(gamma, gParams) |
458 , fTextColor(textColor) | 462 , fTextColor(textColor) |
459 , fFlags(flags & kLCD_DistanceFieldEffectMask) { | 463 , fFlags(flags & kLCD_DistanceFieldEffectMask) { |
460 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); | 464 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); |
461 | 465 |
462 this->addTextureAccess(&fTextureAccess); | 466 this->addTextureAccess(&fTextureAccess); |
463 this->addTextureAccess(&fGammaTextureAccess); | 467 this->addTextureAccess(&fGammaTextureAccess); |
464 this->addVertexAttrib(kVec2f_GrSLType); | 468 this->addVertexAttrib(kAttrTextureCoords); |
465 } | 469 } |
466 | 470 |
467 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrEffect& other) const { | 471 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrEffect& other) const { |
468 const GrDistanceFieldLCDTextureEffect& cte = | 472 const GrDistanceFieldLCDTextureEffect& cte = |
469 CastEffect<GrDistanceFieldLCDTexture
Effect>(other); | 473 CastEffect<GrDistanceFieldLCDTexture
Effect>(other); |
470 return (fTextureAccess == cte.fTextureAccess && | 474 return (fTextureAccess == cte.fTextureAccess && |
471 fGammaTextureAccess == cte.fGammaTextureAccess && | 475 fGammaTextureAccess == cte.fGammaTextureAccess && |
472 fTextColor == cte.fTextColor && | 476 fTextColor == cte.fTextColor && |
473 fFlags == cte.fFlags); | 477 fFlags == cte.fFlags); |
474 } | 478 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 random->nextULessThan(256), | 521 random->nextULessThan(256), |
518 random->nextULessThan(256)); | 522 random->nextULessThan(256)); |
519 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 523 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
520 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 524 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
521 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 525 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
522 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, | 526 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, |
523 textures[texIdx2], params2, | 527 textures[texIdx2], params2, |
524 textColor, | 528 textColor, |
525 flags); | 529 flags); |
526 } | 530 } |
OLD | NEW |