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_SERVICE_OCULUSSESSION_H
8 #define GFX_VR_SERVICE_OCULUSSESSION_H
9 
10 #include "VRSession.h"
11 
12 #include "mozilla/gfx/2D.h"
13 #include "moz_external_vr.h"
14 #include "nsTArray.h"
15 #include "oculus/ovr_capi_dynamic.h"
16 #include "prlink.h"
17 #include "ShaderDefinitionsD3D11.h"  // for VertexShaderConstants and PixelShaderConstants
18 
19 struct ID3D11Device;
20 
21 namespace mozilla {
22 namespace layers {
23 struct VertexShaderConstants;
24 struct PixelShaderConstants;
25 }  // namespace layers
26 namespace gfx {
27 
28 class OculusSession : public VRSession {
29  public:
30   OculusSession();
31   virtual ~OculusSession();
32 
33   bool Initialize(mozilla::gfx::VRSystemState& aSystemState,
34                   bool aDetectRuntimesOnly) override;
35   void Shutdown() override;
36   void ProcessEvents(mozilla::gfx::VRSystemState& aSystemState) override;
37   void StartFrame(mozilla::gfx::VRSystemState& aSystemState) override;
38   bool StartPresentation() override;
39   void StopPresentation() override;
40   bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
41                    ID3D11Texture2D* aTexture) override;
42   void VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
43                      float aIntensity, float aDuration) override;
44   void StopVibrateHaptic(uint32_t aControllerIdx) override;
45   void StopAllHaptics() override;
46 
47  private:
48   bool LoadOvrLib();
49   void UnloadOvrLib();
50   bool StartLib(ovrInitFlags aFlags);
51   void StopLib();
52   bool StartSession();
53   void StopSession();
54   bool StartRendering();
55   void StopRendering();
56   bool CreateD3DObjects();
57   bool CreateShaders();
58   void DestroyShaders();
59   void CoverTransitions();
60   void UpdateVisibility();
61   bool ChangeVisibility(bool bVisible);
62   bool InitState(mozilla::gfx::VRSystemState& aSystemState);
63   void UpdateStageParameters(mozilla::gfx::VRDisplayState& aState);
64   void UpdateEyeParameters(mozilla::gfx::VRSystemState& aState);
65   void UpdateHeadsetPose(mozilla::gfx::VRSystemState& aState);
66   void UpdateControllers(VRSystemState& aState);
67   void UpdateControllerInputs(VRSystemState& aState,
68                               const ovrInputState& aInputState);
69   void UpdateHaptics();
70   void EnumerateControllers(VRSystemState& aState,
71                             const ovrInputState& aInputState);
72   void UpdateControllerPose(VRSystemState& aState,
73                             const ovrInputState& aInputState);
74   void UpdateTelemetry(VRSystemState& aSystemState);
75   bool IsPresentationReady() const;
76   bool UpdateConstantBuffers();
77 
78   PRLibrary* mOvrLib;
79   ovrSession mSession;
80   ovrInitFlags mInitFlags;
81   ovrTextureSwapChain mTextureSet;
82   nsTArray<RefPtr<ID3D11RenderTargetView>> mRTView;
83   nsTArray<RefPtr<ID3D11Texture2D>> mTexture;
84   nsTArray<RefPtr<ID3D11ShaderResourceView>> mSRV;
85 
86   ID3D11VertexShader* mQuadVS;
87   ID3D11PixelShader* mQuadPS;
88   RefPtr<ID3D11SamplerState> mLinearSamplerState;
89   layers::VertexShaderConstants mVSConstants;
90   layers::PixelShaderConstants mPSConstants;
91   RefPtr<ID3D11Buffer> mVSConstantBuffer;
92   RefPtr<ID3D11Buffer> mPSConstantBuffer;
93   RefPtr<ID3D11Buffer> mVertexBuffer;
94   RefPtr<ID3D11InputLayout> mInputLayout;
95 
96   IntSize mPresentationSize;
97   ovrFovPort mFOVPort[2];
98 
99   // Most recent HMD eye poses, from start of frame
100   ovrPosef mFrameStartPose[2];
101 
102   float mRemainingVibrateTime[2];
103   float mHapticPulseIntensity[2];
104   TimeStamp mLastHapticUpdate;
105 
106   // The timestamp of the last ending presentation
107   TimeStamp mLastPresentationEnd;
108   bool mIsPresenting;
109 };
110 
111 }  // namespace gfx
112 }  // namespace mozilla
113 
114 #endif  // GFX_VR_SERVICE_OCULUSSESSION_H
115