Merge " sdm: do resource_impl stop before resource_impl postcommit"
diff --git a/Android.mk b/Android.mk
index 3471e0a..aa34895 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,10 +1,9 @@
-ifneq ($(TARGET_DISABLE_DISPLAY),true)
 sdm-libs := sdm/libs
-display-hals := include $(sdm-libs)/utils $(sdm-libs)/core libdebug
+display-hals := include $(sdm-libs)/utils $(sdm-libs)/core libdebug gpu_tonemapper
 
 ifneq ($(TARGET_IS_HEADLESS), true)
     display-hals += libcopybit liblight libmemtrack hdmi_cec \
-                    gpu_tonemapper libdrmutils
+                    libdrmutils
 endif
 
 display-hals += gralloc
@@ -18,4 +17,3 @@
     include $(call all-named-subdir-makefiles,$(display-hals))
 endif
 endif
-endif #TARGET_DISABLE_DISPLAY
diff --git a/composer/Android.mk b/composer/Android.mk
index 79a2d9c..765a3e4 100644
--- a/composer/Android.mk
+++ b/composer/Android.mk
@@ -21,7 +21,7 @@
                                  liblog libfmq libhardware_legacy \
                                  libsdmcore libqservice libqdutils libqdMetaData \
                                  libdisplaydebug libsdmutils libgrallocutils libui \
-                                 libEGL libGLESv2 libGLESv3 \
+                                 libgpu_tonemapper libEGL libGLESv2 libGLESv3 \
                                  vendor.qti.hardware.display.composer@1.0 \
                                  vendor.qti.hardware.display.composer@2.0 \
                                  android.hardware.graphics.composer@2.1 \
@@ -47,11 +47,6 @@
                                  vendor.display.config@1.12 \
                                  vendor.display.config@1.13
 
-ifneq ($(TARGET_IS_HEADLESS), true)
-    LOCAL_SHARED_LIBRARIES += libgpu_tonemapper
-endif
-
-
 LOCAL_SRC_FILES               := QtiComposer.cpp QtiComposerClient.cpp service.cpp \
                                  QtiComposerHandleImporter.cpp \
                                  hwc_session.cpp \
