summaryrefslogtreecommitdiffstats
path: root/media
diff options
context:
space:
mode:
authorChien-Yu Chen <cychen@google.com>2015-05-21 17:59:22 -0700
committerChien-Yu Chen <cychen@google.com>2015-05-22 11:41:02 -0700
commit11c5d0dd01cccc500796febf63aab404e8ae058b (patch)
treecb6f5cc475ce106280c7bb341fb24561ed47e04e /media
parent310f381eac558bce069b52fbda9a8aeb83608858 (diff)
downloadframeworks_base-11c5d0dd01cccc500796febf63aab404e8ae058b.zip
frameworks_base-11c5d0dd01cccc500796febf63aab404e8ae058b.tar.gz
frameworks_base-11c5d0dd01cccc500796febf63aab404e8ae058b.tar.bz2
ImageReader: Register estimated native allocation
Estimate the native allocation and register it so it can be accounted for during GC. Bug: 20299272 Change-Id: I43af3058b6fe9a41e5e7e119e390ed079402c4b8
Diffstat (limited to 'media')
-rw-r--r--media/java/android/media/ImageReader.java14
-rw-r--r--media/java/android/media/ImageUtils.java56
2 files changed, 70 insertions, 0 deletions
diff --git a/media/java/android/media/ImageReader.java b/media/java/android/media/ImageReader.java
index 9bd721a..c97de5d 100644
--- a/media/java/android/media/ImageReader.java
+++ b/media/java/android/media/ImageReader.java
@@ -23,6 +23,8 @@ import android.os.Looper;
import android.os.Message;
import android.view.Surface;
+import dalvik.system.VMRuntime;
+
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -148,6 +150,13 @@ public class ImageReader implements AutoCloseable {
nativeInit(new WeakReference<ImageReader>(this), width, height, format, maxImages);
mSurface = nativeGetSurface();
+
+ // Estimate the native buffer allocation size and register it so it gets accounted for
+ // during GC. Note that this doesn't include the buffers required by the buffer queue
+ // itself and the buffers requested by the producer.
+ mEstimatedNativeAllocBytes = ImageUtils.getEstimatedNativeAllocBytes(width, height, format,
+ maxImages);
+ VMRuntime.getRuntime().registerNativeAllocation(mEstimatedNativeAllocBytes);
}
/**
@@ -467,6 +476,10 @@ public class ImageReader implements AutoCloseable {
setOnImageAvailableListener(null, null);
if (mSurface != null) mSurface.release();
nativeClose();
+ if (mEstimatedNativeAllocBytes > 0) {
+ VMRuntime.getRuntime().registerNativeFree(mEstimatedNativeAllocBytes);
+ mEstimatedNativeAllocBytes = 0;
+ }
}
@Override
@@ -606,6 +619,7 @@ public class ImageReader implements AutoCloseable {
private final int mMaxImages;
private final int mNumPlanes;
private final Surface mSurface;
+ private int mEstimatedNativeAllocBytes;
private final Object mListenerLock = new Object();
private OnImageAvailableListener mListener;
diff --git a/media/java/android/media/ImageUtils.java b/media/java/android/media/ImageUtils.java
index c312525..2763d1d 100644
--- a/media/java/android/media/ImageUtils.java
+++ b/media/java/android/media/ImageUtils.java
@@ -119,4 +119,60 @@ class ImageUtils {
dstBuffer.rewind();
}
}
+
+ /**
+ * Return the estimated native allocation size in bytes based on width, height, format,
+ * and number of images.
+ *
+ * <p>This is a very rough estimation and should only be used for native allocation
+ * registration in VM so it can be accounted for during GC.</p>
+ *
+ * @param width The width of the images.
+ * @param height The height of the images.
+ * @param format The format of the images.
+ * @param numImages The number of the images.
+ */
+ public static int getEstimatedNativeAllocBytes(int width, int height, int format,
+ int numImages) {
+ double estimatedBytePerPixel;
+ switch (format) {
+ // 10x compression from RGB_888
+ case ImageFormat.JPEG:
+ case ImageFormat.DEPTH_POINT_CLOUD:
+ estimatedBytePerPixel = 0.3;
+ break;
+ case ImageFormat.Y8:
+ estimatedBytePerPixel = 1.0;
+ break;
+ case ImageFormat.RAW10:
+ estimatedBytePerPixel = 1.25;
+ break;
+ case ImageFormat.YV12:
+ case ImageFormat.YUV_420_888:
+ case ImageFormat.NV21:
+ case ImageFormat.PRIVATE: // A really rough estimate because the real size is unknown.
+ estimatedBytePerPixel = 1.5;
+ break;
+ case ImageFormat.NV16:
+ case PixelFormat.RGB_565:
+ case ImageFormat.YUY2:
+ case ImageFormat.Y16:
+ case ImageFormat.RAW_SENSOR:
+ case ImageFormat.DEPTH16:
+ estimatedBytePerPixel = 2.0;
+ break;
+ case PixelFormat.RGB_888:
+ estimatedBytePerPixel = 3.0;
+ break;
+ case PixelFormat.RGBA_8888:
+ case PixelFormat.RGBX_8888:
+ estimatedBytePerPixel = 4.0;
+ break;
+ default:
+ throw new UnsupportedOperationException(
+ String.format("Invalid format specified %d", format));
+ }
+
+ return (int)(width * height * estimatedBytePerPixel * numImages);
+ }
}