From b2abb9a5113626430c0e37f9cd0f45541de219af Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 5 Apr 2022 00:37:03 -0700 Subject: [PATCH] Fix solid stroke contour switching + round cap smoothing, and add transparent path overdraw example (flutter/engine#112) --- .../flutter/impeller/aiks/aiks_unittests.cc | 38 +++++++++++++++++++ .../entity/contents/solid_stroke_contents.cc | 11 +++++- .../entity/contents/solid_stroke_contents.h | 2 - 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index e554bb30aca..73b5039e032 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -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 diff --git a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc index 592c7792c8b..abbc96687ce 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc @@ -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); } @@ -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)); @@ -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 { diff --git a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h index 8bb1501e043..66f81cc002e 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h +++ b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h @@ -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;