• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrTextureAccess_DEFINED
9 #define GrTextureAccess_DEFINED
10 
11 #include "GrNoncopyable.h"
12 #include "SkRefCnt.h"
13 #include "SkShader.h"
14 
15 class GrTexture;
16 
17 /**
18  * Represents the filtering and tile modes used to access a texture. It is mostly used with
19  * GrTextureAccess (defined below). Also, some of the texture cache methods require knowledge about
20  * filtering and tiling to perform a cache lookup. If it wasn't for this latter usage this would
21  * be folded into GrTextureAccess.
22  */
23 class GrTextureParams {
24 public:
GrTextureParams()25     GrTextureParams() {
26         this->reset();
27     }
28 
GrTextureParams(SkShader::TileMode tileXAndY,bool bilerp)29     GrTextureParams(SkShader::TileMode tileXAndY, bool bilerp) {
30         this->reset(tileXAndY, bilerp);
31     }
32 
GrTextureParams(SkShader::TileMode tileModes[2],bool bilerp)33     GrTextureParams(SkShader::TileMode tileModes[2], bool bilerp) {
34         this->reset(tileModes, bilerp);
35     }
36 
GrTextureParams(const GrTextureParams & params)37     GrTextureParams(const GrTextureParams& params) {
38         *this = params;
39     }
40 
41     GrTextureParams& operator= (const GrTextureParams& params) {
42         fTileModes[0] = params.fTileModes[0];
43         fTileModes[1] = params.fTileModes[1];
44         fBilerp = params.fBilerp;
45         return *this;
46     }
47 
reset()48     void reset() {
49         this->reset(SkShader::kClamp_TileMode, false);
50     }
51 
reset(SkShader::TileMode tileXAndY,bool bilerp)52     void reset(SkShader::TileMode tileXAndY, bool bilerp) {
53         fTileModes[0] = fTileModes[1] = tileXAndY;
54         fBilerp = bilerp;
55     }
56 
reset(SkShader::TileMode tileModes[2],bool bilerp)57     void reset(SkShader::TileMode tileModes[2], bool bilerp) {
58         fTileModes[0] = tileModes[0];
59         fTileModes[1] = tileModes[1];
60         fBilerp = bilerp;
61     }
62 
setClampNoFilter()63     void setClampNoFilter() {
64         fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
65         fBilerp = false;
66     }
67 
setClamp()68     void setClamp() {
69         fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
70     }
71 
setBilerp(bool bilerp)72     void setBilerp(bool bilerp) { fBilerp = bilerp; }
73 
setTileModeX(const SkShader::TileMode tm)74     void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
setTileModeY(const SkShader::TileMode tm)75     void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
setTileModeXAndY(const SkShader::TileMode tm)76     void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
77 
getTileModeX()78     SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
79 
getTileModeY()80     SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
81 
isTiled()82     bool isTiled() const {
83         return SkShader::kClamp_TileMode != fTileModes[0] ||
84                SkShader::kClamp_TileMode != fTileModes[1];
85     }
86 
isBilerp()87     bool isBilerp() const { return fBilerp; }
88 
89     bool operator== (const GrTextureParams& other) const {
90         return fTileModes[0] == other.fTileModes[0] &&
91                fTileModes[1] == other.fTileModes[1] &&
92                fBilerp == other.fBilerp;
93     }
94 
95     bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
96 
97 private:
98 
99     SkShader::TileMode fTileModes[2];
100     bool               fBilerp;
101 };
102 
103 /** A class representing the swizzle access pattern for a texture. Note that if the texture is
104  *  an alpha-only texture then the alpha channel is substituted for other components. Any mangling
105  *  to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
106  *  key. However, if a GrEffect uses different swizzles based on its input then it must
107  *  consider that variation in its key-generation.
108  */
109 class GrTextureAccess : GrNoncopyable {
110 public:
111     /**
112      * A default GrTextureAccess must have reset() called on it in a GrEffect subclass's
113      * constructor if it will be accessible via GrEffect::textureAccess().
114      */
115     GrTextureAccess();
116 
117     /**
118      * Uses the default swizzle, "rgba".
119      */
120     GrTextureAccess(GrTexture*, const GrTextureParams&);
121     explicit GrTextureAccess(GrTexture*,
122                              bool bilerp = false,
123                              SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
124 
125     /**
126      * swizzle must be a string between one and four (inclusive) characters containing only 'r',
127      * 'g', 'b',  and/or 'a'.
128      */
129     GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
130     GrTextureAccess(GrTexture*,
131                     const char* swizzle,
132                     bool bilerp = false,
133                     SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
134 
135     void reset(GrTexture*, const GrTextureParams&);
136     void reset(GrTexture*,
137                bool bilerp = false,
138                SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
139     void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
140     void reset(GrTexture*,
141                const char* swizzle,
142                bool bilerp = false,
143                SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
144 
145     bool operator== (const GrTextureAccess& other) const {
146 #if GR_DEBUG
147         // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
148         GrAssert(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
149                  strcmp(fSwizzle, other.fSwizzle));
150 #endif
151         return fParams == other.fParams &&
152                (fTexture.get() == other.fTexture.get()) &&
153                (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
154     }
155 
156     bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
157 
getTexture()158     GrTexture* getTexture() const { return fTexture.get(); }
159 
160     /**
161      * Returns a string representing the swizzle. The string is is null-terminated.
162      */
getSwizzle()163     const char* getSwizzle() const { return fSwizzle; }
164 
165     enum {
166         kR_SwizzleFlag = 0x1,
167         kG_SwizzleFlag = 0x2,
168         kB_SwizzleFlag = 0x4,
169         kA_SwizzleFlag = 0x8,
170 
171         kRGB_SwizzleMask = (kR_SwizzleFlag |  kG_SwizzleFlag | kB_SwizzleFlag),
172     };
173 
174     /** Returns a mask indicating which components are referenced in the swizzle. */
swizzleMask()175     uint32_t swizzleMask() const { return fSwizzleMask; }
176 
getParams()177     const GrTextureParams& getParams() const { return fParams; }
178 
179 private:
180     void setSwizzle(const char*);
181 
182     GrTextureParams         fParams;
183     SkAutoTUnref<GrTexture> fTexture;
184     uint32_t                fSwizzleMask;
185     char                    fSwizzle[5];
186 
187     typedef GrNoncopyable INHERITED;
188 };
189 
190 #endif
191