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_ImageReader.cpp b/media/jni/android_media_ImageReader.cpp
index 62c0d55..3b93b07 100644
--- a/media/jni/android_media_ImageReader.cpp
+++ b/media/jni/android_media_ImageReader.cpp
@@ -375,11 +375,11 @@
 }
 
 static void ImageReader_init(JNIEnv* env, jobject thiz, jobject weakThiz, jint width, jint height,
-                             jint maxImages, jlong ndkUsage, jint nativeFormat, jint dataSpace) {
+                             jint maxImages, jlong ndkUsage, jint nativeHalFormat, jint dataSpace) {
     status_t res;
 
-    ALOGV("%s: width:%d, height: %d, nativeFormat: %d, maxImages:%d",
-          __FUNCTION__, width, height, nativeFormat, maxImages);
+    ALOGV("%s: width:%d, height: %d, nativeHalFormat: %d, maxImages:%d",
+          __FUNCTION__, width, height, nativeHalFormat, maxImages);
 
     android_dataspace nativeDataspace = static_cast<android_dataspace>(dataSpace);
 
@@ -395,7 +395,7 @@
     BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);
     sp<BufferItemConsumer> bufferConsumer;
     String8 consumerName = String8::format("ImageReader-%dx%df%xm%d-%d-%d",
-            width, height, nativeFormat, maxImages, getpid(),
+            width, height, nativeHalFormat, maxImages, getpid(),
             createProcessUniqueId());
     uint64_t consumerUsage =
             android_hardware_HardwareBuffer_convertToGrallocUsageBits(ndkUsage);
@@ -404,8 +404,8 @@
             /*controlledByApp*/true);
     if (bufferConsumer == nullptr) {
         jniThrowExceptionFmt(env, "java/lang/RuntimeException",
-                "Failed to allocate native buffer consumer for format 0x%x and usage 0x%x",
-                nativeFormat, consumerUsage);
+                "Failed to allocate native buffer consumer for hal format 0x%x and usage 0x%x",
+                nativeHalFormat, consumerUsage);
         return;
     }
 
@@ -419,7 +419,7 @@
     ctx->setProducer(gbProducer);
     bufferConsumer->setFrameAvailableListener(ctx);
     ImageReader_setNativeContext(env, thiz, ctx);
-    ctx->setBufferFormat(nativeFormat);
+    ctx->setBufferFormat(nativeHalFormat);
     ctx->setBufferDataspace(nativeDataspace);
     ctx->setBufferWidth(width);
     ctx->setBufferHeight(height);
@@ -428,14 +428,14 @@
     res = bufferConsumer->setDefaultBufferSize(width, height);
     if (res != OK) {
         jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
-                          "Failed to set buffer consumer default size (%dx%d) for format 0x%x",
-                          width, height, nativeFormat);
+                          "Failed to set buffer consumer default size (%dx%d) for Hal format 0x%x",
+                          width, height, nativeHalFormat);
         return;
     }
-    res = bufferConsumer->setDefaultBufferFormat(nativeFormat);
+    res = bufferConsumer->setDefaultBufferFormat(nativeHalFormat);
     if (res != OK) {
         jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
-                          "Failed to set buffer consumer default format 0x%x", nativeFormat);
+                          "Failed to set buffer consumer default Halformat 0x%x", nativeHalFormat);
         return;
     }
     res = bufferConsumer->setDefaultBufferDataSpace(nativeDataspace);
@@ -522,8 +522,7 @@
     ALOGV("%s: Image (format: 0x%x) has been released", __FUNCTION__, ctx->getBufferFormat());
 }
 
