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"
12 #include "ash/ash_switches.h"
13 #include "ash/display/display_info.h"
14 #include "ash/display/display_layout_store.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/shell.h"
17 #include "base/command_line.h"
18 #include "base/logging.h"
19 #include "grit/ash_strings.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/compositor/dip_util.h"
22 #include "ui/display/types/chromeos/display_mode.h"
23 #include "ui/display/types/chromeos/display_snapshot.h"
24 #include "ui/display/util/display_util.h"
25 #include "ui/gfx/display.h"
29 using ui::DisplayConfigurator
;
33 // The DPI threshold to determine the device scale factor.
34 // DPI higher than |dpi| will use |device_scale_factor|.
35 struct DeviceScaleFactorDPIThreshold
{
37 float device_scale_factor
;
40 const DeviceScaleFactorDPIThreshold kThresholdTable
[] = {
47 const float kInchInMm
= 25.4f
;
49 // The minimum pixel width whose monitor can be called as '4K'.
50 const int kMinimumWidthFor4K
= 3840;
52 // The list of device scale factors (in addition to 1.0f) which is
53 // available in extrenal large monitors.
54 const float kAdditionalDeviceScaleFactorsFor4k
[] = {1.25f
, 2.0f
};
56 // Display mode list is sorted by:
57 // * the area in pixels in ascending order
58 // * refresh rate in descending order
59 struct DisplayModeSorter
{
60 bool operator()(const DisplayMode
& a
, const DisplayMode
& b
) {
61 gfx::Size size_a_dip
= a
.GetSizeInDIP();
62 gfx::Size size_b_dip
= b
.GetSizeInDIP();
63 if (size_a_dip
.GetArea() == size_b_dip
.GetArea())
64 return (a
.refresh_rate
> b
.refresh_rate
);
65 return (size_a_dip
.GetArea() < size_b_dip
.GetArea());
72 std::vector
<DisplayMode
> DisplayChangeObserver::GetInternalDisplayModeList(
73 const DisplayInfo
& display_info
,
74 const DisplayConfigurator::DisplayState
& output
) {
75 std::vector
<DisplayMode
> display_mode_list
;
76 const ui::DisplayMode
* ui_native_mode
= output
.display
->native_mode();
77 DisplayMode
native_mode(ui_native_mode
->size(),
78 ui_native_mode
->refresh_rate(),
79 ui_native_mode
->is_interlaced(),
81 native_mode
.device_scale_factor
= display_info
.device_scale_factor();
82 std::vector
<float> ui_scales
=
83 DisplayManager::GetScalesForDisplay(display_info
);
84 for (size_t i
= 0; i
< ui_scales
.size(); ++i
) {
85 DisplayMode mode
= native_mode
;
86 mode
.ui_scale
= ui_scales
[i
];
87 mode
.native
= (ui_scales
[i
] == 1.0f
);
88 display_mode_list
.push_back(mode
);
92 display_mode_list
.begin(), display_mode_list
.end(), DisplayModeSorter());
93 return display_mode_list
;
97 std::vector
<DisplayMode
> DisplayChangeObserver::GetExternalDisplayModeList(
98 const DisplayConfigurator::DisplayState
& output
) {
99 typedef std::map
<std::pair
<int, int>, DisplayMode
> DisplayModeMap
;
100 DisplayModeMap display_mode_map
;
102 DisplayMode native_mode
;
103 for (std::vector
<const ui::DisplayMode
*>::const_iterator it
=
104 output
.display
->modes().begin();
105 it
!= output
.display
->modes().end();
107 const ui::DisplayMode
& mode_info
= **it
;
108 const std::pair
<int, int> size(mode_info
.size().width(),
109 mode_info
.size().height());
110 const DisplayMode
display_mode(mode_info
.size(),
111 mode_info
.refresh_rate(),
112 mode_info
.is_interlaced(),
113 output
.display
->native_mode() == *it
);
114 if (display_mode
.native
)
115 native_mode
= display_mode
;
117 // Add the display mode if it isn't already present and override interlaced
118 // display modes with non-interlaced ones.
119 DisplayModeMap::iterator display_mode_it
= display_mode_map
.find(size
);
120 if (display_mode_it
== display_mode_map
.end())
121 display_mode_map
.insert(std::make_pair(size
, display_mode
));
122 else if (display_mode_it
->second
.interlaced
&& !display_mode
.interlaced
)
123 display_mode_it
->second
= display_mode
;
126 std::vector
<DisplayMode
> display_mode_list
;
127 for (DisplayModeMap::const_iterator iter
= display_mode_map
.begin();
128 iter
!= display_mode_map
.end();
130 display_mode_list
.push_back(iter
->second
);
133 if (native_mode
.size
.width() >= kMinimumWidthFor4K
) {
134 for (size_t i
= 0; i
< arraysize(kAdditionalDeviceScaleFactorsFor4k
);
136 DisplayMode mode
= native_mode
;
137 mode
.device_scale_factor
= kAdditionalDeviceScaleFactorsFor4k
[i
];
139 display_mode_list
.push_back(mode
);
144 display_mode_list
.begin(), display_mode_list
.end(), DisplayModeSorter());
145 return display_mode_list
;
148 DisplayChangeObserver::DisplayChangeObserver() {
149 Shell::GetInstance()->AddShellObserver(this);
152 DisplayChangeObserver::~DisplayChangeObserver() {
153 Shell::GetInstance()->RemoveShellObserver(this);
156 ui::MultipleDisplayState
DisplayChangeObserver::GetStateForDisplayIds(
157 const std::vector
<int64
>& display_ids
) const {
158 CHECK_EQ(2U, display_ids
.size());
159 DisplayIdPair pair
= std::make_pair(display_ids
[0], display_ids
[1]);
160 DisplayLayout layout
= Shell::GetInstance()->display_manager()->
161 layout_store()->GetRegisteredDisplayLayout(pair
);
162 return layout
.mirrored
? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR
:
163 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED
;
166 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id
,
167 gfx::Size
* size
) const {
169 if (!Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
177 void DisplayChangeObserver::OnDisplayModeChanged(
178 const std::vector
<DisplayConfigurator::DisplayState
>& display_states
) {
179 std::vector
<DisplayInfo
> displays
;
181 for (size_t i
= 0; i
< display_states
.size(); ++i
) {
182 const DisplayConfigurator::DisplayState
& state
= display_states
[i
];
184 if (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
&&
185 gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID
) {
186 gfx::Display::SetInternalDisplayId(state
.display
->display_id());
189 const ui::DisplayMode
* mode_info
= state
.display
->current_mode();
193 float device_scale_factor
= 1.0f
;
194 if (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) {
195 if (!ui::IsDisplaySizeBlackListed(state
.display
->physical_size())) {
196 device_scale_factor
=
197 FindDeviceScaleFactor((kInchInMm
* mode_info
->size().width() /
198 state
.display
->physical_size().width()));
202 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
203 state
.display
->display_id(), &mode
)) {
204 device_scale_factor
= mode
.device_scale_factor
;
207 gfx::Rect
display_bounds(state
.display
->origin(), mode_info
->size());
210 state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
?
211 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME
) :
212 state
.display
->display_name();
214 name
= l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME
);
216 bool has_overscan
= state
.display
->has_overscan();
217 int64 id
= state
.display
->display_id();
220 displays
.push_back(DisplayInfo(id
, name
, has_overscan
));
221 DisplayInfo
& new_info
= displays
.back();
222 new_info
.set_device_scale_factor(device_scale_factor
);
223 new_info
.SetBounds(display_bounds
);
224 new_info
.set_native(true);
225 new_info
.set_touch_support(state
.touch_device_id
== 0 ?
226 gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
:
227 gfx::Display::TOUCH_SUPPORT_AVAILABLE
);
228 new_info
.set_touch_device_id(state
.touch_device_id
);
229 new_info
.set_is_aspect_preserving_scaling(
230 state
.display
->is_aspect_preserving_scaling());
232 std::vector
<DisplayMode
> display_modes
=
233 (state
.display
->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
) ?
234 GetInternalDisplayModeList(new_info
, state
) :
235 GetExternalDisplayModeList(state
);
236 new_info
.set_display_modes(display_modes
);
238 new_info
.set_available_color_profiles(
240 ->display_configurator()
241 ->GetAvailableColorCalibrationProfiles(id
));
244 // DisplayManager can be null during the boot.
245 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays
);
248 void DisplayChangeObserver::OnAppTerminating() {
250 // Stop handling display configuration events once the shutdown
251 // process starts. crbug.com/177014.
252 Shell::GetInstance()->display_configurator()->PrepareForExit();
257 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi
) {
258 for (size_t i
= 0; i
< arraysize(kThresholdTable
); ++i
) {
259 if (dpi
> kThresholdTable
[i
].dpi
)
260 return kThresholdTable
[i
].device_scale_factor
;