PuppetSession.cpp (3526B)
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 24 PuppetSession::PuppetSession() = default; 25 26 PuppetSession::~PuppetSession() { Shutdown(); } 27 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 (!VRPuppetCommandBuffer::IsCreated()) { 34 // We only want to initialize VRPuppetCommandBuffer on the main thread. 35 // We can assume if it is not initialized, that the puppet display 36 // would not be enumerated. 37 return false; 38 } 39 if (aDetectRuntimesOnly) { 40 aSystemState.displayState.capabilityFlags |= 41 VRDisplayCapabilityFlags::Cap_ImmersiveVR; 42 return false; 43 } 44 VRPuppetCommandBuffer::Get().Run(aSystemState); 45 if (!aSystemState.displayState.isConnected) { 46 return false; 47 } 48 #if defined(XP_WIN) 49 if (!CreateD3DObjects()) { 50 Shutdown(); 51 return false; 52 } 53 #endif 54 55 // Succeeded 56 return true; 57 } 58 59 #if defined(XP_WIN) 60 bool PuppetSession::CreateD3DObjects() { 61 RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetVRDevice(); 62 if (!device) { 63 return false; 64 } 65 if (!CreateD3DContext(device)) { 66 return false; 67 } 68 return true; 69 } 70 #endif 71 72 void PuppetSession::Shutdown() {} 73 74 void PuppetSession::StartFrame(mozilla::gfx::VRSystemState& aSystemState) { 75 VRPuppetCommandBuffer::Get().Run(aSystemState); 76 } 77 78 void PuppetSession::ProcessEvents(mozilla::gfx::VRSystemState& aSystemState) { 79 VRPuppetCommandBuffer& puppet = VRPuppetCommandBuffer::Get(); 80 puppet.Run(aSystemState); 81 if (!aSystemState.displayState.isConnected) { 82 mShouldQuit = true; 83 } 84 } 85 86 #if defined(XP_WIN) 87 bool PuppetSession::SubmitFrame( 88 const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer, 89 ID3D11Texture2D* aTexture) { 90 return VRPuppetCommandBuffer::Get().SubmitFrame(); 91 } 92 #elif defined(XP_MACOSX) 93 bool PuppetSession::SubmitFrame( 94 const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer, 95 const VRLayerTextureHandle& aTexture) { 96 return VRPuppetCommandBuffer::Get().SubmitFrame(); 97 } 98 #endif 99 100 void PuppetSession::StopPresentation() { 101 VRPuppetCommandBuffer::Get().StopPresentation(); 102 } 103 104 bool PuppetSession::StartPresentation() { 105 VRPuppetCommandBuffer::Get().StartPresentation(); 106 return true; 107 } 108 109 void PuppetSession::VibrateHaptic(uint32_t aControllerIdx, 110 uint32_t aHapticIndex, float aIntensity, 111 float aDuration) { 112 VRPuppetCommandBuffer::Get().VibrateHaptic(aControllerIdx, aHapticIndex, 113 aIntensity, aDuration); 114 } 115 116 void PuppetSession::StopVibrateHaptic(uint32_t aControllerIdx) { 117 VRPuppetCommandBuffer::Get().StopVibrateHaptic(aControllerIdx); 118 } 119 120 void PuppetSession::StopAllHaptics() { 121 VRPuppetCommandBuffer::Get().StopAllHaptics(); 122 } 123 124 } // namespace mozilla::gfx