forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer_tree_impl_unittest.cc
2166 lines (1865 loc) · 86 KB
/
layer_tree_impl_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 2014 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 "cc/trees/layer_tree_impl.h"
#include "cc/layers/heads_up_display_layer_impl.h"
#include "cc/layers/layer.h"
#include "cc/layers/solid_color_scrollbar_layer_impl.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/fake_output_surface.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/layer_tree_host_common_test.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "ui/gfx/geometry/size_conversions.h"
namespace cc {
namespace {
class LayerTreeImplTest : public LayerTreeHostCommonTest {
public:
LayerTreeImplTest() {
LayerTreeSettings settings;
settings.layer_transforms_should_scale_layer_contents = true;
settings.scrollbar_show_scale_threshold = 1.1f;
host_impl_.reset(
new FakeLayerTreeHostImpl(settings, &proxy_, &shared_bitmap_manager_));
EXPECT_TRUE(host_impl_->InitializeRenderer(FakeOutputSurface::Create3d()));
}
FakeLayerTreeHostImpl& host_impl() { return *host_impl_; }
LayerImpl* root_layer() { return host_impl_->active_tree()->root_layer(); }
const LayerImplList& RenderSurfaceLayerList() const {
return host_impl_->active_tree()->RenderSurfaceLayerList();
}
private:
TestSharedBitmapManager shared_bitmap_manager_;
FakeImplProxy proxy_;
scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
};
TEST_F(LayerTreeImplTest, HitTestingForSingleLayer) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point test_point(101, 101);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(-1, -1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the root layer.
test_point = gfx::Point(1, 1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
test_point = gfx::Point(99, 99);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForSingleLayerAndHud) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
scoped_ptr<HeadsUpDisplayLayerImpl> hud =
HeadsUpDisplayLayerImpl::Create(host_impl().active_tree(), 11111);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
// Create hud and add it as a child of root.
gfx::Size hud_bounds(200, 200);
SetLayerPropertiesForTesting(hud.get(), identity_matrix, transform_origin,
position, hud_bounds, true, false, false);
hud->SetDrawsContent(true);
host_impl().active_tree()->set_hud_layer(hud.get());
root->AddChild(hud.Pass());
host_impl().SetViewportSize(hud_bounds);
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(2u, root_layer()->render_surface()->layer_list().size());
// Hit testing for a point inside HUD, but outside root should return null
gfx::Point test_point(101, 101);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(-1, -1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the root layer, never the HUD
// layer.
test_point = gfx::Point(1, 1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
test_point = gfx::Point(99, 99);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForUninvertibleTransform) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
gfx::Transform uninvertible_transform;
uninvertible_transform.matrix().set(0, 0, 0.0);
uninvertible_transform.matrix().set(1, 1, 0.0);
uninvertible_transform.matrix().set(2, 2, 0.0);
uninvertible_transform.matrix().set(3, 3, 0.0);
ASSERT_FALSE(uninvertible_transform.IsInvertible());
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), uninvertible_transform,
transform_origin, position, bounds, true, false,
true);
root->SetDrawsContent(true);
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
ASSERT_FALSE(root_layer()->screen_space_transform().IsInvertible());
// Hit testing any point should not hit the layer. If the invertible matrix is
// accidentally ignored and treated like an identity, then the hit testing
// will incorrectly hit the layer when it shouldn't.
gfx::Point test_point(1, 1);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(10, 10);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(10, 30);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(50, 50);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(67, 48);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(99, 99);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(-1, -1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
}
TEST_F(LayerTreeImplTest, HitTestingForSinglePositionedLayer) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
// this layer is positioned, and hit testing should correctly know where the
// layer is located.
gfx::PointF position(50.f, 50.f);
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point test_point(49, 49);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Even though the layer exists at (101, 101), it should not be visible there
// since the root render surface would clamp it.
test_point = gfx::Point(101, 101);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the root layer.
test_point = gfx::Point(51, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
test_point = gfx::Point(99, 99);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForSingleRotatedLayer) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
gfx::Transform identity_matrix;
gfx::Transform rotation45_degrees_about_center;
rotation45_degrees_about_center.Translate(50.0, 50.0);
rotation45_degrees_about_center.RotateAboutZAxis(45.0);
rotation45_degrees_about_center.Translate(-50.0, -50.0);
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), rotation45_degrees_about_center,
transform_origin, position, bounds, true, false,
true);
root->SetDrawsContent(true);
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
// Hit testing for points outside the layer.
// These corners would have been inside the un-transformed layer, but they
// should not hit the correctly transformed layer.
gfx::Point test_point(99, 99);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(1, 1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the root layer.
test_point = gfx::Point(1, 50);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
// Hit testing the corners that would overlap the unclipped layer, but are
// outside the clipped region.
test_point = gfx::Point(50, -1);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_FALSE(result_layer);
test_point = gfx::Point(-1, 50);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_FALSE(result_layer);
}
TEST_F(LayerTreeImplTest, HitTestingForSinglePerspectiveLayer) {
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 12345);
gfx::Transform identity_matrix;
// perspective_projection_about_center * translation_by_z is designed so that
// the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50).
gfx::Transform perspective_projection_about_center;
perspective_projection_about_center.Translate(50.0, 50.0);
perspective_projection_about_center.ApplyPerspectiveDepth(1.0);
perspective_projection_about_center.Translate(-50.0, -50.0);
gfx::Transform translation_by_z;
translation_by_z.Translate3d(0.0, 0.0, -1.0);
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(
root.get(), perspective_projection_about_center * translation_by_z,
transform_origin, position, bounds, true, false, true);
root->SetDrawsContent(true);
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
// Hit testing for points outside the layer.
// These corners would have been inside the un-transformed layer, but they
// should not hit the correctly transformed layer.
gfx::Point test_point(24, 24);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(76, 76);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the root layer.
test_point = gfx::Point(26, 26);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
test_point = gfx::Point(74, 74);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForSingleLayerWithScaledContents) {
// A layer's visible content rect is actually in the layer's content space.
// The screen space transform converts from the layer's origin space to screen
// space. This test makes sure that hit testing works correctly accounts for
// the contents scale. A contents scale that is not 1 effectively forces a
// non-identity transform between layer's content space and layer's origin
// space. The hit testing code must take this into account.
//
// To test this, the layer is positioned at (25, 25), and is size (50, 50). If
// contents scale is ignored, then hit testing will mis-interpret the visible
// content rect as being larger than the actual bounds of the layer.
//
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
gfx::PointF(), gfx::Size(100, 100), true, false,
true);
{
gfx::PointF position(25.f, 25.f);
gfx::Size bounds(50, 50);
scoped_ptr<LayerImpl> test_layer =
LayerImpl::Create(host_impl().active_tree(), 12345);
SetLayerPropertiesForTesting(test_layer.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
// override content bounds and contents scale
test_layer->SetContentBounds(gfx::Size(100, 100));
test_layer->SetContentsScale(2, 2);
test_layer->SetDrawsContent(true);
root->AddChild(test_layer.Pass());
}
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
// The visible content rect for test_layer is actually 100x100, even though
// its layout size is 50x50, positioned at 25x25.
LayerImpl* test_layer =
host_impl().active_tree()->root_layer()->children()[0];
EXPECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect());
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
// Hit testing for a point outside the layer should return a null pointer (the
// root layer does not draw content, so it will not be hit tested either).
gfx::Point test_point(101, 101);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(24, 24);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(76, 76);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the test layer.
test_point = gfx::Point(26, 26);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
test_point = gfx::Point(74, 74);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(12345, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForSimpleClippedLayer) {
// Test that hit-testing will only work for the visible portion of a layer,
// and not the entire layer bounds. Here we just test the simple axis-aligned
// case.
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
gfx::PointF(), gfx::Size(100, 100), true, false,
true);
{
scoped_ptr<LayerImpl> clipping_layer =
LayerImpl::Create(host_impl().active_tree(), 123);
// this layer is positioned, and hit testing should correctly know where the
// layer is located.
gfx::PointF position(25.f, 25.f);
gfx::Size bounds(50, 50);
SetLayerPropertiesForTesting(clipping_layer.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
clipping_layer->SetMasksToBounds(true);
scoped_ptr<LayerImpl> child =
LayerImpl::Create(host_impl().active_tree(), 456);
position = gfx::PointF(-50.f, -50.f);
bounds = gfx::Size(300, 300);
SetLayerPropertiesForTesting(child.get(), identity_matrix, transform_origin,
position, bounds, true, false, false);
child->SetDrawsContent(true);
clipping_layer->AddChild(child.Pass());
root->AddChild(clipping_layer.Pass());
}
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
ASSERT_EQ(456, root_layer()->render_surface()->layer_list().at(0)->id());
// Hit testing for a point outside the layer should return a null pointer.
// Despite the child layer being very large, it should be clipped to the root
// layer's bounds.
gfx::Point test_point(24, 24);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Even though the layer exists at (101, 101), it should not be visible there
// since the clipping_layer would clamp it.
test_point = gfx::Point(76, 76);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the child layer.
test_point = gfx::Point(26, 26);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(456, result_layer->id());
test_point = gfx::Point(74, 74);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(456, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForMultiClippedRotatedLayer) {
// This test checks whether hit testing correctly avoids hit testing with
// multiple ancestors that clip in non axis-aligned ways. To pass this test,
// the hit testing algorithm needs to recognize that multiple parent layers
// may clip the layer, and should not actually hit those clipped areas.
//
// The child and grand_child layers are both initialized to clip the
// rotated_leaf. The child layer is rotated about the top-left corner, so that
// the root + child clips combined create a triangle. The rotated_leaf will
// only be visible where it overlaps this triangle.
//
scoped_ptr<LayerImpl> root =
LayerImpl::Create(host_impl().active_tree(), 123);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetMasksToBounds(true);
{
scoped_ptr<LayerImpl> child =
LayerImpl::Create(host_impl().active_tree(), 456);
scoped_ptr<LayerImpl> grand_child =
LayerImpl::Create(host_impl().active_tree(), 789);
scoped_ptr<LayerImpl> rotated_leaf =
LayerImpl::Create(host_impl().active_tree(), 2468);
position = gfx::PointF(10.f, 10.f);
bounds = gfx::Size(80, 80);
SetLayerPropertiesForTesting(child.get(), identity_matrix, transform_origin,
position, bounds, true, false, false);
child->SetMasksToBounds(true);
gfx::Transform rotation45_degrees_about_corner;
rotation45_degrees_about_corner.RotateAboutZAxis(45.0);
// remember, positioned with respect to its parent which is already at 10,
// 10
position = gfx::PointF();
bounds =
gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100.
SetLayerPropertiesForTesting(
grand_child.get(), rotation45_degrees_about_corner, transform_origin,
position, bounds, true, false, false);
grand_child->SetMasksToBounds(true);
// Rotates about the center of the layer
gfx::Transform rotated_leaf_transform;
rotated_leaf_transform.Translate(
-10.0, -10.0); // cancel out the grand_parent's position
rotated_leaf_transform.RotateAboutZAxis(
-45.0); // cancel out the corner 45-degree rotation of the parent.
rotated_leaf_transform.Translate(50.0, 50.0);
rotated_leaf_transform.RotateAboutZAxis(45.0);
rotated_leaf_transform.Translate(-50.0, -50.0);
position = gfx::PointF();
bounds = gfx::Size(100, 100);
SetLayerPropertiesForTesting(rotated_leaf.get(), rotated_leaf_transform,
transform_origin, position, bounds, true,
false, false);
rotated_leaf->SetDrawsContent(true);
grand_child->AddChild(rotated_leaf.Pass());
child->AddChild(grand_child.Pass());
root->AddChild(child.Pass());
}
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// (11, 89) is close to the the bottom left corner within the clip, but it is
// not inside the layer.
gfx::Point test_point(11, 89);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Closer inwards from the bottom left will overlap the layer.
test_point = gfx::Point(25, 75);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2468, result_layer->id());
// (4, 50) is inside the unclipped layer, but that corner of the layer should
// be clipped away by the grandparent and should not get hit. If hit testing
// blindly uses visible content rect without considering how parent may clip
// the layer, then hit testing would accidentally think that the point
// successfully hits the layer.
test_point = gfx::Point(4, 50);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// (11, 50) is inside the layer and within the clipped area.
test_point = gfx::Point(11, 50);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2468, result_layer->id());
// Around the middle, just to the right and up, would have hit the layer
// except that that area should be clipped away by the parent.
test_point = gfx::Point(51, 49);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Around the middle, just to the left and down, should successfully hit the
// layer.
test_point = gfx::Point(49, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2468, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForNonClippingIntermediateLayer) {
// This test checks that hit testing code does not accidentally clip to layer
// bounds for a layer that actually does not clip.
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
gfx::PointF(), gfx::Size(100, 100), true, false,
true);
{
scoped_ptr<LayerImpl> intermediate_layer =
LayerImpl::Create(host_impl().active_tree(), 123);
// this layer is positioned, and hit testing should correctly know where the
// layer is located.
gfx::PointF position(10.f, 10.f);
gfx::Size bounds(50, 50);
SetLayerPropertiesForTesting(intermediate_layer.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
// Sanity check the intermediate layer should not clip.
ASSERT_FALSE(intermediate_layer->masks_to_bounds());
ASSERT_FALSE(intermediate_layer->mask_layer());
// The child of the intermediate_layer is translated so that it does not
// overlap intermediate_layer at all. If child is incorrectly clipped, we
// would not be able to hit it successfully.
scoped_ptr<LayerImpl> child =
LayerImpl::Create(host_impl().active_tree(), 456);
position = gfx::PointF(60.f, 60.f); // 70, 70 in screen space
bounds = gfx::Size(20, 20);
SetLayerPropertiesForTesting(child.get(), identity_matrix, transform_origin,
position, bounds, true, false, false);
child->SetDrawsContent(true);
intermediate_layer->AddChild(child.Pass());
root->AddChild(intermediate_layer.Pass());
}
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size());
ASSERT_EQ(456, root_layer()->render_surface()->layer_list().at(0)->id());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point test_point(69, 69);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
test_point = gfx::Point(91, 91);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
EXPECT_FALSE(result_layer);
// Hit testing for a point inside should return the child layer.
test_point = gfx::Point(71, 71);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(456, result_layer->id());
test_point = gfx::Point(89, 89);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(456, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForMultipleLayers) {
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
{
// child 1 and child2 are initialized to overlap between x=50 and x=60.
// grand_child is set to overlap both child1 and child2 between y=50 and
// y=60. The expected stacking order is: (front) child2, (second)
// grand_child, (third) child1, and (back) the root layer behind all other
// layers.
scoped_ptr<LayerImpl> child1 =
LayerImpl::Create(host_impl().active_tree(), 2);
scoped_ptr<LayerImpl> child2 =
LayerImpl::Create(host_impl().active_tree(), 3);
scoped_ptr<LayerImpl> grand_child1 =
LayerImpl::Create(host_impl().active_tree(), 4);
position = gfx::PointF(10.f, 10.f);
bounds = gfx::Size(50, 50);
SetLayerPropertiesForTesting(child1.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
child1->SetDrawsContent(true);
position = gfx::PointF(50.f, 10.f);
bounds = gfx::Size(50, 50);
SetLayerPropertiesForTesting(child2.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
child2->SetDrawsContent(true);
// Remember that grand_child is positioned with respect to its parent (i.e.
// child1). In screen space, the intended position is (10, 50), with size
// 100 x 50.
position = gfx::PointF(0.f, 40.f);
bounds = gfx::Size(100, 50);
SetLayerPropertiesForTesting(grand_child1.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
grand_child1->SetDrawsContent(true);
child1->AddChild(grand_child1.Pass());
root->AddChild(child1.Pass());
root->AddChild(child2.Pass());
}
LayerImpl* child1 = root->children()[0];
LayerImpl* child2 = root->children()[1];
LayerImpl* grand_child1 = child1->children()[0];
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_TRUE(child1);
ASSERT_TRUE(child2);
ASSERT_TRUE(grand_child1);
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
RenderSurfaceImpl* root_render_surface = root_layer()->render_surface();
ASSERT_EQ(4u, root_render_surface->layer_list().size());
ASSERT_EQ(1, root_render_surface->layer_list().at(0)->id()); // root layer
ASSERT_EQ(2, root_render_surface->layer_list().at(1)->id()); // child1
ASSERT_EQ(4, root_render_surface->layer_list().at(2)->id()); // grand_child1
ASSERT_EQ(3, root_render_surface->layer_list().at(3)->id()); // child2
// Nothing overlaps the root_layer at (1, 1), so hit testing there should find
// the root layer.
gfx::Point test_point = gfx::Point(1, 1);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(1, result_layer->id());
// At (15, 15), child1 and root are the only layers. child1 is expected to be
// on top.
test_point = gfx::Point(15, 15);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2, result_layer->id());
// At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
test_point = gfx::Point(51, 20);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(3, result_layer->id());
// At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on
// top.
test_point = gfx::Point(80, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(3, result_layer->id());
// At (51, 51), all layers overlap each other. child2 is expected to be on top
// of all other layers.
test_point = gfx::Point(51, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(3, result_layer->id());
// At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to
// be on top.
test_point = gfx::Point(20, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(4, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingForMultipleLayersAtVaryingDepths) {
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
root->SetShouldFlattenTransform(false);
root->Set3dSortingContextId(1);
{
// child 1 and child2 are initialized to overlap between x=50 and x=60.
// grand_child is set to overlap both child1 and child2 between y=50 and
// y=60. The expected stacking order is: (front) child2, (second)
// grand_child, (third) child1, and (back) the root layer behind all other
// layers.
scoped_ptr<LayerImpl> child1 =
LayerImpl::Create(host_impl().active_tree(), 2);
scoped_ptr<LayerImpl> child2 =
LayerImpl::Create(host_impl().active_tree(), 3);
scoped_ptr<LayerImpl> grand_child1 =
LayerImpl::Create(host_impl().active_tree(), 4);
position = gfx::PointF(10.f, 10.f);
bounds = gfx::Size(50, 50);
SetLayerPropertiesForTesting(child1.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
child1->SetDrawsContent(true);
child1->SetShouldFlattenTransform(false);
child1->Set3dSortingContextId(1);
position = gfx::PointF(50.f, 10.f);
bounds = gfx::Size(50, 50);
gfx::Transform translate_z;
translate_z.Translate3d(0, 0, -10.f);
SetLayerPropertiesForTesting(child2.get(), translate_z, transform_origin,
position, bounds, true, false, false);
child2->SetDrawsContent(true);
child2->SetShouldFlattenTransform(false);
child2->Set3dSortingContextId(1);
// Remember that grand_child is positioned with respect to its parent (i.e.
// child1). In screen space, the intended position is (10, 50), with size
// 100 x 50.
position = gfx::PointF(0.f, 40.f);
bounds = gfx::Size(100, 50);
SetLayerPropertiesForTesting(grand_child1.get(), identity_matrix,
transform_origin, position, bounds, true,
false, false);
grand_child1->SetDrawsContent(true);
grand_child1->SetShouldFlattenTransform(false);
child1->AddChild(grand_child1.Pass());
root->AddChild(child1.Pass());
root->AddChild(child2.Pass());
}
LayerImpl* child1 = root->children()[0];
LayerImpl* child2 = root->children()[1];
LayerImpl* grand_child1 = child1->children()[0];
host_impl().SetViewportSize(root->bounds());
host_impl().active_tree()->SetRootLayer(root.Pass());
host_impl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
// Sanity check the scenario we just created.
ASSERT_TRUE(child1);
ASSERT_TRUE(child2);
ASSERT_TRUE(grand_child1);
ASSERT_EQ(1u, RenderSurfaceLayerList().size());
RenderSurfaceImpl* root_render_surface =
host_impl().active_tree()->root_layer()->render_surface();
ASSERT_EQ(4u, root_render_surface->layer_list().size());
ASSERT_EQ(3, root_render_surface->layer_list().at(0)->id());
ASSERT_EQ(1, root_render_surface->layer_list().at(1)->id());
ASSERT_EQ(2, root_render_surface->layer_list().at(2)->id());
ASSERT_EQ(4, root_render_surface->layer_list().at(3)->id());
// Nothing overlaps the root_layer at (1, 1), so hit testing there should find
// the root layer.
gfx::Point test_point = gfx::Point(1, 1);
LayerImpl* result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(1, result_layer->id());
// At (15, 15), child1 and root are the only layers. child1 is expected to be
// on top.
test_point = gfx::Point(15, 15);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2, result_layer->id());
// At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
// (because 3 is transformed to the back).
test_point = gfx::Point(51, 20);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(2, result_layer->id());
// 3 Would have been on top if it hadn't been transformed to the background.
// Make sure that it isn't hit.
test_point = gfx::Point(80, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(4, result_layer->id());
// 3 Would have been on top if it hadn't been transformed to the background.
// Make sure that it isn't hit.
test_point = gfx::Point(51, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(4, result_layer->id());
// At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to
// be on top.
test_point = gfx::Point(20, 51);
result_layer =
host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point);
ASSERT_TRUE(result_layer);
EXPECT_EQ(4, result_layer->id());
}
TEST_F(LayerTreeImplTest, HitTestingRespectsClipParents) {
scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1);
gfx::Transform identity_matrix;
gfx::Point3F transform_origin;
gfx::PointF position;
gfx::Size bounds(100, 100);
SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
position, bounds, true, false, true);
root->SetDrawsContent(true);
{
scoped_ptr<LayerImpl> child =
LayerImpl::Create(host_impl().active_tree(), 2);
scoped_ptr<LayerImpl> grand_child =
LayerImpl::Create(host_impl().active_tree(), 4);
position = gfx::PointF(10.f, 10.f);
bounds = gfx::Size(1, 1);
SetLayerPropertiesForTesting(child.get(), identity_matrix, transform_origin,
position, bounds, true, false, false);
child->SetDrawsContent(true);
child->SetMasksToBounds(true);