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_ozone.h" | 5 #include "services/ui/display/platform_screen_ozone.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "services/shell/public/cpp/interface_registry.h" | 14 #include "services/shell/public/cpp/interface_registry.h" |
15 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
16 #include "ui/display/types/display_constants.h" | 16 #include "ui/display/types/display_constants.h" |
17 #include "ui/display/types/display_snapshot.h" | 17 #include "ui/display/types/display_snapshot.h" |
18 #include "ui/display/types/native_display_delegate.h" | 18 #include "ui/display/types/native_display_delegate.h" |
19 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
20 #include "ui/ozone/public/ozone_platform.h" | 20 #include "ui/ozone/public/ozone_platform.h" |
21 | 21 |
22 namespace display { | 22 namespace display { |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Needed for DisplayConfigurator::ForceInitialConfigure. | 25 // Needed for DisplayConfigurator::ForceInitialConfigure. |
26 const SkColor kChromeOsBootColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); | 26 const SkColor kChromeOsBootColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); |
27 | 27 |
28 const float kInchInMm = 25.4f; | |
29 | |
30 float ComputeDisplayDPI(const gfx::Size& pixel_size, | |
31 const gfx::Size& physical_size) { | |
32 DCHECK(!physical_size.IsEmpty()); | |
33 return (pixel_size.width() / static_cast<float>(physical_size.width())) * | |
rjkroege
2016/09/21 13:23:46
This might need to be rounded?
kylechar
2016/09/21 16:31:12
Hmm, it's not rounded in DisplayChangeObserver cur
rjkroege
2016/09/23 13:56:23
OK. But I bet that's caused at least one bug. :-)
| |
34 kInchInMm; | |
35 } | |
36 | |
37 float FindDeviceScaleFactor(float dpi) { | |
38 if (display::Display::HasForceDeviceScaleFactor()) | |
39 return display::Display::GetForcedDeviceScaleFactor(); | |
40 | |
41 // TODO(kylechar): If dpi > 150 then ash uses 1.25 now. | |
42 if (dpi > 200.0) | |
43 return 2.0f; | |
44 else | |
45 return 1.0f; | |
46 } | |
47 | |
28 } // namespace | 48 } // namespace |
29 | 49 |
30 // static | 50 // static |
31 std::unique_ptr<PlatformScreen> PlatformScreen::Create() { | 51 std::unique_ptr<PlatformScreen> PlatformScreen::Create() { |
32 return base::MakeUnique<PlatformScreenOzone>(); | 52 return base::MakeUnique<PlatformScreenOzone>(); |
33 } | 53 } |
34 | 54 |
35 PlatformScreenOzone::PlatformScreenOzone() {} | 55 PlatformScreenOzone::PlatformScreenOzone() {} |
36 | 56 |
37 PlatformScreenOzone::~PlatformScreenOzone() { | 57 PlatformScreenOzone::~PlatformScreenOzone() { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 | 99 |
80 int64_t PlatformScreenOzone::GetPrimaryDisplayId() const { | 100 int64_t PlatformScreenOzone::GetPrimaryDisplayId() const { |
81 return primary_display_id_; | 101 return primary_display_id_; |
82 } | 102 } |
83 | 103 |
84 void PlatformScreenOzone::ToggleVirtualDisplay() { | 104 void PlatformScreenOzone::ToggleVirtualDisplay() { |
85 if (!fake_display_controller_ || wait_for_display_config_update_) | 105 if (!fake_display_controller_ || wait_for_display_config_update_) |
86 return; | 106 return; |
87 | 107 |
88 if (cached_displays_.size() == 1) { | 108 if (cached_displays_.size() == 1) { |
89 const gfx::Size& display_size = cached_displays_[0].bounds.size(); | 109 const gfx::Size& pixel_size = cached_displays_[0].pixel_size; |
90 wait_for_display_config_update_ = | 110 wait_for_display_config_update_ = |
91 fake_display_controller_->AddDisplay(display_size) != | 111 fake_display_controller_->AddDisplay(pixel_size) != |
92 Display::kInvalidDisplayID; | 112 Display::kInvalidDisplayID; |
93 } else if (cached_displays_.size() > 1) { | 113 } else if (cached_displays_.size() > 1) { |
94 wait_for_display_config_update_ = | 114 wait_for_display_config_update_ = |
95 fake_display_controller_->RemoveDisplay(cached_displays_.back().id); | 115 fake_display_controller_->RemoveDisplay(cached_displays_.back().id); |
96 } else { | 116 } else { |
97 NOTREACHED(); | 117 NOTREACHED(); |
98 } | 118 } |
99 } | 119 } |
100 | 120 |
101 void PlatformScreenOzone::ProcessRemovedDisplays( | 121 void PlatformScreenOzone::ProcessRemovedDisplays( |
(...skipping 22 matching lines...) Expand all Loading... | |
124 } | 144 } |
125 } | 145 } |
126 } | 146 } |
127 | 147 |
128 void PlatformScreenOzone::ProcessModifiedDisplays( | 148 void PlatformScreenOzone::ProcessModifiedDisplays( |
129 const ui::DisplayConfigurator::DisplayStateList& snapshots) { | 149 const ui::DisplayConfigurator::DisplayStateList& snapshots) { |
130 for (ui::DisplaySnapshot* snapshot : snapshots) { | 150 for (ui::DisplaySnapshot* snapshot : snapshots) { |
131 auto iter = GetCachedDisplayIterator(snapshot->display_id()); | 151 auto iter = GetCachedDisplayIterator(snapshot->display_id()); |
132 if (iter != cached_displays_.end()) { | 152 if (iter != cached_displays_.end()) { |
133 DisplayInfo& display_info = *iter; | 153 DisplayInfo& display_info = *iter; |
134 const ui::DisplayMode* current_mode = snapshot->current_mode(); | 154 DisplayInfo new_info = DisplayInfoFromSnapshot(snapshot); |
135 if (current_mode->size() != display_info.bounds.size()) { | 155 |
136 display_info.bounds.set_size(current_mode->size()); | 156 if (new_info.bounds.size() != display_info.bounds.size() || |
157 new_info.scale_factor != display_info.scale_factor) { | |
158 display_info = DisplayInfoFromSnapshot(snapshot); | |
137 display_info.modified = true; | 159 display_info.modified = true; |
138 } | 160 } |
139 } | 161 } |
140 } | 162 } |
141 } | 163 } |
142 | 164 |
143 void PlatformScreenOzone::UpdateCachedDisplays() { | 165 void PlatformScreenOzone::UpdateCachedDisplays() { |
144 // Walk through cached displays after processing the snapshots to find any | 166 // Walk through cached displays after processing the snapshots to find any |
145 // removed or modified displays. This ensures that we only send one update per | 167 // removed or modified displays. This ensures that we only send one update per |
146 // display to the delegate. | 168 // display to the delegate. |
147 next_display_origin_.SetPoint(0, 0); | 169 next_display_origin_.SetPoint(0, 0); |
148 for (auto iter = cached_displays_.begin(); iter != cached_displays_.end();) { | 170 for (auto iter = cached_displays_.begin(); iter != cached_displays_.end();) { |
149 DisplayInfo& display_info = *iter; | 171 DisplayInfo& display_info = *iter; |
150 if (display_info.removed) { | 172 if (display_info.removed) { |
151 // Update delegate and remove from cache. | 173 // Update delegate and remove from cache. |
152 delegate_->OnDisplayRemoved(display_info.id); | 174 delegate_->OnDisplayRemoved(display_info.id); |
153 iter = cached_displays_.erase(iter); | 175 iter = cached_displays_.erase(iter); |
154 } else { | 176 } else { |
155 // Check if the display origin needs to be updated. | 177 // Check if the display origin needs to be updated. |
156 if (next_display_origin_ != display_info.bounds.origin()) { | 178 if (next_display_origin_ != display_info.bounds.origin()) { |
157 display_info.bounds.set_origin(next_display_origin_); | 179 display_info.bounds.set_origin(next_display_origin_); |
158 display_info.modified = true; | 180 display_info.modified = true; |
159 } | 181 } |
160 next_display_origin_.Offset(display_info.bounds.width(), 0); | 182 next_display_origin_.Offset(display_info.bounds.width(), 0); |
161 | 183 |
162 // Check if the window bounds have changed and update delegate. | 184 // Check if the window bounds have changed and update delegate. |
163 if (display_info.modified) { | 185 if (display_info.modified) { |
164 display_info.modified = false; | 186 display_info.modified = false; |
165 delegate_->OnDisplayModified(display_info.id, display_info.bounds); | 187 delegate_->OnDisplayModified(display_info.id, display_info.bounds, |
188 display_info.pixel_size, | |
189 display_info.scale_factor); | |
166 } | 190 } |
167 ++iter; | 191 ++iter; |
168 } | 192 } |
169 } | 193 } |
170 } | 194 } |
171 | 195 |
172 void PlatformScreenOzone::AddNewDisplays( | 196 void PlatformScreenOzone::AddNewDisplays( |
173 const ui::DisplayConfigurator::DisplayStateList& snapshots) { | 197 const ui::DisplayConfigurator::DisplayStateList& snapshots) { |
174 for (ui::DisplaySnapshot* snapshot : snapshots) { | 198 for (ui::DisplaySnapshot* snapshot : snapshots) { |
175 const int64_t id = snapshot->display_id(); | 199 const int64_t id = snapshot->display_id(); |
176 | 200 |
177 // Check if display already exists and skip. | 201 // Check if display already exists and skip. |
178 if (GetCachedDisplayIterator(id) != cached_displays_.end()) | 202 if (GetCachedDisplayIterator(id) != cached_displays_.end()) |
179 continue; | 203 continue; |
180 | 204 |
181 const ui::DisplayMode* current_mode = snapshot->current_mode(); | 205 // If we have no primary display then this one should be it. |
182 gfx::Rect bounds(next_display_origin_, current_mode->size()); | 206 if (primary_display_id_ == Display::kInvalidDisplayID) |
207 primary_display_id_ = id; | |
208 | |
209 DisplayInfo display_info = DisplayInfoFromSnapshot(snapshot); | |
183 | 210 |
184 // Move the origin so that next display is to the right of current display. | 211 // Move the origin so that next display is to the right of current display. |
185 next_display_origin_.Offset(current_mode->size().width(), 0); | 212 next_display_origin_.Offset(display_info.bounds.width(), 0); |
186 | 213 |
187 // If we have no primary display then this one should be it. | 214 cached_displays_.push_back(display_info); |
188 if (primary_display_id_ == display::Display::kInvalidDisplayID) | 215 delegate_->OnDisplayAdded(display_info.id, display_info.bounds, |
189 primary_display_id_ = id; | 216 display_info.pixel_size, |
190 | 217 display_info.scale_factor); |
191 cached_displays_.push_back(DisplayInfo(id, bounds)); | |
192 delegate_->OnDisplayAdded(id, bounds); | |
193 } | 218 } |
194 } | 219 } |
195 | 220 |
196 PlatformScreenOzone::CachedDisplayIterator | 221 PlatformScreenOzone::CachedDisplayIterator |
197 PlatformScreenOzone::GetCachedDisplayIterator(int64_t display_id) { | 222 PlatformScreenOzone::GetCachedDisplayIterator(int64_t display_id) { |
198 return std::find_if(cached_displays_.begin(), cached_displays_.end(), | 223 return std::find_if(cached_displays_.begin(), cached_displays_.end(), |
199 [display_id](const DisplayInfo& display_info) { | 224 [display_id](const DisplayInfo& display_info) { |
200 return display_info.id == display_id; | 225 return display_info.id == display_id; |
201 }); | 226 }); |
202 } | 227 } |
203 | 228 |
204 void PlatformScreenOzone::OnDisplayModeChanged( | 229 void PlatformScreenOzone::OnDisplayModeChanged( |
205 const ui::DisplayConfigurator::DisplayStateList& displays) { | 230 const ui::DisplayConfigurator::DisplayStateList& displays) { |
206 ProcessRemovedDisplays(displays); | 231 ProcessRemovedDisplays(displays); |
207 ProcessModifiedDisplays(displays); | 232 ProcessModifiedDisplays(displays); |
208 UpdateCachedDisplays(); | 233 UpdateCachedDisplays(); |
209 AddNewDisplays(displays); | 234 AddNewDisplays(displays); |
210 wait_for_display_config_update_ = false; | 235 wait_for_display_config_update_ = false; |
211 } | 236 } |
212 | 237 |
238 PlatformScreenOzone::DisplayInfo PlatformScreenOzone::DisplayInfoFromSnapshot( | |
239 ui::DisplaySnapshot* snapshot) { | |
240 const ui::DisplayMode* current_mode = snapshot->current_mode(); | |
241 DCHECK(current_mode); | |
242 | |
243 DisplayInfo display_info; | |
244 display_info.id = snapshot->display_id(); | |
245 display_info.pixel_size = current_mode->size(); | |
246 display_info.scale_factor = FindDeviceScaleFactor( | |
247 ComputeDisplayDPI(current_mode->size(), snapshot->physical_size())); | |
248 // Get DIP size based on device scale factor. | |
249 display_info.bounds = | |
250 gfx::Rect(next_display_origin_, | |
251 gfx::ScaleToRoundedSize(current_mode->size(), | |
252 1.0f / display_info.scale_factor)); | |
253 | |
254 return display_info; | |
255 } | |
256 | |
213 void PlatformScreenOzone::OnDisplayModeChangeFailed( | 257 void PlatformScreenOzone::OnDisplayModeChangeFailed( |
214 const ui::DisplayConfigurator::DisplayStateList& displays, | 258 const ui::DisplayConfigurator::DisplayStateList& displays, |
215 ui::MultipleDisplayState failed_new_state) { | 259 ui::MultipleDisplayState failed_new_state) { |
216 LOG(ERROR) << "OnDisplayModeChangeFailed from DisplayConfigurator"; | 260 LOG(ERROR) << "OnDisplayModeChangeFailed from DisplayConfigurator"; |
217 wait_for_display_config_update_ = false; | 261 wait_for_display_config_update_ = false; |
218 } | 262 } |
219 | 263 |
220 void PlatformScreenOzone::Create(const shell::Identity& remote_identity, | 264 void PlatformScreenOzone::Create(const shell::Identity& remote_identity, |
221 mojom::DisplayControllerRequest request) { | 265 mojom::DisplayControllerRequest request) { |
222 bindings_.AddBinding(this, std::move(request)); | 266 bindings_.AddBinding(this, std::move(request)); |
223 } | 267 } |
224 | 268 |
225 } // namespace display | 269 } // namespace display |
OLD | NEW |