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