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

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

Issue 2834843002: Add event listening function to openvr device (Closed)
Patch Set: 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
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 vr::EVRInitError init_error;
85 auto vr_system = 85 auto vr_system =
86 vr::VR_Init(&init_error, vr::EVRApplicationType::VRApplication_Scene); 86 vr::VR_Init(&init_error, vr::EVRApplicationType::VRApplication_Scene);
87 87
88 if (init_error != vr::VRInitError_None) { 88 if (init_error != vr::VRInitError_None) {
89 LOG(ERROR) << vr::VR_GetVRInitErrorAsEnglishDescription(init_error); 89 LOG(ERROR) << vr::VR_GetVRInitErrorAsEnglishDescription(init_error);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 if (chaperone) { 142 if (chaperone) {
143 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX, 143 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX,
144 &device->stageParameters->sizeZ); 144 &device->stageParameters->sizeZ);
145 } else { 145 } else {
146 device->stageParameters->sizeX = 0.0f; 146 device->stageParameters->sizeX = 0.0f;
147 device->stageParameters->sizeZ = 0.0f; 147 device->stageParameters->sizeZ = 0.0f;
148 } 148 }
149 149
150 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system); 150 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system);
151 151
152 render_loop_->RegistPollingEventCallback(base::Bind(
153 &OpenVRDevice::OnPollingEvent, weak_ptr_factory_.GetWeakPtr()));
154
152 on_created.Run(std::move(device)); 155 on_created.Run(std::move(device));
153 } 156 }
154 157
155 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client, 158 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client,
156 const base::Callback<void(bool)>& callback) { 159 const base::Callback<void(bool)>& callback) {
157 callback.Run(false); 160 callback.Run(false);
158 // We don't support presentation currently. 161 // We don't support presentation currently.
159 } 162 }
160 163
161 void OpenVRDevice::SetSecureOrigin(bool secure_origin) { 164 void OpenVRDevice::SetSecureOrigin(bool secure_origin) {
(...skipping 10 matching lines...) Expand all
172 } 175 }
173 176
174 void OpenVRDevice::UpdateLayerBounds(int16_t frame_index, 177 void OpenVRDevice::UpdateLayerBounds(int16_t frame_index,
175 mojom::VRLayerBoundsPtr left_bounds, 178 mojom::VRLayerBoundsPtr left_bounds,
176 mojom::VRLayerBoundsPtr right_bounds, 179 mojom::VRLayerBoundsPtr right_bounds,
177 int16_t source_width, 180 int16_t source_width,
178 int16_t source_height) { 181 int16_t source_height) {
179 // We don't support presentation currently, so don't do anything. 182 // We don't support presentation currently, so don't do anything.
180 } 183 }
181 184
185 // TODO(shaobo): OnConnect and OnDisconnect need to be added to VrDisplayClient
186 // in vr_service.mojom
187 void OpenVRDevice::OnPollingEvent(OpenVRDevice::VREvent event) {
188 switch (event) {
189 case OpenVRDevice::VREvent::CONNECTED:
190 case OpenVRDevice::VREvent::DISCONNECTED:
191 break;
192
193 case OpenVRDevice::VREvent::CHANGED:
194 OnChanged();
shaobo.yan 2017/04/24 02:27:27 I just find that this will cause vr_system created
195 break;
196
197 default:
198 break;
199 }
200 }
201
182 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { 202 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) {
183 render_loop_->Bind(std::move(request)); 203 render_loop_->Bind(std::move(request));
184 } 204 }
185 205
186 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system) 206 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system)
187 : vr_system_(vr_system), 207 : vr_system_(vr_system),
188 binding_(this), 208 binding_(this),
189 base::SimpleThread("OpenVRRenderLoop") {} 209 base::SimpleThread("OpenVRRenderLoop") {}
190 210
191 void OpenVRDevice::OpenVRRenderLoop::Bind( 211 void OpenVRDevice::OpenVRRenderLoop::Bind(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 pose->position.value()[1] = m[1][3]; 251 pose->position.value()[1] = m[1][3];
232 pose->position.value()[2] = m[2][3]; 252 pose->position.value()[2] = m[2][3];
233 253
234 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); 254 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity);
235 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); 255 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity);
236 } 256 }
237 257
238 return std::move(pose); 258 return std::move(pose);
239 } 259 }
240 260
261 // Only deal with event which will cause displayInfo changed now.
262 void OpenVRDevice::OpenVRRenderLoop::PollEvent() {
263 if (!vr_system_ || on_polling_event_.is_null())
264 return;
265
266 vr::VREvent_t event;
267 bool is_changed = false;
268 while (vr_system_->PollNextEvent(&event, sizeof(event))) {
269 if (event.trackedDeviceIndex != vr::k_unTrackedDeviceIndex_Hmd &&
270 event.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) {
271 continue;
272 }
273
274 switch (event.eventType) {
275 case vr::VREvent_TrackedDeviceActivated:
276 on_polling_event_.Run(OpenVRDevice::VREvent::CONNECTED);
277 break;
278
279 case vr::VREvent_TrackedDeviceDeactivated:
280 on_polling_event_.Run(OpenVRDevice::VREvent::DISCONNECTED);
281 break;
282
283 case vr::VREvent_TrackedDeviceUpdated:
284 case vr::VREvent_IpdChanged:
285 case vr::VREvent_ChaperoneDataHasChanged:
286 case vr::VREvent_ChaperoneSettingsHaveChanged:
287 case vr::VREvent_ChaperoneUniverseHasChanged:
288 is_changed = true;
nhu 2017/04/24 01:47:06 If there is the only one place to trigger Changed
shaobo.yan 2017/04/24 02:27:27 I introduce this bool variable here for the situat
289 break;
290
291 default:
292 break;
293 }
294 }
295
296 if (is_changed) {
297 on_polling_event_.Run(OpenVRDevice::VREvent::CHANGED);
298 }
299 }
300
301 // Regist a callback function to deal with system event.
302 void OpenVRDevice::OpenVRRenderLoop::RegistPollingEventCallback(
303 const base::Callback<void(OpenVRDevice::VREvent)>& onPollingEvent) {
304 if (onPollingEvent.is_null())
305 return;
306
307 on_polling_event_ = onPollingEvent;
308 }
309
310 void OpenVRDevice::OpenVRRenderLoop::UnregistPollingEventCallback() {
311 on_polling_event_.Reset();
312 }
313
241 void OpenVRDevice::OpenVRRenderLoop::GetVSync( 314 void OpenVRDevice::OpenVRRenderLoop::GetVSync(
242 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { 315 const mojom::VRVSyncProvider::GetVSyncCallback& callback) {
243 static int16_t next_frame = 0; 316 static int16_t next_frame = 0;
244 int16_t frame = next_frame++; 317 int16_t frame = next_frame++;
245 318
319 // VSync could be used as a signal to poll event.
320 PollEvent();
321
246 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the 322 // 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 323 // presentation time for the frame. Just returning a default value for now
248 // since we don't have VSync hooked up. 324 // since we don't have VSync hooked up.
249 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); 325 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0);
250 326
251 device::mojom::VRPosePtr pose = getPose(); 327 device::mojom::VRPosePtr pose = getPose();
252 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this 328 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). 329 // sleep just throttles vsyncs so we don't fill message queues).
254 callback.Run(std::move(pose), time, frame, 330 callback.Run(std::move(pose), time, frame,
255 device::mojom::VRVSyncProvider::Status::SUCCESS); 331 device::mojom::VRVSyncProvider::Status::SUCCESS);
256 } 332 }
257 333
258 } // namespace device 334 } // namespace device
OLDNEW
« device/vr/openvr/openvr_device.h ('K') | « device/vr/openvr/openvr_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698