layer_perftest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "cc/layers/layer.h"
  5. #include "base/threading/thread_task_runner_handle.h"
  6. #include "base/timer/lap_timer.h"
  7. #include "cc/animation/animation_host.h"
  8. #include "cc/test/fake_impl_task_runner_provider.h"
  9. #include "cc/test/fake_layer_tree_host.h"
  10. #include "cc/test/fake_layer_tree_host_client.h"
  11. #include "cc/test/fake_layer_tree_host_impl.h"
  12. #include "cc/test/stub_layer_tree_host_single_thread_client.h"
  13. #include "cc/test/test_task_graph_runner.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "testing/perf/perf_result_reporter.h"
  16. namespace cc {
  17. namespace {
  18. static const int kTimeLimitMillis = 3000;
  19. static const int kWarmupRuns = 5;
  20. static const int kTimeCheckInterval = 10;
  21. class LayerPerfTest : public testing::Test {
  22. public:
  23. LayerPerfTest()
  24. : host_impl_(&task_runner_provider_, &task_graph_runner_),
  25. timer_(kWarmupRuns,
  26. base::Milliseconds(kTimeLimitMillis),
  27. kTimeCheckInterval) {}
  28. protected:
  29. void SetUp() override {
  30. animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  31. layer_tree_host_ = FakeLayerTreeHost::Create(
  32. &fake_client_, &task_graph_runner_, animation_host_.get());
  33. layer_tree_host_->InitializeSingleThreaded(
  34. &single_thread_client_, base::ThreadTaskRunnerHandle::Get());
  35. }
  36. void TearDown() override {
  37. layer_tree_host_->SetRootLayer(nullptr);
  38. layer_tree_host_ = nullptr;
  39. }
  40. perf_test::PerfResultReporter SetUpReporter(
  41. const std::string& metric_basename,
  42. const std::string& story_name) {
  43. perf_test::PerfResultReporter reporter(metric_basename, story_name);
  44. reporter.RegisterImportantMetric("", "runs/s");
  45. return reporter;
  46. }
  47. FakeImplTaskRunnerProvider task_runner_provider_;
  48. TestTaskGraphRunner task_graph_runner_;
  49. FakeLayerTreeHostImpl host_impl_;
  50. StubLayerTreeHostSingleThreadClient single_thread_client_;
  51. FakeLayerTreeHostClient fake_client_;
  52. std::unique_ptr<AnimationHost> animation_host_;
  53. std::unique_ptr<FakeLayerTreeHost> layer_tree_host_;
  54. base::LapTimer timer_;
  55. };
  56. TEST_F(LayerPerfTest, PushPropertiesTo) {
  57. scoped_refptr<Layer> test_layer = Layer::Create();
  58. std::unique_ptr<LayerImpl> impl_layer =
  59. LayerImpl::Create(host_impl_.active_tree(), 1);
  60. layer_tree_host_->SetRootLayer(test_layer);
  61. float transform_origin_z = 0;
  62. bool scrollable = true;
  63. bool contents_opaque = true;
  64. bool hide_layer_and_subtree = true;
  65. bool masks_to_bounds = true;
  66. // Properties changed.
  67. timer_.Reset();
  68. do {
  69. test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
  70. test_layer->SetTransformOrigin(gfx::Point3F(0.f, 0.f, transform_origin_z));
  71. test_layer->SetContentsOpaque(contents_opaque);
  72. test_layer->SetHideLayerAndSubtree(hide_layer_and_subtree);
  73. test_layer->SetMasksToBounds(masks_to_bounds);
  74. // Here and elsewhere: when doing a full commit, we would call
  75. // layer_tree_host_->ActivateCommitState() and the second argument would
  76. // come from layer_tree_host_->active_commit_state(); we use
  77. // pending_commit_state() just to keep the test code simple.
  78. test_layer->PushPropertiesTo(
  79. impl_layer.get(), *layer_tree_host_->GetPendingCommitState(),
  80. layer_tree_host_->GetThreadUnsafeCommitState());
  81. transform_origin_z += 0.01f;
  82. scrollable = !scrollable;
  83. contents_opaque = !contents_opaque;
  84. hide_layer_and_subtree = !hide_layer_and_subtree;
  85. masks_to_bounds = !masks_to_bounds;
  86. timer_.NextLap();
  87. } while (!timer_.HasTimeLimitExpired());
  88. perf_test::PerfResultReporter reporter =
  89. SetUpReporter("push_properties_to", "props_changed");
  90. reporter.AddResult("", timer_.LapsPerSecond());
  91. // Properties didn't change.
  92. timer_.Reset();
  93. do {
  94. test_layer->PushPropertiesTo(
  95. impl_layer.get(), *layer_tree_host_->GetPendingCommitState(),
  96. layer_tree_host_->GetThreadUnsafeCommitState());
  97. timer_.NextLap();
  98. } while (!timer_.HasTimeLimitExpired());
  99. reporter = SetUpReporter("push_properties_to", "props_didnt_change");
  100. reporter.AddResult("", timer_.LapsPerSecond());
  101. }
  102. TEST_F(LayerPerfTest, ImplPushPropertiesTo) {
  103. std::unique_ptr<LayerImpl> test_layer =
  104. LayerImpl::Create(host_impl_.active_tree(), 1);
  105. std::unique_ptr<LayerImpl> impl_layer =
  106. LayerImpl::Create(host_impl_.active_tree(), 2);
  107. SkColor4f background_color = SkColors::kRed;
  108. gfx::Size bounds(1000, 1000);
  109. bool draws_content = true;
  110. bool contents_opaque = true;
  111. bool masks_to_bounds = true;
  112. // Properties changed.
  113. timer_.Reset();
  114. do {
  115. test_layer->SetBackgroundColor(background_color);
  116. test_layer->SetSafeOpaqueBackgroundColor(background_color);
  117. test_layer->SetDrawsContent(draws_content);
  118. test_layer->SetContentsOpaque(contents_opaque);
  119. test_layer->PushPropertiesTo(impl_layer.get());
  120. background_color =
  121. background_color == SkColors::kRed ? SkColors::kGreen : SkColors::kRed;
  122. bounds = bounds == gfx::Size(1000, 1000) ? gfx::Size(500, 500)
  123. : gfx::Size(1000, 1000);
  124. draws_content = !draws_content;
  125. contents_opaque = !contents_opaque;
  126. masks_to_bounds = !masks_to_bounds;
  127. timer_.NextLap();
  128. } while (!timer_.HasTimeLimitExpired());
  129. perf_test::PerfResultReporter reporter =
  130. SetUpReporter("impl_push_properties_to", "props_changed");
  131. reporter.AddResult("", timer_.LapsPerSecond());
  132. // Properties didn't change.
  133. timer_.Reset();
  134. do {
  135. test_layer->PushPropertiesTo(impl_layer.get());
  136. timer_.NextLap();
  137. } while (!timer_.HasTimeLimitExpired());
  138. reporter = SetUpReporter("impl_push_properties_to", "props_didnt_change");
  139. reporter.AddResult("", timer_.LapsPerSecond());
  140. }
  141. } // namespace
  142. } // namespace cc