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