Use a separate DevicePermissionsManager instance in incognito mode.
[chromium-blink-merge.git] / ash / display / display_manager.cc
blob4cec2ea81d73a1332c309e16efe78eb081f4ce7c
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/gfx/display.h"
29 #include "ui/gfx/display_observer.h"
30 #include "ui/gfx/font_render_params.h"
31 #include "ui/gfx/rect.h"
32 #include "ui/gfx/screen.h"
33 #include "ui/gfx/size_conversions.h"
35 #if defined(USE_X11)
36 #include "ui/base/x/x11_util.h"
37 #endif
39 #if defined(OS_CHROMEOS)
40 #include "ash/display/display_configurator_animation.h"
41 #include "base/sys_info.h"
42 #endif
44 #if defined(OS_WIN)
45 #include "base/win/windows_version.h"
46 #endif
48 namespace ash {
49 typedef std::vector<gfx::Display> DisplayList;
50 typedef std::vector<DisplayInfo> DisplayInfoList;
52 namespace {
54 // We need to keep this in order for unittests to tell if
55 // the object in gfx::Screen::GetScreenByType is for shutdown.
56 gfx::Screen* screen_for_shutdown = NULL;
58 // The number of pixels to overlap between the primary and secondary displays,
59 // in case that the offset value is too large.
60 const int kMinimumOverlapForInvalidOffset = 100;
62 // List of value UI Scale values. Scales for 2x are equivalent to 640,
63 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
64 // 2560 pixel width 2x density display. Please see crbug.com/233375
65 // for the full list of resolutions.
66 const float kUIScalesFor2x[] =
67 {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
68 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f };
69 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
70 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
72 struct DisplaySortFunctor {
73 bool operator()(const gfx::Display& a, const gfx::Display& b) {
74 return a.id() < b.id();
78 struct DisplayInfoSortFunctor {
79 bool operator()(const DisplayInfo& a, const DisplayInfo& b) {
80 return a.id() < b.id();
84 struct DisplayModeMatcher {
85 DisplayModeMatcher(const DisplayMode& target_mode)
86 : target_mode(target_mode) {}
87 bool operator()(const DisplayMode& mode) {
88 return target_mode.IsEquivalent(mode);
90 DisplayMode target_mode;
93 struct ScaleComparator {
94 explicit ScaleComparator(float s) : scale(s) {}
96 bool operator()(float s) const {
97 const float kEpsilon = 0.0001f;
98 return std::abs(scale - s) < kEpsilon;
100 float scale;
103 gfx::Display& GetInvalidDisplay() {
104 static gfx::Display* invalid_display = new gfx::Display();
105 return *invalid_display;
108 void MaybeInitInternalDisplay(int64 id) {
109 CommandLine* command_line = CommandLine::ForCurrentProcess();
110 if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal))
111 gfx::Display::SetInternalDisplayId(id);
114 // Scoped objects used to either create or close the non desktop window
115 // at specific timing.
116 class NonDesktopDisplayUpdater {
117 public:
118 NonDesktopDisplayUpdater(DisplayManager* manager,
119 DisplayManager::Delegate* delegate)
120 : manager_(manager),
121 delegate_(delegate),
122 enabled_(manager_->second_display_mode() != DisplayManager::EXTENDED &&
123 manager_->non_desktop_display().is_valid()) {
126 ~NonDesktopDisplayUpdater() {
127 if (!delegate_)
128 return;
130 if (enabled_) {
131 DisplayInfo display_info = manager_->GetDisplayInfo(
132 manager_->non_desktop_display().id());
133 delegate_->CreateOrUpdateNonDesktopDisplay(display_info);
134 } else {
135 delegate_->CloseNonDesktopDisplay();
139 bool enabled() const { return enabled_; }
141 private:
142 DisplayManager* manager_;
143 DisplayManager::Delegate* delegate_;
144 bool enabled_;
145 DISALLOW_COPY_AND_ASSIGN(NonDesktopDisplayUpdater);
148 } // namespace
150 using std::string;
151 using std::vector;
153 DisplayManager::DisplayManager()
154 : delegate_(NULL),
155 screen_ash_(new ScreenAsh),
156 screen_(screen_ash_.get()),
157 layout_store_(new DisplayLayoutStore),
158 first_display_id_(gfx::Display::kInvalidDisplayID),
159 num_connected_displays_(0),
160 force_bounds_changed_(false),
161 change_display_upon_host_resize_(false),
162 second_display_mode_(EXTENDED),
163 mirrored_display_id_(gfx::Display::kInvalidDisplayID),
164 registered_internal_display_rotation_lock_(false),
165 registered_internal_display_rotation_(gfx::Display::ROTATE_0) {
167 #if defined(OS_CHROMEOS)
168 // Enable only on the device so that DisplayManagerFontTest passes.
169 if (base::SysInfo::IsRunningOnChromeOS())
170 DisplayInfo::SetUse125DSFForUIScaling(true);
172 change_display_upon_host_resize_ = !base::SysInfo::IsRunningOnChromeOS();
173 #endif
174 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
175 screen_ash_.get());
176 gfx::Screen* current_native =
177 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
178 // If there is no native, or the native was for shutdown,
179 // use ash's screen.
180 if (!current_native ||
181 current_native == screen_for_shutdown) {
182 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
183 screen_ash_.get());
187 DisplayManager::~DisplayManager() {
188 #if defined(OS_CHROMEOS)
189 // Reset the font params.
190 gfx::SetFontRenderParamsDeviceScaleFactor(1.0f);
191 #endif
194 // static
195 std::vector<float> DisplayManager::GetScalesForDisplay(
196 const DisplayInfo& info) {
198 #define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a))
200 std::vector<float> ret;
201 if (info.device_scale_factor() == 2.0f) {
202 ASSIGN_ARRAY(ret, kUIScalesFor2x);
203 return ret;
204 } else if (info.device_scale_factor() == 1.25f) {
205 ASSIGN_ARRAY(ret, kUIScalesFor1_25x);
206 return ret;
208 switch (info.bounds_in_native().width()) {
209 case 1280:
210 ASSIGN_ARRAY(ret, kUIScalesFor1280);
211 break;
212 case 1366:
213 ASSIGN_ARRAY(ret, kUIScalesFor1366);
214 break;
215 default:
216 ASSIGN_ARRAY(ret, kUIScalesFor1280);
217 #if defined(OS_CHROMEOS)
218 if (base::SysInfo::IsRunningOnChromeOS())
219 NOTREACHED() << "Unknown resolution:" << info.ToString();
220 #endif
222 return ret;
225 // static
226 float DisplayManager::GetNextUIScale(const DisplayInfo& info, bool up) {
227 float scale = info.configured_ui_scale();
228 std::vector<float> scales = GetScalesForDisplay(info);
229 for (size_t i = 0; i < scales.size(); ++i) {
230 if (ScaleComparator(scales[i])(scale)) {
231 if (up && i != scales.size() - 1)
232 return scales[i + 1];
233 if (!up && i != 0)
234 return scales[i - 1];
235 return scales[i];
238 // Fallback to 1.0f if the |scale| wasn't in the list.
239 return 1.0f;
242 bool DisplayManager::InitFromCommandLine() {
243 DisplayInfoList info_list;
244 CommandLine* command_line = CommandLine::ForCurrentProcess();
245 if (!command_line->HasSwitch(switches::kAshHostWindowBounds))
246 return false;
247 const string size_str =
248 command_line->GetSwitchValueASCII(switches::kAshHostWindowBounds);
249 vector<string> parts;
250 base::SplitString(size_str, ',', &parts);
251 for (vector<string>::const_iterator iter = parts.begin();
252 iter != parts.end(); ++iter) {
253 info_list.push_back(DisplayInfo::CreateFromSpec(*iter));
254 info_list.back().set_native(true);
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 info_list.back().set_native(true);
269 MaybeInitInternalDisplay(info_list[0].id());
270 OnNativeDisplaysChanged(info_list);
273 void DisplayManager::InitFontParams() {
274 #if defined(OS_CHROMEOS)
275 if (!HasInternalDisplay())
276 return;
277 const DisplayInfo& display_info =
278 GetDisplayInfo(gfx::Display::InternalDisplayId());
279 gfx::SetFontRenderParamsDeviceScaleFactor(
280 display_info.GetEffectiveDeviceScaleFactor());
281 #endif // OS_CHROMEOS
284 // static
285 void DisplayManager::UpdateDisplayBoundsForLayoutById(
286 const DisplayLayout& layout,
287 const gfx::Display& primary_display,
288 int64 secondary_display_id) {
289 DCHECK_NE(gfx::Display::kInvalidDisplayID, secondary_display_id);
290 UpdateDisplayBoundsForLayout(
291 layout, primary_display,
292 Shell::GetInstance()->display_manager()->
293 FindDisplayForId(secondary_display_id));
296 bool DisplayManager::IsActiveDisplay(const gfx::Display& display) const {
297 for (DisplayList::const_iterator iter = displays_.begin();
298 iter != displays_.end(); ++iter) {
299 if ((*iter).id() == display.id())
300 return true;
302 return false;
305 bool DisplayManager::HasInternalDisplay() const {
306 return gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID;
309 bool DisplayManager::IsInternalDisplayId(int64 id) const {
310 return gfx::Display::InternalDisplayId() == id;
313 DisplayLayout DisplayManager::GetCurrentDisplayLayout() {
314 DCHECK_EQ(2U, num_connected_displays());
315 // Invert if the primary was swapped.
316 if (num_connected_displays() > 1) {
317 DisplayIdPair pair = GetCurrentDisplayIdPair();
318 return layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
320 NOTREACHED() << "DisplayLayout is requested for single display";
321 // On release build, just fallback to default instead of blowing up.
322 DisplayLayout layout =
323 layout_store_->default_display_layout();
324 layout.primary_id = displays_[0].id();
325 return layout;
328 DisplayIdPair DisplayManager::GetCurrentDisplayIdPair() const {
329 if (IsMirrored()) {
330 if (software_mirroring_enabled()) {
331 CHECK_EQ(2u, num_connected_displays());
332 // This comment is to make it easy to distinguish the crash
333 // between two checks.
334 CHECK_EQ(1u, displays_.size());
336 return std::make_pair(displays_[0].id(), mirrored_display_id_);
337 } else {
338 CHECK_GE(2u, displays_.size());
339 int64 id_at_zero = displays_[0].id();
340 if (id_at_zero == gfx::Display::InternalDisplayId() ||
341 id_at_zero == first_display_id()) {
342 return std::make_pair(id_at_zero, displays_[1].id());
343 } else {
344 return std::make_pair(displays_[1].id(), id_at_zero);
349 void DisplayManager::SetLayoutForCurrentDisplays(
350 const DisplayLayout& layout_relative_to_primary) {
351 DCHECK_EQ(2U, GetNumDisplays());
352 if (GetNumDisplays() < 2)
353 return;
354 const gfx::Display& primary = screen_->GetPrimaryDisplay();
355 const DisplayIdPair pair = GetCurrentDisplayIdPair();
356 // Invert if the primary was swapped.
357 DisplayLayout to_set = pair.first == primary.id() ?
358 layout_relative_to_primary : layout_relative_to_primary.Invert();
360 DisplayLayout current_layout =
361 layout_store_->GetRegisteredDisplayLayout(pair);
362 if (to_set.position != current_layout.position ||
363 to_set.offset != current_layout.offset) {
364 to_set.primary_id = primary.id();
365 layout_store_->RegisterLayoutForDisplayIdPair(
366 pair.first, pair.second, to_set);
367 if (delegate_)
368 delegate_->PreDisplayConfigurationChange(false);
369 // PreDisplayConfigurationChange(false);
370 // TODO(oshima): Call UpdateDisplays instead.
371 const DisplayLayout layout = GetCurrentDisplayLayout();
372 UpdateDisplayBoundsForLayoutById(
373 layout, primary,
374 ScreenUtil::GetSecondaryDisplay().id());
376 // Primary's bounds stay the same. Just notify bounds change
377 // on the secondary.
378 screen_ash_->NotifyMetricsChanged(
379 ScreenUtil::GetSecondaryDisplay(),
380 gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
381 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
382 if (delegate_)
383 delegate_->PostDisplayConfigurationChange();
387 const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const {
388 gfx::Display* display =
389 const_cast<DisplayManager*>(this)->FindDisplayForId(id);
390 return display ? *display : GetInvalidDisplay();
393 const gfx::Display& DisplayManager::FindDisplayContainingPoint(
394 const gfx::Point& point_in_screen) const {
395 for (DisplayList::const_iterator iter = displays_.begin();
396 iter != displays_.end(); ++iter) {
397 const gfx::Display& display = *iter;
398 if (display.bounds().Contains(point_in_screen))
399 return display;
401 return GetInvalidDisplay();
404 bool DisplayManager::UpdateWorkAreaOfDisplay(int64 display_id,
405 const gfx::Insets& insets) {
406 gfx::Display* display = FindDisplayForId(display_id);
407 DCHECK(display);
408 gfx::Rect old_work_area = display->work_area();
409 display->UpdateWorkAreaFromInsets(insets);
410 return old_work_area != display->work_area();
413 void DisplayManager::SetOverscanInsets(int64 display_id,
414 const gfx::Insets& insets_in_dip) {
415 display_info_[display_id].SetOverscanInsets(insets_in_dip);
416 DisplayInfoList display_info_list;
417 for (DisplayList::const_iterator iter = displays_.begin();
418 iter != displays_.end(); ++iter) {
419 display_info_list.push_back(GetDisplayInfo(iter->id()));
421 AddMirrorDisplayInfoIfAny(&display_info_list);
422 UpdateDisplays(display_info_list);
425 void DisplayManager::SetDisplayRotation(int64 display_id,
426 gfx::Display::Rotation rotation) {
427 DisplayInfoList display_info_list;
428 for (DisplayList::const_iterator iter = displays_.begin();
429 iter != displays_.end(); ++iter) {
430 DisplayInfo info = GetDisplayInfo(iter->id());
431 if (info.id() == display_id) {
432 if (info.rotation() == rotation)
433 return;
434 info.set_rotation(rotation);
436 display_info_list.push_back(info);
438 AddMirrorDisplayInfoIfAny(&display_info_list);
439 UpdateDisplays(display_info_list);
442 void DisplayManager::SetDisplayUIScale(int64 display_id,
443 float ui_scale) {
444 if (!IsDisplayUIScalingEnabled() ||
445 gfx::Display::InternalDisplayId() != display_id) {
446 return;
449 // TODO(mukai): merge this implementation into SetDisplayMode().
450 DisplayInfoList display_info_list;
451 for (DisplayList::const_iterator iter = displays_.begin();
452 iter != displays_.end(); ++iter) {
453 DisplayInfo info = GetDisplayInfo(iter->id());
454 if (info.id() == display_id) {
455 if (info.configured_ui_scale() == ui_scale)
456 return;
457 std::vector<float> scales = GetScalesForDisplay(info);
458 ScaleComparator comparator(ui_scale);
459 if (std::find_if(scales.begin(), scales.end(), comparator) ==
460 scales.end()) {
461 return;
463 info.set_configured_ui_scale(ui_scale);
465 display_info_list.push_back(info);
467 AddMirrorDisplayInfoIfAny(&display_info_list);
468 UpdateDisplays(display_info_list);
471 void DisplayManager::SetDisplayResolution(int64 display_id,
472 const gfx::Size& resolution) {
473 DCHECK_NE(gfx::Display::InternalDisplayId(), display_id);
474 if (gfx::Display::InternalDisplayId() == display_id)
475 return;
476 const DisplayInfo& display_info = GetDisplayInfo(display_id);
477 const std::vector<DisplayMode>& modes = display_info.display_modes();
478 DCHECK_NE(0u, modes.size());
479 DisplayMode target_mode;
480 target_mode.size = resolution;
481 std::vector<DisplayMode>::const_iterator iter =
482 std::find_if(modes.begin(), modes.end(), DisplayModeMatcher(target_mode));
483 if (iter == modes.end()) {
484 LOG(WARNING) << "Unsupported resolution was requested:"
485 << resolution.ToString();
486 return;
488 display_modes_[display_id] = *iter;
489 #if defined(OS_CHROMEOS)
490 if (base::SysInfo::IsRunningOnChromeOS())
491 Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
492 #endif
495 bool DisplayManager::SetDisplayMode(int64 display_id,
496 const DisplayMode& display_mode) {
497 if (IsInternalDisplayId(display_id)) {
498 SetDisplayUIScale(display_id, display_mode.ui_scale);
499 return false;
502 DisplayInfoList display_info_list;
503 bool display_property_changed = false;
504 bool resolution_changed = false;
505 for (DisplayList::const_iterator iter = displays_.begin();
506 iter != displays_.end(); ++iter) {
507 DisplayInfo info = GetDisplayInfo(iter->id());
508 if (info.id() == display_id) {
509 const std::vector<DisplayMode>& modes = info.display_modes();
510 std::vector<DisplayMode>::const_iterator iter =
511 std::find_if(modes.begin(),
512 modes.end(),
513 DisplayModeMatcher(display_mode));
514 if (iter == modes.end()) {
515 LOG(WARNING) << "Unsupported resolution was requested:"
516 << display_mode.size.ToString();
517 return false;
519 display_modes_[display_id] = *iter;
520 if (info.bounds_in_native().size() != display_mode.size)
521 resolution_changed = true;
522 if (info.device_scale_factor() != display_mode.device_scale_factor) {
523 info.set_device_scale_factor(display_mode.device_scale_factor);
524 display_property_changed = true;
527 display_info_list.push_back(info);
529 if (display_property_changed) {
530 AddMirrorDisplayInfoIfAny(&display_info_list);
531 UpdateDisplays(display_info_list);
533 #if defined(OS_CHROMEOS)
534 if (resolution_changed && base::SysInfo::IsRunningOnChromeOS())
535 Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
536 #endif
537 return resolution_changed;
540 void DisplayManager::RegisterDisplayProperty(
541 int64 display_id,
542 gfx::Display::Rotation rotation,
543 float ui_scale,
544 const gfx::Insets* overscan_insets,
545 const gfx::Size& resolution_in_pixels,
546 float device_scale_factor,
547 ui::ColorCalibrationProfile color_profile) {
548 if (display_info_.find(display_id) == display_info_.end())
549 display_info_[display_id] = DisplayInfo(display_id, std::string(), false);
551 display_info_[display_id].set_rotation(rotation);
552 display_info_[display_id].SetColorProfile(color_profile);
553 // Just in case the preference file was corrupted.
554 // TODO(mukai): register |display_modes_| here as well, so the lookup for the
555 // default mode in GetActiveModeForDisplayId() gets much simpler.
556 if (0.5f <= ui_scale && ui_scale <= 2.0f)
557 display_info_[display_id].set_configured_ui_scale(ui_scale);
558 if (overscan_insets)
559 display_info_[display_id].SetOverscanInsets(*overscan_insets);
560 if (!resolution_in_pixels.IsEmpty()) {
561 DCHECK(!IsInternalDisplayId(display_id));
562 // Default refresh rate, until OnNativeDisplaysChanged() updates us with the
563 // actual display info, is 60 Hz.
564 DisplayMode mode(resolution_in_pixels, 60.0f, false, false);
565 mode.device_scale_factor = device_scale_factor;
566 display_modes_[display_id] = mode;
570 DisplayMode DisplayManager::GetActiveModeForDisplayId(int64 display_id) const {
571 DisplayMode selected_mode;
572 if (GetSelectedModeForDisplayId(display_id, &selected_mode))
573 return selected_mode;
575 // If 'selected' mode is empty, it should return the default mode. This means
576 // the native mode for the external display. Unfortunately this is not true
577 // for the internal display because restoring UI-scale doesn't register the
578 // restored mode to |display_mode_|, so it needs to look up the mode whose
579 // UI-scale value matches. See the TODO in RegisterDisplayProperty().
580 const DisplayInfo& info = GetDisplayInfo(display_id);
581 const std::vector<DisplayMode>& display_modes = info.display_modes();
583 if (IsInternalDisplayId(display_id)) {
584 for (size_t i = 0; i < display_modes.size(); ++i) {
585 if (info.configured_ui_scale() == display_modes[i].ui_scale)
586 return display_modes[i];
588 } else {
589 for (size_t i = 0; i < display_modes.size(); ++i) {
590 if (display_modes[i].native)
591 return display_modes[i];
594 return selected_mode;
597 void DisplayManager::RegisterDisplayRotationProperties(bool rotation_lock,
598 gfx::Display::Rotation rotation) {
599 if (delegate_)
600 delegate_->PreDisplayConfigurationChange(false);
601 registered_internal_display_rotation_lock_ = rotation_lock;
602 registered_internal_display_rotation_ = rotation;
603 if (delegate_)
604 delegate_->PostDisplayConfigurationChange();
607 bool DisplayManager::GetSelectedModeForDisplayId(int64 id,
608 DisplayMode* mode_out) const {
609 std::map<int64, DisplayMode>::const_iterator iter = display_modes_.find(id);
610 if (iter == display_modes_.end())
611 return false;
612 *mode_out = iter->second;
613 return true;
616 bool DisplayManager::IsDisplayUIScalingEnabled() const {
617 return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID;
620 gfx::Insets DisplayManager::GetOverscanInsets(int64 display_id) const {
621 std::map<int64, DisplayInfo>::const_iterator it =
622 display_info_.find(display_id);
623 return (it != display_info_.end()) ?
624 it->second.overscan_insets_in_dip() : gfx::Insets();
627 void DisplayManager::SetColorCalibrationProfile(
628 int64 display_id,
629 ui::ColorCalibrationProfile profile) {
630 #if defined(OS_CHROMEOS)
631 if (!display_info_[display_id].IsColorProfileAvailable(profile))
632 return;
634 if (delegate_)
635 delegate_->PreDisplayConfigurationChange(false);
636 // Just sets color profile if it's not running on ChromeOS (like tests).
637 if (!base::SysInfo::IsRunningOnChromeOS() ||
638 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
639 display_id, profile)) {
640 display_info_[display_id].SetColorProfile(profile);
641 UMA_HISTOGRAM_ENUMERATION(
642 "ChromeOS.Display.ColorProfile", profile, ui::NUM_COLOR_PROFILES);
644 if (delegate_)
645 delegate_->PostDisplayConfigurationChange();
646 #endif
649 void DisplayManager::OnNativeDisplaysChanged(
650 const std::vector<DisplayInfo>& updated_displays) {
651 if (updated_displays.empty()) {
652 VLOG(1) << "OnNativeDisplaysChanged(0): # of current displays="
653 << displays_.size();
654 // If the device is booted without display, or chrome is started
655 // without --ash-host-window-bounds on linux desktop, use the
656 // default display.
657 if (displays_.empty()) {
658 std::vector<DisplayInfo> init_displays;
659 init_displays.push_back(DisplayInfo::CreateFromSpec(std::string()));
660 MaybeInitInternalDisplay(init_displays[0].id());
661 OnNativeDisplaysChanged(init_displays);
662 } else {
663 // Otherwise don't update the displays when all displays are disconnected.
664 // This happens when:
665 // - the device is idle and powerd requested to turn off all displays.
666 // - the device is suspended. (kernel turns off all displays)
667 // - the internal display's brightness is set to 0 and no external
668 // display is connected.
669 // - the internal display's brightness is 0 and external display is
670 // disconnected.
671 // The display will be updated when one of displays is turned on, and the
672 // display list will be updated correctly.
674 return;
676 first_display_id_ = updated_displays[0].id();
677 std::set<gfx::Point> origins;
679 if (updated_displays.size() == 1) {
680 VLOG(1) << "OnNativeDisplaysChanged(1):" << updated_displays[0].ToString();
681 } else {
682 VLOG(1) << "OnNativeDisplaysChanged(" << updated_displays.size()
683 << ") [0]=" << updated_displays[0].ToString()
684 << ", [1]=" << updated_displays[1].ToString();
687 bool internal_display_connected = false;
688 num_connected_displays_ = updated_displays.size();
689 mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
690 non_desktop_display_ = gfx::Display();
691 DisplayInfoList new_display_info_list;
692 for (DisplayInfoList::const_iterator iter = updated_displays.begin();
693 iter != updated_displays.end();
694 ++iter) {
695 if (!internal_display_connected)
696 internal_display_connected = IsInternalDisplayId(iter->id());
697 // Mirrored monitors have the same origins.
698 gfx::Point origin = iter->bounds_in_native().origin();
699 if (origins.find(origin) != origins.end()) {
700 InsertAndUpdateDisplayInfo(*iter);
701 mirrored_display_id_ = iter->id();
702 } else {
703 origins.insert(origin);
704 new_display_info_list.push_back(*iter);
707 DisplayMode new_mode;
708 new_mode.size = iter->bounds_in_native().size();
709 new_mode.device_scale_factor = iter->device_scale_factor();
710 new_mode.ui_scale = iter->configured_ui_scale();
711 const std::vector<DisplayMode>& display_modes = iter->display_modes();
712 // This is empty the displays are initialized from InitFromCommandLine.
713 if (!display_modes.size())
714 continue;
715 std::vector<DisplayMode>::const_iterator display_modes_iter =
716 std::find_if(display_modes.begin(),
717 display_modes.end(),
718 DisplayModeMatcher(new_mode));
719 // Update the actual resolution selected as the resolution request may fail.
720 if (display_modes_iter == display_modes.end())
721 display_modes_.erase(iter->id());
722 else if (display_modes_.find(iter->id()) != display_modes_.end())
723 display_modes_[iter->id()] = *display_modes_iter;
725 if (HasInternalDisplay() &&
726 !internal_display_connected &&
727 display_info_.find(gfx::Display::InternalDisplayId()) ==
728 display_info_.end()) {
729 DisplayInfo internal_display_info(
730 gfx::Display::InternalDisplayId(),
731 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME),
732 false /*Internal display must not have overscan */);
733 internal_display_info.SetBounds(gfx::Rect(0, 0, 800, 600));
734 display_info_[gfx::Display::InternalDisplayId()] = internal_display_info;
736 UpdateDisplays(new_display_info_list);
739 void DisplayManager::UpdateDisplays() {
740 DisplayInfoList display_info_list;
741 for (DisplayList::const_iterator iter = displays_.begin();
742 iter != displays_.end(); ++iter) {
743 display_info_list.push_back(GetDisplayInfo(iter->id()));
745 AddMirrorDisplayInfoIfAny(&display_info_list);
746 UpdateDisplays(display_info_list);
749 void DisplayManager::UpdateDisplays(
750 const std::vector<DisplayInfo>& updated_display_info_list) {
751 #if defined(OS_WIN)
752 DCHECK_EQ(1u, updated_display_info_list.size()) <<
753 ": Multiple display test does not work on Windows bots. Please "
754 "skip (don't disable) the test using SupportsMultipleDisplays()";
755 #endif
757 DisplayInfoList new_display_info_list = updated_display_info_list;
758 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor());
759 std::sort(new_display_info_list.begin(),
760 new_display_info_list.end(),
761 DisplayInfoSortFunctor());
762 DisplayList removed_displays;
763 std::map<size_t, uint32_t> display_changes;
764 std::vector<size_t> added_display_indices;
766 DisplayList::iterator curr_iter = displays_.begin();
767 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin();
769 DisplayList new_displays;
771 // Use the internal display or 1st as the mirror source, then scale
772 // the root window so that it matches the external display's
773 // resolution. This is necessary in order for scaling to work while
774 // mirrored.
775 int64 non_desktop_display_id = gfx::Display::kInvalidDisplayID;
777 if (second_display_mode_ != EXTENDED && new_display_info_list.size() == 2) {
778 bool zero_is_source =
779 first_display_id_ == new_display_info_list[0].id() ||
780 gfx::Display::InternalDisplayId() == new_display_info_list[0].id();
781 if (second_display_mode_ == MIRRORING) {
782 mirrored_display_id_ = new_display_info_list[zero_is_source ? 1 : 0].id();
783 non_desktop_display_id = mirrored_display_id_;
784 } else {
785 // TODO(oshima|bshe): The virtual keyboard is currently assigned to
786 // the 1st display.
787 non_desktop_display_id =
788 new_display_info_list[zero_is_source ? 0 : 1].id();
792 while (curr_iter != displays_.end() ||
793 new_info_iter != new_display_info_list.end()) {
794 if (new_info_iter != new_display_info_list.end() &&
795 non_desktop_display_id == new_info_iter->id()) {
796 DisplayInfo info = *new_info_iter;
797 info.SetOverscanInsets(gfx::Insets());
798 InsertAndUpdateDisplayInfo(info);
799 non_desktop_display_ =
800 CreateDisplayFromDisplayInfoById(non_desktop_display_id);
801 ++new_info_iter;
802 // Remove existing external display if it is going to be used as
803 // non desktop.
804 if (curr_iter != displays_.end() &&
805 curr_iter->id() == non_desktop_display_id) {
806 removed_displays.push_back(*curr_iter);
807 ++curr_iter;
809 continue;
812 if (curr_iter == displays_.end()) {
813 // more displays in new list.
814 added_display_indices.push_back(new_displays.size());
815 InsertAndUpdateDisplayInfo(*new_info_iter);
816 new_displays.push_back(
817 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
818 ++new_info_iter;
819 } else if (new_info_iter == new_display_info_list.end()) {
820 // more displays in current list.
821 removed_displays.push_back(*curr_iter);
822 ++curr_iter;
823 } else if (curr_iter->id() == new_info_iter->id()) {
824 const gfx::Display& current_display = *curr_iter;
825 // Copy the info because |CreateDisplayFromInfo| updates the instance.
826 const DisplayInfo current_display_info =
827 GetDisplayInfo(current_display.id());
828 InsertAndUpdateDisplayInfo(*new_info_iter);
829 gfx::Display new_display =
830 CreateDisplayFromDisplayInfoById(new_info_iter->id());
831 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id());
833 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_NONE;
835 // At that point the new Display objects we have are not entirely updated,
836 // they are missing the translation related to the Display disposition in
837 // the layout.
838 // Using display.bounds() and display.work_area() would fail most of the
839 // time.
840 if (force_bounds_changed_ || (current_display_info.bounds_in_native() !=
841 new_display_info.bounds_in_native()) ||
842 (current_display_info.size_in_pixel() !=
843 new_display.GetSizeInPixel())) {
844 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
845 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
848 if (current_display.device_scale_factor() !=
849 new_display.device_scale_factor()) {
850 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
853 if (current_display.rotation() != new_display.rotation())
854 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_ROTATION;
856 if (metrics != gfx::DisplayObserver::DISPLAY_METRIC_NONE) {
857 display_changes.insert(
858 std::pair<size_t, uint32_t>(new_displays.size(), metrics));
861 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
862 new_displays.push_back(new_display);
863 ++curr_iter;
864 ++new_info_iter;
865 } else if (curr_iter->id() < new_info_iter->id()) {
866 // more displays in current list between ids, which means it is deleted.
867 removed_displays.push_back(*curr_iter);
868 ++curr_iter;
869 } else {
870 // more displays in new list between ids, which means it is added.
871 added_display_indices.push_back(new_displays.size());
872 InsertAndUpdateDisplayInfo(*new_info_iter);
873 new_displays.push_back(
874 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
875 ++new_info_iter;
879 scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater(
880 new NonDesktopDisplayUpdater(this, delegate_));
882 // Clear focus if the display has been removed, but don't clear focus if
883 // the destkop has been moved from one display to another
884 // (mirror -> docked, docked -> single internal).
885 bool clear_focus =
886 !removed_displays.empty() &&
887 !(removed_displays.size() == 1 && added_display_indices.size() == 1);
888 if (delegate_)
889 delegate_->PreDisplayConfigurationChange(clear_focus);
891 // Do not update |displays_| if there's nothing to be updated. Without this,
892 // it will not update the display layout, which causes the bug
893 // http://crbug.com/155948.
894 if (display_changes.empty() && added_display_indices.empty() &&
895 removed_displays.empty()) {
896 // When changing from software mirroring mode to sinlge display mode, it
897 // is possible there is no need to update |displays_| and we early out
898 // here. But we still want to run the PostDisplayConfigurationChange()
899 // cause there are some clients need to act on this, e.g.
900 // TouchTransformerController needs to adjust the TouchTransformer when
901 // switching from dual displays to single display.
902 if (delegate_)
903 delegate_->PostDisplayConfigurationChange();
904 return;
907 size_t updated_index;
908 if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) &&
909 std::find(added_display_indices.begin(),
910 added_display_indices.end(),
911 updated_index) == added_display_indices.end()) {
912 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
913 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
914 if (display_changes.find(updated_index) != display_changes.end())
915 metrics |= display_changes[updated_index];
917 display_changes[updated_index] = metrics;
920 displays_ = new_displays;
922 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false);
924 // Temporarily add displays to be removed because display object
925 // being removed are accessed during shutting down the root.
926 displays_.insert(displays_.end(), removed_displays.begin(),
927 removed_displays.end());
929 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin();
930 iter != removed_displays.rend(); ++iter) {
931 screen_ash_->NotifyDisplayRemoved(displays_.back());
932 displays_.pop_back();
934 // Close the non desktop window here to avoid creating two compositor on
935 // one display.
936 if (!non_desktop_display_updater->enabled())
937 non_desktop_display_updater.reset();
938 for (std::vector<size_t>::iterator iter = added_display_indices.begin();
939 iter != added_display_indices.end(); ++iter) {
940 screen_ash_->NotifyDisplayAdded(displays_[*iter]);
942 // Create the non destkop window after all displays are added so that
943 // it can mirror the display newly added. This can happen when switching
944 // from dock mode to software mirror mode.
945 non_desktop_display_updater.reset();
946 for (std::map<size_t, uint32_t>::iterator iter = display_changes.begin();
947 iter != display_changes.end();
948 ++iter) {
949 screen_ash_->NotifyMetricsChanged(displays_[iter->first], iter->second);
951 if (delegate_)
952 delegate_->PostDisplayConfigurationChange();
954 #if defined(USE_X11) && defined(OS_CHROMEOS)
955 if (!display_changes.empty() && base::SysInfo::IsRunningOnChromeOS())
956 ui::ClearX11DefaultRootWindow();
957 #endif
960 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const {
961 DCHECK_LT(index, displays_.size());
962 return displays_[index];
965 const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const {
966 if (GetNumDisplays() == 1)
967 return displays_[0];
968 DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(
969 GetCurrentDisplayIdPair());
970 return GetDisplayForId(layout.primary_id);
973 size_t DisplayManager::GetNumDisplays() const {
974 return displays_.size();
977 bool DisplayManager::IsMirrored() const {
978 return mirrored_display_id_ != gfx::Display::kInvalidDisplayID;
981 const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const {
982 DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id);
984 std::map<int64, DisplayInfo>::const_iterator iter =
985 display_info_.find(display_id);
986 CHECK(iter != display_info_.end()) << display_id;
987 return iter->second;
990 std::string DisplayManager::GetDisplayNameForId(int64 id) {
991 if (id == gfx::Display::kInvalidDisplayID)
992 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
994 std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id);
995 if (iter != display_info_.end() && !iter->second.name().empty())
996 return iter->second.name();
998 return base::StringPrintf("Display %d", static_cast<int>(id));
1001 int64 DisplayManager::GetDisplayIdForUIScaling() const {
1002 // UI Scaling is effective only on internal display.
1003 int64 display_id = gfx::Display::InternalDisplayId();
1004 #if defined(OS_WIN)
1005 display_id = first_display_id();
1006 #endif
1007 return display_id;
1010 void DisplayManager::SetMirrorMode(bool mirrored) {
1011 if (num_connected_displays() <= 1)
1012 return;
1014 #if defined(OS_CHROMEOS)
1015 if (base::SysInfo::IsRunningOnChromeOS()) {
1016 ui::MultipleDisplayState new_state =
1017 mirrored ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR :
1018 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
1019 Shell::GetInstance()->display_configurator()->SetDisplayMode(new_state);
1020 return;
1022 #endif
1023 // This is fallback path to emulate mirroroing on desktop.
1024 SetSecondDisplayMode(mirrored ? MIRRORING : EXTENDED);
1025 DisplayInfoList display_info_list;
1026 int count = 0;
1027 for (std::map<int64, DisplayInfo>::const_iterator iter =
1028 display_info_.begin();
1029 count < 2; ++iter, ++count) {
1030 display_info_list.push_back(GetDisplayInfo(iter->second.id()));
1032 UpdateDisplays(display_info_list);
1033 #if defined(OS_CHROMEOS)
1034 if (Shell::GetInstance()->display_configurator_animation()) {
1035 Shell::GetInstance()->display_configurator_animation()->
1036 StartFadeInAnimation();
1038 #endif
1041 void DisplayManager::AddRemoveDisplay() {
1042 DCHECK(!displays_.empty());
1043 std::vector<DisplayInfo> new_display_info_list;
1044 const DisplayInfo& first_display = GetDisplayInfo(displays_[0].id());
1045 new_display_info_list.push_back(first_display);
1046 // Add if there is only one display connected.
1047 if (num_connected_displays() == 1) {
1048 // Layout the 2nd display below the primary as with the real device.
1049 gfx::Rect host_bounds = first_display.bounds_in_native();
1050 new_display_info_list.push_back(DisplayInfo::CreateFromSpec(
1051 base::StringPrintf(
1052 "%d+%d-500x400", host_bounds.x(), host_bounds.bottom())));
1054 num_connected_displays_ = new_display_info_list.size();
1055 mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
1056 non_desktop_display_ = gfx::Display();
1057 UpdateDisplays(new_display_info_list);
1060 void DisplayManager::ToggleDisplayScaleFactor() {
1061 DCHECK(!displays_.empty());
1062 std::vector<DisplayInfo> new_display_info_list;
1063 for (DisplayList::const_iterator iter = displays_.begin();
1064 iter != displays_.end(); ++iter) {
1065 DisplayInfo display_info = GetDisplayInfo(iter->id());
1066 display_info.set_device_scale_factor(
1067 display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f);
1068 new_display_info_list.push_back(display_info);
1070 AddMirrorDisplayInfoIfAny(&new_display_info_list);
1071 UpdateDisplays(new_display_info_list);
1074 #if defined(OS_CHROMEOS)
1075 void DisplayManager::SetSoftwareMirroring(bool enabled) {
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