| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/trees/layer_tree_host.h" | 5 #include "cc/trees/layer_tree_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 FakeContentLayerClient client_; | 424 FakeContentLayerClient client_; |
| 425 bool toggled_visibility_; | 425 bool toggled_visibility_; |
| 426 bool did_notify_ready_to_draw_; | 426 bool did_notify_ready_to_draw_; |
| 427 bool did_draw_; | 427 bool did_draw_; |
| 428 }; | 428 }; |
| 429 | 429 |
| 430 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in | 430 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in |
| 431 // single threaded mode. | 431 // single threaded mode. |
| 432 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawVisibility); | 432 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawVisibility); |
| 433 | 433 |
| 434 class LayerTreeHostFreeWorkerContextResourcesTest : public LayerTreeHostTest { | 434 class LayerTreeHostContextCacheTest : public LayerTreeHostTest { |
| 435 public: | 435 public: |
| 436 std::unique_ptr<TestDelegatingOutputSurface> CreateDelegatingOutputSurface( | 436 std::unique_ptr<TestDelegatingOutputSurface> CreateDelegatingOutputSurface( |
| 437 scoped_refptr<ContextProvider> compositor_context_provider, | 437 scoped_refptr<ContextProvider> compositor_context_provider, |
| 438 scoped_refptr<ContextProvider> worker_context_provider) override { | 438 scoped_refptr<ContextProvider> worker_context_provider) override { |
| 439 auto mock_worker_context_support_owned = | 439 // Create the main ContextProvider with a MockContextSupport. |
| 440 base::MakeUnique<MockContextSupport>(); | 440 auto main_support = base::MakeUnique<MockContextSupport>(); |
| 441 mock_context_support_ptr_ = mock_worker_context_support_owned.get(); | 441 mock_main_context_support_ = main_support.get(); |
| 442 auto test_main_context_provider = TestContextProvider::Create( |
| 443 TestWebGraphicsContext3D::Create(), std::move(main_support)); |
| 442 | 444 |
| 443 auto mock_worker_context_provider = make_scoped_refptr( | 445 // Create the main ContextProvider with a MockContextSupport. |
| 444 new MockContextProvider(std::move(mock_worker_context_support_owned))); | 446 auto worker_support = base::MakeUnique<MockContextSupport>(); |
| 445 mock_context_provider_ptr_ = mock_worker_context_provider.get(); | 447 mock_worker_context_support_ = worker_support.get(); |
| 448 auto test_worker_context_provider = TestContextProvider::Create( |
| 449 TestWebGraphicsContext3D::Create(), std::move(worker_support)); |
| 450 test_worker_context_provider->BindToCurrentThread(); |
| 446 | 451 |
| 447 // Workers are bound on the main thread. | 452 // At init, visibility is set to true, so SetAggressivelyFreeResources will |
| 448 mock_worker_context_provider->BindToCurrentThread(); | 453 // be disabled. |
| 449 | 454 EXPECT_CALL(*mock_main_context_support_, |
| 450 // Ensure that our mock calls execute in sequence. | 455 SetAggressivelyFreeResources(false)); |
| 451 ::testing::InSequence s; | 456 EXPECT_CALL(*mock_worker_context_support_, |
| 452 | |
| 453 // At init, visibility is set to true and we will call | |
| 454 // SetAggressivelyFreeResources(false). | |
| 455 EXPECT_CALL(*mock_context_support_ptr_, SetClientVisible(0, true)); | |
| 456 EXPECT_CALL(*mock_context_support_ptr_, AnyClientsVisible()) | |
| 457 .WillOnce(::testing::Return(true)); | |
| 458 EXPECT_CALL(*mock_context_support_ptr_, | |
| 459 SetAggressivelyFreeResources(false)); | 457 SetAggressivelyFreeResources(false)); |
| 460 | 458 |
| 461 return LayerTreeHostTest::CreateDelegatingOutputSurface( | 459 return LayerTreeHostTest::CreateDelegatingOutputSurface( |
| 462 std::move(compositor_context_provider), | 460 std::move(test_main_context_provider), |
| 463 std::move(mock_worker_context_provider)); | 461 std::move(test_worker_context_provider)); |
| 464 } | 462 } |
| 465 | 463 |
| 466 void InitializeSettings(LayerTreeSettings* settings) override { | 464 void InitializeSettings(LayerTreeSettings* settings) override { |
| 467 settings->gpu_rasterization_enabled = true; | 465 settings->gpu_rasterization_enabled = true; |
| 468 settings->gpu_rasterization_forced = true; | 466 settings->gpu_rasterization_forced = true; |
| 469 } | 467 } |
| 470 | 468 |
| 471 void BeginTest() override {} | 469 void BeginTest() override {} |
| 470 void AfterTest() override {} |
| 472 | 471 |
| 473 void BeforeVisibilityChange() { | 472 protected: |
| 474 // Ensure that our initialization expectations have completed. | |
| 475 Mock::VerifyAndClearExpectations(mock_context_support_ptr_); | |
| 476 | |
| 477 // Ensure that our mock calls execute in sequence. | |
| 478 ::testing::InSequence s; | |
| 479 | |
| 480 // While running, visibility is set to false, and we will call | |
| 481 // DeleteCachedResources and SetAggressivelyFreeResources(true). | |
| 482 EXPECT_CALL(*mock_context_support_ptr_, SetClientVisible(0, false)); | |
| 483 EXPECT_CALL(*mock_context_support_ptr_, AnyClientsVisible()) | |
| 484 .WillOnce(::testing::Return(false)); | |
| 485 EXPECT_CALL(*mock_context_provider_ptr_, DeleteCachedResources()); | |
| 486 EXPECT_CALL(*mock_context_support_ptr_, SetAggressivelyFreeResources(true)) | |
| 487 .WillOnce(testing::Invoke([this](bool is_visible) { EndTest(); })); | |
| 488 } | |
| 489 | |
| 490 void AfterTest() override { | |
| 491 // Ensure all expectations are satisfied. | |
| 492 Mock::VerifyAndClearExpectations(mock_context_support_ptr_); | |
| 493 Mock::VerifyAndClearExpectations(mock_context_provider_ptr_); | |
| 494 } | |
| 495 | |
| 496 private: | |
| 497 class MockContextProvider : public TestContextProvider { | |
| 498 public: | |
| 499 explicit MockContextProvider(std::unique_ptr<TestContextSupport> support) | |
| 500 : TestContextProvider(std::move(support), | |
| 501 base::MakeUnique<TestGLES2Interface>(), | |
| 502 TestWebGraphicsContext3D::Create()) {} | |
| 503 | |
| 504 MOCK_METHOD0(DeleteCachedResources, void()); | |
| 505 | |
| 506 private: | |
| 507 ~MockContextProvider() = default; | |
| 508 }; | |
| 509 | |
| 510 class MockContextSupport : public TestContextSupport { | 473 class MockContextSupport : public TestContextSupport { |
| 511 public: | 474 public: |
| 512 MockContextSupport() {} | 475 MockContextSupport() {} |
| 513 MOCK_METHOD1(SetAggressivelyFreeResources, | 476 MOCK_METHOD1(SetAggressivelyFreeResources, |
| 514 void(bool aggressively_free_resources)); | 477 void(bool aggressively_free_resources)); |
| 515 MOCK_METHOD2(SetClientVisible, void(int client_id, bool is_visible)); | |
| 516 MOCK_CONST_METHOD0(AnyClientsVisible, bool()); | |
| 517 }; | 478 }; |
| 518 | 479 |
| 519 MockContextSupport* mock_context_support_ptr_; | 480 MockContextSupport* mock_main_context_support_; |
| 520 MockContextProvider* mock_context_provider_ptr_; | 481 MockContextSupport* mock_worker_context_support_; |
| 521 }; | 482 }; |
| 522 | 483 |
| 523 // Test if the LTH successfully frees resources on the worker context when | 484 // Test if the LTH successfully frees resources on the main/worker |
| 524 // visibility is set to false. | 485 // ContextSupport when visibility is set to false. |
| 525 class LayerTreeHostFreeWorkerContextResourcesOnInvisible | 486 class LayerTreeHostFreesContextResourcesOnInvisible |
| 526 : public LayerTreeHostFreeWorkerContextResourcesTest { | 487 : public LayerTreeHostContextCacheTest { |
| 527 public: | 488 public: |
| 528 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, | 489 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, |
| 529 bool success) override { | 490 bool success) override { |
| 530 BeforeVisibilityChange(); | 491 // Ensure that our initialization expectations have completed. |
| 492 Mock::VerifyAndClearExpectations(mock_main_context_support_); |
| 493 Mock::VerifyAndClearExpectations(mock_worker_context_support_); |
| 494 |
| 495 // Update visibility and make sure resources are freed. |
| 496 EXPECT_CALL(*mock_main_context_support_, |
| 497 SetAggressivelyFreeResources(true)); |
| 498 EXPECT_CALL(*mock_worker_context_support_, |
| 499 SetAggressivelyFreeResources(true)) |
| 500 .WillOnce(testing::Invoke([this](bool is_visible) { EndTest(); })); |
| 531 PostSetVisibleToMainThread(false); | 501 PostSetVisibleToMainThread(false); |
| 532 } | 502 } |
| 533 }; | 503 }; |
| 534 | 504 |
| 535 SINGLE_AND_MULTI_THREAD_TEST_F( | 505 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostFreesContextResourcesOnInvisible); |
| 536 LayerTreeHostFreeWorkerContextResourcesOnInvisible); | |
| 537 | 506 |
| 538 // Test if the LTH successfully frees resources on the worker context when | 507 // Test if the LTH successfully frees worker context resources when the hard |
| 539 // hard memory limit is set to zero. | 508 // memory limit is set to zero. |
| 540 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit | 509 class LayerTreeHostFreesWorkerContextResourcesOnZeroMemoryLimit |
| 541 : public LayerTreeHostFreeWorkerContextResourcesTest { | 510 : public LayerTreeHostContextCacheTest { |
| 542 public: | 511 public: |
| 543 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, | 512 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, |
| 544 bool success) override { | 513 bool success) override { |
| 545 BeforeVisibilityChange(); | 514 // Ensure that our initialization expectations have completed. |
| 515 Mock::VerifyAndClearExpectations(mock_worker_context_support_); |
| 516 |
| 517 // Worker context support should start freeing resources when hard memory |
| 518 // limit is zeroed. |
| 519 EXPECT_CALL(*mock_worker_context_support_, |
| 520 SetAggressivelyFreeResources(true)) |
| 521 .WillOnce(testing::Invoke([this](bool is_visible) { |
| 522 // Main context is unchanged. It will start freeing on destruction. |
| 523 EXPECT_CALL(*mock_main_context_support_, |
| 524 SetAggressivelyFreeResources(true)); |
| 525 EndTest(); |
| 526 })); |
| 546 ManagedMemoryPolicy zero_policy( | 527 ManagedMemoryPolicy zero_policy( |
| 547 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0); | 528 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0); |
| 548 host_impl->SetMemoryPolicy(zero_policy); | 529 host_impl->SetMemoryPolicy(zero_policy); |
| 549 } | 530 } |
| 550 }; | 531 }; |
| 551 | 532 |
| 552 SINGLE_AND_MULTI_THREAD_TEST_F( | 533 SINGLE_AND_MULTI_THREAD_TEST_F( |
| 553 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit); | 534 LayerTreeHostFreesWorkerContextResourcesOnZeroMemoryLimit); |
| 554 | 535 |
| 555 // Test if the LTH successfully frees resources on the worker context when | 536 // Test if the LTH successfully frees worker context resources when hard memory |
| 556 // hard memory limit is set to zero while using a synchronous compositor (like | 537 // limit is set to zero while using a synchronous compositor (Android WebView). |
| 557 // Android WebView). | 538 class LayerTreeHostFreesWorkerContextResourcesOnZeroMemoryLimitSynchronous |
| 558 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous | 539 : public LayerTreeHostFreesWorkerContextResourcesOnZeroMemoryLimit { |
| 559 : public LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit { | |
| 560 public: | 540 public: |
| 561 void InitializeSettings(LayerTreeSettings* settings) override { | 541 void InitializeSettings(LayerTreeSettings* settings) override { |
| 562 LayerTreeHostFreeWorkerContextResourcesTest::InitializeSettings(settings); | 542 LayerTreeHostContextCacheTest::InitializeSettings(settings); |
| 563 settings->using_synchronous_renderer_compositor = true; | 543 settings->using_synchronous_renderer_compositor = true; |
| 564 } | 544 } |
| 565 }; | 545 }; |
| 566 | 546 |
| 567 SINGLE_AND_MULTI_THREAD_TEST_F( | 547 SINGLE_AND_MULTI_THREAD_TEST_F( |
| 568 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous); | 548 LayerTreeHostFreesWorkerContextResourcesOnZeroMemoryLimitSynchronous); |
| 549 |
| 550 // Test if the LTH successfully frees main and worker resources when the |
| 551 // OutputSurface is destroyed. |
| 552 class LayerTreeHostFreeContextResourcesOnDestroy |
| 553 : public LayerTreeHostContextCacheTest { |
| 554 public: |
| 555 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl, |
| 556 const BeginFrameArgs& args) override { |
| 557 // Ensure that our initialization expectations have completed. |
| 558 Mock::VerifyAndClearExpectations(mock_main_context_support_); |
| 559 Mock::VerifyAndClearExpectations(mock_worker_context_support_); |
| 560 |
| 561 // We leave the LTHI visible, so it start freeing resources on destruction. |
| 562 EXPECT_CALL(*mock_worker_context_support_, |
| 563 SetAggressivelyFreeResources(true)); |
| 564 EXPECT_CALL(*mock_main_context_support_, |
| 565 SetAggressivelyFreeResources(true)); |
| 566 EndTest(); |
| 567 } |
| 568 }; |
| 569 |
| 570 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostFreeContextResourcesOnDestroy); |
| 571 |
| 572 // Test if the LTH successfully frees and stops freeing context resources |
| 573 // when the OutputSurface is lost and recreated. |
| 574 class LayerTreeHostCacheBehaviorOnOutputSurfaceRecreated |
| 575 : public LayerTreeHostContextCacheTest { |
| 576 public: |
| 577 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl, |
| 578 const BeginFrameArgs& args) override { |
| 579 // Ensure that our initialization expectations have completed. |
| 580 Mock::VerifyAndClearExpectations(mock_main_context_support_); |
| 581 Mock::VerifyAndClearExpectations(mock_worker_context_support_); |
| 582 |
| 583 if (has_recreated_) { |
| 584 // Destruction exptectations. |
| 585 EXPECT_CALL(*mock_worker_context_support_, |
| 586 SetAggressivelyFreeResources(true)); |
| 587 EXPECT_CALL(*mock_main_context_support_, |
| 588 SetAggressivelyFreeResources(true)); |
| 589 EndTest(); |
| 590 return; |
| 591 } |
| 592 has_recreated_ = true; |
| 593 |
| 594 // Output surface lost expectations. |
| 595 EXPECT_CALL(*mock_worker_context_support_, |
| 596 SetAggressivelyFreeResources(true)); |
| 597 EXPECT_CALL(*mock_main_context_support_, |
| 598 SetAggressivelyFreeResources(true)); |
| 599 host_impl->DidLoseOutputSurface(); |
| 600 } |
| 601 |
| 602 private: |
| 603 bool has_recreated_ = false; |
| 604 }; |
| 605 |
| 606 SINGLE_AND_MULTI_THREAD_TEST_F( |
| 607 LayerTreeHostCacheBehaviorOnOutputSurfaceRecreated); |
| 569 | 608 |
| 570 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1 | 609 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1 |
| 571 // draw with frame 0. | 610 // draw with frame 0. |
| 572 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest { | 611 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest { |
| 573 public: | 612 public: |
| 574 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {} | 613 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {} |
| 575 | 614 |
| 576 void BeginTest() override { | 615 void BeginTest() override { |
| 577 PostSetNeedsCommitToMainThread(); | 616 PostSetNeedsCommitToMainThread(); |
| 578 PostSetNeedsCommitToMainThread(); | 617 PostSetNeedsCommitToMainThread(); |
| (...skipping 1621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2200 | 2239 |
| 2201 // Single thread proxy does not support impl-side page scale changes. | 2240 // Single thread proxy does not support impl-side page scale changes. |
| 2202 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation); | 2241 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation); |
| 2203 | 2242 |
| 2204 class LayerTreeHostTestSetVisible : public LayerTreeHostTest { | 2243 class LayerTreeHostTestSetVisible : public LayerTreeHostTest { |
| 2205 public: | 2244 public: |
| 2206 LayerTreeHostTestSetVisible() : num_draws_(0) {} | 2245 LayerTreeHostTestSetVisible() : num_draws_(0) {} |
| 2207 | 2246 |
| 2208 void BeginTest() override { | 2247 void BeginTest() override { |
| 2209 PostSetNeedsCommitToMainThread(); | 2248 PostSetNeedsCommitToMainThread(); |
| 2249 } |
| 2250 |
| 2251 void WillCommit() override { |
| 2210 PostSetVisibleToMainThread(false); | 2252 PostSetVisibleToMainThread(false); |
| 2211 // This is suppressed while we're invisible. | 2253 // This is suppressed while we're invisible. |
| 2212 PostSetNeedsRedrawToMainThread(); | 2254 PostSetNeedsRedrawToMainThread(); |
| 2213 // Triggers the redraw. | 2255 // Triggers the redraw. |
| 2214 PostSetVisibleToMainThread(true); | 2256 PostSetVisibleToMainThread(true); |
| 2215 } | 2257 } |
| 2216 | 2258 |
| 2217 void DrawLayersOnThread(LayerTreeHostImpl* impl) override { | 2259 void DrawLayersOnThread(LayerTreeHostImpl* impl) override { |
| 2218 EXPECT_TRUE(impl->visible()); | 2260 EXPECT_TRUE(impl->visible()); |
| 2219 ++num_draws_; | 2261 ++num_draws_; |
| (...skipping 4813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7033 private: | 7075 private: |
| 7034 FakeContentLayerClient client_; | 7076 FakeContentLayerClient client_; |
| 7035 const gfx::Size viewport_size_; | 7077 const gfx::Size viewport_size_; |
| 7036 const gfx::Size large_image_size_; | 7078 const gfx::Size large_image_size_; |
| 7037 }; | 7079 }; |
| 7038 | 7080 |
| 7039 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationSucceedsWithLargeImage); | 7081 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationSucceedsWithLargeImage); |
| 7040 | 7082 |
| 7041 } // namespace | 7083 } // namespace |
| 7042 } // namespace cc | 7084 } // namespace cc |
| OLD | NEW |