Home | History | Annotate | Download | only in trees
      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/debug/lap_timer.h"
     16 #include "cc/layers/layer.h"
     17 #include "cc/test/fake_content_layer_client.h"
     18 #include "cc/test/fake_layer_tree_host_client.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_sorter.h"
     23 #include "cc/trees/layer_tree_impl.h"
     24 #include "testing/perf/perf_test.h"
     25 
     26 namespace cc {
     27 namespace {
     28 
     29 static const int kTimeLimitMillis = 2000;
     30 static const int kWarmupRuns = 5;
     31 static const int kTimeCheckInterval = 10;
     32 
     33 class LayerTreeHostCommonPerfTest : public LayerTreeTest {
     34  public:
     35   LayerTreeHostCommonPerfTest()
     36       : timer_(kWarmupRuns,
     37                base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
     38                kTimeCheckInterval) {}
     39 
     40   void ReadTestFile(const std::string& name) {
     41     base::FilePath test_data_dir;
     42     ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir));
     43     base::FilePath json_file = test_data_dir.AppendASCII(name + ".json");
     44     ASSERT_TRUE(base::ReadFileToString(json_file, &json_));
     45   }
     46 
     47   virtual void SetupTree() OVERRIDE {
     48     gfx::Size viewport = gfx::Size(720, 1038);
     49     layer_tree_host()->SetViewportSize(viewport);
     50     scoped_refptr<Layer> root =
     51         ParseTreeFromJson(json_, &content_layer_client_);
     52     ASSERT_TRUE(root.get());
     53     layer_tree_host()->SetRootLayer(root);
     54   }
     55 
     56   void SetTestName(const std::string& name) { test_name_ = name; }
     57 
     58   virtual void AfterTest() OVERRIDE {
     59     CHECK(!test_name_.empty()) << "Must SetTestName() before TearDown().";
     60     perf_test::PrintResult("calc_draw_props_time",
     61                            "",
     62                            test_name_,
     63                            1000 * timer_.MsPerLap(),
     64                            "us",
     65                            true);
     66   }
     67 
     68  protected:
     69   FakeContentLayerClient content_layer_client_;
     70   LapTimer timer_;
     71   std::string test_name_;
     72   std::string json_;
     73 };
     74 
     75 class CalcDrawPropsMainTest : public LayerTreeHostCommonPerfTest {
     76  public:
     77   void RunCalcDrawProps() {
     78     RunTest(false, false, false);
     79   }
     80 
     81   virtual void BeginTest() OVERRIDE {
     82     timer_.Reset();
     83 
     84     do {
     85       bool can_render_to_separate_surface = true;
     86       int max_texture_size = 8096;
     87       RenderSurfaceLayerList update_list;
     88       LayerTreeHostCommon::CalcDrawPropsMainInputs inputs(
     89           layer_tree_host()->root_layer(),
     90           layer_tree_host()->device_viewport_size(),
     91           gfx::Transform(),
     92           layer_tree_host()->device_scale_factor(),
     93           layer_tree_host()->page_scale_factor(),
     94           layer_tree_host()->page_scale_layer(),
     95           max_texture_size,
     96           layer_tree_host()->settings().can_use_lcd_text,
     97           can_render_to_separate_surface,
     98           layer_tree_host()
     99               ->settings()
    100               .layer_transforms_should_scale_layer_contents,
    101           &update_list,
    102           0);
    103       LayerTreeHostCommon::CalculateDrawProperties(&inputs);
    104 
    105       timer_.NextLap();
    106     } while (!timer_.HasTimeLimitExpired());
    107 
    108     EndTest();
    109   }
    110 };
    111 
    112 class CalcDrawPropsImplTest : public LayerTreeHostCommonPerfTest {
    113  public:
    114   void RunCalcDrawProps() {
    115     RunTestWithImplSidePainting();
    116   }
    117 
    118   virtual void BeginTest() OVERRIDE {
    119     PostSetNeedsCommitToMainThread();
    120   }
    121 
    122   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
    123     timer_.Reset();
    124     LayerTreeImpl* active_tree = host_impl->active_tree();
    125 
    126     do {
    127       bool can_render_to_separate_surface = true;
    128       int max_texture_size = 8096;
    129       DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
    130                                max_texture_size,
    131                                active_tree,
    132                                host_impl);
    133 
    134       timer_.NextLap();
    135     } while (!timer_.HasTimeLimitExpired());
    136 
    137     EndTest();
    138   }
    139 
    140   void DoCalcDrawPropertiesImpl(bool can_render_to_separate_surface,
    141                                 int max_texture_size,
    142                                 LayerTreeImpl* active_tree,
    143                                 LayerTreeHostImpl* host_impl) {
    144     LayerImplList update_list;
    145     LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
    146         active_tree->root_layer(),
    147         active_tree->DrawViewportSize(),
    148         host_impl->DrawTransform(),
    149         active_tree->device_scale_factor(),
    150         active_tree->total_page_scale_factor(),
    151         active_tree->InnerViewportContainerLayer(),
    152         max_texture_size,
    153         host_impl->settings().can_use_lcd_text,
    154         can_render_to_separate_surface,
    155         host_impl->settings().layer_transforms_should_scale_layer_contents,
    156         &update_list,
    157         0);
    158     LayerTreeHostCommon::CalculateDrawProperties(&inputs);
    159   }
    160 };
    161 
    162 class LayerSorterMainTest : public CalcDrawPropsImplTest {
    163  public:
    164   void RunSortLayers() { RunTest(false, false, false); }
    165 
    166   virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
    167 
    168   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
    169     LayerTreeImpl* active_tree = host_impl->active_tree();
    170     // First build the tree and then we'll start running tests on layersorter
    171     // itself
    172     bool can_render_to_separate_surface = true;
    173     int max_texture_size = 8096;
    174     DoCalcDrawPropertiesImpl(can_render_to_separate_surface,
    175                              max_texture_size,
    176                              active_tree,
    177                              host_impl);
    178 
    179     // Behaviour of this test is different from that of sorting in practice.
    180     // In this case, all layers that exist in any 3D context are put into a list
    181     // and are sorted as one big 3D context instead of several smaller ones.
    182     BuildLayerImplList(active_tree->root_layer(), &base_list_);
    183     timer_.Reset();
    184     do {
    185       // Here we'll move the layers into a LayerImpl list of their own to be
    186       // sorted so we don't have a sorted list for every run after the first
    187       LayerImplList test_list = base_list_;
    188       layer_sorter_.Sort(test_list.begin(), test_list.end());
    189       timer_.NextLap();
    190     } while (!timer_.HasTimeLimitExpired());
    191 
    192     EndTest();
    193   }
    194 
    195   void BuildLayerImplList(LayerImpl* layer, LayerImplList* list) {
    196     if (layer->Is3dSorted()) {
    197       list->push_back(layer);
    198     }
    199 
    200     for (unsigned int i = 0; i < layer->children().size(); i++) {
    201       BuildLayerImplList(layer->children()[i], list);
    202     }
    203   }
    204 
    205  private:
    206   LayerImplList base_list_;
    207   LayerSorter layer_sorter_;
    208 };
    209 
    210 TEST_F(CalcDrawPropsMainTest, TenTen) {
    211   SetTestName("10_10_main_thread");
    212   ReadTestFile("10_10_layer_tree");
    213   RunCalcDrawProps();
    214 }
    215 
    216 TEST_F(CalcDrawPropsMainTest, HeavyPage) {
    217   SetTestName("heavy_page_main_thread");
    218   ReadTestFile("heavy_layer_tree");
    219   RunCalcDrawProps();
    220 }
    221 
    222 TEST_F(CalcDrawPropsMainTest, TouchRegionLight) {
    223   SetTestName("touch_region_light_main_thread");
    224   ReadTestFile("touch_region_light");
    225   RunCalcDrawProps();
    226 }
    227 
    228 TEST_F(CalcDrawPropsMainTest, TouchRegionHeavy) {
    229   SetTestName("touch_region_heavy_main_thread");
    230   ReadTestFile("touch_region_heavy");
    231   RunCalcDrawProps();
    232 }
    233 
    234 TEST_F(CalcDrawPropsImplTest, TenTen) {
    235   SetTestName("10_10");
    236   ReadTestFile("10_10_layer_tree");
    237   RunCalcDrawProps();
    238 }
    239 
    240 TEST_F(CalcDrawPropsImplTest, HeavyPage) {
    241   SetTestName("heavy_page");
    242   ReadTestFile("heavy_layer_tree");
    243   RunCalcDrawProps();
    244 }
    245 
    246 TEST_F(CalcDrawPropsImplTest, TouchRegionLight) {
    247   SetTestName("touch_region_light");
    248   ReadTestFile("touch_region_light");
    249   RunCalcDrawProps();
    250 }
    251 
    252 TEST_F(CalcDrawPropsImplTest, TouchRegionHeavy) {
    253   SetTestName("touch_region_heavy");
    254   ReadTestFile("touch_region_heavy");
    255   RunCalcDrawProps();
    256 }
    257 
    258 TEST_F(LayerSorterMainTest, LayerSorterCubes) {
    259   SetTestName("layer_sort_cubes");
    260   ReadTestFile("layer_sort_cubes");
    261   RunSortLayers();
    262 }
    263 
    264 TEST_F(LayerSorterMainTest, LayerSorterRubik) {
    265   SetTestName("layer_sort_rubik");
    266   ReadTestFile("layer_sort_rubik");
    267   RunSortLayers();
    268 }
    269 
    270 }  // namespace
    271 }  // namespace cc
    272