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