gralloc1: Add support for layered buffers

Implements the gralloc1 capability GRALLOC1_CAPABILITY_LAYERED_BUFFERS.

CRs-Fixed: 2025988
Change-Id: I80c8ef6aa18844e33ecd8b019c6f4cf08872afb5
diff --git a/libgralloc1/gr_buf_descriptor.h b/libgralloc1/gr_buf_descriptor.h
index 95386fa..1f86867 100644
--- a/libgralloc1/gr_buf_descriptor.h
+++ b/libgralloc1/gr_buf_descriptor.h
@@ -65,6 +65,8 @@
 
   void SetColorFormat(int format) { format_ = format; }
 
+  void SetLayerCount(uint32_t layer_count) { layer_count_ = layer_count; }
+
   gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage_; }
 
   gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage_; }
@@ -75,12 +77,15 @@
 
   int GetFormat() const { return format_; }
 
+  uint32_t GetLayerCount() const { return layer_count_; }
+
   gralloc1_buffer_descriptor_t GetId() const { return id_; }
 
  private:
   int width_ = -1;
   int height_ = -1;
   int format_ = -1;
+  uint32_t layer_count_ = 1;
   gralloc1_producer_usage_t producer_usage_ = GRALLOC1_PRODUCER_USAGE_NONE;
   gralloc1_consumer_usage_t consumer_usage_ = GRALLOC1_CONSUMER_USAGE_NONE;
   const gralloc1_buffer_descriptor_t id_;
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 246bf5c..d3a307b 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -469,6 +469,7 @@
   int format = descriptor.GetFormat();
   gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
   gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
+  uint32_t layer_count = descriptor.GetLayerCount();
 
   // Get implementation defined format
   int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
@@ -478,6 +479,7 @@
   int buffer_type = GetBufferType(gralloc_format);
   allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
   size = (bufferSize >= size) ? bufferSize : size;
+  size = size * layer_count;
 
   int err = 0;
   int flags = 0;
@@ -528,6 +530,7 @@
   hnd->id = ++next_id_;
   hnd->base = 0;
   hnd->base_metadata = 0;
+  hnd->layer_count = layer_count;
 
   ColorSpace_t colorSpace = ITU_R_601;
   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp
index 3649111..08173e8 100644
--- a/libgralloc1/gr_device_impl.cpp
+++ b/libgralloc1/gr_device_impl.cpp
@@ -103,10 +103,11 @@
 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
                                   int32_t  /*gralloc1_capability_t*/ *out_capabilities) {
   if (device != nullptr) {
-    if (out_capabilities != nullptr && *out_count > 0) {
+    if (out_capabilities != nullptr && *out_count >= 2) {
       out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
+      out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
     }
-    *out_count = 1;
+    *out_count = 2;
   }
   return;
 }
@@ -129,6 +130,8 @@
       return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
     case GRALLOC1_FUNCTION_SET_FORMAT:
       return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
+    case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
+      return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
     case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
       return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
     case GRALLOC1_FUNCTION_GET_BACKING_STORE:
@@ -139,6 +142,8 @@
       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
     case GRALLOC1_FUNCTION_GET_FORMAT:
       return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
+    case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
+      return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
     case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
       return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
     case GRALLOC1_FUNCTION_GET_STRIDE:
@@ -257,6 +262,19 @@
   }
 }
 
+gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
+                                            gralloc1_buffer_descriptor_t descriptor,
+                                            uint32_t layer_count) {
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetLayerCount,
+                                                       layer_count);
+  }
+}
+
 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
                                                gralloc1_buffer_descriptor_t descriptor,
                                                gralloc1_producer_usage_t usage) {
@@ -313,6 +331,16 @@
   return status;
 }
 
+gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
+                                            uint32_t *outLayerCount) {
+  gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
+  if (status == GRALLOC1_ERROR_NONE) {
+    *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
+  }
+
+  return status;
+}
+
 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
                                                gralloc1_producer_usage_t *outUsage) {
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
diff --git a/libgralloc1/gr_device_impl.h b/libgralloc1/gr_device_impl.h
index 5a3e7e9..55ce44b 100644
--- a/libgralloc1/gr_device_impl.h
+++ b/libgralloc1/gr_device_impl.h
@@ -76,6 +76,9 @@
                                               uint32_t width, uint32_t height);
   static gralloc1_error_t SetColorFormat(gralloc1_device_t *device,
                                          gralloc1_buffer_descriptor_t descriptor, int32_t format);
+  static gralloc1_error_t SetLayerCount(gralloc1_device_t *device,
+                                        gralloc1_buffer_descriptor_t descriptor,
+                                        uint32_t layer_count);
   static gralloc1_error_t SetProducerUsage(gralloc1_device_t *device,
                                            gralloc1_buffer_descriptor_t descriptor,
                                            gralloc1_producer_usage_t usage);
@@ -87,6 +90,8 @@
                                               uint32_t *out_width, uint32_t *out_height);
   static gralloc1_error_t GetColorFormat(gralloc1_device_t *device, buffer_handle_t descriptor,
                                          int32_t *outFormat);
+  static gralloc1_error_t GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
+                                        uint32_t *out_layer_count);
   static gralloc1_error_t GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
                                            gralloc1_producer_usage_t *out_usage);
   static gralloc1_error_t GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
diff --git a/libgralloc1/gr_priv_handle.h b/libgralloc1/gr_priv_handle.h
index 27d405b..5948aab 100644
--- a/libgralloc1/gr_priv_handle.h
+++ b/libgralloc1/gr_priv_handle.h
@@ -83,6 +83,7 @@
   uint64_t id                              __attribute__((aligned(8)));
   gralloc1_producer_usage_t producer_usage __attribute__((aligned(8)));
   gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8)));
+  unsigned int layer_count;
 
   static const int kNumFds = 2;
   static const int kMagic = 'gmsm';
@@ -123,7 +124,8 @@
         gpuaddr(0),
         id(0),
         producer_usage(prod_usage),
-        consumer_usage(cons_usage) {
+        consumer_usage(cons_usage),
+        layer_count(1) {
     version = static_cast<int>(sizeof(native_handle));
     numInts = NumInts();
     numFds = kNumFds;
@@ -162,11 +164,11 @@
   }
 
   static void Dump(const private_handle_t *hnd) {
-    ALOGD("handle id:%" PRIu64 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x"
-          "prod_usage:0x%" PRIx64" cons_usage:0x%" PRIx64 "format:0x%x",
+    ALOGD("handle id:%" PRIu64 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x "
+          "prod_usage:0x%" PRIx64" cons_usage:0x%" PRIx64 " format:0x%x layer_count: %d",
           hnd->id, hnd->width, hnd->height, hnd->unaligned_width, hnd->unaligned_height, hnd->size,
           hnd->fd, hnd->fd_metadata, hnd->flags, hnd->producer_usage, hnd->consumer_usage,
-          hnd->format);
+          hnd->format, hnd->layer_count);
   }
 
   int GetUnalignedWidth() const { return unaligned_width; }
@@ -175,6 +177,8 @@
 
   int GetColorFormat() const { return format; }
 
+  unsigned int GetLayerCount() const { return layer_count; }
+
   int GetStride() const {
     // In handle we currently store aligned width after allocation.
     return width;