Chromium Code Reviews| 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_display_host_manager.h" | 5 #include "ui/ozone/platform/drm/host/drm_display_host_manager.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <xf86drm.h> | 9 #include <xf86drm.h> |
| 10 | 10 |
| 11 #include "base/files/file_enumerator.h" | |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
| 16 #include "ui/display/types/display_snapshot.h" | 17 #include "ui/display/types/display_snapshot.h" |
| 17 #include "ui/events/ozone/device/device_event.h" | 18 #include "ui/events/ozone/device/device_event.h" |
| 18 #include "ui/events/ozone/device/device_manager.h" | 19 #include "ui/events/ozone/device/device_manager.h" |
| 19 #include "ui/ozone/common/display_snapshot_proxy.h" | 20 #include "ui/ozone/common/display_snapshot_proxy.h" |
| 20 #include "ui/ozone/common/display_util.h" | 21 #include "ui/ozone/common/display_util.h" |
| 21 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" | 22 #include "ui/ozone/common/gpu/ozone_gpu_messages.h" |
| 22 #include "ui/ozone/platform/drm/host/drm_device_handle.h" | 23 #include "ui/ozone/platform/drm/host/drm_device_handle.h" |
| 23 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" | 24 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h" |
| 24 #include "ui/ozone/platform/drm/host/drm_native_display_delegate.h" | 25 #include "ui/ozone/platform/drm/host/drm_native_display_delegate.h" |
| 25 | 26 |
| 26 namespace ui { | 27 namespace ui { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 typedef base::Callback<void(const base::FilePath&, scoped_ptr<DrmDeviceHandle>)> | 31 typedef base::Callback<void(const base::FilePath&, scoped_ptr<DrmDeviceHandle>)> |
| 31 OnOpenDeviceReplyCallback; | 32 OnOpenDeviceReplyCallback; |
| 32 | 33 |
| 33 const char kDefaultGraphicsCardPattern[] = "/dev/dri/card%d"; | 34 const char kDefaultGraphicsCardPattern[] = "/dev/dri/card%d"; |
| 35 const char kVgemSysCardPath[] = "/sys/bus/platform/devices/vgem/drm/"; | |
| 36 base::LazyInstance<std::string> kVgemCardPath; | |
|
spang
2015/05/12 17:20:38
Please don't add state to the global scope. Instea
dshwang
2015/05/12 17:48:02
I see. I move this to member of this class.
dshwang
2015/05/13 13:09:00
Done.
| |
| 34 | 37 |
| 35 const char* kDisplayActionString[] = { | 38 const char* kDisplayActionString[] = { |
| 36 "ADD", | 39 "ADD", |
| 37 "REMOVE", | 40 "REMOVE", |
| 38 "CHANGE", | 41 "CHANGE", |
| 39 }; | 42 }; |
| 40 | 43 |
| 44 // VGEM fd is a /dev/dri device but we cannot know in advance which one, for | |
| 45 // that we inspect the /sys/.../vgem/.../cardX, if we find one such card, then | |
| 46 // VGEM is present in the system and we can reuse the index. | |
| 47 std::string GetVgemCardPath() { | |
| 48 if (!kVgemCardPath.Get().empty()) | |
| 49 return kVgemCardPath.Get(); | |
| 50 | |
| 51 base::FileEnumerator file_iter( | |
| 52 base::FilePath::FromUTF8Unsafe(kVgemSysCardPath), false, | |
| 53 base::FileEnumerator::DIRECTORIES, FILE_PATH_LITERAL("card*")); | |
| 54 | |
| 55 while (!file_iter.Next().empty()) { | |
| 56 // Inspect the card%d directories in the directory and extract the index. | |
| 57 std::string name(file_iter.GetInfo().GetName().BaseName().MaybeAsASCII()); | |
| 58 base::TrimString(name, "card", &name); | |
|
dnicoara
2015/05/12 15:41:22
Rather than extracting the interger, why not just
dshwang
2015/05/12 16:18:57
good point. I'll update.
dshwang
2015/05/13 13:09:00
Done.
| |
| 59 errno = 0; | |
| 60 const int device_index = strtol(name.c_str(), nullptr, 10); | |
| 61 DLOG_IF(ERROR, errno != 0) << "Error extracting integer from " << name; | |
| 62 if (errno != 0) | |
| 63 return std::string(); | |
| 64 | |
| 65 kVgemCardPath.Get() = | |
| 66 base::StringPrintf(kDefaultGraphicsCardPattern, device_index); | |
| 67 DVLOG(1) << "VGEM card path is " << kVgemCardPath.Get(); | |
| 68 break; | |
| 69 } | |
| 70 return kVgemCardPath.Get(); | |
| 71 } | |
| 72 | |
| 41 void OpenDeviceOnWorkerThread( | 73 void OpenDeviceOnWorkerThread( |
| 42 const base::FilePath& path, | 74 const base::FilePath& path, |
| 43 const scoped_refptr<base::TaskRunner>& reply_runner, | 75 const scoped_refptr<base::TaskRunner>& reply_runner, |
| 44 const OnOpenDeviceReplyCallback& callback) { | 76 const OnOpenDeviceReplyCallback& callback) { |
| 77 bool is_vgem = path.MaybeAsASCII() == GetVgemCardPath(); | |
| 45 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); | 78 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); |
| 46 handle->Initialize(path); | 79 handle->Initialize(path, is_vgem); |
| 47 reply_runner->PostTask( | 80 reply_runner->PostTask( |
| 48 FROM_HERE, base::Bind(callback, path, base::Passed(handle.Pass()))); | 81 FROM_HERE, base::Bind(callback, path, base::Passed(handle.Pass()))); |
| 49 } | 82 } |
| 50 | 83 |
| 51 void CloseDeviceOnWorkerThread( | 84 void CloseDeviceOnWorkerThread( |
| 52 scoped_ptr<DrmDeviceHandle> handle, | 85 scoped_ptr<DrmDeviceHandle> handle, |
| 53 const scoped_refptr<base::TaskRunner>& reply_runner, | 86 const scoped_refptr<base::TaskRunner>& reply_runner, |
| 54 const base::Closure& callback) { | 87 const base::Closure& callback) { |
| 55 handle.reset(); | 88 handle.reset(); |
| 56 reply_runner->PostTask(FROM_HERE, callback); | 89 reply_runner->PostTask(FROM_HERE, callback); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 primary_graphics_card_path_(GetPrimaryDisplayCardPath()), | 138 primary_graphics_card_path_(GetPrimaryDisplayCardPath()), |
| 106 has_dummy_display_(false), | 139 has_dummy_display_(false), |
| 107 task_pending_(false), | 140 task_pending_(false), |
| 108 weak_ptr_factory_(this) { | 141 weak_ptr_factory_(this) { |
| 109 { | 142 { |
| 110 // First device needs to be treated specially. We need to open this | 143 // First device needs to be treated specially. We need to open this |
| 111 // synchronously since the GPU process will need it to initialize the | 144 // synchronously since the GPU process will need it to initialize the |
| 112 // graphics state. | 145 // graphics state. |
| 113 base::ThreadRestrictions::ScopedAllowIO allow_io; | 146 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 114 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); | 147 scoped_ptr<DrmDeviceHandle> handle(new DrmDeviceHandle()); |
| 115 if (!handle->Initialize(primary_graphics_card_path_)) { | 148 if (!handle->Initialize(primary_graphics_card_path_, false)) { |
| 116 LOG(FATAL) << "Failed to open primary graphics card"; | 149 LOG(FATAL) << "Failed to open primary graphics card"; |
| 117 return; | 150 return; |
| 118 } | 151 } |
| 119 drm_devices_.add(primary_graphics_card_path_, handle.Pass()); | 152 drm_devices_.add(primary_graphics_card_path_, handle.Pass()); |
| 120 } | 153 } |
| 121 | 154 |
| 122 device_manager_->AddObserver(this); | 155 device_manager_->AddObserver(this); |
| 123 proxy_->RegisterHandler(this); | 156 proxy_->RegisterHandler(this); |
| 124 | 157 |
| 125 DisplaySnapshot_Params params; | 158 DisplaySnapshot_Params params; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 break; | 329 break; |
| 297 } | 330 } |
| 298 } | 331 } |
| 299 } | 332 } |
| 300 | 333 |
| 301 void DrmDisplayHostManager::OnAddGraphicsDevice( | 334 void DrmDisplayHostManager::OnAddGraphicsDevice( |
| 302 const base::FilePath& path, | 335 const base::FilePath& path, |
| 303 scoped_ptr<DrmDeviceHandle> handle) { | 336 scoped_ptr<DrmDeviceHandle> handle) { |
| 304 if (handle->IsValid()) { | 337 if (handle->IsValid()) { |
| 305 base::ScopedFD file = handle->Duplicate(); | 338 base::ScopedFD file = handle->Duplicate(); |
| 339 bool is_vgem = handle->IsVgem(); | |
| 306 drm_devices_.add(path, handle.Pass()); | 340 drm_devices_.add(path, handle.Pass()); |
| 307 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( | 341 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 308 path, base::FileDescriptor(file.Pass()))); | 342 path, base::FileDescriptor(file.Pass()), is_vgem)); |
| 309 NotifyDisplayDelegate(); | 343 NotifyDisplayDelegate(); |
| 310 } | 344 } |
| 311 | 345 |
| 312 task_pending_ = false; | 346 task_pending_ = false; |
| 313 ProcessEvent(); | 347 ProcessEvent(); |
| 314 } | 348 } |
| 315 | 349 |
| 316 void DrmDisplayHostManager::OnUpdateGraphicsDevice() { | 350 void DrmDisplayHostManager::OnUpdateGraphicsDevice() { |
| 317 NotifyDisplayDelegate(); | 351 NotifyDisplayDelegate(); |
| 318 task_pending_ = false; | 352 task_pending_ = false; |
| 319 ProcessEvent(); | 353 ProcessEvent(); |
| 320 } | 354 } |
| 321 | 355 |
| 322 void DrmDisplayHostManager::OnRemoveGraphicsDevice(const base::FilePath& path) { | 356 void DrmDisplayHostManager::OnRemoveGraphicsDevice(const base::FilePath& path) { |
| 323 proxy_->Send(new OzoneGpuMsg_RemoveGraphicsDevice(path)); | 357 proxy_->Send(new OzoneGpuMsg_RemoveGraphicsDevice(path)); |
| 324 NotifyDisplayDelegate(); | 358 NotifyDisplayDelegate(); |
| 325 task_pending_ = false; | 359 task_pending_ = false; |
| 326 ProcessEvent(); | 360 ProcessEvent(); |
| 327 } | 361 } |
| 328 | 362 |
| 329 void DrmDisplayHostManager::OnChannelEstablished( | 363 void DrmDisplayHostManager::OnChannelEstablished( |
| 330 int host_id, | 364 int host_id, |
| 331 scoped_refptr<base::SingleThreadTaskRunner> send_runner, | 365 scoped_refptr<base::SingleThreadTaskRunner> send_runner, |
| 332 const base::Callback<void(IPC::Message*)>& send_callback) { | 366 const base::Callback<void(IPC::Message*)>& send_callback) { |
| 333 auto it = drm_devices_.find(primary_graphics_card_path_); | 367 auto it = drm_devices_.find(primary_graphics_card_path_); |
| 334 DCHECK(it != drm_devices_.end()); | 368 DCHECK(it != drm_devices_.end()); |
| 335 // Send the primary device first since this is used to initialize graphics | 369 // Send the primary device first since this is used to initialize graphics |
| 336 // state. | 370 // state. |
| 337 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( | 371 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 338 it->first, base::FileDescriptor(it->second->Duplicate()))); | 372 it->first, base::FileDescriptor(it->second->Duplicate()), |
| 373 it->second->IsVgem())); | |
| 339 | 374 |
| 340 for (auto pair : drm_devices_) { | 375 for (auto pair : drm_devices_) { |
| 341 if (pair.second->IsValid() && pair.first != primary_graphics_card_path_) { | 376 if (pair.second->IsValid() && pair.first != primary_graphics_card_path_) { |
| 342 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( | 377 proxy_->Send(new OzoneGpuMsg_AddGraphicsDevice( |
| 343 pair.first, base::FileDescriptor(pair.second->Duplicate()))); | 378 pair.first, base::FileDescriptor(pair.second->Duplicate()), |
| 379 it->second->IsVgem())); | |
| 344 } | 380 } |
| 345 } | 381 } |
| 346 | 382 |
| 347 device_manager_->ScanDevices(this); | 383 device_manager_->ScanDevices(this); |
| 348 NotifyDisplayDelegate(); | 384 NotifyDisplayDelegate(); |
| 349 } | 385 } |
| 350 | 386 |
| 351 void DrmDisplayHostManager::OnChannelDestroyed(int host_id) { | 387 void DrmDisplayHostManager::OnChannelDestroyed(int host_id) { |
| 352 // If the channel got destroyed in the middle of a configuration then just | 388 // If the channel got destroyed in the middle of a configuration then just |
| 353 // respond with failure. | 389 // respond with failure. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 const GetDisplaysCallback& callback) const { | 467 const GetDisplaysCallback& callback) const { |
| 432 callback.Run(displays_.get()); | 468 callback.Run(displays_.get()); |
| 433 } | 469 } |
| 434 | 470 |
| 435 void DrmDisplayHostManager::NotifyDisplayDelegate() const { | 471 void DrmDisplayHostManager::NotifyDisplayDelegate() const { |
| 436 if (delegate_) | 472 if (delegate_) |
| 437 delegate_->OnConfigurationChanged(); | 473 delegate_->OnConfigurationChanged(); |
| 438 } | 474 } |
| 439 | 475 |
| 440 } // namespace ui | 476 } // namespace ui |
| OLD | NEW |