drm_hwcomposer: Add refcount for GEM handle

The linux kernel doesn't provide reference counting for the handle
returned by FD_TO_HANDLE ioctl. It means that if the same Gralloc buffer
is imported twice, the first GEM_CLOSE will destroy the handle even if it
is still in use by another import.

Remedy this issue by doing the reference counting directly in the DRM
generic platform support: ImportHandle() will increase the reference,
while ReleaseHandle() will decrease and close it, if necessary.

Signed-off-by: Vincent Donnefort <vincent.donnefort@arm.com>
diff --git a/platform/platformdrmgeneric.h b/platform/platformdrmgeneric.h
index 7bb2ea2..a7cf941 100644
--- a/platform/platformdrmgeneric.h
+++ b/platform/platformdrmgeneric.h
@@ -21,6 +21,7 @@
 #include "platform.h"
 
 #include <hardware/gralloc.h>
+#include <map>
 
 #include <drm/drm_fourcc.h>
 
@@ -40,6 +41,8 @@
   int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
   int ReleaseBuffer(hwc_drm_bo_t *bo) override;
   bool CanImportBuffer(buffer_handle_t handle) override;
+  int ImportHandle(uint32_t gem_handle);
+  int ReleaseHandle(uint32_t gem_handle);
 
   uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
   uint32_t DrmFormatToBitsPerPixel(uint32_t drm_format);
@@ -50,6 +53,9 @@
  private:
   const gralloc_module_t *gralloc_;
   bool exclude_non_hwfb_;
+
+  int CloseHandle(uint32_t gem_handle);
+  std::map<uint32_t, int> gem_refcount_;
 };
 }  // namespace android