gralloc1: Add remaining functionality

* Use private_0 flag
* Add some legacy flags to be used during transition
* Fix gralloc module version
* Adjust makefiles for gralloc1
* Add support for dump, num flex planes and lock flex.
* Use a unique ID for buffer descriptors
* Do not delete buffer handle, fix after verifying framework fix.
* Current gralloc1 clients do not conform to the lock() requirement
in the gralloc1 header. Tracked in b/33588773
* Add perform APIs to get the buffer size  and allocate a buffer
for use by SDM
* Fix reference counting
* Add a unique buffer ID
* Some cleanup in private_handle
* Create a wrapper class of private_handle to do refcounting
* Resolve implementation defined formats At allocation time, update buffer
descriptors to replace implementation defined formats with the ones we mean
to allocate
* Defer ion handle release
* Remove unused drm code.
* Add legacy constructor for private_handle_t used by some
clients

CRs-Fixed: 2007391
Change-Id: Ia9e816cec35ba45483a5b75d99f256325a010138
diff --git a/libgralloc1/gr_priv_handle.h b/libgralloc1/gr_priv_handle.h
index 8a01d38..61190c2 100644
--- a/libgralloc1/gr_priv_handle.h
+++ b/libgralloc1/gr_priv_handle.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution
  *
  * Copyright (C) 2008 The Android Open Source Project
@@ -22,6 +22,7 @@
 
 #include <cutils/log.h>
 #include <hardware/gralloc1.h>
+#include <hardware/gralloc.h>
 
 #define GRALLOC1_FUNCTION_PERFORM 0x00001000
 
@@ -29,20 +30,15 @@
 
 typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...);
 
-typedef int BackStoreFd;
-
 #define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp)
 
 struct private_handle_t : public native_handle_t {
-  // TODO(user): Moving PRIV_FLAGS to #defs & check for each PRIV_FLAG and remove unused.
   enum {
     PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
     PRIV_FLAGS_USES_ION = 0x00000008,
-    PRIV_FLAGS_USES_ASHMEM = 0x00000010,
     PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
     PRIV_FLAGS_INTERNAL_ONLY = 0x00000040,
     PRIV_FLAGS_NON_CPU_WRITER = 0x00000080,
-    PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100,
     PRIV_FLAGS_CACHED = 0x00000200,
     PRIV_FLAGS_SECURE_BUFFER = 0x00000400,
     PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000,
@@ -59,36 +55,31 @@
     PRIV_FLAGS_TILE_RENDERED = 0x02000000,
     PRIV_FLAGS_CPU_RENDERED = 0x04000000,
     PRIV_FLAGS_UBWC_ALIGNED = 0x08000000,
-    PRIV_FLAGS_DISP_CONSUMER = 0x10000000
+    PRIV_FLAGS_DISP_CONSUMER = 0x10000000,
+    PRIV_FLAGS_CLIENT_ALLOCATED = 0x20000000,   // Ion buffer allocated outside of gralloc
   };
 
-  // file-descriptors
+  // file-descriptors dup'd over IPC
   int fd;
   int fd_metadata;
 
-  // ints
+  // values sent over IPC
   int magic;
   int flags;
-  unsigned int size;
-  unsigned int offset;
-  int buffer_type;
-  uint64_t base __attribute__((aligned(8)));
-  unsigned int offset_metadata;
-
-  // The gpu address mapped into the mmu.
-  uint64_t gpuaddr __attribute__((aligned(8)));
-
-  int format;
-  int width;   // holds width of the actual buffer allocated
-  int height;  // holds height of the  actual buffer allocated
-
-  int stride;
-  uint64_t base_metadata __attribute__((aligned(8)));
-  unsigned int fb_id;
-
-  // added for gralloc1
+  int width;        // holds width of the actual buffer allocated
+  int height;       // holds height of the  actual buffer allocated
   int unaligned_width;   // holds width client asked to allocate
   int unaligned_height;  // holds height client asked to allocate
+  int format;
+  int buffer_type;
+  unsigned int size;
+  unsigned int offset;
+  unsigned int offset_metadata;
+  unsigned int fb_id;
+  uint64_t base                            __attribute__((aligned(8)));
+  uint64_t base_metadata                   __attribute__((aligned(8)));
+  uint64_t gpuaddr                         __attribute__((aligned(8)));
+  uint64_t id                              __attribute__((aligned(8)));
   gralloc1_producer_usage_t producer_usage __attribute__((aligned(8)));
   gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8)));
 
