[Impeller] implement TriangleFan mode for drawVertices, fix color issue. (flutter/engine#35586)

This commit is contained in:
Jonah Williams
2022-08-25 22:52:03 -07:00
committed by GitHub
parent 90667ae528
commit 9cf63575d8
6 changed files with 171 additions and 47 deletions

View File

@@ -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

View File

@@ -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 = [

View File

@@ -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<Point> points;
std::vector<uint16_t> indices;
std::vector<Color> 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,

View File

@@ -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<SkPoint> 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<flutter::DlStrokeCap> caps = {

View File

@@ -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<Color> fromColors(const flutter::DlVertices* vertices) {
std::vector<Color> 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<Point> fromPoints(const flutter::DlVertices* vertices) {
std::vector<Point> 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<uint16_t> fromFanIndices(
const flutter::DlVertices* vertices) {
FML_DCHECK(vertices->vertex_count() >= 3);
FML_DCHECK(vertices->mode() == flutter::DlVertexMode::kTriangleFan);
std::vector<uint16_t> 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<uint16_t> fromIndices(const flutter::DlVertices* vertices) {
if (vertices->mode() == flutter::DlVertexMode::kTriangleFan) {
return fromFanIndices(vertices);
}
std::vector<uint16_t> 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<uint16_t> indices = fromIndices(vertices);
std::vector<Point> points = fromPoints(vertices);
std::vector<Color> 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

View File

@@ -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