gralloc: Add RGB format support in getFormatLayout API

CRs-Fixed: 2568703
Change-Id: I183a85fe5d30dc15454de0f5bec39658c24775ee
diff --git a/gralloc/QtiMapperExtensions.cpp b/gralloc/QtiMapperExtensions.cpp
index cace7ba..33cd23d 100644
--- a/gralloc/QtiMapperExtensions.cpp
+++ b/gralloc/QtiMapperExtensions.cpp
@@ -344,7 +344,7 @@
                                                   getFormatLayout_cb hidl_cb) {
   ALOGD_IF(DEBUG, "%s: Input parameters - wxh: %dx%d usage: 0x%" PRIu64 " format: %d", __FUNCTION__,
            width, height, usage, format);
-  auto err = Error::BAD_BUFFER;
+  auto err = Error::NONE;
   hidl_vec<PlaneLayout> plane_info;
   unsigned int alignedw = 0, alignedh = 0;
   int plane_count = 0;
@@ -353,34 +353,41 @@
   BufferInfo info(width, height, custom_format, usage);
   gralloc::GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
   size = gralloc::GetSize(info, alignedw, alignedh);
+  gralloc::PlaneLayoutInfo plane_layout[8] = {};
   ALOGD_IF(DEBUG, "%s: Aligned width and height - wxh: %ux%u custom_format = %d", __FUNCTION__,
            alignedw, alignedh, custom_format);
   if (gralloc::IsYuvFormat(custom_format)) {
-    gralloc::PlaneLayoutInfo yuv_plane_info[8] = {};
     gralloc::GetYUVPlaneInfo(info, custom_format, alignedw, alignedh, flags, &plane_count,
-                             yuv_plane_info);
-    ALOGD_IF(DEBUG, "%s: Number of plane - %d, custom_format - %d", __FUNCTION__, plane_count,
-             custom_format);
-    plane_info.resize(plane_count);
-    for (int i = 0; i < plane_count; i++) {
-      plane_info[i].component = yuv_plane_info[i].component;
-      plane_info[i].h_subsampling = yuv_plane_info[i].h_subsampling;
-      plane_info[i].v_subsampling = yuv_plane_info[i].v_subsampling;
-      plane_info[i].offset = yuv_plane_info[i].offset;
-      plane_info[i].pixel_increment = yuv_plane_info[i].step;
-      plane_info[i].stride = yuv_plane_info[i].stride;
-      plane_info[i].stride_bytes = yuv_plane_info[i].stride_bytes;
-      plane_info[i].scanlines = yuv_plane_info[i].scanlines;
-      plane_info[i].size = yuv_plane_info[i].size;
-      ALOGD_IF(DEBUG, "%s: plane info: component - %d", __FUNCTION__, plane_info[i].component);
-      ALOGD_IF(DEBUG, "h_subsampling - %u, v_subsampling - %u, offset - %u, pixel_increment - %d",
-               plane_info[i].h_subsampling, plane_info[i].v_subsampling, plane_info[i].offset,
-               plane_info[i].pixel_increment);
-      ALOGD_IF(DEBUG, "stride_pixel - %d, stride_bytes - %d, scanlines - %d, size - %u",
-               plane_info[i].stride, plane_info[i].stride_bytes, plane_info[i].scanlines,
-               plane_info[i].size);
-    }
-    err = Error::NONE;
+                             plane_layout);
+  } else if (gralloc::IsUncompressedRGBFormat(custom_format) ||
+             gralloc::IsCompressedRGBFormat(custom_format)) {
+    gralloc::GetRGBPlaneInfo(info, custom_format, alignedw, alignedh, flags, &plane_count,
+                             plane_layout);
+  } else {
+    err = Error::BAD_BUFFER;
+    hidl_cb(err, size, plane_info);
+    return Void();
+  }
+  ALOGD_IF(DEBUG, "%s: Number of plane - %d, custom_format - %d", __FUNCTION__, plane_count,
+           custom_format);
+  plane_info.resize(plane_count);
+  for (int i = 0; i < plane_count; i++) {
+    plane_info[i].component = plane_layout[i].component;
+    plane_info[i].h_subsampling = plane_layout[i].h_subsampling;
+    plane_info[i].v_subsampling = plane_layout[i].v_subsampling;
+    plane_info[i].offset = plane_layout[i].offset;
+    plane_info[i].pixel_increment = plane_layout[i].step;
+    plane_info[i].stride = plane_layout[i].stride;
+    plane_info[i].stride_bytes = plane_layout[i].stride_bytes;
+    plane_info[i].scanlines = plane_layout[i].scanlines;
+    plane_info[i].size = plane_layout[i].size;
+    ALOGD_IF(DEBUG, "%s: plane info: component - %d", __FUNCTION__, plane_info[i].component);
+    ALOGD_IF(DEBUG, "h_subsampling - %u, v_subsampling - %u, offset - %u, pixel_increment - %d",
+             plane_info[i].h_subsampling, plane_info[i].v_subsampling, plane_info[i].offset,
+             plane_info[i].pixel_increment);
+    ALOGD_IF(DEBUG, "stride_pixel - %d, stride_bytes - %d, scanlines - %d, size - %u",
+             plane_info[i].stride, plane_info[i].stride_bytes, plane_info[i].scanlines,
+             plane_info[i].size);
   }
   hidl_cb(err, size, plane_info);
   return Void();
