libgralloc: Store unaligned buffer resolution in private handle.

Add unaligned_width and unaligned_height in private_handle_t to store
the buffer resolution without alignment that client asked to allocate.

Change-Id: I28d757af4178f581e6a83dc06198106c85fc7262
CRs-Fixed: 1040942
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index 0ef0a9a..8d43f98 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -180,6 +180,18 @@
 
 }
 
+void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
+                                               int& unaligned_h) {
+    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
+    if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
+        unaligned_w = metadata->bufferDim.sliceWidth;
+        unaligned_h = metadata->bufferDim.sliceHeight;
+    } else {
+        unaligned_w = hnd->unaligned_width;
+        unaligned_h = hnd->unaligned_height;
+    }
+}
+
 bool isUncompressedRgbFormat(int format)
 {
     bool is_rgb_format = false;
@@ -709,7 +721,6 @@
     return size;
 }
 
-
 void getBufferAttributes(int width, int height, int format, int usage,
         int& alignedw, int &alignedh, int& tiled, unsigned int& size)
 {
@@ -892,7 +903,7 @@
 
     private_handle_t* hnd = new private_handle_t(data.fd, data.size,
                                                  data.allocType, 0, format,
-                                                 alignedw, alignedh);
+                                                 alignedw, alignedh, -1, 0, 0, w, h);
     hnd->base = (uint64_t) data.base;
     hnd->offset = data.offset;
     hnd->gpuaddr = 0;
diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp
index 5b22e6d..8bae926 100644
--- a/libgralloc/gpu.cpp
+++ b/libgralloc/gpu.cpp
@@ -53,6 +53,16 @@
 {
     int err = 0;
     int flags = 0;
+    int alignedw = 0;
+    int alignedh = 0;
+
+    AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
+            height,
+            format,
+            usage,
+            alignedw,
+            alignedh);
+
     size = roundUpToPageSize(size);
     alloc_data data;
     data.offset = 0;
@@ -153,8 +163,8 @@
         flags |= data.allocType;
         uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
         private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
-                bufferType, format, width, height, eData.fd, eData.offset,
-                eBaseAddr);
+                bufferType, format, alignedw, alignedh,
+                eData.fd, eData.offset, eBaseAddr, width, height);
 
         hnd->offset = data.offset;
         hnd->base = (uint64_t)(data.base) + data.offset;
