Skip to content

Commit

Permalink
Removed heap allocations for conical, radial and sweep gradients (#57143
Browse files Browse the repository at this point in the history
)

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
  • Loading branch information
gaaclarke authored Dec 11, 2024
1 parent e352461 commit 92de3d0
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@

namespace flutter {

DlConicalGradientColorSource::DlConicalGradientColorSource(
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)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
start_center_(start_center),
start_radius_(start_radius),
end_center_(end_center),
end_radius_(end_radius) {
store_color_stops(this + 1, colors, stops);
}

DlConicalGradientColorSource::DlConicalGradientColorSource(
const DlConicalGradientColorSource* source)
: DlGradientColorSourceBase(source->stop_count(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ class DlConicalGradientColorSource final : public DlGradientColorSourceBase {
bool equals_(DlColorSource const& other) const override;

private:
template <typename Colors>
DlConicalGradientColorSource(DlPoint start_center,
DlScalar start_radius,
DlPoint end_center,
DlScalar end_radius,
uint32_t stop_count,
const DlColor* colors,
Colors colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix = nullptr);
const DlMatrix* matrix = nullptr)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
start_center_(start_center),
start_radius_(start_radius),
end_center_(end_center),
end_radius_(end_radius) {
store_color_stops(this + 1, colors, stops);
}

explicit DlConicalGradientColorSource(
const DlConicalGradientColorSource* source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,6 @@

namespace flutter {

DlLinearGradientColorSource::DlLinearGradientColorSource(
const DlPoint start_point,
const DlPoint end_point,
uint32_t stop_count,
const DlColor* colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
start_point_(start_point),
end_point_(end_point) {
store_color_stops(this + 1, colors, stops);
}

DlLinearGradientColorSource::DlLinearGradientColorSource(
const DlPoint start_point,
const DlPoint end_point,
uint32_t stop_count,
const DlScalar* colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
start_point_(start_point),
end_point_(end_point) {
store_color_stops(this + 1, colors, stops);
}

DlLinearGradientColorSource::DlLinearGradientColorSource(
const DlLinearGradientColorSource* source)
: DlGradientColorSourceBase(source->stop_count(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,19 @@ class DlLinearGradientColorSource final : public DlGradientColorSourceBase {
bool equals_(DlColorSource const& other) const override;

private:
template <typename Colors>
DlLinearGradientColorSource(const DlPoint start_point,
const DlPoint end_point,
uint32_t stop_count,
const DlColor* colors,
Colors colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix = nullptr);

DlLinearGradientColorSource(const DlPoint start_point,
const DlPoint end_point,
uint32_t stop_count,
const DlScalar* colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix = nullptr);
const DlMatrix* matrix = nullptr)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
start_point_(start_point),
end_point_(end_point) {
store_color_stops(this + 1, colors, stops);
}

explicit DlLinearGradientColorSource(
const DlLinearGradientColorSource* source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@

namespace flutter {

DlRadialGradientColorSource::DlRadialGradientColorSource(DlPoint center,
DlScalar radius,
uint32_t stop_count,
const DlColor* colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
center_(center),
radius_(radius) {
store_color_stops(this + 1, colors, stops);
}

DlRadialGradientColorSource::DlRadialGradientColorSource(
const DlRadialGradientColorSource* source)
: DlGradientColorSourceBase(source->stop_count(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ class DlRadialGradientColorSource final : public DlGradientColorSourceBase {
bool equals_(DlColorSource const& other) const override;

private:
template <typename Colors>
DlRadialGradientColorSource(DlPoint center,
DlScalar radius,
uint32_t stop_count,
const DlColor* colors,
Colors colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix = nullptr);
const DlMatrix* matrix = nullptr)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
center_(center),
radius_(radius) {
store_color_stops(this + 1, colors, stops);
}

explicit DlRadialGradientColorSource(
const DlRadialGradientColorSource* source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@

namespace flutter {

DlSweepGradientColorSource::DlSweepGradientColorSource(DlPoint center,
DlScalar start,
DlScalar end,
uint32_t stop_count,
const DlColor* colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
center_(center),
start_(start),
end_(end) {
store_color_stops(this + 1, colors, stops);
}

DlSweepGradientColorSource::DlSweepGradientColorSource(
const DlSweepGradientColorSource* source)
: DlGradientColorSourceBase(source->stop_count(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ class DlSweepGradientColorSource final : public DlGradientColorSourceBase {
bool equals_(DlColorSource const& other) const override;

private:
template <typename Colors>
DlSweepGradientColorSource(DlPoint center,
DlScalar start,
DlScalar end,
uint32_t stop_count,
const DlColor* colors,
Colors colors,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix = nullptr);
const DlMatrix* matrix = nullptr)
: DlGradientColorSourceBase(stop_count, tile_mode, matrix),
center_(center),
start_(start),
end_(end) {
store_color_stops(this + 1, colors, stops);
}

explicit DlSweepGradientColorSource(const DlSweepGradientColorSource* source);

Expand Down
88 changes: 75 additions & 13 deletions display_list/effects/dl_color_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeImage(
}

namespace {
size_t CalculateLinearGradientSize(uint32_t stop_count) {
return sizeof(DlLinearGradientColorSource) +
template <typename GradientColorSource>
size_t CalculateGradientSize(uint32_t stop_count) {
return sizeof(GradientColorSource) +
(stop_count * (sizeof(DlColor) + sizeof(float)));
}
} // namespace
Expand All @@ -45,7 +46,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeLinear(
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = CalculateLinearGradientSize(stop_count);
size_t needed =
CalculateGradientSize<DlLinearGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlLinearGradientColorSource> ret;
Expand All @@ -64,7 +66,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeLinear(
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = CalculateLinearGradientSize(stop_count);
size_t needed =
CalculateGradientSize<DlLinearGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlLinearGradientColorSource> ret;
Expand All @@ -83,9 +86,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = sizeof(DlRadialGradientColorSource) +
(stop_count * (sizeof(DlColor) + sizeof(float)));

size_t needed =
CalculateGradientSize<DlRadialGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlRadialGradientColorSource> ret;
Expand All @@ -95,6 +97,26 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeRadial(
DlPoint center,
DlScalar radius,
uint32_t stop_count,
const DlScalar* colors_argb,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed =
CalculateGradientSize<DlRadialGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlRadialGradientColorSource> ret;
ret.reset(
new (storage) DlRadialGradientColorSource(
center, radius, stop_count, colors_argb, stops, tile_mode, matrix),
DlGradientDeleter);
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
DlPoint start_center,
DlScalar start_radius,
Expand All @@ -105,9 +127,8 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = sizeof(DlConicalGradientColorSource) +
(stop_count * (sizeof(DlColor) + sizeof(float)));

size_t needed =
CalculateGradientSize<DlConicalGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlConicalGradientColorSource> ret;
Expand All @@ -118,6 +139,29 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeConical(
DlPoint start_center,
DlScalar start_radius,
DlPoint end_center,
DlScalar end_radius,
uint32_t stop_count,
const DlScalar* colors_argb,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed =
CalculateGradientSize<DlConicalGradientColorSource>(stop_count);

void* storage = ::operator new(needed);

std::shared_ptr<DlConicalGradientColorSource> ret;
ret.reset(new (storage) DlConicalGradientColorSource(
start_center, start_radius, end_center, end_radius, stop_count,
colors_argb, stops, tile_mode, matrix),
DlGradientDeleter);
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
DlPoint center,
DlScalar start,
Expand All @@ -127,9 +171,7 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = sizeof(DlSweepGradientColorSource) +
(stop_count * (sizeof(DlColor) + sizeof(float)));

size_t needed = CalculateGradientSize<DlSweepGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlSweepGradientColorSource> ret;
Expand All @@ -140,6 +182,26 @@ std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeSweep(
DlPoint center,
DlScalar start,
DlScalar end,
uint32_t stop_count,
const DlScalar* colors_argb,
const float* stops,
DlTileMode tile_mode,
const DlMatrix* matrix) {
size_t needed = CalculateGradientSize<DlSweepGradientColorSource>(stop_count);
void* storage = ::operator new(needed);

std::shared_ptr<DlSweepGradientColorSource> ret;
ret.reset(new (storage) DlSweepGradientColorSource(center, start, end,
stop_count, colors_argb,
stops, tile_mode, matrix),
DlGradientDeleter);
return ret;
}

std::shared_ptr<DlColorSource> DlColorSource::MakeRuntimeEffect(
sk_sp<DlRuntimeEffect> runtime_effect,
std::vector<std::shared_ptr<DlColorSource>> samplers,
Expand Down
Loading

0 comments on commit 92de3d0

Please sign in to comment.