gralloc: Add allocator and mapper HALs

Implement the hidl .hal interfaces directly instead of using the
default shims.
This is required since new additions to .hal will no longer be
added to the legacy gralloc1 C header.

Change-Id: If577a14f75d7d13da0ff656c96ab451d21c910ce
diff --git a/gralloc/gr_device_impl.cpp b/gralloc/gr_device_impl.cpp
index c900d23..6129bd3 100644
--- a/gralloc/gr_device_impl.cpp
+++ b/gralloc/gr_device_impl.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -29,19 +29,19 @@
 
 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 #include <cutils/log.h>
-#include <utils/Trace.h>
 #include <cutils/trace.h>
 #include <sync/sync.h>
+#include <utils/Trace.h>
 #include <algorithm>
 #include <sstream>
 #include <string>
 
-#include "gr_device_impl.h"
 #include "gr_buf_descriptor.h"
-#include "gralloc_priv.h"
-#include "qd_utils.h"
-#include "qdMetaData.h"
+#include "gr_device_impl.h"
 #include "gr_utils.h"
+#include "gralloc_priv.h"
+#include "qdMetaData.h"
+#include "qd_utils.h"
 
 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
 
@@ -50,23 +50,25 @@
 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
 
 struct gralloc_module_t HAL_MODULE_INFO_SYM = {
-  .common = {
-    .tag = HARDWARE_MODULE_TAG,
-    .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
-    .hal_api_version    = HARDWARE_HAL_API_VERSION,
-    .id = GRALLOC_HARDWARE_MODULE_ID,
-    .name = "Graphics Memory Module",
-    .author = "Code Aurora Forum",
-    .methods = &gralloc_module_methods,
-    .dso = 0,
-    .reserved = {0},
-  },
+    // clang-format off
+    .common = {
+            .tag = HARDWARE_MODULE_TAG,
+            .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
+            .hal_api_version = HARDWARE_HAL_API_VERSION,
+            .id = GRALLOC_HARDWARE_MODULE_ID,
+            .name = "Graphics Memory Module",
+            .author = "Code Aurora Forum",
+            .methods = &gralloc_module_methods,
+            .dso = 0,
+            .reserved = {0},
+        },
+    // clang-format on
 };
 
 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
   int status = -EINVAL;
   if (module && device && !strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
-    gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
+    gralloc::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc::GrallocImpl::GetInstance(module);
     *device = reinterpret_cast<hw_device_t *>(dev);
     if (dev) {
       status = 0;
@@ -77,7 +79,9 @@
   return status;
 }
 
-namespace gralloc1 {
+namespace gralloc {
+
+std::atomic<uint64_t> GrallocImpl::next_descriptor_id_(1);
 
 GrallocImpl::GrallocImpl(const hw_module_t *module) {
   common.tag = HARDWARE_DEVICE_TAG;
@@ -87,7 +91,58 @@
   getFunction = GetFunction;
   getCapabilities = GetCapabilities;
 
-  initalized_ = Init();
+  initialized_ = Init();
+}
+
+inline gralloc1_error_t ToError(Error error) {
+  switch (error) {
+    case Error::NONE:
+      return GRALLOC1_ERROR_NONE;
+    case Error::BAD_DESCRIPTOR:
+      return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+    case Error::BAD_BUFFER:
+      return GRALLOC1_ERROR_BAD_HANDLE;
+    case Error::BAD_VALUE:
+      return GRALLOC1_ERROR_BAD_VALUE;
+    case Error::NO_RESOURCES:
+      return GRALLOC1_ERROR_NO_RESOURCES;
+    case Error::UNSUPPORTED:
+    default:
+      return GRALLOC1_ERROR_UNSUPPORTED;
+  }
+}
+
+static uint64_t ProducerUsageToBufferUsage(gralloc1_producer_usage_t producer_usage) {
+  uint64_t usage = producer_usage & ~(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
+                                      GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
+  if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) ==
+      GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) {
+    usage |= BufferUsage::CPU_READ_OFTEN;
+  } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) ==
+             GRALLOC1_PRODUCER_USAGE_CPU_READ) {
+    usage |= BufferUsage::CPU_READ_RARELY;
+  }
+
+  if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) ==
+      GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
+    usage |= BufferUsage::CPU_WRITE_OFTEN;
+  } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) ==
+             GRALLOC1_PRODUCER_USAGE_CPU_WRITE) {
+    usage |= BufferUsage::CPU_WRITE_RARELY;
+  }
+  return usage;
+}
+
+static uint64_t ConsumerUsageToBufferUsage(gralloc1_consumer_usage_t consumer_usage) {
+  uint64_t usage = consumer_usage & ~(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
+  if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
+      GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
+    usage |= BufferUsage::CPU_READ_OFTEN;
+  } else if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) ==
+             GRALLOC1_CONSUMER_USAGE_CPU_READ) {
+    usage |= BufferUsage::CPU_READ_RARELY;
+  }
+  return usage;
 }
 
 bool GrallocImpl::Init() {
@@ -95,7 +150,26 @@
   return buf_mgr_ != nullptr;
 }
 