diff --git a/composer/gl_color_convert.cpp b/composer/gl_color_convert.cpp
index 03f7377..f398059 100644
--- a/composer/gl_color_convert.cpp
+++ b/composer/gl_color_convert.cpp
@@ -35,7 +35,7 @@
 namespace sdm {
 
 GLColorConvert* GLColorConvert::GetInstance(GLRenderTarget target, bool secure) {
-  GLColorConvertImpl* color_convert =  new GLColorConvertImpl(target, secure);
+  GLColorConvertImpl* color_convert = new GLColorConvertImpl(target, secure);
   if (color_convert == nullptr) {
     DLOGE("Failed to create color convert instance for %d target %d secure", target, secure);
     return nullptr;
diff --git a/composer/gl_common.cpp b/composer/gl_common.cpp
index c350d47..aa3bc45 100644
--- a/composer/gl_common.cpp
+++ b/composer/gl_common.cpp
@@ -147,12 +147,16 @@
   return fd;
 }
 
-void GLCommon::DestroyContext(const GLContext* ctx) {
+void GLCommon::DestroyContext(GLContext* ctx) {
   DTRACE_SCOPED();
+
+  // Clear egl image buffers.
+  image_wrapper_.Deinit();
+
+  EGL(DeleteProgram(ctx->program_id));
   EGL(eglMakeCurrent(ctx->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
   EGL(eglDestroySurface(ctx->egl_display, ctx->egl_surface));
   EGL(eglDestroyContext(ctx->egl_display, ctx->egl_context));
-  EGL(DeleteProgram(ctx->program_id));
   EGL(eglTerminate(ctx->egl_display));
 }
 
diff --git a/composer/gl_common.h b/composer/gl_common.h
index 79af479..c77988b 100644
--- a/composer/gl_common.h
+++ b/composer/gl_common.h
@@ -60,7 +60,7 @@
   virtual void SetProgram(uint32_t id);
   virtual void SetDestinationBuffer(const private_handle_t *dst_hnd, const GLRect &dst_rect);
   virtual void SetSourceBuffer(const private_handle_t *src_hnd);
-  virtual void DestroyContext(const GLContext *ctx);
+  virtual void DestroyContext(GLContext *ctx);
   virtual void DeleteProgram(uint32_t id);
   virtual int WaitOnInputFence(int in_fence_fd);
   virtual int CreateOutputFence();
diff --git a/composer/hwc_callbacks.cpp b/composer/hwc_callbacks.cpp
index 857716b..e36a60f 100644
--- a/composer/hwc_callbacks.cpp
+++ b/composer/hwc_callbacks.cpp
@@ -105,9 +105,6 @@
       return HWC2::Error::BadParameter;
   }
 
-  if (!pointer) {
-    return HWC2::Error::NoResources;
-  }
   return HWC2::Error::None;
 }
 
diff --git a/composer/hwc_display_builtin.cpp b/composer/hwc_display_builtin.cpp
index b7b6515..439170a 100644
--- a/composer/hwc_display_builtin.cpp
+++ b/composer/hwc_display_builtin.cpp
@@ -1103,6 +1103,7 @@
   if (!is_primary_) {
     // Disable on all non-primary builtins.
     DLOGI("Non-primary builtin.");
+    disable_layer_stitch_ = true;
     return true;
   }
 
diff --git a/composer/hwc_layers.cpp b/composer/hwc_layers.cpp
index 101c3f0..eb0687e 100644
--- a/composer/hwc_layers.cpp
+++ b/composer/hwc_layers.cpp
@@ -963,11 +963,6 @@
 }
 
 void HWCLayer::ValidateAndSetCSC(const private_handle_t *handle) {
-  if (per_frame_hdr_metadata_) {
-    // Since client has set PerFrameMetadata, dataspace will be valid
-    // so we can skip reading from ColorMetaData.
-    return;
-  }
   LayerBuffer *layer_buffer = &layer_->input_buffer;
   bool use_color_metadata = true;
   ColorMetaData csc = {};
@@ -997,7 +992,9 @@
      use_color_metadata = true;
   }
 
-  if (use_color_metadata) {
+  // Since client has set PerFrameMetadata, dataspace will be valid
+  // so we can skip reading from ColorMetaData.
+  if (use_color_metadata && !per_frame_hdr_metadata_) {
     ColorMetaData new_metadata = {};
     if (sdm::SetCSC(handle, &new_metadata) == kErrorNone) {
       // If dataspace is KNOWN, overwrite the gralloc metadata CSC using the previously derived CSC
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 2c2fc7e..82836ce 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -805,6 +805,9 @@
         callbacks_.Hotplug(client_id, HWC2::Connection::Connected);
       }
     }
+  }
+
+  if (descriptor == HWC2_CALLBACK_HOTPLUG) {
     client_connected_ = !!pointer;
     // Notfify all displays.
     NotifyClientStatus(client_connected_);
diff --git a/composer/hwc_session_services.cpp b/composer/hwc_session_services.cpp
index 1881c26..26e2e5a 100644
--- a/composer/hwc_session_services.cpp
+++ b/composer/hwc_session_services.cpp
@@ -1048,6 +1048,7 @@
 
       if (release_fence >= 0) {
         status = sync_wait(release_fence, 1000);
+        close(release_fence);
       } else {
         DLOGE("CWB release fence could not be retrieved.");
         status = -1;
diff --git a/config/display-product.mk b/config/display-product.mk
index 97654d5..1ba5f53 100644
--- a/config/display-product.mk
+++ b/config/display-product.mk
@@ -49,6 +49,12 @@
 #QDCM calibration xml file for td4330 panel
 PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_bengal_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_td4330_v2_cmd_mode_dsi_truly_panel.xml
 PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_bengal_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_td4330_v2_video_mode_dsi_truly_panel.xml
+#QDCM calibration xml file for Sharp fhd panel
+PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_Sharp_fhd_cmd_mode_qsync_dsi_panel.xml
+PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_Sharp_fhd_video_mode_qsync_dsi_panel.xml
+#QDCM calibration xml file for Sharp 2k panel
+PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_Sharp_2k_cmd_mode_qsync_dsi_panel.xml
+PRODUCT_COPY_FILES += hardware/qcom/display/config/qdcm_calib_data_default.xml:$(TARGET_COPY_OUT_VENDOR)/etc/qdcm_calib_data_Sharp_2k_video_mode_qsync_dsi_panel.xml
 
 PRODUCT_PROPERTY_OVERRIDES += \
     persist.demo.hdmirotationlock=false \
diff --git a/gpu_tonemapper/Android.mk b/gpu_tonemapper/Android.mk
index ec59eef..fe98897 100644
--- a/gpu_tonemapper/Android.mk
+++ b/gpu_tonemapper/Android.mk
@@ -1,7 +1,6 @@
 LOCAL_PATH := $(call my-dir)
 include $(LOCAL_PATH)/../common.mk
 
-ifneq ($(TARGET_IS_HEADLESS), true)
 include $(CLEAR_VARS)
 LOCAL_COPY_HEADERS_TO     := $(common_header_export_path)
 LOCAL_COPY_HEADERS        := TonemapFactory.h Tonemapper.h
@@ -28,4 +27,3 @@
                              Tonemapper.cpp
 
 include $(BUILD_SHARED_LIBRARY)
-endif
diff --git a/gpu_tonemapper/EGLImageWrapper.cpp b/gpu_tonemapper/EGLImageWrapper.cpp
index 19bf093..560fd5a 100644
--- a/gpu_tonemapper/EGLImageWrapper.cpp
+++ b/gpu_tonemapper/EGLImageWrapper.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -32,36 +32,6 @@
 
 static string pidString = std::to_string(getpid());
 
-#ifndef TARGET_ION_ABI_VERSION
-//-----------------------------------------------------------------------------
-static void free_ion_cookie(int ion_fd, int cookie)
-//-----------------------------------------------------------------------------
-{
-  if (ion_fd && !ioctl(ion_fd, ION_IOC_FREE, &cookie)) {
-  } else {
-      ALOGE("ION_IOC_FREE failed: ion_fd = %d, cookie = %d", ion_fd, cookie);
-  }
-}
-
-//-----------------------------------------------------------------------------
-static int get_ion_cookie(int ion_fd, int fd)
-//-----------------------------------------------------------------------------
-{
-  int cookie = fd;
-
-  struct ion_fd_data fdData;
-  memset(&fdData, 0, sizeof(fdData));
-  fdData.fd = fd;
-
-  if (ion_fd && !ioctl(ion_fd, ION_IOC_IMPORT, &fdData)) {
-       cookie = fdData.handle;
-  } else {
-       ALOGE("ION_IOC_IMPORT failed: ion_fd = %d, fd = %d", ion_fd, fd);
-  }
-
-  return cookie;
-}
-#else
 //-----------------------------------------------------------------------------
 static string get_ion_buff_str(int buff_fd)
 //-----------------------------------------------------------------------------
@@ -80,7 +50,6 @@
 
   return retStr;
 }
-#endif
 
 //-----------------------------------------------------------------------------
 void EGLImageWrapper::DeleteEGLImageCallback::operator()(int& buffInt, EGLImageBuffer*& eglImage)
@@ -90,9 +59,6 @@
     delete eglImage;
   }
 
-#ifndef TARGET_ION_ABI_VERSION
-  free_ion_cookie(ion_fd, buffInt /* cookie */);
-#else
   if (!mapClearPending) {
     for (auto it = buffStrbuffIntMapPtr->begin(); it != buffStrbuffIntMapPtr->end(); it++) {
       if (it->second == buffInt /* counter */) {
@@ -101,7 +67,6 @@
       }
     }
   }
-#endif
 }
 
 //-----------------------------------------------------------------------------
@@ -111,17 +76,19 @@
   eglImageBufferCache = new android::LruCache<int, EGLImageBuffer*>(32);
   callback = new DeleteEGLImageCallback(&buffStrbuffIntMap);
   eglImageBufferCache->setOnEntryRemovedListener(callback);
-
-#ifndef TARGET_ION_ABI_VERSION
-  ion_fd = open("/dev/ion", O_RDONLY);
-  callback->ion_fd = ion_fd;
-#endif
 }
 
 //-----------------------------------------------------------------------------
 EGLImageWrapper::~EGLImageWrapper()
 //-----------------------------------------------------------------------------
 {
+  Deinit();
+}
+
+//-----------------------------------------------------------------------------
+void EGLImageWrapper::Deinit()
+//-----------------------------------------------------------------------------
+{
   if (eglImageBufferCache != 0) {
     if (callback != 0) {
       callback->mapClearPending = true;
@@ -137,12 +104,6 @@
     callback = 0;
   }
 
-#ifndef TARGET_ION_ABI_VERSION
-  if (ion_fd > 0) {
-    close(ion_fd);
-    ion_fd = -1;
-  }
-#endif
 }
 
 //-----------------------------------------------------------------------------
@@ -180,17 +141,6 @@
 {
   const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle);
 
-#ifndef TARGET_ION_ABI_VERSION
-  int ion_cookie = get_ion_cookie(ion_fd, src->fd);
-  EGLImageBuffer* eglImage = nullptr;
-  eglImage = eglImageBufferCache->get(ion_cookie);
-  if (eglImage == 0) {
-    eglImage = L_wrap(src);
-    eglImageBufferCache->put(ion_cookie, eglImage);
-  } else {
-    free_ion_cookie(ion_fd, ion_cookie);
-  }
-#else
   string buffStr = get_ion_buff_str(src->fd);
   EGLImageBuffer* eglImage = nullptr;
   if (!buffStr.empty()) {
@@ -206,7 +156,6 @@
   } else {
     ALOGE("Could not provide an eglImage for fd = %d, EGLImageWrapper = %p", src->fd, this);
   }
-#endif
 
   return eglImage;
 }
