OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/system/chromeos/power/power_event_observer.h" | 5 #include "ash/system/chromeos/power/power_event_observer.h" |
6 | 6 |
7 #include "ash/session/session_state_delegate.h" | 7 #include "ash/session/session_state_delegate.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/system/tray/system_tray_notifier.h" | 9 #include "ash/system/tray/system_tray_notifier.h" |
10 #include "ash/wm/power_button_controller.h" | 10 #include "ash/wm/power_button_controller.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "ui/aura/window.h" |
| 14 #include "ui/aura/window_tree_host.h" |
13 #include "ui/base/user_activity/user_activity_detector.h" | 15 #include "ui/base/user_activity/user_activity_detector.h" |
| 16 #include "ui/compositor/compositor.h" |
14 #include "ui/display/chromeos/display_configurator.h" | 17 #include "ui/display/chromeos/display_configurator.h" |
15 | 18 |
16 namespace ash { | 19 namespace ash { |
17 | 20 |
| 21 namespace { |
| 22 |
| 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(); |
| 30 } |
| 31 } |
| 32 |
| 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); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
18 PowerEventObserver::PowerEventObserver() | 42 PowerEventObserver::PowerEventObserver() |
19 : screen_locked_(false) { | 43 : screen_locked_(false), waiting_for_lock_screen_animations_(false) { |
20 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 44 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> |
21 AddObserver(this); | 45 AddObserver(this); |
22 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 46 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> |
23 AddObserver(this); | 47 AddObserver(this); |
24 } | 48 } |
25 | 49 |
26 PowerEventObserver::~PowerEventObserver() { | 50 PowerEventObserver::~PowerEventObserver() { |
27 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 51 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> |
28 RemoveObserver(this); | 52 RemoveObserver(this); |
29 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 53 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> |
30 RemoveObserver(this); | 54 RemoveObserver(this); |
31 } | 55 } |
32 | 56 |
| 57 void PowerEventObserver::OnLockAnimationsComplete() { |
| 58 VLOG(1) << "Screen locker animations have completed."; |
| 59 waiting_for_lock_screen_animations_ = false; |
| 60 |
| 61 if (!screen_lock_callback_.is_null()) { |
| 62 StopRenderingRequests(); |
| 63 |
| 64 screen_lock_callback_.Run(); |
| 65 screen_lock_callback_.Reset(); |
| 66 } |
| 67 } |
| 68 |
33 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) { | 69 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) { |
34 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged( | 70 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged( |
35 static_cast<double>(level)); | 71 static_cast<double>(level)); |
36 } | 72 } |
37 | 73 |
38 void PowerEventObserver::SuspendImminent() { | 74 void PowerEventObserver::SuspendImminent() { |
39 Shell* shell = Shell::GetInstance(); | 75 Shell* shell = Shell::GetInstance(); |
40 SessionStateDelegate* delegate = shell->session_state_delegate(); | 76 SessionStateDelegate* delegate = shell->session_state_delegate(); |
41 | 77 |
42 // If the lock-before-suspending pref is set, get a callback to block | 78 // This class is responsible for disabling all rendering requests at suspend |
43 // suspend and ask the session manager to lock the screen. | 79 // time and then enabling them at resume time. When the |
| 80 // lock-before-suspending pref is not set this is easy to do since |
| 81 // StopRenderingRequests() is just called directly from this function. If the |
| 82 // lock-before-suspending pref _is_ set, then the suspend needs to be delayed |
| 83 // until the lock screen is fully visible. While it is sufficient from a |
| 84 // security perspective to block only until the lock screen is ready, which |
| 85 // guarantees that the contents of the user's screen are no longer visible, |
| 86 // this leads to poor UX on the first resume since neither the user pod nor |
| 87 // the header bar will be visible for a few hundred milliseconds until the GPU |
| 88 // process starts rendering again. To deal with this, the suspend is delayed |
| 89 // until all the lock screen animations have completed and the suspend request |
| 90 // is unblocked from OnLockAnimationsComplete(). |
44 if (!screen_locked_ && delegate->ShouldLockScreenBeforeSuspending() && | 91 if (!screen_locked_ && delegate->ShouldLockScreenBeforeSuspending() && |
45 delegate->CanLockScreen()) { | 92 delegate->CanLockScreen()) { |
46 screen_lock_callback_ = chromeos::DBusThreadManager::Get()-> | 93 screen_lock_callback_ = chromeos::DBusThreadManager::Get()-> |
47 GetPowerManagerClient()->GetSuspendReadinessCallback(); | 94 GetPowerManagerClient()->GetSuspendReadinessCallback(); |
48 VLOG(1) << "Requesting screen lock from PowerEventObserver"; | 95 VLOG(1) << "Requesting screen lock from PowerEventObserver"; |
49 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 96 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> |
50 RequestLockScreen(); | 97 RequestLockScreen(); |
| 98 } else if (waiting_for_lock_screen_animations_) { |
| 99 // The lock-before-suspending pref has been set and the lock screen is ready |
| 100 // but the animations have not completed yet. This can happen if a suspend |
| 101 // request is canceled after the lock screen is ready but before the |
| 102 // animations have completed and then another suspend request is immediately |
| 103 // started. In practice, it is highly unlikely that this will ever happen |
| 104 // but it's better to be safe since the cost of not dealing with it properly |
| 105 // is a memory leak in the GPU and weird artifacts on the screen. |
| 106 screen_lock_callback_ = chromeos::DBusThreadManager::Get() |
| 107 ->GetPowerManagerClient() |
| 108 ->GetSuspendReadinessCallback(); |
| 109 } else { |
| 110 // The lock-before-suspending pref is not set or the screen has already been |
| 111 // locked and the animations have completed. Rendering can be stopped now. |
| 112 StopRenderingRequests(); |
51 } | 113 } |
52 | 114 |
53 ui::UserActivityDetector::Get()->OnDisplayPowerChanging(); | 115 ui::UserActivityDetector::Get()->OnDisplayPowerChanging(); |
54 shell->display_configurator()->SuspendDisplays(); | 116 shell->display_configurator()->SuspendDisplays(); |
55 } | 117 } |
56 | 118 |
57 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) { | 119 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) { |
58 Shell::GetInstance()->display_configurator()->ResumeDisplays(); | 120 Shell::GetInstance()->display_configurator()->ResumeDisplays(); |
59 Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock(); | 121 Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock(); |
| 122 |
| 123 // If the suspend request was being blocked while waiting for the lock |
| 124 // animation to complete, clear the blocker since the suspend has already |
| 125 // completed. This prevents rendering requests from being blocked after a |
| 126 // resume if the lock screen took too long to show. |
| 127 screen_lock_callback_.Reset(); |
| 128 |
| 129 ResumeRenderingRequests(); |
60 } | 130 } |
61 | 131 |
62 void PowerEventObserver::ScreenIsLocked() { | 132 void PowerEventObserver::ScreenIsLocked() { |
63 screen_locked_ = true; | 133 screen_locked_ = true; |
| 134 waiting_for_lock_screen_animations_ = true; |
64 | 135 |
65 // Stop blocking suspend after the screen is locked. | 136 // The screen is now locked but the pending suspend, if any, will be blocked |
| 137 // until all the animations have completed. |
66 if (!screen_lock_callback_.is_null()) { | 138 if (!screen_lock_callback_.is_null()) { |
67 VLOG(1) << "Screen locked due to suspend"; | 139 VLOG(1) << "Screen locked due to suspend"; |
68 // Run the callback asynchronously. ScreenIsLocked() is currently | |
69 // called asynchronously after RequestLockScreen(), but this guards | |
70 // against it being made synchronous later. | |
71 base::MessageLoop::current()->PostTask(FROM_HERE, screen_lock_callback_); | |
72 screen_lock_callback_.Reset(); | |
73 } else { | 140 } else { |
74 VLOG(1) << "Screen locked without suspend"; | 141 VLOG(1) << "Screen locked without suspend"; |
75 } | 142 } |
76 } | 143 } |
77 | 144 |
78 void PowerEventObserver::ScreenIsUnlocked() { | 145 void PowerEventObserver::ScreenIsUnlocked() { |
79 screen_locked_ = false; | 146 screen_locked_ = false; |
80 } | 147 } |
81 | 148 |
82 } // namespace ash | 149 } // namespace ash |
OLD | NEW |