Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / shaders / gradients / Sk4fGradientPriv.h
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 Sk4fGradientPriv_DEFINED
9 #define Sk4fGradientPriv_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/private/SkColorData.h"
14 #include "include/private/SkHalf.h"
15 #include "include/private/SkVx.h"
16 #include "src/core/SkOpts.h"
17
18 // Templates shared by various 4f gradient flavors.
19
20 namespace {  // NOLINT(google-build-namespaces)
21
22 enum class ApplyPremul { True, False };
23
24 template <ApplyPremul>
25 struct PremulTraits;
26
27 template <>
28 struct PremulTraits<ApplyPremul::False> {
29     static skvx::float4 apply(const skvx::float4& c) { return c; }
30 };
31
32 template <>
33 struct PremulTraits<ApplyPremul::True> {
34     static skvx::float4 apply(const skvx::float4& c) {
35         const float alpha = c[3];
36         // FIXME: portable swizzle?
37         return c * skvx::float4(alpha, alpha, alpha, 1);
38     }
39 };
40
41 // Struct encapsulating various dest-dependent ops:
42 //
43 //   - load()       Load a SkPMColor4f value into skvx::float4.  Normally called once per interval
44 //                  advance.  Also applies a scale and swizzle suitable for DstType.
45 //
46 //   - store()      Store one skvx::float4 to dest.  Optionally handles premul, color space
47 //                  conversion, etc.
48 //
49 //   - store(count) Store the skvx::float4 value repeatedly to dest, count times.
50 //
51 //   - store4x()    Store 4 skvx::float4 values to dest (opportunistic optimization).
52 //
53
54 template <ApplyPremul premul>
55 struct DstTraits {
56     using PM   = PremulTraits<premul>;
57
58     // For L32, prescaling by 255 saves a per-pixel multiplication when premul is not needed.
59     static skvx::float4 load(const SkPMColor4f& c) {
60         skvx::float4 c4f = swizzle_rb_if_bgra(skvx::float4::Load(c.vec()));
61         return premul == ApplyPremul::False
62             ? c4f * skvx::float4(255)
63             : c4f;
64     }
65
66     static void store(const skvx::float4& c, SkPMColor* dst, const skvx::float4& bias) {
67         if (premul == ApplyPremul::False) {
68             // c is pre-scaled by 255 and pre-biased, just store.
69             skvx::cast<uint8_t>(c).store(dst);
70         } else {
71             *dst = Sk4f_toL32(PM::apply(c) + bias);
72         }
73     }
74
75     static void store(const skvx::float4& c, SkPMColor* dst, int n) {
76         SkPMColor pmc;
77         store(c, &pmc, skvx::float4(0));
78         sk_memset32(dst, pmc, n);
79     }
80
81     static void store4x(const skvx::float4& c0, const skvx::float4& c1,
82                         const skvx::float4& c2, const skvx::float4& c3,
83                         SkPMColor* dst,
84                         const skvx::float4& bias0,
85                         const skvx::float4& bias1) {
86         if (premul == ApplyPremul::False) {
87             // colors are pre-scaled and pre-biased.
88             skvx::cast<uint8_t>(c0).store(dst + 0);
89             skvx::cast<uint8_t>(c1).store(dst + 1);
90             skvx::cast<uint8_t>(c2).store(dst + 2);
91             skvx::cast<uint8_t>(c3).store(dst + 3);
92         } else {
93             store(c0, dst + 0, bias0);
94             store(c1, dst + 1, bias1);
95             store(c2, dst + 2, bias0);
96             store(c3, dst + 3, bias1);
97         }
98     }
99
100     static skvx::float4 pre_lerp_bias(const skvx::float4& bias) {
101         // We can apply the bias before interpolation when the colors are premultiplied.
102         return premul == ApplyPremul::False ? bias : 0;
103     }
104 };
105
106 } // anonymous namespace
107
108 #endif // Sk4fGradientPriv_DEFINED