Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: cc/trees/layer_tree_host_unittest.cc

Issue 2278283003: Refactor client visibility handling (Closed)
Patch Set: feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 LayerTreeHostNotifyContextCacheControllerTest : 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 MockContextCacheController.
440 base::MakeUnique<MockContextSupport>(); 440 auto main_support = base::MakeUnique<TestContextSupport>();
441 mock_context_support_ptr_ = mock_worker_context_support_owned.get(); 441 auto main_context = TestWebGraphicsContext3D::Create();
442 auto main_cache_controller =
443 base::MakeUnique<::testing::StrictMock<MockContextCacheController>>(
444 main_support.get());
445 mock_main_cache_controller_ = main_cache_controller.get();
446 auto test_main_context_provider = TestContextProvider::Create(
447 std::move(main_context), std::move(main_support),
448 std::move(main_cache_controller));
442 449
443 auto mock_worker_context_provider = make_scoped_refptr( 450 // Create the worker ContextProvider with a MockContextCacheController.
444 new MockContextProvider(std::move(mock_worker_context_support_owned))); 451 auto worker_support = base::MakeUnique<TestContextSupport>();
445 mock_context_provider_ptr_ = mock_worker_context_provider.get(); 452 auto worker_context = TestWebGraphicsContext3D::Create();
453 auto worker_cache_controller =
454 base::MakeUnique<::testing::StrictMock<MockContextCacheController>>(
455 main_support.get());
456 mock_worker_cache_controller_ = worker_cache_controller.get();
457 auto test_worker_context_provider = TestContextProvider::Create(
458 std::move(worker_context), std::move(worker_support),
459 std::move(worker_cache_controller));
460 test_worker_context_provider->BindToCurrentThread();
446 461
447 // Workers are bound on the main thread. 462 // At init, visibility is set to true.
448 mock_worker_context_provider->BindToCurrentThread(); 463 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameVisible());
449 464 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameVisible());
450 // Ensure that our mock calls execute in sequence.
451 ::testing::InSequence s;
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));
460 465
461 return LayerTreeHostTest::CreateDelegatingOutputSurface( 466 return LayerTreeHostTest::CreateDelegatingOutputSurface(
462 std::move(compositor_context_provider), 467 std::move(test_main_context_provider),
463 std::move(mock_worker_context_provider)); 468 std::move(test_worker_context_provider));
464 } 469 }
465 470
466 void InitializeSettings(LayerTreeSettings* settings) override { 471 void InitializeSettings(LayerTreeSettings* settings) override {
467 settings->gpu_rasterization_enabled = true; 472 settings->gpu_rasterization_enabled = true;
468 settings->gpu_rasterization_forced = true; 473 settings->gpu_rasterization_forced = true;
469 } 474 }
470 475
471 void BeginTest() override {} 476 void BeginTest() override {}
472 477
473 void BeforeVisibilityChange() { 478 void AfterTest() override {
474 // Ensure that our initialization expectations have completed. 479 // Ensure all expectations are satisfied.
danakj 2016/08/29 23:56:20 I don't think these are needed.. gmock will do thi
ericrk 2016/08/30 18:18:58 Done.
475 Mock::VerifyAndClearExpectations(mock_context_support_ptr_); 480 Mock::VerifyAndClearExpectations(mock_main_cache_controller_);
476 481 Mock::VerifyAndClearExpectations(mock_worker_cache_controller_);
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 } 482 }
489 483
490 void AfterTest() override { 484 protected:
491 // Ensure all expectations are satisfied. 485 class MockContextCacheController : public ContextCacheController {
492 Mock::VerifyAndClearExpectations(mock_context_support_ptr_); 486 public:
493 Mock::VerifyAndClearExpectations(mock_context_provider_ptr_); 487 explicit MockContextCacheController(gpu::ContextSupport* support)
494 } 488 : ContextCacheController(support) {}
489 MOCK_METHOD0(OnClientBecameVisible, void());
490 MOCK_METHOD0(OnClientBecameNotVisible, void());
495 491
496 private: 492 std::unique_ptr<ScopedVisibility> ClientBecameVisible() {
497 class MockContextProvider : public TestContextProvider { 493 if (num_visible_clients_ == 0)
498 public: 494 OnClientBecameVisible();
499 explicit MockContextProvider(std::unique_ptr<TestContextSupport> support) 495 ++num_visible_clients_;
500 : TestContextProvider(std::move(support), 496 return CreateScopedVisibilityForTesting();
501 base::MakeUnique<TestGLES2Interface>(), 497 }
502 TestWebGraphicsContext3D::Create()) {}
503 498
504 MOCK_METHOD0(DeleteCachedResources, void()); 499 void ClientBecameNotVisible(
500 std::unique_ptr<ScopedVisibility> scoped_visibility) {
501 --num_visible_clients_;
502 if (num_visible_clients_ == 0)
503 OnClientBecameNotVisible();
504 ReleaseScopedVisibilityForTesting(std::move(scoped_visibility));
505 }
505 506
506 private: 507 private:
507 ~MockContextProvider() = default; 508 int num_visible_clients_ = 0;
508 }; 509 };
509 510
510 class MockContextSupport : public TestContextSupport { 511 MockContextCacheController* mock_main_cache_controller_;
511 public: 512 MockContextCacheController* mock_worker_cache_controller_;
512 MockContextSupport() {}
513 MOCK_METHOD1(SetAggressivelyFreeResources,
514 void(bool aggressively_free_resources));
515 MOCK_METHOD2(SetClientVisible, void(int client_id, bool is_visible));
516 MOCK_CONST_METHOD0(AnyClientsVisible, bool());
517 };
518
519 MockContextSupport* mock_context_support_ptr_;
520 MockContextProvider* mock_context_provider_ptr_;
521 }; 513 };
522 514
523 // Test if the LTH successfully frees resources on the worker context when 515 // Test if the LTH successfully notifies the main/worker
524 // visibility is set to false. 516 // ContextCacheControllers when visibility is set to false.
525 class LayerTreeHostFreeWorkerContextResourcesOnInvisible 517 class LayerTreeHostNotifiesCCCOnInvisible
526 : public LayerTreeHostFreeWorkerContextResourcesTest { 518 : public LayerTreeHostNotifyContextCacheControllerTest {
527 public: 519 public:
528 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, 520 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
529 bool success) override { 521 bool success) override {
530 BeforeVisibilityChange(); 522 // Ensure that our initialization expectations have completed.
523 Mock::VerifyAndClearExpectations(mock_main_cache_controller_);
524 Mock::VerifyAndClearExpectations(mock_worker_cache_controller_);
525
526 // Update visibility and make sure our two CCCs are updated.
527 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameNotVisible());
528 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameNotVisible())
529 .WillOnce(testing::Invoke([this]() { EndTest(); }));
531 PostSetVisibleToMainThread(false); 530 PostSetVisibleToMainThread(false);
532 } 531 }
533 }; 532 };
534 533
535 SINGLE_AND_MULTI_THREAD_TEST_F( 534 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostNotifiesCCCOnInvisible);
536 LayerTreeHostFreeWorkerContextResourcesOnInvisible);
537 535
538 // Test if the LTH successfully frees resources on the worker context when 536 // Test if the LTH successfully notifies the worker ContextCacheController when
539 // hard memory limit is set to zero. 537 // the hard memory limit is set to zero.
540 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit 538 class LayerTreeHostNotifiesCCCOnZeroMemoryLimit
541 : public LayerTreeHostFreeWorkerContextResourcesTest { 539 : public LayerTreeHostNotifyContextCacheControllerTest {
542 public: 540 public:
543 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl, 541 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
544 bool success) override { 542 bool success) override {
545 BeforeVisibilityChange(); 543 // Ensure that our initialization expectations have completed.
544 Mock::VerifyAndClearExpectations(mock_worker_cache_controller_);
545
546 // Worker CC should be notified when hard memory limit is zeroed.
547 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameNotVisible())
548 .WillOnce(testing::Invoke([this]() {
549 // Main context is unchanged. It will go invisible on destruction.
550 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameNotVisible());
551 EndTest();
552 }));
546 ManagedMemoryPolicy zero_policy( 553 ManagedMemoryPolicy zero_policy(
547 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0); 554 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0);
548 host_impl->SetMemoryPolicy(zero_policy); 555 host_impl->SetMemoryPolicy(zero_policy);
danakj 2016/08/29 23:56:20 You could just VerifyAndClear/EXPECT_CALL/EndTest(
ericrk 2016/08/30 18:18:58 Sadly, because we don't want to interrupt in-proce
549 } 556 }
550 }; 557 };
551 558
552 SINGLE_AND_MULTI_THREAD_TEST_F( 559 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostNotifiesCCCOnZeroMemoryLimit);
553 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit);
554 560
555 // Test if the LTH successfully frees resources on the worker context when 561 // Test if the LTH successfully notifies the ContextCacheController of
556 // hard memory limit is set to zero while using a synchronous compositor (like 562 // visibility changes when hard memory limit is set to zero while using a
557 // Android WebView). 563 // synchronous compositor (like Android WebView).
558 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous 564 class LayerTreeHostNotifiesCCCOnZeroMemoryLimitSynchronous
559 : public LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit { 565 : public LayerTreeHostNotifiesCCCOnZeroMemoryLimit {
560 public: 566 public:
561 void InitializeSettings(LayerTreeSettings* settings) override { 567 void InitializeSettings(LayerTreeSettings* settings) override {
562 LayerTreeHostFreeWorkerContextResourcesTest::InitializeSettings(settings); 568 LayerTreeHostNotifyContextCacheControllerTest::InitializeSettings(settings);
563 settings->using_synchronous_renderer_compositor = true; 569 settings->using_synchronous_renderer_compositor = true;
564 } 570 }
565 }; 571 };
566 572
567 SINGLE_AND_MULTI_THREAD_TEST_F( 573 SINGLE_AND_MULTI_THREAD_TEST_F(
568 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous); 574 LayerTreeHostNotifiesCCCOnZeroMemoryLimitSynchronous);
575
576 // Test if the LTH successfully notifies the main and worker
577 // ContextCacheControllers of visibility changes when the OutputSurface is
578 // destroyed.
579 class LayerTreeHostNotifyCCCOnDestroy
580 : public LayerTreeHostNotifyContextCacheControllerTest {
581 public:
582 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
583 const BeginFrameArgs& args) override {
584 // Ensure that our initialization expectations have completed.
585 Mock::VerifyAndClearExpectations(mock_main_cache_controller_);
586 Mock::VerifyAndClearExpectations(mock_worker_cache_controller_);
587
588 // We leave the LTHI visible, so it will notify CC on destruction.
589 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameNotVisible());
590 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameNotVisible());
591 EndTest();
592 }
593 };
594
595 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostNotifyCCCOnDestroy);
596
597 // Test if the LTH successfully notifies the main and worker
598 // ContextCacheControllers of visibility changes when the OutputSurface is
599 // lost and recreated.
600 class LayerTreeHostNotifyCCCOnOutputSurfaceRecreated
601 : public LayerTreeHostNotifyContextCacheControllerTest {
602 public:
603 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
604 const BeginFrameArgs& args) override {
605 // Ensure that our initialization expectations have completed.
606 Mock::VerifyAndClearExpectations(mock_main_cache_controller_);
607 Mock::VerifyAndClearExpectations(mock_worker_cache_controller_);
608
609 if (has_recreated_) {
610 // Destruction exptectations.
611 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameNotVisible());
612 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameNotVisible());
613 EndTest();
614 return;
615 }
616 has_recreated_ = true;
617
618 // Output surface lost expectations.
619 EXPECT_CALL(*mock_worker_cache_controller_, OnClientBecameNotVisible());
620 EXPECT_CALL(*mock_main_cache_controller_, OnClientBecameNotVisible());
621 host_impl->DidLoseOutputSurface();
622 }
623
624 private:
625 bool has_recreated_ = false;
626 };
627
628 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostNotifyCCCOnOutputSurfaceRecreated);
569 629
570 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1 630 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1
571 // draw with frame 0. 631 // draw with frame 0.
572 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest { 632 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest {
573 public: 633 public:
574 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {} 634 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {}
575 635
576 void BeginTest() override { 636 void BeginTest() override {
577 PostSetNeedsCommitToMainThread(); 637 PostSetNeedsCommitToMainThread();
578 PostSetNeedsCommitToMainThread(); 638 PostSetNeedsCommitToMainThread();
(...skipping 1621 matching lines...) Expand 10 before | Expand all | Expand 10 after
2200 2260
2201 // Single thread proxy does not support impl-side page scale changes. 2261 // Single thread proxy does not support impl-side page scale changes.
2202 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation); 2262 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation);
2203 2263
2204 class LayerTreeHostTestSetVisible : public LayerTreeHostTest { 2264 class LayerTreeHostTestSetVisible : public LayerTreeHostTest {
2205 public: 2265 public:
2206 LayerTreeHostTestSetVisible() : num_draws_(0) {} 2266 LayerTreeHostTestSetVisible() : num_draws_(0) {}
2207 2267
2208 void BeginTest() override { 2268 void BeginTest() override {
2209 PostSetNeedsCommitToMainThread(); 2269 PostSetNeedsCommitToMainThread();
2270 }
2271
2272 void WillCommit() override {
2210 PostSetVisibleToMainThread(false); 2273 PostSetVisibleToMainThread(false);
2211 // This is suppressed while we're invisible. 2274 // This is suppressed while we're invisible.
2212 PostSetNeedsRedrawToMainThread(); 2275 PostSetNeedsRedrawToMainThread();
2213 // Triggers the redraw. 2276 // Triggers the redraw.
2214 PostSetVisibleToMainThread(true); 2277 PostSetVisibleToMainThread(true);
2215 } 2278 }
2216 2279
2217 void DrawLayersOnThread(LayerTreeHostImpl* impl) override { 2280 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
2218 EXPECT_TRUE(impl->visible()); 2281 EXPECT_TRUE(impl->visible());
2219 ++num_draws_; 2282 ++num_draws_;
(...skipping 4813 matching lines...) Expand 10 before | Expand all | Expand 10 after
7033 private: 7096 private:
7034 FakeContentLayerClient client_; 7097 FakeContentLayerClient client_;
7035 const gfx::Size viewport_size_; 7098 const gfx::Size viewport_size_;
7036 const gfx::Size large_image_size_; 7099 const gfx::Size large_image_size_;
7037 }; 7100 };
7038 7101
7039 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationSucceedsWithLargeImage); 7102 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationSucceedsWithLargeImage);
7040 7103
7041 } // namespace 7104 } // namespace
7042 } // namespace cc 7105 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698