Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSweepGradient.cpp
Go to the documentation of this file.
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
9
27
28#include <cstdint>
29#include <tuple>
30#include <utility>
31
32class SkArenaAlloc;
33
35 SkScalar t0,
36 SkScalar t1,
37 const Descriptor& desc)
38 : SkGradientBaseShader(desc, SkMatrix::Translate(-center.x(), -center.y()))
39 , fCenter(center)
40 , fTBias(-t0)
41 , fTScale(1 / (t1 - t0)) {
42 SkASSERT(t0 < t1);
43}
44
46 SkMatrix* localMatrix) const {
47 if (info) {
49 info->fPoint[0] = fCenter;
50 }
51 if (localMatrix) {
52 *localMatrix = SkMatrix::I();
53 }
54 return GradientType::kSweep;
55}
56
57static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) {
58 return std::make_tuple(-tBias * 360, (sk_ieee_float_divide(1, tScale) - tBias) * 360);
59}
60
61sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
62 DescriptorScope desc;
63 SkMatrix legacyLocalMatrix, *lmPtr = nullptr;
64 if (!desc.unflatten(buffer, &legacyLocalMatrix)) {
65 return nullptr;
66 }
67 if (!legacyLocalMatrix.isIdentity()) {
68 lmPtr = &legacyLocalMatrix;
69 }
70 const SkPoint center = buffer.readPoint();
71
72 const auto tBias = buffer.readScalar(),
73 tScale = buffer.readScalar();
74 auto [startAngle, endAngle] = angles_from_t_coeff(tBias, tScale);
75
77 desc.fColors,
78 std::move(desc.fColorSpace),
79 desc.fPositions,
80 desc.fColorCount,
81 desc.fTileMode,
82 startAngle,
83 endAngle,
84 desc.fInterpolation,
85 lmPtr);
86}
87
90 buffer.writePoint(fCenter);
91 buffer.writeScalar(fTBias);
92 buffer.writeScalar(fTScale);
93}
94
96 SkRasterPipeline*) const {
97 p->append(SkRasterPipelineOp::xy_to_unit_angle);
98 p->appendMatrix(alloc, SkMatrix::Scale(fTScale, 1) * SkMatrix::Translate(fTBias, 0));
99}
100
102 const SkColor4f colors[],
103 sk_sp<SkColorSpace> colorSpace,
104 const SkScalar pos[],
105 int colorCount,
106 SkTileMode mode,
107 SkScalar startAngle,
108 SkScalar endAngle,
109 const Interpolation& interpolation,
110 const SkMatrix* localMatrix) {
111 if (!SkGradientBaseShader::ValidGradient(colors, colorCount, mode, interpolation)) {
112 return nullptr;
113 }
114 if (1 == colorCount) {
115 return SkShaders::Color(colors[0], std::move(colorSpace));
116 }
117 if (!SkIsFinite(startAngle, endAngle) || startAngle > endAngle) {
118 return nullptr;
119 }
120 if (localMatrix && !localMatrix->invert(nullptr)) {
121 return nullptr;
122 }
123
125 // Degenerate gradient, which should follow default degenerate behavior unless it is
126 // clamped and the angle is greater than 0.
128 // In this case, the first color is repeated from 0 to the angle, then a hardstop
129 // switches to the last color (all other colors are compressed to the infinitely thin
130 // interpolation region).
131 static constexpr SkScalar clampPos[3] = {0, 1, 1};
132 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
133 return MakeSweep(cx, cy, reColors, std::move(colorSpace), clampPos, 3, mode, 0,
134 endAngle, interpolation, localMatrix);
135 } else {
137 colors, pos, colorCount, std::move(colorSpace), mode);
138 }
139 }
140
141 if (startAngle <= 0 && endAngle >= 360) {
142 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
143 mode = SkTileMode::kClamp;
144 }
145
147 colors, std::move(colorSpace), pos, colorCount, mode, interpolation);
148
149 const SkScalar t0 = startAngle / 360,
150 t1 = endAngle / 360;
151
152 return SkLocalMatrixShader::MakeWrapped<SkSweepGradient>(localMatrix,
153 SkPoint::Make(cx, cy),
154 t0, t1,
155 desc);
156}
157
159 const SkColor colors[],
160 const SkScalar pos[],
161 int colorCount,
162 SkTileMode mode,
163 SkScalar startAngle,
164 SkScalar endAngle,
165 uint32_t flags,
166 const SkMatrix* localMatrix) {
167 SkColorConverter converter(colors, colorCount);
168 return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount,
169 mode, startAngle, endAngle, flags, localMatrix);
170}
171
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkPoint pos
#define SkASSERT(cond)
Definition SkAssert.h:116
uint32_t SkColor
Definition SkColor.h:37
#define SK_REGISTER_FLATTENABLE(type)
static bool SkIsFinite(T x, Pack... values)
static constexpr float sk_ieee_float_divide(float numer, float denom)
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:107
static std::tuple< SkScalar, SkScalar > angles_from_t_coeff(SkScalar tBias, SkScalar tScale)
void SkRegisterSweepGradientShaderFlattenable()
SkTileMode
Definition SkTileMode.h:13
static SkScalar center(float pos0, float pos1)
static constexpr SkScalar kDegenerateThreshold
static bool ValidGradient(const SkColor4f colors[], int count, SkTileMode tileMode, const Interpolation &interpolation)
static sk_sp< SkShader > MakeDegenerateGradient(const SkColor4f colors[], const SkScalar pos[], int colorCount, sk_sp< SkColorSpace > colorSpace, SkTileMode mode)
void flatten(SkWriteBuffer &) const override
void commonAsAGradient(GradientInfo *) const
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
bool invert(SkMatrix *inverse) const
Definition SkMatrix.h:1206
static const SkMatrix & I()
bool isIdentity() const
Definition SkMatrix.h:223
void flatten(SkWriteBuffer &buffer) const override
GradientType asGradient(GradientInfo *info, SkMatrix *localMatrix) const override
SkScalar tBias() const
SkScalar tScale() const
SkSweepGradient(const SkPoint &center, SkScalar t0, SkScalar t1, const Descriptor &)
const SkPoint & center() const
void appendGradientStages(SkArenaAlloc *alloc, SkRasterPipeline *tPipeline, SkRasterPipeline *postPipeline) const override
float SkScalar
Definition extension.cpp:12
FlutterSemanticsFlag flags
static const uint8_t buffer[]
double y
double x
SkScalar startAngle
Definition SkRecords.h:250
static constexpr SkPoint Make(float x, float y)
constexpr float y() const
constexpr float x() const