blob: 7682593bed6c0d60eb484eaa548d334ae01226df [file] [log] [blame]
[email protected]ed9b489a2012-11-28 22:01:401// Copyright 2012 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.
4
5#include "cc/layer_tree_host.h"
6
[email protected]91cd7482012-11-30 05:51:457#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
[email protected]91cd7482012-11-30 05:51:459#include "base/path_service.h"
10#include "base/string_piece.h"
[email protected]ed9b489a2012-11-28 22:01:4011#include "cc/content_layer.h"
12#include "cc/nine_patch_layer.h"
13#include "cc/solid_color_layer.h"
14#include "cc/test/fake_content_layer_client.h"
[email protected]f8fef2bd2013-02-04 23:39:2215#include "cc/test/layer_tree_json_parser.h"
[email protected]ed9b489a2012-11-28 22:01:4016#include "cc/test/layer_tree_test_common.h"
[email protected]1b901a32012-12-07 08:52:0317#include "cc/test/paths.h"
[email protected]ed9b489a2012-11-28 22:01:4018
19namespace cc {
20namespace {
21
[email protected]91cd7482012-11-30 05:51:4522static const int kTimeLimitMillis = 2000;
[email protected]f8fef2bd2013-02-04 23:39:2223static const int kWarmupRuns = 5;
24static const int kTimeCheckInterval = 10;
[email protected]ed9b489a2012-11-28 22:01:4025
[email protected]a46f32932012-12-07 21:43:1626class LayerTreeHostPerfTest : public ThreadedTest {
[email protected]ed9b489a2012-11-28 22:01:4027 public:
[email protected]91cd7482012-11-30 05:51:4528 LayerTreeHostPerfTest()
29 : num_draws_(0) {
[email protected]f8fef2bd2013-02-04 23:39:2230 fake_content_layer_client_.setPaintAllOpaque(true);
[email protected]ed9b489a2012-11-28 22:01:4031 }
32
[email protected]91cd7482012-11-30 05:51:4533 virtual void beginTest() OVERRIDE {
34 buildTree();
[email protected]91cd7482012-11-30 05:51:4535 postSetNeedsCommitToMainThread();
36 }
37
38 virtual void drawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
39 ++num_draws_;
[email protected]f8fef2bd2013-02-04 23:39:2240 if (num_draws_ == kWarmupRuns)
41 start_time_ = base::TimeTicks::HighResNow();
42
43 if (!start_time_.is_null() && (num_draws_ % kTimeCheckInterval) == 0) {
44 base::TimeDelta elapsed = base::TimeTicks::HighResNow() - start_time_;
45 if (elapsed >= base::TimeDelta::FromMilliseconds(kTimeLimitMillis)) {
46 elapsed_ = elapsed;
47 endTest();
48 return;
49 }
50 }
51 impl->setNeedsRedraw();
[email protected]91cd7482012-11-30 05:51:4552 }
53
54 virtual void buildTree() {}
55
56 virtual void afterTest() OVERRIDE {
[email protected]91cd7482012-11-30 05:51:4557 // Format matches chrome/test/perf/perf_test.h:PrintResult
58 printf("*RESULT %s: frames= %.2f runs/s\n",
59 test_name_.c_str(),
[email protected]f8fef2bd2013-02-04 23:39:2260 num_draws_ / elapsed_.InSecondsF());
[email protected]ed9b489a2012-11-28 22:01:4061 }
62
[email protected]91cd7482012-11-30 05:51:4563 protected:
64 base::TimeTicks start_time_;
65 int num_draws_;
66 std::string test_name_;
[email protected]f8fef2bd2013-02-04 23:39:2267 base::TimeDelta elapsed_;
68 FakeContentLayerClient fake_content_layer_client_;
[email protected]91cd7482012-11-30 05:51:4569};
70
[email protected]91cd7482012-11-30 05:51:4571
72class LayerTreeHostPerfTestJsonReader : public LayerTreeHostPerfTest {
73 public:
74 LayerTreeHostPerfTestJsonReader()
75 : LayerTreeHostPerfTest() {
76 }
77
78 void readTestFile(std::string name) {
79 test_name_ = name;
[email protected]6d4b67a2013-02-10 04:49:3080 base::FilePath test_data_dir;
[email protected]009dc072012-12-16 20:32:1381 ASSERT_TRUE(PathService::Get(cc::DIR_TEST_DATA, &test_data_dir));
[email protected]6d4b67a2013-02-10 04:49:3082 base::FilePath json_file = test_data_dir.AppendASCII(name + ".json");
[email protected]f8fef2bd2013-02-04 23:39:2283 ASSERT_TRUE(file_util::ReadFileToString(json_file, &json_));
[email protected]91cd7482012-11-30 05:51:4584 }
85
86 virtual void buildTree() OVERRIDE {
87 gfx::Size viewport = gfx::Size(720, 1038);
88 m_layerTreeHost->setViewportSize(viewport, viewport);
[email protected]f8fef2bd2013-02-04 23:39:2289 scoped_refptr<Layer> root = ParseTreeFromJson(json_,
90 &fake_content_layer_client_);
91 ASSERT_TRUE(root.get());
92 m_layerTreeHost->setRootLayer(root);
[email protected]91cd7482012-11-30 05:51:4593 }
94
95 private:
[email protected]f8fef2bd2013-02-04 23:39:2296 std::string json_;
[email protected]91cd7482012-11-30 05:51:4597};
98
[email protected]f8fef2bd2013-02-04 23:39:2299// Simulates a tab switcher scene with two stacks of 10 tabs each.
100TEST_F(LayerTreeHostPerfTestJsonReader, TenTenSingleThread) {
[email protected]91cd7482012-11-30 05:51:45101 readTestFile("10_10_layer_tree");
102 runTest(false);
103}
[email protected]ed9b489a2012-11-28 22:01:40104
[email protected]f8fef2bd2013-02-04 23:39:22105// Simulates main-thread scrolling on each frame.
106class ScrollingLayerTreePerfTest : public LayerTreeHostPerfTestJsonReader {
107 public:
108 ScrollingLayerTreePerfTest()
109 : LayerTreeHostPerfTestJsonReader() {
110 }
111
112 virtual void buildTree() OVERRIDE {
113 LayerTreeHostPerfTestJsonReader::buildTree();
114 m_scrollable = m_layerTreeHost->rootLayer()->children()[1];
115 ASSERT_TRUE(m_scrollable);
116 }
117
118 virtual void layout() OVERRIDE {
119 static const gfx::Vector2d delta = gfx::Vector2d(0, 10);
120 m_scrollable->setScrollOffset(m_scrollable->scrollOffset() + delta);
121 }
122
123 private:
124 scoped_refptr<Layer> m_scrollable;
125};
126
127TEST_F(ScrollingLayerTreePerfTest, LongScrollablePage) {
128 readTestFile("long_scrollable_page");
129 runTest(false);
130}
131
[email protected]ed9b489a2012-11-28 22:01:40132} // namespace
133} // namespace cc