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::HasInternalDisplay()) {
171 gfx::Display::SetInternalDisplayId(state
->display_id());
173 #if defined(USE_OZONE)
174 // TODO(dnicoara) Remove when Ozone can properly perform the initial
175 // display configuration.
176 gfx::Display::SetInternalDisplayId(state
->display_id());
178 DCHECK_EQ(gfx::Display::InternalDisplayId(), state
->display_id());
182 const ui::DisplayMode
* mode_info
= state
->current_mode();
186 float device_scale_factor
= 1.0f
;
187 if (state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
188 if (!ui::IsDisplaySizeBlackListed(state
->physical_size())) {
189 device_scale_factor
=
190 FindDeviceScaleFactor((kInchInMm
* mode_info
->size().width() /
191 state
->physical_size().width()));
195 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
196 state
->display_id(), &mode
)) {
197 device_scale_factor
= mode
.device_scale_factor
;
199 // For monitors that are 40 inches and 4K or above, set
200 // |device_scale_factor| to 2x. For margin purposes, 100 is subtracted
201 // from the value of |k2xThreshouldSizeSquaredFor4KInMm|
202 const int k2xThreshouldSizeSquaredFor4KInMm
=
203 (40 * 40 * kInchInMm
* kInchInMm
) - 100;
204 gfx::Vector2d
size_in_vec(state
->physical_size().width(),
205 state
->physical_size().height());
206 if (size_in_vec
.LengthSquared() > k2xThreshouldSizeSquaredFor4KInMm
&&
207 mode_info
->size().width() >= kMinimumWidthFor4K
) {
208 // Make sure that additional device scale factors table has 2x.
209 DCHECK_EQ(2.0f
, kAdditionalDeviceScaleFactorsFor4k
[1]);
210 device_scale_factor
= 2.0f
;
214 gfx::Rect
display_bounds(state
->origin(), mode_info
->size());
217 state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
218 ? l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME
)
219 : state
->display_name();
221 name
= l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME
);
223 bool has_overscan
= state
->has_overscan();
224 int64 id
= state
->display_id();
227 displays
.push_back(DisplayInfo(id
, name
, has_overscan
));
228 DisplayInfo
& new_info
= displays
.back();
229 new_info
.set_device_scale_factor(device_scale_factor
);
230 new_info
.SetBounds(display_bounds
);
231 new_info
.set_native(true);
232 new_info
.set_is_aspect_preserving_scaling(
233 state
->is_aspect_preserving_scaling());
235 std::vector
<DisplayMode
> display_modes
=
236 (state
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
)
237 ? GetInternalDisplayModeList(new_info
, *state
)
238 : GetExternalDisplayModeList(*state
);
239 new_info
.SetDisplayModes(display_modes
);
241 new_info
.set_available_color_profiles(
243 ->display_configurator()
244 ->GetAvailableColorCalibrationProfiles(id
));
247 AssociateTouchscreens(
248 &displays
, ui::DeviceDataManager::GetInstance()->touchscreen_devices());
249 // DisplayManager can be null during the boot.
250 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays
);
252 // For the purposes of user activity detection, ignore synthetic mouse events
253 // that are triggered by screen resizes: http://crbug.com/360634
254 ui::UserActivityDetector
* user_activity_detector
=
255 ui::UserActivityDetector::Get();
256 if (user_activity_detector
)
257 user_activity_detector
->OnDisplayPowerChanging();
260 void DisplayChangeObserver::OnDisplayModeChangeFailed(
261 const ui::DisplayConfigurator::DisplayStateList
& displays
,
262 ui::MultipleDisplayState failed_new_state
) {
263 // If display configuration failed during startup, simply update the display
264 // manager with detected displays. If no display is detected, it will
265 // create a pseudo display.
266 if (Shell::GetInstance()->display_manager()->GetNumDisplays() == 0)
267 OnDisplayModeChanged(displays
);
270 void DisplayChangeObserver::OnAppTerminating() {
272 // Stop handling display configuration events once the shutdown
273 // process starts. crbug.com/177014.
274 Shell::GetInstance()->display_configurator()->PrepareForExit();
279 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi
) {
280 for (size_t i
= 0; i
< arraysize(kThresholdTable
); ++i
) {
281 if (dpi
> kThresholdTable
[i
].dpi
)
282 return kThresholdTable
[i
].device_scale_factor
;
287 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() {
288 OnDisplayModeChanged(
289 Shell::GetInstance()->display_configurator()->cached_displays());