Bug 735330 - move device motion from dom/system to hal. gonk cleanup. r=jdm
authorDoug Turner <dougt@dougt.org>
Tue, 20 Mar 2012 23:36:17 -0700
changeset 89929 3aa1b84ef8077037a22a93385aec0ec4d672b310
parent 89928 96bcc799772727243f4ec99f4d04acb7a9787e39
child 89930 3d6c7cf537de04da648ca12f5eab6a21ce45c1f3
push id22300
push usermak77@bonardo.net
push dateThu, 22 Mar 2012 12:08:29 +0000
treeherdermozilla-central@b622d692b8ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs735330
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 735330 - move device motion from dom/system to hal. gonk cleanup. r=jdm
hal/gonk/GonkSensor.cpp
layout/build/nsLayoutModule.cpp
--- a/hal/gonk/GonkSensor.cpp
+++ b/hal/gonk/GonkSensor.cpp
@@ -13,108 +13,159 @@
 #include "SensorDevice.h"
 #include "nsThreadUtils.h"
 
 using namespace mozilla::hal;
 using namespace android;
 
 namespace mozilla {
 
+#define DEFAULT_DEVICE_POLL_RATE 100000000 /*100ms*/
+
+
+double radToDeg(double a) {
+  return a * (180.0 / M_PI);
+}
+
 static SensorType
 HardwareSensorToHalSensor(int type)
 {     
   switch(type) {
     case SENSOR_TYPE_ORIENTATION:
       return SENSOR_ORIENTATION;
     case SENSOR_TYPE_ACCELEROMETER:
       return SENSOR_ACCELERATION;
     case SENSOR_TYPE_PROXIMITY:
       return SENSOR_PROXIMITY;
+    case SENSOR_TYPE_GYROSCOPE:
+      return SENSOR_GYROSCOPE;
+    case SENSOR_TYPE_LINEAR_ACCELERATION:
+      return SENSOR_LINEAR_ACCELERATION;
     default:
       return SENSOR_UNKNOWN;
   }
 }
 
+static SensorAccuracyType
+HardwareStatusToHalAccuracy(int status) {
+  return static_cast<SensorAccuracyType>(status);
+}
+
 static int
 HalSensorToHardwareSensor(SensorType type)
 {
   switch(type) {
     case SENSOR_ORIENTATION:
       return SENSOR_TYPE_ORIENTATION;
     case SENSOR_ACCELERATION:
       return SENSOR_TYPE_ACCELEROMETER;
     case SENSOR_PROXIMITY:
       return SENSOR_TYPE_PROXIMITY;
+    case SENSOR_GYROSCOPE:
+      return SENSOR_TYPE_GYROSCOPE;
+    case SENSOR_LINEAR_ACCELERATION:
+      return SENSOR_TYPE_LINEAR_ACCELERATION;
     default:
       return -1;
   }
 }
 
-static bool
-SensorseventToSensorData(const sensors_event_t& data, SensorData* aSensorData)
+static int
+SensorseventStatus(const sensors_event_t& data)
 {
-  aSensorData->sensor() = HardwareSensorToHalSensor(data.type);
+  int type = data.type;
+  switch(type) {
+    case SENSOR_ORIENTATION:
+      return data.orientation.status;
+    case SENSOR_LINEAR_ACCELERATION:
+    case SENSOR_ACCELERATION:
+      return data.acceleration.status;
+    case SENSOR_GYROSCOPE:
+      return data.gyro.status;
+  }
 
-  if (aSensorData->sensor() == SENSOR_UNKNOWN)
-    return false;
 
-  aSensorData->timestamp() = data.timestamp;
-  aSensorData->values()[0] = data.data[0];
-  aSensorData->values()[1] = data.data[1];
-  aSensorData->values()[2] = data.data[2];
-  aSensorData->accuracy()  = SENSOR_ACCURACY_UNKNOWN;
-  return true;
+  return SENSOR_STATUS_UNRELIABLE;
 }
 
-static void
-onSensorChanged(const sensors_event_t& data, SensorData* aSensorData)
+class SensorRunnable : public nsRunnable
 {
-  DebugOnly<bool> convertedData = SensorseventToSensorData(data, aSensorData);
-  MOZ_ASSERT(convertedData);
-  NotifySensorChange(*aSensorData);
-}
+public:
+  SensorRunnable(const sensors_event_t& data)
+  {
+    mSensorData.sensor() = HardwareSensorToHalSensor(data.type);
+    mSensorData.accuracy() = HardwareStatusToHalAccuracy(SensorseventStatus(data));
+    mSensorData.timestamp() = data.timestamp;
+    if (mSensorData.sensor() == SENSOR_GYROSCOPE) {
+      // libhardware returns gyro as rad.  convert.
+      mSensorValues.AppendElement(radToDeg(data.data[0]));
+      mSensorValues.AppendElement(radToDeg(data.data[1]));
+      mSensorValues.AppendElement(radToDeg(data.data[2]));
+    } else {
+      mSensorValues.AppendElement(data.data[0]);
+      mSensorValues.AppendElement(data.data[1]);
+      mSensorValues.AppendElement(data.data[2]);
+    }
+    mSensorData.values() = mSensorValues;
+  }
+
+  ~SensorRunnable() {}
+
+  NS_IMETHOD Run()
+  {
+    NotifySensorChange(mSensorData);
+    return NS_OK;
+  }
+
+private:
+  SensorData mSensorData;
+  InfallibleTArray<float> mSensorValues;
+};
 
 namespace hal_impl {
 
 static pthread_t sThread;
-static bool sInitialized;
-static bool sContinue;
-static int sActivatedSensors;
-static SensorData sSensordata[NUM_SENSOR_TYPE];
+static bool sInitialized = false;
+static bool sContinue = true;
+static int sActivatedSensors = 0;
 static nsCOMPtr<nsIThread> sSwitchThread;
 
 static void*
 UpdateSensorData(void* /*unused*/)
 {
   SensorDevice &device = SensorDevice::getInstance();
   const size_t numEventMax = 16;
   sensors_event_t buffer[numEventMax];
   int count = 0;
 
   while (sContinue) {
     count = device.poll(buffer, numEventMax);
+
     if (count < 0) {
       continue;
     }
 
     for (int i=0; i<count; i++) {
-      onSensorChanged(buffer[i], &sSensordata[HardwareSensorToHalSensor(buffer[i].type)]);
+      if (SensorseventStatus(buffer[i]) == SENSOR_STATUS_UNRELIABLE) {
+        continue;
+      }
+      NS_DispatchToMainThread(new SensorRunnable(buffer[i]));
     }
   }
 
   return NULL;
 }
 
 static void 
 InitializeResources()
 {
+  sInitialized = true;
+  sContinue = true;
   pthread_create(&sThread, NULL, &UpdateSensorData, NULL);
   NS_NewThread(getter_AddRefs(sSwitchThread));
-  sInitialized = true;
-  sContinue = true;
 }
 
 static void 
 ReleaseResources()
 {
   sContinue = false;
   pthread_join(sThread, NULL);
   sSwitchThread->Shutdown();
@@ -127,16 +178,17 @@ class SensorInfo {
     NS_INLINE_DECL_REFCOUNTING(SensorInfo)
 
     SensorInfo(bool aActivate, sensor_t aSensor, pthread_t aThreadId) :
                activate(aActivate), sensor(aSensor), threadId(aThreadId) { }
 
     void Switch() {
      SensorDevice& device = SensorDevice::getInstance();
      device.activate((void*)threadId, sensor.handle, activate);
+     device.setDelay((void*)threadId, sensor.handle, DEFAULT_DEVICE_POLL_RATE);
     }
 
   protected:
     SensorInfo() { };
     bool      activate;
     sensor_t  sensor;
     pthread_t threadId;
 };
@@ -181,15 +233,10 @@ DisableSensorNotifications(SensorType aS
   SensorSwitch(aSensor, false);  
   sActivatedSensors--;
 
   if (!sActivatedSensors) {
     ReleaseResources();  
   }
 }
 
-void 
-GetCurrentSensorDataInformation(SensorType aSensor, SensorData* aSensorData) {
-  *aSensorData = sSensordata[aSensor];
-}
-
 } // hal_impl
 } // mozilla
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -150,19 +150,21 @@ using mozilla::dom::gonk::AudioManager;
 #include "nsTextServicesDocument.h"
 #include "nsTextServicesCID.h"
 
 #include "nsScriptSecurityManager.h"
 #include "nsPrincipal.h"
 #include "nsSystemPrincipal.h"
 #include "nsNullPrincipal.h"
 #include "nsNetCID.h"
