diff options
author | Mathias Agopian <mathias@google.com> | 2011-02-15 12:34:48 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-02-15 12:34:48 -0800 |
commit | f27ebdeb39affdc3ce0a1eb4d2bf1db2583ce834 (patch) | |
tree | eb67d9b6b1f1a2c7d4172d7c9d32799929a69089 /core/java/android/hardware/SensorManager.java | |
parent | f2599ba7889b5cd990891dc49a1c5c908a7499bd (diff) | |
parent | 929ced35698152b84f12a8b3662f5e50cfd12913 (diff) | |
download | frameworks_base-f27ebdeb39affdc3ce0a1eb4d2bf1db2583ce834.zip frameworks_base-f27ebdeb39affdc3ce0a1eb4d2bf1db2583ce834.tar.gz frameworks_base-f27ebdeb39affdc3ce0a1eb4d2bf1db2583ce834.tar.bz2 |
Merge "fix [3452769] SensorEventListener can cause a java allocation at each received event [external]"
Diffstat (limited to 'core/java/android/hardware/SensorManager.java')
-rw-r--r-- | core/java/android/hardware/SensorManager.java | 88 |
1 files changed, 56 insertions, 32 deletions
diff --git a/core/java/android/hardware/SensorManager.java b/core/java/android/hardware/SensorManager.java index 1bd8ef5..dd4096b 100644 --- a/core/java/android/hardware/SensorManager.java +++ b/core/java/android/hardware/SensorManager.java @@ -380,6 +380,58 @@ public class SensorManager /*-----------------------------------------------------------------------*/ + private class SensorEventPool { + private final int mPoolSize; + private final SensorEvent mPool[]; + private int mNumItemsInPool; + + private SensorEvent createSensorEvent() { + // maximal size for all legacy events is 3 + return new SensorEvent(3); + } + + SensorEventPool(int poolSize) { + mPoolSize = poolSize; + mNumItemsInPool = poolSize; + mPool = new SensorEvent[poolSize]; + } + + SensorEvent getFromPool() { + SensorEvent t = null; + synchronized (this) { + if (mNumItemsInPool > 0) { + // remove the "top" item from the pool + final int index = mPoolSize - mNumItemsInPool; + t = mPool[index]; + mPool[index] = null; + mNumItemsInPool--; + } + } + if (t == null) { + // the pool was empty or this item was removed from the pool for + // the first time. In any case, we need to create a new item. + t = createSensorEvent(); + } + return t; + } + + void returnToPool(SensorEvent t) { + synchronized (this) { + // is there space left in the pool? + if (mNumItemsInPool < mPoolSize) { + // if so, return the item to the pool + mNumItemsInPool++; + final int index = mPoolSize - mNumItemsInPool; + mPool[index] = t; + } + } + } + } + + private static SensorEventPool sPool; + + /*-----------------------------------------------------------------------*/ + static private class SensorThread { Thread mThread; @@ -485,10 +537,9 @@ public class SensorManager /*-----------------------------------------------------------------------*/ private class ListenerDelegate { - final SensorEventListener mSensorEventListener; + private final SensorEventListener mSensorEventListener; private final ArrayList<Sensor> mSensorList = new ArrayList<Sensor>(); private final Handler mHandler; - private SensorEvent mValuesPool; public SparseBooleanArray mSensors = new SparseBooleanArray(); public SparseBooleanArray mFirstEvent = new SparseBooleanArray(); public SparseIntArray mSensorAccuracies = new SparseIntArray(); @@ -527,40 +578,12 @@ public class SensorManager } mSensorEventListener.onSensorChanged(t); - returnToPool(t); + sPool.returnToPool(t); } }; addSensor(sensor); } - protected SensorEvent createSensorEvent() { - // maximal size for all legacy events is 3 - return new SensorEvent(3); - } - - protected SensorEvent getFromPool() { - SensorEvent t = null; - synchronized (this) { - // remove the array from the pool - t = mValuesPool; - mValuesPool = null; - } - if (t == null) { - // the pool was empty, we need a new one - t = createSensorEvent(); - } - return t; - } - - protected void returnToPool(SensorEvent t) { - synchronized (this) { - // put back the array into the pool - if (mValuesPool == null) { - mValuesPool = t; - } - } - } - Object getListener() { return mSensorEventListener; } @@ -582,7 +605,7 @@ public class SensorManager } void onSensorChangedLocked(Sensor sensor, float[] values, long[] timestamp, int accuracy) { - SensorEvent t = getFromPool(); + SensorEvent t = sPool.getFromPool(); final float[] v = t.values; v[0] = values[0]; v[1] = values[1]; @@ -644,6 +667,7 @@ public class SensorManager } } while (i>0); + sPool = new SensorEventPool( sFullSensorsList.size()*2 ); sSensorThread = new SensorThread(); } } |