blob: df0bdfda0fd7fce56225b76eb22d9dcdfc641e5f [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:
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000116 /**
joshualittb0a8a372014-09-23 09:50:21 -0700117 * A default GrTextureAccess must have reset() called on it in a GrProcessor subclass's
118 * constructor if it will be accessible via GrProcessor::textureAccess().
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000119 */
120 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +0000121
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000122 /**
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000123 * Uses the default swizzle, "rgba".
124 */
125 GrTextureAccess(GrTexture*, const GrTextureParams&);
126 explicit GrTextureAccess(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000127 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000128 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
129
130 /**
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000131 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
132 * 'g', 'b', and/or 'a'.
133 */
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000134 GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
135 GrTextureAccess(GrTexture*,
136 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000137 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000138 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000139
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000140 void reset(GrTexture*, const GrTextureParams&);
141 void reset(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +0000142 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000143 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
144 void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
145 void reset(GrTexture*,
146 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +0000147 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000148 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +0000149
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000150 bool operator== (const GrTextureAccess& other) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000151#ifdef SK_DEBUG
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000152 // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000154 strcmp(fSwizzle, other.fSwizzle));
155#endif
156 return fParams == other.fParams &&
bsalomon2a9ca782014-09-05 14:27:43 -0700157 (this->getTexture() == other.getTexture()) &&
bsalomon@google.comdb545ae2012-09-20 15:37:30 +0000158 (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000159 }
160
161 bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000162
bsalomonc4923342014-09-16 13:54:53 -0700163 GrTexture* getTexture() const { return fTexture.get(); }
bsalomon2a9ca782014-09-05 14:27:43 -0700164
165 /**
joshualittb0a8a372014-09-23 09:50:21 -0700166 * For internal use by GrProcessor.
bsalomon2a9ca782014-09-05 14:27:43 -0700167 */
bsalomonf96ba022014-09-17 08:05:40 -0700168 const GrGpuResourceRef* getProgramTexture() const { return &fTexture; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000169
170 /**
171 * Returns a string representing the swizzle. The string is is null-terminated.
172 */
173 const char* getSwizzle() const { return fSwizzle; }
174
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000175 /** Returns a mask indicating which components are referenced in the swizzle. The return
176 is a bitfield of GrColorComponentFlags. */
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000177 uint32_t swizzleMask() const { return fSwizzleMask; }
bsalomon@google.com047696c2012-09-11 13:29:29 +0000178
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000179 const GrTextureParams& getParams() const { return fParams; }
180
bsalomon@google.com047696c2012-09-11 13:29:29 +0000181private:
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000182 void setSwizzle(const char*);
183
bsalomonf96ba022014-09-17 08:05:40 -0700184 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
bsalomonc4923342014-09-16 13:54:53 -0700185
186 ProgramTexture fTexture;
bsalomon2a9ca782014-09-05 14:27:43 -0700187 GrTextureParams fParams;
188 uint32_t fSwizzleMask;
189 char fSwizzle[5];
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000190
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000191 typedef SkNoncopyable INHERITED;
bsalomon@google.com047696c2012-09-11 13:29:29 +0000192};
193
194#endif