Add dl vertices object (flutter/engine#32329)

This commit is contained in:
Jim Graham
2022-03-31 00:15:04 -07:00
committed by GitHub
parent d39f658ea9
commit 93be6c2ec4
30 changed files with 1794 additions and 117 deletions

2
DEPS
View File

@@ -116,7 +116,7 @@ deps = {
'src': 'https://github.com/flutter/buildroot.git' + '@' + '9b3955d9ad8621dbf961044dc28d8b12804abb58',
'src/flutter/impeller':
Var('github_git') + '/flutter/impeller' + '@' + '1e4f9e421c7d77d64d2bbe472d23eec3939f7ebb',
Var('github_git') + '/flutter/impeller' + '@' + '2adc41bb87acffab0bca6bcdb9bc8da330068cc9',
# Fuchsia compatibility
#

View File

@@ -94,6 +94,9 @@ FILE: ../../../flutter/display_list/display_list_tile_mode.h
FILE: ../../../flutter/display_list/display_list_unittests.cc
FILE: ../../../flutter/display_list/display_list_utils.cc
FILE: ../../../flutter/display_list/display_list_utils.h
FILE: ../../../flutter/display_list/display_list_vertices.cc
FILE: ../../../flutter/display_list/display_list_vertices.h
FILE: ../../../flutter/display_list/display_list_vertices_unittests.cc
FILE: ../../../flutter/display_list/types.h
FILE: ../../../flutter/flow/compositor_context.cc
FILE: ../../../flutter/flow/compositor_context.h
@@ -458,10 +461,14 @@ FILE: ../../../flutter/impeller/entity/contents/filters/blend_filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/blend_filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/filters/filter_input.cc
FILE: ../../../flutter/impeller/entity/contents/filters/filter_input.h
FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.cc
FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.h
FILE: ../../../flutter/impeller/entity/contents/snapshot.cc
FILE: ../../../flutter/impeller/entity/contents/snapshot.h
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.h
FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.cc

View File

@@ -44,6 +44,8 @@ source_set("display_list") {
"display_list_tile_mode.h",
"display_list_utils.cc",
"display_list_utils.h",
"display_list_vertices.cc",
"display_list_vertices.h",
"types.h",
]
@@ -70,6 +72,7 @@ source_set("unittests") {
"display_list_test_utils.cc",
"display_list_test_utils.h",
"display_list_unittests.cc",
"display_list_vertices_unittests.cc",
]
deps = [

View File

@@ -131,6 +131,7 @@ namespace flutter {
V(DrawLines) \
V(DrawPolygon) \
V(DrawVertices) \
V(DrawSkVertices) \
\
V(DrawImage) \
V(DrawImageWithAttr) \

View File

@@ -21,7 +21,7 @@ static void TestEquals(T& source1, T& source2) {
ASSERT_EQ(source1, source2);
ASSERT_EQ(source2, source1);
ASSERT_TRUE(Equals(&source1, &source2));
ASSERT_TRUE(Equals(source1.shared(), source2.shared()));
ASSERT_TRUE(Equals(&source2, &source1));
}
template <class T>
@@ -33,7 +33,7 @@ static void TestNotEquals(T& source1, T& source2, std::string label) {
ASSERT_NE(source1, source2) << label;
ASSERT_NE(source2, source1) << label;
ASSERT_TRUE(NotEquals(&source1, &source2));
ASSERT_TRUE(NotEquals(source1.shared(), source2.shared()));
ASSERT_TRUE(NotEquals(&source2, &source1));
}
} // namespace testing

View File

@@ -688,11 +688,11 @@ void BM_DrawPath(benchmark::State& state,
// and the final vertex being the center point of the disc.
//
// Each vertex colour will alternate through Red, Green, Blue and Cyan.
sk_sp<SkVertices> GetTestVertices(SkPoint center,
float radius,
size_t vertex_count,
SkVertices::VertexMode mode,
size_t& final_vertex_count) {
std::shared_ptr<DlVertices> GetTestVertices(SkPoint center,
float radius,
size_t vertex_count,
DlVertexMode mode,
size_t& final_vertex_count) {
size_t outer_vertex_count = vertex_count / 2;
std::vector<SkPoint> outer_points =
GetPolygonPoints(outer_vertex_count, center, radius);
@@ -701,7 +701,7 @@ sk_sp<SkVertices> GetTestVertices(SkPoint center,
std::vector<SkColor> colors;
switch (mode) {
case SkVertices::VertexMode::kTriangleFan_VertexMode:
case DlVertexMode::kTriangleFan:
// Calling the points on the outer circle O_0, O_1, O_2, ..., and
// the center point C, this should create a triangle fan with vertices
// C, O_0, O_1, O_2, O_3, ...
@@ -718,7 +718,7 @@ sk_sp<SkVertices> GetTestVertices(SkPoint center,
}
}
break;
case SkVertices::VertexMode::kTriangles_VertexMode:
case DlVertexMode::kTriangles:
// Calling the points on the outer circle O_0, O_1, O_2, ..., and
// the center point C, this should create a series of triangles with
// vertices O_0, O_1, C, O_1, O_2, C, O_2, O_3, C, ...
@@ -731,7 +731,7 @@ sk_sp<SkVertices> GetTestVertices(SkPoint center,
colors.push_back(SK_ColorBLUE);
}
break;
case SkVertices::VertexMode::kTriangleStrip_VertexMode:
case DlVertexMode::kTriangleStrip:
// Calling the points on the outer circle O_0, O_1, O_2, ..., and
// the center point C, this should create a strip with vertices
// O_0, O_1, C, O_2, O_3, C, O_4, O_5, C, ...
@@ -749,17 +749,17 @@ sk_sp<SkVertices> GetTestVertices(SkPoint center,
}
final_vertex_count = vertices.size();
return SkVertices::MakeCopy(mode, vertices.size(), vertices.data(), nullptr,
colors.data());
return DlVertices::Make(mode, vertices.size(), vertices.data(), nullptr,
colors.data());
}
std::string VertexModeToString(SkVertices::VertexMode mode) {
std::string VertexModeToString(DlVertexMode mode) {
switch (mode) {
case SkVertices::VertexMode::kTriangleStrip_VertexMode:
case DlVertexMode::kTriangleStrip:
return "TriangleStrip";
case SkVertices::VertexMode::kTriangleFan_VertexMode:
case DlVertexMode::kTriangleFan:
return "TriangleFan";
case SkVertices::VertexMode::kTriangles_VertexMode:
case DlVertexMode::kTriangles:
return "Triangles";
}
return "Unknown";
@@ -776,7 +776,7 @@ std::string VertexModeToString(SkVertices::VertexMode mode) {
void BM_DrawVertices(benchmark::State& state,
BackendType backend_type,
unsigned attributes,
SkVertices::VertexMode mode) {
DlVertexMode mode) {
auto canvas_provider = CreateCanvasProvider(backend_type);
DisplayListBuilder builder;
builder.setAttributesFromPaint(GetPaintForRun(attributes),
@@ -799,7 +799,7 @@ void BM_DrawVertices(benchmark::State& state,
state.counters["DrawCallCount"] = center_points.size();
for (SkPoint p : center_points) {
sk_sp<SkVertices> vertices =
std::shared_ptr<DlVertices> vertices =
GetTestVertices(p, radius, 50, mode, vertex_count);
total_vertex_count += vertex_count;
builder.drawVertices(vertices, DlBlendMode::kSrc);

View File

@@ -6,6 +6,7 @@
#define FLUTTER_FLOW_DISPLAY_LIST_BENCHMARKS_H_
#include "flutter/display_list/display_list_benchmarks_canvas_provider.h"
#include "flutter/display_list/display_list_vertices.h"
#include "third_party/benchmark/include/benchmark/benchmark.h"
#include "third_party/skia/include/core/SkCanvas.h"
@@ -76,7 +77,7 @@ void BM_DrawPoints(benchmark::State& state,
void BM_DrawVertices(benchmark::State& state,
BackendType backend_type,
unsigned attributes,
SkVertices::VertexMode mode);
DlVertexMode mode);
void BM_DrawImage(benchmark::State& state,
BackendType backend_type,
unsigned attributes,
@@ -238,7 +239,7 @@ void BM_SaveLayer(benchmark::State& state,
TriangleStrip/BACKEND, \
BackendType::k##BACKEND##_Backend, \
ATTRIBUTES, \
SkVertices::VertexMode::kTriangleStrip_VertexMode) \
DlVertexMode::kTriangleStrip) \
->RangeMultiplier(2) \
->Range(16, 2048) \
->UseRealTime() \
@@ -249,7 +250,7 @@ void BM_SaveLayer(benchmark::State& state,
TriangleFan/BACKEND, \
BackendType::k##BACKEND##_Backend, \
ATTRIBUTES, \
SkVertices::VertexMode::kTriangleFan_VertexMode) \
DlVertexMode::kTriangleFan) \
->RangeMultiplier(2) \
->Range(16, 2048) \
->UseRealTime() \
@@ -260,7 +261,7 @@ void BM_SaveLayer(benchmark::State& state,
Triangles/BACKEND, \
BackendType::k##BACKEND##_Backend, \
ATTRIBUTES, \
SkVertices::VertexMode::kTriangles_VertexMode) \
DlVertexMode::kTriangles) \
->RangeMultiplier(2) \
->Range(16, 2048) \
->UseRealTime() \

View File

@@ -587,9 +587,19 @@ void DisplayListBuilder::drawPoints(SkCanvas::PointMode mode,
// See: https://fiddle.skia.org/c/228459001d2de8db117ce25ef5cedb0c
UpdateLayerOpacityCompatibility(false);
}
void DisplayListBuilder::drawVertices(const sk_sp<SkVertices> vertices,
void DisplayListBuilder::drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) {
Push<DrawSkVerticesOp>(0, 1, std::move(vertices), mode);
// DrawVertices applies its colors to the paint so we have no way
// of controlling opacity using the current paint attributes.
// Although, examination of the |mode| might find some predictable
// cases.
UpdateLayerOpacityCompatibility(false);
}
void DisplayListBuilder::drawVertices(const DlVertices* vertices,
DlBlendMode mode) {
Push<DrawVerticesOp>(0, 1, std::move(vertices), mode);
void* pod = Push<DrawVerticesOp>(vertices->size(), 1, mode);
new (pod) DlVertices(vertices);
// DrawVertices applies its colors to the paint so we have no way
// of controlling opacity using the current paint attributes.
// Although, examination of the |mode| might find some predictable

View File

@@ -203,8 +203,13 @@ class DisplayListBuilder final : public virtual Dispatcher,
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint pts[]) override;
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override;
void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) override;
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override;
void drawVertices(const std::shared_ptr<const DlVertices> vertices,
DlBlendMode mode) {
drawVertices(vertices.get(), mode);
}
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -173,9 +173,14 @@ void DisplayListCanvasDispatcher::drawPoints(SkCanvas::PointMode mode,
const SkPoint pts[]) {
canvas_->drawPoints(mode, count, pts, paint());
}
void DisplayListCanvasDispatcher::drawVertices(const sk_sp<SkVertices> vertices,
void DisplayListCanvasDispatcher::drawSkVertices(
const sk_sp<SkVertices> vertices,
SkBlendMode mode) {
canvas_->drawVertices(vertices, mode, paint());
}
void DisplayListCanvasDispatcher::drawVertices(const DlVertices* vertices,
DlBlendMode mode) {
canvas_->drawVertices(vertices, ToSk(mode), paint());
canvas_->drawVertices(vertices->skia_object(), ToSk(mode), paint());
}
void DisplayListCanvasDispatcher::drawImage(const sk_sp<DlImage> image,
const SkPoint point,

View File

@@ -69,8 +69,9 @@ class DisplayListCanvasDispatcher : public virtual Dispatcher,
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint pts[]) override;
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override;
void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) override;
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override;
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -148,7 +148,7 @@ void DisplayListCanvasRecorder::onDrawVerticesObject(const SkVertices* vertices,
SkBlendMode mode,
const SkPaint& paint) {
builder_->setAttributesFromPaint(paint, kDrawVerticesFlags);
builder_->drawVertices(sk_ref_sp(vertices), ToDl(mode));
builder_->drawSkVertices(sk_ref_sp(vertices), mode);
}
void DisplayListCanvasRecorder::onDrawImage2(const SkImage* image,

View File

@@ -2487,20 +2487,20 @@ TEST_F(DisplayListCanvas, DrawVerticesWithColors) {
SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN,
SK_ColorCYAN, SK_ColorYELLOW, SK_ColorMAGENTA,
};
const sk_sp<SkVertices> vertices = SkVertices::MakeCopy(
SkVertices::kTriangles_VertexMode, 6, pts, nullptr, colors);
const std::shared_ptr<DlVertices> vertices =
DlVertices::Make(DlVertexMode::kTriangles, 6, pts, nullptr, colors);
CanvasCompareTester::RenderAll( //
TestParameters(
[=](SkCanvas* canvas, const SkPaint& paint) { //
canvas->drawVertices(vertices.get(), SkBlendMode::kSrcOver, paint);
canvas->drawVertices(vertices->skia_object(), SkBlendMode::kSrcOver,
paint);
},
[=](DisplayListBuilder& builder) { //
builder.drawVertices(vertices, DlBlendMode::kSrcOver);
},
kDrawVerticesFlags)
.set_draw_vertices());
EXPECT_TRUE(vertices->unique());
}
TEST_F(DisplayListCanvas, DrawVerticesWithImage) {
@@ -2531,8 +2531,8 @@ TEST_F(DisplayListCanvas, DrawVerticesWithImage) {
SkPoint::Make(0, 0),
SkPoint::Make(RenderWidth, 0),
};
const sk_sp<SkVertices> vertices = SkVertices::MakeCopy(
SkVertices::kTriangles_VertexMode, 6, pts, tex, nullptr);
const std::shared_ptr<DlVertices> vertices =
DlVertices::Make(DlVertexMode::kTriangles, 6, pts, tex, nullptr);
CanvasCompareTester::RenderAll( //
TestParameters(
@@ -2542,7 +2542,7 @@ TEST_F(DisplayListCanvas, DrawVerticesWithImage) {
v_paint.setShader(
CanvasCompareTester::testImageColorSource.skia_object());
}
canvas->drawVertices(vertices.get(), SkBlendMode::kSrcOver,
canvas->drawVertices(vertices->skia_object(), SkBlendMode::kSrcOver,
v_paint);
},
[=](DisplayListBuilder& builder) { //
@@ -2554,8 +2554,6 @@ TEST_F(DisplayListCanvas, DrawVerticesWithImage) {
},
kDrawVerticesFlags)
.set_draw_vertices());
EXPECT_TRUE(vertices->unique());
}
TEST_F(DisplayListCanvas, DrawImageNearest) {

View File

@@ -476,9 +476,9 @@ void DisplayListGLComplexityCalculator::GLHelper::drawPoints(
AccumulateComplexity(complexity);
}
void DisplayListGLComplexityCalculator::GLHelper::drawVertices(
void DisplayListGLComplexityCalculator::GLHelper::drawSkVertices(
const sk_sp<SkVertices> vertices,
DlBlendMode mode) {
SkBlendMode mode) {
// There is currently no way for us to get the VertexMode from the SkVertices
// object, but for future reference:
//
@@ -504,6 +504,25 @@ void DisplayListGLComplexityCalculator::GLHelper::drawVertices(
AccumulateComplexity(complexity);
}
void DisplayListGLComplexityCalculator::GLHelper::drawVertices(
const DlVertices* vertices,
DlBlendMode mode) {
// There is currently no way for us to get the VertexMode from the SkVertices
// object, but for future reference:
//
// TriangleStrip is roughly 25% more expensive than TriangleFan.
// TriangleFan is roughly 5% more expensive than Triangles.
// For the baseline, it's hard to identify the trend. It might be O(n^1/2)
// For now, treat it as linear as an approximation.
//
// m = 1/1600
// c = 1
unsigned int complexity = (vertices->vertex_count() + 1600) * 250 / 2;
AccumulateComplexity(complexity);
}
void DisplayListGLComplexityCalculator::GLHelper::drawImage(
const sk_sp<DlImage> image,
const SkPoint point,

View File

@@ -54,8 +54,8 @@ class DisplayListGLComplexityCalculator
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint points[]) override;
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override;
void drawSkVertices(const sk_sp<SkVertices>, SkBlendMode mode) override;
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override;
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -428,9 +428,9 @@ void DisplayListMetalComplexityCalculator::MetalHelper::drawPoints(
AccumulateComplexity(complexity);
}
void DisplayListMetalComplexityCalculator::MetalHelper::drawVertices(
void DisplayListMetalComplexityCalculator::MetalHelper::drawSkVertices(
const sk_sp<SkVertices> vertices,
DlBlendMode mode) {
SkBlendMode mode) {
// There is currently no way for us to get the VertexMode from the SkVertices
// object, but for future reference:
//
@@ -456,6 +456,25 @@ void DisplayListMetalComplexityCalculator::MetalHelper::drawVertices(
AccumulateComplexity(complexity);
}
void DisplayListMetalComplexityCalculator::MetalHelper::drawVertices(
const DlVertices* vertices,
DlBlendMode mode) {
// There is currently no way for us to get the VertexMode from the SkVertices
// object, but for future reference:
//
// TriangleStrip is roughly 25% more expensive than TriangleFan.
// TriangleFan is roughly 5% more expensive than Triangles.
// For the baseline, it's hard to identify the trend. It might be O(n^1/2).
// For now, treat it as linear as an approximation.
//
// m = 1/4000
// c = 1
unsigned int complexity = (vertices->vertex_count() + 4000) * 50;
AccumulateComplexity(complexity);
}
void DisplayListMetalComplexityCalculator::MetalHelper::drawImage(
const sk_sp<DlImage> image,
const SkPoint point,

View File

@@ -54,8 +54,9 @@ class DisplayListMetalComplexityCalculator
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint points[]) override;
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override;
void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) override;
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override;
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -283,9 +283,8 @@ TEST(DisplayListComplexity, DrawArc) {
TEST(DisplayListComplexity, DrawVertices) {
auto points = GetTestPoints();
auto vertices =
SkVertices::MakeCopy(SkVertices::VertexMode::kTriangles_VertexMode,
points.size(), points.data(), nullptr, nullptr);
auto vertices = DlVertices::Make(DlVertexMode::kTriangles, points.size(),
points.data(), nullptr, nullptr);
DisplayListBuilder builder;
builder.drawVertices(vertices, DlBlendMode::kSrc);
auto display_list = builder.Build();

View File

@@ -12,6 +12,7 @@
#include "flutter/display_list/display_list_image.h"
#include "flutter/display_list/display_list_image_filter.h"
#include "flutter/display_list/display_list_mask_filter.h"
#include "flutter/display_list/display_list_vertices.h"
namespace flutter {
@@ -205,8 +206,9 @@ class Dispatcher {
virtual void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint points[]) = 0;
virtual void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) = 0;
virtual void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) = 0;
virtual void drawVertices(const DlVertices* vertices, DlBlendMode mode) = 0;
virtual void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -4,6 +4,7 @@
#include "flutter/display_list/display_list_blend_mode.h"
#include "flutter/display_list/display_list_tile_mode.h"
#include "flutter/display_list/display_list_vertices.h"
#include "flutter/display_list/types.h"
#include "gtest/gtest.h"
@@ -24,6 +25,24 @@ TEST(DisplayListEnum, ToSkTileMode) {
ASSERT_EQ(ToSk(DlTileMode::kDecal), SkTileMode::kDecal);
}
TEST(DisplayListEnum, ToDlVertexMode) {
ASSERT_EQ(ToDl(SkVertices::VertexMode::kTriangles_VertexMode),
DlVertexMode::kTriangles);
ASSERT_EQ(ToDl(SkVertices::VertexMode::kTriangleStrip_VertexMode),
DlVertexMode::kTriangleStrip);
ASSERT_EQ(ToDl(SkVertices::VertexMode::kTriangleFan_VertexMode),
DlVertexMode::kTriangleFan);
}
TEST(DisplayListEnum, ToSkVertexMode) {
ASSERT_EQ(ToSk(DlVertexMode::kTriangles),
SkVertices::VertexMode::kTriangles_VertexMode);
ASSERT_EQ(ToSk(DlVertexMode::kTriangleStrip),
SkVertices::VertexMode::kTriangleStrip_VertexMode);
ASSERT_EQ(ToSk(DlVertexMode::kTriangleFan),
SkVertices::VertexMode::kTriangleFan_VertexMode);
}
#define CHECK_TO_DLENUM(V) ASSERT_EQ(ToDl(SkBlendMode::V), DlBlendMode::V);
#define CHECK_TO_SKENUM(V) ASSERT_EQ(ToSk(DlBlendMode::V), SkBlendMode::V);

View File

@@ -594,18 +594,38 @@ DEFINE_DRAW_POINTS_OP(Lines, kLines_PointMode);
DEFINE_DRAW_POINTS_OP(Polygon, kPolygon_PointMode);
#undef DEFINE_DRAW_POINTS_OP
// 4 byte header + 12 byte payload packs efficiently into 16 bytes
// 4 byte header + 4 byte payload packs efficiently into 8 bytes
// The DlVertices object will be pod-allocated after this structure
// and can take any number of bytes so the final efficiency will
// depend on the size of the DlVertices.
// Note that the DlVertices object ends with an array of 16-bit
// indices so the alignment can be up to 6 bytes off leading to
// up to 6 bytes of overhead
struct DrawVerticesOp final : DLOp {
static const auto kType = DisplayListOpType::kDrawVertices;
DrawVerticesOp(sk_sp<SkVertices> vertices, DlBlendMode mode)
: mode(mode), vertices(std::move(vertices)) {}
DrawVerticesOp(DlBlendMode mode) : mode(mode) {}
const DlBlendMode mode;
void dispatch(Dispatcher& dispatcher) const {
const DlVertices* vertices = reinterpret_cast<const DlVertices*>(this + 1);
dispatcher.drawVertices(vertices, mode);
}
};
// 4 byte header + 12 byte payload packs efficiently into 16 bytes
struct DrawSkVerticesOp final : DLOp {
static const auto kType = DisplayListOpType::kDrawSkVertices;
DrawSkVerticesOp(sk_sp<SkVertices> vertices, SkBlendMode mode)
: mode(mode), vertices(std::move(vertices)) {}
const SkBlendMode mode;
const sk_sp<SkVertices> vertices;
void dispatch(Dispatcher& dispatcher) const {
dispatcher.drawVertices(vertices, mode);
dispatcher.drawSkVertices(vertices, mode);
}
};

View File

@@ -191,18 +191,18 @@ static const SkPath TestPath3 =
static const SkMatrix TestMatrix1 = SkMatrix::Scale(2, 2);
static const SkMatrix TestMatrix2 = SkMatrix::RotateDeg(45);
static sk_sp<SkVertices> TestVertices1 =
SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
3,
TestPoints,
nullptr,
colors);
static sk_sp<SkVertices> TestVertices2 =
SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
3,
TestPoints,
nullptr,
colors);
static std::shared_ptr<const DlVertices> TestVertices1 =
DlVertices::Make(DlVertexMode::kTriangles, //
3,
TestPoints,
nullptr,
colors);
static std::shared_ptr<const DlVertices> TestVertices2 =
DlVertices::Make(DlVertexMode::kTriangleFan, //
3,
TestPoints,
nullptr,
colors);
static constexpr int TestDivs1[] = {10, 20, 30};
static constexpr int TestDivs2[] = {15, 20, 25};
@@ -645,9 +645,9 @@ std::vector<DisplayListInvocationGroup> allGroups = {
}
},
{ "DrawVertices", {
{1, 16, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices1, DlBlendMode::kSrcIn);}},
{1, 16, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices1, DlBlendMode::kDstIn);}},
{1, 16, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices2, DlBlendMode::kSrcIn);}},
{1, 112, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices1, DlBlendMode::kSrcIn);}},
{1, 112, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices1, DlBlendMode::kDstIn);}},
{1, 112, 1, 16, [](DisplayListBuilder& b) {b.drawVertices(TestVertices2, DlBlendMode::kSrcIn);}},
}
},
{ "DrawImage", {

View File

@@ -448,7 +448,12 @@ void DisplayListBoundsCalculator::drawPoints(SkCanvas::PointMode mode,
}
}
}
void DisplayListBoundsCalculator::drawVertices(const sk_sp<SkVertices> vertices,
void DisplayListBoundsCalculator::drawSkVertices(
const sk_sp<SkVertices> vertices,
SkBlendMode mode) {
AccumulateOpBounds(vertices->bounds(), kDrawVerticesFlags);
}
void DisplayListBoundsCalculator::drawVertices(const DlVertices* vertices,
DlBlendMode mode) {
AccumulateOpBounds(vertices->bounds(), kDrawVerticesFlags);
}

View File

@@ -114,8 +114,9 @@ class IgnoreDrawDispatchHelper : public virtual Dispatcher {
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint points[]) override {}
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override {}
void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) override {}
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override {}
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,
@@ -426,8 +427,9 @@ class DisplayListBoundsCalculator final
void drawPoints(SkCanvas::PointMode mode,
uint32_t count,
const SkPoint pts[]) override;
void drawVertices(const sk_sp<SkVertices> vertices,
DlBlendMode mode) override;
void drawSkVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) override;
void drawVertices(const DlVertices* vertices, DlBlendMode mode) override;
void drawImage(const sk_sp<DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,

View File

@@ -0,0 +1,305 @@
// 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 "flutter/display_list/display_list_vertices.h"
#include "flutter/display_list/display_list_utils.h"
#include "flutter/fml/logging.h"
namespace flutter {
using Flags = DlVertices::Builder::Flags;
static void DlVerticesDeleter(void* p) {
// Some of our target environments would prefer a sized delete,
// but other target environments do not have that operator.
// Use an unsized delete until we get better agreement in the
// environments.
// See https://github.com/flutter/flutter/issues/100327
::operator delete(p);
}
static size_t bytes_needed(int vertex_count, Flags flags, int index_count) {
int needed = sizeof(DlVertices);
// We always have vertices
needed += vertex_count * sizeof(SkPoint);
if (flags.has_texture_coordinates) {
needed += vertex_count * sizeof(SkPoint);
}
if (flags.has_colors) {
needed += vertex_count * sizeof(SkColor);
}
if (index_count > 0) {
needed += index_count * sizeof(uint16_t);
}
return needed;
}
std::shared_ptr<DlVertices> DlVertices::Make(
DlVertexMode mode,
int vertex_count,
const SkPoint vertices[],
const SkPoint texture_coordinates[],
const SkColor colors[],
int index_count,
const uint16_t indices[]) {
if (!vertices || vertex_count <= 0) {
vertex_count = 0;
texture_coordinates = nullptr;
colors = nullptr;
}
if (!indices || index_count <= 0) {
index_count = 0;
indices = nullptr;
}
Flags flags;
FML_DCHECK(!flags.has_texture_coordinates);
FML_DCHECK(!flags.has_colors);
if (texture_coordinates) {
flags |= Builder::kHasTextureCoordinates;
}
if (colors) {
flags |= Builder::kHasColors;
}
Builder builder(mode, vertex_count, flags, index_count);
builder.store_vertices(vertices);
if (texture_coordinates) {
builder.store_texture_coordinates(texture_coordinates);
}
if (colors) {
builder.store_colors(colors);
}
if (indices) {
builder.store_indices(indices);
}
return builder.build();
}
size_t DlVertices::size() const {
return bytes_needed(vertex_count_,
{{texture_coordinates_offset_ > 0, colors_offset_ > 0}},
index_count_);
}
static SkRect compute_bounds(const SkPoint* points, int count) {
BoundsAccumulator accumulator;
for (int i = 0; i < count; i++) {
accumulator.accumulate(points[i]);
}
return accumulator.bounds();
}
DlVertices::DlVertices(DlVertexMode mode,
int unchecked_vertex_count,
const SkPoint* vertices,
const SkPoint* texture_coordinates,
const SkColor* colors,
int unchecked_index_count,
const uint16_t* indices,
const SkRect* bounds)
: mode_(mode),
vertex_count_(std::max(unchecked_vertex_count, 0)),
index_count_(indices ? std::max(unchecked_index_count, 0) : 0) {
bounds_ = bounds ? *bounds : compute_bounds(vertices, vertex_count_);
char* pod = reinterpret_cast<char*>(this);
size_t offset = sizeof(DlVertices);
auto advance = [pod, &offset](auto* src, int count) {
if (src != nullptr && count > 0) {
size_t bytes = count * sizeof(*src);
memcpy(pod + offset, src, bytes);
size_t ret = offset;
offset += bytes;
return ret;
} else {
return size_t(0);
}
};
vertices_offset_ = advance(vertices, vertex_count_);
texture_coordinates_offset_ = advance(texture_coordinates, vertex_count_);
colors_offset_ = advance(colors, vertex_count_);
indices_offset_ = advance(indices, index_count_);
FML_DCHECK(offset == bytes_needed(vertex_count_,
{{!!texture_coordinates, !!colors}},
index_count_));
}
DlVertices::DlVertices(const DlVertices* other)
: DlVertices(other->mode_,
other->vertex_count_,
other->vertices(),
other->texture_coordinates(),
other->colors(),
other->index_count_,
other->indices(),
&other->bounds_) {}
DlVertices::DlVertices(DlVertexMode mode,
int unchecked_vertex_count,
Flags flags,
int unchecked_index_count)
: mode_(mode),
vertex_count_(std::max(unchecked_vertex_count, 0)),
index_count_(std::max(unchecked_index_count, 0)) {
char* pod = reinterpret_cast<char*>(this);
size_t offset = sizeof(DlVertices);
auto advance = [pod, &offset](size_t size, int count) {
if (count > 0) {
size_t bytes = count * size;
memset(pod + offset, 0, bytes);
size_t ret = offset;
offset += bytes;
return ret;
} else {
return size_t(0);
}
};
vertices_offset_ = advance(sizeof(SkPoint), vertex_count_);
texture_coordinates_offset_ = advance(
sizeof(SkPoint), flags.has_texture_coordinates ? vertex_count_ : 0);
colors_offset_ =
advance(sizeof(SkColor), flags.has_colors ? vertex_count_ : 0);
indices_offset_ = advance(sizeof(uint16_t), index_count_);
FML_DCHECK(offset == bytes_needed(vertex_count_, flags, index_count_));
FML_DCHECK((vertex_count_ != 0) == (vertices() != nullptr));
FML_DCHECK((vertex_count_ != 0 && flags.has_texture_coordinates) ==
(texture_coordinates() != nullptr));
FML_DCHECK((vertex_count_ != 0 && flags.has_colors) == (colors() != nullptr));
FML_DCHECK((index_count_ != 0) == (indices() != nullptr));
}
sk_sp<SkVertices> DlVertices::skia_object() const {
return SkVertices::MakeCopy(ToSk(mode_), vertex_count_, vertices(),
texture_coordinates(), colors(), index_count_,
indices());
}
bool DlVertices::operator==(DlVertices const& other) const {
auto lists_equal = [](auto* a, auto* b, int count) {
if (a == nullptr || b == nullptr) {
return a == b;
}
for (int i = 0; i < count; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
};
return //
mode_ == other.mode_ && //
vertex_count_ == other.vertex_count_ && //
lists_equal(vertices(), other.vertices(), vertex_count_) && //
lists_equal(texture_coordinates(), other.texture_coordinates(), //
vertex_count_) && //
lists_equal(colors(), other.colors(), vertex_count_) && //
index_count_ == other.index_count_ && //
lists_equal(indices(), other.indices(), index_count_);
}
DlVertices::Builder::Builder(DlVertexMode mode,
int vertex_count,
Flags flags,
int index_count)
: needs_vertices_(true),
needs_texture_coords_(flags.has_texture_coordinates),
needs_colors_(flags.has_colors),
needs_indices_(index_count > 0) {
vertex_count = std::max(vertex_count, 0);
index_count = std::max(index_count, 0);
void* storage =
::operator new(bytes_needed(vertex_count, flags, index_count));
vertices_.reset(new (storage)
DlVertices(mode, vertex_count, flags, index_count),
DlVerticesDeleter);
}
static void store_points(char* dst, int offset, const float* src, int count) {
SkPoint* points = reinterpret_cast<SkPoint*>(dst + offset);
for (int i = 0; i < count; i++) {
points[i] = SkPoint::Make(src[i * 2], src[i * 2 + 1]);
}
}
void DlVertices::Builder::store_vertices(const SkPoint vertices[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_vertices_);
char* pod = reinterpret_cast<char*>(vertices_.get());
size_t bytes = vertices_->vertex_count_ * sizeof(vertices[0]);
memcpy(pod + vertices_->vertices_offset_, vertices, bytes);
needs_vertices_ = false;
}
void DlVertices::Builder::store_vertices(const float vertices[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_vertices_);
char* pod = reinterpret_cast<char*>(vertices_.get());
store_points(pod, vertices_->vertices_offset_, vertices,
vertices_->vertex_count_);
needs_vertices_ = false;
}
void DlVertices::Builder::store_texture_coordinates(const SkPoint coords[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_texture_coords_);
char* pod = reinterpret_cast<char*>(vertices_.get());
size_t bytes = vertices_->vertex_count_ * sizeof(coords[0]);
memcpy(pod + vertices_->texture_coordinates_offset_, coords, bytes);
needs_texture_coords_ = false;
}
void DlVertices::Builder::store_texture_coordinates(const float coords[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_texture_coords_);
char* pod = reinterpret_cast<char*>(vertices_.get());
store_points(pod, vertices_->texture_coordinates_offset_, coords,
vertices_->vertex_count_);
needs_texture_coords_ = false;
}
void DlVertices::Builder::store_colors(const SkColor colors[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_colors_);
char* pod = reinterpret_cast<char*>(vertices_.get());
size_t bytes = vertices_->vertex_count_ * sizeof(colors[0]);
memcpy(pod + vertices_->colors_offset_, colors, bytes);
needs_colors_ = false;
}
void DlVertices::Builder::store_indices(const uint16_t indices[]) {
FML_CHECK(is_valid());
FML_CHECK(needs_indices_);
char* pod = reinterpret_cast<char*>(vertices_.get());
size_t bytes = vertices_->index_count_ * sizeof(indices[0]);
memcpy(pod + vertices_->indices_offset_, indices, bytes);
needs_indices_ = false;
}
std::shared_ptr<DlVertices> DlVertices::Builder::build() {
FML_CHECK(is_valid());
if (vertices_->vertex_count() <= 0) {
// We set this to true in the constructor to make sure that they
// call store_vertices() only once, but if there are no vertices
// then we will not object to them never having stored any vertices
needs_vertices_ = false;
}
FML_CHECK(!needs_vertices_);
FML_CHECK(!needs_texture_coords_);
FML_CHECK(!needs_colors_);
FML_CHECK(!needs_indices_);
vertices_->bounds_ =
compute_bounds(vertices_->vertices(), vertices_->vertex_count_);
return std::move(vertices_);
}
} // namespace flutter

View File

@@ -0,0 +1,285 @@
// 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.
#ifndef FLUTTER_DISPLAY_LIST_DISPLAY_LIST_VERTICES_H_
#define FLUTTER_DISPLAY_LIST_DISPLAY_LIST_VERTICES_H_
#include "flutter/display_list/types.h"
namespace flutter {
//------------------------------------------------------------------------------
/// @brief Defines the way in which the vertices of a DlVertices object
/// are separated into triangles into which to render.
///
enum class DlVertexMode {
/// The vertices are taken 3 at a time to form a triangle.
kTriangles,
/// The vertices are taken in overlapping triplets to form triangles, with
/// each triplet sharing 2 of its vertices with the preceding and following
/// triplets.
/// vertices [ABCDE] yield 3 triangles ABC,BCD,CDE
kTriangleStrip,
/// The vertices are taken in overlapping pairs and combined with the first
/// vertex to form triangles that radiate outward from the initial point.
/// vertices [ABCDE] yield 3 triangles ABC,ACD,ADE
kTriangleFan,
};
inline SkVertices::VertexMode ToSk(DlVertexMode dl_mode) {
return static_cast<SkVertices::VertexMode>(dl_mode);
}
inline DlVertexMode ToDl(SkVertices::VertexMode sk_mode) {
return static_cast<DlVertexMode>(sk_mode);
}
//------------------------------------------------------------------------------
/// @brief Holds all of the data (both required and optional) for a
/// DisplayList drawVertices call.
///
/// There are 4 main pices of data:
/// - vertices():
/// the points on the rendering surface that define the pixels
/// being rendered to in a series of triangles. These points
/// can map to triangles in various ways depending on the
/// supplied |DlVertexMode|.
/// - texture_coordinates():
/// the points in the DlColorSource to map to the coordinates
/// of the triangles in the vertices(). If missing, the
/// vertex coordinates themselves will be used to map the
/// source colors to the vertices.
/// - colors():
/// colors to map to each triangle vertex. Note that each
/// vertex is mapped to exactly 1 color even if the DlVertex
/// - indices():
/// An indirection based on indices into the array of vertices
/// (and by extension, their associated texture_coordinates and
/// colors). Note that the DLVertexMode will still apply to the
/// indices in the same way (and instead of the way) that it
/// would normally be applied to the vertices themselves. The
/// indices are useful, for example, to fill the vertices with
/// a grid of points and then use the indices to define a
/// triangular mesh that covers that grid without having to
/// repeat the vertex (and texture coordinate and color)
/// information for the times when a given grid coordinate
/// gets reused in up to 4 mesh triangles.
///
/// Note that each vertex is mapped to exactly 1 texture_coordinate and
/// color even if the DlVertexMode or indices specify that it contributes
/// to more than one output triangle.
///
class DlVertices {
public:
/// @brief A utility class to build up a |DlVertices| object
/// one set of data at a time.
class Builder {
public:
/// @brief flags to indicate/promise which of the optional
/// texture coordinates or colors will be supplied
/// during the build phase.
union Flags {
struct {
unsigned has_texture_coordinates : 1;
unsigned has_colors : 1;
};
uint32_t mask = 0;
inline Flags operator|(const Flags& rhs) const {
return {.mask = (mask | rhs.mask)};
}
inline Flags& operator|=(const Flags& rhs) {
mask = mask | rhs.mask;
return *this;
}
};
static constexpr Flags kNone = {{false, false}};
static constexpr Flags kHasTextureCoordinates = {{true, false}};
static constexpr Flags kHasColors = {{false, true}};
//--------------------------------------------------------------------------
/// @brief Constructs a Builder and prepares room for the
/// required and optional data.
///
/// Vertices are always required. Optional texture coordinates
/// and optional colors are reserved depending on the |Flags|.
/// Optional indices will be reserved iff the index_count is
/// positive (>0).
///
/// The caller must provide all data that is promised by the
/// supplied |flags| and |index_count| parameters before
/// calling the |build()| method.
Builder(DlVertexMode mode, int vertex_count, Flags flags, int index_count);
/// Returns true iff the underlying object was successfully allocated.
bool is_valid() { return vertices_ != nullptr; }
/// @brief Copies the indicated list of points as vertices.
///
/// fails if vertices have already been supplied.
void store_vertices(const SkPoint points[]);
/// @brief Copies the indicated list of float pairs as vertices.
///
/// fails if vertices have already been supplied.
void store_vertices(const float coordinates[]);
/// @brief Copies the indicated list of points as texture coordinates.
///
/// fails if texture coordinates have already been supplied or if they
/// were not promised by the flags.has_texture_coordinates.
void store_texture_coordinates(const SkPoint points[]);
/// @brief Copies the indicated list of float pairs as texture coordinates.
///
/// fails if texture coordinates have already been supplied or if they
/// were not promised by the flags.has_texture_coordinates.
void store_texture_coordinates(const float coordinates[]);
/// @brief Copies the indicated list of colors as vertex colors.
///
/// fails if colors have already been supplied or if they were not
/// promised by the flags.has_colors.
void store_colors(const SkColor colors[]);
/// @brief Copies the indicated list of 16-bit indices as vertex indices.
///
/// fails if indices have already been supplied or if they were not
/// promised by (index_count > 0).
void store_indices(const uint16_t indices[]);
/// @brief Finalizes and the constructed DlVertices object.
///
/// fails if any of the optional data promised in the constructor is
/// missing.
std::shared_ptr<DlVertices> build();
private:
std::shared_ptr<DlVertices> vertices_;
bool needs_vertices_;
bool needs_texture_coords_;
bool needs_colors_;
bool needs_indices_;
};
//--------------------------------------------------------------------------
/// @brief Constructs a DlVector with compact inline storage for all
/// of its required and optional lists of data.
///
/// Vertices are always required. Optional texture coordinates
/// and optional colors are stored if the arguments are non-null.
/// Optional indices will be stored iff the array argument is
/// non-null and the index_count is positive (>0).
static std::shared_ptr<DlVertices> Make(DlVertexMode mode,
int vertex_count,
const SkPoint vertices[],
const SkPoint texture_coordinates[],
const SkColor colors[],
int index_count = 0,
const uint16_t indices[] = nullptr);
/// Returns the size of the object including all of the inlined data.
size_t size() const;
/// Returns the bounds of the vertices.
SkRect bounds() const { return bounds_; }
/// Returns the vertex mode that defines how the vertices (or the indices)
/// are turned into triangles.
DlVertexMode mode() const { return mode_; }
/// Returns the number of vertices, which also applies to the number of
/// texture coordinate and colors if they are provided.
int vertex_count() const { return vertex_count_; }
/// Returns a pointer to the vertex information. Should be non-null.
const SkPoint* vertices() const {
return static_cast<const SkPoint*>(pod(vertices_offset_));
}
/// Returns a pointer to the vertex texture coordinate
/// or null if none were provided.
const SkPoint* texture_coordinates() const {
return static_cast<const SkPoint*>(pod(texture_coordinates_offset_));
}
/// Returns a pointer to the vertex colors
/// or null if none were provided.
const SkColor* colors() const {
return static_cast<const SkColor*>(pod(colors_offset_));
}
/// Returns a pointer to the count of vertex indices
/// or 0 if none were provided.
int index_count() const { return index_count_; }
/// Returns a pointer to the vertex indices
/// or null if none were provided.
const uint16_t* indices() const {
return static_cast<const uint16_t*>(pod(indices_offset_));
}
// Returns an equivalent sk_sp<SkVertices> analog to this object.
sk_sp<SkVertices> skia_object() const;
bool operator==(DlVertices const& other) const;
bool operator!=(DlVertices const& other) const { return !(*this == other); }
private:
// Constructors are designed to encapsulate arrays sequentially in memory
// which means they can only be called by intantiations that use the
// new (ptr) paradigm which precomputes and preallocates the memory for
// the class body and all of its arrays, such as in Builder.
DlVertices(DlVertexMode mode,
int vertex_count,
const SkPoint vertices[],
const SkPoint texture_coordinates[],
const SkColor colors[],
int index_count,
const uint16_t indices[],
const SkRect* bounds = nullptr);
// This constructor is specifically used by the DlVertices::Builder to
// establish the object before the copying of data is requested.
DlVertices(DlVertexMode mode,
int vertex_count,
Builder::Flags flags,
int index_count);
// The copy constructor has the same memory pre-allocation requirements
// as this other constructors. This particular version is used by the
// DisplaylistBuilder to copy the instance into pre-allocated pod memory
// in the display list buffer.
explicit DlVertices(const DlVertices* other);
DlVertexMode mode_;
int vertex_count_;
size_t vertices_offset_;
size_t texture_coordinates_offset_;
size_t colors_offset_;
int index_count_;
size_t indices_offset_;
SkRect bounds_;
const void* pod(int offset) const {
if (offset <= 0) {
return nullptr;
}
const void* base = static_cast<const void*>(this);
return static_cast<const char*>(base) + offset;
}
friend class DisplayListBuilder;
};
} // namespace flutter
#endif // FLUTTER_DISPLAY_LIST_DISPLAY_LIST_VERTICES_H_

View File

@@ -0,0 +1,986 @@
// 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 "flutter/display_list/display_list_attributes_testing.h"
#include "flutter/display_list/display_list_builder.h"
#include "flutter/display_list/display_list_comparable.h"
#include "flutter/display_list/display_list_vertices.h"
#include "flutter/display_list/types.h"
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
TEST(DisplayListVertices, MakeWithZeroAndNegativeVerticesAndIndices) {
std::shared_ptr<const DlVertices> vertices1 = DlVertices::Make(
DlVertexMode::kTriangles, 0, nullptr, nullptr, nullptr, 0, nullptr);
EXPECT_NE(vertices1, nullptr);
EXPECT_EQ(vertices1->vertex_count(), 0);
EXPECT_EQ(vertices1->vertices(), nullptr);
EXPECT_EQ(vertices1->texture_coordinates(), nullptr);
EXPECT_EQ(vertices1->colors(), nullptr);
EXPECT_EQ(vertices1->index_count(), 0);
EXPECT_EQ(vertices1->indices(), nullptr);
EXPECT_NE(vertices1->skia_object(), nullptr);
std::shared_ptr<const DlVertices> vertices2 = DlVertices::Make(
DlVertexMode::kTriangles, -1, nullptr, nullptr, nullptr, -1, nullptr);
EXPECT_NE(vertices2, nullptr);
EXPECT_EQ(vertices2->vertex_count(), 0);
EXPECT_EQ(vertices2->vertices(), nullptr);
EXPECT_EQ(vertices2->texture_coordinates(), nullptr);
EXPECT_EQ(vertices2->colors(), nullptr);
EXPECT_EQ(vertices2->index_count(), 0);
EXPECT_EQ(vertices2->indices(), nullptr);
EXPECT_NE(vertices2->skia_object(), nullptr);
TestEquals(*vertices1, *vertices2);
}
TEST(DisplayListVertices, MakeWithTexAndColorAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, colors, 6, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, MakeWithTexAndColor) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, colors, 6, nullptr);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, MakeWithTexAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, nullptr, 6, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, MakeWithColorAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, colors, 6, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, MakeWithTex) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, nullptr, 6, nullptr);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, MakeWithColor) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, colors, 6, nullptr);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, MakeWithIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, nullptr, 6, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, MakeWithNoOptionalData) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, nullptr, 6, nullptr);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, MakeWithIndicesButZeroIndexCount) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, nullptr, 0, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, MakeWithIndicesButNegativeIndexCount) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, nullptr, nullptr, -5, indices);
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
using Builder = DlVertices::Builder;
TEST(DisplayListVertices, BuilderFlags) {
Builder::Flags flags;
EXPECT_FALSE(flags.has_texture_coordinates);
EXPECT_FALSE(flags.has_colors);
flags |= Builder::kHasTextureCoordinates;
EXPECT_TRUE(flags.has_texture_coordinates);
EXPECT_FALSE(flags.has_colors);
flags |= Builder::kHasColors;
EXPECT_TRUE(flags.has_texture_coordinates);
EXPECT_TRUE(flags.has_colors);
flags = Builder::Flags();
EXPECT_FALSE(flags.has_texture_coordinates);
EXPECT_FALSE(flags.has_colors);
flags |= Builder::kHasColors;
EXPECT_FALSE(flags.has_texture_coordinates);
EXPECT_TRUE(flags.has_colors);
flags |= Builder::kHasTextureCoordinates;
EXPECT_TRUE(flags.has_texture_coordinates);
EXPECT_TRUE(flags.has_colors);
EXPECT_FALSE(Builder::kNone.has_texture_coordinates);
EXPECT_FALSE(Builder::kNone.has_colors);
EXPECT_TRUE(Builder::kHasTextureCoordinates.has_texture_coordinates);
EXPECT_FALSE(Builder::kHasTextureCoordinates.has_colors);
EXPECT_FALSE(Builder::kHasColors.has_texture_coordinates);
EXPECT_TRUE(Builder::kHasColors.has_colors);
EXPECT_TRUE((Builder::kHasTextureCoordinates | Builder::kHasColors) //
.has_texture_coordinates);
EXPECT_TRUE((Builder::kHasTextureCoordinates | Builder::kHasColors) //
.has_colors);
}
TEST(DisplayListVertices, BuildWithZeroAndNegativeVerticesAndIndices) {
Builder builder1(DlVertexMode::kTriangles, 0, Builder::kNone, 0);
EXPECT_TRUE(builder1.is_valid());
std::shared_ptr<DlVertices> vertices1 = builder1.build();
EXPECT_NE(vertices1, nullptr);
EXPECT_EQ(vertices1->vertex_count(), 0);
EXPECT_EQ(vertices1->vertices(), nullptr);
EXPECT_EQ(vertices1->texture_coordinates(), nullptr);
EXPECT_EQ(vertices1->colors(), nullptr);
EXPECT_EQ(vertices1->index_count(), 0);
EXPECT_EQ(vertices1->indices(), nullptr);
EXPECT_NE(vertices1->skia_object(), nullptr);
Builder builder2(DlVertexMode::kTriangles, -1, Builder::kNone, -1);
EXPECT_TRUE(builder2.is_valid());
std::shared_ptr<DlVertices> vertices2 = builder2.build();
EXPECT_NE(vertices2, nullptr);
EXPECT_EQ(vertices2->vertex_count(), 0);
EXPECT_EQ(vertices2->vertices(), nullptr);
EXPECT_EQ(vertices2->texture_coordinates(), nullptr);
EXPECT_EQ(vertices2->colors(), nullptr);
EXPECT_EQ(vertices2->index_count(), 0);
EXPECT_EQ(vertices2->indices(), nullptr);
EXPECT_NE(vertices2->skia_object(), nullptr);
TestEquals(*vertices1, *vertices2);
}
TEST(DisplayListVertices, BuildWithTexAndColorAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates | Builder::kHasColors, 6);
builder.store_vertices(coords);
builder.store_texture_coordinates(texture_coords);
builder.store_colors(colors);
builder.store_indices(indices);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
Builder builder2(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates | Builder::kHasColors, 6);
builder2.store_vertices(coords);
builder2.store_texture_coordinates(texture_coords);
builder2.store_colors(colors);
builder2.store_indices(indices);
std::shared_ptr<const DlVertices> vertices2 = builder2.build();
TestEquals(*vertices, *vertices2);
std::shared_ptr<const DlVertices> vertices3 = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, colors, 6, indices);
TestEquals(*vertices, *vertices3);
}
TEST(DisplayListVertices, BuildWithTexAndColor) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates | Builder::kHasColors, 0);
builder.store_vertices(coords);
builder.store_texture_coordinates(texture_coords);
builder.store_colors(colors);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, BuildWithTexAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates, 6);
builder.store_vertices(coords);
builder.store_texture_coordinates(texture_coords);
builder.store_indices(indices);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, BuildWithColorAndIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasColors, 6);
builder.store_vertices(coords);
builder.store_colors(colors);
builder.store_indices(indices);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, BuildWithTexUsingPoints) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates, 0);
builder.store_vertices(coords);
builder.store_texture_coordinates(texture_coords);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->texture_coordinates()[i], texture_coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, BuildWithTexUsingFloats) {
float coords[6] = {
2, 3, //
5, 6, //
15, 20,
};
float texture_coords[6] = {
102, 103, //
105, 106, //
115, 120,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates, 0);
builder.store_vertices(coords);
builder.store_texture_coordinates(texture_coords);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_NE(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i].fX, coords[i * 2 + 0]);
ASSERT_EQ(vertices->vertices()[i].fY, coords[i * 2 + 1]);
ASSERT_EQ(vertices->texture_coordinates()[i].fX, texture_coords[i * 2 + 0]);
ASSERT_EQ(vertices->texture_coordinates()[i].fY, texture_coords[i * 2 + 1]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, BuildUsingFloatsSameAsPoints) {
SkPoint coord_points[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coord_points[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
float coord_floats[6] = {
2, 3, //
5, 6, //
15, 20,
};
float texture_coord_floats[6] = {
102, 103, //
105, 106, //
115, 120,
};
Builder builder_points(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates, 0);
builder_points.store_vertices(coord_points);
builder_points.store_texture_coordinates(texture_coord_points);
std::shared_ptr<const DlVertices> vertices_points = builder_points.build();
Builder builder_floats(DlVertexMode::kTriangles, 3, //
Builder::kHasTextureCoordinates, 0);
builder_floats.store_vertices(coord_floats);
builder_floats.store_texture_coordinates(texture_coord_floats);
std::shared_ptr<const DlVertices> vertices_floats = builder_floats.build();
TestEquals(*vertices_points, *vertices_floats);
}
TEST(DisplayListVertices, BuildWithColor) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
Builder builder(DlVertexMode::kTriangles, 3, //
Builder::kHasColors, 0);
builder.store_vertices(coords);
builder.store_colors(colors);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_NE(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
ASSERT_EQ(vertices->colors()[i], colors[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, BuildWithIndices) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
Builder builder(DlVertexMode::kTriangles, 3, Builder::kNone, 6);
builder.store_vertices(coords);
builder.store_indices(indices);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_NE(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 6);
for (int i = 0; i < 6; i++) {
ASSERT_EQ(vertices->indices()[i], indices[i]);
}
}
TEST(DisplayListVertices, BuildWithNoOptionalData) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
Builder builder(DlVertexMode::kTriangles, 3, Builder::kNone, 0);
builder.store_vertices(coords);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, BuildWithNegativeIndexCount) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
Builder builder(DlVertexMode::kTriangles, 3, Builder::kNone, -5);
builder.store_vertices(coords);
std::shared_ptr<const DlVertices> vertices = builder.build();
ASSERT_NE(vertices, nullptr);
ASSERT_NE(vertices->vertices(), nullptr);
ASSERT_EQ(vertices->texture_coordinates(), nullptr);
ASSERT_EQ(vertices->colors(), nullptr);
ASSERT_EQ(vertices->indices(), nullptr);
ASSERT_EQ(vertices->bounds(), SkRect::MakeLTRB(2, 3, 15, 20));
ASSERT_EQ(vertices->mode(), DlVertexMode::kTriangles);
ASSERT_EQ(vertices->vertex_count(), 3);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(vertices->vertices()[i], coords[i]);
}
ASSERT_EQ(vertices->index_count(), 0);
}
TEST(DisplayListVertices, TestEquals) {
SkPoint coords[3] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
};
SkPoint texture_coords[3] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
};
SkColor colors[3] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
};
uint16_t indices[6] = {
2, 1, 0, //
1, 2, 0,
};
std::shared_ptr<const DlVertices> vertices1 = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, colors, 6, indices);
std::shared_ptr<const DlVertices> vertices2 = DlVertices::Make(
DlVertexMode::kTriangles, 3, coords, texture_coords, colors, 6, indices);
TestEquals(*vertices1, *vertices2);
}
TEST(DisplayListVertices, TestNotEquals) {
SkPoint coords[4] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
SkPoint::Make(53, 62),
};
SkPoint wrong_coords[4] = {
SkPoint::Make(2, 3),
SkPoint::Make(5, 6),
SkPoint::Make(15, 20),
SkPoint::Make(57, 62),
};
SkPoint texture_coords[4] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 120),
SkPoint::Make(153, 162),
};
SkPoint wrong_texture_coords[4] = {
SkPoint::Make(102, 103),
SkPoint::Make(105, 106),
SkPoint::Make(115, 121),
SkPoint::Make(153, 162),
};
SkColor colors[4] = {
SK_ColorRED,
SK_ColorCYAN,
SK_ColorGREEN,
SK_ColorMAGENTA,
};
SkColor wrong_colors[4] = {
SK_ColorRED,
SK_ColorBLUE,
SK_ColorGREEN,
SK_ColorMAGENTA,
};
uint16_t indices[9] = {
2, 1, 0, //
1, 2, 0, //
1, 2, 3,
};
uint16_t wrong_indices[9] = {
2, 1, 0, //
1, 2, 0, //
2, 3, 1,
};
std::shared_ptr<const DlVertices> vertices1 = DlVertices::Make(
DlVertexMode::kTriangles, 4, coords, texture_coords, colors, 9, indices);
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangleFan, 4, coords, //
texture_coords, colors, 9, indices);
TestNotEquals(*vertices1, *vertices2, "vertex mode differs");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 3, coords, //
texture_coords, colors, 9, indices);
TestNotEquals(*vertices1, *vertices2, "vertex count differs");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 4, wrong_coords, //
texture_coords, colors, 9, indices);
TestNotEquals(*vertices1, *vertices2, "vertex coordinates differ");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 4, coords, //
wrong_texture_coords, colors, 9, indices);
TestNotEquals(*vertices1, *vertices2, "texture coordinates differ");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 4, coords, //
texture_coords, wrong_colors, 9, indices);
TestNotEquals(*vertices1, *vertices2, "colors differ");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 4, coords, //
texture_coords, colors, 6, indices);
TestNotEquals(*vertices1, *vertices2, "index count differs");
}
{
std::shared_ptr<const DlVertices> vertices2 =
DlVertices::Make(DlVertexMode::kTriangles, 4, coords, //
texture_coords, colors, 9, wrong_indices);
TestNotEquals(*vertices1, *vertices2, "indices differ");
}
}
} // namespace testing
} // namespace flutter

