Debian Codegraph @

src/core/SkColorPriv.h

raw
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/SkScalar.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkCPUTypes.h"
15 #include "include/private/base/SkMath.h"
16 #include "include/private/base/SkTArray.h"
17 #include "include/private/base/SkTPin.h"
18 #include "include/private/base/SkTo.h"
19
20 #include <algorithm>
21 #include <cstdint>
22
23 /** Turn 0..255 into 0..256 by adding 1 at the half-way point. Used to turn a
24 byte into a scale value, so that we can say scale * value >> 8 instead of
25 alpha * value / 255.
26
27 In debugging, asserts that alpha is 0..255
28 */
29 static inline unsigned SkAlpha255To256(U8CPU alpha) {
30 SkASSERT(SkToU8(alpha) == alpha);
31 // this one assues that blending on top of an opaque dst keeps it that way
32 // even though it is less accurate than a+(a>>7) for non-opaque dsts
33 return alpha + 1;
34 }
35
36 /** Multiplify value by 0..256, and shift the result down 8
37 (i.e. return (value * alpha256) >> 8)
38 */
39 #define SkAlphaMul(value, alpha256) (((value) * (alpha256)) >> 8)
40
41 static inline U8CPU SkUnitScalarClampToByte(SkScalar x) {
42 return static_cast<U8CPU>(SkTPin(x, 0.0f, 1.0f) * 255 + 0.5f);
43 }
44
45 #define SK_A32_BITS 8
46 #define SK_R32_BITS 8
47 #define SK_G32_BITS 8
48 #define SK_B32_BITS 8
49
50 #define SK_A32_MASK ((1 << SK_A32_BITS) - 1)
51 #define SK_R32_MASK ((1 << SK_R32_BITS) - 1)
52 #define SK_G32_MASK ((1 << SK_G32_BITS) - 1)
53 #define SK_B32_MASK ((1 << SK_B32_BITS) - 1)
54
55 /*
56 * Skia's 32bit backend only supports 1 swizzle order at a time (compile-time).
57 * This is specified by SK_R32_SHIFT=0 or SK_R32_SHIFT=16.
58 *
59 * For easier compatibility with Skia's GPU backend, we further restrict these
60 * to either (in memory-byte-order) RGBA or BGRA. Note that this "order" does
61 * not directly correspond to the same shift-order, since we have to take endianess
62 * into account.
63 *
64 * Here we enforce this constraint.
65 */
66
67 #define SK_RGBA_R32_SHIFT 0
68 #define SK_RGBA_G32_SHIFT 8
69 #define SK_RGBA_B32_SHIFT 16
70 #define SK_RGBA_A32_SHIFT 24
71
72 #define SK_BGRA_B32_SHIFT 0
73 #define SK_BGRA_G32_SHIFT 8
74 #define SK_BGRA_R32_SHIFT 16
75 #define SK_BGRA_A32_SHIFT 24
76
77 #if defined(SK_PMCOLOR_IS_RGBA) || defined(SK_PMCOLOR_IS_BGRA)
78 #error "Configure PMCOLOR by setting SK_R32_SHIFT."
79 #endif
80
81 // Deduce which SK_PMCOLOR_IS_ to define from the _SHIFT defines
82
83 #if (SK_A32_SHIFT == SK_RGBA_A32_SHIFT && \
84 SK_R32_SHIFT == SK_RGBA_R32_SHIFT && \
85 SK_G32_SHIFT == SK_RGBA_G32_SHIFT && \
86 SK_B32_SHIFT == SK_RGBA_B32_SHIFT)
87 #define SK_PMCOLOR_IS_RGBA
88 #elif (SK_A32_SHIFT == SK_BGRA_A32_SHIFT && \
89 SK_R32_SHIFT == SK_BGRA_R32_SHIFT && \
90 SK_G32_SHIFT == SK_BGRA_G32_SHIFT && \
91 SK_B32_SHIFT == SK_BGRA_B32_SHIFT)
92 #define SK_PMCOLOR_IS_BGRA
93 #else
94 #error "need 32bit packing to be either RGBA or BGRA"
95 #endif
96
97 #define SkGetPackedA32(packed) ((uint32_t)((packed) << (24 - SK_A32_SHIFT)) >> 24)
98 #define SkGetPackedR32(packed) ((uint32_t)((packed) << (24 - SK_R32_SHIFT)) >> 24)
99 #define SkGetPackedG32(packed) ((uint32_t)((packed) << (24 - SK_G32_SHIFT)) >> 24)
100 #define SkGetPackedB32(packed) ((uint32_t)((packed) << (24 - SK_B32_SHIFT)) >> 24)
101
102 #define SkA32Assert(a) SkASSERT((unsigned)(a) <= SK_A32_MASK)
103 #define SkR32Assert(r) SkASSERT((unsigned)(r) <= SK_R32_MASK)
104 #define SkG32Assert(g) SkASSERT((unsigned)(g) <= SK_G32_MASK)
105 #define SkB32Assert(b) SkASSERT((unsigned)(b) <= SK_B32_MASK)
106
107 /**
108 * Pack the components into a SkPMColor
109 */
110 static inline SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
111 SkA32Assert(a);
112 SkR32Assert(r);
113 SkG32Assert(g);
114 SkB32Assert(b);
115
116 return (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
117 (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
118 }
119
120 static inline
121 SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
122 SkA32Assert(a);
123 SkR32Assert(r);
124 SkG32Assert(g);
125 SkB32Assert(b);
126
127 if (a != 255) {
128 r = SkMulDiv255Round(r, a);
129 g = SkMulDiv255Round(g, a);
130 b = SkMulDiv255Round(b, a);
131 }
132 return SkPackARGB32(a, r, g, b);
133 }
134
135 // When Android is compiled optimizing for size, SkAlphaMulQ doesn't get
136 // inlined; forcing inlining significantly improves performance.
137 static SK_ALWAYS_INLINE uint32_t SkAlphaMulQ(uint32_t c, unsigned scale) {
138 static constexpr uint32_t kMask = 0x00FF00FF;
139
140 uint32_t rb = ((c & kMask) * scale) >> 8;
141 uint32_t ag = ((c >> 8) & kMask) * scale;
142 return (rb & kMask) | (ag & ~kMask);
143 }
144
145 static inline SkPMColor SkPMSrcOver(SkPMColor src, SkPMColor dst) {
146 uint32_t scale = SkAlpha255To256(255 - SkGetPackedA32(src));
147
148 static constexpr uint32_t kMask = 0x00FF00FF;
149 uint32_t rb = (((dst & kMask) * scale) >> 8) & kMask;
150 uint32_t ag = (((dst >> 8) & kMask) * scale) & ~kMask;
151
152 rb += (src & kMask);
153 ag += (src & ~kMask);
154
155 // Color channels (but not alpha) can overflow, so we have to saturate to 0xFF in each lane.
156 return std::min(rb & 0x000001FF, 0x000000FFU) |
157 std::min(ag & 0x0001FF00, 0x0000FF00U) |
158 std::min(rb & 0x01FF0000, 0x00FF0000U) |
159 (ag & 0xFF000000);
160 }
161
162 struct SkColorConverter {
163 // SkColorConverter() {}
164 SkColorConverter(SkSpan<const SkColor>);
165
166 SkSpan<SkColor4f> colors4f() { return fColors4f; }
167
168 private:
169 skia_private::STArray<2, SkColor4f> fColors4f;
170 };
171
172 #endif