blob: 3d44d9e44f1f084928da2a5f3bb21741759df09d [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 "SkRefCnt.h"
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000012#include "SkShader.h"
bsalomon2a9ca782014-09-05 14:27:43 -070013#include "GrProgramResource.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
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000021 * be folded into GrTextureAccess. The default is clamp tile modes and no filtering.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000022 */
23class GrTextureParams {
24public:
25 GrTextureParams() {
26 this->reset();
27 }
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000028
humper@google.comb86add12013-07-25 18:49:07 +000029 enum FilterMode {
30 kNone_FilterMode,
31 kBilerp_FilterMode,
32 kMipMap_FilterMode
33 };
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000034
humper@google.comb86add12013-07-25 18:49:07 +000035 GrTextureParams(SkShader::TileMode tileXAndY, FilterMode filterMode) {
36 this->reset(tileXAndY, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000037 }
38
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000039 GrTextureParams(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
humper@google.comb86add12013-07-25 18:49:07 +000040 this->reset(tileModes, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000041 }
42
43 GrTextureParams(const GrTextureParams& params) {
44 *this = params;
45 }
46
47 GrTextureParams& operator= (const GrTextureParams& params) {
48 fTileModes[0] = params.fTileModes[0];
49 fTileModes[1] = params.fTileModes[1];
humper@google.comb86add12013-07-25 18:49:07 +000050 fFilterMode = params.fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000051 return *this;
52 }
53
54 void reset() {
humper@google.comb86add12013-07-25 18:49:07 +000055 this->reset(SkShader::kClamp_TileMode, kNone_FilterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000056 }
57
humper@google.comb86add12013-07-25 18:49:07 +000058 void reset(SkShader::TileMode tileXAndY, FilterMode filterMode) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000059 fTileModes[0] = fTileModes[1] = tileXAndY;
humper@google.comb86add12013-07-25 18:49:07 +000060 fFilterMode = filterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000061 }
62
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000063 void reset(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000064 fTileModes[0] = tileModes[0];
65 fTileModes[1] = tileModes[1];
humper@google.comb86add12013-07-25 18:49:07 +000066 fFilterMode = filterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000067 }
68
69 void setClampNoFilter() {
70 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
humper@google.comb86add12013-07-25 18:49:07 +000071 fFilterMode = kNone_FilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000072 }
73
74 void setClamp() {
75 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
76 }
77
humper@google.comb86add12013-07-25 18:49:07 +000078 void setFilterMode(FilterMode filterMode) { fFilterMode = filterMode; }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000079
80 void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
81 void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
82 void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
83
84 SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
85
86 SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
87
88 bool isTiled() const {
89 return SkShader::kClamp_TileMode != fTileModes[0] ||
90 SkShader::kClamp_TileMode != fTileModes[1];
91 }
92
humper@google.comb86add12013-07-25 18:49:07 +000093 FilterMode filterMode() const { return fFilterMode; }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000094
95 bool operator== (const GrTextureParams& other) const {
96 return fTileModes[0] == other.fTileModes[0] &&
97 fTileModes[1] == other.fTileModes[1] &&
humper@google.comb86add12013-07-25 18:49:07 +000098 fFilterMode == other.fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000099 }
100
101 bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
102
103private:
104
105 SkShader::TileMode fTileModes[2];
humper@google.comb86add12013-07-25 18:49:07 +0000106 FilterMode fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000107};
108
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000109/** A class representing the swizzle access pattern for a texture. Note that if the texture is
110 * an alpha-only texture then the alpha channel is substituted for other components. Any mangling
111 * 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 +0000112 * key. However, if a GrEffect uses different swizzles based on its input then it must
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000113 * consider that variation in its key-generation.
bsalomon@google.com047696c2012-09-11 13:29:29 +0000114 */
bsalomon2a9ca782014-09-05 14:27:43 -0700115class GrTextureAccess : public SkNoncopyable {
bsalomon@google.com047696c2012-09-11 13:29:29 +0000116public:
bsalomon2a9ca782014-09-05 14:27:43 -0700117 SK_DECLARE_INST_COUNT_ROOT(GrTextureAccess);
118
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000119 /**
bsalomon@google.coma469c282012-10-24 18:28:34 +0000120 * A default GrTextureAccess must have reset() called on it in a GrEffect subclass's
121 * constructor if it will be accessible via GrEffect::textureAccess().
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000122 */
123 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +0000124
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000125 /**
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000126 * Uses the default swizzle, "rgba".
127 */
128 GrTextureAccess(GrTexture*, const GrTextureParams&);
129 explicit GrTextureAccess(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000130 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000131 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
132
133 /**
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000134 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
135 * 'g', 'b', and/or 'a'.
136 */
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000137 GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
138 GrTextureAccess(GrTexture*,
139 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000140 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000141 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000142
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000143 void reset(GrTexture*, const GrTextureParams&);
144 void reset(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000145 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000146 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
147 void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
148 void reset(GrTexture*,
149 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000150 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000151 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000152
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000153 bool operator== (const GrTextureAccess& other) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000154#ifdef SK_DEBUG
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000155 // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000156 SkASSERT(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000157 strcmp(fSwizzle, other.fSwizzle));
158#endif
159 return fParams == other.fParams &&
bsalomon2a9ca782014-09-05 14:27:43 -0700160 (this->getTexture() == other.getTexture()) &&
bsalomon@google.comdb545ae2012-09-20 15:37:30 +0000161 (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000162 }
163
164 bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000165
bsalomon2a9ca782014-09-05 14:27:43 -0700166 GrTexture* getTexture() const { return (GrTexture*)fTexture.getResource(); }
167
168 /**
169 * For internal use by GrEffect.
170 */
171 const GrProgramResource* getTextureProgramResource() const { return &fTexture; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000172
173 /**
174 * Returns a string representing the swizzle. The string is is null-terminated.
175 */
176 const char* getSwizzle() const { return fSwizzle; }
177
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000178 /** Returns a mask indicating which components are referenced in the swizzle. The return
179 is a bitfield of GrColorComponentFlags. */
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000180 uint32_t swizzleMask() const { return fSwizzleMask; }
bsalomon@google.com047696c2012-09-11 13:29:29 +0000181
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000182 const GrTextureParams& getParams() const { return fParams; }
183
bsalomon@google.com047696c2012-09-11 13:29:29 +0000184private:
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000185 void setSwizzle(const char*);
186
bsalomon2a9ca782014-09-05 14:27:43 -0700187 GrProgramResource fTexture;
188 GrTextureParams fParams;
189 uint32_t fSwizzleMask;
190 char fSwizzle[5];
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000191
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000192 typedef SkNoncopyable INHERITED;
bsalomon@google.com047696c2012-09-11 13:29:29 +0000193};
194
195#endif