-static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz, jobject image,
-                                   jboolean legacyValidateImageFormat) {
+static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz, jobject image) {
     ALOGV("%s:", __FUNCTION__);
     JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
     if (ctx == NULL) {
@@ -577,29 +576,29 @@
         int outputWidth = getBufferWidth(buffer);
         int outputHeight = getBufferHeight(buffer);
 
-        int imgReaderFmt = ctx->getBufferFormat();
+        int imgReaderHalFmt = ctx->getBufferFormat();
         int imageReaderWidth = ctx->getBufferWidth();
         int imageReaderHeight = ctx->getBufferHeight();
         int bufferFormat = buffer->mGraphicBuffer->getPixelFormat();
-        if ((bufferFormat != HAL_PIXEL_FORMAT_BLOB) && (imgReaderFmt != HAL_PIXEL_FORMAT_BLOB) &&
+        if ((bufferFormat != HAL_PIXEL_FORMAT_BLOB) && (imgReaderHalFmt != HAL_PIXEL_FORMAT_BLOB) &&
                 (imageReaderWidth != outputWidth || imageReaderHeight != outputHeight)) {
             ALOGV("%s: Producer buffer size: %dx%d, doesn't match ImageReader configured size: %dx%d",
                     __FUNCTION__, outputWidth, outputHeight, imageReaderWidth, imageReaderHeight);
         }
-        if (legacyValidateImageFormat && imgReaderFmt != bufferFormat) {
-            if (imgReaderFmt == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
+        if (imgReaderHalFmt != bufferFormat) {
+            if (imgReaderHalFmt == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
                     isPossiblyYUV(bufferFormat)) {
                 // Treat formats that are compatible with flexible YUV
                 // (HAL_PIXEL_FORMAT_YCbCr_420_888) as HAL_PIXEL_FORMAT_YCbCr_420_888.
                 ALOGV("%s: Treat buffer format to 0x%x as HAL_PIXEL_FORMAT_YCbCr_420_888",
                         __FUNCTION__, bufferFormat);
-            } else if (imgReaderFmt == HAL_PIXEL_FORMAT_YCBCR_P010 &&
+            } else if (imgReaderHalFmt == HAL_PIXEL_FORMAT_YCBCR_P010 &&
                     isPossibly10BitYUV(bufferFormat)) {
                 // Treat formats that are compatible with flexible 10-bit YUV
                 // (HAL_PIXEL_FORMAT_YCBCR_P010) as HAL_PIXEL_FORMAT_YCBCR_P010.
                 ALOGV("%s: Treat buffer format to 0x%x as HAL_PIXEL_FORMAT_YCBCR_P010",
                         __FUNCTION__, bufferFormat);
-            } else if (imgReaderFmt == HAL_PIXEL_FORMAT_BLOB &&
+            } else if (imgReaderHalFmt == HAL_PIXEL_FORMAT_BLOB &&
                     bufferFormat == HAL_PIXEL_FORMAT_RGBA_8888) {
                 // Using HAL_PIXEL_FORMAT_RGBA_8888 Gralloc buffers containing JPEGs to get around
                 // SW write limitations for (b/17379185).
@@ -842,7 +841,7 @@
 }
 
 static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
-        int numPlanes, int readerFormat, uint64_t ndkReaderUsage)
+        int numPlanes, int halReaderFormat, uint64_t ndkReaderUsage)
 {
     ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
     int rowStride = 0;
@@ -851,9 +850,6 @@
     uint32_t dataSize = 0;
     jobject byteBuffer = NULL;
 
-    PublicFormat publicReaderFormat = static_cast<PublicFormat>(readerFormat);
-    int halReaderFormat = mapPublicFormatToHalFormat(publicReaderFormat);
-
     if (isFormatOpaque(halReaderFormat) && numPlanes > 0) {
         String8 msg;
         msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
@@ -963,7 +959,7 @@
     {"nativeInit",             "(Ljava/lang/Object;IIIJII)V",   (void*)ImageReader_init },
     {"nativeClose",            "()V",                        (void*)ImageReader_close },
     {"nativeReleaseImage",     "(Landroid/media/Image;)V",   (void*)ImageReader_imageRelease },
-    {"nativeImageSetup",       "(Landroid/media/Image;Z)I",   (void*)ImageReader_imageSetup },
+    {"nativeImageSetup",       "(Landroid/media/Image;)I",   (void*)ImageReader_imageSetup },
     {"nativeGetSurface",       "()Landroid/view/Surface;",   (void*)ImageReader_getSurface },
     {"nativeDetachImage",      "(Landroid/media/Image;)I",   (void*)ImageReader_detachImage },
     {"nativeCreateImagePlanes",