diff --git a/gpu_tonemapper/EGLImageWrapper.h b/gpu_tonemapper/EGLImageWrapper.h
index ce1b344..a0a7522 100644
--- a/gpu_tonemapper/EGLImageWrapper.h
+++ b/gpu_tonemapper/EGLImageWrapper.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -21,9 +21,6 @@
 #define __TONEMAPPER_EGLIMAGEWRAPPER_H__
 
 #include <utils/LruCache.h>
-#ifndef TARGET_HEADLESS
-#include <linux/msm_ion.h>
-#endif
 #include <string>
 #include <map>
 #include "EGLImageBuffer.h"
@@ -39,24 +36,18 @@
      void operator()(int& buffInt, EGLImageBuffer*& eglImage);
      map<string, int>* buffStrbuffIntMapPtr = nullptr;
      bool mapClearPending = false;
-   #ifndef TARGET_ION_ABI_VERSION
-     int ion_fd = -1;
-   #endif
   };
 
   android::LruCache<int, EGLImageBuffer *>* eglImageBufferCache;
   map<string, int> buffStrbuffIntMap = {};
   DeleteEGLImageCallback* callback = 0;
- #ifndef TARGET_ION_ABI_VERSION
-   int ion_fd = -1;
- #else
-   uint64_t buffInt = 0;
- #endif
+  uint64_t buffInt = 0;
 
  public:
   EGLImageWrapper();
   ~EGLImageWrapper();
   EGLImageBuffer* wrap(const void *pvt_handle);
