Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ab41626 authored by Zhijun He's avatar Zhijun He
Browse files

ImageReader/Writer: refactor and cleanup

Below changes are included:
* Defer the buffer lock to Image#getPlanes call. This will save quite a bit
CPU cycles associated with lock buffer if the application doesn't really
want to access the data.
* Refactor the code: move some common code to some utility class, and use
one unified consumer (BufferItemConsumer) in ImageReader native implementation.
The code refactoring will also make it easier to support non-opaque image
attach/detach.

Bug: 22356918
Bug: 19962027
Change-Id: I4fb865b0ea3deb6650afc64c32a5906f30e8ccbd
parent 7ba6dc0f
Loading
Loading
Loading
Loading
+24 −28
Original line number Diff line number Diff line
@@ -335,7 +335,6 @@ public class ImageReader implements AutoCloseable {

            switch (status) {
                case ACQUIRE_SUCCESS:
                    si.createSurfacePlanes();
                    si.mIsImageValid = true;
                case ACQUIRE_NO_BUFS:
                case ACQUIRE_MAX_IMAGES:
@@ -693,7 +692,7 @@ public class ImageReader implements AutoCloseable {
                    width = ImageReader.this.getWidth();
                    break;
                default:
                    width = nativeGetWidth(mFormat);
                    width = nativeGetWidth();
            }
            return width;
        }
@@ -709,7 +708,7 @@ public class ImageReader implements AutoCloseable {
                    height = ImageReader.this.getHeight();
                    break;
                default:
                    height = nativeGetHeight(mFormat);
                    height = nativeGetHeight();
            }
            return height;
        }
@@ -729,6 +728,10 @@ public class ImageReader implements AutoCloseable {
        @Override
        public Plane[] getPlanes() {
            throwISEIfImageIsInvalid();

            if (mPlanes == null) {
                mPlanes = nativeCreatePlanes(ImageReader.this.mNumPlanes, ImageReader.this.mFormat);
            }
            // Shallow copy is fine.
            return mPlanes.clone();
        }
@@ -766,7 +769,8 @@ public class ImageReader implements AutoCloseable {
        }

        private void clearSurfacePlanes() {
            if (mIsImageValid) {
            // Image#getPlanes may not be called before the image is closed.
            if (mIsImageValid && mPlanes != null) {
                for (int i = 0; i < mPlanes.length; i++) {
                    if (mPlanes[i] != null) {
                        mPlanes[i].clearBuffer();
@@ -776,32 +780,25 @@ public class ImageReader implements AutoCloseable {
            }
        }

        private void createSurfacePlanes() {
            mPlanes = new SurfacePlane[ImageReader.this.mNumPlanes];
            for (int i = 0; i < ImageReader.this.mNumPlanes; i++) {
                mPlanes[i] = nativeCreatePlane(i, ImageReader.this.mFormat);
            }
        }
        private class SurfacePlane extends android.media.Image.Plane {
            // SurfacePlane instance is created by native code when a new SurfaceImage is created
            private SurfacePlane(int index, int rowStride, int pixelStride) {
                mIndex = index;
            // SurfacePlane instance is created by native code when SurfaceImage#getPlanes() is
            // called
            private SurfacePlane(int rowStride, int pixelStride, ByteBuffer buffer) {
                mRowStride = rowStride;
                mPixelStride = pixelStride;
                mBuffer = buffer;
                /**
                 * Set the byteBuffer order according to host endianness (native
                 * order), otherwise, the byteBuffer order defaults to
                 * ByteOrder.BIG_ENDIAN.
                 */
                mBuffer.order(ByteOrder.nativeOrder());
            }

            @Override
            public ByteBuffer getBuffer() {
                SurfaceImage.this.throwISEIfImageIsInvalid();
                if (mBuffer != null) {
                throwISEIfImageIsInvalid();
                return mBuffer;
                } else {
                    mBuffer = SurfaceImage.this.nativeImageGetBuffer(mIndex,
                            ImageReader.this.mFormat);
                    // Set the byteBuffer order according to host endianness (native order),
                    // otherwise, the byteBuffer order defaults to ByteOrder.BIG_ENDIAN.
                    return mBuffer.order(ByteOrder.nativeOrder());
                }
            }

            @Override
@@ -837,7 +834,6 @@ public class ImageReader implements AutoCloseable {
                mBuffer = null;
            }

            final private int mIndex;
            final private int mPixelStride;
            final private int mRowStride;

@@ -860,10 +856,10 @@ public class ImageReader implements AutoCloseable {
        // If this image is detached from the ImageReader.
        private AtomicBoolean mIsDetached = new AtomicBoolean(false);

        private synchronized native ByteBuffer nativeImageGetBuffer(int idx, int readerFormat);
        private synchronized native SurfacePlane nativeCreatePlane(int idx, int readerFormat);
        private synchronized native int nativeGetWidth(int format);
        private synchronized native int nativeGetHeight(int format);
        private synchronized native SurfacePlane[] nativeCreatePlanes(int numPlanes,
                int readerFormat);
        private synchronized native int nativeGetWidth();
        private synchronized native int nativeGetHeight();
        private synchronized native int nativeGetFormat(int readerFormat);
    }

+3 −3
Original line number Diff line number Diff line
@@ -748,8 +748,8 @@ public class ImageWriter implements AutoCloseable {
            final private int mPixelStride;
            final private int mRowStride;

            // SurfacePlane instance is created by native code when a new
            // SurfaceImage is created
            // SurfacePlane instance is created by native code when SurfaceImage#getPlanes() is
            // called
            private SurfacePlane(int rowStride, int pixelStride, ByteBuffer buffer) {
                mRowStride = rowStride;
                mPixelStride = pixelStride;
@@ -795,7 +795,7 @@ public class ImageWriter implements AutoCloseable {

        }

        // this will create the SurfacePlane object and fill the information
        // Create the SurfacePlane object and fill the information
        private synchronized native SurfacePlane[] nativeCreatePlanes(int numPlanes, int writerFmt);

        private synchronized native int nativeGetWidth();
Loading