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