drm_hwcomposer: Use GraphicBufferMapper for copy buffer handle

Due to gralloc0/gralloc1 API incompatibility(register/unregister
vs retain/release) it is preferred to use generic
GraphicBufferMapper API for copy buffer handle. It will use
an appropriate adapter for the present version of gralloc.
For the reference: hardware/interfaces/graphics/mapper

Change-Id: Ia4740ed3b2ffd43fa3c107eb22d792eaabd4df40
Signed-off-by: Andrii Chepurnyi <andrii_chepurnyi@epam.com>
diff --git a/drmhwcomposer.h b/drmhwcomposer.h
index cd631b2..1723fcb 100644
--- a/drmhwcomposer.h
+++ b/drmhwcomposer.h
@@ -81,13 +81,10 @@
  public:
   DrmHwcNativeHandle() = default;
 
-  DrmHwcNativeHandle(const gralloc_module_t *gralloc, native_handle_t *handle)
-      : gralloc_(gralloc), handle_(handle) {
+  DrmHwcNativeHandle(native_handle_t *handle) : handle_(handle) {
   }
 
   DrmHwcNativeHandle(DrmHwcNativeHandle &&rhs) {
-    gralloc_ = rhs.gralloc_;
-    rhs.gralloc_ = NULL;
     handle_ = rhs.handle_;
     rhs.handle_ = NULL;
   }
@@ -96,14 +93,12 @@
 
   DrmHwcNativeHandle &operator=(DrmHwcNativeHandle &&rhs) {
     Clear();
-    gralloc_ = rhs.gralloc_;
-    rhs.gralloc_ = NULL;
     handle_ = rhs.handle_;
     rhs.handle_ = NULL;
     return *this;
   }
 
-  int CopyBufferHandle(buffer_handle_t handle, const gralloc_module_t *gralloc);
+  int CopyBufferHandle(buffer_handle_t handle);
 
   void Clear();
 
@@ -112,7 +107,6 @@
   }
 
  private:
-  const gralloc_module_t *gralloc_ = NULL;
   native_handle_t *handle_ = NULL;
 };
 
@@ -148,7 +142,7 @@
   UniqueFd acquire_fence;
   OutputFd release_fence;
 
-  int ImportBuffer(Importer *importer, const gralloc_module_t *gralloc);
+  int ImportBuffer(Importer *importer);
 
   void SetTransform(int32_t sf_transform);
   void SetSourceCrop(hwc_frect_t const &crop);
diff --git a/drmhwctwo.cpp b/drmhwctwo.cpp
index 1940964..7ca3e62 100644
--- a/drmhwctwo.cpp
+++ b/drmhwctwo.cpp
@@ -514,7 +514,7 @@
   for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
     DrmHwcLayer layer;
     l.second->PopulateDrmLayer(&layer);
-    int ret = layer.ImportBuffer(importer_.get(), gralloc_);
+    int ret = layer.ImportBuffer(importer_.get());
     if (ret) {
       ALOGE("Failed to import layer, ret=%d", ret);
       return HWC2::Error::NoResources;
diff --git a/hwcutils.cpp b/hwcutils.cpp
index c1685c7..7ba382a 100644
--- a/hwcutils.cpp
+++ b/hwcutils.cpp
@@ -21,6 +21,7 @@
 #include "platform.h"
 
 #include <log/log.h>
+#include <ui/GraphicBufferMapper.h>
 
 namespace android {
 
@@ -58,51 +59,18 @@
   return 0;
 }
 
-static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
-  native_handle_t *new_handle =
-      native_handle_create(handle->numFds, handle->numInts);
-  if (new_handle == NULL)
-    return NULL;
-
-  const int *old_data = handle->data;
-  int *new_data = new_handle->data;
-  for (int i = 0; i < handle->numFds; i++) {
-    *new_data = dup(*old_data);
-    old_data++;
-    new_data++;
-  }
-  memcpy(new_data, old_data, sizeof(int) * handle->numInts);
-
-  return new_handle;
-}
-
-static void free_buffer_handle(native_handle_t *handle) {
-  int ret = native_handle_close(handle);
-  if (ret)
-    ALOGE("Failed to close native handle %d", ret);
-  ret = native_handle_delete(handle);
-  if (ret)
-    ALOGE("Failed to delete native handle %d", ret);
-}
-
-int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
-                                         const gralloc_module_t *gralloc) {
-  native_handle_t *handle_copy = dup_buffer_handle(handle);
-  if (handle_copy == NULL) {
-    ALOGE("Failed to duplicate handle");
-    return -ENOMEM;
-  }
-
-  int ret = gralloc->registerBuffer(gralloc, handle_copy);
+int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
+  native_handle_t *handle_copy;
+  GraphicBufferMapper &gm(GraphicBufferMapper::get());
+  int ret =
+      gm.importBuffer(handle, const_cast<buffer_handle_t *>(&handle_copy));
   if (ret) {
-    ALOGE("Failed to register buffer handle %d", ret);
-    free_buffer_handle(handle_copy);
+    ALOGE("Failed to import buffer handle %d", ret);
     return ret;
   }
 
   Clear();
 
-  gralloc_ = gralloc;
   handle_ = handle_copy;
 
   return 0;
@@ -113,21 +81,22 @@
 }
 
 void DrmHwcNativeHandle::Clear() {
-  if (gralloc_ != NULL && handle_ != NULL) {
-    gralloc_->unregisterBuffer(gralloc_, handle_);
-    free_buffer_handle(handle_);
-    gralloc_ = NULL;
+  if (handle_ != NULL) {
+    GraphicBufferMapper &gm(GraphicBufferMapper::get());
+    int ret = gm.freeBuffer(handle_);
+    if (ret) {
+      ALOGE("Failed to free buffer handle %d", ret);
+    }
     handle_ = NULL;
   }
 }
 
-int DrmHwcLayer::ImportBuffer(Importer *importer,
-                              const gralloc_module_t *gralloc) {
+int DrmHwcLayer::ImportBuffer(Importer *importer) {
   int ret = buffer.ImportBuffer(sf_handle, importer);
   if (ret)
     return ret;
 
-  ret = handle.CopyBufferHandle(sf_handle, gralloc);
+  ret = handle.CopyBufferHandle(sf_handle);
   if (ret)
     return ret;