Fix cts flaky issue on adt3 device.

Relnote: This is a combination of 3 commits cherry-picked from internal
branch, including:
- part of I3f18b9d3550f88be82cde75372637a3ae528b5ec
- If70f8640e6c057fa4be910372282df764bb84341
- I58340f684b4fc9cb2578cee03ef1d65171c9eaed

Bug: 250079821
Test: android.hardware.cts.DataSpaceTest#getDataSpaceWithoutSetDataSpace on adt3 locally for 20 runs

Change-Id: I3f18b9d3550f88be82cde75372637a3ae528b5ec
Merged-In: I3f18b9d3550f88be82cde75372637a3ae528b5ec
diff --git a/media/jni/android_media_ImageWriter.cpp b/media/jni/android_media_ImageWriter.cpp
index 6c6fccb..2c498e5 100644
--- a/media/jni/android_media_ImageWriter.cpp
+++ b/media/jni/android_media_ImageWriter.cpp
@@ -415,7 +415,9 @@
 
     // Get the dimension and format of the producer.
     sp<ANativeWindow> anw = producer;
-    int32_t width, height, surfaceFormat;
+    int32_t width, height, surfaceHalFormat;
+    int32_t surfaceFormat = 0;
+    int32_t surfaceDataspace = 0;
     if (userWidth < 0) {
         if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
             ALOGE("%s: Query Surface width failed: %s (%d)", __FUNCTION__, strerror(-res), res);
@@ -451,11 +453,18 @@
     // Query surface format if no valid user format is specified, otherwise, override surface format
     // with user format.
     if (useSurfaceImageFormatInfo) {
-        if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceFormat)) != OK) {
+        // retrieve hal format
+        if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceHalFormat)) != OK) {
             ALOGE("%s: Query Surface format failed: %s (%d)", __FUNCTION__, strerror(-res), res);
             jniThrowRuntimeException(env, "Failed to query Surface format");
             return 0;
         }
+        if ((res = anw->query(
+                anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE, &surfaceDataspace)) != OK) {
+            ALOGE("%s: Query Surface dataspace failed: %s (%d)", __FUNCTION__, strerror(-res), res);
+            jniThrowRuntimeException(env, "Failed to query Surface dataspace");
+            return 0;
+        }
     } else {
         // Set consumer buffer format to user specified format
         android_dataspace nativeDataspace = static_cast<android_dataspace>(dataSpace);
@@ -475,17 +484,22 @@
             return 0;
         }
         ctx->setBufferDataSpace(nativeDataspace);
-        surfaceFormat = static_cast<int32_t>(mapHalFormatDataspaceToPublicFormat(
-            hardwareBufferFormat, nativeDataspace));
+        surfaceDataspace = dataSpace;
+        surfaceHalFormat = hardwareBufferFormat;
     }
 
-    ctx->setBufferFormat(surfaceFormat);
+    ctx->setBufferFormat(surfaceHalFormat);
+    ctx->setBufferDataSpace(static_cast<android_dataspace>(surfaceDataspace));
+
+    // update class.mWriterFormat
+    surfaceFormat = static_cast<int32_t>(mapHalFormatDataspaceToPublicFormat(
+                surfaceHalFormat, static_cast<android_dataspace>(surfaceDataspace)));
     env->SetIntField(thiz,
             gImageWriterClassInfo.mWriterFormat, reinterpret_cast<jint>(surfaceFormat));
 
     // ndkUsage == -1 means setUsage in ImageWriter class is not called.
     // skip usage setting if setUsage in ImageWriter is not called and imageformat is opaque.
