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/layers/layer.h"
6
7#include "cc/debug/lap_timer.h"
8#include "cc/resources/layer_painter.h"
9#include "cc/test/fake_impl_proxy.h"
10#include "cc/test/fake_layer_tree_host.h"
11#include "cc/test/fake_layer_tree_host_client.h"
12#include "cc/test/fake_layer_tree_host_impl.h"
13
14#include "testing/gtest/include/gtest/gtest.h"
15#include "testing/perf/perf_test.h"
16
17namespace cc {
18namespace {
19
20static const int kTimeLimitMillis = 3000;
21static const int kWarmupRuns = 5;
22static const int kTimeCheckInterval = 10;
23
24class MockLayerPainter : public LayerPainter {
25 public:
26  virtual void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) OVERRIDE {
27  }
28};
29
30
31class LayerPerfTest : public testing::Test {
32 public:
33  LayerPerfTest()
34      : host_impl_(&proxy_, &shared_bitmap_manager_),
35        fake_client_(FakeLayerTreeHostClient::DIRECT_3D),
36        timer_(kWarmupRuns,
37               base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
38               kTimeCheckInterval) {}
39
40 protected:
41  virtual void SetUp() OVERRIDE {
42    layer_tree_host_ = FakeLayerTreeHost::Create(&fake_client_);
43    layer_tree_host_->InitializeSingleThreaded(
44        &fake_client_, base::MessageLoopProxy::current());
45  }
46
47  virtual void TearDown() OVERRIDE {
48    layer_tree_host_->SetRootLayer(NULL);
49    layer_tree_host_.reset();
50  }
51
52  FakeImplProxy proxy_;
53  TestSharedBitmapManager shared_bitmap_manager_;
54  FakeLayerTreeHostImpl host_impl_;
55
56  FakeLayerTreeHostClient fake_client_;
57  scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
58  LapTimer timer_;
59};
60
61TEST_F(LayerPerfTest, PushPropertiesTo) {
62  scoped_refptr<Layer> test_layer = Layer::Create();
63  scoped_ptr<LayerImpl> impl_layer =
64      LayerImpl::Create(host_impl_.active_tree(), 1);
65
66  layer_tree_host_->SetRootLayer(test_layer);
67
68  float transform_origin_z = 0;
69  bool scrollable = true;
70  bool contents_opaque = true;
71  bool double_sided = true;
72  bool hide_layer_and_subtree = true;
73  bool masks_to_bounds = true;
74
75  // Properties changed.
76  timer_.Reset();
77  do {
78    test_layer->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
79    test_layer->SetTransformOrigin(gfx::Point3F(0.f, 0.f, transform_origin_z));
80    test_layer->SetContentsOpaque(contents_opaque);
81    test_layer->SetDoubleSided(double_sided);
82    test_layer->SetHideLayerAndSubtree(hide_layer_and_subtree);
83    test_layer->SetMasksToBounds(masks_to_bounds);
84    test_layer->SetScrollClipLayerId(scrollable ? test_layer->id()
85                                                : Layer::INVALID_ID);
86    test_layer->PushPropertiesTo(impl_layer.get());
87
88    transform_origin_z += 0.01f;
89    scrollable = !scrollable;
90    contents_opaque = !contents_opaque;
91    double_sided = !double_sided;
92    hide_layer_and_subtree = !hide_layer_and_subtree;
93    masks_to_bounds = !masks_to_bounds;
94
95    timer_.NextLap();
96  } while (!timer_.HasTimeLimitExpired());
97
98  perf_test::PrintResult("push_properties_to",
99                         "",
100                         "props_changed",
101                         timer_.LapsPerSecond(),
102                         "runs/s",
103                         true);
104
105  // Properties didn't change.
106  timer_.Reset();
107  do {
108    test_layer->PushPropertiesTo(impl_layer.get());
109    timer_.NextLap();
110  } while (!timer_.HasTimeLimitExpired());
111
112  perf_test::PrintResult("push_properties_to",
113                         "",
114                         "props_didnt_change",
115                         timer_.LapsPerSecond(),
116                         "runs/s",
117                         true);
118}
119
120
121}  // namespace
122}  // namespace cc
123