Bug 1497787 - MacIOSurface should only be access at VRSession thread. r=kip
authorDaosheng Mu <daoshengmu@gmail.com>
Fri, 12 Oct 2018 21:54:09 +0000
changeset 489416 994a0ec3420ff869b56aa868a2ca471390751e60
parent 489415 a232864e100291f7fe7e7b60745aae2094786b43
child 489417 a8ec93d4b848fa03c678e79ac23f91b8501e3417
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewerskip
bugs1497787
milestone64.0a1
Bug 1497787 - MacIOSurface should only be access at VRSession thread. r=kip MozReview-Commit-ID: FmWOuctM3yH Differential Revision: https://phabricator.services.mozilla.com/D8185
gfx/vr/external_api/moz_external_vr.h
gfx/vr/gfxVRExternal.cpp
gfx/vr/service/OSVRSession.cpp
gfx/vr/service/OSVRSession.h
gfx/vr/service/OpenVRSession.cpp
gfx/vr/service/OpenVRSession.h
gfx/vr/service/VRService.h
gfx/vr/service/VRSession.cpp
gfx/vr/service/VRSession.h
gfx/vr/service/moz.build
--- a/gfx/vr/external_api/moz_external_vr.h
+++ b/gfx/vr/external_api/moz_external_vr.h
@@ -53,16 +53,18 @@ static const int kVRControllerNameMaxLen
 static const int kVRControllerMaxCount = 16;
 static const int kVRControllerMaxButtons = 64;
 static const int kVRControllerMaxAxis = 16;
 static const int kVRLayerMaxCount = 8;
 static const int kVRHapticsMaxCount = 32;
 
 #if defined(__ANDROID__)
 typedef uint64_t VRLayerTextureHandle;
