1 // Copyright (c) 2012 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/display/display_controller.h"
11 #include "ash/ash_switches.h"
12 #include "ash/display/cursor_window_controller.h"
13 #include "ash/display/display_layout_store.h"
14 #include "ash/display/display_manager.h"
15 #include "ash/display/mirror_window_controller.h"
16 #include "ash/display/root_window_transformers.h"
17 #include "ash/host/ash_window_tree_host.h"
18 #include "ash/host/ash_window_tree_host_init_params.h"
19 #include "ash/host/root_window_transformer.h"
20 #include "ash/magnifier/magnification_controller.h"
21 #include "ash/magnifier/partial_magnification_controller.h"
22 #include "ash/root_window_controller.h"
23 #include "ash/root_window_settings.h"
24 #include "ash/screen_util.h"
25 #include "ash/shell.h"
26 #include "ash/shell_delegate.h"
27 #include "ash/system/tray/system_tray.h"
28 #include "ash/wm/coordinate_conversion.h"
29 #include "base/command_line.h"
30 #include "base/stl_util.h"
31 #include "base/strings/stringprintf.h"
32 #include "base/strings/utf_string_conversions.h"
33 #include "ui/aura/client/capture_client.h"
34 #include "ui/aura/client/focus_client.h"
35 #include "ui/aura/client/screen_position_client.h"
36 #include "ui/aura/window.h"
37 #include "ui/aura/window_event_dispatcher.h"
38 #include "ui/aura/window_property.h"
39 #include "ui/aura/window_tracker.h"
40 #include "ui/aura/window_tree_host.h"
41 #include "ui/compositor/compositor.h"
42 #include "ui/compositor/compositor_vsync_manager.h"
43 #include "ui/gfx/display.h"
44 #include "ui/gfx/screen.h"
45 #include "ui/wm/core/coordinate_conversion.h"
46 #include "ui/wm/public/activation_client.h"
48 #if defined(OS_CHROMEOS)
49 #include "ash/display/display_configurator_animation.h"
50 #include "base/sys_info.h"
51 #include "base/time/time.h"
52 #endif // defined(OS_CHROMEOS)
55 #include "ui/base/x/x11_util.h"
56 #include "ui/gfx/x/x11_types.h"
58 // Including this at the bottom to avoid other
59 // potential conflict with chrome headers.
60 #include <X11/extensions/Xrandr.h>
62 #endif // defined(USE_X11)
64 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
65 #include "ui/events/ozone/chromeos/cursor_controller.h"
71 // Primary display stored in global object as it can be
72 // accessed after Shell is deleted. A separate display instance is created
73 // during the shutdown instead of always keeping two display instances
74 // (one here and another one in display_manager) in sync, which is error prone.
75 // This is initialized in the constructor, and then in CreatePrimaryHost().
76 int64 primary_display_id
= -1;
78 // Specifies how long the display change should have been disabled
79 // after each display change operations.
80 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid
81 // changing the settings while the system is still configurating
82 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs|
83 // when the display change happens, so the actual timeout is much shorter.
84 const int64 kAfterDisplayChangeThrottleTimeoutMs
= 500;
85 const int64 kCycleDisplayThrottleTimeoutMs
= 4000;
86 const int64 kSwapDisplayThrottleTimeoutMs
= 500;
88 #if defined(USE_OZONE) && defined(OS_CHROMEOS)
89 // Add 20% more cursor motion on non-integrated displays.
90 const float kCursorMultiplierForExternalDisplays
= 1.2f
;
93 DisplayManager
* GetDisplayManager() {
94 return Shell::GetInstance()->display_manager();
97 void SetDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
,
98 const gfx::Display
& display
) {
99 DisplayInfo info
= GetDisplayManager()->GetDisplayInfo(display
.id());
100 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
101 #if defined(OS_CHROMEOS)
103 // Native window property (Atom in X11) that specifies the display's
104 // rotation, scale factor and if it's internal display. They are
105 // read and used by touchpad/mouse driver directly on X (contact
106 // adlr@ for more details on touchpad/mouse driver side). The value
107 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2
108 // (180 degree) or 3 (270 degrees clockwise). The value of the
109 // scale factor is in percent (100, 140, 200 etc).
110 const char kRotationProp
[] = "_CHROME_DISPLAY_ROTATION";
111 const char kScaleFactorProp
[] = "_CHROME_DISPLAY_SCALE_FACTOR";
112 const char kInternalProp
[] = "_CHROME_DISPLAY_INTERNAL";
113 const char kCARDINAL
[] = "CARDINAL";
114 int xrandr_rotation
= RR_Rotate_0
;
115 switch (info
.GetActiveRotation()) {
116 case gfx::Display::ROTATE_0
:
117 xrandr_rotation
= RR_Rotate_0
;
119 case gfx::Display::ROTATE_90
:
120 xrandr_rotation
= RR_Rotate_90
;
122 case gfx::Display::ROTATE_180
:
123 xrandr_rotation
= RR_Rotate_180
;
125 case gfx::Display::ROTATE_270
:
126 xrandr_rotation
= RR_Rotate_270
;
130 int internal
= display
.IsInternal() ? 1 : 0;
131 gfx::AcceleratedWidget xwindow
= host
->GetAcceleratedWidget();
132 ui::SetIntProperty(xwindow
, kInternalProp
, kCARDINAL
, internal
);
133 ui::SetIntProperty(xwindow
, kRotationProp
, kCARDINAL
, xrandr_rotation
);
134 ui::SetIntProperty(xwindow
,
137 100 * display
.device_scale_factor());
138 #elif defined(USE_OZONE)
139 // Scale all motion on High-DPI displays.
140 float scale
= display
.device_scale_factor();
142 if (!display
.IsInternal())
143 scale
*= kCursorMultiplierForExternalDisplays
;
145 ui::CursorController::GetInstance()->SetCursorConfigForWindow(
146 host
->GetAcceleratedWidget(), info
.GetActiveRotation(), scale
);
149 scoped_ptr
<RootWindowTransformer
> transformer(
150 CreateRootWindowTransformerForDisplay(host
->window(), display
));
151 ash_host
->SetRootWindowTransformer(transformer
.Pass());
154 GetDisplayManager()->GetActiveModeForDisplayId(display
.id());
155 if (mode
.refresh_rate
> 0.0f
) {
156 host
->compositor()->vsync_manager()->SetAuthoritativeVSyncInterval(
157 base::TimeDelta::FromMicroseconds(
158 base::Time::kMicrosecondsPerSecond
/ mode
.refresh_rate
));
161 // Just movnig the display requires the full redraw.
162 // chrome-os-partner:33558.
163 host
->compositor()->ScheduleFullRedraw();
166 void ClearDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
) {
167 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
168 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
169 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
170 host
->GetAcceleratedWidget());
174 aura::Window
* GetWindow(AshWindowTreeHost
* ash_host
) {
175 CHECK(ash_host
->AsWindowTreeHost());
176 return ash_host
->AsWindowTreeHost()->window();
181 // A utility class to store/restore focused/active window
182 // when the display configuration has changed.
183 class FocusActivationStore
{
185 FocusActivationStore()
186 : activation_client_(nullptr),
187 capture_client_(nullptr),
188 focus_client_(nullptr),
192 void Store(bool clear_focus
) {
193 if (!activation_client_
) {
194 aura::Window
* root
= Shell::GetPrimaryRootWindow();
195 activation_client_
= aura::client::GetActivationClient(root
);
196 capture_client_
= aura::client::GetCaptureClient(root
);
197 focus_client_
= aura::client::GetFocusClient(root
);
199 focused_
= focus_client_
->GetFocusedWindow();
201 tracker_
.Add(focused_
);
202 active_
= activation_client_
->GetActiveWindow();
203 if (active_
&& focused_
!= active_
)
204 tracker_
.Add(active_
);
206 // Deactivate the window to close menu / bubble windows.
208 activation_client_
->DeactivateWindow(active_
);
210 // Release capture if any.
211 capture_client_
->SetCapture(nullptr);
212 // Clear the focused window if any. This is necessary because a
213 // window may be deleted when losing focus (fullscreen flash for
214 // example). If the focused window is still alive after move, it'll
215 // be re-focused below.
217 focus_client_
->FocusWindow(nullptr);
221 // Restore focused or active window if it's still alive.
222 if (focused_
&& tracker_
.Contains(focused_
)) {
223 focus_client_
->FocusWindow(focused_
);
224 } else if (active_
&& tracker_
.Contains(active_
)) {
225 activation_client_
->ActivateWindow(active_
);
228 tracker_
.Remove(focused_
);
230 tracker_
.Remove(active_
);
236 aura::client::ActivationClient
* activation_client_
;
237 aura::client::CaptureClient
* capture_client_
;
238 aura::client::FocusClient
* focus_client_
;
239 aura::WindowTracker tracker_
;
240 aura::Window
* focused_
;
241 aura::Window
* active_
;
243 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore
);
246 ////////////////////////////////////////////////////////////////////////////////
247 // DisplayChangeLimiter
249 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
250 : throttle_timeout_(base::Time::Now()) {
253 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
256 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms
);
259 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
260 return base::Time::Now() < throttle_timeout_
;
263 ////////////////////////////////////////////////////////////////////////////////
266 DisplayController::DisplayController()
267 : primary_tree_host_for_replace_(nullptr),
268 focus_activation_store_(new FocusActivationStore()),
269 cursor_window_controller_(new CursorWindowController()),
270 mirror_window_controller_(new MirrorWindowController()),
271 cursor_display_id_for_restore_(gfx::Display::kInvalidDisplayID
),
272 weak_ptr_factory_(this) {
273 #if defined(OS_CHROMEOS)
274 if (base::SysInfo::IsRunningOnChromeOS())
275 limiter_
.reset(new DisplayChangeLimiter
);
277 // Reset primary display to make sure that tests don't use
278 // stale display info from previous tests.
279 primary_display_id
= gfx::Display::kInvalidDisplayID
;
282 DisplayController::~DisplayController() {
285 void DisplayController::Start() {
286 Shell::GetScreen()->AddObserver(this);
287 Shell::GetInstance()->display_manager()->set_delegate(this);
290 void DisplayController::Shutdown() {
291 // Unset the display manager's delegate here because
292 // DisplayManager outlives DisplayController.
293 Shell::GetInstance()->display_manager()->set_delegate(nullptr);
295 cursor_window_controller_
.reset();
296 mirror_window_controller_
.reset();
298 Shell::GetScreen()->RemoveObserver(this);
300 int64 primary_id
= Shell::GetScreen()->GetPrimaryDisplay().id();
302 // Delete non primary root window controllers first, then
303 // delete the primary root window controller.
304 aura::Window::Windows root_windows
= DisplayController::GetAllRootWindows();
305 std::vector
<RootWindowController
*> to_delete
;
306 RootWindowController
* primary_rwc
= nullptr;
307 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
308 iter
!= root_windows
.end();
310 RootWindowController
* rwc
= GetRootWindowController(*iter
);
311 if (GetRootWindowSettings(*iter
)->display_id
== primary_id
)
314 to_delete
.push_back(rwc
);
318 STLDeleteElements(&to_delete
);
322 void DisplayController::CreatePrimaryHost(
323 const AshWindowTreeHostInitParams
& init_params
) {
324 const gfx::Display
& primary_candidate
=
325 GetDisplayManager()->GetPrimaryDisplayCandidate();
326 primary_display_id
= primary_candidate
.id();
327 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
328 AddWindowTreeHostForDisplay(primary_candidate
, init_params
);
331 void DisplayController::InitDisplays() {
332 RootWindowController::CreateForPrimaryDisplay(
333 window_tree_hosts_
[primary_display_id
]);
334 DisplayManager
* display_manager
= GetDisplayManager();
335 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
336 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
337 if (primary_display_id
!= display
.id()) {
338 AshWindowTreeHost
* ash_host
= AddWindowTreeHostForDisplay(
339 display
, AshWindowTreeHostInitParams());
340 RootWindowController::CreateForSecondaryDisplay(ash_host
);
344 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplaysInitialized());
347 void DisplayController::AddObserver(Observer
* observer
) {
348 observers_
.AddObserver(observer
);
351 void DisplayController::RemoveObserver(Observer
* observer
) {
352 observers_
.RemoveObserver(observer
);
356 int64
DisplayController::GetPrimaryDisplayId() {
357 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
358 return primary_display_id
;
361 aura::Window
* DisplayController::GetPrimaryRootWindow() {
362 return GetRootWindowForDisplayId(primary_display_id
);
365 aura::Window
* DisplayController::GetRootWindowForDisplayId(int64 id
) {
366 AshWindowTreeHost
* host
= GetAshWindowTreeHostForDisplayId(id
);
368 return GetWindow(host
);
371 AshWindowTreeHost
* DisplayController::GetAshWindowTreeHostForDisplayId(
373 CHECK_EQ(1u, window_tree_hosts_
.count(display_id
))
374 << "display id = " << display_id
;
375 return window_tree_hosts_
[display_id
];
378 void DisplayController::CloseChildWindows() {
379 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
380 it
!= window_tree_hosts_
.end();
382 aura::Window
* root_window
= GetWindow(it
->second
);
383 RootWindowController
* controller
= GetRootWindowController(root_window
);
385 controller
->CloseChildWindows();
387 while (!root_window
->children().empty()) {
388 aura::Window
* child
= root_window
->children()[0];
395 aura::Window::Windows
DisplayController::GetAllRootWindows() {
396 aura::Window::Windows windows
;
397 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
398 it
!= window_tree_hosts_
.end();
401 if (GetRootWindowController(GetWindow(it
->second
)))
402 windows
.push_back(GetWindow(it
->second
));
407 gfx::Insets
DisplayController::GetOverscanInsets(int64 display_id
) const {
408 return GetDisplayManager()->GetOverscanInsets(display_id
);
411 void DisplayController::SetOverscanInsets(int64 display_id
,
412 const gfx::Insets
& insets_in_dip
) {
413 GetDisplayManager()->SetOverscanInsets(display_id
, insets_in_dip
);
416 std::vector
<RootWindowController
*>
417 DisplayController::GetAllRootWindowControllers() {
418 std::vector
<RootWindowController
*> controllers
;
419 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
420 it
!= window_tree_hosts_
.end();
422 RootWindowController
* controller
=
423 GetRootWindowController(GetWindow(it
->second
));
425 controllers
.push_back(controller
);
430 void DisplayController::ToggleMirrorMode() {
431 DisplayManager
* display_manager
= GetDisplayManager();
432 if (display_manager
->num_connected_displays() <= 1)
436 if (limiter_
->IsThrottled())
438 limiter_
->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs
);
440 #if defined(OS_CHROMEOS)
441 Shell
* shell
= Shell::GetInstance();
442 DisplayConfiguratorAnimation
* animation
=
443 shell
->display_configurator_animation();
444 animation
->StartFadeOutAnimation(base::Bind(
445 &DisplayController::SetMirrorModeAfterAnimation
,
446 weak_ptr_factory_
.GetWeakPtr(), !display_manager
->IsInMirrorMode()));
450 void DisplayController::SwapPrimaryDisplay() {
452 if (limiter_
->IsThrottled())
454 limiter_
->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs
);
457 if (Shell::GetScreen()->GetNumDisplays() > 1) {
458 #if defined(OS_CHROMEOS)
459 DisplayConfiguratorAnimation
* animation
=
460 Shell::GetInstance()->display_configurator_animation();
462 animation
->StartFadeOutAnimation(base::Bind(
463 &DisplayController::OnFadeOutForSwapDisplayFinished
,
464 weak_ptr_factory_
.GetWeakPtr()));
466 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
469 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
474 void DisplayController::SetPrimaryDisplayId(int64 id
) {
475 DCHECK_NE(gfx::Display::kInvalidDisplayID
, id
);
476 if (id
== gfx::Display::kInvalidDisplayID
|| primary_display_id
== id
)
479 const gfx::Display
& display
= GetDisplayManager()->GetDisplayForId(id
);
480 if (display
.is_valid())
481 SetPrimaryDisplay(display
);
484 void DisplayController::SetPrimaryDisplay(
485 const gfx::Display
& new_primary_display
) {
486 DisplayManager
* display_manager
= GetDisplayManager();
487 DCHECK(new_primary_display
.is_valid());
488 DCHECK(display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid());
490 if (!new_primary_display
.is_valid() ||
491 !display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid()) {
492 LOG(ERROR
) << "Invalid or non-existent display is requested:"
493 << new_primary_display
.ToString();
497 if (primary_display_id
== new_primary_display
.id() ||
498 window_tree_hosts_
.size() < 2) {
502 AshWindowTreeHost
* non_primary_host
=
503 window_tree_hosts_
[new_primary_display
.id()];
504 LOG_IF(ERROR
, !non_primary_host
)
505 << "Unknown display is requested in SetPrimaryDisplay: id="
506 << new_primary_display
.id();
507 if (!non_primary_host
)
510 gfx::Display old_primary_display
= Shell::GetScreen()->GetPrimaryDisplay();
512 // Swap root windows between current and new primary display.
513 AshWindowTreeHost
* primary_host
= window_tree_hosts_
[primary_display_id
];
515 CHECK_NE(primary_host
, non_primary_host
);
517 window_tree_hosts_
[new_primary_display
.id()] = primary_host
;
518 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
519 new_primary_display
.id();
521 window_tree_hosts_
[old_primary_display
.id()] = non_primary_host
;
522 GetRootWindowSettings(GetWindow(non_primary_host
))->display_id
=
523 old_primary_display
.id();
525 primary_display_id
= new_primary_display
.id();
526 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
527 display_manager
->GetCurrentDisplayIdPair(), primary_display_id
);
529 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host
),
530 old_primary_display
.GetWorkAreaInsets());
531 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host
),
532 new_primary_display
.GetWorkAreaInsets());
534 // Update the dispay manager with new display info.
535 std::vector
<DisplayInfo
> display_info_list
;
536 display_info_list
.push_back(display_manager
->GetDisplayInfo(
537 primary_display_id
));
538 display_info_list
.push_back(display_manager
->GetDisplayInfo(
539 ScreenUtil::GetSecondaryDisplay().id()));
540 GetDisplayManager()->set_force_bounds_changed(true);
541 GetDisplayManager()->UpdateDisplays(display_info_list
);
542 GetDisplayManager()->set_force_bounds_changed(false);
545 void DisplayController::UpdateMouseLocationAfterDisplayChange() {
546 // If the mouse is currently on a display in native location,
547 // use the same native location. Otherwise find the display closest
548 // to the current cursor location in screen coordinates.
550 gfx::Point point_in_screen
= Shell::GetScreen()->GetCursorScreenPoint();
551 gfx::Point target_location_in_native
;
552 int64 closest_distance_squared
= -1;
553 DisplayManager
* display_manager
= GetDisplayManager();
555 aura::Window
* dst_root_window
= nullptr;
556 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
557 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
558 const DisplayInfo display_info
=
559 display_manager
->GetDisplayInfo(display
.id());
560 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
561 if (display_info
.bounds_in_native().Contains(
562 cursor_location_in_native_coords_for_restore_
)) {
563 dst_root_window
= root_window
;
564 target_location_in_native
= cursor_location_in_native_coords_for_restore_
;
567 gfx::Point center
= display
.bounds().CenterPoint();
568 // Use the distance squared from the center of the dislay. This is not
569 // exactly "closest" display, but good enough to pick one
570 // appropriate (and there are at most two displays).
571 // We don't care about actual distance, only relative to other displays, so
572 // using the LengthSquared() is cheaper than Length().
574 int64 distance_squared
= (center
- point_in_screen
).LengthSquared();
575 if (closest_distance_squared
< 0 ||
576 closest_distance_squared
> distance_squared
) {
577 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
578 ::wm::ConvertPointFromScreen(root_window
, ¢er
);
579 root_window
->GetHost()->ConvertPointToNativeScreen(¢er
);
580 dst_root_window
= root_window
;
581 target_location_in_native
= center
;
582 closest_distance_squared
= distance_squared
;
586 gfx::Point target_location_in_root
= target_location_in_native
;
587 dst_root_window
->GetHost()->ConvertPointFromNativeScreen(
588 &target_location_in_root
);
590 #if defined(USE_OZONE)
591 gfx::Point target_location_in_screen
= target_location_in_root
;
592 ::wm::ConvertPointToScreen(dst_root_window
, &target_location_in_screen
);
593 const gfx::Display
& target_display
=
594 display_manager
->FindDisplayContainingPoint(target_location_in_screen
);
595 // If the original location isn't on any of new display, let ozone move
597 if (!target_display
.is_valid())
599 int64 target_display_id
= target_display
.id();
601 // Do not move the cursor if the cursor's location did not change. This avoids
602 // moving (and showing) the cursor:
604 // - When the device is rotated in maximized mode.
605 // |cursor_display_id_for_restore_| is checked to ensure that the cursor is
606 // moved when the cursor's native position does not change but the display
607 // that it is on has changed. This occurs when swapping the primary display.
608 if (target_location_in_native
!=
609 cursor_location_in_native_coords_for_restore_
||
610 target_display_id
!= cursor_display_id_for_restore_
) {
611 dst_root_window
->MoveCursorTo(target_location_in_root
);
612 } else if (target_location_in_screen
!=
613 cursor_location_in_screen_coords_for_restore_
) {
614 // The cursor's native position did not change but its screen position did
615 // change. This occurs when the scale factor or the rotation of the display
616 // that the cursor is on changes.
617 Shell::GetInstance()->cursor_manager()->SetDisplay(target_display
);
619 // Update the cursor's root location. This ends up dispatching a synthetic
620 // mouse move. The synthetic mouse move updates the composited cursor's
621 // location and hover effects. Synthetic mouse moves do not affect the
622 // cursor's visibility.
623 dst_root_window
->GetHost()->dispatcher()->OnCursorMovedToRootLocation(
624 target_location_in_root
);
627 dst_root_window
->MoveCursorTo(target_location_in_root
);
631 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
632 const aura::Window
* window
,
633 const gfx::Insets
& insets
) {
634 const aura::Window
* root_window
= window
->GetRootWindow();
635 int64 id
= GetRootWindowSettings(root_window
)->display_id
;
636 // if id is |kInvaildDisplayID|, it's being deleted.
637 DCHECK(id
!= gfx::Display::kInvalidDisplayID
);
638 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id
, insets
);
641 void DisplayController::OnDisplayAdded(const gfx::Display
& display
) {
642 #if defined(OS_CHROMEOS)
643 if (GetDisplayManager()->default_multi_display_mode() ==
644 DisplayManager::UNIFIED
) {
645 if (primary_display_id
== gfx::Display::kInvalidDisplayID
)
646 primary_display_id
= display
.id();
647 AshWindowTreeHost
* ash_host
=
648 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
649 RootWindowController::CreateForSecondaryDisplay(ash_host
);
651 // Magnifier controllers keep pointers to the current root window.
652 // Update them here to avoid accessing them later.
653 Shell::GetInstance()->magnification_controller()->SwitchTargetRootWindow(
654 ash_host
->AsWindowTreeHost()->window(), false);
656 ->partial_magnification_controller()
657 ->SwitchTargetRootWindow(ash_host
->AsWindowTreeHost()->window());
659 if (primary_tree_host_for_replace_
) {
660 AshWindowTreeHost
* to_delete
= primary_tree_host_for_replace_
;
661 primary_tree_host_for_replace_
= nullptr;
663 // Show the shelf if the original WTH had a visible system
664 // tray. It may or may not be visible depending on OOBE state.
665 ash::SystemTray
* old_tray
=
666 GetRootWindowController(to_delete
->AsWindowTreeHost()->window())
668 ash::SystemTray
* new_tray
=
669 ash::Shell::GetInstance()->GetPrimarySystemTray();
670 if (old_tray
->GetWidget()->IsVisible()) {
671 new_tray
->SetVisible(true);
672 new_tray
->GetWidget()->Show();
675 DeleteHost(to_delete
);
677 auto iter
= std::find_if(
678 window_tree_hosts_
.begin(), window_tree_hosts_
.end(),
679 [to_delete
](const std::pair
<int64
, AshWindowTreeHost
*>& pair
) {
680 return pair
.second
== to_delete
;
682 DCHECK(iter
== window_tree_hosts_
.end());
684 // the host has already been removed from the window_tree_host_.
688 // TODO(oshima): It should be possible to consolidate logic for
689 // unified and non unified, but I'm keeping them separated to minimize
690 // the risk in M44. I'll consolidate this in M45.
691 if (primary_tree_host_for_replace_
) {
692 DCHECK(window_tree_hosts_
.empty());
693 primary_display_id
= display
.id();
694 window_tree_hosts_
[display
.id()] = primary_tree_host_for_replace_
;
695 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_
))
696 ->display_id
= display
.id();
697 primary_tree_host_for_replace_
= nullptr;
698 const DisplayInfo
& display_info
=
699 GetDisplayManager()->GetDisplayInfo(display
.id());
700 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
701 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
702 SetDisplayPropertiesOnHost(ash_host
, display
);
704 if (primary_display_id
== gfx::Display::kInvalidDisplayID
)
705 primary_display_id
= display
.id();
706 DCHECK(!window_tree_hosts_
.empty());
707 AshWindowTreeHost
* ash_host
=
708 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
709 RootWindowController::CreateForSecondaryDisplay(ash_host
);
713 void DisplayController::DeleteHost(AshWindowTreeHost
* host_to_delete
) {
714 ClearDisplayPropertiesOnHost(host_to_delete
);
715 RootWindowController
* controller
=
716 GetRootWindowController(GetWindow(host_to_delete
));
718 controller
->MoveWindowsTo(GetPrimaryRootWindow());
719 // Delete most of root window related objects, but don't delete
720 // root window itself yet because the stack may be using it.
721 controller
->Shutdown();
722 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, controller
);
725 void DisplayController::OnDisplayRemoved(const gfx::Display
& display
) {
726 AshWindowTreeHost
* host_to_delete
= window_tree_hosts_
[display
.id()];
727 CHECK(host_to_delete
) << display
.ToString();
729 // When the primary root window's display is removed, move the primary
730 // root to the other display.
731 if (primary_display_id
== display
.id()) {
732 // Temporarily store the primary root window in
733 // |primary_root_window_for_replace_| when replacing the display.
734 if (window_tree_hosts_
.size() == 1) {
735 primary_display_id
= gfx::Display::kInvalidDisplayID
;
736 primary_tree_host_for_replace_
= host_to_delete
;
737 // Display for root window will be deleted when the Primary RootWindow
738 // is deleted by the Shell.
739 window_tree_hosts_
.erase(display
.id());
742 for (const auto& pair
: window_tree_hosts_
) {
743 if (pair
.first
!= display
.id()) {
744 primary_display_id
= pair
.first
;
748 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
750 AshWindowTreeHost
* primary_host
= host_to_delete
;
751 // Delete the other host instead.
752 host_to_delete
= window_tree_hosts_
[primary_display_id
];
753 GetRootWindowSettings(GetWindow(host_to_delete
))->display_id
= display
.id();
755 // Setup primary root.
756 window_tree_hosts_
[primary_display_id
] = primary_host
;
757 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
760 OnDisplayMetricsChanged(
761 GetDisplayManager()->GetDisplayForId(primary_display_id
),
762 DISPLAY_METRIC_BOUNDS
);
765 DeleteHost(host_to_delete
);
767 // The window tree host should be erased at last because some handlers can
768 // access to the host through GetRootWindowForDisplayId() during
769 // MoveWindowsTo(). See http://crbug.com/415222
770 window_tree_hosts_
.erase(display
.id());
773 void DisplayController::OnDisplayMetricsChanged(const gfx::Display
& display
,
775 if (!(metrics
& (DISPLAY_METRIC_BOUNDS
| DISPLAY_METRIC_ROTATION
|
776 DISPLAY_METRIC_DEVICE_SCALE_FACTOR
)))
778 const DisplayInfo
& display_info
=
779 GetDisplayManager()->GetDisplayInfo(display
.id());
780 DCHECK(!display_info
.bounds_in_native().IsEmpty());
781 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
782 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
783 SetDisplayPropertiesOnHost(ash_host
, display
);
786 void DisplayController::OnHostResized(const aura::WindowTreeHost
* host
) {
787 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestWindow(
788 const_cast<aura::Window
*>(host
->window()));
790 DisplayManager
* display_manager
= GetDisplayManager();
791 if (display_manager
->UpdateDisplayBounds(display
.id(), host
->GetBounds())) {
792 mirror_window_controller_
->UpdateWindow();
793 cursor_window_controller_
->UpdateContainer();
797 void DisplayController::CreateOrUpdateMirroringDisplay(
798 const DisplayInfoList
& info_list
) {
799 if (GetDisplayManager()->IsInMirrorMode() ||
800 GetDisplayManager()->IsInUnifiedMode()) {
801 mirror_window_controller_
->UpdateWindow(info_list
);
802 cursor_window_controller_
->UpdateContainer();
808 void DisplayController::CloseMirroringDisplay() {
809 mirror_window_controller_
->Close();
810 // If cursor_compositing is enabled for large cursor, the cursor window is
811 // always on the desktop display (the visible cursor on the non-desktop
812 // display is drawn through compositor mirroring). Therefore, it's unnecessary
813 // to handle the cursor_window at all. See: http://crbug.com/412910
814 if (!cursor_window_controller_
->is_cursor_compositing_enabled())
815 cursor_window_controller_
->UpdateContainer();
818 void DisplayController::PreDisplayConfigurationChange(bool clear_focus
) {
819 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanging());
820 focus_activation_store_
->Store(clear_focus
);
821 gfx::Screen
* screen
= Shell::GetScreen();
822 gfx::Point point_in_screen
= screen
->GetCursorScreenPoint();
823 cursor_location_in_screen_coords_for_restore_
= point_in_screen
;
825 gfx::Display display
= screen
->GetDisplayNearestPoint(point_in_screen
);
826 cursor_display_id_for_restore_
= display
.id();
828 gfx::Point point_in_native
= point_in_screen
;
829 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
830 ::wm::ConvertPointFromScreen(root_window
, &point_in_native
);
831 root_window
->GetHost()->ConvertPointToNativeScreen(&point_in_native
);
832 cursor_location_in_native_coords_for_restore_
= point_in_native
;
835 void DisplayController::PostDisplayConfigurationChange() {
837 limiter_
->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs
);
839 focus_activation_store_
->Restore();
841 DisplayManager
* display_manager
= GetDisplayManager();
842 DisplayLayoutStore
* layout_store
= display_manager
->layout_store();
843 if (display_manager
->num_connected_displays() > 1) {
844 DisplayIdPair pair
= display_manager
->GetCurrentDisplayIdPair();
845 layout_store
->UpdateMirrorStatus(pair
, display_manager
->IsInMirrorMode());
847 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
848 DisplayLayout layout
= layout_store
->GetRegisteredDisplayLayout(pair
);
849 int64 primary_id
= layout
.primary_id
;
851 primary_id
== gfx::Display::kInvalidDisplayID
?
852 pair
.first
: primary_id
);
853 // Update the primary_id in case the above call is
854 // ignored. Happens when a) default layout's primary id
855 // doesn't exist, or b) the primary_id has already been
856 // set to the same and didn't update it.
857 layout_store
->UpdatePrimaryDisplayId(
858 pair
, Shell::GetScreen()->GetPrimaryDisplay().id());
861 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanged());
862 UpdateMouseLocationAfterDisplayChange();
865 AshWindowTreeHost
* DisplayController::AddWindowTreeHostForDisplay(
866 const gfx::Display
& display
,
867 const AshWindowTreeHostInitParams
& init_params
) {
868 static int host_count
= 0;
869 const DisplayInfo
& display_info
=
870 GetDisplayManager()->GetDisplayInfo(display
.id());
871 AshWindowTreeHostInitParams
params_with_bounds(init_params
);
872 params_with_bounds
.initial_bounds
= display_info
.bounds_in_native();
873 params_with_bounds
.offscreen
=
874 display
.id() == DisplayManager::kUnifiedDisplayId
;
875 AshWindowTreeHost
* ash_host
= AshWindowTreeHost::Create(params_with_bounds
);
876 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
878 host
->window()->SetName(base::StringPrintf(
879 "%sRootWindow-%d", params_with_bounds
.offscreen
? "Offscreen" : "",
881 host
->window()->SetTitle(base::UTF8ToUTF16(display_info
.name()));
882 host
->compositor()->SetBackgroundColor(SK_ColorBLACK
);
883 // No need to remove our observer observer because the DisplayController
884 // outlives the host.
885 host
->AddObserver(this);
886 InitRootWindowSettings(host
->window())->display_id
= display
.id();
889 window_tree_hosts_
[display
.id()] = ash_host
;
890 SetDisplayPropertiesOnHost(ash_host
, display
);
892 #if defined(OS_CHROMEOS)
893 if (switches::ConstrainPointerToRoot())
894 ash_host
->ConfineCursorToRootWindow();
899 void DisplayController::OnFadeOutForSwapDisplayFinished() {
900 #if defined(OS_CHROMEOS)
901 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
902 Shell::GetInstance()->display_configurator_animation()
903 ->StartFadeInAnimation();
907 void DisplayController::SetMirrorModeAfterAnimation(bool mirror
) {
908 GetDisplayManager()->SetMirrorMode(mirror
);