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