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/. */
10 #include "moz_external_vr.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/gfx/2D.h"
16 #include "mozilla/Atomics.h"
17 #include "mozilla/dom/TiedFields.h"
18 #include "mozilla/EnumeratedArray.h"
19 #include "mozilla/TimeStamp.h"
20 #include "mozilla/TypedEnumBits.h"
21 #include <type_traits>
28 enum class GamepadMappingType
: uint8_t;
29 enum class GamepadHand
: uint8_t;
32 enum class VRAPIMode
: uint8_t { WebXR
, WebVR
, NumVRAPIModes
};
36 class VRManagerPromise
;
38 // The maximum number of frames of latency that we would expect before we
39 // should give up applying pose prediction.
40 // If latency is greater than one second, then the experience is not likely
41 // to be corrected by pose prediction. Setting this value too
42 // high may result in unnecessary memory allocation.
43 // As the current fastest refresh rate is 90hz, 100 is selected as a
44 // conservative value.
45 static const int kVRMaxLatencyFrames
= 100;
47 struct VRDisplayInfo
{
49 uint32_t mPresentingGroups
;
53 VRDisplayState mDisplayState
;
54 std::array
<VRControllerState
, kVRControllerMaxCount
> mControllerState
;
55 std::array
<VRHMDSensorState
, kVRMaxLatencyFrames
> mLastSensorState
;
59 auto MutTiedFields() {
60 return std::tie(mDisplayID
, mPresentingGroups
, mGroupMask
, _padding
,
61 mFrameId
, mDisplayState
, mControllerState
,
67 void Clear() { memset(this, 0, sizeof(VRDisplayInfo
)); }
68 const VRHMDSensorState
& GetSensorState() const {
69 return mLastSensorState
[mFrameId
% kVRMaxLatencyFrames
];
72 uint32_t GetDisplayID() const { return mDisplayID
; }
73 const char* GetDisplayName() const {
74 return mDisplayState
.displayName
.data();
76 VRDisplayCapabilityFlags
GetCapabilities() const {
77 return mDisplayState
.capabilityFlags
;
80 const IntSize
SuggestedEyeResolution() const;
81 const Point3D
GetEyeTranslation(uint32_t whichEye
) const;
82 const VRFieldOfView
& GetEyeFOV(uint32_t whichEye
) const {
83 return mDisplayState
.eyeFOV
[whichEye
];
85 bool GetIsConnected() const { return mDisplayState
.isConnected
; }
86 bool GetIsMounted() const { return mDisplayState
.isMounted
; }
87 uint32_t GetPresentingGroups() const { return mPresentingGroups
; }
88 uint32_t GetGroupMask() const { return mGroupMask
; }
89 const Size
GetStageSize() const;
90 const Matrix4x4
GetSittingToStandingTransform() const;
91 uint64_t GetFrameId() const { return mFrameId
; }
93 bool operator==(const VRDisplayInfo
& other
) const {
94 return TiedFields(*this) == TiedFields(other
);
97 bool operator!=(const VRDisplayInfo
& other
) const {
98 return !(*this == other
);
102 static_assert(std::is_pod
<VRDisplayInfo
>::value
,
103 "VRDisplayInfo must be a POD type.");
105 struct VRSubmitFrameResultInfo
{
106 VRSubmitFrameResultInfo()
107 : mFormat(SurfaceFormat::UNKNOWN
), mFrameNum(0), mWidth(0), mHeight(0) {}
109 nsCString mBase64Image
;
110 SurfaceFormat mFormat
;
116 struct VRControllerInfo
{
117 uint32_t GetControllerID() const { return mControllerID
; }
118 const char* GetControllerName() const {
119 return mControllerState
.controllerName
.data();
121 dom::GamepadMappingType
GetMappingType() const { return mMappingType
; }
122 uint32_t GetDisplayID() const { return mDisplayID
; }
123 dom::GamepadHand
GetHand() const { return mControllerState
.hand
; }
124 uint32_t GetNumButtons() const { return mControllerState
.numButtons
; }
125 uint32_t GetNumAxes() const { return mControllerState
.numAxes
; }
126 uint32_t GetNumHaptics() const { return mControllerState
.numHaptics
; }
128 uint32_t mControllerID
;
129 dom::GamepadMappingType mMappingType
;
131 VRControllerState mControllerState
;
132 bool operator==(const VRControllerInfo
& other
) const {
133 // Note that mControllerState is asserted to be a POD type, so memcmp is
135 return mControllerID
== other
.mControllerID
&&
136 memcmp(&mControllerState
, &other
.mControllerState
,
137 sizeof(VRControllerState
)) == 0 &&
138 mMappingType
== other
.mMappingType
&& mDisplayID
== other
.mDisplayID
;
141 bool operator!=(const VRControllerInfo
& other
) const {
142 return !(*this == other
);
147 VRTelemetry() : mLastDroppedFrameCount(-1) {}
150 mPresentationStart
= TimeStamp();
151 mLastDroppedFrameCount
= -1;
154 bool IsLastDroppedFrameValid() { return (mLastDroppedFrameCount
!= -1); }
156 TimeStamp mPresentationStart
;
157 int32_t mLastDroppedFrameCount
;
161 } // namespace mozilla
163 #endif /* GFX_VR_H */