summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Shuzhen Wang <shuzhenwang@google.com> 2024-09-25 23:04:33 -0700
committer Shuzhen Wang <shuzhenwang@google.com> 2024-09-26 08:34:57 -0700
commit7f974147592c4d3242a5ae6fca70b2ed079912ea (patch)
tree9d4d43487183fa4fe34698f902176f9e744d38b0
parent6c8fbe78efc9fec71ab21ebdd42f3141e632b915 (diff)
ImageWriter: Check Surface is valid before use
For some reason, getProducer() returns a nullptr even though the JNI context is valid. The only explanation is that ~JNIImageWriterContext is called right before ImageWriter_attachAndQueueImage(). When ImageWriter_attachAndQueueImage() is called, the context is still valid even though mProducer is already cleared. Flag: EXEMPT bug-fix Test: ImageWriterTest Bug: 362116761 Change-Id: Id24e7fc7ac63e703146337690f42df99e3daac5b
-rw-r--r--media/jni/android_media_ImageWriter.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/media/jni/android_media_ImageWriter.cpp b/media/jni/android_media_ImageWriter.cpp
index 6776f611559c..33650d91e6a3 100644
--- a/media/jni/android_media_ImageWriter.cpp
+++ b/media/jni/android_media_ImageWriter.cpp
@@ -735,10 +735,15 @@ static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, j
}
static status_t attachAndQeueuGraphicBuffer(JNIEnv* env, JNIImageWriterContext *ctx,
- sp<Surface> surface, sp<GraphicBuffer> gb, jlong timestampNs, jint dataSpace,
+ sp<GraphicBuffer> gb, jlong timestampNs, jint dataSpace,
jint left, jint top, jint right, jint bottom, jint transform, jint scalingMode) {
status_t res = OK;
// Step 1. Attach Image
+ sp<Surface> surface = ctx->getProducer();
+ if (surface == nullptr) {
+ jniThrowException(env, "java/lang/IllegalStateException",
+ "Producer surface is null, ImageWriter seems already closed");
+ }
res = surface->attachBuffer(gb.get());
if (res != OK) {
ALOGE("Attach image failed: %s (%d)", strerror(-res), res);
@@ -835,7 +840,6 @@ static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nat
return -1;
}
- sp<Surface> surface = ctx->getProducer();
if (isFormatOpaque(ctx->getBufferFormat()) != isFormatOpaque(nativeHalFormat)) {
jniThrowException(env, "java/lang/IllegalStateException",
"Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa");
@@ -851,7 +855,7 @@ static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nat
return -1;
}
- return attachAndQeueuGraphicBuffer(env, ctx, surface, buffer->mGraphicBuffer, timestampNs,
+ return attachAndQeueuGraphicBuffer(env, ctx, buffer->mGraphicBuffer, timestampNs,
dataSpace, left, top, right, bottom, transform, scalingMode);
}
@@ -866,7 +870,6 @@ static jint ImageWriter_attachAndQueueGraphicBuffer(JNIEnv* env, jobject thiz, j
return -1;
}
- sp<Surface> surface = ctx->getProducer();
if (isFormatOpaque(ctx->getBufferFormat()) != isFormatOpaque(nativeHalFormat)) {
jniThrowException(env, "java/lang/IllegalStateException",
"Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa");
@@ -880,7 +883,8 @@ static jint ImageWriter_attachAndQueueGraphicBuffer(JNIEnv* env, jobject thiz, j
"Trying to attach an invalid graphic buffer");
return -1;
}
- return attachAndQeueuGraphicBuffer(env, ctx, surface, graphicBuffer, timestampNs,
+
+ return attachAndQeueuGraphicBuffer(env, ctx, graphicBuffer, timestampNs,
dataSpace, left, top, right, bottom, transform, scalingMode);
}