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 "device/vr/vr_device_manager.h" | 5 #include "device/vr/vr_device_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 | 13 |
14 #if defined(OS_ANDROID) | 14 #if defined(OS_ANDROID) |
15 #include "device/vr/android/gvr/gvr_device_provider.h" | 15 #include "device/vr/android/gvr/gvr_device_provider.h" |
16 #endif | 16 #endif |
17 | 17 |
18 namespace device { | 18 namespace device { |
19 | 19 |
20 namespace { | 20 namespace { |
21 VRDeviceManager* g_vr_device_manager = nullptr; | 21 VRDeviceManager* g_vr_device_manager = nullptr; |
22 } | 22 } |
23 | 23 |
24 VRDeviceManager::VRDeviceManager() | 24 VRDeviceManager::VRDeviceManager() |
25 : vr_initialized_(false), keep_alive_(false), has_scheduled_poll_(false) { | 25 : vr_initialized_(false), |
26 presenting_service_(nullptr), | |
27 presenting_device_(nullptr), | |
28 keep_alive_(false), | |
29 has_scheduled_poll_(false) { | |
26 // Register VRDeviceProviders for the current platform | 30 // Register VRDeviceProviders for the current platform |
27 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
28 RegisterProvider(base::WrapUnique(new GvrDeviceProvider())); | 32 RegisterProvider(base::WrapUnique(new GvrDeviceProvider())); |
29 #endif | 33 #endif |
30 } | 34 } |
31 | 35 |
32 VRDeviceManager::VRDeviceManager(std::unique_ptr<VRDeviceProvider> provider) | 36 VRDeviceManager::VRDeviceManager(std::unique_ptr<VRDeviceProvider> provider) |
33 : vr_initialized_(false), keep_alive_(true), has_scheduled_poll_(false) { | 37 : vr_initialized_(false), |
38 presenting_service_(nullptr), | |
39 presenting_device_(nullptr), | |
40 keep_alive_(true), | |
41 has_scheduled_poll_(false) { | |
34 thread_checker_.DetachFromThread(); | 42 thread_checker_.DetachFromThread(); |
35 RegisterProvider(std::move(provider)); | 43 RegisterProvider(std::move(provider)); |
36 SetInstance(this); | 44 SetInstance(this); |
37 } | 45 } |
38 | 46 |
39 VRDeviceManager::~VRDeviceManager() { | 47 VRDeviceManager::~VRDeviceManager() { |
40 DCHECK(thread_checker_.CalledOnValidThread()); | 48 DCHECK(thread_checker_.CalledOnValidThread()); |
41 StopSchedulingPollEvents(); | 49 StopSchedulingPollEvents(); |
42 g_vr_device_manager = nullptr; | 50 g_vr_device_manager = nullptr; |
43 } | 51 } |
44 | 52 |
45 VRDeviceManager* VRDeviceManager::GetInstance() { | 53 VRDeviceManager* VRDeviceManager::GetInstance() { |
46 if (!g_vr_device_manager) | 54 if (!g_vr_device_manager) |
47 g_vr_device_manager = new VRDeviceManager(); | 55 g_vr_device_manager = new VRDeviceManager(); |
48 return g_vr_device_manager; | 56 return g_vr_device_manager; |
49 } | 57 } |
50 | 58 |
59 // Returns the requested device with the requested id if the specified service | |
60 // is allowed to access it. | |
61 VRDevice* VRDeviceManager::GetAllowedDevice(VRServiceImpl* service, | |
62 unsigned int index) { | |
63 VRDeviceManager* device_manager = GetInstance(); | |
64 | |
65 // If another service is presenting to the requested device don't allow other | |
66 // services to access it. That could potentially allow them to spy on | |
67 // where the user is looking on another page, spam another application with | |
68 // pose resets, etc. | |
69 if (device_manager->presenting_service_ && | |
70 device_manager->presenting_service_ != service) { | |
71 if (device_manager->presenting_device_ && | |
72 device_manager->presenting_device_->id() == index) | |
73 return nullptr; | |
74 } | |
75 | |
76 return device_manager->GetDevice(index); | |
77 } | |
78 | |
51 void VRDeviceManager::SetInstance(VRDeviceManager* instance) { | 79 void VRDeviceManager::SetInstance(VRDeviceManager* instance) { |
52 // Unit tests can create multiple instances but only one should exist at any | 80 // Unit tests can create multiple instances but only one should exist at any |
53 // given time so g_vr_device_manager should only go from nullptr to | 81 // given time so g_vr_device_manager should only go from nullptr to |
54 // non-nullptr and vica versa. | 82 // non-nullptr and vica versa. |
55 CHECK_NE(!!instance, !!g_vr_device_manager); | 83 CHECK_NE(!!instance, !!g_vr_device_manager); |
56 g_vr_device_manager = instance; | 84 g_vr_device_manager = instance; |
57 } | 85 } |
58 | 86 |
59 bool VRDeviceManager::HasInstance() { | 87 bool VRDeviceManager::HasInstance() { |
60 // For testing. Checks to see if a VRDeviceManager instance is active. | 88 // For testing. Checks to see if a VRDeviceManager instance is active. |
61 return !!g_vr_device_manager; | 89 return !!g_vr_device_manager; |
62 } | 90 } |
63 | 91 |
64 void VRDeviceManager::AddService(VRServiceImpl* service) { | 92 void VRDeviceManager::AddService(VRServiceImpl* service) { |
65 services_.push_back(service); | 93 services_.push_back(service); |
66 | 94 |
67 // Ensure that the device providers are initialized | 95 // Ensure that the device providers are initialized |
68 InitializeProviders(); | 96 InitializeProviders(); |
69 } | 97 } |
70 | 98 |
71 void VRDeviceManager::RemoveService(VRServiceImpl* service) { | 99 void VRDeviceManager::RemoveService(VRServiceImpl* service) { |
72 services_.erase(std::remove(services_.begin(), services_.end(), service), | 100 services_.erase(std::remove(services_.begin(), services_.end(), service), |
73 services_.end()); | 101 services_.end()); |
74 | 102 |
103 if (service == presenting_service_) { | |
mthiesse
2016/09/13 18:46:15
With these checks peppered all over the place, it
bajones
2016/09/13 19:42:57
I see your point (and I already did something simi
mthiesse
2016/09/13 20:01:16
Is there something preventing you from only restri
| |
104 presenting_device_->ExitPresent(); | |
105 | |
106 presenting_service_ = nullptr; | |
107 presenting_device_ = nullptr; | |
108 } | |
109 | |
75 if (services_.empty() && !keep_alive_) { | 110 if (services_.empty() && !keep_alive_) { |
76 // Delete the device manager when it has no active connections. | 111 // Delete the device manager when it has no active connections. |
77 delete g_vr_device_manager; | 112 delete g_vr_device_manager; |
78 } | 113 } |
79 } | 114 } |
80 | 115 |
81 mojo::Array<VRDisplayPtr> VRDeviceManager::GetVRDevices() { | 116 mojo::Array<VRDisplayPtr> VRDeviceManager::GetVRDevices() { |
82 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
83 | 118 |
84 InitializeProviders(); | 119 InitializeProviders(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 return iter->second; | 155 return iter->second; |
121 } | 156 } |
122 | 157 |
123 // These dispatchers must use Clone() instead of std::move to ensure that | 158 // These dispatchers must use Clone() instead of std::move to ensure that |
124 // if there are multiple registered services they all get a copy of the data. | 159 // if there are multiple registered services they all get a copy of the data. |
125 void VRDeviceManager::OnDeviceChanged(VRDisplayPtr device) { | 160 void VRDeviceManager::OnDeviceChanged(VRDisplayPtr device) { |
126 for (const auto& service : services_) | 161 for (const auto& service : services_) |
127 service->client()->OnDisplayChanged(device.Clone()); | 162 service->client()->OnDisplayChanged(device.Clone()); |
128 } | 163 } |
129 | 164 |
165 bool VRDeviceManager::RequestPresent(VRServiceImpl* service, | |
166 unsigned int index) { | |
167 // Is anything presenting currently? | |
168 if (presenting_service_) { | |
169 // Fail if the currently presenting service is not the one making the | |
170 // request. | |
171 if (presenting_service_ != service) | |
172 return false; | |
173 | |
174 // If we are switching presentation from the currently presenting service to | |
175 // a new device stop presening to the previous one. | |
176 if (presenting_device_ && presenting_device_->id() != index) { | |
mthiesse
2016/09/13 18:46:15
Is it possible for there to be a presenting servic
bajones
2016/09/13 19:42:57
Good Call. Done.
| |
177 // Tell the device to stop presenting. | |
178 presenting_device_->ExitPresent(); | |
179 | |
180 // Only the presenting service needs to be notified that presentation is | |
181 // ending on the previous device. | |
182 presenting_service_->client()->OnExitPresent(presenting_device_->id()); | |
183 presenting_device_ = nullptr; | |
184 } | |
185 | |
186 presenting_service_ = nullptr; | |
187 } | |
188 | |
189 VRDevice* requested_device = GetDevice(index); | |
190 // Can't present to a device that doesn't exist. | |
191 if (!requested_device) | |
192 return false; | |
193 | |
194 // Attempt to begin presenting to this device. This could fail for any number | |
195 // of device-specific reasons. | |
196 if (!requested_device->RequestPresent()) | |
197 return false; | |
198 | |
199 // Successfully began presenting! | |
200 presenting_service_ = service; | |
201 presenting_device_ = requested_device; | |
202 | |
203 return true; | |
204 } | |
205 | |
206 void VRDeviceManager::ExitPresent(VRServiceImpl* service, unsigned int index) { | |
207 // Don't allow services other than the currently presenting one to exit | |
208 // presentation. | |
209 if (presenting_service_ != service) | |
210 return; | |
211 | |
212 // Fail if the specified device is not currently presenting. | |
213 if (!presenting_device_ || presenting_device_->id() != index) | |
214 return; | |
215 | |
216 // Tell the device to stop presenting. | |
217 presenting_device_->ExitPresent(); | |
218 presenting_service_->client()->OnExitPresent(index); | |
219 | |
220 // Clear the presenting service and device. | |
221 presenting_service_ = nullptr; | |
222 presenting_device_ = nullptr; | |
223 } | |
224 | |
225 void VRDeviceManager::SubmitFrame(VRServiceImpl* service, | |
226 unsigned int index, | |
227 VRPosePtr pose) { | |
228 // Don't allow services other than the currently presenting one to submit any | |
229 // frames. | |
230 if (presenting_service_ != service) | |
231 return; | |
232 | |
233 // Don't submit frames to devices other than the currently presenting one. | |
234 if (!presenting_device_ || presenting_device_->id() != index) | |
235 return; | |
236 | |
237 presenting_device_->SubmitFrame(std::move(pose)); | |
238 } | |
239 | |
130 void VRDeviceManager::InitializeProviders() { | 240 void VRDeviceManager::InitializeProviders() { |
131 if (vr_initialized_) { | 241 if (vr_initialized_) { |
132 return; | 242 return; |
133 } | 243 } |
134 | 244 |
135 for (const auto& provider : providers_) | 245 for (const auto& provider : providers_) |
136 provider->Initialize(); | 246 provider->Initialize(); |
137 | 247 |
138 vr_initialized_ = true; | 248 vr_initialized_ = true; |
139 } | 249 } |
(...skipping 17 matching lines...) Expand all Loading... | |
157 for (const auto& provider : providers_) | 267 for (const auto& provider : providers_) |
158 provider->PollEvents(); | 268 provider->PollEvents(); |
159 } | 269 } |
160 | 270 |
161 void VRDeviceManager::StopSchedulingPollEvents() { | 271 void VRDeviceManager::StopSchedulingPollEvents() { |
162 if (has_scheduled_poll_) | 272 if (has_scheduled_poll_) |
163 timer_.Stop(); | 273 timer_.Stop(); |
164 } | 274 } |
165 | 275 |
166 } // namespace device | 276 } // namespace device |
OLD | NEW |