sdm: enable gralloc1 from hwc2

Add support for hwc2 to call into gralloc1

CRs-Fixed: 2007392
Change-Id: Iade3b7cba7d3b99685530a8f4dcde67228e78f68
diff --git a/common.mk b/common.mk
index f105330..3a55cca 100644
--- a/common.mk
+++ b/common.mk
@@ -53,6 +53,7 @@
     common_flags += -isystem $(display_top)/libgralloc
 else
     common_flags += -isystem $(display_top)/libgralloc1
+    common_flags += -DUSE_GRALLOC1
 endif
 
 ifeq ($(TARGET_USES_POST_PROCESSING),true)
diff --git a/sdm/libs/hwc2/Android.mk b/sdm/libs/hwc2/Android.mk
index 57c0cf0..d4ff53c 100644
--- a/sdm/libs/hwc2/Android.mk
+++ b/sdm/libs/hwc2/Android.mk
@@ -29,14 +29,18 @@
                                  hwc_display_external.cpp \
                                  hwc_display_virtual.cpp \
                                  ../hwc/hwc_debugger.cpp \
-                                 ../hwc/hwc_buffer_allocator.cpp \
                                  ../hwc/hwc_buffer_sync_handler.cpp \
                                  hwc_color_manager.cpp \
                                  hwc_layers.cpp \
                                  hwc_callbacks.cpp \
-                                 ../hwc/blit_engine_c2d.cpp \
                                  ../hwc/cpuhint.cpp \
                                  ../hwc/hwc_socket_handler.cpp
 
