Skia
2D Graphics Library
SkColorPriv.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 SkColorPriv_DEFINED
9 #define SkColorPriv_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMath.h"
13 #include "include/private/SkTPin.h"
14 #include "include/private/SkTo.h"
15 
22 static inline unsigned SkAlpha255To256(U8CPU alpha) {
23  SkASSERT(SkToU8(alpha) == alpha);
24  // this one assues that blending on top of an opaque dst keeps it that way
25  // even though it is less accurate than a+(a>>7) for non-opaque dsts
26  return alpha + 1;
27 }
28 
32 #define SkAlphaMul(value, alpha256) (((value) * (alpha256)) >> 8)
33 
35  return static_cast<U8CPU>(SkTPin(x, 0.0f, 1.0f) * 255 + 0.5);
36 }
37 
38 #define SK_A32_BITS 8
39 #define SK_R32_BITS 8
40 #define SK_G32_BITS 8
41 #define SK_B32_BITS 8
42 
43 #define SK_A32_MASK ((1 << SK_A32_BITS) - 1)
44 #define SK_R32_MASK ((1 << SK_R32_BITS) - 1)
45 #define SK_G32_MASK ((1 << SK_G32_BITS) - 1)
46 #define SK_B32_MASK ((1 << SK_B32_BITS) - 1)
47 
48 /*
49  * Skia's 32bit backend only supports 1 swizzle order at a time (compile-time).
50  * This is specified by SK_R32_SHIFT=0 or SK_R32_SHIFT=16.
51  *
52  * For easier compatibility with Skia's GPU backend, we further restrict these
53  * to either (in memory-byte-order) RGBA or BGRA. Note that this "order" does
54  * not directly correspond to the same shift-order, since we have to take endianess
55  * into account.
56  *
57  * Here we enforce this constraint.
58  */
59 
60 #define SK_RGBA_R32_SHIFT 0
61 #define SK_RGBA_G32_SHIFT 8
62 #define SK_RGBA_B32_SHIFT 16
63 #define SK_RGBA_A32_SHIFT 24
64 
65 #define SK_BGRA_B32_SHIFT 0
66 #define SK_BGRA_G32_SHIFT 8
67 #define SK_BGRA_R32_SHIFT 16
68 #define SK_BGRA_A32_SHIFT 24
69 
70 #if defined(SK_PMCOLOR_IS_RGBA) || defined(SK_PMCOLOR_IS_BGRA)
71  #error "Configure PMCOLOR by setting SK_R32_SHIFT."
72 #endif
73 
74 // Deduce which SK_PMCOLOR_IS_ to define from the _SHIFT defines
75 
76 #if (SK_A32_SHIFT == SK_RGBA_A32_SHIFT && \
77  SK_R32_SHIFT == SK_RGBA_R32_SHIFT && \
78  SK_G32_SHIFT == SK_RGBA_G32_SHIFT && \
79  SK_B32_SHIFT == SK_RGBA_B32_SHIFT)
80  #define SK_PMCOLOR_IS_RGBA
81 #elif (SK_A32_SHIFT == SK_BGRA_A32_SHIFT && \
82  SK_R32_SHIFT == SK_BGRA_R32_SHIFT && \
83  SK_G32_SHIFT == SK_BGRA_G32_SHIFT && \
84  SK_B32_SHIFT == SK_BGRA_B32_SHIFT)
85  #define SK_PMCOLOR_IS_BGRA
86 #else
87  #error "need 32bit packing to be either RGBA or BGRA"
88 #endif
89 
90 #define SkGetPackedA32(packed) ((uint32_t)((packed) << (24 - SK_A32_SHIFT)) >> 24)
91 #define SkGetPackedR32(packed) ((uint32_t)((packed) << (24 - SK_R32_SHIFT)) >> 24)
92 #define SkGetPackedG32(packed) ((uint32_t)((packed) << (24 - SK_G32_SHIFT)) >> 24)
93 #define SkGetPackedB32(packed) ((uint32_t)((packed) << (24 - SK_B32_SHIFT)) >> 24)
94 
95 #define SkA32Assert(a) SkASSERT((unsigned)(a) <= SK_A32_MASK)
96 #define SkR32Assert(r) SkASSERT((unsigned)(r) <= SK_R32_MASK)
97 #define SkG32Assert(g) SkASSERT((unsigned)(g) <= SK_G32_MASK)
98 #define SkB32Assert(b) SkASSERT((unsigned)(b) <= SK_B32_MASK)
99 
104 static inline SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
105  SkA32Assert(a);
106  SkASSERT(r <= a);
107  SkASSERT(g <= a);
108  SkASSERT(b <= a);
109 
110  return (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
111  (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
112 }
113 
118 static inline SkPMColor SkPackARGB32NoCheck(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
119  return (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
120  (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
121 }
122 
123 static inline
125  SkA32Assert(a);
126  SkR32Assert(r);
127  SkG32Assert(g);
128  SkB32Assert(b);
129 
130  if (a != 255) {
131  r = SkMulDiv255Round(r, a);
132  g = SkMulDiv255Round(g, a);
133  b = SkMulDiv255Round(b, a);
134  }
135  return SkPackARGB32(a, r, g, b);
136 }
137 
138 // When Android is compiled optimizing for size, SkAlphaMulQ doesn't get
139 // inlined; forcing inlining significantly improves performance.
140 static SK_ALWAYS_INLINE uint32_t SkAlphaMulQ(uint32_t c, unsigned scale) {
141  uint32_t mask = 0xFF00FF;
142 
143  uint32_t rb = ((c & mask) * scale) >> 8;
144  uint32_t ag = ((c >> 8) & mask) * scale;
145  return (rb & mask) | (ag & ~mask);
146 }
147 
148 static inline SkPMColor SkPMSrcOver(SkPMColor src, SkPMColor dst) {
149  return src + SkAlphaMulQ(dst, SkAlpha255To256(255 - SkGetPackedA32(src)));
150 }
151 
152 #endif
SK_G32_SHIFT
#define SK_G32_SHIFT
Definition: SkTypes.h:300
SkB32Assert
#define SkB32Assert(b)
Definition: SkColorPriv.h:98
SkG32Assert
#define SkG32Assert(g)
Definition: SkColorPriv.h:97
SkR32Assert
#define SkR32Assert(r)
Definition: SkColorPriv.h:96
SK_A32_SHIFT
#define SK_A32_SHIFT
Definition: SkTypes.h:301
SkPMSrcOver
static SkPMColor SkPMSrcOver(SkPMColor src, SkPMColor dst)
Definition: SkColorPriv.h:148
SK_R32_SHIFT
#define SK_R32_SHIFT
Definition: SkTypes.h:291
SkPackARGB32NoCheck
static SkPMColor SkPackARGB32NoCheck(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Same as SkPackARGB32, but this version guarantees to not check that the values are premultiplied in t...
Definition: SkColorPriv.h:118
SkPremultiplyARGBInline
static SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorPriv.h:124
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkAlphaMulQ
static SK_ALWAYS_INLINE uint32_t SkAlphaMulQ(uint32_t c, unsigned scale)
Definition: SkColorPriv.h:140
SK_B32_SHIFT
#define SK_B32_SHIFT
Definition: SkTypes.h:297
SkGetPackedA32
#define SkGetPackedA32(packed)
Definition: SkColorPriv.h:90
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkA32Assert
#define SkA32Assert(a)
Definition: SkColorPriv.h:95
SkMulDiv255Round
static U8CPU SkMulDiv255Round(U16CPU a, U16CPU b)
Return a*b/255, rounding any fractional bits.
Definition: SkMath.h:50
SK_ALWAYS_INLINE
#define SK_ALWAYS_INLINE
If your judgment is better than the compiler's (i.e.
Definition: SkTypes.h:347
f
float f
Definition: skcms.cc:52
SkPackARGB32
static SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Pack the components into a SkPMColor, checking (in the debug version) that the components are 0....
Definition: SkColorPriv.h:104
SkAlpha255To256
static unsigned SkAlpha255To256(U8CPU alpha)
Turn 0..255 into 0..256 by adding 1 at the half-way point.
Definition: SkColorPriv.h:22
SkPMColor
uint32_t SkPMColor
32-bit ARGB color value, premultiplied.
Definition: SkColor.h:203
U8CPU
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:488
SkColor.h
SkUnitScalarClampToByte
static U8CPU SkUnitScalarClampToByte(SkScalar x)
Definition: SkColorPriv.h:34
SkMath.h