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
16 /** Turn 0..255 into 0..256 by adding 1 at the half-way point. Used to turn a
17 byte into a scale value, so that we can say scale * value >> 8 instead of
18 alpha * value / 255.
19
20 In debugging, asserts that alpha is 0..255
21 */
SkAlpha255To256(U8CPU alpha)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
29 /** Multiplify value by 0..256, and shift the result down 8
30 (i.e. return (value * alpha256) >> 8)
31 */
32 #define SkAlphaMul(value, alpha256) (((value) * (alpha256)) >> 8)
33
SkUnitScalarClampToByte(SkScalar x)34 static inline U8CPU SkUnitScalarClampToByte(SkScalar x) {
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
100 /**
101 * Pack the components into a SkPMColor, checking (in the debug version) that
102 * the components are 0..255, and are already premultiplied (i.e. alpha >= color)
103 */
SkPackARGB32(U8CPU a,U8CPU r,U8CPU g,U8CPU b)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
114 /**
115 * Same as SkPackARGB32, but this version guarantees to not check that the
116 * values are premultiplied in the debug version.
117 */
SkPackARGB32NoCheck(U8CPU a,U8CPU r,U8CPU g,U8CPU b)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
SkPremultiplyARGBInline(U8CPU a,U8CPU r,U8CPU g,U8CPU b)124 SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
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.
SkAlphaMulQ(uint32_t c,unsigned scale)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
SkPMSrcOver(SkPMColor src,SkPMColor dst)148 static inline SkPMColor SkPMSrcOver(SkPMColor src, SkPMColor dst) {
149 return src + SkAlphaMulQ(dst, SkAlpha255To256(255 - SkGetPackedA32(src)));
150 }
151
152 #endif
153