+#elif defined(XP_MACOSX)
+typedef uint32_t VRLayerTextureHandle;
 #else
 typedef void* VRLayerTextureHandle;
 #endif
 
 struct Point3D_POD
 {
   float x;
   float y;
--- a/gfx/vr/gfxVRExternal.cpp
+++ b/gfx/vr/gfxVRExternal.cpp
@@ -212,26 +212,21 @@ VRDisplayExternal::PopulateLayerTexture(
     case SurfaceDescriptor::TSurfaceDescriptorD3D10: {
       const SurfaceDescriptorD3D10& surf = aTexture.get_SurfaceDescriptorD3D10();
       *aTextureType = VRLayerTextureType::LayerTextureType_D3D10SurfaceDescriptor;
       *aTextureHandle = (void *)surf.handle();
       return true;
     }
 #elif defined(XP_MACOSX)
     case SurfaceDescriptor::TSurfaceDescriptorMacIOSurface: {
+      // MacIOSurface ptr can't be fetched or used at different threads.
+      // Both of fetching and using this MacIOSurface are at the VRService thread.
       const auto& desc = aTexture.get_SurfaceDescriptorMacIOSurface();
-      RefPtr<MacIOSurface> surf = MacIOSurface::LookupSurface(desc.surfaceId(),
-                                                              desc.scaleFactor(),
-                                                              !desc.isOpaque());
-      if (!surf) {
-        NS_WARNING("VRDisplayHost::SubmitFrame failed to get a MacIOSurface");
-        return false;
-      }
       *aTextureType = VRLayerTextureType::LayerTextureType_MacIOSurface;
-      *aTextureHandle = (void *)surf->GetIOSurfacePtr();
+      *aTextureHandle = desc.surfaceId();
       return true;
     }
 #elif defined(MOZ_WIDGET_ANDROID)
     case SurfaceDescriptor::TSurfaceTextureDescriptor: {
       const SurfaceTextureDescriptor& desc = aTexture.get_SurfaceTextureDescriptor();
       java::GeckoSurfaceTexture::LocalRef surfaceTexture = java::GeckoSurfaceTexture::Lookup(desc.handle());
       if (!surfaceTexture) {
         NS_WARNING("VRDisplayHost::SubmitFrame failed to get a SurfaceTexture");
--- a/gfx/vr/service/OSVRSession.cpp
+++ b/gfx/vr/service/OSVRSession.cpp
@@ -516,17 +516,17 @@ OSVRSession::SubmitFrame(const mozilla::
                          ID3D11Texture2D* aTexture)
 {
   return false;
   // TODO Implement
 }
 #elif defined(XP_MACOSX)
 bool
 OSVRSession::SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
-                         MacIOSurface* aTexture)
+                         const VRLayerTextureHandle& aTexture)
 {
   return false;
   // TODO Implement
 }
 #endif
 
 void
 OSVRSession::VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
--- a/gfx/vr/service/OSVRSession.h
+++ b/gfx/vr/service/OSVRSession.h
@@ -13,18 +13,16 @@
 #include "mozilla/TimeStamp.h"
 #include "moz_external_vr.h"
 
 #include <osvr/ClientKit/ClientKitC.h>
 #include <osvr/ClientKit/DisplayC.h>
 
 #if defined(XP_WIN)
 #include <d3d11_1.h>
-#elif defined(XP_MACOSX)
-class MacIOSurface;
 #endif
 
 namespace mozilla {
 namespace gfx {
 
 class OSVRSession : public VRSession
 {
 public:
@@ -43,17 +41,17 @@ public:
   void StopAllHaptics() override;
 
 protected:
 #if defined(XP_WIN)
   bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
                    ID3D11Texture2D* aTexture) override;
 #elif defined(XP_MACOSX)
   bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
-                   MacIOSurface* aTexture) override;
+                   const VRLayerTextureHandle& aTexture) override;
 #endif
 
 private:
   bool InitState(mozilla::gfx::VRSystemState& aSystemState);
   void UpdateHeadsetPose(mozilla::gfx::VRSystemState& aState);
   bool mRuntimeLoaded;
   bool mOSVRInitialized;
   bool mClientContextInitialized;
--- a/gfx/vr/service/OpenVRSession.cpp
+++ b/gfx/vr/service/OpenVRSession.cpp
@@ -1,15 +1,17 @@
 #include "OpenVRSession.h"
 #include "gfxPrefs.h"
 
 #if defined(XP_WIN)
 #include <d3d11.h>
 #include "mozilla/gfx/DeviceManagerDx.h"
-#endif // defined(XP_WIN)
+#elif defined(XP_MACOSX)
+#include "mozilla/gfx/MacIOSurface.h"
+#endif
 
 #include "mozilla/dom/GamepadEventTypes.h"
 #include "mozilla/dom/GamepadBinding.h"
 #include "VRThread.h"
 
 #if !defined(M_PI)
 #define M_PI 3.14159265358979323846264338327950288
 #endif
@@ -814,36 +816,48 @@ OpenVRSession::ProcessEvents(mozilla::gf
 bool
 OpenVRSession::SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
                            ID3D11Texture2D* aTexture)
 {
   return SubmitFrame((void *)aTexture,
                      ::vr::ETextureType::TextureType_DirectX,
                      aLayer.mLeftEyeRect, aLayer.mRightEyeRect);
 }
-
 #elif defined(XP_MACOSX)
 bool
 OpenVRSession::SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
-                           MacIOSurface* aTexture)
+                           const VRLayerTextureHandle& aTexture)
 {
-  return SubmitFrame((void *)aTexture,
+  return SubmitFrame(aTexture,
                      ::vr::ETextureType::TextureType_IOSurface,
                      aLayer.mLeftEyeRect, aLayer.mRightEyeRect);
 }
 #endif
 
 bool
-OpenVRSession::SubmitFrame(void* aTextureHandle,
+OpenVRSession::SubmitFrame(const VRLayerTextureHandle& aTextureHandle,
                            ::vr::ETextureType aTextureType,
                            const VRLayerEyeRect& aLeftEyeRect,
                            const VRLayerEyeRect& aRightEyeRect)
 {
   ::vr::Texture_t tex;
+#if defined(XP_MACOSX)
+  // We get aTextureHandle from get_SurfaceDescriptorMacIOSurface() at VRDisplayExternal.
+  // scaleFactor and opaque are skipped because they always are 1.0 and false.
+  RefPtr<MacIOSurface> surf = MacIOSurface::LookupSurface(aTextureHandle);
+  if (!surf) {
+    NS_WARNING("OpenVRSession::SubmitFrame failed to get a MacIOSurface");
+    return false;
+  }
+
+  const void* ioSurface = surf->GetIOSurfacePtr();
+  tex.handle = (void *)ioSurface;
+#else
   tex.handle = aTextureHandle;
+#endif
   tex.eType = aTextureType;
   tex.eColorSpace = ::vr::EColorSpace::ColorSpace_Auto;
 
   ::vr::VRTextureBounds_t bounds;
   bounds.uMin = aLeftEyeRect.x;
   bounds.vMin = 1.0 - aLeftEyeRect.y;
   bounds.uMax = aLeftEyeRect.x + aLeftEyeRect.width;
   bounds.vMax = 1.0 - (aLeftEyeRect.y + aLeftEyeRect.height);
--- a/gfx/vr/service/OpenVRSession.h
+++ b/gfx/vr/service/OpenVRSession.h
@@ -5,24 +5,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_VR_SERVICE_OPENVRSESSION_H
 #define GFX_VR_SERVICE_OPENVRSESSION_H
 
 #include "VRSession.h"
 
 #include "openvr.h"
-#include "mozilla/gfx/2D.h"
 #include "mozilla/TimeStamp.h"
 #include "moz_external_vr.h"
 
 #if defined(XP_WIN)
 #include <d3d11_1.h>
-#elif defined(XP_MACOSX)
-class MacIOSurface;
 #endif
 class nsITimer;
 
 namespace mozilla {
 namespace gfx {
 class VRThread;
 
 static const int kNumOpenVRHaptics = 1;
@@ -45,17 +42,17 @@ public:
   void StopAllHaptics() override;
 
 protected:
 #if defined(XP_WIN)
   bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
                    ID3D11Texture2D* aTexture) override;
 #elif defined(XP_MACOSX)
   bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
-                   MacIOSurface* aTexture) override;
+                   const VRLayerTextureHandle& aTexture) override;
 #endif
 
 private:
   // OpenVR State
   ::vr::IVRSystem* mVRSystem = nullptr;
   ::vr::IVRChaperone* mVRChaperone = nullptr;
   ::vr::IVRCompositor* mVRCompositor = nullptr;
   ::vr::TrackedDeviceIndex_t mControllerDeviceIndex[kVRControllerMaxCount];
@@ -68,17 +65,17 @@ private:
   void UpdateStageParameters(mozilla::gfx::VRDisplayState& aState);
   void UpdateEyeParameters(mozilla::gfx::VRSystemState& aState);
   void UpdateHeadsetPose(mozilla::gfx::VRSystemState& aState);
   void EnumerateControllers(VRSystemState& aState);
   void UpdateControllerPoses(VRSystemState& aState);
   void UpdateControllerButtons(VRSystemState& aState);
   void UpdateTelemetry(VRSystemState& aSystemState);
 
-  bool SubmitFrame(void* aTextureHandle,
+  bool SubmitFrame(const VRLayerTextureHandle& aTextureHandle,
                    ::vr::ETextureType aTextureType,
                    const VRLayerEyeRect& aLeftEyeRect,
                    const VRLayerEyeRect& aRightEyeRect);
 #if defined(XP_WIN)
   bool CreateD3DObjects();
 #endif
   void GetControllerDeviceId(::vr::ETrackedDeviceClass aDeviceType,
                              ::vr::TrackedDeviceIndex_t aDeviceIndex,
@@ -89,12 +86,12 @@ private:
   void StartHapticTimer();
   void StopHapticTimer();
   static void HapticTimerCallback(nsITimer* aTimer, void* aClosure);
   RefPtr<nsITimer> mHapticTimer;
   RefPtr<VRThread> mHapticThread;
   mozilla::Mutex mControllerHapticStateMutex;
 };
 
+} // namespace gfx
 } // namespace mozilla
-} // namespace gfx
 
 #endif // GFX_VR_SERVICE_OPENVRSESSION_H
