| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/display/display_change_observer_chromeos.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "ui/base/l10n/l10n_util.h" | |
| 16 #include "ui/base/user_activity/user_activity_detector.h" | |
| 17 #include "ui/display/display.h" | |
| 18 #include "ui/display/display_layout.h" | |
| 19 #include "ui/display/manager/chromeos/touchscreen_util.h" | |
| 20 #include "ui/display/manager/display_layout_store.h" | |
| 21 #include "ui/display/manager/display_manager.h" | |
| 22 #include "ui/display/manager/display_manager_utilities.h" | |
| 23 #include "ui/display/types/display_mode.h" | |
| 24 #include "ui/display/types/display_snapshot.h" | |
| 25 #include "ui/display/util/display_util.h" | |
| 26 #include "ui/events/devices/input_device_manager.h" | |
| 27 #include "ui/events/devices/touchscreen_device.h" | |
| 28 #include "ui/strings/grit/ui_strings.h" | |
| 29 | |
| 30 namespace ash { | |
| 31 | |
| 32 using ui::DisplayConfigurator; | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 // The DPI threshold to determine the device scale factor. | |
| 37 // DPI higher than |dpi| will use |device_scale_factor|. | |
| 38 struct DeviceScaleFactorDPIThreshold { | |
| 39 float dpi; | |
| 40 float device_scale_factor; | |
| 41 }; | |
| 42 | |
| 43 const DeviceScaleFactorDPIThreshold kThresholdTable[] = { | |
| 44 {200.0f, 2.0f}, | |
| 45 {150.0f, 1.25f}, | |
| 46 {0.0f, 1.0f}, | |
| 47 }; | |
| 48 | |
| 49 // 1 inch in mm. | |
| 50 const float kInchInMm = 25.4f; | |
| 51 | |
| 52 // The minimum pixel width whose monitor can be called as '4K'. | |
| 53 const int kMinimumWidthFor4K = 3840; | |
| 54 | |
| 55 // The list of device scale factors (in addition to 1.0f) which is | |
| 56 // available in external large monitors. | |
| 57 const float kAdditionalDeviceScaleFactorsFor4k[] = {1.25f, 2.0f}; | |
| 58 | |
| 59 void UpdateInternalDisplayId( | |
| 60 const ui::DisplayConfigurator::DisplayStateList& display_states) { | |
| 61 for (auto* state : display_states) { | |
| 62 if (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) { | |
| 63 if (display::Display::HasInternalDisplay()) | |
| 64 DCHECK_EQ(display::Display::InternalDisplayId(), state->display_id()); | |
| 65 display::Display::SetInternalDisplayId(state->display_id()); | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 // static | |
| 73 display::ManagedDisplayInfo::ManagedDisplayModeList | |
| 74 DisplayChangeObserver::GetInternalManagedDisplayModeList( | |
| 75 const display::ManagedDisplayInfo& display_info, | |
| 76 const ui::DisplaySnapshot& output) { | |
| 77 const ui::DisplayMode* ui_native_mode = output.native_mode(); | |
| 78 scoped_refptr<display::ManagedDisplayMode> native_mode = | |
| 79 new display::ManagedDisplayMode(ui_native_mode->size(), | |
| 80 ui_native_mode->refresh_rate(), | |
| 81 ui_native_mode->is_interlaced(), true, | |
| 82 1.0, display_info.device_scale_factor()); | |
| 83 | |
| 84 return display::CreateInternalManagedDisplayModeList(native_mode); | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 display::ManagedDisplayInfo::ManagedDisplayModeList | |
| 89 DisplayChangeObserver::GetExternalManagedDisplayModeList( | |
| 90 const ui::DisplaySnapshot& output) { | |
| 91 using DisplayModeMap = | |
| 92 std::map<std::pair<int, int>, scoped_refptr<display::ManagedDisplayMode>>; | |
| 93 DisplayModeMap display_mode_map; | |
| 94 | |
| 95 scoped_refptr<display::ManagedDisplayMode> native_mode = | |
| 96 new display::ManagedDisplayMode(); | |
| 97 for (const auto& mode_info : output.modes()) { | |
| 98 const std::pair<int, int> size(mode_info->size().width(), | |
| 99 mode_info->size().height()); | |
| 100 scoped_refptr<display::ManagedDisplayMode> display_mode = | |
| 101 new display::ManagedDisplayMode( | |
| 102 mode_info->size(), mode_info->refresh_rate(), | |
| 103 mode_info->is_interlaced(), output.native_mode() == mode_info.get(), | |
| 104 1.0, 1.0); | |
| 105 if (display_mode->native()) | |
| 106 native_mode = display_mode; | |
| 107 | |
| 108 // Add the display mode if it isn't already present and override interlaced | |
| 109 // display modes with non-interlaced ones. | |
| 110 auto display_mode_it = display_mode_map.find(size); | |
| 111 if (display_mode_it == display_mode_map.end()) | |
| 112 display_mode_map.insert(std::make_pair(size, display_mode)); | |
| 113 else if (display_mode_it->second->is_interlaced() && | |
| 114 !display_mode->is_interlaced()) | |
| 115 display_mode_it->second = std::move(display_mode); | |
| 116 } | |
| 117 | |
| 118 display::ManagedDisplayInfo::ManagedDisplayModeList display_mode_list; | |
| 119 for (const auto& display_mode_pair : display_mode_map) | |
| 120 display_mode_list.push_back(std::move(display_mode_pair.second)); | |
| 121 | |
| 122 if (output.native_mode()) { | |
| 123 const std::pair<int, int> size(native_mode->size().width(), | |
| 124 native_mode->size().height()); | |
| 125 auto it = display_mode_map.find(size); | |
| 126 DCHECK(it != display_mode_map.end()) | |
| 127 << "Native mode must be part of the mode list."; | |
| 128 | |
| 129 // If the native mode was replaced re-add it. | |
| 130 if (!it->second->native()) | |
| 131 display_mode_list.push_back(native_mode); | |
| 132 } | |
| 133 | |
| 134 if (native_mode->size().width() >= kMinimumWidthFor4K) { | |
| 135 for (size_t i = 0; i < arraysize(kAdditionalDeviceScaleFactorsFor4k); ++i) { | |
| 136 scoped_refptr<display::ManagedDisplayMode> mode = | |
| 137 new display::ManagedDisplayMode( | |
| 138 native_mode->size(), native_mode->refresh_rate(), | |
| 139 native_mode->is_interlaced(), false /* native */, | |
| 140 native_mode->ui_scale(), kAdditionalDeviceScaleFactorsFor4k[i]); | |
| 141 display_mode_list.push_back(mode); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 return display_mode_list; | |
| 146 } | |
| 147 | |
| 148 DisplayChangeObserver::DisplayChangeObserver( | |
| 149 ui::DisplayConfigurator* display_configurator, | |
| 150 display::DisplayManager* display_manager) | |
| 151 : display_configurator_(display_configurator), | |
| 152 display_manager_(display_manager) { | |
| 153 ui::InputDeviceManager::GetInstance()->AddObserver(this); | |
| 154 } | |
| 155 | |
| 156 DisplayChangeObserver::~DisplayChangeObserver() { | |
| 157 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); | |
| 158 } | |
| 159 | |
| 160 ui::MultipleDisplayState DisplayChangeObserver::GetStateForDisplayIds( | |
| 161 const ui::DisplayConfigurator::DisplayStateList& display_states) const { | |
| 162 UpdateInternalDisplayId(display_states); | |
| 163 if (display_states.size() == 1) | |
| 164 return ui::MULTIPLE_DISPLAY_STATE_SINGLE; | |
| 165 display::DisplayIdList list = display::GenerateDisplayIdList( | |
| 166 display_states.begin(), display_states.end(), | |
| 167 [](const ui::DisplaySnapshot* display_state) { | |
| 168 return display_state->display_id(); | |
| 169 }); | |
| 170 | |
| 171 const display::DisplayLayout& layout = | |
| 172 display_manager_->layout_store()->GetRegisteredDisplayLayout(list); | |
| 173 return layout.mirrored ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR | |
| 174 : ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED; | |
| 175 } | |
| 176 | |
| 177 bool DisplayChangeObserver::GetResolutionForDisplayId(int64_t display_id, | |
| 178 gfx::Size* size) const { | |
| 179 scoped_refptr<display::ManagedDisplayMode> mode = | |
| 180 display_manager_->GetSelectedModeForDisplayId(display_id); | |
| 181 if (!mode) | |
| 182 return false; | |
| 183 *size = mode->size(); | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 void DisplayChangeObserver::OnDisplayModeChanged( | |
| 188 const ui::DisplayConfigurator::DisplayStateList& display_states) { | |
| 189 UpdateInternalDisplayId(display_states); | |
| 190 | |
| 191 std::vector<display::ManagedDisplayInfo> displays; | |
| 192 std::set<int64_t> ids; | |
| 193 for (const ui::DisplaySnapshot* state : display_states) { | |
| 194 const ui::DisplayMode* mode_info = state->current_mode(); | |
| 195 if (!mode_info) | |
| 196 continue; | |
| 197 | |
| 198 float device_scale_factor = 1.0f; | |
| 199 // Sets dpi only if the screen size is not blacklisted. | |
| 200 float dpi = display::IsDisplaySizeBlackListed(state->physical_size()) | |
| 201 ? 0 | |
| 202 : kInchInMm * mode_info->size().width() / | |
| 203 state->physical_size().width(); | |
| 204 if (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) { | |
| 205 if (dpi) | |
| 206 device_scale_factor = FindDeviceScaleFactor(dpi); | |
| 207 } else { | |
| 208 scoped_refptr<display::ManagedDisplayMode> mode = | |
| 209 display_manager_->GetSelectedModeForDisplayId(state->display_id()); | |
| 210 if (mode) { | |
| 211 device_scale_factor = mode->device_scale_factor(); | |
| 212 } else { | |
| 213 // For monitors that are 40 inches and 4K or above, set | |
| 214 // |device_scale_factor| to 2x. For margin purposes, 100 is subtracted | |
| 215 // from the value of |k2xThreshouldSizeSquaredFor4KInMm| | |
| 216 const int k2xThreshouldSizeSquaredFor4KInMm = | |
| 217 (40 * 40 * kInchInMm * kInchInMm) - 100; | |
| 218 gfx::Vector2d size_in_vec(state->physical_size().width(), | |
| 219 state->physical_size().height()); | |
| 220 if (size_in_vec.LengthSquared() > k2xThreshouldSizeSquaredFor4KInMm && | |
| 221 mode_info->size().width() >= kMinimumWidthFor4K) { | |
| 222 // Make sure that additional device scale factors table has 2x. | |
| 223 DCHECK_EQ(2.0f, kAdditionalDeviceScaleFactorsFor4k[1]); | |
| 224 device_scale_factor = 2.0f; | |
| 225 } | |
| 226 } | |
| 227 } | |
| 228 gfx::Rect display_bounds(state->origin(), mode_info->size()); | |
| 229 | |
| 230 std::string name; | |
| 231 switch (state->type()) { | |
| 232 case ui::DISPLAY_CONNECTION_TYPE_INTERNAL: | |
| 233 name = l10n_util::GetStringUTF8(IDS_DISPLAY_NAME_INTERNAL); | |
| 234 break; | |
| 235 case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: | |
| 236 name = l10n_util::GetStringUTF8(IDS_DISPLAY_NAME_VIRTUAL); | |
| 237 break; | |
| 238 default: | |
| 239 name = state->display_name(); | |
| 240 } | |
| 241 | |
| 242 if (name.empty()) | |
| 243 name = l10n_util::GetStringUTF8(IDS_DISPLAY_NAME_UNKNOWN); | |
| 244 | |
| 245 bool has_overscan = state->has_overscan(); | |
| 246 int64_t id = state->display_id(); | |
| 247 ids.insert(id); | |
| 248 | |
| 249 displays.push_back(display::ManagedDisplayInfo(id, name, has_overscan)); | |
| 250 display::ManagedDisplayInfo& new_info = displays.back(); | |
| 251 new_info.set_sys_path(state->sys_path()); | |
| 252 new_info.set_device_scale_factor(device_scale_factor); | |
| 253 new_info.SetBounds(display_bounds); | |
| 254 new_info.set_native(true); | |
| 255 new_info.set_is_aspect_preserving_scaling( | |
| 256 state->is_aspect_preserving_scaling()); | |
| 257 if (dpi) | |
| 258 new_info.set_device_dpi(dpi); | |
| 259 | |
| 260 display::ManagedDisplayInfo::ManagedDisplayModeList display_modes = | |
| 261 (state->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) | |
| 262 ? GetInternalManagedDisplayModeList(new_info, *state) | |
| 263 : GetExternalManagedDisplayModeList(*state); | |
| 264 new_info.SetManagedDisplayModes(display_modes); | |
| 265 | |
| 266 new_info.set_available_color_profiles( | |
| 267 display_configurator_->GetAvailableColorCalibrationProfiles(id)); | |
| 268 new_info.set_maximum_cursor_size(state->maximum_cursor_size()); | |
| 269 } | |
| 270 | |
| 271 display::AssociateTouchscreens( | |
| 272 &displays, | |
| 273 ui::InputDeviceManager::GetInstance()->GetTouchscreenDevices()); | |
| 274 display_manager_->OnNativeDisplaysChanged(displays); | |
| 275 | |
| 276 // For the purposes of user activity detection, ignore synthetic mouse events | |
| 277 // that are triggered by screen resizes: http://crbug.com/360634 | |
| 278 ui::UserActivityDetector* user_activity_detector = | |
| 279 ui::UserActivityDetector::Get(); | |
| 280 if (user_activity_detector) | |
| 281 user_activity_detector->OnDisplayPowerChanging(); | |
| 282 } | |
| 283 | |
| 284 void DisplayChangeObserver::OnDisplayModeChangeFailed( | |
| 285 const ui::DisplayConfigurator::DisplayStateList& displays, | |
| 286 ui::MultipleDisplayState failed_new_state) { | |
| 287 // If display configuration failed during startup, simply update the display | |
| 288 // manager with detected displays. If no display is detected, it will | |
| 289 // create a pseudo display. | |
| 290 if (display_manager_->GetNumDisplays() == 0) | |
| 291 OnDisplayModeChanged(displays); | |
| 292 } | |
| 293 | |
| 294 void DisplayChangeObserver::OnTouchscreenDeviceConfigurationChanged() { | |
| 295 OnDisplayModeChanged(display_configurator_->cached_displays()); | |
| 296 } | |
| 297 | |
| 298 // static | |
| 299 float DisplayChangeObserver::FindDeviceScaleFactor(float dpi) { | |
| 300 for (size_t i = 0; i < arraysize(kThresholdTable); ++i) { | |
| 301 if (dpi > kThresholdTable[i].dpi) | |
| 302 return kThresholdTable[i].device_scale_factor; | |
| 303 } | |
| 304 return 1.0f; | |
| 305 } | |
| 306 | |
| 307 } // namespace ash | |
| OLD | NEW |