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/EnumeratedArray.h"
18 #include "mozilla/TimeStamp.h"
19 #include "mozilla/TypedEnumBits.h"
20 #include <type_traits>
27 enum class GamepadMappingType
: uint8_t;
28 enum class GamepadHand
: uint8_t;
31 enum class VRAPIMode
: uint8_t { WebXR
, WebVR
, NumVRAPIModes
};
35 class VRManagerPromise
;
37 // The maximum number of frames of latency that we would expect before we
38 // should give up applying pose prediction.
39 // If latency is greater than one second, then the experience is not likely
40 // to be corrected by pose prediction. Setting this value too
41 // high may result in unnecessary memory allocation.
42 // As the current fastest refresh rate is 90hz, 100 is selected as a
43 // conservative value.
44 static const int kVRMaxLatencyFrames
= 100;
46 struct VRDisplayInfo
{
48 uint32_t mPresentingGroups
;
51 VRDisplayState mDisplayState
;
52 VRControllerState mControllerState
[kVRControllerMaxCount
];
54 VRHMDSensorState mLastSensorState
[kVRMaxLatencyFrames
];
55 void Clear() { memset(this, 0, sizeof(VRDisplayInfo
)); }
56 const VRHMDSensorState
& GetSensorState() const {
57 return mLastSensorState
[mFrameId
% kVRMaxLatencyFrames
];
60 uint32_t GetDisplayID() const { return mDisplayID
; }
61 const char* GetDisplayName() const { return mDisplayState
.displayName
; }
62 VRDisplayCapabilityFlags
GetCapabilities() const {
63 return mDisplayState
.capabilityFlags
;
66 const IntSize
SuggestedEyeResolution() const;
67 const Point3D
GetEyeTranslation(uint32_t whichEye
) const;
68 const VRFieldOfView
& GetEyeFOV(uint32_t whichEye
) const {
69 return mDisplayState
.eyeFOV
[whichEye
];
71 bool GetIsConnected() const { return mDisplayState
.isConnected
; }
72 bool GetIsMounted() const { return mDisplayState
.isMounted
; }
73 uint32_t GetPresentingGroups() const { return mPresentingGroups
; }
74 uint32_t GetGroupMask() const { return mGroupMask
; }
75 const Size
GetStageSize() const;
76 const Matrix4x4
GetSittingToStandingTransform() const;
77 uint64_t GetFrameId() const { return mFrameId
; }
79 bool operator==(const VRDisplayInfo
& other
) const {
80 for (size_t i
= 0; i
< kVRMaxLatencyFrames
; i
++) {
81 if (mLastSensorState
[i
] != other
.mLastSensorState
[i
]) {
85 // Note that mDisplayState and mControllerState are asserted to be POD
86 // types, so memcmp is safe
87 return mDisplayID
== other
.mDisplayID
&&
88 memcmp(&mDisplayState
, &other
.mDisplayState
,
89 sizeof(VRDisplayState
)) == 0 &&
90 memcmp(mControllerState
, other
.mControllerState
,
91 sizeof(VRControllerState
) * kVRControllerMaxCount
) == 0 &&
92 mPresentingGroups
== other
.mPresentingGroups
&&
93 mGroupMask
== other
.mGroupMask
&& mFrameId
== other
.mFrameId
;
96 bool operator!=(const VRDisplayInfo
& other
) const {
97 return !(*this == other
);
101 static_assert(std::is_pod
<VRDisplayInfo
>::value
,
102 "VRDisplayInfo must be a POD type.");
104 struct VRSubmitFrameResultInfo
{
105 VRSubmitFrameResultInfo()
106 : mFormat(SurfaceFormat::UNKNOWN
), mFrameNum(0), mWidth(0), mHeight(0) {}
108 nsCString mBase64Image
;
109 SurfaceFormat mFormat
;
115 struct VRControllerInfo
{
116 uint32_t GetControllerID() const { return mControllerID
; }
117 const char* GetControllerName() const {
118 return mControllerState
.controllerName
;
120 dom::GamepadMappingType
GetMappingType() const { return mMappingType
; }
121 uint32_t GetDisplayID() const { return mDisplayID
; }
122 dom::GamepadHand
GetHand() const { return mControllerState
.hand
; }
123 uint32_t GetNumButtons() const { return mControllerState
.numButtons
; }
124 uint32_t GetNumAxes() const { return mControllerState
.numAxes
; }
125 uint32_t GetNumHaptics() const { return mControllerState
.numHaptics
; }
127 uint32_t mControllerID
;
128 dom::GamepadMappingType mMappingType
;
130 VRControllerState mControllerState
;
131 bool operator==(const VRControllerInfo
& other
) const {
132 // Note that mControllerState is asserted to be a POD type, so memcmp is
134 return mControllerID
== other
.mControllerID
&&
135 memcmp(&mControllerState
, &other
.mControllerState
,
136 sizeof(VRControllerState
)) == 0 &&
137 mMappingType
== other
.mMappingType
&& mDisplayID
== other
.mDisplayID
;
140 bool operator!=(const VRControllerInfo
& other
) const {
141 return !(*this == other
);
146 VRTelemetry() : mLastDroppedFrameCount(-1) {}
149 mPresentationStart
= TimeStamp();
150 mLastDroppedFrameCount
= -1;
153 bool IsLastDroppedFrameValid() { return (mLastDroppedFrameCount
!= -1); }
155 TimeStamp mPresentationStart
;
156 int32_t mLastDroppedFrameCount
;
160 } // namespace mozilla
162 #endif /* GFX_VR_H */