summaryrefslogtreecommitdiffstats
path: root/cc/trees/layer_sorter_unittest.cc
blob: 74d3cf5a2cf32bc9ffb9b5d2fa24bbf59767cb98 (plain)
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
// Copyright 2011 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_sorter.h"

#include "cc/base/math_util.h"
#include "cc/layers/layer_impl.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/trees/single_thread_proxy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/transform.h"

namespace cc {
namespace {

// Note: In the following overlap tests, the "camera" is looking down the
// negative Z axis, meaning that layers with smaller z values (more negative)
// are further from the camera and therefore must be drawn before layers with
// higher z values.

TEST(LayerSorterTest, BasicOverlap) {
  LayerSorter::ABCompareResult overlap_result;
  const float z_threshold = 0.1f;
  float weight = 0.f;

  // Trivial test, with one layer directly obscuring the other.
  gfx::Transform neg4_translate;
  neg4_translate.Translate3d(0.0, 0.0, -4.0);
  LayerShape front(2.f, 2.f, neg4_translate);

  gfx::Transform neg5_translate;
  neg5_translate.Translate3d(0.0, 0.0, -5.0);
  LayerShape back(2.f, 2.f, neg5_translate);

  overlap_result =
      LayerSorter::CheckOverlap(&front, &back, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::B_BEFORE_A, overlap_result);
  EXPECT_EQ(1.f, weight);

  overlap_result =
      LayerSorter::CheckOverlap(&back, &front, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::A_BEFORE_B, overlap_result);
  EXPECT_EQ(1.f, weight);

  // One layer translated off to the right. No overlap should be detected.
  gfx::Transform right_translate;
  right_translate.Translate3d(10.0, 0.0, -5.0);
  LayerShape back_right(2.f, 2.f, right_translate);
  overlap_result =
      LayerSorter::CheckOverlap(&front, &back_right, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::NONE, overlap_result);

  // When comparing a layer with itself, z difference is always 0.
  overlap_result =
      LayerSorter::CheckOverlap(&front, &front, z_threshold, &weight);
  EXPECT_EQ(0.f, weight);
}

TEST(LayerSorterTest, RightAngleOverlap) {
  LayerSorter::ABCompareResult overlap_result;
  const float z_threshold = 0.1f;
  float weight = 0.f;

  gfx::Transform perspective_matrix;
  perspective_matrix.ApplyPerspectiveDepth(1000.0);

  // Two layers forming a right angle with a perspective viewing transform.
  gfx::Transform left_face_matrix;
  left_face_matrix.Translate3d(-1.0, 0.0, -5.0);
  left_face_matrix.RotateAboutYAxis(-90.0);
  left_face_matrix.Translate(-1.0, -1.0);
  LayerShape left_face(2.f, 2.f, perspective_matrix * left_face_matrix);
  gfx::Transform front_face_matrix;
  front_face_matrix.Translate3d(0.0, 0.0, -4.0);
  front_face_matrix.Translate(-1.0, -1.0);
  LayerShape front_face(2.f, 2.f, perspective_matrix * front_face_matrix);

  overlap_result =
      LayerSorter::CheckOverlap(&front_face, &left_face, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::B_BEFORE_A, overlap_result);
}

TEST(LayerSorterTest, IntersectingLayerOverlap) {
  LayerSorter::ABCompareResult overlap_result;
  const float z_threshold = 0.1f;
  float weight = 0.f;

  gfx::Transform perspective_matrix;
  perspective_matrix.ApplyPerspectiveDepth(1000.0);

  // Intersecting layers. An explicit order will be returned based on relative z
  // values at the overlapping features but the weight returned should be zero.
  gfx::Transform front_face_matrix;
  front_face_matrix.Translate3d(0.0, 0.0, -4.0);
  front_face_matrix.Translate(-1.0, -1.0);
  LayerShape front_face(2.f, 2.f, perspective_matrix * front_face_matrix);

  gfx::Transform through_matrix;
  through_matrix.Translate3d(0.0, 0.0, -4.0);
  through_matrix.RotateAboutYAxis(45.0);
  through_matrix.Translate(-1.0, -1.0);
  LayerShape rotated_face(2.f, 2.f, perspective_matrix * through_matrix);
  overlap_result = LayerSorter::CheckOverlap(&front_face,
                                             &rotated_face,
                                             z_threshold,
                                             &weight);
  EXPECT_NE(LayerSorter::NONE, overlap_result);
  EXPECT_EQ(0.f, weight);
}

TEST(LayerSorterTest, LayersAtAngleOverlap) {
  LayerSorter::ABCompareResult overlap_result;
  const float z_threshold = 0.1f;
  float weight = 0.f;

  // Trickier test with layers at an angle.
  //
  //   -x . . . . 0 . . . . +x
  // -z             /
  //  :            /----B----
  //  0           C
  //  : ----A----/
  // +z         /
  //
  // C is in front of A and behind B (not what you'd expect by comparing
  // centers). A and B don't overlap, so they're incomparable.

  gfx::Transform transform_a;
  transform_a.Translate3d(-6.0, 0.0, 1.0);
  transform_a.Translate(-4.0, -10.0);
  LayerShape layer_a(8.f, 20.f, transform_a);

  gfx::Transform transform_b;
  transform_b.Translate3d(6.0, 0.0, -1.0);
  transform_b.Translate(-4.0, -10.0);
  LayerShape layer_b(8.f, 20.f, transform_b);

  gfx::Transform transform_c;
  transform_c.RotateAboutYAxis(40.0);
  transform_c.Translate(-4.0, -10.0);
  LayerShape layer_c(8.f, 20.f, transform_c);

  overlap_result =
      LayerSorter::CheckOverlap(&layer_a, &layer_c, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::A_BEFORE_B, overlap_result);
  overlap_result =
      LayerSorter::CheckOverlap(&layer_c, &layer_b, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::A_BEFORE_B, overlap_result);
  overlap_result =
      LayerSorter::CheckOverlap(&layer_a, &layer_b, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::NONE, overlap_result);
}

TEST(LayerSorterTest, LayersUnderPathologicalPerspectiveTransform) {
  LayerSorter::ABCompareResult overlap_result;
  const float z_threshold = 0.1f;
  float weight = 0.f;

  // On perspective projection, if w becomes negative, the re-projected point
  // will be invalid and un-usable. Correct code needs to clip away portions of
  // the geometry where w < 0. If the code uses the invalid value, it will think
  // that a layer has different bounds than it really does, which can cause
  // things to sort incorrectly.

  gfx::Transform perspective_matrix;
  perspective_matrix.ApplyPerspectiveDepth(1);

  gfx::Transform transform_a;
  transform_a.Translate3d(-15.0, 0.0, -2.0);
  transform_a.Translate(-5.0, -5.0);
  LayerShape layer_a(10.f, 10.f, perspective_matrix * transform_a);

  // With this sequence of transforms, when layer B is correctly clipped, it
  // will be visible on the left half of the projection plane, in front of
  // layer_a. When it is not clipped, its bounds will actually incorrectly
  // appear much smaller and the correct sorting dependency will not be found.
  gfx::Transform transform_b;
  transform_b.Translate3d(0.f, 0.f, 0.7f);
  transform_b.RotateAboutYAxis(45.0);
  transform_b.Translate(-5.0, -5.0);
  LayerShape layer_b(10.f, 10.f, perspective_matrix * transform_b);

  // Sanity check that the test case actually covers the intended scenario,
  // where part of layer B go behind the w = 0 plane.
  gfx::QuadF test_quad = gfx::QuadF(gfx::RectF(-0.5f, -0.5f, 1.f, 1.f));
  bool clipped = false;
  MathUtil::MapQuad(perspective_matrix * transform_b, test_quad, &clipped);
  ASSERT_TRUE(clipped);

  overlap_result =
      LayerSorter::CheckOverlap(&layer_a, &layer_b, z_threshold, &weight);
  EXPECT_EQ(LayerSorter::A_BEFORE_B, overlap_result);
}

TEST(LayerSorterTest, VerifyExistingOrderingPreservedWhenNoZDiff) {
  // If there is no reason to re-sort the layers (i.e. no 3d z difference), then
  // the existing ordering provided on input should be retained. This test
  // covers the fix in https://bugs.webkit.org/show_bug.cgi?id=75046. Before
  // this fix, ordering was accidentally reversed, causing bugs in z-index
  // ordering on websites when preserves3D triggered the LayerSorter.

  // Input list of layers: [1, 2, 3, 4, 5].
  // Expected output: [3, 4, 1, 2, 5].
  //    - 1, 2, and 5 do not have a 3d z difference, and therefore their
  //      relative ordering should be retained.
  //    - 3 and 4 do not have a 3d z difference, and therefore their relative
  //      ordering should be retained.
  //    - 3 and 4 should be re-sorted so they are in front of 1, 2, and 5.

  FakeImplProxy proxy;
  TestSharedBitmapManager shared_bitmap_manager;
  FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);

