Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / ash / display / display_change_observer_chromeos.cc
blobf167f9c10150fc27ee7ef8e96878f36294cd8580
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"
7 #include <algorithm>
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
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"
34 namespace ash {
36 using ui::DisplayConfigurator;
38 namespace {
40 // The DPI threshold to determine the device scale factor.
41 // DPI higher than |dpi| will use |device_scale_factor|.
42 struct DeviceScaleFactorDPIThreshold {
43 float dpi;
44 float device_scale_factor;
47 const DeviceScaleFactorDPIThreshold kThresholdTable[] = {
48 {200.0f, 2.0f},
49 {150.0f, 1.25f},
50 {0.0f, 1.0f},
53 // 1 inch in mm.
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};
63 void UpdateInternalDisplayId(
64 const ui::DisplayConfigurator::DisplayStateList& display_states) {
65 for (auto* state : display_states) {
66 if (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) {
67 if (gfx::Display::HasInternalDisplay())
68 DCHECK_EQ(gfx::Display::InternalDisplayId(), state->display_id());
69 gfx::Display::SetInternalDisplayId(state->display_id());
74 } // namespace
76 // static
77 std::vector<DisplayMode> DisplayChangeObserver::GetInternalDisplayModeList(
78 const DisplayInfo& display_info,
79 const ui::DisplaySnapshot& output) {
80 const ui::DisplayMode* ui_native_mode = output.native_mode();
81 DisplayMode native_mode(ui_native_mode->size(),
82 ui_native_mode->refresh_rate(),
83 ui_native_mode->is_interlaced(),
84 true);
85 native_mode.device_scale_factor = display_info.device_scale_factor();
87 return CreateInternalDisplayModeList(native_mode);
90 // static
91 std::vector<DisplayMode> DisplayChangeObserver::GetExternalDisplayModeList(
92 const ui::DisplaySnapshot& output) {
93 typedef std::map<std::pair<int, int>, DisplayMode> DisplayModeMap;
94 DisplayModeMap display_mode_map;
96 DisplayMode native_mode;
97 for (const ui::DisplayMode* mode_info : output.modes()) {
98 const std::pair<int, int> size(mode_info->size().width(),
99 mode_info->size().height());
100 const DisplayMode display_mode(mode_info->size(), mode_info->refresh_rate(),
101 mode_info->is_interlaced(),
102 output.native_mode() == mode_info);
103 if (display_mode.native)
104 native_mode = display_mode;
106 // Add the display mode if it isn't already present and override interlaced
107 // display modes with non-interlaced ones.
108 DisplayModeMap::iterator display_mode_it = display_mode_map.find(size);
109 if (display_mode_it == display_mode_map.end())
110 display_mode_map.insert(std::make_pair(size, display_mode));
111 else if (display_mode_it->second.interlaced && !display_mode.interlaced)
112 display_mode_it->second = display_mode;
115 std::vector<DisplayMode> display_mode_list;
116 for (const auto& display_mode_pair : display_mode_map)
117 display_mode_list.push_back(display_mode_pair.second);
119 if (output.native_mode()) {
120 const std::pair<int, int> size(native_mode.size.width(),
121 native_mode.size.height());
122 DisplayModeMap::iterator it = display_mode_map.find(size);
123 DCHECK(it != display_mode_map.end())
124 << "Native mode must be part of the mode list.";
126 // If the native mode was replaced re-add it.
127 if (!it->second.native)
128 display_mode_list.push_back(native_mode);
131 if (native_mode.size.width() >= kMinimumWidthFor4K) {
132 for (size_t i = 0; i < arraysize(kAdditionalDeviceScaleFactorsFor4k);
133 ++i) {
134 DisplayMode mode = native_mode;
135 mode.device_scale_factor = kAdditionalDeviceScaleFactorsFor4k[i];
136 mode.native = false;
137 display_mode_list.push_back(mode);
141 return display_mode_list;
144 DisplayChangeObserver::DisplayChangeObserver() {
145 Shell::GetInstance()->AddShellObserver(this);
146 ui::DeviceDataManager::GetInstance()->AddObserver(this);
149 DisplayChangeObserver::~DisplayChangeObserver() {
150 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
151 Shell::GetInstance()->RemoveShellObserver(this);
154 ui::MultipleDisplayState DisplayChangeObserver::GetStateForDisplayIds(
155 const ui::DisplayConfigurator::DisplayStateList& display_states) const {
156 UpdateInternalDisplayId(display_states);
157 if (display_states.size() != 2)
158 return ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
159 DisplayIdPair pair = CreateDisplayIdPair(display_states[0]->display_id(),
160 display_states[1]->display_id());
161 DisplayLayout layout = Shell::GetInstance()->display_manager()->
162 layout_store()->GetRegisteredDisplayLayout(pair);
163 return layout.mirrored ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR :
164 ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
167 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id,
168 gfx::Size* size) const {
169 DisplayMode mode;
170 if (!Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
171 display_id, &mode))
172 return false;
174 *size = mode.size;
175 return true;
178 void DisplayChangeObserver::OnDisplayModeChanged(
179 const ui::DisplayConfigurator::DisplayStateList& display_states) {
180 UpdateInternalDisplayId(display_states);
182 std::vector<DisplayInfo> displays;
183 std::set<int64> ids;
184 for (const ui::DisplaySnapshot* state : display_states) {
185 const ui::DisplayMode* mode_info = state->current_mode();
186 if (!mode_info)
187 continue;
189 float device_scale_factor = 1.0f;
190 // Sets dpi only if the screen size is not blacklisted.
191 float dpi = ui::IsDisplaySizeBlackListed(state->physical_size())
193 : kInchInMm * mode_info->size().width() /
194 state->physical_size().width();
195 if (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) {
196 if (dpi)
197 device_scale_factor = FindDeviceScaleFactor(dpi);
198 } else {
199 DisplayMode mode;
200 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
201 state->display_id(), &mode)) {
202 device_scale_factor = mode.device_scale_factor;
203 } else {
204 // For monitors that are 40 inches and 4K or above, set
205 // |device_scale_factor| to 2x. For margin purposes, 100 is subtracted
206 // from the value of |k2xThreshouldSizeSquaredFor4KInMm|
207 const int k2xThreshouldSizeSquaredFor4KInMm =
208 (40 * 40 * kInchInMm * kInchInMm) - 100;
209 gfx::Vector2d size_in_vec(state->physical_size().width(),
210 state->physical_size().height());
211 if (size_in_vec.LengthSquared() > k2xThreshouldSizeSquaredFor4KInMm &&
212 mode_info->size().width() >= kMinimumWidthFor4K) {
213 // Make sure that additional device scale factors table has 2x.
214 DCHECK_EQ(2.0f, kAdditionalDeviceScaleFactorsFor4k[1]);
215 device_scale_factor = 2.0f;
219 gfx::Rect display_bounds(state->origin(), mode_info->size());
221 std::string name =
222 state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
223 ? l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME)
224 : state->display_name();
225 if (name.empty())
226 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
228 bool has_overscan = state->has_overscan();
229 int64 id = state->display_id();
230 ids.insert(id);
232 displays.push_back(DisplayInfo(id, name, has_overscan));
233 DisplayInfo& new_info = displays.back();
234 new_info.set_device_scale_factor(device_scale_factor);
235 new_info.SetBounds(display_bounds);
236 new_info.set_native(true);
237 new_info.set_is_aspect_preserving_scaling(
238 state->is_aspect_preserving_scaling());
239 if (dpi)
240 new_info.set_device_dpi(dpi);
242 std::vector<DisplayMode> display_modes =
243 (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL)
244 ? GetInternalDisplayModeList(new_info, *state)
245 : GetExternalDisplayModeList(*state);
246 new_info.SetDisplayModes(display_modes);
248 new_info.set_available_color_profiles(
249 Shell::GetInstance()
250 ->display_configurator()
251 ->GetAvailableColorCalibrationProfiles(id));
254 AssociateTouchscreens(
255 &displays, ui::DeviceDataManager::GetInstance()->touchscreen_devices());
256 // DisplayManager can be null during the boot.
257 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays);
259 // For the purposes of user activity detection, ignore synthetic mouse events
260 // that are triggered by screen resizes: http://crbug.com/360634
261 ui::UserActivityDetector* user_activity_detector =
262 ui::UserActivityDetector::Get();
263 if (user_activity_detector)
264 user_activity_detector->OnDisplayPowerChanging();
267 void DisplayChangeObserver::OnDisplayModeChangeFailed(
268 const ui::DisplayConfigurator::DisplayStateList& displays,
269 ui::MultipleDisplayState failed_new_state) {
270 // If display configuration failed during startup, simply update the display
271 // manager with detected displays. If no display is detected, it will
272 // create a pseudo display.
273 if (Shell::GetInstance()->display_manager()->GetNumDisplays() == 0)
274 OnDisplayModeChanged(displays);
277 void DisplayChangeObserver::OnAppTerminating() {
278 #if defined(USE_ASH)
279 // Stop handling display configuration events once the shutdown
280 // process starts. crbug.com/177014.
281 Shell::GetInstance()->display_configurator()->PrepareForExit();
282 #endif
285 // static
286 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi) {
287 for (size_t i = 0; i < arraysize(kThresholdTable); ++i) {
288 if (dpi > kThresholdTable[i].dpi)
289 return kThresholdTable[i].device_scale_factor;
291 return 1.0f;
294 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() {
295 OnDisplayModeChanged(
296 Shell::GetInstance()->display_configurator()->cached_displays());
299 } // namespace ash