Skip to content

Commit 051a0f4

Browse files
authored
Impeller: Fix for stroked circles with stroke half-width > radius, tessellation scaling (flutter#191102)
`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 the centre. Also fixed a small bug in stroked circle division count scaling. `Tessellator::StrokedCircle` computes its quadrant divisions based on the radius. The radius in this case should be the circle radius + the stroke half-width to get the outer radius of the resulting filled circle. The effect would be under-tessellation of the outer edge that gets worse with increasing half-width. This tosses a pair of brackets on to scale the whole thing. Fixes: flutter#190597 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent d26b43a commit 051a0f4

2 files changed

Lines changed: 126 additions & 12 deletions

File tree

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

Lines changed: 21 additions & 12 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,12 @@ void Tessellator::GenerateStrokedArc(
858862
const TessellatedVertexProc& proc) {
859863
Point center = oval_bounds.GetCenter();
860864
Size base_radii = oval_bounds.GetSize() * 0.5f;
865+
866+
// A stroke width wider than the radii would cross the center and
867+
// self-intersect. Clamp inner edge to center.
861868
Size inner_radii = base_radii - Size(half_width, half_width);
869+
inner_radii = inner_radii.Max(Size(0.0f, 0.0f));
870+
862871
Size outer_radii = base_radii + Size(half_width, half_width);
863872

864873
// Starting cap

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,32 @@ TEST(TessellatorTest, CircleVertexCounts) {
187187
}
188188
}
189189

190+
TEST(TessellatorTest, StrokedCircleVertexCounts) {
191+
Tessellator tessellator;
192+
193+
// The outer edge of a stroked circle sits at |radius| + |half_width|, so it
194+
// must be divided at least as finely as a filled circle of that radius to be
195+
// within |kCircleTolerance|.
196+
auto test = [&tessellator](const Matrix& transform, Scalar radius,
197+
Scalar half_width, const std::string& label) {
198+
ASSERT_LT(half_width, radius) << label;
199+
auto stroked = tessellator.StrokedCircle(transform, {}, radius, half_width);
200+
auto filled = tessellator.FilledCircle(transform, {}, radius + half_width);
201+
202+
EXPECT_GE(stroked.GetVertexCount() / 8, filled.GetVertexCount() / 4)
203+
<< label;
204+
};
205+
206+
test({}, 2.0, 1.0, "small radius");
207+
test({}, 100.0, 10.0, "large radius");
208+
test(Matrix::MakeScale({500.0, 500.0, 0.0}), 2.0, 1.0,
209+
"small radius, scaled up");
210+
test(Matrix::MakeScale({500.0, 500.0, 0.0}), 100.0, 10.0,
211+
"large radius, scaled up");
212+
test(Matrix::MakeScale({0.002, 0.002, 0.0}), 1000.0, 10.0,
213+
"large radius, scaled down");
214+
}
215+
190216
TEST(TessellatorTest, FilledCircleTessellationVertices) {
191217
Tessellator tessellator;
192218

@@ -303,6 +329,48 @@ TEST(TessellatorTest, StrokedCircleTessellationVertices) {
303329
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0, 10.0);
304330
}
305331

332+
TEST(TessellatorTest, StrokedCircleWideStrokeTessellationVertices) {
333+
Tessellator tessellator;
334+
335+
// A stroke at least as wide as the radius fills the circle. Verify it
336+
// tessellates as a filled circle out to the outer edge of the stroke.
337+
auto test = [&tessellator](const Matrix& transform, const Point& center,
338+
Scalar radius, Scalar half_width,
339+
const std::string& label) {
340+
ASSERT_GE(half_width, radius) << label;
341+
auto generator =
342+
tessellator.StrokedCircle(transform, center, radius, half_width);
343+
auto filled =
344+
tessellator.FilledCircle(transform, center, radius + half_width);
345+
346+
EXPECT_EQ(generator.GetTriangleType(), filled.GetTriangleType()) << label;
347+
EXPECT_EQ(generator.GetVertexCount(), filled.GetVertexCount()) << label;
348+
349+
auto vertices = std::vector<Point>();
350+
generator.GenerateVertices([&vertices](const Point& p) { //
351+
vertices.push_back(p);
352+
});
353+
auto filled_vertices = std::vector<Point>();
354+
filled.GenerateVertices([&filled_vertices](const Point& p) { //
355+
filled_vertices.push_back(p);
356+
});
357+
358+
EXPECT_EQ(vertices.size(), generator.GetVertexCount()) << label;
359+
ASSERT_EQ(vertices.size(), filled_vertices.size()) << label;
360+
for (size_t i = 0; i < vertices.size(); i++) {
361+
EXPECT_POINT_NEAR(vertices[i], filled_vertices[i])
362+
<< label << ", vertex " << i;
363+
}
364+
};
365+
366+
test({}, {}, 2.0, 2.0, "half width == radius");
367+
test({}, {}, 2.0, 4.0, "half width > radius");
368+
test({}, {10, 10}, 2.0, 2.0, "offset center");
369+
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {}, 2.0, 2.0, "scaled up");
370+
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0, 1000.0,
371+
"scaled down");
372+
}
373+
306374
TEST(TessellatorTest, FilledArcStripTessellationVertices) {
307375
Tessellator tessellator;
308376

@@ -379,6 +447,43 @@ TEST(TessellatorTest, FilledArcStripTessellationVertices) {
379447
Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(94), Degrees(322), true));
380448
}
381449

450+
TEST(TessellatorTest, StrokedArcWideStrokeTessellationVertices) {
451+
Tessellator tessellator;
452+
453+
// A stroke width wider than the radii would cross the center. Verify the
454+
// inner edge stops there rather than folding through and self-intersecting.
455+
auto test = [&tessellator](const Matrix& transform, const Arc& arc,
456+
Scalar half_width, const std::string& label) {
457+
Scalar radius = arc.GetOvalSize().width * 0.5f;
458+
ASSERT_GE(half_width, radius) << label;
459+
auto generator =
460+
tessellator.StrokedArc(transform, arc, Cap::kButt, half_width);
461+
462+
auto vertices = std::vector<Point>();
463+
generator.GenerateVertices([&vertices](const Point& p) { //
464+
vertices.push_back(p);
465+
});
466+
EXPECT_EQ(vertices.size(), generator.GetVertexCount()) << label;
467+
ASSERT_EQ(vertices.size() % 2, 0u) << label;
468+
469+
Point center = arc.GetOvalBounds().GetCenter();
470+
for (size_t i = 0; i < vertices.size(); i += 2) {
471+
EXPECT_POINT_NEAR(vertices[i], center) << label << ", inner vertex " << i;
472+
EXPECT_NEAR((vertices[i + 1] - center).GetLength(), radius + half_width,
473+
kEhCloseEnough)
474+
<< label << ", outer vertex " << i + 1;
475+
}
476+
};
477+
478+
test({}, Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(0), Degrees(90), false),
479+
50.0, "half width == radius");
480+
test({}, Arc(Rect::MakeXYWH(0, 0, 100, 100), Degrees(0), Degrees(90), false),
481+
75.0, "half width > radius");
482+
test({},
483+
Arc(Rect::MakeXYWH(10, 20, 100, 100), Degrees(94), Degrees(322), false),
484+
60.0, "offset oval, oblique sweep");
485+
}
486+
382487
TEST(TessellatorTest, RoundCapLineTessellationVertices) {
383488
Tessellator tessellator;
384489

0 commit comments

Comments
 (0)