1
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11 #ifndef GrColor_DEFINED
12 #define GrColor_DEFINED
13
14 #include "GrTypes.h"
15 #include "SkColor.h"
16 #include "SkColorPriv.h"
17 #include "SkUnPreMultiply.h"
18
19 /**
20 * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. The components are stored
21 * premultiplied.
22 */
23 typedef uint32_t GrColor;
24
25 // shift amount to assign a component to a GrColor int
26 // These shift values are chosen for compatibility with GL attrib arrays
27 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
28 // we'd have to swizzle in shaders.
29 #ifdef SK_CPU_BENDIAN
30 #define GrColor_SHIFT_R 24
31 #define GrColor_SHIFT_G 16
32 #define GrColor_SHIFT_B 8
33 #define GrColor_SHIFT_A 0
34 #else
35 #define GrColor_SHIFT_R 0
36 #define GrColor_SHIFT_G 8
37 #define GrColor_SHIFT_B 16
38 #define GrColor_SHIFT_A 24
39 #endif
40
41 /**
42 * Pack 4 components (RGBA) into a GrColor int
43 */
GrColorPackRGBA(unsigned r,unsigned g,unsigned b,unsigned a)44 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
45 SkASSERT((uint8_t)r == r);
46 SkASSERT((uint8_t)g == g);
47 SkASSERT((uint8_t)b == b);
48 SkASSERT((uint8_t)a == a);
49 return (r << GrColor_SHIFT_R) |
50 (g << GrColor_SHIFT_G) |
51 (b << GrColor_SHIFT_B) |
52 (a << GrColor_SHIFT_A);
53 }
54
55 /**
56 * Packs a color with an alpha channel replicated across all four channels.
57 */
GrColorPackA4(unsigned a)58 static inline GrColor GrColorPackA4(unsigned a) {
59 SkASSERT((uint8_t)a == a);
60 return (a << GrColor_SHIFT_R) |
61 (a << GrColor_SHIFT_G) |
62 (a << GrColor_SHIFT_B) |
63 (a << GrColor_SHIFT_A);
64 }
65
66 // extract a component (byte) from a GrColor int
67
68 #define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF)
69 #define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF)
70 #define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF)
71 #define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF)
72
73 /**
74 * Since premultiplied means that alpha >= color, we construct a color with
75 * each component==255 and alpha == 0 to be "illegal"
76 */
77 #define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A))
78
79 #define GrColor_WHITE 0xFFFFFFFF
80 #define GrColor_TRANS_BLACK 0x0
81
82 /**
83 * Assert in debug builds that a GrColor is premultiplied.
84 */
GrColorIsPMAssert(GrColor SkDEBUGCODE (c))85 static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) {
86 #ifdef SK_DEBUG
87 unsigned a = GrColorUnpackA(c);
88 unsigned r = GrColorUnpackR(c);
89 unsigned g = GrColorUnpackG(c);
90 unsigned b = GrColorUnpackB(c);
91
92 SkASSERT(r <= a);
93 SkASSERT(g <= a);
94 SkASSERT(b <= a);
95 #endif
96 }
97
98 /** Converts a GrColor to an rgba array of GrGLfloat */
GrColorToRGBAFloat(GrColor color,float rgba[4])99 static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
100 static const float ONE_OVER_255 = 1.f / 255.f;
101 rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
102 rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
103 rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
104 rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
105 }
106
107 /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
GrNormalizeByteToFloat(uint8_t value)108 static inline float GrNormalizeByteToFloat(uint8_t value) {
109 static const float ONE_OVER_255 = 1.f / 255.f;
110 return value * ONE_OVER_255;
111 }
112
113 /** Determines whether the color is opaque or not. */
GrColorIsOpaque(GrColor color)114 static inline bool GrColorIsOpaque(GrColor color) {
115 return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A);
116 }
117
118 /** Returns an unpremuled version of the GrColor. */
GrUnPreMulColor(GrColor color)119 static inline GrColor GrUnPreMulColor(GrColor color) {
120 unsigned r = GrColorUnpackR(color);
121 unsigned g = GrColorUnpackG(color);
122 unsigned b = GrColorUnpackB(color);
123 unsigned a = GrColorUnpackA(color);
124 SkPMColor colorPM = SkPackARGB32(a, r, g, b);
125 SkColor colorUPM = SkUnPreMultiply::PMColorToColor(colorPM);
126
127 r = SkColorGetR(colorUPM);
128 g = SkColorGetG(colorUPM);
129 b = SkColorGetB(colorUPM);
130 a = SkColorGetA(colorUPM);
131
132 return GrColorPackRGBA(r, g, b, a);
133 }
134
135 /**
136 * Flags used for bitfields of color components. They are defined so that the bit order reflects the
137 * GrColor shift order.
138 */
139 enum GrColorComponentFlags {
140 kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
141 kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
142 kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
143 kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
144
145 kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
146 kB_GrColorComponentFlag),
147
148 kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
149 kB_GrColorComponentFlag | kA_GrColorComponentFlag)
150 };
151
GrColorComponentFlagToChar(GrColorComponentFlags component)152 static inline char GrColorComponentFlagToChar(GrColorComponentFlags component) {
153 SkASSERT(SkIsPow2(component));
154 switch (component) {
155 case kR_GrColorComponentFlag:
156 return 'r';
157 case kG_GrColorComponentFlag:
158 return 'g';
159 case kB_GrColorComponentFlag:
160 return 'b';
161 case kA_GrColorComponentFlag:
162 return 'a';
163 default:
164 SkFAIL("Invalid color component flag.");
165 return '\0';
166 }
167 }
168
GrPixelConfigComponentMask(GrPixelConfig config)169 static inline uint32_t GrPixelConfigComponentMask(GrPixelConfig config) {
170 SkASSERT(config >= 0 && config < kGrPixelConfigCnt);
171 static const uint32_t kFlags[] = {
172 0, // kUnknown_GrPixelConfig
173 kA_GrColorComponentFlag, // kAlpha_8_GrPixelConfig
174 kRGBA_GrColorComponentFlags, // kIndex_8_GrPixelConfig
175 kRGB_GrColorComponentFlags, // kRGB_565_GrPixelConfig
176 kRGBA_GrColorComponentFlags, // kRGBA_4444_GrPixelConfig
177 kRGBA_GrColorComponentFlags, // kRGBA_8888_GrPixelConfig
178 kRGBA_GrColorComponentFlags, // kBGRA_8888_GrPixelConfig
179 kRGBA_GrColorComponentFlags, // kSRGBA_8888_GrPixelConfig
180 kRGB_GrColorComponentFlags, // kETC1_GrPixelConfig
181 kA_GrColorComponentFlag, // kLATC_GrPixelConfig
182 kA_GrColorComponentFlag, // kR11_EAC_GrPixelConfig
183 kRGBA_GrColorComponentFlags, // kASTC_12x12_GrPixelConfig
184 kRGBA_GrColorComponentFlags, // kRGBA_float_GrPixelConfig
185 kA_GrColorComponentFlag, // kAlpha_16_GrPixelConfig
186 };
187 return kFlags[config];
188
189 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
190 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
191 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
192 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
193 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
194 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
195 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
196 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
197 GR_STATIC_ASSERT(8 == kETC1_GrPixelConfig);
198 GR_STATIC_ASSERT(9 == kLATC_GrPixelConfig);
199 GR_STATIC_ASSERT(10 == kR11_EAC_GrPixelConfig);
200 GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
201 GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
202 GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
203 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFlags) == kGrPixelConfigCnt);
204 }
205
206 #endif
207