Flutter Engine
 
Loading...
Searching...
No Matches
gradient.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
12
13namespace flutter {
14
15typedef CanvasGradient
16 Gradient; // Because the C++ name doesn't match the Dart name.
17
19
20void CanvasGradient::Create(Dart_Handle wrapper) {
22 auto res = fml::MakeRefCounted<CanvasGradient>();
23 res->AssociateWithDartWrapper(wrapper);
24}
25
26void CanvasGradient::initLinear(const tonic::Float32List& end_points,
27 const tonic::Float32List& colors,
28 const tonic::Float32List& color_stops,
29 DlTileMode tile_mode,
30 const tonic::Float64List& matrix4) {
31 FML_DCHECK(end_points.num_elements() == 4);
32 FML_DCHECK(colors.num_elements() == (color_stops.num_elements() * 4) ||
33 color_stops.data() == nullptr);
34 int num_colors = colors.num_elements() / 4;
35
36 static_assert(sizeof(DlPoint) == sizeof(float) * 2,
37 "DlPoint doesn't use floats.");
38
39 DlMatrix dl_matrix;
40 bool has_matrix = matrix4.data() != nullptr;
41 if (has_matrix) {
42 dl_matrix = ToDlMatrix(matrix4);
43 }
44
45 DlPoint p0 = DlPoint(end_points[0], end_points[1]);
46 DlPoint p1 = DlPoint(end_points[2], end_points[3]);
47 dl_shader_ = DlColorSource::MakeLinear(p0, p1, num_colors, colors.data(),
48 color_stops.data(), tile_mode,
49 has_matrix ? &dl_matrix : nullptr);
50 // Just a sanity check, all gradient shaders should be thread-safe
51 FML_DCHECK(dl_shader_->isUIThreadSafe());
52}
53
54void CanvasGradient::initRadial(double center_x,
55 double center_y,
56 double radius,
57 const tonic::Float32List& colors,
58 const tonic::Float32List& color_stops,
59 DlTileMode tile_mode,
60 const tonic::Float64List& matrix4) {
61 FML_DCHECK(colors.num_elements() == (color_stops.num_elements() * 4) ||
62 color_stops.data() == nullptr);
63 int num_colors = colors.num_elements() / 4;
64
65 DlMatrix dl_matrix;
66 bool has_matrix = matrix4.data() != nullptr;
67 if (has_matrix) {
68 dl_matrix = ToDlMatrix(matrix4);
69 }
70
71 dl_shader_ = DlColorSource::MakeRadial(
72 DlPoint(SafeNarrow(center_x), SafeNarrow(center_y)), SafeNarrow(radius),
73 num_colors, colors.data(), color_stops.data(), tile_mode,
74 has_matrix ? &dl_matrix : nullptr);
75 // Just a sanity check, all gradient shaders should be thread-safe
76 FML_DCHECK(dl_shader_->isUIThreadSafe());
77}
78
79void CanvasGradient::initSweep(double center_x,
80 double center_y,
81 const tonic::Float32List& colors,
82 const tonic::Float32List& color_stops,
83 DlTileMode tile_mode,
84 double start_angle,
85 double end_angle,
86 const tonic::Float64List& matrix4) {
87 FML_DCHECK(colors.num_elements() == (color_stops.num_elements() * 4) ||
88 color_stops.data() == nullptr);
89 int num_colors = colors.num_elements() / 4;
90
91 DlMatrix dl_matrix;
92 bool has_matrix = matrix4.data() != nullptr;
93 if (has_matrix) {
94 dl_matrix = ToDlMatrix(matrix4);
95 }
96
97 dl_shader_ = DlColorSource::MakeSweep(
98 DlPoint(SafeNarrow(center_x), SafeNarrow(center_y)),
99 SafeNarrow(start_angle) * 180.0f / static_cast<float>(M_PI),
100 SafeNarrow(end_angle) * 180.0f / static_cast<float>(M_PI), num_colors,
101 colors.data(), color_stops.data(), tile_mode,
102 has_matrix ? &dl_matrix : nullptr);
103 // Just a sanity check, all gradient shaders should be thread-safe
104 FML_DCHECK(dl_shader_->isUIThreadSafe());
105}
106
108 double start_y,
109 double start_radius,
110 double end_x,
111 double end_y,
112 double end_radius,
113 const tonic::Float32List& colors,
114 const tonic::Float32List& color_stops,
115 DlTileMode tile_mode,
116 const tonic::Float64List& matrix4) {
117 FML_DCHECK(colors.num_elements() == (color_stops.num_elements() * 4) ||
118 color_stops.data() == nullptr);
119 int num_colors = colors.num_elements() / 4;
120
121 DlMatrix dl_matrix;
122 bool has_matrix = matrix4.data() != nullptr;
123 if (has_matrix) {
124 dl_matrix = ToDlMatrix(matrix4);
125 }
126
127 dl_shader_ = DlColorSource::MakeConical(
128 DlPoint(SafeNarrow(start_x), SafeNarrow(start_y)),
129 SafeNarrow(start_radius), DlPoint(SafeNarrow(end_x), SafeNarrow(end_y)),
130 SafeNarrow(end_radius), num_colors, colors.data(), color_stops.data(),
131 tile_mode, has_matrix ? &dl_matrix : nullptr);
132 // Just a sanity check, all gradient shaders should be thread-safe
133 FML_DCHECK(dl_shader_->isUIThreadSafe());
134}
135
136CanvasGradient::CanvasGradient() = default;
137
139
140} // namespace flutter
void initLinear(const tonic::Float32List &end_points, const tonic::Float32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:26
static void Create(Dart_Handle wrapper)
Definition gradient.cc:20
void initSweep(double center_x, double center_y, const tonic::Float32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, double start_angle, double end_angle, const tonic::Float64List &matrix4)
Definition gradient.cc:79
void initTwoPointConical(double start_x, double start_y, double start_radius, double end_x, double end_y, double end_radius, const tonic::Float32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:107
void initRadial(double center_x, double center_y, double radius, const tonic::Float32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:54
static std::shared_ptr< DlColorSource > MakeSweep(DlPoint center, DlScalar start, DlScalar end, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const DlMatrix *matrix=nullptr)
static std::shared_ptr< DlColorSource > MakeLinear(const DlPoint start_point, const DlPoint end_point, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const DlMatrix *matrix=nullptr)
static std::shared_ptr< DlColorSource > MakeConical(DlPoint start_center, DlScalar start_radius, DlPoint end_center, DlScalar end_radius, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const DlMatrix *matrix=nullptr)
static std::shared_ptr< DlColorSource > MakeRadial(DlPoint center, DlScalar radius, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const DlMatrix *matrix=nullptr)
static void ThrowIfUIOperationsProhibited()
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_DCHECK(condition)
Definition logging.h:122
static float SafeNarrow(double value)
CanvasGradient Gradient
Definition dart_ui.cc:53
DlMatrix ToDlMatrix(const SkMatrix &matrix)
impeller::Point DlPoint
A 4x4 matrix using column-major storage.
Definition matrix.h:37