OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ozone/platform/drm/host/drm_native_display_delegate.h" | 5 #include "ui/ozone/platform/drm/host/drm_native_display_delegate.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
13 #include "ui/display/types/display_snapshot.h" | 13 #include "ui/display/types/display_snapshot.h" |
14 #include "ui/display/types/native_display_observer.h" | 14 #include "ui/display/types/native_display_observer.h" |
15 #include "ui/events/ozone/device/device_event.h" | 15 #include "ui/events/ozone/device/device_event.h" |
16 #include "ui/events/ozone/device/device_manager.h" | 16 #include "ui/events/ozone/device/device_manager.h" |
17 #include "ui/ozone/common/display_snapshot_proxy.h" | 17 #include "ui/ozone/common/display_snapshot_proxy.h" |
18 #include "ui/ozone/common/display_util.h" | 18 #include "ui/ozone/common/display_util.h" |
19 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" | 19 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" |
20 #include "ui/ozone/platform/drm/host/display_manager.h" | 20 #include "ui/ozone/platform/drm/host/display_manager.h" |
21 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" | 21 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" |
22 | 22 |
23 namespace ui { | 23 namespace ui { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 typedef base::Callback<void(const base::FilePath&, base::File)> | 27 typedef base::Callback<void(const base::FilePath&, base::File)> |
28 OnOpenDeviceReplyCallback; | 28 OnOpenDeviceReplyCallback; |
29 | 29 |
30 void OpenDeviceOnWorkerThread( | 30 const char* kDisplayActionString[] = { |
31 const base::FilePath& path, | 31 "ADD", |
32 const scoped_refptr<base::TaskRunner>& reply_runner, | 32 "REMOVE", |
33 const OnOpenDeviceReplyCallback& callback) { | 33 "CHANGE", |
34 }; | |
35 | |
36 base::File OpenDrmDevice(const base::FilePath& path) { | |
34 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | | 37 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
35 base::File::FLAG_WRITE); | 38 base::File::FLAG_WRITE); |
36 | 39 |
37 base::File::Info info; | 40 base::File::Info info; |
38 file.GetInfo(&info); | 41 file.GetInfo(&info); |
39 | 42 |
40 CHECK(!info.is_directory); | 43 CHECK(!info.is_directory); |
41 CHECK(path.DirName() == base::FilePath("/dev/dri")); | 44 CHECK(path.DirName() == base::FilePath("/dev/dri")); |
42 | 45 |
43 if (file.IsValid()) { | 46 if (!file.IsValid()) { |
44 reply_runner->PostTask( | 47 LOG(ERROR) << "Failed to open " << path.value() << ": " |
45 FROM_HERE, base::Bind(callback, path, base::Passed(file.Pass()))); | 48 << base::File::ErrorToString(file.error_details()); |
46 } | 49 } |
50 | |
51 return file.Pass(); | |
52 } | |
53 | |
54 void OpenDeviceOnWorkerThread( | |
55 const base::FilePath& path, | |
56 const scoped_refptr<base::TaskRunner>& reply_runner, | |
57 const OnOpenDeviceReplyCallback& callback) { | |
58 base::File file = OpenDrmDevice(path); | |
59 reply_runner->PostTask(FROM_HERE, | |
60 base::Bind(callback, path, base::Passed(file.Pass()))); | |
47 } | 61 } |
48 | 62 |
49 class DrmDisplaySnapshotProxy : public DisplaySnapshotProxy { | 63 class DrmDisplaySnapshotProxy : public DisplaySnapshotProxy { |
50 public: | 64 public: |
51 DrmDisplaySnapshotProxy(const DisplaySnapshot_Params& params, | 65 DrmDisplaySnapshotProxy(const DisplaySnapshot_Params& params, |
52 DisplayManager* display_manager) | 66 DisplayManager* display_manager) |
53 : DisplaySnapshotProxy(params), display_manager_(display_manager) { | 67 : DisplaySnapshotProxy(params), display_manager_(display_manager) { |
54 display_manager_->RegisterDisplay(this); | 68 display_manager_->RegisterDisplay(this); |
55 } | 69 } |
56 | 70 |
(...skipping 14 matching lines...) Expand all Loading... | |
71 DeviceManager* device_manager, | 85 DeviceManager* device_manager, |
72 DisplayManager* display_manager, | 86 DisplayManager* display_manager, |
73 const base::FilePath& primary_graphics_card_path) | 87 const base::FilePath& primary_graphics_card_path) |
74 : proxy_(proxy), | 88 : proxy_(proxy), |
75 device_manager_(device_manager), | 89 device_manager_(device_manager), |
76 display_manager_(display_manager), | 90 display_manager_(display_manager), |
77 primary_graphics_card_path_(primary_graphics_card_path), | 91 primary_graphics_card_path_(primary_graphics_card_path), |
78 has_dummy_display_(false), | 92 has_dummy_display_(false), |
79 weak_ptr_factory_(this) { | 93 weak_ptr_factory_(this) { |
80 proxy_->RegisterHandler(this); | 94 proxy_->RegisterHandler(this); |
95 drm_devices_.insert(primary_graphics_card_path); | |
81 } | 96 } |
82 | 97 |
83 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { | 98 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { |
84 device_manager_->RemoveObserver(this); | 99 device_manager_->RemoveObserver(this); |
85 proxy_->UnregisterHandler(this); | 100 proxy_->UnregisterHandler(this); |
86 } | 101 } |
87 | 102 |
88 void DrmNativeDisplayDelegate::Initialize() { | 103 void DrmNativeDisplayDelegate::Initialize() { |
89 device_manager_->AddObserver(this); | 104 device_manager_->AddObserver(this); |
90 device_manager_->ScanDevices(this); | 105 device_manager_->ScanDevices(this); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 234 } |
220 | 235 |
221 void DrmNativeDisplayDelegate::RemoveObserver(NativeDisplayObserver* observer) { | 236 void DrmNativeDisplayDelegate::RemoveObserver(NativeDisplayObserver* observer) { |
222 observers_.RemoveObserver(observer); | 237 observers_.RemoveObserver(observer); |
223 } | 238 } |
224 | 239 |
225 void DrmNativeDisplayDelegate::OnDeviceEvent(const DeviceEvent& event) { | 240 void DrmNativeDisplayDelegate::OnDeviceEvent(const DeviceEvent& event) { |
226 if (event.device_type() != DeviceEvent::DISPLAY) | 241 if (event.device_type() != DeviceEvent::DISPLAY) |
227 return; | 242 return; |
228 | 243 |
229 switch (event.action_type()) { | 244 event_queue_.push(DisplayEvent(event.action_type(), event.path())); |
230 case DeviceEvent::ADD: | 245 if (event_queue_.size() == 1) |
231 VLOG(1) << "Got display added event for " << event.path().value(); | 246 ProcessEvent(); |
232 // The default card is a special case since it needs to be opened early on | 247 } |
233 // the GPU process in order to initialize EGL. If it is opened here as | 248 |
234 // well, it will cause a race with opening it in the GPU process and the | 249 void DrmNativeDisplayDelegate::ProcessEvent() { |
235 // GPU process may fail initialization. | 250 while (!event_queue_.empty()) { |
spang
2015/04/27 21:54:33
I think it would be clearer to do:
while (!event_
dnicoara
2015/04/28 14:53:04
Done.
| |
236 // TODO(dnicoara) Remove this when the media stack does not require super | 251 DisplayEvent event = event_queue_.front(); |
spang
2015/04/27 21:54:33
Why can't you pop here?
dnicoara
2015/04/28 14:53:04
The event would act as the |task_pending_| bool.
| |
237 // early initialization. | 252 VLOG(1) << "Got display event " << kDisplayActionString[event.action_type] |
238 if (event.path() == primary_graphics_card_path_) | 253 << " for " << event.path.value(); |
254 switch (event.action_type) { | |
255 case DeviceEvent::ADD: | |
256 if (drm_devices_.find(event.path) == drm_devices_.end()) { | |
257 drm_devices_.insert(event.path); | |
258 base::WorkerPool::PostTask( | |
259 FROM_HERE, | |
260 base::Bind( | |
261 &OpenDeviceOnWorkerThread, event.path, | |
262 base::ThreadTaskRunnerHandle::Get(), | |
263 base::Bind(&DrmNativeDisplayDelegate::OnAddGraphicsDevice, | |
264 weak_ptr_factory_.GetWeakPtr())), | |
265 false /* task_is_slow */); | |
266 | |
267 return; | |
268 } | |
269 break; | |
270 case DeviceEvent::CHANGE: | |
271 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
272 FROM_HERE, | |
273 base::Bind(&DrmNativeDisplayDelegate::OnUpdateGraphicsDevice, | |
274 weak_ptr_factory_.GetWeakPtr())); | |
239 return; | 275 return; |
276 case DeviceEvent::REMOVE: | |
277 DCHECK(event.path != primary_graphics_card_path_) | |
278 << "Removing primary graphics card"; | |
279 auto it = drm_devices_.find(event.path); | |
280 if (it != drm_devices_.end()) { | |
281 drm_devices_.erase(it); | |
282 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
283 FROM_HERE, | |
284 base::Bind(&DrmNativeDisplayDelegate::OnRemoveGraphicsDevice, | |
285 weak_ptr_factory_.GetWeakPtr(), event.path)); | |
286 return; | |
287 } | |
288 break; | |
289 } | |
240 | 290 |
241 base::WorkerPool::PostTask( | 291 // The event was a no-op, so remove it and process the next one. |
242 FROM_HERE, | 292 event_queue_.pop(); |
243 base::Bind(&OpenDeviceOnWorkerThread, event.path(), | 293 } |
244 base::ThreadTaskRunnerHandle::Get(), | 294 } |
245 base::Bind(&DrmNativeDisplayDelegate::OnNewGraphicsDevice, | 295 |
246 weak_ptr_factory_.GetWeakPtr())), | 296 void DrmNativeDisplayDelegate::OnAddGraphicsDevice(const base::FilePath& path, |
247 false /* task_is_slow */); | 297 base::File file) { |
248 return; | 298 if (file.IsValid()) { |
249 case DeviceEvent::CHANGE: | 299 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
250 VLOG(1) << "Got display changed event for " << event.path().value(); | 300 path, base::FileDescriptor(file.Pass()))); |
251 break; | 301 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
252 case DeviceEvent::REMOVE: | 302 OnConfigurationChanged()); |
253 VLOG(1) << "Got display removed event for " << event.path().value(); | |
254 // It shouldn't be possible to remove this device. | |
255 DCHECK(primary_graphics_card_path_ != event.path()) | |
256 << "Got event to remove primary graphics card " | |
257 << event.path().value(); | |
258 proxy_->Send(new OzoneGpuMsg_RemoveGraphicsDevice(event.path())); | |
259 break; | |
260 } | 303 } |
261 | 304 |
305 event_queue_.pop(); | |
306 ProcessEvent(); | |
307 } | |
308 | |
309 void DrmNativeDisplayDelegate::OnUpdateGraphicsDevice() { | |
262 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 310 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
263 OnConfigurationChanged()); | 311 OnConfigurationChanged()); |
312 event_queue_.pop(); | |
313 ProcessEvent(); | |
264 } | 314 } |
265 | 315 |
266 void DrmNativeDisplayDelegate::OnNewGraphicsDevice(const base::FilePath& path, | 316 void DrmNativeDisplayDelegate::OnRemoveGraphicsDevice( |
267 base::File file) { | 317 const base::FilePath& path) { |
268 DCHECK(file.IsValid()); | 318 proxy_->Send(new OzoneGpuMsg_RemoveGraphicsDevice(path)); |
269 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( | |
270 path, base::FileDescriptor(file.Pass()))); | |
271 | |
272 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 319 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
273 OnConfigurationChanged()); | 320 OnConfigurationChanged()); |
321 event_queue_.pop(); | |
322 ProcessEvent(); | |
274 } | 323 } |
275 | 324 |
276 void DrmNativeDisplayDelegate::OnChannelEstablished( | 325 void DrmNativeDisplayDelegate::OnChannelEstablished( |
277 int host_id, | 326 int host_id, |
278 scoped_refptr<base::SingleThreadTaskRunner> send_runner, | 327 scoped_refptr<base::SingleThreadTaskRunner> send_runner, |
279 const base::Callback<void(IPC::Message*)>& send_callback) { | 328 const base::Callback<void(IPC::Message*)>& send_callback) { |
329 drm_devices_.clear(); | |
330 drm_devices_.insert(primary_graphics_card_path_); | |
280 device_manager_->ScanDevices(this); | 331 device_manager_->ScanDevices(this); |
281 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 332 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
282 OnConfigurationChanged()); | 333 OnConfigurationChanged()); |
283 } | 334 } |
284 | 335 |
285 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { | 336 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { |
286 // If the channel got destroyed in the middle of a configuration then just | 337 // If the channel got destroyed in the middle of a configuration then just |
287 // respond with failure. | 338 // respond with failure. |
288 if (!get_displays_callback_.is_null()) { | 339 if (!get_displays_callback_.is_null()) { |
289 base::ThreadTaskRunnerHandle::Get()->PostTask( | 340 base::ThreadTaskRunnerHandle::Get()->PostTask( |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 set_hdcp_state_callback_map_.erase(it); | 412 set_hdcp_state_callback_map_.erase(it); |
362 } | 413 } |
363 } | 414 } |
364 | 415 |
365 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( | 416 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( |
366 const GetDisplaysCallback& callback) const { | 417 const GetDisplaysCallback& callback) const { |
367 callback.Run(displays_.get()); | 418 callback.Run(displays_.get()); |
368 } | 419 } |
369 | 420 |
370 } // namespace ui | 421 } // namespace ui |
OLD | NEW |