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 "src/shaders/gradients/SkSweepGradient.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkTileMode.h"
16 #include "include/effects/SkGradientShader.h"
17 #include "include/private/base/SkAssert.h"
18 #include "include/private/base/SkFloatingPoint.h"
19 #include "include/private/base/SkTArray.h"
20 #include "src/core/SkRasterPipeline.h"
21 #include "src/core/SkRasterPipelineOpList.h"
22 #include "src/core/SkReadBuffer.h"
23 #include "src/core/SkWriteBuffer.h"
24 #include "src/shaders/SkShaderBase.h"
25 #include "src/shaders/gradients/SkGradientBaseShader.h"
26
27 #include <cstdint>
28 #include <tuple>
29 #include <utility>
30
31 class SkArenaAlloc;
32
SkSweepGradient(const SkPoint & center,SkScalar t0,SkScalar t1,const Descriptor & desc)33 SkSweepGradient::SkSweepGradient(const SkPoint& center,
34 SkScalar t0,
35 SkScalar t1,
36 const Descriptor& desc)
37 : SkGradientBaseShader(desc, SkMatrix::Translate(-center.x(), -center.y()))
38 , fCenter(center)
39 , fTBias(-t0)
40 , fTScale(1 / (t1 - t0)) {
41 SkASSERT(t0 < t1);
42 }
43
asGradient(GradientInfo * info,SkMatrix * localMatrix) const44 SkShaderBase::GradientType SkSweepGradient::asGradient(GradientInfo* info,
45 SkMatrix* localMatrix) const {
46 if (info) {
47 commonAsAGradient(info);
48 info->fPoint[0] = fCenter;
49 info->fPoint[1].fX = fTScale;
50 info->fPoint[1].fY = fTBias;
51 }
52 if (localMatrix) {
53 *localMatrix = SkMatrix::I();
54 }
55 return GradientType::kSweep;
56 }
57
angles_from_t_coeff(SkScalar tBias,SkScalar tScale)58 static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) {
59 return std::make_tuple(-tBias * 360, (sk_ieee_float_divide(1, tScale) - tBias) * 360);
60 }
61
CreateProc(SkReadBuffer & buffer)62 sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
63 DescriptorScope desc;
64 SkMatrix legacyLocalMatrix, *lmPtr = nullptr;
65 if (!desc.unflatten(buffer, &legacyLocalMatrix)) {
66 return nullptr;
67 }
68 if (!legacyLocalMatrix.isIdentity()) {
69 lmPtr = &legacyLocalMatrix;
70 }
71 const SkPoint center = buffer.readPoint();
72
73 const auto tBias = buffer.readScalar(),
74 tScale = buffer.readScalar();
75 auto [startAngle, endAngle] = angles_from_t_coeff(tBias, tScale);
76
77 return SkGradientShader::MakeSweep(center.x(), center.y(),
78 desc.fColors,
79 std::move(desc.fColorSpace),
80 desc.fPositions,
81 desc.fColorCount,
82 desc.fTileMode,
83 startAngle,
84 endAngle,
85 desc.fInterpolation,
86 lmPtr);
87 }
88
flatten(SkWriteBuffer & buffer) const89 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
90 this->SkGradientBaseShader::flatten(buffer);
91 buffer.writePoint(fCenter);
92 buffer.writeScalar(fTBias);
93 buffer.writeScalar(fTScale);
94 }
95
appendGradientStages(SkArenaAlloc * alloc,SkRasterPipeline * p,SkRasterPipeline *) const96 void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
97 SkRasterPipeline*) const {
98 p->append(SkRasterPipelineOp::xy_to_unit_angle);
99 p->appendMatrix(alloc, SkMatrix::Scale(fTScale, 1) * SkMatrix::Translate(fTBias, 0));
100 }
101
MakeSweep(SkScalar cx,SkScalar cy,const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int colorCount,SkTileMode mode,SkScalar startAngle,SkScalar endAngle,const Interpolation & interpolation,const SkMatrix * localMatrix)102 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
103 const SkColor4f colors[],
104 sk_sp<SkColorSpace> colorSpace,
105 const SkScalar pos[],
106 int colorCount,
107 SkTileMode mode,
108 SkScalar startAngle,
109 SkScalar endAngle,
110 const Interpolation& interpolation,
111 const SkMatrix* localMatrix) {
112 if (!SkGradientBaseShader::ValidGradient(colors, colorCount, mode, interpolation)) {
113 return nullptr;
114 }
115 if (1 == colorCount) {
116 return SkShaders::Color(colors[0], std::move(colorSpace));
117 }
118 if (!SkIsFinite(startAngle, endAngle) || startAngle > endAngle) {
119 return nullptr;
120 }
121 if (localMatrix && !localMatrix->invert(nullptr)) {
122 return nullptr;
123 }
124
125 if (SkScalarNearlyEqual(startAngle, endAngle, SkGradientBaseShader::kDegenerateThreshold)) {
126 // Degenerate gradient, which should follow default degenerate behavior unless it is
127 // clamped and the angle is greater than 0.
128 if (mode == SkTileMode::kClamp && endAngle > SkGradientBaseShader::kDegenerateThreshold) {
129 // In this case, the first color is repeated from 0 to the angle, then a hardstop
130 // switches to the last color (all other colors are compressed to the infinitely thin
131 // interpolation region).
132 static constexpr SkScalar clampPos[3] = {0, 1, 1};
133 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
134 return MakeSweep(cx, cy, reColors, std::move(colorSpace), clampPos, 3, mode, 0,
135 endAngle, interpolation, localMatrix);
136 } else {
137 return SkGradientBaseShader::MakeDegenerateGradient(
138 colors, pos, colorCount, std::move(colorSpace), mode);
139 }
140 }
141
142 if (startAngle <= 0 && endAngle >= 360) {
143 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
144 mode = SkTileMode::kClamp;
145 }
146
147 SkGradientBaseShader::Descriptor desc(
148 colors, std::move(colorSpace), pos, colorCount, mode, interpolation);
149
150 const SkScalar t0 = startAngle / 360,
151 t1 = endAngle / 360;
152
153 sk_sp<SkShader> s = sk_make_sp<SkSweepGradient>(SkPoint::Make(cx, cy), t0, t1, desc);
154 return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I());
155 }
156
MakeSweep(SkScalar cx,SkScalar cy,const SkColor colors[],const SkScalar pos[],int colorCount,SkTileMode mode,SkScalar startAngle,SkScalar endAngle,uint32_t flags,const SkMatrix * localMatrix)157 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
158 const SkColor colors[],
159 const SkScalar pos[],
160 int colorCount,
161 SkTileMode mode,
162 SkScalar startAngle,
163 SkScalar endAngle,
164 uint32_t flags,
165 const SkMatrix* localMatrix) {
166 SkColorConverter converter(colors, colorCount);
167 return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount,
168 mode, startAngle, endAngle, flags, localMatrix);
169 }
170
SkRegisterSweepGradientShaderFlattenable()171 void SkRegisterSweepGradientShaderFlattenable() {
172 SK_REGISTER_FLATTENABLE(SkSweepGradient);
173 }
174