Flutter Engine
The 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
5#include "flutter/lib/ui/painting/gradient.h"
6
7#include "flutter/lib/ui/floating_point.h"
12
13namespace flutter {
14
15typedef CanvasGradient
16 Gradient; // Because the C++ name doesn't match the Dart name.
17
19
22 auto res = fml::MakeRefCounted<CanvasGradient>();
23 res->AssociateWithDartWrapper(wrapper);
24}
25
26void CanvasGradient::initLinear(const tonic::Float32List& end_points,
27 const tonic::Int32List& 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() ||
33 color_stops.data() == nullptr);
34
35 static_assert(sizeof(SkPoint) == sizeof(float) * 2,
36 "SkPoint doesn't use floats.");
37 static_assert(sizeof(SkColor) == sizeof(int32_t),
38 "SkColor doesn't use int32_t.");
39
41 bool has_matrix = matrix4.data() != nullptr;
42 if (has_matrix) {
43 sk_matrix = ToSkMatrix(matrix4);
44 }
45
46 SkPoint p0 = SkPoint::Make(end_points[0], end_points[1]);
47 SkPoint p1 = SkPoint::Make(end_points[2], end_points[3]);
48 const DlColor* colors_array = reinterpret_cast<const DlColor*>(colors.data());
49
50 dl_shader_ = DlColorSource::MakeLinear(
51 p0, p1, colors.num_elements(), colors_array, color_stops.data(),
52 tile_mode, has_matrix ? &sk_matrix : nullptr);
53 // Just a sanity check, all gradient shaders should be thread-safe
54 FML_DCHECK(dl_shader_->isUIThreadSafe());
55}
56
57void CanvasGradient::initRadial(double center_x,
58 double center_y,
59 double radius,
60 const tonic::Int32List& colors,
61 const tonic::Float32List& color_stops,
62 DlTileMode tile_mode,
63 const tonic::Float64List& matrix4) {
64 FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
65 color_stops.data() == nullptr);
66
67 static_assert(sizeof(SkColor) == sizeof(int32_t),
68 "SkColor doesn't use int32_t.");
69
71 bool has_matrix = matrix4.data() != nullptr;
72 if (has_matrix) {
73 sk_matrix = ToSkMatrix(matrix4);
74 }
75
76 const DlColor* colors_array = reinterpret_cast<const DlColor*>(colors.data());
77
78 dl_shader_ = DlColorSource::MakeRadial(
79 SkPoint::Make(SafeNarrow(center_x), SafeNarrow(center_y)),
80 SafeNarrow(radius), colors.num_elements(), colors_array,
81 color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr);
82 // Just a sanity check, all gradient shaders should be thread-safe
83 FML_DCHECK(dl_shader_->isUIThreadSafe());
84}
85
86void CanvasGradient::initSweep(double center_x,
87 double center_y,
88 const tonic::Int32List& colors,
89 const tonic::Float32List& color_stops,
90 DlTileMode tile_mode,
91 double start_angle,
92 double end_angle,
93 const tonic::Float64List& matrix4) {
94 FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
95 color_stops.data() == nullptr);
96
97 static_assert(sizeof(SkColor) == sizeof(int32_t),
98 "SkColor doesn't use int32_t.");
99
101 bool has_matrix = matrix4.data() != nullptr;
102 if (has_matrix) {
103 sk_matrix = ToSkMatrix(matrix4);
104 }
105
106 const DlColor* colors_array = reinterpret_cast<const DlColor*>(colors.data());
107
108 dl_shader_ = DlColorSource::MakeSweep(
109 SkPoint::Make(SafeNarrow(center_x), SafeNarrow(center_y)),
110 SafeNarrow(start_angle) * 180.0f / static_cast<float>(M_PI),
111 SafeNarrow(end_angle) * 180.0f / static_cast<float>(M_PI),
112 colors.num_elements(), colors_array, color_stops.data(), tile_mode,
113 has_matrix ? &sk_matrix : nullptr);
114 // Just a sanity check, all gradient shaders should be thread-safe
115 FML_DCHECK(dl_shader_->isUIThreadSafe());
116}
117
119 double start_y,
120 double start_radius,
121 double end_x,
122 double end_y,
123 double end_radius,
124 const tonic::Int32List& colors,
125 const tonic::Float32List& color_stops,
126 DlTileMode tile_mode,
127 const tonic::Float64List& matrix4) {
128 FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
129 color_stops.data() == nullptr);
130
131 static_assert(sizeof(SkColor) == sizeof(int32_t),
132 "SkColor doesn't use int32_t.");
133
135 bool has_matrix = matrix4.data() != nullptr;
136 if (has_matrix) {
137 sk_matrix = ToSkMatrix(matrix4);
138 }
139
140 const DlColor* colors_array = reinterpret_cast<const DlColor*>(colors.data());
141
142 dl_shader_ = DlColorSource::MakeConical(
143 SkPoint::Make(SafeNarrow(start_x), SafeNarrow(start_y)),
144 SafeNarrow(start_radius),
145 SkPoint::Make(SafeNarrow(end_x), SafeNarrow(end_y)),
146 SafeNarrow(end_radius), colors.num_elements(), colors_array,
147 color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr);
148 // Just a sanity check, all gradient shaders should be thread-safe
149 FML_DCHECK(dl_shader_->isUIThreadSafe());
150}
151
152CanvasGradient::CanvasGradient() = default;
153
155
156} // namespace flutter
#define M_PI
uint32_t SkColor
Definition SkColor.h:37
static void Create(Dart_Handle wrapper)
Definition gradient.cc:20
void initRadial(double center_x, double center_y, double radius, const tonic::Int32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:57
void initLinear(const tonic::Float32List &end_points, const tonic::Int32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:26
void initTwoPointConical(double start_x, double start_y, double start_radius, double end_x, double end_y, double end_radius, const tonic::Int32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, const tonic::Float64List &matrix4)
Definition gradient.cc:118
void initSweep(double center_x, double center_y, const tonic::Int32List &colors, const tonic::Float32List &color_stops, DlTileMode tile_mode, double start_angle, double end_angle, const tonic::Float64List &matrix4)
Definition gradient.cc:86
static std::shared_ptr< DlConicalGradientColorSource > MakeConical(SkPoint start_center, SkScalar start_radius, SkPoint end_center, SkScalar end_radius, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
static std::shared_ptr< DlLinearGradientColorSource > MakeLinear(const SkPoint start_point, const SkPoint end_point, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
static std::shared_ptr< DlSweepGradientColorSource > MakeSweep(SkPoint center, SkScalar start, SkScalar end, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
static std::shared_ptr< DlRadialGradientColorSource > MakeRadial(SkPoint center, SkScalar radius, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
static void ThrowIfUIOperationsProhibited()
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
SkMatrix sk_matrix
#define FML_DCHECK(condition)
Definition logging.h:103
static float SafeNarrow(double value)
CanvasGradient Gradient
Definition dart_ui.cc:57
SkMatrix ToSkMatrix(const tonic::Float64List &matrix4)
Definition matrix.cc:32
static constexpr SkPoint Make(float x, float y)