@@ -327,7 +337,7 @@
         err = gralloc_alloc_framebuffer(usage, pHandle);
     } else {
         err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
-                                   grallocFormat, alignedw, alignedh);
+                                   grallocFormat, w, h);
     }
 
     if (err < 0) {
diff --git a/libgralloc/gr.h b/libgralloc/gr.h
index 578240a..3324840 100644
--- a/libgralloc/gr.h
+++ b/libgralloc/gr.h
@@ -140,6 +140,15 @@
                             int tileEnabled, int& alignedw, int &alignedh);
 
     /*
+     * Function to compute unaligned width and unaligned height based on
+     * private handle
+     *
+     * @return unaligned width, unaligned height
+     */
+    void getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
+                            int& unaligned_h);
+
+    /*
      * Function to return whether GPU support MacroTile feature
      *
      * @return >0 : supported
diff --git a/libgralloc/gralloc_priv.h b/libgralloc/gralloc_priv.h
index 613c066..336ac60 100644
--- a/libgralloc/gralloc_priv.h
+++ b/libgralloc/gralloc_priv.h
@@ -233,9 +233,11 @@
         // The gpu address mapped into the mmu.
         uint64_t gpuaddr __attribute__((aligned(8)));
         int     format;
-        int     width;
-        int     height;
+        int     width;   // holds aligned width of the actual buffer allocated
+        int     height;  // holds aligned height of the  actual buffer allocated
         uint64_t base_metadata __attribute__((aligned(8)));
+        int unaligned_width;   // holds width client asked to allocate
+        int unaligned_height;  // holds height client asked to allocate
 
 #ifdef __cplusplus
         static const int sNumFds = 2;
@@ -246,18 +248,40 @@
         static const int sMagic = 'gmsm';
 
         private_handle_t(int fd, unsigned int size, int flags, int bufferType,
-                         int format, int width, int height, int eFd = -1,
-                         unsigned int eOffset = 0, uint64_t eBase = 0) :
-            fd(fd), fd_metadata(eFd), magic(sMagic),
+                int format, int width, int height) :
+            fd(fd), fd_metadata(-1), magic(sMagic),
             flags(flags), size(size), offset(0), bufferType(bufferType),
-            base(0), offset_metadata(eOffset), gpuaddr(0),
+            base(0), offset_metadata(0), gpuaddr(0),
             format(format), width(width), height(height),
-            base_metadata(eBase)
+            base_metadata(0), unaligned_width(width),
+            unaligned_height(height)
         {
             version = (int) sizeof(native_handle);
             numInts = sNumInts();
             numFds = sNumFds;
         }
+
+        private_handle_t(int fd, unsigned int size, int flags, int bufferType,
+                int format, int width, int height,
+                int eFd, unsigned int eOffset, uint64_t eBase) :
+            private_handle_t(fd, size, flags, bufferType, format, width, height)
+        {
+            fd_metadata = eFd;
+            offset_metadata = eOffset;
+            base_metadata = eBase;
+        }
+
+        private_handle_t(int fd, unsigned int size, int flags, int bufferType,
+                int format, int width, int height,
+                int eFd, unsigned int eOffset, uint64_t eBase,
+                int unaligned_w, int unaligned_h) :
+            private_handle_t(fd, size, flags, bufferType, format, width, height,
+                    eFd, eOffset, eBase)
+        {
+            unaligned_width = unaligned_w;
+            unaligned_height = unaligned_h;
+        }
+
         ~private_handle_t() {
             magic = 0;
         }
diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp
index 142586b..d8880ef 100644
--- a/libgralloc/mapper.cpp
+++ b/libgralloc/mapper.cpp
@@ -309,6 +309,7 @@
                 int width = va_arg(args, int);
                 int height = va_arg(args, int);
                 int format = va_arg(args, int);
+                int alignedw = 0, alignedh = 0;
 
                 native_handle_t** handle = va_arg(args, native_handle_t**);
                 private_handle_t* hnd = (private_handle_t*)native_handle_create(
@@ -321,8 +322,12 @@
                   hnd->offset = offset;
                   hnd->base = uint64_t(base) + offset;
                   hnd->gpuaddr = 0;
-                  hnd->width = width;
-                  hnd->height = height;
+                  AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
+                          height, format, 0, alignedw, alignedh);
+                  hnd->width = alignedw;
+                  hnd->height = alignedh;
+                  hnd->unaligned_width = width;
+                  hnd->unaligned_height = height;
                   hnd->format = format;
                   *handle = (native_handle_t *)hnd;
                   res = 0;
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 33abd40..9bba82b 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -361,8 +361,8 @@
   return flags;
 }
 
-int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int real_w,
-                                  int real_h, int format, int bufferType,
+int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
+                                  int unaligned_h, int format, int bufferType,
                                   gralloc1_producer_usage_t prod_usage,
                                   gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) {
   int err = 0;
@@ -405,7 +405,7 @@
   uint64_t eBaseAddr = (uint64_t)(e_data.base) + e_data.offset;
   private_handle_t *hnd = new private_handle_t(data.fd, size, flags, bufferType, format, aligned_w,
                                                aligned_h, e_data.fd, e_data.offset, eBaseAddr,
-                                               real_w, real_h, prod_usage, cons_usage);
+                                               unaligned_w, unaligned_h, prod_usage, cons_usage);
 
   hnd->offset = data.offset;
   hnd->base = (uint64_t)(data.base) + data.offset;
@@ -493,6 +493,7 @@
       private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
           native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
       if (hnd) {
+        unsigned int alignedw = 0, alignedh = 0;
         hnd->magic = private_handle_t::kMagic;
         hnd->fd = fd;
         hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
@@ -500,8 +501,12 @@
         hnd->offset = offset;
         hnd->base = uint64_t(base) + offset;
         hnd->gpuaddr = 0;
-        hnd->width = width;
-        hnd->height = height;
+        BufferDescriptor descriptor(width, height, format);
+        allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
+        hnd->unaligned_width = width;
+        hnd->unaligned_height = height;
+        hnd->width = alignedw;
+        hnd->height = alignedh;
         hnd->format = format;
         *handle = reinterpret_cast<native_handle_t *>(hnd);
       }
diff --git a/libgralloc1/gr_buf_mgr.h b/libgralloc1/gr_buf_mgr.h
index d3c0e67..ed6b591 100644
--- a/libgralloc1/gr_buf_mgr.h
+++ b/libgralloc1/gr_buf_mgr.h
@@ -49,9 +49,10 @@
   int GetBufferType(int format);
   int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
                      unsigned int bufferSize = 0);
-  int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int real_w, int real_h,
-                     int format, int bufferType, gralloc1_producer_usage_t prod_usage,
-                     gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle);
+  int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
+                     int unaligned_h, int format, int bufferType,
+                     gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
+                     buffer_handle_t *handle);
   int GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
                        gralloc1_consumer_usage_t cons_usage);
   int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp
index 6258941..f837ee2 100644
--- a/libgralloc1/gr_device_impl.cpp
+++ b/libgralloc1/gr_device_impl.cpp
@@ -298,8 +298,8 @@
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
   if (status == GRALLOC1_ERROR_NONE) {
     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
-    *outWidth = UINT(hnd->GetRealWidth());
-    *outHeight = UINT(hnd->GetRealHeight());
+    *outWidth = UINT(hnd->GetUnalignedWidth());
+    *outHeight = UINT(hnd->GetUnalignedHeight());
   }
 
   return status;
diff --git a/libgralloc1/gr_priv_handle.h b/libgralloc1/gr_priv_handle.h
index 444fc80..ee38b4d 100644
--- a/libgralloc1/gr_priv_handle.h
+++ b/libgralloc1/gr_priv_handle.h
@@ -86,8 +86,8 @@
   uint64_t base_metadata __attribute__((aligned(8)));
 
   // added for gralloc1
-  int real_width;   // holds width client asked to allocate
-  int real_height;  // holds height client asked to allocate// holds width client asked to allocate
+  int unaligned_width;   // holds width client asked to allocate
+  int unaligned_height;  // holds height client asked to allocate
   gralloc1_producer_usage_t producer_usage __attribute__((aligned(8)));
   gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8)));
 
@@ -99,33 +99,50 @@
   }
 
   private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
-                   int height, int meta_fd = -1, unsigned int meta_offset = 0,
-                   uint64_t meta_base = 0, int rw = 0, int rh = 0,
-                   gralloc1_producer_usage_t prod_usage = GRALLOC1_PRODUCER_USAGE_NONE,
-                   gralloc1_consumer_usage_t cons_usage = GRALLOC1_CONSUMER_USAGE_NONE)
+                   int height)
       : fd(fd),
-        fd_metadata(meta_fd),
+        fd_metadata(-1),
         magic(kMagic),
         flags(flags),
         size(size),
         offset(0),
         buffer_type(buf_type),
         base(0),
-        offset_metadata(meta_offset),
+        offset_metadata(0),
         gpuaddr(0),
         format(format),
         width(width),
         height(height),
-        base_metadata(meta_base),
-        real_width(rw),
-        real_height(rh),
-        producer_usage(prod_usage),
-        consumer_usage(cons_usage) {
+        base_metadata(0),
+        unaligned_width(width),
+        unaligned_height(height),
+        producer_usage(GRALLOC1_PRODUCER_USAGE_NONE),
+        consumer_usage(GRALLOC1_CONSUMER_USAGE_NONE) {
     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;
+  }
+
   ~private_handle_t() {
     magic = 0;
     ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this);
@@ -151,9 +168,9 @@
     return 0;
   }
 
-  int GetRealWidth() const { return real_width; }
+  int GetUnalignedWidth() const { return unaligned_width; }
 
-  int GetRealHeight() const { return real_height; }
+  int GetUnalignedHeight() const { return unaligned_height; }
 
   int GetColorFormat() const { return format; }