Bug 734391 - coalesce the device motion events. r=jdm
authorDoug Turner <dougt@dougt.org>
Fri, 09 Mar 2012 10:40:43 -0800
changeset 92308 ed1d52905a6147ba68248b1b96a7b6a0372ebebf
parent 92307 fdb8a781820a40e2861c18a647b4706830063237
child 92309 d762c5089a83d6a207445fdf0443955e34023fa2
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs734391
milestone14.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 734391 - coalesce the device motion events. r=jdm
dom/system/Makefile.in
dom/system/android/Makefile.in
dom/system/cocoa/Makefile.in
dom/system/nsDeviceMotion.cpp
dom/system/nsDeviceMotion.h
dom/system/unix/Makefile.in
dom/system/windows/Makefile.in
--- a/dom/system/Makefile.in
+++ b/dom/system/Makefile.in
@@ -82,16 +82,19 @@ EXTRA_COMPONENTS = \
   GPSDGeolocationProvider.manifest \
   $(NULL)
 endif
 
 EXPORTS     = \
   nsDeviceMotion.h \
   $(NULL)
 
+# We fire the nsDOMDeviceAcceleration
+LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src
+
 include $(topsrcdir)/config/config.mk
 
 # we don't want the shared lib, but we want to force the creation of a static lib.
 LIBXUL_LIBRARY   = 1
 FORCE_STATIC_LIB = 1
 EXPORT_LIBRARY = 1
 
 include $(topsrcdir)/config/rules.mk
--- a/dom/system/android/Makefile.in
+++ b/dom/system/android/Makefile.in
@@ -54,12 +54,13 @@ include $(topsrcdir)/ipc/chromium/chromi
 
 CPPSRCS     = \
         nsDeviceMotionSystem.cpp \
         AndroidLocationProvider.cpp \
         nsHapticFeedback.cpp \
         $(NULL)
 
 LOCAL_INCLUDES  += -I$(topsrcdir)/dom/src/geolocation \
