[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 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 90a0908 | 2014-09-10 07:59:46 | [diff] [blame] | 10 | #include "base/files/file_util.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 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 53 | void SetupTree() override { |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 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 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 64 | void AfterTest() override { |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 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 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 87 | void BeginTest() override { |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 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 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 124 | void BeginTest() override { PostSetNeedsCommitToMainThread(); } |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 125 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 126 | void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override { |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 127 | timer_.Reset(); |
| 128 | LayerTreeImpl* active_tree = host_impl->active_tree(); |
| 129 | |
| 130 | do { |
| 131 | bool can_render_to_separate_surface = true; |
| 132 | int max_texture_size = 8096; |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 133 | DoCalcDrawPropertiesImpl(can_render_to_separate_surface, |
| 134 | max_texture_size, |
| 135 | active_tree, |
| 136 | host_impl); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 137 | |
| 138 | timer_.NextLap(); |
| 139 | } while (!timer_.HasTimeLimitExpired()); |
| 140 | |
| 141 | EndTest(); |
| 142 | } |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 143 | |
| 144 | void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface, |
| 145 | int max_texture_size, |
| 146 | LayerTreeImpl* active_tree, |
| 147 | LayerTreeHostImpl* host_impl) { |
| 148 | LayerImplList update_list; |
| 149 | LayerTreeHostCommon::CalcDrawPropsImplInputs inputs( |
| 150 | active_tree->root_layer(), |
| 151 | active_tree->DrawViewportSize(), |
| 152 | host_impl->DrawTransform(), |
| 153 | active_tree->device_scale_factor(), |
| 154 | active_tree->total_page_scale_factor(), |
| 155 | active_tree->InnerViewportContainerLayer(), |
| 156 | max_texture_size, |
| 157 | host_impl->settings().can_use_lcd_text, |
| 158 | can_render_to_separate_surface, |
| 159 | host_impl->settings().layer_transforms_should_scale_layer_contents, |
| 160 | &update_list, |
| 161 | 0); |
| 162 | LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | class LayerSorterMainTest : public CalcDrawPropsImplTest { |
| 167 | public: |
| 168 | void RunSortLayers() { RunTest(false, false, false); } |
| 169 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 170 | void BeginTest() override { PostSetNeedsCommitToMainThread(); } |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 171 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 172 | void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override { |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 173 | LayerTreeImpl* active_tree = host_impl->active_tree(); |
| 174 | // First build the tree and then we'll start running tests on layersorter |
| 175 | // itself |
| 176 | bool can_render_to_separate_surface = true; |
| 177 | int max_texture_size = 8096; |
| 178 | DoCalcDrawPropertiesImpl(can_render_to_separate_surface, |
| 179 | max_texture_size, |
| 180 | active_tree, |
| 181 | host_impl); |
| 182 | |
| 183 | // Behaviour of this test is different from that of sorting in practice. |
| 184 | // In this case, all layers that exist in any 3D context are put into a list |
| 185 | // and are sorted as one big 3D context instead of several smaller ones. |
| 186 | BuildLayerImplList(active_tree->root_layer(), &base_list_); |
| 187 | timer_.Reset(); |
| 188 | do { |
| 189 | // Here we'll move the layers into a LayerImpl list of their own to be |
| 190 | // sorted so we don't have a sorted list for every run after the first |
| 191 | LayerImplList test_list = base_list_; |
| 192 | layer_sorter_.Sort(test_list.begin(), test_list.end()); |
| 193 | timer_.NextLap(); |
| 194 | } while (!timer_.HasTimeLimitExpired()); |
| 195 | |
| 196 | EndTest(); |
| 197 | } |
| 198 | |
| 199 | void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) { |
[email protected] | a9d4d4f | 2014-06-19 06:49:28 | [diff] [blame] | 200 | if (layer->Is3dSorted()) { |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 201 | list->push_back(layer); |
| 202 | } |
| 203 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame] | 204 | for (size_t i = 0; i < layer->children().size(); i++) { |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 205 | BuildLayerImplList(layer->children()[i], list); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private: |
| 210 | LayerImplList base_list_; |
| 211 | LayerSorter layer_sorter_; |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 212 | }; |
| 213 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame] | 214 | class BspTreePerfTest : public LayerSorterMainTest { |
| 215 | public: |
| 216 | void RunSortLayers() { RunTest(false, false, false); } |
| 217 | |
| 218 | void SetNumberOfDuplicates(int num_duplicates) { |
| 219 | num_duplicates_ = num_duplicates; |
| 220 | } |
| 221 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 222 | void BeginTest() override { PostSetNeedsCommitToMainThread(); } |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame] | 223 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame^] | 224 | void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override { |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame] | 225 | LayerTreeImpl* active_tree = host_impl->active_tree(); |
| 226 | // First build the tree and then we'll start running tests on layersorter |
| 227 | // itself |
| 228 | bool can_render_to_separate_surface = true; |
| 229 | int max_texture_size = 8096; |
| 230 | DoCalcDrawPropertiesImpl(can_render_to_separate_surface, |
| 231 | max_texture_size, |
| 232 | active_tree, |
| 233 | host_impl); |
| 234 | |
| 235 | LayerImplList base_list; |
| 236 | BuildLayerImplList(active_tree->root_layer(), &base_list); |
| 237 | |
| 238 | int polygon_counter = 0; |
| 239 | ScopedPtrVector<DrawPolygon> polygon_list; |
| 240 | for (LayerImplList::iterator it = base_list.begin(); it != base_list.end(); |
| 241 | ++it) { |
| 242 | DrawPolygon* draw_polygon = |
| 243 | new DrawPolygon(NULL, |
| 244 | gfx::RectF((*it)->content_bounds()), |
| 245 | (*it)->draw_transform(), |
| 246 | polygon_counter++); |
| 247 | polygon_list.push_back(scoped_ptr<DrawPolygon>(draw_polygon)); |
| 248 | } |
| 249 | |
| 250 | timer_.Reset(); |
| 251 | do { |
| 252 | ScopedPtrDeque<DrawPolygon> test_list; |
| 253 | for (int i = 0; i < num_duplicates_; i++) { |
| 254 | for (size_t i = 0; i < polygon_list.size(); i++) { |
| 255 | test_list.push_back(polygon_list[i]->CreateCopy()); |
| 256 | } |
| 257 | } |
| 258 | BspTree bsp_tree(&test_list); |
| 259 | timer_.NextLap(); |
| 260 | } while (!timer_.HasTimeLimitExpired()); |
| 261 | |
| 262 | EndTest(); |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | int num_duplicates_; |
| 267 | }; |
| 268 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 269 | TEST_F(CalcDrawPropsMainTest, TenTen) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 270 | SetTestName("10_10_main_thread"); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 271 | ReadTestFile("10_10_layer_tree"); |
| 272 | RunCalcDrawProps(); |
| 273 | } |
| 274 | |
| 275 | TEST_F(CalcDrawPropsMainTest, HeavyPage) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 276 | SetTestName("heavy_page_main_thread"); |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 277 | ReadTestFile("heavy_layer_tree"); |
| 278 | RunCalcDrawProps(); |
| 279 | } |
| 280 | |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 281 | TEST_F(CalcDrawPropsMainTest, TouchRegionLight) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 282 | SetTestName("touch_region_light_main_thread"); |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 283 | ReadTestFile("touch_region_light"); |
| 284 | RunCalcDrawProps(); |
| 285 | } |
| 286 | |
| 287 | TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) { |
[email protected] | 27efb2b | 2014-02-10 19:27:16 | [diff] [blame] | 288 | SetTestName("touch_region_heavy_main_thread"); |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 289 | ReadTestFile("touch_region_heavy"); |
| 290 | RunCalcDrawProps(); |
| 291 | } |
| 292 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 293 | TEST_F(CalcDrawPropsImplTest, TenTen) { |
| 294 | SetTestName("10_10"); |
| 295 | ReadTestFile("10_10_layer_tree"); |
| 296 | RunCalcDrawProps(); |
| 297 | } |
| 298 | |
| 299 | TEST_F(CalcDrawPropsImplTest, HeavyPage) { |
| 300 | SetTestName("heavy_page"); |
| 301 | ReadTestFile("heavy_layer_tree"); |
| 302 | RunCalcDrawProps(); |
| 303 | } |
| 304 | |
[email protected] | 9d161d2 | 2013-10-29 20:54:10 | [diff] [blame] | 305 | TEST_F(CalcDrawPropsImplTest, TouchRegionLight) { |
| 306 | SetTestName("touch_region_light"); |
| 307 | ReadTestFile("touch_region_light"); |
| 308 | RunCalcDrawProps(); |
| 309 | } |
| 310 | |
| 311 | TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) { |
| 312 | SetTestName("touch_region_heavy"); |
| 313 | ReadTestFile("touch_region_heavy"); |
| 314 | RunCalcDrawProps(); |
| 315 | } |
| 316 | |
[email protected] | bebb1a8 | 2014-06-06 05:03:54 | [diff] [blame] | 317 | TEST_F(LayerSorterMainTest, LayerSorterCubes) { |
| 318 | SetTestName("layer_sort_cubes"); |
| 319 | ReadTestFile("layer_sort_cubes"); |
| 320 | RunSortLayers(); |
| 321 | } |
| 322 | |
| 323 | TEST_F(LayerSorterMainTest, LayerSorterRubik) { |
| 324 | SetTestName("layer_sort_rubik"); |
| 325 | ReadTestFile("layer_sort_rubik"); |
| 326 | RunSortLayers(); |
| 327 | } |
| 328 | |
[email protected] | 2a021fb7b | 2014-08-02 07:53:49 | [diff] [blame] | 329 | TEST_F(BspTreePerfTest, BspTreeCubes) { |
| 330 | SetTestName("bsp_tree_cubes"); |
| 331 | SetNumberOfDuplicates(1); |
| 332 | ReadTestFile("layer_sort_cubes"); |
| 333 | RunSortLayers(); |
| 334 | } |
| 335 | |
| 336 | TEST_F(BspTreePerfTest, BspTreeRubik) { |
| 337 | SetTestName("bsp_tree_rubik"); |
| 338 | SetNumberOfDuplicates(1); |
| 339 | ReadTestFile("layer_sort_rubik"); |
| 340 | RunSortLayers(); |
| 341 | } |
| 342 | |
| 343 | TEST_F(BspTreePerfTest, BspTreeCubes_2) { |
| 344 | SetTestName("bsp_tree_cubes_2"); |
| 345 | SetNumberOfDuplicates(2); |
| 346 | ReadTestFile("layer_sort_cubes"); |
| 347 | RunSortLayers(); |
| 348 | } |
| 349 | |
| 350 | TEST_F(BspTreePerfTest, BspTreeCubes_4) { |
| 351 | SetTestName("bsp_tree_cubes_4"); |
| 352 | SetNumberOfDuplicates(4); |
| 353 | ReadTestFile("layer_sort_cubes"); |
| 354 | RunSortLayers(); |
| 355 | } |
| 356 | |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 357 | } // namespace |
| 358 | } // namespace cc |