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 "SkColorSpaceXformer.h"
9 #include "SkFloatingPoint.h"
10 #include "SkRasterPipeline.h"
11 #include "SkReadBuffer.h"
12 #include "SkSweepGradient.h"
13 #include "SkWriteBuffer.h"
14
SkSweepGradient(const SkPoint & center,SkScalar t0,SkScalar t1,const Descriptor & desc)15 SkSweepGradient::SkSweepGradient(const SkPoint& center, SkScalar t0, SkScalar t1,
16 const Descriptor& desc)
17 : SkGradientShaderBase(desc, SkMatrix::MakeTrans(-center.x(), -center.y()))
18 , fCenter(center)
19 , fTBias(-t0)
20 , fTScale(1 / (t1 - t0))
21 {
22 SkASSERT(t0 < t1);
23 }
24
asAGradient(GradientInfo * info) const25 SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
26 if (info) {
27 commonAsAGradient(info);
28 info->fPoint[0] = fCenter;
29 }
30 return kSweep_GradientType;
31 }
32
angles_from_t_coeff(SkScalar tBias,SkScalar tScale)33 static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) {
34 return std::make_tuple(-tBias * 360, (sk_ieee_float_divide(1, tScale) - tBias) * 360);
35 }
36
CreateProc(SkReadBuffer & buffer)37 sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
38 DescriptorScope desc;
39 if (!desc.unflatten(buffer)) {
40 return nullptr;
41 }
42 const SkPoint center = buffer.readPoint();
43
44 SkScalar startAngle = 0,
45 endAngle = 360;
46 if (!buffer.isVersionLT(SkReadBuffer::kTileInfoInSweepGradient_Version)) {
47 const auto tBias = buffer.readScalar(),
48 tScale = buffer.readScalar();
49 std::tie(startAngle, endAngle) = angles_from_t_coeff(tBias, tScale);
50 }
51
52 return SkGradientShader::MakeSweep(center.x(), center.y(), desc.fColors,
53 std::move(desc.fColorSpace), desc.fPos, desc.fCount,
54 desc.fTileMode, startAngle, endAngle,
55 desc.fGradFlags, desc.fLocalMatrix);
56 }
57
flatten(SkWriteBuffer & buffer) const58 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
59 this->INHERITED::flatten(buffer);
60 buffer.writePoint(fCenter);
61 buffer.writeScalar(fTBias);
62 buffer.writeScalar(fTScale);
63 }
64
onMakeColorSpace(SkColorSpaceXformer * xformer) const65 sk_sp<SkShader> SkSweepGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
66 const AutoXformColors xformedColors(*this, xformer);
67
68 SkScalar startAngle, endAngle;
69 std::tie(startAngle, endAngle) = angles_from_t_coeff(fTBias, fTScale);
70
71 return SkGradientShader::MakeSweep(fCenter.fX, fCenter.fY, xformedColors.fColors.get(),
72 fOrigPos, fColorCount, fTileMode, startAngle, endAngle,
73 fGradFlags, &this->getLocalMatrix());
74 }
75
appendGradientStages(SkArenaAlloc * alloc,SkRasterPipeline * p,SkRasterPipeline *) const76 void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
77 SkRasterPipeline*) const {
78 p->append(SkRasterPipeline::xy_to_unit_angle);
79 p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeScale(fTScale, 1),
80 SkMatrix::MakeTrans(fTBias , 0)));
81 }
82
83 /////////////////////////////////////////////////////////////////////
84
85 #if SK_SUPPORT_GPU
86
87 #include "gradients/GrGradientShader.h"
88
asFragmentProcessor(const GrFPArgs & args) const89 std::unique_ptr<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(
90 const GrFPArgs& args) const {
91 return GrGradientShader::MakeSweep(*this, args);
92 }
93
94 #endif
95