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/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" 34 35 #if defined(USE_X11) 36 #include "ui/base/x/x11_util.h" 37 #endif 38 39 #if defined(OS_CHROMEOS) 40 #include "ash/display/display_configurator_animation.h" 41 #include "base/sys_info.h" 42 #endif 43 44 #if defined(OS_WIN) 45 #include "base/win/windows_version.h" 46 #endif 47 48 namespace ash { 49 typedef std::vector<gfx::Display> DisplayList; 50 typedef std::vector<DisplayInfo> DisplayInfoList; 51 52 namespace { 53 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; 57 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; 61 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 }; 71 72 struct DisplaySortFunctor { 73 bool operator()(const gfx::Display& a, const gfx::Display& b) { 74 return a.id() < b.id(); 75 } 76 }; 77 78 struct DisplayInfoSortFunctor { 79 bool operator()(const DisplayInfo& a, const DisplayInfo& b) { 80 return a.id() < b.id(); 81 } 82 }; 83 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); 89 } 90 DisplayMode target_mode; 91 }; 92 93 struct ScaleComparator { 94 explicit ScaleComparator(float s) : scale(s) {} 95 96 bool operator()(float s) const { 97 const float kEpsilon = 0.0001f; 98 return std::abs(scale - s) < kEpsilon; 99 } 100 float scale; 101 }; 102 103 gfx::Display& GetInvalidDisplay() { 104 static gfx::Display* invalid_display = new gfx::Display(); 105 return *invalid_display; 106 } 107 108 void MaybeInitInternalDisplay(int64 id) { 109 CommandLine* command_line = CommandLine::ForCurrentProcess(); 110 if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal)) 111 gfx::Display::SetInternalDisplayId(id); 112 } 113 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()) { 124 } 125 126 ~NonDesktopDisplayUpdater() { 127 if (!delegate_) 128 return; 129 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(); 136 } 137 } 138 139 bool enabled() const { return enabled_; } 140 141 private: 142 DisplayManager* manager_; 143 DisplayManager::Delegate* delegate_; 144 bool enabled_; 145 DISALLOW_COPY_AND_ASSIGN(NonDesktopDisplayUpdater); 146 }; 147 148 } // namespace 149 150 using std::string; 151 using std::vector; 152 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) { 166 167 #if defined(OS_CHROMEOS) 168 // Enable only on the device so that DisplayManagerFontTest passes. 169 if (base::SysInfo::IsRunningOnChromeOS()) 170 DisplayInfo::SetUse125DSFForUIScaling(true); 171 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()); 184 } 185 } 186 187 DisplayManager::~DisplayManager() { 188 #if defined(OS_CHROMEOS) 189 // Reset the font params. 190 gfx::SetFontRenderParamsDeviceScaleFactor(1.0f); 191 #endif 192 } 193 194 // static 195 std::vector<float> DisplayManager::GetScalesForDisplay( 196 const DisplayInfo& info) { 197 198 #define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a)) 199 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; 207 } 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 221 } 222 return ret; 223 } 224 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]; 236 } 237 } 238 // Fallback to 1.0f if the |scale| wasn't in the list. 239 return 1.0f; 240 } 241 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); 255 } 256 MaybeInitInternalDisplay(info_list[0].id()); 257 if (info_list.size() > 1 && 258 command_line->HasSwitch(switches::kAshEnableSoftwareMirroring)) { 259 SetSecondDisplayMode(MIRRORING); 260 } 261 OnNativeDisplaysChanged(info_list); 262 return true; 263 } 264 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); 271 } 272 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 282 } 283 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)); 294 } 295 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; 301 } 302 return false; 303 } 304 305 bool DisplayManager::HasInternalDisplay() const { 306 return gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID; 307 } 308 309 bool DisplayManager::IsInternalDisplayId(int64 id) const { 310 return gfx::Display::InternalDisplayId() == id; 311 } 312 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); 319 } 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; 326 } 327 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()); 335 } 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); 345 } 346 } 347 } 348 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(); 359 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()); 375 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(); 384 } 385 } 386 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(); 391 } 392 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; 400 } 401 return GetInvalidDisplay(); 402 } 403 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(); 411 } 412 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())); 420 } 421 AddMirrorDisplayInfoIfAny(&display_info_list); 422 UpdateDisplays(display_info_list); 423 } 424 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); 435 } 436 display_info_list.push_back(info); 437 } 438 AddMirrorDisplayInfoIfAny(&display_info_list); 439 UpdateDisplays(display_info_list); 440 } 441 442 void DisplayManager::SetDisplayUIScale(int64 display_id, 443 float ui_scale) { 444 if (!IsDisplayUIScalingEnabled() || 445 gfx::Display::InternalDisplayId() != display_id) { 446 return; 447 } 448 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; 462 } 463 info.set_configured_ui_scale(ui_scale); 464 } 465 display_info_list.push_back(info); 466 } 467 AddMirrorDisplayInfoIfAny(&display_info_list); 468 UpdateDisplays(display_info_list); 469 } 470 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; 487 } 488 display_modes_[display_id] = *iter; 489 #if defined(OS_CHROMEOS) 490 if (base::SysInfo::IsRunningOnChromeOS()) 491 Shell::GetInstance()->display_configurator()->OnConfigurationChanged(); 492 #endif 493 } 494 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; 500 } 501 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; 518 } 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; 525 } 526 } 527 display_info_list.push_back(info); 528 } 529 if (display_property_changed) { 530 AddMirrorDisplayInfoIfAny(&display_info_list); 531 UpdateDisplays(display_info_list); 532 } 533 #if defined(OS_CHROMEOS) 534 if (resolution_changed && base::SysInfo::IsRunningOnChromeOS()) 535 Shell::GetInstance()->display_configurator()->OnConfigurationChanged(); 536 #endif 537 return resolution_changed; 538 } 539 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); 550 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; 567 } 568 } 569 570 DisplayMode DisplayManager::GetActiveModeForDisplayId(int64 display_id) const { 571 DisplayMode selected_mode; 572 if (GetSelectedModeForDisplayId(display_id, &selected_mode)) 573 return selected_mode; 574 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(); 582 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]; 587 } 588 } else { 589 for (size_t i = 0; i < display_modes.size(); ++i) { 590 if (display_modes[i].native) 591 return display_modes[i]; 592 } 593 } 594 return selected_mode; 595 } 596 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(); 605 } 606 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; 614 } 615 616 bool DisplayManager::IsDisplayUIScalingEnabled() const { 617 return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID; 618 } 619 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(); 625 } 626 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; 633 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); 643 } 644 if (delegate_) 645 delegate_->PostDisplayConfigurationChange(); 646 #endif 647 } 648 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. 673 } 674 return; 675 } 676 first_display_id_ = updated_displays[0].id(); 677 std::set<gfx::Point> origins; 678 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(); 685 } 686 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); 705 } 706 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; 724 } 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; 735 } 736 UpdateDisplays(new_display_info_list); 737 } 738 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())); 744 } 745 AddMirrorDisplayInfoIfAny(&display_info_list); 746 UpdateDisplays(display_info_list); 747 } 748 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 756 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; 765 766 DisplayList::iterator curr_iter = displays_.begin(); 767 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin(); 768 769 DisplayList new_displays; 770 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; 776 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(); 789 } 790 } 791 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; 808 } 809 continue; 810 } 811 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()); 832 833 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_NONE; 834 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; 846 } 847 848 if (current_display.device_scale_factor() != 849 new_display.device_scale_factor()) { 850 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR; 851 } 852 853 if (current_display.rotation() != new_display.rotation()) 854 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_ROTATION; 855 856 if (metrics != gfx::DisplayObserver::DISPLAY_METRIC_NONE) { 857 display_changes.insert( 858 std::pair<size_t, uint32_t>(new_displays.size(), metrics)); 859 } 860 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; 876 } 877 } 878 879 scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater( 880 new NonDesktopDisplayUpdater(this, delegate_)); 881 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); 890 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; 905 } 906 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]; 916 917 display_changes[updated_index] = metrics; 918 } 919 920 displays_ = new_displays; 921 922 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false); 923 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()); 928 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(); 933 } 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]); 941 } 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); 950 } 951 if (delegate_) 952 delegate_->PostDisplayConfigurationChange(); 953 954 #if defined(USE_X11) && defined(OS_CHROMEOS) 955 if (!display_changes.empty() && base::SysInfo::IsRunningOnChromeOS()) 956 ui::ClearX11DefaultRootWindow(); 957 #endif 958 } 959 960 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const { 961 DCHECK_LT(index, displays_.size()); 962 return displays_[index]; 963 } 964 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); 971 } 972 973 size_t DisplayManager::GetNumDisplays() const { 974 return displays_.size(); 975 } 976 977 bool DisplayManager::IsMirrored() const { 978 return mirrored_display_id_ != gfx::Display::kInvalidDisplayID; 979 } 980 981 const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const { 982 DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id); 983 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; 988 } 989 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); 993 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(); 997 998 return base::StringPrintf("Display %d", static_cast<int>(id)); 999 } 1000 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; 1008 } 1009 1010 void DisplayManager::SetMirrorMode(bool mirrored) { 1011 if (num_connected_displays() <= 1) 1012 return; 1013 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; 1021 } 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())); 1031 } 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(); 1037 } 1038 #endif 1039 } 1040 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()))); 1053 } 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); 1058 } 1059 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); 1069 } 1070 AddMirrorDisplayInfoIfAny(&new_display_info_list); 1071 UpdateDisplays(new_display_info_list); 1072 } 1073 1074 #if defined(OS_CHROMEOS) 1075 void DisplayManager::SetSoftwareMirroring(bool enabled) { 1076 SetSecondDisplayMode(enabled ? MIRRORING : EXTENDED); 1077 } 1078 1079 bool DisplayManager::SoftwareMirroringEnabled() const { 1080 return software_mirroring_enabled(); 1081 } 1082 #endif 1083 1084 void DisplayManager::SetSecondDisplayMode(SecondDisplayMode mode) { 1085 second_display_mode_ = mode; 1086 mirrored_display_id_ = gfx::Display::kInvalidDisplayID; 1087 non_desktop_display_ = gfx::Display(); 1088 } 1089 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; 1102 } 1103 return false; 1104 } 1105 1106 void DisplayManager::CreateMirrorWindowIfAny() { 1107 NonDesktopDisplayUpdater updater(this, delegate_); 1108 } 1109 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); 1121 } 1122 } 1123 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); 1129 } 1130 DLOG(WARNING) << "Could not find display:" << id; 1131 return NULL; 1132 } 1133 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_)); 1138 } 1139 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); 1148 } 1149 display_info_[new_info.id()].UpdateDisplaySize(); 1150 1151 OnDisplayInfoUpdated(display_info_[new_info.id()]); 1152 } 1153 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); 1160 } 1161 #endif 1162 } 1163 1164 gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) { 1165 DCHECK(display_info_.find(id) != display_info_.end()); 1166 const DisplayInfo& display_info = display_info_[id]; 1167 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(); 1171 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; 1180 } 1181 1182 bool DisplayManager::UpdateSecondaryDisplayBoundsForLayout( 1183 DisplayList* displays, 1184 size_t* updated_index) const { 1185 if (displays->size() != 2U) 1186 return false; 1187 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); 1196 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; 1207 } 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(); 1217 } 1218 return false; 1219 } 1220 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()); 1227 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(); 1231 1232 DisplayLayout::Position position = layout.position; 1233 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); 1247 } 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; 1261 } 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); 1266 } 1267 1268 } // namespace ash 1269