Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: src/gpu/effects/GrDistanceFieldTextureEffect.cpp

Issue 41213003: Hook in rough distance field support for fonts (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Minor fixes for GrDistanceFieldTextContext. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "GrCustomCoordsTextureEffect.h" 8 #include "GrDistanceFieldTextureEffect.h"
9 #include "gl/GrGLEffect.h" 9 #include "gl/GrGLEffect.h"
10 #include "gl/GrGLSL.h" 10 #include "gl/GrGLSL.h"
11 #include "gl/GrGLTexture.h" 11 #include "gl/GrGLTexture.h"
12 #include "gl/GrGLVertexEffect.h" 12 #include "gl/GrGLVertexEffect.h"
13 #include "GrTBackendEffectFactory.h" 13 #include "GrTBackendEffectFactory.h"
14 #include "GrTexture.h" 14 #include "GrTexture.h"
15 15
robertphillips 2013/11/05 15:30:26 // words of wisdom?
jvanverth1 2013/11/05 17:09:22 Done.
16 class GrGLCustomCoordsTextureEffect : public GrGLVertexEffect { 16 #define THRESHOLD "0.50196078431"
17
18 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect {
17 public: 19 public:
18 GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const G rDrawEffect& drawEffect) 20 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
19 : INHERITED (factory) {} 21 : INHERITED (factory) {}
20 22
21 virtual void emitCode(GrGLFullShaderBuilder* builder, 23 virtual void emitCode(GrGLFullShaderBuilder* builder,
22 const GrDrawEffect& drawEffect, 24 const GrDrawEffect& drawEffect,
23 EffectKey key, 25 EffectKey key,
24 const char* outputColor, 26 const char* outputColor,
25 const char* inputColor, 27 const char* inputColor,
26 const TransformedCoordsArray&, 28 const TransformedCoordsArray&,
27 const TextureSamplerArray& samplers) SK_OVERRIDE { 29 const TextureSamplerArray& samplers) SK_OVERRIDE {
28 SkASSERT(1 == drawEffect.castEffect<GrCustomCoordsTextureEffect>().numVe rtexAttribs()); 30 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV ertexAttribs());
29 31
30 SkString fsCoordName; 32 SkString fsCoordName;
31 const char* vsVaryingName; 33 const char* vsVaryingName;
32 const char* fsVaryingNamePtr; 34 const char* fsVaryingNamePtr;
33 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &f sVaryingNamePtr); 35 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &f sVaryingNamePtr);
34 fsCoordName = fsVaryingNamePtr; 36 fsCoordName = fsVaryingNamePtr;
35 37
36 const char* attrName = 38 const char* attrName =
37 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str(); 39 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str();
38 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); 40 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
39 41
40 builder->fsCodeAppendf("\t%s = ", outputColor); 42 builder->fsCodeAppend("\tvec4 texColor = ");
41 builder->fsAppendTextureLookupAndModulate(inputColor, 43 builder->fsAppendTextureLookup(samplers[0],
42 samplers[0], 44 fsCoordName.c_str(),
43 fsCoordName.c_str(), 45 kVec2f_GrSLType);
44 kVec2f_GrSLType);
45 builder->fsCodeAppend(";\n"); 46 builder->fsCodeAppend(";\n");
47 builder->fsCodeAppend("\tfloat distance = texColor.r;\n");
48 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(vec2(dFdx(distanc e), dFdy(distance)));\n");
bsalomon 2013/11/05 15:28:52 Maybe a comment here or somewhere about how this w
jvanverth1 2013/11/05 17:09:22 Done.
49 builder->fsCodeAppend("\tfloat val = smoothstep("THRESHOLD"-afwidth, "TH RESHOLD"+afwidth, distance);\n");
50
51 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
52 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val") ).c_str());
46 } 53 }
47 54
48 virtual void setData(const GrGLUniformManager& uman, 55 virtual void setData(const GrGLUniformManager& uman,
49 const GrDrawEffect& drawEffect) SK_OVERRIDE {} 56 const GrDrawEffect& drawEffect) SK_OVERRIDE {}
50 57
51 private: 58 private:
52 typedef GrGLVertexEffect INHERITED; 59 typedef GrGLVertexEffect INHERITED;
53 }; 60 };
54 61
55 /////////////////////////////////////////////////////////////////////////////// 62 ///////////////////////////////////////////////////////////////////////////////
56 63
57 GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, 64 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
58 const GrTextureParams& params) 65 const GrTextureParams& params)
59 : fTextureAccess(texture, params) { 66 : fTextureAccess(texture, params) {
60 this->addTextureAccess(&fTextureAccess); 67 this->addTextureAccess(&fTextureAccess);
61 this->addVertexAttrib(kVec2f_GrSLType); 68 this->addVertexAttrib(kVec2f_GrSLType);
62 } 69 }
63 70
64 bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const { 71 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
65 const GrCustomCoordsTextureEffect& cte = CastEffect<GrCustomCoordsTextureEff ect>(other); 72 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other);
66 return fTextureAccess == cte.fTextureAccess; 73 return fTextureAccess == cte.fTextureAccess;
67 } 74 }
68 75
69 void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color, 76 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color,
70 uint32_t* validFlag s) const { 77 uint32_t* validFlag s) const {
71 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) && 78 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) &&
72 GrPixelConfigIsOpaque(this->texture(0)->config())) { 79 GrPixelConfigIsOpaque(this->texture(0)->config())) {
73 *validFlags = kA_GrColorComponentFlag; 80 *validFlags = kA_GrColorComponentFlag;
74 } else { 81 } else {
75 *validFlags = 0; 82 *validFlags = 0;
76 } 83 }
77 } 84 }
78 85
79 const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const { 86 const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const {
80 return GrTBackendEffectFactory<GrCustomCoordsTextureEffect>::getInstance(); 87 return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance();
81 } 88 }
82 89
83 /////////////////////////////////////////////////////////////////////////////// 90 ///////////////////////////////////////////////////////////////////////////////
84 91
85 GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect); 92 GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect);
86 93
87 GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, 94 GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random,
88 GrContext*, 95 GrContext*,
89 const GrDrawTargetCaps&, 96 const GrDrawTargetCaps&,
90 GrTexture* textures[]) { 97 GrTexture* textures[]) {
91 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 98 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
92 GrEffectUnitTest::kAlphaTextureIdx; 99 GrEffectUnitTest::kAlphaTextureIdx;
93 static const SkShader::TileMode kTileModes[] = { 100 static const SkShader::TileMode kTileModes[] = {
94 SkShader::kClamp_TileMode, 101 SkShader::kClamp_TileMode,
95 SkShader::kRepeat_TileMode, 102 SkShader::kRepeat_TileMode,
96 SkShader::kMirror_TileMode, 103 SkShader::kMirror_TileMode,
97 }; 104 };
98 SkShader::TileMode tileModes[] = { 105 SkShader::TileMode tileModes[] = {
99 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 106 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
100 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 107 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
101 }; 108 };
102 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : 109 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode :
103 GrTextureParams::kNon e_FilterMode); 110 GrTextureParams::kNon e_FilterMode);
104 111
105 return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); 112 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params);
106 } 113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698