effect_tree_layer_list_iterator_unittest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2017 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 "cc/layers/effect_tree_layer_list_iterator.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "base/memory/ptr_util.h"
  8. #include "cc/layers/layer.h"
  9. #include "cc/test/fake_layer_tree_host.h"
  10. #include "cc/test/layer_tree_impl_test_base.h"
  11. #include "cc/test/test_task_graph_runner.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "ui/gfx/geometry/transform.h"
  15. namespace cc {
  16. namespace {
  17. class TestLayerImpl : public LayerImpl {
  18. public:
  19. static std::unique_ptr<TestLayerImpl> Create(LayerTreeImpl* tree, int id) {
  20. return base::WrapUnique(new TestLayerImpl(tree, id));
  21. }
  22. ~TestLayerImpl() override = default;
  23. int count_;
  24. private:
  25. explicit TestLayerImpl(LayerTreeImpl* tree, int id)
  26. : LayerImpl(tree, id), count_(-1) {
  27. SetBounds(gfx::Size(100, 100));
  28. SetDrawsContent(true);
  29. }
  30. };
  31. #define EXPECT_COUNT(layer, target, contrib, itself) \
  32. if (GetRenderSurface(layer)) { \
  33. EXPECT_EQ(target, target_surface_count_[layer->effect_tree_index()]); \
  34. EXPECT_EQ(contrib, \
  35. contributing_surface_count_[layer->effect_tree_index()]); \
  36. } \
  37. EXPECT_EQ(itself, layer->count_);
  38. class EffectTreeLayerListIteratorTest : public LayerTreeImplTestBase,
  39. public testing::Test {
  40. public:
  41. void SetUp() override {
  42. // This test suite needs the root layer to be TestLayerImpl.
  43. LayerTreeImpl* active_tree = host_impl()->active_tree();
  44. active_tree->DetachLayers();
  45. active_tree->property_trees()->clear();
  46. active_tree->SetRootLayerForTesting(TestLayerImpl::Create(active_tree, 1));
  47. root_layer()->SetBounds(gfx::Size(1, 1));
  48. SetupRootProperties(root_layer());
  49. }
  50. void IterateFrontToBack() {
  51. ResetCounts();
  52. int count = 0;
  53. for (EffectTreeLayerListIterator it(host_impl()->active_tree());
  54. it.state() != EffectTreeLayerListIterator::State::END; ++it, ++count) {
  55. switch (it.state()) {
  56. case EffectTreeLayerListIterator::State::LAYER:
  57. static_cast<TestLayerImpl*>(it.current_layer())->count_ = count;
  58. break;
  59. case EffectTreeLayerListIterator::State::TARGET_SURFACE:
  60. target_surface_count_[it.target_render_surface()->EffectTreeIndex()] =
  61. count;
  62. break;
  63. case EffectTreeLayerListIterator::State::CONTRIBUTING_SURFACE:
  64. contributing_surface_count_[it.current_render_surface()
  65. ->EffectTreeIndex()] = count;
  66. break;
  67. default:
  68. NOTREACHED();
  69. }
  70. }
  71. }
  72. void ResetCounts() {
  73. for (LayerImpl* layer : *host_impl()->active_tree()) {
  74. static_cast<TestLayerImpl*>(layer)->count_ = -1;
  75. }
  76. target_surface_count_ = std::vector<int>(
  77. host_impl()->active_tree()->property_trees()->effect_tree().size(), -1);
  78. contributing_surface_count_ = std::vector<int>(
  79. host_impl()->active_tree()->property_trees()->effect_tree().size(), -1);
  80. }
  81. protected:
  82. // Tracks when each render surface is visited as a target surface or
  83. // contributing surface. Indexed by effect node id.
  84. std::vector<int> target_surface_count_;
  85. std::vector<int> contributing_surface_count_;
  86. };
  87. TEST_F(EffectTreeLayerListIteratorTest, TreeWithNoDrawnLayers) {
  88. auto* root = static_cast<TestLayerImpl*>(root_layer());
  89. root->SetDrawsContent(false);
  90. UpdateActiveTreeDrawProperties();
  91. IterateFrontToBack();
  92. EXPECT_COUNT(root, 0, -1, -1);
  93. }
  94. TEST_F(EffectTreeLayerListIteratorTest, SimpleTree) {
  95. auto* root = static_cast<TestLayerImpl*>(root_layer());
  96. auto* first = AddLayer<TestLayerImpl>();
  97. CopyProperties(root, first);
  98. auto* second = AddLayer<TestLayerImpl>();
  99. CopyProperties(root, second);
  100. auto* third = AddLayer<TestLayerImpl>();
  101. CopyProperties(root, third);
  102. auto* fourth = AddLayer<TestLayerImpl>();
  103. CopyProperties(root, fourth);
  104. UpdateActiveTreeDrawProperties();
  105. IterateFrontToBack();
  106. EXPECT_COUNT(root, 5, -1, 4);
  107. EXPECT_COUNT(first, 5, -1, 3);
  108. EXPECT_COUNT(second, 5, -1, 2);
  109. EXPECT_COUNT(third, 5, -1, 1);
  110. EXPECT_COUNT(fourth, 5, -1, 0);
  111. }
  112. TEST_F(EffectTreeLayerListIteratorTest, ComplexTreeMultiSurface) {
  113. auto* root = static_cast<TestLayerImpl*>(root_layer());
  114. auto* root1 = AddLayer<TestLayerImpl>();
  115. CopyProperties(root, root1);
  116. auto* root2 = AddLayer<TestLayerImpl>();
  117. root2->SetDrawsContent(false);
  118. CopyProperties(root, root2);
  119. CreateEffectNode(root2).render_surface_reason = RenderSurfaceReason::kTest;
  120. auto* root21 = AddLayer<TestLayerImpl>();
  121. CopyProperties(root2, root21);
  122. auto* root22 = AddLayer<TestLayerImpl>();
  123. CopyProperties(root2, root22);
  124. CreateEffectNode(root22).render_surface_reason = RenderSurfaceReason::kTest;
  125. auto* root221 = AddLayer<TestLayerImpl>();
  126. CopyProperties(root22, root221);
  127. auto* root23 = AddLayer<TestLayerImpl>();
  128. CopyProperties(root2, root23);
  129. CreateEffectNode(root23).render_surface_reason = RenderSurfaceReason::kTest;
  130. auto* root231 = AddLayer<TestLayerImpl>();
  131. CopyProperties(root23, root231);
  132. auto* root3 = AddLayer<TestLayerImpl>();
  133. CopyProperties(root, root3);
  134. UpdateActiveTreeDrawProperties();
  135. IterateFrontToBack();
  136. EXPECT_COUNT(root, 14, -1, 13);
  137. EXPECT_COUNT(root1, 14, -1, 12);
  138. EXPECT_COUNT(root2, 10, 11, -1);
  139. EXPECT_COUNT(root21, 10, 11, 9);
  140. EXPECT_COUNT(root22, 7, 8, 6);
  141. EXPECT_COUNT(root221, 7, 8, 5);
  142. EXPECT_COUNT(root23, 3, 4, 2);
  143. EXPECT_COUNT(root231, 3, 4, 1);
  144. EXPECT_COUNT(root3, 14, -1, 0);
  145. }
  146. } // namespace
  147. } // namespace cc