View File

@@ -610,7 +610,7 @@ void Canvas::drawVertices(const Vertices* vertices,
builder()->drawVertices(vertices->vertices(), blend_mode);
} else if (canvas_) {
SkPaint sk_paint;
canvas_->drawVertices(vertices->vertices(), ToSk(blend_mode),
canvas_->drawVertices(vertices->vertices()->skia_object(), ToSk(blend_mode),
*paint.paint(sk_paint));
}
}

View File

@@ -12,23 +12,6 @@
namespace flutter {
namespace {
void DecodePoints(const tonic::Float32List& coords, SkPoint* points) {
for (int i = 0; i < coords.num_elements(); i += 2) {
points[i / 2] = SkPoint::Make(coords[i], coords[i + 1]);
}
}
template <typename T>
void DecodeInts(const tonic::Int32List& ints, T* out) {
for (int i = 0; i < ints.num_elements(); i++) {
out[i] = ints[i];
}
}
} // namespace
IMPLEMENT_WRAPPERTYPEINFO(ui, Vertices);
#define FOR_EACH_BINDING(V) V(Vertices, init)
@@ -44,48 +27,48 @@ void Vertices::RegisterNatives(tonic::DartLibraryNatives* natives) {
}
bool Vertices::init(Dart_Handle vertices_handle,
SkVertices::VertexMode vertex_mode,
DlVertexMode vertex_mode,
tonic::Float32List& positions,
tonic::Float32List& texture_coordinates,
tonic::Int32List& colors,
tonic::Uint16List& indices) {
UIDartState::ThrowIfUIOperationsProhibited();
uint32_t builderFlags = 0;
if (!positions.data()) {
return false;
}
DlVertices::Builder::Flags flags;
if (texture_coordinates.data()) {
builderFlags |= SkVertices::kHasTexCoords_BuilderFlag;
flags = flags | DlVertices::Builder::kHasTextureCoordinates;
}
if (colors.data()) {
builderFlags |= SkVertices::kHasColors_BuilderFlag;
flags = flags | DlVertices::Builder::kHasColors;
}
DlVertices::Builder builder(vertex_mode, positions.num_elements() / 2, flags,
indices.num_elements());
SkVertices::Builder builder(vertex_mode, positions.num_elements() / 2,
indices.num_elements(), builderFlags);
if (!builder.isValid()) {
if (!builder.is_valid()) {
return false;
}
// positions are required for SkVertices::Builder
FML_DCHECK(positions.data());
if (positions.data()) {
DecodePoints(positions, builder.positions());
}
builder.store_vertices(positions.data());
if (texture_coordinates.data()) {
// SkVertices::Builder assumes equal numbers of elements
FML_DCHECK(positions.num_elements() == texture_coordinates.num_elements());
DecodePoints(texture_coordinates, builder.texCoords());
builder.store_texture_coordinates(texture_coordinates.data());
}
if (colors.data()) {
// SkVertices::Builder assumes equal numbers of elements
FML_DCHECK(positions.num_elements() / 2 == colors.num_elements());
DecodeInts<SkColor>(colors, builder.colors());
builder.store_colors(reinterpret_cast<const SkColor*>(colors.data()));
}
if (indices.data()) {
std::copy(indices.data(), indices.data() + indices.num_elements(),
builder.indices());
builder.store_indices(indices.data());
}
positions.Release();
@@ -94,14 +77,14 @@ bool Vertices::init(Dart_Handle vertices_handle,
indices.Release();
auto vertices = fml::MakeRefCounted<Vertices>();
vertices->vertices_ = builder.detach();
vertices->vertices_ = builder.build();
vertices->AssociateWithDartWrapper(vertices_handle);
return true;
}
size_t Vertices::GetAllocationSize() const {
return sizeof(Vertices) + vertices_->approximateSize();
return vertices_->size();
}
} // namespace flutter

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_LIB_UI_PAINTING_VERTICES_H_
#define FLUTTER_LIB_UI_PAINTING_VERTICES_H_
#include "flutter/display_list/display_list_vertices.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "third_party/skia/include/core/SkVertices.h"
#include "third_party/tonic/typed_data/typed_list.h"
@@ -25,20 +26,20 @@ class Vertices : public RefCountedDartWrappable<Vertices> {
static void RegisterNatives(tonic::DartLibraryNatives* natives);
static bool init(Dart_Handle vertices_handle,
SkVertices::VertexMode vertex_mode,
DlVertexMode vertex_mode,
tonic::Float32List& positions,
tonic::Float32List& texture_coordinates,
tonic::Int32List& colors,
tonic::Uint16List& indices);
const sk_sp<SkVertices>& vertices() const { return vertices_; }
const DlVertices* vertices() const { return vertices_.get(); }
size_t GetAllocationSize() const override;
private:
Vertices();
sk_sp<SkVertices> vertices_;
std::shared_ptr<DlVertices> vertices_;
};
} // namespace flutter