Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / display / display_change_observer_chromeos.cc
blobfd2a66d1fa86e2aeb367725a758722e11a40b590
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 if (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) {
191 if (!ui::IsDisplaySizeBlackListed(state->physical_size())) {
192 device_scale_factor =
193 FindDeviceScaleFactor((kInchInMm * mode_info->size().width() /
194 state->physical_size().width()));
196 } else {
197 DisplayMode mode;
198 if (Shell::GetInstance()->display_manager()->GetSelectedModeForDisplayId(
199 state->display_id(), &mode)) {
200 device_scale_factor = mode.device_scale_factor;
201 } else {
202 // For monitors that are 40 inches and 4K or above, set
203 // |device_scale_factor| to 2x. For margin purposes, 100 is subtracted
204 // from the value of |k2xThreshouldSizeSquaredFor4KInMm|
205 const int k2xThreshouldSizeSquaredFor4KInMm =
206 (40 * 40 * kInchInMm * kInchInMm) - 100;
207 gfx::Vector2d size_in_vec(state->physical_size().width(),
208 state->physical_size().height());
209 if (size_in_vec.LengthSquared() > k2xThreshouldSizeSquaredFor4KInMm &&
210 mode_info->size().width() >= kMinimumWidthFor4K) {
211 // Make sure that additional device scale factors table has 2x.
212 DCHECK_EQ(2.0f, kAdditionalDeviceScaleFactorsFor4k[1]);
213 device_scale_factor = 2.0f;
217 gfx::Rect display_bounds(state->origin(), mode_info->size());
219 std::string name =
220 state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL
221 ? l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME)
222 : state->display_name();
223 if (name.empty())
224 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
226 bool has_overscan = state->has_overscan();
227 int64 id = state->display_id();
228 ids.insert(id);
230 displays.push_back(DisplayInfo(id, name, has_overscan));
231 DisplayInfo& new_info = displays.back();
232 new_info.set_device_scale_factor(device_scale_factor);
233 new_info.SetBounds(display_bounds);
234 new_info.set_native(true);
235 new_info.set_is_aspect_preserving_scaling(
236 state->is_aspect_preserving_scaling());
238 std::vector<DisplayMode> display_modes =
239 (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL)
240 ? GetInternalDisplayModeList(new_info, *state)
241 : GetExternalDisplayModeList(*state);
242 new_info.SetDisplayModes(display_modes);
244 new_info.set_available_color_profiles(
245 Shell::GetInstance()
246 ->display_configurator()
247 ->GetAvailableColorCalibrationProfiles(id));
250 AssociateTouchscreens(
251 &displays, ui::DeviceDataManager::GetInstance()->touchscreen_devices());
252 // DisplayManager can be null during the boot.
253 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays);
255 // For the purposes of user activity detection, ignore synthetic mouse events
256 // that are triggered by screen resizes: http://crbug.com/360634
257 ui::UserActivityDetector* user_activity_detector =
258 ui::UserActivityDetector::Get();
259 if (user_activity_detector)
260 user_activity_detector->OnDisplayPowerChanging();
263 void DisplayChangeObserver::OnDisplayModeChangeFailed(
264 const ui::DisplayConfigurator::DisplayStateList& displays,
265 ui::MultipleDisplayState failed_new_state) {
266 // If display configuration failed during startup, simply update the display
267 // manager with detected displays. If no display is detected, it will
268 // create a pseudo display.
269 if (Shell::GetInstance()->display_manager()->GetNumDisplays() == 0)
270 OnDisplayModeChanged(displays);
273 void DisplayChangeObserver::OnAppTerminating() {
274 #if defined(USE_ASH)
275 // Stop handling display configuration events once the shutdown
276 // process starts. crbug.com/177014.
277 Shell::GetInstance()->display_configurator()->PrepareForExit();
278 #endif
281 // static
282 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi) {
283 for (size_t i = 0; i < arraysize(kThresholdTable); ++i) {
284 if (dpi > kThresholdTable[i].dpi)
285 return kThresholdTable[i].device_scale_factor;
287 return 1.0f;
290 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() {
291 OnDisplayModeChanged(
292 Shell::GetInstance()->display_configurator()->cached_displays());
295 } // namespace ash