Gralloc: Make size calculation consistent at all places

Fix size calculation by consistently using adreno or gralloc calculation APIs

Change-Id: Ic0cef22d6f99a17d7ce392d02a21edc812cd907b
CRs-Fixed: 2262355
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index 41f590d..3d4bfac 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -254,16 +254,6 @@
   return status;
 }
 
-int BufferManager::GetBufferType(int inputFormat) {
-  int buffer_type = BUFFER_TYPE_UI;
-  if (IsYuvFormat(inputFormat)) {
-    // Video format
-    buffer_type = BUFFER_TYPE_VIDEO;
-  }
-
-  return buffer_type;
-}
-
 Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
                                     unsigned int bufferSize) {
   if (!handle)
@@ -282,15 +272,8 @@
   info.format = format;
   info.layer_count = layer_count;
 
-  bool use_adreno_for_size = false;
   GraphicsMetadata graphics_metadata = {};
-
-  use_adreno_for_size = CanUseAdrenoForSize(buffer_type, usage);
-  if (use_adreno_for_size) {
-    GetGpuResourceSizeAndDimensions(info, &size, &alignedw, &alignedh, &graphics_metadata);
-  } else {
-    GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
-  }
+  GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh, &graphics_metadata);
 
   size = (bufferSize >= size) ? bufferSize : size;
   int err = 0;
@@ -337,6 +320,7 @@
   ColorSpace_t colorSpace = (buffer_type == BUFFER_TYPE_VIDEO) ? ITU_R_601 : ITU_R_709;
   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
 
+  bool use_adreno_for_size = CanUseAdrenoForSize(buffer_type, usage);
   if (use_adreno_for_size) {
     setMetaData(hnd, SET_GRAPHICS_METADATA, reinterpret_cast<void *>(&graphics_metadata));
   }
diff --git a/gralloc/gr_buf_mgr.h b/gralloc/gr_buf_mgr.h
index ca0c1b3..6c5f365 100644
--- a/gralloc/gr_buf_mgr.h
+++ b/gralloc/gr_buf_mgr.h
@@ -53,7 +53,6 @@
  private:
   BufferManager();
   Error MapBuffer(private_handle_t const *hnd);
-  int GetBufferType(int format);
 
   // Imports the ion fds into the current process. Returns an error for invalid handles
   Error ImportHandleLocked(private_handle_t *hnd);
diff --git a/gralloc/gr_utils.cpp b/gralloc/gr_utils.cpp
index 10bbf27..7c480dc 100644
--- a/gralloc/gr_utils.cpp
+++ b/gralloc/gr_utils.cpp
@@ -319,8 +319,19 @@
 
 void GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
                                 unsigned int *alignedh) {
-  GetAlignedWidthAndHeight(info, alignedw, alignedh);
-  *size = GetSize(info, *alignedw, *alignedh);
+  GraphicsMetadata graphics_metadata = {};
+  GetBufferSizeAndDimensions(info, size, alignedw, alignedh, &graphics_metadata);
+}
+
+void GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
+                                unsigned int *alignedh, GraphicsMetadata *graphics_metadata) {
+  int buffer_type = GetBufferType(info.format);
+  if (CanUseAdrenoForSize(buffer_type, info.usage)) {
+    GetGpuResourceSizeAndDimensions(info, size, alignedw, alignedh, graphics_metadata);
+  } else {
+    GetAlignedWidthAndHeight(info, alignedw, alignedh);
+    *size = GetSize(info, *alignedw, *alignedh);
+  }
 }
 
 void GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, int color_format,
@@ -1157,4 +1168,8 @@
   return 0;
 }
 
+int GetBufferType(int inputFormat) {
+  return IsYuvFormat(inputFormat) ? BUFFER_TYPE_VIDEO : BUFFER_TYPE_UI;
+}
+
 }  // namespace gralloc
diff --git a/gralloc/gr_utils.h b/gralloc/gr_utils.h
index 9f21f5b..d273754 100644
--- a/gralloc/gr_utils.h
+++ b/gralloc/gr_utils.h
@@ -78,6 +78,8 @@
 unsigned int GetSize(const BufferInfo &d, unsigned int alignedw, unsigned int alignedh);
 void GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
                                 unsigned int *alignedh);
+void GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
+                                unsigned int *alignedh, GraphicsMetadata *graphics_metadata);
 void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height);
 void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space);
 void GetAlignedWidthAndHeight(const BufferInfo &d, unsigned int *aligned_w,
@@ -112,6 +114,7 @@
 uint64_t GetHandleFlags(int format, uint64_t usage);
 int GetImplDefinedFormat(uint64_t usage, int format);
 int GetCustomFormatFlags(int format, uint64_t usage, int *custom_format, uint64_t *priv_flags);
+int GetBufferType(int inputFormat);
 }  // namespace gralloc
 
 #endif  // __GR_UTILS_H__