From 4fd5f57cc0f6c0bf21cbba1550b2317735635a44 Mon Sep 17 00:00:00 2001 From: timvolodine Date: Tue, 31 Mar 2015 03:45:32 -0700 Subject: [PATCH] [Android] Avoid creating new objects on each Device Orientation update. Device Orientation updates are happening very frequently (typically 60Hz), hence it is important to not create any new objects for each update and instead use preallocated memory. This decreases pressure on the gc and improves efficiency. BUG= TEST=http://jsbin.com/device-inertial-sensor-diagnostics Review URL: https://codereview.chromium.org/894633003 Cr-Commit-Position: refs/heads/master@{#323006} --- .../chromium/content/browser/DeviceSensors.java | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java b/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java index 427a0456c865..9a1ee6c769c2 100644 --- a/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java +++ b/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java @@ -48,15 +48,18 @@ class DeviceSensors implements SensorEventListener { // The lock to access the mNativePtr. private final Object mNativePtrLock = new Object(); - // The acceleration vector including gravity expressed in the body frame. - private float[] mAccelerationIncludingGravityVector; - // The geomagnetic vector expressed in the body frame. private float[] mMagneticFieldVector; // Holds a shortened version of the rotation vector for compatibility purposes. private float[] mTruncatedRotationVector; + // Holds current rotation matrix for the device. + private float[] mDeviceRotationMatrix; + + // Holds Euler angles corresponding to the rotation matrix. + private double[] mRotationAngles; + // Lazily initialized when registering for notifications. private SensorManagerProxy mSensorManagerProxy; @@ -122,6 +125,7 @@ class DeviceSensors implements SensorEventListener { true); mUseBackupOrientationSensors = success; } + ensureRotationStructuresAllocated(); break; case DEVICE_MOTION: // note: device motion spec does not require all sensors to be available @@ -360,32 +364,26 @@ class DeviceSensors implements SensorEventListener { } private void getOrientationFromRotationVector(float[] rotationVector) { - float[] deviceRotationMatrix = new float[9]; - SensorManager.getRotationMatrixFromVector(deviceRotationMatrix, rotationVector); + SensorManager.getRotationMatrixFromVector(mDeviceRotationMatrix, rotationVector); + computeDeviceOrientationFromRotationMatrix(mDeviceRotationMatrix, mRotationAngles); - double[] rotationAngles = new double[3]; - computeDeviceOrientationFromRotationMatrix(deviceRotationMatrix, rotationAngles); - - gotOrientation(Math.toDegrees(rotationAngles[0]), - Math.toDegrees(rotationAngles[1]), - Math.toDegrees(rotationAngles[2])); + gotOrientation(Math.toDegrees(mRotationAngles[0]), + Math.toDegrees(mRotationAngles[1]), + Math.toDegrees(mRotationAngles[2])); } private void getOrientationFromGeomagneticVectors(float[] acceleration, float[] magnetic) { - float[] deviceRotationMatrix = new float[9]; if (acceleration == null || magnetic == null) { return; } - if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, acceleration, magnetic)) { + if (!SensorManager.getRotationMatrix(mDeviceRotationMatrix, null, acceleration, magnetic)) { return; } + computeDeviceOrientationFromRotationMatrix(mDeviceRotationMatrix, mRotationAngles); - double[] rotationAngles = new double[3]; - computeDeviceOrientationFromRotationMatrix(deviceRotationMatrix, rotationAngles); - - gotOrientation(Math.toDegrees(rotationAngles[0]), - Math.toDegrees(rotationAngles[1]), - Math.toDegrees(rotationAngles[2])); + gotOrientation(Math.toDegrees(mRotationAngles[0]), + Math.toDegrees(mRotationAngles[1]), + Math.toDegrees(mRotationAngles[2])); } private SensorManagerProxy getSensorManagerProxy() { @@ -422,6 +420,15 @@ class DeviceSensors implements SensorEventListener { } } + private void ensureRotationStructuresAllocated() { + if (mDeviceRotationMatrix == null) { + mDeviceRotationMatrix = new float[9]; + } + if (mRotationAngles == null) { + mRotationAngles = new double[3]; + } + } + /** * @param sensorTypes List of sensors to activate. * @param rateInMicroseconds Intended delay (in microseconds) between sensor readings. -- 2.11.4.GIT