| 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/common/session/session_state_delegate.h" | 7 #include "ash/common/session/session_state_delegate.h" |
| 8 #include "ash/common/system/tray/system_tray_notifier.h" | 8 #include "ash/common/system/tray/system_tray_notifier.h" |
| 9 #include "ash/common/wm_shell.h" | 9 #include "ash/common/wm_shell.h" |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 11 #include "ash/wm/power_button_controller.h" | 11 #include "ash/wm/power_button_controller.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
| 14 #include "ui/aura/window_tree_host.h" | 15 #include "ui/aura/window_tree_host.h" |
| 15 #include "ui/base/user_activity/user_activity_detector.h" | 16 #include "ui/base/user_activity/user_activity_detector.h" |
| 16 #include "ui/compositor/compositor.h" | 17 #include "ui/compositor/compositor.h" |
| 17 #include "ui/display/chromeos/display_configurator.h" | 18 #include "ui/display/chromeos/display_configurator.h" |
| 18 | 19 |
| 19 namespace ash { | 20 namespace ash { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Tells the compositor for each of the displays to finish all pending | 24 // Tells the compositor for each of the displays to finish all pending |
| 24 // rendering requests and block any new ones. | 25 // rendering requests and block any new ones. |
| 25 void StopRenderingRequests() { | 26 void StopRenderingRequests() { |
| 26 for (aura::Window* window : Shell::GetAllRootWindows()) { | 27 for (aura::Window* window : Shell::GetAllRootWindows()) { |
| 27 ui::Compositor* compositor = window->GetHost()->compositor(); | 28 ui::Compositor* compositor = window->GetHost()->compositor(); |
| 28 compositor->SetVisible(false); | 29 compositor->SetVisible(false); |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 | 32 |
| 32 // Tells the compositor for each of the displays to resume sending rendering | 33 // Tells the compositor for each of the displays to resume sending rendering |
| 33 // requests to the GPU. | 34 // requests to the GPU. |
| 34 void ResumeRenderingRequests() { | 35 void ResumeRenderingRequests() { |
| 35 for (aura::Window* window : Shell::GetAllRootWindows()) | 36 for (aura::Window* window : Shell::GetAllRootWindows()) |
| 36 window->GetHost()->compositor()->SetVisible(true); | 37 window->GetHost()->compositor()->SetVisible(true); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void OnSuspendDisplaysCompleted(const base::Closure& suspend_callback, | |
| 40 bool status) { | |
| 41 suspend_callback.Run(); | |
| 42 } | |
| 43 | |
| 44 } // namespace | 40 } // namespace |
| 45 | 41 |
| 46 PowerEventObserver::PowerEventObserver() | 42 PowerEventObserver::PowerEventObserver() |
| 47 : screen_locked_(false), waiting_for_lock_screen_animations_(false) { | 43 : screen_locked_(false), |
| 44 waiting_for_lock_screen_animations_(false), |
| 45 suspend_displays_in_progress_(false), |
| 46 should_undo_suspend_displays_(false), |
| 47 weak_ptr_factory_(this) { |
| 48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 49 this); | 49 this); |
| 50 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver( | 50 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver( |
| 51 this); | 51 this); |
| 52 } | 52 } |
| 53 | 53 |
| 54 PowerEventObserver::~PowerEventObserver() { | 54 PowerEventObserver::~PowerEventObserver() { |
| 55 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 55 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 56 this); | 56 this); |
| 57 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver( | 57 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // is a memory leak in the GPU and weird artifacts on the screen. | 110 // is a memory leak in the GPU and weird artifacts on the screen. |
| 111 screen_lock_callback_ = chromeos::DBusThreadManager::Get() | 111 screen_lock_callback_ = chromeos::DBusThreadManager::Get() |
| 112 ->GetPowerManagerClient() | 112 ->GetPowerManagerClient() |
| 113 ->GetSuspendReadinessCallback(); | 113 ->GetSuspendReadinessCallback(); |
| 114 } else { | 114 } else { |
| 115 // The lock-before-suspending pref is not set or the screen has already been | 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. | 116 // locked and the animations have completed. Rendering can be stopped now. |
| 117 StopRenderingRequests(); | 117 StopRenderingRequests(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Ignore mouse events that might be generated by suspending the displays. |
| 120 ui::UserActivityDetector::Get()->OnDisplayPowerChanging(); | 121 ui::UserActivityDetector::Get()->OnDisplayPowerChanging(); |
| 121 Shell::GetInstance()->display_configurator()->SuspendDisplays(base::Bind( | 122 |
| 122 &OnSuspendDisplaysCompleted, chromeos::DBusThreadManager::Get() | 123 should_undo_suspend_displays_ = false; |
| 123 ->GetPowerManagerClient() | 124 undo_suspend_displays_callback_.Cancel(); |
| 124 ->GetSuspendReadinessCallback())); | 125 if (!suspend_displays_in_progress_) { |
| 126 suspend_displays_in_progress_ = true; |
| 127 Shell::GetInstance()->display_configurator()->SuspendDisplays(base::Bind( |
| 128 &PowerEventObserver::OnSuspendDisplaysCompleted, |
| 129 weak_ptr_factory_.GetWeakPtr(), chromeos::DBusThreadManager::Get() |
| 130 ->GetPowerManagerClient() |
| 131 ->GetSuspendReadinessCallback())); |
| 132 } |
| 133 } |
| 134 |
| 135 void PowerEventObserver::ResumeDisplays() { |
| 136 // If there's an async display-suspending task running, wait until it finishes |
| 137 // to resume the displays. |
| 138 if (suspend_displays_in_progress_) { |
| 139 should_undo_suspend_displays_ = true; |
| 140 return; |
| 141 } |
| 142 |
| 143 Shell::GetInstance()->display_configurator()->ResumeDisplays(); |
| 125 } | 144 } |
| 126 | 145 |
| 127 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) { | 146 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) { |
| 128 Shell::GetInstance()->display_configurator()->ResumeDisplays(); | 147 ResumeDisplays(); |
| 129 WmShell::Get()->system_tray_notifier()->NotifyRefreshClock(); | 148 WmShell::Get()->system_tray_notifier()->NotifyRefreshClock(); |
| 130 | 149 |
| 131 // If the suspend request was being blocked while waiting for the lock | 150 // If the suspend request was being blocked while waiting for the lock |
| 132 // animation to complete, clear the blocker since the suspend has already | 151 // animation to complete, clear the blocker since the suspend has already |
| 133 // completed. This prevents rendering requests from being blocked after a | 152 // completed. This prevents rendering requests from being blocked after a |
| 134 // resume if the lock screen took too long to show. | 153 // resume if the lock screen took too long to show. |
| 135 screen_lock_callback_.Reset(); | 154 screen_lock_callback_.Reset(); |
| 136 | 155 |
| 137 ResumeRenderingRequests(); | 156 ResumeRenderingRequests(); |
| 138 } | 157 } |
| 139 | 158 |
| 140 void PowerEventObserver::ScreenIsLocked() { | 159 void PowerEventObserver::ScreenIsLocked() { |
| 141 screen_locked_ = true; | 160 screen_locked_ = true; |
| 142 waiting_for_lock_screen_animations_ = true; | 161 waiting_for_lock_screen_animations_ = true; |
| 143 | 162 |
| 144 // The screen is now locked but the pending suspend, if any, will be blocked | 163 // The screen is now locked but the pending suspend, if any, will be blocked |
| 145 // until all the animations have completed. | 164 // until all the animations have completed. |
| 146 if (!screen_lock_callback_.is_null()) { | 165 if (!screen_lock_callback_.is_null()) { |
| 147 VLOG(1) << "Screen locked due to suspend"; | 166 VLOG(1) << "Screen locked due to suspend"; |
| 148 } else { | 167 } else { |
| 149 VLOG(1) << "Screen locked without suspend"; | 168 VLOG(1) << "Screen locked without suspend"; |
| 150 } | 169 } |
| 151 } | 170 } |
| 152 | 171 |
| 153 void PowerEventObserver::ScreenIsUnlocked() { | 172 void PowerEventObserver::ScreenIsUnlocked() { |
| 154 screen_locked_ = false; | 173 screen_locked_ = false; |
| 155 } | 174 } |
| 156 | 175 |
| 176 void PowerEventObserver::OnSuspendDisplaysCompleted( |
| 177 const base::Closure& suspend_callback, |
| 178 bool status) { |
| 179 suspend_displays_in_progress_ = false; |
| 180 |
| 181 // If the suspend attempt completed while we were in the process of suspending |
| 182 // displays, we need to resume them now. This needs to happen asynchronously, |
| 183 // as ui::DisplayConfigurator doesn't support having new configure requests |
| 184 // submitted while it's running callbacks from an earlier configure request. |
| 185 if (should_undo_suspend_displays_) { |
| 186 should_undo_suspend_displays_ = false; |
| 187 undo_suspend_displays_callback_.Reset(base::Bind( |
| 188 &PowerEventObserver::ResumeDisplays, weak_ptr_factory_.GetWeakPtr())); |
| 189 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 190 FROM_HERE, undo_suspend_displays_callback_.callback()); |
| 191 } |
| 192 |
| 193 suspend_callback.Run(); |
| 194 } |
| 195 |
| 157 } // namespace ash | 196 } // namespace ash |
| OLD | NEW |