From 841c2b4b1db2e0a41035c53fe9e98e54e6c45b45 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 6 Mar 2024 11:17:57 -0800 Subject: [PATCH] [Impeller] Append fewer redundant points when bridging triangle strips between contours. (flutter/engine#51232) Optimizes out 1-2 points per contour. Improvement to the TessellateConvex changes I made to get StC over the line. --- .../impeller/tessellator/tessellator.cc | 20 ++++++++++++++----- .../tessellator/tessellator_unittests.cc | 10 ++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/impeller/tessellator/tessellator.cc b/engine/src/flutter/impeller/tessellator/tessellator.cc index 40a1a542ab..8f5edc6137 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.cc +++ b/engine/src/flutter/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/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc b/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc index d01a3ba33b..772a794b85 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc +++ b/engine/src/flutter/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); } }