Skip to content

Commit

Permalink
Add support for path composition in path builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent f83a021 commit 66096b5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
33 changes: 17 additions & 16 deletions impeller/display_list/display_list_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,22 @@ static PathBuilder::RoundingRadii ToRoundingRadii(const SkRRect& rrect) {
return radii;
}

static Path ToPath(const SkPath& path) {
UNIMPLEMENTED;
return Path{};
}

static Path ToPath(const SkRRect& rrect) {
return PathBuilder{}
.AddRoundedRect(ToRect(rrect.getBounds()), ToRoundingRadii(rrect))
.TakePath();
}

// |flutter::Dispatcher|
void DisplayListImpeller::clipRRect(const SkRRect& rrect,
SkClipOp clip_op,
bool is_aa) {
auto path =
PathBuilder{}
.AddRoundedRect(ToRect(rrect.getBounds()), ToRoundingRadii(rrect))
.TakePath();
canvas_.ClipPath(std::move(path));
}

static Path ToPath(const SkPath& path) {
UNIMPLEMENTED;
return Path{};
canvas_.ClipPath(ToPath(rrect));
}

// |flutter::Dispatcher|
Expand Down Expand Up @@ -290,17 +292,16 @@ void DisplayListImpeller::drawCircle(const SkPoint& center, SkScalar radius) {

// |flutter::Dispatcher|
void DisplayListImpeller::drawRRect(const SkRRect& rrect) {
auto path =
PathBuilder{}
.AddRoundedRect(ToRect(rrect.getBounds()), ToRoundingRadii(rrect))
.TakePath();
canvas_.DrawPath(std::move(path), paint_);
canvas_.DrawPath(ToPath(rrect), paint_);
}

// |flutter::Dispatcher|
void DisplayListImpeller::drawDRRect(const SkRRect& outer,
const SkRRect& inner) {
UNIMPLEMENTED;
PathBuilder builder;
builder.AddPath(ToPath(outer));
builder.AddPath(ToPath(inner));
canvas_.DrawPath(builder.TakePath(FillType::kOdd), paint_);
}

// |flutter::Dispatcher|
Expand Down
17 changes: 7 additions & 10 deletions impeller/geometry/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,23 @@ bool Path::UpdateCubicComponentAtIndex(size_t index,
return true;
}

static void AddPoints(std::vector<Point>& dest, const std::vector<Point>& src) {
dest.reserve(dest.size() + src.size());
dest.insert(dest.end(), src.begin(), src.end());
}

std::vector<Point> Path::CreatePolyline(
const SmoothingApproximation& approximation) const {
std::vector<Point> points;
auto collect_points = [&points](const std::vector<Point>& collection) {
points.reserve(points.size() + collection.size());
points.insert(points.end(), collection.begin(), collection.end());
};
for (const auto& component : components_) {
switch (component.type) {
case ComponentType::kLinear:
AddPoints(points, linears_[component.index].CreatePolyline());
collect_points(linears_[component.index].CreatePolyline());
break;
case ComponentType::kQuadratic:
AddPoints(points,
quads_[component.index].CreatePolyline(approximation));
collect_points(quads_[component.index].CreatePolyline(approximation));
break;
case ComponentType::kCubic:
AddPoints(points,
cubics_[component.index].CreatePolyline(approximation));
collect_points(cubics_[component.index].CreatePolyline(approximation));
break;
}
}
Expand Down
14 changes: 14 additions & 0 deletions impeller/geometry/path_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,18 @@ const Path& PathBuilder::GetCurrentPath() const {
return prototype_;
}

PathBuilder& PathBuilder::AddPath(const Path& path) {
auto linear = [&](size_t index, const LinearPathComponent& l) {
prototype_.AddLinearComponent(l.p1, l.p2);
};
auto quadratic = [&](size_t index, const QuadraticPathComponent& q) {
prototype_.AddQuadraticComponent(q.p1, q.cp, q.p2);
};
auto cubic = [&](size_t index, const CubicPathComponent& c) {
prototype_.AddCubicComponent(c.p1, c.cp1, c.cp2, c.p2);
};
path.EnumerateComponents(linear, quadratic, cubic);
return *this;
}

} // namespace impeller
2 changes: 2 additions & 0 deletions impeller/geometry/path_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class PathBuilder {

PathBuilder& AddRoundedRect(Rect rect, Scalar radius);

PathBuilder& AddPath(const Path& path);

private:
Point subpath_start_;
Point current_;
Expand Down

0 comments on commit 66096b5

Please sign in to comment.