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

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

Issue 259253002: Add OnDisplayMetricsChanged in DisplayObserver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@update_orientation
Patch Set: jochen comments (inc. git cl format) Created 6 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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_manager.h" 5 #include "ash/display/display_manager.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "ash/ash_switches.h" 12 #include "ash/ash_switches.h"
13 #include "ash/display/display_layout_store.h" 13 #include "ash/display/display_layout_store.h"
14 #include "ash/display/screen_ash.h" 14 #include "ash/display/screen_ash.h"
15 #include "ash/screen_util.h" 15 #include "ash/screen_util.h"
16 #include "ash/shell.h" 16 #include "ash/shell.h"
17 #include "base/auto_reset.h" 17 #include "base/auto_reset.h"
18 #include "base/command_line.h" 18 #include "base/command_line.h"
19 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/metrics/histogram.h" 20 #include "base/metrics/histogram.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h" 22 #include "base/strings/string_split.h"
23 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
24 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "grit/ash_strings.h" 25 #include "grit/ash_strings.h"
26 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/layout.h" 27 #include "ui/base/layout.h"
28 #include "ui/base/resource/resource_bundle.h" 28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/display.h" 29 #include "ui/gfx/display.h"
30 #include "ui/gfx/display_observer.h"
30 #include "ui/gfx/rect.h" 31 #include "ui/gfx/rect.h"
31 #include "ui/gfx/screen.h" 32 #include "ui/gfx/screen.h"
32 #include "ui/gfx/size_conversions.h" 33 #include "ui/gfx/size_conversions.h"
33 34
34 #if defined(USE_X11) 35 #if defined(USE_X11)
35 #include "ui/base/x/x11_util.h" 36 #include "ui/base/x/x11_util.h"
36 #endif 37 #endif
37 38
38 #if defined(OS_CHROMEOS) 39 #if defined(OS_CHROMEOS)
39 #include "ash/display/display_configurator_animation.h" 40 #include "ash/display/display_configurator_animation.h"
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 delegate_->PreDisplayConfigurationChange(false); 341 delegate_->PreDisplayConfigurationChange(false);
341 // PreDisplayConfigurationChange(false); 342 // PreDisplayConfigurationChange(false);
342 // TODO(oshima): Call UpdateDisplays instead. 343 // TODO(oshima): Call UpdateDisplays instead.
343 const DisplayLayout layout = GetCurrentDisplayLayout(); 344 const DisplayLayout layout = GetCurrentDisplayLayout();
344 UpdateDisplayBoundsForLayoutById( 345 UpdateDisplayBoundsForLayoutById(
345 layout, primary, 346 layout, primary,
346 ScreenUtil::GetSecondaryDisplay().id()); 347 ScreenUtil::GetSecondaryDisplay().id());
347 348
348 // Primary's bounds stay the same. Just notify bounds change 349 // Primary's bounds stay the same. Just notify bounds change
349 // on the secondary. 350 // on the secondary.
350 screen_ash_->NotifyBoundsChanged( 351 screen_ash_->NotifyMetricsChanged(
351 ScreenUtil::GetSecondaryDisplay()); 352 ScreenUtil::GetSecondaryDisplay(),
353 gfx::DisplayObserver::DISPLAY_METRICS_BOUNDS |
354 gfx::DisplayObserver::DISPLAY_METRICS_WORK_AREA);
352 if (delegate_) 355 if (delegate_)
353 delegate_->PostDisplayConfigurationChange(); 356 delegate_->PostDisplayConfigurationChange();
354 } 357 }
355 } 358 }
356 359
357 const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const { 360 const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const {
358 gfx::Display* display = 361 gfx::Display* display =
359 const_cast<DisplayManager*>(this)->FindDisplayForId(id); 362 const_cast<DisplayManager*>(this)->FindDisplayForId(id);
360 return display ? *display : GetInvalidDisplay(); 363 return display ? *display : GetInvalidDisplay();
361 } 364 }
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 ": Multiple display test does not work on Windows bots. Please " 639 ": Multiple display test does not work on Windows bots. Please "
637 "skip (don't disable) the test using SupportsMultipleDisplays()"; 640 "skip (don't disable) the test using SupportsMultipleDisplays()";
638 #endif 641 #endif
639 642
640 DisplayInfoList new_display_info_list = updated_display_info_list; 643 DisplayInfoList new_display_info_list = updated_display_info_list;
641 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor()); 644 std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor());
642 std::sort(new_display_info_list.begin(), 645 std::sort(new_display_info_list.begin(),
643 new_display_info_list.end(), 646 new_display_info_list.end(),
644 DisplayInfoSortFunctor()); 647 DisplayInfoSortFunctor());
645 DisplayList removed_displays; 648 DisplayList removed_displays;
646 std::vector<size_t> changed_display_indices; 649 std::map<size_t, gfx::DisplayObserver::DisplayMetrics> display_changes;
647 std::vector<size_t> added_display_indices; 650 std::vector<size_t> added_display_indices;
648 651
649 DisplayList::iterator curr_iter = displays_.begin(); 652 DisplayList::iterator curr_iter = displays_.begin();
650 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin(); 653 DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin();
651 654
652 DisplayList new_displays; 655 DisplayList new_displays;
653 656
654 // Use the internal display or 1st as the mirror source, then scale 657 // Use the internal display or 1st as the mirror source, then scale
655 // the root window so that it matches the external display's 658 // the root window so that it matches the external display's
656 // resolution. This is necessary in order for scaling to work while 659 // resolution. This is necessary in order for scaling to work while
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 } else if (curr_iter->id() == new_info_iter->id()) { 709 } else if (curr_iter->id() == new_info_iter->id()) {
707 const gfx::Display& current_display = *curr_iter; 710 const gfx::Display& current_display = *curr_iter;
708 // Copy the info because |CreateDisplayFromInfo| updates the instance. 711 // Copy the info because |CreateDisplayFromInfo| updates the instance.
709 const DisplayInfo current_display_info = 712 const DisplayInfo current_display_info =
710 GetDisplayInfo(current_display.id()); 713 GetDisplayInfo(current_display.id());
711 InsertAndUpdateDisplayInfo(*new_info_iter); 714 InsertAndUpdateDisplayInfo(*new_info_iter);
712 gfx::Display new_display = 715 gfx::Display new_display =
713 CreateDisplayFromDisplayInfoById(new_info_iter->id()); 716 CreateDisplayFromDisplayInfoById(new_info_iter->id());
714 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id()); 717 const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id());
715 718
716 bool host_window_bounds_changed = 719 gfx::DisplayObserver::DisplayMetrics metrics =
oshima 2014/05/14 15:53:34 uint32
717 current_display_info.bounds_in_native() != 720 gfx::DisplayObserver::DISPLAY_METRICS_NONE;
718 new_display_info.bounds_in_native();
719 721
720 if (force_bounds_changed_ || 722 // At that point the new Display objects we have are not entirely updated,
721 host_window_bounds_changed || 723 // they are missing the translation related to the Display disposition in
722 (current_display.device_scale_factor() != 724 // the layout.
723 new_display.device_scale_factor()) || 725 // Using display.bounds() and display.work_area() would fail most of the
726 // time.
727 if (force_bounds_changed_ || (current_display_info.bounds_in_native() !=
728 new_display_info.bounds_in_native()) ||
724 (current_display_info.size_in_pixel() != 729 (current_display_info.size_in_pixel() !=
725 new_display.GetSizeInPixel()) || 730 new_display.GetSizeInPixel())) {
726 (current_display.rotation() != new_display.rotation())) { 731 metrics |= gfx::DisplayObserver::DISPLAY_METRICS_BOUNDS |
727 changed_display_indices.push_back(new_displays.size()); 732 gfx::DisplayObserver::DISPLAY_METRICS_WORK_AREA;
733 }
734
735 if (current_display.device_scale_factor() !=
736 new_display.device_scale_factor()) {
737 metrics |= gfx::DisplayObserver::DISPLAY_METRICS_DEVICE_SCALE_FACTOR;
738 }
739
740 if (current_display.rotation() != new_display.rotation())
741 metrics |= gfx::DisplayObserver::DISPLAY_METRICS_ROTATION;
742
743 if (metrics != gfx::DisplayObserver::DISPLAY_METRICS_NONE) {
744 display_changes.insert(
745 std::pair<size_t, gfx::DisplayObserver::DisplayMetrics>(
746 new_displays.size(), metrics));
728 } 747 }
729 748
730 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets()); 749 new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
731 new_displays.push_back(new_display); 750 new_displays.push_back(new_display);
732 ++curr_iter; 751 ++curr_iter;
733 ++new_info_iter; 752 ++new_info_iter;
734 } else if (curr_iter->id() < new_info_iter->id()) { 753 } else if (curr_iter->id() < new_info_iter->id()) {
735 // more displays in current list between ids, which means it is deleted. 754 // more displays in current list between ids, which means it is deleted.
736 removed_displays.push_back(*curr_iter); 755 removed_displays.push_back(*curr_iter);
737 ++curr_iter; 756 ++curr_iter;
738 } else { 757 } else {
739 // more displays in new list between ids, which means it is added. 758 // more displays in new list between ids, which means it is added.
740 added_display_indices.push_back(new_displays.size()); 759 added_display_indices.push_back(new_displays.size());
741 InsertAndUpdateDisplayInfo(*new_info_iter); 760 InsertAndUpdateDisplayInfo(*new_info_iter);
742 new_displays.push_back( 761 new_displays.push_back(
743 CreateDisplayFromDisplayInfoById(new_info_iter->id())); 762 CreateDisplayFromDisplayInfoById(new_info_iter->id()));
744 ++new_info_iter; 763 ++new_info_iter;
745 } 764 }
746 } 765 }
747 766
748 scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater( 767 scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater(
749 new NonDesktopDisplayUpdater(this, delegate_)); 768 new NonDesktopDisplayUpdater(this, delegate_));
750 769
751 // Do not update |displays_| if there's nothing to be updated. Without this, 770 // Do not update |displays_| if there's nothing to be updated. Without this,
752 // it will not update the display layout, which causes the bug 771 // it will not update the display layout, which causes the bug
753 // http://crbug.com/155948. 772 // http://crbug.com/155948.
754 if (changed_display_indices.empty() && added_display_indices.empty() && 773 if (display_changes.empty() && added_display_indices.empty() &&
755 removed_displays.empty()) { 774 removed_displays.empty()) {
756 return; 775 return;
757 } 776 }
758 // Clear focus if the display has been removed, but don't clear focus if 777 // Clear focus if the display has been removed, but don't clear focus if
759 // the destkop has been moved from one display to another 778 // the destkop has been moved from one display to another
760 // (mirror -> docked, docked -> single internal). 779 // (mirror -> docked, docked -> single internal).
761 bool clear_focus = 780 bool clear_focus =
762 !removed_displays.empty() && 781 !removed_displays.empty() &&
763 !(removed_displays.size() == 1 && added_display_indices.size() == 1); 782 !(removed_displays.size() == 1 && added_display_indices.size() == 1);
764 if (delegate_) 783 if (delegate_)
765 delegate_->PreDisplayConfigurationChange(clear_focus); 784 delegate_->PreDisplayConfigurationChange(clear_focus);
766 785
767 size_t updated_index; 786 size_t updated_index;
768 if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) && 787 if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) &&
769 std::find(added_display_indices.begin(), 788 std::find(added_display_indices.begin(),
770 added_display_indices.end(), 789 added_display_indices.end(),
771 updated_index) == added_display_indices.end() && 790 updated_index) == added_display_indices.end()) {
772 std::find(changed_display_indices.begin(), 791 gfx::DisplayObserver::DisplayMetrics metrics =
oshima 2014/05/14 15:53:34 ditto
773 changed_display_indices.end(), 792 gfx::DisplayObserver::DISPLAY_METRICS_BOUNDS |
774 updated_index) == changed_display_indices.end()) { 793 gfx::DisplayObserver::DISPLAY_METRICS_WORK_AREA;
775 changed_display_indices.push_back(updated_index); 794 if (display_changes.find(updated_index) != display_changes.end())
795 metrics |= display_changes[updated_index];
796
797 display_changes[updated_index] = metrics;
776 } 798 }
777 799
778 displays_ = new_displays; 800 displays_ = new_displays;
779 801
780 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false); 802 base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false);
781 803
782 // Temporarily add displays to be removed because display object 804 // Temporarily add displays to be removed because display object
783 // being removed are accessed during shutting down the root. 805 // being removed are accessed during shutting down the root.
784 displays_.insert(displays_.end(), removed_displays.begin(), 806 displays_.insert(displays_.end(), removed_displays.begin(),
785 removed_displays.end()); 807 removed_displays.end());
786 808
787 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin(); 809 for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin();
788 iter != removed_displays.rend(); ++iter) { 810 iter != removed_displays.rend(); ++iter) {
789 screen_ash_->NotifyDisplayRemoved(displays_.back()); 811 screen_ash_->NotifyDisplayRemoved(displays_.back());
790 displays_.pop_back(); 812 displays_.pop_back();
791 } 813 }
792 // Close the non desktop window here to avoid creating two compositor on 814 // Close the non desktop window here to avoid creating two compositor on
793 // one display. 815 // one display.
794 if (!non_desktop_display_updater->enabled()) 816 if (!non_desktop_display_updater->enabled())
795 non_desktop_display_updater.reset(); 817 non_desktop_display_updater.reset();
796 for (std::vector<size_t>::iterator iter = added_display_indices.begin(); 818 for (std::vector<size_t>::iterator iter = added_display_indices.begin();
797 iter != added_display_indices.end(); ++iter) { 819 iter != added_display_indices.end(); ++iter) {
798 screen_ash_->NotifyDisplayAdded(displays_[*iter]); 820 screen_ash_->NotifyDisplayAdded(displays_[*iter]);
799 } 821 }
800 // Create the non destkop window after all displays are added so that 822 // Create the non destkop window after all displays are added so that
801 // it can mirror the display newly added. This can happen when switching 823 // it can mirror the display newly added. This can happen when switching
802 // from dock mode to software mirror mode. 824 // from dock mode to software mirror mode.
803 non_desktop_display_updater.reset(); 825 non_desktop_display_updater.reset();
804 for (std::vector<size_t>::iterator iter = changed_display_indices.begin(); 826 for (std::map<size_t, gfx::DisplayObserver::DisplayMetrics>::iterator iter =
805 iter != changed_display_indices.end(); ++iter) { 827 display_changes.begin();
806 screen_ash_->NotifyBoundsChanged(displays_[*iter]); 828 iter != display_changes.end();
829 ++iter) {
830 screen_ash_->NotifyMetricsChanged(displays_[iter->first], iter->second);
807 } 831 }
808 if (delegate_) 832 if (delegate_)
809 delegate_->PostDisplayConfigurationChange(); 833 delegate_->PostDisplayConfigurationChange();
810 834
811 #if defined(USE_X11) && defined(OS_CHROMEOS) 835 #if defined(USE_X11) && defined(OS_CHROMEOS)
812 if (!changed_display_indices.empty() && base::SysInfo::IsRunningOnChromeOS()) 836 if (!display_changes.empty() && base::SysInfo::IsRunningOnChromeOS())
813 ui::ClearX11DefaultRootWindow(); 837 ui::ClearX11DefaultRootWindow();
814 #endif 838 #endif
815 } 839 }
816 840
817 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const { 841 const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const {
818 DCHECK_LT(index, displays_.size()); 842 DCHECK_LT(index, displays_.size());
819 return displays_[index]; 843 return displays_[index];
820 } 844 }
821 845
822 const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const { 846 const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 970
947 bool DisplayManager::UpdateDisplayBounds(int64 display_id, 971 bool DisplayManager::UpdateDisplayBounds(int64 display_id,
948 const gfx::Rect& new_bounds) { 972 const gfx::Rect& new_bounds) {
949 if (change_display_upon_host_resize_) { 973 if (change_display_upon_host_resize_) {
950 display_info_[display_id].SetBounds(new_bounds); 974 display_info_[display_id].SetBounds(new_bounds);
951 // Don't notify observers if the mirrored window has changed. 975 // Don't notify observers if the mirrored window has changed.
952 if (software_mirroring_enabled() && mirrored_display_id_ == display_id) 976 if (software_mirroring_enabled() && mirrored_display_id_ == display_id)
953 return false; 977 return false;
954 gfx::Display* display = FindDisplayForId(display_id); 978 gfx::Display* display = FindDisplayForId(display_id);
955 display->SetSize(display_info_[display_id].size_in_pixel()); 979 display->SetSize(display_info_[display_id].size_in_pixel());
956 screen_ash_->NotifyBoundsChanged(*display); 980 screen_ash_->NotifyMetricsChanged(
981 *display, gfx::DisplayObserver::DISPLAY_METRICS_BOUNDS);
957 return true; 982 return true;
958 } 983 }
959 return false; 984 return false;
960 } 985 }
961 986
962 void DisplayManager::CreateMirrorWindowIfAny() { 987 void DisplayManager::CreateMirrorWindowIfAny() {
963 NonDesktopDisplayUpdater updater(this, delegate_); 988 NonDesktopDisplayUpdater updater(this, delegate_);
964 } 989 }
965 990
966 void DisplayManager::CreateScreenForShutdown() const { 991 void DisplayManager::CreateScreenForShutdown() const {
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 new_secondary_origin.Offset(-secondary_bounds.width(), offset); 1140 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
1116 break; 1141 break;
1117 } 1142 }
1118 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); 1143 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
1119 secondary_display->set_bounds( 1144 secondary_display->set_bounds(
1120 gfx::Rect(new_secondary_origin, secondary_bounds.size())); 1145 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
1121 secondary_display->UpdateWorkAreaFromInsets(insets); 1146 secondary_display->UpdateWorkAreaFromInsets(insets);
1122 } 1147 }
1123 1148
1124 } // namespace ash 1149 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698