Ensure that two threads cannot simultaneously close SurfaceImage

SurfaceImage's `close` method is not thread safe but can be called by
multiple threads. If two threads call `SurfaceImage#close`
simulatneously, it can cause ImageReader to throw
IllegalArgumentException.

This CL synchronizes the `close` method to prevent spurious exceptions
from being thrown when multiple threads attempt to close a SurfaceImage.

Bug: 223447712
Test: All existing CTS tests pass on Oriole
Change-Id: I49426b771736e378862e1550124e65c26f5d7c1b
This commit is contained in:
Avichal Rakesh
2022-05-03 16:17:13 -07:00
parent 476efd5f8b
commit 0f19cc3e90

View File

@@ -643,6 +643,9 @@ public class ImageReader implements AutoCloseable {
/**
* <p>Return the frame to the ImageReader for reuse.</p>
*
* This method should only be called via {@link SurfaceImage#close} which ensures that image
* closing is atomic.
*/
private void releaseImage(Image i) {
if (! (i instanceof SurfaceImage) ) {
@@ -1125,6 +1128,8 @@ public class ImageReader implements AutoCloseable {
}
private class SurfaceImage extends android.media.Image {
private final Object mCloseLock = new Object();
public SurfaceImage(int format) {
mFormat = format;
mHardwareBufferFormat = ImageReader.this.mHardwareBufferFormat;
@@ -1139,7 +1144,9 @@ public class ImageReader implements AutoCloseable {
@Override
public void close() {
ImageReader.this.releaseImage(this);
synchronized (this.mCloseLock) {
ImageReader.this.releaseImage(this);
}
}
public ImageReader getReader() {