blob: edee8618012d52ec48a279f8bf2aeca004985c9e [file] [log] [blame]
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
egdaniel309e3462014-12-09 10:35:58 -08008#include "GrBitmapTextGeoProc.h"
jvanverth5a105ff2015-02-18 11:36:35 -08009#include "GrFontAtlasSizes.h"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080011#include "GrTexture.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "gl/GrGLProcessor.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000013#include "gl/GrGLSL.h"
14#include "gl/GrGLTexture.h"
joshualitt249af152014-09-15 11:41:13 -070015#include "gl/GrGLGeometryProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080016#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000017
joshualitt9b989322014-12-15 14:16:27 -080018struct BitmapTextBatchTracker {
19 GrGPInput fInputColorType;
20 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -080021 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -080022};
23
egdaniel309e3462014-12-09 10:35:58 -080024class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000025public:
joshualitt9b989322014-12-15 14:16:27 -080026 GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&)
27 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
egdaniel309e3462014-12-09 10:35:58 -080030 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt9b989322014-12-15 14:16:27 -080031 const BitmapTextBatchTracker& local = args.fBT.cast<BitmapTextBatchTracker>();
joshualitt2dd1ae02014-12-03 06:24:10 -080032
joshualitt9b989322014-12-15 14:16:27 -080033 GrGLGPBuilder* pb = args.fPB;
34 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000035
joshualittabb52a12015-01-13 15:02:10 -080036 // emit attributes
37 vsBuilder->emitAttributes(cte);
38
joshualitt74077b92014-10-24 11:26:03 -070039 GrGLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080040 pb->addVarying("TextureCoords", &v);
jvanverth5a105ff2015-02-18 11:36:35 -080041 // this is only used with text, so our texture bounds always match the glyph atlas
jvanverthcb251f12015-03-11 11:18:11 -070042 if (cte.maskFormat() == kA8_GrMaskFormat) {
43 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", "
44 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut(),
45 cte.inTextureCoords()->fName);
46 } else {
47 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", "
48 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut(),
49 cte.inTextureCoords()->fName);
50 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000051
joshualitt9b989322014-12-15 14:16:27 -080052 // Setup pass through color
53 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(),
54 &fColorUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080055
joshualittabb52a12015-01-13 15:02:10 -080056 // Setup position
joshualitte578a952015-05-14 10:09:13 -070057 this->setupPosition(pb, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080058
joshualittabb52a12015-01-13 15:02:10 -080059 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -080060 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080061 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
62
egdaniel29bee0f2015-04-29 11:54:42 -070063 GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt02b05012015-02-11 06:56:30 -080064 if (cte.maskFormat() == kARGB_GrMaskFormat) {
65 fsBuilder->codeAppendf("%s = ", args.fOutputColor);
66 fsBuilder->appendTextureLookupAndModulate(args.fOutputColor,
67 args.fSamplers[0],
68 v.fsIn(),
69 kVec2f_GrSLType);
70 fsBuilder->codeAppend(";");
71 fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
72 } else {
73 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage);
74 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
75 fsBuilder->codeAppend(";");
76 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000077 }
78
joshualitt9b989322014-12-15 14:16:27 -080079 virtual void setData(const GrGLProgramDataManager& pdman,
80 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -070081 const GrBatchTracker& bt) override {
joshualitt9b989322014-12-15 14:16:27 -080082 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
83 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
84 GrGLfloat c[4];
85 GrColorToRGBAFloat(local.fColor, c);
86 pdman.set4fv(fColorUniform, 1, c);
87 fColor = local.fColor;
88 }
joshualitt2dd1ae02014-12-03 06:24:10 -080089 }
90
joshualitt9b989322014-12-15 14:16:27 -080091 static inline void GenKey(const GrGeometryProcessor& proc,
92 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -070093 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -080094 GrProcessorKeyBuilder* b) {
95 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
96 // We have to put the optional vertex attribute as part of the key. See the comment
97 // on addVertexAttrib.
98 // TODO When we have deferred geometry we can fix this
99 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800100 uint32_t key = 0;
101 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0;
102 key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0;
joshualitt02b05012015-02-11 06:56:30 -0800103 key |= gp.maskFormat() == kARGB_GrMaskFormat ? 0x4 : 0x0;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800104 b->add32(local.fInputColorType << 16 | key);
joshualitt9b989322014-12-15 14:16:27 -0800105 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800106
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000107private:
joshualitt9b989322014-12-15 14:16:27 -0800108 GrColor fColor;
109 UniformHandle fColorUniform;
110
joshualitt249af152014-09-15 11:41:13 -0700111 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
joshualitt2e3b3e32014-12-09 13:31:14 -0800116GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800117 const GrTextureParams& params, GrMaskFormat format,
joshualitt1ba8cc92015-05-13 12:24:23 -0700118 const SkMatrix& localMatrix)
joshualitte578a952015-05-14 10:09:13 -0700119 : INHERITED(localMatrix)
joshualitt88c23fc2015-05-13 14:18:07 -0700120 , fColor(color)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800121 , fTextureAccess(texture, params)
joshualitt02b05012015-02-11 06:56:30 -0800122 , fInColor(NULL)
123 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800124 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800125 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800126
127 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
128 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800129 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800130 }
joshualitt71c92602015-01-14 08:12:47 -0800131 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800132 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000133 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000134}
135
egdaniel309e3462014-12-09 10:35:58 -0800136void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700137 const GrGLSLCaps& caps,
egdaniel309e3462014-12-09 10:35:58 -0800138 GrProcessorKeyBuilder* b) const {
139 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000140}
141
joshualittabb52a12015-01-13 15:02:10 -0800142GrGLPrimitiveProcessor*
143GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700144 const GrGLSLCaps& caps) const {
egdaniel309e3462014-12-09 10:35:58 -0800145 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800146}
joshualitt9b989322014-12-15 14:16:27 -0800147
joshualitt4d8da812015-01-28 12:53:54 -0800148void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800149 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
150 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
151 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800152 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800153}
154
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000155///////////////////////////////////////////////////////////////////////////////
156
egdaniel309e3462014-12-09 10:35:58 -0800157GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000158
egdaniel309e3462014-12-09 10:35:58 -0800159GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
160 GrContext*,
161 const GrDrawTargetCaps&,
162 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700163 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
164 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000165 static const SkShader::TileMode kTileModes[] = {
166 SkShader::kClamp_TileMode,
167 SkShader::kRepeat_TileMode,
168 SkShader::kMirror_TileMode,
169 };
170 SkShader::TileMode tileModes[] = {
171 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
172 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
173 };
174 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
175 GrTextureParams::kNone_FilterMode);
176
joshualitt02b05012015-02-11 06:56:30 -0800177 GrMaskFormat format;
178 switch (random->nextULessThan(3)) {
179 default:
180 SkFAIL("Incomplete enum\n");
181 case 0:
182 format = kA8_GrMaskFormat;
183 break;
184 case 1:
185 format = kA565_GrMaskFormat;
186 break;
187 case 2:
188 format = kARGB_GrMaskFormat;
189 break;
190 }
191
joshualitt2e3b3e32014-12-09 13:31:14 -0800192 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt1ba8cc92015-05-13 12:24:23 -0700193 format, GrTest::TestMatrix(random));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000194}