Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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(vr::IVRSystem* vr) : vr_system_(vr) {} | 79 OpenVRDevice::OpenVRDevice(vr::IVRSystem* vr) |
| 80 : vr_system_(vr), weak_ptr_factory_(this), is_polling_events_(false) {} | |
| 80 OpenVRDevice::~OpenVRDevice() {} | 81 OpenVRDevice::~OpenVRDevice() {} |
| 81 | 82 |
| 82 void OpenVRDevice::CreateVRDisplayInfo( | 83 void OpenVRDevice::CreateVRDisplayInfo( |
| 83 const base::Callback<void(mojom::VRDisplayInfoPtr)>& on_created) { | 84 const base::Callback<void(mojom::VRDisplayInfoPtr)>& on_created) { |
| 84 if (!vr_system_) { | 85 if (!vr_system_) { |
| 85 on_created.Run(nullptr); | 86 on_created.Run(nullptr); |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 | 89 |
| 89 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); | 90 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 | 136 |
| 136 vr::IVRChaperone* chaperone = vr::VRChaperone(); | 137 vr::IVRChaperone* chaperone = vr::VRChaperone(); |
| 137 if (chaperone) { | 138 if (chaperone) { |
| 138 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX, | 139 chaperone->GetPlayAreaSize(&device->stageParameters->sizeX, |
| 139 &device->stageParameters->sizeZ); | 140 &device->stageParameters->sizeZ); |
| 140 } else { | 141 } else { |
| 141 device->stageParameters->sizeX = 0.0f; | 142 device->stageParameters->sizeX = 0.0f; |
| 142 device->stageParameters->sizeZ = 0.0f; | 143 device->stageParameters->sizeZ = 0.0f; |
| 143 } | 144 } |
| 144 | 145 |
| 145 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system_); | 146 // If it is first initialization, OpenVRRenderLoop instance need to be |
| 147 // careated and callback need to be registed. | |
|
bajones
2017/04/26 18:06:05
careated => created, I assume
| |
| 148 if (!render_loop_) { | |
| 149 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system_); | |
| 150 | |
| 151 render_loop_->RegistPollingEventCallback(base::Bind( | |
| 152 &OpenVRDevice::OnPollingEvents, weak_ptr_factory_.GetWeakPtr())); | |
| 153 } | |
| 146 | 154 |
| 147 on_created.Run(std::move(device)); | 155 on_created.Run(std::move(device)); |
| 148 } | 156 } |
| 149 | 157 |
| 150 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client, | 158 void OpenVRDevice::RequestPresent(mojom::VRSubmitFrameClientPtr submit_client, |
| 151 const base::Callback<void(bool)>& callback) { | 159 const base::Callback<void(bool)>& callback) { |
| 152 callback.Run(false); | 160 callback.Run(false); |
| 153 // We don't support presentation currently. | 161 // We don't support presentation currently. |
| 154 } | 162 } |
| 155 | 163 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 pose->position.value()[1] = m[1][3]; | 234 pose->position.value()[1] = m[1][3]; |
| 227 pose->position.value()[2] = m[2][3]; | 235 pose->position.value()[2] = m[2][3]; |
| 228 | 236 |
| 229 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); | 237 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); |
| 230 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); | 238 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); |
| 231 } | 239 } |
| 232 | 240 |
| 233 return std::move(pose); | 241 return std::move(pose); |
| 234 } | 242 } |
| 235 | 243 |
| 244 // Only deal with event which will cause displayInfo changed now. | |
| 245 void OpenVRDevice::OnPollingEvents() { | |
| 246 if (!vr_system_ || is_polling_events_) | |
| 247 return; | |
| 248 | |
| 249 vr::VREvent_t event; | |
| 250 bool is_changed = false; | |
| 251 while (vr_system_->PollNextEvent(&event, sizeof(event))) { | |
| 252 if (event.trackedDeviceIndex != vr::k_unTrackedDeviceIndex_Hmd && | |
| 253 event.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) { | |
| 254 continue; | |
| 255 } | |
| 256 | |
| 257 switch (event.eventType) { | |
| 258 case vr::VREvent_TrackedDeviceUpdated: | |
| 259 case vr::VREvent_IpdChanged: | |
| 260 case vr::VREvent_ChaperoneDataHasChanged: | |
| 261 case vr::VREvent_ChaperoneSettingsHaveChanged: | |
| 262 case vr::VREvent_ChaperoneUniverseHasChanged: | |
| 263 is_changed = true; | |
| 264 break; | |
| 265 | |
| 266 default: | |
| 267 break; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 if (is_changed) { | |
| 272 OnChanged(); | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 // Regist a callback function to deal with system event. | |
| 277 void OpenVRDevice::OpenVRRenderLoop::RegistPollingEventCallback( | |
| 278 const base::Callback<void()>& onPollingEvents) { | |
| 279 if (onPollingEvents.is_null()) | |
| 280 return; | |
| 281 | |
| 282 on_polling_events_ = onPollingEvents; | |
| 283 } | |
| 284 | |
| 285 void OpenVRDevice::OpenVRRenderLoop::UnregistPollingEventCallback() { | |
| 286 on_polling_events_.Reset(); | |
| 287 } | |
| 288 | |
| 236 void OpenVRDevice::OpenVRRenderLoop::GetVSync( | 289 void OpenVRDevice::OpenVRRenderLoop::GetVSync( |
| 237 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { | 290 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { |
| 238 static int16_t next_frame = 0; | 291 static int16_t next_frame = 0; |
| 239 int16_t frame = next_frame++; | 292 int16_t frame = next_frame++; |
| 240 | 293 |
| 294 // VSync could be used as a signal to poll event. | |
| 295 if (!on_polling_events_.is_null()) { | |
| 296 on_polling_events_.Run(); | |
|
bajones
2017/04/26 18:06:05
This does not appear to be thread safe to me, give
mthiesse
2017/04/26 18:14:16
Alternatively you can post to an anonymous-namespa
| |
| 297 } | |
| 298 | |
| 241 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the | 299 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the |
| 242 // presentation time for the frame. Just returning a default value for now | 300 // presentation time for the frame. Just returning a default value for now |
| 243 // since we don't have VSync hooked up. | 301 // since we don't have VSync hooked up. |
| 244 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); | 302 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); |
| 245 | 303 |
| 246 device::mojom::VRPosePtr pose = getPose(); | 304 device::mojom::VRPosePtr pose = getPose(); |
| 247 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this | 305 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this |
| 248 // sleep just throttles vsyncs so we don't fill message queues). | 306 // sleep just throttles vsyncs so we don't fill message queues). |
| 249 callback.Run(std::move(pose), time, frame, | 307 callback.Run(std::move(pose), time, frame, |
| 250 device::mojom::VRVSyncProvider::Status::SUCCESS); | 308 device::mojom::VRVSyncProvider::Status::SUCCESS); |
| 251 } | 309 } |
| 252 | 310 |
| 253 } // namespace device | 311 } // namespace device |
| OLD | NEW |