display_manager.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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/screen_ash.h" 15#include "ash/shell.h" 16#include "base/auto_reset.h" 17#include "base/command_line.h" 18#include "base/logging.h" 19#include "base/strings/string_number_conversions.h" 20#include "base/strings/string_split.h" 21#include "base/strings/stringprintf.h" 22#include "base/strings/utf_string_conversions.h" 23#include "grit/ash_strings.h" 24#include "ui/base/l10n/l10n_util.h" 25#include "ui/gfx/display.h" 26#include "ui/gfx/rect.h" 27#include "ui/gfx/screen.h" 28#include "ui/gfx/size_conversions.h" 29 30#if defined(USE_X11) 31#include "ui/base/x/x11_util.h" 32#endif 33 34#if defined(OS_CHROMEOS) 35#include "ash/display/output_configurator_animation.h" 36#include "base/chromeos/chromeos_version.h" 37#include "chromeos/display/output_configurator.h" 38#endif 39 40#if defined(OS_WIN) 41#include "base/win/windows_version.h" 42#endif 43 44namespace ash { 45namespace internal { 46typedef std::vector<gfx::Display> DisplayList; 47typedef std::vector<DisplayInfo> DisplayInfoList; 48 49namespace { 50 51// The number of pixels to overlap between the primary and secondary displays, 52// in case that the offset value is too large. 53const int kMinimumOverlapForInvalidOffset = 100; 54 55// List of value UI Scale values. Scales for 2x are equivalent to 640, 56// 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on 57// 2560 pixel width 2x density display. Please see crbug.com/233375 58// for the full list of resolutions. 59const float kUIScalesFor2x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f}; 60const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f }; 61const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f }; 62 63struct DisplaySortFunctor { 64 bool operator()(const gfx::Display& a, const gfx::Display& b) { 65 return a.id() < b.id(); 66 } 67}; 68 69struct DisplayInfoSortFunctor { 70 bool operator()(const DisplayInfo& a, const DisplayInfo& b) { 71 return a.id() < b.id(); 72 } 73}; 74 75struct ScaleComparator { 76 ScaleComparator(float s) : scale(s) {} 77 78 bool operator()(float s) const { 79 const float kEpsilon = 0.0001f; 80 return std::abs(scale - s) < kEpsilon; 81 } 82 float scale; 83}; 84 85gfx::Display& GetInvalidDisplay() { 86 static gfx::Display* invalid_display = new gfx::Display(); 87 return *invalid_display; 88} 89 90// Scoped objects used to either create or close the mirror window 91// at specific timing. 92class MirrorWindowCreator { 93 public: 94 MirrorWindowCreator(DisplayManager::Delegate* delegate, 95 const DisplayInfo& display_info) 96 : delegate_(delegate), 97 display_info_(display_info) { 98 } 99 100 virtual ~MirrorWindowCreator() { 101 if (delegate_) 102 delegate_->CreateOrUpdateMirrorWindow(display_info_); 103 } 104 105 private: 106 DisplayManager::Delegate* delegate_; 107 const DisplayInfo display_info_; 108 DISALLOW_COPY_AND_ASSIGN(MirrorWindowCreator); 109}; 110 111class MirrorWindowCloser { 112 public: 113 explicit MirrorWindowCloser(DisplayManager::Delegate* delegate) 114 : delegate_(delegate) {} 115 116 virtual ~MirrorWindowCloser() { 117 if (delegate_) 118 delegate_->CloseMirrorWindow(); 119 } 120 121 private: 122 DisplayManager::Delegate* delegate_; 123 124 DISALLOW_COPY_AND_ASSIGN(MirrorWindowCloser); 125}; 126 127} // namespace 128 129using std::string; 130using std::vector; 131 132DisplayManager::DisplayManager() 133 : delegate_(NULL), 134 layout_store_(new DisplayLayoutStore), 135 first_display_id_(gfx::Display::kInvalidDisplayID), 136 num_connected_displays_(0), 137 force_bounds_changed_(false), 138 change_display_upon_host_resize_(false), 139 software_mirroring_enabled_(false) { 140#if defined(OS_CHROMEOS) 141 change_display_upon_host_resize_ = !base::chromeos::IsRunningOnChromeOS(); 142#endif 143} 144 145DisplayManager::~DisplayManager() { 146} 147 148// static 149std::vector<float> DisplayManager::GetScalesForDisplay( 150 const DisplayInfo& info) { 151 std::vector<float> ret; 152 if (info.device_scale_factor() == 2.0f) { 153 ret.assign(kUIScalesFor2x, kUIScalesFor2x + arraysize(kUIScalesFor2x)); 154 return ret; 155 } 156 switch (info.bounds_in_pixel().width()) { 157 case 1280: 158 ret.assign(kUIScalesFor1280, 159 kUIScalesFor1280 + arraysize(kUIScalesFor1280)); 160 break; 161 case 1366: 162 ret.assign(kUIScalesFor1366, 163 kUIScalesFor1366 + arraysize(kUIScalesFor1366)); 164 break; 165 default: 166 ret.assign(kUIScalesFor1280, 167 kUIScalesFor1280 + arraysize(kUIScalesFor1280)); 168#if defined(OS_CHROMEOS) 169 if (base::chromeos::IsRunningOnChromeOS()) 170 NOTREACHED() << "Unknown resolution:" << info.ToString(); 171#endif 172 } 173 return ret; 174} 175 176// static 177float DisplayManager::GetNextUIScale(const DisplayInfo& info, bool up) { 178 float scale = info.ui_scale(); 179 std::vector<float> scales = GetScalesForDisplay(info); 180 for (size_t i = 0; i < scales.size(); ++i) { 181 if (ScaleComparator(scales[i])(scale)) { 182 if (up && i != scales.size() - 1) 183 return scales[i + 1]; 184 if (!up && i != 0) 185 return scales[i - 1]; 186 return scales[i]; 187 } 188 } 189 // Fallback to 1.0f if the |scale| wasn't in the list. 190 return 1.0f; 191} 192 193void DisplayManager::InitFromCommandLine() { 194 DisplayInfoList info_list; 195 196 const string size_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 197 switches::kAshHostWindowBounds); 198 vector<string> parts; 199 base::SplitString(size_str, ',', &parts); 200 for (vector<string>::const_iterator iter = parts.begin(); 201 iter != parts.end(); ++iter) { 202 info_list.push_back(DisplayInfo::CreateFromSpec(*iter)); 203 } 204 CommandLine* command_line = CommandLine::ForCurrentProcess(); 205 if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal)) 206 gfx::Display::SetInternalDisplayId(info_list[0].id()); 207 OnNativeDisplaysChanged(info_list); 208} 209 210void DisplayManager::UpdateDisplayBoundsForLayout( 211 const DisplayLayout& layout, 212 const gfx::Display& primary_display, 213 gfx::Display* secondary_display) { 214 DCHECK_EQ("0,0", primary_display.bounds().origin().ToString()); 215 216 const gfx::Rect& primary_bounds = primary_display.bounds(); 217 const gfx::Rect& secondary_bounds = secondary_display->bounds(); 218 gfx::Point new_secondary_origin = primary_bounds.origin(); 219 220 DisplayLayout::Position position = layout.position; 221 222 // Ignore the offset in case the secondary display doesn't share edges with 223 // the primary display. 224 int offset = layout.offset; 225 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) { 226 offset = std::min( 227 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); 228 offset = std::max( 229 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); 230 } else { 231 offset = std::min( 232 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); 233 offset = std::max( 234 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); 235 } 236 switch (position) { 237 case DisplayLayout::TOP: 238 new_secondary_origin.Offset(offset, -secondary_bounds.height()); 239 break; 240 case DisplayLayout::RIGHT: 241 new_secondary_origin.Offset(primary_bounds.width(), offset); 242 break; 243 case DisplayLayout::BOTTOM: 244 new_secondary_origin.Offset(offset, primary_bounds.height()); 245 break; 246 case DisplayLayout::LEFT: 247 new_secondary_origin.Offset(-secondary_bounds.width(), offset); 248 break; 249 } 250 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); 251 secondary_display->set_bounds( 252 gfx::Rect(new_secondary_origin, secondary_bounds.size())); 253 secondary_display->UpdateWorkAreaFromInsets(insets); 254} 255 256bool DisplayManager::IsActiveDisplay(const gfx::Display& display) const { 257 for (DisplayList::const_iterator iter = displays_.begin(); 258 iter != displays_.end(); ++iter) { 259 if ((*iter).id() == display.id()) 260 return true; 261 } 262 return false; 263} 264 265bool DisplayManager::HasInternalDisplay() const { 266 return gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID; 267} 268 269bool DisplayManager::IsInternalDisplayId(int64 id) const { 270 return gfx::Display::InternalDisplayId() == id; 271} 272 273const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const { 274 return const_cast<DisplayManager*>(this)->FindDisplayForId(id); 275} 276 277const gfx::Display& DisplayManager::FindDisplayContainingPoint( 278 const gfx::Point& point_in_screen) const { 279 for (DisplayList::const_iterator iter = displays_.begin(); 280 iter != displays_.end(); ++iter) { 281 const gfx::Display& display = *iter; 282 if (display.bounds().Contains(point_in_screen)) 283 return display; 284 } 285 return GetInvalidDisplay(); 286} 287 288void DisplayManager::SetOverscanInsets(int64 display_id, 289 const gfx::Insets& insets_in_dip) { 290 display_info_[display_id].SetOverscanInsets(insets_in_dip); 291 DisplayInfoList display_info_list; 292 for (DisplayList::const_iterator iter = displays_.begin(); 293 iter != displays_.end(); ++iter) { 294 display_info_list.push_back(GetDisplayInfo(iter->id())); 295 } 296 AddMirrorDisplayInfoIfAny(&display_info_list); 297 UpdateDisplays(display_info_list); 298} 299 300void DisplayManager::SetDisplayRotation(int64 display_id, 301 gfx::Display::Rotation rotation) { 302 if (!IsDisplayRotationEnabled()) 303 return; 304 DisplayInfoList display_info_list; 305 for (DisplayList::const_iterator iter = displays_.begin(); 306 iter != displays_.end(); ++iter) { 307 DisplayInfo info = GetDisplayInfo(iter->id()); 308 if (info.id() == display_id) { 309 if (info.rotation() == rotation) 310 return; 311 info.set_rotation(rotation); 312 } 313 display_info_list.push_back(info); 314 } 315 AddMirrorDisplayInfoIfAny(&display_info_list); 316 UpdateDisplays(display_info_list); 317} 318 319void DisplayManager::SetDisplayUIScale(int64 display_id, 320 float ui_scale) { 321 if (!IsDisplayUIScalingEnabled() || 322 gfx::Display::InternalDisplayId() != display_id) { 323 return; 324 } 325 326 DisplayInfoList display_info_list; 327 for (DisplayList::const_iterator iter = displays_.begin(); 328 iter != displays_.end(); ++iter) { 329 DisplayInfo info = GetDisplayInfo(iter->id()); 330 if (info.id() == display_id) { 331 if (info.ui_scale() == ui_scale) 332 return; 333 std::vector<float> scales = GetScalesForDisplay(info); 334 ScaleComparator comparator(ui_scale); 335 if (std::find_if(scales.begin(), scales.end(), comparator) == 336 scales.end()) { 337 return; 338 } 339 info.set_ui_scale(ui_scale); 340 } 341 display_info_list.push_back(info); 342 } 343 AddMirrorDisplayInfoIfAny(&display_info_list); 344 UpdateDisplays(display_info_list); 345} 346 347void DisplayManager::RegisterDisplayProperty( 348 int64 display_id, 349 gfx::Display::Rotation rotation, 350 float ui_scale, 351 const gfx::Insets* overscan_insets) { 352 if (display_info_.find(display_id) == display_info_.end()) { 353 display_info_[display_id] = 354 DisplayInfo(display_id, std::string(""), false); 355 } 356 357 display_info_[display_id].set_rotation(rotation); 358 // Just in case the preference file was corrupted. 359 if (0.5f <= ui_scale && ui_scale <= 2.0f) 360 display_info_[display_id].set_ui_scale(ui_scale); 361 if (overscan_insets) 362 display_info_[display_id].SetOverscanInsets(*overscan_insets); 363} 364 365bool DisplayManager::IsDisplayRotationEnabled() const { 366 static bool enabled = !CommandLine::ForCurrentProcess()-> 367 HasSwitch(switches::kAshDisableDisplayRotation); 368 return enabled; 369} 370 371bool DisplayManager::IsDisplayUIScalingEnabled() const { 372 static bool enabled = !CommandLine::ForCurrentProcess()-> 373 HasSwitch(switches::kAshDisableUIScaling); 374 if (!enabled) 375 return false; 376 return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID; 377} 378 379gfx::Insets DisplayManager::GetOverscanInsets(int64 display_id) const { 380 std::map<int64, DisplayInfo>::const_iterator it = 381 display_info_.find(display_id); 382 return (it != display_info_.end()) ? 383 it->second.overscan_insets_in_dip() : gfx::Insets(); 384} 385 386void DisplayManager::OnNativeDisplaysChanged( 387 const std::vector<DisplayInfo>& updated_displays) { 388 if (updated_displays.empty()) { 389 // If the device is booted without display, or chrome is started 390 // without --ash-host-window-bounds on linux desktop, use the 391 // default display. 392 if (displays_.empty()) { 393 std::vector<DisplayInfo> init_displays; 394 init_displays.push_back(DisplayInfo::CreateFromSpec(std::string())); 395 OnNativeDisplaysChanged(init_displays); 396 } else { 397 // Otherwise don't update the displays when all displays are disconnected. 398 // This happens when: 399 // - the device is idle and powerd requested to turn off all displays. 400 // - the device is suspended. (kernel turns off all displays) 401 // - the internal display's brightness is set to 0 and no external 402 // display is connected. 403 // - the internal display's brightness is 0 and external display is 404 // disconnected. 405 // The display will be updated when one of displays is turned on, and the 406 // display list will be updated correctly. 407 } 408 return; 409 } 410 first_display_id_ = updated_displays[0].id(); 411 std::set<gfx::Point> origins; 412 413 if (updated_displays.size() == 1) { 414 VLOG(1) << "OnNativeDisplaysChanged(1):" << updated_displays[0].ToString(); 415 } else { 416 VLOG(1) << "OnNativeDisplaysChanged(" << updated_displays.size() 417 << ") [0]=" << updated_displays[0].ToString() 418 << ", [1]=" << updated_displays[1].ToString(); 419 } 420 421 bool internal_display_connected = false; 422 num_connected_displays_ = updated_displays.size(); 423 mirrored_display_ = gfx::Display(); 424 DisplayInfoList new_display_info_list; 425 for (DisplayInfoList::const_iterator iter = updated_displays.begin(); 426 iter != updated_displays.end(); 427 ++iter) { 428 if (!internal_display_connected) 429 internal_display_connected = IsInternalDisplayId(iter->id()); 430 // Mirrored monitors have the same origins. 431 gfx::Point origin = iter->bounds_in_pixel().origin(); 432 if (origins.find(origin) != origins.end()) { 433 InsertAndUpdateDisplayInfo(*iter); 434 mirrored_display_ = CreateDisplayFromDisplayInfoById(iter->id()); 435 } else { 436 origins.insert(origin); 437 new_display_info_list.push_back(*iter); 438 } 439 } 440 if (HasInternalDisplay() && 441 !internal_display_connected && 442 display_info_.find(gfx::Display::InternalDisplayId()) == 443 display_info_.end()) { 444 DisplayInfo internal_display_info( 445 gfx::Display::InternalDisplayId(), 446 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME), 447 false /*Internal display must not have overscan */); 448 internal_display_info.SetBounds(gfx::Rect(0, 0, 800, 600)); 449 display_info_[gfx::Display::InternalDisplayId()] = internal_display_info; 450 } 451 UpdateDisplays(new_display_info_list); 452} 453 454void DisplayManager::UpdateDisplays() { 455 DisplayInfoList display_info_list; 456 for (DisplayList::const_iterator iter = displays_.begin(); 457 iter != displays_.end(); ++iter) { 458 display_info_list.push_back(GetDisplayInfo(iter->id())); 459 } 460 AddMirrorDisplayInfoIfAny(&display_info_list); 461 UpdateDisplays(display_info_list); 462} 463 464void DisplayManager::UpdateDisplays( 465 const std::vector<DisplayInfo>& updated_display_info_list) { 466#if defined(OS_WIN) 467 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 468 DCHECK_EQ(1u, updated_display_info_list.size()) << 469 "Multiple display test does not work on Win8 bots. Please " 470 "skip (don't disable) the test using SupportsMultipleDisplays()"; 471 } 472#endif 473 474 DisplayInfoList new_display_info_list = updated_display_info_list; 475 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor()); 476 std::sort(new_display_info_list.begin(), 477 new_display_info_list.end(), 478 DisplayInfoSortFunctor()); 479 DisplayList removed_displays; 480 std::vector<size_t> changed_display_indices; 481 std::vector<size_t> added_display_indices; 482 483 DisplayList::iterator curr_iter = displays_.begin(); 484 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin(); 485 486 DisplayList new_displays; 487 488 scoped_ptr<MirrorWindowCreator> mirror_window_creater; 489 490 // Use the internal display or 1st as the mirror source, then scale 491 // the root window so that it matches the external display's 492 // resolution. This is necessary in order for scaling to work while 493 // mirrored. 494 int64 mirrored_display_id = gfx::Display::kInvalidDisplayID; 495 if (software_mirroring_enabled_ && new_display_info_list.size() == 2) 496 mirrored_display_id = new_display_info_list[1].id(); 497 498 while (curr_iter != displays_.end() || 499 new_info_iter != new_display_info_list.end()) { 500 if (new_info_iter != new_display_info_list.end() && 501 mirrored_display_id == new_info_iter->id()) { 502 DisplayInfo info = *new_info_iter; 503 info.SetOverscanInsets(gfx::Insets()); 504 InsertAndUpdateDisplayInfo(info); 505 506 mirrored_display_ = CreateDisplayFromDisplayInfoById(new_info_iter->id()); 507 mirror_window_creater.reset(new MirrorWindowCreator( 508 delegate_, display_info_[new_info_iter->id()])); 509 ++new_info_iter; 510 // Remove existing external dispaly if it is going to be mirrored. 511 if (curr_iter != displays_.end() && 512 curr_iter->id() == mirrored_display_id) { 513 removed_displays.push_back(*curr_iter); 514 ++curr_iter; 515 } 516 continue; 517 } 518 519 if (curr_iter == displays_.end()) { 520 // more displays in new list. 521 added_display_indices.push_back(new_displays.size()); 522 InsertAndUpdateDisplayInfo(*new_info_iter); 523 new_displays.push_back( 524 CreateDisplayFromDisplayInfoById(new_info_iter->id())); 525 ++new_info_iter; 526 } else if (new_info_iter == new_display_info_list.end()) { 527 // more displays in current list. 528 removed_displays.push_back(*curr_iter); 529 ++curr_iter; 530 } else if (curr_iter->id() == new_info_iter->id()) { 531 const gfx::Display& current_display = *curr_iter; 532 // Copy the info because |CreateDisplayFromInfo| updates the instance. 533 const DisplayInfo current_display_info = 534 GetDisplayInfo(current_display.id()); 535 InsertAndUpdateDisplayInfo(*new_info_iter); 536 gfx::Display new_display = 537 CreateDisplayFromDisplayInfoById(new_info_iter->id()); 538 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id()); 539 540 bool host_window_bounds_changed = 541 current_display_info.bounds_in_pixel() != 542 new_display_info.bounds_in_pixel(); 543 544 if (force_bounds_changed_ || 545 host_window_bounds_changed || 546 (current_display.device_scale_factor() != 547 new_display.device_scale_factor()) || 548 (current_display_info.size_in_pixel() != 549 new_display.GetSizeInPixel()) || 550 (current_display.rotation() != new_display.rotation())) { 551 552 changed_display_indices.push_back(new_displays.size()); 553 } 554 555 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets()); 556 new_displays.push_back(new_display); 557 ++curr_iter; 558 ++new_info_iter; 559 } else if (curr_iter->id() < new_info_iter->id()) { 560 // more displays in current list between ids, which means it is deleted. 561 removed_displays.push_back(*curr_iter); 562 ++curr_iter; 563 } else { 564 // more displays in new list between ids, which means it is added. 565 added_display_indices.push_back(new_displays.size()); 566 InsertAndUpdateDisplayInfo(*new_info_iter); 567 new_displays.push_back( 568 CreateDisplayFromDisplayInfoById(new_info_iter->id())); 569 ++new_info_iter; 570 } 571 } 572 573 scoped_ptr<MirrorWindowCloser> mirror_window_closer; 574 // Try to close mirror window unless mirror window is necessary. 575 if (!mirror_window_creater.get()) 576 mirror_window_closer.reset(new MirrorWindowCloser(delegate_)); 577 578 // Do not update |displays_| if there's nothing to be updated. Without this, 579 // it will not update the display layout, which causes the bug 580 // http://crbug.com/155948. 581 if (changed_display_indices.empty() && added_display_indices.empty() && 582 removed_displays.empty()) { 583 return; 584 } 585 if (delegate_) 586 delegate_->PreDisplayConfigurationChange(); 587 588 size_t updated_index; 589 if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) && 590 std::find(added_display_indices.begin(), 591 added_display_indices.end(), 592 updated_index) == added_display_indices.end() && 593 std::find(changed_display_indices.begin(), 594 changed_display_indices.end(), 595 updated_index) == changed_display_indices.end()) { 596 changed_display_indices.push_back(updated_index); 597 } 598 599 displays_ = new_displays; 600 601 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false); 602 603 // Temporarily add displays to be removed because display object 604 // being removed are accessed during shutting down the root. 605 displays_.insert(displays_.end(), removed_displays.begin(), 606 removed_displays.end()); 607 608 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin(); 609 iter != removed_displays.rend(); ++iter) { 610 Shell::GetInstance()->screen()->NotifyDisplayRemoved(displays_.back()); 611 displays_.pop_back(); 612 } 613 // Close the mirror window here to avoid creating two compositor on 614 // one display. 615 mirror_window_closer.reset(); 616 for (std::vector<size_t>::iterator iter = added_display_indices.begin(); 617 iter != added_display_indices.end(); ++iter) { 618 Shell::GetInstance()->screen()->NotifyDisplayAdded(displays_[*iter]); 619 } 620 // Create the mirror window after all displays are added so that 621 // it can mirror the display newly added. This can happen when switching 622 // from dock mode to software mirror mode. 623 mirror_window_creater.reset(); 624 for (std::vector<size_t>::iterator iter = changed_display_indices.begin(); 625 iter != changed_display_indices.end(); ++iter) { 626 Shell::GetInstance()->screen()->NotifyBoundsChanged(displays_[*iter]); 627 } 628 if (delegate_) 629 delegate_->PostDisplayConfigurationChange(); 630 631#if defined(USE_X11) && defined(OS_CHROMEOS) 632 if (!changed_display_indices.empty() && base::chromeos::IsRunningOnChromeOS()) 633 ui::ClearX11DefaultRootWindow(); 634#endif 635} 636 637gfx::Display* DisplayManager::GetDisplayAt(size_t index) { 638 return index < displays_.size() ? &displays_[index] : NULL; 639} 640 641const gfx::Display* DisplayManager::GetPrimaryDisplayCandidate() const { 642 const gfx::Display* primary_candidate = &displays_[0]; 643#if defined(OS_CHROMEOS) 644 if (base::chromeos::IsRunningOnChromeOS()) { 645 // On ChromeOS device, root windows are stacked vertically, and 646 // default primary is the one on top. 647 int count = GetNumDisplays(); 648 int y = GetDisplayInfo(primary_candidate->id()).bounds_in_pixel().y(); 649 for (int i = 1; i < count; ++i) { 650 const gfx::Display* display = &displays_[i]; 651 const DisplayInfo& display_info = GetDisplayInfo(display->id()); 652 if (display->IsInternal()) { 653 primary_candidate = display; 654 break; 655 } else if (display_info.bounds_in_pixel().y() < y) { 656 primary_candidate = display; 657 y = display_info.bounds_in_pixel().y(); 658 } 659 } 660 } 661#endif 662 return primary_candidate; 663} 664 665size_t DisplayManager::GetNumDisplays() const { 666 return displays_.size(); 667} 668 669bool DisplayManager::IsMirrored() const { 670 return mirrored_display_.id() != gfx::Display::kInvalidDisplayID; 671} 672 673const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const { 674 std::map<int64, DisplayInfo>::const_iterator iter = 675 display_info_.find(display_id); 676 CHECK(iter != display_info_.end()) << display_id; 677 return iter->second; 678} 679 680std::string DisplayManager::GetDisplayNameForId(int64 id) { 681 if (id == gfx::Display::kInvalidDisplayID) 682 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); 683 684 std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id); 685 if (iter != display_info_.end() && !iter->second.name().empty()) 686 return iter->second.name(); 687 688 return base::StringPrintf("Display %d", static_cast<int>(id)); 689} 690 691int64 DisplayManager::GetDisplayIdForUIScaling() const { 692 // UI Scaling is effective only on internal display. 693 int64 display_id = gfx::Display::InternalDisplayId(); 694#if defined(OS_WIN) 695 display_id = first_display_id(); 696#endif 697 return display_id; 698} 699 700void DisplayManager::SetMirrorMode(bool mirrored) { 701 if (num_connected_displays() <= 1) 702 return; 703 704#if defined(OS_CHROMEOS) 705 if (base::chromeos::IsRunningOnChromeOS()) { 706 chromeos::OutputState new_state = mirrored ? 707 chromeos::STATE_DUAL_MIRROR : chromeos::STATE_DUAL_EXTENDED; 708 Shell::GetInstance()->output_configurator()->SetDisplayMode(new_state); 709 return; 710 } 711#endif 712 SetSoftwareMirroring(mirrored); 713 DisplayInfoList display_info_list; 714 int count = 0; 715 for (std::map<int64, DisplayInfo>::const_iterator iter = 716 display_info_.begin(); 717 count < 2; ++iter, ++count) { 718 display_info_list.push_back(GetDisplayInfo(iter->second.id())); 719 } 720 UpdateDisplays(display_info_list); 721#if defined(OS_CHROMEOS) 722 if (Shell::GetInstance()->output_configurator_animation()) { 723 Shell::GetInstance()->output_configurator_animation()-> 724 StartFadeInAnimation(); 725 } 726#endif 727} 728 729void DisplayManager::AddRemoveDisplay() { 730 DCHECK(!displays_.empty()); 731 std::vector<DisplayInfo> new_display_info_list; 732 DisplayInfo first_display = GetDisplayInfo(displays_[0].id()); 733 new_display_info_list.push_back(first_display); 734 // Add if there is only one display connected. 735 if (num_connected_displays() == 1) { 736 // Layout the 2nd display below the primary as with the real device. 737 gfx::Rect host_bounds = first_display.bounds_in_pixel(); 738 new_display_info_list.push_back(DisplayInfo::CreateFromSpec( 739 base::StringPrintf( 740 "%d+%d-500x400", host_bounds.x(), host_bounds.bottom()))); 741 } 742 num_connected_displays_ = new_display_info_list.size(); 743 mirrored_display_ = gfx::Display(); 744 UpdateDisplays(new_display_info_list); 745} 746 747void DisplayManager::ToggleDisplayScaleFactor() { 748 DCHECK(!displays_.empty()); 749 std::vector<DisplayInfo> new_display_info_list; 750 for (DisplayList::const_iterator iter = displays_.begin(); 751 iter != displays_.end(); ++iter) { 752 DisplayInfo display_info = GetDisplayInfo(iter->id()); 753 display_info.set_device_scale_factor( 754 display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f); 755 new_display_info_list.push_back(display_info); 756 } 757 AddMirrorDisplayInfoIfAny(&new_display_info_list); 758 UpdateDisplays(new_display_info_list); 759} 760 761void DisplayManager::SetSoftwareMirroring(bool enabled) { 762 software_mirroring_enabled_ = enabled; 763 mirrored_display_ = gfx::Display(); 764} 765 766gfx::Display& DisplayManager::FindDisplayForId(int64 id) { 767 for (DisplayList::iterator iter = displays_.begin(); 768 iter != displays_.end(); ++iter) { 769 if ((*iter).id() == id) 770 return *iter; 771 } 772 DLOG(WARNING) << "Could not find display:" << id; 773 return GetInvalidDisplay(); 774} 775 776bool DisplayManager::UpdateDisplayBounds(int64 display_id, 777 const gfx::Rect& new_bounds) { 778 if (change_display_upon_host_resize_) { 779 display_info_[display_id].SetBounds(new_bounds); 780 // Don't notify observers if the mirrored window has changed. 781 if (software_mirroring_enabled_ && mirrored_display_.id() == display_id) 782 return false; 783 gfx::Display& display = FindDisplayForId(display_id); 784 display.SetSize(display_info_[display_id].size_in_pixel()); 785 Shell::GetInstance()->screen()->NotifyBoundsChanged(display); 786 return true; 787 } 788 return false; 789} 790 791void DisplayManager::AddMirrorDisplayInfoIfAny( 792 std::vector<DisplayInfo>* display_info_list) { 793 if (software_mirroring_enabled_ && mirrored_display_.is_valid()) 794 display_info_list->push_back(GetDisplayInfo(mirrored_display_.id())); 795} 796 797void DisplayManager::InsertAndUpdateDisplayInfo(const DisplayInfo& new_info) { 798 std::map<int64, DisplayInfo>::iterator info = 799 display_info_.find(new_info.id()); 800 if (info != display_info_.end()) 801 info->second.Copy(new_info); 802 else { 803 display_info_[new_info.id()] = new_info; 804 display_info_[new_info.id()].set_native(false); 805 } 806 display_info_[new_info.id()].UpdateDisplaySize(); 807} 808 809gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) { 810 DCHECK(display_info_.find(id) != display_info_.end()); 811 const DisplayInfo& display_info = display_info_[id]; 812 813 gfx::Display new_display(display_info.id()); 814 gfx::Rect bounds_in_pixel(display_info.size_in_pixel()); 815 816 // Simply set the origin to (0,0). The primary display's origin is 817 // always (0,0) and the secondary display's bounds will be updated 818 // in |UpdateSecondaryDisplayBoundsForLayout| called in |UpdateDisplay|. 819 new_display.SetScaleAndBounds( 820 display_info.device_scale_factor(), gfx::Rect(bounds_in_pixel.size())); 821 new_display.set_rotation(display_info.rotation()); 822 return new_display; 823} 824 825bool DisplayManager::UpdateSecondaryDisplayBoundsForLayout( 826 DisplayList* displays, 827 size_t* updated_index) const { 828 if (displays->size() != 2U) 829 return false; 830 831 int64 id_at_zero = displays->at(0).id(); 832 DisplayIdPair pair = 833 (id_at_zero == first_display_id_ || 834 id_at_zero == gfx::Display::InternalDisplayId()) ? 835 std::make_pair(id_at_zero, displays->at(1).id()) : 836 std::make_pair(displays->at(1).id(), id_at_zero) ; 837 DisplayLayout layout = 838 layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair); 839 840 // Ignore if a user has a old format (should be extremely rare) 841 // and this will be replaced with DCHECK. 842 if (layout.primary_id != gfx::Display::kInvalidDisplayID) { 843 size_t primary_index, secondary_index; 844 if (displays->at(0).id() == layout.primary_id) { 845 primary_index = 0; 846 secondary_index = 1; 847 } else { 848 primary_index = 1; 849 secondary_index = 0; 850 } 851 gfx::Rect bounds = 852 GetDisplayForId(displays->at(secondary_index).id()).bounds(); 853 UpdateDisplayBoundsForLayout( 854 layout, displays->at(primary_index), &displays->at(secondary_index)); 855 *updated_index = secondary_index; 856 return bounds != displays->at(secondary_index).bounds(); 857 } 858 return false; 859} 860 861} // namespace internal 862} // namespace ash 863