+#ifndef MOZ_WIDGET_GONK
 #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_PLATFORM_MAEMO)
 #include "nsHapticFeedback.h"
 #endif
+#endif
 #include "nsParserUtils.h"
 
 #define NS_EDITORCOMMANDTABLE_CID \
 { 0x4f5e62b8, 0xd659, 0x4156, { 0x84, 0xfc, 0x2f, 0x60, 0x99, 0x40, 0x03, 0x69 }}
 
 #define NS_EDITINGCOMMANDTABLE_CID \
 { 0xcb38a746, 0xbeb8, 0x43f3, { 0x94, 0x29, 0x77, 0x96, 0xe1, 0xa9, 0x3f, 0xb4 }}
 
@@ -235,24 +237,17 @@ NS_NewXULTreeBuilder(nsISupports* aOuter
 static void Shutdown();
 
 #ifdef MOZ_XTF
 #include "nsIXTFService.h"
 #include "nsIXMLContentBuilder.h"
 #endif
 
 #include "nsGeolocation.h"
-#ifndef MOZ_WIDGET_GONK
-#if defined(XP_UNIX)    || \
-    defined(_WINDOWS)   || \
-    defined(machintosh) || \
-    defined(android)
 #include "nsDeviceMotion.h"
-#endif
-#endif
 #include "nsCSPService.h"
 #include "nsISmsService.h"
 #include "nsISmsDatabaseService.h"
 #include "mozilla/dom/sms/SmsRequestManager.h"
 #include "mozilla/dom/sms/SmsServicesFactory.h"
 #include "nsIPowerManagerService.h"
 
 using namespace mozilla::dom::sms;
@@ -290,23 +285,20 @@ NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR
                                          DOMRequestService::FactoryCreate)
 #ifdef MOZ_B2G_RIL
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(SystemWorkerManager,
                                          SystemWorkerManager::FactoryCreate)
 #endif
 
 #ifdef MOZ_WIDGET_GONK
 NS_GENERIC_FACTORY_CONSTRUCTOR(AudioManager)