--- a/gfx/vr/service/VRService.h
+++ b/gfx/vr/service/VRService.h
@@ -3,19 +3,18 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_VR_SERVICE_VRSERVICE_H
 #define GFX_VR_SERVICE_VRSERVICE_H
 
 #include "mozilla/Atomics.h"
-#include "base/process.h"               // for ProcessHandle
-
 #include "moz_external_vr.h"
+#include "base/process.h" // for base::ProcessHandle
 
 namespace base {
 class Thread;
 } // namespace base
 namespace mozilla {
 namespace gfx {
 
 class VRSession;
--- a/gfx/vr/service/VRSession.cpp
+++ b/gfx/vr/service/VRSession.cpp
@@ -122,17 +122,17 @@ VRSession::SubmitFrame(const mozilla::gf
         return false;
       }
       return true;
   }
 
 #elif defined(XP_MACOSX)
 
   if (aLayer.mTextureType == VRLayerTextureType::LayerTextureType_MacIOSurface) {
-    return SubmitFrame(aLayer, (MacIOSurface*)aLayer.mTextureHandle);
+    return SubmitFrame(aLayer, aLayer.mTextureHandle);
   }
 
 #endif
 
   return false;
 }
 
 void
--- a/gfx/vr/service/VRSession.h
+++ b/gfx/vr/service/VRSession.h
@@ -2,18 +2,16 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_VR_SERVICE_VRSESSION_H
 #define GFX_VR_SERVICE_VRSESSION_H
 
