blob: 7b98cb7685ba75f5e93ae3387fdc31642b4cc724 [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]
32 sk_OutColor = half4((angle * 0.1591549430918 + 0.5 + bias) * scale);
33}
34
35//////////////////////////////////////////////////////////////////////////////
36
37@header {
38 #include "SkSweepGradient.h"
39}
40
41@make {
42 static std::unique_ptr<GrFragmentProcessor> Make(const SkSweepGradient& gradient,
43 const GrFPArgs& args);
44}
45
46@cppEnd {
47 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(
48 const SkSweepGradient& grad, const GrFPArgs& args) {
49 SkMatrix matrix;
50 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
51 return nullptr;
52 }
53 matrix.postConcat(grad.getGradientMatrix());
54 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(
55 matrix, grad.getTBias(), grad.getTScale()));
56 }
57}