-
Notifications
You must be signed in to change notification settings - Fork 6k
/
entity_unittests.cc
2506 lines (2180 loc) · 96 KB
/
entity_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <algorithm>
#include <cstring>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "flutter/display_list/testing/dl_test_snippets.h"
#include "fml/logging.h"
#include "gtest/gtest.h"
#include "impeller/core/device_buffer.h"
#include "impeller/core/formats.h"
#include "impeller/core/host_buffer.h"
#include "impeller/core/raw_ptr.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/conical_gradient_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/entity/contents/filters/color_filter_contents.h"
#include "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
#include "impeller/entity/contents/linear_gradient_contents.h"
#include "impeller/entity/contents/radial_gradient_contents.h"
#include "impeller/entity/contents/runtime_effect_contents.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/solid_rrect_blur_contents.h"
#include "impeller/entity/contents/sweep_gradient_contents.h"
#include "impeller/entity/contents/text_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/entity/geometry/geometry.h"
#include "impeller/entity/geometry/point_field_geometry.h"
#include "impeller/entity/geometry/round_superellipse_geometry.h"
#include "impeller/entity/geometry/stroke_path_geometry.h"
#include "impeller/entity/geometry/superellipse_geometry.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/geometry_asserts.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/geometry/point.h"
#include "impeller/geometry/sigma.h"
#include "impeller/geometry/vector.h"
#include "impeller/playground/playground.h"
#include "impeller/playground/widgets.h"
#include "impeller/renderer/command.h"
#include "impeller/renderer/pipeline_descriptor.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/testing/mocks.h"
#include "impeller/renderer/vertex_buffer_builder.h"
#include "impeller/typographer/backends/skia/text_frame_skia.h"
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
#include "third_party/imgui/imgui.h"
#include "third_party/skia/include/core/SkTextBlob.h"
// TODO(zanderso): https://github.com/flutter/flutter/issues/127701
// NOLINTBEGIN(bugprone-unchecked-optional-access)
namespace impeller {
namespace testing {
using EntityTest = EntityPlayground;
INSTANTIATE_PLAYGROUND_SUITE(EntityTest);
TEST_P(EntityTest, CanCreateEntity) {
Entity entity;
ASSERT_TRUE(entity.GetTransform().IsIdentity());
}
TEST_P(EntityTest, FilterCoverageRespectsCropRect) {
auto image = CreateTextureForFixture("boston.jpg");
auto filter = ColorFilterContents::MakeBlend(BlendMode::kSoftLight,
FilterInput::Make({image}));
// Without the crop rect (default behavior).
{
auto actual = filter->GetCoverage({});
auto expected = Rect::MakeSize(image->GetSize());
ASSERT_TRUE(actual.has_value());
ASSERT_RECT_NEAR(actual.value(), expected);
}
// With the crop rect.
{
auto expected = Rect::MakeLTRB(50, 50, 100, 100);
filter->SetCoverageHint(expected);
auto actual = filter->GetCoverage({});
ASSERT_TRUE(actual.has_value());
ASSERT_RECT_NEAR(actual.value(), expected);
}
}
TEST_P(EntityTest, GeometryBoundsAreTransformed) {
auto geometry = Geometry::MakeRect(Rect::MakeXYWH(100, 100, 100, 100));
auto transform = Matrix::MakeScale({2.0, 2.0, 2.0});
ASSERT_RECT_NEAR(geometry->GetCoverage(transform).value(),
Rect::MakeXYWH(200, 200, 200, 200));
}
TEST_P(EntityTest, ThreeStrokesInOnePath) {
Path path = PathBuilder{}
.MoveTo({100, 100})
.LineTo({100, 200})
.MoveTo({100, 300})
.LineTo({100, 400})
.MoveTo({100, 500})
.LineTo({100, 600})
.TakePath();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
auto contents = std::make_unique<SolidColorContents>();
std::unique_ptr<Geometry> geom = Geometry::MakeStrokePath(path, 5.0);
contents->SetGeometry(geom.get());
contents->SetColor(Color::Red());
entity.SetContents(std::move(contents));
ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
}
TEST_P(EntityTest, StrokeWithTextureContents) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
Path path = PathBuilder{}
.MoveTo({100, 100})
.LineTo({100, 200})
.MoveTo({100, 300})
.LineTo({100, 400})
.MoveTo({100, 500})
.LineTo({100, 600})
.TakePath();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
auto contents = std::make_unique<TiledTextureContents>();
std::unique_ptr<Geometry> geom = Geometry::MakeStrokePath(path, 100.0);
contents->SetGeometry(geom.get());
contents->SetTexture(bridge);
contents->SetTileModes(Entity::TileMode::kClamp, Entity::TileMode::kClamp);
entity.SetContents(std::move(contents));
ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
}
TEST_P(EntityTest, TriangleInsideASquare) {
auto callback = [&](ContentContext& context, RenderPass& pass) {
Point offset(100, 100);
PlaygroundPoint point_a(Point(10, 10) + offset, 20, Color::White());
Point a = DrawPlaygroundPoint(point_a);
PlaygroundPoint point_b(Point(210, 10) + offset, 20, Color::White());
Point b = DrawPlaygroundPoint(point_b);
PlaygroundPoint point_c(Point(210, 210) + offset, 20, Color::White());
Point c = DrawPlaygroundPoint(point_c);
PlaygroundPoint point_d(Point(10, 210) + offset, 20, Color::White());
Point d = DrawPlaygroundPoint(point_d);
PlaygroundPoint point_e(Point(50, 50) + offset, 20, Color::White());
Point e = DrawPlaygroundPoint(point_e);
PlaygroundPoint point_f(Point(100, 50) + offset, 20, Color::White());
Point f = DrawPlaygroundPoint(point_f);
PlaygroundPoint point_g(Point(50, 150) + offset, 20, Color::White());
Point g = DrawPlaygroundPoint(point_g);
Path path = PathBuilder{}
.MoveTo(a)
.LineTo(b)
.LineTo(c)
.LineTo(d)
.Close()
.MoveTo(e)
.LineTo(f)
.LineTo(g)
.Close()
.TakePath();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
auto contents = std::make_unique<SolidColorContents>();
std::unique_ptr<Geometry> geom = Geometry::MakeStrokePath(path, 20.0);
contents->SetGeometry(geom.get());
contents->SetColor(Color::Red());
entity.SetContents(std::move(contents));
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, StrokeCapAndJoinTest) {
const Point padding(300, 250);
const Point margin(140, 180);
auto callback = [&](ContentContext& context, RenderPass& pass) {
// Slightly above sqrt(2) by default, so that right angles are just below
// the limit and acute angles are over the limit (causing them to get
// beveled).
static Scalar miter_limit = 1.41421357;
static Scalar width = 30;
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
{
ImGui::SliderFloat("Miter limit", &miter_limit, 0, 30);
ImGui::SliderFloat("Stroke width", &width, 0, 100);
if (ImGui::Button("Reset")) {
miter_limit = 1.41421357;
width = 30;
}
}
ImGui::End();
auto world_matrix = Matrix::MakeScale(GetContentScale());
auto render_path = [width = width, &context, &pass, &world_matrix](
const Path& path, Cap cap, Join join) {
auto contents = std::make_unique<SolidColorContents>();
std::unique_ptr<Geometry> geom =
Geometry::MakeStrokePath(path, width, miter_limit, cap, join);
contents->SetGeometry(geom.get());
contents->SetColor(Color::Red());
Entity entity;
entity.SetTransform(world_matrix);
entity.SetContents(std::move(contents));
auto coverage = entity.GetCoverage();
if (coverage.has_value()) {
auto bounds_contents = std::make_unique<SolidColorContents>();
std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(
PathBuilder{}.AddRect(entity.GetCoverage().value()).TakePath());
bounds_contents->SetGeometry(geom.get());
bounds_contents->SetColor(Color::Green().WithAlpha(0.5));
Entity bounds_entity;
bounds_entity.SetContents(std::move(bounds_contents));
bounds_entity.Render(context, pass);
}
entity.Render(context, pass);
};
const Point a_def(0, 0), b_def(0, 100), c_def(150, 0), d_def(150, -100),
e_def(75, 75);
const Scalar r = 30;
// Cap::kButt demo.
{
Point off = Point(0, 0) * padding + margin;
PlaygroundPoint point_a(off + a_def, r, Color::Black());
PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
PlaygroundPoint point_c(off + c_def, r, Color::Black());
PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kButt, Join::kBevel);
}
// Cap::kSquare demo.
{
Point off = Point(1, 0) * padding + margin;
PlaygroundPoint point_a(off + a_def, r, Color::Black());
PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
PlaygroundPoint point_c(off + c_def, r, Color::Black());
PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kSquare, Join::kBevel);
}
// Cap::kRound demo.
{
Point off = Point(2, 0) * padding + margin;
PlaygroundPoint point_a(off + a_def, r, Color::Black());
PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
PlaygroundPoint point_c(off + c_def, r, Color::Black());
PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kRound, Join::kBevel);
}
// Join::kBevel demo.
{
Point off = Point(0, 1) * padding + margin;
PlaygroundPoint point_a = PlaygroundPoint(off + a_def, r, Color::White());
PlaygroundPoint point_b = PlaygroundPoint(off + e_def, r, Color::White());
PlaygroundPoint point_c = PlaygroundPoint(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kBevel);
}
// Join::kMiter demo.
{
Point off = Point(1, 1) * padding + margin;
PlaygroundPoint point_a(off + a_def, r, Color::White());
PlaygroundPoint point_b(off + e_def, r, Color::White());
PlaygroundPoint point_c(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kMiter);
}
// Join::kRound demo.
{
Point off = Point(2, 1) * padding + margin;
PlaygroundPoint point_a(off + a_def, r, Color::White());
PlaygroundPoint point_b(off + e_def, r, Color::White());
PlaygroundPoint point_c(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kRound);
}
return true;
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, CubicCurveTest) {
// Compare with https://fiddle.skia.org/c/b3625f26122c9de7afe7794fcf25ead3
Path path =
PathBuilder{}
.MoveTo({237.164, 125.003})
.CubicCurveTo({236.709, 125.184}, {236.262, 125.358},
{235.81, 125.538})
.CubicCurveTo({235.413, 125.68}, {234.994, 125.832},
{234.592, 125.977})
.CubicCurveTo({234.592, 125.977}, {234.591, 125.977},
{234.59, 125.977})
.CubicCurveTo({222.206, 130.435}, {207.708, 135.753},
{192.381, 141.429})
.CubicCurveTo({162.77, 151.336}, {122.17, 156.894}, {84.1123, 160})
.Close()
.TakePath();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
auto contents = std::make_shared<SolidColorContents>();
contents->SetColor(Color::Red());
contents->SetGeometry(geom.get());
entity.SetContents(contents);
ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
}
TEST_P(EntityTest, CanDrawCorrectlyWithRotatedTransform) {
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
const char* input_axis[] = {"X", "Y", "Z"};
static int rotation_axis_index = 0;
static float rotation = 0;
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::SliderFloat("Rotation", &rotation, -kPi, kPi);
ImGui::Combo("Rotation Axis", &rotation_axis_index, input_axis,
sizeof(input_axis) / sizeof(char*));
Matrix rotation_matrix;
switch (rotation_axis_index) {
case 0:
rotation_matrix = Matrix::MakeRotationX(Radians(rotation));
break;
case 1:
rotation_matrix = Matrix::MakeRotationY(Radians(rotation));
break;
case 2:
rotation_matrix = Matrix::MakeRotationZ(Radians(rotation));
break;
default:
rotation_matrix = Matrix{};
break;
}
if (ImGui::Button("Reset")) {
rotation = 0;
}
ImGui::End();
Matrix current_transform =
Matrix::MakeScale(GetContentScale())
.MakeTranslation(
Vector3(Point(pass.GetRenderTargetSize().width / 2.0,
pass.GetRenderTargetSize().height / 2.0)));
Matrix result_transform = current_transform * rotation_matrix;
Path path =
PathBuilder{}.AddRect(Rect::MakeXYWH(-300, -400, 600, 800)).TakePath();
Entity entity;
entity.SetTransform(result_transform);
std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
auto contents = std::make_shared<SolidColorContents>();
contents->SetColor(Color::Red());
contents->SetGeometry(geom.get());
entity.SetContents(contents);
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, CubicCurveAndOverlapTest) {
// Compare with https://fiddle.skia.org/c/7a05a3e186c65a8dfb732f68020aae06
Path path =
PathBuilder{}
.MoveTo({359.934, 96.6335})
.CubicCurveTo({358.189, 96.7055}, {356.436, 96.7908},
{354.673, 96.8895})
.CubicCurveTo({354.571, 96.8953}, {354.469, 96.9016},
{354.367, 96.9075})
.CubicCurveTo({352.672, 97.0038}, {350.969, 97.113},
{349.259, 97.2355})
.CubicCurveTo({349.048, 97.2506}, {348.836, 97.2678},
{348.625, 97.2834})
.CubicCurveTo({347.019, 97.4014}, {345.407, 97.5299},
{343.789, 97.6722})
.CubicCurveTo({343.428, 97.704}, {343.065, 97.7402},
{342.703, 97.7734})
.CubicCurveTo({341.221, 97.9086}, {339.736, 98.0505},
{338.246, 98.207})
.CubicCurveTo({337.702, 98.2642}, {337.156, 98.3292},
{336.612, 98.3894})
.CubicCurveTo({335.284, 98.5356}, {333.956, 98.6837},
{332.623, 98.8476})
.CubicCurveTo({332.495, 98.8635}, {332.366, 98.8818},
{332.237, 98.8982})
.LineTo({332.237, 102.601})
.LineTo({321.778, 102.601})
.LineTo({321.778, 100.382})
.CubicCurveTo({321.572, 100.413}, {321.367, 100.442},
{321.161, 100.476})
.CubicCurveTo({319.22, 100.79}, {317.277, 101.123},
{315.332, 101.479})
.CubicCurveTo({315.322, 101.481}, {315.311, 101.482},
{315.301, 101.484})
.LineTo({310.017, 105.94})
.LineTo({309.779, 105.427})
.LineTo({314.403, 101.651})
.CubicCurveTo({314.391, 101.653}, {314.379, 101.656},
{314.368, 101.658})
.CubicCurveTo({312.528, 102.001}, {310.687, 102.366},
{308.846, 102.748})
.CubicCurveTo({307.85, 102.955}, {306.855, 103.182}, {305.859, 103.4})
.CubicCurveTo({305.048, 103.579}, {304.236, 103.75},
{303.425, 103.936})
.LineTo({299.105, 107.578})
.LineTo({298.867, 107.065})
.LineTo({302.394, 104.185})
.LineTo({302.412, 104.171})
.CubicCurveTo({301.388, 104.409}, {300.366, 104.67},
{299.344, 104.921})
.CubicCurveTo({298.618, 105.1}, {297.89, 105.269}, {297.165, 105.455})
.CubicCurveTo({295.262, 105.94}, {293.36, 106.445},
{291.462, 106.979})
.CubicCurveTo({291.132, 107.072}, {290.802, 107.163},
{290.471, 107.257})
.CubicCurveTo({289.463, 107.544}, {288.455, 107.839},
{287.449, 108.139})
.CubicCurveTo({286.476, 108.431}, {285.506, 108.73},
{284.536, 109.035})
.CubicCurveTo({283.674, 109.304}, {282.812, 109.579},
{281.952, 109.859})
.CubicCurveTo({281.177, 110.112}, {280.406, 110.377},
{279.633, 110.638})
.CubicCurveTo({278.458, 111.037}, {277.256, 111.449},
{276.803, 111.607})
.CubicCurveTo({276.76, 111.622}, {276.716, 111.637},
{276.672, 111.653})
.CubicCurveTo({275.017, 112.239}, {273.365, 112.836},
{271.721, 113.463})
.LineTo({271.717, 113.449})
.CubicCurveTo({271.496, 113.496}, {271.238, 113.559},
{270.963, 113.628})
.CubicCurveTo({270.893, 113.645}, {270.822, 113.663},
{270.748, 113.682})
.CubicCurveTo({270.468, 113.755}, {270.169, 113.834},
{269.839, 113.926})
.CubicCurveTo({269.789, 113.94}, {269.732, 113.957},
{269.681, 113.972})
.CubicCurveTo({269.391, 114.053}, {269.081, 114.143},
{268.756, 114.239})
.CubicCurveTo({268.628, 114.276}, {268.5, 114.314},
{268.367, 114.354})
.CubicCurveTo({268.172, 114.412}, {267.959, 114.478},
{267.752, 114.54})
.CubicCurveTo({263.349, 115.964}, {258.058, 117.695},
{253.564, 119.252})
.CubicCurveTo({253.556, 119.255}, {253.547, 119.258},
{253.538, 119.261})
.CubicCurveTo({251.844, 119.849}, {250.056, 120.474},
{248.189, 121.131})
.CubicCurveTo({248, 121.197}, {247.812, 121.264}, {247.621, 121.331})
.CubicCurveTo({247.079, 121.522}, {246.531, 121.715},
{245.975, 121.912})
.CubicCurveTo({245.554, 122.06}, {245.126, 122.212},
{244.698, 122.364})
.CubicCurveTo({244.071, 122.586}, {243.437, 122.811},
{242.794, 123.04})
.CubicCurveTo({242.189, 123.255}, {241.58, 123.472},
{240.961, 123.693})
.CubicCurveTo({240.659, 123.801}, {240.357, 123.909},
{240.052, 124.018})
.CubicCurveTo({239.12, 124.351}, {238.18, 124.687}, {237.22, 125.032})
.LineTo({237.164, 125.003})
.CubicCurveTo({236.709, 125.184}, {236.262, 125.358},
{235.81, 125.538})
.CubicCurveTo({235.413, 125.68}, {234.994, 125.832},
{234.592, 125.977})
.CubicCurveTo({234.592, 125.977}, {234.591, 125.977},
{234.59, 125.977})
.CubicCurveTo({222.206, 130.435}, {207.708, 135.753},
{192.381, 141.429})
.CubicCurveTo({162.77, 151.336}, {122.17, 156.894}, {84.1123, 160})
.LineTo({360, 160})
.LineTo({360, 119.256})
.LineTo({360, 106.332})
.LineTo({360, 96.6307})
.CubicCurveTo({359.978, 96.6317}, {359.956, 96.6326},
{359.934, 96.6335})
.Close()
.MoveTo({337.336, 124.143})
.CubicCurveTo({337.274, 122.359}, {338.903, 121.511},
{338.903, 121.511})
.CubicCurveTo({338.903, 121.511}, {338.96, 123.303},
{337.336, 124.143})
.Close()
.MoveTo({340.082, 121.849})
.CubicCurveTo({340.074, 121.917}, {340.062, 121.992},
{340.046, 122.075})
.CubicCurveTo({340.039, 122.109}, {340.031, 122.142},
{340.023, 122.177})
.CubicCurveTo({340.005, 122.26}, {339.98, 122.346},
{339.952, 122.437})
.CubicCurveTo({339.941, 122.473}, {339.931, 122.507},
{339.918, 122.544})
.CubicCurveTo({339.873, 122.672}, {339.819, 122.804},
{339.75, 122.938})
.CubicCurveTo({339.747, 122.944}, {339.743, 122.949},
{339.74, 122.955})
.CubicCurveTo({339.674, 123.08}, {339.593, 123.205},
{339.501, 123.328})
.CubicCurveTo({339.473, 123.366}, {339.441, 123.401},
{339.41, 123.438})
.CubicCurveTo({339.332, 123.534}, {339.243, 123.625},
{339.145, 123.714})
.CubicCurveTo({339.105, 123.75}, {339.068, 123.786},
{339.025, 123.821})
.CubicCurveTo({338.881, 123.937}, {338.724, 124.048},
{338.539, 124.143})
.CubicCurveTo({338.532, 123.959}, {338.554, 123.79},
{338.58, 123.626})
.CubicCurveTo({338.58, 123.625}, {338.58, 123.625}, {338.58, 123.625})
.CubicCurveTo({338.607, 123.455}, {338.65, 123.299},
{338.704, 123.151})
.CubicCurveTo({338.708, 123.14}, {338.71, 123.127},
{338.714, 123.117})
.CubicCurveTo({338.769, 122.971}, {338.833, 122.838},
{338.905, 122.712})
.CubicCurveTo({338.911, 122.702}, {338.916, 122.69200000000001},
{338.922, 122.682})
.CubicCurveTo({338.996, 122.557}, {339.072, 122.444},
{339.155, 122.34})
.CubicCurveTo({339.161, 122.333}, {339.166, 122.326},
{339.172, 122.319})
.CubicCurveTo({339.256, 122.215}, {339.339, 122.12},
{339.425, 122.037})
.CubicCurveTo({339.428, 122.033}, {339.431, 122.03},
{339.435, 122.027})
.CubicCurveTo({339.785, 121.687}, {340.106, 121.511},
{340.106, 121.511})
.CubicCurveTo({340.106, 121.511}, {340.107, 121.645},
{340.082, 121.849})
.Close()
.MoveTo({340.678, 113.245})
.CubicCurveTo({340.594, 113.488}, {340.356, 113.655},
{340.135, 113.775})
.CubicCurveTo({339.817, 113.948}, {339.465, 114.059},
{339.115, 114.151})
.CubicCurveTo({338.251, 114.379}, {337.34, 114.516},
{336.448, 114.516})
.CubicCurveTo({335.761, 114.516}, {335.072, 114.527},
{334.384, 114.513})
.CubicCurveTo({334.125, 114.508}, {333.862, 114.462},
{333.605, 114.424})
.CubicCurveTo({332.865, 114.318}, {332.096, 114.184},
{331.41, 113.883})
.CubicCurveTo({330.979, 113.695}, {330.442, 113.34},
{330.672, 112.813})
.CubicCurveTo({331.135, 111.755}, {333.219, 112.946},
{334.526, 113.833})
.CubicCurveTo({334.54, 113.816}, {334.554, 113.8}, {334.569, 113.784})
.CubicCurveTo({333.38, 112.708}, {331.749, 110.985},
{332.76, 110.402})
.CubicCurveTo({333.769, 109.82}, {334.713, 111.93},
{335.228, 113.395})
.CubicCurveTo({334.915, 111.889}, {334.59, 109.636},
{335.661, 109.592})
.CubicCurveTo({336.733, 109.636}, {336.408, 111.889},
{336.07, 113.389})
.CubicCurveTo({336.609, 111.93}, {337.553, 109.82},
{338.563, 110.402})
.CubicCurveTo({339.574, 110.984}, {337.942, 112.708},
{336.753, 113.784})
.CubicCurveTo({336.768, 113.8}, {336.782, 113.816},
{336.796, 113.833})
.CubicCurveTo({338.104, 112.946}, {340.187, 111.755},
{340.65, 112.813})
.CubicCurveTo({340.71, 112.95}, {340.728, 113.102},
{340.678, 113.245})
.Close()
.MoveTo({346.357, 106.771})
.CubicCurveTo({346.295, 104.987}, {347.924, 104.139},
{347.924, 104.139})
.CubicCurveTo({347.924, 104.139}, {347.982, 105.931},
{346.357, 106.771})
.Close()
.MoveTo({347.56, 106.771})
.CubicCurveTo({347.498, 104.987}, {349.127, 104.139},
{349.127, 104.139})
.CubicCurveTo({349.127, 104.139}, {349.185, 105.931},
{347.56, 106.771})
.Close()
.TakePath();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
auto contents = std::make_shared<SolidColorContents>();
contents->SetColor(Color::Red());
contents->SetGeometry(geom.get());
entity.SetContents(contents);
ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
}
TEST_P(EntityTest, SolidColorContentsStrokeSetStrokeCapsAndJoins) {
{
auto geometry = Geometry::MakeStrokePath(Path{});
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
// Defaults.
ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kButt);
ASSERT_EQ(path_geometry->GetStrokeJoin(), Join::kMiter);
}
{
auto geometry = Geometry::MakeStrokePath(Path{}, 1.0, 4.0, Cap::kSquare);
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kSquare);
}
{
auto geometry = Geometry::MakeStrokePath(Path{}, 1.0, 4.0, Cap::kRound);
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kRound);
}
}
TEST_P(EntityTest, SolidColorContentsStrokeSetMiterLimit) {
{
auto geometry = Geometry::MakeStrokePath(Path{});
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 4);
}
{
auto geometry = Geometry::MakeStrokePath(Path{}, 1.0,
/*miter_limit=*/8.0);
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 8);
}
{
auto geometry = Geometry::MakeStrokePath(Path{}, 1.0,
/*miter_limit=*/-1.0);
auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 4);
}
}
TEST_P(EntityTest, BlendingModeOptions) {
std::vector<const char*> blend_mode_names;
std::vector<BlendMode> blend_mode_values;
{
// Force an exhausiveness check with a switch. When adding blend modes,
// update this switch with a new name/value to make it selectable in the
// test GUI.
const BlendMode b{};
static_assert(b == BlendMode::kClear); // Ensure the first item in
// the switch is the first
// item in the enum.
static_assert(Entity::kLastPipelineBlendMode == BlendMode::kModulate);
switch (b) {
case BlendMode::kClear:
blend_mode_names.push_back("Clear");
blend_mode_values.push_back(BlendMode::kClear);
case BlendMode::kSource:
blend_mode_names.push_back("Source");
blend_mode_values.push_back(BlendMode::kSource);
case BlendMode::kDestination:
blend_mode_names.push_back("Destination");
blend_mode_values.push_back(BlendMode::kDestination);
case BlendMode::kSourceOver:
blend_mode_names.push_back("SourceOver");
blend_mode_values.push_back(BlendMode::kSourceOver);
case BlendMode::kDestinationOver:
blend_mode_names.push_back("DestinationOver");
blend_mode_values.push_back(BlendMode::kDestinationOver);
case BlendMode::kSourceIn:
blend_mode_names.push_back("SourceIn");
blend_mode_values.push_back(BlendMode::kSourceIn);
case BlendMode::kDestinationIn:
blend_mode_names.push_back("DestinationIn");
blend_mode_values.push_back(BlendMode::kDestinationIn);
case BlendMode::kSourceOut:
blend_mode_names.push_back("SourceOut");
blend_mode_values.push_back(BlendMode::kSourceOut);
case BlendMode::kDestinationOut:
blend_mode_names.push_back("DestinationOut");
blend_mode_values.push_back(BlendMode::kDestinationOut);
case BlendMode::kSourceATop:
blend_mode_names.push_back("SourceATop");
blend_mode_values.push_back(BlendMode::kSourceATop);
case BlendMode::kDestinationATop:
blend_mode_names.push_back("DestinationATop");
blend_mode_values.push_back(BlendMode::kDestinationATop);
case BlendMode::kXor:
blend_mode_names.push_back("Xor");
blend_mode_values.push_back(BlendMode::kXor);
case BlendMode::kPlus:
blend_mode_names.push_back("Plus");
blend_mode_values.push_back(BlendMode::kPlus);
case BlendMode::kModulate:
blend_mode_names.push_back("Modulate");
blend_mode_values.push_back(BlendMode::kModulate);
};
}
auto callback = [&](ContentContext& context, RenderPass& pass) {
auto world_matrix = Matrix::MakeScale(GetContentScale());
auto draw_rect = [&context, &pass, &world_matrix](
Rect rect, Color color, BlendMode blend_mode) -> bool {
using VS = SolidFillPipeline::VertexShader;
using FS = SolidFillPipeline::FragmentShader;
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
{
auto r = rect.GetLTRB();
vtx_builder.AddVertices({
{Point(r[0], r[1])},
{Point(r[2], r[1])},
{Point(r[2], r[3])},
{Point(r[0], r[1])},
{Point(r[2], r[3])},
{Point(r[0], r[3])},
});
}
pass.SetCommandLabel("Blended Rectangle");
auto options = OptionsFromPass(pass);
options.blend_mode = blend_mode;
options.primitive_type = PrimitiveType::kTriangle;
pass.SetPipeline(context.GetSolidFillPipeline(options));
pass.SetVertexBuffer(
vtx_builder.CreateVertexBuffer(context.GetTransientsBuffer()));
VS::FrameInfo frame_info;
frame_info.mvp = pass.GetOrthographicTransform() * world_matrix;
VS::BindFrameInfo(
pass, context.GetTransientsBuffer().EmplaceUniform(frame_info));
FS::FragInfo frag_info;
frag_info.color = color.Premultiply();
FS::BindFragInfo(
pass, context.GetTransientsBuffer().EmplaceUniform(frame_info));
return pass.Draw().ok();
};
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
static Color color1(1, 0, 0, 0.5), color2(0, 1, 0, 0.5);
ImGui::ColorEdit4("Color 1", reinterpret_cast<float*>(&color1));
ImGui::ColorEdit4("Color 2", reinterpret_cast<float*>(&color2));
static int current_blend_index = 3;
ImGui::ListBox("Blending mode", ¤t_blend_index,
blend_mode_names.data(), blend_mode_names.size());
ImGui::End();
BlendMode selected_mode = blend_mode_values[current_blend_index];
Point a, b, c, d;
PlaygroundPoint point_a(Point(400, 100), 20, Color::White());
PlaygroundPoint point_b(Point(200, 300), 20, Color::White());
std::tie(a, b) = DrawPlaygroundLine(point_a, point_b);
PlaygroundPoint point_c(Point(470, 190), 20, Color::White());
PlaygroundPoint point_d(Point(270, 390), 20, Color::White());
std::tie(c, d) = DrawPlaygroundLine(point_c, point_d);
bool result = true;
result = result &&
draw_rect(Rect::MakeXYWH(0, 0, pass.GetRenderTargetSize().width,
pass.GetRenderTargetSize().height),
Color(), BlendMode::kClear);
result = result && draw_rect(Rect::MakeLTRB(a.x, a.y, b.x, b.y), color1,
BlendMode::kSourceOver);
result = result && draw_rect(Rect::MakeLTRB(c.x, c.y, d.x, d.y), color2,
selected_mode);
return result;
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, BezierCircleScaled) {
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
static float scale = 20;
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::SliderFloat("Scale", &scale, 1, 100);
ImGui::End();
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()));
auto path = PathBuilder{}
.MoveTo({97.325, 34.818})
.CubicCurveTo({98.50862885295136, 34.81812293973836},
{99.46822048142015, 33.85863261475589},
{99.46822048142015, 32.67499810206613})
.CubicCurveTo({99.46822048142015, 31.491363589376355},
{98.50862885295136, 30.53187326439389},
{97.32499434685802, 30.531998226542708})
.CubicCurveTo({96.14153655073771, 30.532123170035373},
{95.18222070648729, 31.491540299350355},
{95.18222070648729, 32.67499810206613})
.CubicCurveTo({95.18222070648729, 33.85845590478189},
{96.14153655073771, 34.81787303409686},
{97.32499434685802, 34.81799797758954})
.Close()
.TakePath();
entity.SetTransform(
Matrix::MakeScale({scale, scale, 1.0}).Translate({-90, -20, 0}));
std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
auto contents = std::make_shared<SolidColorContents>();
contents->SetColor(Color::Red());
contents->SetGeometry(geom.get());
entity.SetContents(contents);
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, Filters) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
auto kalimba = CreateTextureForFixture("kalimba.jpg");
ASSERT_TRUE(bridge && boston && kalimba);
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
auto fi_bridge = FilterInput::Make(bridge);
auto fi_boston = FilterInput::Make(boston);
auto fi_kalimba = FilterInput::Make(kalimba);
std::shared_ptr<FilterContents> blend0 = ColorFilterContents::MakeBlend(
BlendMode::kModulate, {fi_kalimba, fi_boston});
auto blend1 = ColorFilterContents::MakeBlend(
BlendMode::kScreen,
{FilterInput::Make(blend0), fi_bridge, fi_bridge, fi_bridge});
Entity entity;
entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation({500, 300}) *
Matrix::MakeScale(Vector2{0.5, 0.5}));
entity.SetContents(blend1);
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, GaussianBlurFilter) {
auto boston =
CreateTextureForFixture("boston.jpg", /*enable_mipmapping=*/true);
ASSERT_TRUE(boston);
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
const char* input_type_names[] = {"Texture", "Solid Color"};
const char* blur_type_names[] = {"Image blur", "Mask blur"};
const char* pass_variation_names[] = {"New"};
const char* blur_style_names[] = {"Normal", "Solid", "Outer", "Inner"};
const char* tile_mode_names[] = {"Clamp", "Repeat", "Mirror", "Decal"};
const FilterContents::BlurStyle blur_styles[] = {
FilterContents::BlurStyle::kNormal, FilterContents::BlurStyle::kSolid,
FilterContents::BlurStyle::kOuter, FilterContents::BlurStyle::kInner};
const Entity::TileMode tile_modes[] = {
Entity::TileMode::kClamp, Entity::TileMode::kRepeat,
Entity::TileMode::kMirror, Entity::TileMode::kDecal};
// UI state.
static int selected_input_type = 0;
static Color input_color = Color::Black();
static int selected_blur_type = 0;
static int selected_pass_variation = 0;
static bool combined_sigma = false;
static float blur_amount_coarse[2] = {0, 0};
static float blur_amount_fine[2] = {10, 10};
static int selected_blur_style = 0;
static int selected_tile_mode = 3;
static Color cover_color(1, 0, 0, 0.2);
static Color bounds_color(0, 1, 0, 0.1);
static float offset[2] = {500, 400};
static float rotation = 0;
static float scale[2] = {0.65, 0.65};
static float skew[2] = {0, 0};
static float path_rect[4] = {0, 0,
static_cast<float>(boston->GetSize().width),
static_cast<float>(boston->GetSize().height)};
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
{
ImGui::Combo("Input type", &selected_input_type, input_type_names,
sizeof(input_type_names) / sizeof(char*));
if (selected_input_type == 0) {
ImGui::SliderFloat("Input opacity", &input_color.alpha, 0, 1);
} else {
ImGui::ColorEdit4("Input color",
reinterpret_cast<float*>(&input_color));
}
ImGui::Combo("Blur type", &selected_blur_type, blur_type_names,
sizeof(blur_type_names) / sizeof(char*));
if (selected_blur_type == 0) {
ImGui::Combo("Pass variation", &selected_pass_variation,
pass_variation_names,
sizeof(pass_variation_names) / sizeof(char*));
}
ImGui::Checkbox("Combined sigma", &combined_sigma);
if (combined_sigma) {
ImGui::SliderFloat("Sigma (coarse)", blur_amount_coarse, 0, 1000);
ImGui::SliderFloat("Sigma (fine)", blur_amount_fine, 0, 10);
blur_amount_coarse[1] = blur_amount_coarse[0];
blur_amount_fine[1] = blur_amount_fine[0];
} else {
ImGui::SliderFloat2("Sigma (coarse)", blur_amount_coarse, 0, 1000);
ImGui::SliderFloat2("Sigma (fine)", blur_amount_fine, 0, 10);
}
ImGui::Combo("Blur style", &selected_blur_style, blur_style_names,
sizeof(blur_style_names) / sizeof(char*));
ImGui::Combo("Tile mode", &selected_tile_mode, tile_mode_names,
sizeof(tile_mode_names) / sizeof(char*));
ImGui::ColorEdit4("Cover color", reinterpret_cast<float*>(&cover_color));
ImGui::ColorEdit4("Bounds color ",
reinterpret_cast<float*>(&bounds_color));
ImGui::SliderFloat2("Translation", offset, 0,
pass.GetRenderTargetSize().width);
ImGui::SliderFloat("Rotation", &rotation, 0, kPi * 2);
ImGui::SliderFloat2("Scale", scale, 0, 3);
ImGui::SliderFloat2("Skew", skew, -3, 3);
ImGui::SliderFloat4("Path XYWH", path_rect, -1000, 1000);
}
ImGui::End();
auto blur_sigma_x = Sigma{blur_amount_coarse[0] + blur_amount_fine[0]};
auto blur_sigma_y = Sigma{blur_amount_coarse[1] + blur_amount_fine[1]};
std::shared_ptr<Contents> input;
Size input_size;
auto input_rect =
Rect::MakeXYWH(path_rect[0], path_rect[1], path_rect[2], path_rect[3]);
std::unique_ptr<Geometry> solid_color_input;
if (selected_input_type == 0) {
auto texture = std::make_shared<TextureContents>();
texture->SetSourceRect(Rect::MakeSize(boston->GetSize()));
texture->SetDestinationRect(input_rect);
texture->SetTexture(boston);
texture->SetOpacity(input_color.alpha);
input = texture;
input_size = input_rect.GetSize();
} else {
auto fill = std::make_shared<SolidColorContents>();
fill->SetColor(input_color);