1// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/trees/layer_sorter.h"
6
7#include "cc/base/math_util.h"
8#include "cc/layers/layer_impl.h"
9#include "cc/test/fake_impl_proxy.h"
10#include "cc/test/fake_layer_tree_host_impl.h"
11#include "cc/trees/single_thread_proxy.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "ui/gfx/transform.h"
14
15namespace cc {
16namespace {
17
18// Note: In the following overlap tests, the "camera" is looking down the
19// negative Z axis, meaning that layers with smaller z values (more negative)
20// are further from the camera and therefore must be drawn before layers with
21// higher z values.
22
23TEST(LayerSorterTest, BasicOverlap) {
24  LayerSorter::ABCompareResult overlap_result;
25  const float z_threshold = 0.1f;
26  float weight = 0.f;
27
28  // Trivial test, with one layer directly obscuring the other.
29  gfx::Transform neg4_translate;
30  neg4_translate.Translate3d(0.0, 0.0, -4.0);
31  LayerShape front(2.f, 2.f, neg4_translate);
32
33  gfx::Transform neg5_translate;
34  neg5_translate.Translate3d(0.0, 0.0, -5.0);
35  LayerShape back(2.f, 2.f, neg5_translate);
36
37  overlap_result =
38      LayerSorter::CheckOverlap(&front, &back, z_threshold, &weight);
39  EXPECT_EQ(LayerSorter::BBeforeA, overlap_result);
40  EXPECT_EQ(1.f, weight);
41
42  overlap_result =
43      LayerSorter::CheckOverlap(&back, &front, z_threshold, &weight);
44  EXPECT_EQ(LayerSorter::ABeforeB, overlap_result);
45  EXPECT_EQ(1.f, weight);
46
47  // One layer translated off to the right. No overlap should be detected.
48  gfx::Transform right_translate;
49  right_translate.Translate3d(10.0, 0.0, -5.0);
50  LayerShape back_right(2.f, 2.f, right_translate);
51  overlap_result =
52      LayerSorter::CheckOverlap(&front, &back_right, z_threshold, &weight);
53  EXPECT_EQ(LayerSorter::None, overlap_result);
54
55  // When comparing a layer with itself, z difference is always 0.
56  overlap_result =
57      LayerSorter::CheckOverlap(&front, &front, z_threshold, &weight);
58  EXPECT_EQ(0.f, weight);
59}
60
61TEST(LayerSorterTest, RightAngleOverlap) {
62  LayerSorter::ABCompareResult overlap_result;
63  const float z_threshold = 0.1f;
64  float weight = 0.f;
65
66  gfx::Transform perspective_matrix;
67  perspective_matrix.ApplyPerspectiveDepth(1000.0);
68
69  // Two layers forming a right angle with a perspective viewing transform.
70  gfx::Transform left_face_matrix;
71  left_face_matrix.Translate3d(-1.0, 0.0, -5.0);
72  left_face_matrix.RotateAboutYAxis(-90.0);
73  left_face_matrix.Translate(-1.0, -1.0);
74  LayerShape left_face(2.f, 2.f, perspective_matrix * left_face_matrix);
75  gfx::Transform front_face_matrix;
76  front_face_matrix.Translate3d(0.0, 0.0, -4.0);
77  front_face_matrix.Translate(-1.0, -1.0);
78  LayerShape front_face(2.f, 2.f, perspective_matrix * front_face_matrix);
79
80  overlap_result =
81      LayerSorter::CheckOverlap(&front_face, &left_face, z_threshold, &weight);
82  EXPECT_EQ(LayerSorter::BBeforeA, overlap_result);
83}
84
85TEST(LayerSorterTest, IntersectingLayerOverlap) {
86  LayerSorter::ABCompareResult overlap_result;
87  const float z_threshold = 0.1f;
88  float weight = 0.f;
89
90  gfx::Transform perspective_matrix;
91  perspective_matrix.ApplyPerspectiveDepth(1000.0);
92
93  // Intersecting layers. An explicit order will be returned based on relative z
94  // values at the overlapping features but the weight returned should be zero.
95  gfx::Transform front_face_matrix;
96  front_face_matrix.Translate3d(0.0, 0.0, -4.0);
97  front_face_matrix.Translate(-1.0, -1.0);
98  LayerShape front_face(2.f, 2.f, perspective_matrix * front_face_matrix);
99
100  gfx::Transform through_matrix;
101  through_matrix.Translate3d(0.0, 0.0, -4.0);
102  through_matrix.RotateAboutYAxis(45.0);
103  through_matrix.Translate(-1.0, -1.0);
104  LayerShape rotated_face(2.f, 2.f, perspective_matrix * through_matrix);
105  overlap_result = LayerSorter::CheckOverlap(&front_face,
106                                             &rotated_face,
107                                             z_threshold,
108                                             &weight);
109  EXPECT_NE(LayerSorter::None, overlap_result);
110  EXPECT_EQ(0.f, weight);
111}
112
113TEST(LayerSorterTest, LayersAtAngleOverlap) {
114  LayerSorter::ABCompareResult overlap_result;
115  const float z_threshold = 0.1f;
116  float weight = 0.f;
117
118  // Trickier test with layers at an angle.
119  //
120  //   -x . . . . 0 . . . . +x
121  // -z             /
122  //  :            /----B----
123  //  0           C
124  //  : ----A----/
125  // +z         /
126  //
127  // C is in front of A and behind B (not what you'd expect by comparing
128  // centers). A and B don't overlap, so they're incomparable.
129
130  gfx::Transform transform_a;
131  transform_a.Translate3d(-6.0, 0.0, 1.0);
132  transform_a.Translate(-4.0, -10.0);
133  LayerShape layer_a(8.f, 20.f, transform_a);
134
135  gfx::Transform transform_b;
136  transform_b.Translate3d(6.0, 0.0, -1.0);
137  transform_b.Translate(-4.0, -10.0);
138  LayerShape layer_b(8.f, 20.f, transform_b);
139
140  gfx::Transform transform_c;
141  transform_c.RotateAboutYAxis(40.0);
142  transform_c.Translate(-4.0, -10.0);
143  LayerShape layer_c(8.f, 20.f, transform_c);
144
145  overlap_result =
146      LayerSorter::CheckOverlap(&layer_a, &layer_c, z_threshold, &weight);
147  EXPECT_EQ(LayerSorter::ABeforeB, overlap_result);
148  overlap_result =
149      LayerSorter::CheckOverlap(&layer_c, &layer_b, z_threshold, &weight);
150  EXPECT_EQ(LayerSorter::ABeforeB, overlap_result);
151  overlap_result =
152      LayerSorter::CheckOverlap(&layer_a, &layer_b, z_threshold, &weight);
153  EXPECT_EQ(LayerSorter::None, overlap_result);
154}
155
156TEST(LayerSorterTest, LayersUnderPathologicalPerspectiveTransform) {
157  LayerSorter::ABCompareResult overlap_result;
158  const float z_threshold = 0.1f;
159  float weight = 0.f;
160
161  // On perspective projection, if w becomes negative, the re-projected point
162  // will be invalid and un-usable. Correct code needs to clip away portions of
163  // the geometry where w < 0. If the code uses the invalid value, it will think
164  // that a layer has different bounds than it really does, which can cause
165  // things to sort incorrectly.
166
167  gfx::Transform perspective_matrix;
168  perspective_matrix.ApplyPerspectiveDepth(1);
169
170  gfx::Transform transform_a;
171  transform_a.Translate3d(-15.0, 0.0, -2.0);
172  transform_a.Translate(-5.0, -5.0);
173  LayerShape layer_a(10.f, 10.f, perspective_matrix * transform_a);
174
175  // With this sequence of transforms, when layer B is correctly clipped, it
176  // will be visible on the left half of the projection plane, in front of
177  // layer_a. When it is not clipped, its bounds will actually incorrectly
178  // appear much smaller and the correct sorting dependency will not be found.
179  gfx::Transform transform_b;
180  transform_b.Translate3d(0.f, 0.f, 0.7f);
181  transform_b.RotateAboutYAxis(45.0);
182  transform_b.Translate(-5.0, -5.0);
183  LayerShape layer_b(10.f, 10.f, perspective_matrix * transform_b);
184
185  // Sanity check that the test case actually covers the intended scenario,
186  // where part of layer B go behind the w = 0 plane.
187  gfx::QuadF test_quad = gfx::QuadF(gfx::RectF(-0.5f, -0.5f, 1.f, 1.f));
188  bool clipped = false;
189  MathUtil::MapQuad(perspective_matrix * transform_b, test_quad, &clipped);
190  ASSERT_TRUE(clipped);
191
192  overlap_result =
193      LayerSorter::CheckOverlap(&layer_a, &layer_b, z_threshold, &weight);
194  EXPECT_EQ(LayerSorter::ABeforeB, overlap_result);
195}
196
197TEST(LayerSorterTest, VerifyExistingOrderingPreservedWhenNoZDiff) {
198  // If there is no reason to re-sort the layers (i.e. no 3d z difference), then
199  // the existing ordering provided on input should be retained. This test
200  // covers the fix in https://bugs.webkit.org/show_bug.cgi?id=75046. Before
201  // this fix, ordering was accidentally reversed, causing bugs in z-index
202  // ordering on websites when preserves3D triggered the LayerSorter.
203
204  // Input list of layers: [1, 2, 3, 4, 5].
205  // Expected output: [3, 4, 1, 2, 5].
206  //    - 1, 2, and 5 do not have a 3d z difference, and therefore their
207  //      relative ordering should be retained.
208  //    - 3 and 4 do not have a 3d z difference, and therefore their relative
209  //      ordering should be retained.
210  //    - 3 and 4 should be re-sorted so they are in front of 1, 2, and 5.
211
212  FakeImplProxy proxy;
213  FakeLayerTreeHostImpl host_impl(&proxy);
214
215  scoped_ptr<LayerImpl> layer1 = LayerImpl::Create(host_impl.active_tree(), 1);
216  scoped_ptr<LayerImpl> layer2 = LayerImpl::Create(host_impl.active_tree(), 2);
217  scoped_ptr<LayerImpl> layer3 = LayerImpl::Create(host_impl.active_tree(), 3);
218  scoped_ptr<LayerImpl> layer4 = LayerImpl::Create(host_impl.active_tree(), 4);
219  scoped_ptr<LayerImpl> layer5 = LayerImpl::Create(host_impl.active_tree(), 5);
220
221  gfx::Transform BehindMatrix;
222  BehindMatrix.Translate3d(0.0, 0.0, 2.0);
223  gfx::Transform FrontMatrix;
224  FrontMatrix.Translate3d(0.0, 0.0, 1.0);
225
226  layer1->SetBounds(gfx::Size(10, 10));
227  layer1->SetContentBounds(gfx::Size(10, 10));
228  layer1->draw_properties().target_space_transform = BehindMatrix;
229  layer1->SetDrawsContent(true);
230
231  layer2->SetBounds(gfx::Size(20, 20));
232  layer2->SetContentBounds(gfx::Size(20, 20));
233  layer2->draw_properties().target_space_transform = BehindMatrix;
234  layer2->SetDrawsContent(true);
235
236  layer3->SetBounds(gfx::Size(30, 30));
237  layer3->SetContentBounds(gfx::Size(30, 30));
238  layer3->draw_properties().target_space_transform = FrontMatrix;
239  layer3->SetDrawsContent(true);
240
241  layer4->SetBounds(gfx::Size(40, 40));
242  layer4->SetContentBounds(gfx::Size(40, 40));
243  layer4->draw_properties().target_space_transform = FrontMatrix;
244  layer4->SetDrawsContent(true);
245
246  layer5->SetBounds(gfx::Size(50, 50));
247  layer5->SetContentBounds(gfx::Size(50, 50));
248  layer5->draw_properties().target_space_transform = BehindMatrix;
249  layer5->SetDrawsContent(true);
250
251  LayerImplList layer_list;
252  layer_list.push_back(layer1.get());
253  layer_list.push_back(layer2.get());
254  layer_list.push_back(layer3.get());
255  layer_list.push_back(layer4.get());
256  layer_list.push_back(layer5.get());
257
258  ASSERT_EQ(5u, layer_list.size());
259  EXPECT_EQ(1, layer_list[0]->id());
260  EXPECT_EQ(2, layer_list[1]->id());
261  EXPECT_EQ(3, layer_list[2]->id());
262  EXPECT_EQ(4, layer_list[3]->id());
263  EXPECT_EQ(5, layer_list[4]->id());
264
265  LayerSorter layer_sorter;
266  layer_sorter.Sort(layer_list.begin(), layer_list.end());
267
268  ASSERT_EQ(5u, layer_list.size());
269  EXPECT_EQ(3, layer_list[0]->id());
270  EXPECT_EQ(4, layer_list[1]->id());
271  EXPECT_EQ(1, layer_list[2]->id());
272  EXPECT_EQ(2, layer_list[3]->id());
273  EXPECT_EQ(5, layer_list[4]->id());
274}
275
276TEST(LayerSorterTest, VerifyConcidentLayerPrecisionLossResultsInDocumentOrder) {
277  FakeImplProxy proxy;
278  FakeLayerTreeHostImpl host_impl(&proxy);
279
280  scoped_ptr<LayerImpl> layer1 = LayerImpl::Create(host_impl.active_tree(), 1);
281  scoped_ptr<LayerImpl> layer2 = LayerImpl::Create(host_impl.active_tree(), 2);
282
283  // Layer 1 should occur before layer 2 in paint.  However, due to numeric
284  // issues in the sorter, it will put the layers in the wrong order
285  // in some situations.  Here we test a patch that results in  document
286  // order rather than calculated order when numeric percision is suspect
287  // in calculated order.
288
289  gfx::Transform BehindMatrix;
290  BehindMatrix.Translate3d(0.f, 0.f, 0.999999f);
291  BehindMatrix.RotateAboutXAxis(38.5);
292  BehindMatrix.RotateAboutYAxis(77.0);
293  gfx::Transform FrontMatrix;
294  FrontMatrix.Translate3d(0, 0, 1.0);
295  FrontMatrix.RotateAboutXAxis(38.5);
296  FrontMatrix.RotateAboutYAxis(77.0);
297
298  layer1->SetBounds(gfx::Size(10, 10));
299  layer1->SetContentBounds(gfx::Size(10, 10));
300  layer1->draw_properties().target_space_transform = BehindMatrix;
301  layer1->SetDrawsContent(true);
302
303  layer2->SetBounds(gfx::Size(10, 10));
304  layer2->SetContentBounds(gfx::Size(10, 10));
305  layer2->draw_properties().target_space_transform = FrontMatrix;
306  layer2->SetDrawsContent(true);
307
308  LayerImplList layer_list;
309  layer_list.push_back(layer1.get());
310  layer_list.push_back(layer2.get());
311
312  ASSERT_EQ(2u, layer_list.size());
313  EXPECT_EQ(1, layer_list[0]->id());
314  EXPECT_EQ(2, layer_list[1]->id());
315
316  LayerSorter layer_sorter;
317  layer_sorter.Sort(layer_list.begin(), layer_list.end());
318
319  ASSERT_EQ(2u, layer_list.size());
320  EXPECT_EQ(1, layer_list[0]->id());
321  EXPECT_EQ(2, layer_list[1]->id());
322}
323
324}  // namespace
325}  // namespace cc
326
327