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_MANAGER_H
8 #define GFX_VR_MANAGER_H
10 #include "nsIObserver.h"
12 #include "nsTHashSet.h"
13 #include "nsThreadUtils.h"
14 #include "mozilla/Atomics.h"
15 #include "mozilla/dom/GamepadHandle.h"
16 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor
17 #include "mozilla/Monitor.h"
18 #include "mozilla/TimeStamp.h"
25 class VRManagerParent
;
30 enum class VRManagerState
: uint32_t {
31 Disabled
, // All VRManager activity is stopped
32 Idle
, // No VR hardware has been activated, but background tasks are running
33 RuntimeDetection
, // Waiting for detection of runtimes without starting up VR
35 Enumeration
, // Waiting for enumeration and startup of VR hardware
36 Active
, // VR hardware is active
37 Stopping
, // Waiting for the VRService to stop
40 class VRManager
: nsIObserver
{
42 NS_DECL_THREADSAFE_ISUPPORTS
45 static void ManagerInit();
46 static VRManager
* Get();
47 static VRManager
* MaybeGet();
49 void AddVRManagerParent(VRManagerParent
* aVRManagerParent
);
50 void RemoveVRManagerParent(VRManagerParent
* aVRManagerParent
);
52 void NotifyVsync(const TimeStamp
& aVsyncTimestamp
);
54 void DetectRuntimes();
55 void EnumerateDevices();
56 void StopAllHaptics();
58 void VibrateHaptic(mozilla::dom::GamepadHandle aGamepadHandle
,
59 uint32_t aHapticIndex
, double aIntensity
, double aDuration
,
60 const VRManagerPromise
& aPromise
);
61 void StopVibrateHaptic(mozilla::dom::GamepadHandle aGamepadHandle
);
62 void NotifyVibrateHapticCompleted(const VRManagerPromise
& aPromise
);
63 void StartVRNavigation(const uint32_t& aDisplayID
);
64 void StopVRNavigation(const uint32_t& aDisplayID
,
65 const TimeDuration
& aTimeout
);
67 void ShutdownVRManagerParents();
68 #if !defined(MOZ_WIDGET_ANDROID)
69 bool RunPuppet(const nsTArray
<uint64_t>& aBuffer
,
70 VRManagerParent
* aManagerParent
);
71 void ResetPuppet(VRManagerParent
* aManagerParent
);
72 void NotifyPuppetComplete();
74 void AddLayer(VRLayerParent
* aLayer
);
75 void RemoveLayer(VRLayerParent
* aLayer
);
76 void SetGroupMask(uint32_t aGroupMask
);
77 void SubmitFrame(VRLayerParent
* aLayer
,
78 const layers::SurfaceDescriptor
& aTexture
, uint64_t aFrameId
,
79 const gfx::Rect
& aLeftEyeRect
,
80 const gfx::Rect
& aRightEyeRect
);
89 static void TaskTimerCallback(nsITimer
* aTimer
, void* aClosure
);
91 void Run1msTasks(double aDeltaTime
);
94 uint32_t GetOptimalTaskInterval();
95 void ProcessManagerState();
96 void ProcessManagerState_Disabled();
97 void ProcessManagerState_Idle();
98 void ProcessManagerState_Idle_StartRuntimeDetection();
99 void ProcessManagerState_Idle_StartEnumeration();
100 void ProcessManagerState_DetectRuntimes();
101 void ProcessManagerState_Enumeration();
102 void ProcessManagerState_Active();
103 void ProcessManagerState_Stopping();
104 void PullState(const std::function
<bool()>& aWaitCondition
= nullptr);
105 void PushState(const bool aNotifyCond
= false);
106 static uint32_t AllocateDisplayID();
107 void DispatchVRDisplayInfoUpdate();
108 void DispatchRuntimeCapabilitiesUpdate();
109 void UpdateRequestedDevices();
110 void CheckForInactiveTimeout();
111 #if !defined(MOZ_WIDGET_ANDROID)
112 void CheckForPuppetCompletion();
114 void CheckForShutdown();
115 void CheckWatchDog();
116 void ExpireNavigationTransition();
119 void UpdateHaptics(double aDeltaTime
);
120 void ClearHapticSlot(size_t aSlot
);
122 void ShutdownSubmitThread();
123 void StartPresentation();
124 void StopPresentation();
125 void CancelCurrentSubmitTask();
127 void SubmitFrameInternal(const layers::SurfaceDescriptor
& aTexture
,
128 uint64_t aFrameId
, const gfx::Rect
& aLeftEyeRect
,
129 const gfx::Rect
& aRightEyeRect
);
130 bool SubmitFrame(const layers::SurfaceDescriptor
& aTexture
, uint64_t aFrameId
,
131 const gfx::Rect
& aLeftEyeRect
,
132 const gfx::Rect
& aRightEyeRect
);
134 Atomic
<VRManagerState
> mState
;
135 typedef nsTHashSet
<RefPtr
<VRManagerParent
>> VRManagerParentSet
;
136 VRManagerParentSet mVRManagerParents
;
137 #if !defined(MOZ_WIDGET_ANDROID)
138 VRManagerParentSet mManagerParentsWaitingForPuppetReset
;
139 RefPtr
<VRManagerParent
> mManagerParentRunningPuppet
;
141 // Weak reference to mLayers entries are cleared in
142 // VRLayerParent destructor
143 nsTArray
<VRLayerParent
*> mLayers
;
145 TimeStamp mLastDisplayEnumerationTime
;
146 TimeStamp mLastActiveTime
;
147 TimeStamp mLastTickTime
;
148 TimeStamp mEarliestRestartTime
;
149 TimeStamp mVRNavigationTransitionEnd
;
150 TimeStamp mLastFrameStart
[kVRMaxLatencyFrames
];
151 double mAccumulator100ms
;
152 bool mRuntimeDetectionRequested
;
153 bool mRuntimeDetectionCompleted
;
154 bool mEnumerationRequested
;
155 bool mEnumerationCompleted
;
156 bool mVRDisplaysRequested
;
157 bool mVRDisplaysRequestedNonFocus
;
158 bool mVRControllersRequested
;
160 uint32_t mTaskInterval
;
161 RefPtr
<nsITimer
> mTaskTimer
;
162 mozilla::Monitor mCurrentSubmitTaskMonitor MOZ_UNANNOTATED
;
163 RefPtr
<CancelableRunnable
> mCurrentSubmitTask
;
164 uint64_t mLastSubmittedFrameId
;
165 uint64_t mLastStartedFrame
;
166 VRDisplayCapabilityFlags mRuntimeSupportFlags
;
169 // Note: mShmem doesn't support RefPtr; thus, do not share this private
170 // pointer so that its lifetime can still be controlled by VRManager
172 bool mVRProcessEnabled
;
174 #if !defined(MOZ_WIDGET_ANDROID)
175 RefPtr
<VRServiceHost
> mServiceHost
;
178 static Atomic
<uint32_t> sDisplayBase
;
179 RefPtr
<VRThread
> mSubmitThread
;
180 VRTelemetry mTelemetry
;
181 nsTArray
<UniquePtr
<VRManagerPromise
>> mHapticPromises
;
182 // Duration of haptic pulse time remaining (milliseconds)
183 double mHapticPulseRemaining
[kVRHapticsMaxCount
];
185 VRDisplayInfo mDisplayInfo
;
186 VRDisplayInfo mLastUpdateDisplayInfo
;
187 VRBrowserState mBrowserState
;
188 VRHMDSensorState mLastSensorState
;
192 } // namespace mozilla
194 #endif // GFX_VR_MANAGER_H