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() { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | |
17 DCHECK(drm_device_map_.empty()); | 37 DCHECK(drm_device_map_.empty()); |
18 } | 38 } |
19 | 39 |
40 bool DrmDeviceManager::AddDrmDevice(const base::FilePath& path, | |
41 const base::FileDescriptor& fd) { | |
42 DCHECK(thread_checker_.CalledOnValidThread()); | |
43 base::File file(fd.fd); | |
44 auto it = | |
45 std::find_if(devices_.begin(), devices_.end(), FindByDevicePath(path)); | |
46 if (it != devices_.end()) { | |
47 VLOG(2) << "Got request to add existing device: " << path.value(); | |
48 return false; | |
49 } | |
50 | |
51 scoped_refptr<DrmDevice> device = | |
52 drm_device_generator_->CreateDevice(path, file.Pass()); | |
53 if (!device) { | |
54 LOG(ERROR) << "Could not initialize DRM device for " << path.value(); | |
55 return false; | |
56 } | |
57 | |
58 if (io_task_runner_) | |
59 device->InitializeTaskRunner(io_task_runner_); | |
60 | |
61 if (!primary_device_) | |
62 primary_device_ = device; | |
63 | |
64 devices_.push_back(device); | |
65 return true; | |
66 } | |
67 | |
68 void DrmDeviceManager::RemoveDrmDevice(const base::FilePath& path) { | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 auto it = | |
71 std::find_if(devices_.begin(), devices_.end(), FindByDevicePath(path)); | |
72 if (it == devices_.end()) { | |
73 VLOG(2) << "Got request to remove non-existent device: " << path.value(); | |
74 return; | |
75 } | |
76 | |
77 DCHECK_NE(primary_device_, *it); | |
78 devices_.erase(it); | |
79 } | |
80 | |
81 void DrmDeviceManager::InitializeIOTaskRunner( | |
82 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | |
83 DCHECK(thread_checker_.CalledOnValidThread()); | |
84 DCHECK(!io_task_runner_); | |
85 io_task_runner_ = task_runner; | |
86 for (const auto& device : devices_) | |
87 device->InitializeTaskRunner(io_task_runner_); | |
88 } | |
89 | |
20 void DrmDeviceManager::UpdateDrmDevice(gfx::AcceleratedWidget widget, | 90 void DrmDeviceManager::UpdateDrmDevice(gfx::AcceleratedWidget widget, |
21 const scoped_refptr<DrmDevice>& device) { | 91 const scoped_refptr<DrmDevice>& device) { |
22 base::AutoLock lock(lock_); | 92 base::AutoLock lock(lock_); |
23 drm_device_map_[widget] = device; | 93 drm_device_map_[widget] = device; |
24 } | 94 } |
25 | 95 |
26 void DrmDeviceManager::RemoveDrmDevice(gfx::AcceleratedWidget widget) { | 96 void DrmDeviceManager::RemoveDrmDevice(gfx::AcceleratedWidget widget) { |
27 base::AutoLock lock(lock_); | 97 base::AutoLock lock(lock_); |
28 auto it = drm_device_map_.find(widget); | 98 auto it = drm_device_map_.find(widget); |
99 // It shouldn't be possible to remove the primary device. | |
100 DCHECK_NE(it->second, primary_device_); | |
dnicoara
2015/04/28 21:08:03
The comment/DCHECK is wrong since this doesn't rem
| |
101 | |
29 if (it != drm_device_map_.end()) | 102 if (it != drm_device_map_.end()) |
30 drm_device_map_.erase(it); | 103 drm_device_map_.erase(it); |
31 } | 104 } |
32 | 105 |
33 scoped_refptr<DrmDevice> DrmDeviceManager::GetDrmDevice( | 106 scoped_refptr<DrmDevice> DrmDeviceManager::GetDrmDevice( |
34 gfx::AcceleratedWidget widget) { | 107 gfx::AcceleratedWidget widget) { |
35 base::AutoLock lock(lock_); | 108 base::AutoLock lock(lock_); |
36 if (widget == gfx::kNullAcceleratedWidget) | 109 if (widget == gfx::kNullAcceleratedWidget) |
37 return primary_device_; | 110 return primary_device_; |
38 | 111 |
39 auto it = drm_device_map_.find(widget); | 112 auto it = drm_device_map_.find(widget); |
40 DCHECK(it != drm_device_map_.end()) | 113 DCHECK(it != drm_device_map_.end()) |
41 << "Attempting to get device for unknown widget " << widget; | 114 << "Attempting to get device for unknown widget " << widget; |
42 // If the widget isn't associated with a display (headless mode) we can | 115 // 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. | 116 // 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. | 117 // Use the primary DRM device as a fallback when allocating these buffers. |
45 if (!it->second) | 118 if (!it->second) |
46 return primary_device_; | 119 return primary_device_; |
47 | 120 |
48 return it->second; | 121 return it->second; |
49 } | 122 } |
50 | 123 |
124 const DrmDeviceVector& DrmDeviceManager::GetDrmDevices() const { | |
125 DCHECK(thread_checker_.CalledOnValidThread()); | |
126 return devices_; | |
127 } | |
128 | |
51 } // namespace ui | 129 } // namespace ui |
OLD | NEW |