GrBitmapTextGeoProc.cpp revision ad70c41f678771f889449c296a8ab48640742050
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
10#include "GrAtlasedShaderHelpers.h"
11#include "GrTexture.h"
12#include "glsl/GrGLSLFragmentShaderBuilder.h"
13#include "glsl/GrGLSLGeometryProcessor.h"
14#include "glsl/GrGLSLProgramDataManager.h"
15#include "glsl/GrGLSLUniformHandler.h"
16#include "glsl/GrGLSLVarying.h"
17#include "glsl/GrGLSLVertexShaderBuilder.h"
18
19class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
20public:
21    GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL), fAtlasSize({0,0}) {}
22
23    void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
24        const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>();
25
26        GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
27        GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
28        GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
29
30        // emit attributes
31        varyingHandler->emitAttributes(btgp);
32
33        const char* atlasSizeInvName;
34        fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
35                                                          kVec2f_GrSLType,
36                                                          kHigh_GrSLPrecision,
37                                                          "AtlasSizeInv",
38                                                          &atlasSizeInvName);
39
40        GrGLSLVertToFrag uv(kVec2f_GrSLType);
41        GrGLSLVertToFrag texIdx(kFloat_GrSLType);
42        append_index_uv_varyings(args, btgp.inTextureCoords()->fName, atlasSizeInvName,
43                                 &uv, &texIdx, nullptr);
44
45        GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
46        // Setup pass through color
47        if (btgp.hasVertexColor()) {
48            varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
49        } else {
50            this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
51                                    &fColorUniform);
52        }
53
54        // Setup position
55        this->writeOutputPosition(vertBuilder, gpArgs, btgp.inPosition()->fName);
56
57        // emit transforms
58        this->emitTransforms(vertBuilder,
59                             varyingHandler,
60                             uniformHandler,
61                             gpArgs->fPositionVar,
62                             btgp.inPosition()->fName,
63                             btgp.localMatrix(),
64                             args.fFPCoordTransformHandler);
65
66        fragBuilder->codeAppend("float4 texColor;");
67        append_multitexture_lookup(args, btgp.numTextureSamplers(),
68                                   texIdx, uv.fsIn(), "texColor");
69
70        if (btgp.maskFormat() == kARGB_GrMaskFormat) {
71            // modulate by color
72            fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
73            fragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
74        } else {
75            fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
76        }
77    }
78
79    void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
80                 FPCoordTransformIter&& transformIter) override {
81        const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
82        if (btgp.color() != fColor && !btgp.hasVertexColor()) {
83            float c[4];
84            GrColorToRGBAFloat(btgp.color(), c);
85            pdman.set4fv(fColorUniform, 1, c);
86            fColor = btgp.color();
87        }
88
89        SkASSERT(btgp.numTextureSamplers() >= 1);
90        GrTexture* atlas = btgp.textureSampler(0).peekTexture();
91        SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
92
93        if (fAtlasSize.fWidth != atlas->width() || fAtlasSize.fHeight != atlas->height()) {
94            pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlas->width(), 1.0f / atlas->height());
95            fAtlasSize.set(atlas->width(), atlas->height());
96        }
97        this->setTransformDataHelper(btgp.localMatrix(), pdman, &transformIter);
98    }
99
100    static inline void GenKey(const GrGeometryProcessor& proc,
101                              const GrShaderCaps&,
102                              GrProcessorKeyBuilder* b) {
103        const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
104        uint32_t key = 0;
105        key |= (btgp.usesLocalCoords() && btgp.localMatrix().hasPerspective()) ? 0x1 : 0x0;
106        key |= btgp.maskFormat() << 1;
107        b->add32(key);
108        b->add32(btgp.numTextureSamplers());
109    }
110
111private:
112    GrColor       fColor;
113    UniformHandle fColorUniform;
114
115    SkISize       fAtlasSize;
116    UniformHandle fAtlasSizeInvUniform;
117
118    typedef GrGLSLGeometryProcessor INHERITED;
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
123GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color,
124                                         const sk_sp<GrTextureProxy> proxies[kMaxTextures],
125                                         const GrSamplerState& params, GrMaskFormat format,
126                                         const SkMatrix& localMatrix, bool usesLocalCoords)
127        : fColor(color)
128        , fLocalMatrix(localMatrix)
129        , fUsesLocalCoords(usesLocalCoords)
130        , fInColor(nullptr)
131        , fMaskFormat(format) {
132    this->initClassID<GrBitmapTextGeoProc>();
133    fInPosition =
134            &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision);
135
136    bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
137                          kA565_GrMaskFormat == fMaskFormat;
138    if (hasVertexColor) {
139        fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
140    }
141
142    fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_uint_GrVertexAttribType,
143                                              kHigh_GrSLPrecision);
144    for (int i = 0; i < kMaxTextures; ++i) {
145        if (proxies[i]) {
146            fTextureSamplers[i].reset(std::move(proxies[i]), params);
147            this->addTextureSampler(&fTextureSamplers[i]);
148        }
149    }
150}
151
152void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
153                                              GrProcessorKeyBuilder* b) const {
154    GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
155}
156
157GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
158    return new GrGLBitmapTextGeoProc();
159}
160
161///////////////////////////////////////////////////////////////////////////////
162
163GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
164
165#if GR_TEST_UTILS
166
167sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
168    int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
169                                        : GrProcessorUnitTest::kAlphaTextureIdx;
170    sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
171        d->textureProxy(texIdx),
172        nullptr,
173        nullptr,
174        nullptr
175    };
176
177    GrSamplerState::WrapMode wrapModes[2];
178    GrTest::TestWrapModes(d->fRandom, wrapModes);
179    GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
180                                                   ? GrSamplerState::Filter::kBilerp
181                                                   : GrSamplerState::Filter::kNearest);
182
183    GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
184    switch (d->fRandom->nextULessThan(3)) {
185        case 0:
186            format = kA8_GrMaskFormat;
187            break;
188        case 1:
189            format = kA565_GrMaskFormat;
190            break;
191        case 2:
192            format = kARGB_GrMaskFormat;
193            break;
194    }
195
196    return GrBitmapTextGeoProc::Make(GrRandomColor(d->fRandom), proxies, samplerState,
197                                     format, GrTest::TestMatrix(d->fRandom),
198                                     d->fRandom->nextBool());
199}
200#endif
201