/work/obj-fuzz/dist/include/gfxVR.h
Line | Count | Source (jump to first uncovered line) |
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_H |
8 | | #define GFX_VR_H |
9 | | |
10 | | #include "moz_external_vr.h" |
11 | | #include "nsTArray.h" |
12 | | #include "nsString.h" |
13 | | #include "nsCOMPtr.h" |
14 | | #include "mozilla/RefPtr.h" |
15 | | #include "mozilla/gfx/2D.h" |
16 | | #include "mozilla/Atomics.h" |
17 | | #include "mozilla/EnumeratedArray.h" |
18 | | #include "mozilla/TimeStamp.h" |
19 | | #include "mozilla/TypedEnumBits.h" |
20 | | |
21 | | namespace mozilla { |
22 | | namespace layers { |
23 | | class PTextureParent; |
24 | | } |
25 | | namespace dom { |
26 | | enum class GamepadMappingType : uint8_t; |
27 | | enum class GamepadHand : uint8_t; |
28 | | struct GamepadPoseState; |
29 | | } |
30 | | namespace gfx { |
31 | | class VRLayerParent; |
32 | | class VRDisplayHost; |
33 | | class VRControllerHost; |
34 | | class VRManagerPromise; |
35 | | |
36 | | // The maximum number of frames of latency that we would expect before we |
37 | | // should give up applying pose prediction. |
38 | | // If latency is greater than one second, then the experience is not likely |
39 | | // to be corrected by pose prediction. Setting this value too |
40 | | // high may result in unnecessary memory allocation. |
41 | | // As the current fastest refresh rate is 90hz, 100 is selected as a |
42 | | // conservative value. |
43 | | static const int kVRMaxLatencyFrames = 100; |
44 | | |
45 | | enum class VRDeviceType : uint16_t { |
46 | | Oculus, |
47 | | OpenVR, |
48 | | OSVR, |
49 | | GVR, |
50 | | Puppet, |
51 | | External, |
52 | | NumVRDeviceTypes |
53 | | }; |
54 | | |
55 | | struct VRDisplayInfo |
56 | | { |
57 | | uint32_t mDisplayID; |
58 | | VRDeviceType mType; |
59 | | uint32_t mPresentingGroups; |
60 | | uint32_t mGroupMask; |
61 | | uint64_t mFrameId; |
62 | | VRDisplayState mDisplayState; |
63 | | VRControllerState mControllerState[kVRControllerMaxCount]; |
64 | | |
65 | | VRHMDSensorState mLastSensorState[kVRMaxLatencyFrames]; |
66 | | const VRHMDSensorState& GetSensorState() const |
67 | 0 | { |
68 | 0 | return mLastSensorState[mFrameId % kVRMaxLatencyFrames]; |
69 | 0 | } |
70 | | |
71 | 0 | VRDeviceType GetType() const { return mType; } |
72 | 0 | uint32_t GetDisplayID() const { return mDisplayID; } |
73 | 0 | const char* GetDisplayName() const { return mDisplayState.mDisplayName; } |
74 | 0 | VRDisplayCapabilityFlags GetCapabilities() const { return mDisplayState.mCapabilityFlags; } |
75 | | |
76 | | const IntSize SuggestedEyeResolution() const; |
77 | | const Point3D GetEyeTranslation(uint32_t whichEye) const; |
78 | 0 | const VRFieldOfView& GetEyeFOV(uint32_t whichEye) const { return mDisplayState.mEyeFOV[whichEye]; } |
79 | 0 | bool GetIsConnected() const { return mDisplayState.mIsConnected; } |
80 | 0 | bool GetIsMounted() const { return mDisplayState.mIsMounted; } |
81 | 0 | uint32_t GetPresentingGroups() const { return mPresentingGroups; } |
82 | 0 | uint32_t GetGroupMask() const { return mGroupMask; } |
83 | | const Size GetStageSize() const; |
84 | | const Matrix4x4 GetSittingToStandingTransform() const; |
85 | 0 | uint64_t GetFrameId() const { return mFrameId; } |
86 | | |
87 | | bool operator==(const VRDisplayInfo& other) const { |
88 | | for (size_t i = 0; i < kVRMaxLatencyFrames; i++) { |
89 | | if (mLastSensorState[i] != other.mLastSensorState[i]) { |
90 | | return false; |
91 | | } |
92 | | } |
93 | | // Note that mDisplayState and mControllerState are asserted to be POD types, so memcmp is safe |
94 | | return mType == other.mType && |
95 | | mDisplayID == other.mDisplayID && |
96 | | memcmp(&mDisplayState, &other.mDisplayState, sizeof(VRDisplayState)) == 0 && |
97 | | memcmp(mControllerState, other.mControllerState, sizeof(VRControllerState) * kVRControllerMaxCount) == 0 && |
98 | | mPresentingGroups == other.mPresentingGroups && |
99 | | mGroupMask == other.mGroupMask && |
100 | | mFrameId == other.mFrameId; |
101 | | } |
102 | | |
103 | 0 | bool operator!=(const VRDisplayInfo& other) const { |
104 | 0 | return !(*this == other); |
105 | 0 | } |
106 | | }; |
107 | | |
108 | | struct VRSubmitFrameResultInfo |
109 | | { |
110 | | VRSubmitFrameResultInfo() |
111 | | : mFormat(SurfaceFormat::UNKNOWN), |
112 | | mFrameNum(0), |
113 | | mWidth(0), |
114 | | mHeight(0) |
115 | 0 | {} |
116 | | |
117 | | nsCString mBase64Image; |
118 | | SurfaceFormat mFormat; |
119 | | uint64_t mFrameNum; |
120 | | uint32_t mWidth; |
121 | | uint32_t mHeight; |
122 | | }; |
123 | | |
124 | | struct VRControllerInfo |
125 | | { |
126 | 0 | VRDeviceType GetType() const { return mType; } |
127 | 0 | uint32_t GetControllerID() const { return mControllerID; } |
128 | 0 | const char* GetControllerName() const { return mControllerState.controllerName; } |
129 | 0 | dom::GamepadMappingType GetMappingType() const { return mMappingType; } |
130 | 0 | uint32_t GetDisplayID() const { return mDisplayID; } |
131 | 0 | dom::GamepadHand GetHand() const { return mControllerState.hand; } |
132 | 0 | uint32_t GetNumButtons() const { return mControllerState.numButtons; } |
133 | 0 | uint32_t GetNumAxes() const { return mControllerState.numAxes; } |
134 | 0 | uint32_t GetNumHaptics() const { return mControllerState.numHaptics; } |
135 | | |
136 | | uint32_t mControllerID; |
137 | | VRDeviceType mType; |
138 | | dom::GamepadMappingType mMappingType; |
139 | | uint32_t mDisplayID; |
140 | | VRControllerState mControllerState; |
141 | 0 | bool operator==(const VRControllerInfo& other) const { |
142 | 0 | // Note that mControllerState is asserted to be a POD type, so memcmp is safe |
143 | 0 | return mType == other.mType && |
144 | 0 | mControllerID == other.mControllerID && |
145 | 0 | memcmp(&mControllerState, &other.mControllerState, sizeof(VRControllerState)) == 0 && |
146 | 0 | mMappingType == other.mMappingType && |
147 | 0 | mDisplayID == other.mDisplayID; |
148 | 0 | } |
149 | | |
150 | 0 | bool operator!=(const VRControllerInfo& other) const { |
151 | 0 | return !(*this == other); |
152 | 0 | } |
153 | | }; |
154 | | |
155 | | struct VRTelemetry |
156 | | { |
157 | | VRTelemetry() |
158 | | : mLastDroppedFrameCount(-1) |
159 | 0 | {} |
160 | | |
161 | 0 | void Clear() { |
162 | 0 | mPresentationStart = TimeStamp(); |
163 | 0 | mLastDroppedFrameCount = -1; |
164 | 0 | } |
165 | | |
166 | 0 | bool IsLastDroppedFrameValid() { |
167 | 0 | return (mLastDroppedFrameCount != -1); |
168 | 0 | } |
169 | | |
170 | | TimeStamp mPresentationStart; |
171 | | int32_t mLastDroppedFrameCount; |
172 | | }; |
173 | | |
174 | | class VRSystemManager { |
175 | | public: |
176 | | static uint32_t AllocateDisplayID(); |
177 | | static uint32_t AllocateControllerID(); |
178 | | |
179 | | protected: |
180 | | static Atomic<uint32_t> sDisplayBase; |
181 | | static Atomic<uint32_t> sControllerBase; |
182 | | |
183 | | public: |
184 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRSystemManager) |
185 | | |
186 | | virtual void Destroy() = 0; |
187 | | virtual void Shutdown() = 0; |
188 | | virtual void Enumerate() = 0; |
189 | | virtual void NotifyVSync(); |
190 | | virtual bool ShouldInhibitEnumeration(); |
191 | | virtual void GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult) = 0; |
192 | | virtual bool GetIsPresenting() = 0; |
193 | | virtual void HandleInput() = 0; |
194 | | virtual void GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult) = 0; |
195 | | virtual void ScanForControllers() = 0; |
196 | | virtual void RemoveControllers() = 0; |
197 | | virtual void VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex, |
198 | | double aIntensity, double aDuration, const VRManagerPromise& aPromise) = 0; |
199 | | virtual void StopVibrateHaptic(uint32_t aControllerIdx) = 0; |
200 | | void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed, bool aTouched, |
201 | | double aValue); |
202 | | void NewAxisMove(uint32_t aIndex, uint32_t aAxis, double aValue); |
203 | | void NewPoseState(uint32_t aIndex, const dom::GamepadPoseState& aPose); |
204 | | void NewHandChangeEvent(uint32_t aIndex, const dom::GamepadHand aHand); |
205 | | void AddGamepad(const VRControllerInfo& controllerInfo); |
206 | | void RemoveGamepad(uint32_t aIndex); |
207 | | |
208 | | protected: |
209 | 0 | VRSystemManager() : mControllerCount(0) { } |
210 | 0 | virtual ~VRSystemManager() = default; |
211 | | |
212 | | uint32_t mControllerCount; |
213 | | }; |
214 | | |
215 | | } // namespace gfx |
216 | | } // namespace mozilla |
217 | | |
218 | | #endif /* GFX_VR_H */ |