hwc2/gralloc1: Create libgrallocutils

--For gralloc1, create a libgrallocutils that provides helpers to
calculate various buffer related info
--Make hwc2/gralloc1 structures independent of libdrmutils
and fb_id / gemhandles
--Implement GetBufferLayout API from BufferAllocator that can be called
by DAL before creating fb_id

Change-Id: I0aede7d1ea87302ecda593047728d381383484b9
CRs-fixed: 1114808
diff --git a/sdm/libs/hwc2/Android.mk b/sdm/libs/hwc2/Android.mk
index ca1ebae..0bc5acf 100644
--- a/sdm/libs/hwc2/Android.mk
+++ b/sdm/libs/hwc2/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SHARED_LIBRARIES        := libsdmcore libqservice libbinder libhardware libhardware_legacy \
                                  libutils libcutils libsync libqdutils libqdMetaData libdl \
-                                 libpowermanager libsdmutils libc++ liblog libdrmutils
+                                 libpowermanager libsdmutils libc++ liblog libgrallocutils
 
 ifneq ($(TARGET_USES_GRALLOC1), true)
     LOCAL_SHARED_LIBRARIES += libmemalloc
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.cpp b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
index 3cfaf0a..b7fcac3 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.cpp
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
@@ -35,8 +35,10 @@
 
 #include "hwc_buffer_allocator.h"
 #include "hwc_debugger.h"
+#include "gr_utils.h"
 
 #define __CLASS__ "HWCBufferAllocator"
+
 namespace sdm {
 
 HWCBufferAllocator::HWCBufferAllocator() {
@@ -64,7 +66,7 @@
   uint32_t width = buffer_config.width;
   uint32_t height = buffer_config.height;
   int format;
-  int alloc_flags = 0;
+  uint64_t alloc_flags = 0;
   int error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
   if (error != 0) {
     return kErrorParameters;
@@ -78,8 +80,8 @@
     // Allocate uncached buffers
     alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
   }
-  uint64_t producer_usage = UINT64(alloc_flags);
-  uint64_t consumer_usage = UINT64(alloc_flags);
+  uint64_t producer_usage = alloc_flags;
+  uint64_t consumer_usage = alloc_flags;
   // CreateBuffer
   private_handle_t *hnd = nullptr;
   Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER, width, height, format,
@@ -103,6 +105,7 @@
   buffer_handle_t hnd = static_cast<private_handle_t *>(buffer_info->private_data);
   ReleaseBuffer_(gralloc_device_, hnd);
   AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
+
   alloc_buffer_info->fd = -1;
   alloc_buffer_info->stride = 0;
   alloc_buffer_info->size = 0;
@@ -136,7 +139,7 @@
 
 uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
   const BufferConfig &buffer_config = buffer_info->buffer_config;
-  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
+  uint64_t alloc_flags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
 
   int width = INT(buffer_config.width);
   int height = INT(buffer_config.height);
@@ -156,17 +159,17 @@
   }
 
   uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
-  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
-  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
+  gralloc1_producer_usage_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
+  gralloc1_consumer_usage_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
   // TODO(user): Currently both flags are treated similarly in gralloc
-  producer_usage = UINT64(alloc_flags);
-  consumer_usage = producer_usage;
-  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
-           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
+  producer_usage = gralloc1_producer_usage_t(alloc_flags);
+  consumer_usage = gralloc1_consumer_usage_t(alloc_flags);
+  gralloc1::BufferInfo info(width, height, format, producer_usage, consumer_usage);
+  GetBufferSizeAndDimensions(info, &aligned_width, &aligned_height, &buffer_size);
   return buffer_size;
 }
 
