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 | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 11 | #include "GrGpuResourceRef.h" |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 12 | #include "GrTexture.h" |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 13 | #include "GrTextureParams.h" |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 15 | #include "SkShader.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 16 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 17 | /** 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.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 22 | */ |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 23 | class GrTextureAccess : public SkNoncopyable { |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 24 | public: |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 25 | /** |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 26 | * 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.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 28 | */ |
| 29 | GrTextureAccess(); |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 30 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 31 | /** |
| 32 | * Uses the default swizzle, "rgba". |
| 33 | */ |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 34 | GrTextureAccess(GrTexture*, const GrTextureParams&); |
| 35 | explicit GrTextureAccess(GrTexture*, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 36 | GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 37 | SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode); |
| 38 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 39 | /** |
| 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.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 49 | void reset(GrTexture*, const GrTextureParams&); |
| 50 | void reset(GrTexture*, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 51 | GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 52 | SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode); |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 53 | 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.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 58 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 59 | 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.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 68 | } |
| 69 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 70 | bool operator!= (const GrTextureAccess& other) const { return !(*this == other); } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 71 | |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 72 | GrTexture* getTexture() const { return fTexture.get(); } |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 75 | * For internal use by GrProcessor. |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 76 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 77 | const GrGpuResourceRef* getProgramTexture() const { return &fTexture; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 78 | |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 79 | /** |
| 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.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 88 | const GrTextureParams& getParams() const { return fParams; } |
| 89 | |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 90 | private: |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 91 | void setSwizzle(const char*); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 92 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 93 | typedef GrTGpuResourceRef<GrTexture> ProgramTexture; |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 94 | |
| 95 | ProgramTexture fTexture; |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 96 | GrTextureParams fParams; |
mtklein | 3183a41 | 2016-01-08 12:11:39 -0800 | [diff] [blame^] | 97 | uint32_t fSwizzleMask; |
| 98 | char fSwizzle[5]; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 100 | typedef SkNoncopyable INHERITED; |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #endif |