| 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 12 matching lines...) Expand all Loading... |
| 69 DrmNativeDisplayDelegate::DrmNativeDisplayDelegate( | 83 DrmNativeDisplayDelegate::DrmNativeDisplayDelegate( |
| 70 DrmGpuPlatformSupportHost* proxy, | 84 DrmGpuPlatformSupportHost* proxy, |
| 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), |
| 93 task_pending_(false), |
| 79 weak_ptr_factory_(this) { | 94 weak_ptr_factory_(this) { |
| 80 proxy_->RegisterHandler(this); | 95 proxy_->RegisterHandler(this); |
| 96 drm_devices_.insert(primary_graphics_card_path); |
| 81 } | 97 } |
| 82 | 98 |
| 83 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { | 99 DrmNativeDisplayDelegate::~DrmNativeDisplayDelegate() { |
| 84 device_manager_->RemoveObserver(this); | 100 device_manager_->RemoveObserver(this); |
| 85 proxy_->UnregisterHandler(this); | 101 proxy_->UnregisterHandler(this); |
| 86 } | 102 } |
| 87 | 103 |
| 88 void DrmNativeDisplayDelegate::Initialize() { | 104 void DrmNativeDisplayDelegate::Initialize() { |
| 89 device_manager_->AddObserver(this); | 105 device_manager_->AddObserver(this); |
| 90 device_manager_->ScanDevices(this); | 106 device_manager_->ScanDevices(this); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 } | 235 } |
| 220 | 236 |
| 221 void DrmNativeDisplayDelegate::RemoveObserver(NativeDisplayObserver* observer) { | 237 void DrmNativeDisplayDelegate::RemoveObserver(NativeDisplayObserver* observer) { |
| 222 observers_.RemoveObserver(observer); | 238 observers_.RemoveObserver(observer); |
| 223 } | 239 } |
| 224 | 240 |
| 225 void DrmNativeDisplayDelegate::OnDeviceEvent(const DeviceEvent& event) { | 241 void DrmNativeDisplayDelegate::OnDeviceEvent(const DeviceEvent& event) { |
| 226 if (event.device_type() != DeviceEvent::DISPLAY) | 242 if (event.device_type() != DeviceEvent::DISPLAY) |
| 227 return; | 243 return; |
| 228 | 244 |
| 229 switch (event.action_type()) { | 245 event_queue_.push(DisplayEvent(event.action_type(), event.path())); |
| 230 case DeviceEvent::ADD: | 246 ProcessEvent(); |
| 231 VLOG(1) << "Got display added event for " << event.path().value(); | 247 } |
| 232 // The default card is a special case since it needs to be opened early on | |
| 233 // the GPU process in order to initialize EGL. If it is opened here as | |
| 234 // well, it will cause a race with opening it in the GPU process and the | |
| 235 // GPU process may fail initialization. | |
| 236 // TODO(dnicoara) Remove this when the media stack does not require super | |
| 237 // early initialization. | |
| 238 if (event.path() == primary_graphics_card_path_) | |
| 239 return; | |
| 240 | 248 |
| 241 base::WorkerPool::PostTask( | 249 void DrmNativeDisplayDelegate::ProcessEvent() { |
| 242 FROM_HERE, | 250 while (!event_queue_.empty() && !task_pending_) { |
| 243 base::Bind(&OpenDeviceOnWorkerThread, event.path(), | 251 DisplayEvent event = event_queue_.front(); |
| 244 base::ThreadTaskRunnerHandle::Get(), | 252 event_queue_.pop(); |
| 245 base::Bind(&DrmNativeDisplayDelegate::OnNewGraphicsDevice, | 253 VLOG(1) << "Got display event " << kDisplayActionString[event.action_type] |
| 246 weak_ptr_factory_.GetWeakPtr())), | 254 << " for " << event.path.value(); |
| 247 false /* task_is_slow */); | 255 switch (event.action_type) { |
| 248 return; | 256 case DeviceEvent::ADD: |
| 249 case DeviceEvent::CHANGE: | 257 if (drm_devices_.find(event.path) == drm_devices_.end()) { |
| 250 VLOG(1) << "Got display changed event for " << event.path().value(); | 258 drm_devices_.insert(event.path); |
| 251 break; | 259 task_pending_ = base::WorkerPool::PostTask( |
| 252 case DeviceEvent::REMOVE: | 260 FROM_HERE, |
| 253 VLOG(1) << "Got display removed event for " << event.path().value(); | 261 base::Bind( |
| 254 // It shouldn't be possible to remove this device. | 262 &OpenDeviceOnWorkerThread, event.path, |
| 255 DCHECK(primary_graphics_card_path_ != event.path()) | 263 base::ThreadTaskRunnerHandle::Get(), |
| 256 << "Got event to remove primary graphics card " | 264 base::Bind(&DrmNativeDisplayDelegate::OnAddGraphicsDevice, |
| 257 << event.path().value(); | 265 weak_ptr_factory_.GetWeakPtr())), |
| 258 proxy_->Send(new OzoneGpuMsg_RemoveGraphicsDevice(event.path())); | 266 false /* task_is_slow */); |
| 259 break; | 267 } |
| 268 break; |
| 269 case DeviceEvent::CHANGE: |
| 270 task_pending_ = base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 271 FROM_HERE, |
| 272 base::Bind(&DrmNativeDisplayDelegate::OnUpdateGraphicsDevice, |
| 273 weak_ptr_factory_.GetWeakPtr())); |
| 274 break; |
| 275 case DeviceEvent::REMOVE: |
| 276 DCHECK(event.path != primary_graphics_card_path_) |
| 277 << "Removing primary graphics card"; |
| 278 auto it = drm_devices_.find(event.path); |
| 279 if (it != drm_devices_.end()) { |
| 280 drm_devices_.erase(it); |
| 281 task_pending_ = base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 282 FROM_HERE, |
| 283 base::Bind(&DrmNativeDisplayDelegate::OnRemoveGraphicsDevice, |
| 284 weak_ptr_factory_.GetWeakPtr(), event.path)); |
| 285 } |
| 286 break; |
| 287 } |
| 288 } |
| 289 } |
| 290 |
| 291 void DrmNativeDisplayDelegate::OnAddGraphicsDevice(const base::FilePath& path, |
| 292 base::File file) { |
| 293 if (file.IsValid()) { |
| 294 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 295 path, base::FileDescriptor(file.Pass()))); |
| 296 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 297 OnConfigurationChanged()); |
| 260 } | 298 } |
| 261 | 299 |
| 300 task_pending_ = false; |
| 301 ProcessEvent(); |
| 302 } |
| 303 |
| 304 void DrmNativeDisplayDelegate::OnUpdateGraphicsDevice() { |
| 262 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 305 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 263 OnConfigurationChanged()); | 306 OnConfigurationChanged()); |
| 307 task_pending_ = false; |
| 308 ProcessEvent(); |
| 264 } | 309 } |
| 265 | 310 |
| 266 void DrmNativeDisplayDelegate::OnNewGraphicsDevice(const base::FilePath& path, | 311 void DrmNativeDisplayDelegate::OnRemoveGraphicsDevice( |
| 267 base::File file) { | 312 const base::FilePath& path) { |
| 268 DCHECK(file.IsValid()); | 313 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_, | 314 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 273 OnConfigurationChanged()); | 315 OnConfigurationChanged()); |
| 316 task_pending_ = false; |
| 317 ProcessEvent(); |
| 274 } | 318 } |
| 275 | 319 |
| 276 void DrmNativeDisplayDelegate::OnChannelEstablished( | 320 void DrmNativeDisplayDelegate::OnChannelEstablished( |
| 277 int host_id, | 321 int host_id, |
| 278 scoped_refptr<base::SingleThreadTaskRunner> send_runner, | 322 scoped_refptr<base::SingleThreadTaskRunner> send_runner, |
| 279 const base::Callback<void(IPC::Message*)>& send_callback) { | 323 const base::Callback<void(IPC::Message*)>& send_callback) { |
| 324 drm_devices_.clear(); |
| 325 drm_devices_.insert(primary_graphics_card_path_); |
| 280 device_manager_->ScanDevices(this); | 326 device_manager_->ScanDevices(this); |
| 281 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, | 327 FOR_EACH_OBSERVER(NativeDisplayObserver, observers_, |
| 282 OnConfigurationChanged()); | 328 OnConfigurationChanged()); |
| 283 } | 329 } |
| 284 | 330 |
| 285 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { | 331 void DrmNativeDisplayDelegate::OnChannelDestroyed(int host_id) { |
| 286 // If the channel got destroyed in the middle of a configuration then just | 332 // If the channel got destroyed in the middle of a configuration then just |
| 287 // respond with failure. | 333 // respond with failure. |
| 288 if (!get_displays_callback_.is_null()) { | 334 if (!get_displays_callback_.is_null()) { |
| 289 base::ThreadTaskRunnerHandle::Get()->PostTask( | 335 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); | 407 set_hdcp_state_callback_map_.erase(it); |
| 362 } | 408 } |
| 363 } | 409 } |
| 364 | 410 |
| 365 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( | 411 void DrmNativeDisplayDelegate::RunUpdateDisplaysCallback( |
| 366 const GetDisplaysCallback& callback) const { | 412 const GetDisplaysCallback& callback) const { |
| 367 callback.Run(displays_.get()); | 413 callback.Run(displays_.get()); |
| 368 } | 414 } |
| 369 | 415 |
| 370 } // namespace ui | 416 } // namespace ui |
| OLD | NEW |