GrBitmapTextGeoProc.cpp revision 018fb62d12d1febf121fe265da5b6117b86a6541
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
8#include "GrBitmapTextGeoProc.h"
9#include "GrInvariantOutput.h"
10#include "GrTexture.h"
11#include "gl/GrGLFragmentProcessor.h"
12#include "gl/GrGLTexture.h"
13#include "gl/GrGLGeometryProcessor.h"
14#include "gl/builders/GrGLProgramBuilder.h"
15#include "glsl/GrGLSLProgramDataManager.h"
16
17class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
18public:
19    GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {}
20
21    void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
22        const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
23
24        GrGLGPBuilder* pb = args.fPB;
25        GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
26
27        // emit attributes
28        vsBuilder->emitAttributes(cte);
29
30        // 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();
33        SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
34        SkScalar recipWidth = 1.0f / atlas->width();
35        SkScalar recipHeight = 1.0f / atlas->height();
36
37        GrGLVertToFrag v(kVec2f_GrSLType);
38        pb->addVarying("TextureCoords", &v);
39        vsBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
40                               GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
41                               GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
42                               cte.inTextureCoords()->fName);
43
44        // Setup pass through color
45        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        }
52
53        // Setup position
54        this->setupPosition(pb, gpArgs, cte.inPosition()->fName);
55
56        // emit transforms
57        this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName,
58                             cte.localMatrix(), args.fTransformsIn, args.fTransformsOut);
59
60        GrGLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
61        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            if (cte.maskFormat() == kA565_GrMaskFormat) {
74                // set alpha to be max of rgb coverage
75                fsBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
76                                       args.fOutputCoverage, args.fOutputCoverage,
77                                       args.fOutputCoverage, args.fOutputCoverage);
78            }
79        }
80    }
81
82    void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
83        const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
84        if (btgp.color() != fColor && !btgp.hasVertexColor()) {
85            float c[4];
86            GrColorToRGBAFloat(btgp.color(), c);
87            pdman.set4fv(fColorUniform, 1, c);
88            fColor = btgp.color();
89        }
90    }
91
92    void setTransformData(const GrPrimitiveProcessor& primProc,
93                          const GrGLSLProgramDataManager& pdman,
94                          int index,
95                          const SkTArray<const GrCoordTransform*, true>& transforms) override {
96        this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
97    }
98
99    static inline void GenKey(const GrGeometryProcessor& proc,
100                              const GrGLSLCaps&,
101                              GrProcessorKeyBuilder* b) {
102        const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
103        uint32_t key = 0;
104        key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
105        key |= gp.colorIgnored() ? 0x2 : 0x0;
106        key |= gp.maskFormat() << 3;
107        b->add32(key);
108
109        // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
110        SkASSERT(gp.numTextures() == 1);
111        GrTexture* atlas = gp.textureAccess(0).getTexture();
112        SkASSERT(atlas);
113        b->add32(atlas->width());
114        b->add32(atlas->height());
115    }
116
117private:
118    GrColor fColor;
119    UniformHandle fColorUniform;
120
121    typedef GrGLGeometryProcessor INHERITED;
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
126GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
127                                         const GrTextureParams& params, GrMaskFormat format,
128                                         const SkMatrix& localMatrix, bool usesLocalCoords)
129    : fColor(color)
130    , fLocalMatrix(localMatrix)
131    , fUsesLocalCoords(usesLocalCoords)
132    , fTextureAccess(texture, params)
133    , fInColor(nullptr)
134    , fMaskFormat(format) {
135    this->initClassID<GrBitmapTextGeoProc>();
136    fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
137
138    // TODO we could think about removing this attribute if color is ignored, but unfortunately
139    // we don't do text positioning in batch, so we can't quite do that yet.
140    bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
141    if (hasVertexColor) {
142        fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
143    }
144    fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
145                                                        kVec2s_GrVertexAttribType));
146    this->addTextureAccess(&fTextureAccess);
147}
148
149void GrBitmapTextGeoProc::getGLProcessorKey(const GrGLSLCaps& caps,GrProcessorKeyBuilder* b) const {
150    GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
151}
152
153GrGLPrimitiveProcessor* GrBitmapTextGeoProc::createGLInstance(const GrGLSLCaps& caps) const {
154    return new GrGLBitmapTextGeoProc();
155}
156
157///////////////////////////////////////////////////////////////////////////////
158
159GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
160
161const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
162    int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
163                                          GrProcessorUnitTest::kAlphaTextureIdx;
164    static const SkShader::TileMode kTileModes[] = {
165        SkShader::kClamp_TileMode,
166        SkShader::kRepeat_TileMode,
167        SkShader::kMirror_TileMode,
168    };
169    SkShader::TileMode tileModes[] = {
170        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
171        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
172    };
173    GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
174                                                           GrTextureParams::kNone_FilterMode);
175
176    GrMaskFormat format;
177    switch (d->fRandom->nextULessThan(3)) {
178        case 0:
179            format = kA8_GrMaskFormat;
180            break;
181        case 1:
182            format = kA565_GrMaskFormat;
183            break;
184        case 2:
185            format = kARGB_GrMaskFormat;
186            break;
187    }
188
189    return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
190                                       format, GrTest::TestMatrix(d->fRandom),
191                                       d->fRandom->nextBool());
192}
193