Bug 1498625 - Part 1: Move VRHapticThread start/end to the main thread. r=kip,froydnj
authorDaosheng Mu <daoshengmu@gmail.com>
Fri, 02 Nov 2018 21:54:12 +0000
changeset 444483 604c2334df1d25f4abc1c5a8a2cad89e1ecfce5a
parent 444482 ccf46ccf351988116723f6a78c887757941c5348
child 444484 65c79ddf98f0fd8c9bf88244fce40e2b1c7b2c4b
push id34996
push userrgurzau@mozilla.com
push dateTue, 06 Nov 2018 09:53:23 +0000
treeherdermozilla-central@e160f0a60e4f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskip, froydnj
bugs1498625
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1498625 - Part 1: Move VRHapticThread start/end to the main thread. r=kip,froydnj Differential Revision: https://phabricator.services.mozilla.com/D10642
gfx/vr/ipc/VRParent.cpp
gfx/vr/service/OpenVRSession.cpp
--- a/gfx/vr/ipc/VRParent.cpp
+++ b/gfx/vr/ipc/VRParent.cpp
@@ -109,16 +109,22 @@ VRParent::ActorDestroy(ActorDestroyReaso
 }
 
 bool
 VRParent::Init(base::ProcessId aParentPid,
                const char* aParentBuildID,
                MessageLoop* aIOLoop,
                IPC::Channel* aChannel)
 {
+  // Initialize the thread manager before starting IPC. Otherwise, messages
+  // may be posted to the main thread and we won't be able to process them.
+  if (NS_WARN_IF(NS_FAILED(nsThreadManager::get().Init()))) {
+    return false;
+  }
+
   // Now it's safe to start IPC.
   if (NS_WARN_IF(!Open(aChannel, aParentPid, aIOLoop))) {
     return false;
   }
 
   // This must be checked before any IPDL message, which may hit sentinel
   // errors due to parent and content processes having different
   // versions.
@@ -131,13 +137,17 @@ VRParent::Init(base::ProcessId aParentPi
 
   // Ensure gfxPrefs are initialized.
   gfxPrefs::GetSingleton();
   gfxConfig::Init();
   gfxVars::Initialize();
 #if defined(XP_WIN)
   DeviceManagerDx::Init();
 #endif
+  if (NS_FAILED(NS_InitMinimalXPCOM())) {
+    return false;
+  }
+
   return true;
 }
 
 } // namespace gfx
 } // namespace mozilla
\ No newline at end of file
--- a/gfx/vr/service/OpenVRSession.cpp
+++ b/gfx/vr/service/OpenVRSession.cpp
@@ -152,18 +152,20 @@ OpenVRSession::Initialize(mozilla::gfx::
   // Configure coordinate system
   mVRCompositor->SetTrackingSpace(::vr::TrackingUniverseSeated);
 
   if (!InitState(aSystemState)) {
     Shutdown();
     return false;
   }
 
-  StartHapticThread();
-  StartHapticTimer();
+  NS_DispatchToMainThread(NS_NewRunnableFunction(
+    "OpenVRSession::StartHapticThread", [this]() {
+      StartHapticThread();
+  }));
   
   // Succeeded
   return true;
 }
 
 #if defined(XP_WIN)
 bool
 OpenVRSession::CreateD3DObjects()
@@ -922,27 +924,33 @@ OpenVRSession::VibrateHaptic(uint32_t aC
    *         and replace the TriggerHapticPulse calls which have been
    *         deprecated.
    */
 }
 
 void
 OpenVRSession::StartHapticThread()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   if (!mHapticThread) {
     mHapticThread = new VRThread(NS_LITERAL_CSTRING("VR_OpenVR_Haptics"));
   }
   mHapticThread->Start();
+  StartHapticTimer();
 }
 
 void
 OpenVRSession::StopHapticThread()
 {
   if (mHapticThread) {
-    mHapticThread->Shutdown();
+    NS_DispatchToMainThread(NS_NewRunnableFunction(
+      "mHapticThread::Shutdown",
+      [thread = mHapticThread]() {
+        thread->Shutdown();
+    }));
     mHapticThread = nullptr;
   }
 }
 
 void
 OpenVRSession::StartHapticTimer()
 {
   if (!mHapticTimer && mHapticThread) {