-GrallocImpl::~GrallocImpl() {
+GrallocImpl::~GrallocImpl() {}
+
+gralloc1_error_t GrallocImpl::CreateBufferDescriptorLocked(
+    gralloc1_buffer_descriptor_t *descriptor_id) {
+  std::lock_guard<std::mutex> lock(descriptor_lock_);
+  auto descriptor = std::make_shared<BufferDescriptor>(next_descriptor_id_++);
+  *descriptor_id = static_cast<gralloc1_buffer_descriptor_t>(descriptor->GetId());
+  descriptors_map_.emplace(*descriptor_id, descriptor);
+  return GRALLOC1_ERROR_NONE;
+}
+
+gralloc1_error_t GrallocImpl::DestroyBufferDescriptorLocked(
+    gralloc1_buffer_descriptor_t descriptor_id) {
+  std::lock_guard<std::mutex> lock(descriptor_lock_);
+  const auto descriptor = descriptors_map_.find(descriptor_id);
+  if (descriptor == descriptors_map_.end()) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  }
+  descriptors_map_.erase(descriptor);
+  return GRALLOC1_ERROR_NONE;
 }
 
 int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
@@ -104,7 +178,7 @@
 }
 
 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
-                                  int32_t  /*gralloc1_capability_t*/ *out_capabilities) {
+                                  int32_t /*gralloc1_capability_t*/ *out_capabilities) {
   if (device != nullptr && out_count != nullptr) {
     if (out_capabilities != nullptr && *out_count >= 3) {
       out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
@@ -216,8 +290,8 @@
   if (!device || !out_descriptor) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
-  GrallocImpl const *dev = GRALLOC_IMPL(device);
-  return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
+  auto *dev = reinterpret_cast<GrallocImpl *>(device);
+  return dev->CreateBufferDescriptorLocked(out_descriptor);
 }
 
 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
@@ -225,8 +299,8 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
-  GrallocImpl const *dev = GRALLOC_IMPL(device);
-  return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
+  auto *dev = reinterpret_cast<GrallocImpl *>(device);
+  return dev->DestroyBufferDescriptorLocked(descriptor);
 }
 
 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
@@ -235,9 +309,9 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   } else {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
-                                                       &BufferDescriptor::SetConsumerUsage, usage);
+    auto *dev = reinterpret_cast<GrallocImpl *>(device);
+    return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetUsage,
+                                             ConsumerUsageToBufferUsage(usage));
   }
 }
 
@@ -247,10 +321,9 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   } else {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
-                                                       &BufferDescriptor::SetDimensions,
-                                                       INT(width), INT(height));
+    auto *dev = reinterpret_cast<GrallocImpl *>(device);
+    return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetDimensions,
+                                             INT(width), INT(height));
   }
 }
 
@@ -260,9 +333,8 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   } else {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
-                                                       &BufferDescriptor::SetColorFormat, format);
+    auto *dev = reinterpret_cast<GrallocImpl *>(device);
+    return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetColorFormat, format);
   }
 }
 
@@ -272,10 +344,9 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   } else {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
-                                                       &BufferDescriptor::SetLayerCount,
-                                                       layer_count);
+    auto *dev = reinterpret_cast<GrallocImpl *>(device);
+    return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetLayerCount,
+                                             layer_count);
   }
 }
 
@@ -285,9 +356,9 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   } else {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
-                                                       &BufferDescriptor::SetProducerUsage, usage);
+    auto *dev = reinterpret_cast<GrallocImpl *>(device);
+    return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetUsage,
+                                             ProducerUsageToBufferUsage(usage));
   }
 }
 
