From a825b8f4139440d26d678fd4aca9e7db53b8adb0 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 8 May 2012 14:36:07 -0700 Subject: [PATCH] Bug 745145, part 0c: Only use nsIScreen on the main thread. r=mwu --- widget/gonk/nsAppShell.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/widget/gonk/nsAppShell.cpp b/widget/gonk/nsAppShell.cpp index e2225957caf..d49b769ceb5 100644 --- a/widget/gonk/nsAppShell.cpp +++ b/widget/gonk/nsAppShell.cpp @@ -517,18 +517,27 @@ GeckoInputDispatcher::unregisterInputChannel(const sp& inputChanne return OK; } +static already_AddRefed +Screen() +{ + nsCOMPtr screenMgr = + do_GetService("@mozilla.org/gfx/screenmanager;1"); + nsCOMPtr 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 screen = Screen(); + return screen->SetRotation(mRotation); } private: - nsCOMPtr mScreen; PRUint32 mRotation; }; @@ -538,11 +547,6 @@ public: : mLastUpdate(0) { } void Notify(const SensorData& aSensorData) { - nsCOMPtr screenMgr = - do_GetService("@mozilla.org/gfx/screenmanager;1"); - nsCOMPtr screen; - screenMgr->GetPrimaryScreen(getter_AddRefs(screen)); - MOZ_ASSERT(aSensorData.sensor() == SensorType::SENSOR_ORIENTATION); InfallibleTArray values = aSensorData.values(); // float azimuth = values[0]; // unused @@ -560,10 +564,8 @@ public: 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(); @@ -572,9 +574,9 @@ public: 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