Skip to content

Commit

Permalink
[Impeller][CP] dont segfault when tessellating empty polygons. (#53212)
Browse files Browse the repository at this point in the history
While this is fixed on ToT, the stable channel is far behind so cherry picking all the tessellation changes is infeasible. Instead, we can land a small fix to avoid the segfault. Atttached unit test crashes without patch.

Fixes flutter/flutter#149646
Fixes flutter/flutter#149309
  • Loading branch information
jonahwilliams authored Jul 1, 2024
1 parent 0dcd108 commit 6934bb5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
29 changes: 15 additions & 14 deletions impeller/tessellator/tessellator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ std::vector<Point> Tessellator::TessellateConvex(const Path& path,
for (auto j = 0u; j < polyline.contours.size(); j++) {
auto [start, end] = polyline.GetContourPointBounds(j);
auto first_point = polyline.GetPoint(start);

// Some polygons will not self close and an additional triangle
// must be inserted, others will self close and we need to avoid
// inserting an extra triangle.
Expand All @@ -221,19 +220,21 @@ std::vector<Point> Tessellator::TessellateConvex(const Path& path,
output.emplace_back(first_point);
}

size_t a = start + 1;
size_t b = end - 1;
while (a < b) {
output.emplace_back(polyline.GetPoint(a));
output.emplace_back(polyline.GetPoint(b));
a++;
b--;
}
if (a == b) {
previous_contour_odd_points = false;
output.emplace_back(polyline.GetPoint(a));
} else {
previous_contour_odd_points = true;
if (start != end) {
size_t a = start + 1;
size_t b = end - 1;
while (a < b) {
output.emplace_back(polyline.GetPoint(a));
output.emplace_back(polyline.GetPoint(b));
a++;
b--;
}
if (a == b) {
previous_contour_odd_points = false;
output.emplace_back(polyline.GetPoint(a));
} else {
previous_contour_odd_points = true;
}
}
}
return output;
Expand Down
11 changes: 11 additions & 0 deletions impeller/tessellator/tessellator_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ TEST(TessellatorTest, TessellateConvex) {
{20, 30}, {30, 30}};
EXPECT_EQ(pts, expected);
}

{
Tessellator t;
PathBuilder builder{};
auto path = builder.MoveTo({10, 10})
.Close()
.AddRect(Rect::MakeLTRB(0, 0, 100, 100))
.TakePath();
// Verify no crash.
t.TessellateConvex(path, 1.0);
}
}

TEST(TessellatorTest, CircleVertexCounts) {
Expand Down

0 comments on commit 6934bb5

Please sign in to comment.