Debian Codegraph @

src/shaders/gradients/SkSweepGradient.cpp

raw
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/SkGradient.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
33 SkSweepGradient::SkSweepGradient(const SkPoint& center,
34 SkScalar t0,
35 SkScalar t1,
36 const SkGradient& 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
44 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
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
62 sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
63 SkGradientScope scope;
64 SkMatrix legacyLocalMatrix, *lmPtr = nullptr;
65 auto grad = scope.unflatten(buffer, &legacyLocalMatrix);
66 if (!grad) {
67 return nullptr;
68 }
69 if (!legacyLocalMatrix.isIdentity()) {
70 lmPtr = &legacyLocalMatrix;
71 }
72 const SkPoint center = buffer.readPoint();
73
74 const auto tBias = buffer.readScalar(),
75 tScale = buffer.readScalar();
76 auto [startAngle, endAngle] = angles_from_t_coeff(tBias, tScale);
77
78 return SkShaders::SweepGradient(center, startAngle, endAngle, *grad, lmPtr);
79 }
80
81 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
82 this->SkGradientBaseShader::flatten(buffer);
83 buffer.writePoint(fCenter);
84 buffer.writeScalar(fTBias);
85 buffer.writeScalar(fTScale);
86 }
87
88 void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
89 SkRasterPipeline*) const {
90 p->append(SkRasterPipelineOp::xy_to_unit_angle);
91 p->appendMatrix(alloc, SkMatrix::Scale(fTScale, 1) * SkMatrix::Translate(fTBias, 0));
92 }
93
94 sk_sp<SkShader> SkShaders::SweepGradient(SkPoint center, float startAngle, float endAngle,
95 const SkGradient& grad, const SkMatrix* lm) {
96 if (!SkIsFinite(startAngle, endAngle) || startAngle > endAngle) {
97 return nullptr;
98 }
99
100 GRADIENT_FACTORY_EARLY_EXIT(grad, lm);
101
102 const auto& colors = grad.colors();
103 const auto& interp = grad.interpolation();
104 SkTileMode mode = colors.tileMode();
105
106 if (SkScalarNearlyEqual(startAngle, endAngle, SkGradientBaseShader::kDegenerateThreshold)) {
107 // Degenerate gradient, which should follow default degenerate behavior unless it is
108 // clamped and the angle is greater than 0.
109 if (mode == SkTileMode::kClamp && endAngle > SkGradientBaseShader::kDegenerateThreshold) {
110 // In this case, the first color is repeated from 0 to the angle, then a hardstop
111 // switches to the last color (all other colors are compressed to the infinitely thin
112 // interpolation region).
113 static constexpr float clampPos[3] = {0, 1, 1};
114 SkSpan<const SkColor4f> srcColors = colors.colors();
115 const SkColor4f reColors[3] = {
116 srcColors.front(), srcColors.front(), srcColors.back()
117 };
118 SkGradient::Colors newColors = {
119 reColors, clampPos, colors.tileMode(), colors.colorSpace()
120 };
121 return SkShaders::SweepGradient(center, 0, endAngle, {newColors, interp}, lm);
122 } else {
123 return SkGradientBaseShader::MakeDegenerateGradient(colors);
124 }
125 }
126
127 if (startAngle <= 0 && endAngle >= 360) {
128 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
129 mode = SkTileMode::kClamp;
130 }
131
132 const SkGradient newGrad {
133 {colors.colors(), colors.positions(), mode, colors.colorSpace()},
134 interp
135 };
136
137 const float t0 = startAngle / 360,
138 t1 = endAngle / 360;
139
140 sk_sp<SkShader> s = sk_make_sp<SkSweepGradient>(center, t0, t1, newGrad);
141 return s->makeWithLocalMatrix(lm ? *lm : SkMatrix::I());
142 }
143
144 void SkRegisterSweepGradientShaderFlattenable() {
145 SK_REGISTER_FLATTENABLE(SkSweepGradient);
146 }