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>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 78 | 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); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 82 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 83 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 84 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 85 | GrColor fColor; |
| 86 | UniformHandle fColorUniform; |
| 87 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 88 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /////////////////////////////////////////////////////////////////////////////// |
| 92 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 93 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 94 | const GrTextureParams& params, bool useColorAttrib, |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 95 | bool opaqueVertexColors, const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 96 | : INHERITED(color, SkMatrix::I(), localMatrix, opaqueVertexColors) |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 97 | , fTextureAccess(texture, params) |
| 98 | , fInColor(NULL) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 99 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 100 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 101 | if (useColorAttrib) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 102 | fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType)); |
| 103 | this->setHasVertexColor(); |
| 104 | } |
| 105 | fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords", |
| 106 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 107 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 108 | } |
| 109 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 110 | bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const { |
| 111 | const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 112 | return SkToBool(this->inColor()) == SkToBool(gp.inColor()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 113 | } |
| 114 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 115 | void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 116 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 117 | out->setUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 118 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 119 | out->setUnknownOpaqueFourComponents(); |
| 120 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 121 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 122 | out->setUnknownFourComponents(); |
| 123 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 127 | void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
| 128 | const GrGLCaps& caps, |
| 129 | GrProcessorKeyBuilder* b) const { |
| 130 | GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 131 | } |
| 132 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 133 | GrGLGeometryProcessor* |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 134 | GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt) const { |
| 135 | return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 136 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 137 | |
| 138 | void 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)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 142 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 143 | } |
| 144 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 145 | bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m, |
| 146 | const GrGeometryProcessor& that, |
| 147 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 148 | const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>(); |
| 149 | const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 150 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 151 | that, theirs.fUsesLocalCoords) && |
| 152 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 153 | theirs.fInputColorType, theirs.fColor); |
| 154 | } |
| 155 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 156 | /////////////////////////////////////////////////////////////////////////////// |
| 157 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 158 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 159 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 160 | GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
| 161 | GrContext*, |
| 162 | const GrDrawTargetCaps&, |
| 163 | GrTexture* textures[]) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 164 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 165 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 166 | 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 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 178 | return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params, |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 179 | random->nextBool(), random->nextBool(), |
| 180 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 181 | } |