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) & ~0xFFu))
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 * scale is [0..256], unlike SkFourByteInterp which takes [0..255]
162 */
SkFourByteInterp256(SkPMColor src,SkPMColor dst,int scale)163 static inline SkPMColor SkFourByteInterp256(SkPMColor src, SkPMColor dst, int scale) {
164 unsigned a = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedA32(src), SkGetPackedA32(dst), scale));
165 unsigned r = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedR32(src), SkGetPackedR32(dst), scale));
166 unsigned g = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedG32(src), SkGetPackedG32(dst), scale));
167 unsigned b = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedB32(src), SkGetPackedB32(dst), scale));
168
169 return SkPackARGB32(a, r, g, b);
170 }
171
172 /**
173 * Abstract 4-byte interpolation, implemented on top of SkPMColor
174 * utility functions. Third parameter controls blending of the first two:
175 * (src, dst, 0) returns dst
176 * (src, dst, 0xFF) returns src
177 */
SkFourByteInterp(SkPMColor src,SkPMColor dst,U8CPU srcWeight)178 static inline SkPMColor SkFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight) {
179 int scale = (int)SkAlpha255To256(srcWeight);
180 return SkFourByteInterp256(src, dst, scale);
181 }
182
183 /**
184 * 0xAARRGGBB -> 0x00AA00GG, 0x00RR00BB
185 */
SkSplay(uint32_t color,uint32_t * ag,uint32_t * rb)186 static inline void SkSplay(uint32_t color, uint32_t* ag, uint32_t* rb) {
187 const uint32_t mask = 0x00FF00FF;
188 *ag = (color >> 8) & mask;
189 *rb = color & mask;
190 }
191
192 /**
193 * 0xAARRGGBB -> 0x00AA00GG00RR00BB
194 * (note, ARGB -> AGRB)
195 */
SkSplay(uint32_t color)196 static inline uint64_t SkSplay(uint32_t color) {
197 const uint32_t mask = 0x00FF00FF;
198 uint64_t agrb = (color >> 8) & mask; // 0x0000000000AA00GG
199 agrb <<= 32; // 0x00AA00GG00000000
200 agrb |= color & mask; // 0x00AA00GG00RR00BB
201 return agrb;
202 }
203
204 /**
205 * 0xAAxxGGxx, 0xRRxxBBxx-> 0xAARRGGBB
206 */
SkUnsplay(uint32_t ag,uint32_t rb)207 static inline uint32_t SkUnsplay(uint32_t ag, uint32_t rb) {
208 const uint32_t mask = 0xFF00FF00;
209 return (ag & mask) | ((rb & mask) >> 8);
210 }
211
212 /**
213 * 0xAAxxGGxxRRxxBBxx -> 0xAARRGGBB
214 * (note, AGRB -> ARGB)
215 */
SkUnsplay(uint64_t agrb)216 static inline uint32_t SkUnsplay(uint64_t agrb) {
217 const uint32_t mask = 0xFF00FF00;
218 return SkPMColor(
219 ((agrb & mask) >> 8) | // 0x00RR00BB
220 ((agrb >> 32) & mask)); // 0xAARRGGBB
221 }
222
SkFastFourByteInterp256_32(SkPMColor src,SkPMColor dst,unsigned scale)223 static inline SkPMColor SkFastFourByteInterp256_32(SkPMColor src, SkPMColor dst, unsigned scale) {
224 SkASSERT(scale <= 256);
225
226 // Two 8-bit blends per two 32-bit registers, with space to make sure the math doesn't collide.
227 uint32_t src_ag, src_rb, dst_ag, dst_rb;
228 SkSplay(src, &src_ag, &src_rb);
229 SkSplay(dst, &dst_ag, &dst_rb);
230
231 const uint32_t ret_ag = src_ag * scale + (256 - scale) * dst_ag;
232 const uint32_t ret_rb = src_rb * scale + (256 - scale) * dst_rb;
233
234 return SkUnsplay(ret_ag, ret_rb);
235 }
236
SkFastFourByteInterp256_64(SkPMColor src,SkPMColor dst,unsigned scale)237 static inline SkPMColor SkFastFourByteInterp256_64(SkPMColor src, SkPMColor dst, unsigned scale) {
238 SkASSERT(scale <= 256);
239 // Four 8-bit blends in one 64-bit register, with space to make sure the math doesn't collide.
240 return SkUnsplay(SkSplay(src) * scale + (256-scale) * SkSplay(dst));
241 }
242
243 // TODO(mtklein): Replace slow versions with fast versions, using scale + (scale>>7) everywhere.
244
245 /**
246 * Same as SkFourByteInterp256, but faster.
247 */
SkFastFourByteInterp256(SkPMColor src,SkPMColor dst,unsigned scale)248 static inline SkPMColor SkFastFourByteInterp256(SkPMColor src, SkPMColor dst, unsigned scale) {
249 // On a 64-bit machine, _64 is about 10% faster than _32, but ~40% slower on a 32-bit machine.
250 if (sizeof(void*) == 4) {
251 return SkFastFourByteInterp256_32(src, dst, scale);
252 } else {
253 return SkFastFourByteInterp256_64(src, dst, scale);
254 }
255 }
256
257 /**
258 * Nearly the same as SkFourByteInterp, but faster and a touch more accurate, due to better
259 * srcWeight scaling to [0, 256].
260 */
SkFastFourByteInterp(SkPMColor src,SkPMColor dst,U8CPU srcWeight)261 static inline SkPMColor SkFastFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight) {
262 SkASSERT(srcWeight <= 255);
263 // scale = srcWeight + (srcWeight >> 7) is more accurate than
264 // scale = srcWeight + 1, but 7% slower
265 return SkFastFourByteInterp256(src, dst, srcWeight + (srcWeight >> 7));
266 }
267
268 /**
269 * Interpolates between colors src and dst using [0,256] scale.
270 */
SkPMLerp(SkPMColor src,SkPMColor dst,unsigned scale)271 static inline SkPMColor SkPMLerp(SkPMColor src, SkPMColor dst, unsigned scale) {
272 return SkFastFourByteInterp256(src, dst, scale);
273 }
274
SkBlendARGB32(SkPMColor src,SkPMColor dst,U8CPU aa)275 static inline SkPMColor SkBlendARGB32(SkPMColor src, SkPMColor dst, U8CPU aa) {
276 SkASSERT((unsigned)aa <= 255);
277
278 unsigned src_scale = SkAlpha255To256(aa);
279 unsigned dst_scale = SkAlphaMulInv256(SkGetPackedA32(src), src_scale);
280
281 const uint32_t mask = 0xFF00FF;
282
283 uint32_t src_rb = (src & mask) * src_scale;
284 uint32_t src_ag = ((src >> 8) & mask) * src_scale;
285
286 uint32_t dst_rb = (dst & mask) * dst_scale;
287 uint32_t dst_ag = ((dst >> 8) & mask) * dst_scale;
288
289 return (((src_rb + dst_rb) >> 8) & mask) | ((src_ag + dst_ag) & ~mask);
290 }
291
292 ////////////////////////////////////////////////////////////////////////////////////////////
293 // Convert a 32bit pixel to a 16bit pixel (no dither)
294
295 #define SkR32ToR16_MACRO(r) ((unsigned)(r) >> (SK_R32_BITS - SK_R16_BITS))
296 #define SkG32ToG16_MACRO(g) ((unsigned)(g) >> (SK_G32_BITS - SK_G16_BITS))
297 #define SkB32ToB16_MACRO(b) ((unsigned)(b) >> (SK_B32_BITS - SK_B16_BITS))
298
299 #ifdef SK_DEBUG
SkR32ToR16(unsigned r)300 static inline unsigned SkR32ToR16(unsigned r) {
301 SkR32Assert(r);
302 return SkR32ToR16_MACRO(r);
303 }
SkG32ToG16(unsigned g)304 static inline unsigned SkG32ToG16(unsigned g) {
305 SkG32Assert(g);
306 return SkG32ToG16_MACRO(g);
307 }
SkB32ToB16(unsigned b)308 static inline unsigned SkB32ToB16(unsigned b) {
309 SkB32Assert(b);
310 return SkB32ToB16_MACRO(b);
311 }
312 #else
313 #define SkR32ToR16(r) SkR32ToR16_MACRO(r)
314 #define SkG32ToG16(g) SkG32ToG16_MACRO(g)
315 #define SkB32ToB16(b) SkB32ToB16_MACRO(b)
316 #endif
317
SkPixel32ToPixel16(SkPMColor c)318 static inline U16CPU SkPixel32ToPixel16(SkPMColor c) {
319 unsigned r = ((c >> (SK_R32_SHIFT + (8 - SK_R16_BITS))) & SK_R16_MASK) << SK_R16_SHIFT;
320 unsigned g = ((c >> (SK_G32_SHIFT + (8 - SK_G16_BITS))) & SK_G16_MASK) << SK_G16_SHIFT;
321 unsigned b = ((c >> (SK_B32_SHIFT + (8 - SK_B16_BITS))) & SK_B16_MASK) << SK_B16_SHIFT;
322 return r | g | b;
323 }
324
SkPack888ToRGB16(U8CPU r,U8CPU g,U8CPU b)325 static inline U16CPU SkPack888ToRGB16(U8CPU r, U8CPU g, U8CPU b) {
326 return (SkR32ToR16(r) << SK_R16_SHIFT) |
327 (SkG32ToG16(g) << SK_G16_SHIFT) |
328 (SkB32ToB16(b) << SK_B16_SHIFT);
329 }
330
331 /////////////////////////////////////////////////////////////////////////////////////////
332
333 /* SrcOver the 32bit src color with the 16bit dst, returning a 16bit value
334 (with dirt in the high 16bits, so caller beware).
335 */
SkSrcOver32To16(SkPMColor src,uint16_t dst)336 static inline U16CPU SkSrcOver32To16(SkPMColor src, uint16_t dst) {
337 unsigned sr = SkGetPackedR32(src);
338 unsigned sg = SkGetPackedG32(src);
339 unsigned sb = SkGetPackedB32(src);
340
341 unsigned dr = SkGetPackedR16(dst);
342 unsigned dg = SkGetPackedG16(dst);
343 unsigned db = SkGetPackedB16(dst);
344
345 unsigned isa = 255 - SkGetPackedA32(src);
346
347 dr = (sr + SkMul16ShiftRound(dr, isa, SK_R16_BITS)) >> (8 - SK_R16_BITS);
348 dg = (sg + SkMul16ShiftRound(dg, isa, SK_G16_BITS)) >> (8 - SK_G16_BITS);
349 db = (sb + SkMul16ShiftRound(db, isa, SK_B16_BITS)) >> (8 - SK_B16_BITS);
350
351 return SkPackRGB16(dr, dg, db);
352 }
353
SkPixel16ToColor(U16CPU src)354 static inline SkColor SkPixel16ToColor(U16CPU src) {
355 SkASSERT(src == SkToU16(src));
356
357 unsigned r = SkPacked16ToR32(src);
358 unsigned g = SkPacked16ToG32(src);
359 unsigned b = SkPacked16ToB32(src);
360
361 SkASSERT((r >> (8 - SK_R16_BITS)) == SkGetPackedR16(src));
362 SkASSERT((g >> (8 - SK_G16_BITS)) == SkGetPackedG16(src));
363 SkASSERT((b >> (8 - SK_B16_BITS)) == SkGetPackedB16(src));
364
365 return SkColorSetRGB(r, g, b);
366 }
367
368 ///////////////////////////////////////////////////////////////////////////////
369
370 typedef uint16_t SkPMColor16;
371
372 // Put in OpenGL order (r g b a)
373 #define SK_A4444_SHIFT 0
374 #define SK_R4444_SHIFT 12
375 #define SK_G4444_SHIFT 8
376 #define SK_B4444_SHIFT 4
377
SkReplicateNibble(unsigned nib)378 static inline U8CPU SkReplicateNibble(unsigned nib) {
379 SkASSERT(nib <= 0xF);
380 return (nib << 4) | nib;
381 }
382
383 #define SkGetPackedA4444(c) (((unsigned)(c) >> SK_A4444_SHIFT) & 0xF)
384 #define SkGetPackedR4444(c) (((unsigned)(c) >> SK_R4444_SHIFT) & 0xF)
385 #define SkGetPackedG4444(c) (((unsigned)(c) >> SK_G4444_SHIFT) & 0xF)
386 #define SkGetPackedB4444(c) (((unsigned)(c) >> SK_B4444_SHIFT) & 0xF)
387
388 #define SkPacked4444ToA32(c) SkReplicateNibble(SkGetPackedA4444(c))
389
SkPixel4444ToPixel32(U16CPU c)390 static inline SkPMColor SkPixel4444ToPixel32(U16CPU c) {
391 uint32_t d = (SkGetPackedA4444(c) << SK_A32_SHIFT) |
392 (SkGetPackedR4444(c) << SK_R32_SHIFT) |
393 (SkGetPackedG4444(c) << SK_G32_SHIFT) |
394 (SkGetPackedB4444(c) << SK_B32_SHIFT);
395 return d | (d << 4);
396 }
397
swizzle_rb(const Sk4f & x)398 static inline Sk4f swizzle_rb(const Sk4f& x) {
399 return SkNx_shuffle<2, 1, 0, 3>(x);
400 }
401
swizzle_rb_if_bgra(const Sk4f & x)402 static inline Sk4f swizzle_rb_if_bgra(const Sk4f& x) {
403 #ifdef SK_PMCOLOR_IS_BGRA
404 return swizzle_rb(x);
405 #else
406 return x;
407 #endif
408 }
409
Sk4f_fromL32(uint32_t px)410 static inline Sk4f Sk4f_fromL32(uint32_t px) {
411 return SkNx_cast<float>(Sk4b::Load(&px)) * (1 / 255.0f);
412 }
413
Sk4f_toL32(const Sk4f & px)414 static inline uint32_t Sk4f_toL32(const Sk4f& px) {
415 Sk4f v = px;
416
417 #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
418 // SkNx_cast<uint8_t, int32_t>() pins, and we don't anticipate giant floats
419 #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON)
420 // SkNx_cast<uint8_t, int32_t>() pins, and so does Sk4f_round().
421 #else
422 // No guarantee of a pin.
423 v = Sk4f::Max(0, Sk4f::Min(v, 1));
424 #endif
425
426 uint32_t l32;
427 SkNx_cast<uint8_t>(Sk4f_round(v * 255.0f)).store(&l32);
428 return l32;
429 }
430
431 using SkPMColor4f = SkRGBA4f<kPremul_SkAlphaType>;
432
433 constexpr SkPMColor4f SK_PMColor4fTRANSPARENT = { 0, 0, 0, 0 };
434 constexpr SkPMColor4f SK_PMColor4fBLACK = { 0, 0, 0, 1 };
435 constexpr SkPMColor4f SK_PMColor4fWHITE = { 1, 1, 1, 1 };
436 constexpr SkPMColor4f SK_PMColor4fILLEGAL = { SK_FloatNegativeInfinity,
437 SK_FloatNegativeInfinity,
438 SK_FloatNegativeInfinity,
439 SK_FloatNegativeInfinity };
440
441 #endif
442