| OLD | NEW |
| 1 // Copyright 2016 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 "ui/display/display_list.h" | 5 #include "ui/display/display_list.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "ui/display/display_observer.h" | 8 #include "ui/display/display_observer.h" |
| 9 | 9 |
| 10 namespace display { | 10 namespace display { |
| 11 | 11 |
| 12 DisplayListObserverLock::~DisplayListObserverLock() { | 12 DisplayListObserverLock::~DisplayListObserverLock() { |
| 13 display_list_->DecrementObserverSuspendLockCount(); | 13 display_list_->DecrementObserverSuspendLockCount(); |
| 14 } | 14 } |
| 15 | 15 |
| 16 DisplayListObserverLock::DisplayListObserverLock(DisplayList* display_list) | 16 DisplayListObserverLock::DisplayListObserverLock(DisplayList* display_list) |
| 17 : display_list_(display_list) { | 17 : display_list_(display_list) { |
| 18 display_list_->IncrementObserverSuspendLockCount(); | 18 display_list_->IncrementObserverSuspendLockCount(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 DisplayList::DisplayList() {} | 21 DisplayList::DisplayList() {} |
| 22 | 22 |
| 23 DisplayList::~DisplayList() { | 23 DisplayList::~DisplayList() { |
| 24 DCHECK_EQ(0, observer_suspend_lock_count_); | 24 DCHECK_EQ(0, observer_suspend_lock_count_); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void DisplayList::AddObserver(display::DisplayObserver* observer) { | 27 void DisplayList::AddObserver(DisplayObserver* observer) { |
| 28 observers_.AddObserver(observer); | 28 observers_.AddObserver(observer); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void DisplayList::RemoveObserver(display::DisplayObserver* observer) { | 31 void DisplayList::RemoveObserver(DisplayObserver* observer) { |
| 32 observers_.RemoveObserver(observer); | 32 observers_.RemoveObserver(observer); |
| 33 } | 33 } |
| 34 | 34 |
| 35 DisplayList::Displays::const_iterator DisplayList::FindDisplayById( | 35 DisplayList::Displays::const_iterator DisplayList::FindDisplayById( |
| 36 int64_t id) const { | 36 int64_t id) const { |
| 37 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { | 37 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { |
| 38 if (iter->id() == id) | 38 if (iter->id() == id) |
| 39 return iter; | 39 return iter; |
| 40 } | 40 } |
| 41 return displays_.end(); | 41 return displays_.end(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DisplayList::Displays::const_iterator DisplayList::GetPrimaryDisplayIterator() | 44 DisplayList::Displays::const_iterator DisplayList::GetPrimaryDisplayIterator() |
| 45 const { | 45 const { |
| 46 return primary_display_index_ == -1 | 46 return primary_display_index_ == -1 |
| 47 ? displays_.end() | 47 ? displays_.end() |
| 48 : displays_.begin() + primary_display_index_; | 48 : displays_.begin() + primary_display_index_; |
| 49 } | 49 } |
| 50 | 50 |
| 51 std::unique_ptr<DisplayListObserverLock> DisplayList::SuspendObserverUpdates() { | 51 std::unique_ptr<DisplayListObserverLock> DisplayList::SuspendObserverUpdates() { |
| 52 return base::WrapUnique(new DisplayListObserverLock(this)); | 52 return base::WrapUnique(new DisplayListObserverLock(this)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void DisplayList::UpdateDisplay(const display::Display& display) { | 55 void DisplayList::UpdateDisplay(const Display& display) { |
| 56 UpdateDisplay(display, GetTypeByDisplayId(display.id())); | 56 UpdateDisplay(display, GetTypeByDisplayId(display.id())); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void DisplayList::UpdateDisplay(const display::Display& display, Type type) { | 59 void DisplayList::UpdateDisplay(const Display& display, Type type) { |
| 60 auto iter = FindDisplayByIdInternal(display.id()); | 60 auto iter = FindDisplayByIdInternal(display.id()); |
| 61 DCHECK(iter != displays_.end()); | 61 DCHECK(iter != displays_.end()); |
| 62 | 62 |
| 63 display::Display* local_display = &(*iter); | 63 Display* local_display = &(*iter); |
| 64 uint32_t changed_values = 0; | 64 uint32_t changed_values = 0; |
| 65 if (type == Type::PRIMARY && | 65 if (type == Type::PRIMARY && |
| 66 static_cast<int>(iter - displays_.begin()) != | 66 static_cast<int>(iter - displays_.begin()) != |
| 67 static_cast<int>(GetPrimaryDisplayIterator() - displays_.begin())) { | 67 static_cast<int>(GetPrimaryDisplayIterator() - displays_.begin())) { |
| 68 primary_display_index_ = static_cast<int>(iter - displays_.begin()); | 68 primary_display_index_ = static_cast<int>(iter - displays_.begin()); |
| 69 // ash::DisplayManager only notifies for the Display gaining primary, not | 69 // ash::DisplayManager only notifies for the Display gaining primary, not |
| 70 // the one losing it. | 70 // the one losing it. |
| 71 changed_values |= display::DisplayObserver::DISPLAY_METRIC_PRIMARY; | 71 changed_values |= DisplayObserver::DISPLAY_METRIC_PRIMARY; |
| 72 } | 72 } |
| 73 if (local_display->bounds() != display.bounds()) { | 73 if (local_display->bounds() != display.bounds()) { |
| 74 local_display->set_bounds(display.bounds()); | 74 local_display->set_bounds(display.bounds()); |
| 75 changed_values |= display::DisplayObserver::DISPLAY_METRIC_BOUNDS; | 75 changed_values |= DisplayObserver::DISPLAY_METRIC_BOUNDS; |
| 76 } | 76 } |
| 77 if (local_display->work_area() != display.work_area()) { | 77 if (local_display->work_area() != display.work_area()) { |
| 78 local_display->set_work_area(display.work_area()); | 78 local_display->set_work_area(display.work_area()); |
| 79 changed_values |= display::DisplayObserver::DISPLAY_METRIC_WORK_AREA; | 79 changed_values |= DisplayObserver::DISPLAY_METRIC_WORK_AREA; |
| 80 } | 80 } |
| 81 if (local_display->rotation() != display.rotation()) { | 81 if (local_display->rotation() != display.rotation()) { |
| 82 local_display->set_rotation(display.rotation()); | 82 local_display->set_rotation(display.rotation()); |
| 83 changed_values |= display::DisplayObserver::DISPLAY_METRIC_ROTATION; | 83 changed_values |= DisplayObserver::DISPLAY_METRIC_ROTATION; |
| 84 } | 84 } |
| 85 if (local_display->device_scale_factor() != display.device_scale_factor()) { | 85 if (local_display->device_scale_factor() != display.device_scale_factor()) { |
| 86 local_display->set_device_scale_factor(display.device_scale_factor()); | 86 local_display->set_device_scale_factor(display.device_scale_factor()); |
| 87 changed_values |= | 87 changed_values |= DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR; |
| 88 display::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR; | |
| 89 } | 88 } |
| 90 if (should_notify_observers()) { | 89 if (should_notify_observers()) { |
| 91 for (display::DisplayObserver& observer : observers_) | 90 for (DisplayObserver& observer : observers_) |
| 92 observer.OnDisplayMetricsChanged(*local_display, changed_values); | 91 observer.OnDisplayMetricsChanged(*local_display, changed_values); |
| 93 } | 92 } |
| 94 } | 93 } |
| 95 | 94 |
| 96 void DisplayList::AddDisplay(const display::Display& display, Type type) { | 95 void DisplayList::AddDisplay(const Display& display, Type type) { |
| 97 DCHECK(displays_.end() == FindDisplayByIdInternal(display.id())); | 96 DCHECK(displays_.end() == FindDisplayByIdInternal(display.id())); |
| 98 displays_.push_back(display); | 97 displays_.push_back(display); |
| 99 if (type == Type::PRIMARY) | 98 if (type == Type::PRIMARY) |
| 100 primary_display_index_ = static_cast<int>(displays_.size()) - 1; | 99 primary_display_index_ = static_cast<int>(displays_.size()) - 1; |
| 101 if (should_notify_observers()) { | 100 if (should_notify_observers()) { |
| 102 for (display::DisplayObserver& observer : observers_) | 101 for (DisplayObserver& observer : observers_) |
| 103 observer.OnDisplayAdded(display); | 102 observer.OnDisplayAdded(display); |
| 104 } | 103 } |
| 105 } | 104 } |
| 106 | 105 |
| 107 void DisplayList::RemoveDisplay(int64_t id) { | 106 void DisplayList::RemoveDisplay(int64_t id) { |
| 108 auto iter = FindDisplayByIdInternal(id); | 107 auto iter = FindDisplayByIdInternal(id); |
| 109 DCHECK(displays_.end() != iter); | 108 DCHECK(displays_.end() != iter); |
| 110 if (primary_display_index_ == static_cast<int>(iter - displays_.begin())) { | 109 if (primary_display_index_ == static_cast<int>(iter - displays_.begin())) { |
| 111 // The primary display can only be removed if it is the last display. | 110 // The primary display can only be removed if it is the last display. |
| 112 // Users must choose a new primary before removing an old primary display. | 111 // Users must choose a new primary before removing an old primary display. |
| 113 DCHECK_EQ(1u, displays_.size()); | 112 DCHECK_EQ(1u, displays_.size()); |
| 114 primary_display_index_ = -1; | 113 primary_display_index_ = -1; |
| 115 } else if (primary_display_index_ > | 114 } else if (primary_display_index_ > |
| 116 static_cast<int>(iter - displays_.begin())) { | 115 static_cast<int>(iter - displays_.begin())) { |
| 117 primary_display_index_--; | 116 primary_display_index_--; |
| 118 } | 117 } |
| 119 const display::Display display = *iter; | 118 const Display display = *iter; |
| 120 displays_.erase(iter); | 119 displays_.erase(iter); |
| 121 if (should_notify_observers()) { | 120 if (should_notify_observers()) { |
| 122 for (display::DisplayObserver& observer : observers_) | 121 for (DisplayObserver& observer : observers_) |
| 123 observer.OnDisplayRemoved(display); | 122 observer.OnDisplayRemoved(display); |
| 124 } | 123 } |
| 125 } | 124 } |
| 126 | 125 |
| 127 void DisplayList::IncrementObserverSuspendLockCount() { | 126 void DisplayList::IncrementObserverSuspendLockCount() { |
| 128 observer_suspend_lock_count_++; | 127 observer_suspend_lock_count_++; |
| 129 } | 128 } |
| 130 | 129 |
| 131 void DisplayList::DecrementObserverSuspendLockCount() { | 130 void DisplayList::DecrementObserverSuspendLockCount() { |
| 132 DCHECK_GT(observer_suspend_lock_count_, 0); | 131 DCHECK_GT(observer_suspend_lock_count_, 0); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 144 DisplayList::Displays::iterator DisplayList::FindDisplayByIdInternal( | 143 DisplayList::Displays::iterator DisplayList::FindDisplayByIdInternal( |
| 145 int64_t id) { | 144 int64_t id) { |
| 146 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { | 145 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { |
| 147 if (iter->id() == id) | 146 if (iter->id() == id) |
| 148 return iter; | 147 return iter; |
| 149 } | 148 } |
| 150 return displays_.end(); | 149 return displays_.end(); |
| 151 } | 150 } |
| 152 | 151 |
| 153 } // namespace display | 152 } // namespace display |
| OLD | NEW |