Flutter Engine
The Flutter Engine
SkLinearGradient.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
22
23#include <cstdint>
24#include <utility>
25
26class SkArenaAlloc;
28enum class SkTileMode;
29
30static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) {
31 SkVector vec = pts[1] - pts[0];
32 SkScalar mag = vec.length();
33 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
34
35 vec.scale(inv);
37 matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
38 matrix.postTranslate(-pts[0].fX, -pts[0].fY);
39 matrix.postScale(inv, inv);
40 return matrix;
41}
42
43///////////////////////////////////////////////////////////////////////////////
44
46 : SkGradientBaseShader(desc, pts_to_unit_matrix(pts)), fStart(pts[0]), fEnd(pts[1]) {}
47
48sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) {
49 DescriptorScope desc;
50 SkMatrix legacyLocalMatrix, *lmPtr = nullptr;
51 if (!desc.unflatten(buffer, &legacyLocalMatrix)) {
52 return nullptr;
53 }
54 if (!legacyLocalMatrix.isIdentity()) {
55 lmPtr = &legacyLocalMatrix;
56 }
57 SkPoint pts[2];
58 pts[0] = buffer.readPoint();
59 pts[1] = buffer.readPoint();
61 desc.fColors,
62 std::move(desc.fColorSpace),
63 desc.fPositions,
64 desc.fColorCount,
65 desc.fTileMode,
66 desc.fInterpolation,
67 lmPtr);
68}
69
71 this->INHERITED::flatten(buffer);
72 buffer.writePoint(fStart);
73 buffer.writePoint(fEnd);
74}
75
77 SkRasterPipeline*) const {
78 // No extra stage needed for linear gradients.
79}
80
82 SkMatrix* localMatrix) const {
83 if (info) {
85 info->fPoint[0] = fStart;
86 info->fPoint[1] = fEnd;
87 }
88 if (localMatrix) {
89 *localMatrix = SkMatrix::I();
90 }
92}
93
95 const SkColor4f colors[],
96 sk_sp<SkColorSpace> colorSpace,
97 const SkScalar pos[],
98 int colorCount,
100 const Interpolation& interpolation,
101 const SkMatrix* localMatrix) {
102 if (!pts || !SkIsFinite((pts[1] - pts[0]).length())) {
103 return nullptr;
104 }
105 if (!SkGradientBaseShader::ValidGradient(colors, colorCount, mode, interpolation)) {
106 return nullptr;
107 }
108 if (1 == colorCount) {
109 return SkShaders::Color(colors[0], std::move(colorSpace));
110 }
111 if (localMatrix && !localMatrix->invert(nullptr)) {
112 return nullptr;
113 }
114
115 if (SkScalarNearlyZero((pts[1] - pts[0]).length(),
117 // Degenerate gradient, the only tricky complication is when in clamp mode, the limit of
118 // the gradient approaches two half planes of solid color (first and last). However, they
119 // are divided by the line perpendicular to the start and end point, which becomes undefined
120 // once start and end are exactly the same, so just use the end color for a stable solution.
122 colors, pos, colorCount, std::move(colorSpace), mode);
123 }
124
126 colors, std::move(colorSpace), pos, colorCount, mode, interpolation);
127
128 sk_sp<SkShader> s = sk_make_sp<SkLinearGradient>(pts, desc);
129 return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I());
130}
131
133 const SkColor colors[],
134 const SkScalar pos[],
135 int colorCount,
137 uint32_t flags,
138 const SkMatrix* localMatrix) {
139 SkColorConverter converter(colors, colorCount);
140 return MakeLinear(pts, converter.fColors4f.begin(), nullptr, pos, colorCount, mode, flags,
141 localMatrix);
142}
143
146}
static SkM44 inv(const SkM44 &m)
Definition: 3d.cpp:26
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkPoint pos
uint32_t SkColor
Definition: SkColor.h:37
#define SK_REGISTER_FLATTENABLE(type)
static bool SkIsFinite(T x, Pack... values)
void SkRegisterLinearGradientShaderFlattenable()
static SkMatrix pts_to_unit_matrix(const SkPoint pts[2])
#define SkScalarInvert(x)
Definition: SkScalar.h:73
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
SkTileMode
Definition: SkTileMode.h:13
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 > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
SkLinearGradient(const SkPoint pts[2], const Descriptor &)
void flatten(SkWriteBuffer &buffer) const override
void appendGradientStages(SkArenaAlloc *alloc, SkRasterPipeline *tPipeline, SkRasterPipeline *postPipeline) const final
GradientType asGradient(GradientInfo *info, SkMatrix *localMatrix) const override
bool invert(SkMatrix *inverse) const
Definition: SkMatrix.h:1206
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
bool isIdentity() const
Definition: SkMatrix.h:223
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
FlutterSemanticsFlag flags
size_t length
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:51
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
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
float fX
x-axis value
Definition: SkPoint_impl.h:164
float length() const
Definition: SkPoint_impl.h:282
void scale(float scale, SkPoint *dst) const
Definition: SkPoint.cpp:17
float fY
y-axis value
Definition: SkPoint_impl.h:165