2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2014-07-09 23:24:49 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef GFX_VR_H
|
|
|
|
#define GFX_VR_H
|
|
|
|
|
2018-03-14 03:09:54 +03:00
|
|
|
#include "moz_external_vr.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
#include "nsTArray.h"
|
2015-04-07 20:26:44 +03:00
|
|
|
#include "nsString.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-04-07 20:26:44 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-09-18 00:23:13 +03:00
|
|
|
#include "mozilla/EnumeratedArray.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/TypedEnumBits.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-07-02 18:58:24 +03:00
|
|
|
namespace layers {
|
2016-02-25 02:54:50 +03:00
|
|
|
class PTextureParent;
|
2015-07-02 18:58:24 +03:00
|
|
|
}
|
2016-10-24 13:09:11 +03:00
|
|
|
namespace dom {
|
2017-01-12 04:23:37 +03:00
|
|
|
enum class GamepadMappingType : uint8_t;
|
|
|
|
enum class GamepadHand : uint8_t;
|
2016-10-24 13:09:11 +03:00
|
|
|
struct GamepadPoseState;
|
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
namespace gfx {
|
2016-02-25 02:54:50 +03:00
|
|
|
class VRLayerParent;
|
|
|
|
class VRDisplayHost;
|
2016-10-07 11:58:01 +03:00
|
|
|
class VRControllerHost;
|
2017-11-30 14:37:07 +03:00
|
|
|
class VRManagerPromise;
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
// The maximum number of frames of latency that we would expect before we
|
|
|
|
// should give up applying pose prediction.
|
|
|
|
// If latency is greater than one second, then the experience is not likely
|
|
|
|
// to be corrected by pose prediction. Setting this value too
|
|
|
|
// high may result in unnecessary memory allocation.
|
|
|
|
// As the current fastest refresh rate is 90hz, 100 is selected as a
|
|
|
|
// conservative value.
|
|
|
|
static const int kVRMaxLatencyFrames = 100;
|
|
|
|
|
2018-03-14 03:09:54 +03:00
|
|
|
enum class VRDeviceType : uint16_t {
|
|
|
|
Oculus,
|
|
|
|
OpenVR,
|
|
|
|
OSVR,
|
|
|
|
GVR,
|
|
|
|
Puppet,
|
|
|
|
External,
|
|
|
|
NumVRDeviceTypes
|
|
|
|
};
|
2017-05-09 02:01:36 +03:00
|
|
|
|
2016-04-13 03:39:28 +03:00
|
|
|
struct VRDisplayInfo
|
2015-09-18 00:23:13 +03:00
|
|
|
{
|
2016-02-25 02:54:50 +03:00
|
|
|
uint32_t mDisplayID;
|
2016-10-04 11:22:42 +03:00
|
|
|
VRDeviceType mType;
|
2017-05-09 02:01:36 +03:00
|
|
|
uint32_t mPresentingGroups;
|
|
|
|
uint32_t mGroupMask;
|
2017-09-15 23:38:47 +03:00
|
|
|
uint64_t mFrameId;
|
2018-03-14 03:09:54 +03:00
|
|
|
VRDisplayState mDisplayState;
|
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
VRHMDSensorState mLastSensorState[kVRMaxLatencyFrames];
|
2018-03-14 03:09:54 +03:00
|
|
|
const VRHMDSensorState& GetSensorState() const
|
|
|
|
{
|
|
|
|
return mLastSensorState[mFrameId % kVRMaxLatencyFrames];
|
|
|
|
}
|
|
|
|
|
|
|
|
VRDeviceType GetType() const { return mType; }
|
|
|
|
uint32_t GetDisplayID() const { return mDisplayID; }
|
|
|
|
const char* GetDisplayName() const { return mDisplayState.mDisplayName; }
|
|
|
|
VRDisplayCapabilityFlags GetCapabilities() const { return mDisplayState.mCapabilityFlags; }
|
|
|
|
|
|
|
|
const IntSize SuggestedEyeResolution() const;
|
|
|
|
const Point3D GetEyeTranslation(uint32_t whichEye) const;
|
|
|
|
const VRFieldOfView& GetEyeFOV(uint32_t whichEye) const { return mDisplayState.mEyeFOV[whichEye]; }
|
|
|
|
bool GetIsConnected() const { return mDisplayState.mIsConnected; }
|
|
|
|
bool GetIsMounted() const { return mDisplayState.mIsMounted; }
|
|
|
|
uint32_t GetPresentingGroups() const { return mPresentingGroups; }
|
|
|
|
uint32_t GetGroupMask() const { return mGroupMask; }
|
|
|
|
const Size GetStageSize() const;
|
|
|
|
const Matrix4x4 GetSittingToStandingTransform() const;
|
|
|
|
uint64_t GetFrameId() const { return mFrameId; }
|
2015-09-18 00:23:13 +03:00
|
|
|
|
2016-04-13 03:39:28 +03:00
|
|
|
bool operator==(const VRDisplayInfo& other) const {
|
2017-05-09 02:01:36 +03:00
|
|
|
for (size_t i = 0; i < kVRMaxLatencyFrames; i++) {
|
|
|
|
if (mLastSensorState[i] != other.mLastSensorState[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 03:09:54 +03:00
|
|
|
// Note that mDisplayState is asserted to be a POD type, so memcmp is safe
|
2015-09-18 00:23:13 +03:00
|
|
|
return mType == other.mType &&
|
2016-02-25 02:54:50 +03:00
|
|
|
mDisplayID == other.mDisplayID &&
|
2018-03-14 03:09:54 +03:00
|
|
|
memcmp(&mDisplayState, &other.mDisplayState, sizeof(VRDisplayState)) == 0 &&
|
2017-05-09 02:01:36 +03:00
|
|
|
mPresentingGroups == other.mPresentingGroups &&
|
|
|
|
mGroupMask == other.mGroupMask &&
|
2018-06-05 01:45:04 +03:00
|
|
|
mFrameId == other.mFrameId;
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 03:39:28 +03:00
|
|
|
bool operator!=(const VRDisplayInfo& other) const {
|
2015-09-18 00:23:13 +03:00
|
|
|
return !(*this == other);
|
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
};
|
|
|
|
|
2017-05-23 11:55:30 +03:00
|
|
|
struct VRSubmitFrameResultInfo
|
|
|
|
{
|
|
|
|
VRSubmitFrameResultInfo()
|
2018-04-13 16:01:28 +03:00
|
|
|
: mFrameNum(0),
|
|
|
|
mWidth(0),
|
|
|
|
mHeight(0)
|
2017-05-23 11:55:30 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
nsCString mBase64Image;
|
|
|
|
SurfaceFormat mFormat;
|
2017-09-15 23:38:47 +03:00
|
|
|
uint64_t mFrameNum;
|
2017-05-23 11:55:30 +03:00
|
|
|
uint32_t mWidth;
|
|
|
|
uint32_t mHeight;
|
|
|
|
};
|
|
|
|
|
2017-02-06 11:12:52 +03:00
|
|
|
struct VRControllerInfo
|
|
|
|
{
|
|
|
|
VRDeviceType GetType() const { return mType; }
|
|
|
|
uint32_t GetControllerID() const { return mControllerID; }
|
2018-03-14 03:09:54 +03:00
|
|
|
const char* GetControllerName() const { return mControllerState.mControllerName; }
|
2017-02-06 11:12:52 +03:00
|
|
|
dom::GamepadMappingType GetMappingType() const { return mMappingType; }
|
2017-07-14 12:01:09 +03:00
|
|
|
uint32_t GetDisplayID() const { return mDisplayID; }
|
2018-03-14 03:09:54 +03:00
|
|
|
dom::GamepadHand GetHand() const { return mControllerState.mHand; }
|
|
|
|
uint32_t GetNumButtons() const { return mControllerState.mNumButtons; }
|
|
|
|
uint32_t GetNumAxes() const { return mControllerState.mNumAxes; }
|
|
|
|
uint32_t GetNumHaptics() const { return mControllerState.mNumHaptics; }
|
2017-02-06 11:12:52 +03:00
|
|
|
|
|
|
|
uint32_t mControllerID;
|
|
|
|
VRDeviceType mType;
|
|
|
|
dom::GamepadMappingType mMappingType;
|
2017-07-14 12:01:09 +03:00
|
|
|
uint32_t mDisplayID;
|
2018-03-14 03:09:54 +03:00
|
|
|
VRControllerState mControllerState;
|
2017-02-06 11:12:52 +03:00
|
|
|
bool operator==(const VRControllerInfo& other) const {
|
|
|
|
return mType == other.mType &&
|
|
|
|
mControllerID == other.mControllerID &&
|
2018-03-14 03:09:54 +03:00
|
|
|
strncmp(mControllerState.mControllerName, other.mControllerState.mControllerName, kVRControllerNameMaxLen) == 0 &&
|
2017-02-06 11:12:52 +03:00
|
|
|
mMappingType == other.mMappingType &&
|
2017-07-14 12:01:09 +03:00
|
|
|
mDisplayID == other.mDisplayID &&
|
2018-03-14 03:09:54 +03:00
|
|
|
mControllerState.mHand == other.mControllerState.mHand &&
|
|
|
|
mControllerState.mNumButtons == other.mControllerState.mNumButtons &&
|
|
|
|
mControllerState.mNumAxes == other.mControllerState.mNumAxes &&
|
|
|
|
mControllerState.mNumHaptics == other.mControllerState.mNumHaptics;
|
2017-02-06 11:12:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const VRControllerInfo& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-28 13:43:25 +03:00
|
|
|
struct VRTelemetry
|
|
|
|
{
|
|
|
|
VRTelemetry()
|
|
|
|
: mLastDroppedFrameCount(-1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
mPresentationStart = TimeStamp();
|
|
|
|
mLastDroppedFrameCount = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLastDroppedFrameValid() {
|
|
|
|
return (mLastDroppedFrameCount != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp mPresentationStart;
|
|
|
|
int32_t mLastDroppedFrameCount;
|
|
|
|
};
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
class VRSystemManager {
|
2015-07-02 18:58:24 +03:00
|
|
|
public:
|
2016-02-25 02:54:50 +03:00
|
|
|
static uint32_t AllocateDisplayID();
|
2017-07-14 12:01:09 +03:00
|
|
|
static uint32_t AllocateControllerID();
|
2015-07-02 18:58:24 +03:00
|
|
|
|
|
|
|
protected:
|
2016-02-25 02:54:50 +03:00
|
|
|
static Atomic<uint32_t> sDisplayBase;
|
2017-07-14 12:01:09 +03:00
|
|
|
static Atomic<uint32_t> sControllerBase;
|
2015-09-18 00:23:13 +03:00
|
|
|
|
2015-04-01 23:02:20 +03:00
|
|
|
public:
|
2017-01-24 12:49:11 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRSystemManager)
|
2015-04-01 23:02:20 +03:00
|
|
|
|
|
|
|
virtual void Destroy() = 0;
|
2017-03-30 16:35:49 +03:00
|
|
|
virtual void Shutdown() = 0;
|
2017-10-11 00:42:37 +03:00
|
|
|
virtual void Enumerate() = 0;
|
|
|
|
virtual void NotifyVSync();
|
|
|
|
virtual bool ShouldInhibitEnumeration();
|
|
|
|
virtual void GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult) = 0;
|
2017-03-22 04:58:06 +03:00
|
|
|
virtual bool GetIsPresenting() = 0;
|
2017-01-24 12:49:11 +03:00
|
|
|
virtual void HandleInput() = 0;
|
|
|
|
virtual void GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult) = 0;
|
|
|
|
virtual void ScanForControllers() = 0;
|
|
|
|
virtual void RemoveControllers() = 0;
|
2017-02-02 09:57:58 +03:00
|
|
|
virtual void VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
|
2017-11-30 14:37:07 +03:00
|
|
|
double aIntensity, double aDuration, const VRManagerPromise& aPromise) = 0;
|
2017-02-02 09:57:58 +03:00
|
|
|
virtual void StopVibrateHaptic(uint32_t aControllerIdx) = 0;
|
2017-04-06 14:00:20 +03:00
|
|
|
void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed, bool aTouched,
|
|
|
|
double aValue);
|
2017-01-24 12:49:11 +03:00
|
|
|
void NewAxisMove(uint32_t aIndex, uint32_t aAxis, double aValue);
|
|
|
|
void NewPoseState(uint32_t aIndex, const dom::GamepadPoseState& aPose);
|
2017-04-18 10:58:34 +03:00
|
|
|
void NewHandChangeEvent(uint32_t aIndex, const dom::GamepadHand aHand);
|
2017-02-06 11:12:52 +03:00
|
|
|
void AddGamepad(const VRControllerInfo& controllerInfo);
|
2017-01-24 12:49:11 +03:00
|
|
|
void RemoveGamepad(uint32_t aIndex);
|
2015-04-01 23:02:20 +03:00
|
|
|
|
|
|
|
protected:
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager() : mControllerCount(0) { }
|
2017-11-20 07:00:31 +03:00
|
|
|
virtual ~VRSystemManager() = default;
|
2017-01-24 12:49:11 +03:00
|
|
|
|
|
|
|
uint32_t mControllerCount;
|
2014-07-09 23:24:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* GFX_VR_H */
|