Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_perftest.cc
blob39bcbe628cc60cf9e21c03d2c9aecde88e6926e3
1 // Copyright 2013 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.
5 #include "cc/trees/layer_tree_host_common.h"
7 #include <sstream>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_piece.h"
14 #include "base/threading/thread.h"
15 #include "base/time/time.h"
16 #include "cc/base/scoped_ptr_deque.h"
17 #include "cc/base/scoped_ptr_vector.h"
18 #include "cc/debug/lap_timer.h"
19 #include "cc/layers/layer.h"
20 #include "cc/output/bsp_tree.h"
21 #include "cc/quads/draw_polygon.h"
22 #include "cc/quads/draw_quad.h"
23 #include "cc/test/fake_content_layer_client.h"
24 #include "cc/test/fake_layer_tree_host_client.h"
25 #include "cc/test/layer_tree_json_parser.h"
26 #include "cc/test/layer_tree_test.h"
27 #include "cc/test/paths.h"
28 #include "cc/trees/layer_sorter.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "testing/perf/perf_test.h"
32 namespace cc {
33 namespace {
35 static const int kTimeLimitMillis = 2000;
36 static const int kWarmupRuns = 5;
37 static const int kTimeCheckInterval = 10;
39 class LayerTreeHostCommonPerfTest : public LayerTreeTest {
40 public:
41 LayerTreeHostCommonPerfTest()
42 : timer_(kWarmupRuns,
43 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
44 kTimeCheckInterval) {}
46 void ReadTestFile(const std::string& name) {
47 base::FilePath test_data_dir;
48 ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir));
49 base::FilePath json_file = test_data_dir.AppendASCII(name + ".json");
50 ASSERT_TRUE(base::ReadFileToString(json_file, &json_));
53 void SetupTree() override {
54 gfx::Size viewport = gfx::Size(720, 1038);
55 layer_tree_host()->SetViewportSize(viewport);
56 scoped_refptr<Layer> root =
57 ParseTreeFromJson(json_, &content_layer_client_);
58 ASSERT_TRUE(root.get());
59 layer_tree_host()->SetRootLayer(root);
62 void SetTestName(const std::string& name) { test_name_ = name; }
64 void AfterTest() override {
65 CHECK(!test_name_.empty()) << "Must SetTestName() before TearDown().";
66 perf_test::PrintResult("calc_draw_props_time",
67 "",
68 test_name_,
69 1000 * timer_.MsPerLap(),
70 "us",
71 true);
74 protected:
75 FakeContentLayerClient content_layer_client_;
76 LapTimer timer_;
77 std::string test_name_;
78 std::string json_;
81 class CalcDrawPropsMainTest : public LayerTreeHostCommonPerfTest {
82 public:
83 void RunCalcDrawProps() {
84 RunTest(false, false, false);
87 void BeginTest() override {
88 timer_.Reset();
90 do {
91 bool can_render_to_separate_surface = true;
92 int max_texture_size = 8096;
93 RenderSurfaceLayerList update_list;
94 LayerTreeHostCommon::CalcDrawPropsMainInputs inputs(
95 layer_tree_host()->root_layer(),
96 layer_tree_host()->device_viewport_size(), gfx::Transform(),
97 layer_tree_host()->device_scale_factor(),
98 layer_tree_host()->page_scale_factor(),
99 layer_tree_host()->page_scale_layer(), max_texture_size,
100 layer_tree_host()->settings().can_use_lcd_text,
101 layer_tree_host()->settings().layers_always_allowed_lcd_text,
102 can_render_to_separate_surface,
103 layer_tree_host()
104 ->settings()
105 .layer_transforms_should_scale_layer_contents,
106 &update_list, 0);
107 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
109 timer_.NextLap();
110 } while (!timer_.HasTimeLimitExpired());
112 EndTest();
116 class CalcDrawPropsImplTest : public LayerTreeHostCommonPerfTest {
117 public:
118 void RunCalcDrawProps() {
119 RunTestWithImplSidePainting();
122 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
124 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
125 timer_.Reset();
126 LayerTreeImpl* active_tree = host_impl->active_tree();
128 do {
129 bool can_render_to_separate_surface = true;
130 int max_texture_size = 8096;
131 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
132 max_texture_size,
133 active_tree,
134 host_impl);
136 timer_.NextLap();
137 } while (!timer_.HasTimeLimitExpired());
139 EndTest();
142 void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface,
143 int max_texture_size,
144 LayerTreeImpl* active_tree,
145 LayerTreeHostImpl* host_impl) {
146 LayerImplList update_list;
147 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
148 active_tree->root_layer(), active_tree->DrawViewportSize(),
149 host_impl->DrawTransform(), active_tree->device_scale_factor(),
150 active_tree->total_page_scale_factor(),
151 active_tree->InnerViewportContainerLayer(), max_texture_size,
152 host_impl->settings().can_use_lcd_text,
153 host_impl->settings().layers_always_allowed_lcd_text,
154 can_render_to_separate_surface,
155 host_impl->settings().layer_transforms_should_scale_layer_contents,
156 &update_list, 0);
157 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
161 class LayerSorterMainTest : public CalcDrawPropsImplTest {
162 public:
163 void RunSortLayers() { RunTest(false, false, false); }
165 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
167 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
168 LayerTreeImpl* active_tree = host_impl->active_tree();
169 // First build the tree and then we'll start running tests on layersorter
170 // itself
171 bool can_render_to_separate_surface = true;
172 int max_texture_size = 8096;
173 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
174 max_texture_size,
175 active_tree,
176 host_impl);
178 // Behaviour of this test is different from that of sorting in practice.
179 // In this case, all layers that exist in any 3D context are put into a list
180 // and are sorted as one big 3D context instead of several smaller ones.
181 BuildLayerImplList(active_tree->root_layer(), &base_list_);
182 timer_.Reset();
183 do {
184 // Here we'll move the layers into a LayerImpl list of their own to be
185 // sorted so we don't have a sorted list for every run after the first
186 LayerImplList test_list = base_list_;
187 layer_sorter_.Sort(test_list.begin(), test_list.end());
188 timer_.NextLap();
189 } while (!timer_.HasTimeLimitExpired());
191 EndTest();
194 void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) {
195 if (layer->Is3dSorted()) {
196 list->push_back(layer);
199 for (size_t i = 0; i < layer->children().size(); i++) {
200 BuildLayerImplList(layer->children()[i], list);
204 private:
205 LayerImplList base_list_;
206 LayerSorter layer_sorter_;
209 class BspTreePerfTest : public LayerSorterMainTest {
210 public:
211 void RunSortLayers() { RunTest(false, false, false); }
213 void SetNumberOfDuplicates(int num_duplicates) {
214 num_duplicates_ = num_duplicates;
217 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
219 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
220 LayerTreeImpl* active_tree = host_impl->active_tree();
221 // First build the tree and then we'll start running tests on layersorter
222 // itself
223 bool can_render_to_separate_surface = true;
224 int max_texture_size = 8096;
225 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
226 max_texture_size,
227 active_tree,
228 host_impl);
230 LayerImplList base_list;
231 BuildLayerImplList(active_tree->root_layer(), &base_list);
233 int polygon_counter = 0;
234 ScopedPtrVector<DrawPolygon> polygon_list;
235 for (LayerImplList::iterator it = base_list.begin(); it != base_list.end();
236 ++it) {
237 DrawPolygon* draw_polygon =
238 new DrawPolygon(NULL,
239 gfx::RectF((*it)->content_bounds()),
240 (*it)->draw_transform(),
241 polygon_counter++);
242 polygon_list.push_back(scoped_ptr<DrawPolygon>(draw_polygon));
245 timer_.Reset();
246 do {
247 ScopedPtrDeque<DrawPolygon> test_list;
248 for (int i = 0; i < num_duplicates_; i++) {
249 for (size_t i = 0; i < polygon_list.size(); i++) {
250 test_list.push_back(polygon_list[i]->CreateCopy());
253 BspTree bsp_tree(&test_list);
254 timer_.NextLap();
255 } while (!timer_.HasTimeLimitExpired());
257 EndTest();
260 private:
261 int num_duplicates_;
264 TEST_F(CalcDrawPropsMainTest, TenTen) {
265 SetTestName("10_10_main_thread");
266 ReadTestFile("10_10_layer_tree");
267 RunCalcDrawProps();
270 TEST_F(CalcDrawPropsMainTest, HeavyPage) {
271 SetTestName("heavy_page_main_thread");
272 ReadTestFile("heavy_layer_tree");
273 RunCalcDrawProps();
276 TEST_F(CalcDrawPropsMainTest, TouchRegionLight) {
277 SetTestName("touch_region_light_main_thread");
278 ReadTestFile("touch_region_light");
279 RunCalcDrawProps();
282 TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) {
283 SetTestName("touch_region_heavy_main_thread");
284 ReadTestFile("touch_region_heavy");
285 RunCalcDrawProps();
288 TEST_F(CalcDrawPropsImplTest, TenTen) {
289 SetTestName("10_10");
290 ReadTestFile("10_10_layer_tree");
291 RunCalcDrawProps();
294 TEST_F(CalcDrawPropsImplTest, HeavyPage) {
295 SetTestName("heavy_page");
296 ReadTestFile("heavy_layer_tree");
297 RunCalcDrawProps();
300 TEST_F(CalcDrawPropsImplTest, TouchRegionLight) {
301 SetTestName("touch_region_light");
302 ReadTestFile("touch_region_light");
303 RunCalcDrawProps();
306 TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) {
307 SetTestName("touch_region_heavy");
308 ReadTestFile("touch_region_heavy");
309 RunCalcDrawProps();
312 TEST_F(LayerSorterMainTest, LayerSorterCubes) {
313 SetTestName("layer_sort_cubes");
314 ReadTestFile("layer_sort_cubes");
315 RunSortLayers();
318 TEST_F(LayerSorterMainTest, LayerSorterRubik) {
319 SetTestName("layer_sort_rubik");
320 ReadTestFile("layer_sort_rubik");
321 RunSortLayers();
324 TEST_F(BspTreePerfTest, BspTreeCubes) {
325 SetTestName("bsp_tree_cubes");
326 SetNumberOfDuplicates(1);
327 ReadTestFile("layer_sort_cubes");
328 RunSortLayers();
331 TEST_F(BspTreePerfTest, BspTreeRubik) {
332 SetTestName("bsp_tree_rubik");
333 SetNumberOfDuplicates(1);
334 ReadTestFile("layer_sort_rubik");
335 RunSortLayers();
338 TEST_F(BspTreePerfTest, BspTreeCubes_2) {
339 SetTestName("bsp_tree_cubes_2");
340 SetNumberOfDuplicates(2);
341 ReadTestFile("layer_sort_cubes");
342 RunSortLayers();
345 TEST_F(BspTreePerfTest, BspTreeCubes_4) {
346 SetTestName("bsp_tree_cubes_4");
347 SetNumberOfDuplicates(4);
348 ReadTestFile("layer_sort_cubes");
349 RunSortLayers();
352 } // namespace
353 } // namespace cc