OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrSwizzle_DEFINED | 8 #ifndef GrSwizzle_DEFINED |
9 #define GrSwizzle_DEFINED | 9 #define GrSwizzle_DEFINED |
10 | 10 |
11 #include "GrTypes.h" | 11 #include "GrTypes.h" |
12 #include "GrColor.h" | |
12 | 13 |
13 /** Represents a rgba swizzle. It can be converted either into a string or a eig ht bit int. | 14 /** Represents a rgba swizzle. It can be converted either into a string or a eig ht bit int. |
14 Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an | 15 Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an |
15 assignment operator. That could be relaxed. */ | 16 assignment operator. That could be relaxed. */ |
16 class GrSwizzle { | 17 class GrSwizzle { |
17 public: | 18 public: |
18 GrSwizzle() { *this = RGBA(); } | 19 GrSwizzle() { *this = RGBA(); } |
19 | 20 |
21 GrSwizzle(const GrSwizzle& that) { *this = that; } | |
22 | |
20 GrSwizzle& operator=(const GrSwizzle& that) { | 23 GrSwizzle& operator=(const GrSwizzle& that) { |
21 memcpy(this, &that, sizeof(GrSwizzle)); | 24 memcpy(this, &that, sizeof(GrSwizzle)); |
22 return *this; | 25 return *this; |
23 } | 26 } |
24 | 27 |
28 /** Recreates a GrSwizzle from the output of asKey() */ | |
29 void setFromKey(uint8_t key) { | |
30 fKey = key; | |
31 for (int i = 0; i < 4; ++i) { | |
32 fSwiz[i] = IdxToChar(key & 3); | |
33 key >>= 2; | |
34 } | |
35 SkASSERT(fSwiz[4] == 0); | |
36 } | |
37 | |
25 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that .asUInt(); } | 38 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that .asUInt(); } |
26 | 39 |
27 bool operator!=(const GrSwizzle& that) const { return !(*this == that); } | 40 bool operator!=(const GrSwizzle& that) const { return !(*this == that); } |
28 | 41 |
29 /** Compact representation of the swizzle suitable for a key. */ | 42 /** Compact representation of the swizzle suitable for a key. */ |
30 uint8_t asKey() const { return fKey; } | 43 uint8_t asKey() const { return fKey; } |
31 | 44 |
32 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a '. */ | 45 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a '. */ |
33 const char* c_str() const { return fSwiz; } | 46 const char* c_str() const { return fSwiz; } |
34 | 47 |
48 /** Applies this swizzle to the input color and returns the swizzled color. */ | |
49 GrColor applyTo(GrColor color) const { | |
50 int idx; | |
51 uint32_t key = fKey; | |
52 // Index of the input color that should be mapped to output r. | |
53 idx = (key & 3); | |
54 uint32_t outR = (color >> idx * 8) & 0xFF; | |
55 key >>= 2; | |
56 idx = (key & 3); | |
57 uint32_t outG = (color >> idx * 8) & 0xFF; | |
58 key >>= 2; | |
59 idx = (key & 3); | |
60 uint32_t outB = (color >> idx * 8) & 0xFF; | |
61 key >>= 2; | |
62 idx = (key & 3); | |
63 uint32_t outA = (color >> idx * 8) & 0xFF; | |
64 return GrColorPackRGBA(outR, outG, outB, outA); | |
65 } | |
66 | |
35 static const GrSwizzle& RGBA() { | 67 static const GrSwizzle& RGBA() { |
36 static GrSwizzle gRGBA("rgba"); | 68 static GrSwizzle gRGBA("rgba"); |
37 return gRGBA; | 69 return gRGBA; |
38 } | 70 } |
39 | 71 |
40 static const GrSwizzle& AAAA() { | 72 static const GrSwizzle& AAAA() { |
41 static GrSwizzle gAAAA("aaaa"); | 73 static GrSwizzle gAAAA("aaaa"); |
42 return gAAAA; | 74 return gAAAA; |
43 } | 75 } |
44 | 76 |
45 static const GrSwizzle& RRRR() { | 77 static const GrSwizzle& RRRR() { |
46 static GrSwizzle gRRRR("rrrr"); | 78 static GrSwizzle gRRRR("rrrr"); |
47 return gRRRR; | 79 return gRRRR; |
48 } | 80 } |
49 | 81 |
50 static const GrSwizzle& BGRA() { | 82 static const GrSwizzle& BGRA() { |
51 static GrSwizzle gBGRA("bgra"); | 83 static GrSwizzle gBGRA("bgra"); |
52 return gBGRA; | 84 return gBGRA; |
53 } | 85 } |
54 | 86 |
55 private: | 87 private: |
56 char fSwiz[5]; | 88 char fSwiz[5]; |
57 uint8_t fKey; | 89 uint8_t fKey; |
58 | 90 |
59 static int CharToIdx(char c) { | 91 static int CharToIdx(char c) { |
60 switch (c) { | 92 switch (c) { |
61 case 'r': | 93 case 'r': |
62 return 0; | 94 return (GrColor_SHIFT_R / 8); |
63 case 'g': | 95 case 'g': |
64 return 1; | 96 return (GrColor_SHIFT_G / 8); |
65 case 'b': | 97 case 'b': |
66 return 2; | 98 return (GrColor_SHIFT_B / 8); |
67 case 'a': | 99 case 'a': |
68 return 3; | 100 return (GrColor_SHIFT_A / 8); |
69 default: | 101 default: |
70 SkFAIL("Invalid swizzle char"); | 102 SkFAIL("Invalid swizzle char"); |
71 return 0; | 103 return 0; |
72 } | 104 } |
73 } | 105 } |
74 | 106 |
107 static /* constexpr */ char IToC(int idx) { | |
108 return (8*idx) == GrColor_SHIFT_R ? 'r' : | |
109 (8*idx) == GrColor_SHIFT_G ? 'g' : | |
110 (8*idx) == GrColor_SHIFT_B ? 'b' : | |
111 'a'; | |
egdaniel
2016/01/12 20:13:55
not a nested ternary operator formatting expert, b
bsalomon
2016/01/12 20:33:55
Moved it up to the prev line, looked better IMO.
| |
112 } | |
113 | |
114 static char IdxToChar(int c) { | |
115 // Hopefully this array gets computed at compile time. | |
116 static const char gStr[4] = { IToC(0), IToC(1), IToC(2), IToC(3) }; | |
117 return gStr[c]; | |
118 } | |
119 | |
75 explicit GrSwizzle(const char* str) { | 120 explicit GrSwizzle(const char* str) { |
76 SkASSERT(strlen(str) == 4); | 121 SkASSERT(strlen(str) == 4); |
77 fSwiz[0] = str[0]; | 122 fSwiz[0] = str[0]; |
78 fSwiz[1] = str[1]; | 123 fSwiz[1] = str[1]; |
79 fSwiz[2] = str[2]; | 124 fSwiz[2] = str[2]; |
80 fSwiz[3] = str[3]; | 125 fSwiz[3] = str[3]; |
81 fSwiz[4] = 0; | 126 fSwiz[4] = 0; |
82 fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) | | 127 fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) | |
83 (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6)); | 128 (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6)); |
84 } | 129 } |
85 | 130 |
86 uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); } | 131 uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); } |
87 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); } | 132 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); } |
88 | 133 |
89 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t)); | 134 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t)); |
90 }; | 135 }; |
91 | 136 |
92 #endif | 137 #endif |
OLD | NEW |