1 | /* |
2 | * Copyright 2012 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 | #include "include/private/SkFloatingPoint.h" |
9 | #include "src/core/SkRasterPipeline.h" |
10 | #include "src/core/SkReadBuffer.h" |
11 | #include "src/core/SkWriteBuffer.h" |
12 | #include "src/shaders/gradients/SkSweepGradient.h" |
13 | |
14 | SkSweepGradient::SkSweepGradient(const SkPoint& center, SkScalar t0, SkScalar t1, |
15 | const Descriptor& desc) |
16 | : SkGradientShaderBase(desc, SkMatrix::MakeTrans(-center.x(), -center.y())) |
17 | , fCenter(center) |
18 | , fTBias(-t0) |
19 | , fTScale(1 / (t1 - t0)) |
20 | { |
21 | SkASSERT(t0 < t1); |
22 | } |
23 | |
24 | SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const { |
25 | if (info) { |
26 | commonAsAGradient(info); |
27 | info->fPoint[0] = fCenter; |
28 | } |
29 | return kSweep_GradientType; |
30 | } |
31 | |
32 | static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) { |
33 | return std::make_tuple(-tBias * 360, (sk_ieee_float_divide(1, tScale) - tBias) * 360); |
34 | } |
35 | |
36 | sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) { |
37 | DescriptorScope desc; |
38 | if (!desc.unflatten(buffer)) { |
39 | return nullptr; |
40 | } |
41 | const SkPoint center = buffer.readPoint(); |
42 | |
43 | SkScalar startAngle = 0, |
44 | endAngle = 360; |
45 | if (!buffer.isVersionLT(SkPicturePriv::kTileInfoInSweepGradient_Version)) { |
46 | const auto tBias = buffer.readScalar(), |
47 | tScale = buffer.readScalar(); |
48 | std::tie(startAngle, endAngle) = angles_from_t_coeff(tBias, tScale); |
49 | } |
50 | |
51 | return SkGradientShader::MakeSweep(center.x(), center.y(), desc.fColors, |
52 | std::move(desc.fColorSpace), desc.fPos, desc.fCount, |
53 | desc.fTileMode, startAngle, endAngle, |
54 | desc.fGradFlags, desc.fLocalMatrix); |
55 | } |
56 | |
57 | void SkSweepGradient::flatten(SkWriteBuffer& buffer) const { |
58 | this->INHERITED::flatten(buffer); |
59 | buffer.writePoint(fCenter); |
60 | buffer.writeScalar(fTBias); |
61 | buffer.writeScalar(fTScale); |
62 | } |
63 | |
64 | void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p, |
65 | SkRasterPipeline*) const { |
66 | p->append(SkRasterPipeline::xy_to_unit_angle); |
67 | p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeScale(fTScale, 1), |
68 | SkMatrix::MakeTrans(fTBias , 0))); |
69 | } |
70 | |
71 | skvm::F32 SkSweepGradient::transformT(skvm::Builder* p, skvm::Uniforms* uniforms, |
72 | skvm::F32 x, skvm::F32 y, skvm::I32* mask) const { |
73 | skvm::F32 xabs = abs(x), |
74 | yabs = abs(y), |
75 | slope = min(xabs, yabs) / max(xabs, yabs); |
76 | skvm::F32 s = slope * slope; |
77 | |
78 | // Use a 7th degree polynomial to approximate atan. |
79 | // This was generated using sollya.gforge.inria.fr. |
80 | // A float optimized polynomial was generated using the following command. |
81 | // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative); |
82 | skvm::F32 phi = slope * poly(s, -7.0547382347285747528076171875e-3f, |
83 | +2.476101927459239959716796875e-2f, |
84 | -5.185396969318389892578125e-2f, |
85 | +0.15912117063999176025390625f); |
86 | phi = select(xabs < yabs, (1/4.0f) - phi, phi); |
87 | phi = select( x < 0.0f, (1/2.0f) - phi, phi); |
88 | phi = select( y < 0.0f, (1/1.0f) - phi, phi); |
89 | |
90 | skvm::F32 t = select(is_NaN(phi), p->splat(0.0f) |
91 | , phi); |
92 | |
93 | if (fTScale != 1.0f || fTBias != 0.0f) { |
94 | t = t * p->uniformF(uniforms->pushF(fTScale)) |
95 | + p->uniformF(uniforms->pushF(fTScale*fTBias)); |
96 | } |
97 | return t; |
98 | } |
99 | |
100 | ///////////////////////////////////////////////////////////////////// |
101 | |
102 | #if SK_SUPPORT_GPU |
103 | |
104 | #include "src/gpu/gradients/GrGradientShader.h" |
105 | |
106 | std::unique_ptr<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor( |
107 | const GrFPArgs& args) const { |
108 | return GrGradientShader::MakeSweep(*this, args); |
109 | } |
110 | |
111 | #endif |
112 | |