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