Skip to content

Commit

Permalink
Implement conversion SkPath to Impeller paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 66096b5 commit d3626cd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
67 changes: 65 additions & 2 deletions impeller/display_list/display_list_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,71 @@ static PathBuilder::RoundingRadii ToRoundingRadii(const SkRRect& rrect) {
}

static Path ToPath(const SkPath& path) {
UNIMPLEMENTED;
return Path{};
auto iterator = SkPath::Iter(path, false);

struct PathData {
union {
SkPoint points[4];
};
};

PathBuilder builder;
PathData data;
auto verb = SkPath::Verb::kDone_Verb;
do {
verb = iterator.next(data.points);
switch (verb) {
case SkPath::kMove_Verb:
builder.MoveTo(ToPoint(data.points[0]));
break;
case SkPath::kLine_Verb:
builder.AddLine(ToPoint(data.points[0]), ToPoint(data.points[1]));
break;
case SkPath::kQuad_Verb:
builder.AddQuadraticCurve(ToPoint(data.points[0]), // p1
ToPoint(data.points[1]), // cp
ToPoint(data.points[2]) // p2
);
break;
case SkPath::kConic_Verb: {
constexpr auto kPow2 = 1; // Only works for sweeps up to 90 degrees.
constexpr auto kQuadCount = 1 + (2 * (1 << kPow2));
SkPoint points[kQuadCount];
const auto curve_count =
SkPath::ConvertConicToQuads(data.points[0], //
data.points[1], //
data.points[2], //
iterator.conicWeight(), //
points, //
kPow2 //
);

for (int curve_index = 0, point_index = 0; //
curve_index < curve_count; //
curve_index++, point_index += 2 //
) {
builder.AddQuadraticCurve(ToPoint(points[point_index + 0]), // p1
ToPoint(points[point_index + 1]), // cp
ToPoint(points[point_index + 2]) // p2
);
}
} break;
case SkPath::kCubic_Verb:
builder.AddCubicCurve(ToPoint(data.points[0]), // p1
ToPoint(data.points[1]), // cp1
ToPoint(data.points[2]), // cp2
ToPoint(data.points[3]) // p2
);
break;
case SkPath::kClose_Verb:
builder.Close();
break;
case SkPath::kDone_Verb:
break;
}
} while (verb != SkPath::Verb::kDone_Verb);
// TODO: Convert fill types.
return builder.TakePath();
}

static Path ToPath(const SkRRect& rrect) {
Expand Down
13 changes: 13 additions & 0 deletions impeller/geometry/path_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ PathBuilder& PathBuilder::SmoothCubicCurveTo(Point point,
return *this;
}

PathBuilder& PathBuilder::AddQuadraticCurve(Point p1, Point cp, Point p2) {
prototype_.AddQuadraticComponent(p1, cp, p2);
return *this;
}

PathBuilder& PathBuilder::AddCubicCurve(Point p1,
Point cp1,
Point cp2,
Point p2) {
prototype_.AddCubicComponent(p1, cp1, cp2, p2);
return *this;
}

PathBuilder& PathBuilder::AddRect(Rect rect) {
current_ = rect.origin;

Expand Down
4 changes: 4 additions & 0 deletions impeller/geometry/path_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class PathBuilder {

PathBuilder& AddLine(const Point& p1, const Point& p2);

PathBuilder& AddQuadraticCurve(Point p1, Point cp, Point p2);

PathBuilder& AddCubicCurve(Point p1, Point cp1, Point cp2, Point p2);

struct RoundingRadii {
Point top_left;
Point bottom_left;
Expand Down

0 comments on commit d3626cd

Please sign in to comment.