blob: 0818fc0d8cff51ded070e6d6d46286108dba885b [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"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "gl/GrGLProcessor.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000012#include "gl/GrGLSL.h"
13#include "gl/GrGLTexture.h"
joshualitt249af152014-09-15 11:41:13 -070014#include "gl/GrGLGeometryProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080015#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000016
joshualitt9b989322014-12-15 14:16:27 -080017struct BitmapTextBatchTracker {
18 GrGPInput fInputColorType;
19 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -080020 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -080021};
22
egdaniel309e3462014-12-09 10:35:58 -080023class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000024public:
joshualitt9b989322014-12-15 14:16:27 -080025 GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&)
26 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000027
joshualittabb52a12015-01-13 15:02:10 -080028 void onEmitCode(EmitArgs& args) SK_OVERRIDE {
egdaniel309e3462014-12-09 10:35:58 -080029 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt9b989322014-12-15 14:16:27 -080030 const BitmapTextBatchTracker& local = args.fBT.cast<BitmapTextBatchTracker>();
joshualitt2dd1ae02014-12-03 06:24:10 -080031
joshualitt9b989322014-12-15 14:16:27 -080032 GrGLGPBuilder* pb = args.fPB;
33 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000034
joshualittabb52a12015-01-13 15:02:10 -080035 // emit attributes
36 vsBuilder->emitAttributes(cte);
37
joshualitt74077b92014-10-24 11:26:03 -070038 GrGLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080039 pb->addVarying("TextureCoords", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -080040 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000041
joshualitt9b989322014-12-15 14:16:27 -080042 // Setup pass through color
43 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(),
44 &fColorUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080045
joshualittee2af952014-12-30 09:04:15 -080046 // setup uniform viewMatrix
47 this->addUniformViewMatrix(pb);
48
joshualittabb52a12015-01-13 15:02:10 -080049 // Setup position
50 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", this->position(), this->uViewM(),
joshualittee2af952014-12-30 09:04:15 -080051 cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080052
joshualittabb52a12015-01-13 15:02:10 -080053 // emit transforms
54 this->emitTransforms(args.fPB, this->position(), cte.inPosition()->fName,
55 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
56
joshualitt9b989322014-12-15 14:16:27 -080057 GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -080058 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage);
59 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -070060 fsBuilder->codeAppend(";");
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000061 }
62
joshualitt9b989322014-12-15 14:16:27 -080063 virtual void setData(const GrGLProgramDataManager& pdman,
64 const GrPrimitiveProcessor& gp,
65 const GrBatchTracker& bt) SK_OVERRIDE {
joshualittee2af952014-12-30 09:04:15 -080066 this->setUniformViewMatrix(pdman, gp.viewMatrix());
67
joshualitt9b989322014-12-15 14:16:27 -080068 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
69 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
70 GrGLfloat c[4];
71 GrColorToRGBAFloat(local.fColor, c);
72 pdman.set4fv(fColorUniform, 1, c);
73 fColor = local.fColor;
74 }
joshualitt2dd1ae02014-12-03 06:24:10 -080075 }
76
joshualitt9b989322014-12-15 14:16:27 -080077 static inline void GenKey(const GrGeometryProcessor& proc,
78 const GrBatchTracker& bt,
79 const GrGLCaps&,
80 GrProcessorKeyBuilder* b) {
81 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
82 // We have to put the optional vertex attribute as part of the key. See the comment
83 // on addVertexAttrib.
84 // TODO When we have deferred geometry we can fix this
85 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -080086 uint32_t key = 0;
87 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0;
88 key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0;
89 b->add32(local.fInputColorType << 16 | key);
joshualitt9b989322014-12-15 14:16:27 -080090 }
joshualitt2dd1ae02014-12-03 06:24:10 -080091
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000092private:
joshualitt9b989322014-12-15 14:16:27 -080093 GrColor fColor;
94 UniformHandle fColorUniform;
95
joshualitt249af152014-09-15 11:41:13 -070096 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000097};
98
99///////////////////////////////////////////////////////////////////////////////
100
joshualitt2e3b3e32014-12-09 13:31:14 -0800101GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt56995b52014-12-11 15:44:02 -0800102 const GrTextureParams& params, bool useColorAttrib,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800103 bool opaqueVertexColors, const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800104 : INHERITED(color, SkMatrix::I(), localMatrix, opaqueVertexColors)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800105 , fTextureAccess(texture, params)
106 , fInColor(NULL) {
egdaniel309e3462014-12-09 10:35:58 -0800107 this->initClassID<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800108 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
egdaniel309e3462014-12-09 10:35:58 -0800109 if (useColorAttrib) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800110 fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType));
111 this->setHasVertexColor();
112 }
113 fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords",
114 kVec2f_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000115 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000116}
117
egdaniel309e3462014-12-09 10:35:58 -0800118bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const {
119 const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800120 return SkToBool(this->inColor()) == SkToBool(gp.inColor());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000121}
122
joshualitt56995b52014-12-11 15:44:02 -0800123void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
egdanielf8449ba2014-11-25 10:24:56 -0800124 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800125 out->setUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800126 } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800127 out->setUnknownOpaqueFourComponents();
128 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000129 } else {
joshualitt56995b52014-12-11 15:44:02 -0800130 out->setUnknownFourComponents();
131 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000132 }
133}
134
egdaniel309e3462014-12-09 10:35:58 -0800135void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
136 const GrGLCaps& caps,
137 GrProcessorKeyBuilder* b) const {
138 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000139}
140
joshualittabb52a12015-01-13 15:02:10 -0800141GrGLPrimitiveProcessor*
142GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt,
143 const GrGLCaps& caps) const {
egdaniel309e3462014-12-09 10:35:58 -0800144 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800145}
joshualitt9b989322014-12-15 14:16:27 -0800146
147void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
148 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
149 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
150 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800151 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800152}
153
joshualitt290c09b2014-12-19 13:45:20 -0800154bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m,
155 const GrGeometryProcessor& that,
156 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800157 const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>();
158 const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800159 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
160 that, theirs.fUsesLocalCoords) &&
161 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800162 theirs.fInputColorType, theirs.fColor);
163}
164
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000165///////////////////////////////////////////////////////////////////////////////
166
egdaniel309e3462014-12-09 10:35:58 -0800167GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000168
egdaniel309e3462014-12-09 10:35:58 -0800169GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
170 GrContext*,
171 const GrDrawTargetCaps&,
172 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700173 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
174 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000175 static const SkShader::TileMode kTileModes[] = {
176 SkShader::kClamp_TileMode,
177 SkShader::kRepeat_TileMode,
178 SkShader::kMirror_TileMode,
179 };
180 SkShader::TileMode tileModes[] = {
181 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
182 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
183 };
184 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
185 GrTextureParams::kNone_FilterMode);
186
joshualitt2e3b3e32014-12-09 13:31:14 -0800187 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800188 random->nextBool(), random->nextBool(),
189 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000190}