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"
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_tree_impl.h"
29 #include "testing/perf/perf_test.h"
34 static const int kTimeLimitMillis
= 2000;
35 static const int kWarmupRuns
= 5;
36 static const int kTimeCheckInterval
= 10;
38 class LayerTreeHostCommonPerfTest
: public LayerTreeTest
{
40 LayerTreeHostCommonPerfTest()
42 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
43 kTimeCheckInterval
) {}
45 void ReadTestFile(const std::string
& name
) {
46 base::FilePath test_data_dir
;
47 ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA
, &test_data_dir
));
48 base::FilePath json_file
= test_data_dir
.AppendASCII(name
+ ".json");
49 ASSERT_TRUE(base::ReadFileToString(json_file
, &json_
));
52 void SetupTree() override
{
53 gfx::Size viewport
= gfx::Size(720, 1038);
54 layer_tree_host()->SetViewportSize(viewport
);
55 scoped_refptr
<Layer
> root
=
56 ParseTreeFromJson(json_
, &content_layer_client_
);
57 ASSERT_TRUE(root
.get());
58 layer_tree_host()->SetRootLayer(root
);
61 void SetTestName(const std::string
& name
) { test_name_
= name
; }
63 void AfterTest() override
{
64 CHECK(!test_name_
.empty()) << "Must SetTestName() before TearDown().";
65 perf_test::PrintResult("calc_draw_props_time",
68 1000 * timer_
.MsPerLap(),
74 FakeContentLayerClient content_layer_client_
;
76 std::string test_name_
;
80 class CalcDrawPropsMainTest
: public LayerTreeHostCommonPerfTest
{
82 void RunCalcDrawProps() { RunTest(false, false, false); }
84 void BeginTest() override
{
88 bool can_render_to_separate_surface
= true;
89 bool verify_property_trees
= false;
90 int max_texture_size
= 8096;
91 RenderSurfaceLayerList update_list
;
92 LayerTreeHostCommon::CalcDrawPropsMainInputs
inputs(
93 layer_tree_host()->root_layer(),
94 layer_tree_host()->device_viewport_size(), gfx::Transform(),
95 layer_tree_host()->device_scale_factor(),
96 layer_tree_host()->page_scale_factor(),
97 layer_tree_host()->overscroll_elasticity_layer(),
98 layer_tree_host()->elastic_overscroll(),
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
,
105 .layer_transforms_should_scale_layer_contents
,
106 verify_property_trees
, &update_list
, 0);
107 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
110 } while (!timer_
.HasTimeLimitExpired());
116 class CalcDrawPropsImplTest
: public LayerTreeHostCommonPerfTest
{
118 void RunCalcDrawProps() {
119 RunTestWithImplSidePainting();
122 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
124 void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) override
{
126 LayerTreeImpl
* active_tree
= host_impl
->active_tree();
129 bool can_render_to_separate_surface
= true;
130 int max_texture_size
= 8096;
131 DoCalcDrawPropertiesImpl(can_render_to_separate_surface
,
137 } while (!timer_
.HasTimeLimitExpired());
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 bool verify_property_trees
= false;
148 LayerTreeHostCommon::CalcDrawPropsImplInputs
inputs(
149 active_tree
->root_layer(), active_tree
->DrawViewportSize(),
150 host_impl
->DrawTransform(), active_tree
->device_scale_factor(),
151 active_tree
->current_page_scale_factor(),
152 active_tree
->InnerViewportContainerLayer(),
153 active_tree
->elastic_overscroll()->Current(active_tree
->IsActiveTree()),
154 active_tree
->overscroll_elasticity_layer(), max_texture_size
,
155 host_impl
->settings().can_use_lcd_text
,
156 host_impl
->settings().layers_always_allowed_lcd_text
,
157 can_render_to_separate_surface
,
158 host_impl
->settings().layer_transforms_should_scale_layer_contents
,
159 verify_property_trees
, &update_list
, 0);
160 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
164 class BspTreePerfTest
: public CalcDrawPropsImplTest
{
166 BspTreePerfTest() : num_duplicates_(1) {}
167 void RunSortLayers() { RunTest(false, false, false); }
169 void SetNumberOfDuplicates(int num_duplicates
) {
170 num_duplicates_
= num_duplicates
;
173 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
175 void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) override
{
176 LayerTreeImpl
* active_tree
= host_impl
->active_tree();
177 // First build the tree and then we'll start running tests on layersorter
179 bool can_render_to_separate_surface
= true;
180 int max_texture_size
= 8096;
181 DoCalcDrawPropertiesImpl(can_render_to_separate_surface
,
186 LayerImplList base_list
;
187 BuildLayerImplList(active_tree
->root_layer(), &base_list
);
189 int polygon_counter
= 0;
190 ScopedPtrVector
<DrawPolygon
> polygon_list
;
191 for (LayerImplList::iterator it
= base_list
.begin(); it
!= base_list
.end();
193 DrawPolygon
* draw_polygon
=
194 new DrawPolygon(NULL
,
195 gfx::RectF((*it
)->content_bounds()),
196 (*it
)->draw_transform(),
198 polygon_list
.push_back(scoped_ptr
<DrawPolygon
>(draw_polygon
));
203 ScopedPtrDeque
<DrawPolygon
> test_list
;
204 for (int i
= 0; i
< num_duplicates_
; i
++) {
205 for (size_t i
= 0; i
< polygon_list
.size(); i
++) {
206 test_list
.push_back(polygon_list
[i
]->CreateCopy());
209 BspTree
bsp_tree(&test_list
);
211 } while (!timer_
.HasTimeLimitExpired());
216 void BuildLayerImplList(LayerImpl
* layer
, LayerImplList
* list
) {
217 if (layer
->Is3dSorted()) {
218 list
->push_back(layer
);
221 for (size_t i
= 0; i
< layer
->children().size(); i
++) {
222 BuildLayerImplList(layer
->children()[i
], list
);
227 LayerImplList base_list_
;
231 TEST_F(CalcDrawPropsMainTest
, TenTen
) {
232 SetTestName("10_10_main_thread");
233 ReadTestFile("10_10_layer_tree");
237 TEST_F(CalcDrawPropsMainTest
, HeavyPage
) {
238 SetTestName("heavy_page_main_thread");
239 ReadTestFile("heavy_layer_tree");
243 TEST_F(CalcDrawPropsMainTest
, TouchRegionLight
) {
244 SetTestName("touch_region_light_main_thread");
245 ReadTestFile("touch_region_light");
249 TEST_F(CalcDrawPropsMainTest
, TouchRegionHeavy
) {
250 SetTestName("touch_region_heavy_main_thread");
251 ReadTestFile("touch_region_heavy");
255 TEST_F(CalcDrawPropsImplTest
, TenTen
) {
256 SetTestName("10_10");
257 ReadTestFile("10_10_layer_tree");
261 TEST_F(CalcDrawPropsImplTest
, HeavyPage
) {
262 SetTestName("heavy_page");
263 ReadTestFile("heavy_layer_tree");
267 TEST_F(CalcDrawPropsImplTest
, TouchRegionLight
) {
268 SetTestName("touch_region_light");
269 ReadTestFile("touch_region_light");
273 TEST_F(CalcDrawPropsImplTest
, TouchRegionHeavy
) {
274 SetTestName("touch_region_heavy");
275 ReadTestFile("touch_region_heavy");
279 TEST_F(BspTreePerfTest
, LayerSorterCubes
) {
280 SetTestName("layer_sort_cubes");
281 ReadTestFile("layer_sort_cubes");
285 TEST_F(BspTreePerfTest
, LayerSorterRubik
) {
286 SetTestName("layer_sort_rubik");
287 ReadTestFile("layer_sort_rubik");
288 // TODO(vollick): Remove verify_property_trees setting after
289 // crbug.com/444219 is fixed.
290 bool old_verify_property_trees
= verify_property_trees();
291 set_verify_property_trees(false);
293 set_verify_property_trees(old_verify_property_trees
);
296 TEST_F(BspTreePerfTest
, BspTreeCubes
) {
297 SetTestName("bsp_tree_cubes");
298 SetNumberOfDuplicates(1);
299 ReadTestFile("layer_sort_cubes");
303 TEST_F(BspTreePerfTest
, BspTreeRubik
) {
304 SetTestName("bsp_tree_rubik");
305 SetNumberOfDuplicates(1);
306 ReadTestFile("layer_sort_rubik");
307 // TODO(vollick): Remove verify_property_trees setting after
308 // crbug.com/444219 is fixed.
309 bool old_verify_property_trees
= verify_property_trees();
310 set_verify_property_trees(false);
312 set_verify_property_trees(old_verify_property_trees
);
315 TEST_F(BspTreePerfTest
, BspTreeCubes_2
) {
316 SetTestName("bsp_tree_cubes_2");
317 SetNumberOfDuplicates(2);
318 ReadTestFile("layer_sort_cubes");
322 TEST_F(BspTreePerfTest
, BspTreeCubes_4
) {
323 SetTestName("bsp_tree_cubes_4");
324 SetNumberOfDuplicates(4);
325 ReadTestFile("layer_sort_cubes");