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

Side by Side Diff: device/vr/openvr/openvr_device.cc

Issue 2834843002: Add event listening function to openvr device (Closed)
Patch Set: Address nhu@ some comments and temporary fix initial vr_system in each OnChange call issue Created 3 years, 8 months 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/openvr/openvr_device.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2017 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 #define _USE_MATH_DEFINES // for M_PI 5 #define _USE_MATH_DEFINES // for M_PI
6 #include "device/vr/openvr/openvr_device.h" 6 #include "device/vr/openvr/openvr_device.h"
7 #include <math.h> 7 #include <math.h>
8 #include "third_party/openvr/src/headers/openvr.h" 8 #include "third_party/openvr/src/headers/openvr.h"
9 9
10 namespace { 10 namespace {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 transform[13] = mat.m[1][3]; 69 transform[13] = mat.m[1][3];
70 transform[14] = mat.m[2][3]; 70 transform[14] = mat.m[2][3];
71 transform[15] = 1.0f; 71 transform[15] = 1.0f;
72 return transform; 72 return transform;
73 } 73 }
74 74
75 } // namespace 75 } // namespace
76 76
77 namespace device { 77 namespace device {
78 78
79 OpenVRDevice::OpenVRDevice() {} 79 OpenVRDevice::OpenVRDevice() : weak_ptr_factory_(this) {}
80 OpenVRDevice::~OpenVRDevice() {} 80 OpenVRDevice::~OpenVRDevice() {}
81 81
82 void OpenVRDevice::CreateVRDisplayInfo( 82 void OpenVRDevice::CreateVRDisplayInfo(
83 const base::Callback<void(mojom::VRDisplayInfoPtr)>& on_created) { 83 const base::Callback<void(mojom::VRDisplayInfoPtr)>& on_created) {
84 vr::EVRInitError init_error; 84 // Initialize init_error with success.
85 vr::EVRInitError init_error = vr::VRInitError_None;
86
87 // vr_system here could be used as openvr device initialization or update
88 // device info.
85 auto vr_system = 89 auto vr_system =
nhu 2017/04/25 00:52:19 In https://codereview.chromium.org/2825203004/, we
86 vr::VR_Init(&init_error, vr::EVRApplicationType::VRApplication_Scene); 90 render_loop_ == nullptr
91 ? vr::VR_Init(&init_error,
92 vr::EVRApplicationType::VRApplication_Scene)
93 : render_loop_->GetVRSystem();
87 94
88 if (init_error != vr::VRInitError_None) { 95 if (init_error != vr::VRInitError_None) {
89 LOG(ERROR) << vr::VR_GetVRInitErrorAsEnglishDescription(init_error); 96 LOG(ERROR) << vr::VR_GetVRInitErrorAsEnglishDescription(init_error);
90 on_created.Run(nullptr); 97 on_created.Run(nullptr);
91 return; 98 return;
92 } 99 }
93 100
101 if (vr_system == nullptr) {
102 LOG(ERROR) << "OpenVRRenderLoop failed to return valid IVRSystem pointer";
103 on_created.Run(nullptr);
104 return;
105 }
106
94 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); 107 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New();
95 device->index = id(); 108 device->index = id();
96 device->displayName = 109 device->displayName =
97 GetOpenVRString(vr_system, vr::Prop_ManufacturerName_String) + " " + 110 GetOpenVRString(vr_system, vr::Prop_ManufacturerName_String) + " " +
98 GetOpenVRString(vr_system, vr::Prop_ModelNumber_String); 111 GetOpenVRString(vr_system, vr::Prop_ModelNumber_String);
99 device->capabilities = mojom::VRDisplayCapabilities::New(); 112 device->capabilities = mojom::VRDisplayCapabilities::New();
100 device->capabilities->hasPosition = true; 113 device->capabilities->hasPosition = true;
101 device->capabilities->hasExternalDisplay = true; 114 device->capabilities->hasExternalDisplay = true;
102 device->capabilities->canPresent = false; 115 device->capabilities->canPresent = false;
103 116
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 153
141 vr::IVRChaperone* chaperone = vr::VRChaperone(); 154 vr::IVRChaperone* chaperone = vr::VRChaperone();
142 if (chaperone) { 155 if (chaperone) {
143 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX, 156 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX,
144 &device->stageParameters->sizeZ); 157 &device->stageParameters->sizeZ);
145 } else { 158 } else {
146 device->stageParameters->sizeX = 0.0f; 159 device->stageParameters->sizeX = 0.0f;
147 device->stageParameters->sizeZ = 0.0f; 160 device->stageParameters->sizeZ = 0.0f;
148 } 161 }
149 162
150 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system); 163 // If it is first initialization, OpenVRRenderLoop instance need to be
164 // careated and callback need to be registed.
165 if (!render_loop_) {
166 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system);
167
168 render_loop_->RegistPollingEventCallback(base::Bind(
169 &OpenVRDevice::OnPollingEvent, weak_ptr_factory_.GetWeakPtr()));
170 }
151 171
152 on_created.Run(std::move(device)); 172 on_created.Run(std::move(device));
153 } 173 }
154 174
155 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client, 175 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client,
156 const base::Callback<void(bool)>& callback) { 176 const base::Callback<void(bool)>& callback) {
157 callback.Run(false); 177 callback.Run(false);
158 // We don't support presentation currently. 178 // We don't support presentation currently.
159 } 179 }
160 180
(...skipping 11 matching lines...) Expand all
172 } 192 }
173 193
174 void OpenVRDevice::UpdateLayerBounds(int16_t frame_index, 194 void OpenVRDevice::UpdateLayerBounds(int16_t frame_index,
175 mojom::VRLayerBoundsPtr left_bounds, 195 mojom::VRLayerBoundsPtr left_bounds,
176 mojom::VRLayerBoundsPtr right_bounds, 196 mojom::VRLayerBoundsPtr right_bounds,
177 int16_t source_width, 197 int16_t source_width,
178 int16_t source_height) { 198 int16_t source_height) {
179 // We don't support presentation currently, so don't do anything. 199 // We don't support presentation currently, so don't do anything.
180 } 200 }
181 201
202 // TODO(shaobo): OnConnect and OnDisconnect need to be added to VrDisplayClient
203 // in vr_service.mojom
204 void OpenVRDevice::OnPollingEvent(OpenVRDevice::VREvent event) {
205 switch (event) {
206 case OpenVRDevice::VREvent::CONNECTED:
207 case OpenVRDevice::VREvent::DISCONNECTED:
208 break;
209
210 case OpenVRDevice::VREvent::CHANGED:
211 OnChanged();
212 break;
213
214 default:
215 break;
216 }
217 }
218
182 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { 219 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) {
183 render_loop_->Bind(std::move(request)); 220 render_loop_->Bind(std::move(request));
184 } 221 }
185 222
186 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system) 223 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system)
187 : vr_system_(vr_system), 224 : vr_system_(vr_system),
188 binding_(this), 225 binding_(this),
189 base::SimpleThread("OpenVRRenderLoop") {} 226 base::SimpleThread("OpenVRRenderLoop") {}
190 227
191 void OpenVRDevice::OpenVRRenderLoop::Bind( 228 void OpenVRDevice::OpenVRRenderLoop::Bind(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 pose->position.value()[1] = m[1][3]; 268 pose->position.value()[1] = m[1][3];
232 pose->position.value()[2] = m[2][3]; 269 pose->position.value()[2] = m[2][3];
233 270
234 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); 271 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity);
235 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); 272 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity);
236 } 273 }
237 274
238 return std::move(pose); 275 return std::move(pose);
239 } 276 }
240 277
278 // TODO(shaobo) : Expose pointer directly will increase the risk that behaviour
279 // out of OpenVRRenderLoop will affect OpenVRRenderLoop. Need to fix it.
280 vr::IVRSystem* OpenVRDevice::OpenVRRenderLoop::GetVRSystem() {
281 return vr_system_;
282 }
283
284 // Only deal with event which will cause displayInfo changed now.
285 void OpenVRDevice::OpenVRRenderLoop::PollEvents() {
286 if (!vr_system_ || on_polling_event_.is_null())
287 return;
288
289 vr::VREvent_t event;
290 bool is_changed = false;
291 while (vr_system_->PollNextEvent(&event, sizeof(event))) {
292 if (event.trackedDeviceIndex != vr::k_unTrackedDeviceIndex_Hmd &&
293 event.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) {
294 continue;
295 }
296
297 switch (event.eventType) {
298 case vr::VREvent_TrackedDeviceActivated:
299 on_polling_event_.Run(OpenVRDevice::VREvent::CONNECTED);
300 break;
301
302 case vr::VREvent_TrackedDeviceDeactivated:
303 on_polling_event_.Run(OpenVRDevice::VREvent::DISCONNECTED);
304 break;
305
306 case vr::VREvent_TrackedDeviceUpdated:
307 case vr::VREvent_IpdChanged:
308 case vr::VREvent_ChaperoneDataHasChanged:
309 case vr::VREvent_ChaperoneSettingsHaveChanged:
310 case vr::VREvent_ChaperoneUniverseHasChanged:
311 is_changed = true;
312 break;
313
314 default:
315 break;
316 }
317 }
318
319 if (is_changed) {
320 on_polling_event_.Run(OpenVRDevice::VREvent::CHANGED);
321 }
322 }
323
324 // Regist a callback function to deal with system event.
325 void OpenVRDevice::OpenVRRenderLoop::RegistPollingEventCallback(
326 const base::Callback<void(OpenVRDevice::VREvent)>& onPollingEvent) {
327 if (onPollingEvent.is_null())
328 return;
329
330 on_polling_event_ = onPollingEvent;
331 }
332
333 void OpenVRDevice::OpenVRRenderLoop::UnregistPollingEventCallback() {
334 on_polling_event_.Reset();
335 }
336
241 void OpenVRDevice::OpenVRRenderLoop::GetVSync( 337 void OpenVRDevice::OpenVRRenderLoop::GetVSync(
242 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { 338 const mojom::VRVSyncProvider::GetVSyncCallback& callback) {
243 static int16_t next_frame = 0; 339 static int16_t next_frame = 0;
244 int16_t frame = next_frame++; 340 int16_t frame = next_frame++;
245 341
342 // VSync could be used as a signal to poll event.
343 PollEvents();
344
246 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the 345 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the
247 // presentation time for the frame. Just returning a default value for now 346 // presentation time for the frame. Just returning a default value for now
248 // since we don't have VSync hooked up. 347 // since we don't have VSync hooked up.
249 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); 348 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0);
250 349
251 device::mojom::VRPosePtr pose = getPose(); 350 device::mojom::VRPosePtr pose = getPose();
252 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this 351 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this
253 // sleep just throttles vsyncs so we don't fill message queues). 352 // sleep just throttles vsyncs so we don't fill message queues).
254 callback.Run(std::move(pose), time, frame, 353 callback.Run(std::move(pose), time, frame,
255 device::mojom::VRVSyncProvider::Status::SUCCESS); 354 device::mojom::VRVSyncProvider::Status::SUCCESS);
256 } 355 }
257 356
258 } // namespace device 357 } // namespace device
OLDNEW
« no previous file with comments | « device/vr/openvr/openvr_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698