Flutter Engine
The Flutter Engine
aiks_dl_vertices_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
8#include "flutter/impeller/aiks/aiks_unittests.h"
9
10#include "flutter/display_list/dl_blend_mode.h"
11#include "flutter/display_list/dl_builder.h"
12#include "flutter/display_list/dl_color.h"
13#include "flutter/display_list/dl_paint.h"
14#include "flutter/testing/testing.h"
16
17namespace impeller {
18namespace testing {
19
20using namespace flutter;
21
22namespace {
23std::shared_ptr<DlVertices> MakeVertices(
25 std::vector<SkPoint> vertices,
26 std::vector<uint16_t> indices,
27 std::vector<SkPoint> texture_coordinates,
28 std::vector<DlColor> colors) {
30 {{texture_coordinates.size() > 0, colors.size() > 0}});
31 DlVertices::Builder builder(mode, vertices.size(), flags, indices.size());
32 if (colors.size() > 0) {
33 builder.store_colors(colors.data());
34 }
35 if (texture_coordinates.size() > 0) {
36 builder.store_texture_coordinates(texture_coordinates.data());
37 }
38 if (indices.size() > 0) {
39 builder.store_indices(indices.data());
40 }
41 builder.store_vertices(vertices.data());
42 return builder.build();
43}
44}; // namespace
45
46// Regression test for https://github.com/flutter/flutter/issues/135441 .
47TEST_P(AiksTest, VerticesGeometryUVPositionData) {
50 auto image =
51 DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
52 auto size = image->impeller_texture()->GetSize();
53
54 paint.setColorSource(std::make_shared<DlImageColorSource>(
56
57 std::vector<SkPoint> vertex_coordinates = {SkPoint::Make(0, 0),
58 SkPoint::Make(size.width, 0),
59 SkPoint::Make(0, size.height)};
60 auto vertices = MakeVertices(DlVertexMode::kTriangleStrip, vertex_coordinates,
61 {0, 1, 2}, {}, {});
62
63 builder.DrawVertices(vertices, DlBlendMode::kSrcOver, paint);
64 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
65}
66
67// Regression test for https://github.com/flutter/flutter/issues/135441 .
68TEST_P(AiksTest, VerticesGeometryUVPositionDataWithTranslate) {
71 auto image =
72 DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
73 auto size = image->impeller_texture()->GetSize();
74
76 matrix.setTranslateX(100);
77 matrix.setTranslateY(100);
78 paint.setColorSource(std::make_shared<DlImageColorSource>(
80 &matrix));
81
82 std::vector<SkPoint> positions = {SkPoint::Make(0, 0),
83 SkPoint::Make(size.width, 0),
84 SkPoint::Make(0, size.height)};
85 auto vertices =
86 MakeVertices(DlVertexMode::kTriangleStrip, positions, {0, 1, 2}, {}, {});
87
88 builder.DrawVertices(vertices, DlBlendMode::kSrcOver, paint);
89 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
90}
91
92// Regression test for https://github.com/flutter/flutter/issues/145707
93TEST_P(AiksTest, VerticesGeometryColorUVPositionData) {
96 auto image =
97 DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
98 auto size = image->impeller_texture()->GetSize();
99
100 paint.setColorSource(std::make_shared<DlImageColorSource>(
102
103 std::vector<SkPoint> positions = {
104 SkPoint::Make(0, 0), SkPoint::Make(size.width, 0),
105 SkPoint::Make(0, size.height), SkPoint::Make(size.width, 0),
106 SkPoint::Make(0, 0), SkPoint::Make(size.width, size.height),
107 };
108 std::vector<DlColor> colors = {
112 };
113
114 auto vertices =
115 MakeVertices(DlVertexMode::kTriangles, positions, {}, {}, colors);
116
117 builder.DrawVertices(vertices, DlBlendMode::kDstOver, paint);
118 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
119}
120
121TEST_P(AiksTest, VerticesGeometryColorUVPositionDataAdvancedBlend) {
124 auto image =
125 DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
126 auto size = image->impeller_texture()->GetSize();
127
128 paint.setColorSource(std::make_shared<DlImageColorSource>(
130
131 std::vector<SkPoint> positions = {
132 SkPoint::Make(0, 0), SkPoint::Make(size.width, 0),
133 SkPoint::Make(0, size.height), SkPoint::Make(size.width, 0),
134 SkPoint::Make(0, 0), SkPoint::Make(size.width, size.height),
135 };
136 std::vector<DlColor> colors = {
143 };
144
145 auto vertices =
146 MakeVertices(DlVertexMode::kTriangles, positions, {}, {}, colors);
147
148 builder.DrawVertices(vertices, DlBlendMode::kColorBurn, paint);
149 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
150}
151
152// Draw a hexagon using triangle fan
153TEST_P(AiksTest, CanConvertTriangleFanToTriangles) {
154 constexpr Scalar hexagon_radius = 125;
155 auto hex_start = Point(200.0, -hexagon_radius + 200.0);
156 auto center_to_flat = 1.73 / 2 * hexagon_radius;
157
158 // clang-format off
159 std::vector<SkPoint> vertices = {
160 SkPoint::Make(hex_start.x, hex_start.y),
161 SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 0.5 * hexagon_radius),
162 SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 1.5 * hexagon_radius),
163 SkPoint::Make(hex_start.x + center_to_flat, hex_start.y + 1.5 * hexagon_radius),
164 SkPoint::Make(hex_start.x, hex_start.y + 2 * hexagon_radius),
165 SkPoint::Make(hex_start.x, hex_start.y + 2 * hexagon_radius),
166 SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 1.5 * hexagon_radius),
167 SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 1.5 * hexagon_radius),
168 SkPoint::Make(hex_start.x - center_to_flat, hex_start.y + 0.5 * hexagon_radius)
169 };
170 // clang-format on
172 auto dl_vertices = flutter::DlVertices::Make(
173 flutter::DlVertexMode::kTriangleFan, vertices.size(), vertices.data(),
174 nullptr, nullptr);
176 builder.DrawVertices(dl_vertices, flutter::DlBlendMode::kSrcOver, paint);
177 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
178}
179
180TEST_P(AiksTest, DrawVerticesSolidColorTrianglesWithoutIndices) {
181 // Use negative coordinates and then scale the transform by -1, -1 to make
182 // sure coverage is taking the transform into account.
183 std::vector<SkPoint> positions = {SkPoint::Make(-100, -300),
184 SkPoint::Make(-200, -100),
185 SkPoint::Make(-300, -300)};
186 std::vector<flutter::DlColor> colors = {flutter::DlColor::kWhite(),
189
190 auto vertices = flutter::DlVertices::Make(
191 flutter::DlVertexMode::kTriangles, 3, positions.data(),
192 /*texture_coordinates=*/nullptr, colors.data());
193
196
197 paint.setColor(flutter::DlColor::kRed().modulateOpacity(0.5));
198 builder.Scale(-1, -1);
199 builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint);
200
201 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
202}
203
204TEST_P(AiksTest, DrawVerticesLinearGradientWithoutIndices) {
205 std::vector<SkPoint> positions = {SkPoint::Make(100, 300),
206 SkPoint::Make(200, 100),
207 SkPoint::Make(300, 300)};
208
209 auto vertices = flutter::DlVertices::Make(
210 flutter::DlVertexMode::kTriangles, 3, positions.data(),
211 /*texture_coordinates=*/nullptr, /*colors=*/nullptr);
212
213 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
215 const float stops[2] = {0.0, 1.0};
216
218 {100.0, 100.0}, {300.0, 300.0}, 2, colors.data(), stops,
220
223
224 paint.setColorSource(linear);
225 builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint);
226
227 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
228}
229
230TEST_P(AiksTest, DrawVerticesLinearGradientWithTextureCoordinates) {
231 std::vector<SkPoint> positions = {SkPoint::Make(100, 300),
232 SkPoint::Make(200, 100),
233 SkPoint::Make(300, 300)};
234 std::vector<SkPoint> texture_coordinates = {SkPoint::Make(300, 100),
235 SkPoint::Make(100, 200),
236 SkPoint::Make(300, 300)};
237
238 auto vertices = flutter::DlVertices::Make(
239 flutter::DlVertexMode::kTriangles, 3, positions.data(),
240 texture_coordinates.data(), /*colors=*/nullptr);
241
242 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
244 const float stops[2] = {0.0, 1.0};
245
247 {100.0, 100.0}, {300.0, 300.0}, 2, colors.data(), stops,
249
252
253 paint.setColorSource(linear);
254 builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint);
255
256 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
257}
258
259TEST_P(AiksTest, DrawVerticesImageSourceWithTextureCoordinates) {
260 auto texture = CreateTextureForFixture("embarcadero.jpg");
261 auto dl_image = DlImageImpeller::Make(texture);
262 std::vector<SkPoint> positions = {SkPoint::Make(100, 300),
263 SkPoint::Make(200, 100),
264 SkPoint::Make(300, 300)};
265 std::vector<SkPoint> texture_coordinates = {
266 SkPoint::Make(0, 0), SkPoint::Make(100, 200), SkPoint::Make(200, 100)};
267
268 auto vertices = flutter::DlVertices::Make(
269 flutter::DlVertexMode::kTriangles, 3, positions.data(),
270 texture_coordinates.data(), /*colors=*/nullptr);
271
274
275 auto image_source = flutter::DlImageColorSource(
277
278 paint.setColorSource(&image_source);
279 builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint);
280
281 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
282}
283
285 DrawVerticesImageSourceWithTextureCoordinatesAndColorBlending) {
286 auto texture = CreateTextureForFixture("embarcadero.jpg");
287 auto dl_image = DlImageImpeller::Make(texture);
288 std::vector<SkPoint> positions = {SkPoint::Make(100, 300),
289 SkPoint::Make(200, 100),
290 SkPoint::Make(300, 300)};
291 std::vector<flutter::DlColor> colors = {flutter::DlColor::kWhite(),
294 std::vector<SkPoint> texture_coordinates = {
295 SkPoint::Make(0, 0), SkPoint::Make(100, 200), SkPoint::Make(200, 100)};
296
297 auto vertices = flutter::DlVertices::Make(
298 flutter::DlVertexMode::kTriangles, 3, positions.data(),
299 texture_coordinates.data(), colors.data());
300
303
304 auto image_source = flutter::DlImageColorSource(
306
307 paint.setColorSource(&image_source);
308 builder.DrawVertices(vertices, flutter::DlBlendMode::kModulate, paint);
309
310 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
311}
312
313TEST_P(AiksTest, DrawVerticesSolidColorTrianglesWithIndices) {
314 std::vector<SkPoint> positions = {
315 SkPoint::Make(100, 300), SkPoint::Make(200, 100), SkPoint::Make(300, 300),
316 SkPoint::Make(200, 500)};
317 std::vector<uint16_t> indices = {0, 1, 2, 0, 2, 3};
318
319 auto vertices = flutter::DlVertices::Make(
320 flutter::DlVertexMode::kTriangles, positions.size(), positions.data(),
321 /*texture_coordinates=*/nullptr, /*colors=*/nullptr, indices.size(),
322 indices.data());
323
326
327 paint.setColor(flutter::DlColor::kRed());
328 builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint);
329
330 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
331}
332
333TEST_P(AiksTest, DrawVerticesPremultipliesColors) {
334 std::vector<SkPoint> positions = {
335 SkPoint::Make(100, 300), SkPoint::Make(200, 100), SkPoint::Make(300, 300),
336 SkPoint::Make(200, 500)};
338 std::vector<uint16_t> indices = {0, 1, 2, 0, 2, 3};
339 std::vector<flutter::DlColor> colors = {color, color, color, color};
340
341 auto vertices = flutter::DlVertices::Make(
342 flutter::DlVertexMode::kTriangles, positions.size(), positions.data(),
343 /*texture_coordinates=*/nullptr, colors.data(), indices.size(),
344 indices.data());
345
349 paint.setColor(flutter::DlColor::kRed());
350
351 builder.DrawRect(SkRect::MakeLTRB(0, 0, 400, 400), paint);
352 builder.DrawVertices(vertices, flutter::DlBlendMode::kDst, paint);
353
354 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
355}
356
357} // namespace testing
358} // namespace impeller
static std::shared_ptr< DlLinearGradientColorSource > MakeLinear(const SkPoint start_point, const SkPoint end_point, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
A utility class to build up a |DlVertices| object one set of data at a time.
Definition: dl_vertices.h:75
static std::shared_ptr< DlVertices > Make(DlVertexMode mode, int vertex_count, const SkPoint vertices[], const SkPoint texture_coordinates[], const DlColor colors[], int index_count=0, const uint16_t indices[]=nullptr)
Constructs a DlVector with compact inline storage for all of its required and optional lists of data.
Definition: dl_vertices.cc:39
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
const Paint & paint
Definition: color_source.cc:38
DlColor color
FlutterSemanticsFlag flags
FlTexture * texture
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< SkColor > colors
Definition: SkRecords.h:276
DlVertexMode
Defines the way in which the vertices of a DlVertices object are separated into triangles into which ...
Definition: dl_vertices.h:20
@ kTriangles
The vertices are taken 3 at a time to form a triangle.
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
@ kSrcOver
r = s + (1-sa)*d
TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer)
float Scalar
Definition: scalar.h:18
TPoint< Scalar > Point
Definition: point.h:322
std::shared_ptr< impeller::VerticesGeometry > MakeVertices(const flutter::DlVertices *vertices)
flutter::DlPaint DlPaint
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
constexpr DlColor modulateOpacity(float opacity) const
Definition: dl_color.h:76
static constexpr DlColor kWhite()
Definition: dl_color.h:23
static constexpr DlColor kBlue()
Definition: dl_color.h:26
constexpr DlColor withAlpha(uint8_t alpha) const
Definition: dl_color.h:63
static constexpr DlColor kRed()
Definition: dl_color.h:24
static constexpr DlColor kGreen()
Definition: dl_color.h:25
static constexpr DlColor kDarkGrey()
Definition: dl_color.h:30
flags to indicate/promise which of the optional texture coordinates or colors will be supplied during...
Definition: dl_vertices.h:80
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)