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

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

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

Powered by Google App Engine
This is Rietveld 408576698