OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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/layers/layer_iterator.h" | 5 #include "cc/layers/effect_tree_layer_list_iterator.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
11 #include "cc/test/fake_layer_tree_host.h" | 11 #include "cc/test/fake_layer_tree_host.h" |
12 #include "cc/test/test_task_graph_runner.h" | 12 #include "cc/test/test_task_graph_runner.h" |
13 #include "cc/trees/layer_tree_host_common.h" | 13 #include "cc/trees/layer_tree_host_common.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
17 | 17 |
18 using ::testing::Mock; | |
19 using ::testing::_; | |
20 using ::testing::AtLeast; | |
21 using ::testing::AnyNumber; | |
22 | |
23 namespace cc { | 18 namespace cc { |
24 namespace { | 19 namespace { |
25 | 20 |
26 class TestLayerImpl : public LayerImpl { | 21 class TestLayerImpl : public LayerImpl { |
27 public: | 22 public: |
28 static std::unique_ptr<TestLayerImpl> Create(LayerTreeImpl* tree, int id) { | 23 static std::unique_ptr<TestLayerImpl> Create(LayerTreeImpl* tree, int id) { |
29 return base::WrapUnique(new TestLayerImpl(tree, id)); | 24 return base::WrapUnique(new TestLayerImpl(tree, id)); |
30 } | 25 } |
31 ~TestLayerImpl() override {} | 26 ~TestLayerImpl() override {} |
32 | 27 |
33 int count_representing_target_surface_; | 28 int count_; |
34 int count_representing_contributing_surface_; | |
35 int count_representing_itself_; | |
36 | 29 |
37 private: | 30 private: |
38 explicit TestLayerImpl(LayerTreeImpl* tree, int id) | 31 explicit TestLayerImpl(LayerTreeImpl* tree, int id) |
39 : LayerImpl(tree, id), | 32 : LayerImpl(tree, id), count_(-1) { |
40 count_representing_target_surface_(-1), | |
41 count_representing_contributing_surface_(-1), | |
42 count_representing_itself_(-1) { | |
43 SetBounds(gfx::Size(100, 100)); | 33 SetBounds(gfx::Size(100, 100)); |
44 SetPosition(gfx::PointF()); | 34 SetPosition(gfx::PointF()); |
45 SetDrawsContent(true); | 35 SetDrawsContent(true); |
46 } | 36 } |
47 }; | 37 }; |
48 | 38 |
49 #define EXPECT_COUNT(layer, target, contrib, itself) \ | 39 #define EXPECT_COUNT(layer, target, contrib, itself) \ |
50 EXPECT_EQ(target, layer->count_representing_target_surface_); \ | 40 if (layer->GetRenderSurface()) { \ |
51 EXPECT_EQ(contrib, layer->count_representing_contributing_surface_); \ | 41 EXPECT_EQ(target, target_surface_count_[layer->effect_tree_index()]); \ |
52 EXPECT_EQ(itself, layer->count_representing_itself_); | 42 EXPECT_EQ(contrib, \ |
| 43 contributing_surface_count_[layer->effect_tree_index()]); \ |
| 44 } \ |
| 45 EXPECT_EQ(itself, layer->count_); |
53 | 46 |
54 void ResetCounts(LayerImplList* render_surface_layer_list) { | 47 class EffectTreeLayerListIteratorTest : public testing::Test { |
55 for (unsigned surface_index = 0; | |
56 surface_index < render_surface_layer_list->size(); | |
57 ++surface_index) { | |
58 TestLayerImpl* render_surface_layer = static_cast<TestLayerImpl*>( | |
59 render_surface_layer_list->at(surface_index)); | |
60 RenderSurfaceImpl* render_surface = | |
61 render_surface_layer->GetRenderSurface(); | |
62 | |
63 render_surface_layer->count_representing_target_surface_ = -1; | |
64 render_surface_layer->count_representing_contributing_surface_ = -1; | |
65 render_surface_layer->count_representing_itself_ = -1; | |
66 | |
67 for (unsigned layer_index = 0; | |
68 layer_index < render_surface->layer_list().size(); | |
69 ++layer_index) { | |
70 TestLayerImpl* layer = static_cast<TestLayerImpl*>( | |
71 render_surface->layer_list()[layer_index]); | |
72 | |
73 layer->count_representing_target_surface_ = -1; | |
74 layer->count_representing_contributing_surface_ = -1; | |
75 layer->count_representing_itself_ = -1; | |
76 } | |
77 } | |
78 } | |
79 | |
80 void IterateFrontToBack(LayerImplList* render_surface_layer_list) { | |
81 ResetCounts(render_surface_layer_list); | |
82 int count = 0; | |
83 for (LayerIterator it = LayerIterator::Begin(render_surface_layer_list); | |
84 it != LayerIterator::End(render_surface_layer_list); ++it, ++count) { | |
85 TestLayerImpl* layer = static_cast<TestLayerImpl*>(*it); | |
86 if (it.represents_target_render_surface()) | |
87 layer->count_representing_target_surface_ = count; | |
88 if (it.represents_contributing_render_surface()) | |
89 layer->count_representing_contributing_surface_ = count; | |
90 if (it.represents_itself()) | |
91 layer->count_representing_itself_ = count; | |
92 } | |
93 } | |
94 | |
95 class LayerIteratorTest : public testing::Test { | |
96 public: | 48 public: |
97 LayerIteratorTest() | 49 EffectTreeLayerListIteratorTest() |
98 : host_impl_(&task_runner_provider_, &task_graph_runner_), id_(1) {} | 50 : host_impl_(&task_runner_provider_, &task_graph_runner_), id_(1) {} |
99 | 51 |
100 std::unique_ptr<TestLayerImpl> CreateLayer() { | 52 std::unique_ptr<TestLayerImpl> CreateLayer() { |
101 return TestLayerImpl::Create(host_impl_.active_tree(), id_++); | 53 return TestLayerImpl::Create(host_impl_.active_tree(), id_++); |
102 } | 54 } |
103 | 55 |
| 56 void IterateFrontToBack() { |
| 57 ResetCounts(); |
| 58 int count = 0; |
| 59 for (EffectTreeLayerListIterator it(host_impl_.active_tree()); |
| 60 it.state() != EffectTreeLayerListIterator::State::END; ++it, ++count) { |
| 61 switch (it.state()) { |
| 62 case EffectTreeLayerListIterator::State::LAYER: |
| 63 static_cast<TestLayerImpl*>(it.current_layer())->count_ = count; |
| 64 break; |
| 65 case EffectTreeLayerListIterator::State::TARGET_SURFACE: |
| 66 target_surface_count_[it.target_render_surface()->EffectTreeIndex()] = |
| 67 count; |
| 68 break; |
| 69 case EffectTreeLayerListIterator::State::CONTRIBUTING_SURFACE: |
| 70 contributing_surface_count_[it.current_render_surface() |
| 71 ->EffectTreeIndex()] = count; |
| 72 break; |
| 73 default: |
| 74 NOTREACHED(); |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 void ResetCounts() { |
| 80 for (LayerImpl* layer : *host_impl_.active_tree()) { |
| 81 static_cast<TestLayerImpl*>(layer)->count_ = -1; |
| 82 } |
| 83 |
| 84 target_surface_count_ = std::vector<int>( |
| 85 host_impl_.active_tree()->property_trees()->effect_tree.size(), -1); |
| 86 contributing_surface_count_ = std::vector<int>( |
| 87 host_impl_.active_tree()->property_trees()->effect_tree.size(), -1); |
| 88 } |
| 89 |
104 protected: | 90 protected: |
105 FakeImplTaskRunnerProvider task_runner_provider_; | 91 FakeImplTaskRunnerProvider task_runner_provider_; |
106 TestTaskGraphRunner task_graph_runner_; | 92 TestTaskGraphRunner task_graph_runner_; |
107 FakeLayerTreeHostImpl host_impl_; | 93 FakeLayerTreeHostImpl host_impl_; |
108 | 94 |
109 int id_; | 95 int id_; |
| 96 |
| 97 // Tracks when each render surface is visited as a target surface or |
| 98 // contributing surface. Indexed by effect node id. |
| 99 std::vector<int> target_surface_count_; |
| 100 std::vector<int> contributing_surface_count_; |
110 }; | 101 }; |
111 | 102 |
112 TEST_F(LayerIteratorTest, EmptyTree) { | 103 TEST_F(EffectTreeLayerListIteratorTest, TreeWithNoDrawnLayers) { |
| 104 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); |
| 105 root_layer->SetDrawsContent(false); |
| 106 |
| 107 TestLayerImpl* root_ptr = root_layer.get(); |
| 108 |
| 109 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); |
| 110 |
113 LayerImplList render_surface_layer_list; | 111 LayerImplList render_surface_layer_list; |
| 112 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 113 root_ptr, root_ptr->bounds(), &render_surface_layer_list); |
| 114 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); |
114 | 115 |
115 IterateFrontToBack(&render_surface_layer_list); | 116 IterateFrontToBack(); |
| 117 EXPECT_COUNT(root_ptr, 0, -1, -1); |
116 } | 118 } |
117 | 119 |
118 TEST_F(LayerIteratorTest, SimpleTree) { | 120 TEST_F(EffectTreeLayerListIteratorTest, SimpleTree) { |
119 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); | 121 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); |
120 std::unique_ptr<TestLayerImpl> first = CreateLayer(); | 122 std::unique_ptr<TestLayerImpl> first = CreateLayer(); |
121 std::unique_ptr<TestLayerImpl> second = CreateLayer(); | 123 std::unique_ptr<TestLayerImpl> second = CreateLayer(); |
122 std::unique_ptr<TestLayerImpl> third = CreateLayer(); | 124 std::unique_ptr<TestLayerImpl> third = CreateLayer(); |
123 std::unique_ptr<TestLayerImpl> fourth = CreateLayer(); | 125 std::unique_ptr<TestLayerImpl> fourth = CreateLayer(); |
124 | 126 |
125 TestLayerImpl* root_ptr = root_layer.get(); | 127 TestLayerImpl* root_ptr = root_layer.get(); |
126 TestLayerImpl* first_ptr = first.get(); | 128 TestLayerImpl* first_ptr = first.get(); |
127 TestLayerImpl* second_ptr = second.get(); | 129 TestLayerImpl* second_ptr = second.get(); |
128 TestLayerImpl* third_ptr = third.get(); | 130 TestLayerImpl* third_ptr = third.get(); |
129 TestLayerImpl* fourth_ptr = fourth.get(); | 131 TestLayerImpl* fourth_ptr = fourth.get(); |
130 | 132 |
131 root_layer->test_properties()->AddChild(std::move(first)); | 133 root_layer->test_properties()->AddChild(std::move(first)); |
132 root_layer->test_properties()->AddChild(std::move(second)); | 134 root_layer->test_properties()->AddChild(std::move(second)); |
133 root_layer->test_properties()->AddChild(std::move(third)); | 135 root_layer->test_properties()->AddChild(std::move(third)); |
134 root_layer->test_properties()->AddChild(std::move(fourth)); | 136 root_layer->test_properties()->AddChild(std::move(fourth)); |
135 | 137 |
136 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); | 138 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); |
137 | 139 |
138 LayerImplList render_surface_layer_list; | 140 LayerImplList render_surface_layer_list; |
139 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( | 141 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
140 root_ptr, root_ptr->bounds(), &render_surface_layer_list); | 142 root_ptr, root_ptr->bounds(), &render_surface_layer_list); |
141 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); | 143 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); |
142 | 144 |
143 IterateFrontToBack(&render_surface_layer_list); | 145 IterateFrontToBack(); |
144 EXPECT_COUNT(root_ptr, 5, -1, 4); | 146 EXPECT_COUNT(root_ptr, 5, -1, 4); |
145 EXPECT_COUNT(first_ptr, -1, -1, 3); | 147 EXPECT_COUNT(first_ptr, -1, -1, 3); |
146 EXPECT_COUNT(second_ptr, -1, -1, 2); | 148 EXPECT_COUNT(second_ptr, -1, -1, 2); |
147 EXPECT_COUNT(third_ptr, -1, -1, 1); | 149 EXPECT_COUNT(third_ptr, -1, -1, 1); |
148 EXPECT_COUNT(fourth_ptr, -1, -1, 0); | 150 EXPECT_COUNT(fourth_ptr, -1, -1, 0); |
149 } | 151 } |
150 | 152 |
151 TEST_F(LayerIteratorTest, ComplexTree) { | 153 TEST_F(EffectTreeLayerListIteratorTest, ComplexTree) { |
152 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); | 154 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); |
153 std::unique_ptr<TestLayerImpl> root1 = CreateLayer(); | 155 std::unique_ptr<TestLayerImpl> root1 = CreateLayer(); |
154 std::unique_ptr<TestLayerImpl> root2 = CreateLayer(); | 156 std::unique_ptr<TestLayerImpl> root2 = CreateLayer(); |
155 std::unique_ptr<TestLayerImpl> root3 = CreateLayer(); | 157 std::unique_ptr<TestLayerImpl> root3 = CreateLayer(); |
156 std::unique_ptr<TestLayerImpl> root21 = CreateLayer(); | 158 std::unique_ptr<TestLayerImpl> root21 = CreateLayer(); |
157 std::unique_ptr<TestLayerImpl> root22 = CreateLayer(); | 159 std::unique_ptr<TestLayerImpl> root22 = CreateLayer(); |
158 std::unique_ptr<TestLayerImpl> root23 = CreateLayer(); | 160 std::unique_ptr<TestLayerImpl> root23 = CreateLayer(); |
159 std::unique_ptr<TestLayerImpl> root221 = CreateLayer(); | 161 std::unique_ptr<TestLayerImpl> root221 = CreateLayer(); |
160 std::unique_ptr<TestLayerImpl> root231 = CreateLayer(); | 162 std::unique_ptr<TestLayerImpl> root231 = CreateLayer(); |
161 | 163 |
(...skipping 16 matching lines...) Expand all Loading... |
178 root_layer->test_properties()->AddChild(std::move(root2)); | 180 root_layer->test_properties()->AddChild(std::move(root2)); |
179 root_layer->test_properties()->AddChild(std::move(root3)); | 181 root_layer->test_properties()->AddChild(std::move(root3)); |
180 | 182 |
181 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); | 183 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); |
182 | 184 |
183 LayerImplList render_surface_layer_list; | 185 LayerImplList render_surface_layer_list; |
184 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( | 186 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
185 root_ptr, root_ptr->bounds(), &render_surface_layer_list); | 187 root_ptr, root_ptr->bounds(), &render_surface_layer_list); |
186 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); | 188 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); |
187 | 189 |
188 IterateFrontToBack(&render_surface_layer_list); | 190 IterateFrontToBack(); |
189 EXPECT_COUNT(root_ptr, 9, -1, 8); | 191 EXPECT_COUNT(root_ptr, 9, -1, 8); |
190 EXPECT_COUNT(root1_ptr, -1, -1, 7); | 192 EXPECT_COUNT(root1_ptr, -1, -1, 7); |
191 EXPECT_COUNT(root2_ptr, -1, -1, 6); | 193 EXPECT_COUNT(root2_ptr, -1, -1, 6); |
192 EXPECT_COUNT(root21_ptr, -1, -1, 5); | 194 EXPECT_COUNT(root21_ptr, -1, -1, 5); |
193 EXPECT_COUNT(root22_ptr, -1, -1, 4); | 195 EXPECT_COUNT(root22_ptr, -1, -1, 4); |
194 EXPECT_COUNT(root221_ptr, -1, -1, 3); | 196 EXPECT_COUNT(root221_ptr, -1, -1, 3); |
195 EXPECT_COUNT(root23_ptr, -1, -1, 2); | 197 EXPECT_COUNT(root23_ptr, -1, -1, 2); |
196 EXPECT_COUNT(root231_ptr, -1, -1, 1); | 198 EXPECT_COUNT(root231_ptr, -1, -1, 1); |
197 EXPECT_COUNT(root3_ptr, -1, -1, 0); | 199 EXPECT_COUNT(root3_ptr, -1, -1, 0); |
198 } | 200 } |
199 | 201 |
200 TEST_F(LayerIteratorTest, ComplexTreeMultiSurface) { | 202 TEST_F(EffectTreeLayerListIteratorTest, ComplexTreeMultiSurface) { |
201 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); | 203 std::unique_ptr<TestLayerImpl> root_layer = CreateLayer(); |
202 std::unique_ptr<TestLayerImpl> root1 = CreateLayer(); | 204 std::unique_ptr<TestLayerImpl> root1 = CreateLayer(); |
203 std::unique_ptr<TestLayerImpl> root2 = CreateLayer(); | 205 std::unique_ptr<TestLayerImpl> root2 = CreateLayer(); |
204 std::unique_ptr<TestLayerImpl> root3 = CreateLayer(); | 206 std::unique_ptr<TestLayerImpl> root3 = CreateLayer(); |
205 std::unique_ptr<TestLayerImpl> root21 = CreateLayer(); | 207 std::unique_ptr<TestLayerImpl> root21 = CreateLayer(); |
206 std::unique_ptr<TestLayerImpl> root22 = CreateLayer(); | 208 std::unique_ptr<TestLayerImpl> root22 = CreateLayer(); |
207 std::unique_ptr<TestLayerImpl> root23 = CreateLayer(); | 209 std::unique_ptr<TestLayerImpl> root23 = CreateLayer(); |
208 std::unique_ptr<TestLayerImpl> root221 = CreateLayer(); | 210 std::unique_ptr<TestLayerImpl> root221 = CreateLayer(); |
209 std::unique_ptr<TestLayerImpl> root231 = CreateLayer(); | 211 std::unique_ptr<TestLayerImpl> root231 = CreateLayer(); |
210 | 212 |
(...skipping 20 matching lines...) Expand all Loading... |
231 root_layer->test_properties()->AddChild(std::move(root2)); | 233 root_layer->test_properties()->AddChild(std::move(root2)); |
232 root_layer->test_properties()->AddChild(std::move(root3)); | 234 root_layer->test_properties()->AddChild(std::move(root3)); |
233 | 235 |
234 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); | 236 host_impl_.active_tree()->SetRootLayerForTesting(std::move(root_layer)); |
235 | 237 |
236 LayerImplList render_surface_layer_list; | 238 LayerImplList render_surface_layer_list; |
237 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( | 239 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
238 root_ptr, root_ptr->bounds(), &render_surface_layer_list); | 240 root_ptr, root_ptr->bounds(), &render_surface_layer_list); |
239 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); | 241 LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs); |
240 | 242 |
241 IterateFrontToBack(&render_surface_layer_list); | 243 IterateFrontToBack(); |
242 EXPECT_COUNT(root_ptr, 14, -1, 13); | 244 EXPECT_COUNT(root_ptr, 14, -1, 13); |
243 EXPECT_COUNT(root1_ptr, -1, -1, 12); | 245 EXPECT_COUNT(root1_ptr, -1, -1, 12); |
244 EXPECT_COUNT(root2_ptr, 10, 11, -1); | 246 EXPECT_COUNT(root2_ptr, 10, 11, -1); |
245 EXPECT_COUNT(root21_ptr, -1, -1, 9); | 247 EXPECT_COUNT(root21_ptr, -1, -1, 9); |
246 EXPECT_COUNT(root22_ptr, 7, 8, 6); | 248 EXPECT_COUNT(root22_ptr, 7, 8, 6); |
247 EXPECT_COUNT(root221_ptr, -1, -1, 5); | 249 EXPECT_COUNT(root221_ptr, -1, -1, 5); |
248 EXPECT_COUNT(root23_ptr, 3, 4, 2); | 250 EXPECT_COUNT(root23_ptr, 3, 4, 2); |
249 EXPECT_COUNT(root231_ptr, -1, -1, 1); | 251 EXPECT_COUNT(root231_ptr, -1, -1, 1); |
250 EXPECT_COUNT(root3_ptr, -1, -1, 0); | 252 EXPECT_COUNT(root3_ptr, -1, -1, 0); |
251 } | 253 } |
252 | 254 |
253 } // namespace | 255 } // namespace |
254 } // namespace cc | 256 } // namespace cc |
OLD | NEW |