aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrSwizzle.h
diff options
context:
space:
mode:
authorGravatar Brian Salomon <bsalomon@google.com>2016-11-18 15:08:06 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2016-11-18 21:19:17 +0000
commit1aaccc77dc14e9f08f52c12ca5661fc449f9c01a (patch)
tree22929d154f429543e93aec821d4997374394b072 /src/gpu/GrSwizzle.h
parentf31ae495271e0343f568fb9a1d7e2df81e196896 (diff)
Make GrSwizzle::GrSwizzle() constexpr
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=5060 Change-Id: I862b895fd92b9b6938627af7b21022564b535e9b Reviewed-on: https://skia-review.googlesource.com/5060 Reviewed-by: Mike Klein <mtklein@chromium.org> Commit-Queue: Mike Klein <mtklein@chromium.org> Commit-Queue: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'src/gpu/GrSwizzle.h')
-rw-r--r--src/gpu/GrSwizzle.h101
1 files changed, 32 insertions, 69 deletions
diff --git a/src/gpu/GrSwizzle.h b/src/gpu/GrSwizzle.h
index c2288b2fe4..5fa39dd987 100644
--- a/src/gpu/GrSwizzle.h
+++ b/src/gpu/GrSwizzle.h
@@ -15,6 +15,32 @@
Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an
assignment operator. That could be relaxed. */
class GrSwizzle {
+private:
+ char fSwiz[5];
+ uint8_t fKey;
+
+ static constexpr int CToI(char c) {
+ return ('r' == c) ? (GrColor_SHIFT_R / 8) :
+ ('g' == c) ? (GrColor_SHIFT_G / 8) :
+ ('b' == c) ? (GrColor_SHIFT_B / 8) :
+ ('a' == c) ? (GrColor_SHIFT_A / 8) : -1;
+ }
+
+ static constexpr char IToC(int idx) {
+ return (8 * idx) == GrColor_SHIFT_R ? 'r' :
+ (8 * idx) == GrColor_SHIFT_G ? 'g' :
+ (8 * idx) == GrColor_SHIFT_B ? 'b' :
+ (8 * idx) == GrColor_SHIFT_A ? 'a' : 'x';
+ }
+
+ constexpr GrSwizzle(const char c[4])
+ : fSwiz{c[0], c[1], c[2], c[3], 0}
+ , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 2) | (CToI(c[2]) << 4) | (CToI(c[3]) << 6)) {}
+
+ GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t));
+ uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); }
+ uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); }
+
public:
GrSwizzle() { *this = RGBA(); }
@@ -29,7 +55,7 @@ public:
void setFromKey(uint8_t key) {
fKey = key;
for (int i = 0; i < 4; ++i) {
- fSwiz[i] = IdxToChar(key & 3);
+ fSwiz[i] = IToC(key & 3);
key >>= 2;
}
SkASSERT(fSwiz[4] == 0);
@@ -64,27 +90,12 @@ public:
return GrColorPackRGBA(outR, outG, outB, outA);
}
- static const GrSwizzle& RGBA() {
- static GrSwizzle gRGBA("rgba");
- return gRGBA;
- }
-
- static const GrSwizzle& AAAA() {
- static GrSwizzle gAAAA("aaaa");
- return gAAAA;
- }
-
- static const GrSwizzle& RRRR() {
- static GrSwizzle gRRRR("rrrr");
- return gRRRR;
- }
-
- static const GrSwizzle& BGRA() {
- static GrSwizzle gBGRA("bgra");
- return gBGRA;
- }
+ static GrSwizzle RGBA() { return GrSwizzle("rgba"); }
+ static GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
+ static GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
+ static GrSwizzle BGRA() { return GrSwizzle("bgra"); }
- static const GrSwizzle& CreateRandom(SkRandom* random) {
+ static GrSwizzle CreateRandom(SkRandom* random) {
switch (random->nextU() % 4) {
case 0:
return RGBA();
@@ -99,54 +110,6 @@ public:
return RGBA();
}
}
-
-private:
- char fSwiz[5];
- uint8_t fKey;
-
- static int CharToIdx(char c) {
- switch (c) {
- case 'r':
- return (GrColor_SHIFT_R / 8);
- case 'g':
- return (GrColor_SHIFT_G / 8);
- case 'b':
- return (GrColor_SHIFT_B / 8);
- case 'a':
- return (GrColor_SHIFT_A / 8);
- default:
- SkFAIL("Invalid swizzle char");
- return 0;
- }
- }
-
- static /* constexpr */ char IToC(int idx) {
- return (8*idx) == GrColor_SHIFT_R ? 'r' :
- (8*idx) == GrColor_SHIFT_G ? 'g' :
- (8*idx) == GrColor_SHIFT_B ? 'b' : 'a';
- }
-
- static char IdxToChar(int c) {
- // Hopefully this array gets computed at compile time.
- static const char gStr[4] = { IToC(0), IToC(1), IToC(2), IToC(3) };
- return gStr[c];
- }
-
- explicit GrSwizzle(const char* str) {
- SkASSERT(strlen(str) == 4);
- fSwiz[0] = str[0];
- fSwiz[1] = str[1];
- fSwiz[2] = str[2];
- fSwiz[3] = str[3];
- fSwiz[4] = 0;
- fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) |
- (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6));
- }
-
- uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); }
- uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); }
-
- GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t));
};
#endif