[Impeller] move path out of PathBuilder in TakePath. (flutter/engine#50444)

We should move this path so we don't end up allocating/deallocating twice. 

Fixes https://github.com/flutter/flutter/issues/142873
This commit is contained in:
Jonah Williams
2024-02-07 14:01:04 -08:00
committed by GitHub
parent c720373a9e
commit a363e03859
4 changed files with 12 additions and 15 deletions

View File

@@ -15,11 +15,7 @@ namespace impeller {
Path::Path() : data_(new Data()) {}
Path::Path(const Data& data) : data_(new Data(data)) {
FML_DCHECK(data_->points.size() == data_->points.capacity());
FML_DCHECK(data_->components.size() == data_->components.capacity());
FML_DCHECK(data_->contours.size() == data_->contours.capacity());
}
Path::Path(Data data) : data_(std::make_shared<Data>(std::move(data))) {}
Path::~Path() = default;

View File

@@ -200,6 +200,9 @@ class Path {
// builder from affecting the existing taken paths.
struct Data {
Data() = default;
Data(Data&& other) = default;
Data(const Data& other) = default;
~Data() = default;
@@ -211,11 +214,9 @@ class Path {
std::vector<ContourComponent> contours;
std::optional<Rect> bounds;
bool locked = false;
};
explicit Path(const Data& data);
explicit Path(Data data);
std::shared_ptr<const Data> data_;
};

View File

@@ -22,7 +22,7 @@ Path PathBuilder::CopyPath(FillType fill) {
Path PathBuilder::TakePath(FillType fill) {
prototype_.fill = fill;
UpdateBounds();
return Path(prototype_);
return Path(std::move(prototype_));
}
void PathBuilder::Reserve(size_t point_size, size_t verb_size) {

View File

@@ -486,7 +486,7 @@ TEST(PathTest, CanBeCloned) {
}
}
TEST(PathTest, PathBuilderDoesNotMutateTakenPaths) {
TEST(PathTest, PathBuilderDoesNotMutateCopiedPaths) {
auto test_isolation =
[](const std::function<void(PathBuilder & builder)>& mutator,
bool will_close, Point mutation_offset, const std::string& label) {
@@ -525,23 +525,23 @@ TEST(PathTest, PathBuilderDoesNotMutateTakenPaths) {
}
};
auto path1 = builder.TakePath();
auto path1 = builder.CopyPath();
verify_path(path1, false, false, {},
"Initial Path1 state before " + label);
for (int i = 0; i < 10; i++) {
auto path = builder.TakePath();
auto path = builder.CopyPath();
verify_path(
path, false, false, {},
"Extra TakePath #" + std::to_string(i + 1) + " for " + label);
"Extra CopyPath #" + std::to_string(i + 1) + " for " + label);
}
mutator(builder);
verify_path(path1, false, false, {},
"Path1 state after subsequent " + label);
auto path2 = builder.TakePath();
auto path2 = builder.CopyPath();
verify_path(path1, false, false, {},
"Path1 state after subsequent " + label + " and TakePath");
"Path1 state after subsequent " + label + " and CopyPath");
verify_path(path2, true, will_close, mutation_offset,
"Initial Path2 state with subsequent " + label);
};