Skip to content

Commit d3626cd

Browse files
chinmaygardednfield
authored andcommitted
Implement conversion SkPath to Impeller paths.
1 parent 66096b5 commit d3626cd

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

impeller/display_list/display_list_impeller.cc

+65-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,71 @@ static PathBuilder::RoundingRadii ToRoundingRadii(const SkRRect& rrect) {
232232
}
233233

234234
static Path ToPath(const SkPath& path) {
235-
UNIMPLEMENTED;
236-
return Path{};
235+
auto iterator = SkPath::Iter(path, false);
236+
237+
struct PathData {
238+
union {
239+
SkPoint points[4];
240+
};
241+
};
242+
243+
PathBuilder builder;
244+
PathData data;
245+
auto verb = SkPath::Verb::kDone_Verb;
246+
do {
247+
verb = iterator.next(data.points);
248+
switch (verb) {
249+
case SkPath::kMove_Verb:
250+
builder.MoveTo(ToPoint(data.points[0]));
251+
break;
252+
case SkPath::kLine_Verb:
253+
builder.AddLine(ToPoint(data.points[0]), ToPoint(data.points[1]));
254+
break;
255+
case SkPath::kQuad_Verb:
256+
builder.AddQuadraticCurve(ToPoint(data.points[0]), // p1
257+
ToPoint(data.points[1]), // cp
258+
ToPoint(data.points[2]) // p2
259+
);
260+
break;
261+
case SkPath::kConic_Verb: {
262+
constexpr auto kPow2 = 1; // Only works for sweeps up to 90 degrees.
263+
constexpr auto kQuadCount = 1 + (2 * (1 << kPow2));
264+
SkPoint points[kQuadCount];
265+
const auto curve_count =
266+
SkPath::ConvertConicToQuads(data.points[0], //
267+
data.points[1], //
268+
data.points[2], //
269+
iterator.conicWeight(), //
270+
points, //
271+
kPow2 //
272+
);
273+
274+
for (int curve_index = 0, point_index = 0; //
275+
curve_index < curve_count; //
276+
curve_index++, point_index += 2 //
277+
) {
278+
builder.AddQuadraticCurve(ToPoint(points[point_index + 0]), // p1
279+
ToPoint(points[point_index + 1]), // cp
280+
ToPoint(points[point_index + 2]) // p2
281+
);
282+
}
283+
} break;
284+
case SkPath::kCubic_Verb:
285+
builder.AddCubicCurve(ToPoint(data.points[0]), // p1
286+
ToPoint(data.points[1]), // cp1
287+
ToPoint(data.points[2]), // cp2
288+
ToPoint(data.points[3]) // p2
289+
);
290+
break;
291+
case SkPath::kClose_Verb:
292+
builder.Close();
293+
break;
294+
case SkPath::kDone_Verb:
295+
break;
296+
}
297+
} while (verb != SkPath::Verb::kDone_Verb);
298+
// TODO: Convert fill types.
299+
return builder.TakePath();
237300
}
238301

239302
static Path ToPath(const SkRRect& rrect) {

impeller/geometry/path_builder.cc

+13
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ PathBuilder& PathBuilder::SmoothCubicCurveTo(Point point,
150150
return *this;
151151
}
152152

153+
PathBuilder& PathBuilder::AddQuadraticCurve(Point p1, Point cp, Point p2) {
154+
prototype_.AddQuadraticComponent(p1, cp, p2);
155+
return *this;
156+
}
157+
158+
PathBuilder& PathBuilder::AddCubicCurve(Point p1,
159+
Point cp1,
160+
Point cp2,
161+
Point p2) {
162+
prototype_.AddCubicComponent(p1, cp1, cp2, p2);
163+
return *this;
164+
}
165+
153166
PathBuilder& PathBuilder::AddRect(Rect rect) {
154167
current_ = rect.origin;
155168

impeller/geometry/path_builder.h

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class PathBuilder {
5656

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

59+
PathBuilder& AddQuadraticCurve(Point p1, Point cp, Point p2);
60+
61+
PathBuilder& AddCubicCurve(Point p1, Point cp1, Point cp2, Point p2);
62+
5963
struct RoundingRadii {
6064
Point top_left;
6165
Point bottom_left;

0 commit comments

Comments
 (0)