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
SkSweepGradient(const SkPoint & center,SkScalar t0,SkScalar t1,const Descriptor & desc)14 SkSweepGradient::SkSweepGradient(const SkPoint& center, SkScalar t0, SkScalar t1,
15 const Descriptor& desc)
16 : SkGradientShaderBase(desc, SkMatrix::Translate(-center.x(), -center.y()))
17 , fCenter(center)
18 , fTBias(-t0)
19 , fTScale(1 / (t1 - t0))
20 {
21 SkASSERT(t0 < t1);
22 }
23
asAGradient(GradientInfo * info) const24 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
angles_from_t_coeff(SkScalar tBias,SkScalar tScale)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
CreateProc(SkReadBuffer & buffer)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 const auto tBias = buffer.readScalar(),
44 tScale = buffer.readScalar();
45 auto [startAngle, endAngle] = angles_from_t_coeff(tBias, tScale);
46
47 return SkGradientShader::MakeSweep(center.x(), center.y(), desc.fColors,
48 std::move(desc.fColorSpace), desc.fPos, desc.fCount,
49 desc.fTileMode, startAngle, endAngle,
50 desc.fGradFlags, desc.fLocalMatrix);
51 }
52
flatten(SkWriteBuffer & buffer) const53 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
54 this->INHERITED::flatten(buffer);
55 buffer.writePoint(fCenter);
56 buffer.writeScalar(fTBias);
57 buffer.writeScalar(fTScale);
58 }
59
appendGradientStages(SkArenaAlloc * alloc,SkRasterPipeline * p,SkRasterPipeline *) const60 void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
61 SkRasterPipeline*) const {
62 p->append(SkRasterPipeline::xy_to_unit_angle);
63 p->append_matrix(alloc, SkMatrix::Scale(fTScale, 1) * SkMatrix::Translate(fTBias, 0));
64 }
65
transformT(skvm::Builder * p,skvm::Uniforms * uniforms,skvm::Coord coord,skvm::I32 * mask) const66 skvm::F32 SkSweepGradient::transformT(skvm::Builder* p, skvm::Uniforms* uniforms,
67 skvm::Coord coord, skvm::I32* mask) const {
68 skvm::F32 xabs = abs(coord.x),
69 yabs = abs(coord.y),
70 slope = min(xabs, yabs) / max(xabs, yabs);
71 skvm::F32 s = slope * slope;
72
73 // Use a 7th degree polynomial to approximate atan.
74 // This was generated using sollya.gforge.inria.fr.
75 // A float optimized polynomial was generated using the following command.
76 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
77 skvm::F32 phi = slope * poly(s, -7.0547382347285747528076171875e-3f,
78 +2.476101927459239959716796875e-2f,
79 -5.185396969318389892578125e-2f,
80 +0.15912117063999176025390625f);
81 phi = select( xabs < yabs, (1/4.0f) - phi, phi);
82 phi = select(coord.x < 0.0f, (1/2.0f) - phi, phi);
83 phi = select(coord.y < 0.0f, (1/1.0f) - phi, phi);
84
85 skvm::F32 t = select(is_NaN(phi), p->splat(0.0f)
86 , phi);
87
88 if (fTScale != 1.0f || fTBias != 0.0f) {
89 t = t * p->uniformF(uniforms->pushF(fTScale))
90 + p->uniformF(uniforms->pushF(fTScale*fTBias));
91 }
92 return t;
93 }
94
95 /////////////////////////////////////////////////////////////////////
96
97 #if SK_SUPPORT_GPU
98
99 #include "src/gpu/gradients/GrGradientShader.h"
100
asFragmentProcessor(const GrFPArgs & args) const101 std::unique_ptr<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(
102 const GrFPArgs& args) const {
103 return GrGradientShader::MakeSweep(*this, args);
104 }
105
106 #endif
107