OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/gpu/drm_device_manager.h" | 5 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h" |
6 | 6 |
| 7 #include "base/file_descriptor_posix.h" |
| 8 #include "base/single_thread_task_runner.h" |
7 #include "ui/ozone/platform/drm/gpu/drm_device.h" | 9 #include "ui/ozone/platform/drm/gpu/drm_device.h" |
| 10 #include "ui/ozone/platform/drm/gpu/drm_device_generator.h" |
8 | 11 |
9 namespace ui { | 12 namespace ui { |
10 | 13 |
| 14 namespace { |
| 15 |
| 16 class FindByDevicePath { |
| 17 public: |
| 18 explicit FindByDevicePath(const base::FilePath& path) : path_(path) {} |
| 19 |
| 20 bool operator()(const scoped_refptr<DrmDevice>& device) { |
| 21 return device->device_path() == path_; |
| 22 } |
| 23 |
| 24 private: |
| 25 base::FilePath path_; |
| 26 }; |
| 27 |
| 28 } // namespace |
| 29 |
11 DrmDeviceManager::DrmDeviceManager( | 30 DrmDeviceManager::DrmDeviceManager( |
12 const scoped_refptr<DrmDevice>& primary_device) | 31 scoped_ptr<DrmDeviceGenerator> drm_device_generator) |
13 : primary_device_(primary_device) { | 32 : drm_device_generator_(drm_device_generator.Pass()) { |
14 } | 33 } |
15 | 34 |
16 DrmDeviceManager::~DrmDeviceManager() { | 35 DrmDeviceManager::~DrmDeviceManager() { |
17 DCHECK(drm_device_map_.empty()); | 36 DCHECK(drm_device_map_.empty()); |
18 } | 37 } |
19 | 38 |
| 39 bool DrmDeviceManager::AddDrmDevice(const base::FilePath& path, |
| 40 const base::FileDescriptor& fd) { |
| 41 base::AutoLock lock(lock_); |
| 42 base::File file(fd.fd); |
| 43 auto it = |
| 44 std::find_if(devices_.begin(), devices_.end(), FindByDevicePath(path)); |
| 45 if (it != devices_.end()) { |
| 46 VLOG(2) << "Got request to add existing device: " << path.value(); |
| 47 return false; |
| 48 } |
| 49 |
| 50 scoped_refptr<DrmDevice> device = |
| 51 drm_device_generator_->CreateDevice(path, file.Pass()); |
| 52 if (!device) { |
| 53 VLOG(2) << "Could not initialize DRM device for " << path.value(); |
| 54 return false; |
| 55 } |
| 56 |
| 57 if (io_task_runner_) |
| 58 device->InitializeTaskRunner(io_task_runner_); |
| 59 |
| 60 if (!primary_device_) |
| 61 primary_device_ = device; |
| 62 |
| 63 devices_.push_back(device); |
| 64 return true; |
| 65 } |
| 66 |
| 67 void DrmDeviceManager::RemoveDrmDevice(const base::FilePath& path) { |
| 68 base::AutoLock lock(lock_); |
| 69 auto it = |
| 70 std::find_if(devices_.begin(), devices_.end(), FindByDevicePath(path)); |
| 71 if (it == devices_.end()) { |
| 72 VLOG(2) << "Got request to remove non-existent device: " << path.value(); |
| 73 return; |
| 74 } |
| 75 |
| 76 DCHECK_NE(primary_device_, *it); |
| 77 devices_.erase(it); |
| 78 } |
| 79 |
| 80 void DrmDeviceManager::InitializeIOTaskRunner( |
| 81 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 82 DCHECK(!io_task_runner_); |
| 83 io_task_runner_ = task_runner; |
| 84 for (const auto& device : devices_) |
| 85 device->InitializeTaskRunner(io_task_runner_); |
| 86 } |
| 87 |
20 void DrmDeviceManager::UpdateDrmDevice(gfx::AcceleratedWidget widget, | 88 void DrmDeviceManager::UpdateDrmDevice(gfx::AcceleratedWidget widget, |
21 const scoped_refptr<DrmDevice>& device) { | 89 const scoped_refptr<DrmDevice>& device) { |
22 base::AutoLock lock(lock_); | 90 base::AutoLock lock(lock_); |
23 drm_device_map_[widget] = device; | 91 drm_device_map_[widget] = device; |
24 } | 92 } |
25 | 93 |
26 void DrmDeviceManager::RemoveDrmDevice(gfx::AcceleratedWidget widget) { | 94 void DrmDeviceManager::RemoveDrmDevice(gfx::AcceleratedWidget widget) { |
27 base::AutoLock lock(lock_); | 95 base::AutoLock lock(lock_); |
28 auto it = drm_device_map_.find(widget); | 96 auto it = drm_device_map_.find(widget); |
| 97 // It shouldn't be possible to remove the primary device. |
| 98 DCHECK_NE(it->second, primary_device_); |
| 99 |
29 if (it != drm_device_map_.end()) | 100 if (it != drm_device_map_.end()) |
30 drm_device_map_.erase(it); | 101 drm_device_map_.erase(it); |
31 } | 102 } |
32 | 103 |
33 scoped_refptr<DrmDevice> DrmDeviceManager::GetDrmDevice( | 104 scoped_refptr<DrmDevice> DrmDeviceManager::GetDrmDevice( |
34 gfx::AcceleratedWidget widget) { | 105 gfx::AcceleratedWidget widget) { |
35 base::AutoLock lock(lock_); | 106 base::AutoLock lock(lock_); |
36 if (widget == gfx::kNullAcceleratedWidget) | 107 if (widget == gfx::kNullAcceleratedWidget) |
37 return primary_device_; | 108 return primary_device_; |
38 | 109 |
39 auto it = drm_device_map_.find(widget); | 110 auto it = drm_device_map_.find(widget); |
40 DCHECK(it != drm_device_map_.end()) | 111 DCHECK(it != drm_device_map_.end()) |
41 << "Attempting to get device for unknown widget " << widget; | 112 << "Attempting to get device for unknown widget " << widget; |
42 // If the widget isn't associated with a display (headless mode) we can | 113 // If the widget isn't associated with a display (headless mode) we can |
43 // allocate buffers from any controller since they will never be scanned out. | 114 // allocate buffers from any controller since they will never be scanned out. |
44 // Use the primary DRM device as a fallback when allocating these buffers. | 115 // Use the primary DRM device as a fallback when allocating these buffers. |
45 if (!it->second) | 116 if (!it->second) |
46 return primary_device_; | 117 return primary_device_; |
47 | 118 |
48 return it->second; | 119 return it->second; |
49 } | 120 } |
50 | 121 |
| 122 std::vector<scoped_refptr<DrmDevice>> DrmDeviceManager::GetDrmDevices() { |
| 123 base::AutoLock lock(lock_); |
| 124 return devices_; |
| 125 } |
| 126 |
51 } // namespace ui | 127 } // namespace ui |
OLD | NEW |