Update scrollbar layer property tree opacity when it becomes active
[chromium-blink-merge.git] / ash / display / display_manager.cc
blob2ac027e3506305ea5a7f0c5e6f68fba0a0e37d60
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 // There is no need to update the displays in mirror mode. Doing
880 // this in hardware mirroring mode can cause crash because display
881 // info in hardware mirroring comes from DisplayConfigurator.
882 if (!IsInMirrorMode())
883 ReconfigureDisplays();
886 bool DisplayManager::IsInUnifiedMode() const {
887 return multi_display_mode_ == UNIFIED &&
888 !software_mirroring_display_list_.empty();
891 const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const {
892 DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id);
894 std::map<int64, DisplayInfo>::const_iterator iter =
895 display_info_.find(display_id);
896 CHECK(iter != display_info_.end()) << display_id;
897 return iter->second;
900 const gfx::Display DisplayManager::GetMirroringDisplayById(
901 int64 display_id) const {
902 auto iter = std::find_if(software_mirroring_display_list_.begin(),
903 software_mirroring_display_list_.end(),
904 [display_id](const gfx::Display& display) {
905 return display.id() == display_id;
907 return iter == software_mirroring_display_list_.end() ? gfx::Display()
908 : *iter;
911 std::string DisplayManager::GetDisplayNameForId(int64 id) {
912 if (id == gfx::Display::kInvalidDisplayID)
913 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
915 std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id);
916 if (iter != display_info_.end() && !iter->second.name().empty())
917 return iter->second.name();
919 return base::StringPrintf("Display %d", static_cast<int>(id));
922 int64 DisplayManager::GetDisplayIdForUIScaling() const {
923 // UI Scaling is effective on internal display.
924 return gfx::Display::HasInternalDisplay() ? gfx::Display::InternalDisplayId()
925 : gfx::Display::kInvalidDisplayID;
928 void DisplayManager::SetMirrorMode(bool mirror) {
929 #if defined(OS_CHROMEOS)
930 if (num_connected_displays() <= 1)
931 return;
933 if (base::SysInfo::IsRunningOnChromeOS()) {
934 ui::MultipleDisplayState new_state =
935 mirror ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR
936 : ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
937 Shell::GetInstance()->display_configurator()->SetDisplayMode(new_state);
938 return;
940 multi_display_mode_ =
941 mirror ? MIRRORING : current_default_multi_display_mode_;
942 ReconfigureDisplays();
943 if (Shell::GetInstance()->display_configurator_animation()) {
944 Shell::GetInstance()->display_configurator_animation()->
945 StartFadeInAnimation();
947 RunPendingTasksForTest();
948 #endif
951 void DisplayManager::AddRemoveDisplay() {
952 DCHECK(!active_display_list_.empty());
953 std::vector<DisplayInfo> new_display_info_list;
954 const DisplayInfo& first_display =
955 IsInUnifiedMode()
956 ? GetDisplayInfo(software_mirroring_display_list_[0].id())
957 : GetDisplayInfo(active_display_list_[0].id());
958 new_display_info_list.push_back(first_display);
959 // Add if there is only one display connected.
960 if (num_connected_displays() == 1) {
961 const int kVerticalOffsetPx = 100;
962 // Layout the 2nd display below the primary as with the real device.
963 gfx::Rect host_bounds = first_display.bounds_in_native();
964 new_display_info_list.push_back(
965 DisplayInfo::CreateFromSpec(base::StringPrintf(
966 "%d+%d-600x%d", host_bounds.x(),
967 host_bounds.bottom() + kVerticalOffsetPx, host_bounds.height())));
969 num_connected_displays_ = new_display_info_list.size();
970 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
971 software_mirroring_display_list_.clear();
972 UpdateDisplays(new_display_info_list);
975 void DisplayManager::ToggleDisplayScaleFactor() {
976 DCHECK(!active_display_list_.empty());
977 std::vector<DisplayInfo> new_display_info_list;
978 for (DisplayList::const_iterator iter = active_display_list_.begin();
979 iter != active_display_list_.end(); ++iter) {
980 DisplayInfo display_info = GetDisplayInfo(iter->id());
981 display_info.set_device_scale_factor(
982 display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f);
983 new_display_info_list.push_back(display_info);
985 AddMirrorDisplayInfoIfAny(&new_display_info_list);
986 UpdateDisplays(new_display_info_list);
989 #if defined(OS_CHROMEOS)
990 void DisplayManager::SetSoftwareMirroring(bool enabled) {
991 SetMultiDisplayMode(enabled ? MIRRORING
992 : current_default_multi_display_mode_);
995 bool DisplayManager::SoftwareMirroringEnabled() const {
996 return software_mirroring_enabled();
998 #endif
1000 void DisplayManager::SetDefaultMultiDisplayModeForCurrentDisplays(
1001 MultiDisplayMode mode) {
1002 DCHECK_NE(MIRRORING, mode);
1003 DisplayIdPair pair = GetCurrentDisplayIdPair();
1004 layout_store_->UpdateMultiDisplayState(pair, IsInMirrorMode(),
1005 mode == UNIFIED);
1008 void DisplayManager::SetMultiDisplayMode(MultiDisplayMode mode) {
1009 multi_display_mode_ = mode;
1010 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
1011 software_mirroring_display_list_.clear();
1014 void DisplayManager::ReconfigureDisplays() {
1015 DisplayInfoList display_info_list;
1016 for (DisplayList::const_iterator iter = active_display_list_.begin();
1017 (display_info_list.size() < 2 && iter != active_display_list_.end());
1018 ++iter) {
1019 if (iter->id() == kUnifiedDisplayId)
1020 continue;
1021 display_info_list.push_back(GetDisplayInfo(iter->id()));
1023 for (auto iter = software_mirroring_display_list_.begin();
1024 (display_info_list.size() < 2 &&
1025 iter != software_mirroring_display_list_.end());
1026 ++iter) {
1027 display_info_list.push_back(GetDisplayInfo(iter->id()));
1029 mirroring_display_id_ = gfx::Display::kInvalidDisplayID;
1030 software_mirroring_display_list_.clear();
1031 UpdateDisplays(display_info_list);
1034 bool DisplayManager::UpdateDisplayBounds(int64 display_id,
1035 const gfx::Rect& new_bounds) {
1036 if (change_display_upon_host_resize_) {
1037 display_info_[display_id].SetBounds(new_bounds);
1038 // Don't notify observers if the mirrored window has changed.
1039 if (software_mirroring_enabled() && mirroring_display_id_ == display_id)
1040 return false;
1041 gfx::Display* display = FindDisplayForId(display_id);
1042 display->SetSize(display_info_[display_id].size_in_pixel());
1043 screen_->NotifyMetricsChanged(*display,
1044 gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1045 return true;
1047 return false;
1050 void DisplayManager::CreateMirrorWindowAsyncIfAny() {
1051 // Do not post a task if the software mirroring doesn't exist, or
1052 // during initialization when compositor's init task isn't posted yet.
1053 // ash::Shell::Init() will call this after the compositor is initialized.
1054 if (software_mirroring_display_list_.empty() || !delegate_)
1055 return;
1056 base::MessageLoopForUI::current()->PostTask(
1057 FROM_HERE,
1058 base::Bind(&DisplayManager::CreateMirrorWindowIfAny,
1059 weak_ptr_factory_.GetWeakPtr()));
1062 scoped_ptr<MouseWarpController> DisplayManager::CreateMouseWarpController(
1063 aura::Window* drag_source) const {
1064 if (IsInUnifiedMode() && num_connected_displays() >= 2)
1065 return make_scoped_ptr(new UnifiedMouseWarpController());
1066 // Extra check for |num_connected_displays()| is for SystemDisplayApiTest
1067 // that injects MockScreen.
1068 if (GetNumDisplays() < 2 || num_connected_displays() < 2)
1069 return make_scoped_ptr(new NullMouseWarpController());
1070 return make_scoped_ptr(new ExtendedMouseWarpController(drag_source));
1073 void DisplayManager::CreateScreenForShutdown() const {
1074 bool native_is_ash =
1075 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE) == screen_.get();
1076 delete screen_for_shutdown;
1077 screen_for_shutdown = screen_->CloneForShutdown();
1078 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
1079 screen_for_shutdown);
1080 if (native_is_ash) {
1081 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
1082 screen_for_shutdown);
1086 void DisplayManager::UpdateInternalDisplayModeListForTest() {
1087 if (!gfx::Display::HasInternalDisplay() ||
1088 display_info_.count(gfx::Display::InternalDisplayId()) == 0)
1089 return;
1090 DisplayInfo* info = &display_info_[gfx::Display::InternalDisplayId()];
1091 SetInternalDisplayModeList(info);
1094 void DisplayManager::CreateSoftwareMirroringDisplayInfo(
1095 DisplayInfoList* display_info_list) {
1096 // Use the internal display or 1st as the mirror source, then scale
1097 // the root window so that it matches the external display's
1098 // resolution. This is necessary in order for scaling to work while
1099 // mirrored.
1100 if (display_info_list->size() == 2) {
1101 switch (multi_display_mode_) {
1102 case MIRRORING: {
1103 bool zero_is_source =
1104 first_display_id_ == (*display_info_list)[0].id() ||
1105 gfx::Display::IsInternalDisplayId((*display_info_list)[0].id());
1106 DCHECK_EQ(MIRRORING, multi_display_mode_);
1107 mirroring_display_id_ =
1108 (*display_info_list)[zero_is_source ? 1 : 0].id();
1110 int64 display_id = mirroring_display_id_;
1111 auto iter =
1112 std::find_if(display_info_list->begin(), display_info_list->end(),
1113 [display_id](const DisplayInfo& info) {
1114 return info.id() == display_id;
1116 DCHECK(iter != display_info_list->end());
1118 DisplayInfo info = *iter;
1119 info.SetOverscanInsets(gfx::Insets());
1120 InsertAndUpdateDisplayInfo(info);
1121 software_mirroring_display_list_.push_back(
1122 CreateMirroringDisplayFromDisplayInfoById(mirroring_display_id_,
1123 gfx::Point(), 1.0f));
1124 display_info_list->erase(iter);
1125 break;
1127 case UNIFIED: {
1128 // TODO(oshima): Currently, all displays are laid out horizontally,
1129 // from left to right. Allow more flexible layouts, such as
1130 // right to left, or vertical layouts.
1131 gfx::Rect unified_bounds;
1132 software_mirroring_display_list_.clear();
1134 // 1st Pass. Find the max size.
1135 int max_height = std::numeric_limits<int>::min();
1137 int default_height = 0;
1138 float default_device_scale_factor = 1.0f;
1139 for (auto& info : *display_info_list) {
1140 max_height = std::max(max_height, info.size_in_pixel().height());
1141 if (!default_height || gfx::Display::IsInternalDisplayId(info.id())) {
1142 default_height = info.size_in_pixel().height();
1143 default_device_scale_factor = info.device_scale_factor();
1147 std::vector<DisplayMode> display_mode_list;
1148 std::set<std::pair<float, float>> dsf_scale_list;
1150 // 2nd Pass. Compute the unified display size.
1151 for (auto& info : *display_info_list) {
1152 InsertAndUpdateDisplayInfo(info);
1153 gfx::Point origin(unified_bounds.right(), 0);
1154 float scale =
1155 info.size_in_pixel().height() / static_cast<float>(max_height);
1156 // The display is scaled to fit the unified desktop size.
1157 gfx::Display display = CreateMirroringDisplayFromDisplayInfoById(
1158 info.id(), origin, 1.0f / scale);
1159 unified_bounds.Union(display.bounds());
1161 dsf_scale_list.insert(
1162 std::make_pair(info.device_scale_factor(), scale));
1165 DisplayInfo info(kUnifiedDisplayId, "Unified Desktop", false);
1167 DisplayMode native_mode(unified_bounds.size(), 60.0f, false, true);
1168 std::vector<DisplayMode> modes =
1169 CreateUnifiedDisplayModeList(native_mode, dsf_scale_list);
1171 // Find the default mode.
1172 auto iter = std::find_if(
1173 modes.begin(), modes.end(),
1174 [default_height,
1175 default_device_scale_factor](const DisplayMode& mode) {
1176 return mode.size.height() == default_height &&
1177 mode.device_scale_factor == default_device_scale_factor;
1179 iter->native = true;
1180 info.SetDisplayModes(modes);
1181 info.set_device_scale_factor(iter->device_scale_factor);
1182 info.SetBounds(gfx::Rect(iter->size));
1184 // Forget the configured resolution if the original unified
1185 // desktop resolution has changed.
1186 if (display_info_.count(kUnifiedDisplayId) != 0 &&
1187 GetMaxNativeSize(display_info_[kUnifiedDisplayId]) !=
1188 unified_bounds.size()) {
1189 display_modes_.erase(kUnifiedDisplayId);
1192 // 3rd Pass. Set the selected mode, then recompute the mirroring
1193 // display size.
1194 DisplayMode mode;
1195 if (GetSelectedModeForDisplayId(kUnifiedDisplayId, &mode) &&
1196 FindDisplayMode(info, mode) != info.display_modes().end()) {
1197 info.set_device_scale_factor(mode.device_scale_factor);
1198 info.SetBounds(gfx::Rect(mode.size));
1199 } else {
1200 display_modes_.erase(kUnifiedDisplayId);
1203 int unified_display_height = info.size_in_pixel().height();
1204 gfx::Point origin;
1205 for (auto& info : *display_info_list) {
1206 float display_scale = info.size_in_pixel().height() /
1207 static_cast<float>(unified_display_height);
1208 gfx::Display display = CreateMirroringDisplayFromDisplayInfoById(
1209 info.id(), origin, 1.0f / display_scale);
1210 origin.Offset(display.size().width(), 0);
1211 display.UpdateWorkAreaFromInsets(gfx::Insets());
1212 software_mirroring_display_list_.push_back(display);
1215 display_info_list->clear();
1216 display_info_list->push_back(info);
1217 InsertAndUpdateDisplayInfo(info);
1218 break;
1220 case EXTENDED:
1221 break;
1226 gfx::Display* DisplayManager::FindDisplayForId(int64 id) {
1227 auto iter = std::find_if(
1228 active_display_list_.begin(), active_display_list_.end(),
1229 [id](const gfx::Display& display) { return display.id() == id; });
1230 if (iter != active_display_list_.end())
1231 return &(*iter);
1232 // TODO(oshima): This happens when a windows in unified desktop have
1233 // been moved to normal window. Fix this.
1234 if (id != kUnifiedDisplayId)
1235 DLOG(WARNING) << "Could not find display:" << id;
1236 return NULL;
1239 void DisplayManager::AddMirrorDisplayInfoIfAny(
1240 std::vector<DisplayInfo>* display_info_list) {
1241 if (software_mirroring_enabled() && IsInMirrorMode())
1242 display_info_list->push_back(GetDisplayInfo(mirroring_display_id_));
1245 void DisplayManager::InsertAndUpdateDisplayInfo(const DisplayInfo& new_info) {
1246 std::map<int64, DisplayInfo>::iterator info =
1247 display_info_.find(new_info.id());
1248 if (info != display_info_.end()) {
1249 info->second.Copy(new_info);
1250 } else {
1251 display_info_[new_info.id()] = new_info;
1252 display_info_[new_info.id()].set_native(false);
1254 display_info_[new_info.id()].UpdateDisplaySize();
1255 OnDisplayInfoUpdated(display_info_[new_info.id()]);
1258 void DisplayManager::OnDisplayInfoUpdated(const DisplayInfo& display_info) {
1259 #if defined(OS_CHROMEOS)
1260 ui::ColorCalibrationProfile color_profile = display_info.color_profile();
1261 if (color_profile != ui::COLOR_PROFILE_STANDARD) {
1262 Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
1263 display_info.id(), color_profile);
1265 #endif
1268 gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) {
1269 DCHECK(display_info_.find(id) != display_info_.end()) << "id=" << id;
1270 const DisplayInfo& display_info = display_info_[id];
1272 gfx::Display new_display(display_info.id());
1273 gfx::Rect bounds_in_native(display_info.size_in_pixel());
1274 float device_scale_factor = display_info.GetEffectiveDeviceScaleFactor();
1276 // Simply set the origin to (0,0). The primary display's origin is
1277 // always (0,0) and the bounds of non-primary display(s) will be updated
1278 // in |UpdateNonPrimaryDisplayBoundsForLayout| called in |UpdateDisplay|.
1279 new_display.SetScaleAndBounds(
1280 device_scale_factor, gfx::Rect(bounds_in_native.size()));
1281 new_display.set_rotation(display_info.GetActiveRotation());
1282 new_display.set_touch_support(display_info.touch_support());
1283 return new_display;
1286 gfx::Display DisplayManager::CreateMirroringDisplayFromDisplayInfoById(
1287 int64 id,
1288 const gfx::Point& origin,
1289 float scale) {
1290 DCHECK(display_info_.find(id) != display_info_.end()) << "id=" << id;
1291 const DisplayInfo& display_info = display_info_[id];
1293 gfx::Display new_display(display_info.id());
1294 new_display.SetScaleAndBounds(
1295 1.0f, gfx::Rect(origin, gfx::ToFlooredSize(gfx::ScaleSize(
1296 display_info.size_in_pixel(), scale))));
1297 new_display.set_touch_support(display_info.touch_support());
1298 return new_display;
1301 bool DisplayManager::UpdateNonPrimaryDisplayBoundsForLayout(
1302 DisplayList* displays,
1303 std::vector<size_t>* updated_indices) const {
1305 if (displays->size() < 2U)
1306 return false;
1308 if (displays->size() > 2U) {
1309 // For more than 2 displays, always use horizontal layout.
1310 int x_offset = displays->at(0).bounds().width();
1311 for (size_t i = 1; i < displays->size(); ++i) {
1312 gfx::Display& display = displays->at(i);
1313 const gfx::Rect& bounds = display.bounds();
1314 gfx::Point origin = gfx::Point(x_offset, 0);
1315 gfx::Insets insets = display.GetWorkAreaInsets();
1316 display.set_bounds(gfx::Rect(origin, bounds.size()));
1317 display.UpdateWorkAreaFromInsets(insets);
1318 x_offset += bounds.width();
1319 updated_indices->push_back(i);
1321 return true;
1324 DisplayLayout layout = layout_store_->ComputeDisplayLayoutForDisplayIdPair(
1325 CreateDisplayIdPair(displays->at(0).id(), displays->at(1).id()));
1327 // Ignore if a user has a old format (should be extremely rare)
1328 // and this will be replaced with DCHECK.
1329 if (layout.primary_id != gfx::Display::kInvalidDisplayID) {
1330 size_t primary_index, secondary_index;
1331 if (displays->at(0).id() == layout.primary_id) {
1332 primary_index = 0;
1333 secondary_index = 1;
1334 } else {
1335 primary_index = 1;
1336 secondary_index = 0;
1338 // This function may be called before the secondary display is
1339 // registered. The bounds is empty in that case and will
1340 // return true.
1341 gfx::Rect bounds =
1342 GetDisplayForId(displays->at(secondary_index).id()).bounds();
1343 UpdateDisplayBoundsForLayout(
1344 layout, displays->at(primary_index), &displays->at(secondary_index));
1345 updated_indices->push_back(secondary_index);
1346 return bounds != displays->at(secondary_index).bounds();
1348 return false;
1351 void DisplayManager::CreateMirrorWindowIfAny() {
1352 if (software_mirroring_display_list_.empty() || !delegate_)
1353 return;
1354 DisplayInfoList list;
1355 for (auto& display : software_mirroring_display_list_)
1356 list.push_back(GetDisplayInfo(display.id()));
1357 delegate_->CreateOrUpdateMirroringDisplay(list);
1360 // static
1361 void DisplayManager::UpdateDisplayBoundsForLayout(
1362 const DisplayLayout& layout,
1363 const gfx::Display& primary_display,
1364 gfx::Display* secondary_display) {
1365 DCHECK_EQ("0,0", primary_display.bounds().origin().ToString());
1367 const gfx::Rect& primary_bounds = primary_display.bounds();
1368 const gfx::Rect& secondary_bounds = secondary_display->bounds();
1369 gfx::Point new_secondary_origin = primary_bounds.origin();
1371 DisplayLayout::Position position = layout.position;
1373 // Ignore the offset in case the secondary display doesn't share edges with
1374 // the primary display.
1375 int offset = layout.offset;
1376 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
1377 offset = std::min(
1378 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
1379 offset = std::max(
1380 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
1381 } else {
1382 offset = std::min(
1383 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
1384 offset = std::max(
1385 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
1387 switch (position) {
1388 case DisplayLayout::TOP:
1389 new_secondary_origin.Offset(offset, -secondary_bounds.height());
1390 break;
1391 case DisplayLayout::RIGHT:
1392 new_secondary_origin.Offset(primary_bounds.width(), offset);
1393 break;
1394 case DisplayLayout::BOTTOM:
1395 new_secondary_origin.Offset(offset, primary_bounds.height());
1396 break;
1397 case DisplayLayout::LEFT:
1398 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
1399 break;
1401 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
1402 secondary_display->set_bounds(
1403 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
1404 secondary_display->UpdateWorkAreaFromInsets(insets);
1407 void DisplayManager::RunPendingTasksForTest() {
1408 if (!software_mirroring_display_list_.empty())
1409 base::RunLoop().RunUntilIdle();
1412 } // namespace ash