Chromium Code Reviews| 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/sys_info.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "ui/display/chromeos/display_configurator.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Specifies how long the display change should have been disabled | |
| 19 // after each display change operations. | |
| 20 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid | |
| 21 // changing the settings while the system is still configurating | |
| 22 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs| | |
| 23 // when the display change happens, so the actual timeout is much shorter. | |
| 24 const int64_t kAfterDisplayChangeThrottleTimeoutMs = 500; | |
| 25 const int64_t kCycleDisplayThrottleTimeoutMs = 4000; | |
| 26 const int64_t kSetPrimaryDisplayThrottleTimeoutMs = 500; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace ash { | |
| 31 | |
| 32 class DisplayConfigurationController::DisplayChangeLimiter { | |
| 33 public: | |
| 34 DisplayChangeLimiter() : throttle_timeout_(base::Time::Now()) {} | |
| 35 | |
| 36 void SetThrottleTimeout(int64_t throttle_ms) { | |
| 37 throttle_timeout_ = | |
| 38 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms); | |
| 39 } | |
| 40 | |
| 41 bool IsThrottled() const { return base::Time::Now() < throttle_timeout_; } | |
| 42 | |
| 43 private: | |
| 44 base::Time throttle_timeout_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); | |
| 47 }; | |
| 48 | |
| 49 DisplayConfigurationController::DisplayConfigurationController( | |
| 50 ui::DisplayConfigurator* display_configurator, | |
| 51 DisplayManager* display_manager, | |
| 52 WindowTreeHostManager* window_tree_host_manager) | |
| 53 : display_configurator_(display_configurator), | |
| 54 display_manager_(display_manager), | |
| 55 window_tree_host_manager_(window_tree_host_manager), | |
| 56 display_animator_(new DisplayAnimator), | |
| 57 weak_ptr_factory_(this) { | |
| 58 if (base::SysInfo::IsRunningOnChromeOS()) | |
| 59 limiter_.reset(new DisplayChangeLimiter); | |
| 60 display_configurator_->AddObserver(display_animator_.get()); | |
| 61 window_tree_host_manager_->AddObserver(this); | |
| 62 } | |
| 63 | |
| 64 DisplayConfigurationController::~DisplayConfigurationController() { | |
| 65 window_tree_host_manager_->RemoveObserver(this); | |
| 66 display_configurator_->RemoveObserver(display_animator_.get()); | |
|
oshima
2016/01/19 18:49:16
can we move this (add/remove) code to animator?
stevenjb
2016/01/19 20:32:04
I'm not sure I understand. This class now owns the
oshima
2016/01/19 20:59:30
DisplayConfigurator is used only for animator, so
stevenjb
2016/01/20 01:25:36
Ah, I think I understand what you mean. I did a bi
| |
| 67 } | |
| 68 | |
| 69 void DisplayConfigurationController::SetDisplayLayout( | |
| 70 int64_t display_id, | |
| 71 const DisplayLayout& layout, | |
| 72 bool user_action) { | |
| 73 if (user_action && display_animator_) { | |
| 74 display_animator_->StartFadeOutAnimation( | |
| 75 base::Bind(&DisplayConfigurationController::SetLayoutImpl, | |
| 76 weak_ptr_factory_.GetWeakPtr(), display_id, layout)); | |
| 77 } else { | |
| 78 SetLayoutImpl(display_id, layout); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void DisplayConfigurationController::SetMirrorMode(bool mirror, | |
| 83 bool user_action) { | |
| 84 if (display_manager_->GetNumDisplays() <= 1 || | |
| 85 display_manager_->IsInMirrorMode() == mirror || IsLimited()) { | |
| 86 return; | |
| 87 } | |
| 88 if (user_action && display_animator_) { | |
| 89 display_animator_->StartFadeOutAnimation( | |
| 90 base::Bind(&DisplayConfigurationController::SetMirrorModeImpl, | |
| 91 weak_ptr_factory_.GetWeakPtr(), mirror)); | |
| 92 } else { | |
| 93 SetMirrorModeImpl(mirror); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void DisplayConfigurationController::SetDisplayRotation( | |
| 98 int64_t display_id, | |
| 99 gfx::Display::Rotation rotation, | |
| 100 gfx::Display::RotationSource source, | |
| 101 bool user_action) { | |
| 102 ash::ScreenRotationAnimator screen_rotation_animator(display_id); | |
| 103 if (user_action && screen_rotation_animator.CanAnimate()) | |
| 104 screen_rotation_animator.Rotate(rotation, source); | |
| 105 else | |
| 106 display_manager_->SetDisplayRotation(display_id, rotation, source); | |
| 107 } | |
| 108 | |
| 109 void DisplayConfigurationController::SetPrimaryDisplayId(int64_t display_id, | |
| 110 bool user_action) { | |
| 111 if (display_manager_->GetNumDisplays() <= 1 || IsLimited()) | |
| 112 return; | |
| 113 | |
| 114 if (user_action && display_animator_) { | |
| 115 display_animator_->StartFadeOutAnimation( | |
| 116 base::Bind(&DisplayConfigurationController::SetPrimaryDisplayIdImpl, | |
| 117 weak_ptr_factory_.GetWeakPtr(), display_id)); | |
| 118 } else { | |
| 119 SetPrimaryDisplayIdImpl(display_id); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void DisplayConfigurationController::OnDisplayConfigurationChanged() { | |
| 124 if (limiter_) | |
| 125 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs); | |
| 126 } | |
| 127 | |
| 128 // Protected | |
| 129 | |
| 130 void DisplayConfigurationController::ResetAnimatorForTest() { | |
| 131 if (!display_animator_) | |
| 132 return; | |
| 133 display_configurator_->RemoveObserver(display_animator_.get()); | |
| 134 display_animator_.reset(); | |
| 135 } | |
| 136 | |
| 137 // Private | |
| 138 | |
| 139 bool DisplayConfigurationController::IsLimited() { | |
| 140 return limiter_ && limiter_->IsThrottled(); | |
| 141 } | |
| 142 | |
| 143 void DisplayConfigurationController::SetLayoutImpl( | |
| 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 |