Cronet: Remove redundant success checks and waits in tests.
[chromium-blink-merge.git] / ash / display / display_controller.cc
blobb5b70c3f987bd21bc984f27f5094cb9844940bc0
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));
158 void ClearDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
159 const gfx::Display& display) {
160 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
161 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
162 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
163 host->GetAcceleratedWidget());
164 #endif
167 aura::Window* GetWindow(AshWindowTreeHost* ash_host) {
168 CHECK(ash_host->AsWindowTreeHost());
169 return ash_host->AsWindowTreeHost()->window();
172 } // namespace
174 // A utility class to store/restore focused/active window
175 // when the display configuration has changed.
176 class FocusActivationStore {
177 public:
178 FocusActivationStore()
179 : activation_client_(NULL),
180 capture_client_(NULL),
181 focus_client_(NULL),
182 focused_(NULL),
183 active_(NULL) {
186 void Store(bool clear_focus) {
187 if (!activation_client_) {
188 aura::Window* root = Shell::GetPrimaryRootWindow();
189 activation_client_ = aura::client::GetActivationClient(root);
190 capture_client_ = aura::client::GetCaptureClient(root);
191 focus_client_ = aura::client::GetFocusClient(root);
193 focused_ = focus_client_->GetFocusedWindow();
194 if (focused_)
195 tracker_.Add(focused_);
196 active_ = activation_client_->GetActiveWindow();
197 if (active_ && focused_ != active_)
198 tracker_.Add(active_);
200 // Deactivate the window to close menu / bubble windows.
201 if (clear_focus)
202 activation_client_->DeactivateWindow(active_);
204 // Release capture if any.
205 capture_client_->SetCapture(NULL);
206 // Clear the focused window if any. This is necessary because a
207 // window may be deleted when losing focus (fullscreen flash for
208 // example). If the focused window is still alive after move, it'll
209 // be re-focused below.
210 if (clear_focus)
211 focus_client_->FocusWindow(NULL);
214 void Restore() {
215 // Restore focused or active window if it's still alive.
216 if (focused_ && tracker_.Contains(focused_)) {
217 focus_client_->FocusWindow(focused_);
218 } else if (active_ && tracker_.Contains(active_)) {
219 activation_client_->ActivateWindow(active_);
221 if (focused_)
222 tracker_.Remove(focused_);
223 if (active_)
224 tracker_.Remove(active_);
225 focused_ = NULL;
226 active_ = NULL;
229 private:
230 aura::client::ActivationClient* activation_client_;
231 aura::client::CaptureClient* capture_client_;
232 aura::client::FocusClient* focus_client_;
233 aura::WindowTracker tracker_;
234 aura::Window* focused_;
235 aura::Window* active_;
237 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore);
240 ////////////////////////////////////////////////////////////////////////////////
241 // DisplayChangeLimiter
243 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
244 : throttle_timeout_(base::Time::Now()) {
247 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
248 int64 throttle_ms) {
249 throttle_timeout_ =
250 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms);
253 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
254 return base::Time::Now() < throttle_timeout_;
257 ////////////////////////////////////////////////////////////////////////////////
258 // DisplayController
260 DisplayController::DisplayController()
261 : primary_tree_host_for_replace_(NULL),
262 focus_activation_store_(new FocusActivationStore()),
263 cursor_window_controller_(new CursorWindowController()),
264 mirror_window_controller_(new MirrorWindowController()),
265 weak_ptr_factory_(this) {
266 #if defined(OS_CHROMEOS)
267 if (base::SysInfo::IsRunningOnChromeOS())
268 limiter_.reset(new DisplayChangeLimiter);
269 #endif
270 // Reset primary display to make sure that tests don't use
271 // stale display info from previous tests.
272 primary_display_id = gfx::Display::kInvalidDisplayID;
275 DisplayController::~DisplayController() {
278 void DisplayController::Start() {
279 Shell::GetScreen()->AddObserver(this);
280 Shell::GetInstance()->display_manager()->set_delegate(this);
283 void DisplayController::Shutdown() {
284 // Unset the display manager's delegate here because
285 // DisplayManager outlives DisplayController.
286 Shell::GetInstance()->display_manager()->set_delegate(NULL);
288 cursor_window_controller_.reset();
289 mirror_window_controller_.reset();
291 Shell::GetScreen()->RemoveObserver(this);
293 int64 primary_id = Shell::GetScreen()->GetPrimaryDisplay().id();
295 // Delete non primary root window controllers first, then
296 // delete the primary root window controller.
297 aura::Window::Windows root_windows = DisplayController::GetAllRootWindows();
298 std::vector<RootWindowController*> to_delete;
299 RootWindowController* primary_rwc = NULL;
300 for (aura::Window::Windows::iterator iter = root_windows.begin();
301 iter != root_windows.end();
302 ++iter) {
303 RootWindowController* rwc = GetRootWindowController(*iter);
304 if (GetRootWindowSettings(*iter)->display_id == primary_id)
305 primary_rwc = rwc;
306 else
307 to_delete.push_back(rwc);
309 CHECK(primary_rwc);
311 STLDeleteElements(&to_delete);
312 delete primary_rwc;
315 void DisplayController::CreatePrimaryHost(
316 const AshWindowTreeHostInitParams& init_params) {
317 const gfx::Display& primary_candidate =
318 GetDisplayManager()->GetPrimaryDisplayCandidate();
319 primary_display_id = primary_candidate.id();
320 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
321 AddWindowTreeHostForDisplay(primary_candidate, init_params);
324 void DisplayController::InitDisplays() {
325 RootWindowController::CreateForPrimaryDisplay(
326 window_tree_hosts_[primary_display_id]);
328 DisplayManager* display_manager = GetDisplayManager();
329 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
330 const gfx::Display& display = display_manager->GetDisplayAt(i);
331 if (primary_display_id != display.id()) {
332 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
333 display, AshWindowTreeHostInitParams());
334 RootWindowController::CreateForSecondaryDisplay(ash_host);
337 UpdateHostWindowNames();
339 FOR_EACH_OBSERVER(Observer, observers_, OnDisplaysInitialized());
342 void DisplayController::AddObserver(Observer* observer) {
343 observers_.AddObserver(observer);
346 void DisplayController::RemoveObserver(Observer* observer) {
347 observers_.RemoveObserver(observer);
350 // static
351 int64 DisplayController::GetPrimaryDisplayId() {
352 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
353 return primary_display_id;
356 aura::Window* DisplayController::GetPrimaryRootWindow() {
357 return GetRootWindowForDisplayId(primary_display_id);
360 aura::Window* DisplayController::GetRootWindowForDisplayId(int64 id) {
361 CHECK_EQ(1u, window_tree_hosts_.count(id));
362 AshWindowTreeHost* host = window_tree_hosts_[id];
363 CHECK(host);
364 return GetWindow(host);
367 void DisplayController::CloseChildWindows() {
368 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
369 it != window_tree_hosts_.end();
370 ++it) {
371 aura::Window* root_window = GetWindow(it->second);
372 RootWindowController* controller = GetRootWindowController(root_window);
373 if (controller) {
374 controller->CloseChildWindows();
375 } else {
376 while (!root_window->children().empty()) {
377 aura::Window* child = root_window->children()[0];
378 delete child;
384 aura::Window::Windows DisplayController::GetAllRootWindows() {
385 aura::Window::Windows windows;
386 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
387 it != window_tree_hosts_.end();
388 ++it) {
389 DCHECK(it->second);
390 if (GetRootWindowController(GetWindow(it->second)))
391 windows.push_back(GetWindow(it->second));
393 return windows;
396 gfx::Insets DisplayController::GetOverscanInsets(int64 display_id) const {
397 return GetDisplayManager()->GetOverscanInsets(display_id);
400 void DisplayController::SetOverscanInsets(int64 display_id,
401 const gfx::Insets& insets_in_dip) {
402 GetDisplayManager()->SetOverscanInsets(display_id, insets_in_dip);
405 std::vector<RootWindowController*>
406 DisplayController::GetAllRootWindowControllers() {
407 std::vector<RootWindowController*> controllers;
408 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
409 it != window_tree_hosts_.end();
410 ++it) {
411 RootWindowController* controller =
412 GetRootWindowController(GetWindow(it->second));
413 if (controller)
414 controllers.push_back(controller);
416 return controllers;
419 void DisplayController::ToggleMirrorMode() {
420 DisplayManager* display_manager = GetDisplayManager();
421 if (display_manager->num_connected_displays() <= 1)
422 return;
424 if (limiter_) {
425 if (limiter_->IsThrottled())
426 return;
427 limiter_->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs);
429 #if defined(OS_CHROMEOS)
430 Shell* shell = Shell::GetInstance();
431 DisplayConfiguratorAnimation* animation =
432 shell->display_configurator_animation();
433 animation->StartFadeOutAnimation(
434 base::Bind(&DisplayController::SetMirrorModeAfterAnimation,
435 weak_ptr_factory_.GetWeakPtr(),
436 !display_manager->IsMirrored()));
437 #endif
440 void DisplayController::SwapPrimaryDisplay() {
441 if (limiter_) {
442 if (limiter_->IsThrottled())
443 return;
444 limiter_->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs);
447 if (Shell::GetScreen()->GetNumDisplays() > 1) {
448 #if defined(OS_CHROMEOS)
449 DisplayConfiguratorAnimation* animation =
450 Shell::GetInstance()->display_configurator_animation();
451 if (animation) {
452 animation->StartFadeOutAnimation(base::Bind(
453 &DisplayController::OnFadeOutForSwapDisplayFinished,
454 weak_ptr_factory_.GetWeakPtr()));
455 } else {
456 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
458 #else
459 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
460 #endif
464 void DisplayController::SetPrimaryDisplayId(int64 id) {
465 DCHECK_NE(gfx::Display::kInvalidDisplayID, id);
466 if (id == gfx::Display::kInvalidDisplayID || primary_display_id == id)
467 return;
469 const gfx::Display& display = GetDisplayManager()->GetDisplayForId(id);
470 if (display.is_valid())
471 SetPrimaryDisplay(display);
474 void DisplayController::SetPrimaryDisplay(
475 const gfx::Display& new_primary_display) {
476 DisplayManager* display_manager = GetDisplayManager();
477 DCHECK(new_primary_display.is_valid());
478 DCHECK(display_manager->IsActiveDisplay(new_primary_display));
480 if (!new_primary_display.is_valid() ||
481 !display_manager->IsActiveDisplay(new_primary_display)) {
482 LOG(ERROR) << "Invalid or non-existent display is requested:"
483 << new_primary_display.ToString();
484 return;
487 if (primary_display_id == new_primary_display.id() ||
488 window_tree_hosts_.size() < 2) {
489 return;
492 AshWindowTreeHost* non_primary_host =
493 window_tree_hosts_[new_primary_display.id()];
494 LOG_IF(ERROR, !non_primary_host)
495 << "Unknown display is requested in SetPrimaryDisplay: id="
496 << new_primary_display.id();
497 if (!non_primary_host)
498 return;
500 gfx::Display old_primary_display = Shell::GetScreen()->GetPrimaryDisplay();
502 // Swap root windows between current and new primary display.
503 AshWindowTreeHost* primary_host = window_tree_hosts_[primary_display_id];
504 CHECK(primary_host);
505 CHECK_NE(primary_host, non_primary_host);
507 window_tree_hosts_[new_primary_display.id()] = primary_host;
508 GetRootWindowSettings(GetWindow(primary_host))->display_id =
509 new_primary_display.id();
511 window_tree_hosts_[old_primary_display.id()] = non_primary_host;
512 GetRootWindowSettings(GetWindow(non_primary_host))->display_id =
513 old_primary_display.id();
515 primary_display_id = new_primary_display.id();
516 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
517 display_manager->GetCurrentDisplayIdPair(), primary_display_id);
519 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host),
520 old_primary_display.GetWorkAreaInsets());
521 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host),
522 new_primary_display.GetWorkAreaInsets());
524 // Update the dispay manager with new display info.
525 std::vector<DisplayInfo> display_info_list;
526 display_info_list.push_back(display_manager->GetDisplayInfo(
527 primary_display_id));
528 display_info_list.push_back(display_manager->GetDisplayInfo(
529 ScreenUtil::GetSecondaryDisplay().id()));
530 GetDisplayManager()->set_force_bounds_changed(true);
531 GetDisplayManager()->UpdateDisplays(display_info_list);
532 GetDisplayManager()->set_force_bounds_changed(false);
535 void DisplayController::EnsurePointerInDisplays() {
536 // If the mouse is currently on a display in native location,
537 // use the same native location. Otherwise find the display closest
538 // to the current cursor location in screen coordinates.
540 gfx::Point point_in_screen = Shell::GetScreen()->GetCursorScreenPoint();
541 gfx::Point target_location_in_native;
542 int64 closest_distance_squared = -1;
543 DisplayManager* display_manager = GetDisplayManager();
545 aura::Window* dst_root_window = NULL;
546 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
547 const gfx::Display& display = display_manager->GetDisplayAt(i);
548 const DisplayInfo display_info =
549 display_manager->GetDisplayInfo(display.id());
550 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
551 if (display_info.bounds_in_native().Contains(
552 cursor_location_in_native_coords_for_restore_)) {
553 dst_root_window = root_window;
554 target_location_in_native = cursor_location_in_native_coords_for_restore_;
555 break;
557 gfx::Point center = display.bounds().CenterPoint();
558 // Use the distance squared from the center of the dislay. This is not
559 // exactly "closest" display, but good enough to pick one
560 // appropriate (and there are at most two displays).
561 // We don't care about actual distance, only relative to other displays, so
562 // using the LengthSquared() is cheaper than Length().
564 int64 distance_squared = (center - point_in_screen).LengthSquared();
565 if (closest_distance_squared < 0 ||
566 closest_distance_squared > distance_squared) {
567 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
568 aura::client::ScreenPositionClient* client =
569 aura::client::GetScreenPositionClient(root_window);
570 client->ConvertPointFromScreen(root_window, &center);
571 root_window->GetHost()->ConvertPointToNativeScreen(&center);
572 dst_root_window = root_window;
573 target_location_in_native = center;
574 closest_distance_squared = distance_squared;
577 dst_root_window->GetHost()->ConvertPointFromNativeScreen(
578 &target_location_in_native);
579 dst_root_window->MoveCursorTo(target_location_in_native);
582 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
583 const aura::Window* window,
584 const gfx::Insets& insets) {
585 const aura::Window* root_window = window->GetRootWindow();
586 int64 id = GetRootWindowSettings(root_window)->display_id;
587 // if id is |kInvaildDisplayID|, it's being deleted.
588 DCHECK(id != gfx::Display::kInvalidDisplayID);
589 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id, insets);
592 void DisplayController::OnDisplayAdded(const gfx::Display& display) {
593 if (primary_tree_host_for_replace_) {
594 DCHECK(window_tree_hosts_.empty());
595 primary_display_id = display.id();
596 window_tree_hosts_[display.id()] = primary_tree_host_for_replace_;
597 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_))
598 ->display_id = display.id();
599 primary_tree_host_for_replace_ = NULL;
600 const DisplayInfo& display_info =
601 GetDisplayManager()->GetDisplayInfo(display.id());
602 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
603 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
604 SetDisplayPropertiesOnHost(ash_host, display);
605 } else {
606 if (primary_display_id == gfx::Display::kInvalidDisplayID)
607 primary_display_id = display.id();
608 DCHECK(!window_tree_hosts_.empty());
609 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
610 display, AshWindowTreeHostInitParams());
611 RootWindowController::CreateForSecondaryDisplay(ash_host);
615 void DisplayController::OnDisplayRemoved(const gfx::Display& display) {
616 AshWindowTreeHost* host_to_delete = window_tree_hosts_[display.id()];
617 CHECK(host_to_delete) << display.ToString();
619 // Display for root window will be deleted when the Primary RootWindow
620 // is deleted by the Shell.
621 window_tree_hosts_.erase(display.id());
623 // When the primary root window's display is removed, move the primary
624 // root to the other display.
625 if (primary_display_id == display.id()) {
626 // Temporarily store the primary root window in
627 // |primary_root_window_for_replace_| when replacing the display.
628 if (window_tree_hosts_.size() == 0) {
629 primary_display_id = gfx::Display::kInvalidDisplayID;
630 primary_tree_host_for_replace_ = host_to_delete;
631 return;
633 DCHECK_EQ(1U, window_tree_hosts_.size());
634 primary_display_id = ScreenUtil::GetSecondaryDisplay().id();
635 AshWindowTreeHost* primary_host = host_to_delete;
637 // Delete the other host instead.
638 host_to_delete = window_tree_hosts_[primary_display_id];
639 GetRootWindowSettings(GetWindow(host_to_delete))->display_id = display.id();
641 // Setup primary root.
642 window_tree_hosts_[primary_display_id] = primary_host;
643 GetRootWindowSettings(GetWindow(primary_host))->display_id =
644 primary_display_id;
646 OnDisplayMetricsChanged(
647 GetDisplayManager()->GetDisplayForId(primary_display_id),
648 DISPLAY_METRIC_BOUNDS);
650 ClearDisplayPropertiesOnHost(host_to_delete, display);
651 RootWindowController* controller =
652 GetRootWindowController(GetWindow(host_to_delete));
653 DCHECK(controller);
654 controller->MoveWindowsTo(GetPrimaryRootWindow());
655 // Delete most of root window related objects, but don't delete
656 // root window itself yet because the stack may be using it.
657 controller->Shutdown();
658 base::MessageLoop::current()->DeleteSoon(FROM_HERE, controller);
661 void DisplayController::OnDisplayMetricsChanged(const gfx::Display& display,
662 uint32_t metrics) {
663 if (!(metrics & (DISPLAY_METRIC_BOUNDS | DISPLAY_METRIC_ROTATION |
664 DISPLAY_METRIC_DEVICE_SCALE_FACTOR)))
665 return;
666 const DisplayInfo& display_info =
667 GetDisplayManager()->GetDisplayInfo(display.id());
668 DCHECK(!display_info.bounds_in_native().IsEmpty());
669 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
670 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
671 SetDisplayPropertiesOnHost(ash_host, display);
674 void DisplayController::OnHostResized(const aura::WindowTreeHost* host) {
675 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
676 const_cast<aura::Window*>(host->window()));
678 DisplayManager* display_manager = GetDisplayManager();
679 if (display_manager->UpdateDisplayBounds(display.id(), host->GetBounds())) {
680 mirror_window_controller_->UpdateWindow();
681 cursor_window_controller_->UpdateContainer();
685 void DisplayController::CreateOrUpdateMirroringDisplay(
686 const DisplayInfo& info) {
687 switch (GetDisplayManager()->second_display_mode()) {
688 case DisplayManager::MIRRORING:
689 mirror_window_controller_->UpdateWindow(info);
690 cursor_window_controller_->UpdateContainer();
691 break;
692 case DisplayManager::EXTENDED:
693 NOTREACHED();
697 void DisplayController::CloseMirroringDisplay() {
698 mirror_window_controller_->Close();
699 // If cursor_compositing is enabled for large cursor, the cursor window is
700 // always on the desktop display (the visible cursor on the non-desktop
701 // display is drawn through compositor mirroring). Therefore, it's unnecessary
702 // to handle the cursor_window at all. See: http://crbug.com/412910
703 if (!cursor_window_controller_->is_cursor_compositing_enabled())
704 cursor_window_controller_->UpdateContainer();
707 void DisplayController::PreDisplayConfigurationChange(bool clear_focus) {
708 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanging());
709 focus_activation_store_->Store(clear_focus);
710 gfx::Screen* screen = Shell::GetScreen();
711 gfx::Point point_in_screen = screen->GetCursorScreenPoint();
712 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
713 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
715 aura::client::ScreenPositionClient* client =
716 aura::client::GetScreenPositionClient(root_window);
717 client->ConvertPointFromScreen(root_window, &point_in_screen);
718 root_window->GetHost()->ConvertPointToNativeScreen(&point_in_screen);
719 cursor_location_in_native_coords_for_restore_ = point_in_screen;
722 void DisplayController::PostDisplayConfigurationChange() {
723 if (limiter_)
724 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs);
726 focus_activation_store_->Restore();
728 DisplayManager* display_manager = GetDisplayManager();
729 DisplayLayoutStore* layout_store = display_manager->layout_store();
730 if (display_manager->num_connected_displays() > 1) {
731 DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair();
732 layout_store->UpdateMirrorStatus(pair, display_manager->IsMirrored());
733 DisplayLayout layout = layout_store->GetRegisteredDisplayLayout(pair);
735 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
736 int64 primary_id = layout.primary_id;
737 SetPrimaryDisplayId(
738 primary_id == gfx::Display::kInvalidDisplayID ?
739 pair.first : primary_id);
740 // Update the primary_id in case the above call is
741 // ignored. Happens when a) default layout's primary id
742 // doesn't exist, or b) the primary_id has already been
743 // set to the same and didn't update it.
744 layout_store->UpdatePrimaryDisplayId(
745 pair, Shell::GetScreen()->GetPrimaryDisplay().id());
748 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanged());
749 UpdateHostWindowNames();
750 EnsurePointerInDisplays();
753 AshWindowTreeHost* DisplayController::AddWindowTreeHostForDisplay(
754 const gfx::Display& display,
755 const AshWindowTreeHostInitParams& init_params) {
756 static int host_count = 0;
757 const DisplayInfo& display_info =
758 GetDisplayManager()->GetDisplayInfo(display.id());
759 AshWindowTreeHostInitParams params_with_bounds(init_params);
760 params_with_bounds.initial_bounds = display_info.bounds_in_native();
761 AshWindowTreeHost* ash_host = AshWindowTreeHost::Create(params_with_bounds);
762 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
764 host->window()->SetName(base::StringPrintf("RootWindow-%d", host_count++));
765 host->window()->SetTitle(base::UTF8ToUTF16(display_info.name()));
766 host->compositor()->SetBackgroundColor(SK_ColorBLACK);
767 // No need to remove our observer observer because the DisplayController
768 // outlives the host.
769 host->AddObserver(this);
770 InitRootWindowSettings(host->window())->display_id = display.id();
771 host->InitHost();
773 window_tree_hosts_[display.id()] = ash_host;
774 SetDisplayPropertiesOnHost(ash_host, display);
776 #if defined(OS_CHROMEOS)
777 static bool force_constrain_pointer_to_root =
778 base::CommandLine::ForCurrentProcess()->HasSwitch(
779 switches::kAshConstrainPointerToRoot);
780 if (base::SysInfo::IsRunningOnChromeOS() || force_constrain_pointer_to_root)
781 ash_host->ConfineCursorToRootWindow();
782 #endif
783 return ash_host;
786 void DisplayController::OnFadeOutForSwapDisplayFinished() {
787 #if defined(OS_CHROMEOS)
788 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
789 Shell::GetInstance()->display_configurator_animation()
790 ->StartFadeInAnimation();
791 #endif
794 void DisplayController::SetMirrorModeAfterAnimation(bool mirror) {
795 GetDisplayManager()->SetMirrorMode(mirror);
798 void DisplayController::UpdateHostWindowNames() {
799 #if defined(USE_X11)
800 // crbug.com/120229 - set the window title for the primary dislpay
801 // to "aura_root_0" so gtalk can find the primary root window to broadcast.
802 // TODO(jhorwich) Remove this once Chrome supports window-based broadcasting.
803 aura::Window* primary = Shell::GetPrimaryRootWindow();
804 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
805 for (size_t i = 0; i < root_windows.size(); ++i) {
806 std::string name =
807 root_windows[i] == primary ? "aura_root_0" : "aura_root_x";
808 gfx::AcceleratedWidget xwindow =
809 root_windows[i]->GetHost()->GetAcceleratedWidget();
810 XStoreName(gfx::GetXDisplay(), xwindow, name.c_str());
812 #endif
815 } // namespace ash