-
Notifications
You must be signed in to change notification settings - Fork 215
/
ExternalVR.cpp
525 lines (460 loc) · 18 KB
/
ExternalVR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ExternalVR.h"
#include "VRBrowser.h"
#include "vrb/Matrix.h"
#include "vrb/Quaternion.h"
#include "vrb/Vector.h"
#include "moz_external_vr.h"
#include <pthread.h>
#include <unistd.h>
namespace {
const float SecondsToNanoseconds = 1e9f;
const int SecondsToNanosecondsI32 = int(1e9);
const int MicrosecondsToNanoseconds = 1000;
class Lock {
pthread_mutex_t* mMutex;
bool mLocked;
public:
Lock() = delete;
explicit Lock(pthread_mutex_t* aMutex) : mMutex(aMutex), mLocked(false) {
if (pthread_mutex_lock(mMutex) == 0) {
mLocked = true;
}
}
~Lock() {
if (mLocked) {
pthread_mutex_unlock(mMutex);
}
}
bool IsLocked() {
return mLocked;
}
private:
VRB_NO_DEFAULTS(Lock)
VRB_NO_NEW_DELETE
};
class Wait {
pthread_mutex_t* mMutex;
pthread_cond_t* mCond;
bool mLocked;
public:
Wait() = delete;
Wait(pthread_mutex_t* aMutex, pthread_cond_t* aCond)
: mMutex(aMutex)
, mCond(aCond)
, mLocked(false)
{}
~Wait() {
if (mLocked) {
pthread_mutex_unlock(mMutex);
}
}
bool DoWait(const float aWait) {
if (mLocked || pthread_mutex_lock(mMutex) == 0) {
mLocked = true;
if (aWait == 0.0f) {
return pthread_cond_wait(mCond, mMutex) == 0;
} else {
float sec = 0;
float nsec = modff(aWait, &sec);
struct timeval tv = {};
struct timespec ts = {};
gettimeofday(&tv, nullptr);
ts.tv_sec = tv.tv_sec + int(sec);
ts.tv_nsec = (tv.tv_usec * MicrosecondsToNanoseconds) + int(SecondsToNanoseconds * nsec);
if (ts.tv_nsec >= SecondsToNanosecondsI32) {
ts.tv_nsec -= SecondsToNanosecondsI32;
ts.tv_sec++;
}
return pthread_cond_timedwait(mCond, mMutex, &ts) == 0;
}
}
return false;
}
bool IsLocked() {
return mLocked;
}
void Lock() {
if (mLocked) {
return;
}
if (pthread_mutex_lock(mMutex) == 0) {
mLocked = true;
}
}
void Unlock() {
if (mLocked) {
mLocked = false;
pthread_mutex_unlock(mMutex);
}
}
private:
VRB_NO_DEFAULTS(Wait)
VRB_NO_NEW_DELETE
};
} // namespace
namespace crow {
struct ExternalVR::State {
static ExternalVR::State* sState;
pthread_mutex_t* browserMutex = nullptr;
pthread_cond_t* browserCond = nullptr;
mozilla::gfx::VRBrowserState* sourceBrowserState = nullptr;
mozilla::gfx::VRExternalShmem data = {};
mozilla::gfx::VRSystemState system = {};
mozilla::gfx::VRBrowserState browser = {};
// device::CapabilityFlags deviceCapabilities = 0;
vrb::Vector eyeOffsets[device::EyeCount];
uint64_t lastFrameId = 0;
bool firstPresentingFrame = false;
bool compositorEnabled = false;
bool waitingForExit = false;
State() {
pthread_mutex_init(&data.systemMutex, nullptr);
pthread_mutex_init(&data.geckoMutex, nullptr);
pthread_mutex_init(&data.servoMutex, nullptr);
pthread_cond_init(&data.systemCond, nullptr);
pthread_cond_init(&data.geckoCond, nullptr);
pthread_cond_init(&data.servoCond, nullptr);
}
~State() {
pthread_mutex_destroy(&(data.systemMutex));
pthread_mutex_destroy(&(data.geckoMutex));
pthread_mutex_destroy(&(data.servoMutex));
pthread_cond_destroy(&(data.systemCond));
pthread_cond_destroy(&(data.geckoCond));
pthread_cond_destroy(&(data.servoCond));
}
void Reset() {
memset(&data, 0, sizeof(mozilla::gfx::VRExternalShmem));
memset(&system, 0, sizeof(mozilla::gfx::VRSystemState));
memset(&browser, 0, sizeof(mozilla::gfx::VRBrowserState));
data.version = mozilla::gfx::kVRExternalVersion;
data.size = sizeof(mozilla::gfx::VRExternalShmem);
system.displayState.isConnected = true;
system.displayState.isMounted = true;
const vrb::Matrix identity = vrb::Matrix::Identity();
memcpy(&(system.sensorState.leftViewMatrix), identity.Data(), sizeof(system.sensorState.leftViewMatrix));
memcpy(&(system.sensorState.rightViewMatrix), identity.Data(), sizeof(system.sensorState.rightViewMatrix));
system.sensorState.pose.orientation[3] = 1.0f;
lastFrameId = 0;
firstPresentingFrame = false;
waitingForExit = false;
SetSourceBrowser(VRBrowserType::Gecko);
}
static ExternalVR::State& Instance() {
if (!sState) {
sState = new State();
}
return *sState;
}
void PullBrowserStateWhileLocked() {
const bool wasPresenting = IsPresenting();
memcpy(&browser, sourceBrowserState, sizeof(mozilla::gfx::VRBrowserState));
if ((!wasPresenting && IsPresenting()) || browser.navigationTransitionActive) {
firstPresentingFrame = true;
}
if (wasPresenting && !IsPresenting()) {
lastFrameId = browser.layerState[0].layer_stereo_immersive.frameId;
waitingForExit = false;
}
}
bool IsPresenting() const {
return browser.presentationActive || browser.navigationTransitionActive || browser.layerState[0].type == mozilla::gfx::VRLayerType::LayerType_Stereo_Immersive;
}
void SetSourceBrowser(VRBrowserType aBrowser) {
if (aBrowser == VRBrowserType::Gecko) {
browserCond = &data.geckoCond;
browserMutex = &data.geckoMutex;
sourceBrowserState = &data.geckoState;
} else {
browserCond = &data.servoCond;
browserMutex = &data.servoMutex;
sourceBrowserState = &data.servoState;
}
}
};
ExternalVR::State * ExternalVR::State::sState = nullptr;
ExternalVRPtr
ExternalVR::Create() {
return std::make_shared<ExternalVR>();
}
mozilla::gfx::VRExternalShmem*
ExternalVR::GetSharedData() {
return &(m.data);
}
void
ExternalVR::SetDeviceName(const std::string& aName) {
if (aName.length() == 0) {
return;
}
strncpy(m.system.displayState.displayName, aName.c_str(),
mozilla::gfx::kVRDisplayNameMaxLen - 1);
m.system.displayState.displayName[mozilla::gfx::kVRDisplayNameMaxLen - 1] = '\0';
}
void
ExternalVR::SetCapabilityFlags(const device::CapabilityFlags aFlags) {
uint16_t result = 0;
if (device::Position & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_Position);
}
if (device::Orientation & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_Orientation);
}
if (device::Present & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_Present);
}
if (device::AngularAcceleration & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_AngularAcceleration);
}
if (device::LinearAcceleration & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_LinearAcceleration);
}
if (device::StageParameters & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_StageParameters);
}
if (device::MountDetection & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_MountDetection);
}
if (device::PositionEmulated & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
}
//m.deviceCapabilities = aFlags;
m.system.displayState.capabilityFlags = static_cast<mozilla::gfx::VRDisplayCapabilityFlags>(result);
m.system.sensorState.flags = m.system.displayState.capabilityFlags;
}
void
ExternalVR::SetFieldOfView(const device::Eye aEye, const double aLeftDegrees,
const double aRightDegrees,
const double aTopDegrees,
const double aBottomDegrees) {
mozilla::gfx::VRDisplayState::Eye which = (aEye == device::Eye::Right
? mozilla::gfx::VRDisplayState::Eye_Right
: mozilla::gfx::VRDisplayState::Eye_Left);
m.system.displayState.eyeFOV[which].upDegrees = aTopDegrees;
m.system.displayState.eyeFOV[which].rightDegrees = aRightDegrees;
m.system.displayState.eyeFOV[which].downDegrees = aBottomDegrees;
m.system.displayState.eyeFOV[which].leftDegrees = aLeftDegrees;
}
void
ExternalVR::SetEyeOffset(const device::Eye aEye, const float aX, const float aY, const float aZ) {
mozilla::gfx::VRDisplayState::Eye which = (aEye == device::Eye::Right
? mozilla::gfx::VRDisplayState::Eye_Right
: mozilla::gfx::VRDisplayState::Eye_Left);
m.system.displayState.eyeTranslation[which].x = aX;
m.system.displayState.eyeTranslation[which].y = aY;
m.system.displayState.eyeTranslation[which].z = aZ;
m.eyeOffsets[device::EyeIndex(aEye)].Set(aX, aY, aZ);
}
void
ExternalVR::SetEyeResolution(const int32_t aWidth, const int32_t aHeight) {
m.system.displayState.eyeResolution.width = aWidth;
m.system.displayState.eyeResolution.height = aHeight;
}
void
ExternalVR::SetSittingToStandingTransform(const vrb::Matrix& aTransform) {
memcpy(&(m.system.displayState.sittingToStandingTransform), aTransform.Data(), sizeof(m.system.displayState.sittingToStandingTransform));
}
void
ExternalVR::PushSystemState() {
Lock lock(&(m.data.systemMutex));
if (lock.IsLocked()) {
memcpy(&(m.data.state), &(m.system), sizeof(mozilla::gfx::VRSystemState));
pthread_cond_signal(&m.data.systemCond);
}
}
void
ExternalVR::PullBrowserState() {
Lock lock(m.browserMutex);
if (lock.IsLocked()) {
m.PullBrowserStateWhileLocked();
}
}
void
ExternalVR::SetSourceBrowser(VRBrowserType aBrowser) {
m.SetSourceBrowser(aBrowser);
}
void
ExternalVR::SetCompositorEnabled(bool aEnabled) {
if (aEnabled == m.compositorEnabled) {
return;
}
m.compositorEnabled = aEnabled;
if (aEnabled) {
VRBrowser::ResumeCompositor();
} else {
// Set suppressFrames to avoid a deadlock between the compositor sync pause call and the gfxVRExternal SubmitFrame result wait.
m.system.displayState.suppressFrames = true;
m.system.displayState.lastSubmittedFrameId = 0;
m.lastFrameId = 0;
PushSystemState();
VRBrowser::PauseCompositor();
m.system.displayState.suppressFrames = false;
PushSystemState();
}
}
bool
ExternalVR::IsPresenting() const {
return m.IsPresenting();
}
uint16_t
ExternalVR::GetControllerCapabilityFlags(device::CapabilityFlags aFlags) {
uint16_t result = 0;
if (device::Position & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_Position);
}
if (device::Orientation & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_Orientation);
}
if (device::AngularAcceleration & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_AngularAcceleration);
}
if (device::LinearAcceleration & aFlags) {
result |= static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_LinearAcceleration);
}
return result;
}
ExternalVR::VRState
ExternalVR::GetVRState() const {
if (!IsPresenting()) {
return VRState::NotPresenting;
} else if (m.browser.navigationTransitionActive) {
return VRState::LinkTraversal;
} else if (m.firstPresentingFrame || m.waitingForExit || m.browser.layerState[0].type != mozilla::gfx::VRLayerType::LayerType_Stereo_Immersive) {
return VRState::Loading;
}
return VRState::Rendering;
}
void
ExternalVR::PushFramePoses(const vrb::Matrix& aHeadTransform, const std::vector<Controller>& aControllers, const double aTimestamp) {
const vrb::Matrix inverseHeadTransform = aHeadTransform.Inverse();
vrb::Quaternion quaternion(inverseHeadTransform);
vrb::Vector translation = aHeadTransform.GetTranslation();
memcpy(&(m.system.sensorState.pose.orientation), quaternion.Data(),
sizeof(m.system.sensorState.pose.orientation));
memcpy(&(m.system.sensorState.pose.position), translation.Data(),
sizeof(m.system.sensorState.pose.position));
m.system.sensorState.inputFrameID++;
m.system.displayState.lastSubmittedFrameId = m.lastFrameId;
vrb::Matrix leftView = vrb::Matrix::Position(-m.eyeOffsets[device::EyeIndex(device::Eye::Left)]).PostMultiply(inverseHeadTransform);
vrb::Matrix rightView = vrb::Matrix::Position(-m.eyeOffsets[device::EyeIndex(device::Eye::Right)]).PostMultiply(inverseHeadTransform);
memcpy(&(m.system.sensorState.leftViewMatrix), leftView.Data(),
sizeof(m.system.sensorState.leftViewMatrix));
memcpy(&(m.system.sensorState.rightViewMatrix), rightView.Data(),
sizeof(m.system.sensorState.rightViewMatrix));
memset(m.system.controllerState, 0, sizeof(m.system.controllerState));
for (int i = 0; i < aControllers.size(); ++i) {
const Controller& controller = aControllers[i];
if (controller.immersiveName.empty() || !controller.enabled) {
continue;
}
mozilla::gfx::VRControllerState& immersiveController = m.system.controllerState[i];
memcpy(immersiveController.controllerName, controller.immersiveName.c_str(), controller.immersiveName.size() + 1);
immersiveController.numButtons = controller.numButtons;
immersiveController.buttonPressed = controller.immersivePressedState;
immersiveController.buttonTouched = controller.immersiveTouchedState;
for (int i = 0; i< controller.numButtons; ++i) {
immersiveController.triggerValue[i] = controller.immersiveTriggerValues[i];
}
immersiveController.numAxes = controller.numAxes;
for (int i = 0; i< controller.numAxes; ++i) {
immersiveController.axisValue[i] = controller.immersiveAxes[i];
}
immersiveController.numHaptics = controller.numHaptics;
immersiveController.hand = controller.leftHanded ? mozilla::gfx::ControllerHand::Left : mozilla::gfx::ControllerHand::Right;
const uint16_t flags = GetControllerCapabilityFlags(controller.deviceCapabilities);
immersiveController.flags = static_cast<mozilla::gfx::ControllerCapabilityFlags>(flags);
if (flags & static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_Orientation)) {
immersiveController.isOrientationValid = true;
vrb::Quaternion quaternion(controller.transformMatrix);
quaternion = quaternion.Inverse();
memcpy(&(immersiveController.pose.orientation), quaternion.Data(), sizeof(immersiveController.pose.orientation));
}
if (flags & static_cast<uint16_t>(mozilla::gfx::ControllerCapabilityFlags::Cap_Position)) {
immersiveController.isPositionValid = true;
vrb::Vector position(controller.transformMatrix.GetTranslation());
memcpy(&(immersiveController.pose.position), position.Data(), sizeof(immersiveController.pose.position));
}
}
m.system.sensorState.timestamp = aTimestamp;
PushSystemState();
}
bool
ExternalVR::WaitFrameResult() {
Wait wait(m.browserMutex, m.browserCond);
wait.Lock();
// browserMutex is locked in wait.lock().
m.PullBrowserStateWhileLocked();
while (true) {
if (!IsPresenting() || m.browser.layerState[0].layer_stereo_immersive.frameId != m.lastFrameId) {
m.firstPresentingFrame = false;
m.system.displayState.lastSubmittedFrameSuccessful = true;
m.system.displayState.lastSubmittedFrameId = m.browser.layerState[0].layer_stereo_immersive.frameId;
// VRB_LOG("RequestFrame BREAK %llu", m.browser.layerState[0].layer_stereo_immersive.frameId);
break;
}
if (m.firstPresentingFrame) {
return true; // Do not block to show loading screen until the first frame arrives.
}
// VRB_LOG("RequestFrame ABOUT TO WAIT FOR FRAME %llu %llu",m.browser.layerState[0].layer_stereo_immersive.frameId, m.lastFrameId);
const float kConditionTimeout = 0.1f;
// Wait causes the current thread to block until the condition variable is notified or the timeout happens.
// Waiting for the condition variable releases the mutex atomically. So GV can modify the browser data.
if (!wait.DoWait(kConditionTimeout)) {
return false;
}
// VRB_LOG("RequestFrame DONE TO WAIT FOR FRAME");
// browserMutex lock is reacquired again after the condition variable wait exits.
m.PullBrowserStateWhileLocked();
}
m.lastFrameId = m.browser.layerState[0].layer_stereo_immersive.frameId;
return true;
}
void
ExternalVR::CompleteEnumeration()
{
m.system.enumerationCompleted = true;
}
void
ExternalVR::GetFrameResult(int32_t& aSurfaceHandle, int32_t& aTextureWidth, int32_t& aTextureHeight,
device::EyeRect& aLeftEye, device::EyeRect& aRightEye) const {
aSurfaceHandle = (int32_t)m.browser.layerState[0].layer_stereo_immersive.textureHandle;
mozilla::gfx::VRLayerEyeRect& left = m.browser.layerState[0].layer_stereo_immersive.leftEyeRect;
mozilla::gfx::VRLayerEyeRect& right = m.browser.layerState[0].layer_stereo_immersive.rightEyeRect;
aLeftEye = device::EyeRect(left.x, left.y, left.width, left.height);
aRightEye = device::EyeRect(right.x, right.y, right.width, right.height);
aTextureWidth = (int32_t)m.browser.layerState[0].layer_stereo_immersive.textureSize.width;
aTextureHeight = (int32_t)m.browser.layerState[0].layer_stereo_immersive.textureSize.height;
}
void
ExternalVR::SetHapticState(ControllerContainerPtr aControllerContainer) const {
const uint32_t count = aControllerContainer->GetControllerCount();
uint32_t i = 0, j = 0;
for (i = 0; i < count; ++i) {
for (j = 0; j < mozilla::gfx::kVRHapticsMaxCount; ++j) {
if (m.browser.hapticState[j].controllerIndex == i && m.browser.hapticState[j].inputFrameID) {
aControllerContainer->SetHapticFeedback(i, m.browser.hapticState[j].inputFrameID,
m.browser.hapticState[j].pulseDuration + m.browser.hapticState[j].pulseStart,
m.browser.hapticState[j].pulseIntensity);
break;
}
}
// All hapticState has already been reset to zero, so it can't be match.
if (j == mozilla::gfx::kVRHapticsMaxCount) {
aControllerContainer->SetHapticFeedback(i, 0, 0.0f, 0.0f);
}
}
}
void
ExternalVR::StopPresenting() {
m.system.displayState.presentingGeneration++;
PushSystemState();
m.waitingForExit = true;
}
ExternalVR::ExternalVR(): m(State::Instance()) {
m.Reset();
PushSystemState();
}
}