blob: 7b179244a1f27654f742f27fec7acdd5555fcbbf [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"
joshualitt249af152014-09-15 11:41:13 -070011#include "gl/GrGLGeometryProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.h"
egdaniel2d721d32015-11-11 13:06:05 -080016#include "glsl/GrGLSLVertexShaderBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000017
egdaniel309e3462014-12-09 10:35:58 -080018class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
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;
egdaniel2d721d32015-11-11 13:06:05 -080026 GrGLSLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000027
joshualittabb52a12015-01-13 15:02:10 -080028 // emit attributes
29 vsBuilder->emitAttributes(cte);
30
joshualitt922c8b12015-08-07 09:55:23 -070031 // compute numbers to be hardcoded to convert texture coordinates from int to float
32 SkASSERT(cte.numTextures() == 1);
33 GrTexture* atlas = cte.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070034 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070035 SkScalar recipWidth = 1.0f / atlas->width();
36 SkScalar recipHeight = 1.0f / atlas->height();
37
egdaniel8dcdedc2015-11-11 06:27:20 -080038 GrGLSLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080039 pb->addVarying("TextureCoords", &v);
joshualitt922c8b12015-08-07 09:55:23 -070040 vsBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
joshualitt7375d6b2015-08-07 13:36:44 -070041 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
42 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
joshualitt922c8b12015-08-07 09:55:23 -070043 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000044
joshualitt9b989322014-12-15 14:16:27 -080045 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070046 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 }
joshualitt2dd1ae02014-12-03 06:24:10 -080053
joshualittabb52a12015-01-13 15:02:10 -080054 // Setup position
joshualitte578a952015-05-14 10:09:13 -070055 this->setupPosition(pb, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080056
joshualittabb52a12015-01-13 15:02:10 -080057 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -080058 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080059 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
60
egdaniel2d721d32015-11-11 13:06:05 -080061 GrGLSLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt02b05012015-02-11 06:56:30 -080062 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(";");
egdaniel27b63352015-09-15 13:13:50 -070074 if (cte.maskFormat() == kA565_GrMaskFormat) {
75 // set alpha to be max of rgb coverage
76 fsBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
77 args.fOutputCoverage, args.fOutputCoverage,
78 args.fOutputCoverage, args.fOutputCoverage);
79 }
joshualitt02b05012015-02-11 06:56:30 -080080 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000081 }
82
egdaniel018fb622015-10-28 07:26:40 -070083 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070084 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
85 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070086 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070087 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080088 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070089 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080090 }
joshualitt2dd1ae02014-12-03 06:24:10 -080091 }
92
joshualitte3ababe2015-05-15 07:56:07 -070093 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -070094 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -070095 int index,
96 const SkTArray<const GrCoordTransform*, true>& transforms) override {
97 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
98 }
99
joshualitt9b989322014-12-15 14:16:27 -0800100 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700101 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800102 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800103 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800104 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700105 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
106 key |= gp.colorIgnored() ? 0x2 : 0x0;
107 key |= gp.maskFormat() << 3;
108 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700109
110 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
111 SkASSERT(gp.numTextures() == 1);
112 GrTexture* atlas = gp.textureAccess(0).getTexture();
113 SkASSERT(atlas);
114 b->add32(atlas->width());
115 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800116 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800117
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000118private:
joshualitt9b989322014-12-15 14:16:27 -0800119 GrColor fColor;
120 UniformHandle fColorUniform;
121
joshualitt249af152014-09-15 11:41:13 -0700122 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000123};
124
125///////////////////////////////////////////////////////////////////////////////
126
joshualitt2e3b3e32014-12-09 13:31:14 -0800127GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800128 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700129 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700130 : fColor(color)
131 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700132 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800133 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700134 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800135 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800136 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800137 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800138
joshualittb8c241a2015-05-19 08:23:30 -0700139 // TODO we could think about removing this attribute if color is ignored, but unfortunately
140 // we don't do text positioning in batch, so we can't quite do that yet.
joshualitt02b05012015-02-11 06:56:30 -0800141 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
142 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800143 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800144 }
joshualitt71c92602015-01-14 08:12:47 -0800145 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800146 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000147 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000148}
149
joshualitt465283c2015-09-11 08:19:35 -0700150void GrBitmapTextGeoProc::getGLProcessorKey(const GrGLSLCaps& caps,GrProcessorKeyBuilder* b) const {
151 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000152}
153
joshualitt465283c2015-09-11 08:19:35 -0700154GrGLPrimitiveProcessor* GrBitmapTextGeoProc::createGLInstance(const GrGLSLCaps& caps) const {
155 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800156}
joshualitt9b989322014-12-15 14:16:27 -0800157
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000158///////////////////////////////////////////////////////////////////////////////
159
egdaniel309e3462014-12-09 10:35:58 -0800160GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000161
bsalomonc21b09e2015-08-28 18:46:56 -0700162const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700163 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
164 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000165 static const SkShader::TileMode kTileModes[] = {
166 SkShader::kClamp_TileMode,
167 SkShader::kRepeat_TileMode,
168 SkShader::kMirror_TileMode,
169 };
170 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700171 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
172 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000173 };
joshualitt0067ff52015-07-08 14:26:19 -0700174 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000175 GrTextureParams::kNone_FilterMode);
176
joshualitt02b05012015-02-11 06:56:30 -0800177 GrMaskFormat format;
joshualitt0067ff52015-07-08 14:26:19 -0700178 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800179 case 0:
180 format = kA8_GrMaskFormat;
181 break;
182 case 1:
183 format = kA565_GrMaskFormat;
184 break;
185 case 2:
186 format = kARGB_GrMaskFormat;
187 break;
188 }
189
joshualitt0067ff52015-07-08 14:26:19 -0700190 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
191 format, GrTest::TestMatrix(d->fRandom),
192 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000193}