Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / ash / display / display_controller.cc
blob7a3b6f9959daef8f7b0067eec767a425a89924be
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"
7 #include <algorithm>
8 #include <cmath>
9 #include <map>
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/root_window_controller.h"
21 #include "ash/root_window_settings.h"
22 #include "ash/screen_util.h"
23 #include "ash/shell.h"
24 #include "ash/shell_delegate.h"
25 #include "ash/wm/coordinate_conversion.h"
26 #include "base/command_line.h"
27 #include "base/stl_util.h"
28 #include "base/strings/stringprintf.h"
29 #include "base/strings/utf_string_conversions.h"
30 #include "ui/aura/client/capture_client.h"
31 #include "ui/aura/client/focus_client.h"
32 #include "ui/aura/client/screen_position_client.h"
33 #include "ui/aura/window.h"
34 #include "ui/aura/window_event_dispatcher.h"
35 #include "ui/aura/window_property.h"
36 #include "ui/aura/window_tracker.h"
37 #include "ui/aura/window_tree_host.h"
38 #include "ui/compositor/compositor.h"
39 #include "ui/compositor/compositor_vsync_manager.h"
40 #include "ui/gfx/display.h"
41 #include "ui/gfx/screen.h"
42 #include "ui/wm/public/activation_client.h"
44 #if defined(OS_CHROMEOS)
45 #include "ash/display/display_configurator_animation.h"
46 #include "base/sys_info.h"
47 #include "base/time/time.h"
48 #endif // defined(OS_CHROMEOS)
50 #if defined(USE_X11)
51 #include "ui/base/x/x11_util.h"
52 #include "ui/gfx/x/x11_types.h"
54 // Including this at the bottom to avoid other
55 // potential conflict with chrome headers.
56 #include <X11/extensions/Xrandr.h>
57 #undef RootWindow
58 #endif // defined(USE_X11)
60 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
61 #include "ui/events/ozone/chromeos/cursor_controller.h"
62 #endif
64 namespace ash {
65 namespace {
67 // Primary display stored in global object as it can be
68 // accessed after Shell is deleted. A separate display instance is created
69 // during the shutdown instead of always keeping two display instances
70 // (one here and another one in display_manager) in sync, which is error prone.
71 // This is initialized in the constructor, and then in CreatePrimaryHost().
72 int64 primary_display_id = -1;
74 // Specifies how long the display change should have been disabled
75 // after each display change operations.
76 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid
77 // changing the settings while the system is still configurating
78 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs|
79 // when the display change happens, so the actual timeout is much shorter.
80 const int64 kAfterDisplayChangeThrottleTimeoutMs = 500;
81 const int64 kCycleDisplayThrottleTimeoutMs = 4000;
82 const int64 kSwapDisplayThrottleTimeoutMs = 500;
84 #if defined(USE_OZONE) && defined(OS_CHROMEOS)
85 // Add 20% more cursor motion on non-integrated displays.
86 const float kCursorMultiplierForExternalDisplays = 1.2f;
87 #endif
89 DisplayManager* GetDisplayManager() {
90 return Shell::GetInstance()->display_manager();
93 void SetDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
94 const gfx::Display& display) {
95 DisplayInfo info = GetDisplayManager()->GetDisplayInfo(display.id());
96 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
97 #if defined(OS_CHROMEOS)
98 #if defined(USE_X11)
99 // Native window property (Atom in X11) that specifies the display's
100 // rotation, scale factor and if it's internal display. They are
101 // read and used by touchpad/mouse driver directly on X (contact
102 // adlr@ for more details on touchpad/mouse driver side). The value
103 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2
104 // (180 degree) or 3 (270 degrees clockwise). The value of the
105 // scale factor is in percent (100, 140, 200 etc).
106 const char kRotationProp[] = "_CHROME_DISPLAY_ROTATION";
107 const char kScaleFactorProp[] = "_CHROME_DISPLAY_SCALE_FACTOR";
108 const char kInternalProp[] = "_CHROME_DISPLAY_INTERNAL";
109 const char kCARDINAL[] = "CARDINAL";
110 int xrandr_rotation = RR_Rotate_0;
111 switch (info.rotation()) {
112 case gfx::Display::ROTATE_0:
113 xrandr_rotation = RR_Rotate_0;
114 break;
115 case gfx::Display::ROTATE_90:
116 xrandr_rotation = RR_Rotate_90;
117 break;
118 case gfx::Display::ROTATE_180:
119 xrandr_rotation = RR_Rotate_180;
120 break;
121 case gfx::Display::ROTATE_270:
122 xrandr_rotation = RR_Rotate_270;
123 break;
126 int internal = display.IsInternal() ? 1 : 0;
127 gfx::AcceleratedWidget xwindow = host->GetAcceleratedWidget();
128 ui::SetIntProperty(xwindow, kInternalProp, kCARDINAL, internal);
129 ui::SetIntProperty(xwindow, kRotationProp, kCARDINAL, xrandr_rotation);
130 ui::SetIntProperty(xwindow,
131 kScaleFactorProp,
132 kCARDINAL,
133 100 * display.device_scale_factor());
134 #elif defined(USE_OZONE)
135 // Scale all motion on High-DPI displays.
136 float scale = display.device_scale_factor();
138 if (!display.IsInternal())
139 scale *= kCursorMultiplierForExternalDisplays;
141 ui::CursorController::GetInstance()->SetCursorConfigForWindow(
142 host->GetAcceleratedWidget(), info.rotation(), scale);
143 #endif
144 #endif
145 scoped_ptr<RootWindowTransformer> transformer(
146 CreateRootWindowTransformerForDisplay(host->window(), display));
147 ash_host->SetRootWindowTransformer(transformer.Pass());
149 DisplayMode mode =
150 GetDisplayManager()->GetActiveModeForDisplayId(display.id());
151 if (mode.refresh_rate > 0.0f) {
152 host->compositor()->vsync_manager()->SetAuthoritativeVSyncInterval(
153 base::TimeDelta::FromMicroseconds(
154 base::Time::kMicrosecondsPerSecond / mode.refresh_rate));
157 // Just movnig the display requires the full redraw.
158 // chrome-os-partner:33558.
159 host->compositor()->ScheduleFullRedraw();
162 void ClearDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
163 const gfx::Display& display) {
164 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
165 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
166 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
167 host->GetAcceleratedWidget());
168 #endif
171 aura::Window* GetWindow(AshWindowTreeHost* ash_host) {
172 CHECK(ash_host->AsWindowTreeHost());
173 return ash_host->AsWindowTreeHost()->window();
176 } // namespace
178 // A utility class to store/restore focused/active window
179 // when the display configuration has changed.
180 class FocusActivationStore {
181 public:
182 FocusActivationStore()
183 : activation_client_(NULL),
184 capture_client_(NULL),
185 focus_client_(NULL),
186 focused_(NULL),
187 active_(NULL) {
190 void Store(bool clear_focus) {
191 if (!activation_client_) {
192 aura::Window* root = Shell::GetPrimaryRootWindow();
193 activation_client_ = aura::client::GetActivationClient(root);
194 capture_client_ = aura::client::GetCaptureClient(root);
195 focus_client_ = aura::client::GetFocusClient(root);
197 focused_ = focus_client_->GetFocusedWindow();
198 if (focused_)
199 tracker_.Add(focused_);
200 active_ = activation_client_->GetActiveWindow();
201 if (active_ && focused_ != active_)
202 tracker_.Add(active_);
204 // Deactivate the window to close menu / bubble windows.
205 if (clear_focus)
206 activation_client_->DeactivateWindow(active_);
208 // Release capture if any.
209 capture_client_->SetCapture(NULL);
210 // Clear the focused window if any. This is necessary because a
211 // window may be deleted when losing focus (fullscreen flash for
212 // example). If the focused window is still alive after move, it'll
213 // be re-focused below.
214 if (clear_focus)
215 focus_client_->FocusWindow(NULL);
218 void Restore() {
219 // Restore focused or active window if it's still alive.
220 if (focused_ && tracker_.Contains(focused_)) {
221 focus_client_->FocusWindow(focused_);
222 } else if (active_ && tracker_.Contains(active_)) {
223 activation_client_->ActivateWindow(active_);
225 if (focused_)
226 tracker_.Remove(focused_);
227 if (active_)
228 tracker_.Remove(active_);
229 focused_ = NULL;
230 active_ = NULL;
233 private:
234 aura::client::ActivationClient* activation_client_;
235 aura::client::CaptureClient* capture_client_;
236 aura::client::FocusClient* focus_client_;
237 aura::WindowTracker tracker_;
238 aura::Window* focused_;
239 aura::Window* active_;
241 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore);
244 ////////////////////////////////////////////////////////////////////////////////
245 // DisplayChangeLimiter
247 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
248 : throttle_timeout_(base::Time::Now()) {
251 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
252 int64 throttle_ms) {
253 throttle_timeout_ =
254 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms);
257 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
258 return base::Time::Now() < throttle_timeout_;
261 ////////////////////////////////////////////////////////////////////////////////
262 // DisplayController
264 DisplayController::DisplayController()
265 : primary_tree_host_for_replace_(NULL),
266 focus_activation_store_(new FocusActivationStore()),
267 cursor_window_controller_(new CursorWindowController()),
268 mirror_window_controller_(new MirrorWindowController()),
269 weak_ptr_factory_(this) {
270 #if defined(OS_CHROMEOS)
271 if (base::SysInfo::IsRunningOnChromeOS())
272 limiter_.reset(new DisplayChangeLimiter);
273 #endif
274 // Reset primary display to make sure that tests don't use
275 // stale display info from previous tests.
276 primary_display_id = gfx::Display::kInvalidDisplayID;
279 DisplayController::~DisplayController() {
282 void DisplayController::Start() {
283 Shell::GetScreen()->AddObserver(this);
284 Shell::GetInstance()->display_manager()->set_delegate(this);
287 void DisplayController::Shutdown() {
288 // Unset the display manager's delegate here because
289 // DisplayManager outlives DisplayController.
290 Shell::GetInstance()->display_manager()->set_delegate(NULL);
292 cursor_window_controller_.reset();
293 mirror_window_controller_.reset();
295 Shell::GetScreen()->RemoveObserver(this);
297 int64 primary_id = Shell::GetScreen()->GetPrimaryDisplay().id();
299 // Delete non primary root window controllers first, then
300 // delete the primary root window controller.
301 aura::Window::Windows root_windows = DisplayController::GetAllRootWindows();
302 std::vector<RootWindowController*> to_delete;
303 RootWindowController* primary_rwc = NULL;
304 for (aura::Window::Windows::iterator iter = root_windows.begin();
305 iter != root_windows.end();
306 ++iter) {
307 RootWindowController* rwc = GetRootWindowController(*iter);
308 if (GetRootWindowSettings(*iter)->display_id == primary_id)
309 primary_rwc = rwc;
310 else
311 to_delete.push_back(rwc);
313 CHECK(primary_rwc);
315 STLDeleteElements(&to_delete);
316 delete primary_rwc;
319 void DisplayController::CreatePrimaryHost(
320 const AshWindowTreeHostInitParams& init_params) {
321 const gfx::Display& primary_candidate =
322 GetDisplayManager()->GetPrimaryDisplayCandidate();
323 primary_display_id = primary_candidate.id();
324 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
325 AddWindowTreeHostForDisplay(primary_candidate, init_params);
328 void DisplayController::InitDisplays() {
329 RootWindowController::CreateForPrimaryDisplay(
330 window_tree_hosts_[primary_display_id]);
332 DisplayManager* display_manager = GetDisplayManager();
333 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
334 const gfx::Display& display = display_manager->GetDisplayAt(i);
335 if (primary_display_id != display.id()) {
336 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
337 display, AshWindowTreeHostInitParams());
338 RootWindowController::CreateForSecondaryDisplay(ash_host);
341 UpdateHostWindowNames();
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);
354 // static
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 CHECK_EQ(1u, window_tree_hosts_.count(id));
366 AshWindowTreeHost* host = window_tree_hosts_[id];
367 CHECK(host);
368 return GetWindow(host);
371 void DisplayController::CloseChildWindows() {
372 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
373 it != window_tree_hosts_.end();
374 ++it) {
375 aura::Window* root_window = GetWindow(it->second);
376 RootWindowController* controller = GetRootWindowController(root_window);
377 if (controller) {
378 controller->CloseChildWindows();
379 } else {
380 while (!root_window->children().empty()) {
381 aura::Window* child = root_window->children()[0];
382 delete child;
388 aura::Window::Windows DisplayController::GetAllRootWindows() {
389 aura::Window::Windows windows;
390 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
391 it != window_tree_hosts_.end();
392 ++it) {
393 DCHECK(it->second);
394 if (GetRootWindowController(GetWindow(it->second)))
395 windows.push_back(GetWindow(it->second));
397 return windows;
400 gfx::Insets DisplayController::GetOverscanInsets(int64 display_id) const {
401 return GetDisplayManager()->GetOverscanInsets(display_id);
404 void DisplayController::SetOverscanInsets(int64 display_id,
405 const gfx::Insets& insets_in_dip) {
406 GetDisplayManager()->SetOverscanInsets(display_id, insets_in_dip);
409 std::vector<RootWindowController*>
410 DisplayController::GetAllRootWindowControllers() {
411 std::vector<RootWindowController*> controllers;
412 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
413 it != window_tree_hosts_.end();
414 ++it) {
415 RootWindowController* controller =
416 GetRootWindowController(GetWindow(it->second));
417 if (controller)
418 controllers.push_back(controller);
420 return controllers;
423 void DisplayController::ToggleMirrorMode() {
424 DisplayManager* display_manager = GetDisplayManager();
425 if (display_manager->num_connected_displays() <= 1)
426 return;
428 if (limiter_) {
429 if (limiter_->IsThrottled())
430 return;
431 limiter_->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs);
433 #if defined(OS_CHROMEOS)
434 Shell* shell = Shell::GetInstance();
435 DisplayConfiguratorAnimation* animation =
436 shell->display_configurator_animation();
437 animation->StartFadeOutAnimation(
438 base::Bind(&DisplayController::SetMirrorModeAfterAnimation,
439 weak_ptr_factory_.GetWeakPtr(),
440 !display_manager->IsMirrored()));
441 #endif
444 void DisplayController::SwapPrimaryDisplay() {
445 if (limiter_) {
446 if (limiter_->IsThrottled())
447 return;
448 limiter_->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs);
451 if (Shell::GetScreen()->GetNumDisplays() > 1) {
452 #if defined(OS_CHROMEOS)
453 DisplayConfiguratorAnimation* animation =
454 Shell::GetInstance()->display_configurator_animation();
455 if (animation) {
456 animation->StartFadeOutAnimation(base::Bind(
457 &DisplayController::OnFadeOutForSwapDisplayFinished,
458 weak_ptr_factory_.GetWeakPtr()));
459 } else {
460 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
462 #else
463 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
464 #endif
468 void DisplayController::SetPrimaryDisplayId(int64 id) {
469 DCHECK_NE(gfx::Display::kInvalidDisplayID, id);
470 if (id == gfx::Display::kInvalidDisplayID || primary_display_id == id)
471 return;
473 const gfx::Display& display = GetDisplayManager()->GetDisplayForId(id);
474 if (display.is_valid())
475 SetPrimaryDisplay(display);
478 void DisplayController::SetPrimaryDisplay(
479 const gfx::Display& new_primary_display) {
480 DisplayManager* display_manager = GetDisplayManager();
481 DCHECK(new_primary_display.is_valid());
482 DCHECK(display_manager->IsActiveDisplay(new_primary_display));
484 if (!new_primary_display.is_valid() ||
485 !display_manager->IsActiveDisplay(new_primary_display)) {
486 LOG(ERROR) << "Invalid or non-existent display is requested:"
487 << new_primary_display.ToString();
488 return;
491 if (primary_display_id == new_primary_display.id() ||
492 window_tree_hosts_.size() < 2) {
493 return;
496 AshWindowTreeHost* non_primary_host =
497 window_tree_hosts_[new_primary_display.id()];
498 LOG_IF(ERROR, !non_primary_host)
499 << "Unknown display is requested in SetPrimaryDisplay: id="
500 << new_primary_display.id();
501 if (!non_primary_host)
502 return;
504 gfx::Display old_primary_display = Shell::GetScreen()->GetPrimaryDisplay();
506 // Swap root windows between current and new primary display.
507 AshWindowTreeHost* primary_host = window_tree_hosts_[primary_display_id];
508 CHECK(primary_host);
509 CHECK_NE(primary_host, non_primary_host);
511 window_tree_hosts_[new_primary_display.id()] = primary_host;
512 GetRootWindowSettings(GetWindow(primary_host))->display_id =
513 new_primary_display.id();
515 window_tree_hosts_[old_primary_display.id()] = non_primary_host;
516 GetRootWindowSettings(GetWindow(non_primary_host))->display_id =
517 old_primary_display.id();
519 primary_display_id = new_primary_display.id();
520 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
521 display_manager->GetCurrentDisplayIdPair(), primary_display_id);
523 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host),
524 old_primary_display.GetWorkAreaInsets());
525 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host),
526 new_primary_display.GetWorkAreaInsets());
528 // Update the dispay manager with new display info.
529 std::vector<DisplayInfo> display_info_list;
530 display_info_list.push_back(display_manager->GetDisplayInfo(
531 primary_display_id));
532 display_info_list.push_back(display_manager->GetDisplayInfo(
533 ScreenUtil::GetSecondaryDisplay().id()));
534 GetDisplayManager()->set_force_bounds_changed(true);
535 GetDisplayManager()->UpdateDisplays(display_info_list);
536 GetDisplayManager()->set_force_bounds_changed(false);
539 void DisplayController::EnsurePointerInDisplays() {
540 // If the mouse is currently on a display in native location,
541 // use the same native location. Otherwise find the display closest
542 // to the current cursor location in screen coordinates.
544 gfx::Point point_in_screen = Shell::GetScreen()->GetCursorScreenPoint();
545 gfx::Point target_location_in_native;
546 int64 closest_distance_squared = -1;
547 DisplayManager* display_manager = GetDisplayManager();
549 aura::Window* dst_root_window = NULL;
550 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
551 const gfx::Display& display = display_manager->GetDisplayAt(i);
552 const DisplayInfo display_info =
553 display_manager->GetDisplayInfo(display.id());
554 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
555 if (display_info.bounds_in_native().Contains(
556 cursor_location_in_native_coords_for_restore_)) {
557 dst_root_window = root_window;
558 target_location_in_native = cursor_location_in_native_coords_for_restore_;
559 break;
561 gfx::Point center = display.bounds().CenterPoint();
562 // Use the distance squared from the center of the dislay. This is not
563 // exactly "closest" display, but good enough to pick one
564 // appropriate (and there are at most two displays).
565 // We don't care about actual distance, only relative to other displays, so
566 // using the LengthSquared() is cheaper than Length().
568 int64 distance_squared = (center - point_in_screen).LengthSquared();
569 if (closest_distance_squared < 0 ||
570 closest_distance_squared > distance_squared) {
571 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
572 aura::client::ScreenPositionClient* client =
573 aura::client::GetScreenPositionClient(root_window);
574 client->ConvertPointFromScreen(root_window, &center);
575 root_window->GetHost()->ConvertPointToNativeScreen(&center);
576 dst_root_window = root_window;
577 target_location_in_native = center;
578 closest_distance_squared = distance_squared;
581 dst_root_window->GetHost()->ConvertPointFromNativeScreen(
582 &target_location_in_native);
583 dst_root_window->MoveCursorTo(target_location_in_native);
586 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
587 const aura::Window* window,
588 const gfx::Insets& insets) {
589 const aura::Window* root_window = window->GetRootWindow();
590 int64 id = GetRootWindowSettings(root_window)->display_id;
591 // if id is |kInvaildDisplayID|, it's being deleted.
592 DCHECK(id != gfx::Display::kInvalidDisplayID);
593 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id, insets);
596 void DisplayController::OnDisplayAdded(const gfx::Display& display) {
597 if (primary_tree_host_for_replace_) {
598 DCHECK(window_tree_hosts_.empty());
599 primary_display_id = display.id();
600 window_tree_hosts_[display.id()] = primary_tree_host_for_replace_;
601 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_))
602 ->display_id = display.id();
603 primary_tree_host_for_replace_ = NULL;
604 const DisplayInfo& display_info =
605 GetDisplayManager()->GetDisplayInfo(display.id());
606 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
607 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
608 SetDisplayPropertiesOnHost(ash_host, display);
609 } else {
610 if (primary_display_id == gfx::Display::kInvalidDisplayID)
611 primary_display_id = display.id();
612 DCHECK(!window_tree_hosts_.empty());
613 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
614 display, AshWindowTreeHostInitParams());
615 RootWindowController::CreateForSecondaryDisplay(ash_host);
619 void DisplayController::OnDisplayRemoved(const gfx::Display& display) {
620 AshWindowTreeHost* host_to_delete = window_tree_hosts_[display.id()];
621 CHECK(host_to_delete) << display.ToString();
623 // Display for root window will be deleted when the Primary RootWindow
624 // is deleted by the Shell.
625 window_tree_hosts_.erase(display.id());
627 // When the primary root window's display is removed, move the primary
628 // root to the other display.
629 if (primary_display_id == display.id()) {
630 // Temporarily store the primary root window in
631 // |primary_root_window_for_replace_| when replacing the display.
632 if (window_tree_hosts_.size() == 0) {
633 primary_display_id = gfx::Display::kInvalidDisplayID;
634 primary_tree_host_for_replace_ = host_to_delete;
635 return;
637 primary_display_id = window_tree_hosts_.begin()->first;
638 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
640 AshWindowTreeHost* primary_host = host_to_delete;
641 // Delete the other host instead.
642 host_to_delete = window_tree_hosts_[primary_display_id];
643 GetRootWindowSettings(GetWindow(host_to_delete))->display_id = display.id();
645 // Setup primary root.
646 window_tree_hosts_[primary_display_id] = primary_host;
647 GetRootWindowSettings(GetWindow(primary_host))->display_id =
648 primary_display_id;
650 OnDisplayMetricsChanged(
651 GetDisplayManager()->GetDisplayForId(primary_display_id),
652 DISPLAY_METRIC_BOUNDS);
654 ClearDisplayPropertiesOnHost(host_to_delete, display);
655 RootWindowController* controller =
656 GetRootWindowController(GetWindow(host_to_delete));
657 DCHECK(controller);
658 controller->MoveWindowsTo(GetPrimaryRootWindow());
659 // Delete most of root window related objects, but don't delete
660 // root window itself yet because the stack may be using it.
661 controller->Shutdown();
662 base::MessageLoop::current()->DeleteSoon(FROM_HERE, controller);
665 void DisplayController::OnDisplayMetricsChanged(const gfx::Display& display,
666 uint32_t metrics) {
667 if (!(metrics & (DISPLAY_METRIC_BOUNDS | DISPLAY_METRIC_ROTATION |
668 DISPLAY_METRIC_DEVICE_SCALE_FACTOR)))
669 return;
670 const DisplayInfo& display_info =
671 GetDisplayManager()->GetDisplayInfo(display.id());
672 DCHECK(!display_info.bounds_in_native().IsEmpty());
673 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
674 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
675 SetDisplayPropertiesOnHost(ash_host, display);
678 void DisplayController::OnHostResized(const aura::WindowTreeHost* host) {
679 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
680 const_cast<aura::Window*>(host->window()));
682 DisplayManager* display_manager = GetDisplayManager();
683 if (display_manager->UpdateDisplayBounds(display.id(), host->GetBounds())) {
684 mirror_window_controller_->UpdateWindow();
685 cursor_window_controller_->UpdateContainer();
689 void DisplayController::CreateOrUpdateMirroringDisplay(
690 const DisplayInfo& info) {
691 switch (GetDisplayManager()->second_display_mode()) {
692 case DisplayManager::MIRRORING:
693 mirror_window_controller_->UpdateWindow(info);
694 cursor_window_controller_->UpdateContainer();
695 break;
696 case DisplayManager::EXTENDED:
697 NOTREACHED();
701 void DisplayController::CloseMirroringDisplay() {
702 mirror_window_controller_->Close();
703 // If cursor_compositing is enabled for large cursor, the cursor window is
704 // always on the desktop display (the visible cursor on the non-desktop
705 // display is drawn through compositor mirroring). Therefore, it's unnecessary
706 // to handle the cursor_window at all. See: http://crbug.com/412910
707 if (!cursor_window_controller_->is_cursor_compositing_enabled())
708 cursor_window_controller_->UpdateContainer();
711 void DisplayController::PreDisplayConfigurationChange(bool clear_focus) {
712 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanging());
713 focus_activation_store_->Store(clear_focus);
714 gfx::Screen* screen = Shell::GetScreen();
715 gfx::Point point_in_screen = screen->GetCursorScreenPoint();
716 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
717 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
719 aura::client::ScreenPositionClient* client =
720 aura::client::GetScreenPositionClient(root_window);
721 client->ConvertPointFromScreen(root_window, &point_in_screen);
722 root_window->GetHost()->ConvertPointToNativeScreen(&point_in_screen);
723 cursor_location_in_native_coords_for_restore_ = point_in_screen;
726 void DisplayController::PostDisplayConfigurationChange() {
727 if (limiter_)
728 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs);
730 focus_activation_store_->Restore();
732 DisplayManager* display_manager = GetDisplayManager();
733 DisplayLayoutStore* layout_store = display_manager->layout_store();
734 if (display_manager->num_connected_displays() > 1) {
735 DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair();
736 layout_store->UpdateMirrorStatus(pair, display_manager->IsMirrored());
737 DisplayLayout layout = layout_store->GetRegisteredDisplayLayout(pair);
739 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
740 int64 primary_id = layout.primary_id;
741 SetPrimaryDisplayId(
742 primary_id == gfx::Display::kInvalidDisplayID ?
743 pair.first : primary_id);
744 // Update the primary_id in case the above call is
745 // ignored. Happens when a) default layout's primary id
746 // doesn't exist, or b) the primary_id has already been
747 // set to the same and didn't update it.
748 layout_store->UpdatePrimaryDisplayId(
749 pair, Shell::GetScreen()->GetPrimaryDisplay().id());
752 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanged());
753 UpdateHostWindowNames();
754 EnsurePointerInDisplays();
757 AshWindowTreeHost* DisplayController::AddWindowTreeHostForDisplay(
758 const gfx::Display& display,
759 const AshWindowTreeHostInitParams& init_params) {
760 static int host_count = 0;
761 const DisplayInfo& display_info =
762 GetDisplayManager()->GetDisplayInfo(display.id());
763 AshWindowTreeHostInitParams params_with_bounds(init_params);
764 params_with_bounds.initial_bounds = display_info.bounds_in_native();
765 AshWindowTreeHost* ash_host = AshWindowTreeHost::Create(params_with_bounds);
766 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
768 host->window()->SetName(base::StringPrintf("RootWindow-%d", host_count++));
769 host->window()->SetTitle(base::UTF8ToUTF16(display_info.name()));
770 host->compositor()->SetBackgroundColor(SK_ColorBLACK);
771 // No need to remove our observer observer because the DisplayController
772 // outlives the host.
773 host->AddObserver(this);
774 InitRootWindowSettings(host->window())->display_id = display.id();
775 host->InitHost();
777 window_tree_hosts_[display.id()] = ash_host;
778 SetDisplayPropertiesOnHost(ash_host, display);
780 #if defined(OS_CHROMEOS)
781 static bool force_constrain_pointer_to_root =
782 base::CommandLine::ForCurrentProcess()->HasSwitch(
783 switches::kAshConstrainPointerToRoot);
784 if (base::SysInfo::IsRunningOnChromeOS() || force_constrain_pointer_to_root)
785 ash_host->ConfineCursorToRootWindow();
786 #endif
787 return ash_host;
790 void DisplayController::OnFadeOutForSwapDisplayFinished() {
791 #if defined(OS_CHROMEOS)
792 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
793 Shell::GetInstance()->display_configurator_animation()
794 ->StartFadeInAnimation();
795 #endif
798 void DisplayController::SetMirrorModeAfterAnimation(bool mirror) {
799 GetDisplayManager()->SetMirrorMode(mirror);
802 void DisplayController::UpdateHostWindowNames() {
803 #if defined(USE_X11)
804 // crbug.com/120229 - set the window title for the primary dislpay
805 // to "aura_root_0" so gtalk can find the primary root window to broadcast.
806 // TODO(jhorwich) Remove this once Chrome supports window-based broadcasting.
807 aura::Window* primary = Shell::GetPrimaryRootWindow();
808 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
809 for (size_t i = 0; i < root_windows.size(); ++i) {
810 std::string name =
811 root_windows[i] == primary ? "aura_root_0" : "aura_root_x";
812 gfx::AcceleratedWidget xwindow =
813 root_windows[i]->GetHost()->GetAcceleratedWidget();
814 XStoreName(gfx::GetXDisplay(), xwindow, name.c_str());
816 #endif
819 } // namespace ash