1/*
2 * Copyright 2018 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// Models two intervals (so 4 colors), that are connected at a specific threshold point.
9
10// Bias and scale for 0 to threshold
11layout(ctype=SkPMColor4f, tracked) in uniform float4 scale01;
12layout(ctype=SkPMColor4f, tracked) in uniform float4 bias01;
13
14// Bias and scale for threshold to 1
15layout(ctype=SkPMColor4f, tracked) in uniform float4 scale23;
16layout(ctype=SkPMColor4f, tracked) in uniform float4 bias23;
17
18layout(tracked) in uniform half threshold;
19
20void main() {
21    half t = sk_InColor.x;
22
23    float4 scale, bias;
24    if (t < threshold) {
25        scale = scale01;
26        bias = bias01;
27    } else {
28        scale = scale23;
29        bias = bias23;
30    }
31
32    sk_OutColor = half4(t * scale + bias);
33}
34
35//////////////////////////////////////////////////////////////////////////////
36
37@make {
38    static std::unique_ptr<GrFragmentProcessor> Make(const SkPMColor4f& c0, const SkPMColor4f& c1,
39                                                     const SkPMColor4f& c2, const SkPMColor4f& c3,
40                                                     float threshold);
41}
42
43@cppEnd {
44    std::unique_ptr<GrFragmentProcessor> GrDualIntervalGradientColorizer::Make(
45            const SkPMColor4f& c0, const SkPMColor4f& c1, const SkPMColor4f& c2, const SkPMColor4f& c3, float threshold) {
46        // Derive scale and biases from the 4 colors and threshold
47        auto vc0 = Sk4f::Load(c0.vec());
48        auto vc1 = Sk4f::Load(c1.vec());
49        auto scale01 = (vc1 - vc0) / threshold;
50        // bias01 = c0
51
52        auto vc2 = Sk4f::Load(c2.vec());
53        auto vc3 = Sk4f::Load(c3.vec());
54        auto scale23 = (vc3 - vc2) / (1 - threshold);
55        auto bias23 = vc2 - threshold * scale23;
56
57        return std::unique_ptr<GrFragmentProcessor>(new GrDualIntervalGradientColorizer(
58                { scale01[0], scale01[1], scale01[2], scale01[3] }, c0,
59                { scale23[0], scale23[1], scale23[2], scale23[3] },
60                { bias23[0], bias23[1], bias23[2], bias23[3] }, threshold));
61    }
62}
63