[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_perftest.cc
blobfbb38d1fcf7fb01ed0d000a33c42bcbbe8e2757b
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_tree_impl.h"
29 #include "testing/perf/perf_test.h"
31 namespace cc {
32 namespace {
34 static const int kTimeLimitMillis = 2000;
35 static const int kWarmupRuns = 5;
36 static const int kTimeCheckInterval = 10;
38 class LayerTreeHostCommonPerfTest : public LayerTreeTest {
39 public:
40 LayerTreeHostCommonPerfTest()
41 : timer_(kWarmupRuns,
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",
66 "",
67 test_name_,
68 1000 * timer_.MsPerLap(),
69 "us",
70 true);
73 protected:
74 FakeContentLayerClient content_layer_client_;
75 LapTimer timer_;
76 std::string test_name_;
77 std::string json_;
80 class CalcDrawPropsTest : public LayerTreeHostCommonPerfTest {
81 public:
82 void RunCalcDrawProps() { RunTest(false, false); }
84 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
86 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
87 timer_.Reset();
88 LayerTreeImpl* active_tree = host_impl->active_tree();
90 do {
91 bool can_render_to_separate_surface = true;
92 int max_texture_size = 8096;
93 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
94 max_texture_size,
95 active_tree,
96 host_impl);
98 timer_.NextLap();
99 } while (!timer_.HasTimeLimitExpired());
101 EndTest();
104 void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface,
105 int max_texture_size,
106 LayerTreeImpl* active_tree,
107 LayerTreeHostImpl* host_impl) {
108 LayerImplList update_list;
109 PropertyTrees property_trees;
110 bool verify_property_trees = false;
111 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
112 active_tree->root_layer(), active_tree->DrawViewportSize(),
113 host_impl->DrawTransform(), active_tree->device_scale_factor(),
114 active_tree->current_page_scale_factor(),
115 active_tree->InnerViewportContainerLayer(),
116 active_tree->InnerViewportScrollLayer(),
117 active_tree->OuterViewportScrollLayer(),
118 active_tree->elastic_overscroll()->Current(active_tree->IsActiveTree()),
119 active_tree->OverscrollElasticityLayer(), max_texture_size,
120 host_impl->settings().can_use_lcd_text,
121 host_impl->settings().layers_always_allowed_lcd_text,
122 can_render_to_separate_surface,
123 host_impl->settings().layer_transforms_should_scale_layer_contents,
124 verify_property_trees, &update_list, 0, &property_trees);
125 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
129 class BspTreePerfTest : public CalcDrawPropsTest {
130 public:
131 BspTreePerfTest() : num_duplicates_(1) {}
132 void RunSortLayers() { RunTest(false, false); }
134 void SetNumberOfDuplicates(int num_duplicates) {
135 num_duplicates_ = num_duplicates;
138 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
140 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
141 LayerTreeImpl* active_tree = host_impl->active_tree();
142 // First build the tree and then we'll start running tests on layersorter
143 // itself
144 bool can_render_to_separate_surface = true;
145 int max_texture_size = 8096;
146 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
147 max_texture_size,
148 active_tree,
149 host_impl);
151 LayerImplList base_list;
152 BuildLayerImplList(active_tree->root_layer(), &base_list);
154 int polygon_counter = 0;
155 ScopedPtrVector<DrawPolygon> polygon_list;
156 for (LayerImplList::iterator it = base_list.begin(); it != base_list.end();
157 ++it) {
158 DrawPolygon* draw_polygon =
159 new DrawPolygon(NULL, gfx::RectF((*it)->bounds()),
160 (*it)->draw_transform(), polygon_counter++);
161 polygon_list.push_back(scoped_ptr<DrawPolygon>(draw_polygon));
164 timer_.Reset();
165 do {
166 ScopedPtrDeque<DrawPolygon> test_list;
167 for (int i = 0; i < num_duplicates_; i++) {
168 for (size_t i = 0; i < polygon_list.size(); i++) {
169 test_list.push_back(polygon_list[i]->CreateCopy());
172 BspTree bsp_tree(&test_list);
173 timer_.NextLap();
174 } while (!timer_.HasTimeLimitExpired());
176 EndTest();
179 void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) {
180 if (layer->Is3dSorted()) {
181 list->push_back(layer);
184 for (size_t i = 0; i < layer->children().size(); i++) {
185 BuildLayerImplList(layer->children()[i], list);
189 private:
190 LayerImplList base_list_;
191 int num_duplicates_;
194 TEST_F(CalcDrawPropsTest, TenTen) {
195 SetTestName("10_10");
196 ReadTestFile("10_10_layer_tree");
197 RunCalcDrawProps();
200 TEST_F(CalcDrawPropsTest, HeavyPage) {
201 SetTestName("heavy_page");
202 ReadTestFile("heavy_layer_tree");
203 RunCalcDrawProps();
206 TEST_F(CalcDrawPropsTest, TouchRegionLight) {
207 SetTestName("touch_region_light");
208 ReadTestFile("touch_region_light");
209 RunCalcDrawProps();
212 TEST_F(CalcDrawPropsTest, TouchRegionHeavy) {
213 SetTestName("touch_region_heavy");
214 ReadTestFile("touch_region_heavy");
215 RunCalcDrawProps();
218 TEST_F(BspTreePerfTest, LayerSorterCubes) {
219 SetTestName("layer_sort_cubes");
220 ReadTestFile("layer_sort_cubes");
221 RunSortLayers();
224 TEST_F(BspTreePerfTest, LayerSorterRubik) {
225 SetTestName("layer_sort_rubik");
226 ReadTestFile("layer_sort_rubik");
227 // TODO(vollick): Remove verify_property_trees setting after
228 // crbug.com/444219 is fixed.
229 bool old_verify_property_trees = verify_property_trees();
230 set_verify_property_trees(false);
231 RunSortLayers();
232 set_verify_property_trees(old_verify_property_trees);
235 TEST_F(BspTreePerfTest, BspTreeCubes) {
236 SetTestName("bsp_tree_cubes");
237 SetNumberOfDuplicates(1);
238 ReadTestFile("layer_sort_cubes");
239 RunSortLayers();
242 TEST_F(BspTreePerfTest, BspTreeRubik) {
243 SetTestName("bsp_tree_rubik");
244 SetNumberOfDuplicates(1);
245 ReadTestFile("layer_sort_rubik");
246 // TODO(vollick): Remove verify_property_trees setting after
247 // crbug.com/444219 is fixed.
248 bool old_verify_property_trees = verify_property_trees();
249 set_verify_property_trees(false);
250 RunSortLayers();
251 set_verify_property_trees(old_verify_property_trees);
254 TEST_F(BspTreePerfTest, BspTreeCubes_2) {
255 SetTestName("bsp_tree_cubes_2");
256 SetNumberOfDuplicates(2);
257 ReadTestFile("layer_sort_cubes");
258 RunSortLayers();
261 TEST_F(BspTreePerfTest, BspTreeCubes_4) {
262 SetTestName("bsp_tree_cubes_4");
263 SetNumberOfDuplicates(4);
264 ReadTestFile("layer_sort_cubes");
265 RunSortLayers();
268 } // namespace
269 } // namespace cc