-#else
-#if defined(XP_UNIX)    || \
-    defined(_WINDOWS)   || \
-    defined(machintosh) || \
-    defined(android)
+#endif
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceMotion)
-#endif
+
+#ifndef MOZ_WIDGET_GONK
 #if defined(ANDROID) || defined(MOZ_PLATFORM_MAEMO)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHapticFeedback)
 #endif
 #endif
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(ThirdPartyUtil, Init)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsService, SmsServicesFactory::CreateSmsService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsDatabaseService, SmsServicesFactory::CreateSmsDatabaseService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIPowerManagerService,
@@ -785,24 +777,19 @@ NS_DEFINE_NAMED_CID(NS_CHILDPROCESSMESSA
 NS_DEFINE_NAMED_CID(NSCHANNELPOLICY_CID);
 NS_DEFINE_NAMED_CID(NS_SCRIPTSECURITYMANAGER_CID);
 NS_DEFINE_NAMED_CID(NS_PRINCIPAL_CID);
 NS_DEFINE_NAMED_CID(NS_SYSTEMPRINCIPAL_CID);
 NS_DEFINE_NAMED_CID(NS_NULLPRINCIPAL_CID);
 NS_DEFINE_NAMED_CID(NS_SECURITYNAMESET_CID);
 NS_DEFINE_NAMED_CID(THIRDPARTYUTIL_CID);
 NS_DEFINE_NAMED_CID(NS_STRUCTUREDCLONECONTAINER_CID);
+NS_DEFINE_NAMED_CID(NS_DEVICE_MOTION_CID);
 
 #ifndef MOZ_WIDGET_GONK
-#if defined(XP_UNIX)    || \
-    defined(_WINDOWS)   || \
-    defined(machintosh) || \
-    defined(android)
-NS_DEFINE_NAMED_CID(NS_DEVICE_MOTION_CID);
-#endif
 #if defined(ANDROID) || defined(MOZ_PLATFORM_MAEMO)
 NS_DEFINE_NAMED_CID(NS_HAPTICFEEDBACK_CID);
 #endif
 #endif
 NS_DEFINE_NAMED_CID(SMS_SERVICE_CID);
 NS_DEFINE_NAMED_CID(SMS_DATABASE_SERVICE_CID);
 NS_DEFINE_NAMED_CID(SMS_REQUEST_MANAGER_CID);
 NS_DEFINE_NAMED_CID(NS_POWERMANAGERSERVICE_CID);
@@ -1058,23 +1045,18 @@ static const mozilla::Module::CIDEntry k
   { &kNS_PARENTPROCESSMESSAGEMANAGER_CID, false, NULL, CreateParentMessageManager },
   { &kNS_CHILDPROCESSMESSAGEMANAGER_CID, false, NULL, CreateChildMessageManager },
   { &kNSCHANNELPOLICY_CID, false, NULL, nsChannelPolicyConstructor },
   { &kNS_SCRIPTSECURITYMANAGER_CID, false, NULL, Construct_nsIScriptSecurityManager },
   { &kNS_PRINCIPAL_CID, false, NULL, nsPrincipalConstructor },
   { &kNS_SYSTEMPRINCIPAL_CID, false, NULL, nsSystemPrincipalConstructor },
   { &kNS_NULLPRINCIPAL_CID, false, NULL, nsNullPrincipalConstructor },
   { &kNS_SECURITYNAMESET_CID, false, NULL, nsSecurityNameSetConstructor },
+  { &kNS_DEVICE_MOTION_CID, false, NULL, nsDeviceMotionConstructor },
 #ifndef MOZ_WIDGET_GONK
-#if defined(XP_UNIX)    || \
-    defined(_WINDOWS)   || \
-    defined(machintosh) || \
-    defined(android)
-  { &kNS_DEVICE_MOTION_CID, false, NULL, nsDeviceMotionConstructor },
-#endif
 #if defined(ANDROID) || defined(MOZ_PLATFORM_MAEMO)
   { &kNS_HAPTICFEEDBACK_CID, false, NULL, nsHapticFeedbackConstructor },
 #endif
 #endif
   { &kTHIRDPARTYUTIL_CID, false, NULL, ThirdPartyUtilConstructor },
   { &kNS_STRUCTUREDCLONECONTAINER_CID, false, NULL, nsStructuredCloneContainerConstructor },
   { &kSMS_SERVICE_CID, false, NULL, nsISmsServiceConstructor },
   { &kSMS_DATABASE_SERVICE_CID, false, NULL, nsISmsDatabaseServiceConstructor },
@@ -1196,23 +1178,18 @@ static const mozilla::Module::ContractID
   { NS_CHILDPROCESSMESSAGEMANAGER_CONTRACTID, &kNS_CHILDPROCESSMESSAGEMANAGER_CID },
   { NSCHANNELPOLICY_CONTRACTID, &kNSCHANNELPOLICY_CID },
   { NS_SCRIPTSECURITYMANAGER_CONTRACTID, &kNS_SCRIPTSECURITYMANAGER_CID },
   { NS_GLOBAL_CHANNELEVENTSINK_CONTRACTID, &kNS_SCRIPTSECURITYMANAGER_CID },
   { NS_PRINCIPAL_CONTRACTID, &kNS_PRINCIPAL_CID },
   { NS_SYSTEMPRINCIPAL_CONTRACTID, &kNS_SYSTEMPRINCIPAL_CID },
   { NS_NULLPRINCIPAL_CONTRACTID, &kNS_NULLPRINCIPAL_CID },
   { NS_SECURITYNAMESET_CONTRACTID, &kNS_SECURITYNAMESET_CID },
+  { NS_DEVICE_MOTION_CONTRACTID, &kNS_DEVICE_MOTION_CID },
 #ifndef MOZ_WIDGET_GONK
-#if defined(XP_UNIX)    || \
-    defined(_WINDOWS)   || \
-    defined(machintosh) || \
-    defined(android)
-  { NS_DEVICE_MOTION_CONTRACTID, &kNS_DEVICE_MOTION_CID },
-#endif
 #if defined(ANDROID) || defined(MOZ_PLATFORM_MAEMO)
   { "@mozilla.org/widget/hapticfeedback;1", &kNS_HAPTICFEEDBACK_CID },
 #endif
 #endif
   { THIRDPARTYUTIL_CONTRACTID, &kTHIRDPARTYUTIL_CID },
   { NS_STRUCTUREDCLONECONTAINER_CONTRACTID, &kNS_STRUCTUREDCLONECONTAINER_CID },
   { SMS_SERVICE_CONTRACTID, &kSMS_SERVICE_CID },
   { SMS_DATABASE_SERVICE_CONTRACTID, &kSMS_DATABASE_SERVICE_CID },