| 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 "gl/builders/GrGLProgramBuilder.h" | 9 #include "gl/builders/GrGLProgramBuilder.h" |
| 10 #include "gl/GrGLProcessor.h" | 10 #include "gl/GrGLProcessor.h" |
| 11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
| 12 #include "gl/GrGLTexture.h" | 12 #include "gl/GrGLTexture.h" |
| 13 #include "gl/GrGLGeometryProcessor.h" | 13 #include "gl/GrGLGeometryProcessor.h" |
| 14 #include "GrTBackendProcessorFactory.h" | 14 #include "GrTBackendProcessorFactory.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, | |
| 20 // we need to slightly bold each glyph. On the Mac, we need a larger bold value. | |
| 21 #if defined(SK_BUILD_FOR_MAC) | |
| 22 #define SK_DistanceFieldLCDFactor "0.33" | |
| 23 #define SK_DistanceFieldNonLCDFactor "0.25" | |
| 24 #else | |
| 25 #define SK_DistanceFieldLCDFactor "0.05" | |
| 26 #define SK_DistanceFieldNonLCDFactor "0.05" | |
| 27 #endif | |
| 28 | |
| 29 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 | 19 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 |
| 30 #define SK_DistanceFieldAAFactor "0.7071" | 20 #define SK_DistanceFieldAAFactor "0.7071" |
| 31 | 21 |
| 32 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { | 22 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { |
| 33 public: | 23 public: |
| 34 GrGLDistanceFieldTextureEffect(const GrBackendProcessorFactory& factory, | 24 GrGLDistanceFieldTextureEffect(const GrBackendProcessorFactory& factory, |
| 35 const GrProcessor&) | 25 const GrProcessor&) |
| 36 : INHERITED (factory) | 26 : INHERITED (factory) |
| 37 , fTextureSize(SkISize::Make(-1,-1)) | 27 , fTextureSize(SkISize::Make(-1,-1)) |
| 38 #ifdef SK_GAMMA_APPLY_TO_A8 | 28 #ifdef SK_GAMMA_APPLY_TO_A8 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 59 fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | 49 fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, |
| 60 kVec2f_GrSLType, "TextureSize", | 50 kVec2f_GrSLType, "TextureSize", |
| 61 &textureSizeUniName); | 51 &textureSizeUniName); |
| 62 | 52 |
| 63 fsBuilder->codeAppend("\tvec4 texColor = "); | 53 fsBuilder->codeAppend("\tvec4 texColor = "); |
| 64 fsBuilder->appendTextureLookup(args.fSamplers[0], | 54 fsBuilder->appendTextureLookup(args.fSamplers[0], |
| 65 v.fsIn(), | 55 v.fsIn(), |
| 66 kVec2f_GrSLType); | 56 kVec2f_GrSLType); |
| 67 fsBuilder->codeAppend(";\n"); | 57 fsBuilder->codeAppend(";\n"); |
| 68 fsBuilder->codeAppend("\tfloat distance = " | 58 fsBuilder->codeAppend("\tfloat distance = " |
| 69 SK_DistanceFieldMultiplier "*(texColor.r - " SK_Distan
ceFieldThreshold ")" | 59 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceF
ieldThreshold ");"); |
| 70 "+ " SK_DistanceFieldNonLCDFactor ";\n"); | |
| 71 | 60 |
| 72 // we adjust for the effect of the transformation on the distance by usi
ng | 61 // we adjust for the effect of the transformation on the distance by usi
ng |
| 73 // the length of the gradient of the texture coordinates. We use st coor
dinates | 62 // the length of the gradient of the texture coordinates. We use st coor
dinates |
| 74 // to ensure we're mapping 1:1 from texel space to pixel space. | 63 // to ensure we're mapping 1:1 from texel space to pixel space. |
| 75 fsBuilder->codeAppendf("\tvec2 uv = %s;\n", v.fsIn()); | 64 fsBuilder->codeAppendf("\tvec2 uv = %s;\n", v.fsIn()); |
| 76 fsBuilder->codeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); | 65 fsBuilder->codeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); |
| 77 fsBuilder->codeAppend("\tfloat afwidth;\n"); | 66 fsBuilder->codeAppend("\tfloat afwidth;\n"); |
| 78 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { | 67 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
| 79 // this gives us a smooth step across approximately one fragment | 68 // this gives us a smooth step across approximately one fragment |
| 80 fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*
dFdx(st.x));\n"); | 69 fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*
dFdx(st.x));\n"); |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 fsBuilder->codeAppend(";\n"); | 447 fsBuilder->codeAppend(";\n"); |
| 459 fsBuilder->codeAppend("\tdistance.x = texColor.r;\n"); | 448 fsBuilder->codeAppend("\tdistance.x = texColor.r;\n"); |
| 460 // blue is distance to right offset | 449 // blue is distance to right offset |
| 461 fsBuilder->codeAppend("\tuv_adjusted = uv + offset;\n"); | 450 fsBuilder->codeAppend("\tuv_adjusted = uv + offset;\n"); |
| 462 fsBuilder->codeAppend("\ttexColor = "); | 451 fsBuilder->codeAppend("\ttexColor = "); |
| 463 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_
GrSLType); | 452 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_
GrSLType); |
| 464 fsBuilder->codeAppend(";\n"); | 453 fsBuilder->codeAppend(";\n"); |
| 465 fsBuilder->codeAppend("\tdistance.z = texColor.r;\n"); | 454 fsBuilder->codeAppend("\tdistance.z = texColor.r;\n"); |
| 466 | 455 |
| 467 fsBuilder->codeAppend("\tdistance = " | 456 fsBuilder->codeAppend("\tdistance = " |
| 468 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_Distance
FieldThreshold"))" | 457 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceF
ieldThreshold"));"); |
| 469 "+ vec3(" SK_DistanceFieldLCDFactor ");\n"); | |
| 470 | 458 |
| 471 // we adjust for the effect of the transformation on the distance by usi
ng | 459 // we adjust for the effect of the transformation on the distance by usi
ng |
| 472 // the length of the gradient of the texture coordinates. We use st coor
dinates | 460 // the length of the gradient of the texture coordinates. We use st coor
dinates |
| 473 // to ensure we're mapping 1:1 from texel space to pixel space. | 461 // to ensure we're mapping 1:1 from texel space to pixel space. |
| 474 | 462 |
| 475 // To be strictly correct, we should compute the anti-aliasing factor se
parately | 463 // To be strictly correct, we should compute the anti-aliasing factor se
parately |
| 476 // for each color component. However, this is only important when using
perspective | 464 // for each color component. However, this is only important when using
perspective |
| 477 // transformations, and even then using a single factor seems like a rea
sonable | 465 // transformations, and even then using a single factor seems like a rea
sonable |
| 478 // trade-off between quality and speed. | 466 // trade-off between quality and speed. |
| 479 fsBuilder->codeAppend("\tfloat afwidth;\n"); | 467 fsBuilder->codeAppend("\tfloat afwidth;\n"); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 random->nextULessThan(256), | 632 random->nextULessThan(256), |
| 645 random->nextULessThan(256)); | 633 random->nextULessThan(256)); |
| 646 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 634 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
| 647 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 635 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
| 648 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 636 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
| 649 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, | 637 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, |
| 650 textures[texIdx2], params2, | 638 textures[texIdx2], params2, |
| 651 textColor, | 639 textColor, |
| 652 flags); | 640 flags); |
| 653 } | 641 } |
| OLD | NEW |