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.
5 #include "ash/system/chromeos/power/power_event_observer.h"
7 #include "ash/session/session_state_delegate.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "ash/wm/power_button_controller.h"
11 #include "base/prefs/pref_service.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/base/user_activity/user_activity_detector.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/display/chromeos/display_configurator.h"
23 // Tells the compositor for each of the displays to finish all pending
24 // rendering requests and block any new ones.
25 void StopRenderingRequests() {
26 for (aura::Window
* window
: Shell::GetAllRootWindows()) {
27 ui::Compositor
* compositor
= window
->GetHost()->compositor();
28 compositor
->SetVisible(false);
29 compositor
->FinishAllRendering();
33 // Tells the compositor for each of the displays to resume sending rendering
34 // requests to the GPU.
35 void ResumeRenderingRequests() {
36 for (aura::Window
* window
: Shell::GetAllRootWindows())
37 window
->GetHost()->compositor()->SetVisible(true);
40 void OnSuspendDisplaysCompleted(const base::Closure
& suspend_callback
,
42 suspend_callback
.Run();
47 PowerEventObserver::PowerEventObserver()
48 : screen_locked_(false), waiting_for_lock_screen_animations_(false) {
49 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
51 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
55 PowerEventObserver::~PowerEventObserver() {
56 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
58 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
62 void PowerEventObserver::OnLockAnimationsComplete() {
63 VLOG(1) << "Screen locker animations have completed.";
64 waiting_for_lock_screen_animations_
= false;
66 if (!screen_lock_callback_
.is_null()) {
67 StopRenderingRequests();
69 screen_lock_callback_
.Run();
70 screen_lock_callback_
.Reset();
74 void PowerEventObserver::BrightnessChanged(int level
, bool user_initiated
) {
75 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged(
76 static_cast<double>(level
));
79 void PowerEventObserver::SuspendImminent() {
80 Shell
* shell
= Shell::GetInstance();
81 SessionStateDelegate
* delegate
= shell
->session_state_delegate();
83 // This class is responsible for disabling all rendering requests at suspend
84 // time and then enabling them at resume time. When the
85 // lock-before-suspending pref is not set this is easy to do since
86 // StopRenderingRequests() is just called directly from this function. If the
87 // lock-before-suspending pref _is_ set, then the suspend needs to be delayed
88 // until the lock screen is fully visible. While it is sufficient from a
89 // security perspective to block only until the lock screen is ready, which
90 // guarantees that the contents of the user's screen are no longer visible,
91 // this leads to poor UX on the first resume since neither the user pod nor
92 // the header bar will be visible for a few hundred milliseconds until the GPU
93 // process starts rendering again. To deal with this, the suspend is delayed
94 // until all the lock screen animations have completed and the suspend request
95 // is unblocked from OnLockAnimationsComplete().
96 if (!screen_locked_
&& delegate
->ShouldLockScreenBeforeSuspending() &&
97 delegate
->CanLockScreen()) {
98 screen_lock_callback_
= chromeos::DBusThreadManager::Get()->
99 GetPowerManagerClient()->GetSuspendReadinessCallback();
100 VLOG(1) << "Requesting screen lock from PowerEventObserver";
101 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
103 } else if (waiting_for_lock_screen_animations_
) {
104 // The lock-before-suspending pref has been set and the lock screen is ready
105 // but the animations have not completed yet. This can happen if a suspend
106 // request is canceled after the lock screen is ready but before the
107 // animations have completed and then another suspend request is immediately
108 // started. In practice, it is highly unlikely that this will ever happen
109 // but it's better to be safe since the cost of not dealing with it properly
110 // is a memory leak in the GPU and weird artifacts on the screen.
111 screen_lock_callback_
= chromeos::DBusThreadManager::Get()
112 ->GetPowerManagerClient()
113 ->GetSuspendReadinessCallback();
115 // The lock-before-suspending pref is not set or the screen has already been
116 // locked and the animations have completed. Rendering can be stopped now.
117 StopRenderingRequests();
120 ui::UserActivityDetector::Get()->OnDisplayPowerChanging();
121 shell
->display_configurator()->SuspendDisplays(base::Bind(
122 &OnSuspendDisplaysCompleted
, chromeos::DBusThreadManager::Get()
123 ->GetPowerManagerClient()
124 ->GetSuspendReadinessCallback()));
127 void PowerEventObserver::SuspendDone(const base::TimeDelta
& sleep_duration
) {
128 Shell::GetInstance()->display_configurator()->ResumeDisplays();
129 Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock();
131 // If the suspend request was being blocked while waiting for the lock
132 // animation to complete, clear the blocker since the suspend has already
133 // completed. This prevents rendering requests from being blocked after a
134 // resume if the lock screen took too long to show.
135 screen_lock_callback_
.Reset();
137 ResumeRenderingRequests();
140 void PowerEventObserver::ScreenIsLocked() {
141 screen_locked_
= true;
142 waiting_for_lock_screen_animations_
= true;
144 // The screen is now locked but the pending suspend, if any, will be blocked
145 // until all the animations have completed.
146 if (!screen_lock_callback_
.is_null()) {
147 VLOG(1) << "Screen locked due to suspend";
149 VLOG(1) << "Screen locked without suspend";
153 void PowerEventObserver::ScreenIsUnlocked() {
154 screen_locked_
= false;