diff --git a/gfx/vr/VRManager.cpp b/gfx/vr/VRManager.cpp index 0bd47500d8f7b..7c2839d239c57 100644 --- a/gfx/vr/VRManager.cpp +++ b/gfx/vr/VRManager.cpp @@ -156,6 +156,7 @@ VRManager::NotifyVsync(const TimeStamp& aVsyncTimestamp) const double kVRDisplayRefreshMaxDuration = 5000; // milliseconds bool bHaveEventListener = false; + bool bHaveControllerListener = false; for (auto iter = mVRManagerParents.Iter(); !iter.Done(); iter.Next()) { VRManagerParent *vmp = iter.Get()->GetKey(); @@ -163,6 +164,7 @@ VRManager::NotifyVsync(const TimeStamp& aVsyncTimestamp) Unused << vmp->SendNotifyVSync(); } bHaveEventListener |= vmp->HaveEventListener(); + bHaveControllerListener |= vmp->HaveControllerListener(); } for (auto iter = mVRDisplays.Iter(); !iter.Done(); iter.Next()) { @@ -180,7 +182,9 @@ VRManager::NotifyVsync(const TimeStamp& aVsyncTimestamp) if (mLastRefreshTime.IsNull()) { // This is the first vsync, must refresh VR displays RefreshVRDisplays(); - RefreshVRControllers(); + if (bHaveControllerListener) { + RefreshVRControllers(); + } mLastRefreshTime = TimeStamp::Now(); } else { // We don't have to do this every frame, so check if we diff --git a/gfx/vr/gfxVR.cpp b/gfx/vr/gfxVR.cpp index 16f403a639528..a2012c6c2bd7d 100644 --- a/gfx/vr/gfxVR.cpp +++ b/gfx/vr/gfxVR.cpp @@ -59,12 +59,15 @@ VRFieldOfView::ConstructProjectionMatrix(float zNear, float zFar, } void -VRSystemManager::AddGamepad(const char* aID, dom::GamepadMappingType aMapping, - dom::GamepadHand aHand, uint32_t aNumButtons, uint32_t aNumAxes) +VRSystemManager::AddGamepad(const VRControllerInfo& controllerInfo) { - dom::GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), mControllerCount, - aMapping, aHand, dom::GamepadServiceType::VR, aNumButtons, - aNumAxes); + dom::GamepadAdded a(NS_ConvertUTF8toUTF16(controllerInfo.GetControllerName()), + mControllerCount, + controllerInfo.GetMappingType(), + controllerInfo.GetHand(), + dom::GamepadServiceType::VR, + controllerInfo.GetNumButtons(), + controllerInfo.GetNumAxes()); VRManager* vm = VRManager::Get(); MOZ_ASSERT(vm); diff --git a/gfx/vr/gfxVR.h b/gfx/vr/gfxVR.h index 18deb3b875c8e..f873f925ab4b8 100644 --- a/gfx/vr/gfxVR.h +++ b/gfx/vr/gfxVR.h @@ -204,6 +204,39 @@ struct VRHMDSensorState { } }; +struct VRControllerInfo +{ + VRDeviceType GetType() const { return mType; } + uint32_t GetControllerID() const { return mControllerID; } + const nsCString& GetControllerName() const { return mControllerName; } + dom::GamepadMappingType GetMappingType() const { return mMappingType; } + dom::GamepadHand GetHand() const { return mHand; } + uint32_t GetNumButtons() const { return mNumButtons; } + uint32_t GetNumAxes() const { return mNumAxes; } + + uint32_t mControllerID; + VRDeviceType mType; + nsCString mControllerName; + dom::GamepadMappingType mMappingType; + dom::GamepadHand mHand; + uint32_t mNumButtons; + uint32_t mNumAxes; + + bool operator==(const VRControllerInfo& other) const { + return mType == other.mType && + mControllerID == other.mControllerID && + mControllerName == other.mControllerName && + mMappingType == other.mMappingType && + mHand == other.mHand && + mNumButtons == other.mNumButtons && + mNumAxes == other.mNumAxes; + } + + bool operator!=(const VRControllerInfo& other) const { + return !(*this == other); + } +}; + class VRSystemManager { public: static uint32_t AllocateDisplayID(); @@ -224,8 +257,7 @@ class VRSystemManager { void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed); void NewAxisMove(uint32_t aIndex, uint32_t aAxis, double aValue); void NewPoseState(uint32_t aIndex, const dom::GamepadPoseState& aPose); - void AddGamepad(const char* aID, dom::GamepadMappingType aMapping, - dom::GamepadHand aHand, uint32_t aNumButtons, uint32_t aNumAxes); + void AddGamepad(const VRControllerInfo& controllerInfo); void RemoveGamepad(uint32_t aIndex); protected: @@ -244,36 +276,6 @@ class VRSystemManager { VRControllerHost* aController) = 0; }; -struct VRControllerInfo -{ - VRDeviceType GetType() const { return mType; } - uint32_t GetControllerID() const { return mControllerID; } - const nsCString& GetControllerName() const { return mControllerName; } - dom::GamepadMappingType GetMappingType() const { return mMappingType; } - uint32_t GetNumButtons() const { return mNumButtons; } - uint32_t GetNumAxes() const { return mNumAxes; } - - uint32_t mControllerID; - VRDeviceType mType; - nsCString mControllerName; - dom::GamepadMappingType mMappingType; - uint32_t mNumButtons; - uint32_t mNumAxes; - - bool operator==(const VRControllerInfo& other) const { - return mType == other.mType && - mControllerID == other.mControllerID && - mControllerName == other.mControllerName && - mMappingType == other.mMappingType && - mNumButtons == other.mNumButtons && - mNumAxes == other.mNumAxes; - } - - bool operator!=(const VRControllerInfo& other) const { - return !(*this == other); - } -}; - } // namespace gfx } // namespace mozilla diff --git a/gfx/vr/gfxVROpenVR.cpp b/gfx/vr/gfxVROpenVR.cpp index 1832934ab9e31..8454775b8e421 100644 --- a/gfx/vr/gfxVROpenVR.cpp +++ b/gfx/vr/gfxVROpenVR.cpp @@ -420,6 +420,34 @@ VRDisplayOpenVR::NotifyVSync() PollEvents(); } +VRControllerOpenVR::VRControllerOpenVR() + : VRControllerHost(VRDeviceType::OpenVR) +{ + MOZ_COUNT_CTOR_INHERITED(VRControllerOpenVR, VRControllerHost); + mControllerInfo.mControllerName.AssignLiteral("OpenVR Gamepad"); + mControllerInfo.mMappingType = GamepadMappingType::_empty; + mControllerInfo.mHand = GamepadHand::_empty; + mControllerInfo.mNumButtons = gNumOpenVRButtonMask; + mControllerInfo.mNumAxes = gNumOpenVRAxis; +} + +VRControllerOpenVR::~VRControllerOpenVR() +{ + MOZ_COUNT_DTOR_INHERITED(VRControllerOpenVR, VRControllerHost); +} + +void +VRControllerOpenVR::SetTrackedIndex(uint32_t aTrackedIndex) +{ + mTrackedIndex = aTrackedIndex; +} + +uint32_t +VRControllerOpenVR::GetTrackedIndex() +{ + return mTrackedIndex; +} + VRSystemManagerOpenVR::VRSystemManagerOpenVR() : mVRSystem(nullptr), mOpenVRInstalled(false) { @@ -463,6 +491,7 @@ VRSystemManagerOpenVR::Destroy() mOpenVRHMD = nullptr; } RemoveControllers(); + mVRSystem = nullptr; mOpenVRInstalled = false; } } @@ -707,11 +736,11 @@ VRSystemManagerOpenVR::ScanForControllers() RefPtr openVRController = new VRControllerOpenVR(); openVRController->SetIndex(mControllerCount); openVRController->SetTrackedIndex(trackedDevice); + openVRController->SetHand(hand); mOpenVRController.AppendElement(openVRController); // Not already present, add it. - AddGamepad("OpenVR Gamepad", GamepadMappingType::_empty, - hand, gNumOpenVRButtonMask, gNumOpenVRAxis); + AddGamepad(openVRController->GetControllerInfo()); ++mControllerCount; } } @@ -723,30 +752,3 @@ VRSystemManagerOpenVR::RemoveControllers() mOpenVRController.Clear(); mControllerCount = 0; } - -VRControllerOpenVR::VRControllerOpenVR() - : VRControllerHost(VRDeviceType::OpenVR) -{ - MOZ_COUNT_CTOR_INHERITED(VRControllerOpenVR, VRControllerHost); - mControllerInfo.mControllerName.AssignLiteral("OpenVR HMD"); - mControllerInfo.mMappingType = GamepadMappingType::_empty; - mControllerInfo.mNumButtons = gNumOpenVRButtonMask; - mControllerInfo.mNumAxes = gNumOpenVRAxis; -} - -VRControllerOpenVR::~VRControllerOpenVR() -{ - MOZ_COUNT_DTOR_INHERITED(VRControllerOpenVR, VRControllerHost); -} - -void -VRControllerOpenVR::SetTrackedIndex(uint32_t aTrackedIndex) -{ - mTrackedIndex = aTrackedIndex; -} - -uint32_t -VRControllerOpenVR::GetTrackedIndex() -{ - return mTrackedIndex; -} diff --git a/gfx/vr/ipc/VRManagerParent.cpp b/gfx/vr/ipc/VRManagerParent.cpp index 7e08fa12dcec9..e455fc4be90ce 100644 --- a/gfx/vr/ipc/VRManagerParent.cpp +++ b/gfx/vr/ipc/VRManagerParent.cpp @@ -22,6 +22,7 @@ namespace gfx { VRManagerParent::VRManagerParent(ProcessId aChildProcessId, bool aIsContentChild) : HostIPCAllocator() , mHaveEventListener(false) + , mHaveControllerListener(false) , mIsContentChild(aIsContentChild) { MOZ_COUNT_CTOR(VRManagerParent); @@ -285,6 +286,12 @@ VRManagerParent::HaveEventListener() return mHaveEventListener; } +bool +VRManagerParent::HaveControllerListener() +{ + return mHaveControllerListener; +} + mozilla::ipc::IPCResult VRManagerParent::RecvSetHaveEventListener(const bool& aHaveEventListener) { @@ -296,6 +303,7 @@ mozilla::ipc::IPCResult VRManagerParent::RecvControllerListenerAdded() { VRManager* vm = VRManager::Get(); + mHaveControllerListener = true; // Ask the connected gamepads to be added to GamepadManager vm->ScanForControllers(); return IPC_OK(); @@ -305,6 +313,7 @@ mozilla::ipc::IPCResult VRManagerParent::RecvControllerListenerRemoved() { VRManager* vm = VRManager::Get(); + mHaveControllerListener = false; vm->RemoveControllers(); return IPC_OK(); } diff --git a/gfx/vr/ipc/VRManagerParent.h b/gfx/vr/ipc/VRManagerParent.h index 814f0415dd965..7f2ce7b40b9be 100644 --- a/gfx/vr/ipc/VRManagerParent.h +++ b/gfx/vr/ipc/VRManagerParent.h @@ -51,6 +51,7 @@ class VRManagerParent final : public PVRManagerParent virtual bool IsSameProcess() const override; bool HaveEventListener(); + bool HaveControllerListener(); virtual void NotifyNotUsed(PTextureParent* aTexture, uint64_t aTransactionId) override; virtual void SendAsyncMessage(const InfallibleTArray& aMessage) override; @@ -109,6 +110,7 @@ class VRManagerParent final : public PVRManagerParent // Keep the VRManager alive, until we have destroyed ourselves. RefPtr mVRManagerHolder; bool mHaveEventListener; + bool mHaveControllerListener; bool mIsContentChild; };