[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 1 | // 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] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 9 | #include "base/path_service.h" |
| 10 | #include "base/string_piece.h" |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 11 | #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] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 15 | #include "cc/test/layer_tree_json_parser.h" |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 16 | #include "cc/test/layer_tree_test_common.h" |
[email protected] | 1b901a3 | 2012-12-07 08:52:03 | [diff] [blame] | 17 | #include "cc/test/paths.h" |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 18 | |
| 19 | namespace cc { |
| 20 | namespace { |
| 21 | |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 22 | static const int kTimeLimitMillis = 2000; |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 23 | static const int kWarmupRuns = 5; |
| 24 | static const int kTimeCheckInterval = 10; |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 25 | |
[email protected] | a46f3293 | 2012-12-07 21:43:16 | [diff] [blame] | 26 | class LayerTreeHostPerfTest : public ThreadedTest { |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 27 | public: |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 28 | LayerTreeHostPerfTest() |
| 29 | : num_draws_(0) { |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 30 | fake_content_layer_client_.setPaintAllOpaque(true); |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 33 | virtual void beginTest() OVERRIDE { |
| 34 | buildTree(); |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 35 | postSetNeedsCommitToMainThread(); |
| 36 | } |
| 37 | |
| 38 | virtual void drawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 39 | ++num_draws_; |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 40 | 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] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | virtual void buildTree() {} |
| 55 | |
| 56 | virtual void afterTest() OVERRIDE { |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 57 | // 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] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 60 | num_draws_ / elapsed_.InSecondsF()); |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 63 | protected: |
| 64 | base::TimeTicks start_time_; |
| 65 | int num_draws_; |
| 66 | std::string test_name_; |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 67 | base::TimeDelta elapsed_; |
| 68 | FakeContentLayerClient fake_content_layer_client_; |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 69 | }; |
| 70 | |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 71 | |
| 72 | class LayerTreeHostPerfTestJsonReader : public LayerTreeHostPerfTest { |
| 73 | public: |
| 74 | LayerTreeHostPerfTestJsonReader() |
| 75 | : LayerTreeHostPerfTest() { |
| 76 | } |
| 77 | |
| 78 | void readTestFile(std::string name) { |
| 79 | test_name_ = name; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 80 | base::FilePath test_data_dir; |
[email protected] | 009dc07 | 2012-12-16 20:32:13 | [diff] [blame] | 81 | ASSERT_TRUE(PathService::Get(cc::DIR_TEST_DATA, &test_data_dir)); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 82 | base::FilePath json_file = test_data_dir.AppendASCII(name + ".json"); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 83 | ASSERT_TRUE(file_util::ReadFileToString(json_file, &json_)); |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | virtual void buildTree() OVERRIDE { |
| 87 | gfx::Size viewport = gfx::Size(720, 1038); |
| 88 | m_layerTreeHost->setViewportSize(viewport, viewport); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 89 | scoped_refptr<Layer> root = ParseTreeFromJson(json_, |
| 90 | &fake_content_layer_client_); |
| 91 | ASSERT_TRUE(root.get()); |
| 92 | m_layerTreeHost->setRootLayer(root); |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 96 | std::string json_; |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 97 | }; |
| 98 | |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 99 | // Simulates a tab switcher scene with two stacks of 10 tabs each. |
| 100 | TEST_F(LayerTreeHostPerfTestJsonReader, TenTenSingleThread) { |
[email protected] | 91cd748 | 2012-11-30 05:51:45 | [diff] [blame] | 101 | readTestFile("10_10_layer_tree"); |
| 102 | runTest(false); |
| 103 | } |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 104 | |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 105 | // Simulates main-thread scrolling on each frame. |
| 106 | class 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 | |
| 127 | TEST_F(ScrollingLayerTreePerfTest, LongScrollablePage) { |
| 128 | readTestFile("long_scrollable_page"); |
| 129 | runTest(false); |
| 130 | } |
| 131 | |
[email protected] | ed9b489a | 2012-11-28 22:01:40 | [diff] [blame] | 132 | } // namespace |
| 133 | } // namespace cc |