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

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

Issue 11363124: Move DisplayManager and DisplayChangeObserverX11 from aura to ash. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix rebase Created 8 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « ash/display/multi_display_manager.h ('k') | ash/display/multi_display_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/multi_display_manager.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "ash/display/display_controller.h"
11 #include "ash/shell.h"
12 #include "base/command_line.h"
13 #include "base/stl_util.h"
14 #include "base/string_split.h"
15 #include "base/stringprintf.h"
16 #include "base/utf_string_conversions.h"
17 #include "grit/ash_strings.h"
18 #include "ui/aura/aura_switches.h"
19 #include "ui/aura/client/screen_position_client.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/root_window.h"
22 #include "ui/aura/root_window_host.h"
23 #include "ui/aura/window_property.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/gfx/display.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/gfx/size_conversions.h"
29
30 #if defined(USE_X11)
31 #include "ui/base/x/x11_util.h"
32 #endif
33
34 #if defined(OS_CHROMEOS)
35 #include "base/chromeos/chromeos_version.h"
36 #include "chromeos/display/output_configurator.h"
37 #endif
38
39 #if defined(OS_WIN)
40 #include "base/win/windows_version.h"
41 #include "ui/aura/remote_root_window_host_win.h"
42 #endif
43
44 DECLARE_WINDOW_PROPERTY_TYPE(int64);
45 typedef std::vector<gfx::Display> DisplayList;
46
47 namespace ash {
48 namespace internal {
49 namespace {
50
51 struct DisplaySortFunctor {
52 bool operator()(const gfx::Display& a, const gfx::Display& b) {
53 return a.id() < b.id();
54 }
55 };
56
57 gfx::Display& GetInvalidDisplay() {
58 static gfx::Display* invalid_display = new gfx::Display();
59 return *invalid_display;
60 }
61
62 #if defined(OS_CHROMEOS)
63 int64 GetDisplayIdForOutput(XID output) {
64 uint16 manufacturer_id = 0;
65 uint32 serial_number = 0;
66 ui::GetOutputDeviceData(
67 output, &manufacturer_id, &serial_number, NULL);
68 return gfx::Display::GetID(manufacturer_id, serial_number);
69 }
70 #endif
71
72 } // namespace
73
74 using aura::RootWindow;
75 using aura::Window;
76 using std::string;
77 using std::vector;
78
79 DEFINE_WINDOW_PROPERTY_KEY(int64, kDisplayIdKey,
80 gfx::Display::kInvalidDisplayID);
81
82 MultiDisplayManager::MultiDisplayManager() :
83 internal_display_id_(gfx::Display::kInvalidDisplayID),
84 force_bounds_changed_(false) {
85 Init();
86 }
87
88 MultiDisplayManager::~MultiDisplayManager() {
89 }
90
91 // static
92 void MultiDisplayManager::CycleDisplay() {
93 MultiDisplayManager* manager = static_cast<MultiDisplayManager*>(
94 aura::Env::GetInstance()->display_manager());
95 manager->CycleDisplayImpl();
96 }
97
98 // static
99 void MultiDisplayManager::ToggleDisplayScale() {
100 MultiDisplayManager* manager = static_cast<MultiDisplayManager*>(
101 aura::Env::GetInstance()->display_manager());
102 manager->ScaleDisplayImpl();
103 }
104
105 bool MultiDisplayManager::IsActiveDisplay(const gfx::Display& display) const {
106 for (DisplayList::const_iterator iter = displays_.begin();
107 iter != displays_.end(); ++iter) {
108 if ((*iter).id() == display.id())
109 return true;
110 }
111 return false;
112 }
113
114 bool MultiDisplayManager::HasInternalDisplay() const {
115 return internal_display_id_ != gfx::Display::kInvalidDisplayID;
116 }
117
118 bool MultiDisplayManager::IsInternalDisplayId(int64 id) const {
119 return internal_display_id_ == id;
120 }
121
122 bool MultiDisplayManager::UpdateWorkAreaOfDisplayNearestWindow(
123 const aura::Window* window,
124 const gfx::Insets& insets) {
125 const RootWindow* root = window->GetRootWindow();
126 gfx::Display& display = FindDisplayForRootWindow(root);
127 gfx::Rect old_work_area = display.work_area();
128 display.UpdateWorkAreaFromInsets(insets);
129 return old_work_area != display.work_area();
130 }
131
132 const gfx::Display& MultiDisplayManager::GetDisplayForId(int64 id) const {
133 return const_cast<MultiDisplayManager*>(this)->FindDisplayForId(id);
134 }
135
136 const gfx::Display& MultiDisplayManager::FindDisplayContainingPoint(
137 const gfx::Point& point_in_screen) const {
138 for (DisplayList::const_iterator iter = displays_.begin();
139 iter != displays_.end(); ++iter) {
140 const gfx::Display& display = *iter;
141 if (display.bounds().Contains(point_in_screen))
142 return display;
143 }
144 return GetInvalidDisplay();
145 }
146
147 void MultiDisplayManager::SetOverscanInsets(int64 display_id,
148 const gfx::Insets& insets_in_dip) {
149 DisplayList displays = displays_;
150 std::map<int64, gfx::Insets>::const_iterator old_overscan =
151 overscan_mapping_.find(display_id);
152 if (old_overscan != overscan_mapping_.end()) {
153 gfx::Insets old_insets = old_overscan->second;
154 for (DisplayList::iterator iter = displays.begin();
155 iter != displays.end(); ++iter) {
156 if (iter->id() == display_id) {
157 // Undo the existing insets before applying the new insets.
158 gfx::Rect bounds = iter->bounds_in_pixel();
159 bounds.Inset(old_insets.Scale(-iter->device_scale_factor()));
160 iter->SetScaleAndBounds(iter->device_scale_factor(), bounds);
161 break;
162 }
163 }
164 }
165 overscan_mapping_[display_id] = insets_in_dip;
166 OnNativeDisplaysChanged(displays);
167 }
168
169 gfx::Insets MultiDisplayManager::GetOverscanInsets(int64 display_id) const {
170 std::map<int64, gfx::Insets>::const_iterator it =
171 overscan_mapping_.find(display_id);
172 return (it != overscan_mapping_.end()) ? it->second : gfx::Insets();
173 }
174
175 void MultiDisplayManager::OnNativeDisplaysChanged(
176 const std::vector<gfx::Display>& updated_displays) {
177 if (updated_displays.empty()) {
178 // Don't update the displays when all displays are disconnected.
179 // This happens when:
180 // - the device is idle and powerd requested to turn off all displays.
181 // - the device is suspended. (kernel turns off all displays)
182 // - the internal display's brightness is set to 0 and no external
183 // display is connected.
184 // - the internal display's brightness is 0 and external display is
185 // disconnected.
186 // The display will be updated when one of displays is turned on, and the
187 // display list will be updated correctly.
188 return;
189 }
190 DisplayList new_displays = updated_displays;
191 if (internal_display_id_ != gfx::Display::kInvalidDisplayID) {
192 bool internal_display_connected = false;
193 for (DisplayList::const_iterator iter = updated_displays.begin();
194 iter != updated_displays.end(); ++iter) {
195 if ((*iter).id() == internal_display_id_) {
196 internal_display_connected = true;
197 // Update the internal display cache.
198 internal_display_.reset(new gfx::Display);
199 *internal_display_.get() = *iter;
200 break;
201 }
202 }
203 // If the internal display wasn't connected, use the cached value.
204 if (!internal_display_connected) {
205 // Internal display may be reported as disconnect during startup time.
206 if (!internal_display_.get()) {
207 internal_display_.reset(new gfx::Display(internal_display_id_,
208 gfx::Rect(800, 600)));
209 }
210 new_displays.push_back(*internal_display_.get());
211 }
212 } else {
213 new_displays = updated_displays;
214 }
215
216 for (DisplayList::iterator iter = new_displays.begin();
217 iter != new_displays.end(); ++iter) {
218 std::map<int64, gfx::Insets>::const_iterator overscan_insets =
219 overscan_mapping_.find(iter->id());
220 if (overscan_insets != overscan_mapping_.end()) {
221 gfx::Rect bounds = iter->bounds_in_pixel();
222 bounds.Inset(overscan_insets->second.Scale(iter->device_scale_factor()));
223 iter->SetScaleAndBounds(iter->device_scale_factor(), bounds);
224 }
225 }
226
227 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor());
228 std::sort(new_displays.begin(), new_displays.end(), DisplaySortFunctor());
229 DisplayList removed_displays;
230 std::vector<size_t> changed_display_indices;
231 std::vector<size_t> added_display_indices;
232 gfx::Display current_primary;
233 if (Shell::HasInstance())
234 current_primary = Shell::GetScreen()->GetPrimaryDisplay();
235
236 for (DisplayList::iterator curr_iter = displays_.begin(),
237 new_iter = new_displays.begin();
238 curr_iter != displays_.end() || new_iter != new_displays.end();) {
239 if (curr_iter == displays_.end()) {
240 // more displays in new list.
241 added_display_indices.push_back(new_iter - new_displays.begin());
242 ++new_iter;
243 } else if (new_iter == new_displays.end()) {
244 // more displays in current list.
245 removed_displays.push_back(*curr_iter);
246 ++curr_iter;
247 } else if ((*curr_iter).id() == (*new_iter).id()) {
248 const gfx::Display& current_display = *curr_iter;
249 gfx::Display& new_display = *new_iter;
250 if (force_bounds_changed_ ||
251 current_display.bounds_in_pixel() != new_display.bounds_in_pixel() ||
252 current_display.device_scale_factor() !=
253 new_display.device_scale_factor()) {
254 changed_display_indices.push_back(new_iter - new_displays.begin());
255 }
256 // If the display is primary, then simpy set the origin to (0,0).
257 // The secondary display's bounds will be updated by
258 // |DisplayController::UpdateDisplayBoundsForLayout|, so no need
259 // to change there.
260 if ((*new_iter).id() == current_primary.id())
261 new_display.set_bounds(gfx::Rect(new_display.bounds().size()));
262
263 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
264 ++curr_iter;
265 ++new_iter;
266 } else if ((*curr_iter).id() < (*new_iter).id()) {
267 // more displays in current list between ids, which means it is deleted.
268 removed_displays.push_back(*curr_iter);
269 ++curr_iter;
270 } else {
271 // more displays in new list between ids, which means it is added.
272 added_display_indices.push_back(new_iter - new_displays.begin());
273 ++new_iter;
274 }
275 }
276
277 // Do not update |displays_| if there's nothing to be updated. Without this,
278 // it will not update the display layout, which causes the bug
279 // http://crbug.com/155948.
280 if (changed_display_indices.empty() && added_display_indices.empty() &&
281 removed_displays.empty()) {
282 return;
283 }
284
285 displays_ = new_displays;
286 RefreshDisplayNames();
287
288 // Temporarily add displays to be removed because display object
289 // being removed are accessed during shutting down the root.
290 displays_.insert(displays_.end(), removed_displays.begin(),
291 removed_displays.end());
292 for (std::vector<size_t>::iterator iter = changed_display_indices.begin();
293 iter != changed_display_indices.end(); ++iter) {
294 NotifyBoundsChanged(displays_[*iter]);
295 }
296 for (std::vector<size_t>::iterator iter = added_display_indices.begin();
297 iter != added_display_indices.end(); ++iter) {
298 NotifyDisplayAdded(displays_[*iter]);
299 }
300
301 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin();
302 iter != removed_displays.rend(); ++iter) {
303 NotifyDisplayRemoved(displays_.back());
304 displays_.pop_back();
305 }
306 EnsurePointerInDisplays();
307 }
308
309 RootWindow* MultiDisplayManager::CreateRootWindowForDisplay(
310 const gfx::Display& display) {
311
312 RootWindow::CreateParams params(display.bounds_in_pixel());
313 #if defined(OS_WIN)
314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
315 params.host = aura::RemoteRootWindowHostWin::Create(
316 display.bounds_in_pixel());
317 }
318 #endif
319 aura::RootWindow* root_window = new aura::RootWindow(params);
320 // No need to remove RootWindowObserver because
321 // the DisplayManager object outlives RootWindow objects.
322 root_window->AddRootWindowObserver(this);
323 root_window->SetProperty(kDisplayIdKey, display.id());
324 root_window->Init();
325 return root_window;
326 }
327
328 gfx::Display* MultiDisplayManager::GetDisplayAt(size_t index) {
329 return index < displays_.size() ? &displays_[index] : NULL;
330 }
331
332 size_t MultiDisplayManager::GetNumDisplays() const {
333 return displays_.size();
334 }
335
336 const gfx::Display& MultiDisplayManager::GetDisplayNearestWindow(
337 const Window* window) const {
338 if (!window)
339 return DisplayController::GetPrimaryDisplay();
340 const RootWindow* root = window->GetRootWindow();
341 MultiDisplayManager* manager = const_cast<MultiDisplayManager*>(this);
342 return root ?
343 manager->FindDisplayForRootWindow(root) :
344 DisplayController::GetPrimaryDisplay();
345 }
346
347 const gfx::Display& MultiDisplayManager::GetDisplayNearestPoint(
348 const gfx::Point& point) const {
349 // Fallback to the primary display if there is no root display containing
350 // the |point|.
351 const gfx::Display& display = FindDisplayContainingPoint(point);
352 return display.is_valid() ? display : DisplayController::GetPrimaryDisplay();
353 }
354
355 const gfx::Display& MultiDisplayManager::GetDisplayMatching(
356 const gfx::Rect& rect) const {
357 if (rect.IsEmpty())
358 return GetDisplayNearestPoint(rect.origin());
359
360 int max = 0;
361 const gfx::Display* matching = 0;
362 for (std::vector<gfx::Display>::const_iterator iter = displays_.begin();
363 iter != displays_.end(); ++iter) {
364 const gfx::Display& display = *iter;
365 gfx::Rect intersect = gfx::IntersectRects(display.bounds(), rect);
366 int area = intersect.width() * intersect.height();
367 if (area > max) {
368 max = area;
369 matching = &(*iter);
370 }
371 }
372 // Fallback to the primary display if there is no matching display.
373 return matching ? *matching : DisplayController::GetPrimaryDisplay();
374 }
375
376 std::string MultiDisplayManager::GetDisplayNameFor(
377 const gfx::Display& display) {
378 if (!display.is_valid())
379 return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
380
381 std::map<int64, std::string>::const_iterator iter =
382 display_names_.find(display.id());
383 if (iter != display_names_.end())
384 return iter->second;
385
386 return base::StringPrintf("Display %d", static_cast<int>(display.id()));
387 }
388
389 void MultiDisplayManager::OnRootWindowResized(const aura::RootWindow* root,
390 const gfx::Size& old_size) {
391 if (!use_fullscreen_host_window()) {
392 gfx::Display& display = FindDisplayForRootWindow(root);
393 if (display.size() != root->GetHostSize()) {
394 display.SetSize(root->GetHostSize());
395 NotifyBoundsChanged(display);
396 }
397 }
398 }
399
400 void MultiDisplayManager::Init() {
401 #if defined(OS_CHROMEOS)
402 if (base::chromeos::IsRunningOnChromeOS()) {
403 std::vector<XID> outputs;
404 ui::GetOutputDeviceHandles(&outputs);
405 std::vector<std::string> output_names = ui::GetOutputNames(outputs);
406 for (size_t i = 0; i < output_names.size(); ++i) {
407 if (chromeos::OutputConfigurator::IsInternalOutputName(
408 output_names[i])) {
409 internal_display_id_ = GetDisplayIdForOutput(outputs[i]);
410 break;
411 }
412 }
413 }
414 #endif
415
416 RefreshDisplayNames();
417
418 #if defined(OS_WIN)
419 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
420 set_use_fullscreen_host_window(true);
421 #endif
422 // TODO(oshima): Move this logic to DisplayChangeObserver.
423 const string size_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
424 switches::kAuraHostWindowSize);
425 vector<string> parts;
426 base::SplitString(size_str, ',', &parts);
427 for (vector<string>::const_iterator iter = parts.begin();
428 iter != parts.end(); ++iter) {
429 AddDisplayFromSpec(*iter);
430 }
431 if (displays_.empty())
432 AddDisplayFromSpec(std::string() /* default */);
433 }
434
435 void MultiDisplayManager::CycleDisplayImpl() {
436 DCHECK(!displays_.empty());
437 std::vector<gfx::Display> new_displays;
438 new_displays.push_back(DisplayController::GetPrimaryDisplay());
439 // Add if there is only one display.
440 if (displays_.size() == 1)
441 new_displays.push_back(CreateDisplayFromSpec("100+200-500x400"));
442 OnNativeDisplaysChanged(new_displays);
443 }
444
445 void MultiDisplayManager::ScaleDisplayImpl() {
446 DCHECK(!displays_.empty());
447 std::vector<gfx::Display> new_displays;
448 for (DisplayList::const_iterator iter = displays_.begin();
449 iter != displays_.end(); ++iter) {
450 gfx::Display display = *iter;
451 float factor = display.device_scale_factor() == 1.0f ? 2.0f : 1.0f;
452 gfx::Point display_origin = display.bounds_in_pixel().origin();
453 gfx::Size display_size = gfx::ToFlooredSize(
454 gfx::ScaleSize(display.size(), factor));
455 display.SetScaleAndBounds(factor, gfx::Rect(display_origin, display_size));
456 new_displays.push_back(display);
457 }
458 OnNativeDisplaysChanged(new_displays);
459 }
460
461 gfx::Display& MultiDisplayManager::FindDisplayForRootWindow(
462 const aura::RootWindow* root_window) {
463 int64 id = root_window->GetProperty(kDisplayIdKey);
464 // if id is |kInvaildDisplayID|, it's being deleted.
465 DCHECK(id != gfx::Display::kInvalidDisplayID);
466 gfx::Display& display = FindDisplayForId(id);
467 DCHECK(display.is_valid());
468 return display;
469 }
470
471 gfx::Display& MultiDisplayManager::FindDisplayForId(int64 id) {
472 for (DisplayList::iterator iter = displays_.begin();
473 iter != displays_.end(); ++iter) {
474 if ((*iter).id() == id)
475 return *iter;
476 }
477 DLOG(WARNING) << "Could not find display:" << id;
478 return GetInvalidDisplay();
479 }
480
481 void MultiDisplayManager::AddDisplayFromSpec(const std::string& spec) {
482 gfx::Display display = CreateDisplayFromSpec(spec);
483
484 const gfx::Insets insets = display.GetWorkAreaInsets();
485 const gfx::Rect& native_bounds = display.bounds_in_pixel();
486 display.SetScaleAndBounds(display.device_scale_factor(), native_bounds);
487 display.UpdateWorkAreaFromInsets(insets);
488 displays_.push_back(display);
489 }
490
491 int64 MultiDisplayManager::SetFirstDisplayAsInternalDisplayForTest() {
492 internal_display_id_ = displays_[0].id();
493 internal_display_.reset(new gfx::Display);
494 *internal_display_ = displays_[0];
495 return internal_display_id_;
496 }
497
498 void MultiDisplayManager::EnsurePointerInDisplays() {
499 // Don't try to move the pointer during the boot/startup.
500 if (!Shell::HasInstance())
501 return;
502 gfx::Point location_in_screen = Shell::GetScreen()->GetCursorScreenPoint();
503 gfx::Point target_location;
504 int64 closest_distance_squared = -1;
505
506 for (DisplayList::const_iterator iter = displays_.begin();
507 iter != displays_.end(); ++iter) {
508 const gfx::Rect& display_bounds = iter->bounds();
509
510 if (display_bounds.Contains(location_in_screen)) {
511 target_location = location_in_screen;
512 break;
513 }
514 gfx::Point center = display_bounds.CenterPoint();
515 // Use the distance squared from the center of the dislay. This is not
516 // exactly "closest" display, but good enough to pick one
517 // appropriate (and there are at most two displays).
518 // We don't care about actual distance, only relative to other displays, so
519 // using the LengthSquared() is cheaper than Length().
520 int64 distance_squared = (center - location_in_screen).LengthSquared();
521 if (closest_distance_squared < 0 ||
522 closest_distance_squared > distance_squared) {
523 target_location = center;
524 closest_distance_squared = distance_squared;
525 }
526 }
527
528 aura::RootWindow* root_window = Shell::GetPrimaryRootWindow();
529 aura::client::ScreenPositionClient* client =
530 aura::client::GetScreenPositionClient(root_window);
531 client->ConvertPointFromScreen(root_window, &target_location);
532
533 root_window->MoveCursorTo(target_location);
534 }
535
536 void MultiDisplayManager::RefreshDisplayNames() {
537 display_names_.clear();
538
539 #if defined(OS_CHROMEOS)
540 if (!base::chromeos::IsRunningOnChromeOS())
541 return;
542 #endif
543
544 #if defined(USE_X11)
545 std::vector<XID> outputs;
546 if (!ui::GetOutputDeviceHandles(&outputs))
547 return;
548
549 for (size_t i = 0; i < outputs.size(); ++i) {
550 uint16 manufacturer_id = 0;
551 uint32 serial_number = 0;
552 std::string name;
553 if (ui::GetOutputDeviceData(
554 outputs[i], &manufacturer_id, &serial_number, &name)) {
555 int64 id = gfx::Display::GetID(manufacturer_id, serial_number);
556 if (IsInternalDisplayId(id)) {
557 display_names_[id] =
558 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME);
559 } else {
560 display_names_[id] = name;
561 }
562 }
563 }
564 #endif
565 }
566
567 void MultiDisplayManager::SetDisplayIdsForTest(DisplayList* to_update) const {
568 DisplayList::iterator iter_to_update = to_update->begin();
569 DisplayList::const_iterator iter = displays_.begin();
570 for (; iter != displays_.end() && iter_to_update != to_update->end();
571 ++iter, ++iter_to_update) {
572 (*iter_to_update).set_id((*iter).id());
573 }
574 }
575
576 } // namespace internal
577 } // namespace ash
OLDNEW
« no previous file with comments | « ash/display/multi_display_manager.h ('k') | ash/display/multi_display_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698