1 // Copyright 2013 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_change_observer_chromeos.h"
14 #include "ash/ash_switches.h"
15 #include "ash/display/display_info.h"
16 #include "ash/display/display_layout_store.h"
17 #include "ash/display/display_manager.h"
18 #include "ash/display/display_util.h"
19 #include "ash/shell.h"
20 #include "ash/touch/touchscreen_util.h"
21 #include "base/command_line.h"
22 #include "base/logging.h"
23 #include "grit/ash_strings.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/user_activity/user_activity_detector.h"
26 #include "ui/compositor/dip_util.h"
27 #include "ui/display/types/display_mode.h"
28 #include "ui/display/types/display_snapshot.h"
29 #include "ui/display/util/display_util.h"
30 #include "ui/events/devices/device_data_manager.h"
31 #include "ui/events/devices/touchscreen_device.h"
32 #include "ui/gfx/display.h"
36 using ui::DisplayConfigurator
;
40 // The DPI threshold to determine the device scale factor.
41 // DPI higher than |dpi| will use |device_scale_factor|.
42 struct DeviceScaleFactorDPIThreshold
{
44 float device_scale_factor
;
47 const DeviceScaleFactorDPIThreshold kThresholdTable
[] = {
54 const float kInchInMm
= 25.4f
;
56 // The minimum pixel width whose monitor can be called as '4K'.
57 const int kMinimumWidthFor4K
= 3840;
59 // The list of device scale factors (in addition to 1.0f) which is
60 // available in extrenal large monitors.
61 const float kAdditionalDeviceScaleFactorsFor4k
[] = {1.25f
, 2.0f
};
66 std::vector
<DisplayMode
> DisplayChangeObserver::GetInternalDisplayModeList(
67 const DisplayInfo
& display_info
,
68 const ui::DisplaySnapshot
& output
) {
69 const ui::DisplayMode
* ui_native_mode
= output
.native_mode();
70 DisplayMode
native_mode(ui_native_mode
->size(),
71 ui_native_mode
->refresh_rate(),
72 ui_native_mode
->is_interlaced(),
74 native_mode
.device_scale_factor
= display_info
.device_scale_factor();
76 return CreateInternalDisplayModeList(native_mode
);
80 std::vector
<DisplayMode
> DisplayChangeObserver::GetExternalDisplayModeList(
81 const ui::DisplaySnapshot
& output
) {
82 typedef std::map
<std::pair
<int, int>, DisplayMode
> DisplayModeMap
;
83 DisplayModeMap display_mode_map
;
85 DisplayMode native_mode
;
86 for (const ui::DisplayMode
* mode_info
: output
.modes()) {
87 const std::pair
<int, int> size(mode_info
->size().width(),
88 mode_info
->size().height());
89 const DisplayMode
display_mode(mode_info
->size(), mode_info
->refresh_rate(),
90 mode_info
->is_interlaced(),
91 output
.native_mode() == mode_info
);
92 if (display_mode
.native
)
93 native_mode
= display_mode
;
95 // Add the display mode if it isn't already present and override interlaced
96 // display modes with non-interlaced ones.
97 DisplayModeMap::iterator display_mode_it
= display_mode_map
.find(size
);
98 if (display_mode_it
== display_mode_map
.end())
99 display_mode_map
.insert(std::make_pair(size
, display_mode
));
100 else if (display_mode_it
->second
.interlaced
&& !display_mode
.interlaced
)
101 display_mode_it
->second
= display_mode
;
104 std::vector
<DisplayMode
> display_mode_list
;
105 for (const auto& display_mode_pair
: display_mode_map
)
106 display_mode_list
.push_back(display_mode_pair
.second
);
108 if (output
.native_mode()) {
109 const std::pair
<int, int> size(native_mode
.size
.width(),
110 native_mode
.size
.height());
111 DisplayModeMap::iterator it
= display_mode_map
.find(size
);
112 DCHECK(it
!= display_mode_map
.end())
113 << "Native mode must be part of the mode list.";
115 // If the native mode was replaced re-add it.
116 if (!it
->second
.native
)
117 display_mode_list
.push_back(native_mode
);
120 if (native_mode
.size
.width() >= kMinimumWidthFor4K
) {
121 for (size_t i
= 0; i
< arraysize(kAdditionalDeviceScaleFactorsFor4k
);
123 DisplayMode mode
= native_mode
;
124 mode
.device_scale_factor
= kAdditionalDeviceScaleFactorsFor4k
[i
];
126 display_mode_list
.push_back(mode
);
130 return display_mode_list
;
133 DisplayChangeObserver::DisplayChangeObserver() {
134 Shell::GetInstance()->AddShellObserver(this);
135 ui::DeviceDataManager::GetInstance()->AddObserver(this);
138 DisplayChangeObserver::~DisplayChangeObserver() {
139 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
140 Shell::GetInstance()->RemoveShellObserver(this);
143 ui::MultipleDisplayState
DisplayChangeObserver::GetStateForDisplayIds(
144 const std::vector
<int64
>& display_ids
) const {
145 CHECK_EQ(2U, display_ids
.size());
146 DisplayIdPair pair
= std::make_pair(display_ids
[0], display_ids
[1]);
147 DisplayLayout layout
= Shell::GetInstance()->display_manager()->
148 layout_store()->GetRegisteredDisplayLayout(pair
);
149 return layout
.mirrored
? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR
:
150 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED
;
153 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id
,
154 gfx::Size
* size
) const {
156 if (!Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
164 void DisplayChangeObserver::OnDisplayModeChanged(
165 const ui::DisplayConfigurator::DisplayStateList
& display_states
) {
166 std::vector
<DisplayInfo
> displays
;
168 for (const ui::DisplaySnapshot
* state
: display_states
) {
169 if (state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
170 if (gfx::Display::InternalDisplayId() ==
171 gfx::Display::kInvalidDisplayID
) {
172 gfx::Display::SetInternalDisplayId(state
->display_id());
174 #if defined(USE_OZONE)
175 // TODO(dnicoara) Remove when Ozone can properly perform the initial
176 // display configuration.
177 gfx::Display::SetInternalDisplayId(state
->display_id());
179 DCHECK_EQ(gfx::Display::InternalDisplayId(), state
->display_id());
183 const ui::DisplayMode
* mode_info
= state
->current_mode();
187 float device_scale_factor
= 1.0f
;
188 if (state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
189 if (!ui::IsDisplaySizeBlackListed(state
->physical_size())) {
190 device_scale_factor
=
191 FindDeviceScaleFactor((kInchInMm
* mode_info
->size().width() /
192 state
->physical_size().width()));
196 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
197 state
->display_id(), &mode
)) {
198 device_scale_factor
= mode
.device_scale_factor
;
201 gfx::Rect
display_bounds(state
->origin(), mode_info
->size());
204 state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
205 ? l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME
)
206 : state
->display_name();
208 name
= l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME
);
210 bool has_overscan
= state
->has_overscan();
211 int64 id
= state
->display_id();
214 displays
.push_back(DisplayInfo(id
, name
, has_overscan
));
215 DisplayInfo
& new_info
= displays
.back();
216 new_info
.set_device_scale_factor(device_scale_factor
);
217 new_info
.SetBounds(display_bounds
);
218 new_info
.set_native(true);
219 new_info
.set_is_aspect_preserving_scaling(
220 state
->is_aspect_preserving_scaling());
222 std::vector
<DisplayMode
> display_modes
=
223 (state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
)
224 ? GetInternalDisplayModeList(new_info
, *state
)
225 : GetExternalDisplayModeList(*state
);
226 new_info
.SetDisplayModes(display_modes
);
228 new_info
.set_available_color_profiles(
230 ->display_configurator()
231 ->GetAvailableColorCalibrationProfiles(id
));
234 AssociateTouchscreens(
235 &displays
, ui::DeviceDataManager::GetInstance()->touchscreen_devices());
236 // DisplayManager can be null during the boot.
237 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays
);
239 // For the purposes of user activity detection, ignore synthetic mouse events
240 // that are triggered by screen resizes: http://crbug.com/360634
241 ui::UserActivityDetector
* user_activity_detector
=
242 ui::UserActivityDetector::Get();
243 if (user_activity_detector
)
244 user_activity_detector
->OnDisplayPowerChanging();
247 void DisplayChangeObserver::OnDisplayModeChangeFailed(
248 const ui::DisplayConfigurator::DisplayStateList
& displays
,
249 ui::MultipleDisplayState failed_new_state
) {
250 // If display configuration failed during startup, simply update the display
251 // manager with detected displays. If no display is detected, it will
252 // create a pseudo display.
253 if (Shell::GetInstance()->display_manager()->GetNumDisplays() == 0)
254 OnDisplayModeChanged(displays
);
257 void DisplayChangeObserver::OnAppTerminating() {
259 // Stop handling display configuration events once the shutdown
260 // process starts. crbug.com/177014.
261 Shell::GetInstance()->display_configurator()->PrepareForExit();
266 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi
) {
267 for (size_t i
= 0; i
< arraysize(kThresholdTable
); ++i
) {
268 if (dpi
> kThresholdTable
[i
].dpi
)
269 return kThresholdTable
[i
].device_scale_factor
;
274 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() {
275 OnDisplayModeChanged(
276 Shell::GetInstance()->display_configurator()->cached_displays());