Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed heap allocations for conical, radial and sweep gradients #57143

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These constructors are so huge but have basically zero logic so I decided not to duplicate them and instead use a template to avoid any sort of bugs where they get out of sync.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

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
Loading