1 // Copyright 2012 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/layers/delegated_renderer_layer_impl.h" 6 7 #include "cc/base/scoped_ptr_vector.h" 8 #include "cc/layers/append_quads_data.h" 9 #include "cc/layers/quad_sink.h" 10 #include "cc/layers/solid_color_layer_impl.h" 11 #include "cc/quads/render_pass_draw_quad.h" 12 #include "cc/quads/solid_color_draw_quad.h" 13 #include "cc/test/fake_delegated_renderer_layer_impl.h" 14 #include "cc/test/fake_layer_tree_host_impl.h" 15 #include "cc/test/fake_layer_tree_host_impl_client.h" 16 #include "cc/test/fake_output_surface.h" 17 #include "cc/test/fake_proxy.h" 18 #include "cc/test/fake_rendering_stats_instrumentation.h" 19 #include "cc/test/geometry_test_utils.h" 20 #include "cc/test/mock_quad_culler.h" 21 #include "cc/test/render_pass_test_common.h" 22 #include "cc/test/render_pass_test_utils.h" 23 #include "cc/test/test_web_graphics_context_3d.h" 24 #include "cc/trees/layer_tree_host_impl.h" 25 #include "cc/trees/layer_tree_impl.h" 26 #include "cc/trees/single_thread_proxy.h" 27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "ui/gfx/frame_time.h" 29 #include "ui/gfx/transform.h" 30 31 namespace cc { 32 namespace { 33 34 class DelegatedRendererLayerImplTest : public testing::Test { 35 public: 36 DelegatedRendererLayerImplTest() 37 : proxy_(), 38 always_impl_thread_and_main_thread_blocked_(&proxy_) { 39 LayerTreeSettings settings; 40 settings.minimum_occlusion_tracking_size = gfx::Size(); 41 42 host_impl_.reset(new FakeLayerTreeHostImpl(settings, &proxy_)); 43 host_impl_->InitializeRenderer(CreateFakeOutputSurface()); 44 host_impl_->SetViewportSize(gfx::Size(10, 10)); 45 } 46 47 protected: 48 FakeProxy proxy_; 49 DebugScopedSetImplThreadAndMainThreadBlocked 50 always_impl_thread_and_main_thread_blocked_; 51 scoped_ptr<LayerTreeHostImpl> host_impl_; 52 }; 53 54 class DelegatedRendererLayerImplTestSimple 55 : public DelegatedRendererLayerImplTest { 56 public: 57 DelegatedRendererLayerImplTestSimple() 58 : DelegatedRendererLayerImplTest() { 59 scoped_ptr<LayerImpl> root_layer = SolidColorLayerImpl::Create( 60 host_impl_->active_tree(), 1).PassAs<LayerImpl>(); 61 scoped_ptr<LayerImpl> layer_before = SolidColorLayerImpl::Create( 62 host_impl_->active_tree(), 2).PassAs<LayerImpl>(); 63 scoped_ptr<LayerImpl> layer_after = SolidColorLayerImpl::Create( 64 host_impl_->active_tree(), 3).PassAs<LayerImpl>(); 65 scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer = 66 FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 4); 67 68 host_impl_->SetViewportSize(gfx::Size(100, 100)); 69 root_layer->SetBounds(gfx::Size(100, 100)); 70 71 layer_before->SetPosition(gfx::Point(20, 20)); 72 layer_before->SetBounds(gfx::Size(14, 14)); 73 layer_before->SetContentBounds(gfx::Size(14, 14)); 74 layer_before->SetDrawsContent(true); 75 layer_before->SetForceRenderSurface(true); 76 77 layer_after->SetPosition(gfx::Point(5, 5)); 78 layer_after->SetBounds(gfx::Size(15, 15)); 79 layer_after->SetContentBounds(gfx::Size(15, 15)); 80 layer_after->SetDrawsContent(true); 81 layer_after->SetForceRenderSurface(true); 82 83 delegated_renderer_layer->SetPosition(gfx::Point(3, 3)); 84 delegated_renderer_layer->SetBounds(gfx::Size(10, 10)); 85 delegated_renderer_layer->SetContentBounds(gfx::Size(10, 10)); 86 delegated_renderer_layer->SetDrawsContent(true); 87 gfx::Transform transform; 88 transform.Translate(1.0, 1.0); 89 delegated_renderer_layer->SetTransform(transform); 90 91 ScopedPtrVector<RenderPass> delegated_render_passes; 92 TestRenderPass* pass1 = AddRenderPass(&delegated_render_passes, 93 RenderPass::Id(9, 6), 94 gfx::Rect(6, 6, 6, 6), 95 gfx::Transform(1, 0, 0, 1, 5, 6)); 96 AddQuad(pass1, gfx::Rect(0, 0, 6, 6), 33u); 97 TestRenderPass* pass2 = AddRenderPass(&delegated_render_passes, 98 RenderPass::Id(9, 7), 99 gfx::Rect(7, 7, 7, 7), 100 gfx::Transform(1, 0, 0, 1, 7, 8)); 101 AddQuad(pass2, gfx::Rect(0, 0, 7, 7), 22u); 102 AddRenderPassQuad(pass2, pass1); 103 TestRenderPass* pass3 = AddRenderPass(&delegated_render_passes, 104 RenderPass::Id(9, 8), 105 gfx::Rect(0, 0, 8, 8), 106 gfx::Transform(1, 0, 0, 1, 9, 10)); 107 AddRenderPassQuad(pass3, pass2); 108 delegated_renderer_layer->SetFrameDataForRenderPasses( 109 &delegated_render_passes); 110 111 // The RenderPasses should be taken by the layer. 112 EXPECT_EQ(0u, delegated_render_passes.size()); 113 114 root_layer_ = root_layer.get(); 115 layer_before_ = layer_before.get(); 116 layer_after_ = layer_after.get(); 117 delegated_renderer_layer_ = delegated_renderer_layer.get(); 118 119 // Force the delegated RenderPasses to come before the RenderPass from 120 // layer_after. 121 layer_after->AddChild(delegated_renderer_layer.PassAs<LayerImpl>()); 122 root_layer->AddChild(layer_after.Pass()); 123 124 // Get the RenderPass generated by layer_before to come before the delegated 125 // RenderPasses. 126 root_layer->AddChild(layer_before.Pass()); 127 host_impl_->active_tree()->SetRootLayer(root_layer.Pass()); 128 } 129 130 protected: 131 LayerImpl* root_layer_; 132 LayerImpl* layer_before_; 133 LayerImpl* layer_after_; 134 DelegatedRendererLayerImpl* delegated_renderer_layer_; 135 }; 136 137 TEST_F(DelegatedRendererLayerImplTestSimple, AddsContributingRenderPasses) { 138 LayerTreeHostImpl::FrameData frame; 139 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 140 141 // Each non-DelegatedRendererLayer added one RenderPass. The 142 // DelegatedRendererLayer added two contributing passes. 143 ASSERT_EQ(5u, frame.render_passes.size()); 144 145 // The DelegatedRendererLayer should have added its contributing RenderPasses 146 // to the frame. 147 EXPECT_EQ(4, frame.render_passes[1]->id.layer_id); 148 EXPECT_EQ(1, frame.render_passes[1]->id.index); 149 EXPECT_EQ(4, frame.render_passes[2]->id.layer_id); 150 EXPECT_EQ(2, frame.render_passes[2]->id.index); 151 // And all other RenderPasses should be non-delegated. 152 EXPECT_NE(4, frame.render_passes[0]->id.layer_id); 153 EXPECT_EQ(0, frame.render_passes[0]->id.index); 154 EXPECT_NE(4, frame.render_passes[3]->id.layer_id); 155 EXPECT_EQ(0, frame.render_passes[3]->id.index); 156 EXPECT_NE(4, frame.render_passes[4]->id.layer_id); 157 EXPECT_EQ(0, frame.render_passes[4]->id.index); 158 159 // The DelegatedRendererLayer should have added its RenderPasses to the frame 160 // in order. 161 EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(), 162 frame.render_passes[1]->output_rect.ToString()); 163 EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(), 164 frame.render_passes[2]->output_rect.ToString()); 165 166 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 167 host_impl_->DidDrawAllLayers(frame); 168 } 169 170 TEST_F(DelegatedRendererLayerImplTestSimple, 171 AddsQuadsToContributingRenderPasses) { 172 LayerTreeHostImpl::FrameData frame; 173 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 174 175 // Each non-DelegatedRendererLayer added one RenderPass. The 176 // DelegatedRendererLayer added two contributing passes. 177 ASSERT_EQ(5u, frame.render_passes.size()); 178 179 // The DelegatedRendererLayer should have added its contributing RenderPasses 180 // to the frame. 181 EXPECT_EQ(4, frame.render_passes[1]->id.layer_id); 182 EXPECT_EQ(1, frame.render_passes[1]->id.index); 183 EXPECT_EQ(4, frame.render_passes[2]->id.layer_id); 184 EXPECT_EQ(2, frame.render_passes[2]->id.index); 185 186 // The DelegatedRendererLayer should have added copies of its quads to 187 // contributing RenderPasses. 188 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 189 EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(), 190 frame.render_passes[1]->quad_list[0]->rect.ToString()); 191 192 // Verify it added the right quads. 193 ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size()); 194 EXPECT_EQ(gfx::Rect(0, 0, 7, 7).ToString(), 195 frame.render_passes[2]->quad_list[0]->rect.ToString()); 196 EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(), 197 frame.render_passes[2]->quad_list[1]->rect.ToString()); 198 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 199 EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(), 200 frame.render_passes[1]->quad_list[0]->rect.ToString()); 201 202 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 203 host_impl_->DidDrawAllLayers(frame); 204 } 205 206 TEST_F(DelegatedRendererLayerImplTestSimple, AddsQuadsToTargetRenderPass) { 207 LayerTreeHostImpl::FrameData frame; 208 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 209 210 // Each non-DelegatedRendererLayer added one RenderPass. The 211 // DelegatedRendererLayer added two contributing passes. 212 ASSERT_EQ(5u, frame.render_passes.size()); 213 214 // The layer's target is the RenderPass from layer_after_. 215 EXPECT_EQ(RenderPass::Id(3, 0), frame.render_passes[3]->id); 216 217 // The DelegatedRendererLayer should have added copies of quads in its root 218 // RenderPass to its target RenderPass. The layer_after_ also adds one quad. 219 ASSERT_EQ(2u, frame.render_passes[3]->quad_list.size()); 220 221 // Verify it added the right quads. 222 EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(), 223 frame.render_passes[3]->quad_list[0]->rect.ToString()); 224 225 // Its target layer should have a quad as well. 226 EXPECT_EQ(gfx::Rect(0, 0, 15, 15).ToString(), 227 frame.render_passes[3]->quad_list[1]->rect.ToString()); 228 229 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 230 host_impl_->DidDrawAllLayers(frame); 231 } 232 233 TEST_F(DelegatedRendererLayerImplTestSimple, 234 QuadsFromRootRenderPassAreModifiedForTheTarget) { 235 LayerTreeHostImpl::FrameData frame; 236 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 237 238 // Each non-DelegatedRendererLayer added one RenderPass. The 239 // DelegatedRendererLayer added two contributing passes. 240 ASSERT_EQ(5u, frame.render_passes.size()); 241 242 // The DelegatedRendererLayer is at position 3,3 compared to its target, and 243 // has a translation transform of 1,1. So its root RenderPass' quads should 244 // all be transformed by that combined amount. 245 // The DelegatedRendererLayer has a size of 10x10, but the root delegated 246 // RenderPass has a size of 8x8, so any quads should be scaled by 10/8. 247 gfx::Transform transform; 248 transform.Translate(4.0, 4.0); 249 transform.Scale(10.0 / 8.0, 10.0 / 8.0); 250 EXPECT_TRANSFORMATION_MATRIX_EQ( 251 transform, frame.render_passes[3]->quad_list[0]->quadTransform()); 252 253 // Quads from non-root RenderPasses should not be shifted though. 254 ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size()); 255 EXPECT_TRANSFORMATION_MATRIX_EQ( 256 gfx::Transform(), frame.render_passes[2]->quad_list[0]->quadTransform()); 257 EXPECT_TRANSFORMATION_MATRIX_EQ( 258 gfx::Transform(), frame.render_passes[2]->quad_list[1]->quadTransform()); 259 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 260 EXPECT_TRANSFORMATION_MATRIX_EQ( 261 gfx::Transform(), frame.render_passes[1]->quad_list[0]->quadTransform()); 262 263 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 264 host_impl_->DidDrawAllLayers(frame); 265 } 266 267 TEST_F(DelegatedRendererLayerImplTestSimple, RenderPassTransformIsModified) { 268 LayerTreeHostImpl::FrameData frame; 269 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 270 271 // The delegated layer has a surface between it and the root. 272 EXPECT_TRUE(delegated_renderer_layer_->render_target()->parent()); 273 274 // Each non-DelegatedRendererLayer added one RenderPass. The 275 // DelegatedRendererLayer added two contributing passes. 276 ASSERT_EQ(5u, frame.render_passes.size()); 277 278 // The DelegatedRendererLayer is at position 9,9 compared to the root, so all 279 // render pass' transforms to the root should be shifted by this amount. 280 // The DelegatedRendererLayer has a size of 10x10, but the root delegated 281 // RenderPass has a size of 8x8, so any render passes should be scaled by 282 // 10/8. 283 gfx::Transform transform; 284 transform.Translate(9.0, 9.0); 285 transform.Scale(10.0 / 8.0, 10.0 / 8.0); 286 287 // The first contributing surface has a translation of 5, 6. 288 gfx::Transform five_six(1, 0, 0, 1, 5, 6); 289 290 // The second contributing surface has a translation of 7, 8. 291 gfx::Transform seven_eight(1, 0, 0, 1, 7, 8); 292 293 EXPECT_TRANSFORMATION_MATRIX_EQ( 294 transform * five_six, frame.render_passes[1]->transform_to_root_target); 295 EXPECT_TRANSFORMATION_MATRIX_EQ( 296 transform * seven_eight, 297 frame.render_passes[2]->transform_to_root_target); 298 299 host_impl_->DrawLayers(&frame, base::TimeTicks::Now()); 300 host_impl_->DidDrawAllLayers(frame); 301 } 302 303 TEST_F(DelegatedRendererLayerImplTestSimple, DoesNotOwnARenderSurface) { 304 LayerTreeHostImpl::FrameData frame; 305 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 306 307 // If the DelegatedRendererLayer is axis aligned and has opacity 1, then it 308 // has no need to be a RenderSurface for the quads it carries. 309 EXPECT_FALSE(delegated_renderer_layer_->render_surface()); 310 311 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 312 host_impl_->DidDrawAllLayers(frame); 313 } 314 315 TEST_F(DelegatedRendererLayerImplTestSimple, DoesOwnARenderSurfaceForOpacity) { 316 delegated_renderer_layer_->SetOpacity(0.5f); 317 318 LayerTreeHostImpl::FrameData frame; 319 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 320 321 // This test case has quads from multiple layers in the delegated renderer, so 322 // if the DelegatedRendererLayer has opacity < 1, it should end up with a 323 // render surface. 324 EXPECT_TRUE(delegated_renderer_layer_->render_surface()); 325 326 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 327 host_impl_->DidDrawAllLayers(frame); 328 } 329 330 TEST_F(DelegatedRendererLayerImplTestSimple, 331 DoesOwnARenderSurfaceForTransform) { 332 gfx::Transform rotation; 333 rotation.RotateAboutZAxis(30.0); 334 delegated_renderer_layer_->SetTransform(rotation); 335 336 LayerTreeHostImpl::FrameData frame; 337 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 338 339 // This test case has quads from multiple layers in the delegated renderer, so 340 // if the DelegatedRendererLayer has opacity < 1, it should end up with a 341 // render surface. 342 EXPECT_TRUE(delegated_renderer_layer_->render_surface()); 343 344 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 345 host_impl_->DidDrawAllLayers(frame); 346 } 347 348 class DelegatedRendererLayerImplTestOwnSurface 349 : public DelegatedRendererLayerImplTestSimple { 350 public: 351 DelegatedRendererLayerImplTestOwnSurface() 352 : DelegatedRendererLayerImplTestSimple() { 353 delegated_renderer_layer_->SetForceRenderSurface(true); 354 } 355 }; 356 357 TEST_F(DelegatedRendererLayerImplTestOwnSurface, AddsRenderPasses) { 358 LayerTreeHostImpl::FrameData frame; 359 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 360 361 // Each non-DelegatedRendererLayer added one RenderPass. The 362 // DelegatedRendererLayer added two contributing passes and its owned surface 363 // added one pass. 364 ASSERT_EQ(6u, frame.render_passes.size()); 365 366 // The DelegatedRendererLayer should have added its contributing RenderPasses 367 // to the frame. 368 EXPECT_EQ(4, frame.render_passes[1]->id.layer_id); 369 EXPECT_EQ(1, frame.render_passes[1]->id.index); 370 EXPECT_EQ(4, frame.render_passes[2]->id.layer_id); 371 EXPECT_EQ(2, frame.render_passes[2]->id.index); 372 // The DelegatedRendererLayer should have added a RenderPass for its surface 373 // to the frame. 374 EXPECT_EQ(4, frame.render_passes[1]->id.layer_id); 375 EXPECT_EQ(0, frame.render_passes[3]->id.index); 376 // And all other RenderPasses should be non-delegated. 377 EXPECT_NE(4, frame.render_passes[0]->id.layer_id); 378 EXPECT_EQ(0, frame.render_passes[0]->id.index); 379 EXPECT_NE(4, frame.render_passes[4]->id.layer_id); 380 EXPECT_EQ(0, frame.render_passes[4]->id.index); 381 EXPECT_NE(4, frame.render_passes[5]->id.layer_id); 382 EXPECT_EQ(0, frame.render_passes[5]->id.index); 383 384 // The DelegatedRendererLayer should have added its RenderPasses to the frame 385 // in order. 386 EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(), 387 frame.render_passes[1]->output_rect.ToString()); 388 EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(), 389 frame.render_passes[2]->output_rect.ToString()); 390 391 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 392 host_impl_->DidDrawAllLayers(frame); 393 } 394 395 TEST_F(DelegatedRendererLayerImplTestOwnSurface, 396 AddsQuadsToContributingRenderPasses) { 397 LayerTreeHostImpl::FrameData frame; 398 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 399 400 // Each non-DelegatedRendererLayer added one RenderPass. The 401 // DelegatedRendererLayer added two contributing passes and its owned surface 402 // added one pass. 403 ASSERT_EQ(6u, frame.render_passes.size()); 404 405 // The DelegatedRendererLayer should have added its contributing RenderPasses 406 // to the frame. 407 EXPECT_EQ(4, frame.render_passes[1]->id.layer_id); 408 EXPECT_EQ(1, frame.render_passes[1]->id.index); 409 EXPECT_EQ(4, frame.render_passes[2]->id.layer_id); 410 EXPECT_EQ(2, frame.render_passes[2]->id.index); 411 412 // The DelegatedRendererLayer should have added copies of its quads to 413 // contributing RenderPasses. 414 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 415 EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(), 416 frame.render_passes[1]->quad_list[0]->rect.ToString()); 417 418 // Verify it added the right quads. 419 ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size()); 420 EXPECT_EQ(gfx::Rect(0, 0, 7, 7).ToString(), 421 frame.render_passes[2]->quad_list[0]->rect.ToString()); 422 EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(), 423 frame.render_passes[2]->quad_list[1]->rect.ToString()); 424 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 425 EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(), 426 frame.render_passes[1]->quad_list[0]->rect.ToString()); 427 428 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 429 host_impl_->DidDrawAllLayers(frame); 430 } 431 432 TEST_F(DelegatedRendererLayerImplTestOwnSurface, AddsQuadsToTargetRenderPass) { 433 LayerTreeHostImpl::FrameData frame; 434 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 435 436 // Each non-DelegatedRendererLayer added one RenderPass. The 437 // DelegatedRendererLayer added two contributing passes and its owned surface 438 // added one pass. 439 ASSERT_EQ(6u, frame.render_passes.size()); 440 441 // The layer's target is the RenderPass owned by itself. 442 EXPECT_EQ(RenderPass::Id(4, 0), frame.render_passes[3]->id); 443 444 // The DelegatedRendererLayer should have added copies of quads in its root 445 // RenderPass to its target RenderPass. 446 // The layer_after also adds one quad. 447 ASSERT_EQ(1u, frame.render_passes[3]->quad_list.size()); 448 449 // Verify it added the right quads. 450 EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(), 451 frame.render_passes[3]->quad_list[0]->rect.ToString()); 452 453 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 454 host_impl_->DidDrawAllLayers(frame); 455 } 456 457 TEST_F(DelegatedRendererLayerImplTestOwnSurface, 458 QuadsFromRootRenderPassAreNotModifiedForTheTarget) { 459 LayerTreeHostImpl::FrameData frame; 460 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 461 462 // Each non-DelegatedRendererLayer added one RenderPass. The 463 // DelegatedRendererLayer added two contributing passes and its owned surface 464 // added one pass. 465 ASSERT_EQ(6u, frame.render_passes.size()); 466 467 // Because the DelegatedRendererLayer owns a RenderSurfaceImpl, its root 468 // RenderPass' quads do not need to be translated at all. However, they are 469 // scaled from the frame's size (8x8) to the layer's bounds (10x10). 470 gfx::Transform transform; 471 transform.Scale(10.0 / 8.0, 10.0 / 8.0); 472 EXPECT_TRANSFORMATION_MATRIX_EQ( 473 transform, frame.render_passes[3]->quad_list[0]->quadTransform()); 474 475 // Quads from non-root RenderPasses should not be shifted either. 476 ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size()); 477 EXPECT_TRANSFORMATION_MATRIX_EQ( 478 gfx::Transform(), frame.render_passes[2]->quad_list[0]->quadTransform()); 479 EXPECT_TRANSFORMATION_MATRIX_EQ( 480 gfx::Transform(), frame.render_passes[2]->quad_list[1]->quadTransform()); 481 ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size()); 482 EXPECT_TRANSFORMATION_MATRIX_EQ( 483 gfx::Transform(), frame.render_passes[1]->quad_list[0]->quadTransform()); 484 485 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 486 host_impl_->DidDrawAllLayers(frame); 487 } 488 489 class DelegatedRendererLayerImplTestTransform 490 : public DelegatedRendererLayerImplTest { 491 public: 492 void SetUpTest() { 493 host_impl_->SetDeviceScaleFactor(2.f); 494 495 scoped_ptr<LayerImpl> root_layer = LayerImpl::Create( 496 host_impl_->active_tree(), 1); 497 scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer = 498 FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 2); 499 500 host_impl_->SetViewportSize(gfx::Size(200, 200)); 501 root_layer->SetBounds(gfx::Size(100, 100)); 502 503 delegated_renderer_layer->SetPosition(gfx::Point(20, 20)); 504 delegated_renderer_layer->SetBounds(gfx::Size(75, 75)); 505 delegated_renderer_layer->SetContentBounds(gfx::Size(75, 75)); 506 delegated_renderer_layer->SetDrawsContent(true); 507 gfx::Transform transform; 508 transform.Scale(2.0, 2.0); 509 transform.Translate(8.0, 8.0); 510 delegated_renderer_layer->SetTransform(transform); 511 512 ScopedPtrVector<RenderPass> delegated_render_passes; 513 514 gfx::Size child_pass_content_bounds(7, 7); 515 gfx::Rect child_pass_rect(20, 20, 7, 7); 516 gfx::Transform child_pass_transform; 517 child_pass_transform.Scale(0.8f, 0.8f); 518 child_pass_transform.Translate(9.0, 9.0); 519 gfx::Rect child_pass_clip_rect(21, 21, 3, 3); 520 bool child_pass_clipped = false; 521 522 { 523 TestRenderPass* pass = AddRenderPass( 524 &delegated_render_passes, 525 RenderPass::Id(10, 7), 526 child_pass_rect, 527 gfx::Transform()); 528 MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list); 529 AppendQuadsData data(pass->id); 530 SharedQuadState* shared_quad_state = quad_sink.UseSharedQuadState( 531 SharedQuadState::Create()); 532 shared_quad_state->SetAll(child_pass_transform, 533 child_pass_content_bounds, 534 child_pass_rect, 535 child_pass_clip_rect, 536 child_pass_clipped, 537 1.f, 538 SkXfermode::kSrcOver_Mode); 539 540 scoped_ptr<SolidColorDrawQuad> color_quad; 541 color_quad = SolidColorDrawQuad::Create(); 542 color_quad->SetNew(shared_quad_state, gfx::Rect(20, 20, 3, 7), 1u, false); 543 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 544 545 color_quad = SolidColorDrawQuad::Create(); 546 color_quad->SetNew(shared_quad_state, gfx::Rect(23, 20, 4, 7), 1u, false); 547 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 548 } 549 550 gfx::Size root_pass_content_bounds(100, 100); 551 gfx::Rect root_pass_rect(0, 0, 100, 100); 552 gfx::Transform root_pass_transform; 553 root_pass_transform.Scale(1.5, 1.5); 554 root_pass_transform.Translate(7.0, 7.0); 555 gfx::Rect root_pass_clip_rect(10, 10, 35, 35); 556 bool root_pass_clipped = root_delegated_render_pass_is_clipped_; 557 558 TestRenderPass* pass = AddRenderPass( 559 &delegated_render_passes, 560 RenderPass::Id(9, 6), 561 root_pass_rect, 562 gfx::Transform()); 563 MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list); 564 AppendQuadsData data(pass->id); 565 SharedQuadState* shared_quad_state = 566 quad_sink.UseSharedQuadState(SharedQuadState::Create()); 567 shared_quad_state->SetAll(root_pass_transform, 568 root_pass_content_bounds, 569 root_pass_rect, 570 root_pass_clip_rect, 571 root_pass_clipped, 572 1.f, 573 SkXfermode::kSrcOver_Mode); 574 575 scoped_ptr<RenderPassDrawQuad> render_pass_quad = 576 RenderPassDrawQuad::Create(); 577 render_pass_quad->SetNew( 578 shared_quad_state, 579 gfx::Rect(5, 5, 7, 7), // rect 580 RenderPass::Id(10, 7), // render_pass_id 581 false, // is_replica 582 0, // mask_resource_id 583 child_pass_rect, // contents_changed_since_last_frame 584 gfx::RectF(), // mask_uv_rect 585 FilterOperations(), // filters 586 FilterOperations()); // background_filters 587 quad_sink.Append(render_pass_quad.PassAs<DrawQuad>(), &data); 588 589 scoped_ptr<SolidColorDrawQuad> color_quad; 590 color_quad = SolidColorDrawQuad::Create(); 591 color_quad->SetNew(shared_quad_state, gfx::Rect(0, 0, 10, 10), 1u, false); 592 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 593 594 color_quad = SolidColorDrawQuad::Create(); 595 color_quad->SetNew(shared_quad_state, gfx::Rect(0, 10, 10, 10), 2u, false); 596 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 597 598 color_quad = SolidColorDrawQuad::Create(); 599 color_quad->SetNew(shared_quad_state, gfx::Rect(10, 0, 10, 10), 3u, false); 600 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 601 602 color_quad = SolidColorDrawQuad::Create(); 603 color_quad->SetNew(shared_quad_state, gfx::Rect(10, 10, 10, 10), 4u, false); 604 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 605 606 delegated_renderer_layer->SetFrameDataForRenderPasses( 607 &delegated_render_passes); 608 609 // The RenderPasses should be taken by the layer. 610 EXPECT_EQ(0u, delegated_render_passes.size()); 611 612 root_layer_ = root_layer.get(); 613 delegated_renderer_layer_ = delegated_renderer_layer.get(); 614 615 root_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>()); 616 host_impl_->active_tree()->SetRootLayer(root_layer.Pass()); 617 } 618 619 void VerifyRenderPasses( 620 const LayerTreeHostImpl::FrameData& frame, 621 size_t num_render_passes, 622 const SharedQuadState** root_delegated_shared_quad_state, 623 const SharedQuadState** contrib_delegated_shared_quad_state) { 624 ASSERT_EQ(num_render_passes, frame.render_passes.size()); 625 // The contributing render pass in the DelegatedRendererLayer. 626 EXPECT_EQ(2, frame.render_passes[0]->id.layer_id); 627 EXPECT_EQ(1, frame.render_passes[0]->id.index); 628 // The root render pass. 629 EXPECT_EQ(1, frame.render_passes.back()->id.layer_id); 630 EXPECT_EQ(0, frame.render_passes.back()->id.index); 631 632 const QuadList& contrib_delegated_quad_list = 633 frame.render_passes[0]->quad_list; 634 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 635 636 const QuadList& root_delegated_quad_list = 637 frame.render_passes[1]->quad_list; 638 ASSERT_EQ(5u, root_delegated_quad_list.size()); 639 640 // All quads in a render pass should share the same state. 641 *contrib_delegated_shared_quad_state = 642 contrib_delegated_quad_list[0]->shared_quad_state; 643 EXPECT_EQ(*contrib_delegated_shared_quad_state, 644 contrib_delegated_quad_list[1]->shared_quad_state); 645 646 *root_delegated_shared_quad_state = 647 root_delegated_quad_list[0]->shared_quad_state; 648 EXPECT_EQ(*root_delegated_shared_quad_state, 649 root_delegated_quad_list[1]->shared_quad_state); 650 EXPECT_EQ(*root_delegated_shared_quad_state, 651 root_delegated_quad_list[2]->shared_quad_state); 652 EXPECT_EQ(*root_delegated_shared_quad_state, 653 root_delegated_quad_list[3]->shared_quad_state); 654 EXPECT_EQ(*root_delegated_shared_quad_state, 655 root_delegated_quad_list[4]->shared_quad_state); 656 657 EXPECT_NE(*contrib_delegated_shared_quad_state, 658 *root_delegated_shared_quad_state); 659 } 660 661 protected: 662 LayerImpl* root_layer_; 663 DelegatedRendererLayerImpl* delegated_renderer_layer_; 664 bool root_delegated_render_pass_is_clipped_; 665 }; 666 667 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsUnclipped_NoSurface) { 668 root_delegated_render_pass_is_clipped_ = false; 669 SetUpTest(); 670 671 LayerTreeHostImpl::FrameData frame; 672 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 673 674 const SharedQuadState* root_delegated_shared_quad_state = NULL; 675 const SharedQuadState* contrib_delegated_shared_quad_state = NULL; 676 VerifyRenderPasses( 677 frame, 678 2, 679 &root_delegated_shared_quad_state, 680 &contrib_delegated_shared_quad_state); 681 682 // When the quads don't have a clip of their own, the clip rect is set to 683 // the drawable_content_rect of the delegated renderer layer. 684 EXPECT_EQ(delegated_renderer_layer_->drawable_content_rect().ToString(), 685 root_delegated_shared_quad_state->clip_rect.ToString()); 686 687 // Even though the quads in the root pass have no clip of their own, they 688 // inherit the clip rect from the delegated renderer layer if it does not 689 // own a surface. 690 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 691 692 gfx::Transform expected; 693 // Device scale factor is 2. 694 expected.Scale(2.0, 2.0); 695 // This is the transform from the layer's space to its target. 696 // The position (20) - the width / scale (75 / 2) = 20 - 37.5 = -17.5 697 expected.Translate(-17.5, -17.5); 698 expected.Scale(2.0, 2.0); 699 expected.Translate(8.0, 8.0); 700 // The frame has size 100x100 but the layer's bounds are 75x75. 701 expected.Scale(75.0 / 100.0, 75.0 / 100.0); 702 // This is the transform within the source frame. 703 expected.Scale(1.5, 1.5); 704 expected.Translate(7.0, 7.0); 705 EXPECT_TRANSFORMATION_MATRIX_EQ( 706 expected, root_delegated_shared_quad_state->content_to_target_transform); 707 708 // The contributing render pass should not be transformed from its input. 709 EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(), 710 contrib_delegated_shared_quad_state->clip_rect.ToString()); 711 EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped); 712 expected.MakeIdentity(); 713 expected.Scale(0.8f, 0.8f); 714 expected.Translate(9.0, 9.0); 715 EXPECT_TRANSFORMATION_MATRIX_EQ( 716 expected, 717 contrib_delegated_shared_quad_state->content_to_target_transform); 718 719 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 720 host_impl_->DidDrawAllLayers(frame); 721 } 722 723 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsClipped_NoSurface) { 724 root_delegated_render_pass_is_clipped_ = true; 725 SetUpTest(); 726 727 LayerTreeHostImpl::FrameData frame; 728 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 729 730 const SharedQuadState* root_delegated_shared_quad_state = NULL; 731 const SharedQuadState* contrib_delegated_shared_quad_state = NULL; 732 VerifyRenderPasses( 733 frame, 734 2, 735 &root_delegated_shared_quad_state, 736 &contrib_delegated_shared_quad_state); 737 738 // Since the quads have a clip_rect it should be modified by delegated 739 // renderer layer's draw_transform. 740 // The position of the resulting clip_rect is: 741 // (clip rect position (10) * scale to layer (75/100) + translate (8)) * 742 // layer scale (2) + layer position (20) = 51 743 // But the layer is centered, so: 51 - (75 / 2) = 51 - 75 / 2 = 13.5 744 // The device scale is 2, so everything gets doubled, giving 27. 745 // 746 // The size is 35x35 scaled to fit inside the layer's bounds at 75x75 from 747 // a frame at 100x100: 35 * 2 (device scale) * 75 / 100 = 52.5. The device 748 // scale doubles this to 105. 749 EXPECT_EQ(gfx::Rect(27, 27, 105, 105).ToString(), 750 root_delegated_shared_quad_state->clip_rect.ToString()); 751 752 // The quads had a clip and it should be preserved. 753 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 754 755 gfx::Transform expected; 756 // Device scale factor is 2. 757 expected.Scale(2.0, 2.0); 758 // This is the transform from the layer's space to its target. 759 // The position (20) - the width / scale (75 / 2) = 20 - 37.5 = -17.5 760 expected.Translate(-17.5, -17.5); 761 expected.Scale(2.0, 2.0); 762 expected.Translate(8.0, 8.0); 763 // The frame has size 100x100 but the layer's bounds are 75x75. 764 expected.Scale(75.0 / 100.0, 75.0 / 100.0); 765 // This is the transform within the source frame. 766 expected.Scale(1.5, 1.5); 767 expected.Translate(7.0, 7.0); 768 EXPECT_TRANSFORMATION_MATRIX_EQ( 769 expected, root_delegated_shared_quad_state->content_to_target_transform); 770 771 // The contributing render pass should not be transformed from its input. 772 EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(), 773 contrib_delegated_shared_quad_state->clip_rect.ToString()); 774 EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped); 775 expected.MakeIdentity(); 776 expected.Scale(0.8f, 0.8f); 777 expected.Translate(9.0, 9.0); 778 EXPECT_TRANSFORMATION_MATRIX_EQ( 779 expected, 780 contrib_delegated_shared_quad_state->content_to_target_transform); 781 782 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 783 host_impl_->DidDrawAllLayers(frame); 784 } 785 786 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsUnclipped_Surface) { 787 root_delegated_render_pass_is_clipped_ = false; 788 SetUpTest(); 789 790 delegated_renderer_layer_->SetForceRenderSurface(true); 791 792 LayerTreeHostImpl::FrameData frame; 793 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 794 795 const SharedQuadState* root_delegated_shared_quad_state = NULL; 796 const SharedQuadState* contrib_delegated_shared_quad_state = NULL; 797 VerifyRenderPasses( 798 frame, 799 3, 800 &root_delegated_shared_quad_state, 801 &contrib_delegated_shared_quad_state); 802 803 // When the layer owns a surface, then its position and translation are not 804 // a part of its draw transform. 805 // The position of the resulting clip_rect is: 806 // (clip rect position (10) * scale to layer (75/100)) * device scale (2) = 15 807 // The size is 35x35 scaled to fit inside the layer's bounds at 75x75 from 808 // a frame at 100x100: 35 * 2 (device scale) * 75 / 100 = 52.5. 809 EXPECT_EQ(gfx::Rect(15, 15, 53, 53).ToString(), 810 root_delegated_shared_quad_state->clip_rect.ToString()); 811 812 // Since the layer owns a surface it doesn't need to clip its quads, so 813 // unclipped quads remain unclipped. 814 EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped); 815 816 gfx::Transform expected; 817 // Device scale factor is 2. 818 expected.Scale(2.0, 2.0); 819 // The frame has size 100x100 but the layer's bounds are 75x75. 820 expected.Scale(75.0 / 100.0, 75.0 / 100.0); 821 // This is the transform within the source frame. 822 expected.Scale(1.5, 1.5); 823 expected.Translate(7.0, 7.0); 824 EXPECT_TRANSFORMATION_MATRIX_EQ( 825 expected, root_delegated_shared_quad_state->content_to_target_transform); 826 827 // The contributing render pass should not be transformed from its input. 828 EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(), 829 contrib_delegated_shared_quad_state->clip_rect.ToString()); 830 EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped); 831 expected.MakeIdentity(); 832 expected.Scale(0.8f, 0.8f); 833 expected.Translate(9.0, 9.0); 834 EXPECT_TRANSFORMATION_MATRIX_EQ( 835 expected, 836 contrib_delegated_shared_quad_state->content_to_target_transform); 837 838 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 839 host_impl_->DidDrawAllLayers(frame); 840 } 841 842 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsClipped_Surface) { 843 root_delegated_render_pass_is_clipped_ = true; 844 SetUpTest(); 845 846 delegated_renderer_layer_->SetForceRenderSurface(true); 847 848 LayerTreeHostImpl::FrameData frame; 849 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 850 851 const SharedQuadState* root_delegated_shared_quad_state = NULL; 852 const SharedQuadState* contrib_delegated_shared_quad_state = NULL; 853 VerifyRenderPasses( 854 frame, 855 3, 856 &root_delegated_shared_quad_state, 857 &contrib_delegated_shared_quad_state); 858 859 // When the layer owns a surface, then its position and translation are not 860 // a part of its draw transform. 861 // The position of the resulting clip_rect is: 862 // (clip rect position (10) * scale to layer (75/100)) * device scale (2) = 15 863 // The size is 35x35 scaled to fit inside the layer's bounds at 75x75 from 864 // a frame at 100x100: 35 * 2 (device scale) * 75 / 100 = 52.5. 865 EXPECT_EQ(gfx::Rect(15, 15, 53, 53).ToString(), 866 root_delegated_shared_quad_state->clip_rect.ToString()); 867 868 // The quads had a clip and it should be preserved. 869 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 870 871 gfx::Transform expected; 872 // Device scale factor is 2. 873 expected.Scale(2.0, 2.0); 874 // The frame has size 100x100 but the layer's bounds are 75x75. 875 expected.Scale(75.0 / 100.0, 75.0 / 100.0); 876 // This is the transform within the source frame. 877 expected.Scale(1.5, 1.5); 878 expected.Translate(7.0, 7.0); 879 EXPECT_TRANSFORMATION_MATRIX_EQ( 880 expected, root_delegated_shared_quad_state->content_to_target_transform); 881 882 // The contributing render pass should not be transformed from its input. 883 EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(), 884 contrib_delegated_shared_quad_state->clip_rect.ToString()); 885 EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped); 886 expected.MakeIdentity(); 887 expected.Scale(0.8f, 0.8f); 888 expected.Translate(9.0, 9.0); 889 EXPECT_TRANSFORMATION_MATRIX_EQ( 890 expected, 891 contrib_delegated_shared_quad_state->content_to_target_transform); 892 893 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 894 host_impl_->DidDrawAllLayers(frame); 895 } 896 897 class DelegatedRendererLayerImplTestClip 898 : public DelegatedRendererLayerImplTest { 899 public: 900 void SetUpTest() { 901 scoped_ptr<LayerImpl> root_layer = 902 LayerImpl::Create(host_impl_->active_tree(), 1); 903 scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer = 904 FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 2); 905 scoped_ptr<LayerImpl> clip_layer = 906 LayerImpl::Create(host_impl_->active_tree(), 3); 907 scoped_ptr<LayerImpl> origin_layer = 908 LayerImpl::Create(host_impl_->active_tree(), 4); 909 910 host_impl_->SetViewportSize(gfx::Size(100, 100)); 911 root_layer->SetBounds(gfx::Size(100, 100)); 912 913 delegated_renderer_layer->SetPosition(gfx::Point(20, 20)); 914 delegated_renderer_layer->SetBounds(gfx::Size(50, 50)); 915 delegated_renderer_layer->SetContentBounds(gfx::Size(50, 50)); 916 delegated_renderer_layer->SetDrawsContent(true); 917 918 ScopedPtrVector<RenderPass> delegated_render_passes; 919 920 gfx::Size child_pass_content_bounds(7, 7); 921 gfx::Rect child_pass_rect(20, 20, 7, 7); 922 gfx::Transform child_pass_transform; 923 gfx::Rect child_pass_clip_rect(21, 21, 3, 3); 924 bool child_pass_clipped = false; 925 926 { 927 TestRenderPass* pass = AddRenderPass( 928 &delegated_render_passes, 929 RenderPass::Id(10, 7), 930 child_pass_rect, 931 gfx::Transform()); 932 MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list); 933 AppendQuadsData data(pass->id); 934 SharedQuadState* shared_quad_state = 935 quad_sink.UseSharedQuadState(SharedQuadState::Create()); 936 shared_quad_state->SetAll(child_pass_transform, 937 child_pass_content_bounds, 938 child_pass_rect, 939 child_pass_clip_rect, 940 child_pass_clipped, 941 1.f, 942 SkXfermode::kSrcOver_Mode); 943 944 scoped_ptr<SolidColorDrawQuad> color_quad; 945 color_quad = SolidColorDrawQuad::Create(); 946 color_quad->SetNew(shared_quad_state, gfx::Rect(20, 20, 3, 7), 1u, false); 947 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 948 949 color_quad = SolidColorDrawQuad::Create(); 950 color_quad->SetNew(shared_quad_state, gfx::Rect(23, 20, 4, 7), 1u, false); 951 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 952 } 953 954 gfx::Size root_pass_content_bounds(50, 50); 955 gfx::Rect root_pass_rect(0, 0, 50, 50); 956 gfx::Transform root_pass_transform; 957 gfx::Rect root_pass_clip_rect(5, 5, 40, 40); 958 bool root_pass_clipped = root_delegated_render_pass_is_clipped_; 959 960 TestRenderPass* pass = AddRenderPass( 961 &delegated_render_passes, 962 RenderPass::Id(9, 6), 963 root_pass_rect, 964 gfx::Transform()); 965 MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list); 966 AppendQuadsData data(pass->id); 967 SharedQuadState* shared_quad_state = 968 quad_sink.UseSharedQuadState(SharedQuadState::Create()); 969 shared_quad_state->SetAll(root_pass_transform, 970 root_pass_content_bounds, 971 root_pass_rect, 972 root_pass_clip_rect, 973 root_pass_clipped, 974 1.f, 975 SkXfermode::kSrcOver_Mode); 976 977 scoped_ptr<RenderPassDrawQuad> render_pass_quad = 978 RenderPassDrawQuad::Create(); 979 render_pass_quad->SetNew( 980 shared_quad_state, 981 gfx::Rect(5, 5, 7, 7), // rect 982 RenderPass::Id(10, 7), // render_pass_id 983 false, // is_replica 984 0, // mask_resource_id 985 child_pass_rect, // contents_changed_since_last_frame 986 gfx::RectF(), // mask_uv_rect 987 FilterOperations(), // filters 988 FilterOperations()); // background_filters 989 quad_sink.Append(render_pass_quad.PassAs<DrawQuad>(), &data); 990 991 scoped_ptr<SolidColorDrawQuad> color_quad; 992 color_quad = SolidColorDrawQuad::Create(); 993 color_quad->SetNew(shared_quad_state, gfx::Rect(0, 0, 10, 10), 1u, false); 994 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 995 996 color_quad = SolidColorDrawQuad::Create(); 997 color_quad->SetNew(shared_quad_state, gfx::Rect(0, 10, 10, 10), 2u, false); 998 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 999 1000 color_quad = SolidColorDrawQuad::Create(); 1001 color_quad->SetNew(shared_quad_state, gfx::Rect(10, 0, 10, 10), 3u, false); 1002 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 1003 1004 color_quad = SolidColorDrawQuad::Create(); 1005 color_quad->SetNew(shared_quad_state, gfx::Rect(10, 10, 10, 10), 4u, false); 1006 quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data); 1007 1008 delegated_renderer_layer->SetFrameDataForRenderPasses( 1009 &delegated_render_passes); 1010 1011 // The RenderPasses should be taken by the layer. 1012 EXPECT_EQ(0u, delegated_render_passes.size()); 1013 1014 root_layer_ = root_layer.get(); 1015 delegated_renderer_layer_ = delegated_renderer_layer.get(); 1016 1017 if (clip_delegated_renderer_layer_) { 1018 gfx::Rect clip_rect(21, 27, 23, 21); 1019 1020 clip_layer->SetPosition(clip_rect.origin()); 1021 clip_layer->SetBounds(clip_rect.size()); 1022 clip_layer->SetContentBounds(clip_rect.size()); 1023 clip_layer->SetMasksToBounds(true); 1024 1025 origin_layer->SetPosition( 1026 gfx::PointAtOffsetFromOrigin(-clip_rect.OffsetFromOrigin())); 1027 1028 origin_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>()); 1029 clip_layer->AddChild(origin_layer.Pass()); 1030 root_layer->AddChild(clip_layer.Pass()); 1031 } else { 1032 root_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>()); 1033 } 1034 1035 host_impl_->active_tree()->SetRootLayer(root_layer.Pass()); 1036 } 1037 1038 protected: 1039 LayerImpl* root_layer_; 1040 DelegatedRendererLayerImpl* delegated_renderer_layer_; 1041 bool root_delegated_render_pass_is_clipped_; 1042 bool clip_delegated_renderer_layer_; 1043 }; 1044 1045 TEST_F(DelegatedRendererLayerImplTestClip, 1046 QuadsUnclipped_LayerUnclipped_NoSurface) { 1047 root_delegated_render_pass_is_clipped_ = false; 1048 clip_delegated_renderer_layer_ = false; 1049 SetUpTest(); 1050 1051 LayerTreeHostImpl::FrameData frame; 1052 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1053 1054 ASSERT_EQ(2u, frame.render_passes.size()); 1055 const QuadList& contrib_delegated_quad_list = 1056 frame.render_passes[0]->quad_list; 1057 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1058 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1059 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1060 const SharedQuadState* root_delegated_shared_quad_state = 1061 root_delegated_quad_list[0]->shared_quad_state; 1062 1063 // When the quads don't have a clip of their own, the clip rect is set to 1064 // the drawable_content_rect of the delegated renderer layer. 1065 EXPECT_EQ(gfx::Rect(20, 20, 50, 50).ToString(), 1066 root_delegated_shared_quad_state->clip_rect.ToString()); 1067 // Quads are clipped to the delegated renderer layer. 1068 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1069 1070 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1071 host_impl_->DidDrawAllLayers(frame); 1072 } 1073 1074 TEST_F(DelegatedRendererLayerImplTestClip, 1075 QuadsClipped_LayerUnclipped_NoSurface) { 1076 root_delegated_render_pass_is_clipped_ = true; 1077 clip_delegated_renderer_layer_ = false; 1078 SetUpTest(); 1079 1080 LayerTreeHostImpl::FrameData frame; 1081 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1082 1083 ASSERT_EQ(2u, frame.render_passes.size()); 1084 const QuadList& contrib_delegated_quad_list = 1085 frame.render_passes[0]->quad_list; 1086 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1087 const QuadList& root_delegated_quad_list = 1088 frame.render_passes[1]->quad_list; 1089 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1090 const SharedQuadState* root_delegated_shared_quad_state = 1091 root_delegated_quad_list[0]->shared_quad_state; 1092 1093 // When the quads have a clip of their own, it is used. 1094 EXPECT_EQ(gfx::Rect(25, 25, 40, 40).ToString(), 1095 root_delegated_shared_quad_state->clip_rect.ToString()); 1096 // Quads came with a clip rect. 1097 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1098 1099 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1100 host_impl_->DidDrawAllLayers(frame); 1101 } 1102 1103 TEST_F(DelegatedRendererLayerImplTestClip, 1104 QuadsUnclipped_LayerClipped_NoSurface) { 1105 root_delegated_render_pass_is_clipped_ = false; 1106 clip_delegated_renderer_layer_ = true; 1107 SetUpTest(); 1108 1109 LayerTreeHostImpl::FrameData frame; 1110 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1111 1112 ASSERT_EQ(2u, frame.render_passes.size()); 1113 const QuadList& contrib_delegated_quad_list = 1114 frame.render_passes[0]->quad_list; 1115 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1116 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1117 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1118 const SharedQuadState* root_delegated_shared_quad_state = 1119 root_delegated_quad_list[0]->shared_quad_state; 1120 1121 // When the quads don't have a clip of their own, the clip rect is set to 1122 // the drawable_content_rect of the delegated renderer layer. When the layer 1123 // is clipped, that should be seen in the quads' clip_rect. 1124 EXPECT_EQ(gfx::Rect(21, 27, 23, 21).ToString(), 1125 root_delegated_shared_quad_state->clip_rect.ToString()); 1126 // Quads are clipped to the delegated renderer layer. 1127 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1128 1129 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1130 host_impl_->DidDrawAllLayers(frame); 1131 } 1132 1133 TEST_F(DelegatedRendererLayerImplTestClip, 1134 QuadsClipped_LayerClipped_NoSurface) { 1135 root_delegated_render_pass_is_clipped_ = true; 1136 clip_delegated_renderer_layer_ = true; 1137 SetUpTest(); 1138 1139 LayerTreeHostImpl::FrameData frame; 1140 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1141 1142 ASSERT_EQ(2u, frame.render_passes.size()); 1143 const QuadList& contrib_delegated_quad_list = 1144 frame.render_passes[0]->quad_list; 1145 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1146 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1147 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1148 const SharedQuadState* root_delegated_shared_quad_state = 1149 root_delegated_quad_list[0]->shared_quad_state; 1150 1151 // When the quads have a clip of their own, it is used, but it is 1152 // combined with the clip rect of the delegated renderer layer. 1153 EXPECT_EQ(gfx::Rect(25, 27, 19, 21).ToString(), 1154 root_delegated_shared_quad_state->clip_rect.ToString()); 1155 // Quads came with a clip rect. 1156 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1157 1158 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1159 host_impl_->DidDrawAllLayers(frame); 1160 } 1161 1162 TEST_F(DelegatedRendererLayerImplTestClip, 1163 QuadsUnclipped_LayerUnclipped_Surface) { 1164 root_delegated_render_pass_is_clipped_ = false; 1165 clip_delegated_renderer_layer_ = false; 1166 SetUpTest(); 1167 1168 delegated_renderer_layer_->SetForceRenderSurface(true); 1169 1170 LayerTreeHostImpl::FrameData frame; 1171 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1172 1173 ASSERT_EQ(3u, frame.render_passes.size()); 1174 const QuadList& contrib_delegated_quad_list = 1175 frame.render_passes[0]->quad_list; 1176 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1177 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1178 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1179 const SharedQuadState* root_delegated_shared_quad_state = 1180 root_delegated_quad_list[0]->shared_quad_state; 1181 1182 // When the layer owns a surface, the quads don't need to be clipped 1183 // further than they already specify. If they aren't clipped, then their 1184 // clip rect is ignored, and they are not set as clipped. 1185 EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped); 1186 1187 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1188 host_impl_->DidDrawAllLayers(frame); 1189 } 1190 1191 TEST_F(DelegatedRendererLayerImplTestClip, 1192 QuadsClipped_LayerUnclipped_Surface) { 1193 root_delegated_render_pass_is_clipped_ = true; 1194 clip_delegated_renderer_layer_ = false; 1195 SetUpTest(); 1196 1197 delegated_renderer_layer_->SetForceRenderSurface(true); 1198 1199 LayerTreeHostImpl::FrameData frame; 1200 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1201 1202 ASSERT_EQ(3u, frame.render_passes.size()); 1203 const QuadList& contrib_delegated_quad_list = 1204 frame.render_passes[0]->quad_list; 1205 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1206 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1207 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1208 const SharedQuadState* root_delegated_shared_quad_state = 1209 root_delegated_quad_list[0]->shared_quad_state; 1210 1211 // When the quads have a clip of their own, it is used. 1212 EXPECT_EQ(gfx::Rect(5, 5, 40, 40).ToString(), 1213 root_delegated_shared_quad_state->clip_rect.ToString()); 1214 // Quads came with a clip rect. 1215 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1216 1217 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1218 host_impl_->DidDrawAllLayers(frame); 1219 } 1220 1221 TEST_F(DelegatedRendererLayerImplTestClip, 1222 QuadsUnclipped_LayerClipped_Surface) { 1223 root_delegated_render_pass_is_clipped_ = false; 1224 clip_delegated_renderer_layer_ = true; 1225 SetUpTest(); 1226 1227 delegated_renderer_layer_->SetForceRenderSurface(true); 1228 1229 LayerTreeHostImpl::FrameData frame; 1230 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1231 1232 ASSERT_EQ(3u, frame.render_passes.size()); 1233 const QuadList& contrib_delegated_quad_list = 1234 frame.render_passes[0]->quad_list; 1235 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1236 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1237 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1238 const SharedQuadState* root_delegated_shared_quad_state = 1239 root_delegated_quad_list[0]->shared_quad_state; 1240 1241 // When the layer owns a surface, the quads don't need to be clipped 1242 // further than they already specify. If they aren't clipped, then their 1243 // clip rect is ignored, and they are not set as clipped. 1244 EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped); 1245 1246 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1247 host_impl_->DidDrawAllLayers(frame); 1248 } 1249 1250 TEST_F(DelegatedRendererLayerImplTestClip, QuadsClipped_LayerClipped_Surface) { 1251 root_delegated_render_pass_is_clipped_ = true; 1252 clip_delegated_renderer_layer_ = true; 1253 SetUpTest(); 1254 1255 delegated_renderer_layer_->SetForceRenderSurface(true); 1256 1257 LayerTreeHostImpl::FrameData frame; 1258 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1259 1260 ASSERT_EQ(3u, frame.render_passes.size()); 1261 const QuadList& contrib_delegated_quad_list = 1262 frame.render_passes[0]->quad_list; 1263 ASSERT_EQ(2u, contrib_delegated_quad_list.size()); 1264 const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list; 1265 ASSERT_EQ(5u, root_delegated_quad_list.size()); 1266 const SharedQuadState* root_delegated_shared_quad_state = 1267 root_delegated_quad_list[0]->shared_quad_state; 1268 1269 // When the quads have a clip of their own, it is used, but it is 1270 // combined with the clip rect of the delegated renderer layer. If the 1271 // layer owns a surface, then it does not have a clip rect of its own. 1272 EXPECT_EQ(gfx::Rect(5, 5, 40, 40).ToString(), 1273 root_delegated_shared_quad_state->clip_rect.ToString()); 1274 // Quads came with a clip rect. 1275 EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped); 1276 1277 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1278 host_impl_->DidDrawAllLayers(frame); 1279 } 1280 1281 TEST_F(DelegatedRendererLayerImplTest, InvalidRenderPassDrawQuad) { 1282 scoped_ptr<LayerImpl> root_layer = LayerImpl::Create( 1283 host_impl_->active_tree(), 1).PassAs<LayerImpl>(); 1284 scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer = 1285 FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 4); 1286 1287 host_impl_->SetViewportSize(gfx::Size(100, 100)); 1288 1289 delegated_renderer_layer->SetPosition(gfx::Point(3, 3)); 1290 delegated_renderer_layer->SetBounds(gfx::Size(10, 10)); 1291 delegated_renderer_layer->SetContentBounds(gfx::Size(10, 10)); 1292 delegated_renderer_layer->SetDrawsContent(true); 1293 1294 ScopedPtrVector<RenderPass> delegated_render_passes; 1295 TestRenderPass* pass1 = AddRenderPass( 1296 &delegated_render_passes, 1297 RenderPass::Id(9, 6), 1298 gfx::Rect(0, 0, 10, 10), 1299 gfx::Transform()); 1300 AddQuad(pass1, gfx::Rect(0, 0, 6, 6), 33u); 1301 1302 // This render pass isn't part of the frame. 1303 scoped_ptr<TestRenderPass> missing_pass(TestRenderPass::Create()); 1304 missing_pass->SetNew(RenderPass::Id(9, 7), 1305 gfx::Rect(7, 7, 7, 7), 1306 gfx::Rect(7, 7, 7, 7), 1307 gfx::Transform()); 1308 1309 // But a render pass quad refers to it. 1310 AddRenderPassQuad(pass1, missing_pass.get()); 1311 1312 delegated_renderer_layer->SetFrameDataForRenderPasses( 1313 &delegated_render_passes); 1314 1315 // The RenderPasses should be taken by the layer. 1316 EXPECT_EQ(0u, delegated_render_passes.size()); 1317 1318 root_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>()); 1319 host_impl_->active_tree()->SetRootLayer(root_layer.Pass()); 1320 1321 LayerTreeHostImpl::FrameData frame; 1322 EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect())); 1323 1324 // The DelegatedRendererLayerImpl should drop the bad RenderPassDrawQuad. 1325 ASSERT_EQ(1u, frame.render_passes.size()); 1326 ASSERT_EQ(1u, frame.render_passes[0]->quad_list.size()); 1327 EXPECT_EQ(DrawQuad::SOLID_COLOR, 1328 frame.render_passes[0]->quad_list[0]->material); 1329 1330 host_impl_->DrawLayers(&frame, gfx::FrameTime::Now()); 1331 host_impl_->DidDrawAllLayers(frame); 1332 } 1333 1334 } // namespace 1335 } // namespace cc 1336