blob: d704d9fb940af10cba89cd8723eaa78d66d520c4 [file] [log] [blame]
Michael Ludwig24d438b2018-09-12 15:22:50 -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
8in half4x4 gradientMatrix;
9
10layout(tracked) in uniform half bias;
11layout(tracked) in uniform half scale;
12
13@coordTransform {
14 gradientMatrix
15}
16
17void main() {
18 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
19 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
20 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
21 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
22 // using atan instead.
23 half angle;
24 if (sk_Caps.atan2ImplementedAsAtanYOverX) {
25 angle = 2 * atan(-sk_TransformedCoords2D[0].y,
26 length(sk_TransformedCoords2D[0]) - sk_TransformedCoords2D[0].x);
27 } else {
28 angle = atan(-sk_TransformedCoords2D[0].y, -sk_TransformedCoords2D[0].x);
29 }
30
31 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
Michael Ludwig8f685082018-09-12 15:23:01 -040032 half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;
33 sk_OutColor = half4(t, 1, 0, 0); // y = 1 for always valid
Michael Ludwig24d438b2018-09-12 15:22:50 -040034}
35
36//////////////////////////////////////////////////////////////////////////////
37
38@header {
39 #include "SkSweepGradient.h"
40}
41
Michael Ludwigb96cba32018-09-14 13:59:24 -040042// The sweep gradient never rejects a pixel so it doesn't change opacity
43@optimizationFlags {
44 kPreservesOpaqueInput_OptimizationFlag
45}
46
Michael Ludwig24d438b2018-09-12 15:22:50 -040047@make {
48 static std::unique_ptr<GrFragmentProcessor> Make(const SkSweepGradient& gradient,
49 const GrFPArgs& args);
50}
51
52@cppEnd {
53 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(
54 const SkSweepGradient& grad, const GrFPArgs& args) {
55 SkMatrix matrix;
56 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
57 return nullptr;
58 }
59 matrix.postConcat(grad.getGradientMatrix());
60 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(
61 matrix, grad.getTBias(), grad.getTScale()));
62 }
63}