Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: device/vr/vr_device_manager.cc

Issue 2488273002: Revert of mojo VR interface simplified (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/vr/vr_device_manager.h ('k') | device/vr/vr_device_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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), 25 : vr_initialized_(false),
26 presenting_service_(nullptr),
27 presenting_device_(nullptr),
26 keep_alive_(false), 28 keep_alive_(false),
27 has_scheduled_poll_(false) { 29 has_scheduled_poll_(false) {
28 // Register VRDeviceProviders for the current platform 30 // Register VRDeviceProviders for the current platform
29 #if defined(OS_ANDROID) 31 #if defined(OS_ANDROID)
30 RegisterProvider(base::WrapUnique(new GvrDeviceProvider())); 32 RegisterProvider(base::WrapUnique(new GvrDeviceProvider()));
31 #endif 33 #endif
32 } 34 }
33 35
34 VRDeviceManager::VRDeviceManager(std::unique_ptr<VRDeviceProvider> provider) 36 VRDeviceManager::VRDeviceManager(std::unique_ptr<VRDeviceProvider> provider)
35 : vr_initialized_(false), 37 : vr_initialized_(false),
38 presenting_service_(nullptr),
39 presenting_device_(nullptr),
36 keep_alive_(true), 40 keep_alive_(true),
37 has_scheduled_poll_(false) { 41 has_scheduled_poll_(false) {
38 thread_checker_.DetachFromThread(); 42 thread_checker_.DetachFromThread();
39 RegisterProvider(std::move(provider)); 43 RegisterProvider(std::move(provider));
40 SetInstance(this); 44 SetInstance(this);
41 } 45 }
42 46
43 VRDeviceManager::~VRDeviceManager() { 47 VRDeviceManager::~VRDeviceManager() {
44 DCHECK(thread_checker_.CalledOnValidThread()); 48 DCHECK(thread_checker_.CalledOnValidThread());
45 StopSchedulingPollEvents(); 49 StopSchedulingPollEvents();
46 g_vr_device_manager = nullptr; 50 g_vr_device_manager = nullptr;
47 } 51 }
48 52
49 VRDeviceManager* VRDeviceManager::GetInstance() { 53 VRDeviceManager* VRDeviceManager::GetInstance() {
50 if (!g_vr_device_manager) 54 if (!g_vr_device_manager)
51 g_vr_device_manager = new VRDeviceManager(); 55 g_vr_device_manager = new VRDeviceManager();
52 return g_vr_device_manager; 56 return g_vr_device_manager;
53 } 57 }
54 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
55 void VRDeviceManager::SetInstance(VRDeviceManager* instance) { 79 void VRDeviceManager::SetInstance(VRDeviceManager* instance) {
56 // 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
57 // 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
58 // non-nullptr and vica versa. 82 // non-nullptr and vica versa.
59 CHECK_NE(!!instance, !!g_vr_device_manager); 83 CHECK_NE(!!instance, !!g_vr_device_manager);
60 g_vr_device_manager = instance; 84 g_vr_device_manager = instance;
61 } 85 }
62 86
63 bool VRDeviceManager::HasInstance() { 87 bool VRDeviceManager::HasInstance() {
64 // For testing. Checks to see if a VRDeviceManager instance is active. 88 // For testing. Checks to see if a VRDeviceManager instance is active.
65 return !!g_vr_device_manager; 89 return !!g_vr_device_manager;
66 } 90 }
67 91
68 void VRDeviceManager::AddService(VRServiceImpl* service) { 92 void VRDeviceManager::AddService(VRServiceImpl* service) {
69 // Loop through any currently active devices and send Connected messages to 93 services_.push_back(service);
70 // the service. Future devices that come online will send a Connected message
71 // when they are created.
72 GetVRDevices(service);
73 94
74 services_.push_back(service); 95 // Ensure that the device providers are initialized
96 InitializeProviders();
75 } 97 }
76 98
77 void VRDeviceManager::RemoveService(VRServiceImpl* service) { 99 void VRDeviceManager::RemoveService(VRServiceImpl* service) {
78 services_.erase(std::remove(services_.begin(), services_.end(), service), 100 services_.erase(std::remove(services_.begin(), services_.end(), service),
79 services_.end()); 101 services_.end());
80 102
81 for (auto device : devices_) { 103 if (service == presenting_service_) {
82 device.second->RemoveService(service); 104 presenting_device_->ExitPresent();
105
106 presenting_service_ = nullptr;
107 presenting_device_ = nullptr;
83 } 108 }
84 109
85 if (services_.empty() && !keep_alive_) { 110 if (services_.empty() && !keep_alive_) {
86 // Delete the device manager when it has no active connections. 111 // Delete the device manager when it has no active connections.
87 delete g_vr_device_manager; 112 delete g_vr_device_manager;
88 } 113 }
89 } 114 }
90 115
91 bool VRDeviceManager::GetVRDevices(VRServiceImpl* service) { 116 mojo::Array<VRDisplayPtr> VRDeviceManager::GetVRDevices() {
92 DCHECK(thread_checker_.CalledOnValidThread()); 117 DCHECK(thread_checker_.CalledOnValidThread());
93 118
94 InitializeProviders(); 119 InitializeProviders();
95 120
96 std::vector<VRDevice*> devices; 121 std::vector<VRDevice*> devices;
97 for (const auto& provider : providers_) 122 for (const auto& provider : providers_)
98 provider->GetDevices(&devices); 123 provider->GetDevices(&devices);
99 124
100 if (devices.empty()) 125 mojo::Array<VRDisplayPtr> out_devices;
101 return false;
102
103 for (auto* device : devices) { 126 for (auto* device : devices) {
104 if (device->id() == VR_DEVICE_LAST_ID) 127 if (device->id() == VR_DEVICE_LAST_ID)
105 continue; 128 continue;
106 129
107 if (devices_.find(device->id()) == devices_.end()) 130 if (devices_.find(device->id()) == devices_.end())
108 devices_[device->id()] = device; 131 devices_[device->id()] = device;
109 132
110 device->AddService(service); 133 VRDisplayPtr vr_device_info = device->GetVRDevice();
134 if (vr_device_info.is_null())
135 continue;
136
137 // GetVRDevice should always set the index of the VRDisplay to its own id.
138 DCHECK(vr_device_info->index == device->id());
139
140 out_devices.push_back(std::move(vr_device_info));
111 } 141 }
112 142
113 return true; 143 return out_devices;
114 }
115
116 unsigned int VRDeviceManager::GetNumberOfConnectedDevices() {
117 DCHECK(thread_checker_.CalledOnValidThread());
118
119 return devices_.size();
120 } 144 }
121 145
122 VRDevice* VRDeviceManager::GetDevice(unsigned int index) { 146 VRDevice* VRDeviceManager::GetDevice(unsigned int index) {
123 DCHECK(thread_checker_.CalledOnValidThread()); 147 DCHECK(thread_checker_.CalledOnValidThread());
124 148
125 if (index == 0) { 149 if (index == 0) {
126 return NULL; 150 return NULL;
127 } 151 }
128 152
129 DeviceMap::iterator iter = devices_.find(index); 153 DeviceMap::iterator iter = devices_.find(index);
130 if (iter == devices_.end()) { 154 if (iter == devices_.end()) {
131 return nullptr; 155 return nullptr;
132 } 156 }
133 return iter->second; 157 return iter->second;
134 } 158 }
135 159
160 // These dispatchers must use Clone() instead of std::move to ensure that
161 // if there are multiple registered services they all get a copy of the data.
162 void VRDeviceManager::OnDeviceChanged(VRDisplayPtr device) {
163 for (const auto& service : services_)
164 service->client()->OnDisplayChanged(device.Clone());
165 }
166
167 bool VRDeviceManager::RequestPresent(VRServiceImpl* service,
168 unsigned int index,
169 bool secure_origin) {
170 // Is anything presenting currently?
171 if (presenting_service_) {
172 // Should never have a presenting service without a presenting device.
173 DCHECK(presenting_device_);
174
175 // Fail if the currently presenting service is not the one making the
176 // request.
177 if (presenting_service_ != service)
178 return false;
179
180 // If we are switching presentation from the currently presenting service to
181 // a new device stop presening to the previous one.
182 if (presenting_device_->id() != index) {
183 // Tell the device to stop presenting.
184 presenting_device_->ExitPresent();
185
186 // Only the presenting service needs to be notified that presentation is
187 // ending on the previous device.
188 presenting_service_->client()->OnExitPresent(presenting_device_->id());
189 presenting_device_ = nullptr;
190 }
191
192 presenting_service_ = nullptr;
193 }
194
195 VRDevice* requested_device = GetDevice(index);
196 // Can't present to a device that doesn't exist.
197 if (!requested_device)
198 return false;
199
200 // Attempt to begin presenting to this device. This could fail for any number
201 // of device-specific reasons.
202 if (!requested_device->RequestPresent(secure_origin))
203 return false;
204
205 // Successfully began presenting!
206 presenting_service_ = service;
207 presenting_device_ = requested_device;
208
209 return true;
210 }
211
212 void VRDeviceManager::ExitPresent(VRServiceImpl* service, unsigned int index) {
213 // Don't allow services other than the currently presenting one to exit
214 // presentation.
215 if (presenting_service_ != service)
216 return;
217
218 // Should never have a presenting service without a presenting device.
219 DCHECK(presenting_device_);
220
221 // Fail if the specified device is not currently presenting.
222 if (presenting_device_->id() != index)
223 return;
224
225 // Tell the client we're done. This must happen before the device's
226 // ExitPresent to avoid invalid mojo state.
227 if (presenting_service_->client()) {
228 presenting_service_->client()->OnExitPresent(index);
229 }
230
231 // Tell the device to stop presenting.
232 presenting_device_->ExitPresent();
233
234 // Clear the presenting service and device.
235 presenting_service_ = nullptr;
236 presenting_device_ = nullptr;
237 }
238
239 void VRDeviceManager::SubmitFrame(VRServiceImpl* service,
240 unsigned int index,
241 VRPosePtr pose) {
242 // Don't allow services other than the currently presenting one to submit any
243 // frames.
244 if (presenting_service_ != service)
245 return;
246
247 // Should never have a presenting service without a presenting device.
248 DCHECK(presenting_device_);
249
250 // Don't submit frames to devices other than the currently presenting one.
251 if (presenting_device_->id() != index)
252 return;
253
254 presenting_device_->SubmitFrame(std::move(pose));
255 }
256
257 void VRDeviceManager::OnDeviceConnectionStatusChanged(VRDevice* device,
258 bool is_connected) {
259 if (is_connected) {
260 VRDisplayPtr vr_device_info = device->GetVRDevice();
261 if (vr_device_info.is_null())
262 return;
263
264 vr_device_info->index = device->id();
265
266 for (const auto& service : services_)
267 service->client()->OnDisplayConnected(vr_device_info.Clone());
268 } else {
269 for (const auto& service : services_)
270 service->client()->OnDisplayDisconnected(device->id());
271 }
272 }
273
274 void VRDeviceManager::OnPresentEnded(VRDevice* device) {
275 // Ensure the presenting device is the one that we've been requested to stop.
276 if (!presenting_device_ || presenting_device_ != device)
277 return;
278
279 // Should never have a presenting device without a presenting service.
280 DCHECK(presenting_service_);
281
282 // Notify the presenting service that it's been forced to end presentation.
283 presenting_service_->client()->OnExitPresent(device->id());
284
285 // Clear the presenting service and device.
286 presenting_service_ = nullptr;
287 presenting_device_ = nullptr;
288 }
289
136 void VRDeviceManager::InitializeProviders() { 290 void VRDeviceManager::InitializeProviders() {
137 if (vr_initialized_) { 291 if (vr_initialized_) {
138 return; 292 return;
139 } 293 }
140 294
141 for (const auto& provider : providers_) { 295 for (const auto& provider : providers_) {
296 provider->SetClient(this);
142 provider->Initialize(); 297 provider->Initialize();
143 } 298 }
144 299
145 vr_initialized_ = true; 300 vr_initialized_ = true;
146 } 301 }
147 302
148 void VRDeviceManager::RegisterProvider( 303 void VRDeviceManager::RegisterProvider(
149 std::unique_ptr<VRDeviceProvider> provider) { 304 std::unique_ptr<VRDeviceProvider> provider) {
150 providers_.push_back(make_linked_ptr(provider.release())); 305 providers_.push_back(make_linked_ptr(provider.release()));
151 } 306 }
(...skipping 12 matching lines...) Expand all
164 for (const auto& provider : providers_) 319 for (const auto& provider : providers_)
165 provider->PollEvents(); 320 provider->PollEvents();
166 } 321 }
167 322
168 void VRDeviceManager::StopSchedulingPollEvents() { 323 void VRDeviceManager::StopSchedulingPollEvents() {
169 if (has_scheduled_poll_) 324 if (has_scheduled_poll_)
170 timer_.Stop(); 325 timer_.Stop();
171 } 326 }
172 327
173 } // namespace device 328 } // namespace device
OLDNEW
« no previous file with comments | « device/vr/vr_device_manager.h ('k') | device/vr/vr_device_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698