blob: 44de98a348d75f0b279aedcd9ed6d5b5b9fcca69 [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>();
joshualitt8fc6c2d2014-12-22 15:27:05 -080078 uint32_t key = 0;
79 key |= SkToBool(gp.inColor()) ? 0x1 : 0x0;
80 key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0;
81 b->add32(local.fInputColorType << 16 | key);
joshualitt9b989322014-12-15 14:16:27 -080082 }
joshualitt2dd1ae02014-12-03 06:24:10 -080083
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000084private:
joshualitt9b989322014-12-15 14:16:27 -080085 GrColor fColor;
86 UniformHandle fColorUniform;
87
joshualitt249af152014-09-15 11:41:13 -070088 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000089};
90
91///////////////////////////////////////////////////////////////////////////////
92
joshualitt2e3b3e32014-12-09 13:31:14 -080093GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt56995b52014-12-11 15:44:02 -080094 const GrTextureParams& params, bool useColorAttrib,
joshualitt8fc6c2d2014-12-22 15:27:05 -080095 bool opaqueVertexColors, const SkMatrix& localMatrix)
96 : INHERITED(color, opaqueVertexColors, localMatrix)
97 , fTextureAccess(texture, params)
98 , fInColor(NULL) {
egdaniel309e3462014-12-09 10:35:58 -080099 this->initClassID<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800100 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
egdaniel309e3462014-12-09 10:35:58 -0800101 if (useColorAttrib) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800102 fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType));
103 this->setHasVertexColor();
104 }
105 fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords",
106 kVec2f_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000107 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000108}
109
egdaniel309e3462014-12-09 10:35:58 -0800110bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const {
111 const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800112 return SkToBool(this->inColor()) == SkToBool(gp.inColor());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000113}
114
joshualitt56995b52014-12-11 15:44:02 -0800115void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
egdanielf8449ba2014-11-25 10:24:56 -0800116 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800117 out->setUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800118 } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800119 out->setUnknownOpaqueFourComponents();
120 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000121 } else {
joshualitt56995b52014-12-11 15:44:02 -0800122 out->setUnknownFourComponents();
123 out->setUsingLCDCoverage();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000124 }
125}
126
egdaniel309e3462014-12-09 10:35:58 -0800127void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
128 const GrGLCaps& caps,
129 GrProcessorKeyBuilder* b) const {
130 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000131}
132
joshualitteb2a6762014-12-04 11:35:33 -0800133GrGLGeometryProcessor*
egdaniel309e3462014-12-09 10:35:58 -0800134GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const {
135 return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt));
joshualitteb2a6762014-12-04 11:35:33 -0800136}
joshualitt9b989322014-12-15 14:16:27 -0800137
138void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
139 BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>();
140 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init,
141 SkToBool(fInColor));
joshualitt290c09b2014-12-19 13:45:20 -0800142 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800143}
144
joshualitt290c09b2014-12-19 13:45:20 -0800145bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m,
146 const GrGeometryProcessor& that,
147 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800148 const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>();
149 const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800150 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
151 that, theirs.fUsesLocalCoords) &&
152 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800153 theirs.fInputColorType, theirs.fColor);
154}
155
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000156///////////////////////////////////////////////////////////////////////////////
157
egdaniel309e3462014-12-09 10:35:58 -0800158GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000159
egdaniel309e3462014-12-09 10:35:58 -0800160GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random,
161 GrContext*,
162 const GrDrawTargetCaps&,
163 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700164 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
165 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000166 static const SkShader::TileMode kTileModes[] = {
167 SkShader::kClamp_TileMode,
168 SkShader::kRepeat_TileMode,
169 SkShader::kMirror_TileMode,
170 };
171 SkShader::TileMode tileModes[] = {
172 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
173 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
174 };
175 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
176 GrTextureParams::kNone_FilterMode);
177
joshualitt2e3b3e32014-12-09 13:31:14 -0800178 return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800179 random->nextBool(), random->nextBool(),
180 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000181}