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 SkPM4f_DEFINED
9 #define SkPM4f_DEFINED
10 
11 #include "SkColorPriv.h"
12 #include "SkNx.h"
13 
swizzle_rb(const Sk4f & x)14 static inline Sk4f swizzle_rb(const Sk4f& x) {
15     return SkNx_shuffle<2, 1, 0, 3>(x);
16 }
17 
swizzle_rb_if_bgra(const Sk4f & x)18 static inline Sk4f swizzle_rb_if_bgra(const Sk4f& x) {
19 #ifdef SK_PMCOLOR_IS_BGRA
20     return swizzle_rb(x);
21 #else
22     return x;
23 #endif
24 }
25 
26 /*
27  *  The float values are 0...1 premultiplied in RGBA order (regardless of SkPMColor order)
28  */
29 struct SkPM4f {
30     enum {
31         R, G, B, A,
32     };
33     float fVec[4];
34 
rSkPM4f35     float r() const { return fVec[R]; }
gSkPM4f36     float g() const { return fVec[G]; }
bSkPM4f37     float b() const { return fVec[B]; }
aSkPM4f38     float a() const { return fVec[A]; }
39 
From4fSkPM4f40     static SkPM4f From4f(const Sk4f& x) {
41         SkPM4f pm;
42         x.store(pm.fVec);
43         return pm;
44     }
45     static SkPM4f FromF16(const uint16_t[4]);
46     static SkPM4f FromPMColor(SkPMColor);
47 
to4fSkPM4f48     Sk4f to4f() const { return Sk4f::Load(fVec); }
to4f_rgbaSkPM4f49     Sk4f to4f_rgba() const { return this->to4f(); }
to4f_bgraSkPM4f50     Sk4f to4f_bgra() const { return swizzle_rb(this->to4f()); }
to4f_pmorderSkPM4f51     Sk4f to4f_pmorder() const { return swizzle_rb_if_bgra(this->to4f()); }
52 
toPMColorSkPM4f53     SkPMColor toPMColor() const {
54         Sk4f value = swizzle_rb_if_bgra(this->to4f());
55         SkPMColor result;
56         SkNx_cast<uint8_t>(value * Sk4f(255) + Sk4f(0.5f)).store(&result);
57         return result;
58     }
59 
60     void toF16(uint16_t[4]) const;
61     uint64_t toF16() const; // 4 float16 values packed into uint64_t
62 
63     SkColor4f unpremul() const;
64 
65 #ifdef SK_DEBUG
66     void assertIsUnit() const;
67 #else
assertIsUnitSkPM4f68     void assertIsUnit() const {}
69 #endif
70 };
71 
72 typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst);
73 
74 #endif
75