GrSimpleTextureEffect.h revision b86add1ad37776818e1f730359ec587c9fdbff5f
168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com/*
268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * Copyright 2013 Google Inc.
368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com *
468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * found in the LICENSE file.
668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com */
768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#ifndef GrSimpleTextureEffect_DEFINED
968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#define GrSimpleTextureEffect_DEFINED
1068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrSingleTextureEffect.h"
1268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comclass GrGLSimpleTextureEffect;
1468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com/**
1668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * The output color of this effect is a modulation of the input color and a sample from a texture.
17c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com * It allows explicit specification of the filtering and wrap modes (GrTextureParams). It can use
18c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com * local coords, positions, or a custom vertex attribute as input texture coords. The input coords
19c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com * can have a matrix applied in the VS in both the local and position cases but not with a custom
20c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com * attribute coords at this time. It will add a varying to input interpolate texture coords to the
21c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com * FS.
2268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com */
2368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comclass GrSimpleTextureEffect : public GrSingleTextureEffect {
2468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.compublic:
2568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    /* unfiltered, clamp mode */
26c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static GrEffectRef* Create(GrTexture* tex,
27c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               const SkMatrix& matrix,
28c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               CoordsType coordsType = kLocal_CoordsType) {
29c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
30b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, GrTextureParams::kNone_FilterMode, coordsType)));
3168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        return CreateEffectRef(effect);
3268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
3368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
3468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    /* clamp mode */
35c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static GrEffectRef* Create(GrTexture* tex,
36c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               const SkMatrix& matrix,
37b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                               GrTextureParams::FilterMode filterMode,
38c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               CoordsType coordsType = kLocal_CoordsType) {
39c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
40c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        AutoEffectUnref effect(
41b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com            SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, filterMode, coordsType)));
4268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        return CreateEffectRef(effect);
4368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
4468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
45c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static GrEffectRef* Create(GrTexture* tex,
46c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               const SkMatrix& matrix,
47c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               const GrTextureParams& p,
48c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                               CoordsType coordsType = kLocal_CoordsType) {
49c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
50c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, p, coordsType)));
51c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        return CreateEffectRef(effect);
52c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    }
53c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
54c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    /** Variant that requires the client to install a custom kVec2 vertex attribute that will be
55c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        the source of the coords. No matrix is allowed in this mode. */
56c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static GrEffectRef* CreateWithCustomCoords(GrTexture* tex, const GrTextureParams& p) {
57c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex,
58c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                                                  SkMatrix::I(),
59c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                                                  p,
60c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                                                  kCustom_CoordsType)));
6168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        return CreateEffectRef(effect);
6268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
6368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    virtual ~GrSimpleTextureEffect() {}
6568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    static const char* Name() { return "Texture"; }
6768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
6968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
7068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    typedef GrGLSimpleTextureEffect GLEffect;
7168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
7268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
7368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
7468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comprivate:
75c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    GrSimpleTextureEffect(GrTexture* texture,
76c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const SkMatrix& matrix,
77b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                          GrTextureParams::FilterMode filterMode,
78c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          CoordsType coordsType)
79b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com        : GrSingleTextureEffect(texture, matrix, filterMode, coordsType) {
80c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType);
81c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    }
82c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
83c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    GrSimpleTextureEffect(GrTexture* texture,
84c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const SkMatrix& matrix,
85c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const GrTextureParams& params,
86c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          CoordsType coordsType)
87c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        : GrSingleTextureEffect(texture, matrix, params, coordsType) {
88c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        if (kCustom_CoordsType == coordsType) {
89c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com            GrAssert(matrix.isIdentity());
90c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com            this->addVertexAttrib(kVec2f_GrSLType);
91c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        }
92c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    }
9368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
948a252f79629b189a03de22cd8ff0312c5bccedd1bsalomon@google.com    virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
956340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com        const GrSimpleTextureEffect& ste = CastEffect<GrSimpleTextureEffect>(other);
96c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        return this->hasSameTextureParamsMatrixAndCoordsType(ste);
9768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
9868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
9968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    GR_DECLARE_EFFECT_TEST;
10068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
10168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    typedef GrSingleTextureEffect INHERITED;
10268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com};
10368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
10468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#endif
105