GrBitmapTextGeoProc.cpp revision a511e6ad1042093bea5b015c22a920313fc57c0b
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
16class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor {
17public:
18    GrGLBitmapTextGeoProc(const GrGeometryProcessor&, const GrBatchTracker&)
19        : 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        }
74    }
75
76    virtual void setData(const GrGLProgramDataManager& pdman,
77                         const GrPrimitiveProcessor& gp,
78                         const GrBatchTracker& bt) override {
79        const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
80        if (btgp.color() != fColor && !btgp.hasVertexColor()) {
81            GrGLfloat c[4];
82            GrColorToRGBAFloat(btgp.color(), c);
83            pdman.set4fv(fColorUniform, 1, c);
84            fColor = btgp.color();
85        }
86    }
87
88    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
95    static inline void GenKey(const GrGeometryProcessor& proc,
96                              const GrBatchTracker& bt,
97                              const GrGLSLCaps&,
98                              GrProcessorKeyBuilder* b) {
99        const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
100        uint32_t key = 0;
101        key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
102        key |= gp.colorIgnored() ? 0x2 : 0x0;
103        key |= gp.maskFormat() << 3;
104        b->add32(key);
105
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());
112    }
113
114private:
115    GrColor fColor;
116    UniformHandle fColorUniform;
117
118    typedef GrGLGeometryProcessor INHERITED;
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
123GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
124                                         const GrTextureParams& params, GrMaskFormat format,
125                                         const SkMatrix& localMatrix, bool usesLocalCoords)
126    : fColor(color)
127    , fLocalMatrix(localMatrix)
128    , fUsesLocalCoords(usesLocalCoords)
129    , fTextureAccess(texture, params)
130    , fInColor(nullptr)
131    , fMaskFormat(format) {
132    this->initClassID<GrBitmapTextGeoProc>();
133    fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
134
135    // 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.
137    bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat;
138    if (hasVertexColor) {
139        fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
140    }
141    fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
142                                                        kVec2s_GrVertexAttribType));
143    this->addTextureAccess(&fTextureAccess);
144}
145
146void GrBitmapTextGeoProc::getGLProcessorKey(const GrBatchTracker& bt,
147                                            const GrGLSLCaps& caps,
148                                            GrProcessorKeyBuilder* b) const {
149    GrGLBitmapTextGeoProc::GenKey(*this, bt, caps, b);
150}
151
152GrGLPrimitiveProcessor*
153GrBitmapTextGeoProc::createGLInstance(const GrBatchTracker& bt,
154                                      const GrGLSLCaps& caps) const {
155    return new GrGLBitmapTextGeoProc(*this, bt);
156}
157
158///////////////////////////////////////////////////////////////////////////////
159
160GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
161
162GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
163    int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
164                                          GrProcessorUnitTest::kAlphaTextureIdx;
165    static const SkShader::TileMode kTileModes[] = {
166        SkShader::kClamp_TileMode,
167        SkShader::kRepeat_TileMode,
168        SkShader::kMirror_TileMode,
169    };
170    SkShader::TileMode tileModes[] = {
171        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
172        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
173    };
174    GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
175                                                           GrTextureParams::kNone_FilterMode);
176
177    GrMaskFormat format;
178    switch (d->fRandom->nextULessThan(3)) {
179        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
190    return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
191                                       format, GrTest::TestMatrix(d->fRandom),
192                                       d->fRandom->nextBool());
193}
194