blob: e1cb1cd27de5b79d2da9ff2c268b03e7647773b4 [file] [log] [blame]
reeddd9ffea2016-02-18 12:39:14 -08001/*
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
Cary Clarka4083c92017-09-15 11:59:23 -040011#include "SkColorData.h"
reed93bb0802016-03-08 10:09:18 -080012#include "SkNx.h"
13
14static inline Sk4f swizzle_rb(const Sk4f& x) {
15 return SkNx_shuffle<2, 1, 0, 3>(x);
16}
17
18static 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}
reeddd9ffea2016-02-18 12:39:14 -080025
26/*
reed93bb0802016-03-08 10:09:18 -080027 * The float values are 0...1 premultiplied in RGBA order (regardless of SkPMColor order)
reeddd9ffea2016-02-18 12:39:14 -080028 */
Derek Sollenberger2fbf1bc2017-09-20 15:51:08 -040029struct SK_API SkPM4f {
reeddd9ffea2016-02-18 12:39:14 -080030 enum {
reed93bb0802016-03-08 10:09:18 -080031 R, G, B, A,
reeddd9ffea2016-02-18 12:39:14 -080032 };
33 float fVec[4];
34
reed93bb0802016-03-08 10:09:18 -080035 float r() const { return fVec[R]; }
36 float g() const { return fVec[G]; }
37 float b() const { return fVec[B]; }
reeddd9ffea2016-02-18 12:39:14 -080038 float a() const { return fVec[A]; }
39
Mike Reedb9641bd2017-05-04 10:57:40 -040040 static SkPM4f FromPremulRGBA(float r, float g, float b, float a) {
41 SkPM4f p;
42 p.fVec[R] = r;
43 p.fVec[G] = g;
44 p.fVec[B] = b;
45 p.fVec[A] = a;
46 return p;
47 }
48
reed93bb0802016-03-08 10:09:18 -080049 static SkPM4f From4f(const Sk4f& x) {
50 SkPM4f pm;
51 x.store(pm.fVec);
52 return pm;
53 }
54 static SkPM4f FromF16(const uint16_t[4]);
reeddd9ffea2016-02-18 12:39:14 -080055 static SkPM4f FromPMColor(SkPMColor);
56
reed93bb0802016-03-08 10:09:18 -080057 Sk4f to4f() const { return Sk4f::Load(fVec); }
58 Sk4f to4f_rgba() const { return this->to4f(); }
59 Sk4f to4f_bgra() const { return swizzle_rb(this->to4f()); }
60 Sk4f to4f_pmorder() const { return swizzle_rb_if_bgra(this->to4f()); }
61
Mike Klein1478f742017-05-03 22:47:38 +000062 SkPMColor toPMColor() const {
63 Sk4f value = swizzle_rb_if_bgra(this->to4f());
64 SkPMColor result;
65 SkNx_cast<uint8_t>(value * Sk4f(255) + Sk4f(0.5f)).store(&result);
66 return result;
67 }
brianosmane2cddc52016-06-24 11:55:32 -070068
reeddd9ffea2016-02-18 12:39:14 -080069 void toF16(uint16_t[4]) const;
70 uint64_t toF16() const; // 4 float16 values packed into uint64_t
reed93bb0802016-03-08 10:09:18 -080071
72 SkColor4f unpremul() const;
reeddd9ffea2016-02-18 12:39:14 -080073
74#ifdef SK_DEBUG
75 void assertIsUnit() const;
76#else
77 void assertIsUnit() const {}
78#endif
79};
80
reeddd9ffea2016-02-18 12:39:14 -080081#endif