blob: e8bcd57e6ddd7739061d7f538af588aa7f760b83 [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"
wangyix6af0c932015-07-22 10:21:17 -070011#include "gl/GrGLFragmentProcessor.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000012#include "gl/GrGLTexture.h"
joshualitt249af152014-09-15 11:41:13 -070013#include "gl/GrGLGeometryProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080014#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000015
egdaniel309e3462014-12-09 10:35:58 -080016class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000017public:
joshualitt465283c2015-09-11 08:19:35 -070018 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000019
joshualitt465283c2015-09-11 08:19:35 -070020 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
egdaniel309e3462014-12-09 10:35:58 -080021 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080022
joshualitt9b989322014-12-15 14:16:27 -080023 GrGLGPBuilder* pb = args.fPB;
24 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000025
joshualittabb52a12015-01-13 15:02:10 -080026 // emit attributes
27 vsBuilder->emitAttributes(cte);
28
joshualitt922c8b12015-08-07 09:55:23 -070029 // 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();
joshualitt7375d6b2015-08-07 13:36:44 -070032 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070033 SkScalar recipWidth = 1.0f / atlas->width();
34 SkScalar recipHeight = 1.0f / atlas->height();
35
joshualitt74077b92014-10-24 11:26:03 -070036 GrGLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080037 pb->addVarying("TextureCoords", &v);
joshualitt922c8b12015-08-07 09:55:23 -070038 vsBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
joshualitt7375d6b2015-08-07 13:36:44 -070039 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
40 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
joshualitt922c8b12015-08-07 09:55:23 -070041 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000042
joshualitt9b989322014-12-15 14:16:27 -080043 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070044 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 }
joshualitt2dd1ae02014-12-03 06:24:10 -080051
joshualittabb52a12015-01-13 15:02:10 -080052 // Setup position
joshualitte578a952015-05-14 10:09:13 -070053 this->setupPosition(pb, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080054
joshualittabb52a12015-01-13 15:02:10 -080055 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -080056 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080057 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
58
egdaniel29bee0f2015-04-29 11:54:42 -070059 GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt02b05012015-02-11 06:56:30 -080060 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.org76eaf742013-09-30 18:41:38 +000073 }
74
joshualitt465283c2015-09-11 08:19:35 -070075 void setData(const GrGLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070076 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
77 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
joshualitt9b989322014-12-15 14:16:27 -080078 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -070079 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080080 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070081 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080082 }
joshualitt2dd1ae02014-12-03 06:24:10 -080083 }
84
joshualitte3ababe2015-05-15 07:56:07 -070085 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
joshualitt9b989322014-12-15 14:16:27 -080092 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -070093 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -080094 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -080095 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -080096 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -070097 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
98 key |= gp.colorIgnored() ? 0x2 : 0x0;
99 key |= gp.maskFormat() << 3;
100 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700101
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());
joshualitt9b989322014-12-15 14:16:27 -0800108 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800109
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000110private:
joshualitt9b989322014-12-15 14:16:27 -0800111 GrColor fColor;
112 UniformHandle fColorUniform;
113
joshualitt249af152014-09-15 11:41:13 -0700114 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000115};
116
117///////////////////////////////////////////////////////////////////////////////
118
joshualitt2e3b3e32014-12-09 13:31:14 -0800119GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800120 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700121 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700122 : fColor(color)
123 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700124 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800125 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700126 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800127 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800128 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800129 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800130
joshualittb8c241a2015-05-19 08:23:30 -0700131 // 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.
joshualitt02b05012015-02-11 06:56:30 -0800133 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
134 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800135 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800136 }
joshualitt71c92602015-01-14 08:12:47 -0800137 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800138 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000139 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000140}
141
joshualitt465283c2015-09-11 08:19:35 -0700142void GrBitmapTextGeoProc::getGLProcessorKey(const GrGLSLCaps& caps,GrProcessorKeyBuilder* b) const {
143 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000144}
145
joshualitt465283c2015-09-11 08:19:35 -0700146GrGLPrimitiveProcessor* GrBitmapTextGeoProc::createGLInstance(const GrGLSLCaps& caps) const {
147 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800148}
joshualitt9b989322014-12-15 14:16:27 -0800149
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000150///////////////////////////////////////////////////////////////////////////////
151
egdaniel309e3462014-12-09 10:35:58 -0800152GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000153
bsalomonc21b09e2015-08-28 18:46:56 -0700154const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700155 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
156 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000157 static const SkShader::TileMode kTileModes[] = {
158 SkShader::kClamp_TileMode,
159 SkShader::kRepeat_TileMode,
160 SkShader::kMirror_TileMode,
161 };
162 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700163 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
164 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000165 };
joshualitt0067ff52015-07-08 14:26:19 -0700166 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000167 GrTextureParams::kNone_FilterMode);
168
joshualitt02b05012015-02-11 06:56:30 -0800169 GrMaskFormat format;
joshualitt0067ff52015-07-08 14:26:19 -0700170 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800171 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
joshualitt0067ff52015-07-08 14:26:19 -0700182 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
183 format, GrTest::TestMatrix(d->fRandom),
184 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000185}