Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: ash/display/display_change_observer_chromeos.cc

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

Powered by Google App Engine
This is Rietveld 408576698