Flutter Engine
The Flutter Engine
SkColorData.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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 SkColorData_DEFINED
9#define SkColorData_DEFINED
10
14
15////////////////////////////////////////////////////////////////////////////////////////////
16// Convert a 16bit pixel to a 32bit pixel
17
18#define SK_R16_BITS 5
19#define SK_G16_BITS 6
20#define SK_B16_BITS 5
21
22#define SK_R16_SHIFT (SK_B16_BITS + SK_G16_BITS)
23#define SK_G16_SHIFT (SK_B16_BITS)
24#define SK_B16_SHIFT 0
25
26#define SK_R16_MASK ((1 << SK_R16_BITS) - 1)
27#define SK_G16_MASK ((1 << SK_G16_BITS) - 1)
28#define SK_B16_MASK ((1 << SK_B16_BITS) - 1)
29
30#define SkGetPackedR16(color) (((unsigned)(color) >> SK_R16_SHIFT) & SK_R16_MASK)
31#define SkGetPackedG16(color) (((unsigned)(color) >> SK_G16_SHIFT) & SK_G16_MASK)
32#define SkGetPackedB16(color) (((unsigned)(color) >> SK_B16_SHIFT) & SK_B16_MASK)
33
34static inline unsigned SkR16ToR32(unsigned r) {
35 return (r << (8 - SK_R16_BITS)) | (r >> (2 * SK_R16_BITS - 8));
36}
37
38static inline unsigned SkG16ToG32(unsigned g) {
39 return (g << (8 - SK_G16_BITS)) | (g >> (2 * SK_G16_BITS - 8));
40}
41
42static inline unsigned SkB16ToB32(unsigned b) {
43 return (b << (8 - SK_B16_BITS)) | (b >> (2 * SK_B16_BITS - 8));
44}
45
46#define SkPacked16ToR32(c) SkR16ToR32(SkGetPackedR16(c))
47#define SkPacked16ToG32(c) SkG16ToG32(SkGetPackedG16(c))
48#define SkPacked16ToB32(c) SkB16ToB32(SkGetPackedB16(c))
49
50//////////////////////////////////////////////////////////////////////////////
51
52#define SkASSERT_IS_BYTE(x) SkASSERT(0 == ((x) & ~0xFFu))
53
54// Reverse the bytes coorsponding to RED and BLUE in a packed pixels. Note the
55// pair of them are in the same 2 slots in both RGBA and BGRA, thus there is
56// no need to pass in the colortype to this function.
57static inline uint32_t SkSwizzle_RB(uint32_t c) {
58 static const uint32_t kRBMask = (0xFF << SK_R32_SHIFT) | (0xFF << SK_B32_SHIFT);
59
60 unsigned c0 = (c >> SK_R32_SHIFT) & 0xFF;
61 unsigned c1 = (c >> SK_B32_SHIFT) & 0xFF;
62 return (c & ~kRBMask) | (c0 << SK_B32_SHIFT) | (c1 << SK_R32_SHIFT);
63}
64
65static inline uint32_t SkPackARGB_as_RGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
70 return (a << SK_RGBA_A32_SHIFT) | (r << SK_RGBA_R32_SHIFT) |
72}
73
74static inline uint32_t SkPackARGB_as_BGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
79 return (a << SK_BGRA_A32_SHIFT) | (r << SK_BGRA_R32_SHIFT) |
81}
82
83static inline SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c) {
84#ifdef SK_PMCOLOR_IS_RGBA
85 return c;
86#else
87 return SkSwizzle_RB(c);
88#endif
89}
90
91static inline SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c) {
92#ifdef SK_PMCOLOR_IS_BGRA
93 return c;
94#else
95 return SkSwizzle_RB(c);
96#endif
97}
98
99//////////////////////////////////////////////////////////////////////////////
100
101///@{
102/** See ITU-R Recommendation BT.709 at http://www.itu.int/rec/R-REC-BT.709/ .*/
103#define SK_ITU_BT709_LUM_COEFF_R (0.2126f)
104#define SK_ITU_BT709_LUM_COEFF_G (0.7152f)
105#define SK_ITU_BT709_LUM_COEFF_B (0.0722f)
106///@}
107
108///@{
109/** A float value which specifies this channel's contribution to luminance. */
110#define SK_LUM_COEFF_R SK_ITU_BT709_LUM_COEFF_R
111#define SK_LUM_COEFF_G SK_ITU_BT709_LUM_COEFF_G
112#define SK_LUM_COEFF_B SK_ITU_BT709_LUM_COEFF_B
113///@}
114
115/** Computes the luminance from the given r, g, and b in accordance with
116 SK_LUM_COEFF_X. For correct results, r, g, and b should be in linear space.
117*/
119 //The following is
120 //r * SK_LUM_COEFF_R + g * SK_LUM_COEFF_G + b * SK_LUM_COEFF_B
121 //with SK_LUM_COEFF_X in 1.8 fixed point (rounding adjusted to sum to 256).
122 return (r * 54 + g * 183 + b * 19) >> 8;
123}
124
125/** Calculates 256 - (value * alpha256) / 255 in range [0,256],
126 * for [0,255] value and [0,256] alpha256.
127 */
128static inline U16CPU SkAlphaMulInv256(U16CPU value, U16CPU alpha256) {
129 unsigned prod = 0xFFFF - value * alpha256;
130 return (prod + (prod >> 8)) >> 8;
131}
132
133// The caller may want negative values, so keep all params signed (int)
134// so we don't accidentally slip into unsigned math and lose the sign
135// extension when we shift (in SkAlphaMul)
136static inline int SkAlphaBlend(int src, int dst, int scale256) {
137 SkASSERT((unsigned)scale256 <= 256);
138 return dst + SkAlphaMul(src - dst, scale256);
139}
140
141static inline uint16_t SkPackRGB16(unsigned r, unsigned g, unsigned b) {
142 SkASSERT(r <= SK_R16_MASK);
143 SkASSERT(g <= SK_G16_MASK);
145
146 return SkToU16((r << SK_R16_SHIFT) | (g << SK_G16_SHIFT) | (b << SK_B16_SHIFT));
147}
148
149#define SK_R16_MASK_IN_PLACE (SK_R16_MASK << SK_R16_SHIFT)
150#define SK_G16_MASK_IN_PLACE (SK_G16_MASK << SK_G16_SHIFT)
151#define SK_B16_MASK_IN_PLACE (SK_B16_MASK << SK_B16_SHIFT)
152
153///////////////////////////////////////////////////////////////////////////////
154
155/**
156 * Abstract 4-byte interpolation, implemented on top of SkPMColor
157 * utility functions. Third parameter controls blending of the first two:
158 * (src, dst, 0) returns dst
159 * (src, dst, 0xFF) returns src
160 * scale is [0..256], unlike SkFourByteInterp which takes [0..255]
161 */
163 unsigned a = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedA32(src), SkGetPackedA32(dst), scale));
164 unsigned r = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedR32(src), SkGetPackedR32(dst), scale));
165 unsigned g = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedG32(src), SkGetPackedG32(dst), scale));
166 unsigned b = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedB32(src), SkGetPackedB32(dst), scale));
167
168 return SkPackARGB32(a, r, g, b);
169}
170
171/**
172 * Abstract 4-byte interpolation, implemented on top of SkPMColor
173 * utility functions. Third parameter controls blending of the first two:
174 * (src, dst, 0) returns dst
175 * (src, dst, 0xFF) returns src
176 */
178 int scale = (int)SkAlpha255To256(srcWeight);
180}
181
182/**
183 * 0xAARRGGBB -> 0x00AA00GG, 0x00RR00BB
184 */
185static inline void SkSplay(uint32_t color, uint32_t* ag, uint32_t* rb) {
186 const uint32_t mask = 0x00FF00FF;
187 *ag = (color >> 8) & mask;
188 *rb = color & mask;
189}
190
191/**
192 * 0xAARRGGBB -> 0x00AA00GG00RR00BB
193 * (note, ARGB -> AGRB)
194 */
195static inline uint64_t SkSplay(uint32_t color) {
196 const uint32_t mask = 0x00FF00FF;
197 uint64_t agrb = (color >> 8) & mask; // 0x0000000000AA00GG
198 agrb <<= 32; // 0x00AA00GG00000000
199 agrb |= color & mask; // 0x00AA00GG00RR00BB
200 return agrb;
201}
202
203/**
204 * 0xAAxxGGxx, 0xRRxxBBxx-> 0xAARRGGBB
205 */
206static inline uint32_t SkUnsplay(uint32_t ag, uint32_t rb) {
207 const uint32_t mask = 0xFF00FF00;
208 return (ag & mask) | ((rb & mask) >> 8);
209}
210
211/**
212 * 0xAAxxGGxxRRxxBBxx -> 0xAARRGGBB
213 * (note, AGRB -> ARGB)
214 */
215static inline uint32_t SkUnsplay(uint64_t agrb) {
216 const uint32_t mask = 0xFF00FF00;
217 return SkPMColor(
218 ((agrb & mask) >> 8) | // 0x00RR00BB
219 ((agrb >> 32) & mask)); // 0xAARRGGBB
220}
221
223 SkASSERT(scale <= 256);
224
225 // Two 8-bit blends per two 32-bit registers, with space to make sure the math doesn't collide.
226 uint32_t src_ag, src_rb, dst_ag, dst_rb;
227 SkSplay(src, &src_ag, &src_rb);
228 SkSplay(dst, &dst_ag, &dst_rb);
229
230 const uint32_t ret_ag = src_ag * scale + (256 - scale) * dst_ag;
231 const uint32_t ret_rb = src_rb * scale + (256 - scale) * dst_rb;
232
233 return SkUnsplay(ret_ag, ret_rb);
234}
235
237 SkASSERT(scale <= 256);
238 // Four 8-bit blends in one 64-bit register, with space to make sure the math doesn't collide.
239 return SkUnsplay(SkSplay(src) * scale + (256-scale) * SkSplay(dst));
240}
241
242// TODO(mtklein): Replace slow versions with fast versions, using scale + (scale>>7) everywhere.
243
244/**
245 * Same as SkFourByteInterp256, but faster.
246 */
248 // On a 64-bit machine, _64 is about 10% faster than _32, but ~40% slower on a 32-bit machine.
249 if (sizeof(void*) == 4) {
251 } else {
253 }
254}
255
256/**
257 * Nearly the same as SkFourByteInterp, but faster and a touch more accurate, due to better
258 * srcWeight scaling to [0, 256].
259 */
261 SkASSERT(srcWeight <= 255);
262 // scale = srcWeight + (srcWeight >> 7) is more accurate than
263 // scale = srcWeight + 1, but 7% slower
264 return SkFastFourByteInterp256(src, dst, srcWeight + (srcWeight >> 7));
265}
266
267/**
268 * Interpolates between colors src and dst using [0,256] scale.
269 */
270static inline SkPMColor SkPMLerp(SkPMColor src, SkPMColor dst, unsigned scale) {
272}
273
275 SkASSERT((unsigned)aa <= 255);
276
277 unsigned src_scale = SkAlpha255To256(aa);
278 unsigned dst_scale = SkAlphaMulInv256(SkGetPackedA32(src), src_scale);
279
280 const uint32_t mask = 0xFF00FF;
281
282 uint32_t src_rb = (src & mask) * src_scale;
283 uint32_t src_ag = ((src >> 8) & mask) * src_scale;
284
285 uint32_t dst_rb = (dst & mask) * dst_scale;
286 uint32_t dst_ag = ((dst >> 8) & mask) * dst_scale;
287
288 return (((src_rb + dst_rb) >> 8) & mask) | ((src_ag + dst_ag) & ~mask);
289}
290
291////////////////////////////////////////////////////////////////////////////////////////////
292// Convert a 32bit pixel to a 16bit pixel (no dither)
293
294#define SkR32ToR16_MACRO(r) ((unsigned)(r) >> (SK_R32_BITS - SK_R16_BITS))
295#define SkG32ToG16_MACRO(g) ((unsigned)(g) >> (SK_G32_BITS - SK_G16_BITS))
296#define SkB32ToB16_MACRO(b) ((unsigned)(b) >> (SK_B32_BITS - SK_B16_BITS))
297
298#ifdef SK_DEBUG
299 static inline unsigned SkR32ToR16(unsigned r) {
300 SkR32Assert(r);
301 return SkR32ToR16_MACRO(r);
302 }
303 static inline unsigned SkG32ToG16(unsigned g) {
304 SkG32Assert(g);
305 return SkG32ToG16_MACRO(g);
306 }
307 static inline unsigned SkB32ToB16(unsigned b) {
308 SkB32Assert(b);
309 return SkB32ToB16_MACRO(b);
310 }
311#else
312 #define SkR32ToR16(r) SkR32ToR16_MACRO(r)
313 #define SkG32ToG16(g) SkG32ToG16_MACRO(g)
314 #define SkB32ToB16(b) SkB32ToB16_MACRO(b)
315#endif
316
318 unsigned r = ((c >> (SK_R32_SHIFT + (8 - SK_R16_BITS))) & SK_R16_MASK) << SK_R16_SHIFT;
319 unsigned g = ((c >> (SK_G32_SHIFT + (8 - SK_G16_BITS))) & SK_G16_MASK) << SK_G16_SHIFT;
320 unsigned b = ((c >> (SK_B32_SHIFT + (8 - SK_B16_BITS))) & SK_B16_MASK) << SK_B16_SHIFT;
321 return r | g | b;
322}
323
324static inline U16CPU SkPack888ToRGB16(U8CPU r, U8CPU g, U8CPU b) {
325 return (SkR32ToR16(r) << SK_R16_SHIFT) |
326 (SkG32ToG16(g) << SK_G16_SHIFT) |
328}
329
330/////////////////////////////////////////////////////////////////////////////////////////
331
333 SkASSERT(src == SkToU16(src));
334
335 unsigned r = SkPacked16ToR32(src);
336 unsigned g = SkPacked16ToG32(src);
337 unsigned b = SkPacked16ToB32(src);
338
339 SkASSERT((r >> (8 - SK_R16_BITS)) == SkGetPackedR16(src));
340 SkASSERT((g >> (8 - SK_G16_BITS)) == SkGetPackedG16(src));
341 SkASSERT((b >> (8 - SK_B16_BITS)) == SkGetPackedB16(src));
342
343 return SkColorSetRGB(r, g, b);
344}
345
346///////////////////////////////////////////////////////////////////////////////
347
348typedef uint16_t SkPMColor16;
349
350// Put in OpenGL order (r g b a)
351#define SK_A4444_SHIFT 0
352#define SK_R4444_SHIFT 12
353#define SK_G4444_SHIFT 8
354#define SK_B4444_SHIFT 4
355
356static inline U8CPU SkReplicateNibble(unsigned nib) {
357 SkASSERT(nib <= 0xF);
358 return (nib << 4) | nib;
359}
360
361#define SkGetPackedA4444(c) (((unsigned)(c) >> SK_A4444_SHIFT) & 0xF)
362#define SkGetPackedR4444(c) (((unsigned)(c) >> SK_R4444_SHIFT) & 0xF)
363#define SkGetPackedG4444(c) (((unsigned)(c) >> SK_G4444_SHIFT) & 0xF)
364#define SkGetPackedB4444(c) (((unsigned)(c) >> SK_B4444_SHIFT) & 0xF)
365
366#define SkPacked4444ToA32(c) SkReplicateNibble(SkGetPackedA4444(c))
367
369 uint32_t d = (SkGetPackedA4444(c) << SK_A32_SHIFT) |
373 return d | (d << 4);
374}
375
377
378constexpr SkPMColor4f SK_PMColor4fTRANSPARENT = { 0, 0, 0, 0 };
379constexpr SkPMColor4f SK_PMColor4fBLACK = { 0, 0, 0, 1 };
380constexpr SkPMColor4f SK_PMColor4fWHITE = { 1, 1, 1, 1 };
385#endif // SkColorData_DEFINED
#define SkASSERT(cond)
Definition: SkAssert.h:116
unsigned U16CPU
Definition: SkCPUTypes.h:23
unsigned U8CPU
Definition: SkCPUTypes.h:18
static int SkAlphaBlend(int src, int dst, int scale256)
Definition: SkColorData.h:136
constexpr SkPMColor4f SK_PMColor4fILLEGAL
Definition: SkColorData.h:381
static SkPMColor SkPMLerp(SkPMColor src, SkPMColor dst, unsigned scale)
Definition: SkColorData.h:270
static unsigned SkB16ToB32(unsigned b)
Definition: SkColorData.h:42
static SkPMColor SkFastFourByteInterp256(SkPMColor src, SkPMColor dst, unsigned scale)
Definition: SkColorData.h:247
#define SkG32ToG16(g)
Definition: SkColorData.h:313
#define SK_R16_MASK
Definition: SkColorData.h:26
#define SkB32ToB16_MACRO(b)
Definition: SkColorData.h:296
static uint32_t SkPackARGB_as_RGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorData.h:65
#define SK_G16_MASK
Definition: SkColorData.h:27
#define SkGetPackedR4444(c)
Definition: SkColorData.h:362
#define SkGetPackedB16(color)
Definition: SkColorData.h:32
#define SkGetPackedG16(color)
Definition: SkColorData.h:31
static void SkSplay(uint32_t color, uint32_t *ag, uint32_t *rb)
Definition: SkColorData.h:185
static U8CPU SkComputeLuminance(U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorData.h:118
#define SK_G16_BITS
Definition: SkColorData.h:19
constexpr SkPMColor4f SK_PMColor4fWHITE
Definition: SkColorData.h:380
constexpr SkPMColor4f SK_PMColor4fTRANSPARENT
Definition: SkColorData.h:378
static SkPMColor SkFastFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight)
Definition: SkColorData.h:260
static uint32_t SkUnsplay(uint32_t ag, uint32_t rb)
Definition: SkColorData.h:206
static U16CPU SkPack888ToRGB16(U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorData.h:324
#define SK_G16_SHIFT
Definition: SkColorData.h:23
#define SkR32ToR16_MACRO(r)
Definition: SkColorData.h:294
static U16CPU SkPixel32ToPixel16(SkPMColor c)
Definition: SkColorData.h:317
static U8CPU SkReplicateNibble(unsigned nib)
Definition: SkColorData.h:356
uint16_t SkPMColor16
Definition: SkColorData.h:348
#define SkG32ToG16_MACRO(g)
Definition: SkColorData.h:295
static SkPMColor SkFastFourByteInterp256_64(SkPMColor src, SkPMColor dst, unsigned scale)
Definition: SkColorData.h:236
static SkPMColor SkPixel4444ToPixel32(U16CPU c)
Definition: SkColorData.h:368
static uint32_t SkSwizzle_RB(uint32_t c)
Definition: SkColorData.h:57
#define SkPacked16ToG32(c)
Definition: SkColorData.h:47
#define SK_B16_MASK
Definition: SkColorData.h:28
#define SkB32ToB16(b)
Definition: SkColorData.h:314
constexpr SkPMColor4f SK_PMColor4fBLACK
Definition: SkColorData.h:379
static uint32_t SkPackARGB_as_BGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorData.h:74
static uint16_t SkPackRGB16(unsigned r, unsigned g, unsigned b)
Definition: SkColorData.h:141
static SkPMColor SkFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight)
Definition: SkColorData.h:177
#define SkGetPackedR16(color)
Definition: SkColorData.h:30
#define SK_R16_BITS
Definition: SkColorData.h:18
static SkColor SkPixel16ToColor(U16CPU src)
Definition: SkColorData.h:332
static SkPMColor SkFourByteInterp256(SkPMColor src, SkPMColor dst, int scale)
Definition: SkColorData.h:162
#define SkPacked16ToR32(c)
Definition: SkColorData.h:46
#define SkASSERT_IS_BYTE(x)
Definition: SkColorData.h:52
#define SK_R16_SHIFT
Definition: SkColorData.h:22
static U16CPU SkAlphaMulInv256(U16CPU value, U16CPU alpha256)
Definition: SkColorData.h:128
static SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c)
Definition: SkColorData.h:83
#define SK_B16_SHIFT
Definition: SkColorData.h:24
#define SkGetPackedB4444(c)
Definition: SkColorData.h:364
static unsigned SkG16ToG32(unsigned g)
Definition: SkColorData.h:38
static SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c)
Definition: SkColorData.h:91
static SkPMColor SkFastFourByteInterp256_32(SkPMColor src, SkPMColor dst, unsigned scale)
Definition: SkColorData.h:222
#define SkR32ToR16(r)
Definition: SkColorData.h:312
#define SkGetPackedG4444(c)
Definition: SkColorData.h:363
#define SkPacked16ToB32(c)
Definition: SkColorData.h:48
static SkPMColor SkBlendARGB32(SkPMColor src, SkPMColor dst, U8CPU aa)
Definition: SkColorData.h:274
static unsigned SkR16ToR32(unsigned r)
Definition: SkColorData.h:34
#define SK_B16_BITS
Definition: SkColorData.h:20
#define SkGetPackedA4444(c)
Definition: SkColorData.h:361
#define SkB32Assert(b)
Definition: SkColorPriv.h:100
#define SK_BGRA_R32_SHIFT
Definition: SkColorPriv.h:69
#define SkGetPackedB32(packed)
Definition: SkColorPriv.h:95
#define SkGetPackedR32(packed)
Definition: SkColorPriv.h:93
#define SK_BGRA_B32_SHIFT
Definition: SkColorPriv.h:67
#define SK_BGRA_G32_SHIFT
Definition: SkColorPriv.h:68
#define SK_RGBA_A32_SHIFT
Definition: SkColorPriv.h:65
#define SkAlphaMul(value, alpha256)
Definition: SkColorPriv.h:34
#define SK_RGBA_G32_SHIFT
Definition: SkColorPriv.h:63
#define SkG32Assert(g)
Definition: SkColorPriv.h:99
#define SkGetPackedA32(packed)
Definition: SkColorPriv.h:92
#define SkGetPackedG32(packed)
Definition: SkColorPriv.h:94
#define SK_RGBA_B32_SHIFT
Definition: SkColorPriv.h:64
#define SkR32Assert(r)
Definition: SkColorPriv.h:98
#define SK_BGRA_A32_SHIFT
Definition: SkColorPriv.h:70
#define SK_RGBA_R32_SHIFT
Definition: SkColorPriv.h:62
static unsigned SkAlpha255To256(U8CPU alpha)
Definition: SkColorPriv.h:24
static SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorPriv.h:106
uint32_t SkColor
Definition: SkColor.h:37
#define SkColorSetRGB(r, g, b)
Definition: SkColor.h:57
uint32_t SkPMColor
Definition: SkColor.h:205
constexpr float SK_FloatNegativeInfinity
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
#define SK_R32_SHIFT
Definition: SkTypes.h:44
#define SK_A32_SHIFT
Definition: SkTypes.h:54
#define SK_B32_SHIFT
Definition: SkTypes.h:50
#define SK_G32_SHIFT
Definition: SkTypes.h:53
DlColor color
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
struct MyStruct a[10]
uint8_t value
dst
Definition: cp.py:12
const Scalar scale