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/private/base/SkMath.h"
13 #include "include/private/base/SkTPin.h"
14 #include "include/private/base/SkTo.h"
15 
16 #include <algorithm>
17 
24 static 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 
34 #define SkAlphaMul(value, alpha256) (((value) * (alpha256)) >> 8)
35 
36 static inline U8CPU SkUnitScalarClampToByte(SkScalar x) {
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 
106 static inline SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
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 
120 static inline SkPMColor SkPackARGB32NoCheck(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
121  return (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
122  (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
123 }
124 
125 static inline
126 SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
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.
142 static 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 
150 static 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 SkB32Assert(b)
Definition: SkColorPriv.h:100
static SkPMColor SkPMSrcOver(SkPMColor src, SkPMColor dst)
Definition: SkColorPriv.h:150
static SK_ALWAYS_INLINE uint32_t SkAlphaMulQ(uint32_t c, unsigned scale)
Definition: SkColorPriv.h:142
#define SkA32Assert(a)
Definition: SkColorPriv.h:97
static SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorPriv.h:126
#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)
Turn 0..255 into 0..256 by adding 1 at the half-way point.
Definition: SkColorPriv.h:24
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:106
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:120
Types, consts, functions, and macros for colors.
uint32_t SkPMColor
32-bit ARGB color value, premultiplied.
Definition: SkColor.h:205
float SkScalar
Definition: SkScalar.h:14
#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