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

Side by Side Diff: ui/display/manager/display_manager_utilities.cc

Issue 2286523002: Relocate reuseable portions of ash/display/display_util.* (Closed)
Patch Set: fixed ozone build Created 4 years, 3 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_util.h" 5 #include "ui/display/manager/display_manager_utilities.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector>
8 9
9 #include "ash/common/new_window_delegate.h" 10 #include "base/sys_info.h"
10 #include "ash/common/system/system_notifier.h"
11 #include "ash/common/wm_shell.h"
12 #include "ash/display/display_manager.h"
13 #include "ash/host/ash_window_tree_host.h"
14 #include "ash/shell.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "grit/ash_resources.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/display/display.h"
22 #include "ui/display/manager/managed_display_info.h" 11 #include "ui/display/manager/managed_display_info.h"
23 #include "ui/gfx/geometry/point.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/geometry/size_conversions.h" 12 #include "ui/gfx/geometry/size_conversions.h"
26 #include "ui/message_center/message_center.h" 13 #include "ui/gfx/geometry/size_f.h"
27 #include "ui/message_center/notification.h"
28 #include "ui/message_center/notification_delegate.h"
29 #include "ui/message_center/notification_list.h"
30 #include "ui/wm/core/coordinate_conversion.h"
31 14
32 #if defined(OS_CHROMEOS) 15 namespace ui {
33 #include "base/sys_info.h"
34 #endif
35 16
36 namespace ash {
37 namespace { 17 namespace {
38 18
39 const char kDisplayErrorNotificationId[] = "chrome://settings/display/error";
40
41 // A notification delegate that will start the feedback app when the notication
42 // is clicked.
43 class DisplayErrorNotificationDelegate
44 : public message_center::NotificationDelegate {
45 public:
46 DisplayErrorNotificationDelegate() = default;
47
48 // message_center::NotificationDelegate:
49 bool HasClickedListener() override { return true; }
50
51 void Click() override {
52 WmShell::Get()->new_window_delegate()->OpenFeedbackPage();
53 }
54
55 private:
56 // Private destructor since NotificationDelegate is ref-counted.
57 ~DisplayErrorNotificationDelegate() override = default;
58
59 DISALLOW_COPY_AND_ASSIGN(DisplayErrorNotificationDelegate);
60 };
61
62 // List of value UI Scale values. Scales for 2x are equivalent to 640, 19 // List of value UI Scale values. Scales for 2x are equivalent to 640,
63 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on 20 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
64 // 2560 pixel width 2x density display. Please see crbug.com/233375 21 // 2560 pixel width 2x density display. Please see crbug.com/233375
65 // for the full list of resolutions. 22 // for the full list of resolutions.
66 const float kUIScalesFor2x[] = {0.5f, 0.625f, 0.8f, 1.0f, 23 const float kUIScalesFor2x[] = {0.5f, 0.625f, 0.8f, 1.0f,
67 1.125f, 1.25f, 1.5f, 2.0f}; 24 1.125f, 1.25f, 1.5f, 2.0f};
68 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f}; 25 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f};
69 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f}; 26 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f};
70 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f}; 27 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f};
71 28
(...skipping 29 matching lines...) Expand all
101 struct ScaleComparator { 58 struct ScaleComparator {
102 explicit ScaleComparator(float s) : scale(s) {} 59 explicit ScaleComparator(float s) : scale(s) {}
103 60
104 bool operator()(const scoped_refptr<ui::ManagedDisplayMode>& mode) const { 61 bool operator()(const scoped_refptr<ui::ManagedDisplayMode>& mode) const {
105 const float kEpsilon = 0.0001f; 62 const float kEpsilon = 0.0001f;
106 return std::abs(scale - mode->ui_scale()) < kEpsilon; 63 return std::abs(scale - mode->ui_scale()) < kEpsilon;
107 } 64 }
108 float scale; 65 float scale;
109 }; 66 };
110 67
111 void ConvertPointFromScreenToNative(aura::WindowTreeHost* host,
112 gfx::Point* point) {
113 ::wm::ConvertPointFromScreen(host->window(), point);
114 host->ConvertPointToNativeScreen(point);
115 }
116
117 scoped_refptr<ui::ManagedDisplayMode> GetDisplayModeForUIScale(
118 const ui::ManagedDisplayInfo& info,
119 float ui_scale) {
120 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes =
121 info.display_modes();
122 auto iter = std::find_if(
123 modes.begin(), modes.end(),
124 [ui_scale](const scoped_refptr<ui::ManagedDisplayMode>& mode) {
125 return mode->ui_scale() == ui_scale;
126 });
127 if (iter == modes.end())
128 return scoped_refptr<ui::ManagedDisplayMode>();
129 return *iter;
130 }
131
132 scoped_refptr<ui::ManagedDisplayMode> FindNextMode( 68 scoped_refptr<ui::ManagedDisplayMode> FindNextMode(
133 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes, 69 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes,
134 size_t index, 70 size_t index,
135 bool up) { 71 bool up) {
136 DCHECK_LT(index, modes.size()); 72 DCHECK_LT(index, modes.size());
137 size_t new_index = index; 73 size_t new_index = index;
138 if (up && (index + 1 < modes.size())) 74 if (up && (index + 1 < modes.size()))
139 ++new_index; 75 ++new_index;
140 else if (!up && index != 0) 76 else if (!up && index != 0)
141 --new_index; 77 --new_index;
142 return modes[new_index]; 78 return modes[new_index];
143 } 79 }
144 80
145 } // namespace 81 } // namespace
146 82
147 ui::ManagedDisplayInfo::ManagedDisplayModeList 83 ui::ManagedDisplayInfo::ManagedDisplayModeList
148 CreateInternalManagedDisplayModeList( 84 CreateInternalManagedDisplayModeList(
149 const scoped_refptr<ui::ManagedDisplayMode>& native_mode) { 85 const scoped_refptr<ui::ManagedDisplayMode>& native_mode) {
150 ui::ManagedDisplayInfo::ManagedDisplayModeList display_mode_list; 86 ManagedDisplayInfo::ManagedDisplayModeList display_mode_list;
151 87
152 float native_ui_scale = (native_mode->device_scale_factor() == 1.25f) 88 float native_ui_scale = (native_mode->device_scale_factor() == 1.25f)
153 ? 1.0f 89 ? 1.0f
154 : native_mode->device_scale_factor(); 90 : native_mode->device_scale_factor();
155 for (float ui_scale : GetScalesForDisplay(native_mode)) { 91 for (float ui_scale : GetScalesForDisplay(native_mode)) {
156 scoped_refptr<ui::ManagedDisplayMode> mode(new ui::ManagedDisplayMode( 92 scoped_refptr<ManagedDisplayMode> mode(new ManagedDisplayMode(
157 native_mode->size(), native_mode->refresh_rate(), 93 native_mode->size(), native_mode->refresh_rate(),
158 native_mode->is_interlaced(), ui_scale == native_ui_scale, ui_scale, 94 native_mode->is_interlaced(), ui_scale == native_ui_scale, ui_scale,
159 native_mode->device_scale_factor())); 95 native_mode->device_scale_factor()));
160 display_mode_list.push_back(mode); 96 display_mode_list.push_back(mode);
161 } 97 }
162 return display_mode_list; 98 return display_mode_list;
163 } 99 }
164 100
165 ui::ManagedDisplayInfo::ManagedDisplayModeList 101 ui::ManagedDisplayInfo::ManagedDisplayModeList
166 CreateUnifiedManagedDisplayModeList( 102 CreateUnifiedManagedDisplayModeList(
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 LOG(WARNING) << "Unsupported resolution was requested:" 143 LOG(WARNING) << "Unsupported resolution was requested:"
208 << resolution.ToString(); 144 << resolution.ToString();
209 return scoped_refptr<ui::ManagedDisplayMode>(); 145 return scoped_refptr<ui::ManagedDisplayMode>();
210 } 146 }
211 return *iter; 147 return *iter;
212 } 148 }
213 149
214 scoped_refptr<ui::ManagedDisplayMode> GetDisplayModeForNextUIScale( 150 scoped_refptr<ui::ManagedDisplayMode> GetDisplayModeForNextUIScale(
215 const ui::ManagedDisplayInfo& info, 151 const ui::ManagedDisplayInfo& info,
216 bool up) { 152 bool up) {
217 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
218 if (!display_manager->IsActiveDisplayId(info.id()) ||
219 !display::Display::IsInternalDisplayId(info.id())) {
220 return scoped_refptr<ui::ManagedDisplayMode>();
221 }
222 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes = 153 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes =
223 info.display_modes(); 154 info.display_modes();
224 ScaleComparator comparator(info.configured_ui_scale()); 155 ScaleComparator comparator(info.configured_ui_scale());
225 auto iter = std::find_if(modes.begin(), modes.end(), comparator); 156 auto iter = std::find_if(modes.begin(), modes.end(), comparator);
226 return FindNextMode(modes, iter - modes.begin(), up); 157 return FindNextMode(modes, iter - modes.begin(), up);
227 } 158 }
228 159
229 scoped_refptr<ui::ManagedDisplayMode> GetDisplayModeForNextResolution( 160 scoped_refptr<ui::ManagedDisplayMode> GetDisplayModeForNextResolution(
230 const ui::ManagedDisplayInfo& info, 161 const ui::ManagedDisplayInfo& info,
231 bool up) { 162 bool up) {
232 if (display::Display::IsInternalDisplayId(info.id())) 163 if (display::Display::IsInternalDisplayId(info.id()))
233 return scoped_refptr<ui::ManagedDisplayMode>(); 164 return scoped_refptr<ui::ManagedDisplayMode>();
234 165
235 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes = 166 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes =
236 info.display_modes(); 167 info.display_modes();
237 scoped_refptr<ui::ManagedDisplayMode> tmp = new ui::ManagedDisplayMode( 168 scoped_refptr<ui::ManagedDisplayMode> tmp = new ui::ManagedDisplayMode(
238 info.size_in_pixel(), 0.0, false, false, 1.0, info.device_scale_factor()); 169 info.size_in_pixel(), 0.0, false, false, 1.0, info.device_scale_factor());
239 gfx::Size resolution = tmp->GetSizeInDIP(false); 170 gfx::Size resolution = tmp->GetSizeInDIP(false);
240 171
241 auto iter = std::find_if( 172 auto iter = std::find_if(
242 modes.begin(), modes.end(), 173 modes.begin(), modes.end(),
243 [resolution](const scoped_refptr<ui::ManagedDisplayMode>& mode) { 174 [resolution](const scoped_refptr<ui::ManagedDisplayMode>& mode) {
244 return mode->GetSizeInDIP(false) == resolution; 175 return mode->GetSizeInDIP(false) == resolution;
245 }); 176 });
246 return FindNextMode(modes, iter - modes.begin(), up); 177 return FindNextMode(modes, iter - modes.begin(), up);
247 } 178 }
248 179
249 bool SetDisplayUIScale(int64_t id, float ui_scale) {
250 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
251 if (!display_manager->IsActiveDisplayId(id) ||
252 !display::Display::IsInternalDisplayId(id)) {
253 return false;
254 }
255 const ui::ManagedDisplayInfo& info = display_manager->GetDisplayInfo(id);
256
257 scoped_refptr<ui::ManagedDisplayMode> mode =
258 GetDisplayModeForUIScale(info, ui_scale);
259 if (!mode)
260 return false;
261 return display_manager->SetDisplayMode(id, mode);
262 }
263
264 bool HasDisplayModeForUIScale(const ui::ManagedDisplayInfo& info, 180 bool HasDisplayModeForUIScale(const ui::ManagedDisplayInfo& info,
265 float ui_scale) { 181 float ui_scale) {
266 ScaleComparator comparator(ui_scale); 182 ScaleComparator comparator(ui_scale);
267 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes = 183 const ui::ManagedDisplayInfo::ManagedDisplayModeList& modes =
268 info.display_modes(); 184 info.display_modes();
269 return std::find_if(modes.begin(), modes.end(), comparator) != modes.end(); 185 return std::find_if(modes.begin(), modes.end(), comparator) != modes.end();
270 } 186 }
271 187
272 bool ComputeBoundary(const display::Display& a_display, 188 bool ComputeBoundary(const display::Display& a_display,
273 const display::Display& b_display, 189 const display::Display& b_display,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 } else { 244 } else {
329 a_edge_in_screen->SetRect(a_bounds.right() - 1, top, 1, bottom - top); 245 a_edge_in_screen->SetRect(a_bounds.right() - 1, top, 1, bottom - top);
330 b_edge_in_screen->SetRect(b_bounds.x(), top, 1, bottom - top); 246 b_edge_in_screen->SetRect(b_bounds.x(), top, 1, bottom - top);
331 } 247 }
332 break; 248 break;
333 } 249 }
334 } 250 }
335 return true; 251 return true;
336 } 252 }
337 253
338 gfx::Rect GetNativeEdgeBounds(AshWindowTreeHost* ash_host,
339 const gfx::Rect& bounds_in_screen) {
340 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
341 gfx::Rect native_bounds = host->GetBounds();
342 native_bounds.Inset(ash_host->GetHostInsets());
343 gfx::Point start_in_native = bounds_in_screen.origin();
344 gfx::Point end_in_native = bounds_in_screen.bottom_right();
345
346 ConvertPointFromScreenToNative(host, &start_in_native);
347 ConvertPointFromScreenToNative(host, &end_in_native);
348
349 if (std::abs(start_in_native.x() - end_in_native.x()) <
350 std::abs(start_in_native.y() - end_in_native.y())) {
351 // vertical in native
352 int x = std::abs(native_bounds.x() - start_in_native.x()) <
353 std::abs(native_bounds.right() - start_in_native.x())
354 ? native_bounds.x()
355 : native_bounds.right() - 1;
356 return gfx::Rect(x, std::min(start_in_native.y(), end_in_native.y()), 1,
357 std::abs(end_in_native.y() - start_in_native.y()));
358 } else {
359 // horizontal in native
360 int y = std::abs(native_bounds.y() - start_in_native.y()) <
361 std::abs(native_bounds.bottom() - start_in_native.y())
362 ? native_bounds.y()
363 : native_bounds.bottom() - 1;
364 return gfx::Rect(std::min(start_in_native.x(), end_in_native.x()), y,
365 std::abs(end_in_native.x() - start_in_native.x()), 1);
366 }
367 }
368
369 // Moves the cursor to the point inside the root that is closest to
370 // the point_in_screen, which is outside of the root window.
371 void MoveCursorTo(AshWindowTreeHost* ash_host,
372 const gfx::Point& point_in_screen,
373 bool update_last_location_now) {
374 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
375 gfx::Point point_in_native = point_in_screen;
376 ::wm::ConvertPointFromScreen(host->window(), &point_in_native);
377 host->ConvertPointToNativeScreen(&point_in_native);
378
379 // now fit the point inside the native bounds.
380 gfx::Rect native_bounds = host->GetBounds();
381 gfx::Point native_origin = native_bounds.origin();
382 native_bounds.Inset(ash_host->GetHostInsets());
383 // Shrink further so that the mouse doesn't warp on the
384 // edge. The right/bottom needs to be shrink by 2 to subtract
385 // the 1 px from width/height value.
386 native_bounds.Inset(1, 1, 2, 2);
387
388 // Ensure that |point_in_native| is inside the |native_bounds|.
389 point_in_native.SetToMax(native_bounds.origin());
390 point_in_native.SetToMin(native_bounds.bottom_right());
391
392 gfx::Point point_in_host = point_in_native;
393
394 point_in_host.Offset(-native_origin.x(), -native_origin.y());
395 host->MoveCursorToHostLocation(point_in_host);
396
397 if (update_last_location_now) {
398 gfx::Point new_point_in_screen;
399 if (Shell::GetInstance()->display_manager()->IsInUnifiedMode()) {
400 new_point_in_screen = point_in_host;
401 // First convert to the unified host.
402 host->ConvertPointFromHost(&new_point_in_screen);
403 // Then convert to the unified screen.
404 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointFromHost(
405 &new_point_in_screen);
406 } else {
407 new_point_in_screen = point_in_native;
408 host->ConvertPointFromNativeScreen(&new_point_in_screen);
409 ::wm::ConvertPointToScreen(host->window(), &new_point_in_screen);
410 }
411 aura::Env::GetInstance()->set_last_mouse_location(new_point_in_screen);
412 }
413 }
414
415 int FindDisplayIndexContainingPoint( 254 int FindDisplayIndexContainingPoint(
416 const std::vector<display::Display>& displays, 255 const std::vector<display::Display>& displays,
417 const gfx::Point& point_in_screen) { 256 const gfx::Point& point_in_screen) {
418 auto iter = std::find_if(displays.begin(), displays.end(), 257 auto iter = std::find_if(displays.begin(), displays.end(),
419 [point_in_screen](const display::Display& display) { 258 [point_in_screen](const display::Display& display) {
420 return display.bounds().Contains(point_in_screen); 259 return display.bounds().Contains(point_in_screen);
421 }); 260 });
422 return iter == displays.end() ? -1 : (iter - displays.begin()); 261 return iter == displays.end() ? -1 : (iter - displays.begin());
423 } 262 }
424 263
(...skipping 22 matching lines...) Expand all
447 DCHECK_NE(id1, id2); 286 DCHECK_NE(id1, id2);
448 // Output index is stored in the first 8 bits. See GetDisplayIdFromEDID 287 // Output index is stored in the first 8 bits. See GetDisplayIdFromEDID
449 // in edid_parser.cc. 288 // in edid_parser.cc.
450 int index_1 = id1 & 0xFF; 289 int index_1 = id1 & 0xFF;
451 int index_2 = id2 & 0xFF; 290 int index_2 = id2 & 0xFF;
452 DCHECK_NE(index_1, index_2) << id1 << " and " << id2; 291 DCHECK_NE(index_1, index_2) << id1 << " and " << id2;
453 return display::Display::IsInternalDisplayId(id1) || 292 return display::Display::IsInternalDisplayId(id1) ||
454 (index_1 < index_2 && !display::Display::IsInternalDisplayId(id2)); 293 (index_1 < index_2 && !display::Display::IsInternalDisplayId(id2));
455 } 294 }
456 295
457 #if defined(OS_CHROMEOS) 296 } // namespace ui
458 void ShowDisplayErrorNotification(int message_id) {
459 // Always remove the notification to make sure the notification appears
460 // as a popup in any situation.
461 message_center::MessageCenter::Get()->RemoveNotification(
462 kDisplayErrorNotificationId, false /* by_user */);
463
464 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
465 std::unique_ptr<message_center::Notification> notification(
466 new message_center::Notification(
467 message_center::NOTIFICATION_TYPE_SIMPLE, kDisplayErrorNotificationId,
468 base::string16(), // title
469 l10n_util::GetStringUTF16(message_id),
470 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
471 base::string16(), // display_source
472 GURL(), message_center::NotifierId(
473 message_center::NotifierId::SYSTEM_COMPONENT,
474 system_notifier::kNotifierDisplayError),
475 message_center::RichNotificationData(),
476 new DisplayErrorNotificationDelegate));
477 message_center::MessageCenter::Get()->AddNotification(
478 std::move(notification));
479 }
480 #endif
481
482 base::string16 GetDisplayErrorNotificationMessageForTest() {
483 message_center::NotificationList::Notifications notifications =
484 message_center::MessageCenter::Get()->GetVisibleNotifications();
485 for (auto* const notification : notifications) {
486 if (notification->id() == kDisplayErrorNotificationId)
487 return notification->message();
488 }
489 return base::string16();
490 }
491
492 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698