Flutter Engine
The Flutter Engine
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
26
27#include <cstdint>
28#include <tuple>
29#include <utility>
30
31class SkArenaAlloc;
32
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
45 SkMatrix* localMatrix) const {
46 if (info) {
48 info->fPoint[0] = fCenter;
49 }
50 if (localMatrix) {
51 *localMatrix = SkMatrix::I();
52 }
53 return GradientType::kSweep;
54}
55
56static 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
60sk_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
76 desc.fColors,
77 std::move(desc.fColorSpace),
78 desc.fPositions,
79 desc.fColorCount,
80 desc.fTileMode,
82 endAngle,
83 desc.fInterpolation,
84 lmPtr);
85}
86
89 buffer.writePoint(fCenter);
90 buffer.writeScalar(fTBias);
91 buffer.writeScalar(fTScale);
92}
93
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
101 const SkColor4f colors[],
102 sk_sp<SkColorSpace> colorSpace,
103 const SkScalar pos[],
104 int colorCount,
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
124 // Degenerate gradient, which should follow default degenerate behavior unless it is
125 // clamped and the angle is greater than 0.
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 {
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).
143 }
144
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
156 const SkColor colors[],
157 const SkScalar pos[],
158 int colorCount,
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
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()
Definition: SkMatrix.cpp:1544
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
struct MyStruct s
FlutterSemanticsFlag flags
double y
double x
SkScalar startAngle
Definition: SkRecords.h:250
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkShader > Color(SkColor)
string converter
Definition: cacheimages.py:19
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181