[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 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" |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 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" |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 16 | #include "cc/base/scoped_ptr_deque.h" |
| 17 | #include "cc/base/scoped_ptr_vector.h" |
[email protected] | 29d39a4 | 2014-05-13 19:40:58 | [diff] [blame] | 18 | #include "cc/debug/lap_timer.h" |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 19 | #include "cc/layers/layer.h" |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 20 | #include "cc/output/bsp_tree.h" |
| 21 | #include "cc/quads/draw_polygon.h" |
| 22 | #include "cc/quads/draw_quad.h" |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 23 | #include "cc/test/fake_content_layer_client.h" |
| 24 | #include "cc/test/fake_layer_tree_host_client.h" |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 25 | #include "cc/test/layer_tree_json_parser.h" |
| 26 | #include "cc/test/layer_tree_test.h" |
| 27 | #include "cc/test/paths.h" |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 28 | #include "cc/trees/layer_sorter.h" |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 29 | #include "cc/trees/layer_tree_impl.h" |
| 30 | #include "testing/perf/perf_test.h" |
| 31 | |
| 32 | namespace cc { |
| 33 | namespace { |
| 34 | |
| 35 | static const int kTimeLimitMillis = 2000; |
| 36 | static const int kWarmupRuns = 5; |
| 37 | static const int kTimeCheckInterval = 10; |
| 38 | |
| 39 | class LayerTreeHostCommonPerfTest : public LayerTreeTest { |
| 40 | public: |
| 41 | LayerTreeHostCommonPerfTest() |
| 42 | : timer_(kWarmupRuns, |
| 43 | base::TimeDelta::FromMilliseconds(kTimeLimitMillis), |
| 44 | kTimeCheckInterval) {} |
| 45 | |
| 46 | void ReadTestFile(const std::string& name) { |
| 47 | base::FilePath test_data_dir; |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 48 | ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir)); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 49 | base::FilePath json_file = test_data_dir.AppendASCII(name + ".json"); |
| 50 | ASSERT_TRUE(base::ReadFileToString(json_file, &json_)); |
| 51 | } |
| 52 | |
| 53 | virtual void SetupTree() OVERRIDE { |
| 54 | gfx::Size viewport = gfx::Size(720, 1038); |
| 55 | layer_tree_host()->SetViewportSize(viewport); |
| 56 | scoped_refptr<Layer> root = |
| 57 | ParseTreeFromJson(json_, &content_layer_client_); |
| 58 | ASSERT_TRUE(root.get()); |
| 59 | layer_tree_host()->SetRootLayer(root); |
| 60 | } |
| 61 | |
| 62 | void SetTestName(const std::string& name) { test_name_ = name; } |
| 63 | |
| 64 | virtual void AfterTest() OVERRIDE { |
| 65 | CHECK(!test_name_.empty()) << "Must SetTestName() before TearDown()."; |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 66 | perf_test::PrintResult("calc_draw_props_time", |
| 67 | "", |
| 68 | test_name_, |
| 69 | 1000 * timer_.MsPerLap(), |
| 70 | "us", |
| 71 | true); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | FakeContentLayerClient content_layer_client_; |
| 76 | LapTimer timer_; |
| 77 | std::string test_name_; |
| 78 | std::string json_; |
| 79 | }; |
| 80 | |
| 81 | class CalcDrawPropsMainTest : public LayerTreeHostCommonPerfTest { |
| 82 | public: |
| 83 | void RunCalcDrawProps() { |
| 84 | RunTest(false, false, false); |
| 85 | } |
| 86 | |
| 87 | virtual void BeginTest() OVERRIDE { |
| 88 | timer_.Reset(); |
| 89 | |
| 90 | do { |
| 91 | bool can_render_to_separate_surface = true; |
| 92 | int max_texture_size = 8096; |
| 93 | RenderSurfaceLayerList update_list; |
| 94 | LayerTreeHostCommon::CalcDrawPropsMainInputs inputs( |
| 95 | layer_tree_host()->root_layer(), |
| 96 | layer_tree_host()->device_viewport_size(), |
| 97 | gfx::Transform(), |
| 98 | layer_tree_host()->device_scale_factor(), |
| 99 | layer_tree_host()->page_scale_factor(), |
| 100 | layer_tree_host()->page_scale_layer(), |
| 101 | max_texture_size, |
| 102 | layer_tree_host()->settings().can_use_lcd_text, |
| 103 | can_render_to_separate_surface, |
| 104 | layer_tree_host() |
| 105 | ->settings() |
| 106 | .layer_transforms_should_scale_layer_contents, |
[email protected] | 390bb1ff | 2014-05-09 17:14:40 | [diff] [blame] | 107 | &update_list, |
| 108 | 0); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 109 | LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 110 | |
| 111 | timer_.NextLap(); |
| 112 | } while (!timer_.HasTimeLimitExpired()); |
| 113 | |
| 114 | EndTest(); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | class CalcDrawPropsImplTest : public LayerTreeHostCommonPerfTest { |
| 119 | public: |
| 120 | void RunCalcDrawProps() { |
| 121 | RunTestWithImplSidePainting(); |
| 122 | } |
| 123 | |
| 124 | virtual void BeginTest() OVERRIDE { |
| 125 | PostSetNeedsCommitToMainThread(); |
| 126 | } |
| 127 | |
| 128 | virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { |
| 129 | timer_.Reset(); |
| 130 | LayerTreeImpl* active_tree = host_impl->active_tree(); |
| 131 | |
| 132 | do { |
| 133 | bool can_render_to_separate_surface = true; |
| 134 | int max_texture_size = 8096; |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 135 | DoCalcDrawPropertiesImpl(can_render_to_separate_surface, |
| 136 | max_texture_size, |
| 137 | active_tree, |
| 138 | host_impl); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 139 | |
| 140 | timer_.NextLap(); |
| 141 | } while (!timer_.HasTimeLimitExpired()); |
| 142 | |
| 143 | EndTest(); |
| 144 | } |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 145 | |
| 146 | void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface, |
| 147 | int max_texture_size, |
| 148 | LayerTreeImpl* active_tree, |
| 149 | LayerTreeHostImpl* host_impl) { |
| 150 | LayerImplList update_list; |
| 151 | LayerTreeHostCommon::CalcDrawPropsImplInputs inputs( |
| 152 | active_tree->root_layer(), |
| 153 | active_tree->DrawViewportSize(), |
| 154 | host_impl->DrawTransform(), |
| 155 | active_tree->device_scale_factor(), |
| 156 | active_tree->total_page_scale_factor(), |
| 157 | active_tree->InnerViewportContainerLayer(), |
| 158 | max_texture_size, |
| 159 | host_impl->settings().can_use_lcd_text, |
| 160 | can_render_to_separate_surface, |
| 161 | host_impl->settings().layer_transforms_should_scale_layer_contents, |
| 162 | &update_list, |
| 163 | 0); |
| 164 | LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | class LayerSorterMainTest : public CalcDrawPropsImplTest { |
| 169 | public: |
| 170 | void RunSortLayers() { RunTest(false, false, false); } |
| 171 | |
| 172 | virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); } |
| 173 | |
| 174 | virtual 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); |
| 184 | |
| 185 | // Behaviour of this test is different from that of sorting in practice. |
| 186 | // In this case, all layers that exist in any 3D context are put into a list |
| 187 | // and are sorted as one big 3D context instead of several smaller ones. |
| 188 | BuildLayerImplList(active_tree->root_layer(), &base_list_); |
| 189 | timer_.Reset(); |
| 190 | do { |
| 191 | // Here we'll move the layers into a LayerImpl list of their own to be |
| 192 | // sorted so we don't have a sorted list for every run after the first |
| 193 | LayerImplList test_list = base_list_; |
| 194 | layer_sorter_.Sort(test_list.begin(), test_list.end()); |
| 195 | timer_.NextLap(); |
| 196 | } while (!timer_.HasTimeLimitExpired()); |
| 197 | |
| 198 | EndTest(); |
| 199 | } |
| 200 | |
| 201 | void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) { |
[email protected] | a9d4d4f | 2014-06-19 06:49:28 | [diff] [blame] | 202 | if (layer->Is3dSorted()) { |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 203 | list->push_back(layer); |
| 204 | } |
| 205 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 206 | for (size_t i = 0; i < layer->children().size(); i++) { |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 207 | BuildLayerImplList(layer->children()[i], list); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | LayerImplList base_list_; |
| 213 | LayerSorter layer_sorter_; |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 214 | }; |
| 215 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 216 | class BspTreePerfTest : public LayerSorterMainTest { |
| 217 | public: |
| 218 | void RunSortLayers() { RunTest(false, false, false); } |
| 219 | |
| 220 | void SetNumberOfDuplicates(int num_duplicates) { |
| 221 | num_duplicates_ = num_duplicates; |
| 222 | } |
| 223 | |
| 224 | virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); } |
| 225 | |
| 226 | virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { |
| 227 | LayerTreeImpl* active_tree = host_impl->active_tree(); |
| 228 | // First build the tree and then we'll start running tests on layersorter |
| 229 | // itself |
| 230 | bool can_render_to_separate_surface = true; |
| 231 | int max_texture_size = 8096; |
| 232 | DoCalcDrawPropertiesImpl(can_render_to_separate_surface, |
| 233 | max_texture_size, |
| 234 | active_tree, |
| 235 | host_impl); |
| 236 | |
| 237 | LayerImplList base_list; |
| 238 | BuildLayerImplList(active_tree->root_layer(), &base_list); |
| 239 | |
| 240 | int polygon_counter = 0; |
| 241 | ScopedPtrVector<DrawPolygon> polygon_list; |
| 242 | for (LayerImplList::iterator it = base_list.begin(); it != base_list.end(); |
| 243 | ++it) { |
| 244 | DrawPolygon* draw_polygon = |
| 245 | new DrawPolygon(NULL, |
| 246 | gfx::RectF((*it)->content_bounds()), |
| 247 | (*it)->draw_transform(), |
| 248 | polygon_counter++); |
| 249 | polygon_list.push_back(scoped_ptr<DrawPolygon>(draw_polygon)); |
| 250 | } |
| 251 | |
| 252 | timer_.Reset(); |
| 253 | do { |
| 254 | ScopedPtrDeque<DrawPolygon> test_list; |
| 255 | for (int i = 0; i < num_duplicates_; i++) { |
| 256 | for (size_t i = 0; i < polygon_list.size(); i++) { |
| 257 | test_list.push_back(polygon_list[i]->CreateCopy()); |
| 258 | } |
| 259 | } |
| 260 | BspTree bsp_tree(&test_list); |
| 261 | timer_.NextLap(); |
| 262 | } while (!timer_.HasTimeLimitExpired()); |
| 263 | |
| 264 | EndTest(); |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | int num_duplicates_; |
| 269 | }; |
| 270 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 271 | TEST_F(CalcDrawPropsMainTest, TenTen) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 272 | SetTestName("10_10_main_thread"); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 273 | ReadTestFile("10_10_layer_tree"); |
| 274 | RunCalcDrawProps(); |
| 275 | } |
| 276 | |
| 277 | TEST_F(CalcDrawPropsMainTest, HeavyPage) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 278 | SetTestName("heavy_page_main_thread"); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 279 | ReadTestFile("heavy_layer_tree"); |
| 280 | RunCalcDrawProps(); |
| 281 | } |
| 282 | |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 283 | TEST_F(CalcDrawPropsMainTest, TouchRegionLight) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 284 | SetTestName("touch_region_light_main_thread"); |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 285 | ReadTestFile("touch_region_light"); |
| 286 | RunCalcDrawProps(); |
| 287 | } |
| 288 | |
| 289 | TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 290 | SetTestName("touch_region_heavy_main_thread"); |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 291 | ReadTestFile("touch_region_heavy"); |
| 292 | RunCalcDrawProps(); |
| 293 | } |
| 294 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 295 | TEST_F(CalcDrawPropsImplTest, TenTen) { |
| 296 | SetTestName("10_10"); |
| 297 | ReadTestFile("10_10_layer_tree"); |
| 298 | RunCalcDrawProps(); |
| 299 | } |
| 300 | |
| 301 | TEST_F(CalcDrawPropsImplTest, HeavyPage) { |
| 302 | SetTestName("heavy_page"); |
| 303 | ReadTestFile("heavy_layer_tree"); |
| 304 | RunCalcDrawProps(); |
| 305 | } |
| 306 | |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 307 | TEST_F(CalcDrawPropsImplTest, TouchRegionLight) { |
| 308 | SetTestName("touch_region_light"); |
| 309 | ReadTestFile("touch_region_light"); |
| 310 | RunCalcDrawProps(); |
| 311 | } |
| 312 | |
| 313 | TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) { |
| 314 | SetTestName("touch_region_heavy"); |
| 315 | ReadTestFile("touch_region_heavy"); |
| 316 | RunCalcDrawProps(); |
| 317 | } |
| 318 | |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 319 | TEST_F(LayerSorterMainTest, LayerSorterCubes) { |
| 320 | SetTestName("layer_sort_cubes"); |
| 321 | ReadTestFile("layer_sort_cubes"); |
| 322 | RunSortLayers(); |
| 323 | } |
| 324 | |
| 325 | TEST_F(LayerSorterMainTest, LayerSorterRubik) { |
| 326 | SetTestName("layer_sort_rubik"); |
| 327 | ReadTestFile("layer_sort_rubik"); |
| 328 | RunSortLayers(); |
| 329 | } |
| 330 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame^] | 331 | TEST_F(BspTreePerfTest, BspTreeCubes) { |
| 332 | SetTestName("bsp_tree_cubes"); |
| 333 | SetNumberOfDuplicates(1); |
| 334 | ReadTestFile("layer_sort_cubes"); |
| 335 | RunSortLayers(); |
| 336 | } |
| 337 | |
| 338 | TEST_F(BspTreePerfTest, BspTreeRubik) { |
| 339 | SetTestName("bsp_tree_rubik"); |
| 340 | SetNumberOfDuplicates(1); |
| 341 | ReadTestFile("layer_sort_rubik"); |
| 342 | RunSortLayers(); |
| 343 | } |
| 344 | |
| 345 | TEST_F(BspTreePerfTest, BspTreeCubes_2) { |
| 346 | SetTestName("bsp_tree_cubes_2"); |
| 347 | SetNumberOfDuplicates(2); |
| 348 | ReadTestFile("layer_sort_cubes"); |
| 349 | RunSortLayers(); |
| 350 | } |
| 351 | |
| 352 | TEST_F(BspTreePerfTest, BspTreeCubes_4) { |
| 353 | SetTestName("bsp_tree_cubes_4"); |
| 354 | SetNumberOfDuplicates(4); |
| 355 | ReadTestFile("layer_sort_cubes"); |
| 356 | RunSortLayers(); |
| 357 | } |
| 358 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 359 | } // namespace |
| 360 | } // namespace cc |