@@ -307,7 +378,7 @@
                                                gralloc1_consumer_usage_t *outUsage) {
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
   if (status == GRALLOC1_ERROR_NONE) {
-    *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
+    *outUsage = static_cast<gralloc1_consumer_usage_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
   }
 
   return status;
@@ -353,8 +424,7 @@
 
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
   if (status == GRALLOC1_ERROR_NONE) {
-    const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
-    *outUsage = hnd->GetProducerUsage();
+    *outUsage = static_cast<gralloc1_producer_usage_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
   }
 
   return status;
@@ -374,6 +444,28 @@
   return status;
 }
 
+gralloc1_error_t GrallocImpl::AllocateBuffer(const gralloc1_buffer_descriptor_t *descriptor_ids,
+                                             buffer_handle_t *out_buffers) {
+  gralloc1_error_t status = GRALLOC1_ERROR_NONE;
+
+  // Validate descriptor
+  std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
+  std::shared_ptr<gralloc::BufferDescriptor> descriptor;
+  const auto map_descriptor = descriptors_map_.find(descriptor_ids[0]);
+  if (map_descriptor == descriptors_map_.end()) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    descriptor = map_descriptor->second;
+  }
+
+  // Allocate separate buffer for each descriptor
+  if (buf_mgr_->AllocateBuffer(*descriptor, &out_buffers[0]) != Error::NONE) {
+    return GRALLOC1_ERROR_NO_RESOURCES;
+  }
+
+  return status;
+}
+
 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
                                               const gralloc1_buffer_descriptor_t *descriptors,
                                               buffer_handle_t *out_buffers) {
@@ -385,9 +477,12 @@
     return GRALLOC1_ERROR_BAD_VALUE;
   }
 
-  GrallocImpl const *dev = GRALLOC_IMPL(device);
-  gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
-                                                           out_buffers);
+  if (num_descriptors != 1) {
+    return GRALLOC1_ERROR_UNSUPPORTED;
+  }
+
+  auto *dev = reinterpret_cast<GrallocImpl *>(device);
+  gralloc1_error_t status = dev->AllocateBuffer(descriptors, out_buffers);
 
   return status;
 }
@@ -397,7 +492,7 @@
   if (status == GRALLOC1_ERROR_NONE) {
     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
     GrallocImpl const *dev = GRALLOC_IMPL(device);
-    status = dev->buf_mgr_->RetainBuffer(hnd);
+    status = ToError(dev->buf_mgr_->RetainBuffer(hnd));
   }
 
   return status;
@@ -410,7 +505,50 @@
 
   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
   GrallocImpl const *dev = GRALLOC_IMPL(device);
-  return dev->buf_mgr_->ReleaseBuffer(hnd);
+  return ToError(dev->buf_mgr_->ReleaseBuffer(hnd));
+}
+
+gralloc1_error_t GrallocImpl::GetFlexLayout(const private_handle_t *hnd,
+                                            struct android_flex_layout *layout) {
+  if (!IsYuvFormat(hnd)) {
+    return GRALLOC1_ERROR_UNSUPPORTED;
+  }
+
+  android_ycbcr yuvPlaneInfo[2];
+  int err = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
+
+  if (err != 0) {
+    return GRALLOC1_ERROR_BAD_HANDLE;
+  }
+
+  layout->format = FLEX_FORMAT_YCbCr;
+  layout->num_planes = 3;
+
+  for (uint32_t i = 0; i < layout->num_planes; i++) {
+    layout->planes[i].bits_per_component = 8;
+    layout->planes[i].bits_used = 8;
+    layout->planes[i].h_increment = 1;
+    layout->planes[i].v_increment = 1;
+    layout->planes[i].h_subsampling = 2;
+    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);
+
+  layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
+  layout->planes[1].component = FLEX_COMPONENT_Cb;
+  layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
+  layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
+
+  layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
+  layout->planes[2].component = FLEX_COMPONENT_Cr;
+  layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
+  layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
+  return GRALLOC1_ERROR_NONE;
 }
 
 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
@@ -421,9 +559,12 @@
 
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
   if (status == GRALLOC1_ERROR_NONE) {
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
-    status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
+    if (!IsYuvFormat(hnd)) {
+      status = GRALLOC1_ERROR_UNSUPPORTED;
+    } else {
+      *out_num_planes = 3;
+    }
   }
   return status;
 }
@@ -469,8 +610,8 @@
     // return GRALLOC1_ERROR_BAD_VALUE;
   }
 
-  // TODO(user): Need to check if buffer was allocated with the same flags
-  status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
+  status = ToError(dev->buf_mgr_->LockBuffer(
+      hnd, ProducerUsageToBufferUsage(prod_usage) | ConsumerUsageToBufferUsage(cons_usage)));
   *out_data = reinterpret_cast<void *>(hnd->base);
 
   return status;
