blob: e3ded34ff8c54ebb5ee9d5caef890d7ef3a82137 [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"
bsalomonafa95e22015-10-12 10:39:46 -070013#include "GrTextureParams.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000014#include "SkRefCnt.h"
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000015#include "SkShader.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000016
mtklein3183a412016-01-08 12:11:39 -080017/** A class representing the swizzle access pattern for a texture. Note that if the texture is
18 * an alpha-only texture then the alpha channel is substituted for other components. Any mangling
19 * to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
20 * key. However, if a GrProcessor uses different swizzles based on its input then it must
21 * consider that variation in its key-generation.
bsalomon@google.com047696c2012-09-11 13:29:29 +000022 */
bsalomon2a9ca782014-09-05 14:27:43 -070023class GrTextureAccess : public SkNoncopyable {
bsalomon@google.com047696c2012-09-11 13:29:29 +000024public:
bsalomon@google.com6d003d12012-09-11 15:45:20 +000025 /**
mtklein3183a412016-01-08 12:11:39 -080026 * A default GrTextureAccess must have reset() called on it in a GrProcessor subclass's
27 * constructor if it will be accessible via GrProcessor::textureAccess().
bsalomon@google.com6d003d12012-09-11 15:45:20 +000028 */
29 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +000030
mtklein3183a412016-01-08 12:11:39 -080031 /**
32 * Uses the default swizzle, "rgba".
33 */
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000034 GrTextureAccess(GrTexture*, const GrTextureParams&);
35 explicit GrTextureAccess(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +000036 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000037 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
38
mtklein3183a412016-01-08 12:11:39 -080039 /**
40 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
41 * 'g', 'b', and/or 'a'.
42 */
43 GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
44 GrTextureAccess(GrTexture*,
45 const char* swizzle,
46 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
47 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
48
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000049 void reset(GrTexture*, const GrTextureParams&);
50 void reset(GrTexture*,
humper@google.comb86add12013-07-25 18:49:07 +000051 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000052 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
mtklein3183a412016-01-08 12:11:39 -080053 void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
54 void reset(GrTexture*,
55 const char* swizzle,
56 GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
57 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
bsalomon@google.com047696c2012-09-11 13:29:29 +000058
mtklein3183a412016-01-08 12:11:39 -080059 bool operator== (const GrTextureAccess& other) const {
60#ifdef SK_DEBUG
61 // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
62 SkASSERT(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
63 strcmp(fSwizzle, other.fSwizzle));
64#endif
65 return fParams == other.fParams &&
66 (this->getTexture() == other.getTexture()) &&
67 (0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000068 }
69
mtklein3183a412016-01-08 12:11:39 -080070 bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000071
bsalomonc4923342014-09-16 13:54:53 -070072 GrTexture* getTexture() const { return fTexture.get(); }
bsalomon2a9ca782014-09-05 14:27:43 -070073
74 /**
joshualittb0a8a372014-09-23 09:50:21 -070075 * For internal use by GrProcessor.
bsalomon2a9ca782014-09-05 14:27:43 -070076 */
bsalomonf96ba022014-09-17 08:05:40 -070077 const GrGpuResourceRef* getProgramTexture() const { return &fTexture; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000078
mtklein3183a412016-01-08 12:11:39 -080079 /**
80 * Returns a string representing the swizzle. The string is is null-terminated.
81 */
82 const char* getSwizzle() const { return fSwizzle; }
83
84 /** Returns a mask indicating which components are referenced in the swizzle. The return
85 is a bitfield of GrColorComponentFlags. */
86 uint32_t swizzleMask() const { return fSwizzleMask; }
87
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000088 const GrTextureParams& getParams() const { return fParams; }
89
bsalomon@google.com047696c2012-09-11 13:29:29 +000090private:
mtklein3183a412016-01-08 12:11:39 -080091 void setSwizzle(const char*);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000092
bsalomonf96ba022014-09-17 08:05:40 -070093 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
bsalomonc4923342014-09-16 13:54:53 -070094
95 ProgramTexture fTexture;
bsalomon2a9ca782014-09-05 14:27:43 -070096 GrTextureParams fParams;
mtklein3183a412016-01-08 12:11:39 -080097 uint32_t fSwizzleMask;
98 char fSwizzle[5];
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000099
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000100 typedef SkNoncopyable INHERITED;
bsalomon@google.com047696c2012-09-11 13:29:29 +0000101};
102
103#endif