blob: 5f44d3231817ff8f0ee756d7c07d93f8f7ba8314 [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
joshualittee2af952014-12-30 09:04:15 -080047 // setup uniform viewMatrix
48 this->addUniformViewMatrix(pb);
49
joshualitt4973d9d2014-11-08 09:24:25 -080050 // setup position varying
joshualittee2af952014-12-30 09:04:15 -080051 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(),
52 cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080053
joshualitt9b989322014-12-15 14:16:27 -080054 GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -080055 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage);
56 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -070057 fsBuilder->codeAppend(";");
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000058 }
59
joshualitt9b989322014-12-15 14:16:27 -080060 virtual void setData(const GrGLProgramDataManager& pdman,
61 const GrPrimitiveProcessor& gp,
62 const GrBatchTracker& bt) SK_OVERRIDE {
joshualittee2af952014-12-30 09:04:15 -080063 this->setUniformViewMatrix(pdman, gp.viewMatrix());
64
joshualitt9b989322014-12-15 14:16:27 -080065 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
66 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
67 GrGLfloat c[4];
68 GrColorToRGBAFloat(local.fColor, c);
69 pdman.set4fv(fColorUniform, 1, c);
70 fColor = local.fColor;
71 }
joshualitt2dd1ae02014-12-03 06:24:10 -080072 }
73
joshualitt9b989322014-12-15 14:16:27 -080074 static inline void GenKey(const GrGeometryProcessor& proc,
75 const GrBatchTracker& bt,
76 const GrGLCaps&,
77 GrProcessorKeyBuilder* b) {
78 const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>();
79 // We have to put the optional vertex attribute as part of the key. See the comment
80 // on addVertexAttrib.
81 // TODO When we have deferred geometry we can fix this
82 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -080083 uint32_t key = 0;
84 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0;
85 key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0;
86 b->add32(local.fInputColorType << 16 | key);
joshualitt9b989322014-12-15 14:16:27 -080087 }
joshualitt2dd1ae02014-12-03 06:24:10 -080088
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000089private:
joshualitt9b989322014-12-15 14:16:27 -080090 GrColor fColor;
91 UniformHandle fColorUniform;
92
joshualitt249af152014-09-15 11:41:13 -070093 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000094};
95
96///////////////////////////////////////////////////////////////////////////////
97
joshualitt2e3b3e32014-12-09 13:31:14 -080098GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt56995b52014-12-11 15:44:02 -080099 const GrTextureParams& params, bool useColorAttrib,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800100 bool opaqueVertexColors, const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800101 : INHERITED(color, SkMatrix::I(), localMatrix, opaqueVertexColors)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800102 , fTextureAccess(texture, params)
103 , fInColor(NULL) {
egdaniel309e3462014-12-09 10:35:58 -0800104 this->initClassID<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800105 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
egdaniel309e3462014-12-09 10:35:58 -0800106 if (useColorAttrib) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800107 fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType));
108 this->setHasVertexColor();
109 }
110 fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords",
111 kVec2f_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000112 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000113}
114
egdaniel309e3462014-12-09 10:35:58 -0800115bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const {
116 const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800117 return SkToBool(this->inColor()) == SkToBool(gp.inColor());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000118}
119
joshualitt56995b52014-12-11 15:44:02 -0800120void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
egdanielf8449ba2014-11-25 10:24:56 -0800121 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800122 out->setUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800123 } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800124 out->setUnknownOpaqueFourComponents();
125 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000126 } else {
joshualitt56995b52014-12-11 15:44:02 -0800127 out->setUnknownFourComponents();
128 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000129 }
130}
131
egdaniel309e3462014-12-09 10:35:58 -0800132void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
133 const GrGLCaps& caps,
134 GrProcessorKeyBuilder* b) const {
135 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000136}
137
joshualitteb2a6762014-12-04 11:35:33 -0800138GrGLGeometryProcessor*
egdaniel309e3462014-12-09 10:35:58 -0800139GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const {
140 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800141}
joshualitt9b989322014-12-15 14:16:27 -0800142
143void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
144 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
145 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
146 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800147 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800148}
149
joshualitt290c09b2014-12-19 13:45:20 -0800150bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m,
151 const GrGeometryProcessor& that,
152 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800153 const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>();
154 const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800155 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
156 that, theirs.fUsesLocalCoords) &&
157 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800158 theirs.fInputColorType, theirs.fColor);
159}
160
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000161///////////////////////////////////////////////////////////////////////////////
162
egdaniel309e3462014-12-09 10:35:58 -0800163GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000164
egdaniel309e3462014-12-09 10:35:58 -0800165GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
166 GrContext*,
167 const GrDrawTargetCaps&,
168 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700169 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
170 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000171 static const SkShader::TileMode kTileModes[] = {
172 SkShader::kClamp_TileMode,
173 SkShader::kRepeat_TileMode,
174 SkShader::kMirror_TileMode,
175 };
176 SkShader::TileMode tileModes[] = {
177 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
178 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
179 };
180 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
181 GrTextureParams::kNone_FilterMode);
182
joshualitt2e3b3e32014-12-09 13:31:14 -0800183 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800184 random->nextBool(), random->nextBool(),
185 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000186}