• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// Provides tiling for the repeat or mirror modes.
9
10in fragmentProcessor colorizer;
11in fragmentProcessor gradLayout;
12
13layout(key) in bool mirror;
14layout(key) in bool makePremul;
15// Trust the creator that this matches the color spec of the gradient
16in bool colorsAreOpaque;
17layout(key) in bool layoutPreservesOpacity;
18
19half4 main() {
20    half4 t = sample(gradLayout);
21
22    if (!layoutPreservesOpacity && t.y < 0) {
23        // layout has rejected this fragment (rely on sksl to remove this branch if the layout FP
24        // preserves opacity is false)
25        return half4(0);
26    } else {
27        @if (mirror) {
28            half t_1 = t.x - 1;
29            half tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;
30            if (sk_Caps.mustDoOpBetweenFloorAndAbs) {
31                // At this point the expected value of tiled_t should between -1 and 1, so this
32                // clamp has no effect other than to break up the floor and abs calls and make sure
33                // the compiler doesn't merge them back together.
34                tiled_t = clamp(tiled_t, -1, 1);
35            }
36            t.x = abs(tiled_t);
37        } else {
38            // Simple repeat mode
39            t.x = fract(t.x);
40        }
41
42        // Always sample from (x, 0), discarding y, since the layout FP can use y as a side-channel.
43        @if (!makePremul) {
44            return sample(colorizer, t.x0);
45        } else {
46            half4 outColor = sample(colorizer, t.x0);
47            return outColor * outColor.aaa1;
48        }
49    }
50}
51
52//////////////////////////////////////////////////////////////////////////////
53
54// If the layout does not preserve opacity, remove the opaque optimization,
55// but otherwise respect the provided color opacity state.
56@optimizationFlags {
57    kCompatibleWithCoverageAsAlpha_OptimizationFlag |
58    (colorsAreOpaque && layoutPreservesOpacity ? kPreservesOpaqueInput_OptimizationFlag
59                                               : kNone_OptimizationFlags)
60}
61
62@make{
63    static std::unique_ptr<GrFragmentProcessor> Make(
64            std::unique_ptr<GrFragmentProcessor> colorizer,
65            std::unique_ptr<GrFragmentProcessor> gradLayout,
66            bool mirror,
67            bool makePremul,
68            bool colorsAreOpaque) {
69        bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
70        return std::unique_ptr<GrFragmentProcessor>(new GrTiledGradientEffect(
71                std::move(colorizer), std::move(gradLayout), mirror, makePremul, colorsAreOpaque,
72                layoutPreservesOpacity));
73    }
74}
75