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