[Impeller] Implement drawPoints in Impeller (flutter/engine#35204)

This commit is contained in:
Jim Graham
2022-08-05 22:59:04 -07:00
committed by GitHub
parent fef4fbff13
commit d4ef12de20
2 changed files with 68 additions and 1 deletions

View File

@@ -762,7 +762,41 @@ void DisplayListDispatcher::drawArc(const SkRect& oval_bounds,
void DisplayListDispatcher::drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint points[]) {
UNIMPLEMENTED;
// auto path = PathBuilder{}.AddLine(ToPoint(p0), ToPoint(p1)).TakePath();
Paint paint = paint_;
paint.style = Paint::Style::kStroke;
switch (mode) {
case SkCanvas::kPoints_PointMode:
if (paint.stroke_cap == SolidStrokeContents::Cap::kButt) {
paint.stroke_cap = SolidStrokeContents::Cap::kSquare;
}
for (uint32_t i = 0; i < count; i++) {
SkPoint p0 = points[i];
// kEhCloseEnough works around a bug where Impeller does not draw
// anything for zero-length lines.
// See: https://github.com/flutter/flutter/issues/109077
SkPoint p1 = points[i] + SkPoint{kEhCloseEnough, 0.0};
auto path = PathBuilder{}.AddLine(ToPoint(p0), ToPoint(p1)).TakePath();
canvas_.DrawPath(std::move(path), paint);
}
break;
case SkCanvas::kLines_PointMode:
for (uint32_t i = 1; i < count; i += 2) {
SkPoint p0 = points[i - 1];
SkPoint p1 = points[i];
auto path = PathBuilder{}.AddLine(ToPoint(p0), ToPoint(p1)).TakePath();
canvas_.DrawPath(std::move(path), paint);
}
break;
case SkCanvas::kPolygon_PointMode:
for (uint32_t i = 1; i < count; i++) {
SkPoint p0 = points[i - 1];
SkPoint p1 = points[i];
auto path = PathBuilder{}.AddLine(ToPoint(p0), ToPoint(p1)).TakePath();
canvas_.DrawPath(std::move(path), paint);
}
break;
}
}
// |flutter::Dispatcher|

View File

@@ -433,5 +433,38 @@ TEST_P(DisplayListTest, CanDrawNinePatchImageCornersScaledDown) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_P(DisplayListTest, CanDrawPoints) {
flutter::DisplayListBuilder builder;
SkPoint points[7] = {
{0, 0}, //
{100, 100}, //
{100, 0}, //
{0, 100}, //
{0, 0}, //
{48, 48}, //
{52, 52}, //
};
std::vector<flutter::DlStrokeCap> caps = {
flutter::DlStrokeCap::kButt,
flutter::DlStrokeCap::kRound,
flutter::DlStrokeCap::kSquare,
};
flutter::DlPaint paint = flutter::DlPaint().setStrokeWidth(20);
builder.translate(50, 50);
for (auto cap : caps) {
paint.setStrokeCap(cap);
paint.setColor(flutter::DlColor::kYellow().withAlpha(127));
builder.save();
builder.drawPoints(SkCanvas::kPoints_PointMode, 7, points, paint);
builder.translate(150, 0);
builder.drawPoints(SkCanvas::kLines_PointMode, 5, points, paint);
builder.translate(150, 0);
builder.drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
builder.restore();
builder.translate(0, 150);
}
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
} // namespace testing
} // namespace impeller