diff --git a/gralloc/gr_utils.cpp b/gralloc/gr_utils.cpp
index 9eca92d..6eb414e 100644
--- a/gralloc/gr_utils.cpp
+++ b/gralloc/gr_utils.cpp
@@ -794,6 +794,28 @@
   return size;
 }
 
+unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage) {
+  unsigned int meta_size = 0;
+  if (!IsUBwcEnabled(format, usage)) {
+    return meta_size;
+  }
+  uint32_t bpp = GetBppForUncompressedRGB(format);
+  switch (format) {
+    case HAL_PIXEL_FORMAT_BGR_565:
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+    case HAL_PIXEL_FORMAT_RGBX_1010102:
+    case HAL_PIXEL_FORMAT_RGBA_FP16:
+      meta_size = GetRgbUBwcMetaBufferSize(width, height, bpp);
+      break;
+    default:
+      ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, format);
+      break;
+  }
+  return meta_size;
+}
+
 int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
   int err = 0;
 
@@ -807,22 +829,8 @@
     *rgb_data = reinterpret_cast<void *>(hnd->base);
     return err;
   }
+  unsigned int meta_size = GetRgbMetaSize(hnd->format, hnd->width, hnd->height, hnd->usage);
 
-  unsigned int meta_size = 0;
-  uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
-  switch (hnd->format) {
-    case HAL_PIXEL_FORMAT_BGR_565:
-    case HAL_PIXEL_FORMAT_RGBA_8888:
-    case HAL_PIXEL_FORMAT_RGBX_8888:
-    case HAL_PIXEL_FORMAT_RGBA_1010102:
-    case HAL_PIXEL_FORMAT_RGBX_1010102:
-      meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
-      break;
-    default:
-      ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
-      err = -EINVAL;
-      break;
-  }
   *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
 
   return err;
@@ -1560,4 +1568,44 @@
   }
 }
 
+bool HasAlphaComponent(int32_t format) {
+  switch (format) {
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+    case HAL_PIXEL_FORMAT_RGBA_5551:
+    case HAL_PIXEL_FORMAT_RGBA_4444:
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+    case HAL_PIXEL_FORMAT_ARGB_2101010:
+    case HAL_PIXEL_FORMAT_BGRA_1010102:
+    case HAL_PIXEL_FORMAT_ABGR_2101010:
+    case HAL_PIXEL_FORMAT_RGBA_FP16:
+      return true;
+    default:
+      return false;
+  }
+}
+
+void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
+                     int32_t /* flags */, int *plane_count, PlaneLayoutInfo *plane_info) {
+  uint64_t usage = info.usage;
+  *plane_count = 1;
+  uint32_t bpp = 0;
+  if (IsUncompressedRGBFormat(format)) {
+    bpp = GetBppForUncompressedRGB(format);
+  }
+  plane_info->component =
+      (PlaneComponent)(PLANE_COMPONENT_R | PLANE_COMPONENT_G | PLANE_COMPONENT_B);
+  if (HasAlphaComponent(format)) {
+    plane_info->component = (PlaneComponent)(plane_info->component | PLANE_COMPONENT_A);
+  }
+  plane_info->size = GetSize(info, width, height);
+  plane_info->step = bpp;
+  plane_info->offset = GetRgbMetaSize(format, width, height, usage);
+  plane_info->h_subsampling = 0;
+  plane_info->v_subsampling = 0;
+  plane_info->stride = width;
+  plane_info->stride_bytes = width * plane_info->step;
+  plane_info->scanlines = height;
+}
+
 }  // namespace gralloc
diff --git a/gralloc/gr_utils.h b/gralloc/gr_utils.h
index 8ecb90f..39d7e9b 100644
--- a/gralloc/gr_utils.h
+++ b/gralloc/gr_utils.h
@@ -144,6 +144,9 @@
 int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]);
 int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
                     int32_t flags, int *plane_count, PlaneLayoutInfo plane_info[8]);
+void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
+                     int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info);
+unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage);
 void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling);
 void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
                                        struct android_ycbcr *ycbcr);
@@ -178,6 +181,7 @@
 int GetCustomFormatFlags(int format, uint64_t usage, int *custom_format, uint64_t *priv_flags);
 int GetBufferType(int inputFormat);
 bool IsGPUFlagSupported(uint64_t usage);
+bool HasAlphaComponent(int32_t format);
 }  // namespace gralloc
 
 #endif  // __GR_UTILS_H__