xref: /aosp_15_r20/external/skia/src/core/SkSwizzlePriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
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 SkSwizzlePriv_DEFINED
9 #define SkSwizzlePriv_DEFINED
10 
11 #include "include/private/SkColorData.h"
12 #include "src/base/SkVx.h"
13 
14 #include <cstdint>
15 
16 namespace SkOpts {
17     // Swizzle input into some sort of 8888 pixel, {premul,unpremul} x {rgba,bgra}.
18     using Swizzle_8888_u32 = void (*)(uint32_t*, const uint32_t*, int);
19     extern Swizzle_8888_u32 RGBA_to_BGRA,          // i.e. just swap RB
20                             RGBA_to_rgbA,          // i.e. just premultiply
21                             RGBA_to_bgrA,          // i.e. swap RB and premultiply
22                             rgbA_to_RGBA,          // i.e. just unpremultiply
23                             rgbA_to_BGRA,          // i.e. swap RB and unpremultiply
24                             inverted_CMYK_to_RGB1, // i.e. convert color space
25                             inverted_CMYK_to_BGR1; // i.e. convert color space
26 
27     using Swizzle_8888_u8 = void (*)(uint32_t*, const uint8_t*, int);
28     extern Swizzle_8888_u8 RGB_to_RGB1,     // i.e. insert an opaque alpha
29                            RGB_to_BGR1,     // i.e. swap RB and insert an opaque alpha
30                            gray_to_RGB1,    // i.e. expand to color channels + an opaque alpha
31                            grayA_to_RGBA,   // i.e. expand to color channels
32                            grayA_to_rgbA;   // i.e. expand to color channels and premultiply
33 
34     void Init_Swizzler();
35 }  // namespace SkOpts
36 
swizzle_rb(const skvx::float4 & x)37 static inline skvx::float4 swizzle_rb(const skvx::float4& x) {
38     return skvx::shuffle<2, 1, 0, 3>(x);
39 }
40 
swizzle_rb_if_bgra(const skvx::float4 & x)41 static inline skvx::float4 swizzle_rb_if_bgra(const skvx::float4& x) {
42 #if defined(SK_PMCOLOR_IS_BGRA)
43     return swizzle_rb(x);
44 #else
45     return x;
46 #endif
47 }
48 
Sk4f_fromL32(uint32_t px)49 static inline skvx::float4 Sk4f_fromL32(uint32_t px) {
50     return skvx::cast<float>(skvx::byte4::Load(&px)) * (1 / 255.0f);
51 }
52 
Sk4f_toL32(const skvx::float4 & px)53 static inline uint32_t Sk4f_toL32(const skvx::float4& px) {
54     uint32_t l32;
55     // For the expected positive color values, the +0.5 before the pin and cast effectively rounds
56     // to the nearest int without having to call round() or lrint().
57     skvx::cast<uint8_t>(skvx::pin(px * 255.f + 0.5f, skvx::float4(0.f), skvx::float4(255.f)))
58                        .store(&l32);
59     return l32;
60 }
61 #endif  // SkSwizzlePriv_DEFINED
62