+ifneq ($(TARGET_USES_GRALLOC1), true)
+    LOCAL_SRC_FILES += ../hwc/hwc_buffer_allocator.cpp
+else
+    LOCAL_SRC_FILES += hwc_buffer_allocator.cpp
+endif
+
 include $(BUILD_SHARED_LIBRARY)
 endif
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.cpp b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
new file mode 100644
index 0000000..5e3cd86
--- /dev/null
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
@@ -0,0 +1,310 @@
+/*
+* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are
+* met:
+*  * Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+*  * Redistributions in binary form must reproduce the above
+*    copyright notice, this list of conditions and the following
+*    disclaimer in the documentation and/or other materials provided
+*    with the distribution.
+*  * Neither the name of The Linux Foundation nor the names of its
+*    contributors may be used to endorse or promote products derived
+*    from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <gralloc_priv.h>
+
+#include <core/buffer_allocator.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+
+#include "hwc_buffer_allocator.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "HWCBufferAllocator"
+namespace sdm {
+
+HWCBufferAllocator::HWCBufferAllocator() {
+  int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module_);
+  if (err != 0) {
+    DLOGE("FATAL: can not open GRALLOC module");
+  } else {
+    gralloc1_open(module_, &gralloc_device_);
+  }
+  ReleaseBuffer_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
+      gralloc_device_->getFunction(gralloc_device_, GRALLOC1_FUNCTION_RELEASE));
+  Perform_ = reinterpret_cast<GRALLOC1_PFN_PERFORM>(
+      gralloc_device_->getFunction(gralloc_device_, GRALLOC1_FUNCTION_PERFORM));
+}
+
+HWCBufferAllocator::~HWCBufferAllocator() {
+  if (gralloc_device_ != nullptr) {
+    gralloc1_close(gralloc_device_);
+  }
+}
+
+DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
+  const BufferConfig &buffer_config = buffer_info->buffer_config;
+  AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
+  uint32_t width = buffer_config.width;
+  uint32_t height = buffer_config.height;
+  int format;
+  int alloc_flags = 0;
+  int error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
+  if (error != 0) {
+    return kErrorParameters;
+  }
+
+  if (buffer_config.secure) {
+    alloc_flags |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
+  }
+
+  if (!buffer_config.cache) {
+    // Allocate uncached buffers
+    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
+  }
+  uint64_t producer_usage = UINT64(alloc_flags);
+  uint64_t consumer_usage = UINT64(alloc_flags);
+  // CreateBuffer
+  private_handle_t *hnd = nullptr;
+  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER, width, height, format,
+           producer_usage, consumer_usage, &hnd);
+
+  if (hnd) {
+    alloc_buffer_info->fd = hnd->fd;
+    alloc_buffer_info->stride = UINT32(hnd->width);
+    alloc_buffer_info->size = hnd->size;
+  } else {
+    DLOGE("Failed to allocate memory");
+    return kErrorMemory;
+  }
+
+  buffer_info->private_data = reinterpret_cast<void *>(hnd);
+  return kErrorNone;
+}
+
+DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
+  DisplayError err = kErrorNone;
+  buffer_handle_t hnd = static_cast<private_handle_t *>(buffer_info->private_data);
+  ReleaseBuffer_(gralloc_device_, hnd);
+  AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
+  alloc_buffer_info->fd = -1;
+  alloc_buffer_info->stride = 0;
+  alloc_buffer_info->size = 0;
+  return err;
+}
+
+void HWCBufferAllocator::GetCustomWidthAndHeight(const private_handle_t *handle, int *width,
+                                                 int *height) {
+  Perform_(gralloc_device_, GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE, handle,
+           width, height);
+}
+
+void HWCBufferAllocator::GetAlignedWidthAndHeight(int width, int height, int format,
+                                                  uint32_t alloc_type, int *aligned_width,
+                                                  int *aligned_height) {
+  int tile_enabled;
+  gralloc1_producer_usage_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
+  gralloc1_consumer_usage_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
+  if (alloc_type & GRALLOC_USAGE_HW_FB) {
+    consumer_usage = GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
+  }
+
+  Perform_(gralloc_device_, GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES, width, height, format,
+           producer_usage, consumer_usage, aligned_width, aligned_height, &tile_enabled);
+}
+
+uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
+  const BufferConfig &buffer_config = buffer_info->buffer_config;
+  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
+
+  int width = INT(buffer_config.width);
+  int height = INT(buffer_config.height);
+  int format;
+
+  if (buffer_config.secure) {
+    alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
+  }
+
+  if (!buffer_config.cache) {
+    // Allocate uncached buffers
+    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
+  }
+
+  if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
+    return 0;
+  }
+
+  uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
+  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
+  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
+  // TODO(user): Currently both flags are treated similarly in gralloc
+  producer_usage = UINT64(alloc_flags);
+  consumer_usage = producer_usage;
+  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
+           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
+  return buffer_size;
+}
+
+int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, int *flags) {
+  switch (format) {
+    case kFormatRGBA8888:
+      *target = HAL_PIXEL_FORMAT_RGBA_8888;
+      break;
+    case kFormatRGBX8888:
+      *target = HAL_PIXEL_FORMAT_RGBX_8888;
+      break;
+    case kFormatRGB888:
+      *target = HAL_PIXEL_FORMAT_RGB_888;
+      break;
+    case kFormatRGB565:
+      *target = HAL_PIXEL_FORMAT_RGB_565;
+      break;
+    case kFormatBGR565:
+      *target = HAL_PIXEL_FORMAT_BGR_565;
+      break;
+    case kFormatBGRA8888:
+      *target = HAL_PIXEL_FORMAT_BGRA_8888;
+      break;
+    case kFormatYCrCb420PlanarStride16:
+      *target = HAL_PIXEL_FORMAT_YV12;
+      break;
+    case kFormatYCrCb420SemiPlanar:
+      *target = HAL_PIXEL_FORMAT_YCrCb_420_SP;
+      break;
+    case kFormatYCbCr420SemiPlanar:
+      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP;
+      break;
+    case kFormatYCbCr422H2V1Packed:
+      *target = HAL_PIXEL_FORMAT_YCbCr_422_I;
+      break;
+    case kFormatYCbCr422H2V1SemiPlanar:
+      *target = HAL_PIXEL_FORMAT_YCbCr_422_SP;
+      break;
+    case kFormatYCbCr420SemiPlanarVenus:
+      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
+      break;
+    case kFormatYCrCb420SemiPlanarVenus:
+      *target = HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS;
+      break;
+    case kFormatYCbCr420SPVenusUbwc:
+      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
+      break;
+    case kFormatRGBA5551:
+      *target = HAL_PIXEL_FORMAT_RGBA_5551;
+      break;
+    case kFormatRGBA4444:
+      *target = HAL_PIXEL_FORMAT_RGBA_4444;
+      break;
+    case kFormatRGBA1010102:
+      *target = HAL_PIXEL_FORMAT_RGBA_1010102;
+      break;
+    case kFormatARGB2101010:
+      *target = HAL_PIXEL_FORMAT_ARGB_2101010;
+      break;
+    case kFormatRGBX1010102:
+      *target = HAL_PIXEL_FORMAT_RGBX_1010102;
+      break;
+    case kFormatXRGB2101010:
+      *target = HAL_PIXEL_FORMAT_XRGB_2101010;
+      break;
+    case kFormatBGRA1010102:
+      *target = HAL_PIXEL_FORMAT_BGRA_1010102;
+      break;
+    case kFormatABGR2101010:
+      *target = HAL_PIXEL_FORMAT_ABGR_2101010;
+      break;
+    case kFormatBGRX1010102:
+      *target = HAL_PIXEL_FORMAT_BGRX_1010102;
+      break;
+    case kFormatXBGR2101010:
+      *target = HAL_PIXEL_FORMAT_XBGR_2101010;
+      break;
+    case kFormatYCbCr420P010:
+      *target = HAL_PIXEL_FORMAT_YCbCr_420_P010;
+      break;
+    case kFormatYCbCr420TP10Ubwc:
+      *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
+      break;
+    case kFormatRGBA8888Ubwc:
+      *target = HAL_PIXEL_FORMAT_RGBA_8888;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      break;
+    case kFormatRGBX8888Ubwc:
+      *target = HAL_PIXEL_FORMAT_RGBX_8888;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      break;
+    case kFormatBGR565Ubwc:
+      *target = HAL_PIXEL_FORMAT_BGR_565;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      break;
+    case kFormatRGBA1010102Ubwc:
+      *target = HAL_PIXEL_FORMAT_RGBA_1010102;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      break;
+    case kFormatRGBX1010102Ubwc:
+      *target = HAL_PIXEL_FORMAT_RGBX_1010102;
+      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      break;
+    default:
+      DLOGE("Unsupported format = 0x%x", format);
+      return -EINVAL;
+  }
+  return 0;
+}
+
+DisplayError HWCBufferAllocator::GetAllocatedBufferInfo(
+    const BufferConfig &buffer_config, AllocatedBufferInfo *allocated_buffer_info) {
+  // TODO(user): This API should pass the buffer_info of the already allocated buffer
+  // The private_data can then be typecast to the private_handle and used directly.
+  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
+
+  int width = INT(buffer_config.width);
+  int height = INT(buffer_config.height);
+  int format;
+
+  if (buffer_config.secure) {
+    alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
+  }
+
+  if (!buffer_config.cache) {
+    // Allocate uncached buffers
+    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
+  }
+
+  if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
+    return kErrorParameters;
+  }
+
+  uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
+  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
+  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
+  // TODO(user): Currently both flags are treated similarly in gralloc
+  producer_usage = UINT64(alloc_flags);
+  consumer_usage = producer_usage;
+  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
+           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
+  allocated_buffer_info->stride = UINT32(aligned_width);
+  allocated_buffer_info->aligned_width = UINT32(aligned_width);
+  allocated_buffer_info->aligned_height = UINT32(aligned_height);
+  allocated_buffer_info->size = UINT32(buffer_size);
+
+  return kErrorNone;
+}
+
+}  // namespace sdm
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.h b/sdm/libs/hwc2/hwc_buffer_allocator.h
new file mode 100644
index 0000000..c28a94e
--- /dev/null
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.h
@@ -0,0 +1,74 @@
+/*
+* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are
+* met:
+*  * Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+*  * Redistributions in binary form must reproduce the above
+*    copyright notice, this list of conditions and the following
+*    disclaimer in the documentation and/or other materials provided
+*    with the distribution.
+*  * Neither the name of The Linux Foundation nor the names of its
+*    contributors may be used to endorse or promote products derived
+*    from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#ifdef USE_GRALLOC1
+#ifndef __HWC_BUFFER_ALLOCATOR_H__
+#define __HWC_BUFFER_ALLOCATOR_H__
+
+#include <fcntl.h>
+#include <sys/mman.h>
+
+#include <hardware/gralloc1.h>
+#include "gralloc_priv.h"
+
+namespace sdm {
+
+template <class Type>
+inline Type ALIGN(Type x, Type align) {
+  return (x + align - 1) & ~(align - 1);
+}
+
+class HWCBufferAllocator : public BufferAllocator {
+ public:
+  HWCBufferAllocator();
+  ~HWCBufferAllocator();
+
+  DisplayError AllocateBuffer(BufferInfo *buffer_info);
+  DisplayError FreeBuffer(BufferInfo *buffer_info);
+  uint32_t GetBufferSize(BufferInfo *buffer_info);
+
+  void GetCustomWidthAndHeight(const private_handle_t *handle, int *width, int *height);
+  void GetAlignedWidthAndHeight(int width, int height, int format, uint32_t alloc_type,
+                                int *aligned_width, int *aligned_height);
+  DisplayError GetAllocatedBufferInfo(const BufferConfig &buffer_config,
+                                      AllocatedBufferInfo *allocated_buffer_info);
+  int SetBufferInfo(LayerBufferFormat format, int *target, int *flags);
+
+ private:
+  gralloc1_device_t *gralloc_device_ = nullptr;
+  const hw_module_t *module_;
+  GRALLOC1_PFN_RELEASE ReleaseBuffer_ = nullptr;
+  GRALLOC1_PFN_PERFORM Perform_ = nullptr;
+};
+
+}  // namespace sdm
+#endif  // __HWC_BUFFER_ALLOCATOR_H__
+#else
+#include "../hwc/hwc_buffer_allocator.h"
+#endif  // __HWC_BUFFER_ALLOCATOR_H__
+
diff --git a/sdm/libs/hwc2/hwc_display.cpp b/sdm/libs/hwc2/hwc_display.cpp
index 382e234..e3b0523 100644
--- a/sdm/libs/hwc2/hwc_display.cpp
+++ b/sdm/libs/hwc2/hwc_display.cpp
@@ -19,8 +19,6 @@
 
 #include <cutils/properties.h>
 #include <errno.h>
-#include <gr.h>
-#include <gralloc_priv.h>
 #include <math.h>
 #include <sync/sync.h>
 #include <utils/constants.h>
@@ -39,6 +37,9 @@
 #include "hwc_display.h"
 #include "hwc_debugger.h"
 #include "blit_engine_c2d.h"
+#ifndef USE_GRALLOC1
+#include <gr.h>
+#endif
 
 #ifdef QTI_BSP
 #include <hardware/display_defs.h>
@@ -236,21 +237,14 @@
     swap_interval_zero_ = true;
   }
 
+  buffer_allocator_ = new HWCBufferAllocator();
 
-  client_target_ = new HWCLayer(id_);
+  client_target_ = new HWCLayer(id_, buffer_allocator_);
+
   int blit_enabled = 0;
   HWCDebugHandler::Get()->GetProperty("persist.hwc.blit.comp", &blit_enabled);
   if (needs_blit_ && blit_enabled) {
-    blit_engine_ = new BlitEngineC2d();
-    if (!blit_engine_) {
-      DLOGI("Create Blit Engine C2D failed");
-    } else {
-      if (blit_engine_->Init() < 0) {
-        DLOGI("Blit Engine Init failed, Blit Composition will not be used!!");
-        delete blit_engine_;
-        blit_engine_ = NULL;
-      }
-    }
+    // TODO(user): Add blit engine when needed
   }
 
   display_intf_->GetRefreshRateRange(&min_refresh_rate_, &max_refresh_rate_);
@@ -268,10 +262,9 @@
 
   delete client_target_;
 
-  if (blit_engine_) {
-    blit_engine_->DeInit();
-    delete blit_engine_;
-    blit_engine_ = NULL;
+  if (buffer_allocator_) {
+    delete buffer_allocator_;
+    buffer_allocator_ = NULL;
   }
 
   if (color_mode_) {
@@ -284,7 +277,7 @@
 
 // LayerStack operations
 HWC2::Error HWCDisplay::CreateLayer(hwc2_layer_t *out_layer_id) {
-  HWCLayer *layer = *layer_set_.emplace(new HWCLayer(id_));
+  HWCLayer *layer = *layer_set_.emplace(new HWCLayer(id_, buffer_allocator_));
   layer_map_.emplace(std::make_pair(layer->GetId(), layer));
   *out_layer_id = layer->GetId();
   geometry_changes_ |= GeometryChanges::kAdded;
@@ -351,7 +344,11 @@
     const private_handle_t *handle =
         reinterpret_cast<const private_handle_t *>(layer->input_buffer.buffer_id);
     if (handle) {
+#ifdef USE_GRALLOC1
+      if (handle->buffer_type == BUFFER_TYPE_VIDEO) {
+#else
       if (handle->bufferType == BUFFER_TYPE_VIDEO) {
+#endif
         layer_stack_.flags.video_present = true;
       }
       // TZ Protected Buffer - L1
@@ -688,10 +685,6 @@
   dump_frame_index_ = 0;
   dump_input_layers_ = ((bit_mask_layer_type & (1 << INPUT_LAYER_DUMP)) != 0);
 
-  if (blit_engine_) {
-    blit_engine_->SetFrameDumpConfig(count);
-  }
-
   DLOGI("num_frame_dump %d, input_layer_dump_enable %d", dump_frame_count_, dump_input_layers_);
 }
 
@@ -1264,7 +1257,7 @@
 
   int aligned_width;
   int aligned_height;
-  int usage = GRALLOC_USAGE_HW_FB;
+  uint32_t usage = GRALLOC_USAGE_HW_FB;
   int format = HAL_PIXEL_FORMAT_RGBA_8888;
   int ubwc_enabled = 0;
   int flags = 0;
@@ -1273,8 +1266,14 @@
     usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
     flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
   }
-  AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format, usage,
-                                                        aligned_width, aligned_height);
+
+#ifdef USE_GRALLOC1
+  buffer_allocator_->GetAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format, usage,
+                                              &aligned_width, &aligned_height);
+#else
+  AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format,
+                                                        INT(usage), aligned_width, aligned_height);
+#endif
 
   // TODO(user): How does the dirty region get set on the client target? File bug on Google
   client_target_layer->composition = kCompositionGPUTarget;
diff --git a/sdm/libs/hwc2/hwc_display.h b/sdm/libs/hwc2/hwc_display.h
index bc3c929..d8f85bd 100644
--- a/sdm/libs/hwc2/hwc_display.h
+++ b/sdm/libs/hwc2/hwc_display.h
@@ -32,6 +32,7 @@
 #include <utility>
 #include <vector>
 
+#include "hwc_buffer_allocator.h"
 #include "hwc_callbacks.h"
 #include "hwc_layers.h"
 
@@ -229,6 +230,7 @@
 
   CoreInterface *core_intf_ = nullptr;
   HWCCallbacks *callbacks_  = nullptr;
+  HWCBufferAllocator *buffer_allocator_ = NULL;
   DisplayType type_;
   hwc2_display_t id_;
   bool needs_blit_ = false;
@@ -269,7 +271,6 @@
 
  private:
   void DumpInputBuffers(void);
-  BlitEngine *blit_engine_ = NULL;
   qService::QService *qservice_ = NULL;
   DisplayClass display_class_;
   uint32_t geometry_changes_ = GeometryChanges::kNone;
diff --git a/sdm/libs/hwc2/hwc_display_virtual.cpp b/sdm/libs/hwc2/hwc_display_virtual.cpp
index 1197c7b..46fec4e 100644
--- a/sdm/libs/hwc2/hwc_display_virtual.cpp
+++ b/sdm/libs/hwc2/hwc_display_virtual.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -31,7 +31,9 @@
 #include <utils/debug.h>
 #include <sync/sync.h>
 #include <stdarg.h>
+#ifndef USE_GRALLOC1
 #include <gr.h>
+#endif
 
 #include "hwc_display_virtual.h"
 #include "hwc_debugger.h"
@@ -189,18 +191,17 @@
     }
 
     int aligned_width, aligned_height;
-    int unaligned_width, unaligned_height;
-
+#ifdef USE_GRALLOC1
+    buffer_allocator_->GetCustomWidthAndHeight(output_handle, &aligned_width, &aligned_height);
+#else
     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(output_handle, aligned_width,
                                                           aligned_height);
-    AdrenoMemInfo::getInstance().getUnalignedWidthAndHeight(output_handle, unaligned_width,
-                                                            unaligned_height);
+#endif
 
     output_buffer_->width = UINT32(aligned_width);
     output_buffer_->height = UINT32(aligned_height);
-    output_buffer_->unaligned_width = UINT32(unaligned_width);
-    output_buffer_->unaligned_height = UINT32(unaligned_height);
-    // TODO(mkavm): Handle DRC and metadata changes
+    output_buffer_->unaligned_width = UINT32(output_handle->unaligned_width);
+    output_buffer_->unaligned_height = UINT32(output_handle->unaligned_height);
     output_buffer_->flags.secure = 0;
     output_buffer_->flags.video = 0;
 
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index c8fd163..440f047 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -18,7 +18,9 @@
  */
 
 #include "hwc_layers.h"
