summaryrefslogtreecommitdiffstats
path: root/gfx/vr/service/VRService.cpp
blob: 2b774c25319a9646bb71a0c937041df63bed7556 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "VRService.h"

#include <cstring>  // for memcmp

#include "../VRShMem.h"
#include "../gfxVRMutex.h"
#include "PuppetSession.h"
#include "mozilla/BackgroundHangMonitor.h"
#include "mozilla/StaticPrefs_dom.h"
#include "nsThread.h"
#include "nsXULAppAPI.h"

#if defined(XP_WIN)
#  include "OculusSession.h"
#endif

#if defined(XP_WIN) || defined(XP_MACOSX) || \
    (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID))
#  include "OpenVRSession.h"
#endif
#if !defined(MOZ_WIDGET_ANDROID)
#  include "OSVRSession.h"
#endif

using namespace mozilla;
using namespace mozilla::gfx;

namespace {

int64_t FrameIDFromBrowserState(const mozilla::gfx::VRBrowserState& aState) {
  for (const auto& layer : aState.layerState) {
    if (layer.type == VRLayerType::LayerType_Stereo_Immersive) {
      return layer.layer_stereo_immersive.frameId;
    }
  }
  return 0;
}

bool IsImmersiveContentActive(const mozilla::gfx::VRBrowserState& aState) {
  for (const auto& layer : aState.layerState) {
    if (layer.type == VRLayerType::LayerType_Stereo_Immersive) {
      return true;
    }
  }
  return false;
}

}  // anonymous namespace

/*static*/
already_AddRefed<VRService> VRService::Create(
    volatile VRExternalShmem* aShmem) {
  RefPtr<VRService> service = new VRService(aShmem);
  return service.forget();
}

VRService::VRService(volatile VRExternalShmem* aShmem)
    : mSystemState{},
      mBrowserState{},
      mShutdownRequested(false),
      mLastHapticState{},
      mFrameStartTime{} {
  // When we have the VR process, we map the memory
  // of mAPIShmem from GPU process and pass it to the CTOR.
  // If we don't have the VR process, we will instantiate
  // mAPIShmem in VRService.
  mShmem = new VRShMem(aShmem, aShmem == nullptr /*aRequiresMutex*/);
}

VRService::~VRService() {
  // PSA: We must store the value of any staticPrefs preferences as this
  // destructor will be called after staticPrefs has been shut down.
  StopInternal(true /*aFromDtor*/);
}

void VRService::Refresh() {
  if (mShmem != nullptr && mShmem->IsDisplayStateShutdown()) {
    Stop();
  }
}

void VRService::Start() {
  if (!mServiceThread) {
    /**
     * We must ensure that any time the service is re-started, that
     * the VRSystemState is reset, including mSystemState.enumerationCompleted
     * This must happen before VRService::Start returns to the caller, in order
     * to prevent the WebVR/WebXR promises from being resolved before the
     * enumeration has been completed.
     */
    memset(&mSystemState, 0, sizeof(mSystemState));
    PushState(mSystemState);
    RefPtr<VRService> self = this;
    nsCOMPtr<nsIThread> thread;
    nsresult rv = NS_NewNamedThread(
        "VRService", getter_AddRefs(thread),
        NS_NewRunnableFunction("VRService::ServiceThreadStartup", [self]() {
          self->mBackgroundHangMonitor =
              MakeUnique<mozilla::BackgroundHangMonitor>(
                  "VRService",
                  /* Timeout values are powers-of-two to enable us get better
                     data. 128ms is chosen for transient hangs because 8Hz
                     should be the minimally acceptable goal for Compositor
                     responsiveness (normal goal is 60Hz). */
                  128,
                  /* 2048ms is chosen for permanent hangs because it's longer
                   * than most Compositor hangs seen in the wild, but is short
                   * enough to not miss getting native hang stacks. */
                  2048);
          static_cast<nsThread*>(NS_GetCurrentThread())
              ->SetUseHangMonitor(true);
        }));

    if (NS_FAILED(rv)) {
      return;
    }
    thread.swap(mServiceThread);
    // ServiceInitialize needs mServiceThread to be set in order to be able to
    // assert that it's running on the right thread as well as dispatching new
    // tasks. It can't be run within the NS_NewRunnableFunction initial event.
    MOZ_ALWAYS_SUCCEEDS(mServiceThread->Dispatch(
        NewRunnableMethod("gfx::VRService::ServiceInitialize", this,
                          &VRService::ServiceInitialize)));
  }
}

void VRService::Stop() { StopInternal(false /*aFromDtor*/); }