+  void Deinit();
 };
 
 #endif  // __TONEMAPPER_EGLIMAGEWRAPPER_H__
diff --git a/gralloc/QtiMapperExtensions.cpp b/gralloc/QtiMapperExtensions.cpp
index cace7ba..33cd23d 100644
--- a/gralloc/QtiMapperExtensions.cpp
+++ b/gralloc/QtiMapperExtensions.cpp
@@ -344,7 +344,7 @@
                                                   getFormatLayout_cb hidl_cb) {
   ALOGD_IF(DEBUG, "%s: Input parameters - wxh: %dx%d usage: 0x%" PRIu64 " format: %d", __FUNCTION__,
            width, height, usage, format);
-  auto err = Error::BAD_BUFFER;
+  auto err = Error::NONE;
   hidl_vec<PlaneLayout> plane_info;
   unsigned int alignedw = 0, alignedh = 0;
   int plane_count = 0;
@@ -353,34 +353,41 @@
   BufferInfo info(width, height, custom_format, usage);
   gralloc::GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
   size = gralloc::GetSize(info, alignedw, alignedh);
+  gralloc::PlaneLayoutInfo plane_layout[8] = {};
   ALOGD_IF(DEBUG, "%s: Aligned width and height - wxh: %ux%u custom_format = %d", __FUNCTION__,
            alignedw, alignedh, custom_format);
   if (gralloc::IsYuvFormat(custom_format)) {
-    gralloc::PlaneLayoutInfo yuv_plane_info[8] = {};
     gralloc::GetYUVPlaneInfo(info, custom_format, alignedw, alignedh, flags, &plane_count,
-                             yuv_plane_info);
-    ALOGD_IF(DEBUG, "%s: Number of plane - %d, custom_format - %d", __FUNCTION__, plane_count,
-             custom_format);
-    plane_info.resize(plane_count);
-    for (int i = 0; i < plane_count; i++) {
-      plane_info[i].component = yuv_plane_info[i].component;
-      plane_info[i].h_subsampling = yuv_plane_info[i].h_subsampling;
-      plane_info[i].v_subsampling = yuv_plane_info[i].v_subsampling;
-      plane_info[i].offset = yuv_plane_info[i].offset;
-      plane_info[i].pixel_increment = yuv_plane_info[i].step;
-      plane_info[i].stride = yuv_plane_info[i].stride;
-      plane_info[i].stride_bytes = yuv_plane_info[i].stride_bytes;
-      plane_info[i].scanlines = yuv_plane_info[i].scanlines;
-      plane_info[i].size = yuv_plane_info[i].size;
-      ALOGD_IF(DEBUG, "%s: plane info: component - %d", __FUNCTION__, plane_info[i].component);
-      ALOGD_IF(DEBUG, "h_subsampling - %u, v_subsampling - %u, offset - %u, pixel_increment - %d",
-               plane_info[i].h_subsampling, plane_info[i].v_subsampling, plane_info[i].offset,
-               plane_info[i].pixel_increment);
-      ALOGD_IF(DEBUG, "stride_pixel - %d, stride_bytes - %d, scanlines - %d, size - %u",
-               plane_info[i].stride, plane_info[i].stride_bytes, plane_info[i].scanlines,
-               plane_info[i].size);
-    }
-    err = Error::NONE;
+                             plane_layout);
+  } else if (gralloc::IsUncompressedRGBFormat(custom_format) ||
+             gralloc::IsCompressedRGBFormat(custom_format)) {
+    gralloc::GetRGBPlaneInfo(info, custom_format, alignedw, alignedh, flags, &plane_count,
+                             plane_layout);
+  } else {
+    err = Error::BAD_BUFFER;
+    hidl_cb(err, size, plane_info);
+    return Void();
+  }
+  ALOGD_IF(DEBUG, "%s: Number of plane - %d, custom_format - %d", __FUNCTION__, plane_count,
+           custom_format);
+  plane_info.resize(plane_count);
+  for (int i = 0; i < plane_count; i++) {
+    plane_info[i].component = plane_layout[i].component;
+    plane_info[i].h_subsampling = plane_layout[i].h_subsampling;
+    plane_info[i].v_subsampling = plane_layout[i].v_subsampling;
+    plane_info[i].offset = plane_layout[i].offset;
+    plane_info[i].pixel_increment = plane_layout[i].step;
+    plane_info[i].stride = plane_layout[i].stride;
+    plane_info[i].stride_bytes = plane_layout[i].stride_bytes;
+    plane_info[i].scanlines = plane_layout[i].scanlines;
+    plane_info[i].size = plane_layout[i].size;
+    ALOGD_IF(DEBUG, "%s: plane info: component - %d", __FUNCTION__, plane_info[i].component);
+    ALOGD_IF(DEBUG, "h_subsampling - %u, v_subsampling - %u, offset - %u, pixel_increment - %d",
+             plane_info[i].h_subsampling, plane_info[i].v_subsampling, plane_info[i].offset,
+             plane_info[i].pixel_increment);
+    ALOGD_IF(DEBUG, "stride_pixel - %d, stride_bytes - %d, scanlines - %d, size - %u",
+             plane_info[i].stride, plane_info[i].stride_bytes, plane_info[i].scanlines,
+             plane_info[i].size);
   }
   hidl_cb(err, size, plane_info);
   return Void();
