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) {} | |
|
billorr1
2017/05/09 15:50:03
What is is_polling_events_ used for? I never see
shaobo.yan
2017/05/10 03:20:33
Thx for this ! The usage of is_polling_events_ is
| |
| 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 |
|
billorr1
2017/05/09 15:50:03
nit:
If it is the first initialization, OpenVRRend
| |
| 147 // created and callback need to be registed. | |
| 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 event which will cause displayInfo changed now. | |
|
billorr1
2017/05/09 15:50:02
Only deal with events that will cause displayInfo
shaobo.yan
2017/05/10 03:20:33
Done.
| |
| 248 void OpenVRDevice::OnPollingEvents() { | |
| 249 if (!vr_system_ || is_polling_events_) | |
| 250 return; | |
| 251 | |
| 252 vr::VREvent_t event; | |
| 253 bool is_changed = false; | |
| 254 while (vr_system_->PollNextEvent(&event, sizeof(event))) { | |
| 255 if (event.trackedDeviceIndex != vr::k_unTrackedDeviceIndex_Hmd && | |
| 256 event.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) { | |
| 257 continue; | |
| 258 } | |
| 259 | |
| 260 switch (event.eventType) { | |
| 261 case vr::VREvent_TrackedDeviceUpdated: | |
| 262 case vr::VREvent_IpdChanged: | |
| 263 case vr::VREvent_ChaperoneDataHasChanged: | |
| 264 case vr::VREvent_ChaperoneSettingsHaveChanged: | |
| 265 case vr::VREvent_ChaperoneUniverseHasChanged: | |
| 266 is_changed = true; | |
| 267 break; | |
| 268 | |
| 269 default: | |
| 270 break; | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 if (is_changed) { | |
| 275 OnChanged(); | |
| 276 } | |
| 277 } | |
| 278 | |
| 279 // Register a callback function to deal with system event. | |
|
billorr1
2017/05/09 15:50:03
nit: system events.
shaobo.yan
2017/05/10 03:20:33
Done.
| |
| 280 void OpenVRDevice::OpenVRRenderLoop::RegisterPollingEventCallback( | |
| 281 const base::Callback<void()>& onPollingEvents) { | |
| 282 if (onPollingEvents.is_null()) | |
| 283 return; | |
| 284 | |
| 285 on_polling_events_ = onPollingEvents; | |
| 286 } | |
| 287 | |
| 288 void OpenVRDevice::OpenVRRenderLoop::UnregisterPollingEventCallback() { | |
| 289 on_polling_events_.Reset(); | |
| 290 } | |
| 291 | |
| 236 void OpenVRDevice::OpenVRRenderLoop::GetVSync( | 292 void OpenVRDevice::OpenVRRenderLoop::GetVSync( |
| 237 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { | 293 const mojom::VRVSyncProvider::GetVSyncCallback& callback) { |
| 238 static int16_t next_frame = 0; | 294 static int16_t next_frame = 0; |
| 239 int16_t frame = next_frame++; | 295 int16_t frame = next_frame++; |
| 240 | 296 |
| 297 // VSync could be used as a signal to poll event. | |
|
billorr1
2017/05/09 15:50:03
Vsync could be used as a signal to poll events.
shaobo.yan
2017/05/10 03:20:33
Done.
| |
| 298 if (!on_polling_events_.is_null()) { | |
| 299 main_thread_task_runner_->PostTask(FROM_HERE, on_polling_events_); | |
| 300 } | |
| 301 | |
| 241 // TODO(BillOrr): Give real values when VSync loop is hooked up. This is the | 302 // 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 | 303 // presentation time for the frame. Just returning a default value for now |
| 243 // since we don't have VSync hooked up. | 304 // since we don't have VSync hooked up. |
| 244 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); | 305 base::TimeDelta time = base::TimeDelta::FromSecondsD(2.0); |
| 245 | 306 |
| 246 device::mojom::VRPosePtr pose = getPose(); | 307 device::mojom::VRPosePtr pose = getPose(); |
| 247 Sleep(11); // TODO (billorr): Use real vsync timing instead of a sleep (this | 308 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). | 309 // sleep just throttles vsyncs so we don't fill message queues). |
| 249 callback.Run(std::move(pose), time, frame, | 310 callback.Run(std::move(pose), time, frame, |
| 250 device::mojom::VRVSyncProvider::Status::SUCCESS); | 311 device::mojom::VRVSyncProvider::Status::SUCCESS); |
| 251 } | 312 } |
| 252 | 313 |
| 253 } // namespace device | 314 } // namespace device |
| OLD | NEW |