void VRService::StopInternal(bool aFromDtor) {
  if (mServiceThread) {
    // We must disable the background hang monitor before we can shutdown this
    // thread. Dispatched a last task to do so. No task will be allowed to run
    // on the service thread after this one.
    mServiceThread->Dispatch(NS_NewRunnableFunction(
        "VRService::StopInternal", [self = RefPtr<VRService>(this), this] {
          static_cast<nsThread*>(NS_GetCurrentThread())
              ->SetUseHangMonitor(false);
          mBackgroundHangMonitor = nullptr;
        }));
    mShutdownRequested = true;
    mServiceThread->Shutdown();
    mServiceThread = nullptr;
  }

  if (mShmem != nullptr && (aFromDtor || !mShmem->IsSharedExternalShmem())) {
    // Only leave the VRShMem and clean up the pointer when the struct
    // was not passed in. Otherwise, VRService will no longer have a
    // way to access that struct if VRService starts again.
    mShmem->LeaveShMem();
    delete mShmem;
    mShmem = nullptr;
  }

  mSession = nullptr;
}

bool VRService::InitShmem() { return mShmem->JoinShMem(); }

bool VRService::IsInServiceThread() {
  return mServiceThread && mServiceThread->IsOnCurrentThread();
}

void VRService::ServiceInitialize() {
  MOZ_ASSERT(IsInServiceThread());

  if (!InitShmem()) {
    return;
  }

  mShutdownRequested = false;
  // Get initial state from the browser
  PullState(mBrowserState);

  // Try to start a VRSession
  UniquePtr<VRSession> session;

  if (StaticPrefs::dom_vr_puppet_enabled()) {
    // When the VR Puppet is enabled, we don't want
    // to enumerate any real devices
    session = MakeUnique<PuppetSession>();
    if (!session->Initialize(mSystemState, mBrowserState.detectRuntimesOnly)) {
      session = nullptr;
    }
  } else {
    // We try Oculus first to ensure we use Oculus
    // devices trough the most native interface
    // when possible.
#if defined(XP_WIN)
    // Try Oculus
    if (!session) {
      session = MakeUnique<OculusSession>();
      if (!session->Initialize(mSystemState,
                               mBrowserState.detectRuntimesOnly)) {
        session = nullptr;
      }
    }
#endif

#if defined(XP_WIN) || defined(XP_MACOSX) || \
    (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID))
    // Try OpenVR
    if (!session) {
      session = MakeUnique<OpenVRSession>();
      if (!session->Initialize(mSystemState,
                               mBrowserState.detectRuntimesOnly)) {
        session = nullptr;
      }
    }
#endif
#if !defined(MOZ_WIDGET_ANDROID)
    // Try OSVR
    if (!session) {
      session = MakeUnique<OSVRSession>();
      if (!session->Initialize(mSystemState,
                               mBrowserState.detectRuntimesOnly)) {
        session = nullptr;
      }
    }
#endif

  }  // if (staticPrefs:VRPuppetEnabled())

  if (session) {
    mSession = std::move(session);
    // Setting enumerationCompleted to true indicates to the browser
    // that it should resolve any promises in the WebVR/WebXR API
    // waiting for hardware detection.
    mSystemState.enumerationCompleted = true;
    PushState(mSystemState);

    mServiceThread->Dispatch(
        NewRunnableMethod("gfx::VRService::ServiceWaitForImmersive", this,
                          &VRService::ServiceWaitForImmersive));
  } else {
    // VR hardware was not detected.
    // We must inform the browser of the failure so it may try again
    // later and resolve WebVR promises.  A failure or shutdown is
    // indicated by enumerationCompleted being set to true, with all
    // other fields remaining zeroed out.
    VRDisplayCapabilityFlags capFlags =
        mSystemState.displayState.capabilityFlags;
    memset(&mSystemState, 0, sizeof(mSystemState));
    mSystemState.enumerationCompleted = true;

    if (mBrowserState.detectRuntimesOnly) {
      mSystemState.displayState.capabilityFlags = capFlags;
    } else {
      mSystemState.displayState.minRestartInterval =
          StaticPrefs::dom_vr_external_notdetected_timeout();
    }
    mSystemState.displayState.shutdown = true;
    PushState(mSystemState);
  }
}

void VRService::ServiceShutdown() {
  MOZ_ASSERT(IsInServiceThread());

  // Notify the browser that we have shut down.
  // This is indicated by enumerationCompleted being set
  // to true, with all other fields remaining zeroed out.
  memset(&mSystemState, 0, sizeof(mSystemState));
  mSystemState.enumerationCompleted = true;
  mSystemState.displayState.shutdown = true;
  if (mSession && mSession->ShouldQuit()) {
    mSystemState.displayState.minRestartInterval =
        StaticPrefs::dom_vr_external_quit_timeout();
  }
  PushState(mSystemState);
  mSession = nullptr;
}