-int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, int *flags) {
+int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, uint64_t *flags) {
   switch (format) {
     case kFormatRGBA8888:
       *target = HAL_PIXEL_FORMAT_RGBA_8888;
@@ -209,6 +212,7 @@
       break;
     case kFormatYCbCr420SPVenusUbwc:
       *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
       break;
     case kFormatRGBA5551:
       *target = HAL_PIXEL_FORMAT_RGBA_5551;
@@ -245,9 +249,11 @@
       break;
     case kFormatYCbCr420TP10Ubwc:
       *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
       break;
     case kFormatYCbCr420P010Ubwc:
       *target = HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
       break;
     case kFormatRGBA8888Ubwc:
       *target = HAL_PIXEL_FORMAT_RGBA_8888;
@@ -280,7 +286,7 @@
     const BufferConfig &buffer_config, AllocatedBufferInfo *allocated_buffer_info) {
   // TODO(user): This API should pass the buffer_info of the already allocated buffer
   // The private_data can then be typecast to the private_handle and used directly.
-  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
+  uint64_t alloc_flags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
 
   int width = INT(buffer_config.width);
   int height = INT(buffer_config.height);
@@ -300,13 +306,13 @@
   }
 
   uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
-  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
-  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
+  gralloc1_producer_usage_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
+  gralloc1_consumer_usage_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
   // TODO(user): Currently both flags are treated similarly in gralloc
-  producer_usage = UINT64(alloc_flags);
-  consumer_usage = producer_usage;
-  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
-           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
+  producer_usage = gralloc1_producer_usage_t(alloc_flags);
+  consumer_usage = gralloc1_consumer_usage_t(alloc_flags);
+  gralloc1::BufferInfo info(width, height, format, producer_usage, consumer_usage);
+  GetBufferSizeAndDimensions(info, &aligned_width, &aligned_height, &buffer_size);
   allocated_buffer_info->stride = UINT32(aligned_width);
   allocated_buffer_info->aligned_width = UINT32(aligned_width);
   allocated_buffer_info->aligned_height = UINT32(aligned_height);
@@ -315,4 +321,30 @@
   return kErrorNone;
 }
 
+DisplayError HWCBufferAllocator::GetBufferLayout(const AllocatedBufferInfo &buf_info,
+                                                 uint32_t stride[4], uint32_t offset[4],
+                                                 uint32_t *num_planes) {
+  // TODO(user): Transition APIs to not need a private handle
+  private_handle_t hnd(-1, 0, 0, 0, 0, 0, 0);
+  int format = HAL_PIXEL_FORMAT_RGBA_8888;
+  uint64_t flags = 0;
+
+  SetBufferInfo(buf_info.format, &format, &flags);
+  // Setup only the required stuff, skip rest
+  hnd.format = format;
+  hnd.width = INT32(buf_info.aligned_width);
+  hnd.height = INT32(buf_info.aligned_height);
+  if (flags & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) {
+    hnd.flags = private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
+  }
+
+  int ret = gralloc1::GetBufferLayout(&hnd, stride, offset, num_planes);
+  if (ret < 0) {
+    DLOGE("GetBufferLayout failed");
+    return kErrorParameters;
+  }
+
+  return kErrorNone;
+}
+
 }  // namespace sdm
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.h b/sdm/libs/hwc2/hwc_buffer_allocator.h
index c28a94e..d574401 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.h
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.h
@@ -57,7 +57,10 @@
                                 int *aligned_width, int *aligned_height);
   DisplayError GetAllocatedBufferInfo(const BufferConfig &buffer_config,
                                       AllocatedBufferInfo *allocated_buffer_info);
-  int SetBufferInfo(LayerBufferFormat format, int *target, int *flags);
+  DisplayError GetBufferLayout(const AllocatedBufferInfo &buf_info,
+                               uint32_t stride[4], uint32_t offset[4],
+                               uint32_t *num_planes);
+  int SetBufferInfo(LayerBufferFormat format, int *target, uint64_t *flags);
 
  private:
   gralloc1_device_t *gralloc_device_ = nullptr;
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 302e339..aec44ad 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -154,7 +154,6 @@
   layer_buffer->acquire_fence_fd = acquire_fence;
   layer_buffer->size = handle->size;
   layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
-  layer_buffer->fb_id = handle->fb_id;
 
   return HWC2::Error::None;
 }