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" |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 9 | #include "GrFontAtlasSizes.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 11 | #include "GrTexture.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "gl/GrGLProcessor.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 13 | #include "gl/GrGLSL.h" |
| 14 | #include "gl/GrGLTexture.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 15 | #include "gl/GrGLGeometryProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 16 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 17 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 18 | struct BitmapTextBatchTracker { |
| 19 | GrGPInput fInputColorType; |
| 20 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 21 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 24 | class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 25 | public: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 26 | GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&) |
| 27 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 30 | const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 31 | const BitmapTextBatchTracker& local = args.fBT.cast<BitmapTextBatchTracker>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 32 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 33 | GrGLGPBuilder* pb = args.fPB; |
| 34 | GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 35 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 36 | // emit attributes |
| 37 | vsBuilder->emitAttributes(cte); |
| 38 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 39 | GrGLVertToFrag v(kVec2f_GrSLType); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 40 | pb->addVarying("TextureCoords", &v); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 41 | // this is only used with text, so our texture bounds always match the glyph atlas |
jvanverth | cb251f1 | 2015-03-11 11:18:11 -0700 | [diff] [blame] | 42 | if (cte.maskFormat() == kA8_GrMaskFormat) { |
| 43 | vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", " |
| 44 | GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut(), |
| 45 | cte.inTextureCoords()->fName); |
| 46 | } else { |
| 47 | vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " |
| 48 | GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", v.vsOut(), |
| 49 | cte.inTextureCoords()->fName); |
| 50 | } |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 51 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 52 | // Setup pass through color |
| 53 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, cte.inColor(), |
| 54 | &fColorUniform); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 55 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 56 | // Setup position |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame^] | 57 | this->setupPosition(pb, gpArgs, cte.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 58 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 59 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 60 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 61 | cte.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
| 62 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 63 | GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 64 | if (cte.maskFormat() == kARGB_GrMaskFormat) { |
| 65 | fsBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 66 | fsBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 67 | args.fSamplers[0], |
| 68 | v.fsIn(), |
| 69 | kVec2f_GrSLType); |
| 70 | fsBuilder->codeAppend(";"); |
| 71 | fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
| 72 | } else { |
| 73 | fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
| 74 | fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); |
| 75 | fsBuilder->codeAppend(";"); |
| 76 | } |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 77 | } |
| 78 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 79 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 80 | const GrPrimitiveProcessor& gp, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 81 | const GrBatchTracker& bt) override { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 82 | const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); |
| 83 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 84 | GrGLfloat c[4]; |
| 85 | GrColorToRGBAFloat(local.fColor, c); |
| 86 | pdman.set4fv(fColorUniform, 1, c); |
| 87 | fColor = local.fColor; |
| 88 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 89 | } |
| 90 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 91 | static inline void GenKey(const GrGeometryProcessor& proc, |
| 92 | const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 93 | const GrGLSLCaps&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 94 | GrProcessorKeyBuilder* b) { |
| 95 | const BitmapTextBatchTracker& local = bt.cast<BitmapTextBatchTracker>(); |
| 96 | // We have to put the optional vertex attribute as part of the key. See the comment |
| 97 | // on addVertexAttrib. |
| 98 | // TODO When we have deferred geometry we can fix this |
| 99 | const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 100 | uint32_t key = 0; |
| 101 | key |= SkToBool(gp.inColor()) ? 0x1 : 0x0; |
| 102 | key |= local.fUsesLocalCoords && proc.localMatrix().hasPerspective() ? 0x2 : 0x0; |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 103 | key |= gp.maskFormat() == kARGB_GrMaskFormat ? 0x4 : 0x0; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 104 | b->add32(local.fInputColorType << 16 | key); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 105 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 106 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 107 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 108 | GrColor fColor; |
| 109 | UniformHandle fColorUniform; |
| 110 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 111 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 116 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 117 | const GrTextureParams& params, GrMaskFormat format, |
joshualitt | 1ba8cc9 | 2015-05-13 12:24:23 -0700 | [diff] [blame] | 118 | const SkMatrix& localMatrix) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame^] | 119 | : INHERITED(localMatrix) |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 120 | , fColor(color) |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 121 | , fTextureAccess(texture, params) |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 122 | , fInColor(NULL) |
| 123 | , fMaskFormat(format) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 124 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 125 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 126 | |
| 127 | bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat; |
| 128 | if (hasVertexColor) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 129 | fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 130 | } |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 131 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 132 | kVec2s_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 133 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 134 | } |
| 135 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 136 | void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 137 | const GrGLSLCaps& caps, |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 138 | GrProcessorKeyBuilder* b) const { |
| 139 | GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 140 | } |
| 141 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 142 | GrGLPrimitiveProcessor* |
| 143 | GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 144 | const GrGLSLCaps& caps) const { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 145 | return SkNEW_ARGS(GrGLBitmapTextGeoProc, (*this, bt)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 146 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 147 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 148 | void GrBitmapTextGeoProc::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 149 | BitmapTextBatchTracker* local = bt->cast<BitmapTextBatchTracker>(); |
| 150 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, |
| 151 | SkToBool(fInColor)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 152 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 153 | } |
| 154 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 157 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 158 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 159 | GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(SkRandom* random, |
| 160 | GrContext*, |
| 161 | const GrDrawTargetCaps&, |
| 162 | GrTexture* textures[]) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 163 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 164 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 165 | static const SkShader::TileMode kTileModes[] = { |
| 166 | SkShader::kClamp_TileMode, |
| 167 | SkShader::kRepeat_TileMode, |
| 168 | SkShader::kMirror_TileMode, |
| 169 | }; |
| 170 | SkShader::TileMode tileModes[] = { |
| 171 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 172 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 173 | }; |
| 174 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 175 | GrTextureParams::kNone_FilterMode); |
| 176 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 177 | GrMaskFormat format; |
| 178 | switch (random->nextULessThan(3)) { |
| 179 | default: |
| 180 | SkFAIL("Incomplete enum\n"); |
| 181 | case 0: |
| 182 | format = kA8_GrMaskFormat; |
| 183 | break; |
| 184 | case 1: |
| 185 | format = kA565_GrMaskFormat; |
| 186 | break; |
| 187 | case 2: |
| 188 | format = kARGB_GrMaskFormat; |
| 189 | break; |
| 190 | } |
| 191 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 192 | return GrBitmapTextGeoProc::Create(GrRandomColor(random), textures[texIdx], params, |
joshualitt | 1ba8cc9 | 2015-05-13 12:24:23 -0700 | [diff] [blame] | 193 | format, GrTest::TestMatrix(random)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 194 | } |