1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef GFX_VR_OPENVR_H
7 #define GFX_VR_OPENVR_H
8 
9 #include "nsTArray.h"
10 #include "nsIScreen.h"
11 #include "nsCOMPtr.h"
12 #include "mozilla/RefPtr.h"
13 
14 #include "mozilla/gfx/2D.h"
15 #include "mozilla/EnumeratedArray.h"
16 
17 #include "gfxVR.h"
18 
19 // OpenVR Interfaces
20 namespace vr {
21 class IVRChaperone;
22 class IVRCompositor;
23 class IVRSystem;
24 struct TrackedDevicePose_t;
25 }
26 
27 namespace mozilla {
28 namespace gfx {
29 namespace impl {
30 
31 class VRDisplayOpenVR : public VRDisplayHost
32 {
33 public:
34   virtual void NotifyVSync() override;
35   virtual VRHMDSensorState GetSensorState() override;
36   virtual VRHMDSensorState GetImmediateSensorState() override;
37   void ZeroSensor() override;
38 
39 protected:
40   virtual void StartPresentation() override;
41   virtual void StopPresentation() override;
42 #if defined(XP_WIN)
43   virtual void SubmitFrame(mozilla::layers::TextureSourceD3D11* aSource,
44                            const IntSize& aSize,
45                            const VRHMDSensorState& aSensorState,
46                            const gfx::Rect& aLeftEyeRect,
47                            const gfx::Rect& aRightEyeRect) override;
48 #endif
49 
50 public:
51   explicit VRDisplayOpenVR(::vr::IVRSystem *aVRSystem,
52                            ::vr::IVRChaperone *aVRChaperone,
53                            ::vr::IVRCompositor *aVRCompositor);
54 
55 protected:
56   virtual ~VRDisplayOpenVR();
57   void Destroy();
58 
59   VRHMDSensorState GetSensorState(double timeOffset);
60 
61   // not owned by us; global from OpenVR
62   ::vr::IVRSystem *mVRSystem;
63   ::vr::IVRChaperone *mVRChaperone;
64   ::vr::IVRCompositor *mVRCompositor;
65 
66   bool mIsPresenting;
67 
68   void UpdateStageParameters();
69 };
70 
71 } // namespace impl
72 
73 class VRDisplayManagerOpenVR : public VRDisplayManager
74 {
75 public:
76   static already_AddRefed<VRDisplayManagerOpenVR> Create();
77 
78   virtual bool Init() override;
79   virtual void Destroy() override;
80   virtual void GetHMDs(nsTArray<RefPtr<VRDisplayHost> >& aHMDResult) override;
81 protected:
82   VRDisplayManagerOpenVR();
83 
84   // there can only be one
85   RefPtr<impl::VRDisplayOpenVR> mOpenVRHMD;
86   bool mOpenVRInstalled;
87 };
88 
89 namespace impl {
90 
91 class VRControllerOpenVR : public VRControllerHost
92 {
93 public:
94   explicit VRControllerOpenVR();
95   void SetTrackedIndex(uint32_t aTrackedIndex);
96   uint32_t GetTrackedIndex();
97 
98 protected:
99   virtual ~VRControllerOpenVR();
100 
101   // The index of tracked devices from vr::IVRSystem.
102   uint32_t mTrackedIndex;
103 };
104 
105 } // namespace impl
106 
107 class VRControllerManagerOpenVR : public VRControllerManager
108 {
109 public:
110   static already_AddRefed<VRControllerManagerOpenVR> Create();
111 
112   virtual bool Init() override;
113   virtual void Destroy() override;
114   virtual void HandleInput() override;
115   virtual void GetControllers(nsTArray<RefPtr<VRControllerHost>>&
116                               aControllerResult) override;
117   virtual void ScanForDevices() override;
118 
119 private:
120   VRControllerManagerOpenVR();
121   ~VRControllerManagerOpenVR();
122 
123   virtual void HandleButtonPress(uint32_t aControllerIdx,
124                                  uint64_t aButtonPressed) override;
125   virtual void HandleAxisMove(uint32_t aControllerIdx, uint32_t aAxis,
126                               float aValue) override;
127   virtual void HandlePoseTracking(uint32_t aControllerIdx,
128                                   const dom::GamepadPoseState& aPose,
129                                   VRControllerHost* aController) override;
130 
131   bool mOpenVRInstalled;
132   nsTArray<RefPtr<impl::VRControllerOpenVR>> mOpenVRController;
133   vr::IVRSystem *mVRSystem;
134 };
135 
136 } // namespace gfx
137 } // namespace mozilla
138 
139 
140 #endif /* GFX_VR_OPENVR_H */
141