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 #include "PuppetSession.h"
8 
9 #include "nsString.h"
10 #include "VRPuppetCommandBuffer.h"
11 #include "mozilla/StaticPrefs_dom.h"
12 
13 #if defined(XP_WIN)
14 #  include <d3d11.h>
15 #  include "mozilla/gfx/DeviceManagerDx.h"
16 #elif defined(XP_MACOSX)
17 #  include "mozilla/gfx/MacIOSurface.h"
18 #endif
19 
20 using namespace mozilla::gfx;
21 
22 namespace mozilla::gfx {
23 
PuppetSession()24 PuppetSession::PuppetSession() : VRSession() {}
25 
~PuppetSession()26 PuppetSession::~PuppetSession() { Shutdown(); }
27 
Initialize(mozilla::gfx::VRSystemState & aSystemState,bool aDetectRuntimesOnly)28 bool PuppetSession::Initialize(mozilla::gfx::VRSystemState& aSystemState,
29                                bool aDetectRuntimesOnly) {
30   if (!StaticPrefs::dom_vr_enabled() || !StaticPrefs::dom_vr_puppet_enabled()) {
31     return false;
32   }
33   if (aDetectRuntimesOnly) {
34     aSystemState.displayState.capabilityFlags |=
35         VRDisplayCapabilityFlags::Cap_ImmersiveVR;
36     return false;
37   }
38   VRPuppetCommandBuffer::Get().Run(aSystemState);
39   if (!aSystemState.displayState.isConnected) {
40     return false;
41   }
42 #if defined(XP_WIN)
43   if (!CreateD3DObjects()) {
44     Shutdown();
45     return false;
46   }
47 #endif
48 
49   // Succeeded
50   return true;
51 }
52 
53 #if defined(XP_WIN)
CreateD3DObjects()54 bool PuppetSession::CreateD3DObjects() {
55   RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetVRDevice();
56   if (!device) {
57     return false;
58   }
59   if (!CreateD3DContext(device)) {
60     return false;
61   }
62   return true;
63 }
64 #endif
65 
Shutdown()66 void PuppetSession::Shutdown() {}
67 
StartFrame(mozilla::gfx::VRSystemState & aSystemState)68 void PuppetSession::StartFrame(mozilla::gfx::VRSystemState& aSystemState) {
69   VRPuppetCommandBuffer::Get().Run(aSystemState);
70 }
71 
ProcessEvents(mozilla::gfx::VRSystemState & aSystemState)72 void PuppetSession::ProcessEvents(mozilla::gfx::VRSystemState& aSystemState) {
73   VRPuppetCommandBuffer& puppet = VRPuppetCommandBuffer::Get();
74   puppet.Run(aSystemState);
75   if (!aSystemState.displayState.isConnected) {
76     mShouldQuit = true;
77   }
78 }
79 
80 #if defined(XP_WIN)
SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive & aLayer,ID3D11Texture2D * aTexture)81 bool PuppetSession::SubmitFrame(
82     const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
83     ID3D11Texture2D* aTexture) {
84   return VRPuppetCommandBuffer::Get().SubmitFrame();
85 }
86 #elif defined(XP_MACOSX)
SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive & aLayer,const VRLayerTextureHandle & aTexture)87 bool PuppetSession::SubmitFrame(
88     const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
89     const VRLayerTextureHandle& aTexture) {
90   return VRPuppetCommandBuffer::Get().SubmitFrame();
91 }
92 #endif
93 
StopPresentation()94 void PuppetSession::StopPresentation() {
95   VRPuppetCommandBuffer::Get().StopPresentation();
96 }
97 
StartPresentation()98 bool PuppetSession::StartPresentation() {
99   VRPuppetCommandBuffer::Get().StartPresentation();
100   return true;
101 }
102 
VibrateHaptic(uint32_t aControllerIdx,uint32_t aHapticIndex,float aIntensity,float aDuration)103 void PuppetSession::VibrateHaptic(uint32_t aControllerIdx,
104                                   uint32_t aHapticIndex, float aIntensity,
105                                   float aDuration) {
106   VRPuppetCommandBuffer::Get().VibrateHaptic(aControllerIdx, aHapticIndex,
107                                              aIntensity, aDuration);
108 }
109 
StopVibrateHaptic(uint32_t aControllerIdx)110 void PuppetSession::StopVibrateHaptic(uint32_t aControllerIdx) {
111   VRPuppetCommandBuffer::Get().StopVibrateHaptic(aControllerIdx);
112 }
113 
StopAllHaptics()114 void PuppetSession::StopAllHaptics() {
115   VRPuppetCommandBuffer::Get().StopAllHaptics();
116 }
117 
118 }  // namespace mozilla::gfx
119