display: metadata fixes

* Do not log error on invalid metadata fds, such usages are
possible in some scenarios
* Correct usage of metadata API for some cases.

CRs-Fixed: 2122143
Change-Id: Id9cc7f4d27ef1890668f414d92b96cd419b6a066
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 35c4b02..eb97f15 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -725,7 +725,7 @@
         return GRALLOC1_ERROR_BAD_VALUE;
       }
 
-      if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
+      if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
         *map_secure_buffer = 0;
       }
     } break;
@@ -853,8 +853,8 @@
     return GRALLOC1_ERROR_UNSUPPORTED;
   }
 
-  android_ycbcr ycbcr;
-  int err = GetYUVPlaneInfo(hnd, &ycbcr);
+  android_ycbcr yuvPlaneInfo[2];
+  int err = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
 
   if (err != 0) {
     return GRALLOC1_ERROR_BAD_HANDLE;
@@ -872,6 +872,8 @@
     layout->planes[i].v_subsampling = 2;
   }
 
+  // We are only returning flex layout for progressive or single field formats.
+  struct android_ycbcr ycbcr = yuvPlaneInfo[0];
   layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
   layout->planes[0].component = FLEX_COMPONENT_Y;
   layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
diff --git a/libgralloc1/gr_utils.cpp b/libgralloc1/gr_utils.cpp
index 945dea4..a7e8c91 100644
--- a/libgralloc1/gr_utils.cpp
+++ b/libgralloc1/gr_utils.cpp
@@ -312,7 +312,7 @@
 }
 
 void GetYuvUbwcInterlacedSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
-                                     int color_format, struct android_ycbcr *ycbcr) {
+                                     int color_format, struct android_ycbcr ycbcr[2]) {
   unsigned int uv_stride, uv_height, uv_size;
   unsigned int alignment = 4096;
   uint64_t field_base;
@@ -329,6 +329,7 @@
   field_base = base;
   GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[0]);
 
+  memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
   field_base = reinterpret_cast<uint64_t>(ycbcr[0].cb) + uv_size;
   GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[1]);
 }
@@ -346,7 +347,7 @@
   ycbcr->chroma_step = 2 * bpp;
 }
 
-int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr) {
+int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
   int err = 0;
   uint32_t width = UINT(hnd->width);
   uint32_t height = UINT(hnd->height);
@@ -382,7 +383,7 @@
   // Check metadata for interlaced content.
   int interlace_flag = 0;
   if (getMetaData(const_cast<private_handle_t *>(hnd),
-                  GET_PP_PARAM_INTERLACED, &interlace_flag) != 0) {
+                  GET_PP_PARAM_INTERLACED, &interlace_flag) == 0) {
     interlaced = interlace_flag;
   }
 
@@ -772,7 +773,7 @@
     return -EINVAL;
   }
 
-  struct android_ycbcr yuvInfo = {};
+  struct android_ycbcr yuvPlaneInfo[2] = {};
   *num_planes = 1;
   stride[0] = 0;
 
@@ -808,12 +809,14 @@
   }
 
   (*num_planes)++;
-  int ret = GetYUVPlaneInfo(hnd, &yuvInfo);
+  int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
   if (ret < 0) {
     ALOGE("%s failed", __FUNCTION__);
     return ret;
   }
 
+  // We are only returning buffer layout for progressive or single field formats.
+  struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
   stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
   offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
   stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
diff --git a/libgralloc1/gr_utils.h b/libgralloc1/gr_utils.h
index 2a08539..4b2f136 100644
--- a/libgralloc1/gr_utils.h
+++ b/libgralloc1/gr_utils.h
@@ -71,7 +71,7 @@
                                 unsigned int *alignedw, unsigned int *alignedh);
 void GetAlignedWidthAndHeight(const BufferInfo &d, unsigned int *aligned_w,
                               unsigned int *aligned_h);
-int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr);
+int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]);
 int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data);
 bool IsUBwcFormat(int format);
 bool IsUBwcSupported(int format);
@@ -84,7 +84,7 @@
 void GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, int color_format,
                            struct android_ycbcr *ycbcr);
 void GetYuvUbwcInterlacedSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
-                                     int color_format, struct android_ycbcr *ycbcr);
+                                     int color_format, struct android_ycbcr ycbcr[2]);
 void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height);
 unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp);
 unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,