@@ -96,58 +87,57 @@
   static const int kMagic = 'gmsm';
 
   static inline int NumInts() {
-    return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds;
+    return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int))
+        - kNumFds;
   }
 
-  private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
-                   int height)
+  private_handle_t(int fd,
+                   int meta_fd,
+                   int flags,
+                   int width,
+                   int height,
+                   int uw,
+                   int uh,
+                   int format,
+                   int buf_type,
+                   unsigned int size,
+                   gralloc1_producer_usage_t prod_usage = GRALLOC1_PRODUCER_USAGE_NONE,
+                   gralloc1_consumer_usage_t cons_usage = GRALLOC1_CONSUMER_USAGE_NONE)
       : fd(fd),
-        fd_metadata(-1),
+        fd_metadata(meta_fd),
         magic(kMagic),
         flags(flags),
-        size(size),
-        offset(0),
-        buffer_type(buf_type),
-        base(0),
-        offset_metadata(0),
-        gpuaddr(0),
-        format(format),
         width(width),
         height(height),
+        unaligned_width(uw),
+        unaligned_height(uh),
+        format(format),
+        buffer_type(buf_type),
+        size(size),
+        offset(0),
+        offset_metadata(0),
+        fb_id(0),
+        base(0),
         base_metadata(0),
-        unaligned_width(width),
-        unaligned_height(height),
-        producer_usage(GRALLOC1_PRODUCER_USAGE_NONE),
-        consumer_usage(GRALLOC1_CONSUMER_USAGE_NONE),
-        fb_id(0) {
+        gpuaddr(0),
+        id(0),
+        producer_usage(prod_usage),
+        consumer_usage(cons_usage) {
     version = static_cast<int>(sizeof(native_handle));
     numInts = NumInts();
     numFds = kNumFds;
   }
 
-  private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
-                   int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base)
-      : private_handle_t(fd, size, flags, buf_type, format, width, height) {
-    fd_metadata = meta_fd;
-    offset_metadata = meta_offset;
-    base_metadata = meta_base;
-  }
-
-  private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
-                   int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base,
-                   int unaligned_w , int unaligned_h,
-                   gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage)
-      : private_handle_t(fd, size, flags, buf_type, format, width, height, meta_fd, meta_offset
-                         meta_base) {
-    unaligned_width = unaligned_w;
-    unaligned_height = unaligned_h;
-    producer_usage = prod_usage;
-    consumer_usage = cons_usage;
+// Legacy constructor used by some clients
+  private_handle_t(int fd, unsigned int size, int usage, int buf_type, int format, int w, int h)
+  : private_handle_t(fd, -1, PRIV_FLAGS_CLIENT_ALLOCATED, w, h, 0, 0, format, buf_type, size,
+                     static_cast<gralloc1_producer_usage_t>(usage),
+                     static_cast<gralloc1_consumer_usage_t>(usage)) {
   }
 
   ~private_handle_t() {
     magic = 0;
-    ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this);
+    ALOGE_IF(DBG_HANDLE, "Deleting buffer handle %p", this);
   }
 
   static int validate(const native_handle *h) {
@@ -177,8 +167,7 @@
   int GetColorFormat() const { return format; }
 
   int GetStride() const {
-    // In handle we are storing aligned width after allocation.
-    // Why GetWidth & GetStride?? Are we supposed to maintain unaligned values??
+    // In handle we currently store aligned width after allocation.
     return width;
   }
 
@@ -186,7 +175,7 @@
 
   gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; }
 
-  BackStoreFd GetBackingstore() const { return fd; }
+  int GetBackingstore() const { return fd; }
 };
 
 #endif  // __GR_PRIV_HANDLE_H__