Bug 1305889 - Part 2: Using VRControllerInfo as the parameter for AddGamepad; r=kip
authorDaosheng Mu <daoshengmu@gmail.com>
Mon, 06 Feb 2017 16:12:52 +0800
changeset 479886 595bbe23999f3d935c17fe77e50313ff1bb3c149
parent 479885 270900fec93245a4c8e93df3e74be837735766d7
child 479887 7ec2ddd042e65ee9e48445fd0bd342b86922192e
push id44393
push userVYV03354@nifty.ne.jp
push dateTue, 07 Feb 2017 13:53:48 +0000
reviewerskip
bugs1305889
milestone54.0a1
Bug 1305889 - Part 2: Using VRControllerInfo as the parameter for AddGamepad; r=kip MozReview-Commit-ID: Coye62hZxRs
gfx/vr/VRManager.cpp
gfx/vr/gfxVR.cpp
gfx/vr/gfxVR.h
gfx/vr/gfxVROpenVR.cpp
gfx/vr/ipc/VRManagerParent.cpp
gfx/vr/ipc/VRManagerParent.h
--- a/gfx/vr/VRManager.cpp
+++ b/gfx/vr/VRManager.cpp
@@ -151,23 +151,25 @@ VRManager::RemoveVRManagerParent(VRManag
 }
 
 void
 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();
     if (mVRDisplays.Count()) {
       Unused << vmp->SendNotifyVSync();
     }
     bHaveEventListener |= vmp->HaveEventListener();
+    bHaveControllerListener |= vmp->HaveControllerListener();
   }
 
   for (auto iter = mVRDisplays.Iter(); !iter.Done(); iter.Next()) {
     gfx::VRDisplayHost* display = iter.UserData();
     display->NotifyVSync();
   }
 
   if (bHaveEventListener) {
@@ -175,17 +177,19 @@ VRManager::NotifyVsync(const TimeStamp& 
     // we must continually refresh the VR display enumeration to check
     // for events that we must fire such as Window.onvrdisplayconnect
     // Note that enumeration itself may activate display hardware, such
     // as Oculus, so we only do this when we know we are displaying content
     // that is looking for VR displays.
     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
       // have refreshed recently.
       TimeDuration duration = TimeStamp::Now() - mLastRefreshTime;
       if (duration.ToMilliseconds() > kVRDisplayRefreshMaxDuration) {
         RefreshVRDisplays();
         RefreshVRControllers();
--- a/gfx/vr/gfxVR.cpp
+++ b/gfx/vr/gfxVR.cpp
@@ -54,22 +54,25 @@ VRFieldOfView::ConstructProjectionMatrix
 
   m[2*4+3] = handednessScale;
   m[3*4+3] = 0.0f;
 
   return mobj;
 }
 
 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);
   vm->NotifyGamepadChange<dom::GamepadAdded>(a);
 }
 
 void
 VRSystemManager::RemoveGamepad(uint32_t aIndex)
--- a/gfx/vr/gfxVR.h
+++ b/gfx/vr/gfxVR.h
@@ -199,16 +199,49 @@ struct VRHMDSensorState {
   float linearVelocity[3];
   float linearAcceleration[3];
 
   void Clear() {
     memset(this, 0, sizeof(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();
 
 protected:
   static Atomic<uint32_t> sDisplayBase;
 
 public:
@@ -219,18 +252,17 @@ public:
   virtual void GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult) = 0;
   virtual void HandleInput() = 0;
   virtual void GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult) = 0;
   virtual void ScanForControllers() = 0;
   virtual void RemoveControllers() = 0;
   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:
   VRSystemManager() : mControllerCount(0) { }
   virtual ~VRSystemManager() { }
 
   uint32_t mControllerCount;
 
@@ -239,42 +271,12 @@ private:
                                  uint64_t aButtonPressed) = 0;
   virtual void HandleAxisMove(uint32_t aControllerIdx, uint32_t aAxis,
                               float aValue) = 0;
   virtual void HandlePoseTracking(uint32_t aControllerIdx,
                                   const dom::GamepadPoseState& aPose,
                                   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
 
 #endif /* GFX_VR_H */
--- a/gfx/vr/gfxVROpenVR.cpp
+++ b/gfx/vr/gfxVROpenVR.cpp
@@ -415,16 +415,44 @@ VRDisplayOpenVR::NotifyVSync()
 {
   // We update mIsConneced once per frame.
   mDisplayInfo.mIsConnected = vr_IsHmdPresent();
 
   // Make sure we respond to OpenVR events even when not presenting
   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)
 {
 }
 
 /*static*/ already_AddRefed<VRSystemManagerOpenVR>
 VRSystemManagerOpenVR::Create()
 {
@@ -458,16 +486,17 @@ VRSystemManagerOpenVR::Init()
 void
 VRSystemManagerOpenVR::Destroy()
 {
   if (mOpenVRInstalled) {
     if (mOpenVRHMD) {
       mOpenVRHMD = nullptr;
     }
     RemoveControllers();
+    mVRSystem = nullptr;
     mOpenVRInstalled = false;
   }
 }
 
 void
 VRSystemManagerOpenVR::GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult)
 {
   if (!mOpenVRInstalled) {
@@ -702,51 +731,24 @@ VRSystemManagerOpenVR::ScanForController
           break;
         case vr::ETrackedControllerRole::TrackedControllerRole_RightHand:
           hand = GamepadHand::Right;
           break;
       }
       RefPtr<VRControllerOpenVR> 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;
     }
   }
 }
 
 void
 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;
