blob: aba5b6c74cba987a1ddff05f386652e6716eb334 [file] [log] [blame]
Michael Ludwig4f94ef62018-09-12 15:22:16 -04001/*
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;
Michael Ludwigb96cba32018-09-14 13:59:24 -040014layout(key) in bool makePremul;
15// Trust the creator that this matches the color spec of the gradient
16in bool colorsAreOpaque;
Michael Ludwig4f94ef62018-09-12 15:22:16 -040017
18void main() {
Ethan Nicholas13863662019-07-29 13:05:15 -040019 half4 t = sample(gradLayout);
Michael Ludwig4f94ef62018-09-12 15:22:16 -040020
Michael Ludwigb96cba32018-09-14 13:59:24 -040021 if (!gradLayout.preservesOpaqueInput && t.y < 0) {
22 // layout has rejected this fragment (rely on sksl to remove this branch if the layout FP
23 // preserves opacity is false)
Michael Ludwig8f685082018-09-12 15:23:01 -040024 sk_OutColor = half4(0);
Michael Ludwig4f94ef62018-09-12 15:22:16 -040025 } else {
Michael Ludwig8f685082018-09-12 15:23:01 -040026 @if(mirror) {
27 half t_1 = t.x - 1;
28 half tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;
29 if (sk_Caps.mustDoOpBetweenFloorAndAbs) {
30 // At this point the expected value of tiled_t should between -1 and 1, so this
31 // clamp has no effect other than to break up the floor and abs calls and make sure
32 // the compiler doesn't merge them back together.
33 tiled_t = clamp(tiled_t, -1, 1);
34 }
35 t.x = abs(tiled_t);
36 } else {
37 // Simple repeat mode
38 t.x = fract(t.x);
39 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -040040
Michael Ludwig8f685082018-09-12 15:23:01 -040041 // t.x has been tiled (repeat or mirrored), but pass through remaining 3 components
42 // unmodified.
Ethan Nicholas13863662019-07-29 13:05:15 -040043 sk_OutColor = sample(colorizer, t);
Michael Ludwig8f685082018-09-12 15:23:01 -040044 }
Michael Ludwigb96cba32018-09-14 13:59:24 -040045
46 @if (makePremul) {
47 sk_OutColor.xyz *= sk_OutColor.w;
48 }
49}
50
51//////////////////////////////////////////////////////////////////////////////
52
53// If the layout does not preserve opacity, remove the opaque optimization,
54// but otherwise respect the provided color opacity state.
55@optimizationFlags {
56 kCompatibleWithCoverageAsAlpha_OptimizationFlag |
57 (colorsAreOpaque && gradLayout->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag
58 : kNone_OptimizationFlags)
Michael Ludwig4f94ef62018-09-12 15:22:16 -040059}