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 DisplayConfigurator::DisplayState
& output
) {
69 const ui::DisplayMode
* ui_native_mode
= output
.display
->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 DisplayConfigurator::DisplayState
& output
) {
82 typedef std::map
<std::pair
<int, int>, DisplayMode
> DisplayModeMap
;
83 DisplayModeMap display_mode_map
;
85 DisplayMode native_mode
;
86 for (std::vector
<const ui::DisplayMode
*>::const_iterator it
=
87 output
.display
->modes().begin();
88 it
!= output
.display
->modes().end();
90 const ui::DisplayMode
& mode_info
= **it
;
91 const std::pair
<int, int> size(mode_info
.size().width(),
92 mode_info
.size().height());
93 const DisplayMode
display_mode(mode_info
.size(),
94 mode_info
.refresh_rate(),
95 mode_info
.is_interlaced(),
96 output
.display
->native_mode() == *it
);
97 if (display_mode
.native
)
98 native_mode
= display_mode
;
100 // Add the display mode if it isn't already present and override interlaced
101 // display modes with non-interlaced ones.
102 DisplayModeMap::iterator display_mode_it
= display_mode_map
.find(size
);
103 if (display_mode_it
== display_mode_map
.end())
104 display_mode_map
.insert(std::make_pair(size
, display_mode
));
105 else if (display_mode_it
->second
.interlaced
&& !display_mode
.interlaced
)
106 display_mode_it
->second
= display_mode
;
109 std::vector
<DisplayMode
> display_mode_list
;
110 for (DisplayModeMap::const_iterator iter
= display_mode_map
.begin();
111 iter
!= display_mode_map
.end();
113 display_mode_list
.push_back(iter
->second
);
116 if (output
.display
->native_mode()) {
117 const std::pair
<int, int> size(native_mode
.size
.width(),
118 native_mode
.size
.height());
119 DisplayModeMap::iterator it
= display_mode_map
.find(size
);
120 DCHECK(it
!= display_mode_map
.end())
121 << "Native mode must be part of the mode list.";
123 // If the native mode was replaced re-add it.
124 if (!it
->second
.native
)
125 display_mode_list
.push_back(native_mode
);
128 if (native_mode
.size
.width() >= kMinimumWidthFor4K
) {
129 for (size_t i
= 0; i
< arraysize(kAdditionalDeviceScaleFactorsFor4k
);
131 DisplayMode mode
= native_mode
;
132 mode
.device_scale_factor
= kAdditionalDeviceScaleFactorsFor4k
[i
];
134 display_mode_list
.push_back(mode
);
138 return display_mode_list
;
141 DisplayChangeObserver::DisplayChangeObserver() {
142 Shell::GetInstance()->AddShellObserver(this);
143 ui::DeviceDataManager::GetInstance()->AddObserver(this);
146 DisplayChangeObserver::~DisplayChangeObserver() {
147 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
148 Shell::GetInstance()->RemoveShellObserver(this);
151 ui::MultipleDisplayState
DisplayChangeObserver::GetStateForDisplayIds(
152 const std::vector
<int64
>& display_ids
) const {
153 CHECK_EQ(2U, display_ids
.size());
154 DisplayIdPair pair
= std::make_pair(display_ids
[0], display_ids
[1]);
155 DisplayLayout layout
= Shell::GetInstance()->display_manager()->
156 layout_store()->GetRegisteredDisplayLayout(pair
);
157 return layout
.mirrored
? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR
:
158 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED
;
161 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id
,
162 gfx::Size
* size
) const {
164 if (!Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
172 void DisplayChangeObserver::OnDisplayModeChanged(
173 const std::vector
<DisplayConfigurator::DisplayState
>& display_states
) {
174 std::vector
<DisplayInfo
> displays
;
176 for (size_t i
= 0; i
< display_states
.size(); ++i
) {
177 const DisplayConfigurator::DisplayState
& state
= display_states
[i
];
179 if (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
180 if (gfx::Display::InternalDisplayId() ==
181 gfx::Display::kInvalidDisplayID
) {
182 gfx::Display::SetInternalDisplayId(state
.display
->display_id());
184 #if defined(USE_OZONE)
185 // TODO(dnicoara) Remove when Ozone can properly perform the initial
186 // display configuration.
187 gfx::Display::SetInternalDisplayId(state
.display
->display_id());
189 DCHECK_EQ(gfx::Display::InternalDisplayId(),
190 state
.display
->display_id());
194 const ui::DisplayMode
* mode_info
= state
.display
->current_mode();
198 float device_scale_factor
= 1.0f
;
199 if (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
200 if (!ui::IsDisplaySizeBlackListed(state
.display
->physical_size())) {
201 device_scale_factor
=
202 FindDeviceScaleFactor((kInchInMm
* mode_info
->size().width() /
203 state
.display
->physical_size().width()));
207 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
208 state
.display
->display_id(), &mode
)) {
209 device_scale_factor
= mode
.device_scale_factor
;
212 gfx::Rect
display_bounds(state
.display
->origin(), mode_info
->size());
215 state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
?
216 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME
) :
217 state
.display
->display_name();
219 name
= l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME
);
221 bool has_overscan
= state
.display
->has_overscan();
222 int64 id
= state
.display
->display_id();
225 displays
.push_back(DisplayInfo(id
, name
, has_overscan
));
226 DisplayInfo
& new_info
= displays
.back();
227 new_info
.set_device_scale_factor(device_scale_factor
);
228 new_info
.SetBounds(display_bounds
);
229 new_info
.set_native(true);
230 new_info
.set_is_aspect_preserving_scaling(
231 state
.display
->is_aspect_preserving_scaling());
233 std::vector
<DisplayMode
> display_modes
=
234 (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) ?
235 GetInternalDisplayModeList(new_info
, state
) :
236 GetExternalDisplayModeList(state
);
237 new_info
.SetDisplayModes(display_modes
);
239 new_info
.set_available_color_profiles(
241 ->display_configurator()
242 ->GetAvailableColorCalibrationProfiles(id
));
245 AssociateTouchscreens(
246 &displays
, ui::DeviceDataManager::GetInstance()->touchscreen_devices());
247 // DisplayManager can be null during the boot.
248 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays
);
250 // For the purposes of user activity detection, ignore synthetic mouse events
251 // that are triggered by screen resizes: http://crbug.com/360634
252 ui::UserActivityDetector
* user_activity_detector
=
253 ui::UserActivityDetector::Get();
254 if (user_activity_detector
)
255 user_activity_detector
->OnDisplayPowerChanging();
258 void DisplayChangeObserver::OnDisplayModeChangeFailed(
259 const ui::DisplayConfigurator::DisplayStateList
& displays
,
260 ui::MultipleDisplayState failed_new_state
) {
261 // If display configuration failed during startup, simply update the display
262 // manager with detected displays. If no display is detected, it will
263 // create a pseudo display.
264 if (Shell::GetInstance()->display_manager()->GetNumDisplays() == 0)
265 OnDisplayModeChanged(displays
);
268 void DisplayChangeObserver::OnAppTerminating() {
270 // Stop handling display configuration events once the shutdown
271 // process starts. crbug.com/177014.
272 Shell::GetInstance()->display_configurator()->PrepareForExit();
277 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi
) {
278 for (size_t i
= 0; i
< arraysize(kThresholdTable
); ++i
) {
279 if (dpi
> kThresholdTable
[i
].dpi
)
280 return kThresholdTable
[i
].device_scale_factor
;
285 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() {
286 OnDisplayModeChanged(
287 Shell::GetInstance()->display_configurator()->cached_displays());