mirror_layer_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 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. #include <memory>
  5. #include <utility>
  6. #include "cc/animation/animation_host.h"
  7. #include "cc/layers/mirror_layer.h"
  8. #include "cc/layers/mirror_layer_impl.h"
  9. #include "cc/test/fake_impl_task_runner_provider.h"
  10. #include "cc/test/fake_layer_tree_host.h"
  11. #include "cc/test/fake_layer_tree_host_client.h"
  12. #include "cc/test/fake_layer_tree_host_impl.h"
  13. #include "cc/test/test_task_graph_runner.h"
  14. #include "cc/trees/tree_synchronizer.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace cc {
  17. namespace {
  18. class MirrorLayerTest : public testing::Test {
  19. public:
  20. MirrorLayerTest() : host_impl_(&task_runner_provider_, &task_graph_runner_) {}
  21. // Synchronizes |layer_tree_host_| and |host_impl_| and pushes surface ids.
  22. void SynchronizeTrees() {
  23. TreeSynchronizer::PushLayerProperties(
  24. *layer_tree_host_->GetPendingCommitState(),
  25. layer_tree_host_->GetThreadUnsafeCommitState(),
  26. host_impl_.pending_tree());
  27. }
  28. protected:
  29. void SetUp() override {
  30. animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  31. layer_tree_host_ = FakeLayerTreeHost::Create(
  32. &fake_client_, &task_graph_runner_, animation_host_.get());
  33. layer_tree_host_->SetViewportRectAndScale(gfx::Rect(10, 10), 1.f,
  34. viz::LocalSurfaceId());
  35. host_impl_.CreatePendingTree();
  36. }
  37. void TearDown() override {
  38. layer_tree_host_->SetRootLayer(nullptr);
  39. layer_tree_host_ = nullptr;
  40. }
  41. FakeLayerTreeHostClient fake_client_;
  42. FakeImplTaskRunnerProvider task_runner_provider_;
  43. TestTaskGraphRunner task_graph_runner_;
  44. std::unique_ptr<AnimationHost> animation_host_;
  45. std::unique_ptr<FakeLayerTreeHost> layer_tree_host_;
  46. FakeLayerTreeHostImpl host_impl_;
  47. };
  48. // This test verifies that MirrorLayer properties are pushed across to
  49. // MirrorLayerImpl.
  50. TEST_F(MirrorLayerTest, PushProperties) {
  51. auto root = Layer::Create();
  52. layer_tree_host_->SetRootLayer(root);
  53. auto mirrored = Layer::Create();
  54. root->AddChild(mirrored);
  55. auto mirror = MirrorLayer::Create(mirrored);
  56. root->AddChild(mirror);
  57. EXPECT_EQ(1, mirrored->mirror_count());
  58. EXPECT_EQ(mirrored.get(), mirror->mirrored_layer());
  59. auto root_impl = LayerImpl::Create(host_impl_.pending_tree(), root->id());
  60. auto mirrored_impl =
  61. LayerImpl::Create(host_impl_.pending_tree(), mirrored->id());
  62. auto mirror_impl =
  63. MirrorLayerImpl::Create(host_impl_.pending_tree(), mirror->id());
  64. // Verify that impl layers have default property values.
  65. EXPECT_EQ(0, mirror_impl->mirrored_layer_id());
  66. SynchronizeTrees();
  67. // Verify that property values are pushed to impl layers.
  68. EXPECT_EQ(mirrored_impl->id(), mirror_impl->mirrored_layer_id());
  69. }
  70. // This test verifies adding/removing mirror layers updates mirror count
  71. // properly and sets appropriate bits on the layer tree host.
  72. TEST_F(MirrorLayerTest, MirrorCount) {
  73. auto mirrored = Layer::Create();
  74. mirrored->SetLayerTreeHost(layer_tree_host_.get());
  75. layer_tree_host_->WillCommit(/*completion_event=*/nullptr,
  76. /*has_updates=*/true);
  77. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  78. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  79. EXPECT_EQ(0, mirrored->mirror_count());
  80. // Creating the first mirror layer should trigger property trees rebuild.
  81. auto mirror1 = MirrorLayer::Create(mirrored);
  82. EXPECT_EQ(1, mirrored->mirror_count());
  83. EXPECT_EQ(mirrored.get(), mirror1->mirrored_layer());
  84. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  85. EXPECT_TRUE(
  86. const_cast<const FakeLayerTreeHost*>(layer_tree_host_.get())
  87. ->pending_commit_state()
  88. ->layers_that_should_push_properties.contains(mirrored.get()));
  89. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  90. // Creating a second mirror layer should not trigger property trees rebuild.
  91. auto mirror2 = MirrorLayer::Create(mirrored);
  92. EXPECT_EQ(2, mirrored->mirror_count());
  93. EXPECT_EQ(mirrored.get(), mirror2->mirrored_layer());
  94. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  95. EXPECT_TRUE(
  96. const_cast<const FakeLayerTreeHost*>(layer_tree_host_.get())
  97. ->pending_commit_state()
  98. ->layers_that_should_push_properties.contains(mirrored.get()));
  99. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  100. // Destroying one of the mirror layers should not trigger property trees
  101. // rebuild.
  102. mirror1->RemoveFromParent();
  103. mirror1 = nullptr;
  104. EXPECT_EQ(1, mirrored->mirror_count());
  105. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  106. EXPECT_EQ(1u, const_cast<const FakeLayerTreeHost*>(layer_tree_host_.get())
  107. ->pending_commit_state()
  108. ->layers_that_should_push_properties.size());
  109. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  110. // Destroying the only remaining mirror layer should trigger property trees
  111. // rebuild.
  112. mirror2->RemoveFromParent();
  113. mirror2 = nullptr;
  114. EXPECT_EQ(0, mirrored->mirror_count());
  115. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  116. EXPECT_TRUE(
  117. const_cast<const FakeLayerTreeHost*>(layer_tree_host_.get())
  118. ->pending_commit_state()
  119. ->layers_that_should_push_properties.contains(mirrored.get()));
  120. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  121. mirrored->SetLayerTreeHost(nullptr);
  122. }
  123. } // namespace
  124. } // namespace cc