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/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_piece.h"
13 #include "base/threading/thread.h"
14 #include "base/time/time.h"
15 #include "cc/debug/lap_timer.h"
16 #include "cc/layers/layer.h"
17 #include "cc/test/fake_content_layer_client.h"
18 #include "cc/test/fake_layer_tree_host_client.h"
19 #include "cc/test/layer_tree_json_parser.h"
20 #include "cc/test/layer_tree_test.h"
21 #include "cc/test/paths.h"
22 #include "cc/trees/layer_sorter.h"
23 #include "cc/trees/layer_tree_impl.h"
24 #include "testing/perf/perf_test.h"
29 static const int kTimeLimitMillis
= 2000;
30 static const int kWarmupRuns
= 5;
31 static const int kTimeCheckInterval
= 10;
33 class LayerTreeHostCommonPerfTest
: public LayerTreeTest
{
35 LayerTreeHostCommonPerfTest()
37 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
38 kTimeCheckInterval
) {}
40 void ReadTestFile(const std::string
& name
) {
41 base::FilePath test_data_dir
;
42 ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA
, &test_data_dir
));
43 base::FilePath json_file
= test_data_dir
.AppendASCII(name
+ ".json");
44 ASSERT_TRUE(base::ReadFileToString(json_file
, &json_
));
47 virtual void SetupTree() OVERRIDE
{
48 gfx::Size viewport
= gfx::Size(720, 1038);
49 layer_tree_host()->SetViewportSize(viewport
);
50 scoped_refptr
<Layer
> root
=
51 ParseTreeFromJson(json_
, &content_layer_client_
);
52 ASSERT_TRUE(root
.get());
53 layer_tree_host()->SetRootLayer(root
);
56 void SetTestName(const std::string
& name
) { test_name_
= name
; }
58 virtual void AfterTest() OVERRIDE
{
59 CHECK(!test_name_
.empty()) << "Must SetTestName() before TearDown().";
60 perf_test::PrintResult("calc_draw_props_time",
63 1000 * timer_
.MsPerLap(),
69 FakeContentLayerClient content_layer_client_
;
71 std::string test_name_
;
75 class CalcDrawPropsMainTest
: public LayerTreeHostCommonPerfTest
{
77 void RunCalcDrawProps() {
78 RunTest(false, false, false);
81 virtual void BeginTest() OVERRIDE
{
85 bool can_render_to_separate_surface
= true;
86 int max_texture_size
= 8096;
87 RenderSurfaceLayerList update_list
;
88 LayerTreeHostCommon::CalcDrawPropsMainInputs
inputs(
89 layer_tree_host()->root_layer(),
90 layer_tree_host()->device_viewport_size(),
92 layer_tree_host()->device_scale_factor(),
93 layer_tree_host()->page_scale_factor(),
94 layer_tree_host()->page_scale_layer(),
96 layer_tree_host()->settings().can_use_lcd_text
,
97 can_render_to_separate_surface
,
100 .layer_transforms_should_scale_layer_contents
,
103 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
106 } while (!timer_
.HasTimeLimitExpired());
112 class CalcDrawPropsImplTest
: public LayerTreeHostCommonPerfTest
{
114 void RunCalcDrawProps() {
115 RunTestWithImplSidePainting();
118 virtual void BeginTest() OVERRIDE
{
119 PostSetNeedsCommitToMainThread();
122 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) OVERRIDE
{
124 LayerTreeImpl
* active_tree
= host_impl
->active_tree();
127 bool can_render_to_separate_surface
= true;
128 int max_texture_size
= 8096;
129 DoCalcDrawPropertiesImpl(can_render_to_separate_surface
,
135 } while (!timer_
.HasTimeLimitExpired());
140 void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface
,
141 int max_texture_size
,
142 LayerTreeImpl
* active_tree
,
143 LayerTreeHostImpl
* host_impl
) {
144 LayerImplList update_list
;
145 LayerTreeHostCommon::CalcDrawPropsImplInputs
inputs(
146 active_tree
->root_layer(),
147 active_tree
->DrawViewportSize(),
148 host_impl
->DrawTransform(),
149 active_tree
->device_scale_factor(),
150 active_tree
->total_page_scale_factor(),
151 active_tree
->InnerViewportContainerLayer(),
153 host_impl
->settings().can_use_lcd_text
,
154 can_render_to_separate_surface
,
155 host_impl
->settings().layer_transforms_should_scale_layer_contents
,
158 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
162 class LayerSorterMainTest
: public CalcDrawPropsImplTest
{
164 void RunSortLayers() { RunTest(false, false, false); }
166 virtual void BeginTest() OVERRIDE
{ PostSetNeedsCommitToMainThread(); }
168 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) OVERRIDE
{
169 LayerTreeImpl
* active_tree
= host_impl
->active_tree();
170 // First build the tree and then we'll start running tests on layersorter
172 bool can_render_to_separate_surface
= true;
173 int max_texture_size
= 8096;
174 DoCalcDrawPropertiesImpl(can_render_to_separate_surface
,
179 // Behaviour of this test is different from that of sorting in practice.
180 // In this case, all layers that exist in any 3D context are put into a list
181 // and are sorted as one big 3D context instead of several smaller ones.
182 BuildLayerImplList(active_tree
->root_layer(), &base_list_
);
185 // Here we'll move the layers into a LayerImpl list of their own to be
186 // sorted so we don't have a sorted list for every run after the first
187 LayerImplList test_list
= base_list_
;
188 layer_sorter_
.Sort(test_list
.begin(), test_list
.end());
190 } while (!timer_
.HasTimeLimitExpired());
195 void BuildLayerImplList(LayerImpl
* layer
, LayerImplList
* list
) {
196 if (layer
->Is3dSorted()) {
197 list
->push_back(layer
);
200 for (unsigned int i
= 0; i
< layer
->children().size(); i
++) {
201 BuildLayerImplList(layer
->children()[i
], list
);
206 LayerImplList base_list_
;
207 LayerSorter layer_sorter_
;
210 TEST_F(CalcDrawPropsMainTest
, TenTen
) {
211 SetTestName("10_10_main_thread");
212 ReadTestFile("10_10_layer_tree");
216 TEST_F(CalcDrawPropsMainTest
, HeavyPage
) {
217 SetTestName("heavy_page_main_thread");
218 ReadTestFile("heavy_layer_tree");
222 TEST_F(CalcDrawPropsMainTest
, TouchRegionLight
) {
223 SetTestName("touch_region_light_main_thread");
224 ReadTestFile("touch_region_light");
228 TEST_F(CalcDrawPropsMainTest
, TouchRegionHeavy
) {
229 SetTestName("touch_region_heavy_main_thread");
230 ReadTestFile("touch_region_heavy");
234 TEST_F(CalcDrawPropsImplTest
, TenTen
) {
235 SetTestName("10_10");
236 ReadTestFile("10_10_layer_tree");
240 TEST_F(CalcDrawPropsImplTest
, HeavyPage
) {
241 SetTestName("heavy_page");
242 ReadTestFile("heavy_layer_tree");
246 TEST_F(CalcDrawPropsImplTest
, TouchRegionLight
) {
247 SetTestName("touch_region_light");
248 ReadTestFile("touch_region_light");
252 TEST_F(CalcDrawPropsImplTest
, TouchRegionHeavy
) {
253 SetTestName("touch_region_heavy");
254 ReadTestFile("touch_region_heavy");
258 TEST_F(LayerSorterMainTest
, LayerSorterCubes
) {
259 SetTestName("layer_sort_cubes");
260 ReadTestFile("layer_sort_cubes");
264 TEST_F(LayerSorterMainTest
, LayerSorterRubik
) {
265 SetTestName("layer_sort_rubik");
266 ReadTestFile("layer_sort_rubik");