blob: 4afbf423528d80251517ffeef899682943e78cf6 [file] [log] [blame]
[email protected]ed9b489a2012-11-28 22:01:401// Copyright 2012 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
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host.h"
[email protected]ed9b489a2012-11-28 22:01:406
[email protected]3393fa92013-10-18 03:37:527#include <sstream>
8
[email protected]91cd7482012-11-30 05:51:459#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]91cd7482012-11-30 05:51:4511#include "base/path_service.h"
[email protected]daf079a2013-04-17 21:42:4012#include "base/strings/string_piece.h"
[email protected]542ce1a2013-08-29 14:25:4413#include "base/time/time.h"
[email protected]cc3cfaa2013-03-18 09:05:5214#include "cc/layers/content_layer.h"
15#include "cc/layers/nine_patch_layer.h"
16#include "cc/layers/solid_color_layer.h"
[email protected]3393fa92013-10-18 03:37:5217#include "cc/layers/texture_layer.h"
18#include "cc/resources/texture_mailbox.h"
[email protected]ed9b489a2012-11-28 22:01:4019#include "cc/test/fake_content_layer_client.h"
[email protected]542ce1a2013-08-29 14:25:4420#include "cc/test/lap_timer.h"
[email protected]f8fef2bd2013-02-04 23:39:2221#include "cc/test/layer_tree_json_parser.h"
[email protected]e216fef02013-03-20 22:56:1022#include "cc/test/layer_tree_test.h"
[email protected]1b901a32012-12-07 08:52:0323#include "cc/test/paths.h"
[email protected]d993e032013-06-07 00:16:1624#include "cc/trees/layer_tree_impl.h"
[email protected]ab1555a2013-09-04 02:16:1625#include "testing/perf/perf_test.h"
[email protected]ed9b489a2012-11-28 22:01:4026
27namespace cc {
28namespace {
29
[email protected]91cd7482012-11-30 05:51:4530static const int kTimeLimitMillis = 2000;
[email protected]f8fef2bd2013-02-04 23:39:2231static const int kWarmupRuns = 5;
32static const int kTimeCheckInterval = 10;
[email protected]ed9b489a2012-11-28 22:01:4033
[email protected]e216fef02013-03-20 22:56:1034class LayerTreeHostPerfTest : public LayerTreeTest {
[email protected]ed9b489a2012-11-28 22:01:4035 public:
[email protected]91cd7482012-11-30 05:51:4536 LayerTreeHostPerfTest()
[email protected]542ce1a2013-08-29 14:25:4437 : draw_timer_(kWarmupRuns,
38 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
39 kTimeCheckInterval),
40 commit_timer_(0, base::TimeDelta(), 1),
[email protected]a49d0f8a2013-05-09 23:26:1941 full_damage_each_frame_(false),
42 animation_driven_drawing_(false),
43 measure_commit_cost_(false) {
[email protected]2384c8a2013-03-21 08:35:0444 fake_content_layer_client_.set_paint_all_opaque(true);
[email protected]ed9b489a2012-11-28 22:01:4045 }
46
[email protected]1673d6e2013-10-28 21:51:1747 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
48 settings->throttle_frame_production = false;
49 }
50
[email protected]e216fef02013-03-20 22:56:1051 virtual void BeginTest() OVERRIDE {
52 BuildTree();
53 PostSetNeedsCommitToMainThread();
[email protected]91cd7482012-11-30 05:51:4554 }
55
[email protected]a49d0f8a2013-05-09 23:26:1956 virtual void Animate(base::TimeTicks monotonic_time) OVERRIDE {
[email protected]5f6fe5962014-03-03 21:03:4057 if (animation_driven_drawing_ && !TestEnded()) {
[email protected]8b9e52b2014-01-17 16:35:3158 layer_tree_host()->SetNeedsAnimate();
[email protected]5f6fe5962014-03-03 21:03:4059 layer_tree_host()->SetNextCommitForcesRedraw();
60 }
[email protected]a49d0f8a2013-05-09 23:26:1961 }
62
63 virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
64 if (measure_commit_cost_)
[email protected]542ce1a2013-08-29 14:25:4465 commit_timer_.Start();
[email protected]a49d0f8a2013-05-09 23:26:1966 }
67
68 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
[email protected]542ce1a2013-08-29 14:25:4469 if (measure_commit_cost_ && draw_timer_.IsWarmedUp()) {
70 commit_timer_.NextLap();
[email protected]d993e032013-06-07 00:16:1671 }
[email protected]a49d0f8a2013-05-09 23:26:1972 }
73
[email protected]e216fef02013-03-20 22:56:1074 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
[email protected]6485a302013-12-10 22:19:1775 if (TestEnded() || CleanUpStarted())
[email protected]542ce1a2013-08-29 14:25:4476 return;
[email protected]542ce1a2013-08-29 14:25:4477 draw_timer_.NextLap();
78 if (draw_timer_.HasTimeLimitExpired()) {
[email protected]6485a302013-12-10 22:19:1779 CleanUpAndEndTest(impl);
[email protected]542ce1a2013-08-29 14:25:4480 return;
[email protected]f8fef2bd2013-02-04 23:39:2281 }
[email protected]a49d0f8a2013-05-09 23:26:1982 if (!animation_driven_drawing_)
83 impl->SetNeedsRedraw();
[email protected]e0341352013-04-06 05:01:2084 if (full_damage_each_frame_)
85 impl->SetFullRootLayerDamage();
[email protected]91cd7482012-11-30 05:51:4586 }
87
[email protected]6485a302013-12-10 22:19:1788 virtual void CleanUpAndEndTest(LayerTreeHostImpl* host_impl) { EndTest(); }
89
90 virtual bool CleanUpStarted() { return false; }
91
[email protected]e216fef02013-03-20 22:56:1092 virtual void BuildTree() {}
[email protected]91cd7482012-11-30 05:51:4593
[email protected]e216fef02013-03-20 22:56:1094 virtual void AfterTest() OVERRIDE {
[email protected]ab1555a2013-09-04 02:16:1695 CHECK(!test_name_.empty()) << "Must SetTestName() before AfterTest().";
[email protected]ab1555a2013-09-04 02:16:1696 perf_test::PrintResult("layer_tree_host_frame_time", "", test_name_,
97 1000 * draw_timer_.MsPerLap(), "us", true);
[email protected]a49d0f8a2013-05-09 23:26:1998 if (measure_commit_cost_) {
[email protected]ab1555a2013-09-04 02:16:1699 perf_test::PrintResult("layer_tree_host_commit_time", "", test_name_,
100 1000 * commit_timer_.MsPerLap(), "us", true);
[email protected]a49d0f8a2013-05-09 23:26:19101 }
[email protected]ed9b489a2012-11-28 22:01:40102 }
103
[email protected]91cd7482012-11-30 05:51:45104 protected:
[email protected]542ce1a2013-08-29 14:25:44105 LapTimer draw_timer_;
106 LapTimer commit_timer_;
107
[email protected]91cd7482012-11-30 05:51:45108 std::string test_name_;
[email protected]f8fef2bd2013-02-04 23:39:22109 FakeContentLayerClient fake_content_layer_client_;
[email protected]e0341352013-04-06 05:01:20110 bool full_damage_each_frame_;
[email protected]a49d0f8a2013-05-09 23:26:19111 bool animation_driven_drawing_;
112
113 bool measure_commit_cost_;
[email protected]91cd7482012-11-30 05:51:45114};
115
[email protected]91cd7482012-11-30 05:51:45116
117class LayerTreeHostPerfTestJsonReader : public LayerTreeHostPerfTest {
118 public:
119 LayerTreeHostPerfTestJsonReader()
120 : LayerTreeHostPerfTest() {
121 }
122
[email protected]ab1555a2013-09-04 02:16:16123 void SetTestName(const std::string& name) {
[email protected]91cd7482012-11-30 05:51:45124 test_name_ = name;
[email protected]ab1555a2013-09-04 02:16:16125 }
126
127 void ReadTestFile(const std::string& name) {
[email protected]6d4b67a2013-02-10 04:49:30128 base::FilePath test_data_dir;
[email protected]e51444a2013-12-10 23:05:01129 ASSERT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir));
[email protected]6d4b67a2013-02-10 04:49:30130 base::FilePath json_file = test_data_dir.AppendASCII(name + ".json");
[email protected]82f84b92013-08-30 18:23:50131 ASSERT_TRUE(base::ReadFileToString(json_file, &json_));
[email protected]91cd7482012-11-30 05:51:45132 }
133
[email protected]e216fef02013-03-20 22:56:10134 virtual void BuildTree() OVERRIDE {
[email protected]91cd7482012-11-30 05:51:45135 gfx::Size viewport = gfx::Size(720, 1038);
[email protected]18ce59702013-04-09 04:58:40136 layer_tree_host()->SetViewportSize(viewport);
[email protected]f8fef2bd2013-02-04 23:39:22137 scoped_refptr<Layer> root = ParseTreeFromJson(json_,
138 &fake_content_layer_client_);
139 ASSERT_TRUE(root.get());
[email protected]e216fef02013-03-20 22:56:10140 layer_tree_host()->SetRootLayer(root);
[email protected]91cd7482012-11-30 05:51:45141 }
142
143 private:
[email protected]f8fef2bd2013-02-04 23:39:22144 std::string json_;
[email protected]91cd7482012-11-30 05:51:45145};
146
[email protected]f8fef2bd2013-02-04 23:39:22147// Simulates a tab switcher scene with two stacks of 10 tabs each.
148TEST_F(LayerTreeHostPerfTestJsonReader, TenTenSingleThread) {
[email protected]ab1555a2013-09-04 02:16:16149 SetTestName("10_10_single_thread");
[email protected]e216fef02013-03-20 22:56:10150 ReadTestFile("10_10_layer_tree");
[email protected]2a61ad52013-05-13 14:01:29151 RunTest(false, false, false);
[email protected]91cd7482012-11-30 05:51:45152}
[email protected]ed9b489a2012-11-28 22:01:40153
[email protected]c0f7a7f2013-10-15 21:33:56154TEST_F(LayerTreeHostPerfTestJsonReader, TenTenThreadedImplSide) {
155 SetTestName("10_10_threaded_impl_side");
156 ReadTestFile("10_10_layer_tree");
157 RunTestWithImplSidePainting();
158}
159
[email protected]e0341352013-04-06 05:01:20160// Simulates a tab switcher scene with two stacks of 10 tabs each.
161TEST_F(LayerTreeHostPerfTestJsonReader,
162 TenTenSingleThread_FullDamageEachFrame) {
163 full_damage_each_frame_ = true;
[email protected]ab1555a2013-09-04 02:16:16164 SetTestName("10_10_single_thread_full_damage_each_frame");
[email protected]e0341352013-04-06 05:01:20165 ReadTestFile("10_10_layer_tree");
[email protected]2a61ad52013-05-13 14:01:29166 RunTest(false, false, false);
[email protected]e0341352013-04-06 05:01:20167}
168
[email protected]c0f7a7f2013-10-15 21:33:56169TEST_F(LayerTreeHostPerfTestJsonReader,
170 TenTenThreadedImplSide_FullDamageEachFrame) {
171 full_damage_each_frame_ = true;
172 SetTestName("10_10_threaded_impl_side_full_damage_each_frame");
173 ReadTestFile("10_10_layer_tree");
174 RunTestWithImplSidePainting();
175}
176
[email protected]f4e25f92013-07-13 20:54:53177// Invalidates a leaf layer in the tree on the main thread after every commit.
178class LayerTreeHostPerfTestLeafInvalidates
179 : public LayerTreeHostPerfTestJsonReader {
180 public:
181 virtual void BuildTree() OVERRIDE {
182 LayerTreeHostPerfTestJsonReader::BuildTree();
183
184 // Find a leaf layer.
185 for (layer_to_invalidate_ = layer_tree_host()->root_layer();
186 layer_to_invalidate_->children().size();
187 layer_to_invalidate_ = layer_to_invalidate_->children()[0]) {}
188 }
189
190 virtual void DidCommitAndDrawFrame() OVERRIDE {
191 if (TestEnded())
192 return;
193
194 static bool flip = true;
195 layer_to_invalidate_->SetOpacity(flip ? 1.f : 0.5f);
196 flip = !flip;
197 }
198
199 protected:
200 Layer* layer_to_invalidate_;
201};
202
203// Simulates a tab switcher scene with two stacks of 10 tabs each. Invalidate a
204// property on a leaf layer in the tree every commit.
205TEST_F(LayerTreeHostPerfTestLeafInvalidates, TenTenSingleThread) {
[email protected]ab1555a2013-09-04 02:16:16206 SetTestName("10_10_single_thread_leaf_invalidates");
[email protected]f4e25f92013-07-13 20:54:53207 ReadTestFile("10_10_layer_tree");
208 RunTest(false, false, false);
209}
210
[email protected]c0f7a7f2013-10-15 21:33:56211TEST_F(LayerTreeHostPerfTestLeafInvalidates, TenTenThreadedImplSide) {
212 SetTestName("10_10_threaded_impl_side_leaf_invalidates");
213 ReadTestFile("10_10_layer_tree");
214 RunTestWithImplSidePainting();
215}
216
[email protected]f8fef2bd2013-02-04 23:39:22217// Simulates main-thread scrolling on each frame.
218class ScrollingLayerTreePerfTest : public LayerTreeHostPerfTestJsonReader {
219 public:
220 ScrollingLayerTreePerfTest()
221 : LayerTreeHostPerfTestJsonReader() {
222 }
223
[email protected]e216fef02013-03-20 22:56:10224 virtual void BuildTree() OVERRIDE {
225 LayerTreeHostPerfTestJsonReader::BuildTree();
226 scrollable_ = layer_tree_host()->root_layer()->children()[1];
[email protected]22898ed2013-06-01 04:52:30227 ASSERT_TRUE(scrollable_.get());
[email protected]f8fef2bd2013-02-04 23:39:22228 }
229
[email protected]e216fef02013-03-20 22:56:10230 virtual void Layout() OVERRIDE {
[email protected]f8fef2bd2013-02-04 23:39:22231 static const gfx::Vector2d delta = gfx::Vector2d(0, 10);
[email protected]7aba6662013-03-12 10:17:34232 scrollable_->SetScrollOffset(scrollable_->scroll_offset() + delta);
[email protected]f8fef2bd2013-02-04 23:39:22233 }
234
235 private:
[email protected]7aba6662013-03-12 10:17:34236 scoped_refptr<Layer> scrollable_;
[email protected]f8fef2bd2013-02-04 23:39:22237};
238
[email protected]c0f7a7f2013-10-15 21:33:56239TEST_F(ScrollingLayerTreePerfTest, LongScrollablePageSingleThread) {
[email protected]ab1555a2013-09-04 02:16:16240 SetTestName("long_scrollable_page");
[email protected]e216fef02013-03-20 22:56:10241 ReadTestFile("long_scrollable_page");
[email protected]2a61ad52013-05-13 14:01:29242 RunTest(false, false, false);
[email protected]f8fef2bd2013-02-04 23:39:22243}
244
[email protected]c0f7a7f2013-10-15 21:33:56245TEST_F(ScrollingLayerTreePerfTest, LongScrollablePageThreadedImplSide) {
246 SetTestName("long_scrollable_page_threaded_impl_side");
247 ReadTestFile("long_scrollable_page");
248 RunTestWithImplSidePainting();
249}
[email protected]a49d0f8a2013-05-09 23:26:19250
[email protected]df41e252014-02-03 23:39:50251static void EmptyReleaseCallback(uint32 sync_point, bool lost_resource) {}
[email protected]3393fa92013-10-18 03:37:52252
253// Simulates main-thread scrolling on each frame.
254class BrowserCompositorInvalidateLayerTreePerfTest
255 : public LayerTreeHostPerfTestJsonReader {
256 public:
257 BrowserCompositorInvalidateLayerTreePerfTest()
[email protected]00c51dc2014-03-19 21:16:50258 : LayerTreeHostPerfTestJsonReader(),
259 next_sync_point_(1),
260 clean_up_started_(false) {}
[email protected]3393fa92013-10-18 03:37:52261
262 virtual void BuildTree() OVERRIDE {
263 LayerTreeHostPerfTestJsonReader::BuildTree();
264 tab_contents_ =
265 static_cast<TextureLayer*>(
266 layer_tree_host()->root_layer()->children()[0]->
267 children()[0]->
268 children()[0]->
269 children()[0].get());
270 ASSERT_TRUE(tab_contents_.get());
[email protected]e9b29f1c42013-12-11 02:14:54271 }
[email protected]3393fa92013-10-18 03:37:52272
[email protected]e9b29f1c42013-12-11 02:14:54273 virtual void WillCommit() OVERRIDE {
[email protected]00c51dc2014-03-19 21:16:50274 if (CleanUpStarted())
275 return;
[email protected]3393fa92013-10-18 03:37:52276 gpu::Mailbox gpu_mailbox;
277 std::ostringstream name_stream;
278 name_stream << "name" << next_sync_point_;
[email protected]6485a302013-12-10 22:19:17279 gpu_mailbox.SetName(
280 reinterpret_cast<const int8*>(name_stream.str().c_str()));
[email protected]3393fa92013-10-18 03:37:52281 scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create(
282 base::Bind(&EmptyReleaseCallback));
[email protected]df41e252014-02-03 23:39:50283 TextureMailbox mailbox(gpu_mailbox, GL_TEXTURE_2D, next_sync_point_);
[email protected]3393fa92013-10-18 03:37:52284 next_sync_point_++;
285
286 tab_contents_->SetTextureMailbox(mailbox, callback.Pass());
287 }
288
[email protected]e9b29f1c42013-12-11 02:14:54289 virtual void DidCommit() OVERRIDE {
290 if (CleanUpStarted())
291 return;
292 layer_tree_host()->SetNeedsCommit();
293 }
294
[email protected]6485a302013-12-10 22:19:17295 virtual void CleanUpAndEndTest(LayerTreeHostImpl* host_impl) OVERRIDE {
296 clean_up_started_ = true;
297 MainThreadTaskRunner()->PostTask(
298 FROM_HERE,
299 base::Bind(&BrowserCompositorInvalidateLayerTreePerfTest::
300 CleanUpAndEndTestOnMainThread,
301 base::Unretained(this)));
302 }
303
304 void CleanUpAndEndTestOnMainThread() {
305 tab_contents_->SetTextureMailbox(TextureMailbox(),
306 scoped_ptr<SingleReleaseCallback>());
[email protected]00c51dc2014-03-19 21:16:50307 EndTest();
[email protected]6485a302013-12-10 22:19:17308 }
309
310 virtual bool CleanUpStarted() OVERRIDE { return clean_up_started_; }
311
[email protected]3393fa92013-10-18 03:37:52312 private:
313 scoped_refptr<TextureLayer> tab_contents_;
314 unsigned next_sync_point_;
[email protected]6485a302013-12-10 22:19:17315 bool clean_up_started_;
[email protected]3393fa92013-10-18 03:37:52316};
317
[email protected]00c51dc2014-03-19 21:16:50318TEST_F(BrowserCompositorInvalidateLayerTreePerfTest, DenseBrowserUI) {
319 measure_commit_cost_ = true;
[email protected]3393fa92013-10-18 03:37:52320 SetTestName("dense_layer_tree");
321 ReadTestFile("dense_layer_tree");
322 RunTestWithImplSidePainting();
323}
324
[email protected]a49d0f8a2013-05-09 23:26:19325// Simulates a page with several large, transformed and animated layers.
[email protected]c0f7a7f2013-10-15 21:33:56326TEST_F(LayerTreeHostPerfTestJsonReader, HeavyPageThreadedImplSide) {
[email protected]a49d0f8a2013-05-09 23:26:19327 animation_driven_drawing_ = true;
328 measure_commit_cost_ = true;
[email protected]ab1555a2013-09-04 02:16:16329 SetTestName("heavy_page");
[email protected]a49d0f8a2013-05-09 23:26:19330 ReadTestFile("heavy_layer_tree");
[email protected]2a61ad52013-05-13 14:01:29331 RunTestWithImplSidePainting();
[email protected]a49d0f8a2013-05-09 23:26:19332}
333
[email protected]ed9b489a2012-11-28 22:01:40334} // namespace
335} // namespace cc