From 892d301b87b7d0a95281dc65028039d344402b99 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Mon, 22 May 2023 22:46:41 -0700 Subject: [PATCH] [Impeller] Place Entity Geometry classes in separate TUs (flutter/engine#42236) Resolves https://github.com/flutter/flutter/issues/122854. One of those quality of life things I keep putting off... --- .../ci/licenses_golden/licenses_flutter | 32 +- engine/src/flutter/impeller/aiks/canvas.cc | 2 +- engine/src/flutter/impeller/aiks/canvas.h | 3 +- engine/src/flutter/impeller/aiks/paint.cc | 2 +- engine/src/flutter/impeller/aiks/paint.h | 2 +- .../impeller/display_list/dl_dispatcher.cc | 2 +- .../display_list/dl_vertices_geometry.h | 2 +- engine/src/flutter/impeller/entity/BUILD.gn | 16 +- .../entity/contents/atlas_contents.cc | 2 +- .../impeller/entity/contents/clip_contents.h | 2 +- .../entity/contents/color_source_contents.h | 2 +- .../contents/conical_gradient_contents.cc | 2 +- .../contents/radial_gradient_contents.cc | 2 +- .../entity/contents/solid_color_contents.h | 2 +- .../entity/contents/tiled_texture_contents.cc | 2 +- .../entity/contents/vertices_contents.h | 3 +- .../impeller/entity/entity_unittests.cc | 4 +- .../src/flutter/impeller/entity/geometry.cc | 1110 ----------------- engine/src/flutter/impeller/entity/geometry.h | 313 ----- .../entity/geometry/cover_geometry.cc | 57 + .../impeller/entity/geometry/cover_geometry.h | 41 + .../entity/geometry/fill_path_geometry.cc | 150 +++ .../entity/geometry/fill_path_geometry.h | 42 + .../impeller/entity/geometry/geometry.cc | 141 +++ .../impeller/entity/geometry/geometry.h | 92 ++ .../entity/geometry/point_field_geometry.cc | 296 +++++ .../entity/geometry/point_field_geometry.h | 57 + .../impeller/entity/geometry/rect_geometry.cc | 50 + .../impeller/entity/geometry/rect_geometry.h | 41 + .../entity/geometry/stroke_path_geometry.cc | 444 +++++++ .../entity/geometry/stroke_path_geometry.h | 94 ++ .../entity/geometry/vertices_geometry.cc | 11 + .../entity/geometry/vertices_geometry.h | 25 + 33 files changed, 1602 insertions(+), 1444 deletions(-) delete mode 100644 engine/src/flutter/impeller/entity/geometry.cc delete mode 100644 engine/src/flutter/impeller/entity/geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/cover_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/cover_geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/point_field_geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/rect_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/rect_geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h create mode 100644 engine/src/flutter/impeller/entity/geometry/vertices_geometry.cc create mode 100644 engine/src/flutter/impeller/entity/geometry/vertices_geometry.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 062a2adb77..7e33115b91 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1241,8 +1241,20 @@ ORIGIN: ../../../flutter/impeller/entity/entity_pass_target.cc + ../../../flutte ORIGIN: ../../../flutter/impeller/entity/entity_pass_target.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/entity_playground.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/entity_playground.h + ../../../flutter/LICENSE -ORIGIN: ../../../flutter/impeller/entity/geometry.cc + ../../../flutter/LICENSE -ORIGIN: ../../../flutter/impeller/entity/geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/cover_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/cover_geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/fill_path_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/fill_path_geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/point_field_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/point_field_geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/rect_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/rect_geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/geometry/vertices_geometry.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl + ../../../flutter/LICENSE @@ -3869,8 +3881,20 @@ FILE: ../../../flutter/impeller/entity/entity_pass_target.cc FILE: ../../../flutter/impeller/entity/entity_pass_target.h FILE: ../../../flutter/impeller/entity/entity_playground.cc FILE: ../../../flutter/impeller/entity/entity_playground.h -FILE: ../../../flutter/impeller/entity/geometry.cc -FILE: ../../../flutter/impeller/entity/geometry.h +FILE: ../../../flutter/impeller/entity/geometry/cover_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/cover_geometry.h +FILE: ../../../flutter/impeller/entity/geometry/fill_path_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/fill_path_geometry.h +FILE: ../../../flutter/impeller/entity/geometry/geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/geometry.h +FILE: ../../../flutter/impeller/entity/geometry/point_field_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/point_field_geometry.h +FILE: ../../../flutter/impeller/entity/geometry/rect_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/rect_geometry.h +FILE: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.h +FILE: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc +FILE: ../../../flutter/impeller/entity/geometry/vertices_geometry.h FILE: ../../../flutter/impeller/entity/inline_pass_context.cc FILE: ../../../flutter/impeller/entity/inline_pass_context.h FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 6ac4df2f0d..eeef419894 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -17,7 +17,7 @@ #include "impeller/entity/contents/text_contents.h" #include "impeller/entity/contents/texture_contents.h" #include "impeller/entity/contents/vertices_contents.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/path_builder.h" namespace impeller { diff --git a/engine/src/flutter/impeller/aiks/canvas.h b/engine/src/flutter/impeller/aiks/canvas.h index 5833317c00..6fb5484f0d 100644 --- a/engine/src/flutter/impeller/aiks/canvas.h +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -16,7 +16,8 @@ #include "impeller/aiks/picture.h" #include "impeller/core/sampler_descriptor.h" #include "impeller/entity/entity_pass.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" +#include "impeller/entity/geometry/vertices_geometry.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 47af19746a..fa0dc47eef 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -5,7 +5,7 @@ #include "impeller/aiks/paint.h" #include "impeller/entity/contents/color_source_contents.h" #include "impeller/entity/contents/solid_color_contents.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" namespace impeller { diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 709e66e6de..077b5264c0 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -15,7 +15,7 @@ #include "impeller/entity/contents/radial_gradient_contents.h" #include "impeller/entity/contents/sweep_gradient_contents.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/color.h" namespace impeller { diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc index be3fb80af7..0b79f9e704 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc @@ -29,7 +29,7 @@ #include "impeller/entity/contents/sweep_gradient_contents.h" #include "impeller/entity/contents/tiled_texture_contents.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" #include "impeller/geometry/scalar.h" diff --git a/engine/src/flutter/impeller/display_list/dl_vertices_geometry.h b/engine/src/flutter/impeller/display_list/dl_vertices_geometry.h index 031b75abf0..538b65a6d5 100644 --- a/engine/src/flutter/impeller/display_list/dl_vertices_geometry.h +++ b/engine/src/flutter/impeller/display_list/dl_vertices_geometry.h @@ -9,7 +9,7 @@ #include "flutter/display_list/dl_vertices.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/vertices_geometry.h" #include "impeller/geometry/color.h" #include "impeller/geometry/point.h" #include "impeller/geometry/rect.h" diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 25acf63131..8137b8ccae 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -223,8 +223,20 @@ impeller_component("entity") { "entity_pass_delegate.h", "entity_pass_target.cc", "entity_pass_target.h", - "geometry.cc", - "geometry.h", + "geometry/cover_geometry.cc", + "geometry/cover_geometry.h", + "geometry/fill_path_geometry.cc", + "geometry/fill_path_geometry.h", + "geometry/geometry.cc", + "geometry/geometry.h", + "geometry/point_field_geometry.cc", + "geometry/point_field_geometry.h", + "geometry/rect_geometry.cc", + "geometry/rect_geometry.h", + "geometry/stroke_path_geometry.cc", + "geometry/stroke_path_geometry.h", + "geometry/vertices_geometry.cc", + "geometry/vertices_geometry.h", "inline_pass_context.cc", "inline_pass_context.h", ] diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc index 38d187f26a..a3a85ca068 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc @@ -16,7 +16,7 @@ #include "impeller/entity/contents/framebuffer_blend_contents.h" #include "impeller/entity/contents/texture_contents.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/entity/texture_fill.frag.h" #include "impeller/entity/texture_fill.vert.h" #include "impeller/renderer/render_pass.h" diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.h b/engine/src/flutter/impeller/entity/contents/clip_contents.h index f75c352712..3b0faac98b 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.h +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.h @@ -11,7 +11,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/contents.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.h b/engine/src/flutter/impeller/entity/contents/color_source_contents.h index b13d2d9085..a4f0766e1f 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.h +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.h @@ -6,7 +6,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/contents.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/path.h" diff --git a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc index b65964db87..2f04124360 100644 --- a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc @@ -9,7 +9,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/gradient_generator.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/gradient.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc index e7bd713524..c24e7df158 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -9,7 +9,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/gradient_generator.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/gradient.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h index b034c6dbdc..d3cbb35300 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h @@ -11,7 +11,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/color_source_contents.h" #include "impeller/entity/contents/contents.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/geometry/color.h" #include "impeller/geometry/path.h" diff --git a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc index 9322e31f76..cb9275da08 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc @@ -6,7 +6,7 @@ #include "impeller/entity/contents/clip_contents.h" #include "impeller/entity/contents/content_context.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" #include "impeller/entity/texture_fill.frag.h" #include "impeller/entity/tiled_texture_fill.frag.h" #include "impeller/entity/tiled_texture_fill.vert.h" diff --git a/engine/src/flutter/impeller/entity/contents/vertices_contents.h b/engine/src/flutter/impeller/entity/contents/vertices_contents.h index 1ba524899c..2909176879 100644 --- a/engine/src/flutter/impeller/entity/contents/vertices_contents.h +++ b/engine/src/flutter/impeller/entity/contents/vertices_contents.h @@ -12,7 +12,8 @@ #include "impeller/core/sampler_descriptor.h" #include "impeller/entity/contents/contents.h" #include "impeller/entity/entity.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" +#include "impeller/entity/geometry/vertices_geometry.h" #include "impeller/geometry/color.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 1222e8e6ba..c4339fcb95 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -35,7 +35,9 @@ #include "impeller/entity/entity_pass.h" #include "impeller/entity/entity_pass_delegate.h" #include "impeller/entity/entity_playground.h" -#include "impeller/entity/geometry.h" +#include "impeller/entity/geometry/geometry.h" +#include "impeller/entity/geometry/point_field_geometry.h" +#include "impeller/entity/geometry/stroke_path_geometry.h" #include "impeller/geometry/color.h" #include "impeller/geometry/geometry_asserts.h" #include "impeller/geometry/path_builder.h" diff --git a/engine/src/flutter/impeller/entity/geometry.cc b/engine/src/flutter/impeller/entity/geometry.cc deleted file mode 100644 index 23467d5070..0000000000 --- a/engine/src/flutter/impeller/entity/geometry.cc +++ /dev/null @@ -1,1110 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "impeller/entity/geometry.h" - -#include "impeller/core/device_buffer.h" -#include "impeller/entity/contents/content_context.h" -#include "impeller/entity/entity.h" -#include "impeller/entity/points.comp.h" -#include "impeller/entity/position_color.vert.h" -#include "impeller/entity/texture_fill.vert.h" -#include "impeller/entity/uv.comp.h" -#include "impeller/geometry/matrix.h" -#include "impeller/geometry/path_builder.h" -#include "impeller/renderer/command_buffer.h" -#include "impeller/renderer/render_pass.h" -#include "impeller/tessellator/tessellator.h" - -namespace impeller { - -/// Given a convex polyline, create a triangle fan structure. -std::pair, std::vector> TessellateConvex( - Path::Polyline polyline) { - std::vector output; - std::vector indices; - - for (auto j = 0u; j < polyline.contours.size(); j++) { - auto [start, end] = polyline.GetContourPointBounds(j); - auto center = polyline.points[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. - if (polyline.points[end - 1] == polyline.points[start]) { - end--; - } - output.emplace_back(center); - output.emplace_back(polyline.points[start + 1]); - - for (auto i = start + 2; i < end; i++) { - const auto& point_b = polyline.points[i]; - output.emplace_back(point_b); - - indices.emplace_back(0); - indices.emplace_back(i - 1); - indices.emplace_back(i); - } - } - return std::make_pair(output, indices); -} - -/// Compute UV geometry for a VBB that contains only position geometry. -/// -/// texture_origin should be set to 0, 0 for stroke and stroke based geometry, -/// like the point field. -static VertexBufferBuilder -ComputeUVGeometryCPU( - VertexBufferBuilder& input, - Point texture_origin, - Size texture_coverage, - Matrix effect_transform) { - VertexBufferBuilder vertex_builder; - vertex_builder.Reserve(input.GetVertexCount()); - input.IterateVertices( - [&vertex_builder, &texture_coverage, &effect_transform, - &texture_origin](SolidFillVertexShader::PerVertexData old_vtx) { - TextureFillVertexShader::PerVertexData data; - data.position = old_vtx.position; - auto coverage_coords = - (old_vtx.position - texture_origin) / texture_coverage; - data.texture_coords = effect_transform * coverage_coords; - vertex_builder.AppendVertex(data); - }); - return vertex_builder; -} - -Geometry::Geometry() = default; - -Geometry::~Geometry() = default; - -GeometryResult Geometry::GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - return {}; -} - -// static -std::unique_ptr Geometry::MakeFillPath(const Path& path) { - return std::make_unique(path); -} - -// static -std::unique_ptr Geometry::MakePointField(std::vector points, - Scalar radius, - bool round) { - return std::make_unique(std::move(points), radius, round); -} - -// static -std::unique_ptr Geometry::MakeStrokePath(const Path& path, - Scalar stroke_width, - Scalar miter_limit, - Cap stroke_cap, - Join stroke_join) { - // Skia behaves like this. - if (miter_limit < 0) { - miter_limit = 4.0; - } - return std::make_unique(path, stroke_width, miter_limit, - stroke_cap, stroke_join); -} - -std::unique_ptr Geometry::MakeCover() { - return std::make_unique(); -} - -std::unique_ptr Geometry::MakeRect(Rect rect) { - return std::make_unique(rect); -} - -static GeometryResult ComputeUVGeometryForRect(Rect source_rect, - Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - auto& host_buffer = pass.GetTransientsBuffer(); - - std::vector data(8); - auto points = source_rect.GetPoints(); - for (auto i = 0u, j = 0u; i < 8; i += 2, j++) { - data[i] = points[j]; - data[i + 1] = effect_transform * ((points[j] - texture_coverage.origin) / - texture_coverage.size); - } - - return GeometryResult{ - .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = - { - .vertex_buffer = host_buffer.Emplace( - data.data(), 16 * sizeof(float), alignof(float)), - .vertex_count = 4, - .index_type = IndexType::kNone, - }, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -/////// Path Geometry /////// - -FillPathGeometry::FillPathGeometry(const Path& path) : path_(path) {} - -FillPathGeometry::~FillPathGeometry() = default; - -GeometryResult FillPathGeometry::GetPositionBuffer( - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - auto& host_buffer = pass.GetTransientsBuffer(); - VertexBuffer vertex_buffer; - - if (path_.GetFillType() == FillType::kNonZero && // - path_.IsConvex()) { - auto [points, indices] = TessellateConvex( - path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength())); - - vertex_buffer.vertex_buffer = host_buffer.Emplace( - points.data(), points.size() * sizeof(Point), alignof(Point)); - vertex_buffer.index_buffer = host_buffer.Emplace( - indices.data(), indices.size() * sizeof(uint16_t), alignof(uint16_t)); - vertex_buffer.vertex_count = indices.size(); - vertex_buffer.index_type = IndexType::k16bit; - - return GeometryResult{ - .type = PrimitiveType::kTriangle, - .vertex_buffer = vertex_buffer, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; - } - - auto tesselation_result = renderer.GetTessellator()->Tessellate( - path_.GetFillType(), - path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength()), - [&vertex_buffer, &host_buffer]( - const float* vertices, size_t vertices_count, const uint16_t* indices, - size_t indices_count) { - vertex_buffer.vertex_buffer = host_buffer.Emplace( - vertices, vertices_count * sizeof(float), alignof(float)); - vertex_buffer.index_buffer = host_buffer.Emplace( - indices, indices_count * sizeof(uint16_t), alignof(uint16_t)); - vertex_buffer.vertex_count = indices_count; - vertex_buffer.index_type = IndexType::k16bit; - return true; - }); - if (tesselation_result != Tessellator::Result::kSuccess) { - return {}; - } - return GeometryResult{ - .type = PrimitiveType::kTriangle, - .vertex_buffer = vertex_buffer, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -// |Geometry| -GeometryResult FillPathGeometry::GetPositionUVBuffer( - Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - using VS = TextureFillVertexShader; - - if (path_.GetFillType() == FillType::kNonZero && // - path_.IsConvex()) { - auto [points, indices] = TessellateConvex( - path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength())); - - VertexBufferBuilder vertex_builder; - vertex_builder.Reserve(points.size()); - vertex_builder.ReserveIndices(indices.size()); - for (auto i = 0u; i < points.size(); i++) { - VS::PerVertexData data; - data.position = points[i]; - auto coverage_coords = - (points[i] - texture_coverage.origin) / texture_coverage.size; - data.texture_coords = effect_transform * coverage_coords; - vertex_builder.AppendVertex(data); - } - for (auto i = 0u; i < indices.size(); i++) { - vertex_builder.AppendIndex(indices[i]); - } - - return GeometryResult{ - .type = PrimitiveType::kTriangle, - .vertex_buffer = - vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer()), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; - } - - VertexBufferBuilder vertex_builder; - auto tesselation_result = renderer.GetTessellator()->Tessellate( - path_.GetFillType(), - path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength()), - [&vertex_builder, &texture_coverage, &effect_transform]( - const float* vertices, size_t vertices_count, const uint16_t* indices, - size_t indices_count) { - for (auto i = 0u; i < vertices_count; i += 2) { - VS::PerVertexData data; - Point vtx = {vertices[i], vertices[i + 1]}; - data.position = vtx; - auto coverage_coords = - (vtx - texture_coverage.origin) / texture_coverage.size; - data.texture_coords = effect_transform * coverage_coords; - vertex_builder.AppendVertex(data); - } - FML_DCHECK(vertex_builder.GetVertexCount() == vertices_count / 2); - for (auto i = 0u; i < indices_count; i++) { - vertex_builder.AppendIndex(indices[i]); - } - return true; - }); - if (tesselation_result != Tessellator::Result::kSuccess) { - return {}; - } - return GeometryResult{ - .type = PrimitiveType::kTriangle, - .vertex_buffer = - vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer()), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -GeometryVertexType FillPathGeometry::GetVertexType() const { - return GeometryVertexType::kPosition; -} - -std::optional FillPathGeometry::GetCoverage( - const Matrix& transform) const { - return path_.GetTransformedBoundingBox(transform); -} - -///// Stroke Geometry ////// - -StrokePathGeometry::StrokePathGeometry(const Path& path, - Scalar stroke_width, - Scalar miter_limit, - Cap stroke_cap, - Join stroke_join) - : path_(path), - stroke_width_(stroke_width), - miter_limit_(miter_limit), - stroke_cap_(stroke_cap), - stroke_join_(stroke_join) {} - -StrokePathGeometry::~StrokePathGeometry() = default; - -Scalar StrokePathGeometry::GetStrokeWidth() const { - return stroke_width_; -} - -Scalar StrokePathGeometry::GetMiterLimit() const { - return miter_limit_; -} - -Cap StrokePathGeometry::GetStrokeCap() const { - return stroke_cap_; -} - -Join StrokePathGeometry::GetStrokeJoin() const { - return stroke_join_; -} - -// static -Scalar StrokePathGeometry::CreateBevelAndGetDirection( - VertexBufferBuilder& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset) { - SolidFillVertexShader::PerVertexData vtx; - vtx.position = position; - vtx_builder.AppendVertex(vtx); - - Scalar dir = start_offset.Cross(end_offset) > 0 ? -1 : 1; - vtx.position = position + start_offset * dir; - vtx_builder.AppendVertex(vtx); - vtx.position = position + end_offset * dir; - vtx_builder.AppendVertex(vtx); - - return dir; -} - -// static -StrokePathGeometry::JoinProc StrokePathGeometry::GetJoinProc(Join stroke_join) { - using VS = SolidFillVertexShader; - StrokePathGeometry::JoinProc join_proc; - switch (stroke_join) { - case Join::kBevel: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - CreateBevelAndGetDirection(vtx_builder, position, start_offset, - end_offset); - }; - break; - case Join::kMiter: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - Point start_normal = start_offset.Normalize(); - Point end_normal = end_offset.Normalize(); - - // 1 for no joint (straight line), 0 for max joint (180 degrees). - Scalar alignment = (start_normal.Dot(end_normal) + 1) / 2; - if (ScalarNearlyEqual(alignment, 1)) { - return; - } - - Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, - start_offset, end_offset); - - Point miter_point = (start_offset + end_offset) / 2 / alignment; - if (miter_point.GetDistanceSquared({0, 0}) > - miter_limit * miter_limit) { - return; // Convert to bevel when we exceed the miter limit. - } - - // Outer miter point. - VS::PerVertexData vtx; - vtx.position = position + miter_point * dir; - vtx_builder.AppendVertex(vtx); - }; - break; - case Join::kRound: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - Point start_normal = start_offset.Normalize(); - Point end_normal = end_offset.Normalize(); - - // 0 for no joint (straight line), 1 for max joint (180 degrees). - Scalar alignment = 1 - (start_normal.Dot(end_normal) + 1) / 2; - if (ScalarNearlyEqual(alignment, 0)) { - return; - } - - Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, - start_offset, end_offset); - - Point middle = - (start_offset + end_offset).Normalize() * start_offset.GetLength(); - Point middle_normal = middle.Normalize(); - - Point middle_handle = middle + Point(-middle.y, middle.x) * - PathBuilder::kArcApproximationMagic * - alignment * dir; - Point start_handle = - start_offset + Point(start_offset.y, -start_offset.x) * - PathBuilder::kArcApproximationMagic * alignment * - dir; - - auto arc_points = CubicPathComponent(start_offset, start_handle, - middle_handle, middle) - .CreatePolyline(scale); - - VS::PerVertexData vtx; - for (const auto& point : arc_points) { - vtx.position = position + point * dir; - vtx_builder.AppendVertex(vtx); - vtx.position = position + (-point * dir).Reflect(middle_normal); - vtx_builder.AppendVertex(vtx); - } - }; - break; - } - return join_proc; -} - -// static -StrokePathGeometry::CapProc StrokePathGeometry::GetCapProc(Cap stroke_cap) { - using VS = SolidFillVertexShader; - StrokePathGeometry::CapProc cap_proc; - switch (stroke_cap) { - case Cap::kButt: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - VS::PerVertexData vtx; - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - }; - break; - case Cap::kRound: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - - VS::PerVertexData vtx; - - Point forward(offset.y, -offset.x); - Point forward_normal = forward.Normalize(); - - CubicPathComponent arc; - if (reverse) { - arc = CubicPathComponent( - forward, - forward + orientation * PathBuilder::kArcApproximationMagic, - orientation + forward * PathBuilder::kArcApproximationMagic, - orientation); - } else { - arc = CubicPathComponent( - orientation, - orientation + forward * PathBuilder::kArcApproximationMagic, - forward + orientation * PathBuilder::kArcApproximationMagic, - forward); - } - - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - for (const auto& point : arc.CreatePolyline(scale)) { - vtx.position = position + point; - vtx_builder.AppendVertex(vtx); - vtx.position = position + (-point).Reflect(forward_normal); - vtx_builder.AppendVertex(vtx); - } - }; - break; - case Cap::kSquare: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - - VS::PerVertexData vtx; - - Point forward(offset.y, -offset.x); - - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position + orientation + forward; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation + forward; - vtx_builder.AppendVertex(vtx); - }; - break; - } - return cap_proc; -} - -// static -VertexBufferBuilder -StrokePathGeometry::CreateSolidStrokeVertices( - const Path& path, - Scalar stroke_width, - Scalar scaled_miter_limit, - const StrokePathGeometry::JoinProc& join_proc, - const StrokePathGeometry::CapProc& cap_proc, - Scalar scale) { - VertexBufferBuilder vtx_builder; - auto polyline = path.CreatePolyline(scale); - - VS::PerVertexData vtx; - - // Offset state. - Point offset; - Point previous_offset; // Used for computing joins. - - auto compute_offset = [&polyline, &offset, &previous_offset, - &stroke_width](size_t point_i) { - previous_offset = offset; - Point direction = - (polyline.points[point_i] - polyline.points[point_i - 1]).Normalize(); - offset = Vector2{-direction.y, direction.x} * stroke_width * 0.5; - }; - - for (size_t contour_i = 0; contour_i < polyline.contours.size(); - contour_i++) { - auto contour = polyline.contours[contour_i]; - size_t contour_start_point_i, contour_end_point_i; - std::tie(contour_start_point_i, contour_end_point_i) = - polyline.GetContourPointBounds(contour_i); - - switch (contour_end_point_i - contour_start_point_i) { - case 1: { - Point p = polyline.points[contour_start_point_i]; - cap_proc(vtx_builder, p, {-stroke_width * 0.5f, 0}, scale, false); - cap_proc(vtx_builder, p, {stroke_width * 0.5f, 0}, scale, false); - continue; - } - case 0: - continue; // This contour has no renderable content. - default: - break; - } - - // The first point's offset is always the same as the second point. - compute_offset(contour_start_point_i + 1); - const Point contour_first_offset = offset; - - if (contour_i > 0) { - // This branch only executes when we've just finished drawing a contour - // and are switching to a new one. - // We're drawing a triangle strip, so we need to "pick up the pen" by - // appending two vertices at the end of the previous contour and two - // vertices at the start of the new contour (thus connecting the two - // contours with two zero volume triangles, which will be discarded by - // the rasterizer). - vtx.position = polyline.points[contour_start_point_i - 1]; - // Append two vertices when "picking up" the pen so that the triangle - // drawn when moving to the beginning of the new contour will have zero - // volume. - vtx_builder.AppendVertex(vtx); - vtx_builder.AppendVertex(vtx); - - vtx.position = polyline.points[contour_start_point_i]; - // Append two vertices at the beginning of the new contour, which - // appends two triangles of zero area. - vtx_builder.AppendVertex(vtx); - vtx_builder.AppendVertex(vtx); - } - - // Generate start cap. - if (!polyline.contours[contour_i].is_closed) { - auto cap_offset = - Vector2(-contour.start_direction.y, contour.start_direction.x) * - stroke_width * 0.5; // Counterclockwise normal - cap_proc(vtx_builder, polyline.points[contour_start_point_i], cap_offset, - scale, true); - } - - // Generate contour geometry. - for (size_t point_i = contour_start_point_i + 1; - point_i < contour_end_point_i; point_i++) { - // Generate line rect. - vtx.position = polyline.points[point_i - 1] + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.points[point_i - 1] - offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.points[point_i] + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.points[point_i] - offset; - vtx_builder.AppendVertex(vtx); - - if (point_i < contour_end_point_i - 1) { - compute_offset(point_i + 1); - - // Generate join from the current line to the next line. - join_proc(vtx_builder, polyline.points[point_i], previous_offset, - offset, scaled_miter_limit, scale); - } - } - - // Generate end cap or join. - if (!polyline.contours[contour_i].is_closed) { - auto cap_offset = - Vector2(-contour.end_direction.y, contour.end_direction.x) * - stroke_width * 0.5; // Clockwise normal - cap_proc(vtx_builder, polyline.points[contour_end_point_i - 1], - cap_offset, scale, false); - } else { - join_proc(vtx_builder, polyline.points[contour_start_point_i], offset, - contour_first_offset, scaled_miter_limit, scale); - } - } - - return vtx_builder; -} - -GeometryResult StrokePathGeometry::GetPositionBuffer( - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - if (stroke_width_ < 0.0) { - return {}; - } - auto determinant = entity.GetTransformation().GetDeterminant(); - if (determinant == 0) { - return {}; - } - - Scalar min_size = 1.0f / sqrt(std::abs(determinant)); - Scalar stroke_width = std::max(stroke_width_, min_size); - - auto& host_buffer = pass.GetTransientsBuffer(); - auto vertex_builder = CreateSolidStrokeVertices( - path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, - GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), - entity.GetTransformation().GetMaxBasisLength()); - - return GeometryResult{ - .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = true, - }; -} - -GeometryResult StrokePathGeometry::GetPositionUVBuffer( - Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - if (stroke_width_ < 0.0) { - return {}; - } - auto determinant = entity.GetTransformation().GetDeterminant(); - if (determinant == 0) { - return {}; - } - - Scalar min_size = 1.0f / sqrt(std::abs(determinant)); - Scalar stroke_width = std::max(stroke_width_, min_size); - - auto& host_buffer = pass.GetTransientsBuffer(); - auto stroke_builder = CreateSolidStrokeVertices( - path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, - GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), - entity.GetTransformation().GetMaxBasisLength()); - auto vertex_builder = ComputeUVGeometryCPU( - stroke_builder, {0, 0}, texture_coverage.size, effect_transform); - - return GeometryResult{ - .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = true, - }; -} - -GeometryVertexType StrokePathGeometry::GetVertexType() const { - return GeometryVertexType::kPosition; -} - -std::optional StrokePathGeometry::GetCoverage( - const Matrix& transform) const { - auto path_bounds = path_.GetBoundingBox(); - if (!path_bounds.has_value()) { - return std::nullopt; - } - auto path_coverage = path_bounds->TransformBounds(transform); - - Scalar max_radius = 0.5; - if (stroke_cap_ == Cap::kSquare) { - max_radius = max_radius * kSqrt2; - } - if (stroke_join_ == Join::kMiter) { - max_radius = std::max(max_radius, miter_limit_ * 0.5f); - } - Scalar determinant = transform.GetDeterminant(); - if (determinant == 0) { - return std::nullopt; - } - Scalar min_size = 1.0f / sqrt(std::abs(determinant)); - Vector2 max_radius_xy = - transform - .TransformDirection(Vector2(max_radius, max_radius) * - std::max(stroke_width_, min_size)) - .Abs(); - return Rect(path_coverage.origin - max_radius_xy, - Size(path_coverage.size.width + max_radius_xy.x * 2, - path_coverage.size.height + max_radius_xy.y * 2)); -} - -/////// Cover Geometry /////// - -CoverGeometry::CoverGeometry() = default; - -CoverGeometry::~CoverGeometry() = default; - -GeometryResult CoverGeometry::GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - auto rect = Rect(Size(pass.GetRenderTargetSize())); - constexpr uint16_t kRectIndicies[4] = {0, 1, 2, 3}; - auto& host_buffer = pass.GetTransientsBuffer(); - return GeometryResult{ - .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = - { - .vertex_buffer = host_buffer.Emplace( - rect.GetPoints().data(), 8 * sizeof(float), alignof(float)), - .index_buffer = host_buffer.Emplace( - kRectIndicies, 4 * sizeof(uint16_t), alignof(uint16_t)), - .vertex_count = 4, - .index_type = IndexType::k16bit, - }, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()), - .prevent_overdraw = false, - }; -} - -// |Geometry| -GeometryResult CoverGeometry::GetPositionUVBuffer( - Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - auto rect = Rect(Size(pass.GetRenderTargetSize())); - return ComputeUVGeometryForRect(rect, texture_coverage, effect_transform, - renderer, entity, pass); -} - -GeometryVertexType CoverGeometry::GetVertexType() const { - return GeometryVertexType::kPosition; -} - -std::optional CoverGeometry::GetCoverage(const Matrix& transform) const { - return Rect::MakeMaximum(); -} - -/////// Rect Geometry /////// - -RectGeometry::RectGeometry(Rect rect) : rect_(rect) {} - -RectGeometry::~RectGeometry() = default; - -GeometryResult RectGeometry::GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - auto& host_buffer = pass.GetTransientsBuffer(); - return GeometryResult{ - .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = - { - .vertex_buffer = host_buffer.Emplace( - rect_.GetPoints().data(), 8 * sizeof(float), alignof(float)), - .vertex_count = 4, - .index_type = IndexType::kNone, - }, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -// |Geometry| -GeometryResult RectGeometry::GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - return ComputeUVGeometryForRect(rect_, texture_coverage, effect_transform, - renderer, entity, pass); -} - -GeometryVertexType RectGeometry::GetVertexType() const { - return GeometryVertexType::kPosition; -} - -std::optional RectGeometry::GetCoverage(const Matrix& transform) const { - return rect_.TransformBounds(transform); -} - -/////// PointFieldGeometry Geometry /////// - -PointFieldGeometry::PointFieldGeometry(std::vector points, - Scalar radius, - bool round) - : points_(std::move(points)), radius_(radius), round_(round) {} - -PointFieldGeometry::~PointFieldGeometry() = default; - -GeometryResult PointFieldGeometry::GetPositionBuffer( - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - if (renderer.GetDeviceCapabilities().SupportsCompute()) { - return GetPositionBufferGPU(renderer, entity, pass); - } - auto vtx_builder = GetPositionBufferCPU(renderer, entity, pass); - if (!vtx_builder.has_value()) { - return {}; - } - - auto& host_buffer = pass.GetTransientsBuffer(); - return { - .type = PrimitiveType::kTriangle, - .vertex_buffer = vtx_builder->CreateVertexBuffer(host_buffer), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -GeometryResult PointFieldGeometry::GetPositionUVBuffer( - Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - if (renderer.GetDeviceCapabilities().SupportsCompute()) { - return GetPositionBufferGPU(renderer, entity, pass, texture_coverage, - effect_transform); - } - - auto vtx_builder = GetPositionBufferCPU(renderer, entity, pass); - if (!vtx_builder.has_value()) { - return {}; - } - auto uv_vtx_builder = ComputeUVGeometryCPU( - vtx_builder.value(), {0, 0}, texture_coverage.size, effect_transform); - - auto& host_buffer = pass.GetTransientsBuffer(); - return { - .type = PrimitiveType::kTriangle, - .vertex_buffer = uv_vtx_builder.CreateVertexBuffer(host_buffer), - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -std::optional> -PointFieldGeometry::GetPositionBufferCPU(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) { - if (radius_ < 0.0) { - return std::nullopt; - } - auto determinant = entity.GetTransformation().GetDeterminant(); - if (determinant == 0) { - return std::nullopt; - } - - Scalar min_size = 1.0f / sqrt(std::abs(determinant)); - Scalar radius = std::max(radius_, min_size); - - auto vertices_per_geom = ComputeCircleDivisions( - entity.GetTransformation().GetMaxBasisLength() * radius, round_); - auto points_per_circle = 3 + (vertices_per_geom - 3) * 3; - auto total = points_per_circle * points_.size(); - auto radian_start = round_ ? 0.0f : 0.785398f; - auto radian_step = k2Pi / vertices_per_geom; - - VertexBufferBuilder vtx_builder; - vtx_builder.Reserve(total); - - /// Precompute all relative points and angles for a fixed geometry size. - auto elapsed_angle = radian_start; - std::vector angle_table(vertices_per_geom); - for (auto i = 0u; i < vertices_per_geom; i++) { - angle_table[i] = Point(cos(elapsed_angle), sin(elapsed_angle)) * radius; - elapsed_angle += radian_step; - } - - for (auto i = 0u; i < points_.size(); i++) { - auto center = points_[i]; - - auto origin = center + angle_table[0]; - vtx_builder.AppendVertex({origin}); - - auto pt1 = center + angle_table[1]; - vtx_builder.AppendVertex({pt1}); - - auto pt2 = center + angle_table[2]; - vtx_builder.AppendVertex({pt2}); - - for (auto j = 0u; j < vertices_per_geom - 3; j++) { - vtx_builder.AppendVertex({origin}); - vtx_builder.AppendVertex({pt2}); - - pt2 = center + angle_table[j + 3]; - vtx_builder.AppendVertex({pt2}); - } - } - return vtx_builder; -} - -GeometryResult PointFieldGeometry::GetPositionBufferGPU( - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass, - std::optional texture_coverage, - std::optional effect_transform) { - FML_DCHECK(renderer.GetDeviceCapabilities().SupportsCompute()); - if (radius_ < 0.0) { - return {}; - } - auto determinant = entity.GetTransformation().GetDeterminant(); - if (determinant == 0) { - return {}; - } - - Scalar min_size = 1.0f / sqrt(std::abs(determinant)); - Scalar radius = std::max(radius_, min_size); - - auto vertices_per_geom = ComputeCircleDivisions( - entity.GetTransformation().GetMaxBasisLength() * radius, round_); - - auto points_per_circle = 3 + (vertices_per_geom - 3) * 3; - auto total = points_per_circle * points_.size(); - - auto cmd_buffer = renderer.GetContext()->CreateCommandBuffer(); - auto compute_pass = cmd_buffer->CreateComputePass(); - auto& host_buffer = compute_pass->GetTransientsBuffer(); - - auto points_data = - host_buffer.Emplace(points_.data(), points_.size() * sizeof(Point), - DefaultUniformAlignment()); - - DeviceBufferDescriptor buffer_desc; - buffer_desc.size = total * sizeof(Point); - buffer_desc.storage_mode = StorageMode::kDevicePrivate; - - auto geometry_buffer = renderer.GetContext() - ->GetResourceAllocator() - ->CreateBuffer(buffer_desc) - ->AsBufferView(); - - BufferView output; - { - using PS = PointsComputeShader; - ComputeCommand cmd; - cmd.label = "Points Geometry"; - cmd.pipeline = renderer.GetPointComputePipeline(); - - PS::FrameInfo frame_info; - frame_info.count = points_.size(); - frame_info.radius = radius; - frame_info.radian_start = round_ ? 0.0f : kPiOver4; - frame_info.radian_step = k2Pi / vertices_per_geom; - frame_info.points_per_circle = points_per_circle; - frame_info.divisions_per_circle = vertices_per_geom; - - PS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); - PS::BindGeometryData(cmd, geometry_buffer); - PS::BindPointData(cmd, points_data); - - if (!compute_pass->AddCommand(std::move(cmd))) { - return {}; - } - output = geometry_buffer; - } - - if (texture_coverage.has_value() && effect_transform.has_value()) { - DeviceBufferDescriptor buffer_desc; - buffer_desc.size = total * sizeof(Vector4); - buffer_desc.storage_mode = StorageMode::kDevicePrivate; - - auto geometry_uv_buffer = renderer.GetContext() - ->GetResourceAllocator() - ->CreateBuffer(buffer_desc) - ->AsBufferView(); - - using UV = UvComputeShader; - - ComputeCommand cmd; - cmd.label = "UV Geometry"; - cmd.pipeline = renderer.GetUvComputePipeline(); - - UV::FrameInfo frame_info; - frame_info.count = total; - frame_info.effect_transform = effect_transform.value(); - frame_info.texture_origin = {0, 0}; - frame_info.texture_size = Vector2(texture_coverage.value().size); - - UV::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); - UV::BindGeometryData(cmd, geometry_buffer); - UV::BindGeometryUVData(cmd, geometry_uv_buffer); - - if (!compute_pass->AddCommand(std::move(cmd))) { - return {}; - } - output = geometry_uv_buffer; - } - - compute_pass->SetGridSize(ISize(total, 1)); - compute_pass->SetThreadGroupSize(ISize(total, 1)); - - if (!compute_pass->EncodeCommands() || !cmd_buffer->SubmitCommands()) { - return {}; - } - - return { - .type = PrimitiveType::kTriangle, - .vertex_buffer = {.vertex_buffer = output, - .vertex_count = total, - .index_type = IndexType::kNone}, - .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * - entity.GetTransformation(), - .prevent_overdraw = false, - }; -} - -/// @brief Compute the number of vertices to divide each circle into. -/// -/// @return the number of vertices. -size_t PointFieldGeometry::ComputeCircleDivisions(Scalar scaled_radius, - bool round) { - if (!round) { - return 4; - } - - // Note: these values are approximated based on the values returned from - // the decomposition of 4 cubics performed by Path::CreatePolyline. - if (scaled_radius < 1.0) { - return 4; - } - if (scaled_radius < 2.0) { - return 8; - } - if (scaled_radius < 12.0) { - return 24; - } - if (scaled_radius < 22.0) { - return 34; - } - return std::min(scaled_radius, 140.0f); -} - -// |Geometry| -GeometryVertexType PointFieldGeometry::GetVertexType() const { - return GeometryVertexType::kPosition; -} - -// |Geometry| -std::optional PointFieldGeometry::GetCoverage( - const Matrix& transform) const { - if (points_.size() > 0) { - // Doesn't use MakePointBounds as this isn't resilient to points that - // all lie along the same axis. - auto first = points_.begin(); - auto last = points_.end(); - auto left = first->x; - auto top = first->y; - auto right = first->x; - auto bottom = first->y; - for (auto it = first + 1; it < last; ++it) { - left = std::min(left, it->x); - top = std::min(top, it->y); - right = std::max(right, it->x); - bottom = std::max(bottom, it->y); - } - return Rect::MakeLTRB(left - radius_, top - radius_, right + radius_, - bottom + radius_); - } - return std::nullopt; -} - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry.h b/engine/src/flutter/impeller/entity/geometry.h deleted file mode 100644 index a1a48899b9..0000000000 --- a/engine/src/flutter/impeller/entity/geometry.h +++ /dev/null @@ -1,313 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#pragma once - -#include "impeller/core/allocator.h" -#include "impeller/core/host_buffer.h" -#include "impeller/core/vertex_buffer.h" -#include "impeller/entity/contents/contents.h" -#include "impeller/entity/entity.h" -#include "impeller/entity/solid_fill.vert.h" -#include "impeller/geometry/color.h" -#include "impeller/geometry/path.h" -#include "impeller/geometry/point.h" -#include "impeller/geometry/scalar.h" -#include "impeller/renderer/vertex_buffer_builder.h" - -namespace impeller { - -class Tessellator; - -struct GeometryResult { - PrimitiveType type; - VertexBuffer vertex_buffer; - Matrix transform; - bool prevent_overdraw; -}; - -enum GeometryVertexType { - kPosition, - kColor, - kUV, -}; - -/// @brief Given a polyline created from a convex filled path, perform a -/// tessellation. -std::pair, std::vector> TessellateConvex( - Path::Polyline polyline); - -class Geometry { - public: - Geometry(); - - virtual ~Geometry(); - - static std::unique_ptr MakeFillPath(const Path& path); - - static std::unique_ptr MakeStrokePath( - const Path& path, - Scalar stroke_width = 0.0, - Scalar miter_limit = 4.0, - Cap stroke_cap = Cap::kButt, - Join stroke_join = Join::kMiter); - - static std::unique_ptr MakeCover(); - - static std::unique_ptr MakeRect(Rect rect); - - static std::unique_ptr MakePointField(std::vector points, - Scalar radius, - bool round); - - virtual GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) = 0; - - virtual GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass); - - virtual GeometryVertexType GetVertexType() const = 0; - - virtual std::optional GetCoverage(const Matrix& transform) const = 0; -}; - -/// @brief A geometry that is created from a vertices object. -class VerticesGeometry : public Geometry { - public: - virtual GeometryResult GetPositionColorBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) = 0; - - virtual bool HasVertexColors() const = 0; - - virtual bool HasTextureCoordinates() const = 0; - - virtual std::optional GetTextureCoordinateCoverge() const = 0; -}; - -/// @brief A geometry that is created from a filled path object. -class FillPathGeometry : public Geometry { - public: - explicit FillPathGeometry(const Path& path); - - ~FillPathGeometry(); - - private: - // |Geometry| - GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryVertexType GetVertexType() const override; - - // |Geometry| - std::optional GetCoverage(const Matrix& transform) const override; - - // |Geometry| - GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - Path path_; - - FML_DISALLOW_COPY_AND_ASSIGN(FillPathGeometry); -}; - -/// @brief A geometry that is created from a stroked path object. -class StrokePathGeometry : public Geometry { - public: - StrokePathGeometry(const Path& path, - Scalar stroke_width, - Scalar miter_limit, - Cap stroke_cap, - Join stroke_join); - - ~StrokePathGeometry(); - - Scalar GetStrokeWidth() const; - - Scalar GetMiterLimit() const; - - Cap GetStrokeCap() const; - - Join GetStrokeJoin() const; - - private: - using VS = SolidFillVertexShader; - - using CapProc = - std::function& vtx_builder, - const Point& position, - const Point& offset, - Scalar scale, - bool reverse)>; - using JoinProc = - std::function& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset, - Scalar miter_limit, - Scalar scale)>; - - // |Geometry| - GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryVertexType GetVertexType() const override; - - // |Geometry| - std::optional GetCoverage(const Matrix& transform) const override; - - bool SkipRendering() const; - - static Scalar CreateBevelAndGetDirection( - VertexBufferBuilder& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset); - - static VertexBufferBuilder - CreateSolidStrokeVertices(const Path& path, - Scalar stroke_width, - Scalar scaled_miter_limit, - const JoinProc& join_proc, - const CapProc& cap_proc, - Scalar scale); - - static StrokePathGeometry::JoinProc GetJoinProc(Join stroke_join); - - static StrokePathGeometry::CapProc GetCapProc(Cap stroke_cap); - - Path path_; - Scalar stroke_width_; - Scalar miter_limit_; - Cap stroke_cap_; - Join stroke_join_; - - FML_DISALLOW_COPY_AND_ASSIGN(StrokePathGeometry); -}; - -/// @brief A geometry that implements "drawPaint" like behavior by covering -/// the entire render pass area. -class CoverGeometry : public Geometry { - public: - CoverGeometry(); - - ~CoverGeometry(); - - private: - // |Geometry| - GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryVertexType GetVertexType() const override; - - // |Geometry| - std::optional GetCoverage(const Matrix& transform) const override; - - // |Geometry| - GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - FML_DISALLOW_COPY_AND_ASSIGN(CoverGeometry); -}; - -class RectGeometry : public Geometry { - public: - explicit RectGeometry(Rect rect); - - ~RectGeometry(); - - private: - // |Geometry| - GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryVertexType GetVertexType() const override; - - // |Geometry| - std::optional GetCoverage(const Matrix& transform) const override; - - // |Geometry| - GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - Rect rect_; - - FML_DISALLOW_COPY_AND_ASSIGN(RectGeometry); -}; - -class PointFieldGeometry : public Geometry { - public: - PointFieldGeometry(std::vector points, Scalar radius, bool round); - - ~PointFieldGeometry(); - - static size_t ComputeCircleDivisions(Scalar scaled_radius, bool round); - - private: - // |Geometry| - GeometryResult GetPositionBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryResult GetPositionUVBuffer(Rect texture_coverage, - Matrix effect_transform, - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) override; - - // |Geometry| - GeometryVertexType GetVertexType() const override; - - // |Geometry| - std::optional GetCoverage(const Matrix& transform) const override; - - GeometryResult GetPositionBufferGPU( - const ContentContext& renderer, - const Entity& entity, - RenderPass& pass, - std::optional texture_coverage = std::nullopt, - std::optional effect_transform = std::nullopt); - - std::optional> - GetPositionBufferCPU(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass); - - std::vector points_; - Scalar radius_; - bool round_; - - FML_DISALLOW_COPY_AND_ASSIGN(PointFieldGeometry); -}; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/cover_geometry.cc b/engine/src/flutter/impeller/entity/geometry/cover_geometry.cc new file mode 100644 index 0000000000..f2eb6c6814 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/cover_geometry.cc @@ -0,0 +1,57 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/cover_geometry.h" + +#include "impeller/renderer/render_pass.h" + +namespace impeller { + +CoverGeometry::CoverGeometry() = default; + +CoverGeometry::~CoverGeometry() = default; + +GeometryResult CoverGeometry::GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + auto rect = Rect(Size(pass.GetRenderTargetSize())); + constexpr uint16_t kRectIndicies[4] = {0, 1, 2, 3}; + auto& host_buffer = pass.GetTransientsBuffer(); + return GeometryResult{ + .type = PrimitiveType::kTriangleStrip, + .vertex_buffer = + { + .vertex_buffer = host_buffer.Emplace( + rect.GetPoints().data(), 8 * sizeof(float), alignof(float)), + .index_buffer = host_buffer.Emplace( + kRectIndicies, 4 * sizeof(uint16_t), alignof(uint16_t)), + .vertex_count = 4, + .index_type = IndexType::k16bit, + }, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()), + .prevent_overdraw = false, + }; +} + +// |Geometry| +GeometryResult CoverGeometry::GetPositionUVBuffer( + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + auto rect = Rect(Size(pass.GetRenderTargetSize())); + return ComputeUVGeometryForRect(rect, texture_coverage, effect_transform, + renderer, entity, pass); +} + +GeometryVertexType CoverGeometry::GetVertexType() const { + return GeometryVertexType::kPosition; +} + +std::optional CoverGeometry::GetCoverage(const Matrix& transform) const { + return Rect::MakeMaximum(); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/cover_geometry.h b/engine/src/flutter/impeller/entity/geometry/cover_geometry.h new file mode 100644 index 0000000000..8bf20e1cc2 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/cover_geometry.h @@ -0,0 +1,41 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +/// @brief A geometry that implements "drawPaint" like behavior by covering +/// the entire render pass area. +class CoverGeometry : public Geometry { + public: + CoverGeometry(); + + ~CoverGeometry(); + + private: + // |Geometry| + GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryVertexType GetVertexType() const override; + + // |Geometry| + std::optional GetCoverage(const Matrix& transform) const override; + + // |Geometry| + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + FML_DISALLOW_COPY_AND_ASSIGN(CoverGeometry); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc new file mode 100644 index 0000000000..af0767f596 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc @@ -0,0 +1,150 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/fill_path_geometry.h" + +namespace impeller { + +FillPathGeometry::FillPathGeometry(const Path& path) : path_(path) {} + +FillPathGeometry::~FillPathGeometry() = default; + +GeometryResult FillPathGeometry::GetPositionBuffer( + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + auto& host_buffer = pass.GetTransientsBuffer(); + VertexBuffer vertex_buffer; + + if (path_.GetFillType() == FillType::kNonZero && // + path_.IsConvex()) { + auto [points, indices] = TessellateConvex( + path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength())); + + vertex_buffer.vertex_buffer = host_buffer.Emplace( + points.data(), points.size() * sizeof(Point), alignof(Point)); + vertex_buffer.index_buffer = host_buffer.Emplace( + indices.data(), indices.size() * sizeof(uint16_t), alignof(uint16_t)); + vertex_buffer.vertex_count = indices.size(); + vertex_buffer.index_type = IndexType::k16bit; + + return GeometryResult{ + .type = PrimitiveType::kTriangle, + .vertex_buffer = vertex_buffer, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; + } + + auto tesselation_result = renderer.GetTessellator()->Tessellate( + path_.GetFillType(), + path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength()), + [&vertex_buffer, &host_buffer]( + const float* vertices, size_t vertices_count, const uint16_t* indices, + size_t indices_count) { + vertex_buffer.vertex_buffer = host_buffer.Emplace( + vertices, vertices_count * sizeof(float), alignof(float)); + vertex_buffer.index_buffer = host_buffer.Emplace( + indices, indices_count * sizeof(uint16_t), alignof(uint16_t)); + vertex_buffer.vertex_count = indices_count; + vertex_buffer.index_type = IndexType::k16bit; + return true; + }); + if (tesselation_result != Tessellator::Result::kSuccess) { + return {}; + } + return GeometryResult{ + .type = PrimitiveType::kTriangle, + .vertex_buffer = vertex_buffer, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +// |Geometry| +GeometryResult FillPathGeometry::GetPositionUVBuffer( + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + using VS = TextureFillVertexShader; + + if (path_.GetFillType() == FillType::kNonZero && // + path_.IsConvex()) { + auto [points, indices] = TessellateConvex( + path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength())); + + VertexBufferBuilder vertex_builder; + vertex_builder.Reserve(points.size()); + vertex_builder.ReserveIndices(indices.size()); + for (auto i = 0u; i < points.size(); i++) { + VS::PerVertexData data; + data.position = points[i]; + auto coverage_coords = + (points[i] - texture_coverage.origin) / texture_coverage.size; + data.texture_coords = effect_transform * coverage_coords; + vertex_builder.AppendVertex(data); + } + for (auto i = 0u; i < indices.size(); i++) { + vertex_builder.AppendIndex(indices[i]); + } + + return GeometryResult{ + .type = PrimitiveType::kTriangle, + .vertex_buffer = + vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer()), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; + } + + VertexBufferBuilder vertex_builder; + auto tesselation_result = renderer.GetTessellator()->Tessellate( + path_.GetFillType(), + path_.CreatePolyline(entity.GetTransformation().GetMaxBasisLength()), + [&vertex_builder, &texture_coverage, &effect_transform]( + const float* vertices, size_t vertices_count, const uint16_t* indices, + size_t indices_count) { + for (auto i = 0u; i < vertices_count; i += 2) { + VS::PerVertexData data; + Point vtx = {vertices[i], vertices[i + 1]}; + data.position = vtx; + auto coverage_coords = + (vtx - texture_coverage.origin) / texture_coverage.size; + data.texture_coords = effect_transform * coverage_coords; + vertex_builder.AppendVertex(data); + } + FML_DCHECK(vertex_builder.GetVertexCount() == vertices_count / 2); + for (auto i = 0u; i < indices_count; i++) { + vertex_builder.AppendIndex(indices[i]); + } + return true; + }); + if (tesselation_result != Tessellator::Result::kSuccess) { + return {}; + } + return GeometryResult{ + .type = PrimitiveType::kTriangle, + .vertex_buffer = + vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer()), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +GeometryVertexType FillPathGeometry::GetVertexType() const { + return GeometryVertexType::kPosition; +} + +std::optional FillPathGeometry::GetCoverage( + const Matrix& transform) const { + return path_.GetTransformedBoundingBox(transform); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h new file mode 100644 index 0000000000..087505e87e --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h @@ -0,0 +1,42 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +/// @brief A geometry that is created from a filled path object. +class FillPathGeometry : public Geometry { + public: + explicit FillPathGeometry(const Path& path); + + ~FillPathGeometry(); + + private: + // |Geometry| + GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryVertexType GetVertexType() const override; + + // |Geometry| + std::optional GetCoverage(const Matrix& transform) const override; + + // |Geometry| + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + Path path_; + + FML_DISALLOW_COPY_AND_ASSIGN(FillPathGeometry); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/geometry.cc b/engine/src/flutter/impeller/entity/geometry/geometry.cc new file mode 100644 index 0000000000..2842b7974f --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/geometry.cc @@ -0,0 +1,141 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/geometry.h" + +#include "impeller/entity/geometry/cover_geometry.h" +#include "impeller/entity/geometry/fill_path_geometry.h" +#include "impeller/entity/geometry/point_field_geometry.h" +#include "impeller/entity/geometry/rect_geometry.h" +#include "impeller/entity/geometry/stroke_path_geometry.h" + +namespace impeller { + +/// Given a convex polyline, create a triangle fan structure. +std::pair, std::vector> TessellateConvex( + Path::Polyline polyline) { + std::vector output; + std::vector indices; + + for (auto j = 0u; j < polyline.contours.size(); j++) { + auto [start, end] = polyline.GetContourPointBounds(j); + auto center = polyline.points[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. + if (polyline.points[end - 1] == polyline.points[start]) { + end--; + } + output.emplace_back(center); + output.emplace_back(polyline.points[start + 1]); + + for (auto i = start + 2; i < end; i++) { + const auto& point_b = polyline.points[i]; + output.emplace_back(point_b); + + indices.emplace_back(0); + indices.emplace_back(i - 1); + indices.emplace_back(i); + } + } + return std::make_pair(output, indices); +} + +VertexBufferBuilder +ComputeUVGeometryCPU( + VertexBufferBuilder& input, + Point texture_origin, + Size texture_coverage, + Matrix effect_transform) { + VertexBufferBuilder vertex_builder; + vertex_builder.Reserve(input.GetVertexCount()); + input.IterateVertices( + [&vertex_builder, &texture_coverage, &effect_transform, + &texture_origin](SolidFillVertexShader::PerVertexData old_vtx) { + TextureFillVertexShader::PerVertexData data; + data.position = old_vtx.position; + auto coverage_coords = + (old_vtx.position - texture_origin) / texture_coverage; + data.texture_coords = effect_transform * coverage_coords; + vertex_builder.AppendVertex(data); + }); + return vertex_builder; +} + +GeometryResult ComputeUVGeometryForRect(Rect source_rect, + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + auto& host_buffer = pass.GetTransientsBuffer(); + + std::vector data(8); + auto points = source_rect.GetPoints(); + for (auto i = 0u, j = 0u; i < 8; i += 2, j++) { + data[i] = points[j]; + data[i + 1] = effect_transform * ((points[j] - texture_coverage.origin) / + texture_coverage.size); + } + + return GeometryResult{ + .type = PrimitiveType::kTriangleStrip, + .vertex_buffer = + { + .vertex_buffer = host_buffer.Emplace( + data.data(), 16 * sizeof(float), alignof(float)), + .vertex_count = 4, + .index_type = IndexType::kNone, + }, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +Geometry::Geometry() = default; + +Geometry::~Geometry() = default; + +GeometryResult Geometry::GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + return {}; +} + +std::unique_ptr Geometry::MakeFillPath(const Path& path) { + return std::make_unique(path); +} + +std::unique_ptr Geometry::MakePointField(std::vector points, + Scalar radius, + bool round) { + return std::make_unique(std::move(points), radius, round); +} + +std::unique_ptr Geometry::MakeStrokePath(const Path& path, + Scalar stroke_width, + Scalar miter_limit, + Cap stroke_cap, + Join stroke_join) { + // Skia behaves like this. + if (miter_limit < 0) { + miter_limit = 4.0; + } + return std::make_unique(path, stroke_width, miter_limit, + stroke_cap, stroke_join); +} + +std::unique_ptr Geometry::MakeCover() { + return std::make_unique(); +} + +std::unique_ptr Geometry::MakeRect(Rect rect) { + return std::make_unique(rect); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/geometry.h b/engine/src/flutter/impeller/entity/geometry/geometry.h new file mode 100644 index 0000000000..95bf6799ad --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/geometry.h @@ -0,0 +1,92 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/core/formats.h" +#include "impeller/core/vertex_buffer.h" +#include "impeller/entity/contents/content_context.h" +#include "impeller/entity/entity.h" +#include "impeller/entity/texture_fill.vert.h" +#include "impeller/renderer/render_pass.h" + +namespace impeller { + +class Tessellator; + +struct GeometryResult { + PrimitiveType type; + VertexBuffer vertex_buffer; + Matrix transform; + bool prevent_overdraw; +}; + +enum GeometryVertexType { + kPosition, + kColor, + kUV, +}; + +/// @brief Compute UV geometry for a VBB that contains only position geometry. +/// +/// texture_origin should be set to 0, 0 for stroke and stroke based geometry, +/// like the point field. +VertexBufferBuilder +ComputeUVGeometryCPU( + VertexBufferBuilder& input, + Point texture_origin, + Size texture_coverage, + Matrix effect_transform); + +GeometryResult ComputeUVGeometryForRect(Rect source_rect, + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass); + +/// @brief Given a polyline created from a convex filled path, perform a +/// tessellation. +std::pair, std::vector> TessellateConvex( + Path::Polyline polyline); + +class Geometry { + public: + Geometry(); + + virtual ~Geometry(); + + static std::unique_ptr MakeFillPath(const Path& path); + + static std::unique_ptr MakeStrokePath( + const Path& path, + Scalar stroke_width = 0.0, + Scalar miter_limit = 4.0, + Cap stroke_cap = Cap::kButt, + Join stroke_join = Join::kMiter); + + static std::unique_ptr MakeCover(); + + static std::unique_ptr MakeRect(Rect rect); + + static std::unique_ptr MakePointField(std::vector points, + Scalar radius, + bool round); + + virtual GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) = 0; + + virtual GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass); + + virtual GeometryVertexType GetVertexType() const = 0; + + virtual std::optional GetCoverage(const Matrix& transform) const = 0; +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc new file mode 100644 index 0000000000..83c763fe0e --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc @@ -0,0 +1,296 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/point_field_geometry.h" + +#include "impeller/renderer/command_buffer.h" +#include "impeller/renderer/compute_command.h" + +namespace impeller { + +PointFieldGeometry::PointFieldGeometry(std::vector points, + Scalar radius, + bool round) + : points_(std::move(points)), radius_(radius), round_(round) {} + +PointFieldGeometry::~PointFieldGeometry() = default; + +GeometryResult PointFieldGeometry::GetPositionBuffer( + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + if (renderer.GetDeviceCapabilities().SupportsCompute()) { + return GetPositionBufferGPU(renderer, entity, pass); + } + auto vtx_builder = GetPositionBufferCPU(renderer, entity, pass); + if (!vtx_builder.has_value()) { + return {}; + } + + auto& host_buffer = pass.GetTransientsBuffer(); + return { + .type = PrimitiveType::kTriangle, + .vertex_buffer = vtx_builder->CreateVertexBuffer(host_buffer), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +GeometryResult PointFieldGeometry::GetPositionUVBuffer( + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + if (renderer.GetDeviceCapabilities().SupportsCompute()) { + return GetPositionBufferGPU(renderer, entity, pass, texture_coverage, + effect_transform); + } + + auto vtx_builder = GetPositionBufferCPU(renderer, entity, pass); + if (!vtx_builder.has_value()) { + return {}; + } + auto uv_vtx_builder = ComputeUVGeometryCPU( + vtx_builder.value(), {0, 0}, texture_coverage.size, effect_transform); + + auto& host_buffer = pass.GetTransientsBuffer(); + return { + .type = PrimitiveType::kTriangle, + .vertex_buffer = uv_vtx_builder.CreateVertexBuffer(host_buffer), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +std::optional> +PointFieldGeometry::GetPositionBufferCPU(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + if (radius_ < 0.0) { + return std::nullopt; + } + auto determinant = entity.GetTransformation().GetDeterminant(); + if (determinant == 0) { + return std::nullopt; + } + + Scalar min_size = 1.0f / sqrt(std::abs(determinant)); + Scalar radius = std::max(radius_, min_size); + + auto vertices_per_geom = ComputeCircleDivisions( + entity.GetTransformation().GetMaxBasisLength() * radius, round_); + auto points_per_circle = 3 + (vertices_per_geom - 3) * 3; + auto total = points_per_circle * points_.size(); + auto radian_start = round_ ? 0.0f : 0.785398f; + auto radian_step = k2Pi / vertices_per_geom; + + VertexBufferBuilder vtx_builder; + vtx_builder.Reserve(total); + + /// Precompute all relative points and angles for a fixed geometry size. + auto elapsed_angle = radian_start; + std::vector angle_table(vertices_per_geom); + for (auto i = 0u; i < vertices_per_geom; i++) { + angle_table[i] = Point(cos(elapsed_angle), sin(elapsed_angle)) * radius; + elapsed_angle += radian_step; + } + + for (auto i = 0u; i < points_.size(); i++) { + auto center = points_[i]; + + auto origin = center + angle_table[0]; + vtx_builder.AppendVertex({origin}); + + auto pt1 = center + angle_table[1]; + vtx_builder.AppendVertex({pt1}); + + auto pt2 = center + angle_table[2]; + vtx_builder.AppendVertex({pt2}); + + for (auto j = 0u; j < vertices_per_geom - 3; j++) { + vtx_builder.AppendVertex({origin}); + vtx_builder.AppendVertex({pt2}); + + pt2 = center + angle_table[j + 3]; + vtx_builder.AppendVertex({pt2}); + } + } + return vtx_builder; +} + +GeometryResult PointFieldGeometry::GetPositionBufferGPU( + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass, + std::optional texture_coverage, + std::optional effect_transform) { + FML_DCHECK(renderer.GetDeviceCapabilities().SupportsCompute()); + if (radius_ < 0.0) { + return {}; + } + auto determinant = entity.GetTransformation().GetDeterminant(); + if (determinant == 0) { + return {}; + } + + Scalar min_size = 1.0f / sqrt(std::abs(determinant)); + Scalar radius = std::max(radius_, min_size); + + auto vertices_per_geom = ComputeCircleDivisions( + entity.GetTransformation().GetMaxBasisLength() * radius, round_); + + auto points_per_circle = 3 + (vertices_per_geom - 3) * 3; + auto total = points_per_circle * points_.size(); + + auto cmd_buffer = renderer.GetContext()->CreateCommandBuffer(); + auto compute_pass = cmd_buffer->CreateComputePass(); + auto& host_buffer = compute_pass->GetTransientsBuffer(); + + auto points_data = + host_buffer.Emplace(points_.data(), points_.size() * sizeof(Point), + DefaultUniformAlignment()); + + DeviceBufferDescriptor buffer_desc; + buffer_desc.size = total * sizeof(Point); + buffer_desc.storage_mode = StorageMode::kDevicePrivate; + + auto geometry_buffer = renderer.GetContext() + ->GetResourceAllocator() + ->CreateBuffer(buffer_desc) + ->AsBufferView(); + + BufferView output; + { + using PS = PointsComputeShader; + ComputeCommand cmd; + cmd.label = "Points Geometry"; + cmd.pipeline = renderer.GetPointComputePipeline(); + + PS::FrameInfo frame_info; + frame_info.count = points_.size(); + frame_info.radius = radius; + frame_info.radian_start = round_ ? 0.0f : kPiOver4; + frame_info.radian_step = k2Pi / vertices_per_geom; + frame_info.points_per_circle = points_per_circle; + frame_info.divisions_per_circle = vertices_per_geom; + + PS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + PS::BindGeometryData(cmd, geometry_buffer); + PS::BindPointData(cmd, points_data); + + if (!compute_pass->AddCommand(std::move(cmd))) { + return {}; + } + output = geometry_buffer; + } + + if (texture_coverage.has_value() && effect_transform.has_value()) { + DeviceBufferDescriptor buffer_desc; + buffer_desc.size = total * sizeof(Vector4); + buffer_desc.storage_mode = StorageMode::kDevicePrivate; + + auto geometry_uv_buffer = renderer.GetContext() + ->GetResourceAllocator() + ->CreateBuffer(buffer_desc) + ->AsBufferView(); + + using UV = UvComputeShader; + + ComputeCommand cmd; + cmd.label = "UV Geometry"; + cmd.pipeline = renderer.GetUvComputePipeline(); + + UV::FrameInfo frame_info; + frame_info.count = total; + frame_info.effect_transform = effect_transform.value(); + frame_info.texture_origin = {0, 0}; + frame_info.texture_size = Vector2(texture_coverage.value().size); + + UV::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + UV::BindGeometryData(cmd, geometry_buffer); + UV::BindGeometryUVData(cmd, geometry_uv_buffer); + + if (!compute_pass->AddCommand(std::move(cmd))) { + return {}; + } + output = geometry_uv_buffer; + } + + compute_pass->SetGridSize(ISize(total, 1)); + compute_pass->SetThreadGroupSize(ISize(total, 1)); + + if (!compute_pass->EncodeCommands() || !cmd_buffer->SubmitCommands()) { + return {}; + } + + return { + .type = PrimitiveType::kTriangle, + .vertex_buffer = {.vertex_buffer = output, + .vertex_count = total, + .index_type = IndexType::kNone}, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +/// @brief Compute the number of vertices to divide each circle into. +/// +/// @return the number of vertices. +size_t PointFieldGeometry::ComputeCircleDivisions(Scalar scaled_radius, + bool round) { + if (!round) { + return 4; + } + + // Note: these values are approximated based on the values returned from + // the decomposition of 4 cubics performed by Path::CreatePolyline. + if (scaled_radius < 1.0) { + return 4; + } + if (scaled_radius < 2.0) { + return 8; + } + if (scaled_radius < 12.0) { + return 24; + } + if (scaled_radius < 22.0) { + return 34; + } + return std::min(scaled_radius, 140.0f); +} + +// |Geometry| +GeometryVertexType PointFieldGeometry::GetVertexType() const { + return GeometryVertexType::kPosition; +} + +// |Geometry| +std::optional PointFieldGeometry::GetCoverage( + const Matrix& transform) const { + if (points_.size() > 0) { + // Doesn't use MakePointBounds as this isn't resilient to points that + // all lie along the same axis. + auto first = points_.begin(); + auto last = points_.end(); + auto left = first->x; + auto top = first->y; + auto right = first->x; + auto bottom = first->y; + for (auto it = first + 1; it < last; ++it) { + left = std::min(left, it->x); + top = std::min(top, it->y); + right = std::max(right, it->x); + bottom = std::max(bottom, it->y); + } + return Rect::MakeLTRB(left - radius_, top - radius_, right + radius_, + bottom + radius_); + } + return std::nullopt; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/point_field_geometry.h b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.h new file mode 100644 index 0000000000..deb0ce8dd3 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.h @@ -0,0 +1,57 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +class PointFieldGeometry : public Geometry { + public: + PointFieldGeometry(std::vector points, Scalar radius, bool round); + + ~PointFieldGeometry(); + + static size_t ComputeCircleDivisions(Scalar scaled_radius, bool round); + + private: + // |Geometry| + GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryVertexType GetVertexType() const override; + + // |Geometry| + std::optional GetCoverage(const Matrix& transform) const override; + + GeometryResult GetPositionBufferGPU( + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass, + std::optional texture_coverage = std::nullopt, + std::optional effect_transform = std::nullopt); + + std::optional> + GetPositionBufferCPU(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass); + + std::vector points_; + Scalar radius_; + bool round_; + + FML_DISALLOW_COPY_AND_ASSIGN(PointFieldGeometry); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/rect_geometry.cc b/engine/src/flutter/impeller/entity/geometry/rect_geometry.cc new file mode 100644 index 0000000000..0c34b8b36f --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/rect_geometry.cc @@ -0,0 +1,50 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/rect_geometry.h" + +namespace impeller { + +RectGeometry::RectGeometry(Rect rect) : rect_(rect) {} + +RectGeometry::~RectGeometry() = default; + +GeometryResult RectGeometry::GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + auto& host_buffer = pass.GetTransientsBuffer(); + return GeometryResult{ + .type = PrimitiveType::kTriangleStrip, + .vertex_buffer = + { + .vertex_buffer = host_buffer.Emplace( + rect_.GetPoints().data(), 8 * sizeof(float), alignof(float)), + .vertex_count = 4, + .index_type = IndexType::kNone, + }, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; +} + +// |Geometry| +GeometryResult RectGeometry::GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + return ComputeUVGeometryForRect(rect_, texture_coverage, effect_transform, + renderer, entity, pass); +} + +GeometryVertexType RectGeometry::GetVertexType() const { + return GeometryVertexType::kPosition; +} + +std::optional RectGeometry::GetCoverage(const Matrix& transform) const { + return rect_.TransformBounds(transform); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/rect_geometry.h b/engine/src/flutter/impeller/entity/geometry/rect_geometry.h new file mode 100644 index 0000000000..1833d70d27 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/rect_geometry.h @@ -0,0 +1,41 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +class RectGeometry : public Geometry { + public: + explicit RectGeometry(Rect rect); + + ~RectGeometry(); + + private: + // |Geometry| + GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryVertexType GetVertexType() const override; + + // |Geometry| + std::optional GetCoverage(const Matrix& transform) const override; + + // |Geometry| + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + Rect rect_; + + FML_DISALLOW_COPY_AND_ASSIGN(RectGeometry); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc new file mode 100644 index 0000000000..43648a548e --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc @@ -0,0 +1,444 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/stroke_path_geometry.h" + +#include "impeller/geometry/path_builder.h" + +namespace impeller { + +StrokePathGeometry::StrokePathGeometry(const Path& path, + Scalar stroke_width, + Scalar miter_limit, + Cap stroke_cap, + Join stroke_join) + : path_(path), + stroke_width_(stroke_width), + miter_limit_(miter_limit), + stroke_cap_(stroke_cap), + stroke_join_(stroke_join) {} + +StrokePathGeometry::~StrokePathGeometry() = default; + +Scalar StrokePathGeometry::GetStrokeWidth() const { + return stroke_width_; +} + +Scalar StrokePathGeometry::GetMiterLimit() const { + return miter_limit_; +} + +Cap StrokePathGeometry::GetStrokeCap() const { + return stroke_cap_; +} + +Join StrokePathGeometry::GetStrokeJoin() const { + return stroke_join_; +} + +// static +Scalar StrokePathGeometry::CreateBevelAndGetDirection( + VertexBufferBuilder& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset) { + SolidFillVertexShader::PerVertexData vtx; + vtx.position = position; + vtx_builder.AppendVertex(vtx); + + Scalar dir = start_offset.Cross(end_offset) > 0 ? -1 : 1; + vtx.position = position + start_offset * dir; + vtx_builder.AppendVertex(vtx); + vtx.position = position + end_offset * dir; + vtx_builder.AppendVertex(vtx); + + return dir; +} + +// static +StrokePathGeometry::JoinProc StrokePathGeometry::GetJoinProc(Join stroke_join) { + using VS = SolidFillVertexShader; + StrokePathGeometry::JoinProc join_proc; + switch (stroke_join) { + case Join::kBevel: + join_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& start_offset, + const Point& end_offset, Scalar miter_limit, + Scalar scale) { + CreateBevelAndGetDirection(vtx_builder, position, start_offset, + end_offset); + }; + break; + case Join::kMiter: + join_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& start_offset, + const Point& end_offset, Scalar miter_limit, + Scalar scale) { + Point start_normal = start_offset.Normalize(); + Point end_normal = end_offset.Normalize(); + + // 1 for no joint (straight line), 0 for max joint (180 degrees). + Scalar alignment = (start_normal.Dot(end_normal) + 1) / 2; + if (ScalarNearlyEqual(alignment, 1)) { + return; + } + + Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, + start_offset, end_offset); + + Point miter_point = (start_offset + end_offset) / 2 / alignment; + if (miter_point.GetDistanceSquared({0, 0}) > + miter_limit * miter_limit) { + return; // Convert to bevel when we exceed the miter limit. + } + + // Outer miter point. + VS::PerVertexData vtx; + vtx.position = position + miter_point * dir; + vtx_builder.AppendVertex(vtx); + }; + break; + case Join::kRound: + join_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& start_offset, + const Point& end_offset, Scalar miter_limit, + Scalar scale) { + Point start_normal = start_offset.Normalize(); + Point end_normal = end_offset.Normalize(); + + // 0 for no joint (straight line), 1 for max joint (180 degrees). + Scalar alignment = 1 - (start_normal.Dot(end_normal) + 1) / 2; + if (ScalarNearlyEqual(alignment, 0)) { + return; + } + + Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, + start_offset, end_offset); + + Point middle = + (start_offset + end_offset).Normalize() * start_offset.GetLength(); + Point middle_normal = middle.Normalize(); + + Point middle_handle = middle + Point(-middle.y, middle.x) * + PathBuilder::kArcApproximationMagic * + alignment * dir; + Point start_handle = + start_offset + Point(start_offset.y, -start_offset.x) * + PathBuilder::kArcApproximationMagic * alignment * + dir; + + auto arc_points = CubicPathComponent(start_offset, start_handle, + middle_handle, middle) + .CreatePolyline(scale); + + VS::PerVertexData vtx; + for (const auto& point : arc_points) { + vtx.position = position + point * dir; + vtx_builder.AppendVertex(vtx); + vtx.position = position + (-point * dir).Reflect(middle_normal); + vtx_builder.AppendVertex(vtx); + } + }; + break; + } + return join_proc; +} + +// static +StrokePathGeometry::CapProc StrokePathGeometry::GetCapProc(Cap stroke_cap) { + using VS = SolidFillVertexShader; + StrokePathGeometry::CapProc cap_proc; + switch (stroke_cap) { + case Cap::kButt: + cap_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& offset, Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + VS::PerVertexData vtx; + vtx.position = position + orientation; + vtx_builder.AppendVertex(vtx); + vtx.position = position - orientation; + vtx_builder.AppendVertex(vtx); + }; + break; + case Cap::kRound: + cap_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& offset, Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + + VS::PerVertexData vtx; + + Point forward(offset.y, -offset.x); + Point forward_normal = forward.Normalize(); + + CubicPathComponent arc; + if (reverse) { + arc = CubicPathComponent( + forward, + forward + orientation * PathBuilder::kArcApproximationMagic, + orientation + forward * PathBuilder::kArcApproximationMagic, + orientation); + } else { + arc = CubicPathComponent( + orientation, + orientation + forward * PathBuilder::kArcApproximationMagic, + forward + orientation * PathBuilder::kArcApproximationMagic, + forward); + } + + vtx.position = position + orientation; + vtx_builder.AppendVertex(vtx); + vtx.position = position - orientation; + vtx_builder.AppendVertex(vtx); + for (const auto& point : arc.CreatePolyline(scale)) { + vtx.position = position + point; + vtx_builder.AppendVertex(vtx); + vtx.position = position + (-point).Reflect(forward_normal); + vtx_builder.AppendVertex(vtx); + } + }; + break; + case Cap::kSquare: + cap_proc = [](VertexBufferBuilder& vtx_builder, + const Point& position, const Point& offset, Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + + VS::PerVertexData vtx; + + Point forward(offset.y, -offset.x); + + vtx.position = position + orientation; + vtx_builder.AppendVertex(vtx); + vtx.position = position - orientation; + vtx_builder.AppendVertex(vtx); + vtx.position = position + orientation + forward; + vtx_builder.AppendVertex(vtx); + vtx.position = position - orientation + forward; + vtx_builder.AppendVertex(vtx); + }; + break; + } + return cap_proc; +} + +// static +VertexBufferBuilder +StrokePathGeometry::CreateSolidStrokeVertices( + const Path& path, + Scalar stroke_width, + Scalar scaled_miter_limit, + const StrokePathGeometry::JoinProc& join_proc, + const StrokePathGeometry::CapProc& cap_proc, + Scalar scale) { + VertexBufferBuilder vtx_builder; + auto polyline = path.CreatePolyline(scale); + + VS::PerVertexData vtx; + + // Offset state. + Point offset; + Point previous_offset; // Used for computing joins. + + auto compute_offset = [&polyline, &offset, &previous_offset, + &stroke_width](size_t point_i) { + previous_offset = offset; + Point direction = + (polyline.points[point_i] - polyline.points[point_i - 1]).Normalize(); + offset = Vector2{-direction.y, direction.x} * stroke_width * 0.5; + }; + + for (size_t contour_i = 0; contour_i < polyline.contours.size(); + contour_i++) { + auto contour = polyline.contours[contour_i]; + size_t contour_start_point_i, contour_end_point_i; + std::tie(contour_start_point_i, contour_end_point_i) = + polyline.GetContourPointBounds(contour_i); + + switch (contour_end_point_i - contour_start_point_i) { + case 1: { + Point p = polyline.points[contour_start_point_i]; + cap_proc(vtx_builder, p, {-stroke_width * 0.5f, 0}, scale, false); + cap_proc(vtx_builder, p, {stroke_width * 0.5f, 0}, scale, false); + continue; + } + case 0: + continue; // This contour has no renderable content. + default: + break; + } + + // The first point's offset is always the same as the second point. + compute_offset(contour_start_point_i + 1); + const Point contour_first_offset = offset; + + if (contour_i > 0) { + // This branch only executes when we've just finished drawing a contour + // and are switching to a new one. + // We're drawing a triangle strip, so we need to "pick up the pen" by + // appending two vertices at the end of the previous contour and two + // vertices at the start of the new contour (thus connecting the two + // contours with two zero volume triangles, which will be discarded by + // the rasterizer). + vtx.position = polyline.points[contour_start_point_i - 1]; + // Append two vertices when "picking up" the pen so that the triangle + // drawn when moving to the beginning of the new contour will have zero + // volume. + vtx_builder.AppendVertex(vtx); + vtx_builder.AppendVertex(vtx); + + vtx.position = polyline.points[contour_start_point_i]; + // Append two vertices at the beginning of the new contour, which + // appends two triangles of zero area. + vtx_builder.AppendVertex(vtx); + vtx_builder.AppendVertex(vtx); + } + + // Generate start cap. + if (!polyline.contours[contour_i].is_closed) { + auto cap_offset = + Vector2(-contour.start_direction.y, contour.start_direction.x) * + stroke_width * 0.5; // Counterclockwise normal + cap_proc(vtx_builder, polyline.points[contour_start_point_i], cap_offset, + scale, true); + } + + // Generate contour geometry. + for (size_t point_i = contour_start_point_i + 1; + point_i < contour_end_point_i; point_i++) { + // Generate line rect. + vtx.position = polyline.points[point_i - 1] + offset; + vtx_builder.AppendVertex(vtx); + vtx.position = polyline.points[point_i - 1] - offset; + vtx_builder.AppendVertex(vtx); + vtx.position = polyline.points[point_i] + offset; + vtx_builder.AppendVertex(vtx); + vtx.position = polyline.points[point_i] - offset; + vtx_builder.AppendVertex(vtx); + + if (point_i < contour_end_point_i - 1) { + compute_offset(point_i + 1); + + // Generate join from the current line to the next line. + join_proc(vtx_builder, polyline.points[point_i], previous_offset, + offset, scaled_miter_limit, scale); + } + } + + // Generate end cap or join. + if (!polyline.contours[contour_i].is_closed) { + auto cap_offset = + Vector2(-contour.end_direction.y, contour.end_direction.x) * + stroke_width * 0.5; // Clockwise normal + cap_proc(vtx_builder, polyline.points[contour_end_point_i - 1], + cap_offset, scale, false); + } else { + join_proc(vtx_builder, polyline.points[contour_start_point_i], offset, + contour_first_offset, scaled_miter_limit, scale); + } + } + + return vtx_builder; +} + +GeometryResult StrokePathGeometry::GetPositionBuffer( + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + if (stroke_width_ < 0.0) { + return {}; + } + auto determinant = entity.GetTransformation().GetDeterminant(); + if (determinant == 0) { + return {}; + } + + Scalar min_size = 1.0f / sqrt(std::abs(determinant)); + Scalar stroke_width = std::max(stroke_width_, min_size); + + auto& host_buffer = pass.GetTransientsBuffer(); + auto vertex_builder = CreateSolidStrokeVertices( + path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, + GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), + entity.GetTransformation().GetMaxBasisLength()); + + return GeometryResult{ + .type = PrimitiveType::kTriangleStrip, + .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = true, + }; +} + +GeometryResult StrokePathGeometry::GetPositionUVBuffer( + Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + if (stroke_width_ < 0.0) { + return {}; + } + auto determinant = entity.GetTransformation().GetDeterminant(); + if (determinant == 0) { + return {}; + } + + Scalar min_size = 1.0f / sqrt(std::abs(determinant)); + Scalar stroke_width = std::max(stroke_width_, min_size); + + auto& host_buffer = pass.GetTransientsBuffer(); + auto stroke_builder = CreateSolidStrokeVertices( + path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, + GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), + entity.GetTransformation().GetMaxBasisLength()); + auto vertex_builder = ComputeUVGeometryCPU( + stroke_builder, {0, 0}, texture_coverage.size, effect_transform); + + return GeometryResult{ + .type = PrimitiveType::kTriangleStrip, + .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = true, + }; +} + +GeometryVertexType StrokePathGeometry::GetVertexType() const { + return GeometryVertexType::kPosition; +} + +std::optional StrokePathGeometry::GetCoverage( + const Matrix& transform) const { + auto path_bounds = path_.GetBoundingBox(); + if (!path_bounds.has_value()) { + return std::nullopt; + } + auto path_coverage = path_bounds->TransformBounds(transform); + + Scalar max_radius = 0.5; + if (stroke_cap_ == Cap::kSquare) { + max_radius = max_radius * kSqrt2; + } + if (stroke_join_ == Join::kMiter) { + max_radius = std::max(max_radius, miter_limit_ * 0.5f); + } + Scalar determinant = transform.GetDeterminant(); + if (determinant == 0) { + return std::nullopt; + } + Scalar min_size = 1.0f / sqrt(std::abs(determinant)); + Vector2 max_radius_xy = + transform + .TransformDirection(Vector2(max_radius, max_radius) * + std::max(stroke_width_, min_size)) + .Abs(); + return Rect(path_coverage.origin - max_radius_xy, + Size(path_coverage.size.width + max_radius_xy.x * 2, + path_coverage.size.height + max_radius_xy.y * 2)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h new file mode 100644 index 0000000000..e40675342b --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h @@ -0,0 +1,94 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +/// @brief A geometry that is created from a stroked path object. +class StrokePathGeometry : public Geometry { + public: + StrokePathGeometry(const Path& path, + Scalar stroke_width, + Scalar miter_limit, + Cap stroke_cap, + Join stroke_join); + + ~StrokePathGeometry(); + + Scalar GetStrokeWidth() const; + + Scalar GetMiterLimit() const; + + Cap GetStrokeCap() const; + + Join GetStrokeJoin() const; + + private: + using VS = SolidFillVertexShader; + + using CapProc = + std::function& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse)>; + using JoinProc = + std::function& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale)>; + + // |Geometry| + GeometryResult GetPositionBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) override; + + // |Geometry| + GeometryVertexType GetVertexType() const override; + + // |Geometry| + std::optional GetCoverage(const Matrix& transform) const override; + + bool SkipRendering() const; + + static Scalar CreateBevelAndGetDirection( + VertexBufferBuilder& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset); + + static VertexBufferBuilder + CreateSolidStrokeVertices(const Path& path, + Scalar stroke_width, + Scalar scaled_miter_limit, + const JoinProc& join_proc, + const CapProc& cap_proc, + Scalar scale); + + static StrokePathGeometry::JoinProc GetJoinProc(Join stroke_join); + + static StrokePathGeometry::CapProc GetCapProc(Cap stroke_cap); + + Path path_; + Scalar stroke_width_; + Scalar miter_limit_; + Cap stroke_cap_; + Join stroke_join_; + + FML_DISALLOW_COPY_AND_ASSIGN(StrokePathGeometry); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/vertices_geometry.cc b/engine/src/flutter/impeller/entity/geometry/vertices_geometry.cc new file mode 100644 index 0000000000..2d27592853 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/vertices_geometry.cc @@ -0,0 +1,11 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/geometry/vertices_geometry.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/vertices_geometry.h b/engine/src/flutter/impeller/entity/geometry/vertices_geometry.h new file mode 100644 index 0000000000..09a6b37765 --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/vertices_geometry.h @@ -0,0 +1,25 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/geometry/geometry.h" + +namespace impeller { + +/// @brief A geometry that is created from a vertices object. +class VerticesGeometry : public Geometry { + public: + virtual GeometryResult GetPositionColorBuffer(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) = 0; + + virtual bool HasVertexColors() const = 0; + + virtual bool HasTextureCoordinates() const = 0; + + virtual std::optional GetTextureCoordinateCoverge() const = 0; +}; + +} // namespace impeller