-}
--- a/gfx/vr/ipc/VRManagerParent.cpp
+++ b/gfx/vr/ipc/VRManagerParent.cpp
@@ -17,16 +17,17 @@
 
 namespace mozilla {
 using namespace layers;
 namespace gfx {
 
 VRManagerParent::VRManagerParent(ProcessId aChildProcessId, bool aIsContentChild)
   : HostIPCAllocator()
   , mHaveEventListener(false)
+  , mHaveControllerListener(false)
   , mIsContentChild(aIsContentChild)
 {
   MOZ_COUNT_CTOR(VRManagerParent);
   MOZ_ASSERT(NS_IsMainThread());
 
   SetOtherProcessId(aChildProcessId);
 }
 
@@ -280,36 +281,44 @@ VRManagerParent::RecvGetImmediateSensorS
 }
 
 bool
 VRManagerParent::HaveEventListener()
 {
   return mHaveEventListener;
 }
 
+bool
+VRManagerParent::HaveControllerListener()
+{
+  return mHaveControllerListener;
+}
+
 mozilla::ipc::IPCResult
 VRManagerParent::RecvSetHaveEventListener(const bool& aHaveEventListener)
 {
   mHaveEventListener = aHaveEventListener;
   return IPC_OK();
 }
 
 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();
 }
 
 mozilla::ipc::IPCResult
 VRManagerParent::RecvControllerListenerRemoved()
 {
   VRManager* vm = VRManager::Get();
+  mHaveControllerListener = false;
   vm->RemoveControllers();
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult
 VRManagerParent::RecvGetControllers(nsTArray<VRControllerInfo> *aControllers)
 {
   VRManager* vm = VRManager::Get();
--- a/gfx/vr/ipc/VRManagerParent.h
+++ b/gfx/vr/ipc/VRManagerParent.h
@@ -46,16 +46,17 @@ public:
   virtual bool AllocUnsafeShmem(size_t aSize,
     ipc::SharedMemory::SharedMemoryType aType,
     ipc::Shmem* aShmem) override;
 
   virtual void DeallocShmem(ipc::Shmem& aShmem) override;
 
   virtual bool IsSameProcess() const override;
   bool HaveEventListener();
+  bool HaveControllerListener();
 
   virtual void NotifyNotUsed(PTextureParent* aTexture, uint64_t aTransactionId) override;
   virtual void SendAsyncMessage(const InfallibleTArray<AsyncParentMessageData>& aMessage) override;
   bool SendGamepadUpdate(const GamepadChangeEvent& aGamepadEvent);
 
 protected:
   ~VRManagerParent();
 
@@ -104,15 +105,16 @@ private:
   RefPtr<VRManagerParent> mSelfRef;
 
   // Keep the compositor thread alive, until we have destroyed ourselves.
   RefPtr<layers::CompositorThreadHolder> mCompositorThreadHolder;
 
   // Keep the VRManager alive, until we have destroyed ourselves.
   RefPtr<VRManager> mVRManagerHolder;
   bool mHaveEventListener;
+  bool mHaveControllerListener;
   bool mIsContentChild;
 };
 
 } // namespace mozilla
 } // namespace gfx
 
 #endif // MOZILLA_GFX_VR_VRMANAGERPARENT_H