forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_unittest.cc
3230 lines (2672 loc) · 116 KB
/
layer_unittest.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 (c) 2012 The Chromium 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 "ui/compositor/layer.h"
#include <stddef.h>
#include <memory>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "cc/animation/animation.h"
#include "cc/animation/animation_events.h"
#include "cc/animation/animation_host.h"
#include "cc/animation/keyframe_effect.h"
#include "cc/layers/layer.h"
#include "cc/layers/mirror_layer.h"
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/common/surfaces/surface_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "ui/compositor/compositor_observer.h"
#include "ui/compositor/dip_util.h"
#include "ui/compositor/layer_animation_element.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/paint_context.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/compositor/test/test_compositor_host.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/compositor/test/test_layers.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/interpolated_transform.h"
#include "ui/gfx/skia_util.h"
using cc::MatchesPNGFile;
using cc::WritePNGFile;
namespace ui {
namespace {
// There are three test classes in here that configure the Compositor and
// Layer's slightly differently:
// - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
// is typically the base class you want to use.
// - LayerWithDelegateTest uses LayerDelegate on the delegates.
// - LayerWithRealCompositorTest when a real compositor is required for testing.
// - Slow because they bring up a window and run the real compositor. This
// is typically not what you want.
class ColoredLayer : public Layer, public LayerDelegate {
public:
explicit ColoredLayer(SkColor color)
: Layer(LAYER_TEXTURED),
color_(color) {
set_delegate(this);
}
~ColoredLayer() override {}
// Overridden from LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override {
ui::PaintRecorder recorder(context, size());
recorder.canvas()->DrawColor(color_);
}
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {}
private:
SkColor color_;
};
// Param specifies whether to use SkiaRenderer or not
class LayerWithRealCompositorTest : public testing::TestWithParam<bool> {
public:
LayerWithRealCompositorTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {
gfx::FontList::SetDefaultFontDescription("Segoe UI, 15px");
}
~LayerWithRealCompositorTest() override {}
// Overridden from testing::Test:
void SetUp() override {
ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_));
test_data_dir_ = test_data_dir_.Append(FILE_PATH_LITERAL("ui"))
.Append(FILE_PATH_LITERAL("gfx"))
.Append(FILE_PATH_LITERAL("test"))
.Append(FILE_PATH_LITERAL("data"))
.Append(FILE_PATH_LITERAL("compositor"));
ASSERT_TRUE(base::PathExists(test_data_dir_));
const bool enable_pixel_output = true;
context_factories_ =
std::make_unique<TestContextFactories>(enable_pixel_output, GetParam());
const gfx::Rect host_bounds(10, 10, 500, 500);
compositor_host_.reset(TestCompositorHost::Create(
host_bounds, context_factories_->GetContextFactory()));
compositor_host_->Show();
}
void TearDown() override {
ResetCompositor();
context_factories_.reset();
}
Compositor* GetCompositor() { return compositor_host_->GetCompositor(); }
void ResetCompositor() {
compositor_host_.reset();
}
std::unique_ptr<Layer> CreateLayer(LayerType type) {
return std::make_unique<Layer>(type);
}
std::unique_ptr<Layer> CreateColorLayer(SkColor color,
const gfx::Rect& bounds) {
auto layer = std::make_unique<ColoredLayer>(color);
layer->SetBounds(bounds);
return layer;
}
std::unique_ptr<Layer> CreateNoTextureLayer(const gfx::Rect& bounds) {
std::unique_ptr<Layer> layer = CreateLayer(LAYER_NOT_DRAWN);
layer->SetBounds(bounds);
return layer;
}
void DrawTree(Layer* root) {
GetCompositor()->SetRootLayer(root);
GetCompositor()->ScheduleDraw();
WaitForSwap();
}
void ReadPixels(SkBitmap* bitmap) {
ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
}
void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) {
scoped_refptr<ReadbackHolder> holder(new ReadbackHolder);
std::unique_ptr<viz::CopyOutputRequest> request =
std::make_unique<viz::CopyOutputRequest>(
viz::CopyOutputRequest::ResultFormat::RGBA_BITMAP,
base::BindOnce(&ReadbackHolder::OutputRequestCallback, holder));
request->set_area(source_rect);
GetCompositor()->root_layer()->RequestCopyOfOutput(std::move(request));
// Wait for copy response. This needs to wait as the compositor could
// be in the middle of a draw right now, and the commit with the
// copy output request may not be done on the first draw.
for (int i = 0; i < 2; i++) {
GetCompositor()->ScheduleFullRedraw();
WaitForDraw();
}
// Waits for the callback to finish run and return result.
holder->WaitForReadback();
*bitmap = holder->result();
}
void WaitForDraw() {
ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor());
}
void WaitForSwap() {
ui::DrawWaiterForTest::WaitForCompositingEnded(GetCompositor());
}
void WaitForCommit() {
ui::DrawWaiterForTest::WaitForCommit(GetCompositor());
}
// Invalidates the entire contents of the layer.
void SchedulePaintForLayer(Layer* layer) {
layer->SchedulePaint(
gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
}
const base::FilePath& test_data_dir() const { return test_data_dir_; }
private:
class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> {
public:
ReadbackHolder() : run_loop_(std::make_unique<base::RunLoop>()) {}
void OutputRequestCallback(std::unique_ptr<viz::CopyOutputResult> result) {
if (result->IsEmpty())
result_.reset();
else
result_ = std::make_unique<SkBitmap>(result->AsSkBitmap());
run_loop_->Quit();
}
void WaitForReadback() { run_loop_->Run(); }
const SkBitmap& result() const { return *result_; }
private:
friend class base::RefCountedThreadSafe<ReadbackHolder>;
virtual ~ReadbackHolder() {}
std::unique_ptr<SkBitmap> result_;
std::unique_ptr<base::RunLoop> run_loop_;
};
base::test::TaskEnvironment task_environment_;
std::unique_ptr<TestContextFactories> context_factories_;
std::unique_ptr<TestCompositorHost> compositor_host_;
// The root directory for test files.
base::FilePath test_data_dir_;
DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
};
// LayerDelegate that paints colors to the layer.
class TestLayerDelegate : public LayerDelegate {
public:
TestLayerDelegate() { reset(); }
~TestLayerDelegate() override {}
void AddColor(SkColor color) {
colors_.push_back(color);
}
int color_index() const { return color_index_; }
float device_scale_factor() const {
return device_scale_factor_;
}
void set_layer_bounds(const gfx::Rect& layer_bounds) {
layer_bounds_ = layer_bounds;
}
// Overridden from LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override {
ui::PaintRecorder recorder(context, layer_bounds_.size());
recorder.canvas()->DrawColor(colors_[color_index_]);
color_index_ = (color_index_ + 1) % static_cast<int>(colors_.size());
}
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {
device_scale_factor_ = new_device_scale_factor;
}
MOCK_METHOD2(OnLayerBoundsChanged,
void(const gfx::Rect&, PropertyChangeReason));
MOCK_METHOD2(OnLayerTransformed,
void(const gfx::Transform&, PropertyChangeReason));
MOCK_METHOD1(OnLayerOpacityChanged, void(PropertyChangeReason));
MOCK_METHOD0(OnLayerAlphaShapeChanged, void());
void reset() {
color_index_ = 0;
device_scale_factor_ = 0.0f;
}
private:
std::vector<SkColor> colors_;
int color_index_;
float device_scale_factor_;
gfx::Rect layer_bounds_;
DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate);
};
// LayerDelegate that verifies that a layer was asked to update its canvas.
class DrawTreeLayerDelegate : public LayerDelegate {
public:
explicit DrawTreeLayerDelegate(const gfx::Rect& layer_bounds)
: painted_(false), layer_bounds_(layer_bounds) {}
~DrawTreeLayerDelegate() override = default;
void Reset() {
painted_ = false;
}
bool painted() const { return painted_; }
private:
// Overridden from LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override {
painted_ = true;
ui::PaintRecorder recorder(context, layer_bounds_.size());
recorder.canvas()->DrawColor(SK_ColorWHITE);
}
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {}
bool painted_;
const gfx::Rect layer_bounds_;
DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate);
};
// The simplest possible layer delegate. Does nothing.
class NullLayerDelegate : public LayerDelegate {
public:
NullLayerDelegate() {}
~NullLayerDelegate() override {}
gfx::Rect invalidation() const { return invalidation_; }
private:
gfx::Rect invalidation_;
// Overridden from LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override {
invalidation_ = context.InvalidationForTesting();
}
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {}
DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate);
};
// Remembers if it has been notified.
class TestCompositorObserver : public CompositorObserver {
public:
TestCompositorObserver() = default;
bool committed() const { return committed_; }
bool notified() const { return started_ && ended_; }
void Reset() {
committed_ = false;
started_ = false;
ended_ = false;
}
private:
void OnCompositingDidCommit(Compositor* compositor) override {
committed_ = true;
}
void OnCompositingStarted(Compositor* compositor,
base::TimeTicks start_time) override {
started_ = true;
}
void OnCompositingEnded(Compositor* compositor) override { ended_ = true; }
bool committed_ = false;
bool started_ = false;
bool ended_ = false;
DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver);
};
class TestCompositorAnimationObserver : public CompositorAnimationObserver {
public:
explicit TestCompositorAnimationObserver(ui::Compositor* compositor)
: compositor_(compositor),
animation_step_count_(0),
shutdown_(false) {
DCHECK(compositor_);
compositor_->AddAnimationObserver(this);
}
~TestCompositorAnimationObserver() override {
if (compositor_)
compositor_->RemoveAnimationObserver(this);
}
size_t animation_step_count() const { return animation_step_count_; }
bool shutdown() const { return shutdown_; }
private:
void OnAnimationStep(base::TimeTicks timestamp) override {
++animation_step_count_;
}
void OnCompositingShuttingDown(Compositor* compositor) override {
DCHECK_EQ(compositor_, compositor);
compositor_->RemoveAnimationObserver(this);
compositor_ = nullptr;
shutdown_ = true;
}
ui::Compositor* compositor_;
size_t animation_step_count_;
bool shutdown_;
DISALLOW_COPY_AND_ASSIGN(TestCompositorAnimationObserver);
};
// An animation observer that invokes a callback when the animation ends.
class TestCallbackAnimationObserver : public ImplicitAnimationObserver {
public:
TestCallbackAnimationObserver() = default;
void SetCallback(base::OnceClosure callback) {
callback_ = std::move(callback);
}
// ui::ImplicitAnimationObserver overrides:
void OnImplicitAnimationsCompleted() override {}
void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
if (callback_)
std::move(callback_).Run();
}
private:
base::OnceClosure callback_;
};
} // namespace
INSTANTIATE_TEST_SUITE_P(All, LayerWithRealCompositorTest, ::testing::Bool());
TEST_P(LayerWithRealCompositorTest, Draw) {
std::unique_ptr<Layer> layer =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 50, 50));
DrawTree(layer.get());
}
// Create this hierarchy:
// L1 - red
// +-- L2 - blue
// | +-- L3 - yellow
// +-- L4 - magenta
//
TEST_P(LayerWithRealCompositorTest, Hierarchy) {
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> l2 =
CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350));
std::unique_ptr<Layer> l3 =
CreateColorLayer(SK_ColorYELLOW, gfx::Rect(5, 5, 25, 25));
std::unique_ptr<Layer> l4 =
CreateColorLayer(SK_ColorMAGENTA, gfx::Rect(300, 300, 100, 100));
l1->Add(l2.get());
l1->Add(l4.get());
l2->Add(l3.get());
DrawTree(l1.get());
}
class LayerWithDelegateTest : public testing::Test {
public:
LayerWithDelegateTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}
~LayerWithDelegateTest() override {}
// Overridden from testing::Test:
void SetUp() override {
const bool enable_pixel_output = false;
context_factories_ =
std::make_unique<TestContextFactories>(enable_pixel_output);
const gfx::Rect host_bounds(1000, 1000);
compositor_host_.reset(TestCompositorHost::Create(
host_bounds, context_factories_->GetContextFactory()));
compositor_host_->Show();
}
void TearDown() override {
compositor_host_.reset();
context_factories_.reset();
}
Compositor* compositor() { return compositor_host_->GetCompositor(); }
virtual std::unique_ptr<Layer> CreateLayer(LayerType type) {
return std::make_unique<Layer>(type);
}
std::unique_ptr<Layer> CreateColorLayer(SkColor color,
const gfx::Rect& bounds) {
auto layer = std::make_unique<ColoredLayer>(color);
layer->SetBounds(bounds);
return layer;
}
virtual std::unique_ptr<Layer> CreateNoTextureLayer(const gfx::Rect& bounds) {
std::unique_ptr<Layer> layer = CreateLayer(LAYER_NOT_DRAWN);
layer->SetBounds(bounds);
return layer;
}
void DrawTree(Layer* root) {
compositor()->SetRootLayer(root);
Draw();
}
// Invalidates the entire contents of the layer.
void SchedulePaintForLayer(Layer* layer) {
layer->SchedulePaint(
gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
}
// Invokes DrawTree on the compositor.
void Draw() {
compositor()->ScheduleDraw();
WaitForDraw();
}
void WaitForDraw() {
DrawWaiterForTest::WaitForCompositingStarted(compositor());
}
void WaitForCommit() {
DrawWaiterForTest::WaitForCommit(compositor());
}
private:
base::test::TaskEnvironment task_environment_;
std::unique_ptr<TestContextFactories> context_factories_;
std::unique_ptr<TestCompositorHost> compositor_host_;
DISALLOW_COPY_AND_ASSIGN(LayerWithDelegateTest);
};
void ReturnMailbox(bool* run, const gpu::SyncToken& sync_token, bool is_lost) {
*run = true;
}
TEST(LayerStandaloneTest, ReleaseMailboxOnDestruction) {
auto layer = std::make_unique<Layer>(LAYER_TEXTURED);
bool callback_run = false;
constexpr gfx::Size size(64, 64);
auto resource = viz::TransferableResource::MakeGL(
gpu::Mailbox::Generate(), GL_LINEAR, GL_TEXTURE_2D, gpu::SyncToken(),
size, false /* is_overlay_candidate */);
layer->SetTransferableResource(
resource,
viz::SingleReleaseCallback::Create(
base::BindOnce(ReturnMailbox, &callback_run)),
gfx::Size(10, 10));
EXPECT_FALSE(callback_run);
layer.reset();
EXPECT_TRUE(callback_run);
}
// L1
// +-- L2
TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Simple) {
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> l2 =
CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350));
l1->Add(l2.get());
DrawTree(l1.get());
gfx::PointF point1_in_l2_coords(5, 5);
Layer::ConvertPointToLayer(l2.get(), l1.get(), &point1_in_l2_coords);
gfx::PointF point1_in_l1_coords(15, 15);
EXPECT_EQ(point1_in_l1_coords, point1_in_l2_coords);
gfx::PointF point2_in_l1_coords(5, 5);
Layer::ConvertPointToLayer(l1.get(), l2.get(), &point2_in_l1_coords);
gfx::PointF point2_in_l2_coords(-5, -5);
EXPECT_EQ(point2_in_l2_coords, point2_in_l1_coords);
}
// L1
// +-- L2
// +-- L3
TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Medium) {
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> l2 =
CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350));
std::unique_ptr<Layer> l3 =
CreateColorLayer(SK_ColorYELLOW, gfx::Rect(10, 10, 100, 100));
l1->Add(l2.get());
l2->Add(l3.get());
DrawTree(l1.get());
gfx::PointF point1_in_l3_coords(5, 5);
Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords);
gfx::PointF point1_in_l1_coords(25, 25);
EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords);
gfx::PointF point2_in_l1_coords(5, 5);
Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords);
gfx::PointF point2_in_l3_coords(-15, -15);
EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords);
}
TEST_P(LayerWithRealCompositorTest, Delegate) {
// This test makes sure that whenever paint happens at a layer, its layer
// delegate gets the paint, which in this test update its color and
// |color_index|.
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorBLACK, gfx::Rect(20, 20, 400, 400));
GetCompositor()->SetRootLayer(l1.get());
WaitForDraw();
TestLayerDelegate delegate;
l1->set_delegate(&delegate);
delegate.set_layer_bounds(l1->bounds());
delegate.AddColor(SK_ColorWHITE);
delegate.AddColor(SK_ColorYELLOW);
delegate.AddColor(SK_ColorGREEN);
l1->SchedulePaint(gfx::Rect(0, 0, 400, 400));
WaitForDraw();
// Test that paint happened at layer delegate.
EXPECT_EQ(1, delegate.color_index());
l1->SchedulePaint(gfx::Rect(10, 10, 200, 200));
WaitForDraw();
// Test that paint happened at layer delegate.
EXPECT_EQ(2, delegate.color_index());
l1->SchedulePaint(gfx::Rect(5, 5, 50, 50));
WaitForDraw();
// Test that paint happened at layer delegate.
EXPECT_EQ(0, delegate.color_index());
}
TEST_P(LayerWithRealCompositorTest, DrawTree) {
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> l2 =
CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350));
std::unique_ptr<Layer> l3 =
CreateColorLayer(SK_ColorYELLOW, gfx::Rect(10, 10, 100, 100));
l1->Add(l2.get());
l2->Add(l3.get());
GetCompositor()->SetRootLayer(l1.get());
WaitForDraw();
DrawTreeLayerDelegate d1(l1->bounds());
l1->set_delegate(&d1);
DrawTreeLayerDelegate d2(l2->bounds());
l2->set_delegate(&d2);
DrawTreeLayerDelegate d3(l3->bounds());
l3->set_delegate(&d3);
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
WaitForDraw();
EXPECT_FALSE(d1.painted());
EXPECT_TRUE(d2.painted());
EXPECT_FALSE(d3.painted());
}
// Tests that scheduling paint on a layer with a mask updates the mask.
TEST_P(LayerWithRealCompositorTest, SchedulePaintUpdatesMask) {
std::unique_ptr<Layer> layer =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> mask_layer = CreateLayer(ui::LAYER_TEXTURED);
mask_layer->SetBounds(gfx::Rect(layer->GetTargetBounds().size()));
layer->SetMaskLayer(mask_layer.get());
GetCompositor()->SetRootLayer(layer.get());
WaitForDraw();
DrawTreeLayerDelegate d1(layer->bounds());
layer->set_delegate(&d1);
DrawTreeLayerDelegate d2(mask_layer->bounds());
mask_layer->set_delegate(&d2);
layer->SchedulePaint(gfx::Rect(5, 5, 5, 5));
WaitForDraw();
EXPECT_TRUE(d1.painted());
EXPECT_TRUE(d2.painted());
}
// Tests no-texture Layers.
// Create this hierarchy:
// L1 - red
// +-- L2 - NO TEXTURE
// | +-- L3 - yellow
// +-- L4 - magenta
//
TEST_P(LayerWithRealCompositorTest, HierarchyNoTexture) {
std::unique_ptr<Layer> l1 =
CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400));
std::unique_ptr<Layer> l2 = CreateNoTextureLayer(gfx::Rect(10, 10, 350, 350));
std::unique_ptr<Layer> l3 =
CreateColorLayer(SK_ColorYELLOW, gfx::Rect(5, 5, 25, 25));
std::unique_ptr<Layer> l4 =
CreateColorLayer(SK_ColorMAGENTA, gfx::Rect(300, 300, 100, 100));
l1->Add(l2.get());
l1->Add(l4.get());
l2->Add(l3.get());
GetCompositor()->SetRootLayer(l1.get());
WaitForDraw();
DrawTreeLayerDelegate d2(l2->bounds());
l2->set_delegate(&d2);
DrawTreeLayerDelegate d3(l3->bounds());
l3->set_delegate(&d3);
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
l3->SchedulePaint(gfx::Rect(5, 5, 5, 5));
WaitForDraw();
// |d2| should not have received a paint notification since it has no texture.
EXPECT_FALSE(d2.painted());
// |d3| should have received a paint notification.
EXPECT_TRUE(d3.painted());
}
TEST_F(LayerWithDelegateTest, Cloning) {
std::unique_ptr<Layer> layer = CreateLayer(LAYER_SOLID_COLOR);
gfx::Transform transform;
transform.Scale(2, 1);
transform.Translate(10, 5);
gfx::Rect clip_rect(1, 1, 2, 2);
layer->SetTransform(transform);
layer->SetColor(SK_ColorRED);
layer->SetLayerInverted(true);
layer->AddCacheRenderSurfaceRequest();
layer->AddTrilinearFilteringRequest();
layer->SetClipRect(clip_rect);
layer->SetRoundedCornerRadius({1, 2, 4, 5});
layer->SetIsFastRoundedCorner(true);
auto clone = layer->Clone();
// Cloning preserves layer state.
EXPECT_EQ(transform, clone->GetTargetTransform());
EXPECT_EQ(SK_ColorRED, clone->background_color());
EXPECT_EQ(SK_ColorRED, clone->GetTargetColor());
EXPECT_TRUE(clone->layer_inverted());
// Cloning should not preserve cache_render_surface flag.
EXPECT_NE(layer->cc_layer_for_testing()->cache_render_surface(),
clone->cc_layer_for_testing()->cache_render_surface());
// Cloning should not preserve trilinear_filtering flag.
EXPECT_NE(layer->cc_layer_for_testing()->trilinear_filtering(),
clone->cc_layer_for_testing()->trilinear_filtering());
EXPECT_EQ(clip_rect, clone->clip_rect());
EXPECT_EQ(layer->rounded_corner_radii(), clone->rounded_corner_radii());
EXPECT_EQ(layer->is_fast_rounded_corner(), clone->is_fast_rounded_corner());
layer->SetTransform(gfx::Transform());
layer->SetColor(SK_ColorGREEN);
layer->SetLayerInverted(false);
layer->SetClipRect(gfx::Rect(10, 10, 10, 10));
layer->SetIsFastRoundedCorner(false);
layer->SetRoundedCornerRadius({3, 6, 9, 12});
// The clone is an independent copy, so state changes do not propagate.
EXPECT_EQ(transform, clone->GetTargetTransform());
EXPECT_EQ(SK_ColorRED, clone->background_color());
EXPECT_EQ(SK_ColorRED, clone->GetTargetColor());
EXPECT_TRUE(clone->layer_inverted());
EXPECT_EQ(clip_rect, clone->clip_rect());
EXPECT_FALSE(layer->is_fast_rounded_corner());
EXPECT_TRUE(clone->is_fast_rounded_corner());
EXPECT_NE(layer->rounded_corner_radii(), clone->rounded_corner_radii());
constexpr SkColor kTransparent = SK_ColorTRANSPARENT;
layer->SetColor(kTransparent);
layer->SetFillsBoundsOpaquely(false);
// Color and opaqueness targets should be preserved during cloning, even after
// switching away from solid color content.
ASSERT_TRUE(layer->SwitchCCLayerForTest());
clone = layer->Clone();
// The clone is a copy of the latest state.
EXPECT_TRUE(clone->GetTargetTransform().IsIdentity());
EXPECT_EQ(kTransparent, clone->background_color());
EXPECT_EQ(kTransparent, clone->GetTargetColor());
EXPECT_FALSE(clone->layer_inverted());
EXPECT_FALSE(clone->fills_bounds_opaquely());
// A solid color layer with transparent color can be marked as opaque. The
// clone should retain this state.
layer = CreateLayer(LAYER_SOLID_COLOR);
layer->SetColor(kTransparent);
layer->SetFillsBoundsOpaquely(true);
clone = layer->Clone();
EXPECT_TRUE(clone->GetTargetTransform().IsIdentity());
EXPECT_EQ(kTransparent, clone->background_color());
EXPECT_EQ(kTransparent, clone->GetTargetColor());
EXPECT_FALSE(clone->layer_inverted());
EXPECT_TRUE(clone->fills_bounds_opaquely());
layer = CreateLayer(LAYER_SOLID_COLOR);
layer->SetVisible(true);
layer->SetOpacity(1.0f);
layer->SetColor(SK_ColorRED);
ScopedLayerAnimationSettings settings(layer->GetAnimator());
layer->SetVisible(false);
layer->SetOpacity(0.0f);
layer->SetColor(SK_ColorGREEN);
EXPECT_TRUE(layer->visible());
EXPECT_EQ(1.0f, layer->opacity());
EXPECT_EQ(SK_ColorRED, layer->background_color());
clone = layer->Clone();
// Cloning copies animation targets.
EXPECT_FALSE(clone->visible());
EXPECT_EQ(0.0f, clone->opacity());
EXPECT_EQ(SK_ColorGREEN, clone->background_color());
}
TEST_F(LayerWithDelegateTest, Mirroring) {
std::unique_ptr<Layer> root = CreateNoTextureLayer(gfx::Rect(0, 0, 100, 100));
std::unique_ptr<Layer> child = CreateLayer(LAYER_TEXTURED);
const gfx::Rect bounds(0, 0, 50, 50);
child->SetBounds(bounds);
child->SetVisible(true);
DrawTreeLayerDelegate delegate(child->bounds());
child->set_delegate(&delegate);
const auto mirror1 = child->Mirror();
// Bounds and visibility are preserved.
EXPECT_EQ(bounds, mirror1->bounds());
EXPECT_TRUE(mirror1->visible());
root->Add(child.get());
root->Add(mirror1.get());
DrawTree(root.get());
EXPECT_TRUE(delegate.painted());
delegate.Reset();
// Both layers should be clean.
EXPECT_TRUE(child->damaged_region_for_testing().IsEmpty());
EXPECT_TRUE(mirror1->damaged_region_for_testing().IsEmpty());
const gfx::Rect damaged_rect(10, 10, 20, 20);
EXPECT_TRUE(child->SchedulePaint(damaged_rect));
EXPECT_EQ(damaged_rect, child->damaged_region_for_testing().bounds());
DrawTree(root.get());
EXPECT_TRUE(delegate.painted());
delegate.Reset();
// Damage should be propagated to the mirror.
EXPECT_EQ(damaged_rect, mirror1->damaged_region_for_testing().bounds());
EXPECT_TRUE(child->damaged_region_for_testing().IsEmpty());
DrawTree(root.get());
EXPECT_TRUE(delegate.painted());
// Mirror should be clean.
EXPECT_TRUE(mirror1->damaged_region_for_testing().IsEmpty());
const auto mirror2 = child->Mirror();
root->Add(mirror2.get());
// Bounds are not synchronized by default.
const gfx::Rect new_bounds(10, 10, 10, 10);
child->SetBounds(new_bounds);
EXPECT_EQ(bounds, mirror1->bounds());
EXPECT_EQ(bounds, mirror2->bounds());
child->SetBounds(bounds);
// Bounds should be synchronized only for the mirror layer that requested it.
mirror1->set_sync_bounds_with_source(true);
child->SetBounds(new_bounds);
EXPECT_EQ(new_bounds, mirror1->bounds());
EXPECT_EQ(bounds, mirror2->bounds());
// Check for rounded corner mirror behavior
EXPECT_TRUE(mirror1->rounded_corner_radii().IsEmpty());
EXPECT_FALSE(mirror1->is_fast_rounded_corner());
constexpr gfx::RoundedCornersF kCornerRadii(2, 3, 4, 5);
child->SetRoundedCornerRadius(kCornerRadii);
child->SetIsFastRoundedCorner(true);
EXPECT_EQ(kCornerRadii, mirror1->rounded_corner_radii());
EXPECT_TRUE(mirror1->is_fast_rounded_corner());
}
// Tests for SurfaceLayer cloning and mirroring. This tests certain properties
// are preserved.
TEST_F(LayerWithDelegateTest, SurfaceLayerCloneAndMirror) {
const viz::FrameSinkId arbitrary_frame_sink(1, 1);
viz::ParentLocalSurfaceIdAllocator allocator;
std::unique_ptr<Layer> layer = CreateLayer(LAYER_SOLID_COLOR);
allocator.GenerateId();
viz::LocalSurfaceId local_surface_id = allocator.GetCurrentLocalSurfaceId();
viz::SurfaceId surface_id_one(arbitrary_frame_sink, local_surface_id);
layer->SetShowSurface(surface_id_one, gfx::Size(10, 10), SK_ColorWHITE,
cc::DeadlinePolicy::UseDefaultDeadline(), false);
EXPECT_FALSE(layer->StretchContentToFillBounds());
auto clone = layer->Clone();
EXPECT_FALSE(clone->StretchContentToFillBounds());
auto mirror = layer->Mirror();
EXPECT_FALSE(mirror->StretchContentToFillBounds());
allocator.GenerateId();
local_surface_id = allocator.GetCurrentLocalSurfaceId();
viz::SurfaceId surface_id_two(arbitrary_frame_sink, local_surface_id);
layer->SetShowSurface(surface_id_two, gfx::Size(10, 10), SK_ColorWHITE,
cc::DeadlinePolicy::UseDefaultDeadline(), true);
EXPECT_TRUE(layer->StretchContentToFillBounds());
clone = layer->Clone();
EXPECT_TRUE(clone->StretchContentToFillBounds());
mirror = layer->Mirror();
EXPECT_TRUE(mirror->StretchContentToFillBounds());
}
class LayerWithNullDelegateTest : public LayerWithDelegateTest {
public:
LayerWithNullDelegateTest() {}
~LayerWithNullDelegateTest() override {}
void SetUp() override {
LayerWithDelegateTest::SetUp();
default_layer_delegate_ = std::make_unique<NullLayerDelegate>();
}
std::unique_ptr<Layer> CreateLayer(LayerType type) override {
auto layer = std::make_unique<Layer>(type);
layer->set_delegate(default_layer_delegate_.get());
return layer;
}
std::unique_ptr<Layer> CreateTextureRootLayer(const gfx::Rect& bounds) {
std::unique_ptr<Layer> layer = CreateTextureLayer(bounds);
compositor()->SetRootLayer(layer.get());
return layer;
}
std::unique_ptr<Layer> CreateTextureLayer(const gfx::Rect& bounds) {
std::unique_ptr<Layer> layer = CreateLayer(LAYER_TEXTURED);
layer->SetBounds(bounds);
return layer;
}
std::unique_ptr<Layer> CreateNoTextureLayer(
const gfx::Rect& bounds) override {
std::unique_ptr<Layer> layer = CreateLayer(LAYER_NOT_DRAWN);
layer->SetBounds(bounds);
return layer;
}
gfx::Rect LastInvalidation() const {
return default_layer_delegate_->invalidation();
}
private:
std::unique_ptr<NullLayerDelegate> default_layer_delegate_;
DISALLOW_COPY_AND_ASSIGN(LayerWithNullDelegateTest);
};
TEST_F(LayerWithNullDelegateTest, SwitchLayerPreservesCCLayerState) {
std::unique_ptr<Layer> l1 = CreateLayer(LAYER_SOLID_COLOR);
l1->SetFillsBoundsOpaquely(true);
l1->SetVisible(false);
l1->SetBounds(gfx::Rect(4, 5));
constexpr gfx::RoundedCornersF kCornerRadii(1, 2, 3, 4);