bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 11 | #include "GrNoncopyable.h" |
| 12 | #include "SkRefCnt.h" |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 14 | |
| 15 | class GrTexture; |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 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: |
| 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 | |
| 97 | private: |
| 98 | |
| 99 | SkShader::TileMode fTileModes[2]; |
| 100 | bool fBilerp; |
| 101 | }; |
| 102 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 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 |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 106 | * key. However, if a GrEffect uses different swizzles based on its input then it must |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 107 | * consider that variation in its key-generation. |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 108 | */ |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 109 | class GrTextureAccess : GrNoncopyable { |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 110 | public: |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 111 | /** |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 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(). |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 114 | */ |
| 115 | GrTextureAccess(); |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 117 | /** |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 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 | /** |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 126 | * swizzle must be a string between one and four (inclusive) characters containing only 'r', |
| 127 | * 'g', 'b', and/or 'a'. |
| 128 | */ |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 134 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 144 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | db545ae | 2012-09-20 15:37:30 +0000 | [diff] [blame] | 148 | GrAssert(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) == |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 149 | strcmp(fSwizzle, other.fSwizzle)); |
| 150 | #endif |
| 151 | return fParams == other.fParams && |
bsalomon@google.com | db545ae | 2012-09-20 15:37:30 +0000 | [diff] [blame] | 152 | (fTexture.get() == other.fTexture.get()) && |
| 153 | (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1)); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool operator!= (const GrTextureAccess& other) const { return !(*this == other); } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 157 | |
| 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.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 176 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 177 | const GrTextureParams& getParams() const { return fParams; } |
| 178 | |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 179 | private: |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 180 | void setSwizzle(const char*); |
| 181 | |
| 182 | GrTextureParams fParams; |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 183 | SkAutoTUnref<GrTexture> fTexture; |
| 184 | uint32_t fSwizzleMask; |
| 185 | char fSwizzle[5]; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 186 | |
| 187 | typedef GrNoncopyable INHERITED; |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | #endif |