diff --git a/gralloc/gr_allocator.cpp b/gralloc/gr_allocator.cpp
index ef350b6..5b7efaa 100644
--- a/gralloc/gr_allocator.cpp
+++ b/gralloc/gr_allocator.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2019, 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
@@ -32,6 +32,10 @@
 #include <algorithm>
 #include <vector>
 
+#ifndef QMAA
+#include <linux/msm_ion.h>
+#endif
+
 #include "gr_allocator.h"
 #include "gr_utils.h"
 #include "gralloc_priv.h"
diff --git a/gralloc/gr_ion_alloc.cpp b/gralloc/gr_ion_alloc.cpp
index c9a74ce..68d3083 100644
--- a/gralloc/gr_ion_alloc.cpp
+++ b/gralloc/gr_ion_alloc.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2019, 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,15 +31,9 @@
 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 #include <sys/ioctl.h>
 #include <sys/mman.h>
-#include <linux/ion.h>
-#ifndef QMAA
-#include <linux/msm_ion.h>
-#endif
 
-#if TARGET_ION_ABI_VERSION >= 2
 #include <linux/dma-buf.h>
 #include <ion/ion.h>
-#endif
 #include <stdlib.h>
 #include <fcntl.h>
 #include <log/log.h>
@@ -68,8 +62,6 @@
   return true;
 }
 
-#if TARGET_ION_ABI_VERSION >= 2  // Use libion APIs for new ion
-
 int IonAlloc::OpenIonDevice() {
   return ion_open();
 }
@@ -162,169 +154,6 @@
   return 0;
 }
 
