surface_layer_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright 2014 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 <stdint.h>
  5. #include <iostream>
  6. #include <limits>
  7. #include <set>
  8. #include <vector>
  9. #include "base/location.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "base/time/time.h"
  14. #include "cc/animation/animation_host.h"
  15. #include "cc/layers/solid_color_layer.h"
  16. #include "cc/layers/surface_layer.h"
  17. #include "cc/layers/surface_layer_impl.h"
  18. #include "cc/test/fake_impl_task_runner_provider.h"
  19. #include "cc/test/fake_layer_tree_host.h"
  20. #include "cc/test/fake_layer_tree_host_client.h"
  21. #include "cc/test/fake_layer_tree_host_impl.h"
  22. #include "cc/test/layer_tree_test.h"
  23. #include "cc/test/test_task_graph_runner.h"
  24. #include "cc/trees/layer_tree_host.h"
  25. #include "components/viz/common/quads/compositor_frame.h"
  26. #include "components/viz/common/surfaces/surface_info.h"
  27. #include "testing/gmock/include/gmock/gmock.h"
  28. #include "testing/gtest/include/gtest/gtest.h"
  29. namespace cc {
  30. namespace {
  31. using testing::_;
  32. using testing::Eq;
  33. using testing::ElementsAre;
  34. using testing::SizeIs;
  35. constexpr viz::FrameSinkId kArbitraryFrameSinkId(1, 1);
  36. class SurfaceLayerTest : public testing::Test {
  37. public:
  38. SurfaceLayerTest()
  39. : host_impl_(&task_runner_provider_, &task_graph_runner_) {}
  40. // Synchronizes |layer_tree_host_| and |host_impl_| and pushes surface ids.
  41. void SynchronizeTrees() {
  42. auto& unsafe_state = layer_tree_host_->GetThreadUnsafeCommitState();
  43. std::unique_ptr<CommitState> commit_state =
  44. layer_tree_host_->ActivateCommitState();
  45. TreeSynchronizer::PushLayerProperties(*commit_state, unsafe_state,
  46. host_impl_.pending_tree());
  47. if (commit_state->needs_surface_ranges_sync) {
  48. host_impl_.pending_tree()->ClearSurfaceRanges();
  49. host_impl_.pending_tree()->SetSurfaceRanges(
  50. commit_state->SurfaceRanges());
  51. }
  52. }
  53. protected:
  54. void SetUp() override {
  55. animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  56. layer_tree_host_ = FakeLayerTreeHost::Create(
  57. &fake_client_, &task_graph_runner_, animation_host_.get());
  58. layer_tree_host_->SetViewportRectAndScale(gfx::Rect(10, 10), 1.f,
  59. viz::LocalSurfaceId());
  60. host_impl_.CreatePendingTree();
  61. }
  62. void TearDown() override {
  63. if (layer_tree_host_) {
  64. layer_tree_host_->SetRootLayer(nullptr);
  65. layer_tree_host_ = nullptr;
  66. }
  67. }
  68. FakeLayerTreeHostClient fake_client_;
  69. FakeImplTaskRunnerProvider task_runner_provider_;
  70. TestTaskGraphRunner task_graph_runner_;
  71. std::unique_ptr<AnimationHost> animation_host_;
  72. std::unique_ptr<FakeLayerTreeHost> layer_tree_host_;
  73. FakeLayerTreeHostImpl host_impl_;
  74. };
  75. // This test verifies that if UseExistingDeadline() is used on a new
  76. // SurfaceLayer then the deadline will be 0 frames.
  77. TEST_F(SurfaceLayerTest, UseExistingDeadlineForNewSurfaceLayer) {
  78. scoped_refptr<SurfaceLayer> layer = SurfaceLayer::Create();
  79. layer_tree_host_->SetRootLayer(layer);
  80. viz::SurfaceId primary_id(
  81. kArbitraryFrameSinkId,
  82. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  83. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseExistingDeadline());
  84. EXPECT_EQ(0u, layer->deadline_in_frames());
  85. }
  86. // This test verifies that if UseInfiniteDeadline() is used on a new
  87. // SurfaceLayer then the deadline will be max number of frames.
  88. TEST_F(SurfaceLayerTest, UseInfiniteDeadlineForNewSurfaceLayer) {
  89. scoped_refptr<SurfaceLayer> layer = SurfaceLayer::Create();
  90. layer_tree_host_->SetRootLayer(layer);
  91. viz::SurfaceId primary_id(
  92. kArbitraryFrameSinkId,
  93. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  94. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseInfiniteDeadline());
  95. EXPECT_EQ(std::numeric_limits<uint32_t>::max(), layer->deadline_in_frames());
  96. }
  97. // This test verifies that if an invalid primary surface ID is set then the
  98. // deadline will be reset to 0 frames.
  99. TEST_F(SurfaceLayerTest, ResetDeadlineOnInvalidSurfaceId) {
  100. scoped_refptr<SurfaceLayer> layer = SurfaceLayer::Create();
  101. layer_tree_host_->SetRootLayer(layer);
  102. viz::SurfaceId primary_id(
  103. kArbitraryFrameSinkId,
  104. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  105. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseSpecifiedDeadline(3u));
  106. EXPECT_EQ(3u, layer->deadline_in_frames());
  107. // Reset the surface layer to an invalid SurfaceId. Verify that the deadline
  108. // is reset.
  109. layer->SetSurfaceId(viz::SurfaceId(),
  110. DeadlinePolicy::UseSpecifiedDeadline(3u));
  111. EXPECT_EQ(0u, layer->deadline_in_frames());
  112. }
  113. // This test verifies that SurfaceLayer properties are pushed across to
  114. // SurfaceLayerImpl.
  115. TEST_F(SurfaceLayerTest, PushProperties) {
  116. scoped_refptr<SurfaceLayer> layer = SurfaceLayer::Create();
  117. layer_tree_host_->SetRootLayer(layer);
  118. viz::SurfaceId primary_id(
  119. kArbitraryFrameSinkId,
  120. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  121. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseSpecifiedDeadline(1u));
  122. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseSpecifiedDeadline(2u));
  123. layer->SetSurfaceId(primary_id, DeadlinePolicy::UseExistingDeadline());
  124. layer->SetOldestAcceptableFallback(primary_id);
  125. layer->SetBackgroundColor(SkColors::kBlue);
  126. layer->SetStretchContentToFillBounds(true);
  127. EXPECT_TRUE(
  128. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  129. EXPECT_EQ(layer_tree_host_->GetPendingCommitState()->SurfaceRanges().size(),
  130. 1u);
  131. // Verify that pending tree has no surface ids already.
  132. EXPECT_FALSE(host_impl_.pending_tree()->needs_surface_ranges_sync());
  133. EXPECT_EQ(host_impl_.pending_tree()->SurfaceRanges().size(), 0u);
  134. std::unique_ptr<SurfaceLayerImpl> layer_impl =
  135. SurfaceLayerImpl::Create(host_impl_.pending_tree(), layer->id());
  136. SynchronizeTrees();
  137. // Verify that pending tree received the surface id and also has
  138. // needs_surface_ranges_sync set to true as it needs to sync with active tree.
  139. EXPECT_TRUE(host_impl_.pending_tree()->needs_surface_ranges_sync());
  140. EXPECT_EQ(host_impl_.pending_tree()->SurfaceRanges().size(), 1u);
  141. // Verify we have reset the state on layer tree host.
  142. EXPECT_FALSE(
  143. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  144. // Verify that the primary and fallback SurfaceIds are pushed through.
  145. EXPECT_EQ(primary_id, layer_impl->range().end());
  146. EXPECT_EQ(primary_id, layer_impl->range().start());
  147. EXPECT_EQ(SkColors::kBlue, layer_impl->background_color());
  148. EXPECT_TRUE(layer_impl->stretch_content_to_fill_bounds());
  149. EXPECT_EQ(2u, layer_impl->deadline_in_frames());
  150. viz::SurfaceId fallback_id(
  151. kArbitraryFrameSinkId,
  152. viz::LocalSurfaceId(2, base::UnguessableToken::Create()));
  153. layer->SetOldestAcceptableFallback(fallback_id);
  154. layer->SetSurfaceId(fallback_id, DeadlinePolicy::UseExistingDeadline());
  155. layer->SetBackgroundColor(SkColors::kGreen);
  156. layer->SetStretchContentToFillBounds(false);
  157. // Verify that fallback surface id is not recorded on the layer tree host as
  158. // surface synchronization is not enabled.
  159. EXPECT_TRUE(
  160. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  161. EXPECT_EQ(layer_tree_host_->GetPendingCommitState()->SurfaceRanges().size(),
  162. 1u);
  163. SynchronizeTrees();
  164. EXPECT_EQ(host_impl_.pending_tree()->SurfaceRanges().size(), 1u);
  165. // Verify that the primary viz::SurfaceId stays the same and the new
  166. // fallback viz::SurfaceId is pushed through.
  167. EXPECT_EQ(fallback_id, layer_impl->range().end());
  168. EXPECT_EQ(fallback_id, layer_impl->range().start());
  169. EXPECT_EQ(SkColors::kGreen, layer_impl->background_color());
  170. // The deadline resets back to 0 (no deadline) after the first commit.
  171. EXPECT_EQ(0u, layer_impl->deadline_in_frames());
  172. EXPECT_FALSE(layer_impl->stretch_content_to_fill_bounds());
  173. }
  174. // This test verifies the list of surface ids is correct when there are cloned
  175. // surface layers. This emulates the flow of maximize and minimize animations on
  176. // Chrome OS.
  177. TEST_F(SurfaceLayerTest, CheckSurfaceReferencesForClonedLayer) {
  178. const viz::SurfaceId old_surface_id(
  179. kArbitraryFrameSinkId,
  180. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  181. // This layer will always contain the old surface id and will be deleted when
  182. // animation is done.
  183. scoped_refptr<SurfaceLayer> layer1 = SurfaceLayer::Create();
  184. layer1->SetLayerTreeHost(layer_tree_host_.get());
  185. layer1->SetSurfaceId(old_surface_id, DeadlinePolicy::UseDefaultDeadline());
  186. layer1->SetOldestAcceptableFallback(old_surface_id);
  187. // This layer will eventually be switched be switched to show the new surface
  188. // id and will be retained when animation is done.
  189. scoped_refptr<SurfaceLayer> layer2 = SurfaceLayer::Create();
  190. layer2->SetLayerTreeHost(layer_tree_host_.get());
  191. layer2->SetSurfaceId(old_surface_id, DeadlinePolicy::UseDefaultDeadline());
  192. layer2->SetOldestAcceptableFallback(old_surface_id);
  193. std::unique_ptr<SurfaceLayerImpl> layer_impl1 =
  194. SurfaceLayerImpl::Create(host_impl_.pending_tree(), layer1->id());
  195. std::unique_ptr<SurfaceLayerImpl> layer_impl2 =
  196. SurfaceLayerImpl::Create(host_impl_.pending_tree(), layer2->id());
  197. SynchronizeTrees();
  198. // Verify that only |old_surface_id| is going to be referenced.
  199. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  200. ElementsAre(viz::SurfaceRange(old_surface_id)));
  201. EXPECT_THAT(host_impl_.pending_tree()->SurfaceRanges(),
  202. ElementsAre(viz::SurfaceRange(old_surface_id)));
  203. const viz::SurfaceId new_surface_id(
  204. kArbitraryFrameSinkId,
  205. viz::LocalSurfaceId(2, base::UnguessableToken::Create()));
  206. // Switch the new layer to use |new_surface_id|.
  207. layer2->SetSurfaceId(new_surface_id, DeadlinePolicy::UseDefaultDeadline());
  208. layer2->SetOldestAcceptableFallback(new_surface_id);
  209. SynchronizeTrees();
  210. // Verify that both surface ids are going to be referenced.
  211. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  212. ElementsAre(viz::SurfaceRange(old_surface_id),
  213. viz::SurfaceRange(new_surface_id)));
  214. EXPECT_THAT(host_impl_.pending_tree()->SurfaceRanges(),
  215. ElementsAre(viz::SurfaceRange(old_surface_id),
  216. viz::SurfaceRange(new_surface_id)));
  217. // Unparent the old layer like it's being destroyed at the end of animation.
  218. layer1->SetLayerTreeHost(nullptr);
  219. SynchronizeTrees();
  220. // Verify that only |new_surface_id| is going to be referenced.
  221. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  222. ElementsAre(viz::SurfaceRange(new_surface_id)));
  223. EXPECT_THAT(host_impl_.pending_tree()->SurfaceRanges(),
  224. ElementsAre(viz::SurfaceRange(new_surface_id)));
  225. // Cleanup for destruction.
  226. layer2->SetLayerTreeHost(nullptr);
  227. }
  228. // This test verifies LayerTreeHost::needs_surface_ranges_sync() is correct when
  229. // there are cloned surface layers.
  230. TEST_F(SurfaceLayerTest, CheckNeedsSurfaceIdsSyncForClonedLayers) {
  231. const viz::SurfaceId surface_id(
  232. kArbitraryFrameSinkId,
  233. viz::LocalSurfaceId(1, base::UnguessableToken::Create()));
  234. scoped_refptr<SurfaceLayer> layer1 = SurfaceLayer::Create();
  235. layer1->SetLayerTreeHost(layer_tree_host_.get());
  236. layer1->SetSurfaceId(surface_id, DeadlinePolicy::UseDefaultDeadline());
  237. layer1->SetOldestAcceptableFallback(surface_id);
  238. // Verify the surface id is in SurfaceLayerIds() and
  239. // needs_surface_ranges_sync() is true.
  240. EXPECT_TRUE(
  241. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  242. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  243. SizeIs(1));
  244. std::unique_ptr<SurfaceLayerImpl> layer_impl1 =
  245. SurfaceLayerImpl::Create(host_impl_.pending_tree(), layer1->id());
  246. SynchronizeTrees();
  247. // After syncchronizing trees verify needs_surface_ranges_sync() is false.
  248. EXPECT_FALSE(
  249. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  250. // Create the second layer that is a clone of the first.
  251. scoped_refptr<SurfaceLayer> layer2 = SurfaceLayer::Create();
  252. layer2->SetLayerTreeHost(layer_tree_host_.get());
  253. layer2->SetSurfaceId(surface_id, DeadlinePolicy::UseDefaultDeadline());
  254. layer2->SetOldestAcceptableFallback(surface_id);
  255. // Verify that after creating the second layer with the same surface id that
  256. // needs_surface_ranges_sync is still false.
  257. EXPECT_TRUE(
  258. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  259. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  260. SizeIs(1));
  261. std::unique_ptr<SurfaceLayerImpl> layer_impl2 =
  262. SurfaceLayerImpl::Create(host_impl_.pending_tree(), layer2->id());
  263. SynchronizeTrees();
  264. // Verify needs_surface_ranges_sync is still false after synchronizing
  265. // trees.
  266. EXPECT_FALSE(
  267. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  268. // Destroy one of the layers, leaving one layer with the surface id.
  269. layer1->SetLayerTreeHost(nullptr);
  270. // Verify needs_surface_ranges_sync is still false.
  271. EXPECT_FALSE(
  272. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  273. // Destroy the last layer, this should change the set of layer surface ids.
  274. layer2->SetLayerTreeHost(nullptr);
  275. // Verify SurfaceLayerIds() is empty and needs_surface_ranges_sync is true.
  276. EXPECT_TRUE(
  277. layer_tree_host_->GetPendingCommitState()->needs_surface_ranges_sync);
  278. EXPECT_THAT(layer_tree_host_->GetPendingCommitState()->SurfaceRanges(),
  279. SizeIs(0));
  280. }
  281. } // namespace
  282. } // namespace cc