GrSingleTextureEffect.h revision c78188896e28a4ae49e406a7422b345ae177dafe
1/*
2 * Copyright 2012 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 GrSingleTextureEffect_DEFINED
9#define GrSingleTextureEffect_DEFINED
10
11#include "GrEffect.h"
12#include "SkMatrix.h"
13
14class GrTexture;
15
16/**
17 * A base class for effects that draw a single texture with a texture matrix. This effect has no
18 * backend implementations. One must be provided by the subclass.
19 */
20class GrSingleTextureEffect : public GrEffect {
21public:
22    virtual ~GrSingleTextureEffect();
23
24    const SkMatrix& getMatrix() const { return fMatrix; }
25
26    /** Indicates whether the matrix operates on local coords or positions */
27    CoordsType coordsType() const { return fCoordsType; }
28
29protected:
30    /** unfiltered, clamp mode */
31    GrSingleTextureEffect(GrTexture*, const SkMatrix&, CoordsType = kLocal_CoordsType);
32    /** clamp mode */
33    GrSingleTextureEffect(GrTexture*, const SkMatrix&, bool bilerp, CoordsType = kLocal_CoordsType);
34    GrSingleTextureEffect(GrTexture*,
35                          const SkMatrix&,
36                          const GrTextureParams&,
37                          CoordsType = kLocal_CoordsType);
38
39    /**
40     * Helper for subclass onIsEqual() functions.
41     */
42    bool hasSameTextureParamsMatrixAndCoordsType(const GrSingleTextureEffect& other) const {
43        const GrTextureAccess& otherAccess = other.fTextureAccess;
44        // We don't have to check the accesses' swizzles because they are inferred from the texture.
45        return fTextureAccess.getTexture() == otherAccess.getTexture() &&
46               fTextureAccess.getParams() == otherAccess.getParams() &&
47               this->getMatrix().cheapEqualTo(other.getMatrix()) &&
48               fCoordsType == other.fCoordsType;
49    }
50
51    /**
52     * Can be used as a helper to implement subclass getConstantColorComponents(). It assumes that
53     * the subclass output color will be a modulation of the input color with a value read from the
54     * texture.
55     */
56    void updateConstantColorComponentsForModulation(GrColor* color, uint32_t* validFlags) const {
57        if ((*validFlags & kA_ValidComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
58            GrPixelConfigIsOpaque(this->texture(0)->config())) {
59            *validFlags = kA_ValidComponentFlag;
60        } else {
61            *validFlags = 0;
62        }
63    }
64
65private:
66    GrTextureAccess fTextureAccess;
67    SkMatrix        fMatrix;
68    CoordsType      fCoordsType;
69
70    typedef GrEffect INHERITED;
71};
72
73#endif
74