-#include "VRSession.h"
-
 #include "moz_external_vr.h"
 
 #if defined(XP_WIN)
 #include <d3d11_1.h>
 #elif defined(XP_MACOSX)
 class MacIOSurface;
 #endif
 
@@ -48,17 +46,17 @@ protected:
   RefPtr<ID3D11Device1> mDevice;
   RefPtr<ID3D11DeviceContext1> mContext;
   ID3D11Device1* GetD3DDevice();
   ID3D11DeviceContext1* GetD3DDeviceContext();
   ID3DDeviceContextState* GetD3DDeviceContextState();
   RefPtr<ID3DDeviceContextState> mDeviceContextState;
 #elif defined(XP_MACOSX)
   virtual bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
-                           MacIOSurface* aTexture) = 0;
+                           const VRLayerTextureHandle& aTexture) = 0;
 #endif
   void UpdateTrigger(VRControllerState& aState, uint32_t aButtonIndex, float aValue, float aThreshold);
 };
 
 } // namespace mozilla
 } // namespace gfx
 
 #endif // GFX_VR_SERVICE_VRSESSION_H
--- a/gfx/vr/service/moz.build
+++ b/gfx/vr/service/moz.build
@@ -16,23 +16,26 @@ if CONFIG['OS_TARGET'] != 'Android':
         'OSVRSession.cpp',
         'VRService.cpp',
         'VRSession.cpp',
     ]
     include('/ipc/chromium/chromium-config.mozbuild')
 
 # Build OpenVR on Windows, Linux, and macOS desktop targets
 if CONFIG['OS_TARGET'] in ('WINNT', 'Linux', 'Darwin'):
-    UNIFIED_SOURCES += [
-        'OpenVRSession.cpp',
-    ]
     LOCAL_INCLUDES += [
         '/dom/base',
-        '/gfx/layers/d3d11',
-        '/gfx/thebes',
+        '/gfx/layers/d3d11'
+    ]
+
+    # OpenVRSession includes MacIOSurface.h which includes Mac headers
+    # which define Size and Points types in the root namespace that
+    # often conflict with our own types.
+    SOURCES += [
+        'OpenVRSession.cpp'
     ]
 
 FINAL_LIBRARY = 'xul'
 
 # This is intended as a temporary hack to enable VS2015 builds.
 if CONFIG['CC_TYPE'] in ('msvc', 'clang-cl'):
     # ovr_capi_dynamic.h '<unnamed-tag>': Alignment specifier is less than
     # actual alignment (8), and will be ignored