1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef GFX_VR_SERVICE_VRSERVICE_H
8 #define GFX_VR_SERVICE_VRSERVICE_H
9 
10 #include "moz_external_vr.h"
11 #include "base/process.h"  // for base::ProcessHandle
12 #include "mozilla/Atomics.h"
13 #include "mozilla/TimeStamp.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsCOMPtr.h"
16 
17 class nsIThread;
18 namespace mozilla {
19 class BackgroundHangMonitor;
20 namespace gfx {
21 
22 class VRSession;
23 class VRShMem;
24 
25 static const int kVRFrameTimingHistoryDepth = 100;
26 
27 class VRService {
28  public:
29   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRService)
30   static already_AddRefed<VRService> Create(
31       volatile VRExternalShmem* aShmem = nullptr);
32 
33   void Refresh();
34   void Start();
35   void Stop();
36 
37  private:
38   explicit VRService(volatile VRExternalShmem* aShmem);
39   ~VRService();
40 
41   void StopInternal(bool aFromDtor);
42 
43   bool InitShmem();
44   void PushState(const mozilla::gfx::VRSystemState& aState);
45   void PullState(mozilla::gfx::VRBrowserState& aState);
46 
47   /**
48    * VRSystemState contains the most recent state of the VR
49    * system, to be shared with the browser by Shmem.
50    * mSystemState is the VR Service copy of this data, which
51    * is memcpy'ed atomically to the Shmem.
52    * VRSystemState is written by the VR Service, but read-only
53    * by the browser.
54    */
55   VRSystemState mSystemState;
56   /**
57    * VRBrowserState contains the most recent state of the browser.
58    * mBrowserState is memcpy'ed from the Shmem atomically
59    */
60   VRBrowserState mBrowserState;
61 
62   UniquePtr<VRSession> mSession;
63   nsCOMPtr<nsIThread> mServiceThread;
64   // Only ever accessed on the service thread.
65   UniquePtr<mozilla::BackgroundHangMonitor> mBackgroundHangMonitor;
66 
67   Atomic<bool> mShutdownRequested;
68 
69   // Note: mShmem doesn't support RefPtr; thus, do not share this private
70   // pointer so that its lifetime can still be controlled by VRService
71   VRShMem* mShmem;
72   VRHapticState mLastHapticState[kVRHapticsMaxCount];
73   TimeStamp mFrameStartTime[kVRFrameTimingHistoryDepth];
74 
75   bool IsInServiceThread();
76   void UpdateHaptics();
77 
78   /**
79    * The VR Service thread is a state machine that always has one
80    * task queued depending on the state.
81    *
82    * VR Service thread state task functions:
83    */
84   void ServiceInitialize();
85   void ServiceShutdown();
86   void ServiceWaitForImmersive();
87   void ServiceImmersiveMode();
88 };
89 
90 }  // namespace gfx
91 }  // namespace mozilla
92 
93 #endif  // GFX_VR_SERVICE_VRSERVICE_H
94