Skip to content

Commit c4f1a59

Browse files
bderodnfield
authored andcommitted
Fix solid stroke contour switching + round cap smoothing, and add transparent path overdraw example (flutter#112)
1 parent 78eb5d6 commit c4f1a59

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

impeller/aiks/aiks_unittests.cc

+38
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,43 @@ TEST_F(AiksTest, TransformMultipliesCorrectly) {
482482
// clang-format on
483483
}
484484

485+
TEST_F(AiksTest, PathsShouldHaveUniformAlpha) {
486+
// Compare with https://fiddle.skia.org/c/027392122bec8ac2b5d5de00a4b9bbe2
487+
Canvas canvas;
488+
Paint paint;
489+
490+
paint.color = Color::White();
491+
canvas.DrawPaint(paint);
492+
493+
paint.color = Color::Black().WithAlpha(0.5);
494+
paint.style = Paint::Style::kStroke;
495+
paint.stroke_width = 10;
496+
497+
Path path = PathBuilder{}
498+
.MoveTo({20, 20})
499+
.QuadraticCurveTo({60, 20}, {60, 60})
500+
.Close()
501+
.MoveTo({60, 20})
502+
.QuadraticCurveTo({60, 60}, {20, 60})
503+
.TakePath();
504+
505+
canvas.Scale({3, 3});
506+
for (auto join :
507+
{SolidStrokeContents::Join::kBevel, SolidStrokeContents::Join::kRound,
508+
SolidStrokeContents::Join::kMiter}) {
509+
paint.stroke_join = join;
510+
for (auto cap :
511+
{SolidStrokeContents::Cap::kButt, SolidStrokeContents::Cap::kSquare,
512+
SolidStrokeContents::Cap::kRound}) {
513+
paint.stroke_cap = cap;
514+
canvas.DrawPath(path, paint);
515+
canvas.Translate({80, 0});
516+
}
517+
canvas.Translate({-240, 60});
518+
}
519+
520+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
521+
}
522+
485523
} // namespace testing
486524
} // namespace impeller

impeller/entity/contents/solid_stroke_contents.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ static VertexBuffer CreateSolidStrokeVertices(
7979
vtx.vertex_normal = {};
8080
vtx.pen_down = 0.0;
8181
vtx_builder.AppendVertex(vtx);
82+
8283
vtx.vertex_position = polyline.points[contour_start_point_i];
84+
// Append two transparent vertices at the beginning of the new contour
85+
// because it's a triangle strip.
86+
vtx_builder.AppendVertex(vtx);
8387
vtx_builder.AppendVertex(vtx);
8488
}
8589

@@ -152,9 +156,13 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
152156
cmd.pipeline =
153157
renderer.GetSolidStrokePipeline(OptionsFromPassAndEntity(pass, entity));
154158
cmd.stencil_reference = entity.GetStencilDepth();
159+
160+
auto smoothing = SmoothingApproximation(
161+
5.0 / (stroke_size_ * entity.GetTransformation().GetMaxBasisLength()),
162+
0.0, 0.0);
155163
cmd.BindVertices(CreateSolidStrokeVertices(
156164
entity.GetPath(), pass.GetTransientsBuffer(), cap_proc_, join_proc_,
157-
miter_limit_, arc_smoothing_approximation_));
165+
miter_limit_, smoothing));
158166
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
159167
VS::BindStrokeInfo(cmd,
160168
pass.GetTransientsBuffer().EmplaceUniform(stroke_info));
@@ -166,7 +174,6 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
166174

167175
void SolidStrokeContents::SetStrokeSize(Scalar size) {
168176
stroke_size_ = size;
169-
arc_smoothing_approximation_ = SmoothingApproximation(5.0 / size, 0.0, 0.0);
170177
}
171178

172179
Scalar SolidStrokeContents::GetStrokeSize() const {

impeller/entity/contents/solid_stroke_contents.h

-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ class SolidStrokeContents final : public Contents {
7474
RenderPass& pass) const override;
7575

7676
private:
77-
SmoothingApproximation arc_smoothing_approximation_;
78-
7977
Color color_;
8078
Scalar stroke_size_ = 0.0;
8179
Scalar miter_limit_ = 4.0;

0 commit comments

Comments
 (0)