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 }
50 if (localMatrix) {
51 *localMatrix = SkMatrix::I();
52 }
53 return GradientType::kSweep;
54 }
55
angles_from_t_coeff(SkScalar tBias,SkScalar tScale)56 static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) {
57 return std::make_tuple(-tBias * 360, (sk_ieee_float_divide(1, tScale) - tBias) * 360);
58 }
59
CreateProc(SkReadBuffer & buffer)60 sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
61 DescriptorScope desc;
62 SkMatrix legacyLocalMatrix, *lmPtr = nullptr;
63 if (!desc.unflatten(buffer, &legacyLocalMatrix)) {
64 return nullptr;
65 }
66 if (!legacyLocalMatrix.isIdentity()) {
67 lmPtr = &legacyLocalMatrix;
68 }
69 const SkPoint center = buffer.readPoint();
70
71 const auto tBias = buffer.readScalar(),
72 tScale = buffer.readScalar();
73 auto [startAngle, endAngle] = angles_from_t_coeff(tBias, tScale);
74
75 return SkGradientShader::MakeSweep(center.x(), center.y(),
76 desc.fColors,
77 std::move(desc.fColorSpace),
78 desc.fPositions,
79 desc.fColorCount,
80 desc.fTileMode,
81 startAngle,
82 endAngle,
83 desc.fInterpolation,
84 lmPtr);
85 }
86
flatten(SkWriteBuffer & buffer) const87 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
88 this->SkGradientBaseShader::flatten(buffer);
89 buffer.writePoint(fCenter);
90 buffer.writeScalar(fTBias);
91 buffer.writeScalar(fTScale);
92 }
93
appendGradientStages(SkArenaAlloc * alloc,SkRasterPipeline * p,SkRasterPipeline *) const94 void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
95 SkRasterPipeline*) const {
96 p->append(SkRasterPipelineOp::xy_to_unit_angle);
97 p->appendMatrix(alloc, SkMatrix::Scale(fTScale, 1) * SkMatrix::Translate(fTBias, 0));
98 }
99
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)100 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
101 const SkColor4f colors[],
102 sk_sp<SkColorSpace> colorSpace,
103 const SkScalar pos[],
104 int colorCount,
105 SkTileMode mode,
106 SkScalar startAngle,
107 SkScalar endAngle,
108 const Interpolation& interpolation,
109 const SkMatrix* localMatrix) {
110 if (!SkGradientBaseShader::ValidGradient(colors, colorCount, mode, interpolation)) {
111 return nullptr;
112 }
113 if (1 == colorCount) {
114 return SkShaders::Color(colors[0], std::move(colorSpace));
115 }
116 if (!SkIsFinite(startAngle, endAngle) || startAngle > endAngle) {
117 return nullptr;
118 }
119 if (localMatrix && !localMatrix->invert(nullptr)) {
120 return nullptr;
121 }
122
123 if (SkScalarNearlyEqual(startAngle, endAngle, SkGradientBaseShader::kDegenerateThreshold)) {
124 // Degenerate gradient, which should follow default degenerate behavior unless it is
125 // clamped and the angle is greater than 0.
126 if (mode == SkTileMode::kClamp && endAngle > SkGradientBaseShader::kDegenerateThreshold) {
127 // In this case, the first color is repeated from 0 to the angle, then a hardstop
128 // switches to the last color (all other colors are compressed to the infinitely thin
129 // interpolation region).
130 static constexpr SkScalar clampPos[3] = {0, 1, 1};
131 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
132 return MakeSweep(cx, cy, reColors, std::move(colorSpace), clampPos, 3, mode, 0,
133 endAngle, interpolation, localMatrix);
134 } else {
135 return SkGradientBaseShader::MakeDegenerateGradient(
136 colors, pos, colorCount, std::move(colorSpace), mode);
137 }
138 }
139
140 if (startAngle <= 0 && endAngle >= 360) {
141 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
142 mode = SkTileMode::kClamp;
143 }
144
145 SkGradientBaseShader::Descriptor desc(
146 colors, std::move(colorSpace), pos, colorCount, mode, interpolation);
147
148 const SkScalar t0 = startAngle / 360,
149 t1 = endAngle / 360;
150
151 sk_sp<SkShader> s = sk_make_sp<SkSweepGradient>(SkPoint::Make(cx, cy), t0, t1, desc);
152 return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I());
153 }
154
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)155 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
156 const SkColor colors[],
157 const SkScalar pos[],
158 int colorCount,
159 SkTileMode mode,
160 SkScalar startAngle,
161 SkScalar endAngle,
162 uint32_t flags,
163 const SkMatrix* localMatrix) {
164 SkColorConverter converter(colors, colorCount);
165 return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount,
166 mode, startAngle, endAngle, flags, localMatrix);
167 }
168
SkRegisterSweepGradientShaderFlattenable()169 void SkRegisterSweepGradientShaderFlattenable() {
170 SK_REGISTER_FLATTENABLE(SkSweepGradient);
171 }
172