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 | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame^] | 47 | // setup uniform viewMatrix |
| 48 | this->addUniformViewMatrix(pb); |
| 49 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 50 | // setup position varying |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame^] | 51 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(), |
| 52 | cte.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 53 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 54 | GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 55 | fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
| 56 | fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 57 | fsBuilder->codeAppend(";"); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 58 | } |
| 59 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 60 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 61 | const GrPrimitiveProcessor& gp, |
| 62 | const GrBatchTracker& bt) SK_OVERRIDE { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame^] | 63 | this->setUniformViewMatrix(pdman, gp.viewMatrix()); |
| 64 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 65 | 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 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 72 | } |
| 73 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 74 | 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>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 83 | 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); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 87 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 89 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 90 | GrColor fColor; |
| 91 | UniformHandle fColorUniform; |
| 92 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 93 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 98 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 99 | const GrTextureParams& params, bool useColorAttrib, |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 100 | bool opaqueVertexColors, const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 101 | : INHERITED(color, SkMatrix::I(), localMatrix, opaqueVertexColors) |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 102 | , fTextureAccess(texture, params) |
| 103 | , fInColor(NULL) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 104 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 105 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 106 | if (useColorAttrib) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 107 | fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType)); |
| 108 | this->setHasVertexColor(); |
| 109 | } |
| 110 | fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords", |
| 111 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 112 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 113 | } |
| 114 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 115 | bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const { |
| 116 | const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 117 | return SkToBool(this->inColor()) == SkToBool(gp.inColor()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 120 | void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 121 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 122 | out->setUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 123 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 124 | out->setUnknownOpaqueFourComponents(); |
| 125 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 126 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 127 | out->setUnknownFourComponents(); |
| 128 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 132 | void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
| 133 | const GrGLCaps& caps, |
| 134 | GrProcessorKeyBuilder* b) const { |
| 135 | GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 136 | } |
| 137 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 138 | GrGLGeometryProcessor* |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 139 | GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const { |
| 140 | return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 141 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 142 | |
| 143 | void 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)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 147 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 148 | } |
| 149 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 150 | bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m, |
| 151 | const GrGeometryProcessor& that, |
| 152 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 153 | const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>(); |
| 154 | const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 155 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 156 | that, theirs.fUsesLocalCoords) && |
| 157 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 158 | theirs.fInputColorType, theirs.fColor); |
| 159 | } |
| 160 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 163 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 164 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 165 | GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
| 166 | GrContext*, |
| 167 | const GrDrawTargetCaps&, |
| 168 | GrTexture* textures[]) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 169 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 170 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 171 | 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 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 183 | return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params, |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 184 | random->nextBool(), random->nextBool(), |
| 185 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 186 | } |