+#ifndef USE_GRALLOC1
 #include <gr.h>
+#endif
 #include <utils/debug.h>
 #include <cmath>
 
@@ -64,7 +66,8 @@
 }
 
 // Layer operations
-HWCLayer::HWCLayer(hwc2_display_t display_id) : id_(next_id_++), display_id_(display_id) {
+HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
+  : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
   layer_ = new Layer();
   // Fences are deferred, so the first time this layer is presented, return -1
   // TODO(user): Verify that fences are properly obtained on suspend/resume
@@ -107,23 +110,28 @@
 
   LayerBuffer *layer_buffer = &layer_->input_buffer;
   int aligned_width, aligned_height;
-  int unaligned_width, unaligned_height;
-
+#ifdef USE_GRALLOC1
+  buffer_allocator_->GetCustomWidthAndHeight(handle, &aligned_width, &aligned_height);
+#else
   AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(handle, aligned_width, aligned_height);
-  AdrenoMemInfo::getInstance().getUnalignedWidthAndHeight(handle, unaligned_width,
-                                                          unaligned_height);
+#endif
 
   layer_buffer->width = UINT32(aligned_width);
   layer_buffer->height = UINT32(aligned_height);
-  layer_buffer->unaligned_width = UINT32(unaligned_width);
-  layer_buffer->unaligned_height = UINT32(unaligned_height);
+  layer_buffer->unaligned_width = UINT32(handle->unaligned_width);
+  layer_buffer->unaligned_height = UINT32(handle->unaligned_height);
 
   layer_buffer->format = GetSDMFormat(handle->format, handle->flags);
   if (SetMetaData(handle, layer_) != kErrorNone) {
     return HWC2::Error::BadLayer;
   }
 
-  if (handle->bufferType == BUFFER_TYPE_VIDEO) {
+#ifdef USE_GRALLOC1
+  // TODO(user): Clean this up
+  if (handle->buffer_type == BUFFER_TYPE_VIDEO) {
+#else
+    if (handle->bufferType == BUFFER_TYPE_VIDEO) {
+#endif
     layer_buffer->flags.video = true;
   }
   // TZ Protected Buffer - L1
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index 6a80b1b..f9cb23e 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -31,9 +31,11 @@
 #include <hardware/hwcomposer2.h>
 #undef HWC2_INCLUDE_STRINGIFICATION
 #undef HWC2_USE_CPP11
-#include <set>
 #include <map>
 #include <queue>
+#include <set>
+#include "core/buffer_allocator.h"
+#include "hwc_buffer_allocator.h"
 
 namespace sdm {
 
@@ -54,7 +56,7 @@
 
 class HWCLayer {
  public:
-  explicit HWCLayer(hwc2_display_t display_id);
+  explicit HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator);
   ~HWCLayer();
   uint32_t GetZ() const { return z_; }
   hwc2_layer_t GetId() const { return id_; }
@@ -89,6 +91,7 @@
   static std::atomic<hwc2_layer_t> next_id_;
   std::queue<int32_t> release_fences_;
   int ion_fd_ = -1;
+  HWCBufferAllocator *buffer_allocator_ = NULL;
 
   // Composition requested by client(SF)
   HWC2::Composition client_requested_ = HWC2::Composition::Device;
diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp
index ee4a6e7..cc780d8 100644
--- a/sdm/libs/hwc2/hwc_session.cpp
+++ b/sdm/libs/hwc2/hwc_session.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -28,8 +28,6 @@
 #include <sys/prctl.h>
 #include <binder/Parcel.h>
 #include <QService.h>
-#include <gr.h>
-#include <gralloc_priv.h>
 #include <display_config.h>
 #include <utils/debug.h>
 #include <sync/sync.h>