-#else
-#ifndef TARGET_ION_ABI_VERSION  // Use old ion apis directly
-
-int IonAlloc::OpenIonDevice() {
-  return open(kIonDevice, O_RDONLY);
-}
-
-void IonAlloc::CloseIonDevice() {
-  if (ion_dev_fd_ > FD_INIT) {
-    close(ion_dev_fd_);
-  }
-
-  ion_dev_fd_ = FD_INIT;
-}
-
-int IonAlloc::AllocBuffer(AllocData *data) {
-  ATRACE_CALL();
-  int err = 0;
-  struct ion_handle_data handle_data;
-  struct ion_fd_data fd_data;
-  struct ion_allocation_data ion_alloc_data;
-
-  ion_alloc_data.len = data->size;
-  ion_alloc_data.align = data->align;
-  ion_alloc_data.heap_id_mask = data->heap_id;
-  ion_alloc_data.flags = data->flags;
-  ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED;
-  std::string tag_name{};
-  if (ATRACE_ENABLED()) {
-    tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
-  }
-
-  ATRACE_BEGIN(tag_name.c_str());
-  if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) {
-    err = -errno;
-    ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
-    return err;
-  }
-  ATRACE_END();
-
-  fd_data.handle = ion_alloc_data.handle;
-  handle_data.handle = ion_alloc_data.handle;
-  ATRACE_BEGIN("ION_IOC_MAP");
-  if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) {
-    err = -errno;
-    ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno));
-    ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
-    return err;
-  }
-  ATRACE_END();
-
-  data->fd = fd_data.fd;
-  data->ion_handle = handle_data.handle;
-  ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd,
-           data->ion_handle);
-
-  return 0;
-}
-
-int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
-                         int ion_handle) {
-  ATRACE_CALL();
-  int err = 0;
-  ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
-           ion_handle);
-
-  if (base) {
-    err = UnmapBuffer(base, size, offset);
-  }
-
-  if (ion_handle > 0) {
-    struct ion_handle_data handle_data;
-    handle_data.handle = ion_handle;
-    ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
-  }
-  close(fd);
-  return err;
-}
-
-int IonAlloc::ImportBuffer(int fd) {
-  struct ion_fd_data fd_data;
-  int err = 0;
-  fd_data.fd = fd;
-  if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) {
-    err = -errno;
-    ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno));
-    return err;
-  }
-  return fd_data.handle;
-}
-
-int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
-                          int /*fd*/) {
-  if (op == CACHE_READ_DONE)  {
-    return 0;
-  }
-
-#ifndef QMAA
-  ATRACE_CALL();
-  ATRACE_INT("operation id", op);
-  struct ion_flush_data flush_data;
-  int err = 0;
-
-  flush_data.handle = handle;
-  flush_data.vaddr = base;
-  // offset and length are unsigned int
-  flush_data.offset = offset;
-  flush_data.length = size;
-
-  struct ion_custom_data d;
-  switch (op) {
-    case CACHE_CLEAN:
-      d.cmd = ION_IOC_CLEAN_CACHES;
-      break;
-    case CACHE_INVALIDATE:
-      d.cmd = ION_IOC_INV_CACHES;
-      break;
-    case CACHE_CLEAN_AND_INVALIDATE:
-    default:
-      d.cmd = ION_IOC_CLEAN_INV_CACHES;
-  }
-
-  d.arg = (unsigned long)(&flush_data);  // NOLINT
-  if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) {
-    err = -errno;
-    ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno));
-    return err;
-  }
-#endif
-  return 0;
-}
-
-#else  // This ion version is not supported
-
-int IonAlloc::OpenIonDevice() {
-  return -EINVAL;
-}
-
-void IonAlloc::CloseIonDevice() {
-}
-
-int IonAlloc::AllocBuffer(AllocData * /*data*/) {
-  return -EINVAL;
-}
-
-int IonAlloc::FreeBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
-                         int /*fd*/, int /*ion_handle*/) {
-  return -EINVAL;
-}
-
-int IonAlloc::ImportBuffer(int /*fd*/) {
-  return -EINVAL;
-}
-
-int IonAlloc::CleanBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
-                          int /*handle*/, int /*op*/, int /*fd*/) {
-  return -EINVAL;
-}
-
-#endif
-#endif  // TARGET_ION_ABI_VERSION
-
-
 int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
   ATRACE_CALL();
   int err = 0;
diff --git a/gralloc/gr_ion_alloc.h b/gralloc/gr_ion_alloc.h
index 13474f5..e84a510 100644
--- a/gralloc/gr_ion_alloc.h
+++ b/gralloc/gr_ion_alloc.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2019, 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
@@ -30,10 +30,6 @@
 #ifndef __GR_ION_ALLOC_H__
 #define __GR_ION_ALLOC_H__
 
