2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef Sk4fGradientPriv_DEFINED
9 #define Sk4fGradientPriv_DEFINED
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"
18 // Templates shared by various 4f gradient flavors.
20 namespace { // NOLINT(google-build-namespaces)
22 enum class ApplyPremul { True, False };
24 template <ApplyPremul>
28 struct PremulTraits<ApplyPremul::False> {
29 static skvx::float4 apply(const skvx::float4& c) { return c; }
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);
41 // Struct encapsulating various dest-dependent ops:
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.
46 // - store() Store one skvx::float4 to dest. Optionally handles premul, color space
49 // - store(count) Store the skvx::float4 value repeatedly to dest, count times.
51 // - store4x() Store 4 skvx::float4 values to dest (opportunistic optimization).
54 template <ApplyPremul premul>
56 using PM = PremulTraits<premul>;
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)
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);
71 *dst = Sk4f_toL32(PM::apply(c) + bias);
75 static void store(const skvx::float4& c, SkPMColor* dst, int n) {
77 store(c, &pmc, skvx::float4(0));
78 sk_memset32(dst, pmc, n);
81 static void store4x(const skvx::float4& c0, const skvx::float4& c1,
82 const skvx::float4& c2, const skvx::float4& c3,
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);
93 store(c0, dst + 0, bias0);
94 store(c1, dst + 1, bias1);
95 store(c2, dst + 2, bias0);
96 store(c3, dst + 3, bias1);
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;
106 } // anonymous namespace
108 #endif // Sk4fGradientPriv_DEFINED