diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 01f29903d4..8b3ebc77a7 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -521,6 +521,8 @@ FILE: ../../../flutter/impeller/display_list/display_list_playground.h FILE: ../../../flutter/impeller/display_list/display_list_unittests.cc FILE: ../../../flutter/impeller/display_list/nine_patch_converter.cc FILE: ../../../flutter/impeller/display_list/nine_patch_converter.h +FILE: ../../../flutter/impeller/display_list/vertices_converter.cc +FILE: ../../../flutter/impeller/display_list/vertices_converter.h FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image1.png FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image10.png FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image11.png diff --git a/engine/src/flutter/impeller/display_list/BUILD.gn b/engine/src/flutter/impeller/display_list/BUILD.gn index d68ae5b9dd..a6c4fcd60e 100644 --- a/engine/src/flutter/impeller/display_list/BUILD.gn +++ b/engine/src/flutter/impeller/display_list/BUILD.gn @@ -12,6 +12,8 @@ impeller_component("display_list") { "display_list_image_impeller.h", "nine_patch_converter.cc", "nine_patch_converter.h", + "vertices_converter.cc", + "vertices_converter.h", ] public_deps = [ diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index c8169482a5..e28a4c035d 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -16,6 +16,7 @@ #include "flutter/fml/trace_event.h" #include "impeller/display_list/display_list_image_impeller.h" #include "impeller/display_list/nine_patch_converter.h" +#include "impeller/display_list/vertices_converter.h" #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/linear_gradient_contents.h" @@ -796,53 +797,6 @@ static Path ToPath(const SkRRect& rrect) { .TakePath(); } -static Vertices ToVertices(const flutter::DlVertices* vertices) { - std::vector points; - std::vector indices; - std::vector colors; - for (int i = 0; i < vertices->vertex_count(); i++) { - auto point = vertices->vertices()[i]; - points.push_back(Point(point.x(), point.y())); - } - for (int i = 0; i < vertices->index_count(); i++) { - auto index = vertices->indices()[i]; - indices.push_back(index); - } - - auto* dl_colors = vertices->colors(); - if (dl_colors != nullptr) { - auto color_length = vertices->index_count() > 0 ? vertices->index_count() - : vertices->vertex_count(); - for (int i = 0; i < color_length; i++) { - auto dl_color = dl_colors[i]; - colors.push_back({ - dl_color.getRedF(), - dl_color.getGreenF(), - dl_color.getBlueF(), - dl_color.getAlphaF(), - }); - } - } - VertexMode mode; - switch (vertices->mode()) { - case flutter::DlVertexMode::kTriangles: - mode = VertexMode::kTriangle; - break; - case flutter::DlVertexMode::kTriangleStrip: - mode = VertexMode::kTriangleStrip; - break; - case flutter::DlVertexMode::kTriangleFan: - FML_DLOG(ERROR) << "Unimplemented vertex mode TriangleFan in " - << __FUNCTION__; - mode = VertexMode::kTriangle; - break; - } - - auto bounds = vertices->bounds(); - return Vertices(std::move(points), std::move(indices), std::move(colors), - mode, ToRect(bounds)); -} - // |flutter::Dispatcher| void DisplayListDispatcher::clipRRect(const SkRRect& rrect, SkClipOp clip_op, diff --git a/engine/src/flutter/impeller/display_list/display_list_unittests.cc b/engine/src/flutter/impeller/display_list/display_list_unittests.cc index c29a999cc6..f09fa4a40b 100644 --- a/engine/src/flutter/impeller/display_list/display_list_unittests.cc +++ b/engine/src/flutter/impeller/display_list/display_list_unittests.cc @@ -521,6 +521,34 @@ TEST_P(DisplayListTest, CanDrawShadow) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +// Draw a hexagon using triangle fan +TEST_P(DisplayListTest, CanConvertTriangleFanToTriangles) { + constexpr Scalar hexagon_radius = 125; + auto hex_start = Point(200.0, -hexagon_radius + 200.0); + auto center_to_flat = 1.73 / 2 * hexagon_radius; + + // clang-format off + std::vector vertices = { + SkPoint::Make(hex_start.x, hex_start.y), + SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 0.5 * hexagon_radius), + SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 1.5 * hexagon_radius), + SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 1.5 * hexagon_radius), + SkPoint::Make(hex_start.x, hex_start.y + 2 * hexagon_radius), + SkPoint::Make(hex_start.x, hex_start.y + 2 * hexagon_radius), + SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 1.5 * hexagon_radius), + SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 1.5 * hexagon_radius), + SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 0.5 * hexagon_radius) + }; + // clang-format on + auto paint = flutter::DlPaint().setColor(flutter::DlColor::kDarkGrey()); + auto dl_vertices = flutter::DlVertices::Make( + flutter::DlVertexMode::kTriangleFan, vertices.size(), vertices.data(), + nullptr, nullptr); + flutter::DisplayListBuilder builder; + builder.drawVertices(dl_vertices, flutter::DlBlendMode::kSrcOver, paint); + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + TEST_P(DisplayListTest, CanDrawZeroWidthLine) { flutter::DisplayListBuilder builder; std::vector caps = { diff --git a/engine/src/flutter/impeller/display_list/vertices_converter.cc b/engine/src/flutter/impeller/display_list/vertices_converter.cc new file mode 100644 index 0000000000..3bcb5e9f1c --- /dev/null +++ b/engine/src/flutter/impeller/display_list/vertices_converter.cc @@ -0,0 +1,122 @@ +// 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/display_list/vertices_converter.h" +#include "flutter/display_list/display_list_vertices.h" +#include "impeller/entity/entity.h" +#include "impeller/geometry/point.h" + +namespace impeller { + +static Rect ToRect(const SkRect& rect) { + return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); +} + +static std::vector fromColors(const flutter::DlVertices* vertices) { + std::vector colors; + auto* dl_colors = vertices->colors(); + if (dl_colors == nullptr) { + return colors; + } + auto color_count = vertices->vertex_count(); + colors.reserve(color_count); + for (int i = 0; i < color_count; i++) { + auto dl_color = dl_colors[i]; + colors.push_back({ + dl_color.getRedF(), + dl_color.getGreenF(), + dl_color.getBlueF(), + dl_color.getAlphaF(), + }); + } + return colors; +} + +static std::vector fromPoints(const flutter::DlVertices* vertices) { + std::vector points; + auto vertex_count = vertices->vertex_count(); + auto* dl_vertices = vertices->vertices(); + points.reserve(vertex_count); + for (int i = 0; i < vertex_count; i++) { + auto point = dl_vertices[i]; + points.push_back(Point(point.x(), point.y())); + } + return points; +} + +// Fan mode isn't natively supported. Unroll into triangle mode by +// manipulating the index array. +// +// In Triangle fan, the first vertex is shared across all triangles, and then +// each sliding window of two vertices plus that first vertex defines a +// triangle. +static std::vector fromFanIndices( + const flutter::DlVertices* vertices) { + FML_DCHECK(vertices->vertex_count() >= 3); + FML_DCHECK(vertices->mode() == flutter::DlVertexMode::kTriangleFan); + + std::vector indices; + + // Un-fan index buffer if provided. + if (vertices->index_count() > 0) { + auto* dl_indices = vertices->indices(); + auto center_point = dl_indices[0]; + for (int i = 1; i < vertices->index_count() - 1; i++) { + indices.push_back(center_point); + indices.push_back(dl_indices[i]); + indices.push_back(dl_indices[i + 1]); + } + } else { + // If indices were not provided, create an index buffer that unfans + // triangles instead of re-writing points, colors, et cetera. + for (int i = 1; i < vertices->vertex_count() - 1; i++) { + indices.push_back(0); + indices.push_back(i); + indices.push_back(i + 1); + } + } + return indices; +} + +static std::vector fromIndices(const flutter::DlVertices* vertices) { + if (vertices->mode() == flutter::DlVertexMode::kTriangleFan) { + return fromFanIndices(vertices); + } + + std::vector indices; + auto index_count = vertices->index_count(); + auto* dl_indices = vertices->indices(); + indices.reserve(index_count); + for (int i = 0; i < index_count; i++) { + auto index = dl_indices[i]; + indices.push_back(index); + } + return indices; +} + +Vertices ToVertices(const flutter::DlVertices* vertices) { + std::vector indices = fromIndices(vertices); + std::vector points = fromPoints(vertices); + std::vector colors = fromColors(vertices); + + VertexMode mode; + switch (vertices->mode()) { + case flutter::DlVertexMode::kTriangles: + mode = VertexMode::kTriangle; + break; + case flutter::DlVertexMode::kTriangleStrip: + mode = VertexMode::kTriangleStrip; + break; + case flutter::DlVertexMode::kTriangleFan: + // Unrolled into triangle mode by fromIndices. + mode = VertexMode::kTriangle; + break; + } + + auto bounds = vertices->bounds(); + return Vertices(std::move(points), std::move(indices), std::move(colors), + mode, ToRect(bounds)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/display_list/vertices_converter.h b/engine/src/flutter/impeller/display_list/vertices_converter.h new file mode 100644 index 0000000000..0b68fc5142 --- /dev/null +++ b/engine/src/flutter/impeller/display_list/vertices_converter.h @@ -0,0 +1,16 @@ +// 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 "flutter/display_list/display_list.h" +#include "flutter/display_list/display_list_vertices.h" +#include "impeller/geometry/vertices.h" + +namespace impeller { + +/// Convert DlVertices into impeller Vertices. +Vertices ToVertices(const flutter::DlVertices* vertices); + +} // namespace impeller