-#ifndef QMAA
-#include <linux/msm_ion.h>
-#endif
-
 #define FD_INIT -1
 
 namespace gralloc {
@@ -74,10 +70,6 @@
   int CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op, int fd);
 
  private:
-#ifndef TARGET_ION_ABI_VERSION
-  const char *kIonDevice = "/dev/ion";
-#endif
-
   int OpenIonDevice();
   void CloseIonDevice();
 
diff --git a/gralloc/gr_utils.cpp b/gralloc/gr_utils.cpp
index 49dcfb4..16e41b1 100644
--- a/gralloc/gr_utils.cpp
+++ b/gralloc/gr_utils.cpp
@@ -853,6 +853,28 @@
   return size;
 }
 
+unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage) {
+  unsigned int meta_size = 0;
+  if (!IsUBwcEnabled(format, usage)) {
+    return meta_size;
+  }
+  uint32_t bpp = GetBppForUncompressedRGB(format);
+  switch (format) {
+    case HAL_PIXEL_FORMAT_BGR_565:
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+    case HAL_PIXEL_FORMAT_RGBX_1010102:
+    case HAL_PIXEL_FORMAT_RGBA_FP16:
+      meta_size = GetRgbUBwcMetaBufferSize(width, height, bpp);
+      break;
+    default:
+      ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, format);
+      break;
+  }
+  return meta_size;
+}
+
 int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
   int err = 0;
 
@@ -866,23 +888,8 @@
     *rgb_data = reinterpret_cast<void *>(hnd->base);
     return err;
   }
+  unsigned int meta_size = GetRgbMetaSize(hnd->format, hnd->width, hnd->height, hnd->usage);
 
-  unsigned int meta_size = 0;
-  uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
-  switch (hnd->format) {
-    case HAL_PIXEL_FORMAT_BGR_565:
-    case HAL_PIXEL_FORMAT_RGBA_8888:
-    case HAL_PIXEL_FORMAT_RGBX_8888:
-    case HAL_PIXEL_FORMAT_RGBA_1010102:
-    case HAL_PIXEL_FORMAT_RGBX_1010102:
-    case HAL_PIXEL_FORMAT_RGBA_FP16:
-      meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
-      break;
-    default:
-      ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
-      err = -EINVAL;
-      break;
-  }
   *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
 
   return err;
@@ -1652,4 +1659,40 @@
   }
 }
 
+bool HasAlphaComponent(int32_t format) {
+  switch (format) {
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+    case HAL_PIXEL_FORMAT_RGBA_5551:
+    case HAL_PIXEL_FORMAT_RGBA_4444:
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+    case HAL_PIXEL_FORMAT_ARGB_2101010:
+    case HAL_PIXEL_FORMAT_BGRA_1010102:
+    case HAL_PIXEL_FORMAT_ABGR_2101010:
+    case HAL_PIXEL_FORMAT_RGBA_FP16:
+      return true;
+    default:
+      return false;
+  }
+}
+
+void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
+                     int32_t /* flags */, int *plane_count, PlaneLayoutInfo *plane_info) {
+  uint64_t usage = info.usage;
+  *plane_count = 1;
+  plane_info->component =
+      (PlaneComponent)(PLANE_COMPONENT_R | PLANE_COMPONENT_G | PLANE_COMPONENT_B);
+  if (HasAlphaComponent(format)) {
+    plane_info->component = (PlaneComponent)(plane_info->component | PLANE_COMPONENT_A);
+  }
+  plane_info->size = GetSize(info, width, height);
+  plane_info->step = GetBpp(format);
+  plane_info->offset = GetRgbMetaSize(format, width, height, usage);
+  plane_info->h_subsampling = 0;
+  plane_info->v_subsampling = 0;
+  plane_info->stride = width;
+  plane_info->stride_bytes = width * plane_info->step;
+  plane_info->scanlines = height;
+}
+
 }  // namespace gralloc
diff --git a/gralloc/gr_utils.h b/gralloc/gr_utils.h
index bc5a670..6dbe889 100644
--- a/gralloc/gr_utils.h
+++ b/gralloc/gr_utils.h
@@ -145,6 +145,9 @@
 int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]);
 int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
                     int32_t flags, int *plane_count, PlaneLayoutInfo plane_info[8]);
+void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
+                     int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info);
+unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage);
 void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling);
 void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
                                        struct android_ycbcr *ycbcr);
