commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 8 | #include "GrBitmapTextGeoProc.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 10 | #include "GrTexture.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "gl/GrGLProcessor.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 12 | #include "gl/GrGLSL.h" |
| 13 | #include "gl/GrGLTexture.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 14 | #include "gl/GrGLGeometryProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 15 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 16 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 17 | struct BitmapTextBatchTracker { |
| 18 | GrGPInput fInputColorType; |
| 19 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame^] | 20 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 21 | }; |
| 22 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 23 | class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 24 | public: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 25 | GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&) |
| 26 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 27 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 28 | virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 29 | const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 30 | const BitmapTextBatchTracker& local = args.fBT.cast<BitmapTextBatchTracker>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 31 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 32 | GrGLGPBuilder* pb = args.fPB; |
| 33 | GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 34 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 35 | GrGLVertToFrag v(kVec2f_GrSLType); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 36 | pb->addVarying("TextureCoords", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 37 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), cte.inTextureCoords()->fName); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 38 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 39 | // Setup pass through color |
| 40 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(), |
| 41 | &fColorUniform); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 42 | |
| 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.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 46 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 47 | // setup position varying |
| 48 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 49 | vsBuilder->uViewM(), cte.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 50 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 51 | GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 52 | fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
| 53 | fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 54 | fsBuilder->codeAppend(";"); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 55 | } |
| 56 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 57 | 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 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 67 | } |
| 68 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 69 | 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 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 82 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 83 | GrColor fColor; |
| 84 | UniformHandle fColorUniform; |
| 85 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 86 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 91 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 92 | const GrTextureParams& params, bool useColorAttrib, |
| 93 | bool opaqueVertexColors) |
| 94 | : INHERITED(color, opaqueVertexColors), fTextureAccess(texture, params), fInColor(NULL) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 95 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 96 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 97 | if (useColorAttrib) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 98 | fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType)); |
| 99 | this->setHasVertexColor(); |
| 100 | } |
| 101 | fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords", |
| 102 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 103 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 104 | } |
| 105 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 106 | bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const { |
| 107 | const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 108 | return SkToBool(this->inColor()) == SkToBool(gp.inColor()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 109 | } |
| 110 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 111 | void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 112 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 113 | out->setUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 114 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 115 | out->setUnknownOpaqueFourComponents(); |
| 116 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 117 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 118 | out->setUnknownFourComponents(); |
| 119 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 123 | void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
| 124 | const GrGLCaps& caps, |
| 125 | GrProcessorKeyBuilder* b) const { |
| 126 | GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 127 | } |
| 128 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 129 | GrGLGeometryProcessor* |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 130 | GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const { |
| 131 | return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 132 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 133 | |
| 134 | void 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)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame^] | 138 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame^] | 141 | bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m, |
| 142 | const GrGeometryProcessor& that, |
| 143 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 144 | const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>(); |
| 145 | const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame^] | 146 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 147 | that, theirs.fUsesLocalCoords) && |
| 148 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 149 | theirs.fInputColorType, theirs.fColor); |
| 150 | } |
| 151 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 154 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 155 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 156 | GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
| 157 | GrContext*, |
| 158 | const GrDrawTargetCaps&, |
| 159 | GrTexture* textures[]) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 160 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 161 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 162 | 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 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 174 | return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 175 | random->nextBool(), random->nextBool()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 176 | } |