From 317c683dd347cdc6d4bd8a4729b6fce31eadc23f Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 6 Mar 2024 10:34:46 -0800 Subject: [PATCH] [Impeller] Remove redundant points at the end of contours. --- impeller/tessellator/tessellator.cc | 20 ++++++++++++++----- impeller/tessellator/tessellator_unittests.cc | 10 ++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/impeller/tessellator/tessellator.cc b/impeller/tessellator/tessellator.cc index 40a1a542ab761..8f5edc61374d5 100644 --- a/impeller/tessellator/tessellator.cc +++ b/impeller/tessellator/tessellator.cc @@ -189,6 +189,7 @@ std::vector Tessellator::TessellateConvex(const Path& path, output.reserve(polyline.points->size() + (4 * (polyline.contours.size() - 1))); + bool previous_contour_odd_points = false; for (auto j = 0u; j < polyline.contours.size(); j++) { auto [start, end] = polyline.GetContourPointBounds(j); auto first_point = polyline.GetPoint(start); @@ -205,22 +206,31 @@ std::vector Tessellator::TessellateConvex(const Path& path, output.emplace_back(output.back()); output.emplace_back(first_point); output.emplace_back(first_point); - output.emplace_back(first_point); + + // If the contour has an odd number of points, insert an extra point when + // bridging to the next contour to preserve the correct triangle winding + // order. + if (previous_contour_odd_points) { + output.emplace_back(first_point); + } } else { output.emplace_back(first_point); } size_t a = start + 1; size_t b = end - 1; - while (a <= b) { - // If the contour has an odd number of points, two identical points will - // be appended when a == b. This ensures the triangle winding order will - // remain the same after bridging to the next contour. + 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; } diff --git a/impeller/tessellator/tessellator_unittests.cc b/impeller/tessellator/tessellator_unittests.cc index d01a3ba33b2c3..772a794b85f87 100644 --- a/impeller/tessellator/tessellator_unittests.cc +++ b/impeller/tessellator/tessellator_unittests.cc @@ -111,9 +111,7 @@ TEST(TessellatorTest, TessellateConvex) { auto pts = t.TessellateConvex( PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 10, 10)).TakePath(), 1.0); - std::vector expected = { - {0, 0}, {10, 0}, {0, 10}, {10, 10}, {10, 10}, // - }; + std::vector expected = {{0, 0}, {10, 0}, {0, 10}, {10, 10}}; EXPECT_EQ(pts, expected); } @@ -125,9 +123,9 @@ TEST(TessellatorTest, TessellateConvex) { .TakePath(), 1.0); - std::vector expected = { - {0, 0}, {10, 0}, {0, 10}, {10, 10}, {10, 10}, {10, 10}, {20, 20}, - {20, 20}, {20, 20}, {30, 20}, {20, 30}, {30, 30}, {30, 30}}; + std::vector expected = {{0, 0}, {10, 0}, {0, 10}, {10, 10}, + {10, 10}, {20, 20}, {20, 20}, {30, 20}, + {20, 30}, {30, 30}}; EXPECT_EQ(pts, expected); } }