blob: 6698aa668479b24116fc8ff789076f338d40b749 [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"
Michael Ludwig7f8c5242018-09-14 15:07:55 -040040 #include "GrGradientShader.h"
Michael Ludwig24d438b2018-09-12 15:22:50 -040041}
42
Michael Ludwigb96cba32018-09-14 13:59:24 -040043// The sweep gradient never rejects a pixel so it doesn't change opacity
44@optimizationFlags {
45 kPreservesOpaqueInput_OptimizationFlag
46}
47
Michael Ludwig24d438b2018-09-12 15:22:50 -040048@make {
49 static std::unique_ptr<GrFragmentProcessor> Make(const SkSweepGradient& gradient,
50 const GrFPArgs& args);
51}
52
53@cppEnd {
54 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(
55 const SkSweepGradient& grad, const GrFPArgs& args) {
56 SkMatrix matrix;
57 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
58 return nullptr;
59 }
60 matrix.postConcat(grad.getGradientMatrix());
61 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(
62 matrix, grad.getTBias(), grad.getTScale()));
63 }
64}
Michael Ludwig7f8c5242018-09-14 15:07:55 -040065
66//////////////////////////////////////////////////////////////////////////////
67
68@test(d) {
69 SkPoint center = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
70
71 GrGradientShader::RandomParams params(d->fRandom);
72 auto shader = params.fUseColors4f ?
73 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f, params.fColorSpace,
74 params.fStops, params.fColorCount) :
75 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors,
76 params.fStops, params.fColorCount);
77 GrTest::TestAsFPArgs asFPArgs(d);
78 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
79 GrAlwaysAssert(fp);
80 return fp;
81}