blob: e4e373294afa8b3c70f79bbd85e14570150c2def [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 "GrNoncopyable.h"
12#include "SkRefCnt.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000013
14class GrTexture;
bsalomon@google.com047696c2012-09-11 13:29:29 +000015
bsalomon@google.com6d003d12012-09-11 15:45:20 +000016/** A class representing the swizzle access pattern for a texture. Note that if the texture is
17 * an alpha-only texture then the alpha channel is substituted for other components. Any mangling
18 * to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
19 * key. However, if a GrCustomStage uses different swizzles based on its input then it must
20 * consider that variation in its key-generation.
bsalomon@google.com047696c2012-09-11 13:29:29 +000021 */
bsalomon@google.com6d003d12012-09-11 15:45:20 +000022class GrTextureAccess : GrNoncopyable {
bsalomon@google.com047696c2012-09-11 13:29:29 +000023public:
bsalomon@google.com6d003d12012-09-11 15:45:20 +000024 /**
25 * A default GrTextureAccess must have reset() called on it in a GrCustomStage subclass's
26 * constructor if it will be accessible via GrCustomStage::textureAccess().
27 */
28 GrTextureAccess();
bsalomon@google.com047696c2012-09-11 13:29:29 +000029
bsalomon@google.com6d003d12012-09-11 15:45:20 +000030 /**
31 * swizzle must be a string between one and four (inclusive) characters containing only 'r',
32 * 'g', 'b', and/or 'a'.
33 */
34 GrTextureAccess(GrTexture*, const char* swizzle);
bsalomon@google.com047696c2012-09-11 13:29:29 +000035
bsalomon@google.com6d003d12012-09-11 15:45:20 +000036 /**
37 * Uses the default swizzle, "rgba".
38 */
39 GrTextureAccess(GrTexture*);
bsalomon@google.com047696c2012-09-11 13:29:29 +000040
bsalomon@google.com6d003d12012-09-11 15:45:20 +000041 void reset(GrTexture*, const char* swizzle);
42 void reset(GrTexture*);
43
44 GrTexture* getTexture() const { return fTexture.get(); }
45
46 /**
47 * Returns a string representing the swizzle. The string is is null-terminated.
48 */
49 const char* getSwizzle() const { return fSwizzle; }
50
51 enum {
52 kR_SwizzleFlag = 0x1,
53 kG_SwizzleFlag = 0x2,
54 kB_SwizzleFlag = 0x4,
55 kA_SwizzleFlag = 0x8,
56
57 kRGB_SwizzleMask = (kR_SwizzleFlag | kG_SwizzleFlag | kB_SwizzleFlag),
58 };
59
60 /** Returns a mask indicating which components are referenced in the swizzle. */
61 uint32_t swizzleMask() const { return fSwizzleMask; }
bsalomon@google.com047696c2012-09-11 13:29:29 +000062
63private:
bsalomon@google.com6d003d12012-09-11 15:45:20 +000064 SkAutoTUnref<GrTexture> fTexture;
65 uint32_t fSwizzleMask;
66 char fSwizzle[5];
bsalomon@google.com047696c2012-09-11 13:29:29 +000067};
68
69#endif