void VRService::ServiceWaitForImmersive() {
  MOZ_ASSERT(IsInServiceThread());
  MOZ_ASSERT(mSession);

  mSession->ProcessEvents(mSystemState);
  PushState(mSystemState);
  PullState(mBrowserState);

  if (mSession->ShouldQuit() || mShutdownRequested) {
    // Shut down
    mServiceThread->Dispatch(NewRunnableMethod(
        "gfx::VRService::ServiceShutdown", this, &VRService::ServiceShutdown));
  } else if (IsImmersiveContentActive(mBrowserState)) {
    // Enter Immersive Mode
    mSession->StartPresentation();
    mSession->StartFrame(mSystemState);
    PushState(mSystemState);

    mServiceThread->Dispatch(
        NewRunnableMethod("gfx::VRService::ServiceImmersiveMode", this,
                          &VRService::ServiceImmersiveMode));
  } else {
    // Continue waiting for immersive mode
    mServiceThread->Dispatch(
        NewRunnableMethod("gfx::VRService::ServiceWaitForImmersive", this,
                          &VRService::ServiceWaitForImmersive));
  }
}

void VRService::ServiceImmersiveMode() {
  MOZ_ASSERT(IsInServiceThread());
  MOZ_ASSERT(mSession);

  mSession->ProcessEvents(mSystemState);
  UpdateHaptics();
  PushState(mSystemState);
  PullState(mBrowserState);

  if (mSession->ShouldQuit() || mShutdownRequested) {
    // Shut down
    mServiceThread->Dispatch(NewRunnableMethod(
        "gfx::VRService::ServiceShutdown", this, &VRService::ServiceShutdown));
    return;
  }

  if (!IsImmersiveContentActive(mBrowserState)) {
    // Exit immersive mode
    mSession->StopAllHaptics();
    mSession->StopPresentation();
    mServiceThread->Dispatch(
        NewRunnableMethod("gfx::VRService::ServiceWaitForImmersive", this,
                          &VRService::ServiceWaitForImmersive));
    return;
  }

  uint64_t newFrameId = FrameIDFromBrowserState(mBrowserState);
  if (newFrameId != mSystemState.displayState.lastSubmittedFrameId) {
    // A new immersive frame has been received.
    // Submit the textures to the VR system compositor.
    bool success = false;
    for (const auto& layer : mBrowserState.layerState) {
      if (layer.type == VRLayerType::LayerType_Stereo_Immersive) {
        // SubmitFrame may block in order to control the timing for
        // the next frame start
        success = mSession->SubmitFrame(layer.layer_stereo_immersive);
        break;
      }
    }

    // Changing mLastSubmittedFrameId triggers a new frame to start
    // rendering.  Changes to mLastSubmittedFrameId and the values
    // used for rendering, such as headset pose, must be pushed
    // atomically to the browser.
    mSystemState.displayState.lastSubmittedFrameId = newFrameId;
    mSystemState.displayState.lastSubmittedFrameSuccessful = success;

    // StartFrame may block to control the timing for the next frame start
    mSession->StartFrame(mSystemState);
    mSystemState.sensorState.inputFrameID++;
    size_t historyIndex =
        mSystemState.sensorState.inputFrameID % ArrayLength(mFrameStartTime);
    mFrameStartTime[historyIndex] = TimeStamp::Now();
    PushState(mSystemState);
  }

  // Continue immersive mode
  mServiceThread->Dispatch(
      NewRunnableMethod("gfx::VRService::ServiceImmersiveMode", this,
                        &VRService::ServiceImmersiveMode));
}

void VRService::UpdateHaptics() {
  MOZ_ASSERT(IsInServiceThread());
  MOZ_ASSERT(mSession);

  for (size_t i = 0; i < ArrayLength(mBrowserState.hapticState); i++) {
    VRHapticState& state = mBrowserState.hapticState[i];
    VRHapticState& lastState = mLastHapticState[i];
    // Note that VRHapticState is asserted to be a POD type, thus memcmp is safe
    if (memcmp(&state, &lastState, sizeof(VRHapticState)) == 0) {
      // No change since the last update
      continue;
    }
    if (state.inputFrameID == 0) {
      // The haptic feedback was stopped
      mSession->StopVibrateHaptic(state.controllerIndex);
    } else {
      TimeStamp now;
      if (now.IsNull()) {
        // TimeStamp::Now() is expensive, so we
        // must call it only when needed and save the
        // output for further loop iterations.
        now = TimeStamp::Now();
      }
      // This is a new haptic pulse, or we are overriding a prior one
      size_t historyIndex = state.inputFrameID % ArrayLength(mFrameStartTime);
      float startOffset =
          (float)(now - mFrameStartTime[historyIndex]).ToSeconds();

      // state.pulseStart is guaranteed never to be in the future
      mSession->VibrateHaptic(
          state.controllerIndex, state.hapticIndex, state.pulseIntensity,
          state.pulseDuration + state.pulseStart - startOffset);
    }
    // Record the state for comparison in the next run
    memcpy(&lastState, &state, sizeof(VRHapticState));
  }
}

void VRService::PushState(const mozilla::gfx::VRSystemState& aState) {
  if (mShmem != nullptr) {
    mShmem->PushSystemState(aState);
  }
}

void VRService::PullState(mozilla::gfx::VRBrowserState& aState) {
  if (mShmem != nullptr) {
    mShmem->PullBrowserState(aState);
  }
}