aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrSimpleTextureEffect.cpp
blob: c2dc360e286a187e61ef4fc0befb903394bb3490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrSimpleTextureEffect.h"
#include "gl/GrGLEffect.h"
#include "gl/GrGLEffectMatrix.h"
#include "gl/GrGLSL.h"
#include "gl/GrGLTexture.h"
#include "GrTBackendEffectFactory.h"
#include "GrTexture.h"

class GrGLSimpleTextureEffect : public GrGLEffect {
public:
    GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
    : INHERITED (factory) {
        GrEffect::CoordsType coordsType =
            drawEffect.castEffect<GrSimpleTextureEffect>().coordsType();
        if (GrEffect::kCustom_CoordsType != coordsType) {
            SkNEW_IN_TLAZY(&fEffectMatrix, GrGLEffectMatrix, (coordsType));
        }
    }

    virtual void emitCode(GrGLShaderBuilder* builder,
                          const GrDrawEffect& drawEffect,
                          EffectKey key,
                          const char* outputColor,
                          const char* inputColor,
                          const TextureSamplerArray& samplers) SK_OVERRIDE {
        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
        const char* fsCoordName;
        GrSLType fsCoordSLType;
        if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
            GrAssert(ste.getMatrix().isIdentity());
            GrAssert(1 == ste.numVertexAttribs());
            fsCoordSLType = kVec2f_GrSLType;
            const char* vsVaryingName;
            builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsCoordName);
            const char* attrName =
                builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
            builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
        } else {
            fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoordName);
        }
        builder->fsCodeAppendf("\t%s = ", outputColor);
        builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
                                                inputColor,
                                                samplers[0],
                                                fsCoordName,
                                                fsCoordSLType);
        builder->fsCodeAppend(";\n");
    }

    static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
        if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
            return 1 << GrGLEffectMatrix::kKeyBits;
        } else {
            return GrGLEffectMatrix::GenKey(ste.getMatrix(),
                                            drawEffect,
                                            ste.coordsType(),
                                            ste.texture(0));
        }
    }

    virtual void setData(const GrGLUniformManager& uman,
                         const GrDrawEffect& drawEffect) SK_OVERRIDE {
        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
        if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
            GrAssert(ste.getMatrix().isIdentity());
        } else {
            fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.texture(0));
        }
    }

private:
    SkTLazy<GrGLEffectMatrix> fEffectMatrix;
    typedef GrGLEffect INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
    this->updateConstantColorComponentsForModulation(color, validFlags);
}

const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
    return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
}

///////////////////////////////////////////////////////////////////////////////

GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);

GrEffectRef* GrSimpleTextureEffect::TestCreate(SkMWCRandom* random,
                                               GrContext*,
                                               const GrDrawTargetCaps&,
                                               GrTexture* textures[]) {
    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
                                      GrEffectUnitTest::kAlphaTextureIdx;
    static const SkShader::TileMode kTileModes[] = {
        SkShader::kClamp_TileMode,
        SkShader::kRepeat_TileMode,
        SkShader::kMirror_TileMode,
    };
    SkShader::TileMode tileModes[] = {
        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
    };
    GrTextureParams params(tileModes, random->nextBool());

    static const CoordsType kCoordsTypes[] = {
        kLocal_CoordsType,
        kPosition_CoordsType,
        kCustom_CoordsType
    };
    CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))];

    if (kCustom_CoordsType == coordsType) {
        return GrSimpleTextureEffect::CreateWithCustomCoords(textures[texIdx], params);
    } else {
        const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
        return GrSimpleTextureEffect::Create(textures[texIdx], matrix);
    }
}