Commit ec264133 authored by yjliu's avatar yjliu Committed by Commit Bot

Added FrameThrottlingObserver and updated relevant unit tests.

The FrameThrottlingObserver class observes the start/end of frame
throttling.

Bug: None
Change-Id: Ied963e3098ab5dff447fb68c9939314568bb4aaa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2466207
Commit-Queue: Jun Liu <yjliu@chromium.org>
Reviewed-by: default avatarJun Mukai <mukai@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Auto-Submit: Jun Liu <yjliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#816592}
parent 9913c81a
...@@ -449,6 +449,7 @@ component("ash") { ...@@ -449,6 +449,7 @@ component("ash") {
"frame/wide_frame_view.cc", "frame/wide_frame_view.cc",
"frame_throttler/frame_throttling_controller.cc", "frame_throttler/frame_throttling_controller.cc",
"frame_throttler/frame_throttling_controller.h", "frame_throttler/frame_throttling_controller.h",
"frame_throttler/frame_throttling_observer.h",
"high_contrast/high_contrast_controller.cc", "high_contrast/high_contrast_controller.cc",
"high_contrast/high_contrast_controller.h", "high_contrast/high_contrast_controller.h",
"highlighter/highlighter_controller.cc", "highlighter/highlighter_controller.cc",
......
...@@ -50,7 +50,7 @@ FrameThrottlingController::FrameThrottlingController( ...@@ -50,7 +50,7 @@ FrameThrottlingController::FrameThrottlingController(
int value; int value;
if (base::StringToInt(cl->GetSwitchValueASCII(switches::kFrameThrottleFps), if (base::StringToInt(cl->GetSwitchValueASCII(switches::kFrameThrottleFps),
&value)) { &value)) {
fps_ = value; throttled_fps_ = value;
} }
} }
} }
...@@ -69,10 +69,10 @@ void FrameThrottlingController::StartThrottling( ...@@ -69,10 +69,10 @@ void FrameThrottlingController::StartThrottling(
frame_sink_ids.reserve(windows.size()); frame_sink_ids.reserve(windows.size());
CollectBrowserFrameSinkIds(windows, &frame_sink_ids); CollectBrowserFrameSinkIds(windows, &frame_sink_ids);
if (!frame_sink_ids.empty()) if (!frame_sink_ids.empty())
StartThrottling(frame_sink_ids, fps_); StartThrottling(frame_sink_ids, throttled_fps_);
for (auto& observer : observers_) { for (auto& observer : observers_) {
observer.OnThrottlingStarted(windows); observer.OnThrottlingStarted(windows, throttled_fps_);
} }
} }
...@@ -97,11 +97,12 @@ void FrameThrottlingController::EndThrottling() { ...@@ -97,11 +97,12 @@ void FrameThrottlingController::EndThrottling() {
windows_throttled_ = false; windows_throttled_ = false;
} }
void FrameThrottlingController::AddObserver(Observer* observer) { void FrameThrottlingController::AddObserver(FrameThrottlingObserver* observer) {
observers_.AddObserver(observer); observers_.AddObserver(observer);
} }
void FrameThrottlingController::RemoveObserver(Observer* observer) { void FrameThrottlingController::RemoveObserver(
FrameThrottlingObserver* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "ash/frame_throttler/frame_throttling_observer.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/observer_list_types.h" #include "base/observer_list_types.h"
...@@ -28,13 +29,6 @@ constexpr uint8_t kDefaultThrottleFps = 20; ...@@ -28,13 +29,6 @@ constexpr uint8_t kDefaultThrottleFps = 20;
class ASH_EXPORT FrameThrottlingController { class ASH_EXPORT FrameThrottlingController {
public: public:
class Observer : public base::CheckedObserver {
public:
virtual void OnThrottlingStarted(
const std::vector<aura::Window*>& windows) {}
virtual void OnThrottlingEnded() {}
};
explicit FrameThrottlingController(ui::ContextFactory* context_factory); explicit FrameThrottlingController(ui::ContextFactory* context_factory);
FrameThrottlingController(const FrameThrottlingController&) = delete; FrameThrottlingController(const FrameThrottlingController&) = delete;
FrameThrottlingController& operator=(const FrameThrottlingController&) = FrameThrottlingController& operator=(const FrameThrottlingController&) =
...@@ -46,17 +40,19 @@ class ASH_EXPORT FrameThrottlingController { ...@@ -46,17 +40,19 @@ class ASH_EXPORT FrameThrottlingController {
// Ends throttling of all throttled windows. // Ends throttling of all throttled windows.
void EndThrottling(); void EndThrottling();
void AddObserver(Observer* observer); void AddObserver(FrameThrottlingObserver* observer);
void RemoveObserver(Observer* observer); void RemoveObserver(FrameThrottlingObserver* observer);
uint8_t throttled_fps() const { return throttled_fps_; }
private: private:
void StartThrottling(const std::vector<viz::FrameSinkId>& frame_sink_ids, void StartThrottling(const std::vector<viz::FrameSinkId>& frame_sink_ids,
uint8_t fps); uint8_t fps);
ui::ContextFactory* context_factory_ = nullptr; ui::ContextFactory* context_factory_ = nullptr;
base::ObserverList<Observer> observers_; base::ObserverList<FrameThrottlingObserver> observers_;
// The fps used for throttling. // The fps used for throttling.
uint8_t fps_ = kDefaultThrottleFps; uint8_t throttled_fps_ = kDefaultThrottleFps;
bool windows_throttled_ = false; bool windows_throttled_ = false;
}; };
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_FRAME_THROTTLER_FRAME_THROTTLING_OBSERVER_H_
#define ASH_FRAME_THROTTLER_FRAME_THROTTLING_OBSERVER_H_
#include <stdint.h>
#include <vector>
#include "ash/ash_export.h"
#include "base/macros.h"
#include "base/observer_list_types.h"
namespace aura {
class Window;
}
namespace ash {
// This class observes the start and end of frame throttling.
class ASH_EXPORT FrameThrottlingObserver : public base::CheckedObserver {
public:
virtual void OnThrottlingStarted(const std::vector<aura::Window*>& windows,
uint8_t fps) {}
virtual void OnThrottlingEnded() {}
};
} // namespace ash
#endif // ASH_FRAME_THROTTLER_FRAME_THROTTLING_OBSERVER_H_
...@@ -7,20 +7,19 @@ ...@@ -7,20 +7,19 @@
#include <vector> #include <vector>
#include "ash/frame_throttler/frame_throttling_controller.h" #include "ash/frame_throttler/frame_throttling_observer.h"
#include "base/memory/weak_ptr.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
namespace ash { namespace ash {
class MockFrameThrottlingObserver : public FrameThrottlingController::Observer { class MockFrameThrottlingObserver : public FrameThrottlingObserver {
public: public:
MockFrameThrottlingObserver(); MockFrameThrottlingObserver();
~MockFrameThrottlingObserver() override; ~MockFrameThrottlingObserver() override;
MOCK_METHOD(void, MOCK_METHOD(void,
OnThrottlingStarted, OnThrottlingStarted,
(const std::vector<aura::Window*>& windows), (const std::vector<aura::Window*>& windows, uint8_t fps),
(override)); (override));
MOCK_METHOD(void, OnThrottlingEnded, (), (override)); MOCK_METHOD(void, OnThrottlingEnded, (), (override));
}; };
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "ash/app_list/test/app_list_test_helper.h" #include "ash/app_list/test/app_list_test_helper.h"
#include "ash/frame_throttler/frame_throttling_controller.h"
#include "ash/frame_throttler/mock_frame_throttling_observer.h" #include "ash/frame_throttler/mock_frame_throttling_observer.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h" #include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/keyboard/ui/keyboard_util.h" #include "ash/keyboard/ui/keyboard_util.h"
...@@ -689,13 +690,14 @@ TEST_F(OverviewControllerTest, FrameThrottling) { ...@@ -689,13 +690,14 @@ TEST_F(OverviewControllerTest, FrameThrottling) {
std::unique_ptr<aura::Window> created_windows[window_count]; std::unique_ptr<aura::Window> created_windows[window_count];
std::vector<aura::Window*> windows(window_count, nullptr); std::vector<aura::Window*> windows(window_count, nullptr);
for (int i = 0; i < window_count; ++i) { for (int i = 0; i < window_count; ++i) {
created_windows[i] = CreateTestWindow(); created_windows[i] = CreateAppWindow(gfx::Rect(), AppType::BROWSER);
windows[i] = created_windows[i].get(); windows[i] = created_windows[i].get();
} }
auto* controller = Shell::Get()->overview_controller(); auto* controller = Shell::Get()->overview_controller();
EXPECT_CALL(observer, EXPECT_CALL(observer, OnThrottlingStarted(
OnThrottlingStarted(testing::UnorderedElementsAreArray(windows))); testing::UnorderedElementsAreArray(windows),
frame_throttling_controller->throttled_fps()));
controller->StartOverview(); controller->StartOverview();
EXPECT_CALL(observer, OnThrottlingEnded()); EXPECT_CALL(observer, OnThrottlingEnded());
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "ash/wm/overview/overview_grid.h" #include "ash/wm/overview/overview_grid.h"
#include "ash/frame_throttler/frame_throttling_controller.h"
#include "ash/frame_throttler/mock_frame_throttling_observer.h" #include "ash/frame_throttler/mock_frame_throttling_observer.h"
#include "ash/screen_util.h" #include "ash/screen_util.h"
#include "ash/shell.h" #include "ash/shell.h"
...@@ -295,23 +296,26 @@ TEST_F(OverviewGridTest, FrameThrottling) { ...@@ -295,23 +296,26 @@ TEST_F(OverviewGridTest, FrameThrottling) {
testing::NiceMock<MockFrameThrottlingObserver> observer; testing::NiceMock<MockFrameThrottlingObserver> observer;
FrameThrottlingController* frame_throttling_controller = FrameThrottlingController* frame_throttling_controller =
Shell::Get()->frame_throttling_controller(); Shell::Get()->frame_throttling_controller();
uint8_t throttled_fps = frame_throttling_controller->throttled_fps();
frame_throttling_controller->AddObserver(&observer); frame_throttling_controller->AddObserver(&observer);
const int window_count = 5; const int window_count = 5;
std::unique_ptr<aura::Window> created_windows[window_count]; std::unique_ptr<aura::Window> created_windows[window_count];
std::vector<aura::Window*> windows(window_count, nullptr); std::vector<aura::Window*> windows(window_count, nullptr);
for (int i = 0; i < window_count; ++i) { for (int i = 0; i < window_count; ++i) {
created_windows[i] = CreateTestWindow(); created_windows[i] = CreateAppWindow(gfx::Rect(), AppType::BROWSER);
windows[i] = created_windows[i].get(); windows[i] = created_windows[i].get();
} }
InitializeGrid(windows); InitializeGrid(windows);
frame_throttling_controller->StartThrottling(windows); frame_throttling_controller->StartThrottling(windows);
// Add a new window to overview. // Add a new window to overview.
std::unique_ptr<aura::Window> new_window(CreateTestWindow()); std::unique_ptr<aura::Window> new_window(
CreateAppWindow(gfx::Rect(), AppType::BROWSER));
windows.push_back(new_window.get()); windows.push_back(new_window.get());
EXPECT_CALL(observer, OnThrottlingEnded()); EXPECT_CALL(observer, OnThrottlingEnded());
EXPECT_CALL(observer, EXPECT_CALL(observer,
OnThrottlingStarted(testing::UnorderedElementsAreArray(windows))); OnThrottlingStarted(testing::UnorderedElementsAreArray(windows),
throttled_fps));
grid()->AppendItem(new_window.get(), /*reposition=*/false, /*animate=*/false, grid()->AppendItem(new_window.get(), /*reposition=*/false, /*animate=*/false,
/*use_spawn_animation=*/false); /*use_spawn_animation=*/false);
...@@ -320,8 +324,9 @@ TEST_F(OverviewGridTest, FrameThrottling) { ...@@ -320,8 +324,9 @@ TEST_F(OverviewGridTest, FrameThrottling) {
aura::Window* window = windows[0]; aura::Window* window = windows[0];
windows.erase(windows.begin()); windows.erase(windows.begin());
EXPECT_CALL(observer, OnThrottlingEnded()); EXPECT_CALL(observer, OnThrottlingEnded());
EXPECT_CALL(observer, OnThrottlingStarted( EXPECT_CALL(observer,
testing::UnorderedElementsAreArray(windows))); OnThrottlingStarted(testing::UnorderedElementsAreArray(windows),
throttled_fps));
OverviewItem* item = grid()->GetOverviewItemContaining(window); OverviewItem* item = grid()->GetOverviewItemContaining(window);
grid()->RemoveItem(item, /*item_destroying=*/false, /*reposition=*/false); grid()->RemoveItem(item, /*item_destroying=*/false, /*reposition=*/false);
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "ash/app_list/test/app_list_test_helper.h" #include "ash/app_list/test/app_list_test_helper.h"
#include "ash/focus_cycler.h" #include "ash/focus_cycler.h"
#include "ash/frame_throttler/frame_throttling_controller.h"
#include "ash/frame_throttler/mock_frame_throttling_observer.h" #include "ash/frame_throttler/mock_frame_throttling_observer.h"
#include "ash/home_screen/home_screen_controller.h" #include "ash/home_screen/home_screen_controller.h"
#include "ash/public/cpp/ash_features.h" #include "ash/public/cpp/ash_features.h"
...@@ -1217,28 +1218,32 @@ TEST_F(WindowCycleControllerTest, FrameThrottling) { ...@@ -1217,28 +1218,32 @@ TEST_F(WindowCycleControllerTest, FrameThrottling) {
MockFrameThrottlingObserver observer; MockFrameThrottlingObserver observer;
FrameThrottlingController* frame_throttling_controller = FrameThrottlingController* frame_throttling_controller =
Shell::Get()->frame_throttling_controller(); Shell::Get()->frame_throttling_controller();
uint8_t throttled_fps = frame_throttling_controller->throttled_fps();
frame_throttling_controller->AddObserver(&observer); frame_throttling_controller->AddObserver(&observer);
const int window_count = 5; const int window_count = 5;
std::unique_ptr<aura::Window> created_windows[window_count]; std::unique_ptr<aura::Window> created_windows[window_count];
std::vector<aura::Window*> windows(window_count, nullptr); std::vector<aura::Window*> windows(window_count, nullptr);
for (int i = 0; i < window_count; ++i) { for (int i = 0; i < window_count; ++i) {
created_windows[i] = CreateTestWindow(); created_windows[i] = CreateAppWindow(gfx::Rect(), AppType::BROWSER);
windows[i] = created_windows[i].get(); windows[i] = created_windows[i].get();
} }
WindowCycleController* controller = Shell::Get()->window_cycle_controller(); WindowCycleController* controller = Shell::Get()->window_cycle_controller();
EXPECT_CALL(observer, EXPECT_CALL(observer,
OnThrottlingStarted(testing::UnorderedElementsAreArray(windows))); OnThrottlingStarted(testing::UnorderedElementsAreArray(windows),
throttled_fps));
controller->HandleCycleWindow(WindowCycleController::FORWARD); controller->HandleCycleWindow(WindowCycleController::FORWARD);
EXPECT_CALL(observer, EXPECT_CALL(observer,
OnThrottlingStarted(testing::UnorderedElementsAreArray(windows))) OnThrottlingStarted(testing::UnorderedElementsAreArray(windows),
throttled_fps))
.Times(0); .Times(0);
controller->HandleCycleWindow(WindowCycleController::FORWARD); controller->HandleCycleWindow(WindowCycleController::FORWARD);
EXPECT_CALL(observer, OnThrottlingEnded()); EXPECT_CALL(observer, OnThrottlingEnded());
controller->CompleteCycling(); controller->CompleteCycling();
EXPECT_CALL(observer, EXPECT_CALL(observer,
OnThrottlingStarted(testing::UnorderedElementsAreArray(windows))); OnThrottlingStarted(testing::UnorderedElementsAreArray(windows),
throttled_fps));
controller->HandleCycleWindow(WindowCycleController::FORWARD); controller->HandleCycleWindow(WindowCycleController::FORWARD);
EXPECT_CALL(observer, OnThrottlingEnded()); EXPECT_CALL(observer, OnThrottlingEnded());
controller->CancelCycling(); controller->CancelCycling();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment