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