Bug 745145, part 0c: Only use nsIScreen on the main thread. r=mwu
authorChris Jones <jones.chris.g@gmail.com>
Tue, 08 May 2012 14:36:07 -0700
changeset 95797 61ce3cbe0ca12c2aed7e799865815da521e631d2
parent 95796 94ebe5e63dd264166d3c082c21b20a14c11d1383
child 95798 251188d5a55c0fe0cc323505678d041db855bc53
push id1439
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 20:19:22 +0000
treeherdermozilla-aurora@ea74834dccd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmwu
bugs745145
milestone15.0a1
Bug 745145, part 0c: Only use nsIScreen on the main thread. r=mwu
widget/gonk/nsAppShell.cpp
--- a/widget/gonk/nsAppShell.cpp
+++ b/widget/gonk/nsAppShell.cpp
@@ -512,42 +512,46 @@ GeckoInputDispatcher::registerInputChann
 }
 
 status_t
 GeckoInputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel)
 {
     return OK;
 }
 
+static already_AddRefed<nsIScreen>
+Screen()
+{
+    nsCOMPtr<nsIScreenManager> screenMgr =
+        do_GetService("@mozilla.org/gfx/screenmanager;1");
+    nsCOMPtr<nsIScreen> screen;
+    screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
+    return screen.forget();
+}
+
 class ScreenRotateEvent : public nsRunnable {
 public:
-  ScreenRotateEvent(nsIScreen* aScreen, PRUint32 aRotation)
-    : mScreen(aScreen),
-      mRotation(aRotation) {
+  ScreenRotateEvent(PRUint32 aRotation)
+    : mRotation(aRotation) {
   }
   NS_IMETHOD Run() {
-    return mScreen->SetRotation(mRotation);
+    nsCOMPtr<nsIScreen> screen = Screen();
+    return screen->SetRotation(mRotation);
   }
 
 private:
-  nsCOMPtr<nsIScreen> mScreen;
   PRUint32 mRotation;
 };
 
 class OrientationSensorObserver : public ISensorObserver {
 public:
   OrientationSensorObserver ()
     : mLastUpdate(0) {
   }
   void Notify(const SensorData& aSensorData) {
-    nsCOMPtr<nsIScreenManager> screenMgr =
-        do_GetService("@mozilla.org/gfx/screenmanager;1");
-    nsCOMPtr<nsIScreen> screen;
-    screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
-
     MOZ_ASSERT(aSensorData.sensor() == SensorType::SENSOR_ORIENTATION);
     InfallibleTArray<float> values = aSensorData.values();
     // float azimuth = values[0]; // unused
     float pitch = values[1];
     float roll = values[2];
     PRUint32 rotation;
     if (roll > 45)
       rotation = nsIScreen::ROTATION_90_DEG;
@@ -555,31 +559,29 @@ public:
       rotation = nsIScreen::ROTATION_270_DEG;
     else if (pitch < -45)
       rotation = nsIScreen::ROTATION_0_DEG;
     else if (pitch > 45)
       rotation = nsIScreen::ROTATION_180_DEG;
     else
       return;  // don't rotate if undecidable
 
-    PRUint32 currRotation;
-    nsresult res;
-    res = screen->GetRotation(&currRotation);
-    if (NS_FAILED(res) || rotation == currRotation)
+    // This check is racy, but that's OK.
+    if (rotation == nsScreenGonk::GetRotation())
       return;
 
     PRTime now = PR_Now();
     MOZ_ASSERT(now > mLastUpdate);
     if (now - mLastUpdate < sMinUpdateInterval)
       return;
 
     mLastUpdate = now;
-    NS_DispatchToMainThread(new ScreenRotateEvent(screen, rotation));
+    NS_DispatchToMainThread(new ScreenRotateEvent(rotation));
+  }
 
-  }
 private:
   PRTime mLastUpdate;
   static const PRTime sMinUpdateInterval = 500 * 1000; // 500 ms
 };
 
 nsAppShell::nsAppShell()
     : mNativeCallbackRequest(false)
     , mHandlers()