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