| 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 #include <xf86drm.h> | |
| 9 | 8 |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 13 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 14 #include "ui/display/types/display_snapshot.h" | 13 #include "ui/display/types/display_snapshot.h" |
| 15 #include "ui/display/types/native_display_observer.h" | 14 #include "ui/display/types/native_display_observer.h" |
| 16 #include "ui/events/ozone/device/device_event.h" | 15 #include "ui/events/ozone/device/device_event.h" |
| 17 #include "ui/events/ozone/device/device_manager.h" | 16 #include "ui/events/ozone/device/device_manager.h" |
| 18 #include "ui/ozone/common/display_snapshot_proxy.h" | 17 #include "ui/ozone/common/display_snapshot_proxy.h" |
| 19 #include "ui/ozone/common/display_util.h" | 18 #include "ui/ozone/common/display_util.h" |
| 20 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" | 19 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" |
| 21 #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_device_handle.h" |
| 22 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" | 22 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" |
| 23 | 23 |
| 24 namespace ui { | 24 namespace ui { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 typedef base::Callback<void(const base::FilePath&, base::File)> | 28 typedef base::Callback<void(const base::FilePath&, scoped_ptr<DrmDeviceHandle>)> |
| 29 OnOpenDeviceReplyCallback; | 29 OnOpenDeviceReplyCallback; |
| 30 | 30 |
| 31 const char* kDisplayActionString[] = { | 31 const char* kDisplayActionString[] = { |
| 32 "ADD", | 32 "ADD", |
| 33 "REMOVE", | 33 "REMOVE", |
| 34 "CHANGE", | 34 "CHANGE", |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 bool Authenticate(int fd) { | |
| 38 drm_magic_t magic = 0; | |
| 39 // We need to make sure the DRM device has enough privilege. Use the DRM | |
| 40 // authentication logic to figure out if the device has enough permissions. | |
| 41 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); | |
| 42 } | |
| 43 | |
| 44 base::File OpenDrmDevice(const base::FilePath& path) { | |
| 45 base::File file; | |
| 46 bool print_warning = true; | |
| 47 while (true) { | |
| 48 file = base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ | | |
| 49 base::File::FLAG_WRITE); | |
| 50 | |
| 51 base::File::Info info; | |
| 52 file.GetInfo(&info); | |
| 53 | |
| 54 CHECK(!info.is_directory); | |
| 55 CHECK(path.DirName() == base::FilePath("/dev/dri")); | |
| 56 | |
| 57 if (!file.IsValid()) { | |
| 58 LOG(ERROR) << "Failed to open " << path.value() << ": " | |
| 59 << base::File::ErrorToString(file.error_details()); | |
| 60 return file.Pass(); | |
| 61 } | |
| 62 | |
| 63 if (Authenticate(file.GetPlatformFile())) | |
| 64 break; | |
| 65 | |
| 66 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value(); | |
| 67 | |
| 68 print_warning = false; | |
| 69 usleep(100000); | |
| 70 } | |
| 71 | |
| 72 VLOG(1) << "Succeeded authenticating " << path.value(); | |
| 73 return file.Pass(); | |
| 74 } | |
| 75 | |
| 76 void OpenDeviceOnWorkerThread( | 37 void OpenDeviceOnWorkerThread( |
| 77 const base::FilePath& path, | 38 const base::FilePath& path, |
| 78 const scoped_refptr<base::TaskRunner>& reply_runner, | 39 const scoped_refptr<base::TaskRunner>& reply_runner, |
| 79 const OnOpenDeviceReplyCallback& callback) { | 40 const OnOpenDeviceReplyCallback& callback) { |
| 80 base::File file = OpenDrmDevice(path); | 41 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); |
| 81 reply_runner->PostTask(FROM_HERE, | 42 handle->Initialize(path); |
| 82 base::Bind(callback, path, base::Passed(file.Pass()))); | 43 reply_runner->PostTask( |
| 44 FROM_HERE, base::Bind(callback, path, base::Passed(handle.Pass()))); |
| 45 } |
| 46 |
| 47 void CloseDeviceOnWorkerThread( |
| 48 scoped_ptr<DrmDeviceHandle> handle, |
| 49 const scoped_refptr<base::TaskRunner>& reply_runner, |
| 50 const base::Closure& callback) { |
| 51 handle->Shutdown(); |
| 52 reply_runner->PostTask(FROM_HERE, callback); |
| 83 } | 53 } |
| 84 | 54 |
| 85 class DrmDisplaySnapshotProxy : public DisplaySnapshotProxy { | 55 class DrmDisplaySnapshotProxy : public DisplaySnapshotProxy { |
| 86 public: | 56 public: |
| 87 DrmDisplaySnapshotProxy(const DisplaySnapshot_Params& params, | 57 DrmDisplaySnapshotProxy(const DisplaySnapshot_Params& params, |
| 88 DisplayManager* display_manager) | 58 DisplayManager* display_manager) |
| 89 : DisplaySnapshotProxy(params), display_manager_(display_manager) { | 59 : DisplaySnapshotProxy(params), display_manager_(display_manager) { |
| 90 display_manager_->RegisterDisplay(this); | 60 display_manager_->RegisterDisplay(this); |
| 91 } | 61 } |
| 92 | 62 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 107 DeviceManager* device_manager, | 77 DeviceManager* device_manager, |
| 108 DisplayManager* display_manager, | 78 DisplayManager* display_manager, |
| 109 const base::FilePath& primary_graphics_card_path) | 79 const base::FilePath& primary_graphics_card_path) |
| 110 : proxy_(proxy), | 80 : proxy_(proxy), |
| 111 device_manager_(device_manager), | 81 device_manager_(device_manager), |
| 112 display_manager_(display_manager), | 82 display_manager_(display_manager), |
| 113 primary_graphics_card_path_(primary_graphics_card_path), | 83 primary_graphics_card_path_(primary_graphics_card_path), |
| 114 has_dummy_display_(false), | 84 has_dummy_display_(false), |
| 115 weak_ptr_factory_(this) { | 85 weak_ptr_factory_(this) { |
| 116 proxy_->RegisterHandler(this); | 86 proxy_->RegisterHandler(this); |
| 117 drm_devices_.insert(primary_graphics_card_path); | |
| 118 } | 87 } |
| 119 | 88 |
| 120 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { | 89 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { |
| 121 device_manager_->RemoveObserver(this); | 90 device_manager_->RemoveObserver(this); |
| 122 proxy_->UnregisterHandler(this); | 91 proxy_->UnregisterHandler(this); |
| 123 } | 92 } |
| 124 | 93 |
| 125 void DrmNativeDisplayDelegate::Initialize() { | 94 void DrmNativeDisplayDelegate::Initialize() { |
| 95 { |
| 96 // First device needs to be treated specially. We need to open this |
| 97 // synchronously since the GPU process will need it to initialize the |
| 98 // graphics state. |
| 99 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 100 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); |
| 101 if (!handle->Initialize(primary_graphics_card_path_)) { |
| 102 LOG(FATAL) << "Failed to open primary graphics card"; |
| 103 return; |
| 104 } |
| 105 drm_devices_.add(primary_graphics_card_path_, handle.Pass()); |
| 106 } |
| 107 |
| 126 device_manager_->AddObserver(this); | 108 device_manager_->AddObserver(this); |
| 127 device_manager_->ScanDevices(this); | 109 device_manager_->ScanDevices(this); |
| 128 | 110 |
| 129 if (!displays_.empty()) | 111 if (!displays_.empty()) |
| 130 return; | 112 return; |
| 131 DisplaySnapshot_Params params; | 113 DisplaySnapshot_Params params; |
| 132 bool success = false; | 114 bool success = false; |
| 133 { | 115 { |
| 134 // The file generated by frecon that contains EDID for the 1st display. | 116 // The file generated by frecon that contains EDID for the 1st display. |
| 135 const base::FilePath kEDIDFile("/tmp/display_info.bin"); | 117 const base::FilePath kEDIDFile("/tmp/display_info.bin"); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 251 } |
| 270 | 252 |
| 271 void DrmNativeDisplayDelegate::ProcessEvent() { | 253 void DrmNativeDisplayDelegate::ProcessEvent() { |
| 272 while (!event_queue_.empty()) { | 254 while (!event_queue_.empty()) { |
| 273 DisplayEvent event = event_queue_.front(); | 255 DisplayEvent event = event_queue_.front(); |
| 274 VLOG(1) << "Got display event " << kDisplayActionString[event.action_type] | 256 VLOG(1) << "Got display event " << kDisplayActionString[event.action_type] |
| 275 << " for " << event.path.value(); | 257 << " for " << event.path.value(); |
| 276 switch (event.action_type) { | 258 switch (event.action_type) { |
| 277 case DeviceEvent::ADD: | 259 case DeviceEvent::ADD: |
| 278 if (drm_devices_.find(event.path) == drm_devices_.end()) { | 260 if (drm_devices_.find(event.path) == drm_devices_.end()) { |
| 279 drm_devices_.insert(event.path); | |
| 280 base::WorkerPool::PostTask( | 261 base::WorkerPool::PostTask( |
| 281 FROM_HERE, | 262 FROM_HERE, |
| 282 base::Bind( | 263 base::Bind( |
| 283 &OpenDeviceOnWorkerThread, event.path, | 264 &OpenDeviceOnWorkerThread, event.path, |
| 284 base::ThreadTaskRunnerHandle::Get(), | 265 base::ThreadTaskRunnerHandle::Get(), |
| 285 base::Bind(&DrmNativeDisplayDelegate::OnAddGraphicsDevice, | 266 base::Bind(&DrmNativeDisplayDelegate::OnAddGraphicsDevice, |
| 286 weak_ptr_factory_.GetWeakPtr())), | 267 weak_ptr_factory_.GetWeakPtr())), |
| 287 false /* task_is_slow */); | 268 false /* task_is_slow */); |
| 288 | 269 |
| 289 return; | 270 return; |
| 290 } | 271 } |
| 291 break; | 272 break; |
| 292 case DeviceEvent::CHANGE: | 273 case DeviceEvent::CHANGE: |
| 293 base::ThreadTaskRunnerHandle::Get()->PostTask( | 274 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 294 FROM_HERE, | 275 FROM_HERE, |
| 295 base::Bind(&DrmNativeDisplayDelegate::OnUpdateGraphicsDevice, | 276 base::Bind(&DrmNativeDisplayDelegate::OnUpdateGraphicsDevice, |
| 296 weak_ptr_factory_.GetWeakPtr())); | 277 weak_ptr_factory_.GetWeakPtr())); |
| 297 return; | 278 return; |
| 298 case DeviceEvent::REMOVE: | 279 case DeviceEvent::REMOVE: |
| 299 DCHECK(event.path != primary_graphics_card_path_) | 280 DCHECK(event.path != primary_graphics_card_path_) |
| 300 << "Removing primary graphics card"; | 281 << "Removing primary graphics card"; |
| 301 auto it = drm_devices_.find(event.path); | 282 auto it = drm_devices_.find(event.path); |
| 302 if (it != drm_devices_.end()) { | 283 if (it != drm_devices_.end()) { |
| 303 drm_devices_.erase(it); | 284 base::WorkerPool::PostTask( |
| 304 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 305 FROM_HERE, | 285 FROM_HERE, |
| 306 base::Bind(&DrmNativeDisplayDelegate::OnRemoveGraphicsDevice, | 286 base::Bind( |
| 307 weak_ptr_factory_.GetWeakPtr(), event.path)); | 287 &CloseDeviceOnWorkerThread, |
| 288 base::Passed(drm_devices_.take_and_erase(it)), |
| 289 base::ThreadTaskRunnerHandle::Get(), |
| 290 base::Bind(&DrmNativeDisplayDelegate::OnRemoveGraphicsDevice, |
| 291 weak_ptr_factory_.GetWeakPtr(), event.path)), |
| 292 false /* task_is_slow */); |
| 308 return; | 293 return; |
| 309 } | 294 } |
| 310 break; | 295 break; |
| 311 } | 296 } |
| 312 | 297 |
| 313 // The event was a no-op, so remove it and process the next one. | 298 // The event was a no-op, so remove it and process the next one. |
| 314 event_queue_.pop(); | 299 event_queue_.pop(); |
| 315 } | 300 } |
| 316 } | 301 } |
| 317 | 302 |
| 318 void DrmNativeDisplayDelegate::OnAddGraphicsDevice(const base::FilePath& path, | 303 void DrmNativeDisplayDelegate::OnAddGraphicsDevice( |
| 319 base::File file) { | 304 const base::FilePath& path, |
| 320 if (file.IsValid()) { | 305 scoped_ptr<DrmDeviceHandle> handle) { |
| 306 if (handle->IsValid()) { |
| 307 base::File file = handle->DuplicateFile(); |
| 308 drm_devices_.add(path, handle.Pass()); |
| 321 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( | 309 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 322 path, base::FileDescriptor(file.Pass()))); | 310 path, base::FileDescriptor(file.Pass()))); |
| 323 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 311 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 324 OnConfigurationChanged()); | 312 OnConfigurationChanged()); |
| 325 } | 313 } |
| 326 | 314 |
| 327 event_queue_.pop(); | 315 event_queue_.pop(); |
| 328 ProcessEvent(); | 316 ProcessEvent(); |
| 329 } | 317 } |
| 330 | 318 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 341 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 329 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 342 OnConfigurationChanged()); | 330 OnConfigurationChanged()); |
| 343 event_queue_.pop(); | 331 event_queue_.pop(); |
| 344 ProcessEvent(); | 332 ProcessEvent(); |
| 345 } | 333 } |
| 346 | 334 |
| 347 void DrmNativeDisplayDelegate::OnChannelEstablished( | 335 void DrmNativeDisplayDelegate::OnChannelEstablished( |
| 348 int host_id, | 336 int host_id, |
| 349 scoped_refptr<base::SingleThreadTaskRunner> send_runner, | 337 scoped_refptr<base::SingleThreadTaskRunner> send_runner, |
| 350 const base::Callback<void(IPC::Message*)>& send_callback) { | 338 const base::Callback<void(IPC::Message*)>& send_callback) { |
| 351 drm_devices_.clear(); | 339 for (auto pair : drm_devices_) { |
| 352 drm_devices_.insert(primary_graphics_card_path_); | 340 if (pair.second->IsValid()) { |
| 353 { | 341 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 354 // First device needs to be treated specially. We need to open this | 342 pair.first, base::FileDescriptor(pair.second->DuplicateFile()))); |
| 355 // synchronously since the GPU process will need it to initialize the | |
| 356 // graphics state. | |
| 357 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 358 base::File file = OpenDrmDevice(primary_graphics_card_path_); | |
| 359 if (!file.IsValid()) { | |
| 360 LOG(FATAL) << "Failed to open primary graphics card"; | |
| 361 return; | |
| 362 } | 343 } |
| 363 OnAddGraphicsDevice(primary_graphics_card_path_, file.Pass()); | |
| 364 } | 344 } |
| 365 | 345 |
| 366 device_manager_->ScanDevices(this); | 346 device_manager_->ScanDevices(this); |
| 367 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 347 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 368 OnConfigurationChanged()); | 348 OnConfigurationChanged()); |
| 369 } | 349 } |
| 370 | 350 |
| 371 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { | 351 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { |
| 372 // If the channel got destroyed in the middle of a configuration then just | 352 // If the channel got destroyed in the middle of a configuration then just |
| 373 // respond with failure. | 353 // respond with failure. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 set_hdcp_state_callback_map_.erase(it); | 427 set_hdcp_state_callback_map_.erase(it); |
| 448 } | 428 } |
| 449 } | 429 } |
| 450 | 430 |
| 451 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( | 431 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( |
| 452 const GetDisplaysCallback& callback) const { | 432 const GetDisplaysCallback& callback) const { |
| 453 callback.Run(displays_.get()); | 433 callback.Run(displays_.get()); |
| 454 } | 434 } |
| 455 | 435 |
| 456 } // namespace ui | 436 } // namespace ui |
| OLD | NEW |