Skip to content

Commit a3d1232

Browse files
committed
Impeller: Fix assert on stroked circles wider than their radius
`Tessellator::StrokedCircle` delegated to `GenerateStrokedCircle` whenever `half_width > 0`, but that generator zig-zags between an inner and an outer circle and asserts `half_width < radius`, since the inner radius goes negative once the stroke is at least as wide as the radius. That case should render as a filled circle whose radius reaches the outer edge of the stroke. We were hitting this when enabling construction lines during debugging. With `debugPaintSizeEnabled` set, `RenderSliver.debugPaint` draws a circle of radius 2.0 with a stroke width of 4.0, so toggling construction lines on in unopt builds triggered this `FML_DCHECK`: ``` [FATAL:flutter/impeller/tessellator/tessellator.cc(755)] Check failed: data.half_width > 0 && data.half_width < data.radii.width. ``` `GenerateStrokedArc` had the same issue but didn't assert: the inner radii went negative and the inner edge folded back through the centre of the oval, producing a self-intersecting strip. We now clamp to zero. Fixes: flutter#190597
1 parent b25d94d commit a3d1232

2 files changed

Lines changed: 99 additions & 13 deletions

File tree

‎engine/src/flutter/impeller/tessellator/tessellator.cc‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,24 @@ EllipticalVertexGenerator Tessellator::StrokedCircle(
506506
const Point& center,
507507
Scalar radius,
508508
Scalar half_width) {
509-
if (half_width > 0) {
510-
auto divisions = ComputeQuadrantDivisions(
511-
view_transform.GetMaxBasisLengthXY() * radius + half_width);
512-
return EllipticalVertexGenerator(Tessellator::GenerateStrokedCircle,
513-
GetTrigsForDivisions(divisions),
514-
PrimitiveType::kTriangleStrip, 8,
515-
{
516-
.reference_centers = {center, center},
517-
.radii = {radius, radius},
518-
.half_width = half_width,
519-
});
520-
} else {
509+
if (half_width <= 0) {
521510
return FilledCircle(view_transform, center, radius);
522511
}
512+
if (half_width >= radius) {
513+
// Identical to a filled circle whose radius reaches the outer edge of the
514+
// stroke.
515+
return FilledCircle(view_transform, center, radius + half_width);
516+
}
517+
auto divisions = ComputeQuadrantDivisions(
518+
view_transform.GetMaxBasisLengthXY() * radius + half_width);
519+
return EllipticalVertexGenerator(Tessellator::GenerateStrokedCircle,
520+
GetTrigsForDivisions(divisions),
521+
PrimitiveType::kTriangleStrip, 8,
522+
{
523+
.reference_centers = {center, center},
524+
.radii = {radius, radius},
525+
.half_width = half_width,
526+
});
523527
}
524528

525529
ArcVertexGenerator ArcVertexGenerator::MakeFilled(
@@ -858,7 +862,11 @@ void Tessellator::GenerateStrokedArc(
858862
const TessellatedVertexProc& proc) {
859863
Point center = oval_bounds.GetCenter();
860864
Size base_radii = oval_bounds.GetSize() * 0.5f;
861-
Size inner_radii = base_radii - Size(half_width, half_width);
865+
// A stroke at least as wide as the radii covers the center of the oval, so
866+
// the inner edge is clamped to the center rather than allowed to fold back
867+
// through it and self-intersect.
868+
Size inner_radii =
869+
(base_radii - Size(half_width, half_width)).Max(Size(0.0f, 0.0f));
862870
Size outer_radii = base_radii + Size(half_width, half_width);
863871

864872
// Starting cap

‎engine/src/flutter/impeller/tessellator/tessellator_unittests.cc‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,46 @@ TEST(TessellatorTest, StrokedCircleTessellationVertices) {
303303
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0, 10.0);
304304
}
305305

306+
TEST(TessellatorTest, StrokedCircleWideStrokeTessellationVertices) {
307+
Tessellator tessellator;
308+
309+
// A stroke at least as wide as the radius fills the whole circle.
310+
// Verify it tessellates as a filled circle whose radius reaches the outer
311+
// edge of the stroke rather than as an annulus with a negative inner radius.
312+
auto test = [&tessellator](const Matrix& transform, const Point& center,
313+
Scalar radius, Scalar half_width) {
314+
ASSERT_GE(half_width, radius);
315+
auto generator =
316+
tessellator.StrokedCircle(transform, center, radius, half_width);
317+
auto filled =
318+
tessellator.FilledCircle(transform, center, radius + half_width);
319+
320+
EXPECT_EQ(generator.GetTriangleType(), filled.GetTriangleType());
321+
EXPECT_EQ(generator.GetVertexCount(), filled.GetVertexCount());
322+
323+
auto vertices = std::vector<Point>();
324+
generator.GenerateVertices([&vertices](const Point& p) { //
325+
vertices.push_back(p);
326+
});
327+
auto filled_vertices = std::vector<Point>();
328+
filled.GenerateVertices([&filled_vertices](const Point& p) { //
329+
filled_vertices.push_back(p);
330+
});
331+
332+
EXPECT_EQ(vertices.size(), generator.GetVertexCount());
333+
ASSERT_EQ(vertices.size(), filled_vertices.size());
334+
for (size_t i = 0; i < vertices.size(); i++) {
335+
EXPECT_POINT_NEAR(vertices[i], filled_vertices[i]) << "vertex " << i;
336+
}
337+
};
338+
339+
test({}, {}, 2.0, 2.0);
340+
test({}, {}, 2.0, 4.0);
341+
test({}, {10, 10}, 2.0, 2.0);
342+
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {}, 2.0, 2.0);
343+
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0, 1000.0);
344+
}
345+
306346
TEST(TessellatorTest, FilledArcStripTessellationVertices) {
307347
Tessellator tessellator;
308348

@@ -379,6 +419,44 @@ TEST(TessellatorTest, FilledArcStripTessellationVertices) {
379419
Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(94), Degrees(322), true));
380420
}
381421

