blob: ade0904c0174125f11c22eefe2f89e015781906e [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
joshualittdd219872015-02-12 14:48:42 -080057 this->setupPosition(pb, gpArgs, cte.inPosition()->fName, cte.viewMatrix());
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 {
joshualittee2af952014-12-30 09:04:15 -080082 this->setUniformViewMatrix(pdman, gp.viewMatrix());
83
joshualitt9b989322014-12-15 14:16:27 -080084 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
85 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
86 GrGLfloat c[4];
87 GrColorToRGBAFloat(local.fColor, c);
88 pdman.set4fv(fColorUniform, 1, c);
89 fColor = local.fColor;
90 }
joshualitt2dd1ae02014-12-03 06:24:10 -080091 }
92
joshualitt9b989322014-12-15 14:16:27 -080093 static inline void GenKey(const GrGeometryProcessor& proc,
94 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -070095 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -080096 GrProcessorKeyBuilder* b) {
97 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
98 // We have to put the optional vertex attribute as part of the key. See the comment
99 // on addVertexAttrib.
100 // TODO When we have deferred geometry we can fix this
101 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800102 uint32_t key = 0;
103 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0;
104 key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0;
joshualitt02b05012015-02-11 06:56:30 -0800105 key |= gp.maskFormat() == kARGB_GrMaskFormat ? 0x4 : 0x0;
106 key |= ComputePosKey(gp.viewMatrix()) << 3;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800107 b->add32(local.fInputColorType << 16 | key);
joshualitt9b989322014-12-15 14:16:27 -0800108 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800109
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000110private:
joshualitt9b989322014-12-15 14:16:27 -0800111 GrColor fColor;
112 UniformHandle fColorUniform;
113
joshualitt249af152014-09-15 11:41:13 -0700114 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000115};
116
117///////////////////////////////////////////////////////////////////////////////
118
joshualitt2e3b3e32014-12-09 13:31:14 -0800119GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800120 const GrTextureParams& params, GrMaskFormat format,
joshualitt1ba8cc92015-05-13 12:24:23 -0700121 const SkMatrix& localMatrix)
122 : INHERITED(color, SkMatrix::I(), localMatrix)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800123 , fTextureAccess(texture, params)
joshualitt02b05012015-02-11 06:56:30 -0800124 , fInColor(NULL)
125 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800126 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800127 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800128
129 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
130 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800131 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800132 }
joshualitt71c92602015-01-14 08:12:47 -0800133 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800134 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000135 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000136}
137
egdaniel309e3462014-12-09 10:35:58 -0800138void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700139 const GrGLSLCaps& caps,
egdaniel309e3462014-12-09 10:35:58 -0800140 GrProcessorKeyBuilder* b) const {
141 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000142}
143
joshualittabb52a12015-01-13 15:02:10 -0800144GrGLPrimitiveProcessor*
145GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700146 const GrGLSLCaps& caps) const {
egdaniel309e3462014-12-09 10:35:58 -0800147 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800148}
joshualitt9b989322014-12-15 14:16:27 -0800149
joshualitt4d8da812015-01-28 12:53:54 -0800150void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800151 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
152 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
153 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800154 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800155}
156
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000157///////////////////////////////////////////////////////////////////////////////
158
egdaniel309e3462014-12-09 10:35:58 -0800159GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000160
egdaniel309e3462014-12-09 10:35:58 -0800161GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
162 GrContext*,
163 const GrDrawTargetCaps&,
164 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700165 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
166 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000167 static const SkShader::TileMode kTileModes[] = {
168 SkShader::kClamp_TileMode,
169 SkShader::kRepeat_TileMode,
170 SkShader::kMirror_TileMode,
171 };
172 SkShader::TileMode tileModes[] = {
173 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
174 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
175 };
176 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
177 GrTextureParams::kNone_FilterMode);
178
joshualitt02b05012015-02-11 06:56:30 -0800179 GrMaskFormat format;
180 switch (random->nextULessThan(3)) {
181 default:
182 SkFAIL("Incomplete enum\n");
183 case 0:
184 format = kA8_GrMaskFormat;
185 break;
186 case 1:
187 format = kA565_GrMaskFormat;
188 break;
189 case 2:
190 format = kARGB_GrMaskFormat;
191 break;
192 }
193
joshualitt2e3b3e32014-12-09 13:31:14 -0800194 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt1ba8cc92015-05-13 12:24:23 -0700195 format, GrTest::TestMatrix(random));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000196}