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 "mozilla/Atomics.h"
11 #include "moz_external_vr.h"
12 #include "base/process.h"  // for base::ProcessHandle
13 
14 namespace base {
15 class Thread;
16 }  // namespace base
17 namespace mozilla {
18 namespace gfx {
19 
20 class VRSession;
21 class VRShMem;
22 
23 static const int kVRFrameTimingHistoryDepth = 100;
24 
25 class VRService {
26  public:
27   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRService)
28   static already_AddRefed<VRService> Create(
29       volatile VRExternalShmem* aShmem = nullptr);
30 
31   void Refresh();
32   void Start();
33   void Stop();
34 
35  private:
36   explicit VRService(volatile VRExternalShmem* aShmem);
37   ~VRService();
38 
39   void StopInternal(bool aFromDtor);
40 
41   bool InitShmem();
42   void PushState(const mozilla::gfx::VRSystemState& aState);
43   void PullState(mozilla::gfx::VRBrowserState& aState);
44 
45   /**
46    * VRSystemState contains the most recent state of the VR
47    * system, to be shared with the browser by Shmem.
48    * mSystemState is the VR Service copy of this data, which
49    * is memcpy'ed atomically to the Shmem.
50    * VRSystemState is written by the VR Service, but read-only
51    * by the browser.
52    */
53   VRSystemState mSystemState;
54   /**
55    * VRBrowserState contains the most recent state of the browser.
56    * mBrowserState is memcpy'ed from the Shmem atomically
57    */
58   VRBrowserState mBrowserState;
59 
60   UniquePtr<VRSession> mSession;
61   base::Thread* mServiceThread;
62   bool mShutdownRequested;
63 
64   // Note: mShmem doesn't support RefPtr; thus, do not share this private
65   // pointer so that its lifetime can still be controlled by VRService
66   VRShMem* mShmem;
67   VRHapticState mLastHapticState[kVRHapticsMaxCount];
68   TimeStamp mFrameStartTime[kVRFrameTimingHistoryDepth];
69 
70   bool IsInServiceThread();
71   void UpdateHaptics();
72 
73   /**
74    * The VR Service thread is a state machine that always has one
75    * task queued depending on the state.
76    *
77    * VR Service thread state task functions:
78    */
79   void ServiceInitialize();
80   void ServiceShutdown();
81   void ServiceWaitForImmersive();
82   void ServiceImmersiveMode();
83 };
84 
85 }  // namespace gfx
86 }  // namespace mozilla
87 
88 #endif  // GFX_VR_SERVICE_VRSERVICE_H
89