ash_background_filter_blur_perftest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2017 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 <memory>
  5. #include "ash/shell.h"
  6. #include "ash/test/ash_test_base.h"
  7. #include "base/timer/lap_timer.h"
  8. #include "testing/perf/perf_test.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/compositor/layer.h"
  11. #include "ui/compositor/test/draw_waiter_for_test.h"
  12. namespace ash {
  13. namespace {
  14. // TODO(wutao): On chromeos_linux builds, the tests only run with
  15. // use_ozone = false.
  16. class AshBackgroundFilterBlurPerfTest : public AshTestBase {
  17. public:
  18. AshBackgroundFilterBlurPerfTest() : timer_(0, base::TimeDelta(), 1) {}
  19. AshBackgroundFilterBlurPerfTest(const AshBackgroundFilterBlurPerfTest&) =
  20. delete;
  21. AshBackgroundFilterBlurPerfTest& operator=(
  22. const AshBackgroundFilterBlurPerfTest&) = delete;
  23. ~AshBackgroundFilterBlurPerfTest() override = default;
  24. // AshTestBase:
  25. void SetUp() override;
  26. protected:
  27. std::unique_ptr<ui::Layer> CreateSolidColorLayer(SkColor color);
  28. void WithBoundsChange(ui::Layer* layer,
  29. int num_iteration,
  30. const std::string& test_name);
  31. void WithOpacityChange(ui::Layer* layer,
  32. int num_iteration,
  33. const std::string& test_name);
  34. std::unique_ptr<ui::Layer> background_layer_;
  35. std::unique_ptr<ui::Layer> blur_layer_;
  36. private:
  37. ui::Layer* root_layer_ = nullptr;
  38. ui::Compositor* compositor_ = nullptr;
  39. base::LapTimer timer_;
  40. };
  41. void AshBackgroundFilterBlurPerfTest::SetUp() {
  42. AshTestBase::SetUp();
  43. // This is for consistency even if the default display size changed.
  44. UpdateDisplay("800x600");
  45. root_layer_ = Shell::GetAllRootWindows()[0]->layer();
  46. compositor_ = root_layer_->GetCompositor();
  47. background_layer_ = CreateSolidColorLayer(SK_ColorGREEN);
  48. blur_layer_ = CreateSolidColorLayer(SK_ColorBLACK);
  49. }
  50. std::unique_ptr<ui::Layer>
  51. AshBackgroundFilterBlurPerfTest::CreateSolidColorLayer(SkColor color) {
  52. std::unique_ptr<ui::Layer> layer =
  53. std::make_unique<ui::Layer>(ui::LAYER_SOLID_COLOR);
  54. layer->SetBounds(root_layer_->bounds());
  55. layer->SetColor(color);
  56. root_layer_->Add(layer.get());
  57. root_layer_->StackAtTop(layer.get());
  58. return layer;
  59. }
  60. void AshBackgroundFilterBlurPerfTest::WithBoundsChange(
  61. ui::Layer* layer,
  62. int num_iteration,
  63. const std::string& test_name) {
  64. const gfx::Rect init_bounds = layer->GetTargetBounds();
  65. // Wait for a DidCommit before starts the loop, and do not measure the last
  66. // iteration of the loop.
  67. ui::DrawWaiterForTest::WaitForCommit(compositor_);
  68. timer_.Reset();
  69. for (int i = 1; i <= num_iteration + 1; ++i) {
  70. float fraction = (static_cast<float>(i) / num_iteration);
  71. const gfx::Rect bounds =
  72. gfx::Rect(0, 0, static_cast<int>(init_bounds.width() * fraction),
  73. static_cast<int>(init_bounds.height() * fraction));
  74. layer->SetBounds(bounds);
  75. ui::DrawWaiterForTest::WaitForCommit(compositor_);
  76. if (i <= num_iteration)
  77. timer_.NextLap();
  78. }
  79. perf_test::PrintResult("AshBackgroundFilterBlurPerfTest", std::string(),
  80. test_name, timer_.LapsPerSecond(), "runs/s", true);
  81. }
  82. void AshBackgroundFilterBlurPerfTest::WithOpacityChange(
  83. ui::Layer* layer,
  84. int num_iteration,
  85. const std::string& test_name) {
  86. float init_opacity = layer->GetTargetOpacity();
  87. // Wait for a DidCommit before starts the loop, and do not measure the last
  88. // iteration of the loop.
  89. ui::DrawWaiterForTest::WaitForCommit(compositor_);
  90. timer_.Reset();
  91. for (int i = 1; i <= num_iteration + 1; ++i) {
  92. float fraction = (static_cast<float>(i) / num_iteration);
  93. float opacity = std::min(1.0f, init_opacity * fraction);
  94. layer->SetOpacity(opacity);
  95. ui::DrawWaiterForTest::WaitForCommit(compositor_);
  96. if (i <= num_iteration)
  97. timer_.NextLap();
  98. }
  99. perf_test::PrintResult("AshBackgroundFilterBlurPerfTest", std::string(),
  100. test_name, timer_.LapsPerSecond(), "runs/s", true);
  101. }
  102. TEST_F(AshBackgroundFilterBlurPerfTest, NoBlurBackgroundLayerBoundsChange) {
  103. WithBoundsChange(background_layer_.get(), 100,
  104. "no_blur_background_layer_bounds_change");
  105. }
  106. TEST_F(AshBackgroundFilterBlurPerfTest, NoBlurBlurLayerBoundsChange) {
  107. WithBoundsChange(blur_layer_.get(), 100, "no_blur_blur_layer_bounds_change");
  108. }
  109. TEST_F(AshBackgroundFilterBlurPerfTest, BackgroundLayerBoundsChange) {
  110. blur_layer_->SetBackgroundBlur(10.f);
  111. WithBoundsChange(background_layer_.get(), 100,
  112. "background_layer_bounds_change");
  113. }
  114. TEST_F(AshBackgroundFilterBlurPerfTest, BlurLayerBoundsChange) {
  115. blur_layer_->SetBackgroundBlur(10.f);
  116. WithBoundsChange(blur_layer_.get(), 100, "blur_layer_bounds_change");
  117. }
  118. TEST_F(AshBackgroundFilterBlurPerfTest, NoBlurBackgroundLayerOpacityChange) {
  119. WithOpacityChange(background_layer_.get(), 100,
  120. "no_blur_background_layer_opacity_change");
  121. }
  122. TEST_F(AshBackgroundFilterBlurPerfTest, NoBlurBlurLayerOpacityChange) {
  123. WithOpacityChange(blur_layer_.get(), 100,
  124. "no_blur_blur_layer_opacity_change");
  125. }
  126. TEST_F(AshBackgroundFilterBlurPerfTest, BackgroundLayerOpacityChange) {
  127. blur_layer_->SetBackgroundBlur(10.f);
  128. WithOpacityChange(background_layer_.get(), 100,
  129. "background_layer_opacity_change");
  130. }
  131. TEST_F(AshBackgroundFilterBlurPerfTest, BlurLayerOpacityChange) {
  132. blur_layer_->SetBackgroundBlur(10.f);
  133. WithOpacityChange(blur_layer_.get(), 100, "blur_layer_opacity_change");
  134. }
  135. } // namespace
  136. } // namespace ash