+                     -I$(topsrcdir)/content/events/src
                      $(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
--- a/dom/system/cocoa/Makefile.in
+++ b/dom/system/cocoa/Makefile.in
@@ -44,16 +44,19 @@ include $(DEPTH)/config/autoconf.mk
 MODULE      = dom
 LIBRARY_NAME    = domsystemcocoa_s
 
 # we don't want the shared lib, but we want to force the creation of a static lib.
 LIBXUL_LIBRARY   = 1
 FORCE_STATIC_LIB = 1
 EXPORT_LIBRARY = 1
 
+# We fire the nsDOMDeviceAcceleration
+LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src
+
 include $(topsrcdir)/config/config.mk
 
 CMMSRCS     = \
         smslib.mm \
         nsDeviceMotionSystem.mm \
         $(NULL)
 
 include $(topsrcdir)/config/rules.mk
--- a/dom/system/nsDeviceMotion.cpp
+++ b/dom/system/nsDeviceMotion.cpp
@@ -39,21 +39,21 @@
 #include "nsAutoPtr.h"
 #include "nsIDOMEvent.h"
 #include "nsIDOMWindow.h"
 #include "nsPIDOMWindow.h"
 #include "nsIDOMDocument.h"
 #include "nsIDOMEventTarget.h"
 #include "nsIServiceManager.h"
 #include "nsIPrivateDOMEvent.h"
-#include "nsIDOMDeviceOrientationEvent.h"
-#include "nsIDOMDeviceMotionEvent.h"
 #include "nsIServiceManager.h"
 #include "nsIPrefService.h"
-#include "nsDOMDeviceMotionEvent.h"
+
+using mozilla::TimeStamp;
+using mozilla::TimeDuration;
 
 // also see sDefaultSensorHint in mobile/android/base/GeckoAppShell.java
 #define DEFAULT_SENSOR_POLL 100
 
 static const nsTArray<nsIDOMWindow*>::index_type NoIndex =
     nsTArray<nsIDOMWindow*>::NoIndex;
 
 class nsDeviceMotionData : public nsIDeviceMotionData
@@ -124,16 +124,17 @@ nsDeviceMotion::nsDeviceMotion()
 {
   nsCOMPtr<nsIPrefBranch> prefSrv = do_GetService(NS_PREFSERVICE_CONTRACTID);
   if (prefSrv) {
     bool bvalue;
     nsresult rv = prefSrv->GetBoolPref("device.motion.enabled", &bvalue);
     if (NS_SUCCEEDED(rv) && bvalue == false)
       mEnabled = false;
   }
+  mLastDOMMotionEventTime = TimeStamp::Now();
 }
 
 nsDeviceMotion::~nsDeviceMotion()
 {
   if (mTimeoutTimer)
     mTimeoutTimer->Cancel();
 }
 
@@ -356,50 +357,58 @@ nsDeviceMotion::FireDOMOrientationEvent(
 
 void
 nsDeviceMotion::FireDOMMotionEvent(nsIDOMDocument *domdoc,
                                    nsIDOMEventTarget *target,
                                    PRUint32 type,
                                    double x,
                                    double y,
                                    double z) {
+  // Attempt to coalesce events
+  bool fireEvent = TimeStamp::Now() > mLastDOMMotionEventTime + TimeDuration::FromMilliseconds(DEFAULT_SENSOR_POLL);
+
+  switch (type) {
+  case nsIDeviceMotionData::TYPE_LINEAR_ACCELERATION:
+      mLastAcceleration = new nsDOMDeviceAcceleration(x, y, z);
+      break;
+  case nsIDeviceMotionData::TYPE_ACCELERATION:
+      mLastAccelerationIncluduingGravity = new nsDOMDeviceAcceleration(x, y, z);
+      break;
+  case nsIDeviceMotionData::TYPE_GYROSCOPE:
+      mLastRotationRate = new nsDOMDeviceRotationRate(x, y, z);
+      break;
+  }
+
+  if (!fireEvent && (!mLastAcceleration || !mLastAccelerationIncluduingGravity || !mLastRotationRate)) {
+      return;
+  }
+
   nsCOMPtr<nsIDOMEvent> event;
-  bool defaultActionEnabled = true;
   domdoc->CreateEvent(NS_LITERAL_STRING("DeviceMotionEvent"), getter_AddRefs(event));
 
   nsCOMPtr<nsIDOMDeviceMotionEvent> me = do_QueryInterface(event);
 
   if (!me) {
     return;
-}
-
-  nsRefPtr<nsDOMDeviceAcceleration> acceleration;
-  nsRefPtr<nsDOMDeviceAcceleration> accelerationIncluduingGravity;
-  nsRefPtr<nsDOMDeviceRotationRate> rotationRate;
-
-  switch (type) {
-  case nsIDeviceMotionData::TYPE_LINEAR_ACCELERATION:
-      acceleration = new nsDOMDeviceAcceleration(x, y, z);
-      break;
-  case nsIDeviceMotionData::TYPE_ACCELERATION:
-      accelerationIncluduingGravity = new nsDOMDeviceAcceleration(x, y, z);
-      break;
-  case nsIDeviceMotionData::TYPE_GYROSCOPE:
-      rotationRate = new nsDOMDeviceRotationRate(x, y, z);
-      break;
   }
 
-
   me->InitDeviceMotionEvent(NS_LITERAL_STRING("devicemotion"),
                             true,
                             false,
-                            acceleration,
-                            accelerationIncluduingGravity,
-                            rotationRate,
+                            mLastAcceleration,
+                            mLastAccelerationIncluduingGravity,
+                            mLastRotationRate,
                             DEFAULT_SENSOR_POLL);
 
   nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
   if (privateEvent)
     privateEvent->SetTrusted(true);
   
+  bool defaultActionEnabled = true;
   target->DispatchEvent(event, &defaultActionEnabled);
+
+  mLastRotationRate = nsnull;
+  mLastAccelerationIncluduingGravity = nsnull;
+  mLastAcceleration = nsnull;
+  mLastDOMMotionEventTime = TimeStamp::Now();
+
 }
 
--- a/dom/system/nsDeviceMotion.h
+++ b/dom/system/nsDeviceMotion.h
@@ -38,16 +38,20 @@
 #define nsDeviceMotion_h
 
 #include "nsIDeviceMotion.h"
 #include "nsIDOMDeviceMotionEvent.h"
 #include "nsCOMArray.h"
 #include "nsTArray.h"
 #include "nsCOMPtr.h"
 #include "nsITimer.h"
+#include "nsIDOMDeviceOrientationEvent.h"
+#include "nsIDOMDeviceMotionEvent.h"
+#include "nsDOMDeviceMotionEvent.h"
+#include "mozilla/TimeStamp.h"
 
 #define NS_DEVICE_MOTION_CID \
 { 0xecba5203, 0x77da, 0x465a, \
 { 0x86, 0x5e, 0x78, 0xb7, 0xaf, 0x10, 0xd8, 0xf7 } }
 
 #define NS_DEVICE_MOTION_CONTRACTID "@mozilla.org/devicemotion;1"
 
 class nsIDOMWindow;
@@ -86,15 +90,21 @@ private:
                                double gamma);
 
   void FireDOMMotionEvent(class nsIDOMDocument *domDoc, 
                           class nsIDOMEventTarget *target,
                           PRUint32 type,
                           double x,
                           double y,
                           double z);
-  bool mEnabled;
 
   virtual void Startup()  = 0;
   virtual void Shutdown() = 0;
+
+  bool mEnabled;
+  mozilla::TimeStamp mLastDOMMotionEventTime;
+  nsRefPtr<nsDOMDeviceAcceleration> mLastAcceleration;
+  nsRefPtr<nsDOMDeviceAcceleration> mLastAccelerationIncluduingGravity;
+  nsRefPtr<nsDOMDeviceRotationRate> mLastRotationRate;
+
 };
 
 #endif
--- a/dom/system/unix/Makefile.in
+++ b/dom/system/unix/Makefile.in
@@ -77,9 +77,12 @@ CPPSRCS         += $(MOCSRCS) \
                    nsQTMDeviceMotionSystem.cpp \
                    $(NULL)
 LOCAL_INCLUDES  += $(MOZ_QT_CFLAGS) \
                    -I$(topsrcdir)/dom/src/geolocation \
                    $(NULL)
 endif
 endif
 
+# We fire the nsDOMDeviceAcceleration
+LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src
+
 include $(topsrcdir)/config/rules.mk
--- a/dom/system/windows/Makefile.in
+++ b/dom/system/windows/Makefile.in
@@ -44,16 +44,19 @@ include $(DEPTH)/config/autoconf.mk
 MODULE      = dom
 LIBRARY_NAME    = domsystemwindows_s
 
 # we don't want the shared lib, but we want to force the creation of a static lib.
 LIBXUL_LIBRARY   = 1
 FORCE_STATIC_LIB = 1
 EXPORT_LIBRARY = 1
 
+# We fire the nsDOMDeviceAcceleration
+LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src
+
 include $(topsrcdir)/config/config.mk
 
 CPPSRCS     = \
         nsDeviceMotionSystem.cpp \
         nsHapticFeedback.cpp \
         $(NULL)
 
 include $(topsrcdir)/config/rules.mk