ServiceWorker: Introduce SWMessageSender in order to mock IPC messaging
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_perftest.cc
blob1732e5588981f89d175935ae9d984b6cd5c05ad1
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 CalcDrawPropsMainTest : public LayerTreeHostCommonPerfTest {
81 public:
82 void RunCalcDrawProps() { RunTest(false, false, false); }
84 void BeginTest() override {
85 timer_.Reset();
87 do {
88 bool can_render_to_separate_surface = true;
89 int max_texture_size = 8096;
90 RenderSurfaceLayerList update_list;
91 LayerTreeHostCommon::CalcDrawPropsMainInputs inputs(
92 layer_tree_host()->root_layer(),
93 layer_tree_host()->device_viewport_size(), gfx::Transform(),
94 layer_tree_host()->device_scale_factor(),
95 layer_tree_host()->page_scale_factor(),
96 layer_tree_host()->overscroll_elasticity_layer(),
97 layer_tree_host()->elastic_overscroll(),
98 layer_tree_host()->page_scale_layer(), max_texture_size,
99 layer_tree_host()->settings().can_use_lcd_text,
100 layer_tree_host()->settings().layers_always_allowed_lcd_text,
101 can_render_to_separate_surface,
102 layer_tree_host()
103 ->settings()
104 .layer_transforms_should_scale_layer_contents,
105 layer_tree_host()->settings().verify_property_trees,
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->current_page_scale_factor(),
151 active_tree->InnerViewportContainerLayer(),
152 active_tree->elastic_overscroll()->Current(active_tree->IsActiveTree()),
153 active_tree->overscroll_elasticity_layer(), max_texture_size,
154 host_impl->settings().can_use_lcd_text,
155 host_impl->settings().layers_always_allowed_lcd_text,
156 can_render_to_separate_surface,
157 host_impl->settings().layer_transforms_should_scale_layer_contents,
158 host_impl->settings().verify_property_trees,
159 &update_list, 0);
160 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
164 class BspTreePerfTest : public CalcDrawPropsImplTest {
165 public:
166 void RunSortLayers() { RunTest(false, false, false); }
168 void SetNumberOfDuplicates(int num_duplicates) {
169 num_duplicates_ = num_duplicates;
172 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
174 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
175 LayerTreeImpl* active_tree = host_impl->active_tree();
176 // First build the tree and then we'll start running tests on layersorter
177 // itself
178 bool can_render_to_separate_surface = true;
179 int max_texture_size = 8096;
180 DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
181 max_texture_size,
182 active_tree,
183 host_impl);
185 LayerImplList base_list;
186 BuildLayerImplList(active_tree->root_layer(), &base_list);
188 int polygon_counter = 0;
189 ScopedPtrVector<DrawPolygon> polygon_list;
190 for (LayerImplList::iterator it = base_list.begin(); it != base_list.end();
191 ++it) {
192 DrawPolygon* draw_polygon =
193 new DrawPolygon(NULL,
194 gfx::RectF((*it)->content_bounds()),
195 (*it)->draw_transform(),
196 polygon_counter++);
197 polygon_list.push_back(scoped_ptr<DrawPolygon>(draw_polygon));
200 timer_.Reset();
201 do {
202 ScopedPtrDeque<DrawPolygon> test_list;
203 for (int i = 0; i < num_duplicates_; i++) {
204 for (size_t i = 0; i < polygon_list.size(); i++) {
205 test_list.push_back(polygon_list[i]->CreateCopy());
208 BspTree bsp_tree(&test_list);
209 timer_.NextLap();
210 } while (!timer_.HasTimeLimitExpired());
212 EndTest();
215 void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) {
216 if (layer->Is3dSorted()) {
217 list->push_back(layer);
220 for (size_t i = 0; i < layer->children().size(); i++) {
221 BuildLayerImplList(layer->children()[i], list);
225 private:
226 LayerImplList base_list_;
227 int num_duplicates_;
230 TEST_F(CalcDrawPropsMainTest, TenTen) {
231 SetTestName("10_10_main_thread");
232 ReadTestFile("10_10_layer_tree");
233 RunCalcDrawProps();
236 TEST_F(CalcDrawPropsMainTest, HeavyPage) {
237 SetTestName("heavy_page_main_thread");
238 ReadTestFile("heavy_layer_tree");
239 RunCalcDrawProps();
242 TEST_F(CalcDrawPropsMainTest, TouchRegionLight) {
243 SetTestName("touch_region_light_main_thread");
244 ReadTestFile("touch_region_light");
245 RunCalcDrawProps();
248 TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) {
249 SetTestName("touch_region_heavy_main_thread");
250 ReadTestFile("touch_region_heavy");
251 RunCalcDrawProps();
254 TEST_F(CalcDrawPropsImplTest, TenTen) {
255 SetTestName("10_10");
256 ReadTestFile("10_10_layer_tree");
257 RunCalcDrawProps();
260 TEST_F(CalcDrawPropsImplTest, HeavyPage) {
261 SetTestName("heavy_page");
262 ReadTestFile("heavy_layer_tree");
263 RunCalcDrawProps();
266 TEST_F(CalcDrawPropsImplTest, TouchRegionLight) {
267 SetTestName("touch_region_light");
268 ReadTestFile("touch_region_light");
269 RunCalcDrawProps();
272 TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) {
273 SetTestName("touch_region_heavy");
274 ReadTestFile("touch_region_heavy");
275 RunCalcDrawProps();
278 TEST_F(BspTreePerfTest, LayerSorterCubes) {
279 SetTestName("layer_sort_cubes");
280 ReadTestFile("layer_sort_cubes");
281 RunSortLayers();
284 TEST_F(BspTreePerfTest, LayerSorterRubik) {
285 SetTestName("layer_sort_rubik");
286 ReadTestFile("layer_sort_rubik");
287 // TODO(vollick): Remove verify_property_trees setting after
288 // crbug.com/444219 is fixed.
289 bool old_verify_property_trees = verify_property_trees();
290 set_verify_property_trees(false);
291 RunSortLayers();
292 set_verify_property_trees(old_verify_property_trees);
295 TEST_F(BspTreePerfTest, BspTreeCubes) {
296 SetTestName("bsp_tree_cubes");
297 SetNumberOfDuplicates(1);
298 ReadTestFile("layer_sort_cubes");
299 RunSortLayers();
302 TEST_F(BspTreePerfTest, BspTreeRubik) {
303 SetTestName("bsp_tree_rubik");
304 SetNumberOfDuplicates(1);
305 ReadTestFile("layer_sort_rubik");
306 // TODO(vollick): Remove verify_property_trees setting after
307 // crbug.com/444219 is fixed.
308 bool old_verify_property_trees = verify_property_trees();
309 set_verify_property_trees(false);
310 RunSortLayers();
311 set_verify_property_trees(old_verify_property_trees);
314 TEST_F(BspTreePerfTest, BspTreeCubes_2) {
315 SetTestName("bsp_tree_cubes_2");
316 SetNumberOfDuplicates(2);
317 ReadTestFile("layer_sort_cubes");
318 RunSortLayers();
321 TEST_F(BspTreePerfTest, BspTreeCubes_4) {
322 SetTestName("bsp_tree_cubes_4");
323 SetNumberOfDuplicates(4);
324 ReadTestFile("layer_sort_cubes");
325 RunSortLayers();
328 } // namespace
329 } // namespace cc