@@ -180,6 +183,7 @@
 int GetCustomFormatFlags(int format, uint64_t usage, int *custom_format, uint64_t *priv_flags);
 int GetBufferType(int inputFormat);
 bool IsGPUFlagSupported(uint64_t usage);
+bool HasAlphaComponent(int32_t format);
 }  // namespace gralloc
 
 #endif  // __GR_UTILS_H__
diff --git a/libdrmutils/drm_interface.h b/libdrmutils/drm_interface.h
index e41c8ab..41543d0 100644
--- a/libdrmutils/drm_interface.h
+++ b/libdrmutils/drm_interface.h
@@ -525,6 +525,8 @@
   uint32_t rotation_limit_index = 0;
   uint32_t line_width_constraints_count = 0;
   std::vector< std::pair <uint32_t, uint32_t> > line_width_limits;
+  uint32_t num_mnocports;
+  uint32_t mnoc_bus_width;
 };
 
 enum struct DRMPlaneType {
diff --git a/sde-drm/drm_crtc.cpp b/sde-drm/drm_crtc.cpp
index ef1e692..f8890d1 100644
--- a/sde-drm/drm_crtc.cpp
+++ b/sde-drm/drm_crtc.cpp
@@ -349,6 +349,8 @@
   string solidfill_stages = "dim_layer_v1_max_layers=";
   string has_hdr = "has_hdr=";
   string min_prefill_lines = "min_prefill_lines=";
+  string num_mnocports = "num_mnoc_ports=";
+  string mnoc_bus_width = "axi_bus_width=";
 
   crtc_info_.max_solidfill_stages = 0;  // default _
   string dest_scaler_count = "dest_scaler_count=";
@@ -439,6 +441,10 @@
       crtc_info_.min_prefill_lines = std::stoi(string(line, min_prefill_lines.length()));
     } else if (line.find(sec_ui_blendstage) != string::npos) {
       crtc_info_.secure_disp_blend_stage = std::stoi(string(line, (sec_ui_blendstage).length()));
+    } else if (line.find(num_mnocports) != string::npos) {
+      crtc_info_.num_mnocports = std::stoi(string(line, num_mnocports.length()));
+    } else if (line.find(mnoc_bus_width) != string::npos) {
+      crtc_info_.mnoc_bus_width = std::stoi(string(line, mnoc_bus_width.length()));
     } else if (line.find(linewidth_constraints) != string::npos) {
       crtc_info_.line_width_constraints_count =
                             std::stoi(string(line, (linewidth_constraints).length()));
diff --git a/sdm/include/private/hw_info_types.h b/sdm/include/private/hw_info_types.h
index d9abd6c..8997cc6 100644
--- a/sdm/include/private/hw_info_types.h
+++ b/sdm/include/private/hw_info_types.h
@@ -342,6 +342,8 @@
   uint32_t line_width_constraints_count = 0;
   vector< pair <uint32_t, uint32_t> > line_width_limits;
   vector< pair <uint32_t, uint32_t> > line_width_constraints;
+  uint32_t num_mnocports = 2;
+  uint32_t mnoc_bus_width = 32;
 };
 
 struct HWSplitInfo {
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index 6985f7a..8f60f08 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -631,6 +631,11 @@
     active_ = active;
     state_ = state;
     comp_manager_->SetDisplayState(display_comp_ctx_, state, release_fence ? *release_fence : -1);
+    // If previously requested doze state is still pending reset it on any new display state request
+    // and handle the new request.
+    if (state_ != kStateDoze) {
+      pending_doze_ = false;
+    }
   }
 
   // Handle vsync pending on resume, Since the power on commit is synchronous we pass -1 as retire
diff --git a/sdm/libs/core/drm/hw_info_drm.cpp b/sdm/libs/core/drm/hw_info_drm.cpp
index eaa065c..44fddcb 100644
--- a/sdm/libs/core/drm/hw_info_drm.cpp
+++ b/sdm/libs/core/drm/hw_info_drm.cpp
@@ -336,6 +336,9 @@
     width_constraints.push_back(std::make_pair(kPipeScalingLimit, info.scaling_limit_index));
     width_constraints.push_back(std::make_pair(kPipeRotationLimit, info.rotation_limit_index));
   }
+  // In case driver doesn't report bus width default to 256 bit bus.
+  hw_resource->num_mnocports = info.num_mnocports ? info.num_mnocports : 2;
+  hw_resource->mnoc_bus_width = info.mnoc_bus_width ? info.mnoc_bus_width : 32;
 }
 
 void HWInfoDRM::GetHWPlanesInfo(HWResourceInfo *hw_resource) {