OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "ash/display/display_configuration_controller.h" |
| 6 |
| 7 #include "ash/display/display_animator.h" |
| 8 #include "ash/display/display_layout.h" |
| 9 #include "ash/display/display_manager.h" |
| 10 #include "ash/rotator/screen_rotation_animator.h" |
| 11 #include "ash/screen_util.h" |
| 12 #include "base/time/time.h" |
| 13 |
| 14 #if defined(OS_CHROMEOS) |
| 15 #include "ash/display/display_animator_chromeos.h" |
| 16 #include "base/sys_info.h" |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Specifies how long the display change should have been disabled |
| 22 // after each display change operations. |
| 23 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid |
| 24 // changing the settings while the system is still configurating |
| 25 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs| |
| 26 // when the display change happens, so the actual timeout is much shorter. |
| 27 const int64_t kAfterDisplayChangeThrottleTimeoutMs = 500; |
| 28 const int64_t kCycleDisplayThrottleTimeoutMs = 4000; |
| 29 const int64_t kSetPrimaryDisplayThrottleTimeoutMs = 500; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 namespace ash { |
| 34 |
| 35 class DisplayConfigurationController::DisplayChangeLimiter { |
| 36 public: |
| 37 DisplayChangeLimiter() : throttle_timeout_(base::Time::Now()) {} |
| 38 |
| 39 void SetThrottleTimeout(int64_t throttle_ms) { |
| 40 throttle_timeout_ = |
| 41 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms); |
| 42 } |
| 43 |
| 44 bool IsThrottled() const { return base::Time::Now() < throttle_timeout_; } |
| 45 |
| 46 private: |
| 47 base::Time throttle_timeout_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); |
| 50 }; |
| 51 |
| 52 DisplayConfigurationController::DisplayConfigurationController( |
| 53 DisplayManager* display_manager, |
| 54 WindowTreeHostManager* window_tree_host_manager) |
| 55 : display_manager_(display_manager), |
| 56 window_tree_host_manager_(window_tree_host_manager), |
| 57 weak_ptr_factory_(this) { |
| 58 window_tree_host_manager_->AddObserver(this); |
| 59 #if defined(OS_CHROMEOS) |
| 60 if (base::SysInfo::IsRunningOnChromeOS()) |
| 61 limiter_.reset(new DisplayChangeLimiter); |
| 62 display_animator_.reset(new DisplayAnimatorChromeOS()); |
| 63 #endif |
| 64 } |
| 65 |
| 66 DisplayConfigurationController::~DisplayConfigurationController() { |
| 67 window_tree_host_manager_->RemoveObserver(this); |
| 68 } |
| 69 |
| 70 void DisplayConfigurationController::SetDisplayLayout( |
| 71 int64_t display_id, |
| 72 const DisplayLayout& layout, |
| 73 bool user_action) { |
| 74 if (user_action && display_animator_) { |
| 75 display_animator_->StartFadeOutAnimation( |
| 76 base::Bind(&DisplayConfigurationController::SetDisplayLayoutImpl, |
| 77 weak_ptr_factory_.GetWeakPtr(), display_id, layout)); |
| 78 } else { |
| 79 SetDisplayLayoutImpl(display_id, layout); |
| 80 } |
| 81 } |
| 82 |
| 83 void DisplayConfigurationController::SetMirrorMode(bool mirror, |
| 84 bool user_action) { |
| 85 if (display_manager_->GetNumDisplays() <= 1 || |
| 86 display_manager_->IsInMirrorMode() == mirror || IsLimited()) { |
| 87 return; |
| 88 } |
| 89 if (user_action && display_animator_) { |
| 90 display_animator_->StartFadeOutAnimation( |
| 91 base::Bind(&DisplayConfigurationController::SetMirrorModeImpl, |
| 92 weak_ptr_factory_.GetWeakPtr(), mirror)); |
| 93 } else { |
| 94 SetMirrorModeImpl(mirror); |
| 95 } |
| 96 } |
| 97 |
| 98 void DisplayConfigurationController::SetDisplayRotation( |
| 99 int64_t display_id, |
| 100 gfx::Display::Rotation rotation, |
| 101 gfx::Display::RotationSource source, |
| 102 bool user_action) { |
| 103 ash::ScreenRotationAnimator screen_rotation_animator(display_id); |
| 104 if (user_action && screen_rotation_animator.CanAnimate()) |
| 105 screen_rotation_animator.Rotate(rotation, source); |
| 106 else |
| 107 display_manager_->SetDisplayRotation(display_id, rotation, source); |
| 108 } |
| 109 |
| 110 void DisplayConfigurationController::SetPrimaryDisplayId(int64_t display_id, |
| 111 bool user_action) { |
| 112 if (display_manager_->GetNumDisplays() <= 1 || IsLimited()) |
| 113 return; |
| 114 |
| 115 if (user_action && display_animator_) { |
| 116 display_animator_->StartFadeOutAnimation( |
| 117 base::Bind(&DisplayConfigurationController::SetPrimaryDisplayIdImpl, |
| 118 weak_ptr_factory_.GetWeakPtr(), display_id)); |
| 119 } else { |
| 120 SetPrimaryDisplayIdImpl(display_id); |
| 121 } |
| 122 } |
| 123 |
| 124 void DisplayConfigurationController::OnDisplayConfigurationChanged() { |
| 125 if (limiter_) |
| 126 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs); |
| 127 } |
| 128 |
| 129 // Protected |
| 130 |
| 131 void DisplayConfigurationController::ResetAnimatorForTest() { |
| 132 if (!display_animator_) |
| 133 return; |
| 134 display_animator_.reset(); |
| 135 } |
| 136 |
| 137 // Private |
| 138 |
| 139 bool DisplayConfigurationController::IsLimited() { |
| 140 return limiter_ && limiter_->IsThrottled(); |
| 141 } |
| 142 |
| 143 void DisplayConfigurationController::SetDisplayLayoutImpl( |
| 144 int64_t display_id, |
| 145 const DisplayLayout& layout) { |
| 146 // TODO(oshima/stevenjb): Add support for 3+ displays. |
| 147 display_manager_->SetLayoutForCurrentDisplays(layout); |
| 148 if (display_animator_) |
| 149 display_animator_->StartFadeInAnimation(); |
| 150 } |
| 151 |
| 152 void DisplayConfigurationController::SetMirrorModeImpl(bool mirror) { |
| 153 display_manager_->SetMirrorMode(mirror); |
| 154 if (limiter_) |
| 155 limiter_->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs); |
| 156 if (display_animator_) |
| 157 display_animator_->StartFadeInAnimation(); |
| 158 } |
| 159 |
| 160 void DisplayConfigurationController::SetPrimaryDisplayIdImpl( |
| 161 int64_t display_id) { |
| 162 window_tree_host_manager_->SetPrimaryDisplayId(display_id); |
| 163 if (limiter_) |
| 164 limiter_->SetThrottleTimeout(kSetPrimaryDisplayThrottleTimeoutMs); |
| 165 if (display_animator_) |
| 166 display_animator_->StartFadeInAnimation(); |
| 167 } |
| 168 |
| 169 } // namespace ash |
OLD | NEW |