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 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 28 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) 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 | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 35 | // emit attributes |
| 36 | vsBuilder->emitAttributes(cte); |
| 37 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 38 | GrGLVertToFrag v(kVec2f_GrSLType); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 39 | pb->addVarying("TextureCoords", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 40 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), cte.inTextureCoords()->fName); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 41 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 42 | // Setup pass through color |
| 43 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(), |
| 44 | &fColorUniform); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 45 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 46 | // setup uniform viewMatrix |
| 47 | this->addUniformViewMatrix(pb); |
| 48 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 49 | // Setup position |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 50 | SetupPosition(vsBuilder, gpArgs, cte.inPosition()->fName, cte.viewMatrix(), this->uViewM()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 51 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 52 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 53 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 54 | cte.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
| 55 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 56 | GrGLGPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 57 | if (cte.maskFormat() == kARGB_GrMaskFormat) { |
| 58 | fsBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 59 | fsBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 60 | args.fSamplers[0], |
| 61 | v.fsIn(), |
| 62 | kVec2f_GrSLType); |
| 63 | fsBuilder->codeAppend(";"); |
| 64 | fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
| 65 | } else { |
| 66 | fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
| 67 | fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); |
| 68 | fsBuilder->codeAppend(";"); |
| 69 | } |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 70 | } |
| 71 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 72 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 73 | const GrPrimitiveProcessor& gp, |
| 74 | const GrBatchTracker& bt) SK_OVERRIDE { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 75 | this->setUniformViewMatrix(pdman, gp.viewMatrix()); |
| 76 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 77 | const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); |
| 78 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 79 | GrGLfloat c[4]; |
| 80 | GrColorToRGBAFloat(local.fColor, c); |
| 81 | pdman.set4fv(fColorUniform, 1, c); |
| 82 | fColor = local.fColor; |
| 83 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 84 | } |
| 85 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 86 | static inline void GenKey(const GrGeometryProcessor& proc, |
| 87 | const GrBatchTracker& bt, |
| 88 | const GrGLCaps&, |
| 89 | GrProcessorKeyBuilder* b) { |
| 90 | const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); |
| 91 | // We have to put the optional vertex attribute as part of the key. See the comment |
| 92 | // on addVertexAttrib. |
| 93 | // TODO When we have deferred geometry we can fix this |
| 94 | const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 95 | uint32_t key = 0; |
| 96 | key |= SkToBool(gp.inColor()) ? 0x1 : 0x0; |
| 97 | key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0; |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 98 | key |= gp.maskFormat() == kARGB_GrMaskFormat ? 0x4 : 0x0; |
| 99 | key |= ComputePosKey(gp.viewMatrix()) << 3; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 100 | b->add32(local.fInputColorType << 16 | key); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 101 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 103 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 104 | GrColor fColor; |
| 105 | UniformHandle fColorUniform; |
| 106 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 107 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 112 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 113 | const GrTextureParams& params, GrMaskFormat format, |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 114 | bool opaqueVertexColors, const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 115 | : INHERITED(color, SkMatrix::I(), localMatrix, opaqueVertexColors) |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 116 | , fTextureAccess(texture, params) |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 117 | , fInColor(NULL) |
| 118 | , fMaskFormat(format) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 119 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 120 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 121 | |
| 122 | bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat; |
| 123 | if (hasVertexColor) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 124 | fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 125 | this->setHasVertexColor(); |
| 126 | } |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 127 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 128 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 129 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 132 | bool GrBitmapTextGeoProc::onIsEqual(const GrGeometryProcessor& other) const { |
| 133 | const GrBitmapTextGeoProc& gp = other.cast<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 134 | return SkToBool(this->inColor()) == SkToBool(gp.inColor()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 135 | } |
| 136 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 137 | void GrBitmapTextGeoProc::onGetInvariantOutputColor(GrInitInvariantOutput* out) const { |
| 138 | if (kARGB_GrMaskFormat == fMaskFormat) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 139 | out->setUnknownFourComponents(); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | void GrBitmapTextGeoProc::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 144 | if (kARGB_GrMaskFormat != fMaskFormat) { |
| 145 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
| 146 | out->setUnknownSingleComponent(); |
| 147 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 148 | out->setUnknownOpaqueFourComponents(); |
| 149 | out->setUsingLCDCoverage(); |
| 150 | } else { |
| 151 | out->setUnknownFourComponents(); |
| 152 | out->setUsingLCDCoverage(); |
| 153 | } |
| 154 | } else { |
| 155 | out->setKnownSingleComponent(0xff); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 159 | void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
| 160 | const GrGLCaps& caps, |
| 161 | GrProcessorKeyBuilder* b) const { |
| 162 | GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 163 | } |
| 164 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 165 | GrGLPrimitiveProcessor* |
| 166 | GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt, |
| 167 | const GrGLCaps& caps) const { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 168 | return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 169 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 170 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 171 | void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 172 | BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>(); |
| 173 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, |
| 174 | SkToBool(fInColor)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 175 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 176 | } |
| 177 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 178 | bool GrBitmapTextGeoProc::onCanMakeEqual(const GrBatchTracker& m, |
| 179 | const GrGeometryProcessor& that, |
| 180 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 181 | const BitmapTextBatchTracker& mine = m.cast<BitmapTextBatchTracker>(); |
| 182 | const BitmapTextBatchTracker& theirs = t.cast<BitmapTextBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 183 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 184 | that, theirs.fUsesLocalCoords) && |
| 185 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 186 | theirs.fInputColorType, theirs.fColor); |
| 187 | } |
| 188 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 191 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 192 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 193 | GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
| 194 | GrContext*, |
| 195 | const GrDrawTargetCaps&, |
| 196 | GrTexture* textures[]) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 197 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 198 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 199 | static const SkShader::TileMode kTileModes[] = { |
| 200 | SkShader::kClamp_TileMode, |
| 201 | SkShader::kRepeat_TileMode, |
| 202 | SkShader::kMirror_TileMode, |
| 203 | }; |
| 204 | SkShader::TileMode tileModes[] = { |
| 205 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 206 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 207 | }; |
| 208 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 209 | GrTextureParams::kNone_FilterMode); |
| 210 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 211 | GrMaskFormat format; |
| 212 | switch (random->nextULessThan(3)) { |
| 213 | default: |
| 214 | SkFAIL("Incomplete enum\n"); |
| 215 | case 0: |
| 216 | format = kA8_GrMaskFormat; |
| 217 | break; |
| 218 | case 1: |
| 219 | format = kA565_GrMaskFormat; |
| 220 | break; |
| 221 | case 2: |
| 222 | format = kARGB_GrMaskFormat; |
| 223 | break; |
| 224 | } |
| 225 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 226 | return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 227 | format, random->nextBool(), |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 228 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 229 | } |