422+
TEST(TessellatorTest, StrokedArcWideStrokeTessellationVertices) {
423+
Tessellator tessellator;
424+
425+
// A stroke at least as wide as the radius reaches the center of the oval.
426+
// Verify the inner edge stops at the center so we don't mirror the inner
427+
// vertices onto the far side and produce a self-intersecting strip.
428+
auto test = [&tessellator](const Matrix& transform, const Arc& arc,
429+
Scalar half_width) {
430+
Scalar radius = arc.GetOvalSize().width * 0.5f;
431+
ASSERT_GE(half_width, radius);
432+
auto generator =
433+
tessellator.StrokedArc(transform, arc, Cap::kButt, half_width);
434+
435+
auto vertices = std::vector<Point>();
436+
generator.GenerateVertices([&vertices](const Point& p) { //
437+
vertices.push_back(p);
438+
});
439+
EXPECT_EQ(vertices.size(), generator.GetVertexCount());
440+
ASSERT_EQ(vertices.size() % 2, 0u);
441+
442+
Point center = arc.GetOvalBounds().GetCenter();
443+
for (size_t i = 0; i < vertices.size(); i += 2) {
444+
EXPECT_POINT_NEAR(vertices[i], center) << "inner vertex " << i;
445+
EXPECT_NEAR((vertices[i + 1] - center).GetLength(), radius + half_width,
446+
kEhCloseEnough)
447+
<< "outer vertex " << i + 1;
448+
}
449+
};
450+
451+
test({}, Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(0), Degrees(90), false),
452+
50.0);
453+
test({}, Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(0), Degrees(90), false),
454+
75.0);
455+
test({},
456+
Arc(Rect::MakeXYWH(10, 20, 100, 100), Degrees(94), Degrees(322), false),
457+
60.0);
458+
}
459+
382460
TEST(TessellatorTest, RoundCapLineTessellationVertices) {
383461
Tessellator tessellator;
384462

0 commit comments

Comments
 (0)