| 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_) { |
| 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 // Should never have a presenting service without a presenting device. |
| 170 DCHECK(presenting_device_); |
| 171 |
| 172 // Fail if the currently presenting service is not the one making the |
| 173 // request. |
| 174 if (presenting_service_ != service) |
| 175 return false; |
| 176 |
| 177 // If we are switching presentation from the currently presenting service to |
| 178 // a new device stop presening to the previous one. |
| 179 if (presenting_device_->id() != index) { |
| 180 // Tell the device to stop presenting. |
| 181 presenting_device_->ExitPresent(); |
| 182 |
| 183 // Only the presenting service needs to be notified that presentation is |
| 184 // ending on the previous device. |
| 185 presenting_service_->client()->OnExitPresent(presenting_device_->id()); |
| 186 presenting_device_ = nullptr; |
| 187 } |
| 188 |
| 189 presenting_service_ = nullptr; |
| 190 } |
| 191 |
| 192 VRDevice* requested_device = GetDevice(index); |
| 193 // Can't present to a device that doesn't exist. |
| 194 if (!requested_device) |
| 195 return false; |
| 196 |
| 197 // Attempt to begin presenting to this device. This could fail for any number |
| 198 // of device-specific reasons. |
| 199 if (!requested_device->RequestPresent()) |
| 200 return false; |
| 201 |
| 202 // Successfully began presenting! |
| 203 presenting_service_ = service; |
| 204 presenting_device_ = requested_device; |
| 205 |
| 206 return true; |
| 207 } |
| 208 |
| 209 void VRDeviceManager::ExitPresent(VRServiceImpl* service, unsigned int index) { |
| 210 // Don't allow services other than the currently presenting one to exit |
| 211 // presentation. |
| 212 if (presenting_service_ != service) |
| 213 return; |
| 214 |
| 215 // Should never have a presenting service without a presenting device. |
| 216 DCHECK(presenting_device_); |
| 217 |
| 218 // Fail if the specified device is not currently presenting. |
| 219 if (presenting_device_->id() != index) |
| 220 return; |
| 221 |
| 222 // Tell the device to stop presenting. |
| 223 presenting_device_->ExitPresent(); |
| 224 presenting_service_->client()->OnExitPresent(index); |
| 225 |
| 226 // Clear the presenting service and device. |
| 227 presenting_service_ = nullptr; |
| 228 presenting_device_ = nullptr; |
| 229 } |
| 230 |
| 231 void VRDeviceManager::SubmitFrame(VRServiceImpl* service, |
| 232 unsigned int index, |
| 233 VRPosePtr pose) { |
| 234 // Don't allow services other than the currently presenting one to submit any |
| 235 // frames. |
| 236 if (presenting_service_ != service) |
| 237 return; |
| 238 |
| 239 // Should never have a presenting service without a presenting device. |
| 240 DCHECK(presenting_device_); |
| 241 |
| 242 // Don't submit frames to devices other than the currently presenting one. |
| 243 if (presenting_device_->id() != index) |
| 244 return; |
| 245 |
| 246 presenting_device_->SubmitFrame(std::move(pose)); |
| 247 } |
| 248 |
| 130 void VRDeviceManager::InitializeProviders() { | 249 void VRDeviceManager::InitializeProviders() { |
| 131 if (vr_initialized_) { | 250 if (vr_initialized_) { |
| 132 return; | 251 return; |
| 133 } | 252 } |
| 134 | 253 |
| 135 for (const auto& provider : providers_) | 254 for (const auto& provider : providers_) |
| 136 provider->Initialize(); | 255 provider->Initialize(); |
| 137 | 256 |
| 138 vr_initialized_ = true; | 257 vr_initialized_ = true; |
| 139 } | 258 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 157 for (const auto& provider : providers_) | 276 for (const auto& provider : providers_) |
| 158 provider->PollEvents(); | 277 provider->PollEvents(); |
| 159 } | 278 } |
| 160 | 279 |
| 161 void VRDeviceManager::StopSchedulingPollEvents() { | 280 void VRDeviceManager::StopSchedulingPollEvents() { |
| 162 if (has_scheduled_poll_) | 281 if (has_scheduled_poll_) |
| 163 timer_.Stop(); | 282 timer_.Stop(); |
| 164 } | 283 } |
| 165 | 284 |
| 166 } // namespace device | 285 } // namespace device |
| OLD | NEW |