blob: 594646256e58ff30db01d6e3e14fd4000965f161 [file] [log] [blame]
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +00001/*
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
egdaniel309e3462014-12-09 10:35:58 -08008#include "GrBitmapTextGeoProc.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080012#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
egdaniel2d721d32015-11-11 13:06:05 -080016#include "glsl/GrGLSLVertexShaderBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000017
egdaniele659a582015-11-13 09:55:43 -080018class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000019public:
joshualitt465283c2015-09-11 08:19:35 -070020 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000021
joshualitt465283c2015-09-11 08:19:35 -070022 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
egdaniel309e3462014-12-09 10:35:58 -080023 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080024
egdaniel8dcdedc2015-11-11 06:27:20 -080025 GrGLSLGPBuilder* pb = args.fPB;
egdaniel4ca2e602015-11-18 08:01:26 -080026 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080027 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000028
joshualittabb52a12015-01-13 15:02:10 -080029 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080030 varyingHandler->emitAttributes(cte);
joshualittabb52a12015-01-13 15:02:10 -080031
joshualitt922c8b12015-08-07 09:55:23 -070032 // compute numbers to be hardcoded to convert texture coordinates from int to float
33 SkASSERT(cte.numTextures() == 1);
34 GrTexture* atlas = cte.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070035 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070036 SkScalar recipWidth = 1.0f / atlas->width();
37 SkScalar recipHeight = 1.0f / atlas->height();
38
egdaniel8dcdedc2015-11-11 06:27:20 -080039 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -080040 varyingHandler->addVarying("TextureCoords", &v);
egdaniel4ca2e602015-11-18 08:01:26 -080041 vertBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
42 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
43 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
44 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000045
egdaniel4ca2e602015-11-18 08:01:26 -080046 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080047 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070048 if (!cte.colorIgnored()) {
49 if (cte.hasVertexColor()) {
egdaniel0eafe792015-11-20 14:01:22 -080050 varyingHandler->addPassThroughAttribute(cte.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070051 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080052 this->setupUniformColor(pb, fragBuilder, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070053 }
54 }
joshualitt2dd1ae02014-12-03 06:24:10 -080055
joshualittabb52a12015-01-13 15:02:10 -080056 // Setup position
egdaniel4ca2e602015-11-18 08:01:26 -080057 this->setupPosition(pb, vertBuilder, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080058
joshualittabb52a12015-01-13 15:02:10 -080059 // emit transforms
egdaniel4ca2e602015-11-18 08:01:26 -080060 this->emitTransforms(args.fPB,
61 vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080062 varyingHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080063 gpArgs->fPositionVar,
64 cte.inPosition()->fName,
65 cte.localMatrix(),
66 args.fTransformsIn,
67 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -080068
joshualitt02b05012015-02-11 06:56:30 -080069 if (cte.maskFormat() == kARGB_GrMaskFormat) {
egdaniel4ca2e602015-11-18 08:01:26 -080070 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
71 fragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
72 args.fSamplers[0],
73 v.fsIn(),
74 kVec2f_GrSLType);
75 fragBuilder->codeAppend(";");
76 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080077 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080078 fragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
79 fragBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
80 fragBuilder->codeAppend(";");
egdaniel27b63352015-09-15 13:13:50 -070081 if (cte.maskFormat() == kA565_GrMaskFormat) {
82 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -080083 fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
84 args.fOutputCoverage, args.fOutputCoverage,
85 args.fOutputCoverage, args.fOutputCoverage);
egdaniel27b63352015-09-15 13:13:50 -070086 }
joshualitt02b05012015-02-11 06:56:30 -080087 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000088 }
89
egdaniel018fb622015-10-28 07:26:40 -070090 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070091 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
92 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070093 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070094 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080095 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070096 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080097 }
joshualitt2dd1ae02014-12-03 06:24:10 -080098 }
99
joshualitte3ababe2015-05-15 07:56:07 -0700100 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700101 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700102 int index,
103 const SkTArray<const GrCoordTransform*, true>& transforms) override {
104 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
105 }
106
joshualitt9b989322014-12-15 14:16:27 -0800107 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700108 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800109 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800110 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800111 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700112 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
113 key |= gp.colorIgnored() ? 0x2 : 0x0;
114 key |= gp.maskFormat() << 3;
115 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700116
117 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
118 SkASSERT(gp.numTextures() == 1);
119 GrTexture* atlas = gp.textureAccess(0).getTexture();
120 SkASSERT(atlas);
121 b->add32(atlas->width());
122 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800123 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800124
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000125private:
joshualitt9b989322014-12-15 14:16:27 -0800126 GrColor fColor;
127 UniformHandle fColorUniform;
128
egdaniele659a582015-11-13 09:55:43 -0800129 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000130};
131
132///////////////////////////////////////////////////////////////////////////////
133
joshualitt2e3b3e32014-12-09 13:31:14 -0800134GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800135 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700136 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700137 : fColor(color)
138 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700139 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800140 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700141 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800142 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800143 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800144 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800145
joshualittb8c241a2015-05-19 08:23:30 -0700146 // TODO we could think about removing this attribute if color is ignored, but unfortunately
147 // we don't do text positioning in batch, so we can't quite do that yet.
joshualitt02b05012015-02-11 06:56:30 -0800148 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
149 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800150 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800151 }
joshualitt71c92602015-01-14 08:12:47 -0800152 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800153 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000154 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000155}
156
egdaniel57d3b032015-11-13 11:57:27 -0800157void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
158 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700159 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000160}
161
egdaniel57d3b032015-11-13 11:57:27 -0800162GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700163 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800164}
joshualitt9b989322014-12-15 14:16:27 -0800165
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000166///////////////////////////////////////////////////////////////////////////////
167
egdaniel309e3462014-12-09 10:35:58 -0800168GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000169
bsalomonc21b09e2015-08-28 18:46:56 -0700170const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700171 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
172 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000173 static const SkShader::TileMode kTileModes[] = {
174 SkShader::kClamp_TileMode,
175 SkShader::kRepeat_TileMode,
176 SkShader::kMirror_TileMode,
177 };
178 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700179 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
180 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000181 };
joshualitt0067ff52015-07-08 14:26:19 -0700182 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000183 GrTextureParams::kNone_FilterMode);
184
joshualitt02b05012015-02-11 06:56:30 -0800185 GrMaskFormat format;
joshualitt0067ff52015-07-08 14:26:19 -0700186 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800187 case 0:
188 format = kA8_GrMaskFormat;
189 break;
190 case 1:
191 format = kA565_GrMaskFormat;
192 break;
193 case 2:
194 format = kARGB_GrMaskFormat;
195 break;
196 }
197
joshualitt0067ff52015-07-08 14:26:19 -0700198 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
199 format, GrTest::TestMatrix(d->fRandom),
200 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000201}