blob: 85d08629ac5bb4bf852923667df4e68d37108719 [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:
joshualitt9b989322014-12-15 14:16:27 -080018 GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&)
19 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000020
mtklein36352bf2015-03-25 18:17:31 -070021 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
egdaniel309e3462014-12-09 10:35:58 -080022 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080023
joshualitt9b989322014-12-15 14:16:27 -080024 GrGLGPBuilder* pb = args.fPB;
25 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000026
joshualittabb52a12015-01-13 15:02:10 -080027 // emit attributes
28 vsBuilder->emitAttributes(cte);
29
joshualitt922c8b12015-08-07 09:55:23 -070030 // compute numbers to be hardcoded to convert texture coordinates from int to float
31 SkASSERT(cte.numTextures() == 1);
32 GrTexture* atlas = cte.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070033 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070034 SkScalar recipWidth = 1.0f / atlas->width();
35 SkScalar recipHeight = 1.0f / atlas->height();
36
joshualitt74077b92014-10-24 11:26:03 -070037 GrGLVertToFrag v(kVec2f_GrSLType);
joshualitt9b989322014-12-15 14:16:27 -080038 pb->addVarying("TextureCoords", &v);
joshualitt922c8b12015-08-07 09:55:23 -070039 vsBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
joshualitt7375d6b2015-08-07 13:36:44 -070040 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
41 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
joshualitt922c8b12015-08-07 09:55:23 -070042 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000043
joshualitt9b989322014-12-15 14:16:27 -080044 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070045 if (!cte.colorIgnored()) {
46 if (cte.hasVertexColor()) {
47 pb->addPassThroughAttribute(cte.inColor(), args.fOutputColor);
48 } else {
49 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
50 }
51 }
joshualitt2dd1ae02014-12-03 06:24:10 -080052
joshualittabb52a12015-01-13 15:02:10 -080053 // Setup position
joshualitte578a952015-05-14 10:09:13 -070054 this->setupPosition(pb, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080055
joshualittabb52a12015-01-13 15:02:10 -080056 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -080057 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080058 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
59
egdaniel29bee0f2015-04-29 11:54:42 -070060 GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
joshualitt02b05012015-02-11 06:56:30 -080061 if (cte.maskFormat() == kARGB_GrMaskFormat) {
62 fsBuilder->codeAppendf("%s = ", args.fOutputColor);
63 fsBuilder->appendTextureLookupAndModulate(args.fOutputColor,
64 args.fSamplers[0],
65 v.fsIn(),
66 kVec2f_GrSLType);
67 fsBuilder->codeAppend(";");
68 fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
69 } else {
70 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage);
71 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
72 fsBuilder->codeAppend(";");
73 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000074 }
75
joshualitt9b989322014-12-15 14:16:27 -080076 virtual void setData(const GrGLProgramDataManager& pdman,
77 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -070078 const GrBatchTracker& bt) override {
joshualittb8c241a2015-05-19 08:23:30 -070079 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
80 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
joshualitt9b989322014-12-15 14:16:27 -080081 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -070082 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080083 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070084 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080085 }
joshualitt2dd1ae02014-12-03 06:24:10 -080086 }
87
joshualitte3ababe2015-05-15 07:56:07 -070088 void setTransformData(const GrPrimitiveProcessor& primProc,
89 const GrGLProgramDataManager& pdman,
90 int index,
91 const SkTArray<const GrCoordTransform*, true>& transforms) override {
92 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
93 }
94
joshualitt9b989322014-12-15 14:16:27 -080095 static inline void GenKey(const GrGeometryProcessor& proc,
96 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -070097 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -080098 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -080099 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800100 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700101 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
102 key |= gp.colorIgnored() ? 0x2 : 0x0;
103 key |= gp.maskFormat() << 3;
104 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700105
106 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
107 SkASSERT(gp.numTextures() == 1);
108 GrTexture* atlas = gp.textureAccess(0).getTexture();
109 SkASSERT(atlas);
110 b->add32(atlas->width());
111 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800112 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800113
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000114private:
joshualitt9b989322014-12-15 14:16:27 -0800115 GrColor fColor;
116 UniformHandle fColorUniform;
117
joshualitt249af152014-09-15 11:41:13 -0700118 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000119};
120
121///////////////////////////////////////////////////////////////////////////////
122
joshualitt2e3b3e32014-12-09 13:31:14 -0800123GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800124 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700125 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700126 : fColor(color)
127 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700128 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800129 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700130 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800131 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800132 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800133 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800134
joshualittb8c241a2015-05-19 08:23:30 -0700135 // TODO we could think about removing this attribute if color is ignored, but unfortunately
136 // we don't do text positioning in batch, so we can't quite do that yet.
joshualitt02b05012015-02-11 06:56:30 -0800137 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
138 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800139 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800140 }
joshualitt71c92602015-01-14 08:12:47 -0800141 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800142 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000143 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000144}
145
egdaniel309e3462014-12-09 10:35:58 -0800146void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700147 const GrGLSLCaps& caps,
egdaniel309e3462014-12-09 10:35:58 -0800148 GrProcessorKeyBuilder* b) const {
149 GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000150}
151
joshualittabb52a12015-01-13 15:02:10 -0800152GrGLPrimitiveProcessor*
153GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700154 const GrGLSLCaps& caps) const {
halcanary385fe4d2015-08-26 13:07:48 -0700155 return new GrGLBitmapTextGeoProc(*this, bt);
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
joshualitt0067ff52015-07-08 14:26:19 -0700162GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
163 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}