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 "GrBitmapTextGeoProc.h" | 8 #include "GrBitmapTextGeoProc.h" |
9 #include "GrFontAtlasSizes.h" | 9 #include "GrFontAtlasSizes.h" |
10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
11 #include "GrTexture.h" | 11 #include "GrTexture.h" |
12 #include "gl/GrGLProcessor.h" | 12 #include "gl/GrGLProcessor.h" |
13 #include "gl/GrGLSL.h" | 13 #include "gl/GrGLSL.h" |
14 #include "gl/GrGLTexture.h" | 14 #include "gl/GrGLTexture.h" |
15 #include "gl/GrGLGeometryProcessor.h" | 15 #include "gl/GrGLGeometryProcessor.h" |
16 #include "gl/builders/GrGLProgramBuilder.h" | 16 #include "gl/builders/GrGLProgramBuilder.h" |
17 | 17 |
18 struct BitmapTextBatchTracker { | |
19 GrGPInput fInputColorType; | |
20 GrColor fColor; | |
21 bool fUsesLocalCoords; | |
22 }; | |
23 | |
24 class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor { | 18 class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor { |
25 public: | 19 public: |
26 GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&) | 20 GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&) |
27 : fColor(GrColor_ILLEGAL) {} | 21 : fColor(GrColor_ILLEGAL) {} |
28 | 22 |
29 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ | 23 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
30 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); | 24 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); |
31 const BitmapTextBatchTracker& local = args.fBT.cast<BitmapTextBatchTrack
er>(); | |
32 | 25 |
33 GrGLGPBuilder* pb = args.fPB; | 26 GrGLGPBuilder* pb = args.fPB; |
34 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder(); | 27 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder(); |
35 | 28 |
36 // emit attributes | 29 // emit attributes |
37 vsBuilder->emitAttributes(cte); | 30 vsBuilder->emitAttributes(cte); |
38 | 31 |
39 GrGLVertToFrag v(kVec2f_GrSLType); | 32 GrGLVertToFrag v(kVec2f_GrSLType); |
40 pb->addVarying("TextureCoords", &v); | 33 pb->addVarying("TextureCoords", &v); |
41 // this is only used with text, so our texture bounds always match the g
lyph atlas | 34 // this is only used with text, so our texture bounds always match the g
lyph atlas |
42 if (cte.maskFormat() == kA8_GrMaskFormat) { | 35 if (cte.maskFormat() == kA8_GrMaskFormat) { |
43 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ",
" | 36 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ",
" |
44 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut()
, | 37 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut()
, |
45 cte.inTextureCoords()->fName); | 38 cte.inTextureCoords()->fName); |
46 } else { | 39 } else { |
47 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " | 40 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " |
48 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut()
, | 41 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut()
, |
49 cte.inTextureCoords()->fName); | 42 cte.inTextureCoords()->fName); |
50 } | 43 } |
51 | 44 |
52 // Setup pass through color | 45 // Setup pass through color |
53 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, cte.inColor(), | 46 if (!cte.colorIgnored()) { |
54 &fColorUniform); | 47 if (cte.hasVertexColor()) { |
| 48 pb->addPassThroughAttribute(cte.inColor(), args.fOutputColor); |
| 49 } else { |
| 50 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform); |
| 51 } |
| 52 } |
55 | 53 |
56 // Setup position | 54 // Setup position |
57 this->setupPosition(pb, gpArgs, cte.inPosition()->fName); | 55 this->setupPosition(pb, gpArgs, cte.inPosition()->fName); |
58 | 56 |
59 // emit transforms | 57 // emit transforms |
60 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->f
Name, | 58 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->f
Name, |
61 cte.localMatrix(), args.fTransformsIn, args.fTransf
ormsOut); | 59 cte.localMatrix(), args.fTransformsIn, args.fTransf
ormsOut); |
62 | 60 |
63 GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); | 61 GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); |
64 if (cte.maskFormat() == kARGB_GrMaskFormat) { | 62 if (cte.maskFormat() == kARGB_GrMaskFormat) { |
65 fsBuilder->codeAppendf("%s = ", args.fOutputColor); | 63 fsBuilder->codeAppendf("%s = ", args.fOutputColor); |
66 fsBuilder->appendTextureLookupAndModulate(args.fOutputColor, | 64 fsBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
67 args.fSamplers[0], | 65 args.fSamplers[0], |
68 v.fsIn(), | 66 v.fsIn(), |
69 kVec2f_GrSLType); | 67 kVec2f_GrSLType); |
70 fsBuilder->codeAppend(";"); | 68 fsBuilder->codeAppend(";"); |
71 fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); | 69 fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
72 } else { | 70 } else { |
73 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); | 71 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
74 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_G
rSLType); | 72 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_G
rSLType); |
75 fsBuilder->codeAppend(";"); | 73 fsBuilder->codeAppend(";"); |
76 } | 74 } |
77 } | 75 } |
78 | 76 |
79 virtual void setData(const GrGLProgramDataManager& pdman, | 77 virtual void setData(const GrGLProgramDataManager& pdman, |
80 const GrPrimitiveProcessor& gp, | 78 const GrPrimitiveProcessor& gp, |
81 const GrBatchTracker& bt) override { | 79 const GrBatchTracker& bt) override { |
82 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); | 80 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>(); |
83 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColo
r) { | 81 if (btgp.color() != fColor && !btgp.hasVertexColor()) { |
84 GrGLfloat c[4]; | 82 GrGLfloat c[4]; |
85 GrColorToRGBAFloat(local.fColor, c); | 83 GrColorToRGBAFloat(btgp.color(), c); |
86 pdman.set4fv(fColorUniform, 1, c); | 84 pdman.set4fv(fColorUniform, 1, c); |
87 fColor = local.fColor; | 85 fColor = btgp.color(); |
88 } | 86 } |
89 } | 87 } |
90 | 88 |
91 void setTransformData(const GrPrimitiveProcessor& primProc, | 89 void setTransformData(const GrPrimitiveProcessor& primProc, |
92 const GrGLProgramDataManager& pdman, | 90 const GrGLProgramDataManager& pdman, |
93 int index, | 91 int index, |
94 const SkTArray<const GrCoordTransform*, true>& transfo
rms) override { | 92 const SkTArray<const GrCoordTransform*, true>& transfo
rms) override { |
95 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index
, transforms); | 93 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index
, transforms); |
96 } | 94 } |
97 | 95 |
98 static inline void GenKey(const GrGeometryProcessor& proc, | 96 static inline void GenKey(const GrGeometryProcessor& proc, |
99 const GrBatchTracker& bt, | 97 const GrBatchTracker& bt, |
100 const GrGLSLCaps&, | 98 const GrGLSLCaps&, |
101 GrProcessorKeyBuilder* b) { | 99 GrProcessorKeyBuilder* b) { |
102 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); | |
103 // We have to put the optional vertex attribute as part of the key. See
the comment | |
104 // on addVertexAttrib. | |
105 // TODO When we have deferred geometry we can fix this | |
106 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); | 100 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); |
107 uint32_t key = 0; | 101 uint32_t key = 0; |
108 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0; | 102 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 :
0x0; |
109 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x2
: 0x0; | 103 key |= gp.colorIgnored() ? 0x2 : 0x0; |
110 key |= gp.maskFormat() == kARGB_GrMaskFormat ? 0x4 : 0x0; | 104 key |= gp.maskFormat() << 3; |
111 b->add32(local.fInputColorType << 16 | key); | 105 b->add32(key); |
112 } | 106 } |
113 | 107 |
114 private: | 108 private: |
115 GrColor fColor; | 109 GrColor fColor; |
116 UniformHandle fColorUniform; | 110 UniformHandle fColorUniform; |
117 | 111 |
118 typedef GrGLGeometryProcessor INHERITED; | 112 typedef GrGLGeometryProcessor INHERITED; |
119 }; | 113 }; |
120 | 114 |
121 /////////////////////////////////////////////////////////////////////////////// | 115 /////////////////////////////////////////////////////////////////////////////// |
122 | 116 |
123 GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, | 117 GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
124 const GrTextureParams& params, GrMaskFo
rmat format, | 118 const GrTextureParams& params, GrMaskFo
rmat format, |
125 const SkMatrix& localMatrix) | 119 const SkMatrix& localMatrix, bool usesL
ocalCoords) |
126 : fColor(color) | 120 : fColor(color) |
127 , fLocalMatrix(localMatrix) | 121 , fLocalMatrix(localMatrix) |
| 122 , fUsesLocalCoords(usesLocalCoords) |
128 , fTextureAccess(texture, params) | 123 , fTextureAccess(texture, params) |
129 , fInColor(NULL) | 124 , fInColor(NULL) |
130 , fMaskFormat(format) { | 125 , fMaskFormat(format) { |
131 this->initClassID<GrBitmapTextGeoProc>(); | 126 this->initClassID<GrBitmapTextGeoProc>(); |
132 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 127 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
133 | 128 |
| 129 // TODO we could think about removing this attribute if color is ignored, bu
t unfortunately |
| 130 // we don't do text positioning in batch, so we can't quite do that yet. |
134 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat; | 131 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat; |
135 if (hasVertexColor) { | 132 if (hasVertexColor) { |
136 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); | 133 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); |
137 } | 134 } |
138 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 135 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
139 kVec2s_GrVertexAttribTyp
e)); | 136 kVec2s_GrVertexAttribTyp
e)); |
140 this->addTextureAccess(&fTextureAccess); | 137 this->addTextureAccess(&fTextureAccess); |
141 } | 138 } |
142 | 139 |
143 void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, | 140 void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
144 const GrGLSLCaps& caps, | 141 const GrGLSLCaps& caps, |
145 GrProcessorKeyBuilder* b) const { | 142 GrProcessorKeyBuilder* b) const { |
146 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); | 143 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
147 } | 144 } |
148 | 145 |
149 GrGLPrimitiveProcessor* | 146 GrGLPrimitiveProcessor* |
150 GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt, | 147 GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt, |
151 const GrGLSLCaps& caps) const { | 148 const GrGLSLCaps& caps) const { |
152 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); | 149 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
153 } | 150 } |
154 | 151 |
155 void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const GrPipelineI
nfo& init) const { | |
156 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>(); | |
157 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), in
it, | |
158 SkToBool(fInColor)); | |
159 local->fUsesLocalCoords = init.fUsesLocalCoords; | |
160 } | |
161 | |
162 /////////////////////////////////////////////////////////////////////////////// | 152 /////////////////////////////////////////////////////////////////////////////// |
163 | 153 |
164 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); | 154 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
165 | 155 |
166 GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, | 156 GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
167 GrContext*, | 157 GrContext*, |
168 const GrDrawTargetCaps&, | 158 const GrDrawTargetCaps&, |
169 GrTexture* textures[]) { | 159 GrTexture* textures[]) { |
170 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : | 160 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
171 GrProcessorUnitTest::kAlphaTextureIdx; | 161 GrProcessorUnitTest::kAlphaTextureIdx; |
(...skipping 18 matching lines...) Expand all Loading... |
190 break; | 180 break; |
191 case 1: | 181 case 1: |
192 format = kA565_GrMaskFormat; | 182 format = kA565_GrMaskFormat; |
193 break; | 183 break; |
194 case 2: | 184 case 2: |
195 format = kARGB_GrMaskFormat; | 185 format = kARGB_GrMaskFormat; |
196 break; | 186 break; |
197 } | 187 } |
198 | 188 |
199 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx],
params, | 189 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx],
params, |
200 format, GrTest::TestMatrix(random)); | 190 format, GrTest::TestMatrix(random), rando
m->nextBool()); |
201 } | 191 } |
OLD | NEW |