blob: 5055e103f579ecb89595c9ecbb0c68c187cad365 [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
bsalomonf96ba022014-09-17 08:05:40 -070011#include "GrGpuResourceRef.h"
bsalomonc4923342014-09-16 13:54:53 -070012#include "GrTexture.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000013#include "SkRefCnt.h"
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000014#include "SkShader.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000015
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000016/**
17 * Represents the filtering and tile modes used to access a texture. It is mostly used with
18 * GrTextureAccess (defined below). Also, some of the texture cache methods require knowledge about
19 * 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 +000020 * be folded into GrTextureAccess. The default is clamp tile modes and no filtering.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000021 */
22class GrTextureParams {
23public:
24 GrTextureParams() {
25 this->reset();
26 }
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000027
humper@google.comb86add12013-07-25 18:49:07 +000028 enum FilterMode {
29 kNone_FilterMode,
30 kBilerp_FilterMode,
31 kMipMap_FilterMode
32 };
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000033
humper@google.comb86add12013-07-25 18:49:07 +000034 GrTextureParams(SkShader::TileMode tileXAndY, FilterMode filterMode) {
35 this->reset(tileXAndY, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000036 }
37
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000038 GrTextureParams(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
humper@google.comb86add12013-07-25 18:49:07 +000039 this->reset(tileModes, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000040 }
41
42 GrTextureParams(const GrTextureParams& params) {
43 *this = params;
44 }
45
46 GrTextureParams& operator= (const GrTextureParams& params) {
47 fTileModes[0] = params.fTileModes[0];
48 fTileModes[1] = params.fTileModes[1];
humper@google.comb86add12013-07-25 18:49:07 +000049 fFilterMode = params.fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000050 return *this;
51 }
52
53 void reset() {
humper@google.comb86add12013-07-25 18:49:07 +000054 this->reset(SkShader::kClamp_TileMode, kNone_FilterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000055 }
56
humper@google.comb86add12013-07-25 18:49:07 +000057 void reset(SkShader::TileMode tileXAndY, FilterMode filterMode) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000058 fTileModes[0] = fTileModes[1] = tileXAndY;
humper@google.comb86add12013-07-25 18:49:07 +000059 fFilterMode = filterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000060 }
61
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000062 void reset(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000063 fTileModes[0] = tileModes[0];
64 fTileModes[1] = tileModes[1];
humper@google.comb86add12013-07-25 18:49:07 +000065 fFilterMode = filterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000066 }
67
68 void setClampNoFilter() {
69 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
humper@google.comb86add12013-07-25 18:49:07 +000070 fFilterMode = kNone_FilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000071 }
72
73 void setClamp() {
74 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
75 }
76
humper@google.comb86add12013-07-25 18:49:07 +000077 void setFilterMode(FilterMode filterMode) { fFilterMode = filterMode; }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000078
79 void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
80 void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
81 void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
82
83 SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
84
85 SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
86
87 bool isTiled() const {
88 return SkShader::kClamp_TileMode != fTileModes[0] ||
89 SkShader::kClamp_TileMode != fTileModes[1];
90 }
91
humper@google.comb86add12013-07-25 18:49:07 +000092 FilterMode filterMode() const { return fFilterMode; }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000093
94 bool operator== (const GrTextureParams& other) const {
95 return fTileModes[0] == other.fTileModes[0] &&
96 fTileModes[1] == other.fTileModes[1] &&
humper@google.comb86add12013-07-25 18:49:07 +000097 fFilterMode == other.fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000098 }
99
100 bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
101
102private:
103
104 SkShader::TileMode fTileModes[2];
humper@google.comb86add12013-07-25 18:49:07 +0000105 FilterMode fFilterMode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000106};
107
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000108/** A class representing the swizzle access pattern for a texture. Note that if the texture is
109 * an alpha-only texture then the alpha channel is substituted for other components. Any mangling
110 * to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
joshualittb0a8a372014-09-23 09:50:21 -0700111 * key. However, if a GrProcessor uses different swizzles based on its input then it must
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000112 * consider that variation in its key-generation.
bsalomon@google.com047696c2012-09-11 13:29:29 +0000113 */
bsalomon2a9ca782014-09-05 14:27:43 -0700114class GrTextureAccess : public SkNoncopyable {
bsalomon@google.com047696c2012-09-11 13:29:29 +0000115public:
bsalomon2a9ca782014-09-05 14:27:43 -0700116 SK_DECLARE_INST_COUNT_ROOT(GrTextureAccess);
117
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000118 /**
joshualittb0a8a372014-09-23 09:50:21 -0700119 * A default GrTextureAccess must have reset() called on it in a GrProcessor subclass's
120 * constructor if it will be accessible via GrProcessor::textureAccess().
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000121 */
122 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +0000123
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000124 /**
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000125 * Uses the default swizzle, "rgba".
126 */
127 GrTextureAccess(GrTexture*, const GrTextureParams&);
128 explicit GrTextureAccess(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000129 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000130 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
131
132 /**
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000133 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
134 * 'g', 'b', and/or 'a'.
135 */
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000136 GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
137 GrTextureAccess(GrTexture*,
138 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000139 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000140 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000141
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000142 void reset(GrTexture*, const GrTextureParams&);
143 void reset(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000144 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000145 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
146 void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
147 void reset(GrTexture*,
148 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000149 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000150 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000151
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000152 bool operator== (const GrTextureAccess& other) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000153#ifdef SK_DEBUG
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000154 // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000155 SkASSERT(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000156 strcmp(fSwizzle, other.fSwizzle));
157#endif
158 return fParams == other.fParams &&
bsalomon2a9ca782014-09-05 14:27:43 -0700159 (this->getTexture() == other.getTexture()) &&
bsalomon@google.comdb545ae2012-09-20 15:37:30 +0000160 (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000161 }
162
163 bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000164
bsalomonc4923342014-09-16 13:54:53 -0700165 GrTexture* getTexture() const { return fTexture.get(); }
bsalomon2a9ca782014-09-05 14:27:43 -0700166
167 /**
joshualittb0a8a372014-09-23 09:50:21 -0700168 * For internal use by GrProcessor.
bsalomon2a9ca782014-09-05 14:27:43 -0700169 */
bsalomonf96ba022014-09-17 08:05:40 -0700170 const GrGpuResourceRef* getProgramTexture() const { return &fTexture; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000171
172 /**
173 * Returns a string representing the swizzle. The string is is null-terminated.
174 */
175 const char* getSwizzle() const { return fSwizzle; }
176
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000177 /** Returns a mask indicating which components are referenced in the swizzle. The return
178 is a bitfield of GrColorComponentFlags. */
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000179 uint32_t swizzleMask() const { return fSwizzleMask; }
bsalomon@google.com047696c2012-09-11 13:29:29 +0000180
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000181 const GrTextureParams& getParams() const { return fParams; }
182
bsalomon@google.com047696c2012-09-11 13:29:29 +0000183private:
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000184 void setSwizzle(const char*);
185
bsalomonf96ba022014-09-17 08:05:40 -0700186 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
bsalomonc4923342014-09-16 13:54:53 -0700187
188 ProgramTexture fTexture;
bsalomon2a9ca782014-09-05 14:27:43 -0700189 GrTextureParams fParams;
190 uint32_t fSwizzleMask;
191 char fSwizzle[5];
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000192
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000193 typedef SkNoncopyable INHERITED;
bsalomon@google.com047696c2012-09-11 13:29:29 +0000194};
195
196#endif