  scoped_ptr<LayerImpl> layer1 = LayerImpl::Create(host_impl.active_tree(), 1);
  scoped_ptr<LayerImpl> layer2 = LayerImpl::Create(host_impl.active_tree(), 2);
  scoped_ptr<LayerImpl> layer3 = LayerImpl::Create(host_impl.active_tree(), 3);
  scoped_ptr<LayerImpl> layer4 = LayerImpl::Create(host_impl.active_tree(), 4);
  scoped_ptr<LayerImpl> layer5 = LayerImpl::Create(host_impl.active_tree(), 5);

  gfx::Transform BehindMatrix;
  BehindMatrix.Translate3d(0.0, 0.0, 2.0);
  gfx::Transform FrontMatrix;
  FrontMatrix.Translate3d(0.0, 0.0, 1.0);

  layer1->SetBounds(gfx::Size(10, 10));
  layer1->SetContentBounds(gfx::Size(10, 10));
  layer1->draw_properties().target_space_transform = BehindMatrix;
  layer1->SetDrawsContent(true);

  layer2->SetBounds(gfx::Size(20, 20));
  layer2->SetContentBounds(gfx::Size(20, 20));
  layer2->draw_properties().target_space_transform = BehindMatrix;
  layer2->SetDrawsContent(true);

