Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / gfx / vr / service / VRService.h
blob2e41cd2980f86c94871bc3453d4a05faa74603e3
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/. */
7 #ifndef GFX_VR_SERVICE_VRSERVICE_H
8 #define GFX_VR_SERVICE_VRSERVICE_H
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"
17 class nsIThread;
18 namespace mozilla {
19 class BackgroundHangMonitor;
20 namespace gfx {
22 class VRSession;
23 class VRShMem;
25 static const int kVRFrameTimingHistoryDepth = 100;
27 class VRService {
28 public:
29 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRService)
30 static already_AddRefed<VRService> Create(
31 volatile VRExternalShmem* aShmem = nullptr);
33 void Refresh();
34 void Start();
35 void Stop();
37 private:
38 explicit VRService(volatile VRExternalShmem* aShmem);
39 ~VRService();
41 void StopInternal(bool aFromDtor);
43 bool InitShmem();
44 void PushState(const mozilla::gfx::VRSystemState& aState);
45 void PullState(mozilla::gfx::VRBrowserState& aState);
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.
55 VRSystemState mSystemState;
56 /**
57 * VRBrowserState contains the most recent state of the browser.
58 * mBrowserState is memcpy'ed from the Shmem atomically
60 VRBrowserState mBrowserState;
62 UniquePtr<VRSession> mSession;
63 nsCOMPtr<nsIThread> mServiceThread;
64 // Only ever accessed on the service thread.
65 UniquePtr<mozilla::BackgroundHangMonitor> mBackgroundHangMonitor;
67 Atomic<bool> mShutdownRequested;
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];
75 bool IsInServiceThread();
76 void UpdateHaptics();
78 /**
79 * The VR Service thread is a state machine that always has one
80 * task queued depending on the state.
82 * VR Service thread state task functions:
84 void ServiceInitialize();
85 void ServiceShutdown();
86 void ServiceWaitForImmersive();
87 void ServiceImmersiveMode();
90 } // namespace gfx
91 } // namespace mozilla
93 #endif // GFX_VR_SERVICE_VRSERVICE_H