Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 92de3d0

Browse files
authored
Removed heap allocations for conical, radial and sweep gradients (#57143)
fixes flutter/flutter#154650 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/engine/blob/main/docs/testing/Testing-the-engine.md [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
1 parent e352461 commit 92de3d0

12 files changed

+235
-137
lines changed

display_list/effects/color_sources/dl_conical_gradient_color_source.cc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,6 @@
66

77
namespace flutter {
88

9-
DlConicalGradientColorSource::DlConicalGradientColorSource(
10-
DlPoint start_center,
11-
DlScalar start_radius,
12-
DlPoint end_center,
13-
DlScalar end_radius,
14-
uint32_t stop_count,
15-
const DlColor* colors,
16-
const float* stops,
17-
DlTileMode tile_mode,
18-
const DlMatrix* matrix)
19-
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
20-
start_center_(start_center),
21-
start_radius_(start_radius),
22-
end_center_(end_center),
23-
end_radius_(end_radius) {
24-
store_color_stops(this + 1, colors, stops);
25-
}
26-
279
DlConicalGradientColorSource::DlConicalGradientColorSource(
2810
const DlConicalGradientColorSource* source)
2911
: DlGradientColorSourceBase(source->stop_count(),

display_list/effects/color_sources/dl_conical_gradient_color_source.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,23 @@ class DlConicalGradientColorSource final : public DlGradientColorSourceBase {
3535
bool equals_(DlColorSource const& other) const override;
3636

3737
private:
38+
template <typename Colors>
3839
DlConicalGradientColorSource(DlPoint start_center,
3940
DlScalar start_radius,
4041
DlPoint end_center,
4142
DlScalar end_radius,
4243
uint32_t stop_count,
43-
const DlColor* colors,
44+
Colors colors,
4445
const float* stops,
4546
DlTileMode tile_mode,
46-
const DlMatrix* matrix = nullptr);
47+
const DlMatrix* matrix = nullptr)
48+
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
49+
start_center_(start_center),
50+
start_radius_(start_radius),
51+
end_center_(end_center),
52+
end_radius_(end_radius) {
53+
store_color_stops(this + 1, colors, stops);
54+
}
4755

4856
explicit DlConicalGradientColorSource(
4957
const DlConicalGradientColorSource* source);

display_list/effects/color_sources/dl_linear_gradient_color_source.cc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,6 @@
66

77
namespace flutter {
88

9-
DlLinearGradientColorSource::DlLinearGradientColorSource(
10-
const DlPoint start_point,
11-
const DlPoint end_point,
12-
uint32_t stop_count,
13-
const DlColor* colors,
14-
const float* stops,
15-
DlTileMode tile_mode,
16-
const DlMatrix* matrix)
17-
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
18-
start_point_(start_point),
19-
end_point_(end_point) {
20-
store_color_stops(this + 1, colors, stops);
21-
}
22-
23-
DlLinearGradientColorSource::DlLinearGradientColorSource(
24-
const DlPoint start_point,
25-
const DlPoint end_point,
26-
uint32_t stop_count,
27-
const DlScalar* colors,
28-
const float* stops,
29-
DlTileMode tile_mode,
30-
const DlMatrix* matrix)
31-
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
32-
start_point_(start_point),
33-
end_point_(end_point) {
34-
store_color_stops(this + 1, colors, stops);
35-
}
36-
379
DlLinearGradientColorSource::DlLinearGradientColorSource(
3810
const DlLinearGradientColorSource* source)
3911
: DlGradientColorSourceBase(source->stop_count(),

display_list/effects/color_sources/dl_linear_gradient_color_source.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ class DlLinearGradientColorSource final : public DlGradientColorSourceBase {
3333
bool equals_(DlColorSource const& other) const override;
3434

3535
private:
36+
template <typename Colors>
3637
DlLinearGradientColorSource(const DlPoint start_point,
3738
const DlPoint end_point,
3839
uint32_t stop_count,
39-
const DlColor* colors,
40+
Colors colors,
4041
const float* stops,
4142
DlTileMode tile_mode,
42-
const DlMatrix* matrix = nullptr);
43-
44-
DlLinearGradientColorSource(const DlPoint start_point,
45-
const DlPoint end_point,
46-
uint32_t stop_count,
47-
const DlScalar* colors,
48-
const float* stops,
49-
DlTileMode tile_mode,
50-
const DlMatrix* matrix = nullptr);
43+
const DlMatrix* matrix = nullptr)
44+
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
45+
start_point_(start_point),
46+
end_point_(end_point) {
47+
store_color_stops(this + 1, colors, stops);
48+
}
5149

5250
explicit DlLinearGradientColorSource(
5351
const DlLinearGradientColorSource* source);

display_list/effects/color_sources/dl_radial_gradient_color_source.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,6 @@
66

77
namespace flutter {
88

9-
DlRadialGradientColorSource::DlRadialGradientColorSource(DlPoint center,
10-
DlScalar radius,
11-
uint32_t stop_count,
12-
const DlColor* colors,
13-
const float* stops,
14-
DlTileMode tile_mode,
15-
const DlMatrix* matrix)
16-
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
17-
center_(center),
18-
radius_(radius) {
19-
store_color_stops(this + 1, colors, stops);
20-
}
21-
229
DlRadialGradientColorSource::DlRadialGradientColorSource(
2310
const DlRadialGradientColorSource* source)
2411
: DlGradientColorSourceBase(source->stop_count(),

display_list/effects/color_sources/dl_radial_gradient_color_source.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ class DlRadialGradientColorSource final : public DlGradientColorSourceBase {
3333
bool equals_(DlColorSource const& other) const override;
3434

3535
private:
36+
template <typename Colors>
3637
DlRadialGradientColorSource(DlPoint center,
3738
DlScalar radius,
3839
uint32_t stop_count,
39-
const DlColor* colors,
40+
Colors colors,
4041
const float* stops,
4142
DlTileMode tile_mode,
42-
const DlMatrix* matrix = nullptr);
43+
const DlMatrix* matrix = nullptr)
44+
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
45+
center_(center),
46+
radius_(radius) {
47+
store_color_stops(this + 1, colors, stops);
48+
}
4349

4450
explicit DlRadialGradientColorSource(
4551
const DlRadialGradientColorSource* source);

display_list/effects/color_sources/dl_sweep_gradient_color_source.cc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66

77
namespace flutter {
88

9-
DlSweepGradientColorSource::DlSweepGradientColorSource(DlPoint center,
10-
DlScalar start,
11-
DlScalar end,
12-
uint32_t stop_count,
13-
const DlColor* colors,
14-
const float* stops,
15-
DlTileMode tile_mode,
16-
const DlMatrix* matrix)
17-
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
18-
center_(center),
19-
start_(start),
20-
end_(end) {
21-
store_color_stops(this + 1, colors, stops);
22-
}
23-
249
DlSweepGradientColorSource::DlSweepGradientColorSource(
2510
const DlSweepGradientColorSource* source)
2611
: DlGradientColorSourceBase(source->stop_count(),

display_list/effects/color_sources/dl_sweep_gradient_color_source.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,21 @@ class DlSweepGradientColorSource final : public DlGradientColorSourceBase {
3434
bool equals_(DlColorSource const& other) const override;
3535

3636
private:
37+
template <typename Colors>
3738
DlSweepGradientColorSource(DlPoint center,
3839
DlScalar start,
3940
DlScalar end,
4041
uint32_t stop_count,
41-
const DlColor* colors,
42+
Colors colors,
4243
const float* stops,
4344
DlTileMode tile_mode,
44-
const DlMatrix* matrix = nullptr);
45+
const DlMatrix* matrix = nullptr)
46+
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
47+
center_(center),
48+
start_(start),
49+
end_(end) {
50+
store_color_stops(this + 1, colors, stops);
51+
}
4552

4653
explicit DlSweepGradientColorSource(const DlSweepGradientColorSource* source);
4754

display_list/effects/dl_color_source.cc

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeImage(
3131
}
3232

3333
namespace {
34-
size_t CalculateLinearGradientSize(uint32_t stop_count) {
35-
return sizeof(DlLinearGradientColorSource) +
34+
template <typename GradientColorSource>
35+
size_t CalculateGradientSize(uint32_t stop_count) {
36+
return sizeof(GradientColorSource) +
3637
(stop_count * (sizeof(DlColor) + sizeof(float)));
3738
}
3839
} // namespace
@@ -45,7 +46,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeLinear(
4546
const float* stops,
4647
DlTileMode tile_mode,
4748
const DlMatrix* matrix) {
48-
size_t needed = CalculateLinearGradientSize(stop_count);
49+
size_t needed =
50+
CalculateGradientSize<DlLinearGradientColorSource>(stop_count);
4951
void* storage = ::operator new(needed);
5052

5153
std::shared_ptr<DlLinearGradientColorSource> ret;
@@ -64,7 +66,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeLinear(
6466
const float* stops,
6567
DlTileMode tile_mode,
6668
const DlMatrix* matrix) {
67-
size_t needed = CalculateLinearGradientSize(stop_count);
69+
size_t needed =
70+
CalculateGradientSize<DlLinearGradientColorSource>(stop_count);
6871
void* storage = ::operator new(needed);
6972

7073
std::shared_ptr<DlLinearGradientColorSource> ret;
@@ -83,9 +86,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
8386
const float* stops,
8487
DlTileMode tile_mode,
8588
const DlMatrix* matrix) {
86-
size_t needed = sizeof(DlRadialGradientColorSource) +
87-
(stop_count * (sizeof(DlColor) + sizeof(float)));
88-
89+
size_t needed =
90+
CalculateGradientSize<DlRadialGradientColorSource>(stop_count);
8991
void* storage = ::operator new(needed);
9092

9193
std::shared_ptr<DlRadialGradientColorSource> ret;
@@ -95,6 +97,26 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
9597
return ret;
9698
}
9799

100+
std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
101+
DlPoint center,
102+
DlScalar radius,
103+
uint32_t stop_count,
104+
const DlScalar* colors_argb,
105+
const float* stops,
106+
DlTileMode tile_mode,
107+
const DlMatrix* matrix) {
108+
size_t needed =
109+
CalculateGradientSize<DlRadialGradientColorSource>(stop_count);
110+
void* storage = ::operator new(needed);
111+
112+
std::shared_ptr<DlRadialGradientColorSource> ret;
113+
ret.reset(
114+
new (storage) DlRadialGradientColorSource(
115+
center, radius, stop_count, colors_argb, stops, tile_mode, matrix),
116+
DlGradientDeleter);
117+
return ret;
118+
}
119+
98120
std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
99121
DlPoint start_center,
100122
DlScalar start_radius,
@@ -105,9 +127,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
105127
const float* stops,
106128
DlTileMode tile_mode,
107129
const DlMatrix* matrix) {
108-
size_t needed = sizeof(DlConicalGradientColorSource) +
109-
(stop_count * (sizeof(DlColor) + sizeof(float)));
110-
130+
size_t needed =
131+
CalculateGradientSize<DlConicalGradientColorSource>(stop_count);
111132
void* storage = ::operator new(needed);
112133

113134
std::shared_ptr<DlConicalGradientColorSource> ret;
@@ -118,6 +139,29 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
118139
return ret;
119140
}
120141

142+
std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
143+
DlPoint start_center,
144+
DlScalar start_radius,
145+
DlPoint end_center,
146+
DlScalar end_radius,
147+
uint32_t stop_count,
148+
const DlScalar* colors_argb,
149+
const float* stops,
150+
DlTileMode tile_mode,
151+
const DlMatrix* matrix) {
152+
size_t needed =
153+
CalculateGradientSize<DlConicalGradientColorSource>(stop_count);
154+
155+
void* storage = ::operator new(needed);
156+
157+
std::shared_ptr<DlConicalGradientColorSource> ret;
158+
ret.reset(new (storage) DlConicalGradientColorSource(
159+
start_center, start_radius, end_center, end_radius, stop_count,
160+
colors_argb, stops, tile_mode, matrix),
161+
DlGradientDeleter);
162+
return ret;
163+
}
164+
121165
std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
122166
DlPoint center,
123167
DlScalar start,
@@ -127,9 +171,7 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
127171
const float* stops,
128172
DlTileMode tile_mode,
129173
const DlMatrix* matrix) {
130-
size_t needed = sizeof(DlSweepGradientColorSource) +
131-
(stop_count * (sizeof(DlColor) + sizeof(float)));
132-
174+
size_t needed = CalculateGradientSize<DlSweepGradientColorSource>(stop_count);
133175
void* storage = ::operator new(needed);
134176

135177
std::shared_ptr<DlSweepGradientColorSource> ret;
@@ -140,6 +182,26 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
140182
return ret;
141183
}
142184

185+
std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
186+
DlPoint center,
187+
DlScalar start,
188+
DlScalar end,
189+
uint32_t stop_count,
190+
const DlScalar* colors_argb,
191+
const float* stops,
192+
DlTileMode tile_mode,
193+
const DlMatrix* matrix) {
194+
size_t needed = CalculateGradientSize<DlSweepGradientColorSource>(stop_count);
195+
void* storage = ::operator new(needed);
196+
197+
std::shared_ptr<DlSweepGradientColorSource> ret;
198+
ret.reset(new (storage) DlSweepGradientColorSource(center, start, end,
199+
stop_count, colors_argb,
200+
stops, tile_mode, matrix),
201+
DlGradientDeleter);
202+
return ret;
203+
}
204+
143205
std::shared_ptr<DlColorSource> DlColorSource::MakeRuntimeEffect(
144206
sk_sp<DlRuntimeEffect> runtime_effect,
145207
std::vector<std::shared_ptr<DlColorSource>> samplers,

0 commit comments

Comments
 (0)