1/*
2 * Copyright 2017 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@header {
9    #include "GrShaderCaps.h"
10}
11
12layout(key) in GrClipEdgeType edgeType;
13in float2 center;
14in float2 radii;
15
16float2 prevCenter;
17float2 prevRadii = float2(-1);
18// The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2)
19// The last two terms can underflow when float != fp32, so we also provide a workaround.
20uniform float4 ellipse;
21
22bool useScale = !sk_Caps.floatIs32Bits;
23layout(when=useScale) uniform float2 scale;
24
25@make {
26    static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, SkPoint center,
27                                                     SkPoint radii, const GrShaderCaps& caps) {
28        // Small radii produce bad results on devices without full float.
29        if (!caps.floatIs32Bits() && (radii.fX < 0.5f || radii.fY < 0.5f)) {
30            return nullptr;
31        }
32        return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(edgeType, center, radii));
33    }
34}
35
36@optimizationFlags { kCompatibleWithCoverageAsAlpha_OptimizationFlag }
37
38@setData(pdman) {
39    if (radii != prevRadii || center != prevCenter) {
40        float invRXSqd;
41        float invRYSqd;
42        // If we're using a scale factor to work around precision issues, choose the larger radius
43        // as the scale factor. The inv radii need to be pre-adjusted by the scale factor.
44        if (scale.isValid()) {
45            if (radii.fX > radii.fY) {
46                invRXSqd = 1.f;
47                invRYSqd = (radii.fX * radii.fX) /
48                           (radii.fY * radii.fY);
49                pdman.set2f(scale, radii.fX, 1.f / radii.fX);
50            } else {
51                invRXSqd = (radii.fY * radii.fY) /
52                           (radii.fX * radii.fX);
53                invRYSqd = 1.f;
54                pdman.set2f(scale, radii.fY, 1.f / radii.fY);
55            }
56        } else {
57            invRXSqd = 1.f / (radii.fX * radii.fX);
58            invRYSqd = 1.f / (radii.fY * radii.fY);
59        }
60        pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd);
61        prevCenter = center;
62        prevRadii = radii;
63    }
64}
65
66void main() {
67    // d is the offset to the ellipse center
68    float2 d = sk_FragCoord.xy - ellipse.xy;
69    // If we're on a device with a "real" mediump then we'll do the distance computation in a space
70    // that is normalized by the larger radius. The scale uniform will be scale, 1/scale. The
71    // inverse squared radii uniform values are already in this normalized space. The center is
72    // not.
73    @if (useScale) {
74        d *= scale.y;
75    }
76    float2 Z = d * ellipse.zw;
77    // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1.
78    float implicit = dot(Z, d) - 1;
79    // grad_dot is the squared length of the gradient of the implicit.
80    float grad_dot = 4 * dot(Z, Z);
81    // Avoid calling inversesqrt on zero.
82    grad_dot = max(grad_dot, 1e-4);
83    float approx_dist = implicit * inversesqrt(grad_dot);
84    @if (useScale) {
85        approx_dist *= scale.x;
86    }
87
88    half alpha;
89    @switch (edgeType) {
90        case GrClipEdgeType::kFillBW:
91            alpha = approx_dist > 0.0 ? 0.0 : 1.0;
92            break;
93        case GrClipEdgeType::kFillAA:
94            alpha = saturate(0.5 - approx_dist);
95            break;
96        case GrClipEdgeType::kInverseFillBW:
97            alpha = approx_dist > 0.0 ? 1.0 : 0.0;
98            break;
99        case GrClipEdgeType::kInverseFillAA:
100            alpha = saturate(0.5 + approx_dist);
101            break;
102        default:
103            // hairline not supported
104            discard;
105    }
106    sk_OutColor = sk_InColor * alpha;
107}
108
109@test(testData) {
110    SkPoint center;
111    center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
112    center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
113    SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f);
114    SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f);
115    GrClipEdgeType et;
116    do {
117        et = (GrClipEdgeType) testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
118    } while (GrClipEdgeType::kHairlineAA == et);
119    return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry),
120                                 *testData->caps()->shaderCaps());
121}
122