| 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 the first initialization, OpenVRRenderLoop instance needs to be |
| 147 // created and the polling event callback needs to be registered. |
| 148 if (!render_loop_) { |
| 149 render_loop_ = std::make_unique<OpenVRRenderLoop>(vr_system_); |
| 150 |
| 151 render_loop_->RegisterPollingEventCallback(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 17 matching lines...) Expand all Loading... |
| 173 int16_t source_height) { | 181 int16_t source_height) { |
| 174 // We don't support presentation currently, so don't do anything. | 182 // We don't support presentation currently, so don't do anything. |
| 175 } | 183 } |
| 176 | 184 |
| 177 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { | 185 void OpenVRDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { |
| 178 render_loop_->Bind(std::move(request)); | 186 render_loop_->Bind(std::move(request)); |
| 179 } | 187 } |
| 180 | 188 |
| 181 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system) | 189 OpenVRDevice::OpenVRRenderLoop::OpenVRRenderLoop(vr::IVRSystem* vr_system) |
| 182 : vr_system_(vr_system), | 190 : vr_system_(vr_system), |
| 191 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 183 binding_(this), | 192 binding_(this), |
| 184 base::SimpleThread("OpenVRRenderLoop") {} | 193 base::SimpleThread("OpenVRRenderLoop") { |
| 194 DCHECK(main_thread_task_runner_); |
| 195 } |
| 185 | 196 |
| 186 void OpenVRDevice::OpenVRRenderLoop::Bind( | 197 void OpenVRDevice::OpenVRRenderLoop::Bind( |
| 187 mojom::VRVSyncProviderRequest request) { | 198 mojom::VRVSyncProviderRequest request) { |
| 188 binding_.Close(); | 199 binding_.Close(); |
| 189 binding_.Bind(std::move(request)); | 200 binding_.Bind(std::move(request)); |
| 190 } | 201 } |
| 191 | 202 |
| 192 void OpenVRDevice::OpenVRRenderLoop::Run() { | 203 void OpenVRDevice::OpenVRRenderLoop::Run() { |
| 193 // TODO (BillOrr): We will wait for VSyncs on this thread using WaitGetPoses | 204 // TODO (BillOrr): We will wait for VSyncs on this thread using WaitGetPoses |
| 194 // when we support presentation. | 205 // when we support presentation. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 pose->position.value()[1] = m[1][3]; | 237 pose->position.value()[1] = m[1][3]; |
| 227 pose->position.value()[2] = m[2][3]; | 238 pose->position.value()[2] = m[2][3]; |
| 228 | 239 |
| 229 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); | 240 pose->linearVelocity = HmdVector3ToWebVR(hmdPose.vVelocity); |
| 230 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); | 241 pose->angularVelocity = HmdVector3ToWebVR(hmdPose.vAngularVelocity); |
| 231 } | 242 } |
| 232 | 243 |
| 233 return std::move(pose); | 244 return std::move(pose); |
| 234 } | 245 } |
| 235 | 246 |
| 247 // Only deal with events that will cause displayInfo changes for now. |
| 248 void OpenVRDevice::OnPollingEvents() { |
| 249 if (!vr_system_ || is_polling_events_) |
| 250 return; |
| 251 |
| 252 // Polling events through vr_system_ has started. |
| 253 is_polling_events_ = true; |
| 254 |
| 255 vr::VREvent_t event; |
| 256 bool is_changed = false; |
| 257 while (vr_system_->PollNextEvent(&event, sizeof(event))) { |
| 258 if (event.trackedDeviceIndex != vr::k_unTrackedDeviceIndex_Hmd && |
| 259 event.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) { |
| 260 continue; |
| 261 } |
| 262 |
| 263 switch (event.eventType) { |
| 264 case vr::VREvent_TrackedDeviceUpdated: |
| 265 case vr::VREvent_IpdChanged: |
| 266 case vr::VREvent_ChaperoneDataHasChanged: |
| 267 case vr::VREvent_ChaperoneSettingsHaveChanged: |
| 268 case vr::VREvent_ChaperoneUniverseHasChanged: |
| 269 is_changed = true; |
| 270 break; |
| 271 |
| 272 default: |
| 273 break; |
| 274 } |
| 275 } |
| 276 |
| 277 // Polling events through vr_system_ has finished. |
| 278 is_polling_events_ = false; |
| 279 |
| 280 if (is_changed) { |
| 281 OnChanged(); |
| 282 } |
| 283 } |
| 284 |
| 285 // Register a callback function to deal with system events. |
| 286 void OpenVRDevice::OpenVRRenderLoop::RegisterPollingEventCallback( |
| 287 const base::Callback<void()>& onPollingEvents) { |
| 288 if (onPollingEvents.is_null()) |
| 289 return; |
| 290 |
| 291 on_polling_events_ = onPollingEvents; |
| 292 } |
| 293 |
| 294 void OpenVRDevice::OpenVRRenderLoop::UnregisterPollingEventCallback() { |
| 295 on_polling_events_.Reset(); |
| 296 } |
| 297 |
| 236 void OpenVRDevice::OpenVRRenderLoop::GetVSync( | 298 void OpenVRDevice::OpenVRRenderLoop::GetVSync( |
| 237 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { | 299 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { |
| 238 static int16_t next_frame = 0; | 300 static int16_t next_frame = 0; |
| 239 int16_t frame = next_frame++; | 301 int16_t frame = next_frame++; |
| 240 | 302 |
| 303 // VSync could be used as a signal to poll events. |
| 304 if (!on_polling_events_.is_null()) { |
| 305 main_thread_task_runner_->PostTask(FROM_HERE, on_polling_events_); |
| 306 } |
| 307 |
| 241 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the | 308 // 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 | 309 // presentation time for the frame. Just returning a default value for now |
| 243 // since we don't have VSync hooked up. | 310 // since we don't have VSync hooked up. |
| 244 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); | 311 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); |
| 245 | 312 |
| 246 device::mojom::VRPosePtr pose = getPose(); | 313 device::mojom::VRPosePtr pose = getPose(); |
| 247 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this | 314 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). | 315 // sleep just throttles vsyncs so we don't fill message queues). |
| 249 callback.Run(std::move(pose), time, frame, | 316 callback.Run(std::move(pose), time, frame, |
| 250 device::mojom::VRVSyncProvider::Status::SUCCESS); | 317 device::mojom::VRVSyncProvider::Status::SUCCESS); |
| 251 } | 318 } |
| 252 | 319 |
| 253 } // namespace device | 320 } // namespace device |
| OLD | NEW |