From 590b254c1a5bed8fbeef9cc13d5c371bd146c29e Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 1 May 2024 18:05:55 -0700 Subject: [PATCH] [Impeller] remove incremental allocation during filled path tessellation. (flutter/engine#52401) Don't allocate a polyline (and resulting std::vectors) when performing convex tessellation. Instead use a listener pattern and iterate through path verbs. --- .../entity/geometry/fill_path_geometry.cc | 11 +-- .../impeller/geometry/geometry_benchmarks.cc | 49 ++++------ engine/src/flutter/impeller/geometry/path.cc | 66 ++++++++++++++ engine/src/flutter/impeller/geometry/path.h | 8 ++ .../impeller/geometry/path_component.cc | 77 ++++++++++++++++ .../impeller/geometry/path_component.h | 26 +++++- .../impeller/renderer/renderer_unittests.cc | 28 ++---- .../src/flutter/impeller/tessellator/BUILD.gn | 2 - .../impeller/tessellator/c/tessellator.cc | 2 +- .../impeller/tessellator/tessellator.cc | 91 ++++++++----------- .../impeller/tessellator/tessellator.h | 21 ++++- .../tessellator/tessellator_libtess.cc | 25 +++-- .../tessellator/tessellator_libtess.h | 15 ++- .../tessellator/tessellator_unittests.cc | 86 ++++++++++++------ 14 files changed, 339 insertions(+), 168 deletions(-) diff --git a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc index 8bfe86ced1..209a9d647a 100644 --- a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc @@ -36,15 +36,8 @@ GeometryResult FillPathGeometry::GetPositionBuffer( }; } - VertexBuffer vertex_buffer; - - auto points = renderer.GetTessellator()->TessellateConvex( - path_, entity.GetTransform().GetMaxBasisLength()); - - vertex_buffer.vertex_buffer = host_buffer.Emplace( - points.data(), points.size() * sizeof(Point), alignof(Point)); - vertex_buffer.index_buffer = {}, vertex_buffer.vertex_count = points.size(); - vertex_buffer.index_type = IndexType::kNone; + VertexBuffer vertex_buffer = renderer.GetTessellator()->TessellateConvex( + path_, host_buffer, entity.GetTransform().GetMaxBasisLength()); return GeometryResult{ .type = PrimitiveType::kTriangleStrip, diff --git a/engine/src/flutter/impeller/geometry/geometry_benchmarks.cc b/engine/src/flutter/impeller/geometry/geometry_benchmarks.cc index c3ab47f86e..23efab04d6 100644 --- a/engine/src/flutter/impeller/geometry/geometry_benchmarks.cc +++ b/engine/src/flutter/impeller/geometry/geometry_benchmarks.cc @@ -9,7 +9,6 @@ #include "impeller/entity/geometry/stroke_path_geometry.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" -#include "impeller/tessellator/tessellator.h" #include "impeller/tessellator/tessellator_libtess.h" namespace impeller { @@ -44,39 +43,22 @@ template static void BM_Polyline(benchmark::State& state, Args&&... args) { auto args_tuple = std::make_tuple(std::move(args)...); auto path = std::get(args_tuple); - bool tessellate = std::get(args_tuple); size_t point_count = 0u; size_t single_point_count = 0u; auto points = std::make_unique>(); points->reserve(2048); while (state.KeepRunning()) { - if (tessellate) { - tess.Tessellate(path, 1.0f, - [&point_count, &single_point_count]( - const float* vertices, size_t vertices_count, - const uint16_t* indices, size_t indices_count) { - if (indices_count > 0) { - single_point_count = indices_count; - point_count += indices_count; - } else { - single_point_count = vertices_count; - point_count += vertices_count; - } - return true; - }); - } else { - auto polyline = path.CreatePolyline( - // Clang-tidy doesn't know that the points get moved back before - // getting moved again in this loop. - // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move) - 1.0f, std::move(points), - [&points](Path::Polyline::PointBufferPtr reclaimed) { - points = std::move(reclaimed); - }); - single_point_count = polyline.points->size(); - point_count += single_point_count; - } + auto polyline = path.CreatePolyline( + // Clang-tidy doesn't know that the points get moved back before + // getting moved again in this loop. + // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move) + 1.0f, std::move(points), + [&points](Path::Polyline::PointBufferPtr reclaimed) { + points = std::move(reclaimed); + }); + single_point_count = polyline.points->size(); + point_count += single_point_count; } state.counters["SinglePointCount"] = single_point_count; state.counters["TotalPointCount"] = point_count; @@ -121,11 +103,14 @@ static void BM_Convex(benchmark::State& state, Args&&... args) { size_t point_count = 0u; size_t single_point_count = 0u; auto points = std::make_unique>(); + auto indices = std::make_unique>(); points->reserve(2048); while (state.KeepRunning()) { - auto points = tess.TessellateConvex(path, 1.0f); - single_point_count = points.size(); - point_count += points.size(); + points->clear(); + indices->clear(); + Tessellator::TessellateConvexInternal(path, *points, *indices, 1.0f); + single_point_count = indices->size(); + point_count += indices->size(); } state.counters["SinglePointCount"] = single_point_count; state.counters["TotalPointCount"] = point_count; @@ -143,14 +128,12 @@ static void BM_Convex(benchmark::State& state, Args&&... args) { MAKE_STROKE_BENCHMARK_CAPTURE(path, Round, Bevel, false, uvname, uvtype) BENCHMARK_CAPTURE(BM_Polyline, cubic_polyline, CreateCubic(true), false); -BENCHMARK_CAPTURE(BM_Polyline, cubic_polyline_tess, CreateCubic(true), true); BENCHMARK_CAPTURE(BM_Polyline, unclosed_cubic_polyline, CreateCubic(false), false); BENCHMARK_CAPTURE(BM_Polyline, quad_polyline, CreateQuadratic(true), false); -BENCHMARK_CAPTURE(BM_Polyline, quad_polyline_tess, CreateQuadratic(true), true); BENCHMARK_CAPTURE(BM_Polyline, unclosed_quad_polyline, CreateQuadratic(false), diff --git a/engine/src/flutter/impeller/geometry/path.cc b/engine/src/flutter/impeller/geometry/path.cc index 2eb682f5ff..9db3663c09 100644 --- a/engine/src/flutter/impeller/geometry/path.cc +++ b/engine/src/flutter/impeller/geometry/path.cc @@ -103,6 +103,72 @@ void Path::EnumerateComponents( } } +void Path::WritePolyline(Scalar scale, VertexWriter& writer) const { + auto& path_components = data_->components; + auto& path_points = data_->points; + bool started_contour = false; + bool first_point = true; + + for (size_t component_i = 0; component_i < path_components.size(); + component_i++) { + const auto& path_component = path_components[component_i]; + switch (path_component.type) { + case ComponentType::kLinear: { + const LinearPathComponent* linear = + reinterpret_cast( + &path_points[path_component.index]); + if (first_point) { + writer.Write(linear->p1); + first_point = false; + } + writer.Write(linear->p2); + break; + } + case ComponentType::kQuadratic: { + const QuadraticPathComponent* quad = + reinterpret_cast( + &path_points[path_component.index]); + if (first_point) { + writer.Write(quad->p1); + first_point = false; + } + quad->ToLinearPathComponents(scale, writer); + break; + } + case ComponentType::kCubic: { + const CubicPathComponent* cubic = + reinterpret_cast( + &path_points[path_component.index]); + if (first_point) { + writer.Write(cubic->p1); + first_point = false; + } + cubic->ToLinearPathComponents(scale, writer); + break; + } + case ComponentType::kContour: + if (component_i == path_components.size() - 1) { + // If the last component is a contour, that means it's an empty + // contour, so skip it. + continue; + } + // The contour component type is the first segment in a contour. + // Since this should contain the destination (if closed), we + // can start with this point. If there was already an open + // contour, or we've reached the end of the verb list, we + // also close the contour. + if (started_contour) { + writer.EndContour(); + } + started_contour = true; + first_point = true; + } + } + if (started_contour) { + writer.EndContour(); + } +} + bool Path::GetLinearComponentAtIndex(size_t index, LinearPathComponent& linear) const { auto& components = data_->components; diff --git a/engine/src/flutter/impeller/geometry/path.h b/engine/src/flutter/impeller/geometry/path.h index ba56a4101c..0eebfab5cf 100644 --- a/engine/src/flutter/impeller/geometry/path.h +++ b/engine/src/flutter/impeller/geometry/path.h @@ -11,6 +11,7 @@ #include #include "impeller/geometry/path_component.h" +#include "impeller/geometry/rect.h" namespace impeller { @@ -172,6 +173,13 @@ class Path { std::optional GetTransformedBoundingBox(const Matrix& transform) const; + /// Generate a polyline into the temporary storage held by the [writer]. + /// + /// It is suitable to use the max basis length of the matrix used to transform + /// the path. If the provided scale is 0, curves will revert to straight + /// lines. + void WritePolyline(Scalar scale, VertexWriter& writer) const; + private: friend class PathBuilder; diff --git a/engine/src/flutter/impeller/geometry/path_component.cc b/engine/src/flutter/impeller/geometry/path_component.cc index 3a00414dfa..6c3492fd2a 100644 --- a/engine/src/flutter/impeller/geometry/path_component.cc +++ b/engine/src/flutter/impeller/geometry/path_component.cc @@ -10,6 +10,64 @@ namespace impeller { +VertexWriter::VertexWriter(std::vector& points, + std::vector& indices) + : points_(points), indices_(indices) {} + +void VertexWriter::EndContour() { + if (points_.size() == 0u || contour_start_ == points_.size() - 1) { + // Empty or first contour. + return; + } + + auto start = contour_start_; + auto end = points_.size() - 1; + // All filled paths are drawn as if they are closed, but if + // there is an explicit close then a lineTo to the origin + // is inserted. This point isn't strictly necesary to + // correctly render the shape and can be dropped. + if (points_[end] == points_[start]) { + end--; + } + + // Triangle strip break for subsequent contours + if (contour_start_ != 0) { + auto back = indices_.back(); + indices_.push_back(back); + indices_.push_back(start); + indices_.push_back(start); + + // 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_) { + indices_.push_back(start); + } + } else { + indices_.push_back(start); + } + + size_t a = start + 1; + size_t b = end; + while (a < b) { + indices_.push_back(a); + indices_.push_back(b); + a++; + b--; + } + if (a == b) { + indices_.push_back(a); + previous_contour_odd_points_ = false; + } else { + previous_contour_odd_points_ = true; + } + contour_start_ = points_.size(); +} + +void VertexWriter::Write(Point point) { + points_.push_back(point); +} + /* * Based on: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases */ @@ -100,6 +158,16 @@ Point QuadraticPathComponent::SolveDerivative(Scalar time) const { }; } +void QuadraticPathComponent::ToLinearPathComponents( + Scalar scale, + VertexWriter& writer) const { + Scalar line_count = std::ceilf(ComputeQuadradicSubdivisions(scale, *this)); + for (size_t i = 1; i < line_count; i += 1) { + writer.Write(Solve(i / line_count)); + } + writer.Write(p2); +} + void QuadraticPathComponent::AppendPolylinePoints( Scalar scale_factor, std::vector& points) const { @@ -165,6 +233,15 @@ void CubicPathComponent::AppendPolylinePoints( scale, [&points](const Point& point) { points.emplace_back(point); }); } +void CubicPathComponent::ToLinearPathComponents(Scalar scale, + VertexWriter& writer) const { + Scalar line_count = std::ceilf(ComputeCubicSubdivisions(scale, *this)); + for (size_t i = 1; i < line_count; i++) { + writer.Write(Solve(i / line_count)); + } + writer.Write(p2); +} + inline QuadraticPathComponent CubicPathComponent::Lower() const { return QuadraticPathComponent(3.0 * (cp1 - p1), 3.0 * (cp2 - cp1), 3.0 * (p2 - cp2)); diff --git a/engine/src/flutter/impeller/geometry/path_component.h b/engine/src/flutter/impeller/geometry/path_component.h index c310052a89..dca03126bb 100644 --- a/engine/src/flutter/impeller/geometry/path_component.h +++ b/engine/src/flutter/impeller/geometry/path_component.h @@ -6,16 +6,36 @@ #define FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_ #include +#include #include #include #include #include "impeller/geometry/point.h" -#include "impeller/geometry/rect.h" #include "impeller/geometry/scalar.h" namespace impeller { +/// @brief An interface for generating a multi contour polyline as a triangle +/// strip. +class VertexWriter { + public: + explicit VertexWriter(std::vector& points, + std::vector& indices); + + ~VertexWriter() = default; + + void EndContour(); + + void Write(Point point); + + private: + bool previous_contour_odd_points_ = false; + size_t contour_start_ = 0u; + std::vector& points_; + std::vector& indices_; +}; + struct LinearPathComponent { Point p1; Point p2; @@ -64,6 +84,8 @@ struct QuadraticPathComponent { void ToLinearPathComponents(Scalar scale_factor, const PointProc& proc) const; + void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const; + std::vector Extrema() const; bool operator==(const QuadraticPathComponent& other) const { @@ -109,6 +131,8 @@ struct CubicPathComponent { void ToLinearPathComponents(Scalar scale, const PointProc& proc) const; + void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const; + CubicPathComponent Subsegment(Scalar t0, Scalar t1) const; bool operator==(const CubicPathComponent& other) const { diff --git a/engine/src/flutter/impeller/renderer/renderer_unittests.cc b/engine/src/flutter/impeller/renderer/renderer_unittests.cc index f26d2283c2..545e4cd6c4 100644 --- a/engine/src/flutter/impeller/renderer/renderer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/renderer_unittests.cc @@ -35,7 +35,6 @@ #include "impeller/renderer/render_target.h" #include "impeller/renderer/renderer.h" #include "impeller/renderer/vertex_buffer_builder.h" -#include "impeller/tessellator/tessellator_libtess.h" #include "third_party/imgui/imgui.h" // TODO(zanderso): https://github.com/flutter/flutter/issues/127701 @@ -389,25 +388,14 @@ TEST_P(RendererTest, CanRenderInstanced) { using FS = InstancedDrawFragmentShader; VertexBufferBuilder builder; - - ASSERT_EQ(Tessellator::Result::kSuccess, - TessellatorLibtess{}.Tessellate( - PathBuilder{} - .AddRect(Rect::MakeXYWH(10, 10, 100, 100)) - .TakePath(FillType::kOdd), - 1.0f, - [&builder](const float* vertices, size_t vertices_count, - const uint16_t* indices, size_t indices_count) { - for (auto i = 0u; i < vertices_count * 2; i += 2) { - VS::PerVertexData data; - data.vtx = {vertices[i], vertices[i + 1]}; - builder.AppendVertex(data); - } - for (auto i = 0u; i < indices_count; i++) { - builder.AppendIndex(indices[i]); - } - return true; - })); + builder.AddVertices({ + VS::PerVertexData{Point{10, 10}}, + VS::PerVertexData{Point{10, 110}}, + VS::PerVertexData{Point{110, 10}}, + VS::PerVertexData{Point{10, 110}}, + VS::PerVertexData{Point{110, 10}}, + VS::PerVertexData{Point{110, 110}}, + }); ASSERT_NE(GetContext(), nullptr); auto pipeline = diff --git a/engine/src/flutter/impeller/tessellator/BUILD.gn b/engine/src/flutter/impeller/tessellator/BUILD.gn index 1ad6a208c3..2a821e667b 100644 --- a/engine/src/flutter/impeller/tessellator/BUILD.gn +++ b/engine/src/flutter/impeller/tessellator/BUILD.gn @@ -27,7 +27,6 @@ impeller_component("tessellator_libtess") { public_deps = [ "../geometry" ] deps = [ - ":tessellator", "../core", "//flutter/fml", "//third_party/libtess2", @@ -48,7 +47,6 @@ impeller_component("tessellator_shared") { ] deps = [ - ":tessellator", ":tessellator_libtess", "../core", "../geometry", diff --git a/engine/src/flutter/impeller/tessellator/c/tessellator.cc b/engine/src/flutter/impeller/tessellator/c/tessellator.cc index 9b4d6ea20b..fe3587beb2 100644 --- a/engine/src/flutter/impeller/tessellator/c/tessellator.cc +++ b/engine/src/flutter/impeller/tessellator/c/tessellator.cc @@ -59,7 +59,7 @@ struct Vertices* Tessellate(PathBuilder* builder, points.push_back(point.y); } return true; - }) != Tessellator::Result::kSuccess) { + }) != TessellatorLibtess::Result::kSuccess) { return nullptr; } diff --git a/engine/src/flutter/impeller/tessellator/tessellator.cc b/engine/src/flutter/impeller/tessellator/tessellator.cc index cfc26a7adf..9c756e3ba1 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator.cc @@ -7,8 +7,10 @@ namespace impeller { Tessellator::Tessellator() - : point_buffer_(std::make_unique>()) { + : point_buffer_(std::make_unique>()), + index_buffer_(std::make_unique>()) { point_buffer_->reserve(2048); + index_buffer_->reserve(2048); } Tessellator::~Tessellator() = default; @@ -25,67 +27,48 @@ Path::Polyline Tessellator::CreateTempPolyline(const Path& path, return polyline; } -std::vector Tessellator::TessellateConvex(const Path& path, - Scalar tolerance) { +VertexBuffer Tessellator::TessellateConvex(const Path& path, + HostBuffer& host_buffer, + Scalar tolerance) { FML_DCHECK(point_buffer_); + FML_DCHECK(index_buffer_); + TessellateConvexInternal(path, *point_buffer_, *index_buffer_, tolerance); - std::vector output; - point_buffer_->clear(); - auto polyline = - path.CreatePolyline(tolerance, std::move(point_buffer_), - [this](Path::Polyline::PointBufferPtr point_buffer) { - point_buffer_ = std::move(point_buffer); - }); - if (polyline.points->size() == 0) { - return output; + if (point_buffer_->empty()) { + return VertexBuffer{ + .vertex_buffer = {}, + .index_buffer = {}, + .vertex_count = 0u, + .index_type = IndexType::k16bit, + }; } - 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); + BufferView vertex_buffer = host_buffer.Emplace( + point_buffer_->data(), sizeof(Point) * point_buffer_->size(), + alignof(Point)); - // 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. - if (polyline.GetPoint(end - 1) == first_point) { - end--; - } + BufferView index_buffer = host_buffer.Emplace( + index_buffer_->data(), sizeof(uint16_t) * index_buffer_->size(), + alignof(uint16_t)); - if (j > 0) { - // Triangle strip break. - output.emplace_back(output.back()); - output.emplace_back(first_point); - output.emplace_back(first_point); + return VertexBuffer{ + .vertex_buffer = std::move(vertex_buffer), + .index_buffer = std::move(index_buffer), + .vertex_count = index_buffer_->size(), + .index_type = IndexType::k16bit, + }; +} - // 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); - } +void Tessellator::TessellateConvexInternal(const Path& path, + std::vector& point_buffer, + std::vector& index_buffer, + Scalar tolerance) { + point_buffer.clear(); + index_buffer.clear(); - 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; + VertexWriter writer(point_buffer, index_buffer); + + path.WritePolyline(tolerance, writer); } static constexpr int kPrecomputedDivisionCount = 1024; diff --git a/engine/src/flutter/impeller/tessellator/tessellator.h b/engine/src/flutter/impeller/tessellator/tessellator.h index 8b801192d8..06e4a1228e 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.h +++ b/engine/src/flutter/impeller/tessellator/tessellator.h @@ -10,6 +10,8 @@ #include #include "impeller/core/formats.h" +#include "impeller/core/host_buffer.h" +#include "impeller/core/vertex_buffer.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" #include "impeller/geometry/trig.h" @@ -180,7 +182,21 @@ class Tessellator { /// /// @return A point vector containing the vertices in triangle strip format. /// - std::vector TessellateConvex(const Path& path, Scalar tolerance); + /// @param[in] host_buffer The host buffer for allocation of vertices/index + /// data. + /// @return A vertex buffer containing all data from the provided curve. + VertexBuffer TessellateConvex(const Path& path, + HostBuffer& host_buffer, + Scalar tolerance); + + /// Visible for testing. + /// + /// This method only exists for the ease of benchmarking without using the + /// real allocator needed by the [host_buffer]. + static void TessellateConvexInternal(const Path& path, + std::vector& point_buffer, + std::vector& index_buffer, + Scalar tolerance); //---------------------------------------------------------------------------- /// @brief Create a temporary polyline. Only one per-process can exist at @@ -269,9 +285,10 @@ class Tessellator { protected: /// Used for polyline generation. std::unique_ptr> point_buffer_; + std::unique_ptr> index_buffer_; private: - // Data for variouos Circle/EllipseGenerator classes, cached per + // Data for various Circle/EllipseGenerator classes, cached per // Tessellator instance which is usually the foreground life of an app // if not longer. static constexpr size_t kCachedTrigCount = 300; diff --git a/engine/src/flutter/impeller/tessellator/tessellator_libtess.cc b/engine/src/flutter/impeller/tessellator/tessellator_libtess.cc index 38efb79305..a0b66aceab 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator_libtess.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator_libtess.cc @@ -32,7 +32,7 @@ static const TESSalloc kAlloc = { }; TessellatorLibtess::TessellatorLibtess() - : Tessellator(), c_tessellator_(nullptr, &DestroyTessellator) { + : c_tessellator_(nullptr, &DestroyTessellator) { TESSalloc alloc = kAlloc; { // libTess2 copies the TESSalloc despite the non-const argument. @@ -58,25 +58,22 @@ TessellatorLibtess::Result TessellatorLibtess::Tessellate( Scalar tolerance, const BuilderCallback& callback) { if (!callback) { - return Result::kInputError; + return TessellatorLibtess::Result::kInputError; } - point_buffer_->clear(); - auto polyline = - path.CreatePolyline(tolerance, std::move(point_buffer_), - [this](Path::Polyline::PointBufferPtr point_buffer) { - point_buffer_ = std::move(point_buffer); - }); + std::unique_ptr> point_buffer = + std::make_unique>(); + auto polyline = path.CreatePolyline(tolerance, std::move(point_buffer)); auto fill_type = path.GetFillType(); if (polyline.points->empty()) { - return Result::kInputError; + return TessellatorLibtess::Result::kInputError; } auto tessellator = c_tessellator_.get(); if (!tessellator) { - return Result::kTessellationError; + return TessellatorLibtess::Result::kTessellationError; } constexpr int kVertexSize = 2; @@ -112,7 +109,7 @@ TessellatorLibtess::Result TessellatorLibtess::Tessellate( ); if (result != 1) { - return Result::kTessellationError; + return TessellatorLibtess::Result::kTessellationError; } int element_item_count = tessGetElementCount(tessellator) * kPolygonSize; @@ -135,7 +132,7 @@ TessellatorLibtess::Result TessellatorLibtess::Tessellate( } if (!callback(vertices, vertex_item_count, indices.data(), element_item_count)) { - return Result::kInputError; + return TessellatorLibtess::Result::kInputError; } } else { std::vector points; @@ -156,11 +153,11 @@ TessellatorLibtess::Result TessellatorLibtess::Tessellate( data.emplace_back(points[elements[i]].y); } if (!callback(data.data(), element_item_count, nullptr, 0u)) { - return Result::kInputError; + return TessellatorLibtess::Result::kInputError; } } - return Result::kSuccess; + return TessellatorLibtess::Result::kSuccess; } void DestroyTessellator(TESStesselator* tessellator) { diff --git a/engine/src/flutter/impeller/tessellator/tessellator_libtess.h b/engine/src/flutter/impeller/tessellator/tessellator_libtess.h index ff8ecf1391..d7fab33e45 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator_libtess.h +++ b/engine/src/flutter/impeller/tessellator/tessellator_libtess.h @@ -9,7 +9,6 @@ #include #include "impeller/geometry/path.h" -#include "impeller/tessellator/tessellator.h" struct TESStesselator; @@ -27,12 +26,18 @@ using CTessellator = /// This object is not thread safe, and its methods must not be /// called from multiple threads. /// -class TessellatorLibtess : public Tessellator { +class TessellatorLibtess { public: TessellatorLibtess(); ~TessellatorLibtess(); + enum class Result { + kSuccess, + kInputError, + kTessellationError, + }; + /// @brief A callback that returns the results of the tessellation. /// /// The index buffer may not be populated, in which case [indices] will @@ -55,9 +60,9 @@ class TessellatorLibtess : public Tessellator { /// /// @return The result status of the tessellation. /// - Tessellator::Result Tessellate(const Path& path, - Scalar tolerance, - const BuilderCallback& callback); + TessellatorLibtess::Result Tessellate(const Path& path, + Scalar tolerance, + const BuilderCallback& callback); private: CTessellator c_tessellator_; diff --git a/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc b/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc index 571308eff8..cff81e9886 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc @@ -8,6 +8,7 @@ #include "impeller/geometry/geometry_asserts.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" +#include "impeller/tessellator/tessellator.h" #include "impeller/tessellator/tessellator_libtess.h" namespace impeller { @@ -18,36 +19,36 @@ TEST(TessellatorTest, TessellatorBuilderReturnsCorrectResultStatus) { { TessellatorLibtess t; auto path = PathBuilder{}.TakePath(FillType::kOdd); - Tessellator::Result result = t.Tessellate( + TessellatorLibtess::Result result = t.Tessellate( path, 1.0f, [](const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count) { return true; }); - ASSERT_EQ(result, Tessellator::Result::kInputError); + ASSERT_EQ(result, TessellatorLibtess::Result::kInputError); } // One point. { TessellatorLibtess t; auto path = PathBuilder{}.LineTo({0, 0}).TakePath(FillType::kOdd); - Tessellator::Result result = t.Tessellate( + TessellatorLibtess::Result result = t.Tessellate( path, 1.0f, [](const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count) { return true; }); - ASSERT_EQ(result, Tessellator::Result::kSuccess); + ASSERT_EQ(result, TessellatorLibtess::Result::kSuccess); } // Two points. { TessellatorLibtess t; auto path = PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath(FillType::kOdd); - Tessellator::Result result = t.Tessellate( + TessellatorLibtess::Result result = t.Tessellate( path, 1.0f, [](const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count) { return true; }); - ASSERT_EQ(result, Tessellator::Result::kSuccess); + ASSERT_EQ(result, TessellatorLibtess::Result::kSuccess); } // Many points. @@ -59,53 +60,82 @@ TEST(TessellatorTest, TessellatorBuilderReturnsCorrectResultStatus) { builder.AddLine({coord, coord}, {coord + 1, coord + 1}); } auto path = builder.TakePath(FillType::kOdd); - Tessellator::Result result = t.Tessellate( + TessellatorLibtess::Result result = t.Tessellate( path, 1.0f, [](const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count) { return true; }); - ASSERT_EQ(result, Tessellator::Result::kSuccess); + ASSERT_EQ(result, TessellatorLibtess::Result::kSuccess); } // Closure fails. { TessellatorLibtess t; auto path = PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath(FillType::kOdd); - Tessellator::Result result = t.Tessellate( + TessellatorLibtess::Result result = t.Tessellate( path, 1.0f, [](const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count) { return false; }); - ASSERT_EQ(result, Tessellator::Result::kInputError); + ASSERT_EQ(result, TessellatorLibtess::Result::kInputError); } } TEST(TessellatorTest, TessellateConvex) { { - Tessellator t; + std::vector points; + std::vector indices; // Sanity check simple rectangle. - auto pts = t.TessellateConvex( - PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 10, 10)).TakePath(), 1.0); + Tessellator::TessellateConvexInternal( + PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 10, 10)).TakePath(), points, + indices, 1.0); - std::vector expected = {{0, 0}, {10, 0}, {0, 10}, {10, 10}}; - EXPECT_EQ(pts, expected); + // Note: the origin point is repeated but not referenced in the indices + // below + std::vector expected = {{0, 0}, {10, 0}, {10, 10}, {0, 10}, {0, 0}}; + std::vector expected_indices = {0, 1, 3, 2}; + EXPECT_EQ(points, expected); + EXPECT_EQ(indices, expected_indices); } { - Tessellator t; - auto pts = t.TessellateConvex(PathBuilder{} - .AddRect(Rect::MakeLTRB(0, 0, 10, 10)) - .AddRect(Rect::MakeLTRB(20, 20, 30, 30)) - .TakePath(), - 1.0); + std::vector points; + std::vector indices; + Tessellator::TessellateConvexInternal( + PathBuilder{} + .AddRect(Rect::MakeLTRB(0, 0, 10, 10)) + .AddRect(Rect::MakeLTRB(20, 20, 30, 30)) + .TakePath(), + points, indices, 1.0); - 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); + std::vector expected = {{0, 0}, {10, 0}, {10, 10}, {0, 10}, + {0, 0}, {20, 20}, {30, 20}, {30, 30}, + {20, 30}, {20, 20}}; + std::vector expected_indices = {0, 1, 3, 2, 2, 5, 5, 6, 8, 7}; + EXPECT_EQ(points, expected); + EXPECT_EQ(indices, expected_indices); } } +// Filled Paths without an explicit close should still be closed +TEST(TessellatorTest, TessellateConvexUnclosedPath) { + std::vector points; + std::vector indices; + + // Create a rectangle that lacks an explicit close. + Path path = PathBuilder{} + .LineTo({100, 0}) + .LineTo({100, 100}) + .LineTo({0, 100}) + .TakePath(); + Tessellator::TessellateConvexInternal(path, points, indices, 1.0); + + std::vector expected = {{0, 0}, {100, 0}, {100, 100}, {0, 100}}; + std::vector expected_indices = {0, 1, 3, 2}; + EXPECT_EQ(points, expected); + EXPECT_EQ(indices, expected_indices); +} + TEST(TessellatorTest, CircleVertexCounts) { auto tessellator = std::make_shared(); @@ -469,12 +499,14 @@ TEST(TessellatorTest, FilledRoundRectTessellationVertices) { TEST(TessellatorTest, EarlyReturnEmptyConvexShape) { // This path is not technically empty (it has a size in one dimension), // but is otherwise completely flat. - auto tessellator = std::make_shared(); PathBuilder builder; builder.MoveTo({0, 0}); builder.MoveTo({10, 10}, /*relative=*/true); - auto points = tessellator->TessellateConvex(builder.TakePath(), 3.0); + std::vector points; + std::vector indices; + Tessellator::TessellateConvexInternal(builder.TakePath(), points, indices, + 3.0); EXPECT_TRUE(points.empty()); }