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