Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / display / display_manager.cc
blob29eced4cc03ec6eee89c4e70a04ad674edb7a05b
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_manager.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "ash/ash_switches.h"
14 #include "ash/display/display_layout_store.h"
15 #include "ash/display/screen_ash.h"
16 #include "ash/screen_util.h"
17 #include "ash/shell.h"
18 #include "base/auto_reset.h"
19 #include "base/command_line.h"
20 #include "base/logging.h"
21 #include "base/metrics/histogram.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "grit/ash_strings.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/layout.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/gfx/display.h"
31 #include "ui/gfx/display_observer.h"
32 #include "ui/gfx/font_render_params.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/size_conversions.h"
37 #if defined(USE_X11)
38 #include "ui/base/x/x11_util.h"
39 #endif
41 #if defined(OS_CHROMEOS)
42 #include "ash/display/display_configurator_animation.h"
43 #include "base/sys_info.h"
44 #endif
46 #if defined(OS_WIN)
47 #include "base/win/windows_version.h"
48 #endif
50 namespace ash {
51 typedef std::vector<gfx::Display> DisplayList;
52 typedef std::vector<DisplayInfo> DisplayInfoList;
54 namespace {
56 // We need to keep this in order for unittests to tell if
57 // the object in gfx::Screen::GetScreenByType is for shutdown.
58 gfx::Screen* screen_for_shutdown = NULL;
60 // The number of pixels to overlap between the primary and secondary displays,
61 // in case that the offset value is too large.
62 const int kMinimumOverlapForInvalidOffset = 100;
64 // List of value UI Scale values. Scales for 2x are equivalent to 640,
65 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
66 // 2560 pixel width 2x density display. Please see crbug.com/233375
67 // for the full list of resolutions.
68 const float kUIScalesFor2x[] =
69 {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
70 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f };
71 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
72 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
74 struct DisplaySortFunctor {
75 bool operator()(const gfx::Display& a, const gfx::Display& b) {
76 return a.id() < b.id();
80 struct DisplayInfoSortFunctor {
81 bool operator()(const DisplayInfo& a, const DisplayInfo& b) {
82 return a.id() < b.id();
86 struct DisplayModeMatcher {
87 DisplayModeMatcher(const DisplayMode& target_mode)
88 : target_mode(target_mode) {}
89 bool operator()(const DisplayMode& mode) {
90 return target_mode.IsEquivalent(mode);
92 DisplayMode target_mode;
95 struct ScaleComparator {
96 explicit ScaleComparator(float s) : scale(s) {}
98 bool operator()(float s) const {
99 const float kEpsilon = 0.0001f;
100 return std::abs(scale - s) < kEpsilon;
102 float scale;
105 gfx::Display& GetInvalidDisplay() {
106 static gfx::Display* invalid_display = new gfx::Display();
107 return *invalid_display;
110 void MaybeInitInternalDisplay(int64 id) {
111 CommandLine* command_line = CommandLine::ForCurrentProcess();
112 if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal))
113 gfx::Display::SetInternalDisplayId(id);
116 // Scoped objects used to either create or close the non desktop window
117 // at specific timing.
118 class NonDesktopDisplayUpdater {
119 public:
120 NonDesktopDisplayUpdater(DisplayManager* manager,
121 DisplayManager::Delegate* delegate)
122 : manager_(manager),
123 delegate_(delegate),
124 enabled_(manager_->second_display_mode() != DisplayManager::EXTENDED &&
125 manager_->non_desktop_display().is_valid()) {
128 ~NonDesktopDisplayUpdater() {
129 if (!delegate_)
130 return;
132 if (enabled_) {
133 DisplayInfo display_info = manager_->GetDisplayInfo(
134 manager_->non_desktop_display().id());
135 delegate_->CreateOrUpdateNonDesktopDisplay(display_info);
136 } else {
137 delegate_->CloseNonDesktopDisplay();
141 bool enabled() const { return enabled_; }
143 private:
144 DisplayManager* manager_;
145 DisplayManager::Delegate* delegate_;
146 bool enabled_;
147 DISALLOW_COPY_AND_ASSIGN(NonDesktopDisplayUpdater);
150 } // namespace
152 using std::string;
153 using std::vector;
155 DisplayManager::DisplayManager()
156 : delegate_(NULL),
157 screen_ash_(new ScreenAsh),
158 screen_(screen_ash_.get()),
159 layout_store_(new DisplayLayoutStore),
160 first_display_id_(gfx::Display::kInvalidDisplayID),
161 num_connected_displays_(0),
162 force_bounds_changed_(false),
163 change_display_upon_host_resize_(false),
164 second_display_mode_(EXTENDED),
165 mirrored_display_id_(gfx::Display::kInvalidDisplayID),
166 registered_internal_display_rotation_lock_(false),
167 registered_internal_display_rotation_(gfx::Display::ROTATE_0) {
168 #if defined(OS_CHROMEOS)
169 change_display_upon_host_resize_ = !base::SysInfo::IsRunningOnChromeOS();
170 #endif
171 DisplayInfo::SetAllowUpgradeToHighDPI(
172 ui::ResourceBundle::GetSharedInstance().GetMaxScaleFactor() ==
173 ui::SCALE_FACTOR_200P);
175 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
176 screen_ash_.get());
177 gfx::Screen* current_native =
178 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
179 // If there is no native, or the native was for shutdown,
180 // use ash's screen.
181 if (!current_native ||
182 current_native == screen_for_shutdown) {
183 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
184 screen_ash_.get());
188 DisplayManager::~DisplayManager() {
189 #if defined(OS_CHROMEOS)
190 // Reset the font params.
191 gfx::SetFontRenderParamsDeviceScaleFactor(1.0f);
192 #endif
195 // static
196 std::vector<float> DisplayManager::GetScalesForDisplay(
197 const DisplayInfo& info) {
199 #define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a))
201 std::vector<float> ret;
202 if (info.device_scale_factor() == 2.0f) {
203 ASSIGN_ARRAY(ret, kUIScalesFor2x);
204 return ret;
205 } else if (info.device_scale_factor() == 1.25f) {
206 ASSIGN_ARRAY(ret, kUIScalesFor1_25x);
207 return ret;
209 switch (info.bounds_in_native().width()) {
210 case 1280:
211 ASSIGN_ARRAY(ret, kUIScalesFor1280);
212 break;
213 case 1366:
214 ASSIGN_ARRAY(ret, kUIScalesFor1366);
215 break;
216 default:
217 ASSIGN_ARRAY(ret, kUIScalesFor1280);
218 #if defined(OS_CHROMEOS)
219 if (base::SysInfo::IsRunningOnChromeOS())
220 NOTREACHED() << "Unknown resolution:" << info.ToString();
221 #endif
223 return ret;
226 // static
227 float DisplayManager::GetNextUIScale(const DisplayInfo& info, bool up) {
228 float scale = info.configured_ui_scale();
229 std::vector<float> scales = GetScalesForDisplay(info);
230 for (size_t i = 0; i < scales.size(); ++i) {
231 if (ScaleComparator(scales[i])(scale)) {
232 if (up && i != scales.size() - 1)
233 return scales[i + 1];
234 if (!up && i != 0)
235 return scales[i - 1];
236 return scales[i];
239 // Fallback to 1.0f if the |scale| wasn't in the list.
240 return 1.0f;
243 bool DisplayManager::InitFromCommandLine() {
244 DisplayInfoList info_list;
245 CommandLine* command_line = CommandLine::ForCurrentProcess();
246 if (!command_line->HasSwitch(switches::kAshHostWindowBounds))
247 return false;
248 const string size_str =
249 command_line->GetSwitchValueASCII(switches::kAshHostWindowBounds);
250 vector<string> parts;
251 base::SplitString(size_str, ',', &parts);
252 for (vector<string>::const_iterator iter = parts.begin();
253 iter != parts.end(); ++iter) {
254 info_list.push_back(DisplayInfo::CreateFromSpec(*iter));
256 MaybeInitInternalDisplay(info_list[0].id());
257 if (info_list.size() > 1 &&
258 command_line->HasSwitch(switches::kAshEnableSoftwareMirroring)) {
259 SetSecondDisplayMode(MIRRORING);
261 OnNativeDisplaysChanged(info_list);
262 return true;
265 void DisplayManager::InitDefaultDisplay() {
266 DisplayInfoList info_list;
267 info_list.push_back(DisplayInfo::CreateFromSpec(std::string()));
268 MaybeInitInternalDisplay(info_list[0].id());
269 OnNativeDisplaysChanged(info_list);
272 void DisplayManager::InitFontParams() {
273 #if defined(OS_CHROMEOS)
274 if (!HasInternalDisplay())
275 return;
276 const DisplayInfo& display_info =
277 GetDisplayInfo(gfx::Display::InternalDisplayId());
278 gfx::SetFontRenderParamsDeviceScaleFactor(
279 display_info.device_scale_factor());
280 #endif // OS_CHROMEOS
283 // static
284 void DisplayManager::UpdateDisplayBoundsForLayoutById(
285 const DisplayLayout& layout,
286 const gfx::Display& primary_display,
287 int64 secondary_display_id) {
288 DCHECK_NE(gfx::Display::kInvalidDisplayID, secondary_display_id);
289 UpdateDisplayBoundsForLayout(
290 layout, primary_display,
291 Shell::GetInstance()->display_manager()->
292 FindDisplayForId(secondary_display_id));
295 bool DisplayManager::IsActiveDisplay(const gfx::Display& display) const {
296 for (DisplayList::const_iterator iter = displays_.begin();
297 iter != displays_.end(); ++iter) {
298 if ((*iter).id() == display.id())
299 return true;
301 return false;
304 bool DisplayManager::HasInternalDisplay() const {
305 return gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID;
308 bool DisplayManager::IsInternalDisplayId(int64 id) const {
309 return gfx::Display::InternalDisplayId() == id;
312 DisplayLayout DisplayManager::GetCurrentDisplayLayout() {
313 DCHECK_EQ(2U, num_connected_displays());
314 // Invert if the primary was swapped.
315 if (num_connected_displays() > 1) {
316 DisplayIdPair pair = GetCurrentDisplayIdPair();
317 return layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
319 NOTREACHED() << "DisplayLayout is requested for single display";
320 // On release build, just fallback to default instead of blowing up.
321 DisplayLayout layout =
322 layout_store_->default_display_layout();
323 layout.primary_id = displays_[0].id();
324 return layout;
327 DisplayIdPair DisplayManager::GetCurrentDisplayIdPair() const {
328 if (IsMirrored()) {
329 if (software_mirroring_enabled()) {
330 CHECK_EQ(2u, num_connected_displays());
331 // This comment is to make it easy to distinguish the crash
332 // between two checks.
333 CHECK_EQ(1u, displays_.size());
335 return std::make_pair(displays_[0].id(), mirrored_display_id_);
336 } else {
337 CHECK_GE(2u, displays_.size());
338 int64 id_at_zero = displays_[0].id();
339 if (id_at_zero == gfx::Display::InternalDisplayId() ||
340 id_at_zero == first_display_id()) {
341 return std::make_pair(id_at_zero, displays_[1].id());
342 } else {
343 return std::make_pair(displays_[1].id(), id_at_zero);
348 void DisplayManager::SetLayoutForCurrentDisplays(
349 const DisplayLayout& layout_relative_to_primary) {
350 DCHECK_EQ(2U, GetNumDisplays());
351 if (GetNumDisplays() < 2)
352 return;
353 const gfx::Display& primary = screen_->GetPrimaryDisplay();
354 const DisplayIdPair pair = GetCurrentDisplayIdPair();
355 // Invert if the primary was swapped.
356 DisplayLayout to_set = pair.first == primary.id() ?
357 layout_relative_to_primary : layout_relative_to_primary.Invert();
359 DisplayLayout current_layout =
360 layout_store_->GetRegisteredDisplayLayout(pair);
361 if (to_set.position != current_layout.position ||
362 to_set.offset != current_layout.offset) {
363 to_set.primary_id = primary.id();
364 layout_store_->RegisterLayoutForDisplayIdPair(
365 pair.first, pair.second, to_set);
366 if (delegate_)
367 delegate_->PreDisplayConfigurationChange(false);
368 // PreDisplayConfigurationChange(false);
369 // TODO(oshima): Call UpdateDisplays instead.
370 const DisplayLayout layout = GetCurrentDisplayLayout();
371 UpdateDisplayBoundsForLayoutById(
372 layout, primary,
373 ScreenUtil::GetSecondaryDisplay().id());
375 // Primary's bounds stay the same. Just notify bounds change
376 // on the secondary.
377 screen_ash_->NotifyMetricsChanged(
378 ScreenUtil::GetSecondaryDisplay(),
379 gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
380 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
381 if (delegate_)
382 delegate_->PostDisplayConfigurationChange();
386 const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const {
387 gfx::Display* display =
388 const_cast<DisplayManager*>(this)->FindDisplayForId(id);
389 return display ? *display : GetInvalidDisplay();
392 const gfx::Display& DisplayManager::FindDisplayContainingPoint(
393 const gfx::Point& point_in_screen) const {
394 for (DisplayList::const_iterator iter = displays_.begin();
395 iter != displays_.end(); ++iter) {
396 const gfx::Display& display = *iter;
397 if (display.bounds().Contains(point_in_screen))
398 return display;
400 return GetInvalidDisplay();
403 bool DisplayManager::UpdateWorkAreaOfDisplay(int64 display_id,
404 const gfx::Insets& insets) {
405 gfx::Display* display = FindDisplayForId(display_id);
406 DCHECK(display);
407 gfx::Rect old_work_area = display->work_area();
408 display->UpdateWorkAreaFromInsets(insets);
409 return old_work_area != display->work_area();
412 void DisplayManager::SetOverscanInsets(int64 display_id,
413 const gfx::Insets& insets_in_dip) {
414 display_info_[display_id].SetOverscanInsets(insets_in_dip);
415 DisplayInfoList display_info_list;
416 for (DisplayList::const_iterator iter = displays_.begin();
417 iter != displays_.end(); ++iter) {
418 display_info_list.push_back(GetDisplayInfo(iter->id()));
420 AddMirrorDisplayInfoIfAny(&display_info_list);
421 UpdateDisplays(display_info_list);
424 void DisplayManager::SetDisplayRotation(int64 display_id,
425 gfx::Display::Rotation rotation) {
426 DisplayInfoList display_info_list;
427 for (DisplayList::const_iterator iter = displays_.begin();
428 iter != displays_.end(); ++iter) {
429 DisplayInfo info = GetDisplayInfo(iter->id());
430 if (info.id() == display_id) {
431 if (info.rotation() == rotation)
432 return;
433 info.set_rotation(rotation);
435 display_info_list.push_back(info);
437 AddMirrorDisplayInfoIfAny(&display_info_list);
438 if (virtual_keyboard_root_window_enabled() &&
439 display_id == non_desktop_display_.id()) {
440 DisplayInfo info = GetDisplayInfo(display_id);
441 info.set_rotation(rotation);
442 display_info_list.push_back(info);
444 UpdateDisplays(display_info_list);
447 void DisplayManager::SetDisplayUIScale(int64 display_id,
448 float ui_scale) {
449 if (!IsDisplayUIScalingEnabled() ||
450 gfx::Display::InternalDisplayId() != display_id) {
451 return;
454 // TODO(mukai): merge this implementation into SetDisplayMode().
455 DisplayInfoList display_info_list;
456 for (DisplayList::const_iterator iter = displays_.begin();
457 iter != displays_.end(); ++iter) {
458 DisplayInfo info = GetDisplayInfo(iter->id());
459 if (info.id() == display_id) {
460 if (info.configured_ui_scale() == ui_scale)
461 return;
462 std::vector<float> scales = GetScalesForDisplay(info);
463 ScaleComparator comparator(ui_scale);
464 if (std::find_if(scales.begin(), scales.end(), comparator) ==
465 scales.end()) {
466 return;
468 info.set_configured_ui_scale(ui_scale);
470 display_info_list.push_back(info);
472 AddMirrorDisplayInfoIfAny(&display_info_list);
473 UpdateDisplays(display_info_list);
476 void DisplayManager::SetDisplayResolution(int64 display_id,
477 const gfx::Size& resolution) {
478 DCHECK_NE(gfx::Display::InternalDisplayId(), display_id);
479 if (gfx::Display::InternalDisplayId() == display_id)
480 return;
481 const DisplayInfo& display_info = GetDisplayInfo(display_id);
482 const std::vector<DisplayMode>& modes = display_info.display_modes();
483 DCHECK_NE(0u, modes.size());
484 DisplayMode target_mode;
485 target_mode.size = resolution;
486 std::vector<DisplayMode>::const_iterator iter =
487 std::find_if(modes.begin(), modes.end(), DisplayModeMatcher(target_mode));
488 if (iter == modes.end()) {
489 LOG(WARNING) << "Unsupported resolution was requested:"
490 << resolution.ToString();
491 return;
493 display_modes_[display_id] = *iter;
494 #if defined(OS_CHROMEOS)
495 if (base::SysInfo::IsRunningOnChromeOS())
496 Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
497 #endif
500 bool DisplayManager::SetDisplayMode(int64 display_id,
501 const DisplayMode& display_mode) {
502 if (IsInternalDisplayId(display_id)) {
503 SetDisplayUIScale(display_id, display_mode.ui_scale);
504 return false;
507 DisplayInfoList display_info_list;
508 bool display_property_changed = false;
509 bool resolution_changed = false;
510 for (DisplayList::const_iterator iter = displays_.begin();
511 iter != displays_.end(); ++iter) {
512 DisplayInfo info = GetDisplayInfo(iter->id());
513 if (info.id() == display_id) {
514 const std::vector<DisplayMode>& modes = info.display_modes();
515 std::vector<DisplayMode>::const_iterator iter =
516 std::find_if(modes.begin(),
517 modes.end(),
518 DisplayModeMatcher(display_mode));
519 if (iter == modes.end()) {
520 LOG(WARNING) << "Unsupported resolution was requested:"
521 << display_mode.size.ToString();
522 return false;
524 display_modes_[display_id] = *iter;
525 if (info.bounds_in_native().size() != display_mode.size)
526 resolution_changed = true;
527 if (info.device_scale_factor() != display_mode.device_scale_factor) {
528 info.set_device_scale_factor(display_mode.device_scale_factor);
529 display_property_changed = true;
532 display_info_list.push_back(info);
534 if (display_property_changed) {
535 AddMirrorDisplayInfoIfAny(&display_info_list);
536 UpdateDisplays(display_info_list);
538 #if defined(OS_CHROMEOS)
539 if (resolution_changed && base::SysInfo::IsRunningOnChromeOS())
540 Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
541 #endif
542 return resolution_changed;
545 void DisplayManager::RegisterDisplayProperty(
546 int64 display_id,
547 gfx::Display::Rotation rotation,
548 float ui_scale,
549 const gfx::Insets* overscan_insets,
550 const gfx::Size& resolution_in_pixels,
551 float device_scale_factor,
552 ui::ColorCalibrationProfile color_profile) {
553 if (display_info_.find(display_id) == display_info_.end())
554 display_info_[display_id] = DisplayInfo(display_id, std::string(), false);
556 display_info_[display_id].set_rotation(rotation);
557 display_info_[display_id].SetColorProfile(color_profile);
558 // Just in case the preference file was corrupted.
559 // TODO(mukai): register |display_modes_| here as well, so the lookup for the
560 // default mode in GetActiveModeForDisplayId() gets much simpler.
561 if (0.5f <= ui_scale && ui_scale <= 2.0f)
562 display_info_[display_id].set_configured_ui_scale(ui_scale);
563 if (overscan_insets)
564 display_info_[display_id].SetOverscanInsets(*overscan_insets);
565 if (!resolution_in_pixels.IsEmpty()) {
566 DCHECK(!IsInternalDisplayId(display_id));
567 // Default refresh rate, until OnNativeDisplaysChanged() updates us with the
568 // actual display info, is 60 Hz.
569 DisplayMode mode(resolution_in_pixels, 60.0f, false, false);
570 mode.device_scale_factor = device_scale_factor;
571 display_modes_[display_id] = mode;
575 DisplayMode DisplayManager::GetActiveModeForDisplayId(int64 display_id) const {
576 DisplayMode selected_mode;
577 if (GetSelectedModeForDisplayId(display_id, &selected_mode))
578 return selected_mode;
580 // If 'selected' mode is empty, it should return the default mode. This means
581 // the native mode for the external display. Unfortunately this is not true
582 // for the internal display because restoring UI-scale doesn't register the
583 // restored mode to |display_mode_|, so it needs to look up the mode whose
584 // UI-scale value matches. See the TODO in RegisterDisplayProperty().
585 const DisplayInfo& info = GetDisplayInfo(display_id);
586 const std::vector<DisplayMode>& display_modes = info.display_modes();
588 if (IsInternalDisplayId(display_id)) {
589 for (size_t i = 0; i < display_modes.size(); ++i) {
590 if (info.configured_ui_scale() == display_modes[i].ui_scale)
591 return display_modes[i];
593 } else {
594 for (size_t i = 0; i < display_modes.size(); ++i) {
595 if (display_modes[i].native)
596 return display_modes[i];
599 return selected_mode;
602 void DisplayManager::RegisterDisplayRotationProperties(bool rotation_lock,
603 gfx::Display::Rotation rotation) {
604 if (delegate_)
605 delegate_->PreDisplayConfigurationChange(false);
606 registered_internal_display_rotation_lock_ = rotation_lock;
607 registered_internal_display_rotation_ = rotation;
608 if (delegate_)
609 delegate_->PostDisplayConfigurationChange();
612 bool DisplayManager::GetSelectedModeForDisplayId(int64 id,
613 DisplayMode* mode_out) const {
614 std::map<int64, DisplayMode>::const_iterator iter = display_modes_.find(id);
615 if (iter == display_modes_.end())
616 return false;
617 *mode_out = iter->second;
618 return true;
621 bool DisplayManager::IsDisplayUIScalingEnabled() const {
622 return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID;
625 gfx::Insets DisplayManager::GetOverscanInsets(int64 display_id) const {
626 std::map<int64, DisplayInfo>::const_iterator it =
627 display_info_.find(display_id);
628 return (it != display_info_.end()) ?
629 it->second.overscan_insets_in_dip() : gfx::Insets();
632 void DisplayManager::SetColorCalibrationProfile(
633 int64 display_id,
634 ui::ColorCalibrationProfile profile) {
635 #if defined(OS_CHROMEOS)
636 if (!display_info_[display_id].IsColorProfileAvailable(profile))
637 return;
639 if (delegate_)
640 delegate_->PreDisplayConfigurationChange(false);
641 // Just sets color profile if it's not running on ChromeOS (like tests).
642 if (!base::SysInfo::IsRunningOnChromeOS() ||
643 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
644 display_id, profile)) {
645 display_info_[display_id].SetColorProfile(profile);
646 UMA_HISTOGRAM_ENUMERATION(
647 "ChromeOS.Display.ColorProfile", profile, ui::NUM_COLOR_PROFILES);
649 if (delegate_)
650 delegate_->PostDisplayConfigurationChange();
651 #endif
654 void DisplayManager::OnNativeDisplaysChanged(
655 const std::vector<DisplayInfo>& updated_displays) {
656 if (updated_displays.empty()) {
657 VLOG(1) << "OnNativeDisplayChanged(0): # of current displays="
658 << displays_.size();
659 // If the device is booted without display, or chrome is started
660 // without --ash-host-window-bounds on linux desktop, use the
661 // default display.
662 if (displays_.empty()) {
663 std::vector<DisplayInfo> init_displays;
664 init_displays.push_back(DisplayInfo::CreateFromSpec(std::string()));
665 MaybeInitInternalDisplay(init_displays[0].id());
666 OnNativeDisplaysChanged(init_displays);
667 } else {
668 // Otherwise don't update the displays when all displays are disconnected.
669 // This happens when:
670 // - the device is idle and powerd requested to turn off all displays.
671 // - the device is suspended. (kernel turns off all displays)
672 // - the internal display's brightness is set to 0 and no external
673 // display is connected.
674 // - the internal display's brightness is 0 and external display is
675 // disconnected.
676 // The display will be updated when one of displays is turned on, and the
677 // display list will be updated correctly.
679 return;
681 first_display_id_ = updated_displays[0].id();
682 std::set<gfx::Point> origins;
684 if (updated_displays.size() == 1) {
685 VLOG(1) << "OnNativeDisplaysChanged(1):" << updated_displays[0].ToString();
686 } else {
687 VLOG(1) << "OnNativeDisplaysChanged(" << updated_displays.size()
688 << ") [0]=" << updated_displays[0].ToString()
689 << ", [1]=" << updated_displays[1].ToString();
692 bool internal_display_connected = false;
693 num_connected_displays_ = updated_displays.size();
694 mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
695 non_desktop_display_ = gfx::Display();
696 DisplayInfoList new_display_info_list;
697 for (DisplayInfoList::const_iterator iter = updated_displays.begin();
698 iter != updated_displays.end();
699 ++iter) {
700 if (!internal_display_connected)
701 internal_display_connected = IsInternalDisplayId(iter->id());
702 // Mirrored monitors have the same origins.
703 gfx::Point origin = iter->bounds_in_native().origin();
704 if (origins.find(origin) != origins.end()) {
705 InsertAndUpdateDisplayInfo(*iter);
706 mirrored_display_id_ = iter->id();
707 } else {
708 origins.insert(origin);
709 new_display_info_list.push_back(*iter);
712 DisplayMode new_mode;
713 new_mode.size = iter->bounds_in_native().size();
714 new_mode.device_scale_factor = iter->device_scale_factor();
715 new_mode.ui_scale = iter->configured_ui_scale();
716 const std::vector<DisplayMode>& display_modes = iter->display_modes();
717 // This is empty the displays are initialized from InitFromCommandLine.
718 if (!display_modes.size())
719 continue;
720 std::vector<DisplayMode>::const_iterator display_modes_iter =
721 std::find_if(display_modes.begin(),
722 display_modes.end(),
723 DisplayModeMatcher(new_mode));
724 // Update the actual resolution selected as the resolution request may fail.
725 if (display_modes_iter == display_modes.end())
726 display_modes_.erase(iter->id());
727 else if (display_modes_.find(iter->id()) != display_modes_.end())
728 display_modes_[iter->id()] = *display_modes_iter;
730 if (HasInternalDisplay() &&
731 !internal_display_connected &&
732 display_info_.find(gfx::Display::InternalDisplayId()) ==
733 display_info_.end()) {
734 DisplayInfo internal_display_info(
735 gfx::Display::InternalDisplayId(),
736 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME),
737 false /*Internal display must not have overscan */);
738 internal_display_info.SetBounds(gfx::Rect(0, 0, 800, 600));
739 display_info_[gfx::Display::InternalDisplayId()] = internal_display_info;
741 UpdateDisplays(new_display_info_list);
744 void DisplayManager::UpdateDisplays() {
745 DisplayInfoList display_info_list;
746 for (DisplayList::const_iterator iter = displays_.begin();
747 iter != displays_.end(); ++iter) {
748 display_info_list.push_back(GetDisplayInfo(iter->id()));
750 AddMirrorDisplayInfoIfAny(&display_info_list);
751 UpdateDisplays(display_info_list);
754 void DisplayManager::UpdateDisplays(
755 const std::vector<DisplayInfo>& updated_display_info_list) {
756 #if defined(OS_WIN)
757 DCHECK_EQ(1u, updated_display_info_list.size()) <<
758 ": Multiple display test does not work on Windows bots. Please "
759 "skip (don't disable) the test using SupportsMultipleDisplays()";
760 #endif
762 DisplayInfoList new_display_info_list = updated_display_info_list;
763 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor());
764 std::sort(new_display_info_list.begin(),
765 new_display_info_list.end(),
766 DisplayInfoSortFunctor());
767 DisplayList removed_displays;
768 std::map<size_t, uint32_t> display_changes;
769 std::vector<size_t> added_display_indices;
771 DisplayList::iterator curr_iter = displays_.begin();
772 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin();
774 DisplayList new_displays;
776 // Use the internal display or 1st as the mirror source, then scale
777 // the root window so that it matches the external display's
778 // resolution. This is necessary in order for scaling to work while
779 // mirrored.
780 int64 non_desktop_display_id = gfx::Display::kInvalidDisplayID;
782 if (second_display_mode_ != EXTENDED && new_display_info_list.size() == 2) {
783 bool zero_is_source =
784 first_display_id_ == new_display_info_list[0].id() ||
785 gfx::Display::InternalDisplayId() == new_display_info_list[0].id();
786 if (second_display_mode_ == MIRRORING) {
787 mirrored_display_id_ = new_display_info_list[zero_is_source ? 1 : 0].id();
788 non_desktop_display_id = mirrored_display_id_;
789 } else {
790 // TODO(oshima|bshe): The virtual keyboard is currently assigned to
791 // the 1st display.
792 non_desktop_display_id =
793 new_display_info_list[zero_is_source ? 0 : 1].id();
797 while (curr_iter != displays_.end() ||
798 new_info_iter != new_display_info_list.end()) {
799 if (new_info_iter != new_display_info_list.end() &&
800 non_desktop_display_id == new_info_iter->id()) {
801 DisplayInfo info = *new_info_iter;
802 info.SetOverscanInsets(gfx::Insets());
803 InsertAndUpdateDisplayInfo(info);
804 non_desktop_display_ =
805 CreateDisplayFromDisplayInfoById(non_desktop_display_id);
806 ++new_info_iter;
807 // Remove existing external display if it is going to be used as
808 // non desktop.
809 if (curr_iter != displays_.end() &&
810 curr_iter->id() == non_desktop_display_id) {
811 removed_displays.push_back(*curr_iter);
812 ++curr_iter;
814 continue;
817 if (curr_iter == displays_.end()) {
818 // more displays in new list.
819 added_display_indices.push_back(new_displays.size());
820 InsertAndUpdateDisplayInfo(*new_info_iter);
821 new_displays.push_back(
822 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
823 ++new_info_iter;
824 } else if (new_info_iter == new_display_info_list.end()) {
825 // more displays in current list.
826 removed_displays.push_back(*curr_iter);
827 ++curr_iter;
828 } else if (curr_iter->id() == new_info_iter->id()) {
829 const gfx::Display& current_display = *curr_iter;
830 // Copy the info because |CreateDisplayFromInfo| updates the instance.
831 const DisplayInfo current_display_info =
832 GetDisplayInfo(current_display.id());
833 InsertAndUpdateDisplayInfo(*new_info_iter);
834 gfx::Display new_display =
835 CreateDisplayFromDisplayInfoById(new_info_iter->id());
836 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id());
838 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_NONE;
840 // At that point the new Display objects we have are not entirely updated,
841 // they are missing the translation related to the Display disposition in
842 // the layout.
843 // Using display.bounds() and display.work_area() would fail most of the
844 // time.
845 if (force_bounds_changed_ || (current_display_info.bounds_in_native() !=
846 new_display_info.bounds_in_native()) ||
847 (current_display_info.size_in_pixel() !=
848 new_display.GetSizeInPixel())) {
849 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
850 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
853 if (current_display.device_scale_factor() !=
854 new_display.device_scale_factor()) {
855 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
858 if (current_display.rotation() != new_display.rotation())
859 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_ROTATION;
861 if (metrics != gfx::DisplayObserver::DISPLAY_METRIC_NONE) {
862 display_changes.insert(
863 std::pair<size_t, uint32_t>(new_displays.size(), metrics));
866 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
867 new_displays.push_back(new_display);
868 ++curr_iter;
869 ++new_info_iter;
870 } else if (curr_iter->id() < new_info_iter->id()) {
871 // more displays in current list between ids, which means it is deleted.
872 removed_displays.push_back(*curr_iter);
873 ++curr_iter;
874 } else {
875 // more displays in new list between ids, which means it is added.
876 added_display_indices.push_back(new_displays.size());
877 InsertAndUpdateDisplayInfo(*new_info_iter);
878 new_displays.push_back(
879 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
880 ++new_info_iter;
884 scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater(
885 new NonDesktopDisplayUpdater(this, delegate_));
887 // Do not update |displays_| if there's nothing to be updated. Without this,
888 // it will not update the display layout, which causes the bug
889 // http://crbug.com/155948.
890 if (display_changes.empty() && added_display_indices.empty() &&
891 removed_displays.empty()) {
892 return;
894 // Clear focus if the display has been removed, but don't clear focus if
895 // the destkop has been moved from one display to another
896 // (mirror -> docked, docked -> single internal).
897 bool clear_focus =
898 !removed_displays.empty() &&
899 !(removed_displays.size() == 1 && added_display_indices.size() == 1);
900 if (delegate_)
901 delegate_->PreDisplayConfigurationChange(clear_focus);
903 size_t updated_index;
904 if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) &&
905 std::find(added_display_indices.begin(),
906 added_display_indices.end(),
907 updated_index) == added_display_indices.end()) {
908 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
909 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
910 if (display_changes.find(updated_index) != display_changes.end())
911 metrics |= display_changes[updated_index];
913 display_changes[updated_index] = metrics;
916 displays_ = new_displays;
918 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false);
920 // Temporarily add displays to be removed because display object
921 // being removed are accessed during shutting down the root.
922 displays_.insert(displays_.end(), removed_displays.begin(),
923 removed_displays.end());
925 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin();
926 iter != removed_displays.rend(); ++iter) {
927 screen_ash_->NotifyDisplayRemoved(displays_.back());
928 displays_.pop_back();
930 // Close the non desktop window here to avoid creating two compositor on
931 // one display.
932 if (!non_desktop_display_updater->enabled())
933 non_desktop_display_updater.reset();
934 for (std::vector<size_t>::iterator iter = added_display_indices.begin();
935 iter != added_display_indices.end(); ++iter) {
936 screen_ash_->NotifyDisplayAdded(displays_[*iter]);
938 // Create the non destkop window after all displays are added so that
939 // it can mirror the display newly added. This can happen when switching
940 // from dock mode to software mirror mode.
941 non_desktop_display_updater.reset();
942 for (std::map<size_t, uint32_t>::iterator iter = display_changes.begin();
943 iter != display_changes.end();
944 ++iter) {
945 screen_ash_->NotifyMetricsChanged(displays_[iter->first], iter->second);
947 if (delegate_)
948 delegate_->PostDisplayConfigurationChange();
950 #if defined(USE_X11) && defined(OS_CHROMEOS)
951 if (!display_changes.empty() && base::SysInfo::IsRunningOnChromeOS())
952 ui::ClearX11DefaultRootWindow();
953 #endif
956 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const {
957 DCHECK_LT(index, displays_.size());
958 return displays_[index];
961 const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const {
962 if (GetNumDisplays() == 1)
963 return displays_[0];
964 DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(
965 GetCurrentDisplayIdPair());
966 return GetDisplayForId(layout.primary_id);
969 size_t DisplayManager::GetNumDisplays() const {
970 return displays_.size();
973 bool DisplayManager::IsMirrored() const {
974 return mirrored_display_id_ != gfx::Display::kInvalidDisplayID;
977 const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const {
978 DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id);
980 std::map<int64, DisplayInfo>::const_iterator iter =
981 display_info_.find(display_id);
982 CHECK(iter != display_info_.end()) << display_id;
983 return iter->second;
986 std::string DisplayManager::GetDisplayNameForId(int64 id) {
987 if (id == gfx::Display::kInvalidDisplayID)
988 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
990 std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id);
991 if (iter != display_info_.end() && !iter->second.name().empty())
992 return iter->second.name();
994 return base::StringPrintf("Display %d", static_cast<int>(id));
997 int64 DisplayManager::GetDisplayIdForUIScaling() const {
998 // UI Scaling is effective only on internal display.
999 int64 display_id = gfx::Display::InternalDisplayId();
1000 #if defined(OS_WIN)
1001 display_id = first_display_id();
1002 #endif
1003 return display_id;
1006 void DisplayManager::SetMirrorMode(bool mirrored) {
1007 if (num_connected_displays() <= 1)
1008 return;
1010 #if defined(OS_CHROMEOS)
1011 if (base::SysInfo::IsRunningOnChromeOS()) {
1012 ui::MultipleDisplayState new_state =
1013 mirrored ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR :
1014 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
1015 Shell::GetInstance()->display_configurator()->SetDisplayMode(new_state);
1016 return;
1018 #endif
1019 // This is fallback path to emulate mirroroing on desktop.
1020 SetSecondDisplayMode(mirrored ? MIRRORING : EXTENDED);
1021 DisplayInfoList display_info_list;
1022 int count = 0;
1023 for (std::map<int64, DisplayInfo>::const_iterator iter =
1024 display_info_.begin();
1025 count < 2; ++iter, ++count) {
1026 display_info_list.push_back(GetDisplayInfo(iter->second.id()));
1028 UpdateDisplays(display_info_list);
1029 #if defined(OS_CHROMEOS)
1030 if (Shell::GetInstance()->display_configurator_animation()) {
1031 Shell::GetInstance()->display_configurator_animation()->
1032 StartFadeInAnimation();
1034 #endif
1037 void DisplayManager::AddRemoveDisplay() {
1038 DCHECK(!displays_.empty());
1039 std::vector<DisplayInfo> new_display_info_list;
1040 const DisplayInfo& first_display = GetDisplayInfo(displays_[0].id());
1041 new_display_info_list.push_back(first_display);
1042 // Add if there is only one display connected.
1043 if (num_connected_displays() == 1) {
1044 // Layout the 2nd display below the primary as with the real device.
1045 gfx::Rect host_bounds = first_display.bounds_in_native();
1046 new_display_info_list.push_back(DisplayInfo::CreateFromSpec(
1047 base::StringPrintf(
1048 "%d+%d-500x400", host_bounds.x(), host_bounds.bottom())));
1050 num_connected_displays_ = new_display_info_list.size();
1051 mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
1052 non_desktop_display_ = gfx::Display();
1053 UpdateDisplays(new_display_info_list);
1056 void DisplayManager::ToggleDisplayScaleFactor() {
1057 DCHECK(!displays_.empty());
1058 std::vector<DisplayInfo> new_display_info_list;
1059 for (DisplayList::const_iterator iter = displays_.begin();
1060 iter != displays_.end(); ++iter) {
1061 DisplayInfo display_info = GetDisplayInfo(iter->id());
1062 display_info.set_device_scale_factor(
1063 display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f);
1064 new_display_info_list.push_back(display_info);
1066 AddMirrorDisplayInfoIfAny(&new_display_info_list);
1067 UpdateDisplays(new_display_info_list);
1070 #if defined(OS_CHROMEOS)
1071 void DisplayManager::SetSoftwareMirroring(bool enabled) {
1072 // TODO(oshima|bshe): Support external display on the system
1073 // that has virtual keyboard display.
1074 if (second_display_mode_ == VIRTUAL_KEYBOARD)
1075 return;
1076 SetSecondDisplayMode(enabled ? MIRRORING : EXTENDED);
1079 bool DisplayManager::SoftwareMirroringEnabled() const {
1080 return software_mirroring_enabled();
1082 #endif
1084 void DisplayManager::SetSecondDisplayMode(SecondDisplayMode mode) {
1085 second_display_mode_ = mode;
1086 mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
1087 non_desktop_display_ = gfx::Display();
1090 bool DisplayManager::UpdateDisplayBounds(int64 display_id,
1091 const gfx::Rect& new_bounds) {
1092 if (change_display_upon_host_resize_) {
1093 display_info_[display_id].SetBounds(new_bounds);
1094 // Don't notify observers if the mirrored window has changed.
1095 if (software_mirroring_enabled() && mirrored_display_id_ == display_id)
1096 return false;
1097 gfx::Display* display = FindDisplayForId(display_id);
1098 display->SetSize(display_info_[display_id].size_in_pixel());
1099 screen_ash_->NotifyMetricsChanged(
1100 *display, gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1101 return true;
1103 return false;
1106 void DisplayManager::CreateMirrorWindowIfAny() {
1107 NonDesktopDisplayUpdater updater(this, delegate_);
1110 void DisplayManager::CreateScreenForShutdown() const {
1111 bool native_is_ash =
1112 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE) ==
1113 screen_ash_.get();
1114 delete screen_for_shutdown;
1115 screen_for_shutdown = screen_ash_->CloneForShutdown();
1116 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
1117 screen_for_shutdown);
1118 if (native_is_ash) {
1119 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
1120 screen_for_shutdown);
1124 gfx::Display* DisplayManager::FindDisplayForId(int64 id) {
1125 for (DisplayList::iterator iter = displays_.begin();
1126 iter != displays_.end(); ++iter) {
1127 if ((*iter).id() == id)
1128 return &(*iter);
1130 DLOG(WARNING) << "Could not find display:" << id;
1131 return NULL;
1134 void DisplayManager::AddMirrorDisplayInfoIfAny(
1135 std::vector<DisplayInfo>* display_info_list) {
1136 if (software_mirroring_enabled() && IsMirrored())
1137 display_info_list->push_back(GetDisplayInfo(mirrored_display_id_));
1140 void DisplayManager::InsertAndUpdateDisplayInfo(const DisplayInfo& new_info) {
1141 std::map<int64, DisplayInfo>::iterator info =
1142 display_info_.find(new_info.id());
1143 if (info != display_info_.end()) {
1144 info->second.Copy(new_info);
1145 } else {
1146 display_info_[new_info.id()] = new_info;
1147 display_info_[new_info.id()].set_native(false);
1149 display_info_[new_info.id()].UpdateDisplaySize();
1151 OnDisplayInfoUpdated(display_info_[new_info.id()]);
1154 void DisplayManager::OnDisplayInfoUpdated(const DisplayInfo& display_info) {
1155 #if defined(OS_CHROMEOS)
1156 ui::ColorCalibrationProfile color_profile = display_info.color_profile();
1157 if (color_profile != ui::COLOR_PROFILE_STANDARD) {
1158 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
1159 display_info.id(), color_profile);
1161 #endif
1164 gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) {
1165 DCHECK(display_info_.find(id) != display_info_.end());
1166 const DisplayInfo& display_info = display_info_[id];
1168 gfx::Display new_display(display_info.id());
1169 gfx::Rect bounds_in_native(display_info.size_in_pixel());
1170 float device_scale_factor = display_info.GetEffectiveDeviceScaleFactor();
1172 // Simply set the origin to (0,0). The primary display's origin is
1173 // always (0,0) and the secondary display's bounds will be updated
1174 // in |UpdateSecondaryDisplayBoundsForLayout| called in |UpdateDisplay|.
1175 new_display.SetScaleAndBounds(
1176 device_scale_factor, gfx::Rect(bounds_in_native.size()));
1177 new_display.set_rotation(display_info.rotation());
1178 new_display.set_touch_support(display_info.touch_support());
1179 return new_display;
1182 bool DisplayManager::UpdateSecondaryDisplayBoundsForLayout(
1183 DisplayList* displays,
1184 size_t* updated_index) const {
1185 if (displays->size() != 2U)
1186 return false;
1188 int64 id_at_zero = displays->at(0).id();
1189 DisplayIdPair pair =
1190 (id_at_zero == first_display_id_ ||
1191 id_at_zero == gfx::Display::InternalDisplayId()) ?
1192 std::make_pair(id_at_zero, displays->at(1).id()) :
1193 std::make_pair(displays->at(1).id(), id_at_zero);
1194 DisplayLayout layout =
1195 layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
1197 // Ignore if a user has a old format (should be extremely rare)
1198 // and this will be replaced with DCHECK.
1199 if (layout.primary_id != gfx::Display::kInvalidDisplayID) {
1200 size_t primary_index, secondary_index;
1201 if (displays->at(0).id() == layout.primary_id) {
1202 primary_index = 0;
1203 secondary_index = 1;
1204 } else {
1205 primary_index = 1;
1206 secondary_index = 0;
1208 // This function may be called before the secondary display is
1209 // registered. The bounds is empty in that case and will
1210 // return true.
1211 gfx::Rect bounds =
1212 GetDisplayForId(displays->at(secondary_index).id()).bounds();
1213 UpdateDisplayBoundsForLayout(
1214 layout, displays->at(primary_index), &displays->at(secondary_index));
1215 *updated_index = secondary_index;
1216 return bounds != displays->at(secondary_index).bounds();
1218 return false;
1221 // static
1222 void DisplayManager::UpdateDisplayBoundsForLayout(
1223 const DisplayLayout& layout,
1224 const gfx::Display& primary_display,
1225 gfx::Display* secondary_display) {
1226 DCHECK_EQ("0,0", primary_display.bounds().origin().ToString());
1228 const gfx::Rect& primary_bounds = primary_display.bounds();
1229 const gfx::Rect& secondary_bounds = secondary_display->bounds();
1230 gfx::Point new_secondary_origin = primary_bounds.origin();
1232 DisplayLayout::Position position = layout.position;
1234 // Ignore the offset in case the secondary display doesn't share edges with
1235 // the primary display.
1236 int offset = layout.offset;
1237 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
1238 offset = std::min(
1239 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
1240 offset = std::max(
1241 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
1242 } else {
1243 offset = std::min(
1244 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
1245 offset = std::max(
1246 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
1248 switch (position) {
1249 case DisplayLayout::TOP:
1250 new_secondary_origin.Offset(offset, -secondary_bounds.height());
1251 break;
1252 case DisplayLayout::RIGHT:
1253 new_secondary_origin.Offset(primary_bounds.width(), offset);
1254 break;
1255 case DisplayLayout::BOTTOM:
1256 new_secondary_origin.Offset(offset, primary_bounds.height());
1257 break;
1258 case DisplayLayout::LEFT:
1259 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
1260 break;
1262 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
1263 secondary_display->set_bounds(
1264 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
1265 secondary_display->UpdateWorkAreaFromInsets(insets);
1268 } // namespace ash