blob: 09eee1c70d1ba106be2843d0acf130a178798ae9 [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
joshualittc369e7c2014-10-22 10:56:26 -070028 virtual void emitCode(const 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
joshualitt74077b92014-10-24 11:26:03 -070035 GrGLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080036 pb->addVarying("TextureCoords", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -080037 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000038
joshualitt9b989322014-12-15 14:16:27 -080039 // Setup pass through color
40 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(),
41 &fColorUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080042
43 // setup output coords
44 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), cte.inPosition()->fName);
45 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), cte.inPosition()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000046
joshualitt4973d9d2014-11-08 09:24:25 -080047 // setup position varying
48 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(),
joshualitt2dd1ae02014-12-03 06:24:10 -080049 vsBuilder->uViewM(), cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080050
joshualitt9b989322014-12-15 14:16:27 -080051 GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -080052 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage);
53 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -070054 fsBuilder->codeAppend(";");
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000055 }
56
joshualitt9b989322014-12-15 14:16:27 -080057 virtual void setData(const GrGLProgramDataManager& pdman,
58 const GrPrimitiveProcessor& gp,
59 const GrBatchTracker& bt) SK_OVERRIDE {
60 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
61 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
62 GrGLfloat c[4];
63 GrColorToRGBAFloat(local.fColor, c);
64 pdman.set4fv(fColorUniform, 1, c);
65 fColor = local.fColor;
66 }
joshualitt2dd1ae02014-12-03 06:24:10 -080067 }
68
joshualitt9b989322014-12-15 14:16:27 -080069 static inline void GenKey(const GrGeometryProcessor& proc,
70 const GrBatchTracker& bt,
71 const GrGLCaps&,
72 GrProcessorKeyBuilder* b) {
73 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
74 // We have to put the optional vertex attribute as part of the key. See the comment
75 // on addVertexAttrib.
76 // TODO When we have deferred geometry we can fix this
77 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
78 b->add32(SkToBool(gp.inColor()));
79 b->add32(local.fInputColorType);
80 }
joshualitt2dd1ae02014-12-03 06:24:10 -080081
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000082private:
joshualitt9b989322014-12-15 14:16:27 -080083 GrColor fColor;
84 UniformHandle fColorUniform;
85
joshualitt249af152014-09-15 11:41:13 -070086 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000087};
88
89///////////////////////////////////////////////////////////////////////////////
90
joshualitt2e3b3e32014-12-09 13:31:14 -080091GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt56995b52014-12-11 15:44:02 -080092 const GrTextureParams& params, bool useColorAttrib,
93 bool opaqueVertexColors)
94 : INHERITED(color, opaqueVertexColors), fTextureAccess(texture, params), fInColor(NULL) {
egdaniel309e3462014-12-09 10:35:58 -080095 this->initClassID<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080096 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
egdaniel309e3462014-12-09 10:35:58 -080097 if (useColorAttrib) {
joshualitt2dd1ae02014-12-03 06:24:10 -080098 fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType));
99 this->setHasVertexColor();
100 }
101 fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords",
102 kVec2f_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000103 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000104}
105
egdaniel309e3462014-12-09 10:35:58 -0800106bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const {
107 const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800108 return SkToBool(this->inColor()) == SkToBool(gp.inColor());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000109}
110
joshualitt56995b52014-12-11 15:44:02 -0800111void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
egdanielf8449ba2014-11-25 10:24:56 -0800112 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800113 out->setUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800114 } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800115 out->setUnknownOpaqueFourComponents();
116 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000117 } else {
joshualitt56995b52014-12-11 15:44:02 -0800118 out->setUnknownFourComponents();
119 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000120 }
121}
122
egdaniel309e3462014-12-09 10:35:58 -0800123void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
124 const GrGLCaps& caps,
125 GrProcessorKeyBuilder* b) const {
126 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000127}
128
joshualitteb2a6762014-12-04 11:35:33 -0800129GrGLGeometryProcessor*
egdaniel309e3462014-12-09 10:35:58 -0800130GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const {
131 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800132}
joshualitt9b989322014-12-15 14:16:27 -0800133
134void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
135 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
136 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
137 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800138 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800139}
140
joshualitt290c09b2014-12-19 13:45:20 -0800141bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m,
142 const GrGeometryProcessor& that,
143 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800144 const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>();
145 const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800146 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
147 that, theirs.fUsesLocalCoords) &&
148 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800149 theirs.fInputColorType, theirs.fColor);
150}
151
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000152///////////////////////////////////////////////////////////////////////////////
153
egdaniel309e3462014-12-09 10:35:58 -0800154GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000155
egdaniel309e3462014-12-09 10:35:58 -0800156GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
157 GrContext*,
158 const GrDrawTargetCaps&,
159 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700160 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
161 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000162 static const SkShader::TileMode kTileModes[] = {
163 SkShader::kClamp_TileMode,
164 SkShader::kRepeat_TileMode,
165 SkShader::kMirror_TileMode,
166 };
167 SkShader::TileMode tileModes[] = {
168 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
169 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
170 };
171 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
172 GrTextureParams::kNone_FilterMode);
173
joshualitt2e3b3e32014-12-09 13:31:14 -0800174 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt56995b52014-12-11 15:44:02 -0800175 random->nextBool(), random->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000176}