aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrSimpleTextureEffect.h
blob: f80ff8da8661fcf2a03a97404a7bea1e6fbb2632 (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrSimpleTextureEffect_DEFINED
#define GrSimpleTextureEffect_DEFINED

#include "GrSingleTextureEffect.h"

class GrGLSimpleTextureEffect;

/**
 * The output color of this effect is a modulation of the input color and a sample from a texture.
 * It allows explicit specification of the filtering and wrap modes (GrTextureParams). It can use
 * local coords, positions, or a custom vertex attribute as input texture coords. The input coords
 * can have a matrix applied in the VS in both the local and position cases but not with a custom
 * attribute coords at this time. It will add a varying to input interpolate texture coords to the
 * FS.
 */
class GrSimpleTextureEffect : public GrSingleTextureEffect {
public:
    /* unfiltered, clamp mode */
    static GrEffectRef* Create(GrTexture* tex,
                               const SkMatrix& matrix,
                               CoordsType coordsType = kLocal_CoordsType) {
        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, GrTextureParams::kNone_FilterMode, coordsType)));
        return CreateEffectRef(effect);
    }

    /* clamp mode */
    static GrEffectRef* Create(GrTexture* tex,
                               const SkMatrix& matrix,
                               GrTextureParams::FilterMode filterMode,
                               CoordsType coordsType = kLocal_CoordsType) {
        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
        AutoEffectUnref effect(
            SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, filterMode, coordsType)));
        return CreateEffectRef(effect);
    }

    static GrEffectRef* Create(GrTexture* tex,
                               const SkMatrix& matrix,
                               const GrTextureParams& p,
                               CoordsType coordsType = kLocal_CoordsType) {
        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, p, coordsType)));
        return CreateEffectRef(effect);
    }

    /** Variant that requires the client to install a custom kVec2 vertex attribute that will be
        the source of the coords. No matrix is allowed in this mode. */
    static GrEffectRef* CreateWithCustomCoords(GrTexture* tex, const GrTextureParams& p) {
        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex,
                                                                  SkMatrix::I(),
                                                                  p,
                                                                  kCustom_CoordsType)));
        return CreateEffectRef(effect);
    }

    virtual ~GrSimpleTextureEffect() {}

    static const char* Name() { return "Texture"; }

    virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;

    typedef GrGLSimpleTextureEffect GLEffect;

    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;

private:
    GrSimpleTextureEffect(GrTexture* texture,
                          const SkMatrix& matrix,
                          GrTextureParams::FilterMode filterMode,
                          CoordsType coordsType)
        : GrSingleTextureEffect(texture, matrix, filterMode, coordsType) {
        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
    }

    GrSimpleTextureEffect(GrTexture* texture,
                          const SkMatrix& matrix,
                          const GrTextureParams& params,
                          CoordsType coordsType)
        : GrSingleTextureEffect(texture, matrix, params, coordsType) {
        if (kCustom_CoordsType == coordsType) {
            GrAssert(matrix.isIdentity());
            this->addVertexAttrib(kVec2f_GrSLType);
        }
    }

    virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
        const GrSimpleTextureEffect& ste = CastEffect<GrSimpleTextureEffect>(other);
        return this->hasSameTextureParamsMatrixAndCoordsType(ste);
    }

    GR_DECLARE_EFFECT_TEST;

    typedef GrSingleTextureEffect INHERITED;
};

#endif