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" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVarying.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 17 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 18 | class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 19 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 20 | GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 21 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 22 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 23 | const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 24 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 25 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 26 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 27 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
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 |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 30 | varyingHandler->emitAttributes(cte); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 31 | |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 32 | // compute numbers to be hardcoded to convert texture coordinates from int to float |
| 33 | SkASSERT(cte.numTextures() == 1); |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 34 | SkDEBUGCODE(GrTexture* atlas = cte.textureAccess(0).getTexture()); |
joshualitt | 7375d6b | 2015-08-07 13:36:44 -0700 | [diff] [blame] | 35 | SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height())); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 36 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 37 | GrGLSLVertToFrag v(kVec2f_GrSLType); |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 38 | varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision); |
| 39 | vertBuilder->codeAppendf("%s = %s;", v.vsOut(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 40 | cte.inTextureCoords()->fName); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 41 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 42 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
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()) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 46 | varyingHandler->addPassThroughAttribute(cte.inColor(), args.fOutputColor); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 47 | } else { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 48 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 49 | &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 52 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 53 | // Setup position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 54 | this->setupPosition(vertBuilder, gpArgs, cte.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 55 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 56 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 57 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 58 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 59 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 60 | gpArgs->fPositionVar, |
| 61 | cte.inPosition()->fName, |
| 62 | cte.localMatrix(), |
| 63 | args.fTransformsIn, |
| 64 | args.fTransformsOut); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 65 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 66 | if (cte.maskFormat() == kARGB_GrMaskFormat) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 67 | fragBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 68 | fragBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 69 | args.fTexSamplers[0], |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 70 | v.fsIn(), |
| 71 | kVec2f_GrSLType); |
| 72 | fragBuilder->codeAppend(";"); |
| 73 | fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 74 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 75 | fragBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 76 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], v.fsIn(), kVec2f_GrSLType); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 77 | fragBuilder->codeAppend(";"); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 78 | if (cte.maskFormat() == kA565_GrMaskFormat) { |
| 79 | // set alpha to be max of rgb coverage |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 80 | fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);", |
| 81 | args.fOutputCoverage, args.fOutputCoverage, |
| 82 | args.fOutputCoverage, args.fOutputCoverage); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 83 | } |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 84 | } |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 85 | } |
| 86 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 87 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 88 | const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>(); |
| 89 | if (btgp.color() != fColor && !btgp.hasVertexColor()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 90 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 91 | GrColorToRGBAFloat(btgp.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 92 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 93 | fColor = btgp.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 94 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 95 | } |
| 96 | |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 97 | void setTransformData(const GrPrimitiveProcessor& primProc, |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 98 | const GrGLSLProgramDataManager& pdman, |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 99 | int index, |
| 100 | const SkTArray<const GrCoordTransform*, true>& transforms) override { |
bsalomon | e4f2461 | 2016-08-17 10:30:17 -0700 | [diff] [blame] | 101 | this->setTransformDataHelper(primProc.cast<GrBitmapTextGeoProc>().localMatrix(), pdman, |
| 102 | index, transforms); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 103 | } |
| 104 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 105 | static inline void GenKey(const GrGeometryProcessor& proc, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 106 | const GrGLSLCaps&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 107 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 108 | const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 109 | uint32_t key = 0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 110 | key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 111 | key |= gp.colorIgnored() ? 0x2 : 0x0; |
| 112 | key |= gp.maskFormat() << 3; |
| 113 | b->add32(key); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 114 | |
| 115 | // Currently we hardcode numbers to convert atlas coordinates to normalized floating point |
| 116 | SkASSERT(gp.numTextures() == 1); |
| 117 | GrTexture* atlas = gp.textureAccess(0).getTexture(); |
| 118 | SkASSERT(atlas); |
| 119 | b->add32(atlas->width()); |
| 120 | b->add32(atlas->height()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 121 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 122 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 123 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 124 | GrColor fColor; |
| 125 | UniformHandle fColorUniform; |
| 126 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 127 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 132 | GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 133 | const GrTextureParams& params, GrMaskFormat format, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 134 | const SkMatrix& localMatrix, bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 135 | : fColor(color) |
| 136 | , fLocalMatrix(localMatrix) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 137 | , fUsesLocalCoords(usesLocalCoords) |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 138 | , fTextureAccess(texture, params) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | , fInColor(nullptr) |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 140 | , fMaskFormat(format) { |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 141 | this->initClassID<GrBitmapTextGeoProc>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 142 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 143 | |
joshualitt | d9d30f7 | 2015-12-08 10:47:55 -0800 | [diff] [blame] | 144 | bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat || |
| 145 | kA565_GrMaskFormat == fMaskFormat; |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 146 | if (hasVertexColor) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 147 | fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 148 | } |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 149 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 150 | kVec2us_GrVertexAttribType, |
| 151 | kHigh_GrSLPrecision)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 152 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 153 | } |
| 154 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 155 | void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 156 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 157 | GrGLBitmapTextGeoProc::GenKey(*this, caps, b); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 158 | } |
| 159 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 160 | GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 161 | return new GrGLBitmapTextGeoProc(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 162 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 163 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 166 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 167 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 168 | sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 169 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 170 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 171 | static const SkShader::TileMode kTileModes[] = { |
| 172 | SkShader::kClamp_TileMode, |
| 173 | SkShader::kRepeat_TileMode, |
| 174 | SkShader::kMirror_TileMode, |
| 175 | }; |
| 176 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 177 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 178 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 179 | }; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 180 | GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 181 | GrTextureParams::kNone_FilterMode); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 182 | |
robertphillips | b63c576 | 2016-04-08 05:24:21 -0700 | [diff] [blame] | 183 | GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 184 | switch (d->fRandom->nextULessThan(3)) { |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 185 | case 0: |
| 186 | format = kA8_GrMaskFormat; |
| 187 | break; |
| 188 | case 1: |
| 189 | format = kA565_GrMaskFormat; |
| 190 | break; |
| 191 | case 2: |
| 192 | format = kARGB_GrMaskFormat; |
| 193 | break; |
| 194 | } |
| 195 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 196 | return GrBitmapTextGeoProc::Make(GrRandomColor(d->fRandom), d->fTextures[texIdx], params, |
| 197 | format, GrTest::TestMatrix(d->fRandom), |
| 198 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 199 | } |