-    if (!(ndkUsage == -1 && isFormatOpaque(surfaceFormat))) {
+    if (!(ndkUsage == -1 && isFormatOpaque(surfaceHalFormat))) {
         if (ndkUsage == -1) {
             ndkUsage = GRALLOC_USAGE_SW_WRITE_OFTEN;
         }
@@ -809,7 +823,7 @@
 }
 
 static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nativeCtx,
-        jlong nativeBuffer, jint imageFormat, jlong timestampNs, jint dataSpace,
+        jlong nativeBuffer, jint nativeHalFormat, jlong timestampNs, jint dataSpace,
         jint left, jint top, jint right, jint bottom, jint transform, jint scalingMode) {
     ALOGV("%s", __FUNCTION__);
     JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
@@ -820,7 +834,7 @@
     }
 
     sp<Surface> surface = ctx->getProducer();
-    if (isFormatOpaque(imageFormat) != isFormatOpaque(ctx->getBufferFormat())) {
+    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");
         return -1;
@@ -840,8 +854,8 @@
 }
 
 static jint ImageWriter_attachAndQueueGraphicBuffer(JNIEnv* env, jobject thiz, jlong nativeCtx,
-        jobject buffer, jint format, jlong timestampNs, jint dataSpace, jint left, jint top,
-        jint right, jint bottom, jint transform, jint scalingMode) {
+        jobject buffer, jint nativeHalFormat, jlong timestampNs, jint dataSpace,
+        jint left, jint top, jint right, jint bottom, jint transform, jint scalingMode) {
     ALOGV("%s", __FUNCTION__);
     JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
     if (ctx == NULL || thiz == NULL) {
@@ -851,7 +865,7 @@
     }
 
     sp<Surface> surface = ctx->getProducer();
-    if (isFormatOpaque(format) != isFormatOpaque(ctx->getBufferFormat())) {
+    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");
         return -1;
@@ -1028,32 +1042,32 @@
 }
 
 static bool Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
-        int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
+        int32_t halFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
     ALOGV("%s", __FUNCTION__);
 
-    status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
+    status_t res = getLockedImageInfo(buffer, idx, halFormat, base, size,
             pixelStride, rowStride);
     if (res != OK) {
         jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
-                             "Pixel format: 0x%x is unsupported", writerFormat);
+                             "Pixel format: 0x%x is unsupported", halFormat);
         return false;
     }
     return true;
 }
 
 static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
-        int numPlanes, int writerFormat, int dataSpace) {
+        int numPlanes, int writerFormat) {
     ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
     int rowStride, pixelStride;
     uint8_t *pData;
     uint32_t dataSize;
     jobject byteBuffer;
+    int halFormat = mapPublicFormatToHalFormat(static_cast<PublicFormat>(writerFormat));
 
-    int format = Image_getFormat(env, thiz, dataSpace);
-    if (isFormatOpaque(format) && numPlanes > 0) {
+    if (isFormatOpaque(halFormat) && numPlanes > 0) {
         String8 msg;
         msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
-                " must be 0", format, numPlanes);
+                " must be 0", writerFormat, numPlanes);
         jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
         return NULL;
     }
@@ -1065,7 +1079,8 @@
                 " probably out of memory");
         return NULL;
     }
-    if (isFormatOpaque(format)) {
+
+    if (isFormatOpaque(halFormat)) {
         return surfacePlanes;
     }
 
@@ -1074,10 +1089,8 @@
     Image_getLockedImage(env, thiz, &lockedImg);
 
     // Create all SurfacePlanes
-    PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat);
-    writerFormat = mapPublicFormatToHalFormat(publicWriterFormat);
     for (int i = 0; i < numPlanes; i++) {
-        if (!Image_getLockedImageInfo(env, &lockedImg, i, writerFormat,
+        if (!Image_getLockedImageInfo(env, &lockedImg, i, halFormat,
                 &pData, &dataSize, &pixelStride, &rowStride)) {
             return NULL;
         }
@@ -1119,7 +1132,7 @@
 };
 
 static JNINativeMethod gImageMethods[] = {
-    {"nativeCreatePlanes",      "(III)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
+    {"nativeCreatePlanes",      "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
                                                                (void*)Image_createSurfacePlanes },
     {"nativeGetWidth",          "()I",                         (void*)Image_getWidth },
     {"nativeGetHeight",         "()I",                         (void*)Image_getHeight },