Skip to content

Commit

Permalink
Fix solid stroke contour switching + round cap smoothing, and add tra…
Browse files Browse the repository at this point in the history
…nsparent path overdraw example (flutter#112)
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent 78eb5d6 commit c4f1a59
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
38 changes: 38 additions & 0 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,43 @@ TEST_F(AiksTest, TransformMultipliesCorrectly) {
// clang-format on
}

TEST_F(AiksTest, PathsShouldHaveUniformAlpha) {
// Compare with https://fiddle.skia.org/c/027392122bec8ac2b5d5de00a4b9bbe2
Canvas canvas;
Paint paint;

paint.color = Color::White();
canvas.DrawPaint(paint);

paint.color = Color::Black().WithAlpha(0.5);
paint.style = Paint::Style::kStroke;
paint.stroke_width = 10;

Path path = PathBuilder{}
.MoveTo({20, 20})
.QuadraticCurveTo({60, 20}, {60, 60})
.Close()
.MoveTo({60, 20})
.QuadraticCurveTo({60, 60}, {20, 60})
.TakePath();

canvas.Scale({3, 3});
for (auto join :
{SolidStrokeContents::Join::kBevel, SolidStrokeContents::Join::kRound,
SolidStrokeContents::Join::kMiter}) {
paint.stroke_join = join;
for (auto cap :
{SolidStrokeContents::Cap::kButt, SolidStrokeContents::Cap::kSquare,
SolidStrokeContents::Cap::kRound}) {
paint.stroke_cap = cap;
canvas.DrawPath(path, paint);
canvas.Translate({80, 0});
}
canvas.Translate({-240, 60});
}

ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

} // namespace testing
} // namespace impeller
11 changes: 9 additions & 2 deletions impeller/entity/contents/solid_stroke_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ static VertexBuffer CreateSolidStrokeVertices(
vtx.vertex_normal = {};
vtx.pen_down = 0.0;
vtx_builder.AppendVertex(vtx);

vtx.vertex_position = polyline.points[contour_start_point_i];
// Append two transparent vertices at the beginning of the new contour
// because it's a triangle strip.
vtx_builder.AppendVertex(vtx);
vtx_builder.AppendVertex(vtx);
}

Expand Down Expand Up @@ -152,9 +156,13 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
cmd.pipeline =
renderer.GetSolidStrokePipeline(OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();

auto smoothing = SmoothingApproximation(
5.0 / (stroke_size_ * entity.GetTransformation().GetMaxBasisLength()),
0.0, 0.0);
cmd.BindVertices(CreateSolidStrokeVertices(
entity.GetPath(), pass.GetTransientsBuffer(), cap_proc_, join_proc_,
miter_limit_, arc_smoothing_approximation_));
miter_limit_, smoothing));
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
VS::BindStrokeInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(stroke_info));
Expand All @@ -166,7 +174,6 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,

void SolidStrokeContents::SetStrokeSize(Scalar size) {
stroke_size_ = size;
arc_smoothing_approximation_ = SmoothingApproximation(5.0 / size, 0.0, 0.0);
}

Scalar SolidStrokeContents::GetStrokeSize() const {
Expand Down
2 changes: 0 additions & 2 deletions impeller/entity/contents/solid_stroke_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class SolidStrokeContents final : public Contents {
RenderPass& pass) const override;

private:
SmoothingApproximation arc_smoothing_approximation_;

Color color_;
Scalar stroke_size_ = 0.0;
Scalar miter_limit_ = 4.0;
Expand Down

0 comments on commit c4f1a59

Please sign in to comment.