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::MakeTrans(-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 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
flatten(SkWriteBuffer & buffer) const57 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
appendGradientStages(SkArenaAlloc * alloc,SkRasterPipeline * p,SkRasterPipeline *) const64 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 /////////////////////////////////////////////////////////////////////
72
73 #if SK_SUPPORT_GPU
74
75 #include "src/gpu/gradients/GrGradientShader.h"
76
asFragmentProcessor(const GrFPArgs & args) const77 std::unique_ptr<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(
78 const GrFPArgs& args) const {
79 return GrGradientShader::MakeSweep(*this, args);
80 }
81
82 #endif
83