bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 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 | #ifndef GrSimpleTextureEffect_DEFINED |
| 9 | #define GrSimpleTextureEffect_DEFINED |
| 10 | |
| 11 | #include "GrSingleTextureEffect.h" |
| 12 | |
| 13 | class GrGLSimpleTextureEffect; |
| 14 | |
| 15 | /** |
| 16 | * The output color of this effect is a modulation of the input color and a sample from a texture. |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 17 | * It allows explicit specification of the filtering and wrap modes (GrTextureParams). It can use |
| 18 | * local coords, positions, or a custom vertex attribute as input texture coords. The input coords |
| 19 | * can have a matrix applied in the VS in both the local and position cases but not with a custom |
| 20 | * attribute coords at this time. It will add a varying to input interpolate texture coords to the |
| 21 | * FS. |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 22 | */ |
| 23 | class GrSimpleTextureEffect : public GrSingleTextureEffect { |
| 24 | public: |
| 25 | /* unfiltered, clamp mode */ |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 26 | static GrEffectRef* Create(GrTexture* tex, |
| 27 | const SkMatrix& matrix, |
| 28 | CoordsType coordsType = kLocal_CoordsType) { |
| 29 | GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType); |
| 30 | AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, false, coordsType))); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 31 | return CreateEffectRef(effect); |
| 32 | } |
| 33 | |
| 34 | /* clamp mode */ |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 35 | static GrEffectRef* Create(GrTexture* tex, |
| 36 | const SkMatrix& matrix, |
| 37 | bool bilerp, |
| 38 | CoordsType coordsType = kLocal_CoordsType) { |
| 39 | GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType); |
| 40 | AutoEffectUnref effect( |
| 41 | SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, bilerp, coordsType))); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 42 | return CreateEffectRef(effect); |
| 43 | } |
| 44 | |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 45 | static GrEffectRef* Create(GrTexture* tex, |
| 46 | const SkMatrix& matrix, |
| 47 | const GrTextureParams& p, |
| 48 | CoordsType coordsType = kLocal_CoordsType) { |
| 49 | GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType); |
| 50 | AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, matrix, p, coordsType))); |
| 51 | return CreateEffectRef(effect); |
| 52 | } |
| 53 | |
| 54 | /** Variant that requires the client to install a custom kVec2 vertex attribute that will be |
| 55 | the source of the coords. No matrix is allowed in this mode. */ |
| 56 | static GrEffectRef* CreateWithCustomCoords(GrTexture* tex, const GrTextureParams& p) { |
| 57 | AutoEffectUnref effect(SkNEW_ARGS(GrSimpleTextureEffect, (tex, |
| 58 | SkMatrix::I(), |
| 59 | p, |
| 60 | kCustom_CoordsType))); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 61 | return CreateEffectRef(effect); |
| 62 | } |
| 63 | |
| 64 | virtual ~GrSimpleTextureEffect() {} |
| 65 | |
| 66 | static const char* Name() { return "Texture"; } |
| 67 | |
| 68 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 69 | |
| 70 | typedef GrGLSimpleTextureEffect GLEffect; |
| 71 | |
| 72 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 73 | |
| 74 | private: |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 75 | GrSimpleTextureEffect(GrTexture* texture, |
| 76 | const SkMatrix& matrix, |
| 77 | bool bilerp, |
| 78 | CoordsType coordsType) |
| 79 | : GrSingleTextureEffect(texture, matrix, bilerp, coordsType) { |
| 80 | GrAssert(kLocal_CoordsType == coordsType || kPosition_CoordsType == coordsType); |
| 81 | } |
| 82 | |
| 83 | GrSimpleTextureEffect(GrTexture* texture, |
| 84 | const SkMatrix& matrix, |
| 85 | const GrTextureParams& params, |
| 86 | CoordsType coordsType) |
| 87 | : GrSingleTextureEffect(texture, matrix, params, coordsType) { |
| 88 | if (kCustom_CoordsType == coordsType) { |
| 89 | GrAssert(matrix.isIdentity()); |
| 90 | this->addVertexAttrib(kVec2f_GrSLType); |
| 91 | } |
| 92 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 94 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 95 | const GrSimpleTextureEffect& ste = CastEffect<GrSimpleTextureEffect>(other); |
bsalomon@google.com | ae81d5c | 2013-03-20 17:32:27 +0000 | [diff] [blame] | 96 | return this->hasSameTextureParamsMatrixAndCoordsType(ste); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | GR_DECLARE_EFFECT_TEST; |
| 100 | |
| 101 | typedef GrSingleTextureEffect INHERITED; |
| 102 | }; |
| 103 | |
| 104 | #endif |