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 "GrDistanceFieldTextureEffect.h" | 8 #include "GrDistanceFieldTextureEffect.h" |
9 #include "GrInvariantOutput.h" | 9 #include "GrInvariantOutput.h" |
10 #include "gl/builders/GrGLProgramBuilder.h" | 10 #include "gl/builders/GrGLProgramBuilder.h" |
11 #include "gl/GrGLProcessor.h" | 11 #include "gl/GrGLProcessor.h" |
12 #include "gl/GrGLSL.h" | 12 #include "gl/GrGLSL.h" |
13 #include "gl/GrGLTexture.h" | 13 #include "gl/GrGLTexture.h" |
14 #include "gl/GrGLGeometryProcessor.h" | 14 #include "gl/GrGLGeometryProcessor.h" |
15 #include "GrTBackendProcessorFactory.h" | 15 #include "GrTBackendProcessorFactory.h" |
16 #include "GrTexture.h" | 16 #include "GrTexture.h" |
17 | 17 |
18 #include "SkDistanceFieldGen.h" | 18 #include "SkDistanceFieldGen.h" |
19 | 19 |
20 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 | 20 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 |
21 #define SK_DistanceFieldAAFactor "0.7071" | 21 #define SK_DistanceFieldAAFactor "0.7071" |
22 | 22 |
23 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { | 23 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { |
24 public: | 24 public: |
25 GrGLDistanceFieldTextureEffect(const GrBackendProcessorFactory& factory, | 25 GrGLDistanceFieldTextureEffect(const GrBackendProcessorFactory& factory, |
26 const GrProcessor&) | 26 const GrGeometryProcessor&, |
| 27 const GrBatchTracker&) |
27 : INHERITED (factory) | 28 : INHERITED (factory) |
28 , fTextureSize(SkISize::Make(-1,-1)) | 29 , fTextureSize(SkISize::Make(-1,-1)) |
29 #ifdef SK_GAMMA_APPLY_TO_A8 | 30 #ifdef SK_GAMMA_APPLY_TO_A8 |
30 , fLuminance(-1.0f) | 31 , fLuminance(-1.0f) |
31 #endif | 32 #endif |
32 {} | 33 {} |
33 | 34 |
34 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { | 35 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { |
35 const GrDistanceFieldTextureEffect& dfTexEffect = | 36 const GrDistanceFieldTextureEffect& dfTexEffect = |
36 args.fGP.cast<GrDistanceFieldTextureEffect>(); | 37 args.fGP.cast<GrDistanceFieldTextureEffect>(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 fsBuilder->codeAppend("\tvec4 gammaColor = "); | 118 fsBuilder->codeAppend("\tvec4 gammaColor = "); |
118 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 119 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
119 fsBuilder->codeAppend(";\n"); | 120 fsBuilder->codeAppend(";\n"); |
120 fsBuilder->codeAppend("\tval = gammaColor.r;\n"); | 121 fsBuilder->codeAppend("\tval = gammaColor.r;\n"); |
121 #endif | 122 #endif |
122 | 123 |
123 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 124 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
124 } | 125 } |
125 | 126 |
126 virtual void setData(const GrGLProgramDataManager& pdman, | 127 virtual void setData(const GrGLProgramDataManager& pdman, |
127 const GrProcessor& effect) SK_OVERRIDE { | 128 const GrGeometryProcessor& proc, |
| 129 const GrBatchTracker&) SK_OVERRIDE { |
128 SkASSERT(fTextureSizeUni.isValid()); | 130 SkASSERT(fTextureSizeUni.isValid()); |
129 | 131 |
130 GrTexture* texture = effect.texture(0); | 132 GrTexture* texture = proc.texture(0); |
131 if (texture->width() != fTextureSize.width() || | 133 if (texture->width() != fTextureSize.width() || |
132 texture->height() != fTextureSize.height()) { | 134 texture->height() != fTextureSize.height()) { |
133 fTextureSize = SkISize::Make(texture->width(), texture->height()); | 135 fTextureSize = SkISize::Make(texture->width(), texture->height()); |
134 pdman.set2f(fTextureSizeUni, | 136 pdman.set2f(fTextureSizeUni, |
135 SkIntToScalar(fTextureSize.width()), | 137 SkIntToScalar(fTextureSize.width()), |
136 SkIntToScalar(fTextureSize.height())); | 138 SkIntToScalar(fTextureSize.height())); |
137 } | 139 } |
138 #ifdef SK_GAMMA_APPLY_TO_A8 | 140 #ifdef SK_GAMMA_APPLY_TO_A8 |
139 const GrDistanceFieldTextureEffect& dfTexEffect = | 141 const GrDistanceFieldTextureEffect& dfTexEffect = |
140 effect.cast<GrDistanceFieldTextureEffect>(); | 142 proc.cast<GrDistanceFieldTextureEffect>(); |
141 float luminance = dfTexEffect.getLuminance(); | 143 float luminance = dfTexEffect.getLuminance(); |
142 if (luminance != fLuminance) { | 144 if (luminance != fLuminance) { |
143 pdman.set1f(fLuminanceUni, luminance); | 145 pdman.set1f(fLuminanceUni, luminance); |
144 fLuminance = luminance; | 146 fLuminance = luminance; |
145 } | 147 } |
146 #endif | 148 #endif |
147 } | 149 } |
148 | 150 |
149 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&, | 151 static inline void GenKey(const GrGeometryProcessor& processor, |
| 152 const GrBatchTracker&, |
| 153 const GrGLCaps&, |
150 GrProcessorKeyBuilder* b) { | 154 GrProcessorKeyBuilder* b) { |
151 const GrDistanceFieldTextureEffect& dfTexEffect = | 155 const GrDistanceFieldTextureEffect& dfTexEffect = |
152 processor.cast<GrDistanceFieldTextureEffect>(); | 156 processor.cast<GrDistanceFieldTextureEffect>(); |
153 | 157 |
154 b->add32(dfTexEffect.getFlags()); | 158 b->add32(dfTexEffect.getFlags()); |
155 } | 159 } |
156 | 160 |
157 private: | 161 private: |
158 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | 162 GrGLProgramDataManager::UniformHandle fTextureSizeUni; |
159 SkISize fTextureSize; | 163 SkISize fTextureSize; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 #endif | 252 #endif |
249 random->nextBool() ? | 253 random->nextBool() ? |
250 kSimilarity_DistanceFieldEff
ectFlag : 0); | 254 kSimilarity_DistanceFieldEff
ectFlag : 0); |
251 } | 255 } |
252 | 256 |
253 /////////////////////////////////////////////////////////////////////////////// | 257 /////////////////////////////////////////////////////////////////////////////// |
254 | 258 |
255 class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { | 259 class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { |
256 public: | 260 public: |
257 GrGLDistanceFieldNoGammaTextureEffect(const GrBackendProcessorFactory& facto
ry, | 261 GrGLDistanceFieldNoGammaTextureEffect(const GrBackendProcessorFactory& facto
ry, |
258 const GrProcessor& effect) | 262 const GrGeometryProcessor&, |
| 263 const GrBatchTracker&) |
259 : INHERITED(factory) | 264 : INHERITED(factory) |
260 , fTextureSize(SkISize::Make(-1, -1)) {} | 265 , fTextureSize(SkISize::Make(-1, -1)) {} |
261 | 266 |
262 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { | 267 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { |
263 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = | 268 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = |
264 args.fGP.cast<GrDistanceFieldNoGammaTextureEffect>(); | 269 args.fGP.cast<GrDistanceFieldNoGammaTextureEffect>(); |
265 | 270 |
266 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 271 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
267 SkAssertResult(fsBuilder->enableFeature( | 272 SkAssertResult(fsBuilder->enableFeature( |
268 GrGLFragmentShaderBuilder::kStandardDerivat
ives_GLSLFeature)); | 273 GrGLFragmentShaderBuilder::kStandardDerivat
ives_GLSLFeature)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 336 |
332 // this gives us a smooth step across approximately one fragment | 337 // this gives us a smooth step across approximately one fragment |
333 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); | 338 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); |
334 } | 339 } |
335 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); | 340 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); |
336 | 341 |
337 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 342 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
338 } | 343 } |
339 | 344 |
340 virtual void setData(const GrGLProgramDataManager& pdman, | 345 virtual void setData(const GrGLProgramDataManager& pdman, |
341 const GrProcessor& effect) SK_OVERRIDE { | 346 const GrGeometryProcessor& proc, |
| 347 const GrBatchTracker&) SK_OVERRIDE { |
342 SkASSERT(fTextureSizeUni.isValid()); | 348 SkASSERT(fTextureSizeUni.isValid()); |
343 | 349 |
344 GrTexture* texture = effect.texture(0); | 350 GrTexture* texture = proc.texture(0); |
345 if (texture->width() != fTextureSize.width() || | 351 if (texture->width() != fTextureSize.width() || |
346 texture->height() != fTextureSize.height()) { | 352 texture->height() != fTextureSize.height()) { |
347 fTextureSize = SkISize::Make(texture->width(), texture->height()); | 353 fTextureSize = SkISize::Make(texture->width(), texture->height()); |
348 pdman.set2f(fTextureSizeUni, | 354 pdman.set2f(fTextureSizeUni, |
349 SkIntToScalar(fTextureSize.width()), | 355 SkIntToScalar(fTextureSize.width()), |
350 SkIntToScalar(fTextureSize.height())); | 356 SkIntToScalar(fTextureSize.height())); |
351 } | 357 } |
352 } | 358 } |
353 | 359 |
354 static inline void GenKey(const GrProcessor& effect, const GrGLCaps&, | 360 static inline void GenKey(const GrGeometryProcessor& proc, |
| 361 const GrBatchTracker&, |
| 362 const GrGLCaps&, |
355 GrProcessorKeyBuilder* b) { | 363 GrProcessorKeyBuilder* b) { |
356 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = | 364 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = |
357 effect.cast<GrDistanceFieldNoGammaTextureEffect>(); | 365 proc.cast<GrDistanceFieldNoGammaTextureEffect>(); |
358 | 366 |
359 b->add32(dfTexEffect.getFlags()); | 367 b->add32(dfTexEffect.getFlags()); |
360 } | 368 } |
361 | 369 |
362 private: | 370 private: |
363 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | 371 GrGLProgramDataManager::UniformHandle fTextureSizeUni; |
364 SkISize fTextureSize; | 372 SkISize fTextureSize; |
365 | 373 |
366 typedef GrGLGeometryProcessor INHERITED; | 374 typedef GrGLGeometryProcessor INHERITED; |
367 }; | 375 }; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 431 |
424 return GrDistanceFieldNoGammaTextureEffect::Create(textures[texIdx], params, | 432 return GrDistanceFieldNoGammaTextureEffect::Create(textures[texIdx], params, |
425 random->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0); | 433 random->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0); |
426 } | 434 } |
427 | 435 |
428 /////////////////////////////////////////////////////////////////////////////// | 436 /////////////////////////////////////////////////////////////////////////////// |
429 | 437 |
430 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { | 438 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
431 public: | 439 public: |
432 GrGLDistanceFieldLCDTextureEffect(const GrBackendProcessorFactory& factory, | 440 GrGLDistanceFieldLCDTextureEffect(const GrBackendProcessorFactory& factory, |
433 const GrProcessor&) | 441 const GrGeometryProcessor&, |
| 442 const GrBatchTracker&) |
434 : INHERITED (factory) | 443 : INHERITED (factory) |
435 , fTextureSize(SkISize::Make(-1,-1)) | 444 , fTextureSize(SkISize::Make(-1,-1)) |
436 , fTextColor(GrColor_ILLEGAL) {} | 445 , fTextColor(GrColor_ILLEGAL) {} |
437 | 446 |
438 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { | 447 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { |
439 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 448 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
440 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); | 449 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); |
441 | 450 |
442 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 451 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
443 GrGLVertToFrag v(kVec2f_GrSLType); | 452 GrGLVertToFrag v(kVec2f_GrSLType); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); | 565 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); |
557 fsBuilder->codeAppend("\tgammaColor = "); | 566 fsBuilder->codeAppend("\tgammaColor = "); |
558 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 567 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
559 fsBuilder->codeAppend(";\n"); | 568 fsBuilder->codeAppend(";\n"); |
560 fsBuilder->codeAppend("\tval.z = gammaColor.r;\n"); | 569 fsBuilder->codeAppend("\tval.z = gammaColor.r;\n"); |
561 | 570 |
562 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 571 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
563 } | 572 } |
564 | 573 |
565 virtual void setData(const GrGLProgramDataManager& pdman, | 574 virtual void setData(const GrGLProgramDataManager& pdman, |
566 const GrProcessor& processor) SK_OVERRIDE { | 575 const GrGeometryProcessor& processor, |
| 576 const GrBatchTracker&) SK_OVERRIDE { |
567 SkASSERT(fTextureSizeUni.isValid()); | 577 SkASSERT(fTextureSizeUni.isValid()); |
568 SkASSERT(fTextColorUni.isValid()); | 578 SkASSERT(fTextColorUni.isValid()); |
569 | 579 |
570 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 580 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
571 processor.cast<GrDistanceFieldLCDTextureEffect>(); | 581 processor.cast<GrDistanceFieldLCDTextureEffect>(); |
572 GrTexture* texture = processor.texture(0); | 582 GrTexture* texture = processor.texture(0); |
573 if (texture->width() != fTextureSize.width() || | 583 if (texture->width() != fTextureSize.width() || |
574 texture->height() != fTextureSize.height()) { | 584 texture->height() != fTextureSize.height()) { |
575 fTextureSize = SkISize::Make(texture->width(), texture->height()); | 585 fTextureSize = SkISize::Make(texture->width(), texture->height()); |
576 float delta = 1.0f/(3.0f*texture->width()); | 586 float delta = 1.0f/(3.0f*texture->width()); |
(...skipping 10 matching lines...) Expand all Loading... |
587 if (textColor != fTextColor) { | 597 if (textColor != fTextColor) { |
588 static const float ONE_OVER_255 = 1.f / 255.f; | 598 static const float ONE_OVER_255 = 1.f / 255.f; |
589 pdman.set3f(fTextColorUni, | 599 pdman.set3f(fTextColorUni, |
590 GrColorUnpackR(textColor) * ONE_OVER_255, | 600 GrColorUnpackR(textColor) * ONE_OVER_255, |
591 GrColorUnpackG(textColor) * ONE_OVER_255, | 601 GrColorUnpackG(textColor) * ONE_OVER_255, |
592 GrColorUnpackB(textColor) * ONE_OVER_255); | 602 GrColorUnpackB(textColor) * ONE_OVER_255); |
593 fTextColor = textColor; | 603 fTextColor = textColor; |
594 } | 604 } |
595 } | 605 } |
596 | 606 |
597 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&, | 607 static inline void GenKey(const GrGeometryProcessor& processor, |
| 608 const GrBatchTracker&, |
| 609 const GrGLCaps&, |
598 GrProcessorKeyBuilder* b) { | 610 GrProcessorKeyBuilder* b) { |
599 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 611 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
600 processor.cast<GrDistanceFieldLCDTextureEffect>(); | 612 processor.cast<GrDistanceFieldLCDTextureEffect>(); |
601 | 613 |
602 b->add32(dfTexEffect.getFlags()); | 614 b->add32(dfTexEffect.getFlags()); |
603 } | 615 } |
604 | 616 |
605 private: | 617 private: |
606 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | 618 GrGLProgramDataManager::UniformHandle fTextureSizeUni; |
607 SkISize fTextureSize; | 619 SkISize fTextureSize; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 random->nextULessThan(256), | 686 random->nextULessThan(256), |
675 random->nextULessThan(256)); | 687 random->nextULessThan(256)); |
676 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 688 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
677 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 689 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
678 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 690 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
679 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, | 691 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, |
680 textures[texIdx2], params2, | 692 textures[texIdx2], params2, |
681 textColor, | 693 textColor, |
682 flags); | 694 flags); |
683 } | 695 } |
OLD | NEW |