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/gfx/display.h"
43 #include "ui/gfx/screen.h"
44 #include "ui/wm/core/coordinate_conversion.h"
45 #include "ui/wm/public/activation_client.h"
47 #if defined(OS_CHROMEOS)
48 #include "ash/display/display_configurator_animation.h"
49 #include "base/sys_info.h"
50 #include "base/time/time.h"
51 #endif // defined(OS_CHROMEOS)
54 #include "ui/base/x/x11_util.h"
55 #include "ui/gfx/x/x11_types.h"
57 // Including this at the bottom to avoid other
58 // potential conflict with chrome headers.
59 #include <X11/extensions/Xrandr.h>
61 #endif // defined(USE_X11)
63 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
64 #include "ui/events/ozone/chromeos/cursor_controller.h"
70 // Primary display stored in global object as it can be
71 // accessed after Shell is deleted. A separate display instance is created
72 // during the shutdown instead of always keeping two display instances
73 // (one here and another one in display_manager) in sync, which is error prone.
74 // This is initialized in the constructor, and then in CreatePrimaryHost().
75 int64 primary_display_id
= -1;
77 // Specifies how long the display change should have been disabled
78 // after each display change operations.
79 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid
80 // changing the settings while the system is still configurating
81 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs|
82 // when the display change happens, so the actual timeout is much shorter.
83 const int64 kAfterDisplayChangeThrottleTimeoutMs
= 500;
84 const int64 kCycleDisplayThrottleTimeoutMs
= 4000;
85 const int64 kSwapDisplayThrottleTimeoutMs
= 500;
87 #if defined(USE_OZONE) && defined(OS_CHROMEOS)
88 // Add 20% more cursor motion on non-integrated displays.
89 const float kCursorMultiplierForExternalDisplays
= 1.2f
;
92 DisplayManager
* GetDisplayManager() {
93 return Shell::GetInstance()->display_manager();
96 void SetDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
,
97 const gfx::Display
& display
) {
98 DisplayInfo info
= GetDisplayManager()->GetDisplayInfo(display
.id());
99 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
100 #if defined(OS_CHROMEOS)
102 // Native window property (Atom in X11) that specifies the display's
103 // rotation, scale factor and if it's internal display. They are
104 // read and used by touchpad/mouse driver directly on X (contact
105 // adlr@ for more details on touchpad/mouse driver side). The value
106 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2
107 // (180 degree) or 3 (270 degrees clockwise). The value of the
108 // scale factor is in percent (100, 140, 200 etc).
109 const char kRotationProp
[] = "_CHROME_DISPLAY_ROTATION";
110 const char kScaleFactorProp
[] = "_CHROME_DISPLAY_SCALE_FACTOR";
111 const char kInternalProp
[] = "_CHROME_DISPLAY_INTERNAL";
112 const char kCARDINAL
[] = "CARDINAL";
113 int xrandr_rotation
= RR_Rotate_0
;
114 switch (info
.GetActiveRotation()) {
115 case gfx::Display::ROTATE_0
:
116 xrandr_rotation
= RR_Rotate_0
;
118 case gfx::Display::ROTATE_90
:
119 xrandr_rotation
= RR_Rotate_90
;
121 case gfx::Display::ROTATE_180
:
122 xrandr_rotation
= RR_Rotate_180
;
124 case gfx::Display::ROTATE_270
:
125 xrandr_rotation
= RR_Rotate_270
;
129 int internal
= display
.IsInternal() ? 1 : 0;
130 gfx::AcceleratedWidget xwindow
= host
->GetAcceleratedWidget();
131 ui::SetIntProperty(xwindow
, kInternalProp
, kCARDINAL
, internal
);
132 ui::SetIntProperty(xwindow
, kRotationProp
, kCARDINAL
, xrandr_rotation
);
133 ui::SetIntProperty(xwindow
,
136 100 * display
.device_scale_factor());
137 #elif defined(USE_OZONE)
138 // Scale all motion on High-DPI displays.
139 float scale
= display
.device_scale_factor();
141 if (!display
.IsInternal())
142 scale
*= kCursorMultiplierForExternalDisplays
;
144 ui::CursorController::GetInstance()->SetCursorConfigForWindow(
145 host
->GetAcceleratedWidget(), info
.GetActiveRotation(), scale
);
148 scoped_ptr
<RootWindowTransformer
> transformer(
149 CreateRootWindowTransformerForDisplay(host
->window(), display
));
150 ash_host
->SetRootWindowTransformer(transformer
.Pass());
153 GetDisplayManager()->GetActiveModeForDisplayId(display
.id());
154 if (mode
.refresh_rate
> 0.0f
) {
155 host
->compositor()->SetAuthoritativeVSyncInterval(
156 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond
/
160 // Just movnig the display requires the full redraw.
161 // chrome-os-partner:33558.
162 host
->compositor()->ScheduleFullRedraw();
165 void ClearDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
) {
166 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
167 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
168 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
169 host
->GetAcceleratedWidget());
173 aura::Window
* GetWindow(AshWindowTreeHost
* ash_host
) {
174 CHECK(ash_host
->AsWindowTreeHost());
175 return ash_host
->AsWindowTreeHost()->window();
180 // A utility class to store/restore focused/active window
181 // when the display configuration has changed.
182 class FocusActivationStore
{
184 FocusActivationStore()
185 : activation_client_(nullptr),
186 capture_client_(nullptr),
187 focus_client_(nullptr),
191 void Store(bool clear_focus
) {
192 if (!activation_client_
) {
193 aura::Window
* root
= Shell::GetPrimaryRootWindow();
194 activation_client_
= aura::client::GetActivationClient(root
);
195 capture_client_
= aura::client::GetCaptureClient(root
);
196 focus_client_
= aura::client::GetFocusClient(root
);
198 focused_
= focus_client_
->GetFocusedWindow();
200 tracker_
.Add(focused_
);
201 active_
= activation_client_
->GetActiveWindow();
202 if (active_
&& focused_
!= active_
)
203 tracker_
.Add(active_
);
205 // Deactivate the window to close menu / bubble windows.
207 activation_client_
->DeactivateWindow(active_
);
209 // Release capture if any.
210 capture_client_
->SetCapture(nullptr);
211 // Clear the focused window if any. This is necessary because a
212 // window may be deleted when losing focus (fullscreen flash for
213 // example). If the focused window is still alive after move, it'll
214 // be re-focused below.
216 focus_client_
->FocusWindow(nullptr);
220 // Restore focused or active window if it's still alive.
221 if (focused_
&& tracker_
.Contains(focused_
)) {
222 focus_client_
->FocusWindow(focused_
);
223 } else if (active_
&& tracker_
.Contains(active_
)) {
224 activation_client_
->ActivateWindow(active_
);
227 tracker_
.Remove(focused_
);
229 tracker_
.Remove(active_
);
235 aura::client::ActivationClient
* activation_client_
;
236 aura::client::CaptureClient
* capture_client_
;
237 aura::client::FocusClient
* focus_client_
;
238 aura::WindowTracker tracker_
;
239 aura::Window
* focused_
;
240 aura::Window
* active_
;
242 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore
);
245 ////////////////////////////////////////////////////////////////////////////////
246 // DisplayChangeLimiter
248 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
249 : throttle_timeout_(base::Time::Now()) {
252 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
255 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms
);
258 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
259 return base::Time::Now() < throttle_timeout_
;
262 ////////////////////////////////////////////////////////////////////////////////
265 DisplayController::DisplayController()
266 : primary_tree_host_for_replace_(nullptr),
267 focus_activation_store_(new FocusActivationStore()),
268 cursor_window_controller_(new CursorWindowController()),
269 mirror_window_controller_(new MirrorWindowController()),
270 cursor_display_id_for_restore_(gfx::Display::kInvalidDisplayID
),
271 weak_ptr_factory_(this) {
272 #if defined(OS_CHROMEOS)
273 if (base::SysInfo::IsRunningOnChromeOS())
274 limiter_
.reset(new DisplayChangeLimiter
);
276 // Reset primary display to make sure that tests don't use
277 // stale display info from previous tests.
278 primary_display_id
= gfx::Display::kInvalidDisplayID
;
281 DisplayController::~DisplayController() {
284 void DisplayController::Start() {
285 Shell::GetScreen()->AddObserver(this);
286 Shell::GetInstance()->display_manager()->set_delegate(this);
289 void DisplayController::Shutdown() {
290 // Unset the display manager's delegate here because
291 // DisplayManager outlives DisplayController.
292 Shell::GetInstance()->display_manager()->set_delegate(nullptr);
294 cursor_window_controller_
.reset();
295 mirror_window_controller_
.reset();
297 Shell::GetScreen()->RemoveObserver(this);
299 int64 primary_id
= Shell::GetScreen()->GetPrimaryDisplay().id();
301 // Delete non primary root window controllers first, then
302 // delete the primary root window controller.
303 aura::Window::Windows root_windows
= DisplayController::GetAllRootWindows();
304 std::vector
<RootWindowController
*> to_delete
;
305 RootWindowController
* primary_rwc
= nullptr;
306 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
307 iter
!= root_windows
.end();
309 RootWindowController
* rwc
= GetRootWindowController(*iter
);
310 if (GetRootWindowSettings(*iter
)->display_id
== primary_id
)
313 to_delete
.push_back(rwc
);
317 STLDeleteElements(&to_delete
);
321 void DisplayController::CreatePrimaryHost(
322 const AshWindowTreeHostInitParams
& init_params
) {
323 const gfx::Display
& primary_candidate
=
324 GetDisplayManager()->GetPrimaryDisplayCandidate();
325 primary_display_id
= primary_candidate
.id();
326 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
327 AddWindowTreeHostForDisplay(primary_candidate
, init_params
);
330 void DisplayController::InitDisplays() {
331 RootWindowController::CreateForPrimaryDisplay(
332 window_tree_hosts_
[primary_display_id
]);
333 DisplayManager
* display_manager
= GetDisplayManager();
334 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
335 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
336 if (primary_display_id
!= display
.id()) {
337 AshWindowTreeHost
* ash_host
= AddWindowTreeHostForDisplay(
338 display
, AshWindowTreeHostInitParams());
339 RootWindowController::CreateForSecondaryDisplay(ash_host
);
343 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplaysInitialized());
346 void DisplayController::AddObserver(Observer
* observer
) {
347 observers_
.AddObserver(observer
);
350 void DisplayController::RemoveObserver(Observer
* observer
) {
351 observers_
.RemoveObserver(observer
);
355 int64
DisplayController::GetPrimaryDisplayId() {
356 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
357 return primary_display_id
;
360 aura::Window
* DisplayController::GetPrimaryRootWindow() {
361 return GetRootWindowForDisplayId(primary_display_id
);
364 aura::Window
* DisplayController::GetRootWindowForDisplayId(int64 id
) {
365 AshWindowTreeHost
* host
= GetAshWindowTreeHostForDisplayId(id
);
367 return GetWindow(host
);
370 AshWindowTreeHost
* DisplayController::GetAshWindowTreeHostForDisplayId(
372 CHECK_EQ(1u, window_tree_hosts_
.count(display_id
))
373 << "display id = " << display_id
;
374 return window_tree_hosts_
[display_id
];
377 void DisplayController::CloseChildWindows() {
378 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
379 it
!= window_tree_hosts_
.end();
381 aura::Window
* root_window
= GetWindow(it
->second
);
382 RootWindowController
* controller
= GetRootWindowController(root_window
);
384 controller
->CloseChildWindows();
386 while (!root_window
->children().empty()) {
387 aura::Window
* child
= root_window
->children()[0];
394 aura::Window::Windows
DisplayController::GetAllRootWindows() {
395 aura::Window::Windows windows
;
396 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
397 it
!= window_tree_hosts_
.end();
400 if (GetRootWindowController(GetWindow(it
->second
)))
401 windows
.push_back(GetWindow(it
->second
));
406 gfx::Insets
DisplayController::GetOverscanInsets(int64 display_id
) const {
407 return GetDisplayManager()->GetOverscanInsets(display_id
);
410 void DisplayController::SetOverscanInsets(int64 display_id
,
411 const gfx::Insets
& insets_in_dip
) {
412 GetDisplayManager()->SetOverscanInsets(display_id
, insets_in_dip
);
415 std::vector
<RootWindowController
*>
416 DisplayController::GetAllRootWindowControllers() {
417 std::vector
<RootWindowController
*> controllers
;
418 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
419 it
!= window_tree_hosts_
.end();
421 RootWindowController
* controller
=
422 GetRootWindowController(GetWindow(it
->second
));
424 controllers
.push_back(controller
);
429 void DisplayController::ToggleMirrorMode() {
430 DisplayManager
* display_manager
= GetDisplayManager();
431 if (display_manager
->num_connected_displays() <= 1)
435 if (limiter_
->IsThrottled())
437 limiter_
->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs
);
439 #if defined(OS_CHROMEOS)
440 Shell
* shell
= Shell::GetInstance();
441 DisplayConfiguratorAnimation
* animation
=
442 shell
->display_configurator_animation();
443 animation
->StartFadeOutAnimation(base::Bind(
444 &DisplayController::SetMirrorModeAfterAnimation
,
445 weak_ptr_factory_
.GetWeakPtr(), !display_manager
->IsInMirrorMode()));
449 void DisplayController::SwapPrimaryDisplay() {
451 if (limiter_
->IsThrottled())
453 limiter_
->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs
);
456 if (Shell::GetScreen()->GetNumDisplays() > 1) {
457 #if defined(OS_CHROMEOS)
458 DisplayConfiguratorAnimation
* animation
=
459 Shell::GetInstance()->display_configurator_animation();
461 animation
->StartFadeOutAnimation(base::Bind(
462 &DisplayController::OnFadeOutForSwapDisplayFinished
,
463 weak_ptr_factory_
.GetWeakPtr()));
465 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
468 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
473 void DisplayController::SetPrimaryDisplayId(int64 id
) {
474 DCHECK_NE(gfx::Display::kInvalidDisplayID
, id
);
475 if (id
== gfx::Display::kInvalidDisplayID
|| primary_display_id
== id
)
478 const gfx::Display
& display
= GetDisplayManager()->GetDisplayForId(id
);
479 if (display
.is_valid())
480 SetPrimaryDisplay(display
);
483 void DisplayController::SetPrimaryDisplay(
484 const gfx::Display
& new_primary_display
) {
485 DisplayManager
* display_manager
= GetDisplayManager();
486 DCHECK(new_primary_display
.is_valid());
487 DCHECK(display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid());
489 if (!new_primary_display
.is_valid() ||
490 !display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid()) {
491 LOG(ERROR
) << "Invalid or non-existent display is requested:"
492 << new_primary_display
.ToString();
496 if (primary_display_id
== new_primary_display
.id() ||
497 window_tree_hosts_
.size() < 2) {
501 AshWindowTreeHost
* non_primary_host
=
502 window_tree_hosts_
[new_primary_display
.id()];
503 LOG_IF(ERROR
, !non_primary_host
)
504 << "Unknown display is requested in SetPrimaryDisplay: id="
505 << new_primary_display
.id();
506 if (!non_primary_host
)
509 gfx::Display old_primary_display
= Shell::GetScreen()->GetPrimaryDisplay();
511 // Swap root windows between current and new primary display.
512 AshWindowTreeHost
* primary_host
= window_tree_hosts_
[primary_display_id
];
514 CHECK_NE(primary_host
, non_primary_host
);
516 window_tree_hosts_
[new_primary_display
.id()] = primary_host
;
517 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
518 new_primary_display
.id();
520 window_tree_hosts_
[old_primary_display
.id()] = non_primary_host
;
521 GetRootWindowSettings(GetWindow(non_primary_host
))->display_id
=
522 old_primary_display
.id();
524 primary_display_id
= new_primary_display
.id();
525 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
526 display_manager
->GetCurrentDisplayIdPair(), primary_display_id
);
528 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host
),
529 old_primary_display
.GetWorkAreaInsets());
530 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host
),
531 new_primary_display
.GetWorkAreaInsets());
533 // Update the dispay manager with new display info.
534 std::vector
<DisplayInfo
> display_info_list
;
535 display_info_list
.push_back(display_manager
->GetDisplayInfo(
536 primary_display_id
));
537 display_info_list
.push_back(display_manager
->GetDisplayInfo(
538 ScreenUtil::GetSecondaryDisplay().id()));
539 GetDisplayManager()->set_force_bounds_changed(true);
540 GetDisplayManager()->UpdateDisplays(display_info_list
);
541 GetDisplayManager()->set_force_bounds_changed(false);
544 void DisplayController::UpdateMouseLocationAfterDisplayChange() {
545 // If the mouse is currently on a display in native location,
546 // use the same native location. Otherwise find the display closest
547 // to the current cursor location in screen coordinates.
549 gfx::Point point_in_screen
= Shell::GetScreen()->GetCursorScreenPoint();
550 gfx::Point target_location_in_native
;
551 int64 closest_distance_squared
= -1;
552 DisplayManager
* display_manager
= GetDisplayManager();
554 aura::Window
* dst_root_window
= nullptr;
555 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
556 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
557 const DisplayInfo display_info
=
558 display_manager
->GetDisplayInfo(display
.id());
559 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
560 if (display_info
.bounds_in_native().Contains(
561 cursor_location_in_native_coords_for_restore_
)) {
562 dst_root_window
= root_window
;
563 target_location_in_native
= cursor_location_in_native_coords_for_restore_
;
566 gfx::Point center
= display
.bounds().CenterPoint();
567 // Use the distance squared from the center of the dislay. This is not
568 // exactly "closest" display, but good enough to pick one
569 // appropriate (and there are at most two displays).
570 // We don't care about actual distance, only relative to other displays, so
571 // using the LengthSquared() is cheaper than Length().
573 int64 distance_squared
= (center
- point_in_screen
).LengthSquared();
574 if (closest_distance_squared
< 0 ||
575 closest_distance_squared
> distance_squared
) {
576 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
577 ::wm::ConvertPointFromScreen(root_window
, ¢er
);
578 root_window
->GetHost()->ConvertPointToNativeScreen(¢er
);
579 dst_root_window
= root_window
;
580 target_location_in_native
= center
;
581 closest_distance_squared
= distance_squared
;
585 gfx::Point target_location_in_root
= target_location_in_native
;
586 dst_root_window
->GetHost()->ConvertPointFromNativeScreen(
587 &target_location_in_root
);
589 #if defined(USE_OZONE)
590 gfx::Point target_location_in_screen
= target_location_in_root
;
591 ::wm::ConvertPointToScreen(dst_root_window
, &target_location_in_screen
);
592 const gfx::Display
& target_display
=
593 display_manager
->FindDisplayContainingPoint(target_location_in_screen
);
594 // If the original location isn't on any of new display, let ozone move
596 if (!target_display
.is_valid())
598 int64 target_display_id
= target_display
.id();
600 // Do not move the cursor if the cursor's location did not change. This avoids
601 // moving (and showing) the cursor:
603 // - When the device is rotated in maximized mode.
604 // |cursor_display_id_for_restore_| is checked to ensure that the cursor is
605 // moved when the cursor's native position does not change but the display
606 // that it is on has changed. This occurs when swapping the primary display.
607 if (target_location_in_native
!=
608 cursor_location_in_native_coords_for_restore_
||
609 target_display_id
!= cursor_display_id_for_restore_
) {
610 dst_root_window
->MoveCursorTo(target_location_in_root
);
611 } else if (target_location_in_screen
!=
612 cursor_location_in_screen_coords_for_restore_
) {
613 // The cursor's native position did not change but its screen position did
614 // change. This occurs when the scale factor or the rotation of the display
615 // that the cursor is on changes.
616 Shell::GetInstance()->cursor_manager()->SetDisplay(target_display
);
618 // Update the cursor's root location. This ends up dispatching a synthetic
619 // mouse move. The synthetic mouse move updates the composited cursor's
620 // location and hover effects. Synthetic mouse moves do not affect the
621 // cursor's visibility.
622 dst_root_window
->GetHost()->dispatcher()->OnCursorMovedToRootLocation(
623 target_location_in_root
);
626 dst_root_window
->MoveCursorTo(target_location_in_root
);
630 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
631 const aura::Window
* window
,
632 const gfx::Insets
& insets
) {
633 const aura::Window
* root_window
= window
->GetRootWindow();
634 int64 id
= GetRootWindowSettings(root_window
)->display_id
;
635 // if id is |kInvaildDisplayID|, it's being deleted.
636 DCHECK(id
!= gfx::Display::kInvalidDisplayID
);
637 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id
, insets
);
640 void DisplayController::OnDisplayAdded(const gfx::Display
& display
) {
641 #if defined(OS_CHROMEOS)
642 // If we're switching from/to offscreen WTH, we need to
643 // create new WTH for primary display instead of reusing.
644 if (primary_tree_host_for_replace_
&&
645 (GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_
))
646 ->display_id
== DisplayManager::kUnifiedDisplayId
||
647 display
.id() == DisplayManager::kUnifiedDisplayId
)) {
648 DCHECK_EQ(gfx::Display::kInvalidDisplayID
, primary_display_id
);
649 primary_display_id
= display
.id();
651 AshWindowTreeHost
* ash_host
=
652 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
653 RootWindowController::CreateForSecondaryDisplay(ash_host
);
655 // Magnifier controllers keep pointers to the current root window.
656 // Update them here to avoid accessing them later.
657 Shell::GetInstance()->magnification_controller()->SwitchTargetRootWindow(
658 ash_host
->AsWindowTreeHost()->window(), false);
660 ->partial_magnification_controller()
661 ->SwitchTargetRootWindow(ash_host
->AsWindowTreeHost()->window());
663 AshWindowTreeHost
* to_delete
= primary_tree_host_for_replace_
;
664 primary_tree_host_for_replace_
= nullptr;
666 // Show the shelf if the original WTH had a visible system
667 // tray. It may or may not be visible depending on OOBE state.
668 ash::SystemTray
* old_tray
=
669 GetRootWindowController(to_delete
->AsWindowTreeHost()->window())
671 ash::SystemTray
* new_tray
=
672 ash::Shell::GetInstance()->GetPrimarySystemTray();
673 if (old_tray
->GetWidget()->IsVisible()) {
674 new_tray
->SetVisible(true);
675 new_tray
->GetWidget()->Show();
678 DeleteHost(to_delete
);
680 auto iter
= std::find_if(
681 window_tree_hosts_
.begin(), window_tree_hosts_
.end(),
682 [to_delete
](const std::pair
<int64
, AshWindowTreeHost
*>& pair
) {
683 return pair
.second
== to_delete
;
685 DCHECK(iter
== window_tree_hosts_
.end());
687 // the host has already been removed from the window_tree_host_.
690 // TODO(oshima): It should be possible to consolidate logic for
691 // unified and non unified, but I'm keeping them separated to minimize
692 // the risk in M44. I'll consolidate this in M45.
693 if (primary_tree_host_for_replace_
) {
694 DCHECK(window_tree_hosts_
.empty());
695 primary_display_id
= display
.id();
696 window_tree_hosts_
[display
.id()] = primary_tree_host_for_replace_
;
697 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_
))
698 ->display_id
= display
.id();
699 primary_tree_host_for_replace_
= nullptr;
700 const DisplayInfo
& display_info
=
701 GetDisplayManager()->GetDisplayInfo(display
.id());
702 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
703 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
704 SetDisplayPropertiesOnHost(ash_host
, display
);
706 if (primary_display_id
== gfx::Display::kInvalidDisplayID
)
707 primary_display_id
= display
.id();
708 DCHECK(!window_tree_hosts_
.empty());
709 AshWindowTreeHost
* ash_host
=
710 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
711 RootWindowController::CreateForSecondaryDisplay(ash_host
);
715 void DisplayController::DeleteHost(AshWindowTreeHost
* host_to_delete
) {
716 ClearDisplayPropertiesOnHost(host_to_delete
);
717 RootWindowController
* controller
=
718 GetRootWindowController(GetWindow(host_to_delete
));
720 controller
->MoveWindowsTo(GetPrimaryRootWindow());
721 // Delete most of root window related objects, but don't delete
722 // root window itself yet because the stack may be using it.
723 controller
->Shutdown();
724 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, controller
);
727 void DisplayController::OnDisplayRemoved(const gfx::Display
& display
) {
728 AshWindowTreeHost
* host_to_delete
= window_tree_hosts_
[display
.id()];
729 CHECK(host_to_delete
) << display
.ToString();
731 // When the primary root window's display is removed, move the primary
732 // root to the other display.
733 if (primary_display_id
== display
.id()) {
734 // Temporarily store the primary root window in
735 // |primary_root_window_for_replace_| when replacing the display.
736 if (window_tree_hosts_
.size() == 1) {
737 primary_display_id
= gfx::Display::kInvalidDisplayID
;
738 primary_tree_host_for_replace_
= host_to_delete
;
739 // Display for root window will be deleted when the Primary RootWindow
740 // is deleted by the Shell.
741 window_tree_hosts_
.erase(display
.id());
744 for (const auto& pair
: window_tree_hosts_
) {
745 if (pair
.first
!= display
.id()) {
746 primary_display_id
= pair
.first
;
750 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
752 AshWindowTreeHost
* primary_host
= host_to_delete
;
753 // Delete the other host instead.
754 host_to_delete
= window_tree_hosts_
[primary_display_id
];
755 GetRootWindowSettings(GetWindow(host_to_delete
))->display_id
= display
.id();
757 // Setup primary root.
758 window_tree_hosts_
[primary_display_id
] = primary_host
;
759 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
762 OnDisplayMetricsChanged(
763 GetDisplayManager()->GetDisplayForId(primary_display_id
),
764 DISPLAY_METRIC_BOUNDS
);
767 DeleteHost(host_to_delete
);
769 // The window tree host should be erased at last because some handlers can
770 // access to the host through GetRootWindowForDisplayId() during
771 // MoveWindowsTo(). See http://crbug.com/415222
772 window_tree_hosts_
.erase(display
.id());
775 void DisplayController::OnDisplayMetricsChanged(const gfx::Display
& display
,
777 if (!(metrics
& (DISPLAY_METRIC_BOUNDS
| DISPLAY_METRIC_ROTATION
|
778 DISPLAY_METRIC_DEVICE_SCALE_FACTOR
)))
780 const DisplayInfo
& display_info
=
781 GetDisplayManager()->GetDisplayInfo(display
.id());
782 DCHECK(!display_info
.bounds_in_native().IsEmpty());
783 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
784 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
785 SetDisplayPropertiesOnHost(ash_host
, display
);
788 void DisplayController::OnHostResized(const aura::WindowTreeHost
* host
) {
789 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestWindow(
790 const_cast<aura::Window
*>(host
->window()));
792 DisplayManager
* display_manager
= GetDisplayManager();
793 if (display_manager
->UpdateDisplayBounds(display
.id(), host
->GetBounds())) {
794 mirror_window_controller_
->UpdateWindow();
795 cursor_window_controller_
->UpdateContainer();
799 void DisplayController::CreateOrUpdateMirroringDisplay(
800 const DisplayInfoList
& info_list
) {
801 if (GetDisplayManager()->IsInMirrorMode() ||
802 GetDisplayManager()->IsInUnifiedMode()) {
803 mirror_window_controller_
->UpdateWindow(info_list
);
804 cursor_window_controller_
->UpdateContainer();
810 void DisplayController::CloseMirroringDisplayIfNotNecessary() {
811 mirror_window_controller_
->CloseIfNotNecessary();
812 // If cursor_compositing is enabled for large cursor, the cursor window is
813 // always on the desktop display (the visible cursor on the non-desktop
814 // display is drawn through compositor mirroring). Therefore, it's unnecessary
815 // to handle the cursor_window at all. See: http://crbug.com/412910
816 if (!cursor_window_controller_
->is_cursor_compositing_enabled())
817 cursor_window_controller_
->UpdateContainer();
820 void DisplayController::PreDisplayConfigurationChange(bool clear_focus
) {
821 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanging());
822 focus_activation_store_
->Store(clear_focus
);
823 gfx::Screen
* screen
= Shell::GetScreen();
824 gfx::Point point_in_screen
= screen
->GetCursorScreenPoint();
825 cursor_location_in_screen_coords_for_restore_
= point_in_screen
;
827 gfx::Display display
= screen
->GetDisplayNearestPoint(point_in_screen
);
828 cursor_display_id_for_restore_
= display
.id();
830 gfx::Point point_in_native
= point_in_screen
;
831 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
832 ::wm::ConvertPointFromScreen(root_window
, &point_in_native
);
833 root_window
->GetHost()->ConvertPointToNativeScreen(&point_in_native
);
834 cursor_location_in_native_coords_for_restore_
= point_in_native
;
837 void DisplayController::PostDisplayConfigurationChange() {
839 limiter_
->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs
);
841 focus_activation_store_
->Restore();
843 DisplayManager
* display_manager
= GetDisplayManager();
844 DisplayLayoutStore
* layout_store
= display_manager
->layout_store();
845 if (display_manager
->num_connected_displays() > 1) {
846 DisplayIdPair pair
= display_manager
->GetCurrentDisplayIdPair();
847 layout_store
->UpdateMultiDisplayState(
848 pair
, display_manager
->IsInMirrorMode(),
849 display_manager
->default_multi_display_mode() ==
850 DisplayManager::UNIFIED
);
852 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
853 DisplayLayout layout
= layout_store
->GetRegisteredDisplayLayout(pair
);
854 int64 primary_id
= layout
.primary_id
;
856 primary_id
== gfx::Display::kInvalidDisplayID
?
857 pair
.first
: primary_id
);
858 // Update the primary_id in case the above call is
859 // ignored. Happens when a) default layout's primary id
860 // doesn't exist, or b) the primary_id has already been
861 // set to the same and didn't update it.
862 layout_store
->UpdatePrimaryDisplayId(
863 pair
, Shell::GetScreen()->GetPrimaryDisplay().id());
866 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanged());
867 UpdateMouseLocationAfterDisplayChange();
870 AshWindowTreeHost
* DisplayController::AddWindowTreeHostForDisplay(
871 const gfx::Display
& display
,
872 const AshWindowTreeHostInitParams
& init_params
) {
873 static int host_count
= 0;
874 const DisplayInfo
& display_info
=
875 GetDisplayManager()->GetDisplayInfo(display
.id());
876 AshWindowTreeHostInitParams
params_with_bounds(init_params
);
877 params_with_bounds
.initial_bounds
= display_info
.bounds_in_native();
878 params_with_bounds
.offscreen
=
879 display
.id() == DisplayManager::kUnifiedDisplayId
;
880 AshWindowTreeHost
* ash_host
= AshWindowTreeHost::Create(params_with_bounds
);
881 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
883 host
->window()->SetName(base::StringPrintf(
884 "%sRootWindow-%d", params_with_bounds
.offscreen
? "Offscreen" : "",
886 host
->window()->SetTitle(base::UTF8ToUTF16(display_info
.name()));
887 host
->compositor()->SetBackgroundColor(SK_ColorBLACK
);
888 // No need to remove our observer observer because the DisplayController
889 // outlives the host.
890 host
->AddObserver(this);
891 InitRootWindowSettings(host
->window())->display_id
= display
.id();
894 window_tree_hosts_
[display
.id()] = ash_host
;
895 SetDisplayPropertiesOnHost(ash_host
, display
);
897 #if defined(OS_CHROMEOS)
898 if (switches::ConstrainPointerToRoot())
899 ash_host
->ConfineCursorToRootWindow();
904 void DisplayController::OnFadeOutForSwapDisplayFinished() {
905 #if defined(OS_CHROMEOS)
906 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
907 Shell::GetInstance()->display_configurator_animation()
908 ->StartFadeInAnimation();
912 void DisplayController::SetMirrorModeAfterAnimation(bool mirror
) {
913 GetDisplayManager()->SetMirrorMode(mirror
);