  layer3->SetBounds(gfx::Size(30, 30));
  layer3->SetContentBounds(gfx::Size(30, 30));
  layer3->draw_properties().target_space_transform = FrontMatrix;
  layer3->SetDrawsContent(true);

  layer4->SetBounds(gfx::Size(40, 40));
  layer4->SetContentBounds(gfx::Size(40, 40));
  layer4->draw_properties().target_space_transform = FrontMatrix;
  layer4->SetDrawsContent(true);

  layer5->SetBounds(gfx::Size(50, 50));
  layer5->SetContentBounds(gfx::Size(50, 50));
  layer5->draw_properties().target_space_transform = BehindMatrix;
  layer5->SetDrawsContent(true);

  LayerImplList layer_list;
  layer_list.push_back(layer1.get());
  layer_list.push_back(layer2.get());
  layer_list.push_back(layer3.get());
  layer_list.push_back(layer4.get());
  layer_list.push_back(layer5.get());

  ASSERT_EQ(5u, layer_list.size());
  EXPECT_EQ(1, layer_list[0]->id());
  EXPECT_EQ(2, layer_list[1]->id());
  EXPECT_EQ(3, layer_list[2]->id());
  EXPECT_EQ(4, layer_list[3]->id());
  EXPECT_EQ(5, layer_list[4]->id());

  LayerSorter layer_sorter;
  layer_sorter.Sort(layer_list.begin(), layer_list.end());

  ASSERT_EQ(5u, layer_list.size());
  EXPECT_EQ(3, layer_list[0]->id());
  EXPECT_EQ(4, layer_list[1]->id());
  EXPECT_EQ(1, layer_list[2]->id());
  EXPECT_EQ(2, layer_list[3]->id());
  EXPECT_EQ(5, layer_list[4]->id());
}

TEST(LayerSorterTest, VerifyConcidentLayerPrecisionLossResultsInDocumentOrder) {
  FakeImplProxy proxy;
  TestSharedBitmapManager shared_bitmap_manager;
  FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);

  scoped_ptr<LayerImpl> layer1 = LayerImpl::Create(host_impl.active_tree(), 1);
  scoped_ptr<LayerImpl> layer2 = LayerImpl::Create(host_impl.active_tree(), 2);

  // Layer 1 should occur before layer 2 in paint.  However, due to numeric
  // issues in the sorter, it will put the layers in the wrong order
  // in some situations.  Here we test a patch that results in  document
  // order rather than calculated order when numeric percision is suspect
  // in calculated order.

  gfx::Transform BehindMatrix;
  BehindMatrix.Translate3d(0.f, 0.f, 0.999999f);
  BehindMatrix.RotateAboutXAxis(38.5);
  BehindMatrix.RotateAboutYAxis(77.0);
  gfx::Transform FrontMatrix;
  FrontMatrix.Translate3d(0, 0, 1.0);
  FrontMatrix.RotateAboutXAxis(38.5);
  FrontMatrix.RotateAboutYAxis(77.0);

  layer1->SetBounds(gfx::Size(10, 10));
  layer1->SetContentBounds(gfx::Size(10, 10));
  layer1->draw_properties().target_space_transform = BehindMatrix;
  layer1->SetDrawsContent(true);

  layer2->SetBounds(gfx::Size(10, 10));
  layer2->SetContentBounds(gfx::Size(10, 10));
  layer2->draw_properties().target_space_transform = FrontMatrix;
  layer2->SetDrawsContent(true);

  LayerImplList layer_list;
  layer_list.push_back(layer1.get());
  layer_list.push_back(layer2.get());

  ASSERT_EQ(2u, layer_list.size());
  EXPECT_EQ(1, layer_list[0]->id());
  EXPECT_EQ(2, layer_list[1]->id());

  LayerSorter layer_sorter;
  layer_sorter.Sort(layer_list.begin(), layer_list.end());

  ASSERT_EQ(2u, layer_list.size());
  EXPECT_EQ(1, layer_list[0]->id());
  EXPECT_EQ(2, layer_list[1]->id());
}

}  // namespace
}  // namespace cc