1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media;
18 
19 import android.annotation.IntRange;
20 import android.annotation.NonNull;
21 import android.graphics.ImageFormat;
22 import android.graphics.ImageFormat.Format;
23 import android.hardware.HardwareBuffer;
24 import android.hardware.HardwareBuffer.Usage;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.os.Message;
28 import android.view.Surface;
29 
30 import dalvik.system.VMRuntime;
31 
32 import java.lang.ref.WeakReference;
33 import java.nio.ByteBuffer;
34 import java.nio.ByteOrder;
35 import java.nio.NioUtils;
36 import java.util.List;
37 import java.util.concurrent.CopyOnWriteArrayList;
38 import java.util.concurrent.atomic.AtomicBoolean;
39 
40 /**
41  * <p>The ImageReader class allows direct application access to image data
42  * rendered into a {@link android.view.Surface}</p>
43  *
44  * <p>Several Android media API classes accept Surface objects as targets to
45  * render to, including {@link MediaPlayer}, {@link MediaCodec},
46  * {@link android.hardware.camera2.CameraDevice}, {@link ImageWriter} and
47  * {@link android.renderscript.Allocation RenderScript Allocations}. The image
48  * sizes and formats that can be used with each source vary, and should be
49  * checked in the documentation for the specific API.</p>
50  *
51  * <p>The image data is encapsulated in {@link Image} objects, and multiple such
52  * objects can be accessed at the same time, up to the number specified by the
53  * {@code maxImages} constructor parameter. New images sent to an ImageReader
54  * through its {@link Surface} are queued until accessed through the {@link #acquireLatestImage}
55  * or {@link #acquireNextImage} call. Due to memory limits, an image source will
56  * eventually stall or drop Images in trying to render to the Surface if the
57  * ImageReader does not obtain and release Images at a rate equal to the
58  * production rate.</p>
59  */
60 public class ImageReader implements AutoCloseable {
61 
62     /**
63      * Returned by nativeImageSetup when acquiring the image was successful.
64      */
65     private static final int ACQUIRE_SUCCESS = 0;
66     /**
67      * Returned by nativeImageSetup when we couldn't acquire the buffer,
68      * because there were no buffers available to acquire.
69      */
70     private static final int ACQUIRE_NO_BUFS = 1;
71     /**
72      * Returned by nativeImageSetup when we couldn't acquire the buffer
73      * because the consumer has already acquired {@maxImages} and cannot
74      * acquire more than that.
75      */
76     private static final int ACQUIRE_MAX_IMAGES = 2;
77 
78     /**
79      * <p>
80      * Create a new reader for images of the desired size and format.
81      * </p>
82      * <p>
83      * The {@code maxImages} parameter determines the maximum number of
84      * {@link Image} objects that can be be acquired from the
85      * {@code ImageReader} simultaneously. Requesting more buffers will use up
86      * more memory, so it is important to use only the minimum number necessary
87      * for the use case.
88      * </p>
89      * <p>
90      * The valid sizes and formats depend on the source of the image data.
91      * </p>
92      * <p>
93      * If the {@code format} is {@link ImageFormat#PRIVATE PRIVATE}, the created
94      * {@link ImageReader} will produce images that are not directly accessible
95      * by the application. The application can still acquire images from this
96      * {@link ImageReader}, and send them to the
97      * {@link android.hardware.camera2.CameraDevice camera} for reprocessing via
98      * {@link ImageWriter} interface. However, the {@link Image#getPlanes()
99      * getPlanes()} will return an empty array for {@link ImageFormat#PRIVATE
100      * PRIVATE} format images. The application can check if an existing reader's
101      * format by calling {@link #getImageFormat()}.
102      * </p>
103      * <p>
104      * {@link ImageFormat#PRIVATE PRIVATE} format {@link ImageReader
105      * ImageReaders} are more efficient to use when application access to image
106      * data is not necessary, compared to ImageReaders using other format such
107      * as {@link ImageFormat#YUV_420_888 YUV_420_888}.
108      * </p>
109      *
110      * @param width The default width in pixels of the Images that this reader
111      *            will produce.
112      * @param height The default height in pixels of the Images that this reader
113      *            will produce.
114      * @param format The format of the Image that this reader will produce. This
115      *            must be one of the {@link android.graphics.ImageFormat} or
116      *            {@link android.graphics.PixelFormat} constants. Note that not
117      *            all formats are supported, like ImageFormat.NV21.
118      * @param maxImages The maximum number of images the user will want to
119      *            access simultaneously. This should be as small as possible to
120      *            limit memory use. Once maxImages Images are obtained by the
121      *            user, one of them has to be released before a new Image will
122      *            become available for access through
123      *            {@link #acquireLatestImage()} or {@link #acquireNextImage()}.
124      *            Must be greater than 0.
125      * @see Image
126      */
newInstance( @ntRangefrom = 1) int width, @IntRange(from = 1) int height, @Format int format, @IntRange(from = 1) int maxImages)127     public static @NonNull ImageReader newInstance(
128             @IntRange(from = 1) int width,
129             @IntRange(from = 1) int height,
130             @Format             int format,
131             @IntRange(from = 1) int maxImages) {
132         // If the format is private don't default to USAGE_CPU_READ_OFTEN since it may not
133         // work, and is inscrutable anyway
134         return new ImageReader(width, height, format, maxImages,
135                 format == ImageFormat.PRIVATE ? 0 : HardwareBuffer.USAGE_CPU_READ_OFTEN);
136     }
137 
138     /**
139      * <p>
140      * Create a new reader for images of the desired size, format and consumer usage flag.
141      * </p>
142      * <p>
143      * The {@code maxImages} parameter determines the maximum number of {@link Image} objects that
144      * can be be acquired from the {@code ImageReader} simultaneously. Requesting more buffers will
145      * use up more memory, so it is important to use only the minimum number necessary for the use
146      * case.
147      * </p>
148      * <p>
149      * The valid sizes and formats depend on the source of the image data.
150      * </p>
151      * <p>
152      * The format and usage flag combination describes how the buffer will be used by
153      * consumer end-points. For example, if the application intends to send the images to
154      * {@link android.media.MediaCodec} or {@link android.media.MediaRecorder} for hardware video
155      * encoding, the format and usage flag combination needs to be
156      * {@link ImageFormat#PRIVATE PRIVATE} and {@link HardwareBuffer#USAGE_VIDEO_ENCODE}. When an
157      * {@link ImageReader} object is created with a valid size and such format/usage flag
158      * combination, the application can send the {@link Image images} to an {@link ImageWriter} that
159      * is created with the input {@link android.view.Surface} provided by the
160      * {@link android.media.MediaCodec} or {@link android.media.MediaRecorder}.
161      * </p>
162      * <p>
163      * If the {@code format} is {@link ImageFormat#PRIVATE PRIVATE}, the created {@link ImageReader}
164      * will produce images that are not directly accessible by the application. The application can
165      * still acquire images from this {@link ImageReader}, and send them to the
166      * {@link android.hardware.camera2.CameraDevice camera} for reprocessing, or to the
167      * {@link android.media.MediaCodec} / {@link android.media.MediaRecorder} for hardware video
168      * encoding via {@link ImageWriter} interface. However, the {@link Image#getPlanes()
169      * getPlanes()} will return an empty array for {@link ImageFormat#PRIVATE PRIVATE} format
170      * images. The application can check if an existing reader's format by calling
171      * {@link #getImageFormat()}.
172      * </p>
173      * <p>
174      * {@link ImageFormat#PRIVATE PRIVATE} format {@link ImageReader ImageReaders} are more
175      * efficient to use when application access to image data is not necessary, compared to
176      * ImageReaders using other format such as {@link ImageFormat#YUV_420_888 YUV_420_888}.
177      * </p>
178      * <p>
179      * Note that not all format and usage flag combinations are supported by the
180      * {@link ImageReader}. Below are the supported combinations by the {@link ImageReader}
181      * (assuming the consumer end-points support the such image consumption, e.g., hardware video
182      * encoding).
183      * <table>
184      * <tr>
185      *   <th>Format</th>
186      *   <th>Compatible usage flags</th>
187      * </tr>
188      * <tr>
189      *   <td>non-{@link android.graphics.ImageFormat#PRIVATE PRIVATE} formats defined by
190      *   {@link android.graphics.ImageFormat ImageFormat} or
191      *   {@link android.graphics.PixelFormat PixelFormat}</td>
192      *   <td>{@link HardwareBuffer#USAGE_CPU_READ_RARELY} or
193      *   {@link HardwareBuffer#USAGE_CPU_READ_OFTEN}</td>
194      * </tr>
195      * <tr>
196      *   <td>{@link android.graphics.ImageFormat#PRIVATE}</td>
197      *   <td>{@link HardwareBuffer#USAGE_VIDEO_ENCODE} or
198      *   {@link HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE}, or combined</td>
199      * </tr>
200      * </table>
201      * Using other combinations may result in {@link IllegalArgumentException}.
202      * </p>
203      * <p>
204      * If the {@link ImageReader} is used as an output target for a {@link
205      * android.hardware.camera2.CameraDevice}, and if the usage flag contains
206      * {@link HardwareBuffer#USAGE_VIDEO_ENCODE}, the timestamps of the
207      * {@link Image images} produced by the {@link ImageReader} won't be in the same timebase as
208      * {@link android.os.SystemClock#elapsedRealtimeNanos}, even if
209      * {@link android.hardware.camera2.CameraCharacteristics#SENSOR_INFO_TIMESTAMP_SOURCE} is
210      * {@link android.hardware.camera2.CameraCharacteristics#SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME}.
211      * Instead, the timestamps will be roughly in the same timebase as in
212      * {@link android.os.SystemClock#uptimeMillis}, so that A/V synchronization could work for
213      * video recording. In this case, the timestamps from the {@link ImageReader} with
214      * {@link HardwareBuffer#USAGE_VIDEO_ENCODE} usage flag may not be directly comparable with
215      * timestamps of other streams or capture result metadata.
216      * </p>
217      * @param width The default width in pixels of the Images that this reader will produce.
218      * @param height The default height in pixels of the Images that this reader will produce.
219      * @param format The format of the Image that this reader will produce. This must be one of the
220      *            {@link android.graphics.ImageFormat} or {@link android.graphics.PixelFormat}
221      *            constants. Note that not all formats are supported, like ImageFormat.NV21.
222      * @param maxImages The maximum number of images the user will want to access simultaneously.
223      *            This should be as small as possible to limit memory use. Once maxImages Images are
224      *            obtained by the user, one of them has to be released before a new Image will
225      *            become available for access through {@link #acquireLatestImage()} or
226      *            {@link #acquireNextImage()}. Must be greater than 0.
227      * @param usage The intended usage of the images produced by this ImageReader. See the usages
228      *              on {@link HardwareBuffer} for a list of valid usage bits. See also
229      *              {@link HardwareBuffer#isSupported(int, int, int, int, long)} for checking
230      *              if a combination is supported. If it's not supported this will throw
231      *              an {@link IllegalArgumentException}.
232      * @see Image
233      * @see HardwareBuffer
234      */
newInstance( @ntRangefrom = 1) int width, @IntRange(from = 1) int height, @Format int format, @IntRange(from = 1) int maxImages, @Usage long usage)235     public static @NonNull ImageReader newInstance(
236             @IntRange(from = 1) int width,
237             @IntRange(from = 1) int height,
238             @Format             int format,
239             @IntRange(from = 1) int maxImages,
240             @Usage              long usage) {
241         // TODO: Check this - can't do it just yet because format support is different
242         // Unify formats! The only reliable way to validate usage is to just try it and see.
243 
244 //        if (!HardwareBuffer.isSupported(width, height, format, 1, usage)) {
245 //            throw new IllegalArgumentException("The given format=" + Integer.toHexString(format)
246 //                + " & usage=" + Long.toHexString(usage) + " is not supported");
247 //        }
248         return new ImageReader(width, height, format, maxImages, usage);
249     }
250 
251     /**
252      * @hide
253      */
ImageReader(int width, int height, int format, int maxImages, long usage)254     protected ImageReader(int width, int height, int format, int maxImages, long usage) {
255         mWidth = width;
256         mHeight = height;
257         mFormat = format;
258         mMaxImages = maxImages;
259 
260         if (width < 1 || height < 1) {
261             throw new IllegalArgumentException(
262                 "The image dimensions must be positive");
263         }
264         if (mMaxImages < 1) {
265             throw new IllegalArgumentException(
266                 "Maximum outstanding image count must be at least 1");
267         }
268 
269         if (format == ImageFormat.NV21) {
270             throw new IllegalArgumentException(
271                     "NV21 format is not supported");
272         }
273 
274         mNumPlanes = ImageUtils.getNumPlanesForFormat(mFormat);
275 
276         nativeInit(new WeakReference<>(this), width, height, format, maxImages, usage);
277 
278         mSurface = nativeGetSurface();
279 
280         mIsReaderValid = true;
281         // Estimate the native buffer allocation size and register it so it gets accounted for
282         // during GC. Note that this doesn't include the buffers required by the buffer queue
283         // itself and the buffers requested by the producer.
284         // Only include memory for 1 buffer, since actually accounting for the memory used is
285         // complex, and 1 buffer is enough for the VM to treat the ImageReader as being of some
286         // size.
287         mEstimatedNativeAllocBytes = ImageUtils.getEstimatedNativeAllocBytes(
288                 width, height, format, /*buffer count*/ 1);
289         VMRuntime.getRuntime().registerNativeAllocation(mEstimatedNativeAllocBytes);
290     }
291 
292     /**
293      * The default width of {@link Image Images}, in pixels.
294      *
295      * <p>The width may be overridden by the producer sending buffers to this
296      * ImageReader's Surface. If so, the actual width of the images can be
297      * found using {@link Image#getWidth}.</p>
298      *
299      * @return the expected width of an Image
300      */
getWidth()301     public int getWidth() {
302         return mWidth;
303     }
304 
305     /**
306      * The default height of {@link Image Images}, in pixels.
307      *
308      * <p>The height may be overridden by the producer sending buffers to this
309      * ImageReader's Surface. If so, the actual height of the images can be
310      * found using {@link Image#getHeight}.</p>
311      *
312      * @return the expected height of an Image
313      */
getHeight()314     public int getHeight() {
315         return mHeight;
316     }
317 
318     /**
319      * The default {@link ImageFormat image format} of {@link Image Images}.
320      *
321      * <p>Some color formats may be overridden by the producer sending buffers to
322      * this ImageReader's Surface if the default color format allows. ImageReader
323      * guarantees that all {@link Image Images} acquired from ImageReader
324      * (for example, with {@link #acquireNextImage}) will have a "compatible"
325      * format to what was specified in {@link #newInstance}.
326      * As of now, each format is only compatible to itself.
327      * The actual format of the images can be found using {@link Image#getFormat}.</p>
328      *
329      * @return the expected format of an Image
330      *
331      * @see ImageFormat
332      */
getImageFormat()333     public int getImageFormat() {
334         return mFormat;
335     }
336 
337     /**
338      * Maximum number of images that can be acquired from the ImageReader by any time (for example,
339      * with {@link #acquireNextImage}).
340      *
341      * <p>An image is considered acquired after it's returned by a function from ImageReader, and
342      * until the Image is {@link Image#close closed} to release the image back to the ImageReader.
343      * </p>
344      *
345      * <p>Attempting to acquire more than {@code maxImages} concurrently will result in the
346      * acquire function throwing a {@link IllegalStateException}. Furthermore,
347      * while the max number of images have been acquired by the ImageReader user, the producer
348      * enqueueing additional images may stall until at least one image has been released. </p>
349      *
350      * @return Maximum number of images for this ImageReader.
351      *
352      * @see Image#close
353      */
getMaxImages()354     public int getMaxImages() {
355         return mMaxImages;
356     }
357 
358     /**
359      * <p>Get a {@link Surface} that can be used to produce {@link Image Images} for this
360      * {@code ImageReader}.</p>
361      *
362      * <p>Until valid image data is rendered into this {@link Surface}, the
363      * {@link #acquireNextImage} method will return {@code null}. Only one source
364      * can be producing data into this Surface at the same time, although the
365      * same {@link Surface} can be reused with a different API once the first source is
366      * disconnected from the {@link Surface}.</p>
367      *
368      * <p>Please note that holding on to the Surface object returned by this method is not enough
369      * to keep its parent ImageReader from being reclaimed. In that sense, a Surface acts like a
370      * {@link java.lang.ref.WeakReference weak reference} to the ImageReader that provides it.</p>
371      *
372      * @return A {@link Surface} to use for a drawing target for various APIs.
373      */
getSurface()374     public Surface getSurface() {
375         return mSurface;
376     }
377 
378     /**
379      * <p>
380      * Acquire the latest {@link Image} from the ImageReader's queue, dropping older
381      * {@link Image images}. Returns {@code null} if no new image is available.
382      * </p>
383      * <p>
384      * This operation will acquire all the images possible from the ImageReader,
385      * but {@link #close} all images that aren't the latest. This function is
386      * recommended to use over {@link #acquireNextImage} for most use-cases, as it's
387      * more suited for real-time processing.
388      * </p>
389      * <p>
390      * Note that {@link #getMaxImages maxImages} should be at least 2 for
391      * {@link #acquireLatestImage} to be any different than {@link #acquireNextImage} -
392      * discarding all-but-the-newest {@link Image} requires temporarily acquiring two
393      * {@link Image Images} at once. Or more generally, calling {@link #acquireLatestImage}
394      * with less than two images of margin, that is
395      * {@code (maxImages - currentAcquiredImages < 2)} will not discard as expected.
396      * </p>
397      * <p>
398      * This operation will fail by throwing an {@link IllegalStateException} if
399      * {@code maxImages} have been acquired with {@link #acquireLatestImage} or
400      * {@link #acquireNextImage}. In particular a sequence of {@link #acquireLatestImage}
401      * calls greater than {@link #getMaxImages} without calling {@link Image#close} in-between
402      * will exhaust the underlying queue. At such a time, {@link IllegalStateException}
403      * will be thrown until more images are
404      * released with {@link Image#close}.
405      * </p>
406      *
407      * @return latest frame of image data, or {@code null} if no image data is available.
408      * @throws IllegalStateException if too many images are currently acquired
409      */
acquireLatestImage()410     public Image acquireLatestImage() {
411         Image image = acquireNextImage();
412         if (image == null) {
413             return null;
414         }
415         try {
416             for (;;) {
417                 Image next = acquireNextImageNoThrowISE();
418                 if (next == null) {
419                     Image result = image;
420                     image = null;
421                     return result;
422                 }
423                 image.close();
424                 image = next;
425             }
426         } finally {
427             if (image != null) {
428                 image.close();
429             }
430         }
431     }
432 
433     /**
434      * Don't throw IllegalStateException if there are too many images acquired.
435      *
436      * @return Image if acquiring succeeded, or null otherwise.
437      *
438      * @hide
439      */
acquireNextImageNoThrowISE()440     public Image acquireNextImageNoThrowISE() {
441         SurfaceImage si = new SurfaceImage(mFormat);
442         return acquireNextSurfaceImage(si) == ACQUIRE_SUCCESS ? si : null;
443     }
444 
445     /**
446      * Attempts to acquire the next image from the underlying native implementation.
447      *
448      * <p>
449      * Note that unexpected failures will throw at the JNI level.
450      * </p>
451      *
452      * @param si A blank SurfaceImage.
453      * @return One of the {@code ACQUIRE_*} codes that determine success or failure.
454      *
455      * @see #ACQUIRE_MAX_IMAGES
456      * @see #ACQUIRE_NO_BUFS
457      * @see #ACQUIRE_SUCCESS
458      */
acquireNextSurfaceImage(SurfaceImage si)459     private int acquireNextSurfaceImage(SurfaceImage si) {
460         synchronized (mCloseLock) {
461             // A null image will eventually be returned if ImageReader is already closed.
462             int status = ACQUIRE_NO_BUFS;
463             if (mIsReaderValid) {
464                 status = nativeImageSetup(si);
465             }
466 
467             switch (status) {
468                 case ACQUIRE_SUCCESS:
469                     si.mIsImageValid = true;
470                 case ACQUIRE_NO_BUFS:
471                 case ACQUIRE_MAX_IMAGES:
472                     break;
473                 default:
474                     throw new AssertionError("Unknown nativeImageSetup return code " + status);
475             }
476 
477             // Only keep track the successfully acquired image, as the native buffer is only mapped
478             // for such case.
479             if (status == ACQUIRE_SUCCESS) {
480                 mAcquiredImages.add(si);
481             }
482             return status;
483         }
484     }
485 
486     /**
487      * <p>
488      * Acquire the next Image from the ImageReader's queue. Returns {@code null} if
489      * no new image is available.
490      * </p>
491      *
492      * <p><i>Warning:</i> Consider using {@link #acquireLatestImage()} instead, as it will
493      * automatically release older images, and allow slower-running processing routines to catch
494      * up to the newest frame. Usage of {@link #acquireNextImage} is recommended for
495      * batch/background processing. Incorrectly using this function can cause images to appear
496      * with an ever-increasing delay, followed by a complete stall where no new images seem to
497      * appear.
498      * </p>
499      *
500      * <p>
501      * This operation will fail by throwing an {@link IllegalStateException} if
502      * {@code maxImages} have been acquired with {@link #acquireNextImage} or
503      * {@link #acquireLatestImage}. In particular a sequence of {@link #acquireNextImage} or
504      * {@link #acquireLatestImage} calls greater than {@link #getMaxImages maxImages} without
505      * calling {@link Image#close} in-between will exhaust the underlying queue. At such a time,
506      * {@link IllegalStateException} will be thrown until more images are released with
507      * {@link Image#close}.
508      * </p>
509      *
510      * @return a new frame of image data, or {@code null} if no image data is available.
511      * @throws IllegalStateException if {@code maxImages} images are currently acquired
512      * @see #acquireLatestImage
513      */
acquireNextImage()514     public Image acquireNextImage() {
515         // Initialize with reader format, but can be overwritten by native if the image
516         // format is different from the reader format.
517         SurfaceImage si = new SurfaceImage(mFormat);
518         int status = acquireNextSurfaceImage(si);
519 
520         switch (status) {
521             case ACQUIRE_SUCCESS:
522                 return si;
523             case ACQUIRE_NO_BUFS:
524                 return null;
525             case ACQUIRE_MAX_IMAGES:
526                 throw new IllegalStateException(
527                         String.format(
528                                 "maxImages (%d) has already been acquired, " +
529                                 "call #close before acquiring more.", mMaxImages));
530             default:
531                 throw new AssertionError("Unknown nativeImageSetup return code " + status);
532         }
533     }
534 
535     /**
536      * <p>Return the frame to the ImageReader for reuse.</p>
537      */
releaseImage(Image i)538     private void releaseImage(Image i) {
539         if (! (i instanceof SurfaceImage) ) {
540             throw new IllegalArgumentException(
541                 "This image was not produced by an ImageReader");
542         }
543         SurfaceImage si = (SurfaceImage) i;
544         if (si.mIsImageValid == false) {
545             return;
546         }
547 
548         if (si.getReader() != this || !mAcquiredImages.contains(i)) {
549             throw new IllegalArgumentException(
550                 "This image was not produced by this ImageReader");
551         }
552 
553         si.clearSurfacePlanes();
554         nativeReleaseImage(i);
555         si.mIsImageValid = false;
556         mAcquiredImages.remove(i);
557     }
558 
559     /**
560      * Register a listener to be invoked when a new image becomes available
561      * from the ImageReader.
562      *
563      * @param listener
564      *            The listener that will be run.
565      * @param handler
566      *            The handler on which the listener should be invoked, or null
567      *            if the listener should be invoked on the calling thread's looper.
568      * @throws IllegalArgumentException
569      *            If no handler specified and the calling thread has no looper.
570      */
setOnImageAvailableListener(OnImageAvailableListener listener, Handler handler)571     public void setOnImageAvailableListener(OnImageAvailableListener listener, Handler handler) {
572         synchronized (mListenerLock) {
573             if (listener != null) {
574                 Looper looper = handler != null ? handler.getLooper() : Looper.myLooper();
575                 if (looper == null) {
576                     throw new IllegalArgumentException(
577                             "handler is null but the current thread is not a looper");
578                 }
579                 if (mListenerHandler == null || mListenerHandler.getLooper() != looper) {
580                     mListenerHandler = new ListenerHandler(looper);
581                 }
582                 mListener = listener;
583             } else {
584                 mListener = null;
585                 mListenerHandler = null;
586             }
587         }
588     }
589 
590     /**
591      * Callback interface for being notified that a new image is available.
592      *
593      * <p>
594      * The onImageAvailable is called per image basis, that is, callback fires for every new frame
595      * available from ImageReader.
596      * </p>
597      */
598     public interface OnImageAvailableListener {
599         /**
600          * Callback that is called when a new image is available from ImageReader.
601          *
602          * @param reader the ImageReader the callback is associated with.
603          * @see ImageReader
604          * @see Image
605          */
onImageAvailable(ImageReader reader)606         void onImageAvailable(ImageReader reader);
607     }
608 
609     /**
610      * Free up all the resources associated with this ImageReader.
611      *
612      * <p>
613      * After calling this method, this ImageReader can not be used. Calling
614      * any methods on this ImageReader and Images previously provided by
615      * {@link #acquireNextImage} or {@link #acquireLatestImage}
616      * will result in an {@link IllegalStateException}, and attempting to read from
617      * {@link ByteBuffer ByteBuffers} returned by an earlier
618      * {@link Image.Plane#getBuffer Plane#getBuffer} call will
619      * have undefined behavior.
620      * </p>
621      */
622     @Override
close()623     public void close() {
624         setOnImageAvailableListener(null, null);
625         if (mSurface != null) mSurface.release();
626 
627         /**
628          * Close all outstanding acquired images before closing the ImageReader. It is a good
629          * practice to close all the images as soon as it is not used to reduce system instantaneous
630          * memory pressure. CopyOnWrite list will use a copy of current list content. For the images
631          * being closed by other thread (e.g., GC thread), doubling the close call is harmless. For
632          * the image being acquired by other threads, mCloseLock is used to synchronize close and
633          * acquire operations.
634          */
635         synchronized (mCloseLock) {
636             mIsReaderValid = false;
637             for (Image image : mAcquiredImages) {
638                 image.close();
639             }
640             mAcquiredImages.clear();
641 
642             nativeClose();
643 
644             if (mEstimatedNativeAllocBytes > 0) {
645                 VMRuntime.getRuntime().registerNativeFree(mEstimatedNativeAllocBytes);
646                 mEstimatedNativeAllocBytes = 0;
647             }
648         }
649     }
650 
651     /**
652      * Discard any free buffers owned by this ImageReader.
653      *
654      * <p>
655      * Generally, the ImageReader caches buffers for reuse once they have been
656      * allocated, for best performance. However, sometimes it may be important to
657      * release all the cached, unused buffers to save on memory.
658      * </p>
659      * <p>
660      * Calling this method will discard all free cached buffers. This does not include any buffers
661      * associated with Images acquired from the ImageReader, any filled buffers waiting to be
662      * acquired, and any buffers currently in use by the source rendering buffers into the
663      * ImageReader's Surface.
664      * <p>
665      * The ImageReader continues to be usable after this call, but may need to reallocate buffers
666      * when more buffers are needed for rendering.
667      * </p>
668      */
discardFreeBuffers()669     public void discardFreeBuffers() {
670         synchronized (mCloseLock) {
671             nativeDiscardFreeBuffers();
672         }
673     }
674 
675     @Override
finalize()676     protected void finalize() throws Throwable {
677         try {
678             close();
679         } finally {
680             super.finalize();
681         }
682     }
683 
684     /**
685      * <p>
686      * Remove the ownership of this image from the ImageReader.
687      * </p>
688      * <p>
689      * After this call, the ImageReader no longer owns this image, and the image
690      * ownership can be transfered to another entity like {@link ImageWriter}
691      * via {@link ImageWriter#queueInputImage}. It's up to the new owner to
692      * release the resources held by this image. For example, if the ownership
693      * of this image is transfered to an {@link ImageWriter}, the image will be
694      * freed by the ImageWriter after the image data consumption is done.
695      * </p>
696      * <p>
697      * This method can be used to achieve zero buffer copy for use cases like
698      * {@link android.hardware.camera2.CameraDevice Camera2 API} PRIVATE and YUV
699      * reprocessing, where the application can select an output image from
700      * {@link ImageReader} and transfer this image directly to
701      * {@link ImageWriter}, where this image can be consumed by camera directly.
702      * For PRIVATE reprocessing, this is the only way to send input buffers to
703      * the {@link android.hardware.camera2.CameraDevice camera} for
704      * reprocessing.
705      * </p>
706      * <p>
707      * This is a package private method that is only used internally.
708      * </p>
709      *
710      * @param image The image to be detached from this ImageReader.
711      * @throws IllegalStateException If the ImageReader or image have been
712      *             closed, or the has been detached, or has not yet been
713      *             acquired.
714      */
detachImage(Image image)715      void detachImage(Image image) {
716        if (image == null) {
717            throw new IllegalArgumentException("input image must not be null");
718        }
719        if (!isImageOwnedbyMe(image)) {
720            throw new IllegalArgumentException("Trying to detach an image that is not owned by"
721                    + " this ImageReader");
722        }
723 
724         SurfaceImage si = (SurfaceImage) image;
725         si.throwISEIfImageIsInvalid();
726 
727         if (si.isAttachable()) {
728             throw new IllegalStateException("Image was already detached from this ImageReader");
729         }
730 
731         nativeDetachImage(image);
732         si.clearSurfacePlanes();
733         si.mPlanes = null;
734         si.setDetached(true);
735     }
736 
isImageOwnedbyMe(Image image)737     private boolean isImageOwnedbyMe(Image image) {
738         if (!(image instanceof SurfaceImage)) {
739             return false;
740         }
741         SurfaceImage si = (SurfaceImage) image;
742         return si.getReader() == this;
743     }
744 
745     /**
746      * Called from Native code when an Event happens.
747      *
748      * This may be called from an arbitrary Binder thread, so access to the ImageReader must be
749      * synchronized appropriately.
750      */
postEventFromNative(Object selfRef)751     private static void postEventFromNative(Object selfRef) {
752         @SuppressWarnings("unchecked")
753         WeakReference<ImageReader> weakSelf = (WeakReference<ImageReader>)selfRef;
754         final ImageReader ir = weakSelf.get();
755         if (ir == null) {
756             return;
757         }
758 
759         final Handler handler;
760         synchronized (ir.mListenerLock) {
761             handler = ir.mListenerHandler;
762         }
763         if (handler != null) {
764             handler.sendEmptyMessage(0);
765         }
766     }
767 
768     private final int mWidth;
769     private final int mHeight;
770     private final int mFormat;
771     private final int mMaxImages;
772     private final int mNumPlanes;
773     private final Surface mSurface;
774     private int mEstimatedNativeAllocBytes;
775 
776     private final Object mListenerLock = new Object();
777     private final Object mCloseLock = new Object();
778     private boolean mIsReaderValid = false;
779     private OnImageAvailableListener mListener;
780     private ListenerHandler mListenerHandler;
781     // Keep track of the successfully acquired Images. This need to be thread safe as the images
782     // could be closed by different threads (e.g., application thread and GC thread).
783     private List<Image> mAcquiredImages = new CopyOnWriteArrayList<>();
784 
785     /**
786      * This field is used by native code, do not access or modify.
787      */
788     private long mNativeContext;
789 
790     /**
791      * This custom handler runs asynchronously so callbacks don't get queued behind UI messages.
792      */
793     private final class ListenerHandler extends Handler {
ListenerHandler(Looper looper)794         public ListenerHandler(Looper looper) {
795             super(looper, null, true /*async*/);
796         }
797 
798         @Override
handleMessage(Message msg)799         public void handleMessage(Message msg) {
800             OnImageAvailableListener listener;
801             synchronized (mListenerLock) {
802                 listener = mListener;
803             }
804 
805             // It's dangerous to fire onImageAvailable() callback when the ImageReader is being
806             // closed, as application could acquire next image in the onImageAvailable() callback.
807             boolean isReaderValid = false;
808             synchronized (mCloseLock) {
809                 isReaderValid = mIsReaderValid;
810             }
811             if (listener != null && isReaderValid) {
812                 listener.onImageAvailable(ImageReader.this);
813             }
814         }
815     }
816 
817     private class SurfaceImage extends android.media.Image {
SurfaceImage(int format)818         public SurfaceImage(int format) {
819             mFormat = format;
820         }
821 
822         @Override
close()823         public void close() {
824             ImageReader.this.releaseImage(this);
825         }
826 
getReader()827         public ImageReader getReader() {
828             return ImageReader.this;
829         }
830 
831         @Override
getFormat()832         public int getFormat() {
833             throwISEIfImageIsInvalid();
834             int readerFormat = ImageReader.this.getImageFormat();
835             // Assume opaque reader always produce opaque images.
836             mFormat = (readerFormat == ImageFormat.PRIVATE) ? readerFormat :
837                 nativeGetFormat(readerFormat);
838             return mFormat;
839         }
840 
841         @Override
getWidth()842         public int getWidth() {
843             throwISEIfImageIsInvalid();
844             int width;
845             switch(getFormat()) {
846                 case ImageFormat.JPEG:
847                 case ImageFormat.DEPTH_POINT_CLOUD:
848                 case ImageFormat.RAW_PRIVATE:
849                 case ImageFormat.DEPTH_JPEG:
850                 case ImageFormat.HEIC:
851                     width = ImageReader.this.getWidth();
852                     break;
853                 default:
854                     width = nativeGetWidth();
855             }
856             return width;
857         }
858 
859         @Override
getHeight()860         public int getHeight() {
861             throwISEIfImageIsInvalid();
862             int height;
863             switch(getFormat()) {
864                 case ImageFormat.JPEG:
865                 case ImageFormat.DEPTH_POINT_CLOUD:
866                 case ImageFormat.RAW_PRIVATE:
867                 case ImageFormat.DEPTH_JPEG:
868                 case ImageFormat.HEIC:
869                     height = ImageReader.this.getHeight();
870                     break;
871                 default:
872                     height = nativeGetHeight();
873             }
874             return height;
875         }
876 
877         @Override
getTimestamp()878         public long getTimestamp() {
879             throwISEIfImageIsInvalid();
880             return mTimestamp;
881         }
882 
883         @Override
getTransform()884         public int getTransform() {
885             throwISEIfImageIsInvalid();
886             return mTransform;
887         }
888 
889         @Override
getScalingMode()890         public int getScalingMode() {
891             throwISEIfImageIsInvalid();
892             return mScalingMode;
893         }
894 
895         @Override
getHardwareBuffer()896         public HardwareBuffer getHardwareBuffer() {
897             throwISEIfImageIsInvalid();
898             return nativeGetHardwareBuffer();
899         }
900 
901         @Override
setTimestamp(long timestampNs)902         public void setTimestamp(long timestampNs) {
903             throwISEIfImageIsInvalid();
904             mTimestamp = timestampNs;
905         }
906 
907         @Override
getPlanes()908         public Plane[] getPlanes() {
909             throwISEIfImageIsInvalid();
910 
911             if (mPlanes == null) {
912                 mPlanes = nativeCreatePlanes(ImageReader.this.mNumPlanes, ImageReader.this.mFormat);
913             }
914             // Shallow copy is fine.
915             return mPlanes.clone();
916         }
917 
918         @Override
finalize()919         protected final void finalize() throws Throwable {
920             try {
921                 close();
922             } finally {
923                 super.finalize();
924             }
925         }
926 
927         @Override
isAttachable()928         boolean isAttachable() {
929             throwISEIfImageIsInvalid();
930             return mIsDetached.get();
931         }
932 
933         @Override
getOwner()934         ImageReader getOwner() {
935             throwISEIfImageIsInvalid();
936             return ImageReader.this;
937         }
938 
939         @Override
getNativeContext()940         long getNativeContext() {
941             throwISEIfImageIsInvalid();
942             return mNativeBuffer;
943         }
944 
setDetached(boolean detached)945         private void setDetached(boolean detached) {
946             throwISEIfImageIsInvalid();
947             mIsDetached.getAndSet(detached);
948         }
949 
clearSurfacePlanes()950         private void clearSurfacePlanes() {
951             // Image#getPlanes may not be called before the image is closed.
952             if (mIsImageValid && mPlanes != null) {
953                 for (int i = 0; i < mPlanes.length; i++) {
954                     if (mPlanes[i] != null) {
955                         mPlanes[i].clearBuffer();
956                         mPlanes[i] = null;
957                     }
958                 }
959             }
960         }
961 
962         private class SurfacePlane extends android.media.Image.Plane {
963             // SurfacePlane instance is created by native code when SurfaceImage#getPlanes() is
964             // called
SurfacePlane(int rowStride, int pixelStride, ByteBuffer buffer)965             private SurfacePlane(int rowStride, int pixelStride, ByteBuffer buffer) {
966                 mRowStride = rowStride;
967                 mPixelStride = pixelStride;
968                 mBuffer = buffer;
969                 /**
970                  * Set the byteBuffer order according to host endianness (native
971                  * order), otherwise, the byteBuffer order defaults to
972                  * ByteOrder.BIG_ENDIAN.
973                  */
974                 mBuffer.order(ByteOrder.nativeOrder());
975             }
976 
977             @Override
getBuffer()978             public ByteBuffer getBuffer() {
979                 throwISEIfImageIsInvalid();
980                 return mBuffer;
981             }
982 
983             @Override
getPixelStride()984             public int getPixelStride() {
985                 SurfaceImage.this.throwISEIfImageIsInvalid();
986                 if (ImageReader.this.mFormat == ImageFormat.RAW_PRIVATE) {
987                     throw new UnsupportedOperationException(
988                             "getPixelStride is not supported for RAW_PRIVATE plane");
989                 }
990                 return mPixelStride;
991             }
992 
993             @Override
getRowStride()994             public int getRowStride() {
995                 SurfaceImage.this.throwISEIfImageIsInvalid();
996                 if (ImageReader.this.mFormat == ImageFormat.RAW_PRIVATE) {
997                     throw new UnsupportedOperationException(
998                             "getRowStride is not supported for RAW_PRIVATE plane");
999                 }
1000                 return mRowStride;
1001             }
1002 
clearBuffer()1003             private void clearBuffer() {
1004                 // Need null check first, as the getBuffer() may not be called before an image
1005                 // is closed.
1006                 if (mBuffer == null) {
1007                     return;
1008                 }
1009 
1010                 if (mBuffer.isDirect()) {
1011                     NioUtils.freeDirectBuffer(mBuffer);
1012                 }
1013                 mBuffer = null;
1014             }
1015 
1016             final private int mPixelStride;
1017             final private int mRowStride;
1018 
1019             private ByteBuffer mBuffer;
1020         }
1021 
1022         /**
1023          * This field is used to keep track of native object and used by native code only.
1024          * Don't modify.
1025          */
1026         private long mNativeBuffer;
1027 
1028         /**
1029          * These fields are set by native code during nativeImageSetup().
1030          */
1031         private long mTimestamp;
1032         private int mTransform;
1033         private int mScalingMode;
1034 
1035         private SurfacePlane[] mPlanes;
1036         private int mFormat = ImageFormat.UNKNOWN;
1037         // If this image is detached from the ImageReader.
1038         private AtomicBoolean mIsDetached = new AtomicBoolean(false);
1039 
nativeCreatePlanes(int numPlanes, int readerFormat)1040         private synchronized native SurfacePlane[] nativeCreatePlanes(int numPlanes,
1041                 int readerFormat);
nativeGetWidth()1042         private synchronized native int nativeGetWidth();
nativeGetHeight()1043         private synchronized native int nativeGetHeight();
nativeGetFormat(int readerFormat)1044         private synchronized native int nativeGetFormat(int readerFormat);
nativeGetHardwareBuffer()1045         private synchronized native HardwareBuffer nativeGetHardwareBuffer();
1046     }
1047 
nativeInit(Object weakSelf, int w, int h, int fmt, int maxImgs, long consumerUsage)1048     private synchronized native void nativeInit(Object weakSelf, int w, int h,
1049                                                     int fmt, int maxImgs, long consumerUsage);
nativeClose()1050     private synchronized native void nativeClose();
nativeReleaseImage(Image i)1051     private synchronized native void nativeReleaseImage(Image i);
nativeGetSurface()1052     private synchronized native Surface nativeGetSurface();
nativeDetachImage(Image i)1053     private synchronized native int nativeDetachImage(Image i);
nativeDiscardFreeBuffers()1054     private synchronized native void nativeDiscardFreeBuffers();
1055 
1056     /**
1057      * @return A return code {@code ACQUIRE_*}
1058      *
1059      * @see #ACQUIRE_SUCCESS
1060      * @see #ACQUIRE_NO_BUFS
1061      * @see #ACQUIRE_MAX_IMAGES
1062      */
nativeImageSetup(Image i)1063     private synchronized native int nativeImageSetup(Image i);
1064 
1065     /**
1066      * We use a class initializer to allow the native code to cache some
1067      * field offsets.
1068      */
nativeClassInit()1069     private static native void nativeClassInit();
1070     static {
1071         System.loadLibrary("media_jni");
nativeClassInit()1072         nativeClassInit();
1073     }
1074 }
1075