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. 4 5 #include "cc/trees/layer_tree_host_common.h" 6 7 #include <sstream> 8 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/layers/layer.h" 16 #include "cc/test/fake_content_layer_client.h" 17 #include "cc/test/fake_layer_tree_host_client.h" 18 #include "cc/test/lap_timer.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_tree_impl.h" 23 #include "testing/perf/perf_test.h" 24 25 namespace cc { 26 namespace { 27 28 static const int kTimeLimitMillis = 2000; 29 static const int kWarmupRuns = 5; 30 static const int kTimeCheckInterval = 10; 31 32 class LayerTreeHostCommonPerfTest : public LayerTreeTest { 33 public: 34 LayerTreeHostCommonPerfTest() 35 : timer_(kWarmupRuns, 36 base::TimeDelta::FromMilliseconds(kTimeLimitMillis), 37 kTimeCheckInterval) {} 38 39 void ReadTestFile(const std::string& name) { 40 base::FilePath test_data_dir; 41 ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir)); 42 base::FilePath json_file = test_data_dir.AppendASCII(name + ".json"); 43 ASSERT_TRUE(base::ReadFileToString(json_file, &json_)); 44 } 45 46 virtual void SetupTree() OVERRIDE { 47 gfx::Size viewport = gfx::Size(720, 1038); 48 layer_tree_host()->SetViewportSize(viewport); 49 scoped_refptr<Layer> root = 50 ParseTreeFromJson(json_, &content_layer_client_); 51 ASSERT_TRUE(root.get()); 52 layer_tree_host()->SetRootLayer(root); 53 } 54 55 void SetTestName(const std::string& name) { test_name_ = name; } 56 57 virtual void AfterTest() OVERRIDE { 58 CHECK(!test_name_.empty()) << "Must SetTestName() before TearDown()."; 59 perf_test::PrintResult("calc_draw_props_time", 60 "", 61 test_name_, 62 1000 * timer_.MsPerLap(), 63 "us", 64 true); 65 } 66 67 protected: 68 FakeContentLayerClient content_layer_client_; 69 LapTimer timer_; 70 std::string test_name_; 71 std::string json_; 72 }; 73 74 class CalcDrawPropsMainTest : public LayerTreeHostCommonPerfTest { 75 public: 76 void RunCalcDrawProps() { 77 RunTest(false, false, false); 78 } 79 80 virtual void BeginTest() OVERRIDE { 81 timer_.Reset(); 82 83 do { 84 bool can_render_to_separate_surface = true; 85 int max_texture_size = 8096; 86 RenderSurfaceLayerList update_list; 87 LayerTreeHostCommon::CalcDrawPropsMainInputs inputs( 88 layer_tree_host()->root_layer(), 89 layer_tree_host()->device_viewport_size(), 90 gfx::Transform(), 91 layer_tree_host()->device_scale_factor(), 92 layer_tree_host()->page_scale_factor(), 93 layer_tree_host()->page_scale_layer(), 94 max_texture_size, 95 layer_tree_host()->settings().can_use_lcd_text, 96 can_render_to_separate_surface, 97 layer_tree_host() 98 ->settings() 99 .layer_transforms_should_scale_layer_contents, 100 &update_list); 101 LayerTreeHostCommon::CalculateDrawProperties(&inputs); 102 103 timer_.NextLap(); 104 } while (!timer_.HasTimeLimitExpired()); 105 106 EndTest(); 107 } 108 }; 109 110 class CalcDrawPropsImplTest : public LayerTreeHostCommonPerfTest { 111 public: 112 void RunCalcDrawProps() { 113 RunTestWithImplSidePainting(); 114 } 115 116 virtual void BeginTest() OVERRIDE { 117 PostSetNeedsCommitToMainThread(); 118 } 119 120 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { 121 timer_.Reset(); 122 LayerTreeImpl* active_tree = host_impl->active_tree(); 123 124 do { 125 bool can_render_to_separate_surface = true; 126 int max_texture_size = 8096; 127 LayerImplList update_list; 128 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs( 129 active_tree->root_layer(), 130 active_tree->DrawViewportSize(), 131 host_impl->DrawTransform(), 132 active_tree->device_scale_factor(), 133 active_tree->total_page_scale_factor(), 134 active_tree->RootContainerLayer(), 135 max_texture_size, 136 host_impl->settings().can_use_lcd_text, 137 can_render_to_separate_surface, 138 host_impl->settings().layer_transforms_should_scale_layer_contents, 139 &update_list); 140 LayerTreeHostCommon::CalculateDrawProperties(&inputs); 141 142 timer_.NextLap(); 143 } while (!timer_.HasTimeLimitExpired()); 144 145 EndTest(); 146 } 147 }; 148 149 TEST_F(CalcDrawPropsMainTest, TenTen) { 150 SetTestName("10_10"); 151 ReadTestFile("10_10_layer_tree"); 152 RunCalcDrawProps(); 153 } 154 155 TEST_F(CalcDrawPropsMainTest, HeavyPage) { 156 SetTestName("heavy_page"); 157 ReadTestFile("heavy_layer_tree"); 158 RunCalcDrawProps(); 159 } 160 161 TEST_F(CalcDrawPropsMainTest, TouchRegionLight) { 162 SetTestName("touch_region_light"); 163 ReadTestFile("touch_region_light"); 164 RunCalcDrawProps(); 165 } 166 167 TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) { 168 SetTestName("touch_region_heavy"); 169 ReadTestFile("touch_region_heavy"); 170 RunCalcDrawProps(); 171 } 172 173 TEST_F(CalcDrawPropsImplTest, TenTen) { 174 SetTestName("10_10"); 175 ReadTestFile("10_10_layer_tree"); 176 RunCalcDrawProps(); 177 } 178 179 TEST_F(CalcDrawPropsImplTest, HeavyPage) { 180 SetTestName("heavy_page"); 181 ReadTestFile("heavy_layer_tree"); 182 RunCalcDrawProps(); 183 } 184 185 TEST_F(CalcDrawPropsImplTest, TouchRegionLight) { 186 SetTestName("touch_region_light"); 187 ReadTestFile("touch_region_light"); 188 RunCalcDrawProps(); 189 } 190 191 TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) { 192 SetTestName("touch_region_heavy"); 193 ReadTestFile("touch_region_heavy"); 194 RunCalcDrawProps(); 195 } 196 197 } // namespace 198 } // namespace cc 199