Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 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 | in half4x4 gradientMatrix; |
| 9 | |
| 10 | layout(tracked) in uniform half bias; |
| 11 | layout(tracked) in uniform half scale; |
| 12 | |
| 13 | @coordTransform { |
| 14 | gradientMatrix |
| 15 | } |
| 16 | |
| 17 | void 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 Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 32 | half t = (angle * 0.1591549430918 + 0.5 + bias) * scale; |
| 33 | sk_OutColor = half4(t, 1, 0, 0); // y = 1 for always valid |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | @header { |
| 39 | #include "SkSweepGradient.h" |
| 40 | } |
| 41 | |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame^] | 42 | // The sweep gradient never rejects a pixel so it doesn't change opacity |
| 43 | @optimizationFlags { |
| 44 | kPreservesOpaqueInput_OptimizationFlag |
| 45 | } |
| 46 | |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 47 | @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 | } |