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_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);