| 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 "services/ui/display/platform_screen_impl_ozone.h" | 5 #include "services/ui/display/platform_screen_impl_ozone.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } else { | 57 } else { |
| 58 display_configurator_.AddVirtualDisplay(gfx::Size(1024, 768)); | 58 display_configurator_.AddVirtualDisplay(gfx::Size(1024, 768)); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 int64_t PlatformScreenImplOzone::GetPrimaryDisplayId() const { | 63 int64_t PlatformScreenImplOzone::GetPrimaryDisplayId() const { |
| 64 return primary_display_id_; | 64 return primary_display_id_; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void PlatformScreenImplOzone::OnDisplayModeChanged( | 67 void PlatformScreenImplOzone::ProcessRemovedDisplays( |
| 68 const ui::DisplayConfigurator::DisplayStateList& displays) { | 68 const ui::DisplayConfigurator::DisplayStateList& snapshots) { |
| 69 if (displays.size() > 1) { | 69 std::vector<int64_t> current_ids; |
| 70 LOG(ERROR) | 70 for (ui::DisplaySnapshot* snapshot : snapshots) |
| 71 << "Mus doesn't really support multiple displays, expect it to crash"; | 71 current_ids.push_back(snapshot->display_id()); |
| 72 |
| 73 // Find cached displays with no matching snapshot and mark as removed. |
| 74 for (DisplayInfo& display : cached_displays_) { |
| 75 if (std::find(current_ids.begin(), current_ids.end(), display.id) == |
| 76 current_ids.end()) { |
| 77 display.removed = true; |
| 78 if (primary_display_id_ == display.id) |
| 79 primary_display_id_ = display::Display::kInvalidDisplayID; |
| 80 } |
| 72 } | 81 } |
| 73 | 82 |
| 74 // TODO(kylechar): Use DisplayLayout/DisplayLayoutStore here when possible. | 83 // If the primary display was removed find a new primary display id. |
| 75 std::set<uint64_t> all_displays; | 84 if (primary_display_id_ == display::Display::kInvalidDisplayID) { |
| 76 for (ui::DisplaySnapshot* display : displays) { | 85 for (const DisplayInfo& display : cached_displays_) { |
| 77 const int64_t id = display->display_id(); | 86 if (!display.removed) { |
| 87 primary_display_id_ = display.id; |
| 88 break; |
| 89 } |
| 90 } |
| 91 } |
| 92 } |
| 78 | 93 |
| 79 all_displays.insert(id); | 94 void PlatformScreenImplOzone::ProcessModifiedDisplays( |
| 95 const ui::DisplayConfigurator::DisplayStateList& snapshots) { |
| 96 for (ui::DisplaySnapshot* snapshot : snapshots) { |
| 97 auto iter = GetCachedDisplayIterator(snapshot->display_id()); |
| 98 if (iter != cached_displays_.end()) { |
| 99 DisplayInfo& display_info = *iter; |
| 100 const ui::DisplayMode* current_mode = snapshot->current_mode(); |
| 101 if (current_mode->size() != display_info.bounds.size()) { |
| 102 display_info.bounds.set_size(current_mode->size()); |
| 103 display_info.modified = true; |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
| 80 | 108 |
| 81 if (displays_.find(id) != displays_.end()) | 109 void PlatformScreenImplOzone::UpdateCachedDisplays() { |
| 110 // Walk through cached displays after processing the snapshots to find any |
| 111 // removed or modified displays. This ensures that we only send one update per |
| 112 // display to the delegate. |
| 113 next_display_origin_.SetPoint(0, 0); |
| 114 for (auto iter = cached_displays_.begin(); iter != cached_displays_.end();) { |
| 115 DisplayInfo& display_info = *iter; |
| 116 if (display_info.removed) { |
| 117 // Update delegate and remove from cache. |
| 118 delegate_->OnDisplayRemoved(display_info.id); |
| 119 iter = cached_displays_.erase(iter); |
| 120 } else { |
| 121 // Check if the display origin needs to be updated. |
| 122 if (next_display_origin_ != display_info.bounds.origin()) { |
| 123 display_info.bounds.set_origin(next_display_origin_); |
| 124 display_info.modified = true; |
| 125 } |
| 126 next_display_origin_.Offset(display_info.bounds.width(), 0); |
| 127 |
| 128 // Check if the window bounds have changed and update delegate. |
| 129 if (display_info.modified) { |
| 130 display_info.modified = false; |
| 131 delegate_->OnDisplayModified(display_info.id, display_info.bounds); |
| 132 } |
| 133 ++iter; |
| 134 } |
| 135 } |
| 136 } |
| 137 |
| 138 void PlatformScreenImplOzone::AddNewDisplays( |
| 139 const ui::DisplayConfigurator::DisplayStateList& snapshots) { |
| 140 for (ui::DisplaySnapshot* snapshot : snapshots) { |
| 141 const int64_t id = snapshot->display_id(); |
| 142 |
| 143 // Check if display already exists and skip. |
| 144 if (GetCachedDisplayIterator(id) != cached_displays_.end()) |
| 82 continue; | 145 continue; |
| 83 | 146 |
| 84 const ui::DisplayMode* current_mode = display->current_mode(); | 147 const ui::DisplayMode* current_mode = snapshot->current_mode(); |
| 85 gfx::Rect bounds(next_display_origin_, current_mode->size()); | 148 gfx::Rect bounds(next_display_origin_, current_mode->size()); |
| 86 | 149 |
| 87 // Move the origin so that next display is to the right of current display. | 150 // Move the origin so that next display is to the right of current display. |
| 88 next_display_origin_.Offset(current_mode->size().width(), 0); | 151 next_display_origin_.Offset(current_mode->size().width(), 0); |
| 89 | 152 |
| 90 // The first display added will be our primary display. | 153 // If we have no primary display then this one should be it. |
| 91 if (displays_.empty()) | 154 if (primary_display_id_ == display::Display::kInvalidDisplayID) |
| 92 primary_display_id_ = id; | 155 primary_display_id_ = id; |
| 93 | 156 |
| 94 // Keep track of what displays have already been added. | 157 cached_displays_.push_back(DisplayInfo(id, bounds)); |
| 95 displays_.insert(display->display_id()); | |
| 96 | |
| 97 delegate_->OnDisplayAdded(this, id, bounds); | 158 delegate_->OnDisplayAdded(this, id, bounds); |
| 98 } | 159 } |
| 160 } |
| 99 | 161 |
| 100 DCHECK(displays_ == all_displays) << "Removing displays is not supported."; | 162 PlatformScreenImplOzone::CachedDisplayIterator |
| 163 PlatformScreenImplOzone::GetCachedDisplayIterator(int64_t display_id) { |
| 164 return std::find_if(cached_displays_.begin(), cached_displays_.end(), |
| 165 [display_id](const DisplayInfo& display_info) { |
| 166 return display_info.id == display_id; |
| 167 }); |
| 168 } |
| 169 |
| 170 void PlatformScreenImplOzone::OnDisplayModeChanged( |
| 171 const ui::DisplayConfigurator::DisplayStateList& displays) { |
| 172 ProcessRemovedDisplays(displays); |
| 173 ProcessModifiedDisplays(displays); |
| 174 UpdateCachedDisplays(); |
| 175 AddNewDisplays(displays); |
| 101 } | 176 } |
| 102 | 177 |
| 103 void PlatformScreenImplOzone::OnDisplayModeChangeFailed( | 178 void PlatformScreenImplOzone::OnDisplayModeChangeFailed( |
| 104 const ui::DisplayConfigurator::DisplayStateList& displays, | 179 const ui::DisplayConfigurator::DisplayStateList& displays, |
| 105 ui::MultipleDisplayState failed_new_state) { | 180 ui::MultipleDisplayState failed_new_state) { |
| 106 LOG(ERROR) << "OnDisplayModeChangeFailed from DisplayConfigurator"; | 181 LOG(ERROR) << "OnDisplayModeChangeFailed from DisplayConfigurator"; |
| 107 } | 182 } |
| 108 | 183 |
| 109 } // namespace display | 184 } // namespace display |
| OLD | NEW |