blob: b03e6e6dc97d71bb5e84ccb7ac071779766c4772 [file] [log] [blame]
bsalomon@google.com047696c2012-09-11 13:29:29 +00001/*
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
bsalomon@google.com6d003d12012-09-11 15:45:20 +000011#include "GrNoncopyable.h"
12#include "SkRefCnt.h"
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000013#include "SkShader.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000014
15class GrTexture;
bsalomon@google.com047696c2012-09-11 13:29:29 +000016
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000017/**
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 */
23class GrTextureParams {
24public:
25 GrTextureParams() {
26 this->reset();
27 }
28
29 GrTextureParams(SkShader::TileMode tileXAndY, bool bilerp) {
30 this->reset(tileXAndY, bilerp);
31 }
32
33 GrTextureParams(SkShader::TileMode tileModes[2], bool bilerp) {
34 this->reset(tileModes, bilerp);
35 }
36
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
48 void reset() {
49 this->reset(SkShader::kClamp_TileMode, false);
50 }
51
52 void reset(SkShader::TileMode tileXAndY, bool bilerp) {
53 fTileModes[0] = fTileModes[1] = tileXAndY;
54 fBilerp = bilerp;
55 }
56
57 void reset(SkShader::TileMode tileModes[2], bool bilerp) {
58 fTileModes[0] = tileModes[0];
59 fTileModes[1] = tileModes[1];
60 fBilerp = bilerp;
61 }
62
63 void setClampNoFilter() {
64 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
65 fBilerp = false;
66 }
67
68 void setClamp() {
69 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
70 }
71
72 void setBilerp(bool bilerp) { fBilerp = bilerp; }
73
74 void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
75 void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
76 void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
77
78 SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
79
80 SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
81
82 bool isTiled() const {
83 return SkShader::kClamp_TileMode != fTileModes[0] ||
84 SkShader::kClamp_TileMode != fTileModes[1];
85 }
86
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
97private:
98
99 SkShader::TileMode fTileModes[2];
100 bool fBilerp;
101};
102
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000103/** 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
bsalomon@google.coma469c282012-10-24 18:28:34 +0000106 * key. However, if a GrEffect uses different swizzles based on its input then it must
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000107 * consider that variation in its key-generation.
bsalomon@google.com047696c2012-09-11 13:29:29 +0000108 */
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000109class GrTextureAccess : GrNoncopyable {
bsalomon@google.com047696c2012-09-11 13:29:29 +0000110public:
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000111 /**
bsalomon@google.coma469c282012-10-24 18:28:34 +0000112 * A default GrTextureAccess must have reset() called on it in a GrEffect subclass's
113 * constructor if it will be accessible via GrEffect::textureAccess().
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000114 */
115 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +0000116
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000117 /**
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000118 * 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 /**
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000126 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
127 * 'g', 'b', and/or 'a'.
128 */
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000129 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);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000134
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000135 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);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000144
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000145 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.
bsalomon@google.comdb545ae2012-09-20 15:37:30 +0000148 GrAssert(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000149 strcmp(fSwizzle, other.fSwizzle));
150#endif
151 return fParams == other.fParams &&
bsalomon@google.comdb545ae2012-09-20 15:37:30 +0000152 (fTexture.get() == other.fTexture.get()) &&
153 (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000154 }
155
156 bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000157
158 GrTexture* getTexture() const { return fTexture.get(); }
159
160 /**
161 * Returns a string representing the swizzle. The string is is null-terminated.
162 */
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. */
175 uint32_t swizzleMask() const { return fSwizzleMask; }
bsalomon@google.com047696c2012-09-11 13:29:29 +0000176
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000177 const GrTextureParams& getParams() const { return fParams; }
178
bsalomon@google.com047696c2012-09-11 13:29:29 +0000179private:
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000180 void setSwizzle(const char*);
181
182 GrTextureParams fParams;
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000183 SkAutoTUnref<GrTexture> fTexture;
184 uint32_t fSwizzleMask;
185 char fSwizzle[5];
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000186
187 typedef GrNoncopyable INHERITED;
bsalomon@google.com047696c2012-09-11 13:29:29 +0000188};
189
190#endif