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