@@ -487,16 +628,16 @@
     return GRALLOC1_ERROR_BAD_VALUE;
   }
 
-  void *out_data {};
+  void *out_data{};
   gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
                                                     &out_data, acquire_fence);
   if (status != GRALLOC1_ERROR_NONE) {
     return status;
   }
 
-  GrallocImpl const *dev = GRALLOC_IMPL(device);
+  auto *dev = reinterpret_cast<GrallocImpl *>(device);
   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
-  dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
+  dev->GetFlexLayout(hnd, out_flex_layout);
   return status;
 }
 
@@ -516,7 +657,200 @@
 
   *release_fence = -1;
 
-  return dev->buf_mgr_->UnlockBuffer(hnd);
+  return ToError(dev->buf_mgr_->UnlockBuffer(hnd));
+}
+
+static gralloc1_error_t Perform(int operation, va_list args) {
+  switch (operation) {
+    case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
+      int width = va_arg(args, int);
+      int format = va_arg(args, int);
+      int *stride = va_arg(args, int *);
+      unsigned int alignedw = 0, alignedh = 0;
+
+      if (!stride) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      BufferInfo info(width, width, format);
+      GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
+      *stride = INT(alignedw);
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *stride = va_arg(args, int *);
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!stride) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      BufferDim_t buffer_dim;
+      if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
+        *stride = buffer_dim.sliceWidth;
+      } else {
+        *stride = hnd->width;
+      }
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *stride = va_arg(args, int *);
+      int *height = va_arg(args, int *);
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!stride || !height) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      GetCustomDimensions(hnd, stride, height);
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
+      int width = va_arg(args, int);
+      int height = va_arg(args, int);
+      int format = va_arg(args, int);
+      uint64_t usage = va_arg(args, uint64_t);
+      usage |= va_arg(args, uint64_t);
+
+      int *aligned_width = va_arg(args, int *);
+      int *aligned_height = va_arg(args, int *);
+      int *tile_enabled = va_arg(args, int *);
+      if (!aligned_width || !aligned_height || !tile_enabled) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      unsigned int alignedw, alignedh;
+      BufferInfo info(width, height, format, usage);
+      *tile_enabled = IsUBwcEnabled(format, usage);
+      GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
+      *aligned_width = INT(alignedw);
+      *aligned_height = INT(alignedh);
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *color_space = va_arg(args, int *);
+
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!color_space) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      *color_space = 0;
+      GetColorSpaceFromMetadata(hnd, color_space);
+    } break;
+    case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!ycbcr) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      if (GetYUVPlaneInfo(hnd, ycbcr)) {
+        return GRALLOC1_ERROR_UNDEFINED;
+      }
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *map_secure_buffer = va_arg(args, int *);
+
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!map_secure_buffer) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
+        *map_secure_buffer = 0;
+      }
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *flag = va_arg(args, int *);
+
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!flag) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
+      int linear_format = 0;
+      if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) {
+        if (linear_format) {
+          *flag = 0;
+        }
+      }
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      void **rgb_data = va_arg(args, void **);
+
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!rgb_data) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      if (GetRgbDataAddress(hnd, rgb_data)) {
+        return GRALLOC1_ERROR_UNDEFINED;
+      }
+    } break;
+
+    case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      int *flag = va_arg(args, int *);
+
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+
+      if (!flag) {
+        return GRALLOC1_ERROR_BAD_VALUE;
+      }
+
+      if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
+        *flag = 0;
+      }
+    } break;
+
+    case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: {
+      private_handle_t *hnd = va_arg(args, private_handle_t *);
+      uint32_t *enable = va_arg(args, uint32_t *);
+      if (private_handle_t::validate(hnd) != 0) {
+        return GRALLOC1_ERROR_BAD_HANDLE;
+      }
+      if (setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable) != 0) {
+        return GRALLOC1_ERROR_UNSUPPORTED;
+      }
+    } break;
+
+    default:
+      break;
+  }
+  return GRALLOC1_ERROR_NONE;
 }
 
 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
@@ -526,11 +860,10 @@
 
   va_list args;
   va_start(args, operation);
-  GrallocImpl const *dev = GRALLOC_IMPL(device);
-  gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
+  gralloc1_error_t err = Perform(operation, args);
   va_end(args);
 
   return err;
 }
 
-}  // namespace gralloc1
+}  // namespace gralloc