diff options
author | Chien-Yu Chen <cychen@google.com> | 2015-05-22 20:33:25 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-05-22 20:33:26 +0000 |
commit | 9e07ffdbfbd85ba5431d5145a98df9e688de7b41 (patch) | |
tree | f848eda9a892ccd04eabd127022fa4dfa7318ea3 /media | |
parent | ed3236e07176ba02c7549da4794f680fc468455c (diff) | |
parent | 11c5d0dd01cccc500796febf63aab404e8ae058b (diff) | |
download | frameworks_base-9e07ffdbfbd85ba5431d5145a98df9e688de7b41.zip frameworks_base-9e07ffdbfbd85ba5431d5145a98df9e688de7b41.tar.gz frameworks_base-9e07ffdbfbd85ba5431d5145a98df9e688de7b41.tar.bz2 |
Merge "ImageReader: Register estimated native allocation" into mnc-dev
Diffstat (limited to 'media')
-rw-r--r-- | media/java/android/media/ImageReader.java | 14 | ||||
-rw-r--r-- | media/java/android/media/ImageUtils.java | 56 |
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); + } } |