Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / display / display_manager.cc
blob6c0fca1f2e73592eec25a36be1979e1892837182
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/display/display_manager.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "ash/ash_switches.h"
15 #include "ash/display/display_layout_store.h"
16 #include "ash/display/display_util.h"
17 #include "ash/display/extended_mouse_warp_controller.h"
18 #include "ash/display/null_mouse_warp_controller.h"
19 #include "ash/display/screen_ash.h"
20 #include "ash/display/unified_mouse_warp_controller.h"
21 #include "ash/screen_util.h"
22 #include "ash/shell.h"
23 #include "base/auto_reset.h"
24 #include "base/command_line.h"
25 #include "base/logging.h"
26 #include "base/metrics/histogram.h"
27 #include "base/run_loop.h"
28 #include "base/strings/string_number_conversions.h"
29 #include "base/strings/string_split.h"
30 #include "base/strings/stringprintf.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "grit/ash_strings.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/gfx/display.h"
35 #include "ui/gfx/display_observer.h"
36 #include "ui/gfx/font_render_params.h"
37 #include "ui/gfx/geometry/rect.h"
38 #include "ui/gfx/geometry/size_conversions.h"
39 #include "ui/gfx/screen.h"
41 #if defined(USE_X11)
42 #include "ui/base/x/x11_util.h"
43 #endif
45 #if defined(OS_CHROMEOS)
46 #include "ash/display/display_configurator_animation.h"
47 #include "base/sys_info.h"
48 #endif
50 #if defined(OS_WIN)
51 #include "base/win/windows_version.h"
52 #endif
54 namespace ash {
55 typedef std::vector<gfx::Display> DisplayList;
56 typedef std::vector<DisplayInfo> DisplayInfoList;
58 namespace {
60 // We need to keep this in order for unittests to tell if
61 // the object in gfx::Screen::GetScreenByType is for shutdown.
62 gfx::Screen* screen_for_shutdown = NULL;
64 // The number of pixels to overlap between the primary and secondary displays,
65 // in case that the offset value is too large.
66 const int kMinimumOverlapForInvalidOffset = 100;
68 struct DisplaySortFunctor {
69 bool operator()(const gfx::Display& a, const gfx::Display& b) {
70 return CompareDisplayIds(a.id(), b.id());
74 struct DisplayInfoSortFunctor {
75 bool operator()(const DisplayInfo& a, const DisplayInfo& b) {
76 return CompareDisplayIds(a.id(), b.id());
80 gfx::Display& GetInvalidDisplay() {
81 static gfx::Display* invalid_display = new gfx::Display();
82 return *invalid_display;
85 std::vector<DisplayMode>::const_iterator FindDisplayMode(
86 const DisplayInfo& info,
87 const DisplayMode& target_mode) {
88 const std::vector<DisplayMode>& modes = info.display_modes();
89 return std::find_if(modes.begin(), modes.end(),
90 [target_mode](const DisplayMode& mode) {
91 return target_mode.IsEquivalent(mode);
92 });
95 void SetInternalDisplayModeList(DisplayInfo* info) {
96 DisplayMode native_mode;
97 native_mode.size = info->bounds_in_native().size();
98 native_mode.device_scale_factor = info->device_scale_factor();
99 native_mode.ui_scale = 1.0f;
100 info->SetDisplayModes(CreateInternalDisplayModeList(native_mode));
103 void MaybeInitInternalDisplay(DisplayInfo* info) {
104 int64 id = info->id();
105 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
106 if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal)) {
107 gfx::Display::SetInternalDisplayId(id);
108 SetInternalDisplayModeList(info);
112 gfx::Size GetMaxNativeSize(const DisplayInfo& info) {
113 gfx::Size size;
114 for (auto& mode : info.display_modes()) {
115 if (mode.size.GetArea() > size.GetArea())
116 size = mode.size;
118 return size;
121 } // namespace
123 using std::string;
124 using std::vector;
126 // static
127 int64 DisplayManager::kUnifiedDisplayId = -10;
129 DisplayManager::DisplayManager()
130 : delegate_(NULL),
131 screen_(new ScreenAsh),
132 layout_store_(new DisplayLayoutStore),
133 first_display_id_(gfx::Display::kInvalidDisplayID),
134 num_connected_displays_(0),
135 force_bounds_changed_(false),
136 change_display_upon_host_resize_(false),
137 multi_display_mode_(EXTENDED),
138 current_default_multi_display_mode_(EXTENDED),
139 mirroring_display_id_(gfx::Display::kInvalidDisplayID),
140 registered_internal_display_rotation_lock_(false),
141 registered_internal_display_rotation_(gfx::Display::ROTATE_0),
142 unified_desktop_enabled_(false),
143 weak_ptr_factory_(this) {
144 #if defined(OS_CHROMEOS)
145 change_display_upon_host_resize_ = !base::SysInfo::IsRunningOnChromeOS();
146 unified_desktop_enabled_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
147 switches::kAshEnableUnifiedDesktop);
148 #endif
149 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_.get());
150 gfx::Screen* current_native =
151 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
152 // If there is no native, or the native was for shutdown,
153 // use ash's screen.
154 if (!current_native ||
155 current_native == screen_for_shutdown) {
156 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
160 DisplayManager::~DisplayManager() {
161 #if defined(OS_CHROMEOS)
162 // Reset the font params.
163 gfx::SetFontRenderParamsDeviceScaleFactor(1.0f);
164 #endif
167 bool DisplayManager::InitFromCommandLine() {
168 DisplayInfoList info_list;
169 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
170 if (!command_line->HasSwitch(switches::kAshHostWindowBounds))
171 return false;
172 const string size_str =
173 command_line->GetSwitchValueASCII(switches::kAshHostWindowBounds);
174 for (const std::string& part : base::SplitString(
175 size_str, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
176 info_list.push_back(DisplayInfo::CreateFromSpec(part));
177 info_list.back().set_native(true);
179 MaybeInitInternalDisplay(&info_list[0]);
180 if (info_list.size() > 1 &&
181 command_line->HasSwitch(switches::kAshEnableSoftwareMirroring)) {
182 SetMultiDisplayMode(MIRRORING);
184 OnNativeDisplaysChanged(info_list);
185 return true;
188 void DisplayManager::InitDefaultDisplay() {
189 DisplayInfoList info_list;
190 info_list.push_back(DisplayInfo::CreateFromSpec(std::string()));
191 info_list.back().set_native(true);
192 MaybeInitInternalDisplay(&info_list[0]);
193 OnNativeDisplaysChanged(info_list);
196 void DisplayManager::RefreshFontParams() {
197 #if defined(OS_CHROMEOS)
198 // Use the largest device scale factor among currently active displays. Non
199 // internal display may have bigger scale factor in case the external display
200 // is an 4K display.
201 float largest_device_scale_factor = 1.0f;
202 for (const gfx::Display& display : active_display_list_) {
203 const ash::DisplayInfo& info = display_info_[display.id()];
204 largest_device_scale_factor = std::max(
205 largest_device_scale_factor, info.GetEffectiveDeviceScaleFactor());
207 gfx::SetFontRenderParamsDeviceScaleFactor(largest_device_scale_factor);
208 #endif // OS_CHROMEOS
211 DisplayLayout DisplayManager::GetCurrentDisplayLayout() {
212 DCHECK_LE(2U, num_connected_displays());
213 // Invert if the primary was swapped.
214 if (num_connected_displays() == 2) {
215 DisplayIdPair pair = GetCurrentDisplayIdPair();
216 return layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
217 } else if (num_connected_displays() > 2) {
218 // Return fixed horizontal layout for >= 3 displays.
219 DisplayLayout layout(DisplayLayout::RIGHT, 0);
220 return layout;
222 NOTREACHED() << "DisplayLayout is requested for single display";
223 // On release build, just fallback to default instead of blowing up.
224 DisplayLayout layout =
225 layout_store_->default_display_layout();
226 layout.primary_id = active_display_list_[0].id();
227 return layout;
230 DisplayIdPair DisplayManager::GetCurrentDisplayIdPair() const {
231 if (IsInUnifiedMode()) {
232 return CreateDisplayIdPair(software_mirroring_display_list_[0].id(),
233 software_mirroring_display_list_[1].id());
234 } else if (IsInMirrorMode()) {
235 if (software_mirroring_enabled()) {
236 CHECK_EQ(2u, num_connected_displays());
237 // This comment is to make it easy to distinguish the crash
238 // between two checks.
239 CHECK_EQ(1u, active_display_list_.size());
241 return CreateDisplayIdPair(active_display_list_[0].id(),
242 mirroring_display_id_);
243 } else {
244 CHECK_LE(2u, active_display_list_.size());
245 return CreateDisplayIdPair(active_display_list_[0].id(),
246 active_display_list_[1].id());
250 void DisplayManager::SetLayoutForCurrentDisplays(
251 const DisplayLayout& layout_relative_to_primary) {
252 if (GetNumDisplays() != 2)
253 return;
254 const gfx::Display& primary = screen_->GetPrimaryDisplay();
255 const DisplayIdPair pair = GetCurrentDisplayIdPair();
256 // Invert if the primary was swapped.
257 DisplayLayout to_set = pair.first == primary.id() ?
258 layout_relative_to_primary : layout_relative_to_primary.Invert();
260 DisplayLayout current_layout =
261 layout_store_->GetRegisteredDisplayLayout(pair);
262 if (to_set.position != current_layout.position ||
263 to_set.offset != current_layout.offset) {
264 to_set.primary_id = primary.id();
265 layout_store_->RegisterLayoutForDisplayIdPair(
266 pair.first, pair.second, to_set);
267 if (delegate_)
268 delegate_->PreDisplayConfigurationChange(false);
269 // PreDisplayConfigurationChange(false);
270 // TODO(oshima): Call UpdateDisplays instead.
271 const DisplayLayout layout = GetCurrentDisplayLayout();
272 UpdateDisplayBoundsForLayout(
273 layout, primary,
274 FindDisplayForId(ScreenUtil::GetSecondaryDisplay().id()));
276 // Primary's bounds stay the same. Just notify bounds change
277 // on the secondary.
278 screen_->NotifyMetricsChanged(
279 ScreenUtil::GetSecondaryDisplay(),
280 gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
281 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
282 if (delegate_)
283 delegate_->PostDisplayConfigurationChange();
287 const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const {
288 gfx::Display* display =
289 const_cast<DisplayManager*>(this)->FindDisplayForId(id);
290 return display ? *display : GetInvalidDisplay();
293 const gfx::Display& DisplayManager::FindDisplayContainingPoint(
294 const gfx::Point& point_in_screen) const {
295 int index =
296 FindDisplayIndexContainingPoint(active_display_list_, point_in_screen);
297 return index < 0 ? GetInvalidDisplay() : active_display_list_[index];
300 bool DisplayManager::UpdateWorkAreaOfDisplay(int64 display_id,
301 const gfx::Insets& insets) {
302 gfx::Display* display = FindDisplayForId(display_id);
303 DCHECK(display);
304 gfx::Rect old_work_area = display->work_area();
305 display->UpdateWorkAreaFromInsets(insets);
306 return old_work_area != display->work_area();
309 void DisplayManager::SetOverscanInsets(int64 display_id,
310 const gfx::Insets& insets_in_dip) {
311 bool update = false;
312 DisplayInfoList display_info_list;
313 for (const auto& display : active_display_list_) {
314 DisplayInfo info = GetDisplayInfo(display.id());
315 if (info.id() == display_id) {
316 if (insets_in_dip.empty()) {
317 info.set_clear_overscan_insets(true);
318 } else {
319 info.set_clear_overscan_insets(false);
320 info.SetOverscanInsets(insets_in_dip);
322 update = true;
324 display_info_list.push_back(info);
326 if (update) {
327 AddMirrorDisplayInfoIfAny(&display_info_list);
328 UpdateDisplays(display_info_list);
329 } else {
330 display_info_[display_id].SetOverscanInsets(insets_in_dip);
334 void DisplayManager::SetDisplayRotation(int64 display_id,
335 gfx::Display::Rotation rotation,
336 gfx::Display::RotationSource source) {
337 if (IsInUnifiedMode())
338 return;
340 DisplayInfoList display_info_list;
341 bool is_active = false;
342 for (const auto& display : active_display_list_) {
343 DisplayInfo info = GetDisplayInfo(display.id());
344 if (info.id() == display_id) {
345 if (info.GetRotation(source) == rotation &&
346 info.GetActiveRotation() == rotation) {
347 return;
349 info.SetRotation(rotation, source);
350 is_active = true;
352 display_info_list.push_back(info);
354 if (is_active) {
355 AddMirrorDisplayInfoIfAny(&display_info_list);
356 UpdateDisplays(display_info_list);
357 } else if (display_info_.find(display_id) != display_info_.end()) {
358 // Inactive displays can reactivate, ensure they have been updated.
359 display_info_[display_id].SetRotation(rotation, source);
363 bool DisplayManager::SetDisplayMode(int64 display_id,
364 const DisplayMode& display_mode) {
365 bool change_ui_scale = GetDisplayIdForUIScaling() == display_id;
367 DisplayInfoList display_info_list;
368 bool display_property_changed = false;
369 bool resolution_changed = false;
370 for (const auto& display : active_display_list_) {
371 DisplayInfo info = GetDisplayInfo(display.id());
372 if (info.id() == display_id) {
373 auto iter = FindDisplayMode(info, display_mode);
374 if (iter == info.display_modes().end()) {
375 LOG(WARNING) << "Unsupported display mode was requested:"
376 << "size=" << display_mode.size.ToString()
377 << ", ui scale=" << display_mode.ui_scale
378 << ", scale fator=" << display_mode.device_scale_factor;
379 return false;
382 if (change_ui_scale) {
383 if (info.configured_ui_scale() == display_mode.ui_scale)
384 return true;
385 info.set_configured_ui_scale(display_mode.ui_scale);
386 display_property_changed = true;
387 } else {
388 display_modes_[display_id] = *iter;
389 if (info.bounds_in_native().size() != display_mode.size)
390 resolution_changed = true;
391 if (info.device_scale_factor() != display_mode.device_scale_factor) {
392 info.set_device_scale_factor(display_mode.device_scale_factor);
393 display_property_changed = true;
397 display_info_list.push_back(info);
399 if (display_property_changed) {
400 AddMirrorDisplayInfoIfAny(&display_info_list);
401 UpdateDisplays(display_info_list);
403 if (resolution_changed && IsInUnifiedMode()) {
404 ReconfigureDisplays();
405 #if defined(OS_CHROMEOS)
406 } else if (resolution_changed && base::SysInfo::IsRunningOnChromeOS()) {
407 Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
408 #endif
410 return resolution_changed || display_property_changed;
413 void DisplayManager::RegisterDisplayProperty(
414 int64 display_id,
415 gfx::Display::Rotation rotation,
416 float ui_scale,
417 const gfx::Insets* overscan_insets,
418 const gfx::Size& resolution_in_pixels,
419 float device_scale_factor,
420 ui::ColorCalibrationProfile color_profile) {
421 if (display_info_.find(display_id) == display_info_.end())
422 display_info_[display_id] = DisplayInfo(display_id, std::string(), false);
424 // Do not allow rotation in unified desktop mode.
425 if (display_id == kUnifiedDisplayId)
426 rotation = gfx::Display::ROTATE_0;
428 display_info_[display_id].SetRotation(rotation,
429 gfx::Display::ROTATION_SOURCE_USER);
430 display_info_[display_id].SetRotation(rotation,
431 gfx::Display::ROTATION_SOURCE_ACTIVE);
432 display_info_[display_id].SetColorProfile(color_profile);
433 // Just in case the preference file was corrupted.
434 // TODO(mukai): register |display_modes_| here as well, so the lookup for the
435 // default mode in GetActiveModeForDisplayId() gets much simpler.
436 if (0.5f <= ui_scale && ui_scale <= 2.0f)
437 display_info_[display_id].set_configured_ui_scale(ui_scale);
438 if (overscan_insets)
439 display_info_[display_id].SetOverscanInsets(*overscan_insets);
440 if (!resolution_in_pixels.IsEmpty()) {
441 DCHECK(!gfx::Display::IsInternalDisplayId(display_id));
442 // Default refresh rate, until OnNativeDisplaysChanged() updates us with the
443 // actual display info, is 60 Hz.
444 DisplayMode mode(resolution_in_pixels, 60.0f, false, false);
445 mode.device_scale_factor = device_scale_factor;
446 display_modes_[display_id] = mode;
450 DisplayMode DisplayManager::GetActiveModeForDisplayId(int64 display_id) const {
451 DisplayMode selected_mode;
452 if (GetSelectedModeForDisplayId(display_id, &selected_mode))
453 return selected_mode;
455 // If 'selected' mode is empty, it should return the default mode. This means
456 // the native mode for the external display. Unfortunately this is not true
457 // for the internal display because restoring UI-scale doesn't register the
458 // restored mode to |display_mode_|, so it needs to look up the mode whose
459 // UI-scale value matches. See the TODO in RegisterDisplayProperty().
460 const DisplayInfo& info = GetDisplayInfo(display_id);
462 for (auto& mode : info.display_modes()) {
463 if (GetDisplayIdForUIScaling() == display_id) {
464 if (info.configured_ui_scale() == mode.ui_scale)
465 return mode;
466 } else if (mode.native) {
467 return mode;
470 return selected_mode;
473 void DisplayManager::RegisterDisplayRotationProperties(bool rotation_lock,
474 gfx::Display::Rotation rotation) {
475 if (delegate_)
476 delegate_->PreDisplayConfigurationChange(false);
477 registered_internal_display_rotation_lock_ = rotation_lock;
478 registered_internal_display_rotation_ = rotation;
479 if (delegate_)
480 delegate_->PostDisplayConfigurationChange();
483 bool DisplayManager::GetSelectedModeForDisplayId(int64 id,
484 DisplayMode* mode_out) const {
485 std::map<int64, DisplayMode>::const_iterator iter = display_modes_.find(id);
486 if (iter == display_modes_.end())
487 return false;
488 *mode_out = iter->second;
489 return true;
492 bool DisplayManager::IsDisplayUIScalingEnabled() const {
493 return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID;
496 gfx::Insets DisplayManager::GetOverscanInsets(int64 display_id) const {
497 std::map<int64, DisplayInfo>::const_iterator it =
498 display_info_.find(display_id);
499 return (it != display_info_.end()) ?
500 it->second.overscan_insets_in_dip() : gfx::Insets();
503 void DisplayManager::SetColorCalibrationProfile(
504 int64 display_id,
505 ui::ColorCalibrationProfile profile) {
506 #if defined(OS_CHROMEOS)
507 if (!display_info_[display_id].IsColorProfileAvailable(profile))
508 return;
510 if (delegate_)
511 delegate_->PreDisplayConfigurationChange(false);
512 // Just sets color profile if it's not running on ChromeOS (like tests).
513 if (!base::SysInfo::IsRunningOnChromeOS() ||
514 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
515 display_id, profile)) {
516 display_info_[display_id].SetColorProfile(profile);
517 UMA_HISTOGRAM_ENUMERATION(
518 "ChromeOS.Display.ColorProfile", profile, ui::NUM_COLOR_PROFILES);
520 if (delegate_)
521 delegate_->PostDisplayConfigurationChange();
522 #endif
525 void DisplayManager::OnNativeDisplaysChanged(
526 const std::vector<DisplayInfo>& updated_displays) {
527 if (updated_displays.empty()) {
528 VLOG(1) << "OnNativeDisplaysChanged(0): # of current displays="
529 << active_display_list_.size();
530 // If the device is booted without display, or chrome is started
531 // without --ash-host-window-bounds on linux desktop, use the
532 // default display.
533 if (active_display_list_.empty()) {
534 std::vector<DisplayInfo> init_displays;
535 init_displays.push_back(DisplayInfo::CreateFromSpec(std::string()));
536 MaybeInitInternalDisplay(&init_displays[0]);
537 OnNativeDisplaysChanged(init_displays);
538 } else {
539 // Otherwise don't update the displays when all displays are disconnected.
540 // This happens when:
541 // - the device is idle and powerd requested to turn off all displays.
542 // - the device is suspended. (kernel turns off all displays)
543 // - the internal display's brightness is set to 0 and no external
544 // display is connected.
545 // - the internal display's brightness is 0 and external display is
546 // disconnected.
547 // The display will be updated when one of displays is turned on, and the
548 // display list will be updated correctly.
550 return;
552 first_display_id_ = updated_displays[0].id();
553 std::set<gfx::Point> origins;
555 if (updated_displays.size() == 1) {
556 VLOG(1) << "OnNativeDisplaysChanged(1):" << updated_displays[0].ToString();
557 } else {
558 VLOG(1) << "OnNativeDisplaysChanged(" << updated_displays.size()
559 << ") [0]=" << updated_displays[0].ToString()
560 << ", [1]=" << updated_displays[1].ToString();
563 bool internal_display_connected = false;
564 num_connected_displays_ = updated_displays.size();
565 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
566 software_mirroring_display_list_.clear();
567 DisplayInfoList new_display_info_list;
568 for (DisplayInfoList::const_iterator iter = updated_displays.begin();
569 iter != updated_displays.end();
570 ++iter) {
571 if (!internal_display_connected)
572 internal_display_connected =
573 gfx::Display::IsInternalDisplayId(iter->id());
574 // Mirrored monitors have the same origins.
575 gfx::Point origin = iter->bounds_in_native().origin();
576 if (origins.find(origin) != origins.end()) {
577 InsertAndUpdateDisplayInfo(*iter);
578 mirroring_display_id_ = iter->id();
579 } else {
580 origins.insert(origin);
581 new_display_info_list.push_back(*iter);
584 DisplayMode new_mode;
585 new_mode.size = iter->bounds_in_native().size();
586 new_mode.device_scale_factor = iter->device_scale_factor();
587 new_mode.ui_scale = iter->configured_ui_scale();
588 const std::vector<DisplayMode>& display_modes = iter->display_modes();
589 // This is empty the displays are initialized from InitFromCommandLine.
590 if (!display_modes.size())
591 continue;
592 auto display_modes_iter = FindDisplayMode(*iter, new_mode);
593 // Update the actual resolution selected as the resolution request may fail.
594 if (display_modes_iter == display_modes.end())
595 display_modes_.erase(iter->id());
596 else if (display_modes_.find(iter->id()) != display_modes_.end())
597 display_modes_[iter->id()] = *display_modes_iter;
599 if (gfx::Display::HasInternalDisplay() && !internal_display_connected) {
600 if (display_info_.find(gfx::Display::InternalDisplayId()) ==
601 display_info_.end()) {
602 // Create a dummy internal display if the chrome restarted
603 // in docked mode.
604 DisplayInfo internal_display_info(
605 gfx::Display::InternalDisplayId(),
606 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME),
607 false /*Internal display must not have overscan */);
608 internal_display_info.SetBounds(gfx::Rect(0, 0, 800, 600));
609 display_info_[gfx::Display::InternalDisplayId()] = internal_display_info;
610 } else {
611 // Internal display is no longer active. Reset its rotation to user
612 // preference, so that it is restored when the internal display becomes
613 // active again.
614 gfx::Display::Rotation user_rotation =
615 display_info_[gfx::Display::InternalDisplayId()].GetRotation(
616 gfx::Display::ROTATION_SOURCE_USER);
617 display_info_[gfx::Display::InternalDisplayId()].SetRotation(
618 user_rotation, gfx::Display::ROTATION_SOURCE_USER);
622 #if defined(OS_CHROMEOS)
623 if (!base::SysInfo::IsRunningOnChromeOS() &&
624 new_display_info_list.size() > 1) {
625 DisplayIdPair pair = CreateDisplayIdPair(new_display_info_list[0].id(),
626 new_display_info_list[1].id());
627 DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(pair);
628 // Mirror mode is set by DisplayConfigurator on the device.
629 // Emulate it when running on linux desktop.
630 if (layout.mirrored)
631 SetMultiDisplayMode(MIRRORING);
633 #endif
635 UpdateDisplays(new_display_info_list);
638 void DisplayManager::UpdateDisplays() {
639 DisplayInfoList display_info_list;
640 for (const auto& display : active_display_list_)
641 display_info_list.push_back(GetDisplayInfo(display.id()));
642 AddMirrorDisplayInfoIfAny(&display_info_list);
643 UpdateDisplays(display_info_list);
646 void DisplayManager::UpdateDisplays(
647 const std::vector<DisplayInfo>& updated_display_info_list) {
648 #if defined(OS_WIN)
649 DCHECK_EQ(1u, updated_display_info_list.size()) <<
650 ": Multiple display test does not work on Windows bots. Please "
651 "skip (don't disable) the test using SupportsMultipleDisplays()";
652 #endif
654 DisplayInfoList new_display_info_list = updated_display_info_list;
655 std::sort(active_display_list_.begin(), active_display_list_.end(),
656 DisplaySortFunctor());
657 std::sort(new_display_info_list.begin(),
658 new_display_info_list.end(),
659 DisplayInfoSortFunctor());
661 if (new_display_info_list.size() > 1) {
662 DisplayIdPair pair = CreateDisplayIdPair(new_display_info_list[0].id(),
663 new_display_info_list[1].id());
664 DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(pair);
665 current_default_multi_display_mode_ =
666 (layout.default_unified && unified_desktop_enabled_) ? UNIFIED
667 : EXTENDED;
670 if (multi_display_mode_ != MIRRORING)
671 multi_display_mode_ = current_default_multi_display_mode_;
673 CreateSoftwareMirroringDisplayInfo(&new_display_info_list);
675 // Close the mirroring window if any here to avoid creating two compositor on
676 // one display.
677 if (delegate_)
678 delegate_->CloseMirroringDisplayIfNotNecessary();
680 DisplayList new_displays;
681 DisplayList removed_displays;
682 std::map<size_t, uint32_t> display_changes;
683 std::vector<size_t> added_display_indices;
685 DisplayList::iterator curr_iter = active_display_list_.begin();
686 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin();
688 while (curr_iter != active_display_list_.end() ||
689 new_info_iter != new_display_info_list.end()) {
690 if (curr_iter == active_display_list_.end()) {
691 // more displays in new list.
692 added_display_indices.push_back(new_displays.size());
693 InsertAndUpdateDisplayInfo(*new_info_iter);
694 new_displays.push_back(
695 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
696 ++new_info_iter;
697 } else if (new_info_iter == new_display_info_list.end()) {
698 // more displays in current list.
699 removed_displays.push_back(*curr_iter);
700 ++curr_iter;
701 } else if (curr_iter->id() == new_info_iter->id()) {
702 const gfx::Display& current_display = *curr_iter;
703 // Copy the info because |CreateDisplayFromInfo| updates the instance.
704 const DisplayInfo current_display_info =
705 GetDisplayInfo(current_display.id());
706 InsertAndUpdateDisplayInfo(*new_info_iter);
707 gfx::Display new_display =
708 CreateDisplayFromDisplayInfoById(new_info_iter->id());
709 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id());
711 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_NONE;
713 // At that point the new Display objects we have are not entirely updated,
714 // they are missing the translation related to the Display disposition in
715 // the layout.
716 // Using display.bounds() and display.work_area() would fail most of the
717 // time.
718 if (force_bounds_changed_ ||
719 (current_display_info.bounds_in_native() !=
720 new_display_info.bounds_in_native()) ||
721 (current_display_info.GetOverscanInsetsInPixel() !=
722 new_display_info.GetOverscanInsetsInPixel()) ||
723 current_display.size() != new_display.size()) {
724 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
725 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
728 if (current_display.device_scale_factor() !=
729 new_display.device_scale_factor()) {
730 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
733 if (current_display.rotation() != new_display.rotation())
734 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_ROTATION;
736 if (metrics != gfx::DisplayObserver::DISPLAY_METRIC_NONE) {
737 display_changes.insert(
738 std::pair<size_t, uint32_t>(new_displays.size(), metrics));
741 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
742 new_displays.push_back(new_display);
743 ++curr_iter;
744 ++new_info_iter;
745 } else if (curr_iter->id() < new_info_iter->id()) {
746 // more displays in current list between ids, which means it is deleted.
747 removed_displays.push_back(*curr_iter);
748 ++curr_iter;
749 } else {
750 // more displays in new list between ids, which means it is added.
751 added_display_indices.push_back(new_displays.size());
752 InsertAndUpdateDisplayInfo(*new_info_iter);
753 new_displays.push_back(
754 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
755 ++new_info_iter;
758 gfx::Display old_primary;
759 if (delegate_)
760 old_primary = screen_->GetPrimaryDisplay();
762 // Clear focus if the display has been removed, but don't clear focus if
763 // the destkop has been moved from one display to another
764 // (mirror -> docked, docked -> single internal).
765 bool clear_focus =
766 !removed_displays.empty() &&
767 !(removed_displays.size() == 1 && added_display_indices.size() == 1);
768 if (delegate_)
769 delegate_->PreDisplayConfigurationChange(clear_focus);
771 std::vector<size_t> updated_indices;
772 if (UpdateNonPrimaryDisplayBoundsForLayout(&new_displays, &updated_indices)) {
773 for (std::vector<size_t>::iterator it = updated_indices.begin();
774 it != updated_indices.end(); ++it) {
775 size_t updated_index = *it;
776 if (std::find(added_display_indices.begin(),
777 added_display_indices.end(),
778 updated_index) == added_display_indices.end()) {
779 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
780 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
781 if (display_changes.find(updated_index) != display_changes.end())
782 metrics |= display_changes[updated_index];
784 display_changes[updated_index] = metrics;
789 active_display_list_ = new_displays;
791 RefreshFontParams();
792 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false);
794 int active_display_list_size = active_display_list_.size();
795 // Temporarily add displays to be removed because display object
796 // being removed are accessed during shutting down the root.
797 active_display_list_.insert(active_display_list_.end(),
798 removed_displays.begin(), removed_displays.end());
800 for (const auto& display : removed_displays)
801 screen_->NotifyDisplayRemoved(display);
803 for (size_t index : added_display_indices)
804 screen_->NotifyDisplayAdded(active_display_list_[index]);
806 active_display_list_.resize(active_display_list_size);
808 bool notify_primary_change =
809 delegate_ ? old_primary.id() != screen_->GetPrimaryDisplay().id() : false;
811 for (std::map<size_t, uint32_t>::iterator iter = display_changes.begin();
812 iter != display_changes.end();
813 ++iter) {
814 uint32_t metrics = iter->second;
815 const gfx::Display& updated_display = active_display_list_[iter->first];
817 if (notify_primary_change &&
818 updated_display.id() == screen_->GetPrimaryDisplay().id()) {
819 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_PRIMARY;
820 notify_primary_change = false;
822 screen_->NotifyMetricsChanged(updated_display, metrics);
825 if (notify_primary_change) {
826 // This happens when a primary display has moved to anther display without
827 // bounds change.
828 const gfx::Display& primary = screen_->GetPrimaryDisplay();
829 if (primary.id() != old_primary.id()) {
830 uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_PRIMARY;
831 if (primary.size() != old_primary.size()) {
832 metrics |= (gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS |
833 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
835 if (primary.device_scale_factor() != old_primary.device_scale_factor())
836 metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
838 screen_->NotifyMetricsChanged(primary, metrics);
842 if (delegate_)
843 delegate_->PostDisplayConfigurationChange();
845 #if defined(USE_X11) && defined(OS_CHROMEOS)
846 if (!display_changes.empty() && base::SysInfo::IsRunningOnChromeOS())
847 ui::ClearX11DefaultRootWindow();
848 #endif
850 // Create the mirroring window asynchronously after all displays
851 // are added so that it can mirror the display newly added. This can
852 // happen when switching from dock mode to software mirror mode.
853 CreateMirrorWindowAsyncIfAny();
856 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const {
857 DCHECK_LT(index, active_display_list_.size());
858 return active_display_list_[index];
861 const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const {
862 if (GetNumDisplays() != 2)
863 return active_display_list_[0];
864 DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(
865 GetCurrentDisplayIdPair());
866 return GetDisplayForId(layout.primary_id);
869 size_t DisplayManager::GetNumDisplays() const {
870 return active_display_list_.size();
873 bool DisplayManager::IsInMirrorMode() const {
874 return mirroring_display_id_ != gfx::Display::kInvalidDisplayID;
877 void DisplayManager::SetUnifiedDesktopEnabled(bool enable) {
878 unified_desktop_enabled_ = enable;
879 ReconfigureDisplays();
882 bool DisplayManager::IsInUnifiedMode() const {
883 return multi_display_mode_ == UNIFIED &&
884 !software_mirroring_display_list_.empty();
887 const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const {
888 DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id);
890 std::map<int64, DisplayInfo>::const_iterator iter =
891 display_info_.find(display_id);
892 CHECK(iter != display_info_.end()) << display_id;
893 return iter->second;
896 const gfx::Display DisplayManager::GetMirroringDisplayById(
897 int64 display_id) const {
898 auto iter = std::find_if(software_mirroring_display_list_.begin(),
899 software_mirroring_display_list_.end(),
900 [display_id](const gfx::Display& display) {
901 return display.id() == display_id;
903 return iter == software_mirroring_display_list_.end() ? gfx::Display()
904 : *iter;
907 std::string DisplayManager::GetDisplayNameForId(int64 id) {
908 if (id == gfx::Display::kInvalidDisplayID)
909 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
911 std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id);
912 if (iter != display_info_.end() && !iter->second.name().empty())
913 return iter->second.name();
915 return base::StringPrintf("Display %d", static_cast<int>(id));
918 int64 DisplayManager::GetDisplayIdForUIScaling() const {
919 // UI Scaling is effective on internal display.
920 return gfx::Display::HasInternalDisplay() ? gfx::Display::InternalDisplayId()
921 : gfx::Display::kInvalidDisplayID;
924 void DisplayManager::SetMirrorMode(bool mirror) {
925 #if defined(OS_CHROMEOS)
926 if (num_connected_displays() <= 1)
927 return;
929 if (base::SysInfo::IsRunningOnChromeOS()) {
930 ui::MultipleDisplayState new_state =
931 mirror ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR
932 : ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
933 Shell::GetInstance()->display_configurator()->SetDisplayMode(new_state);
934 return;
936 multi_display_mode_ =
937 mirror ? MIRRORING : current_default_multi_display_mode_;
938 ReconfigureDisplays();
939 if (Shell::GetInstance()->display_configurator_animation()) {
940 Shell::GetInstance()->display_configurator_animation()->
941 StartFadeInAnimation();
943 RunPendingTasksForTest();
944 #endif
947 void DisplayManager::AddRemoveDisplay() {
948 DCHECK(!active_display_list_.empty());
949 std::vector<DisplayInfo> new_display_info_list;
950 const DisplayInfo& first_display =
951 IsInUnifiedMode()
952 ? GetDisplayInfo(software_mirroring_display_list_[0].id())
953 : GetDisplayInfo(active_display_list_[0].id());
954 new_display_info_list.push_back(first_display);
955 // Add if there is only one display connected.
956 if (num_connected_displays() == 1) {
957 const int kVerticalOffsetPx = 100;
958 // Layout the 2nd display below the primary as with the real device.
959 gfx::Rect host_bounds = first_display.bounds_in_native();
960 new_display_info_list.push_back(
961 DisplayInfo::CreateFromSpec(base::StringPrintf(
962 "%d+%d-600x%d", host_bounds.x(),
963 host_bounds.bottom() + kVerticalOffsetPx, host_bounds.height())));
965 num_connected_displays_ = new_display_info_list.size();
966 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
967 software_mirroring_display_list_.clear();
968 UpdateDisplays(new_display_info_list);
971 void DisplayManager::ToggleDisplayScaleFactor() {
972 DCHECK(!active_display_list_.empty());
973 std::vector<DisplayInfo> new_display_info_list;
974 for (DisplayList::const_iterator iter = active_display_list_.begin();
975 iter != active_display_list_.end(); ++iter) {
976 DisplayInfo display_info = GetDisplayInfo(iter->id());
977 display_info.set_device_scale_factor(
978 display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f);
979 new_display_info_list.push_back(display_info);
981 AddMirrorDisplayInfoIfAny(&new_display_info_list);
982 UpdateDisplays(new_display_info_list);
985 #if defined(OS_CHROMEOS)
986 void DisplayManager::SetSoftwareMirroring(bool enabled) {
987 SetMultiDisplayMode(enabled ? MIRRORING
988 : current_default_multi_display_mode_);
991 bool DisplayManager::SoftwareMirroringEnabled() const {
992 return software_mirroring_enabled();
994 #endif
996 void DisplayManager::SetDefaultMultiDisplayModeForCurrentDisplays(
997 MultiDisplayMode mode) {
998 DCHECK_NE(MIRRORING, mode);
999 DisplayIdPair pair = GetCurrentDisplayIdPair();
1000 layout_store_->UpdateMultiDisplayState(pair, IsInMirrorMode(),
1001 mode == UNIFIED);
1004 void DisplayManager::SetMultiDisplayMode(MultiDisplayMode mode) {
1005 multi_display_mode_ = mode;
1006 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
1007 software_mirroring_display_list_.clear();
1010 void DisplayManager::ReconfigureDisplays() {
1011 DisplayInfoList display_info_list;
1012 for (DisplayList::const_iterator iter = active_display_list_.begin();
1013 (display_info_list.size() < 2 && iter != active_display_list_.end());
1014 ++iter) {
1015 if (iter->id() == kUnifiedDisplayId)
1016 continue;
1017 display_info_list.push_back(GetDisplayInfo(iter->id()));
1019 for (auto iter = software_mirroring_display_list_.begin();
1020 (display_info_list.size() < 2 &&
1021 iter != software_mirroring_display_list_.end());
1022 ++iter) {
1023 display_info_list.push_back(GetDisplayInfo(iter->id()));
1025 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
1026 software_mirroring_display_list_.clear();
1027 UpdateDisplays(display_info_list);
1030 bool DisplayManager::UpdateDisplayBounds(int64 display_id,
1031 const gfx::Rect& new_bounds) {
1032 if (change_display_upon_host_resize_) {
1033 display_info_[display_id].SetBounds(new_bounds);
1034 // Don't notify observers if the mirrored window has changed.
1035 if (software_mirroring_enabled() && mirroring_display_id_ == display_id)
1036 return false;
1037 gfx::Display* display = FindDisplayForId(display_id);
1038 display->SetSize(display_info_[display_id].size_in_pixel());
1039 screen_->NotifyMetricsChanged(*display,
1040 gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1041 return true;
1043 return false;
1046 void DisplayManager::CreateMirrorWindowAsyncIfAny() {
1047 // Do not post a task if the software mirroring doesn't exist, or
1048 // during initialization when compositor's init task isn't posted yet.
1049 // ash::Shell::Init() will call this after the compositor is initialized.
1050 if (software_mirroring_display_list_.empty() || !delegate_)
1051 return;
1052 base::MessageLoopForUI::current()->PostTask(
1053 FROM_HERE,
1054 base::Bind(&DisplayManager::CreateMirrorWindowIfAny,
1055 weak_ptr_factory_.GetWeakPtr()));
1058 scoped_ptr<MouseWarpController> DisplayManager::CreateMouseWarpController(
1059 aura::Window* drag_source) const {
1060 if (IsInUnifiedMode() && num_connected_displays() >= 2)
1061 return make_scoped_ptr(new UnifiedMouseWarpController());
1062 // Extra check for |num_connected_displays()| is for SystemDisplayApiTest
1063 // that injects MockScreen.
1064 if (GetNumDisplays() < 2 || num_connected_displays() < 2)
1065 return make_scoped_ptr(new NullMouseWarpController());
1066 return make_scoped_ptr(new ExtendedMouseWarpController(drag_source));
1069 void DisplayManager::CreateScreenForShutdown() const {
1070 bool native_is_ash =
1071 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE) == screen_.get();
1072 delete screen_for_shutdown;
1073 screen_for_shutdown = screen_->CloneForShutdown();
1074 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
1075 screen_for_shutdown);
1076 if (native_is_ash) {
1077 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
1078 screen_for_shutdown);
1082 void DisplayManager::UpdateInternalDisplayModeListForTest() {
1083 if (!gfx::Display::HasInternalDisplay() ||
1084 display_info_.count(gfx::Display::InternalDisplayId()) == 0)
1085 return;
1086 DisplayInfo* info = &display_info_[gfx::Display::InternalDisplayId()];
1087 SetInternalDisplayModeList(info);
1090 void DisplayManager::CreateSoftwareMirroringDisplayInfo(
1091 DisplayInfoList* display_info_list) {
1092 // Use the internal display or 1st as the mirror source, then scale
1093 // the root window so that it matches the external display's
1094 // resolution. This is necessary in order for scaling to work while
1095 // mirrored.
1096 if (display_info_list->size() == 2) {
1097 switch (multi_display_mode_) {
1098 case MIRRORING: {
1099 bool zero_is_source =
1100 first_display_id_ == (*display_info_list)[0].id() ||
1101 gfx::Display::IsInternalDisplayId((*display_info_list)[0].id());
1102 DCHECK_EQ(MIRRORING, multi_display_mode_);
1103 mirroring_display_id_ =
1104 (*display_info_list)[zero_is_source ? 1 : 0].id();
1106 int64 display_id = mirroring_display_id_;
1107 auto iter =
1108 std::find_if(display_info_list->begin(), display_info_list->end(),
1109 [display_id](const DisplayInfo& info) {
1110 return info.id() == display_id;
1112 DCHECK(iter != display_info_list->end());
1114 DisplayInfo info = *iter;
1115 info.SetOverscanInsets(gfx::Insets());
1116 InsertAndUpdateDisplayInfo(info);
1117 software_mirroring_display_list_.push_back(
1118 CreateMirroringDisplayFromDisplayInfoById(mirroring_display_id_,
1119 gfx::Point(), 1.0f));
1120 display_info_list->erase(iter);
1121 break;
1123 case UNIFIED: {
1124 // TODO(oshima): Currently, all displays are laid out horizontally,
1125 // from left to right. Allow more flexible layouts, such as
1126 // right to left, or vertical layouts.
1127 gfx::Rect unified_bounds;
1128 software_mirroring_display_list_.clear();
1130 // 1st Pass. Find the max size.
1131 int max_height = std::numeric_limits<int>::min();
1133 int default_height = 0;
1134 float default_device_scale_factor = 1.0f;
1135 for (auto& info : *display_info_list) {
1136 max_height = std::max(max_height, info.size_in_pixel().height());
1137 if (!default_height || gfx::Display::IsInternalDisplayId(info.id())) {
1138 default_height = info.size_in_pixel().height();
1139 default_device_scale_factor = info.device_scale_factor();
1143 std::vector<DisplayMode> display_mode_list;
1144 std::set<std::pair<float, float>> dsf_scale_list;
1146 // 2nd Pass. Compute the unified display size.
1147 for (auto& info : *display_info_list) {
1148 InsertAndUpdateDisplayInfo(info);
1149 gfx::Point origin(unified_bounds.right(), 0);
1150 float scale =
1151 info.size_in_pixel().height() / static_cast<float>(max_height);
1152 // The display is scaled to fit the unified desktop size.
1153 gfx::Display display = CreateMirroringDisplayFromDisplayInfoById(
1154 info.id(), origin, 1.0f / scale);
1155 unified_bounds.Union(display.bounds());
1157 dsf_scale_list.insert(
1158 std::make_pair(info.device_scale_factor(), scale));
1161 DisplayInfo info(kUnifiedDisplayId, "Unified Desktop", false);
1163 DisplayMode native_mode(unified_bounds.size(), 60.0f, false, true);
1164 std::vector<DisplayMode> modes =
1165 CreateUnifiedDisplayModeList(native_mode, dsf_scale_list);
1167 // Find the default mode.
1168 auto iter = std::find_if(
1169 modes.begin(), modes.end(),
1170 [default_height,
1171 default_device_scale_factor](const DisplayMode& mode) {
1172 return mode.size.height() == default_height &&
1173 mode.device_scale_factor == default_device_scale_factor;
1175 iter->native = true;
1176 info.SetDisplayModes(modes);
1177 info.set_device_scale_factor(iter->device_scale_factor);
1178 info.SetBounds(gfx::Rect(iter->size));
1180 // Forget the configured resolution if the original unified
1181 // desktop resolution has changed.
1182 if (display_info_.count(kUnifiedDisplayId) != 0 &&
1183 GetMaxNativeSize(display_info_[kUnifiedDisplayId]) !=
1184 unified_bounds.size()) {
1185 display_modes_.erase(kUnifiedDisplayId);
1188 // 3rd Pass. Set the selected mode, then recompute the mirroring
1189 // display size.
1190 DisplayMode mode;
1191 if (GetSelectedModeForDisplayId(kUnifiedDisplayId, &mode) &&
1192 FindDisplayMode(info, mode) != info.display_modes().end()) {
1193 info.set_device_scale_factor(mode.device_scale_factor);
1194 info.SetBounds(gfx::Rect(mode.size));
1195 } else {
1196 display_modes_.erase(kUnifiedDisplayId);
1199 int unified_display_height = info.size_in_pixel().height();
1200 gfx::Point origin;
1201 for (auto& info : *display_info_list) {
1202 float display_scale = info.size_in_pixel().height() /
1203 static_cast<float>(unified_display_height);
1204 gfx::Display display = CreateMirroringDisplayFromDisplayInfoById(
1205 info.id(), origin, 1.0f / display_scale);
1206 origin.Offset(display.size().width(), 0);
1207 display.UpdateWorkAreaFromInsets(gfx::Insets());
1208 software_mirroring_display_list_.push_back(display);
1211 display_info_list->clear();
1212 display_info_list->push_back(info);
1213 InsertAndUpdateDisplayInfo(info);
1214 break;
1216 case EXTENDED:
1217 break;
1222 gfx::Display* DisplayManager::FindDisplayForId(int64 id) {
1223 auto iter = std::find_if(
1224 active_display_list_.begin(), active_display_list_.end(),
1225 [id](const gfx::Display& display) { return display.id() == id; });
1226 if (iter != active_display_list_.end())
1227 return &(*iter);
1228 // TODO(oshima): This happens when a windows in unified desktop have
1229 // been moved to normal window. Fix this.
1230 if (id != kUnifiedDisplayId)
1231 DLOG(WARNING) << "Could not find display:" << id;
1232 return NULL;
1235 void DisplayManager::AddMirrorDisplayInfoIfAny(
1236 std::vector<DisplayInfo>* display_info_list) {
1237 if (software_mirroring_enabled() && IsInMirrorMode())
1238 display_info_list->push_back(GetDisplayInfo(mirroring_display_id_));
1241 void DisplayManager::InsertAndUpdateDisplayInfo(const DisplayInfo& new_info) {
1242 std::map<int64, DisplayInfo>::iterator info =
1243 display_info_.find(new_info.id());
1244 if (info != display_info_.end()) {
1245 info->second.Copy(new_info);
1246 } else {
1247 display_info_[new_info.id()] = new_info;
1248 display_info_[new_info.id()].set_native(false);
1250 display_info_[new_info.id()].UpdateDisplaySize();
1251 OnDisplayInfoUpdated(display_info_[new_info.id()]);
1254 void DisplayManager::OnDisplayInfoUpdated(const DisplayInfo& display_info) {
1255 #if defined(OS_CHROMEOS)
1256 ui::ColorCalibrationProfile color_profile = display_info.color_profile();
1257 if (color_profile != ui::COLOR_PROFILE_STANDARD) {
1258 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
1259 display_info.id(), color_profile);
1261 #endif
1264 gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) {
1265 DCHECK(display_info_.find(id) != display_info_.end()) << "id=" << id;
1266 const DisplayInfo& display_info = display_info_[id];
1268 gfx::Display new_display(display_info.id());
1269 gfx::Rect bounds_in_native(display_info.size_in_pixel());
1270 float device_scale_factor = display_info.GetEffectiveDeviceScaleFactor();
1272 // Simply set the origin to (0,0). The primary display's origin is
1273 // always (0,0) and the bounds of non-primary display(s) will be updated
1274 // in |UpdateNonPrimaryDisplayBoundsForLayout| called in |UpdateDisplay|.
1275 new_display.SetScaleAndBounds(
1276 device_scale_factor, gfx::Rect(bounds_in_native.size()));
1277 new_display.set_rotation(display_info.GetActiveRotation());
1278 new_display.set_touch_support(display_info.touch_support());
1279 return new_display;
1282 gfx::Display DisplayManager::CreateMirroringDisplayFromDisplayInfoById(
1283 int64 id,
1284 const gfx::Point& origin,
1285 float scale) {
1286 DCHECK(display_info_.find(id) != display_info_.end()) << "id=" << id;
1287 const DisplayInfo& display_info = display_info_[id];
1289 gfx::Display new_display(display_info.id());
1290 new_display.SetScaleAndBounds(
1291 1.0f, gfx::Rect(origin, gfx::ToFlooredSize(gfx::ScaleSize(
1292 display_info.size_in_pixel(), scale))));
1293 new_display.set_touch_support(display_info.touch_support());
1294 return new_display;
1297 bool DisplayManager::UpdateNonPrimaryDisplayBoundsForLayout(
1298 DisplayList* displays,
1299 std::vector<size_t>* updated_indices) const {
1301 if (displays->size() < 2U)
1302 return false;
1304 if (displays->size() > 2U) {
1305 // For more than 2 displays, always use horizontal layout.
1306 int x_offset = displays->at(0).bounds().width();
1307 for (size_t i = 1; i < displays->size(); ++i) {
1308 gfx::Display& display = displays->at(i);
1309 const gfx::Rect& bounds = display.bounds();
1310 gfx::Point origin = gfx::Point(x_offset, 0);
1311 gfx::Insets insets = display.GetWorkAreaInsets();
1312 display.set_bounds(gfx::Rect(origin, bounds.size()));
1313 display.UpdateWorkAreaFromInsets(insets);
1314 x_offset += bounds.width();
1315 updated_indices->push_back(i);
1317 return true;
1320 DisplayLayout layout = layout_store_->ComputeDisplayLayoutForDisplayIdPair(
1321 CreateDisplayIdPair(displays->at(0).id(), displays->at(1).id()));
1323 // Ignore if a user has a old format (should be extremely rare)
1324 // and this will be replaced with DCHECK.
1325 if (layout.primary_id != gfx::Display::kInvalidDisplayID) {
1326 size_t primary_index, secondary_index;
1327 if (displays->at(0).id() == layout.primary_id) {
1328 primary_index = 0;
1329 secondary_index = 1;
1330 } else {
1331 primary_index = 1;
1332 secondary_index = 0;
1334 // This function may be called before the secondary display is
1335 // registered. The bounds is empty in that case and will
1336 // return true.
1337 gfx::Rect bounds =
1338 GetDisplayForId(displays->at(secondary_index).id()).bounds();
1339 UpdateDisplayBoundsForLayout(
1340 layout, displays->at(primary_index), &displays->at(secondary_index));
1341 updated_indices->push_back(secondary_index);
1342 return bounds != displays->at(secondary_index).bounds();
1344 return false;
1347 void DisplayManager::CreateMirrorWindowIfAny() {
1348 if (software_mirroring_display_list_.empty() || !delegate_)
1349 return;
1350 DisplayInfoList list;
1351 for (auto& display : software_mirroring_display_list_)
1352 list.push_back(GetDisplayInfo(display.id()));
1353 delegate_->CreateOrUpdateMirroringDisplay(list);
1356 // static
1357 void DisplayManager::UpdateDisplayBoundsForLayout(
1358 const DisplayLayout& layout,
1359 const gfx::Display& primary_display,
1360 gfx::Display* secondary_display) {
1361 DCHECK_EQ("0,0", primary_display.bounds().origin().ToString());
1363 const gfx::Rect& primary_bounds = primary_display.bounds();
1364 const gfx::Rect& secondary_bounds = secondary_display->bounds();
1365 gfx::Point new_secondary_origin = primary_bounds.origin();
1367 DisplayLayout::Position position = layout.position;
1369 // Ignore the offset in case the secondary display doesn't share edges with
1370 // the primary display.
1371 int offset = layout.offset;
1372 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
1373 offset = std::min(
1374 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
1375 offset = std::max(
1376 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
1377 } else {
1378 offset = std::min(
1379 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
1380 offset = std::max(
1381 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
1383 switch (position) {
1384 case DisplayLayout::TOP:
1385 new_secondary_origin.Offset(offset, -secondary_bounds.height());
1386 break;
1387 case DisplayLayout::RIGHT:
1388 new_secondary_origin.Offset(primary_bounds.width(), offset);
1389 break;
1390 case DisplayLayout::BOTTOM:
1391 new_secondary_origin.Offset(offset, primary_bounds.height());
1392 break;
1393 case DisplayLayout::LEFT:
1394 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
1395 break;
1397 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
1398 secondary_display->set_bounds(
1399 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
1400 secondary_display->UpdateWorkAreaFromInsets(insets);
1403 void DisplayManager::RunPendingTasksForTest() {
1404 if (!software_mirroring_display_list_.empty())
1405 base::RunLoop().RunUntilIdle();
1408 } // namespace ash