msm8998: Update to 07.00.00.279.027

msm8998: from hardware/qcom/display
  9da3a9caf Merge "display: Move all opensource modules to vendor partition"
  cecde8719 display: Move all opensource modules to vendor partition
  1d7f83c14 gralloc1: Fixes for hidl
  ecd7e84ea sdm: enable gralloc1 from hwc2
  9c02dc799 gralloc1: Add remaining functionality
  a110e65f5 libdrmutils: Remove dependency on drmModeAddFB3
  e9a2c4ece Merge branch 'display.lnx.3.0-dev' into display.lnx.3.9-rel
  852358c51 Merge "sdm: Intf change propagating fb config for partial update"
  d0d29296a Merge "sdm: Do not mark video layer with non-integral crop as skip"
  51915d6c1 Merge "sdm: Fix dynamic resolution change for virtual display"
  96cffc4ce Merge "sdm: drm: Destroy DRMManager and resource info singletons"
  ab7807cd5 sdm: drm: Destroy DRMManager and resource info singletons
  fad1afd01 libdrmutils: Add support for RMFB2
  672f0f788 Merge "sdm: Consider external display as active in doze suspend."
  095e4405d Merge "sdm: Do not handle SKIP Cursor layer as H/W Cursor"
  6d9608c9d Merge "libmemtrack: Update the flags field for sparse memory type"
  66c941bd5 sdm: drm: Add Atomic commit support
  7d476edfb sdm: drm: Add support for default non-atomic mode
  0f26d84e3 libmemtrack: Update the flags field for sparse memory type
  1f7aa5ccf sdm: Do not mark video layer with non-integral crop as skip
  8ec9d36c2 sdm: Fix dynamic resolution change for virtual display
  4cfb380e8 sdm: Intf change propagating fb config for partial update
  172a743b1 sdm: Consider external display as active in doze suspend.
  9b47ae740 sdm: Do not handle SKIP Cursor layer as H/W Cursor
  2e06deff0 Add explicit liblog dependency
  34f165638 Include additional system header search path

Bug: 36064845
Signed-off-by: Thierry Strudel <tstrudel@google.com>
Change-Id: If42b63f7cbc285dac84ea1970fcc7d21867bd70c
(cherry picked from commit 6cfbcfa35e4da3a5975d9904a8caae9968acc768)
diff --git a/msm8998/sdm/.clang-format b/msm8998/.clang-format
similarity index 100%
rename from msm8998/sdm/.clang-format
rename to msm8998/.clang-format
diff --git a/msm8998/Android.mk b/msm8998/Android.mk
index 0b875fb..226e8a0 100644
--- a/msm8998/Android.mk
+++ b/msm8998/Android.mk
@@ -3,10 +3,14 @@
 
 ifneq ($(TARGET_IS_HEADLESS), true)
     display-hals += libcopybit liblight libmemtrack hdmi_cec \
-                    $(sdm-libs)/hwc $(sdm-libs)/hwc2 gpu_tonemapper
+                    $(sdm-libs)/hwc $(sdm-libs)/hwc2 gpu_tonemapper libdrmutils
 endif
 
-display-hals += libgralloc
+ifneq ($(TARGET_USES_GRALLOC1), true)
+    display-hals += libgralloc
+else
+    display-hals += libgralloc1
+endif
 
 ifeq ($(call is-vendor-board-platform,QCOM),true)
     include $(call all-named-subdir-makefiles,$(display-hals))
diff --git a/msm8998/common.mk b/msm8998/common.mk
index b6634cb..a0d8c74 100644
--- a/msm8998/common.mk
+++ b/msm8998/common.mk
@@ -4,6 +4,9 @@
 #Common C flags
 common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
 common_flags += -Wconversion -Wall -Werror -std=c++11
+ifneq ($(TARGET_IS_HEADLESS), true)
+    common_flags += -DCOMPILE_DRM
+endif
 
 ifeq ($(TARGET_USES_COLOR_METADATA), true)
 common_flags += -DUSE_COLOR_METADATA
@@ -20,6 +23,7 @@
 common_includes += $(display_top)/gpu_tonemapper
 ifneq ($(TARGET_IS_HEADLESS), true)
     common_includes += $(display_top)/libcopybit
+    common_includes += $(display_top)/libdrmutils
 endif
 
 common_includes += $(display_top)/include
@@ -37,7 +41,12 @@
     LOCAL_CLANG := true
 endif
 
-common_flags += -isystem $(display_top)/libgralloc
+ifneq ($(TARGET_USES_GRALLOC1), true)
+    common_flags += -isystem $(display_top)/libgralloc
+else
+    common_flags += -isystem $(display_top)/libgralloc1
+    common_flags += -DUSE_GRALLOC1
+endif
 
 ifeq ($(TARGET_USES_POST_PROCESSING),true)
     common_flags     += -DUSES_POST_PROCESSING
@@ -52,8 +61,6 @@
     common_flags += -DMASTER_SIDE_CP
 endif
 
-common_flags += -D__STDC_FORMAT_MACROS
-
 common_deps  :=
 kernel_includes :=
 
diff --git a/msm8998/gpu_tonemapper/Android.mk b/msm8998/gpu_tonemapper/Android.mk
index a69ce3e..f009fcc 100644
--- a/msm8998/gpu_tonemapper/Android.mk
+++ b/msm8998/gpu_tonemapper/Android.mk
@@ -1,17 +1,19 @@
 LOCAL_PATH := $(call my-dir)
 include $(LOCAL_PATH)/../common.mk
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_COPY_HEADERS_TO     := $(common_header_export_path)
 LOCAL_COPY_HEADERS        := TonemapFactory.h Tonemapper.h
-LOCAL_SHARED_LIBRARIES    := libEGL libGLESv2 libui libutils liblog
-LOCAL_EXPORT_SHARED_LIBRARY_HEADERS := libui
 include $(BUILD_COPY_HEADERS)
 
+include $(CLEAR_VARS)
 LOCAL_MODULE              := libgpu_tonemapper
 LOCAL_MODULE_TAGS         := optional
+LOCAL_PROPRIETARY_MODULE  := true
 LOCAL_C_INCLUDES          := $(TARGET_OUT_HEADERS)/qcom/display/
 LOCAL_C_INCLUDES          += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_C_INCLUDES          += frameworks/native/libs/arect/include
+LOCAL_SHARED_LIBRARIES    := libEGL libGLESv2 libui libutils liblog
 
 LOCAL_CFLAGS              := $(version_flag) -Wno-missing-field-initializers -Wall \
                              -Wno-unused-parameter -std=c++11 -DLOG_TAG=\"GPU_TONEMAPPER\"
diff --git a/msm8998/libcopybit/Android.mk b/msm8998/libcopybit/Android.mk
index 3666783..0b7b49f 100644
--- a/msm8998/libcopybit/Android.mk
+++ b/msm8998/libcopybit/Android.mk
@@ -14,14 +14,16 @@
 
 LOCAL_PATH:= $(call my-dir)
 include $(LOCAL_PATH)/../common.mk
-
 include $(CLEAR_VARS)
+
 LOCAL_COPY_HEADERS_TO         := $(common_header_export_path)
 LOCAL_COPY_HEADERS            := copybit.h copybit_priv.h c2d2.h
+#Copy the headers regardless of whether copybit is built
 include $(BUILD_COPY_HEADERS)
 
-include $(CLEAR_VARS)
+ifneq ($(TARGET_USES_GRALLOC1), true)
 LOCAL_MODULE                  := copybit.$(TARGET_BOARD_PLATFORM)
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_PROPRIETARY_MODULE      := true
 LOCAL_MODULE_TAGS             := optional
@@ -48,3 +50,4 @@
         endif
     endif
 endif
+endif
diff --git a/msm8998/libdrmutils/Android.mk b/msm8998/libdrmutils/Android.mk
new file mode 100644
index 0000000..f683664
--- /dev/null
+++ b/msm8998/libdrmutils/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE                  := libdrmutils
+LOCAL_MODULE_TAGS             := optional
+LOCAL_PROPRIETARY_MODULE      := true
+LOCAL_C_INCLUDES              := external/libdrm \
+                                 $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_SHARED_LIBRARIES        := libdrm libdl
+LOCAL_CFLAGS                  := -DLOG_TAG=\"DRMUTILS\" -Wall -std=c++11 -Werror -fno-operator-names
+LOCAL_CLANG                   := true
+ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+endif
+LOCAL_SRC_FILES               := drm_master.cpp drm_res_mgr.cpp drm_lib_loader.cpp
+LOCAL_COPY_HEADERS_TO         := qcom/display
+LOCAL_COPY_HEADERS            := drm_master.h drm_res_mgr.h drm_lib_loader.h drm_logger.h drm_interface.h
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/msm8998/libdrmutils/drm_interface.h b/msm8998/libdrmutils/drm_interface.h
new file mode 100644
index 0000000..94a48e1
--- /dev/null
+++ b/msm8998/libdrmutils/drm_interface.h
@@ -0,0 +1,370 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __DRM_INTERFACE_H__
+#define __DRM_INTERFACE_H__
+
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+
+namespace sde_drm {
+/*
+ * Drm Atomic Operation Codes
+ */
+enum struct DRMOps {
+  /*
+   * Op: Sets plane source crop
+   * Arg: uint32_t - Plane ID
+   *      DRMRect  - Source Rectangle
+   */
+  PLANE_SET_SRC_RECT,
+  /*
+   * Op: Sets plane destination rect
+   * Arg: uint32_t - Plane ID
+   *      DRMRect - Dst Rectangle
+   */
+  PLANE_SET_DST_RECT,
+  /*
+   * Op: Sets plane zorder
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - zorder
+   */
+  PLANE_SET_ZORDER,
+  /*
+   * Op: Sets plane rotation flags
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - bit mask of rotation flags (See drm_mode.h for enums)
+   */
+  PLANE_SET_ROTATION,
+  /*
+   * Op: Sets plane alpha
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - alpha value
+   */
+  PLANE_SET_ALPHA,
+  /*
+   * Op: Sets the blend type
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - blend type (see DRMBlendType)
+   */
+  PLANE_SET_BLEND_TYPE,
+  /*
+   * Op: Sets horizontal decimation
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - decimation factor
+   */
+  PLANE_SET_H_DECIMATION,
+  /*
+   * Op: Sets vertical decimation
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - decimation factor
+   */
+  PLANE_SET_V_DECIMATION,
+  /*
+   * Op: Sets frame buffer ID for plane. Set together with CRTC.
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - Framebuffer ID
+   */
+  PLANE_SET_FB_ID,
+  /*
+   * Op: Sets the crtc for this plane. Set together with FB_ID.
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - CRTC ID
+   */
+  PLANE_SET_CRTC,
+  /*
+   * Op: Sets acquire fence for this plane's buffer. Set together with FB_ID, CRTC.
+   * Arg: uint32_t - Plane ID
+   *      uint32_t - Input fence
+   */
+  PLANE_SET_INPUT_FENCE,
+  /*
+   * Op: Activate or deactivate a CRTC
+   * Arg: uint32_t - CRTC ID
+   *      uint32_t - 1 to enable, 0 to disable
+   */
+  CRTC_SET_ACTIVE,
+  /*
+   * Op: Sets display mode
+   * Arg: uint32_t - CRTC ID
+   *      drmModeModeInfo* - Pointer to display mode
+   */
+  CRTC_SET_MODE,
+  /*
+   * Op: Sets an offset indicating when a release fence should be signalled.
+   * Arg: uint32_t - offset
+   *      0: non-speculative, default
+   *      1: speculative
+   */
+  CRTC_SET_OUTPUT_FENCE_OFFSET,
+  /*
+   * Op: Returns release fence for this frame. Should be called after Commit() on
+   * DRMAtomicReqInterface.
+   * Arg: uint32_t - CRTC ID
+   *      int * - Pointer to an integer that will hold the returned fence
+   */
+  CRTC_GET_RELEASE_FENCE,
+  /*
+   * Op: Returns retire fence for this commit. Should be called after Commit() on
+   * DRMAtomicReqInterface.
+   * Arg: uint32_t - Connector ID
+   *      int * - Pointer to an integer that will hold the returned fence
+   */
+  CONNECTOR_GET_RETIRE_FENCE,
+  /*
+   * Op: Sets writeback connector destination rect
+   * Arg: uint32_t - Connector ID
+   *      DRMRect - Dst Rectangle
+   */
+  CONNECTOR_SET_OUTPUT_RECT,
+  /*
+   * Op: Sets frame buffer ID for writeback connector.
+   * Arg: uint32_t - Connector ID
+   *      uint32_t - Framebuffer ID
+   */
+  CONNECTOR_SET_OUTPUT_FB_ID,
+};
+
+enum struct DRMBlendType {
+  UNDEFINED = 0,
+  OPAQUE = 1,
+  PREMULTIPLIED = 2,
+  COVERAGE = 3,
+};
+
+/* Display type to identify a suitable connector */
+enum struct DRMDisplayType {
+  PERIPHERAL,
+  TV,
+  VIRTUAL,
+};
+
+struct DRMRect {
+  uint32_t left;    // Left-most pixel coordinate.
+  uint32_t top;     // Top-most pixel coordinate.
+  uint32_t right;   // Right-most pixel coordinate.
+  uint32_t bottom;  // Bottom-most pixel coordinate.
+};
+
+//------------------------------------------------------------------------
+// DRM Info Query Types
+//------------------------------------------------------------------------
+
+enum struct QSEEDVersion {
+  V1,
+  V2,
+  V3,
+};
+
+/* Per CRTC Resource Info*/
+struct DRMCrtcInfo {
+  bool has_src_split;
+  uint32_t max_blend_stages;
+  QSEEDVersion qseed_version;
+};
+
+enum struct DRMPlaneType {
+  // Has CSC and scaling capability
+  VIG = 0,
+  // Has scaling capability but no CSC
+  RGB,
+  // No scaling support
+  DMA,
+  // Supports a small dimension and doesn't use a CRTC stage
+  CURSOR,
+  MAX,
+};
+
+struct DRMPlaneTypeInfo {
+  // FourCC format enum and modifier
+  std::vector<std::pair<uint32_t, uint64_t>> formats_supported;
+  uint32_t max_linewidth;
+  uint32_t max_upscale;
+  uint32_t max_downscale;
+  uint32_t max_horizontal_deci;
+  uint32_t max_vertical_deci;
+};
+
+/* All DRM Planes Info*/
+struct DRMPlanesInfo {
+  // Plane id and plane type sorted by highest to lowest priority
+  std::vector<std::pair<uint32_t, DRMPlaneType>> planes;
+  // Plane type and type info
+  std::map<DRMPlaneType, DRMPlaneTypeInfo> types;
+};
+
+enum struct DRMTopology {
+  UNKNOWN,  // To be compat with driver defs in sde_kms.h
+  SINGLE_LM,
+  DUAL_LM,
+  PPSPLIT,
+  DUAL_LM_MERGE,
+};
+
+enum struct DRMPanelMode {
+  VIDEO,
+  COMMAND,
+};
+
+/* Per Connector Info*/
+struct DRMConnectorInfo {
+  uint32_t mmWidth;
+  uint32_t mmHeight;
+  uint32_t type;
+  uint32_t num_modes;
+  drmModeModeInfo *modes;
+  DRMTopology topology;
+  std::string panel_name;
+  DRMPanelMode panel_mode;
+  bool is_primary;
+  // Valid only if DRMPanelMode is VIDEO
+  bool dynamic_fps;
+  // FourCC format enum and modifier
+  std::vector<std::pair<uint32_t, uint64_t>> formats_supported;
+  // Valid only if type is DRM_MODE_CONNECTOR_VIRTUAL
+  uint32_t max_linewidth;
+};
+
+/* Identifier token for a display */
+struct DRMDisplayToken {
+  uint32_t conn_id;
+  uint32_t crtc_id;
+};
+
+/* DRM Atomic Request Property Set.
+ *
+ * Helper class to create and populate atomic properties of DRM components
+ * when rendered in DRM atomic mode */
+class DRMAtomicReqInterface {
+ public:
+  virtual ~DRMAtomicReqInterface() {}
+  /* Perform request operation.
+   *
+   * [input]: opcode: operation code from DRMOps list.
+   *          var_arg: arguments for DRMOps's can differ in number and
+   *          data type. Refer above DRMOps to details.
+   * [return]: Error code if the API fails, 0 on success.
+   */
+  virtual int Perform(DRMOps opcode, ...) = 0;
+
+  /*
+   * Commit the params set via Perform(). Also resets the properties after commit. Needs to be
+   * called every frame.
+   * [input]: synchronous: Determines if the call should block until a h/w flip
+   * [return]: Error code if the API fails, 0 on success.
+   */
+  virtual int Commit(bool synchronous) = 0;
+  /*
+   * Validate the params set via Perform().
+   * [return]: Error code if the API fails, 0 on success.
+   */
+  virtual int Validate() = 0;
+};
+
+class DRMManagerInterface;
+
+/* Populates a singleton instance of DRMManager */
+typedef int (*GetDRMManager)(int fd, DRMManagerInterface **intf);
+
+/* Destroy DRMManager instance */
+typedef int (*DestroyDRMManager)();
+
+/*
+ * DRM Manager Interface - Any class which plans to implement helper function for vendor
+ * specific DRM driver implementation must implement the below interface routines to work
+ * with SDM.
+ */
+
+class DRMManagerInterface {
+ public:
+  virtual ~DRMManagerInterface() {}
+
+  /*
+   * Since SDM completely manages the planes. GetPlanesInfo will provide all
+   * the plane information.
+   * [output]: DRMPlanesInfo: Resource Info for planes.
+   */
+  virtual void GetPlanesInfo(DRMPlanesInfo *info) = 0;
+
+  /*
+   * Will provide all the information of a selected crtc.
+   * [input]: Use crtc id 0 to obtain system wide info
+   * [output]: DRMCrtcInfo: Resource Info for the given CRTC id.
+   */
+  virtual void GetCrtcInfo(uint32_t crtc_id, DRMCrtcInfo *info) = 0;
+
+  /*
+   * Will provide all the information of a selected connector.
+   * [output]: DRMConnectorInfo: Resource Info for the given connector id
+   */
+  virtual void GetConnectorInfo(uint32_t conn_id, DRMConnectorInfo *info) = 0;
+
+  /*
+   * Register a logical display to receive a token.
+   * Each display pipeline in DRM is identified by its CRTC and Connector(s).
+   * On display connect(bootup or hotplug), clients should invoke this interface to
+   * establish the pipeline for the display and should get a DisplayToken
+   * populated with crtc and connnector(s) id's. Here onwards, Client should
+   * use this token to represent the display for any Perform operations if
+   * needed.
+   *
+   * [input]: disp_type - Peripheral / TV / Virtual
+   * [output]: DRMDisplayToken - CRTC and Connector id's for the display
+   * [return]: 0 on success, a negative error value otherwise
+   */
+  virtual int RegisterDisplay(DRMDisplayType disp_type, DRMDisplayToken *tok) = 0;
+
+  /* Client should invoke this interface on display disconnect.
+   * [input]: DRMDisplayToken - identifier for the display.
+   */
+  virtual void UnregisterDisplay(const DRMDisplayToken &token) = 0;
+
+  /*
+   * Creates and returns an instance of DRMAtomicReqInterface corresponding to a display token
+   * returned as part of RegisterDisplay API. Needs to be called per display.
+   * [input]: DRMDisplayToken that identifies a display pipeline
+   * [output]: Pointer to an instance of DRMAtomicReqInterface.
+   * [return]: Error code if the API fails, 0 on success.
+   */
+  virtual int CreateAtomicReq(const DRMDisplayToken &token, DRMAtomicReqInterface **intf) = 0;
+
+  /*
+   * Destroys the instance of DRMAtomicReqInterface
+   * [input]: Pointer to a DRMAtomicReqInterface
+   * [return]: Error code if the API fails, 0 on success.
+   */
+  virtual int DestroyAtomicReq(DRMAtomicReqInterface *intf) = 0;
+};
+}  // namespace sde_drm
+#endif  // __DRM_INTERFACE_H__
diff --git a/msm8998/libdrmutils/drm_lib_loader.cpp b/msm8998/libdrmutils/drm_lib_loader.cpp
new file mode 100644
index 0000000..83c9f1b
--- /dev/null
+++ b/msm8998/libdrmutils/drm_lib_loader.cpp
@@ -0,0 +1,92 @@
+/*
+* Copyright (c) 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 <dlfcn.h>
+
+#include "drm_lib_loader.h"
+
+#define __CLASS__ "DRMLibLoader"
+
+using std::mutex;
+using std::lock_guard;
+
+namespace drm_utils {
+
+DRMLibLoader *DRMLibLoader::s_instance = nullptr;
+mutex DRMLibLoader::s_lock;
+
+DRMLibLoader *DRMLibLoader::GetInstance() {
+  lock_guard<mutex> obj(s_lock);
+
+  if (!s_instance) {
+    s_instance = new DRMLibLoader();
+  }
+
+  return s_instance;
+}
+
+void DRMLibLoader::Destroy() {
+  lock_guard<mutex> obj(s_lock);
+  if (s_instance) {
+    delete s_instance;
+    s_instance = nullptr;
+  }
+}
+
+DRMLibLoader::DRMLibLoader() {
+  if (Open("libsdedrm.so")) {
+    if (Sym("GetDRMManager", reinterpret_cast<void **>(&func_get_drm_manager_)) &&
+        Sym("DestroyDRMManager", reinterpret_cast<void **>(&func_destroy_drm_manager_))) {
+      is_loaded_ = true;
+    }
+  }
+}
+
+DRMLibLoader::~DRMLibLoader() {
+  if (lib_) {
+    ::dlclose(lib_);
+    lib_ = nullptr;
+  }
+}
+
+bool DRMLibLoader::Open(const char *lib_name) {
+  lib_ = ::dlopen(lib_name, RTLD_NOW);
+
+  return (lib_ != nullptr);
+}
+
+bool DRMLibLoader::Sym(const char *func_name, void **func_ptr) {
+  if (lib_) {
+    *func_ptr = ::dlsym(lib_, func_name);
+  }
+
+  return (*func_ptr != nullptr);
+}
+
+}  // namespace drm_utils
diff --git a/msm8998/libdrmutils/drm_lib_loader.h b/msm8998/libdrmutils/drm_lib_loader.h
new file mode 100644
index 0000000..d132d69
--- /dev/null
+++ b/msm8998/libdrmutils/drm_lib_loader.h
@@ -0,0 +1,64 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __DRM_LIB_LOADER_H__
+#define __DRM_LIB_LOADER_H__
+
+#include <drm_interface.h>
+#include <mutex>
+
+namespace drm_utils {
+
+class DRMLibLoader {
+ public:
+  ~DRMLibLoader();
+  bool IsLoaded() { return is_loaded_; }
+  sde_drm::GetDRMManager FuncGetDRMManager() { return func_get_drm_manager_; }
+  sde_drm::DestroyDRMManager FuncDestroyDRMManager() { return func_destroy_drm_manager_; }
+
+  static DRMLibLoader *GetInstance();
+  static void Destroy();
+
+ private:
+  DRMLibLoader();
+  bool Open(const char *lib_name);
+  bool Sym(const char *func_name, void **func_ptr);
+
+  void *lib_ = {};
+  sde_drm::GetDRMManager func_get_drm_manager_ = {};
+  sde_drm::DestroyDRMManager func_destroy_drm_manager_ = {};
+  bool is_loaded_ = false;
+
+  static DRMLibLoader *s_instance;  // Singleton instance
+  static std::mutex s_lock;
+};
+
+}  // namespace drm_utils
+
+#endif  // __DRM_LIB_LOADER_H__
diff --git a/msm8998/libdrmutils/drm_logger.h b/msm8998/libdrmutils/drm_logger.h
new file mode 100644
index 0000000..d0b0773
--- /dev/null
+++ b/msm8998/libdrmutils/drm_logger.h
@@ -0,0 +1,69 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __DRM_LOGGER_H__
+#define __DRM_LOGGER_H__
+
+#include <utility>
+
+namespace drm_utils {
+
+class DRMLogger {
+ public:
+  virtual ~DRMLogger() {}
+  virtual void Error(const char *format, ...) = 0;
+  virtual void Warning(const char *format, ...) = 0;
+  virtual void Info(const char *format, ...) = 0;
+  virtual void Debug(const char *format, ...) = 0;
+
+  static void Set(DRMLogger *logger) { s_instance = logger; }
+  static DRMLogger *Get() { return s_instance; }
+
+ private:
+  static DRMLogger *s_instance;
+};
+
+#define DRM_LOG(method, format, ...)                            \
+  if (drm_utils::DRMLogger::Get()) {                            \
+    drm_utils::DRMLogger::Get()->method(format, ##__VA_ARGS__); \
+  }
+
+#define DRM_LOG_CONTEXT(method, format, ...) \
+  DRM_LOG(method, __CLASS__ "::%s: " format, __FUNCTION__, ##__VA_ARGS__);
+
+#define DRM_LOGE(format, ...) DRM_LOG_CONTEXT(Error, format, ##__VA_ARGS__)
+#define DRM_LOGW(format, ...) DRM_LOG_CONTEXT(Warning, format, ##__VA_ARGS__)
+#define DRM_LOGI(format, ...) DRM_LOG_CONTEXT(Info, format, ##__VA_ARGS__)
+#define DRM_LOGD_IF(pred, format, ...) \
+  if (pred)                            \
+  DRM_LOG_CONTEXT(Debug, format, ##__VA_ARGS__)
+
+}  // namespace drm_utils
+
+#endif  // __DRM_LOGGER_H__
diff --git a/msm8998/libdrmutils/drm_master.cpp b/msm8998/libdrmutils/drm_master.cpp
new file mode 100644
index 0000000..239ec4c
--- /dev/null
+++ b/msm8998/libdrmutils/drm_master.cpp
@@ -0,0 +1,147 @@
+/*
+* Copyright (c) 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 <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+// Intentionally included after xf86 headers so that they in-turn include libdrm version of drm.h
+// that doesn't use keyword "virtual" for a variable name. Not doing so leads to the kernel version
+// of drm.h being included causing compilation to fail
+#include <drm/msm_drm.h>
+#include <algorithm>
+#include <iterator>
+
+#include "drm_master.h"
+
+#define __CLASS__ "DRMMaster"
+
+using std::mutex;
+using std::lock_guard;
+using std::begin;
+using std::copy;
+using std::end;
+using std::fill;
+
+namespace drm_utils {
+
+DRMLogger *DRMLogger::s_instance = nullptr;
+DRMMaster *DRMMaster::s_instance = nullptr;
+mutex DRMMaster::s_lock;
+
+int DRMMaster::GetInstance(DRMMaster **master) {
+  lock_guard<mutex> obj(s_lock);
+
+  if (!s_instance) {
+    s_instance = new DRMMaster();
+    if (s_instance->Init() < 0) {
+      delete s_instance;
+      s_instance = nullptr;
+      return -ENODEV;
+    }
+  }
+
+  *master = s_instance;
+  return 0;
+}
+
+int DRMMaster::Init() {
+  dev_fd_ = drmOpen("msm_drm", nullptr);
+  if (dev_fd_ < 0) {
+    DRM_LOGE("drmOpen failed with error %d", dev_fd_);
+    return -ENODEV;
+  }
+
+  return 0;
+}
+
+DRMMaster::~DRMMaster() {
+  drmClose(dev_fd_);
+  dev_fd_ = -1;
+}
+
+int DRMMaster::CreateFbId(const DRMBuffer &drm_buffer, uint32_t *gem_handle, uint32_t *fb_id) {
+  int ret = drmPrimeFDToHandle(dev_fd_, drm_buffer.fd, gem_handle);
+  if (ret) {
+    DRM_LOGE("drmPrimeFDToHandle failed with error %d", ret);
+    return ret;
+  }
+
+  struct drm_mode_fb_cmd2 cmd2 {};
+  cmd2.width = drm_buffer.width;
+  cmd2.height = drm_buffer.height;
+  cmd2.pixel_format = drm_buffer.drm_format;
+  cmd2.flags = DRM_MODE_FB_MODIFIERS;
+  fill(begin(cmd2.handles), begin(cmd2.handles) + drm_buffer.num_planes, *gem_handle);
+  copy(begin(drm_buffer.stride), end(drm_buffer.stride), begin(cmd2.pitches));
+  copy(begin(drm_buffer.offset), end(drm_buffer.offset), begin(cmd2.offsets));
+  fill(begin(cmd2.modifier), begin(cmd2.modifier) + drm_buffer.num_planes,
+       drm_buffer.drm_format_modifier);
+
+  if ((ret = drmIoctl(dev_fd_, DRM_IOCTL_MODE_ADDFB2, &cmd2))) {
+    DRM_LOGE("DRM_IOCTL_MODE_ADDFB2 failed with error %d", ret);
+    struct drm_gem_close gem_close = {};
+    gem_close.handle = *gem_handle;
+    int ret1 = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
+    if (ret1) {
+      DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret1);
+      return ret1;
+    }
+    return ret;
+  }
+
+  *fb_id = cmd2.fb_id;
+  return 0;
+}
+
+int DRMMaster::RemoveFbId(uint32_t gem_handle, uint32_t fb_id) {
+  struct drm_gem_close gem_close = {};
+  gem_close.handle = gem_handle;
+  int ret = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
+  if (ret) {
+    DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", errno);
+  }
+
+#ifdef DRM_IOCTL_MSM_RMFB2
+  ret = drmIoctl(dev_fd_, DRM_IOCTL_MSM_RMFB2, &fb_id);
+  if (ret) {
+    DRM_LOGE("drmIoctl::DRM_IOCTL_MSM_RMFB2 failed for fb_id %d with error %d", fb_id, errno);
+  }
+#else
+  ret = drmModeRmFB(dev_fd_, fb_id);
+  if (ret) {
+    DRM_LOGE("drmModeRmFB failed for fb_id %d with error %d", fb_id, ret);
+  }
+#endif
+  return ret;
+}
+
+}  // namespace drm_utils
diff --git a/msm8998/libdrmutils/drm_master.h b/msm8998/libdrmutils/drm_master.h
new file mode 100644
index 0000000..8c32a1b
--- /dev/null
+++ b/msm8998/libdrmutils/drm_master.h
@@ -0,0 +1,94 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __DRM_MASTER_H__
+#define __DRM_MASTER_H__
+
+#include <mutex>
+
+#include "drm_logger.h"
+
+namespace drm_utils {
+
+struct DRMBuffer {
+  int fd = -1;
+  uint32_t width = 0;
+  uint32_t height = 0;
+  uint32_t drm_format = 0;
+  uint64_t drm_format_modifier = 0;
+  uint32_t stride[4] = {};
+  uint32_t offset[4] = {};
+  uint32_t num_planes = 1;
+};
+
+class DRMMaster {
+ public:
+  ~DRMMaster();
+  /* Converts from ION fd --> Prime Handle --> FB_ID.
+   * Input:
+   *   drm_buffer: A DRMBuffer obj that packages description of buffer
+   * Output:
+   *   fb_id: Pointer to store DRM framebuffer id into
+   * Returns:
+   *   ioctl error code
+   */
+  int CreateFbId(const DRMBuffer &drm_buffer, uint32_t *gem_handle, uint32_t *fb_id);
+  /* Removes the fb_id from DRM
+   * Input:
+   *   fb_id: DRM FB to be removed
+   * Returns:
+   *   ioctl error code
+   */
+  int RemoveFbId(uint32_t gem_handle, uint32_t fb_id);
+  /* Poplulates master DRM fd
+   * Input:
+   *   fd: Pointer to store master fd into
+   */
+  void GetHandle(int *fd) { *fd = dev_fd_; }
+
+  /* Creates an instance of DRMMaster if it doesn't exist and initializes it. Threadsafe.
+   * Input:
+   *   master: Pointer to store a pointer to the instance
+   * Returns:
+   *   -ENODEV if device cannot be opened or initilization fails
+   */
+  static int GetInstance(DRMMaster **master);
+
+ private:
+  DRMMaster() {}
+  int Init();
+
+  int dev_fd_ = -1;              // Master fd for DRM
+  static DRMMaster *s_instance;  // Singleton instance
+  static std::mutex s_lock;
+};
+
+}  // namespace drm_utils
+
+#endif  // __DRM_MASTER_H__
diff --git a/msm8998/libdrmutils/drm_res_mgr.cpp b/msm8998/libdrmutils/drm_res_mgr.cpp
new file mode 100644
index 0000000..1d29495
--- /dev/null
+++ b/msm8998/libdrmutils/drm_res_mgr.cpp
@@ -0,0 +1,151 @@
+/*
+* Copyright (c) 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 <errno.h>
+
+#include "drm_master.h"
+#include "drm_res_mgr.h"
+
+#define DEBUG 0
+#define __CLASS__ "DRMResMgr"
+
+using std::mutex;
+using std::lock_guard;
+
+namespace drm_utils {
+
+DRMResMgr *DRMResMgr::s_instance = nullptr;
+mutex DRMResMgr::s_lock;
+
+static bool GetConnector(int dev_fd, drmModeRes *res, drmModeConnector **connector) {
+  for (auto i = 0; i < res->count_connectors; i++) {
+    drmModeConnector *conn = drmModeGetConnector(dev_fd, res->connectors[i]);
+    if (conn && conn->connector_type == DRM_MODE_CONNECTOR_DSI && conn->count_modes &&
+        conn->connection == DRM_MODE_CONNECTED) {
+      *connector = conn;
+      DRM_LOGI("Found connector %d", conn->connector_id);
+      return true;
+    }
+  }
+
+  return false;
+}
+
+static bool GetEncoder(int dev_fd, drmModeConnector *conn, drmModeEncoder **encoder) {
+  for (auto i = 0; i < conn->count_encoders; i++) {
+    drmModeEncoder *enc = drmModeGetEncoder(dev_fd, conn->encoders[i]);
+    if (enc && enc->encoder_type == DRM_MODE_ENCODER_DSI) {
+      *encoder = enc;
+      DRM_LOGI("Found encoder %d", enc->encoder_id);
+      return true;
+    }
+  }
+  return false;
+}
+
+static bool GetCrtc(int dev_fd, drmModeRes *res, drmModeEncoder *enc, drmModeCrtc **crtc) {
+  for (auto i = 0; i < res->count_crtcs; i++) {
+    if (enc->possible_crtcs & (1 << i)) {
+      drmModeCrtc *c = drmModeGetCrtc(dev_fd, res->crtcs[i]);
+      if (c) {
+        *crtc = c;
+        DRM_LOGI("Found crtc %d", c->crtc_id);
+        return true;
+      }
+    }
+  }
+
+  return false;
+}
+
+#define __CLASS__ "DRMResMgr"
+
+int DRMResMgr::GetInstance(DRMResMgr **res_mgr) {
+  lock_guard<mutex> obj(s_lock);
+
+  if (!s_instance) {
+    s_instance = new DRMResMgr();
+    if (s_instance->Init() < 0) {
+      delete s_instance;
+      s_instance = nullptr;
+      return -ENODEV;
+    }
+  }
+
+  *res_mgr = s_instance;
+  return 0;
+}
+
+int DRMResMgr::Init() {
+  DRMMaster *master = nullptr;
+  int dev_fd = -1;
+
+  int ret = DRMMaster::GetInstance(&master);
+  if (ret < 0) {
+    return ret;
+  }
+
+  master->GetHandle(&dev_fd);
+  drmModeRes *res = drmModeGetResources(dev_fd);
+  if (res == nullptr) {
+    DRM_LOGE("drmModeGetResources failed");
+    return -ENODEV;
+  }
+
+  drmModeConnector *conn = nullptr;
+  if (!GetConnector(dev_fd, res, &conn)) {
+    DRM_LOGE("Failed to find a connector");
+    return -ENODEV;
+  }
+
+  drmModeEncoder *enc = nullptr;
+  if (!GetEncoder(dev_fd, conn, &enc)) {
+    DRM_LOGE("Failed to find an encoder");
+    drmModeFreeConnector(conn);
+    return -ENODEV;
+  }
+
+  drmModeCrtc *crtc = nullptr;
+  if (!GetCrtc(dev_fd, res, enc, &crtc)) {
+    DRM_LOGE("Failed to find a crtc");
+    drmModeFreeEncoder(enc);
+    drmModeFreeConnector(conn);
+    drmModeFreeResources(res);
+    return -ENODEV;
+  }
+
+  res_ = res;
+  conn_ = conn;
+  enc_ = enc;
+  crtc_ = crtc;
+
+  return 0;
+}
+
+}  // namespace drm_utils
diff --git a/msm8998/libdrmutils/drm_res_mgr.h b/msm8998/libdrmutils/drm_res_mgr.h
new file mode 100644
index 0000000..3a8378c
--- /dev/null
+++ b/msm8998/libdrmutils/drm_res_mgr.h
@@ -0,0 +1,72 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __DRM_RES_MGR_H__
+#define __DRM_RES_MGR_H__
+
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+#include <mutex>
+
+namespace drm_utils {
+
+class DRMResMgr {
+ public:
+  /* Returns the default connector id for primary panel */
+  void GetConnectorId(uint32_t *id) { *id = conn_->connector_id; }
+  /* Returns the default crtc id for primary pipeline */
+  void GetCrtcId(uint32_t *id) { *id = crtc_->crtc_id; }
+  /* Returns the default mode currently used by the connector */
+  void GetMode(drmModeModeInfo *mode) { *mode = conn_->modes[0]; }
+  /* Returns the panel dimensions in mm */
+  void GetDisplayDimInMM(uint32_t *w, uint32_t *h) {
+    *w = conn_->mmWidth;
+    *h = conn_->mmHeight;
+  }
+
+  /* Creates and initializes an instance of DRMResMgr. On success, returns a pointer to it, on
+   * failure returns -ENODEV */
+  static int GetInstance(DRMResMgr **res_mgr);
+
+ private:
+  int Init();
+
+  drmModeRes *res_ = nullptr;
+  drmModeConnector *conn_ = nullptr;
+  drmModeEncoder *enc_ = nullptr;
+  drmModeCrtc *crtc_ = nullptr;
+
+  static DRMResMgr *s_instance;
+  static std::mutex s_lock;
+};
+
+}  // namespace drm_utils
+
+#endif  // __DRM_RES_MGR_H__
diff --git a/msm8998/libgralloc/Android.mk b/msm8998/libgralloc/Android.mk
index 3c25f67..0042bdd 100644
--- a/msm8998/libgralloc/Android.mk
+++ b/msm8998/libgralloc/Android.mk
@@ -24,7 +24,7 @@
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libmemalloc libqdMetaData libqdutils
 ifneq ($(TARGET_IS_HEADLESS), true)
-LOCAL_SHARED_LIBRARIES        += libGLESv1_CM
+LOCAL_SHARED_LIBRARIES        += libGLESv1_CM libdrmutils
 endif
 LOCAL_CFLAGS                  := $(common_flags) -DLOG_TAG=\"qdgralloc\" -Wno-sign-conversion
 LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps) $(kernel_deps)
@@ -32,12 +32,6 @@
 LOCAL_COPY_HEADERS_TO         := $(common_header_export_path)
 LOCAL_COPY_HEADERS            := gralloc_priv.h gr.h adreno_utils.h
 
-LOCAL_STATIC_LIBRARIES        := libgralloc1-adapter
-LOCAL_SHARED_LIBRARIES        += libsync
-ifeq ($(TARGET_USES_GRALLOC1), true)
-LOCAL_CFLAGS += -DADVERTISE_GRALLOC1
-endif
-
 include $(BUILD_SHARED_LIBRARY)
 
 # MemAlloc Library
diff --git a/msm8998/libgralloc/gpu.cpp b/msm8998/libgralloc/gpu.cpp
index 461a4ab..848c741 100644
--- a/msm8998/libgralloc/gpu.cpp
+++ b/msm8998/libgralloc/gpu.cpp
@@ -15,28 +15,221 @@
  * limitations under the License.
  */
 
-#include <atomic>
 #include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <cutils/properties.h>
 #include <sys/mman.h>
+#include <linux/msm_ion.h>
+#ifdef COMPILE_DRM
+#include <drm/drm_fourcc.h>
+#include <drm_master.h>
+#endif
+#include <qdMetaData.h>
+#include <qd_utils.h>
+
+#include <algorithm>
 
 #include "gr.h"
 #include "gpu.h"
 #include "memalloc.h"
 #include "alloc_controller.h"
-#include <qdMetaData.h>
-#include <linux/msm_ion.h>
+
+#ifdef COMPILE_DRM
+#ifndef DRM_FORMAT_MOD_QCOM_COMPRESSED
+#define DRM_FORMAT_MOD_QCOM_COMPRESSED fourcc_mod_code(QCOM, 1)
+#endif
+#endif
 
 using namespace gralloc;
 
-static uint64_t next_backing_store_id()
-{
-    static std::atomic<uint64_t> next_id(1);
-    return next_id++;
+#ifdef COMPILE_DRM
+using namespace drm_utils;
+
+static int getPlaneStrideOffset(private_handle_t *hnd, uint32_t *stride,
+        uint32_t *offset, uint32_t *num_planes) {
+    struct android_ycbcr yuvInfo = {};
+    *num_planes = 1;
+
+    switch (hnd->format) {
+        case HAL_PIXEL_FORMAT_RGB_565:
+        case HAL_PIXEL_FORMAT_BGR_565:
+        case HAL_PIXEL_FORMAT_RGBA_5551:
+        case HAL_PIXEL_FORMAT_RGBA_4444:
+            stride[0] = hnd->width * 2;
+            break;
+        case HAL_PIXEL_FORMAT_RGB_888:
+            stride[0] = hnd->width * 3;
+            break;
+        case HAL_PIXEL_FORMAT_RGBA_8888:
+        case HAL_PIXEL_FORMAT_BGRA_8888:
+        case HAL_PIXEL_FORMAT_RGBX_8888:
+        case HAL_PIXEL_FORMAT_BGRX_8888:
+        case HAL_PIXEL_FORMAT_RGBA_1010102:
+        case HAL_PIXEL_FORMAT_ARGB_2101010:
+        case HAL_PIXEL_FORMAT_RGBX_1010102:
+        case HAL_PIXEL_FORMAT_XRGB_2101010:
+        case HAL_PIXEL_FORMAT_BGRA_1010102:
+        case HAL_PIXEL_FORMAT_ABGR_2101010:
+        case HAL_PIXEL_FORMAT_BGRX_1010102:
+        case HAL_PIXEL_FORMAT_XBGR_2101010:
+            stride[0] = hnd->width * 4;
+            break;
+    }
+
+    // Format is RGB
+    if (stride[0]) {
+        return 0;
+    }
+
+    (*num_planes)++;
+    int ret = getYUVPlaneInfo(hnd, &yuvInfo);
+    if (ret < 0) {
+        ALOGE("%s failed", __FUNCTION__);
+        return ret;
+    }
+
+    stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
+    offset[0] = static_cast<uint32_t>(
+                    reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
+    stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
+    switch (hnd->format) {
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+        case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+        case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+        case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+        case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+            offset[1] = static_cast<uint32_t>(
+                    reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
+            break;
+        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+        case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+        case HAL_PIXEL_FORMAT_YCrCb_422_SP:
+            offset[1] = static_cast<uint32_t>(
+                    reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
+            break;
+        case HAL_PIXEL_FORMAT_YV12:
+            offset[1] = static_cast<uint32_t>(
+                    reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
+            stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
+            offset[2] = static_cast<uint32_t>(
+                    reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
+            (*num_planes)++;
+            break;
+        default:
+            ALOGW("%s: Unsupported format %s", __FUNCTION__,
+                    qdutils::GetHALPixelFormatString(hnd->format));
+    }
+
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
+        std::fill(offset, offset + 4, 0);
+    }
+
+    return 0;
 }
 
+static void getDRMFormat(int hal_format, int flags, uint32_t *drm_format,
+        uint64_t *drm_format_modifier) {
+
+    if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
+        *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
+    }
+
+    switch (hal_format) {
+        case HAL_PIXEL_FORMAT_RGBA_8888:
+            *drm_format = DRM_FORMAT_RGBA8888;
+            break;
+        case HAL_PIXEL_FORMAT_RGBA_5551:
+            *drm_format = DRM_FORMAT_RGBA5551;
+            break;
+        case HAL_PIXEL_FORMAT_RGBA_4444:
+            *drm_format = DRM_FORMAT_RGBA4444;
+            break;
+        case HAL_PIXEL_FORMAT_BGRA_8888:
+            *drm_format = DRM_FORMAT_BGRA8888;
+            break;
+        case HAL_PIXEL_FORMAT_RGBX_8888:
+            *drm_format = DRM_FORMAT_RGBX8888;
+            break;
+        case HAL_PIXEL_FORMAT_BGRX_8888:
+            *drm_format = DRM_FORMAT_BGRX8888;
+            break;
+        case HAL_PIXEL_FORMAT_RGB_888:
+            *drm_format = DRM_FORMAT_RGB888;
+            break;
+        case HAL_PIXEL_FORMAT_RGB_565:
+            *drm_format = DRM_FORMAT_RGB565;
+            break;
+        case HAL_PIXEL_FORMAT_BGR_565:
+            *drm_format = DRM_FORMAT_BGR565;
+            break;
+        case HAL_PIXEL_FORMAT_RGBA_1010102:
+            *drm_format = DRM_FORMAT_RGBA1010102;
+            break;
+        case HAL_PIXEL_FORMAT_ARGB_2101010:
+            *drm_format = DRM_FORMAT_ARGB2101010;
+            break;
+        case HAL_PIXEL_FORMAT_RGBX_1010102:
+            *drm_format = DRM_FORMAT_RGBX1010102;
+            break;
+        case HAL_PIXEL_FORMAT_XRGB_2101010:
+            *drm_format = DRM_FORMAT_XRGB2101010;
+            break;
+        case HAL_PIXEL_FORMAT_BGRA_1010102:
+            *drm_format = DRM_FORMAT_BGRA1010102;
+            break;
+        case HAL_PIXEL_FORMAT_ABGR_2101010:
+            *drm_format = DRM_FORMAT_ABGR2101010;
+            break;
+        case HAL_PIXEL_FORMAT_BGRX_1010102:
+            *drm_format = DRM_FORMAT_BGRX1010102;
+            break;
+        case HAL_PIXEL_FORMAT_XBGR_2101010:
+            *drm_format = DRM_FORMAT_XBGR2101010;
+            break;
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+            *drm_format = DRM_FORMAT_NV12;
+            break;
+        case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+            *drm_format = DRM_FORMAT_NV12;
+            break;
+        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+            *drm_format = DRM_FORMAT_NV12;
+            *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
+            break;
+        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+            *drm_format = DRM_FORMAT_NV21;
+            break;
+        case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+            *drm_format = DRM_FORMAT_NV21;
+            break;
+        case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+            // TODO *drm_format = DRM_FORMAT_P010;
+            break;
+        case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+            // TODO *drm_format = DRM_FORMAT_P010;
+            // *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED |
+            //        DRM_FORMAT_MOD_QCOM_TIGHT;
+            break;
+        case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+            *drm_format = DRM_FORMAT_NV16;
+            break;
+        case HAL_PIXEL_FORMAT_YCrCb_422_SP:
+            *drm_format = DRM_FORMAT_NV61;
+            break;
+        case HAL_PIXEL_FORMAT_YV12:
+            *drm_format = DRM_FORMAT_YVU420;
+            break;
+        default:
+            ALOGW("%s: Unsupported format %s", __FUNCTION__,
+                    qdutils::GetHALPixelFormatString(hal_format));
+    }
+}
+#endif
+
 gpu_context_t::gpu_context_t(const private_module_t* module,
                              IAllocController* alloc_ctrl ) :
     mAllocCtrl(alloc_ctrl)
@@ -176,11 +369,46 @@
         ColorSpace_t colorSpace = ITU_R_601;
         setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
 
+#ifdef COMPILE_DRM
+        if (qdutils::getDriverType() == qdutils::DriverType::DRM &&
+                usage & GRALLOC_USAGE_HW_COMPOSER) {
+            DRMBuffer buf = {};
+            int ret = getPlaneStrideOffset(hnd, buf.stride, buf.offset,
+                    &buf.num_planes);
+            if (ret < 0) {
+                ALOGE("%s failed", __FUNCTION__);
+                return ret;
+            }
+
+            buf.fd = hnd->fd;
+            buf.width = hnd->width;
+            buf.height = hnd->height;
+            getDRMFormat(hnd->format, flags, &buf.drm_format,
+                    &buf.drm_format_modifier);
+
+            DRMMaster *master = nullptr;
+            ret = DRMMaster::GetInstance(&master);
+            if (ret < 0) {
+                ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
+                return ret;
+            }
+
+            ret = master->CreateFbId(buf, &hnd->gem_handle, &hnd->fb_id);
+            if (ret < 0) {
+                ALOGE("%s: CreateFbId failed. width %d, height %d, " \
+                        "format: %s, stride %u, error %d", __FUNCTION__,
+                        buf.width, buf.height,
+                        qdutils::GetHALPixelFormatString(hnd->format),
+                        buf.stride[0], errno);
+                return ret;
+            }
+        }
+#endif
+
         *pHandle = hnd;
     }
 
     ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
-
     return err;
 }
 
@@ -349,11 +577,6 @@
         return err;
     }
 
-    auto hnd = (private_handle_t*) *pHandle;
-    hnd->backing_store = next_backing_store_id();
-    hnd->original_width = w;
-    hnd->original_format = format;
-
     *pStride = alignedw;
     return 0;
 }
@@ -381,6 +604,23 @@
         if (err)
             return err;
     }
+
+#ifdef COMPILE_DRM
+    if (hnd->fb_id) {
+        DRMMaster *master = nullptr;
+        int ret = DRMMaster::GetInstance(&master);
+        if (ret < 0) {
+            ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
+            return ret;
+        }
+        ret = master->RemoveFbId(hnd->gem_handle, hnd->fb_id);
+        if (ret < 0) {
+            ALOGE("%s: Removing fb_id %d failed with error %d", __FUNCTION__,
+                    hnd->fb_id, errno);
+        }
+    }
+#endif
+
     delete hnd;
     return 0;
 }
diff --git a/msm8998/libgralloc/gralloc.cpp b/msm8998/libgralloc/gralloc.cpp
index eb373df..237b8a8 100644
--- a/msm8998/libgralloc/gralloc.cpp
+++ b/msm8998/libgralloc/gralloc.cpp
@@ -23,7 +23,6 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <cutils/properties.h>
-#include <gralloc1-adapter.h>
 
 #include "gr.h"
 #include "gpu.h"
@@ -70,11 +69,7 @@
     .base = {
         .common = {
             .tag = HARDWARE_MODULE_TAG,
-#ifdef ADVERTISE_GRALLOC1
-            .version_major = GRALLOC1_ADAPTER_MODULE_API_VERSION_1_0,
-#else
             .version_major = 1,
-#endif
             .version_minor = 0,
             .id = GRALLOC_HARDWARE_MODULE_ID,
             .name = "Graphics Memory Allocator Module",
@@ -103,11 +98,6 @@
                         hw_device_t** device)
 {
     int status = -EINVAL;
-#ifdef ADVERTISE_GRALLOC1
-    if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
-        return gralloc1_adapter_device_open(module, name, device);
-    }
-#endif
     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
         const private_module_t* m = reinterpret_cast<const private_module_t*>(
             module);
diff --git a/msm8998/libgralloc/gralloc_priv.h b/msm8998/libgralloc/gralloc_priv.h
index 9e5c39c..5782180 100644
--- a/msm8998/libgralloc/gralloc_priv.h
+++ b/msm8998/libgralloc/gralloc_priv.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2008 The Android Open Source Project
- * Copyright (c) 2011 - 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011 - 2017, The Linux Foundation. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -247,12 +247,8 @@
         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
-
-        int original_width;
-        int original_format;
-        int producer_usage;
-        int consumer_usage;
-        uint64_t backing_store __attribute__((aligned(8)));
+        unsigned int gem_handle;
+        unsigned int fb_id;
 
 #ifdef __cplusplus
         static const int sNumFds = 2;
@@ -269,10 +265,7 @@
             base(0), offset_metadata(0), gpuaddr(0),
             format(format), width(width), height(height),
             base_metadata(0), unaligned_width(width),
-            unaligned_height(height),
-            original_width(0), original_format(0),
-            producer_usage(0), consumer_usage(0),
-            backing_store(0)
+            unaligned_height(height), gem_handle(0), fb_id(0)
         {
             version = (int) sizeof(native_handle);
             numInts = sNumInts();
diff --git a/msm8998/libgralloc/mapper.cpp b/msm8998/libgralloc/mapper.cpp
index 0042258..acf5e2c 100644
--- a/msm8998/libgralloc/mapper.cpp
+++ b/msm8998/libgralloc/mapper.cpp
@@ -35,8 +35,6 @@
 #include <hardware/hardware.h>
 #include <hardware/gralloc.h>
 
-#include <gralloc1-adapter.h>
-
 #include "gralloc_priv.h"
 #include "gr.h"
 #include "alloc_controller.h"
@@ -301,68 +299,6 @@
 
 /*****************************************************************************/
 
-static bool isYUV(private_handle_t* hnd)
-{
-    bool is_yuv;
-
-    switch (hnd->format) {
-        //Semiplanar
-        case HAL_PIXEL_FORMAT_YCbCr_420_SP:
-        case HAL_PIXEL_FORMAT_YCbCr_422_SP:
-        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
-        case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
-        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
-        case HAL_PIXEL_FORMAT_YCrCb_422_SP:
-        case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
-        case HAL_PIXEL_FORMAT_NV21_ZSL:
-        case HAL_PIXEL_FORMAT_RAW10:
-        case HAL_PIXEL_FORMAT_RAW16:
-        //Planar
-        case HAL_PIXEL_FORMAT_YV12:
-            is_yuv = true;
-        break;
-        //Unsupported formats
-        case HAL_PIXEL_FORMAT_YCbCr_422_I:
-        case HAL_PIXEL_FORMAT_YCrCb_422_I:
-        case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
-        default:
-            is_yuv = false;
-            break;
-    }
-
-    return is_yuv;
-}
-
-static void ycbcr_to_flexible_layout(const struct android_ycbcr* ycbcr,
-        struct android_flex_layout* layout)
-{
-    layout->format = FLEX_FORMAT_YCbCr;
-    layout->num_planes = 3;
-
-    for (uint32_t i = 0; i < layout->num_planes; i++) {
-        layout->planes[i].bits_per_component = 8;
-        layout->planes[i].bits_used = 8;
-        layout->planes[i].h_increment = 1;
-        layout->planes[i].v_increment = 1;
-        layout->planes[i].h_subsampling = 2;
-        layout->planes[i].v_subsampling = 2;
-    }
-
-    layout->planes[0].top_left = (uint8_t*)ycbcr->y;
-    layout->planes[0].component = FLEX_COMPONENT_Y;
-    layout->planes[0].v_increment = (int32_t)ycbcr->ystride;
-
-    layout->planes[1].top_left = (uint8_t*)ycbcr->cb;
-    layout->planes[1].component = FLEX_COMPONENT_Cb;
-    layout->planes[1].h_increment = (int32_t)ycbcr->chroma_step;
-    layout->planes[1].v_increment = (int32_t)ycbcr->cstride;
-
-    layout->planes[2].top_left = (uint8_t*)ycbcr->cr;
-    layout->planes[2].component = FLEX_COMPONENT_Cr;
-    layout->planes[2].h_increment = (int32_t)ycbcr->chroma_step;
-    layout->planes[2].v_increment = (int32_t)ycbcr->cstride;
-}
-
 int gralloc_perform(struct gralloc_module_t const* module,
                     int operation, ... )
 {
@@ -579,102 +515,6 @@
                     res = 0;
                 }
             } break;
-        case GRALLOC1_ADAPTER_PERFORM_GET_REAL_MODULE_API_VERSION_MINOR:
-            {
-                auto outMinorVersion = va_arg(args, int*);
-                *outMinorVersion = 1; // GRALLOC_MODULE_API_VERSION_0_1
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_SET_USAGES:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto producerUsage = va_arg(args, int);
-                auto consumerUsage = va_arg(args, int);
-                hnd->producer_usage = producerUsage;
-                hnd->consumer_usage = consumerUsage;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_DIMENSIONS:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outWidth = va_arg(args, int*);
-                auto outHeight = va_arg(args, int*);
-                *outWidth = hnd->original_width;
-                *outHeight = hnd->height;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_FORMAT:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outFormat = va_arg(args, int*);
-                *outFormat = hnd->original_format;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_PRODUCER_USAGE:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outUsage = va_arg(args, int*);
-                *outUsage = hnd->producer_usage;
-            } break;
-       case GRALLOC1_ADAPTER_PERFORM_GET_CONSUMER_USAGE:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outUsage = va_arg(args, int*);
-                *outUsage = hnd->consumer_usage;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_BACKING_STORE:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outBackingStore = va_arg(args, uint64_t*);
-                *outBackingStore = hnd->backing_store;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_NUM_FLEX_PLANES:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outNumFlexPlanes = va_arg(args, int*);
-
-                (void) hnd;
-                // for simpilicity
-                *outNumFlexPlanes = 4;
-            } break;
-
-        case GRALLOC1_ADAPTER_PERFORM_GET_STRIDE:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto outStride = va_arg(args, int*);
-                *outStride = hnd->width;
-            } break;
-        case GRALLOC1_ADAPTER_PERFORM_LOCK_FLEX:
-            {
-                auto hnd =  va_arg(args, private_handle_t*);
-                auto producerUsage = va_arg(args, int);
-                auto consumerUsage = va_arg(args, int);
-                auto left = va_arg(args, int);
-                auto top = va_arg(args, int);
-                auto width = va_arg(args, int);
-                auto height = va_arg(args, int);
-                auto outLayout = va_arg(args, android_flex_layout*);
-                // always -1
-                auto acquireFence = va_arg(args, int);
-                (void) acquireFence;
-
-                // TODO lock RGB as a flexible format
-                if (!isYUV(hnd)) {
-                    return -EINVAL;
-                }
-
-                struct android_ycbcr ycbcr;
-                res = gralloc_lock_ycbcr(module, hnd,
-                        producerUsage | consumerUsage,
-                        left, top, width, height, &ycbcr);
-                if (res != 0) {
-                    return res;
-                }
-
-                ycbcr_to_flexible_layout(&ycbcr, outLayout);
-            } break;
         default:
             break;
     }
diff --git a/msm8998/libgralloc1/Android.mk b/msm8998/libgralloc1/Android.mk
index 874335e..d553034 100644
--- a/msm8998/libgralloc1/Android.mk
+++ b/msm8998/libgralloc1/Android.mk
@@ -11,6 +11,9 @@
                                  external/libcxx/include/
 
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libqdMetaData libsync libqdutils
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_SHARED_LIBRARIES        += libdrmutils
+endif
 LOCAL_CFLAGS                  := $(common_flags) -DLOG_TAG=\"qdgralloc\" -Wall -std=c++11 -Werror
 LOCAL_CFLAGS                  += -isystem  $(kernel_includes)
 LOCAL_CLANG                   := true
diff --git a/msm8998/libgralloc1/gr_allocator.cpp b/msm8998/libgralloc1/gr_allocator.cpp
index fefd664..3c01fbd 100644
--- a/msm8998/libgralloc1/gr_allocator.cpp
+++ b/msm8998/libgralloc1/gr_allocator.cpp
@@ -29,6 +29,7 @@
 
 #include <cutils/log.h>
 #include <algorithm>
+#include <vector>
 
 #include "gr_utils.h"
 #include "gr_allocator.h"
@@ -68,6 +69,9 @@
 #define ION_SC_PREVIEW_FLAGS ION_SECURE
 #endif
 
+using std::vector;
+using std::shared_ptr;
+
 namespace gralloc1 {
 
 Allocator::Allocator() : ion_allocator_(NULL), adreno_helper_(NULL) {
@@ -117,43 +121,6 @@
   return ret;
 }
 
-// Allocates buffer from width, height and format into a
-// private_handle_t. It is the responsibility of the caller
-// to free the buffer using the FreeBuffer function
-int Allocator::AllocateBuffer(const BufferDescriptor &descriptor, private_handle_t **pHnd) {
-  AllocData data;
-  unsigned int aligned_w, aligned_h;
-  data.base = 0;
-  data.fd = -1;
-  data.offset = 0;
-  data.align = (unsigned int)getpagesize();
-  int format = descriptor.GetFormat();
-  gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
-  gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
-  GetBufferSizeAndDimensions(descriptor, &data.size, &aligned_w, &aligned_h);
-
-  int err = AllocateMem(&data, prod_usage, cons_usage);
-  if (0 != err) {
-    ALOGE("%s: allocate failed", __FUNCTION__);
-    return -ENOMEM;
-  }
-
-  if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
-    data.alloc_type |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
-  }
-
-  // Metadata is not allocated. would be empty
-  private_handle_t *hnd = new private_handle_t(
-      data.fd, data.size, INT(data.alloc_type), 0, INT(format), INT(aligned_w), INT(aligned_h), -1,
-      0, 0, descriptor.GetWidth(), descriptor.GetHeight(), prod_usage, cons_usage);
-  hnd->base = (uint64_t)data.base;
-  hnd->offset = data.offset;
-  hnd->gpuaddr = 0;
-  *pHnd = hnd;
-
-  return 0;
-}
-
 int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
   if (ion_allocator_) {
     return ion_allocator_->MapBuffer(base, size, offset, fd);
@@ -162,9 +129,10 @@
   return -EINVAL;
 }
 
-int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd) {
+int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
+                          int handle) {
   if (ion_allocator_) {
-    return ion_allocator_->FreeBuffer(base, size, offset, fd);
+    return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
   }
 
   return -EINVAL;
@@ -178,8 +146,9 @@
   return -EINVAL;
 }
 
-bool Allocator::CheckForBufferSharing(uint32_t num_descriptors, const BufferDescriptor *descriptors,
-                                      int *max_index) {
+bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
+                                      const vector<shared_ptr<BufferDescriptor>>& descriptors,
+                                      ssize_t *max_index) {
   unsigned int cur_heap_id = 0, prev_heap_id = 0;
   unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
   unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
@@ -190,8 +159,8 @@
   *max_index = -1;
   for (uint32_t i = 0; i < num_descriptors; i++) {
     // Check Cached vs non-cached and all the ION flags
-    cur_uncached = UseUncached(descriptors[i].GetProducerUsage());
-    GetIonHeapInfo(descriptors[i].GetProducerUsage(), descriptors[i].GetConsumerUsage(),
+    cur_uncached = UseUncached(descriptors[i]->GetProducerUsage());
+    GetIonHeapInfo(descriptors[i]->GetProducerUsage(), descriptors[i]->GetConsumerUsage(),
                    &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
 
     if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
@@ -200,8 +169,8 @@
     }
 
     // For same format type, find the descriptor with bigger size
-    GetAlignedWidthAndHeight(descriptors[i], &alignedw, &alignedh);
-    unsigned int size = GetSize(descriptors[i], alignedw, alignedh);
+    GetAlignedWidthAndHeight(*descriptors[i], &alignedw, &alignedh);
+    unsigned int size = GetSize(*descriptors[i], alignedw, alignedh);
     if (max_size < size) {
       *max_index = INT(i);
       max_size = size;
@@ -552,7 +521,7 @@
                                unsigned int *alloc_type, unsigned int *ion_flags) {
   unsigned int heap_id = 0;
   unsigned int type = 0;
-  int flags = 0;
+  unsigned int flags = 0;
   if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
     if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
       heap_id = ION_HEAP(SD_HEAP_ID);
@@ -597,7 +566,7 @@
   }
 
   *alloc_type = type;
-  *ion_flags = (unsigned int)flags;
+  *ion_flags = flags;
   *ion_heap_id = heap_id;
 
   return;
diff --git a/msm8998/libgralloc1/gr_allocator.h b/msm8998/libgralloc1/gr_allocator.h
index da4fbee..e73bd8c 100644
--- a/msm8998/libgralloc1/gr_allocator.h
+++ b/msm8998/libgralloc1/gr_allocator.h
@@ -36,6 +36,8 @@
 #define SECURE_ALIGN SZ_1M
 #endif
 
+#include <vector>
+
 #include "gralloc_priv.h"
 #include "gr_buf_descriptor.h"
 #include "gr_adreno_info.h"
@@ -48,15 +50,15 @@
   Allocator();
   ~Allocator();
   bool Init();
-  int AllocateBuffer(const BufferDescriptor &descriptor, private_handle_t **pHnd);
   int MapBuffer(void **base, unsigned int size, unsigned int offset, int fd);
-  int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd);
+  int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle);
   int CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op);
   int AllocateMem(AllocData *data, gralloc1_producer_usage_t prod_usage,
                   gralloc1_consumer_usage_t cons_usage);
   // @return : index of the descriptor with maximum buffer size req
-  bool CheckForBufferSharing(uint32_t num_descriptors, const BufferDescriptor *descriptors,
-                             int *max_index);
+  bool CheckForBufferSharing(uint32_t num_descriptors,
+                             const std::vector<std::shared_ptr<BufferDescriptor>>& descriptors,
+                             ssize_t *max_index);
   int GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
                            gralloc1_consumer_usage_t cons_usage, int format);
   unsigned int GetSize(const BufferDescriptor &d, unsigned int alignedw, unsigned int alignedh);
diff --git a/msm8998/libgralloc1/gr_buf_descriptor.h b/msm8998/libgralloc1/gr_buf_descriptor.h
index 1c3572d..95386fa 100644
--- a/msm8998/libgralloc1/gr_buf_descriptor.h
+++ b/msm8998/libgralloc1/gr_buf_descriptor.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-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
@@ -32,18 +32,18 @@
 
 #include <hardware/gralloc1.h>
 
-#define BUF_DESCRIPTOR(exp) reinterpret_cast<BufferDescriptor *>(exp)
-
+namespace gralloc1 {
 class BufferDescriptor {
  public:
-  BufferDescriptor() {}
+  BufferDescriptor() : id_(next_id_++) {}
 
   BufferDescriptor(int w, int h, int f)
       : width_(w),
         height_(h),
         format_(f),
         producer_usage_(GRALLOC1_PRODUCER_USAGE_NONE),
-        consumer_usage_(GRALLOC1_CONSUMER_USAGE_NONE) {}
+        consumer_usage_(GRALLOC1_CONSUMER_USAGE_NONE),
+        id_(next_id_++) {}
 
   BufferDescriptor(int w, int h, int f, gralloc1_producer_usage_t prod_usage,
                    gralloc1_consumer_usage_t cons_usage)
@@ -51,9 +51,8 @@
         height_(h),
         format_(f),
         producer_usage_(prod_usage),
-        consumer_usage_(cons_usage) {}
-
-  bool IsValid() { return (magic == kMagic); }
+        consumer_usage_(cons_usage),
+        id_(next_id_++) {}
 
   void SetConsumerUsage(gralloc1_consumer_usage_t usage) { consumer_usage_ = usage; }
 
@@ -76,15 +75,16 @@
 
   int GetFormat() const { return format_; }
 
- private:
-  static const int kMagic = 'gr1d';
+  gralloc1_buffer_descriptor_t GetId() const { return id_; }
 
-  int magic = kMagic;
+ private:
   int width_ = -1;
   int height_ = -1;
   int format_ = -1;
   gralloc1_producer_usage_t producer_usage_ = GRALLOC1_PRODUCER_USAGE_NONE;
   gralloc1_consumer_usage_t consumer_usage_ = GRALLOC1_CONSUMER_USAGE_NONE;
+  const gralloc1_buffer_descriptor_t id_;
+  static std::atomic<gralloc1_buffer_descriptor_t> next_id_;
 };
-
+};  // namespace gralloc1
 #endif  // __GR_BUF_DESCRIPTOR_H__
diff --git a/msm8998/libgralloc1/gr_buf_mgr.cpp b/msm8998/libgralloc1/gr_buf_mgr.cpp
index cea8ac9..51166d8 100644
--- a/msm8998/libgralloc1/gr_buf_mgr.cpp
+++ b/msm8998/libgralloc1/gr_buf_mgr.cpp
@@ -18,6 +18,7 @@
  */
 
 #include <utility>
+#include <vector>
 
 #include "qd_utils.h"
 #include "gr_priv_handle.h"
@@ -27,8 +28,9 @@
 #include "qdMetaData.h"
 
 namespace gralloc1 {
+std::atomic<gralloc1_buffer_descriptor_t> BufferDescriptor::next_id_(1);
 
-BufferManager::BufferManager() {
+BufferManager::BufferManager() : next_id_(0) {
   char property[PROPERTY_VALUE_MAX];
 
   // Map framebuffer memory
@@ -46,6 +48,29 @@
   }
 
   handles_map_.clear();
+  allocator_ = new Allocator();
+  allocator_->Init();
+}
+
+
+gralloc1_error_t BufferManager::CreateBufferDescriptor(
+    gralloc1_buffer_descriptor_t *descriptor_id) {
+  std::lock_guard<std::mutex> lock(locker_);
+  auto descriptor = std::make_shared<BufferDescriptor>();
+  descriptors_map_.emplace(descriptor->GetId(), descriptor);
+  *descriptor_id = descriptor->GetId();
+  return GRALLOC1_ERROR_NONE;
+}
+
+gralloc1_error_t BufferManager::DestroyBufferDescriptor(
+    gralloc1_buffer_descriptor_t descriptor_id) {
+  std::lock_guard<std::mutex> lock(locker_);
+  const auto descriptor = descriptors_map_.find(descriptor_id);
+  if (descriptor == descriptors_map_.end()) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  }
+  descriptors_map_.erase(descriptor);
+  return GRALLOC1_ERROR_NONE;
 }
 
 BufferManager::~BufferManager() {
@@ -54,14 +79,8 @@
   }
 }
 
-bool BufferManager::Init() {
-  allocator_ = new Allocator();
-
-  return allocator_->Init();
-}
-
 gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
-                                                const BufferDescriptor *descriptors,
+                                                const gralloc1_buffer_descriptor_t *descriptor_ids,
                                                 buffer_handle_t *out_buffers) {
   bool shared = true;
   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
@@ -70,10 +89,28 @@
   // client can ask to test the allocation by passing NULL out_buffers
   bool test_allocate = !out_buffers;
 
+  // Validate descriptors
+  std::vector<std::shared_ptr<BufferDescriptor>> descriptors;
+  for (uint32_t i = 0; i < num_descriptors; i++) {
+    const auto map_descriptor = descriptors_map_.find(descriptor_ids[i]);
+    if (map_descriptor == descriptors_map_.end()) {
+      return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+    } else {
+      descriptors.push_back(map_descriptor->second);
+    }
+  }
+
+  //  Resolve implementation defined formats
+  for (auto &descriptor : descriptors) {
+    descriptor->SetColorFormat(allocator_->GetImplDefinedFormat(descriptor->GetProducerUsage(),
+                                                                descriptor->GetConsumerUsage(),
+                                                                descriptor->GetFormat()));
+  }
+
   // Check if input descriptors can be supported AND
   // Find out if a single buffer can be shared for all the given input descriptors
   uint32_t i = 0;
-  int max_buf_index = -1;
+  ssize_t max_buf_index = -1;
   shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
 
   if (test_allocate) {
@@ -83,7 +120,7 @@
 
   if (shared && (max_buf_index >= 0)) {
     // Allocate one and duplicate/copy the handles for each descriptor
-    if (AllocateBuffer(descriptors[max_buf_index], &out_buffers[max_buf_index])) {
+    if (AllocateBuffer(*descriptors[UINT(max_buf_index)], &out_buffers[max_buf_index])) {
       return GRALLOC1_ERROR_NO_RESOURCES;
     }
 
@@ -92,20 +129,14 @@
       // Current assumption is even MetaData memory would be same
       // Need to revisit if there is a need for own metadata memory
       if (i != UINT(max_buf_index)) {
-        CreateSharedHandle(out_buffers[max_buf_index], descriptors[i], &out_buffers[i]);
-
-        // since we just created handle out of existing handle add it to map
-        locker_.lock();
-        handles_map_.insert(std::pair<private_handle_t const *, int>(
-            reinterpret_cast<private_handle_t const *>(out_buffers[i]), 1));
-        locker_.unlock();
+        CreateSharedHandle(out_buffers[max_buf_index], *descriptors[i], &out_buffers[i]);
       }
     }
   } else {
     // Buffer sharing is not feasible.
-    // Allocate seperate buffer for each descriptor
+    // Allocate separate buffer for each descriptor
     for (i = 0; i < num_descriptors; i++) {
-      if (AllocateBuffer(descriptors[i], &out_buffers[i])) {
+      if (AllocateBuffer(*descriptors[i], &out_buffers[i])) {
         return GRALLOC1_ERROR_NO_RESOURCES;
       }
     }
@@ -133,31 +164,41 @@
   int buffer_type = GetBufferType(descriptor.GetFormat());
 
   // Duplicate the fds
-  private_handle_t *out_hnd = new private_handle_t(
-      dup(input->fd), input->size, flags, buffer_type, descriptor.GetFormat(), INT(alignedw),
-      INT(alignedh), dup(input->fd_metadata), input->offset_metadata, input->base_metadata,
-      descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetProducerUsage(),
-      descriptor.GetConsumerUsage());
-
+  // TODO(user): Not sure what to do for fb_id. Use duped fd and new dimensions?
+  private_handle_t *out_hnd = new private_handle_t(dup(input->fd),
+                                                   dup(input->fd_metadata),
+                                                   flags,
+                                                   INT(alignedw),
+                                                   INT(alignedh),
+                                                   descriptor.GetWidth(),
+                                                   descriptor.GetHeight(),
+                                                   descriptor.GetFormat(),
+                                                   buffer_type,
+                                                   input->size,
+                                                   descriptor.GetProducerUsage(),
+                                                   descriptor.GetConsumerUsage());
+  out_hnd->id = ++next_id_;
+  // TODO(user): Base address of shared handle and ion handles
+  auto buffer = std::make_shared<Buffer>(out_hnd);
+  handles_map_.emplace(std::make_pair(out_hnd->id, buffer));
   *outbuffer = out_hnd;
 }
 
-gralloc1_error_t BufferManager::FreeBuffer(private_handle_t const *hnd) {
+gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
+  auto hnd = buf->handle;
   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
-                             hnd->fd) != 0) {
+                             hnd->fd, buf->ion_handle_main) != 0) {
     return GRALLOC1_ERROR_BAD_HANDLE;
   }
 
   unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
-                             hnd->offset_metadata, hnd->fd_metadata) != 0) {
+                             hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
     return GRALLOC1_ERROR_BAD_HANDLE;
   }
 
-  // delete handle also
-  private_handle_t *handle = const_cast<private_handle_t *>(hnd);
-  delete handle;
-
+  // TODO(user): delete handle once framework bug around this is confirmed
+  // to be resolved
   return GRALLOC1_ERROR_NONE;
 }
 
@@ -181,44 +222,42 @@
 }
 
 gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
-  locker_.lock();
+  std::lock_guard<std::mutex> lock(locker_);
 
   // find if this handle is already in map
-  auto it = handles_map_.find(hnd);
+  auto it = handles_map_.find(hnd->id);
   if (it != handles_map_.end()) {
     // It's already in map, Just increment refcnt
     // No need to mmap the memory.
-    it->second = it->second + 1;
+    auto buf = it->second;
+    buf->ref_count++;
   } else {
     // not present in the map. mmap and then add entry to map
     if (MapBuffer(hnd) == GRALLOC1_ERROR_NONE) {
-      handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
+      auto buffer = std::make_shared<Buffer>(hnd);
+      handles_map_.emplace(std::make_pair(hnd->id, buffer));
     }
   }
 
-  locker_.unlock();
   return GRALLOC1_ERROR_NONE;
 }
 
 gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
-  locker_.lock();
-
+  std::lock_guard<std::mutex> lock(locker_);
   // find if this handle is already in map
-  auto it = handles_map_.find(hnd);
+  auto it = handles_map_.find(hnd->id);
   if (it == handles_map_.end()) {
     // Corrupt handle or map.
-    locker_.unlock();
+    ALOGE("Could not find handle");
     return GRALLOC1_ERROR_BAD_HANDLE;
   } else {
-    it->second = it->second - 1;
+    auto buf = it->second;
+    buf->ref_count--;
+    if (buf->ref_count == 0) {
+      handles_map_.erase(it);
+      FreeBuffer(buf);
+    }
   }
-
-  if (!it->second) {
-    handles_map_.erase(it);
-    FreeBuffer(hnd);
-  }
-
-  locker_.unlock();
   return GRALLOC1_ERROR_NONE;
 }
 
@@ -277,9 +316,9 @@
   return status;
 }
 
-int BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
+uint32_t BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
                                     gralloc1_consumer_usage_t cons_usage) {
-  int align = getpagesize();
+  uint32_t align = UINT(getpagesize());
   if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
     align = 8192;
   }
@@ -362,61 +401,61 @@
                                   int unaligned_h, int format, int bufferType,
                                   gralloc1_producer_usage_t prod_usage,
                                   gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) {
+  auto page_size = UINT(getpagesize());
   int err = 0;
   int flags = 0;
-  size = ALIGN(size, PAGE_SIZE);
   AllocData data;
-  data.align = (unsigned int)GetDataAlignment(format, prod_usage, cons_usage);
-  size = ALIGN(size, data.align);
-  data.size = size;
+  data.align = GetDataAlignment(format, prod_usage, cons_usage);
+  data.size = ALIGN(size, data.align);
   data.handle = (uintptr_t)handle;
-
-  // Allocate memory
   data.uncached = allocator_->UseUncached(prod_usage);
+
+  // Allocate buffer memory
   err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
   if (err) {
     ALOGE("gralloc failed to allocate err=%s", strerror(-err));
-    *handle = 0;
     return err;
   }
 
-  // allocate memory for MetaData
+  // Allocate memory for MetaData
   AllocData e_data;
-  e_data.size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
+  e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
   e_data.handle = data.handle;
-  e_data.align = (unsigned int)getpagesize();
-
-  ColorSpace_t colorSpace = ITU_R_601;
-  if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
-    colorSpace = ITU_R_601_FR;
-  }
+  e_data.align = page_size;
 
   err =
       allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
-  ALOGE_IF(err, "gralloc failed for e_daata error=%s", strerror(-err));
+  if (err) {
+    ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
+    return err;
+  }
 
   flags = GetHandleFlags(format, prod_usage, cons_usage);
   flags |= data.alloc_type;
 
   // Create handle
-  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,
-                                               unaligned_w, unaligned_h, prod_usage, cons_usage);
+  private_handle_t *hnd = new private_handle_t(data.fd,
+                                               e_data.fd,
+                                               flags,
+                                               aligned_w,
+                                               aligned_h,
+                                               unaligned_w,
+                                               unaligned_h,
+                                               format,
+                                               bufferType,
+                                               size,
+                                               prod_usage,
+                                               cons_usage);
 
-  hnd->offset = data.offset;
-  hnd->base = (uint64_t)(data.base) + data.offset;
-  hnd->gpuaddr = 0;
+  hnd->id = ++next_id_;
+  hnd->base = reinterpret_cast<uint64_t >(data.base);
+  hnd->base_metadata = reinterpret_cast<uint64_t >(e_data.base);
 
+  ColorSpace_t colorSpace = ITU_R_601;
   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
-
   *handle = hnd;
-
-  // we have just allocated the buffer & mmapped. Add to map
-  locker_.lock();
-  handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
-  locker_.unlock();
-
+  auto buffer = std::make_shared<Buffer>(hnd, data.ion_handle, e_data.ion_handle);
+  handles_map_.emplace(std::make_pair(hnd->id, buffer));
   return err;
 }
 
@@ -502,8 +541,8 @@
         allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
         hnd->unaligned_width = width;
         hnd->unaligned_height = height;
-        hnd->width = alignedw;
-        hnd->height = alignedh;
+        hnd->width = INT(alignedw);
+        hnd->height = INT(alignedh);
         hnd->format = format;
         *handle = reinterpret_cast<native_handle_t *>(hnd);
       }
@@ -652,11 +691,116 @@
       }
     } break;
 
+    case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
+      int width = va_arg(args, int);
+      int height = va_arg(args, int);
+      int format = va_arg(args, int);
+      uint64_t p_usage = va_arg(args, uint64_t);
+      uint64_t c_usage = va_arg(args, uint64_t);
+      gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
+      gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
+      uint32_t *aligned_width = va_arg(args, uint32_t *);
+      uint32_t *aligned_height = va_arg(args, uint32_t *);
+      uint32_t *size = va_arg(args, uint32_t *);
+      auto descriptor = BufferDescriptor(width, height, format, producer_usage, consumer_usage);
+      allocator_->GetBufferSizeAndDimensions(descriptor, size, aligned_width, aligned_height);
+      // Align size
+      auto align = GetDataAlignment(format, producer_usage, consumer_usage);
+      *size = ALIGN(*size, align);
+    } break;
+
+      // TODO(user): Break out similar functionality, preferably moving to a common lib.
+
+    case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
+      int width = va_arg(args, int);
+      int height = va_arg(args, int);
+      int format = va_arg(args, int);
+      uint64_t p_usage = va_arg(args, uint64_t);
+      uint64_t c_usage = va_arg(args, uint64_t);
+      buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
+      gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
+      gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
+      BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
+      unsigned int size;
+      unsigned int alignedw, alignedh;
+      allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
+      AllocateBuffer(descriptor, hnd, size);
+    } break;
+
     default:
       break;
   }
-
   return GRALLOC1_ERROR_NONE;
 }
 
+static bool IsYuvFormat(const private_handle_t *hnd) {
+  switch (hnd->format) {
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+    case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:   // Same as YCbCr_420_SP_VENUS
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+    case HAL_PIXEL_FORMAT_YCrCb_422_SP:
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
+    case HAL_PIXEL_FORMAT_NV21_ZSL:
+    case HAL_PIXEL_FORMAT_RAW16:
+    case HAL_PIXEL_FORMAT_RAW10:
+    case HAL_PIXEL_FORMAT_YV12:
+      return true;
+    default:
+      return false;
+  }
+}
+
+gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
+                                                 uint32_t *out_num_planes) {
+  if (!IsYuvFormat(hnd)) {
+    return GRALLOC1_ERROR_UNSUPPORTED;
+  } else {
+    *out_num_planes = 3;
+  }
+  return GRALLOC1_ERROR_NONE;
+}
+
+gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
+                                              struct android_flex_layout *layout) {
+  if (!IsYuvFormat(hnd)) {
+    return GRALLOC1_ERROR_UNSUPPORTED;
+  }
+
+  android_ycbcr ycbcr;
+  int err = allocator_->GetYUVPlaneInfo(hnd, &ycbcr);
+
+  if (err != 0) {
+    return GRALLOC1_ERROR_BAD_HANDLE;
+  }
+
+  layout->format = FLEX_FORMAT_YCbCr;
+  layout->num_planes = 3;
+
+  for (uint32_t i = 0; i < layout->num_planes; i++) {
+    layout->planes[i].bits_per_component = 8;
+    layout->planes[i].bits_used = 8;
+    layout->planes[i].h_increment = 1;
+    layout->planes[i].v_increment = 1;
+    layout->planes[i].h_subsampling = 2;
+    layout->planes[i].v_subsampling = 2;
+  }
+
+  layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
+  layout->planes[0].component = FLEX_COMPONENT_Y;
+  layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
+
+  layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
+  layout->planes[1].component = FLEX_COMPONENT_Cb;
+  layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
+  layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
+
+  layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
+  layout->planes[2].component = FLEX_COMPONENT_Cr;
+  layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
+  layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
+  return GRALLOC1_ERROR_NONE;
+}
 }  //  namespace gralloc1
diff --git a/msm8998/libgralloc1/gr_buf_mgr.h b/msm8998/libgralloc1/gr_buf_mgr.h
index ed6b591..08ff2ef 100644
--- a/msm8998/libgralloc1/gr_buf_mgr.h
+++ b/msm8998/libgralloc1/gr_buf_mgr.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,30 +22,55 @@
 
 #include <pthread.h>
 #include <unordered_map>
+#include <unordered_set>
+#include <utility>
 #include <mutex>
 
 #include "gralloc_priv.h"
 #include "gr_allocator.h"
+#include "gr_buf_descriptor.h"
 
 namespace gralloc1 {
 
 class BufferManager {
  public:
-  BufferManager();
   ~BufferManager();
-  bool Init();
-  gralloc1_error_t AllocateBuffers(uint32_t numDescriptors, const BufferDescriptor *descriptors,
-                                   buffer_handle_t *outBuffers);
+  gralloc1_error_t CreateBufferDescriptor(gralloc1_buffer_descriptor_t *descriptor_id);
+  gralloc1_error_t DestroyBufferDescriptor(gralloc1_buffer_descriptor_t descriptor_id);
+  gralloc1_error_t AllocateBuffers(uint32_t num_descriptors,
+                                   const gralloc1_buffer_descriptor_t *descriptor_ids,
+                                   buffer_handle_t *out_buffers);
   gralloc1_error_t RetainBuffer(private_handle_t const *hnd);
   gralloc1_error_t ReleaseBuffer(private_handle_t const *hnd);
   gralloc1_error_t LockBuffer(const private_handle_t *hnd, gralloc1_producer_usage_t prod_usage,
                               gralloc1_consumer_usage_t cons_usage);
   gralloc1_error_t UnlockBuffer(const private_handle_t *hnd);
   gralloc1_error_t Perform(int operation, va_list args);
+  gralloc1_error_t GetFlexLayout(const private_handle_t *hnd, struct android_flex_layout *layout);
+  gralloc1_error_t GetNumFlexPlanes(const private_handle_t *hnd, uint32_t *out_num_planes);
+
+  template <typename... Args>
+  gralloc1_error_t CallBufferDescriptorFunction(gralloc1_buffer_descriptor_t descriptor_id,
+                                                void (BufferDescriptor::*member)(Args...),
+                                                Args... args) {
+    std::lock_guard<std::mutex> lock(locker_);
+    const auto map_descriptor = descriptors_map_.find(descriptor_id);
+    if (map_descriptor == descriptors_map_.end()) {
+      return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+    }
+    const auto descriptor = map_descriptor->second;
+    (descriptor.get()->*member)(std::forward<Args>(args)...);
+    return GRALLOC1_ERROR_NONE;
+  }
+
+  static BufferManager* GetInstance() {
+    static BufferManager *instance = new BufferManager();
+    return instance;
+  }
 
  private:
+  BufferManager();
   gralloc1_error_t MapBuffer(private_handle_t const *hnd);
-  gralloc1_error_t FreeBuffer(private_handle_t const *hnd);
   int GetBufferType(int format);
   int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
                      unsigned int bufferSize = 0);
@@ -53,18 +78,44 @@
                      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,
+  uint32_t 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,
                      gralloc1_consumer_usage_t cons_usage);
   void CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
                           buffer_handle_t *out_buffer);
 
+  // Wrapper structure over private handle
+  // Values associated with the private handle
+  // that do not need to go over IPC can be placed here
+  // This structure is also not expected to be ABI stable
+  // unlike private_handle_t
+  struct Buffer {
+    const private_handle_t *handle = nullptr;
+    int ref_count = 1;
+    // Hold the main and metadata ion handles
+    // Freed from the allocator process
+    // and unused in the mapping process
+    int ion_handle_main = -1;
+    int ion_handle_meta = -1;
+
+    Buffer() = delete;
+    explicit Buffer(const private_handle_t* h, int ih_main = -1, int ih_meta = -1):
+        handle(h),
+        ion_handle_main(ih_main),
+        ion_handle_meta(ih_meta) {
+    }
+  };
+  gralloc1_error_t FreeBuffer(std::shared_ptr<Buffer> buf);
+
   bool map_fb_mem_ = false;
   bool ubwc_for_fb_ = false;
   Allocator *allocator_ = NULL;
   std::mutex locker_;
-  std::unordered_map<private_handle_t const *, int> handles_map_ = {};
+  std::unordered_map<uint64_t, std::shared_ptr<Buffer>> handles_map_ = {};
+  std::unordered_map<gralloc1_buffer_descriptor_t,
+                     std::shared_ptr<BufferDescriptor>> descriptors_map_ = {};
+  std::atomic<uint64_t> next_id_;
 };
 
 }  // namespace gralloc1
diff --git a/msm8998/libgralloc1/gr_device_impl.cpp b/msm8998/libgralloc1/gr_device_impl.cpp
index f837ee2..2bb66d1 100644
--- a/msm8998/libgralloc1/gr_device_impl.cpp
+++ b/msm8998/libgralloc1/gr_device_impl.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-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
@@ -29,6 +29,8 @@
 
 #include <cutils/log.h>
 #include <sync/sync.h>
+#include <sstream>
+#include <string>
 
 #include "gr_device_impl.h"
 #include "gr_buf_descriptor.h"
@@ -43,13 +45,11 @@
 
 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
 
-struct hw_module_t gralloc_module = {};
-
-struct private_module_t HAL_MODULE_INFO_SYM = {
-  .base = {
+struct gralloc_module_t HAL_MODULE_INFO_SYM = {
+  .common = {
     .tag = HARDWARE_MODULE_TAG,
-    .version_major = 1,
-    .version_minor = 0,
+    .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
+    .hal_api_version    = HARDWARE_HAL_API_VERSION,
     .id = GRALLOC_HARDWARE_MODULE_ID,
     .name = "Graphics Memory Module",
     .author = "Code Aurora Forum",
@@ -62,42 +62,34 @@
 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
   int status = -EINVAL;
   if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
-    const private_module_t *m = reinterpret_cast<const private_module_t *>(module);
-    gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = new gralloc1::GrallocImpl(m);
+    gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
     *device = reinterpret_cast<hw_device_t *>(dev);
-
     if (dev->Init()) {
       status = 0;
     } else {
       ALOGE(" Error in opening gralloc1 device");
-      return status;
     }
   }
-
   return status;
 }
 
 namespace gralloc1 {
 
-GrallocImpl::GrallocImpl(const private_module_t *module) {
+GrallocImpl::GrallocImpl(const hw_module_t *module) {
   common.tag = HARDWARE_DEVICE_TAG;
-  common.version = 1;  // TODO(user): cross check version
-  common.module = const_cast<hw_module_t *>(&module->base);
+  common.version = GRALLOC_MODULE_API_VERSION_1_0;
+  common.module = const_cast<hw_module_t *>(module);
   common.close = CloseDevice;
   getFunction = GetFunction;
   getCapabilities = GetCapabilities;
 }
 
 bool GrallocImpl::Init() {
-  buf_mgr_ = new BufferManager();
-
-  return buf_mgr_->Init();
+  buf_mgr_ = BufferManager::GetInstance();
+  return true;
 }
 
 GrallocImpl::~GrallocImpl() {
-  if (buf_mgr_) {
-    delete buf_mgr_;
-  }
 }
 
 int GrallocImpl::CloseDevice(hw_device_t *device) {
@@ -108,16 +100,13 @@
 }
 
 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
-                                  int32_t /*gralloc1_capability_t*/ *out_capabilities) {
-  if (!device) {
-    // Need to plan for adding more capabilities
-    if (out_capabilities == NULL) {
-      *out_count = 1;
-    } else {
-      *out_capabilities = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
+                                  int32_t  /*gralloc1_capability_t*/ *out_capabilities) {
+  if (device != nullptr) {
+    if (out_capabilities != nullptr && *out_count > 0) {
+      out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
     }
+    *out_count = 1;
   }
-
   return;
 }
 
@@ -127,6 +116,8 @@
   }
 
   switch (function) {
+    case GRALLOC1_FUNCTION_DUMP:
+      return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
     case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
       return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
     case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
@@ -157,17 +148,12 @@
       return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
     case GRALLOC1_FUNCTION_RELEASE:
       return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
-    /*  TODO(user) :definition of flex plane is not known yet
-     *  Need to implement after clarification from Google.
-    * case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
-      return reinterpret_cast<gralloc1_function_pointer_t> (; */
+    case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
+      return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
     case GRALLOC1_FUNCTION_LOCK:
       return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
-    /*  TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure is not known yet.
-     *  Need to implement after clarification from Google.
-    case GRALLOC1_PFN_LOCK_FLEX:
-      return reinterpret_cast<gralloc1_function_pointer_t> (LockYCbCrBuffer;
-    */
+    case GRALLOC1_FUNCTION_LOCK_FLEX:
+      return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
     case GRALLOC1_FUNCTION_UNLOCK:
       return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
     case GRALLOC1_FUNCTION_PERFORM:
@@ -180,12 +166,24 @@
   return NULL;
 }
 
-gralloc1_error_t GrallocImpl::CheckDeviceAndDescriptor(gralloc1_device_t *device,
-                                                       gralloc1_buffer_descriptor_t descriptor) {
-  if (!device || !BUF_DESCRIPTOR(descriptor)->IsValid()) {
-    ALOGE("Gralloc Error : device=%p, descriptor=%p", (void *)device, (void *)descriptor);
+gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
+                                   char *out_buffer) {
+  if (!device) {
+    ALOGE("Gralloc Error : device=%p", (void *)device);
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
+  if (out_buffer == nullptr) {
+    *out_size = 1024;
+  } else {
+    std::ostringstream os;
+    // TODO(user): implement in buffer manager
+    os << "-------------------------------" << std::endl;
+    os << "QTI gralloc dump:" << std::endl;
+    os << "-------------------------------" << std::endl;
+    auto copy_size = os.str().size() < *out_size ? os.str().size() : *out_size;
+    std::copy_n(out_buffer, copy_size, os.str().begin());
+    *out_size = static_cast<uint32_t>(copy_size);
+  }
 
   return GRALLOC1_ERROR_NONE;
 }
@@ -206,69 +204,66 @@
   if (!device) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
-
-  BufferDescriptor *descriptor = new BufferDescriptor();
-  if (descriptor == NULL) {
-    return GRALLOC1_ERROR_NO_RESOURCES;
-  }
-
-  *out_descriptor = reinterpret_cast<gralloc1_buffer_descriptor_t>(descriptor);
-
-  return GRALLOC1_ERROR_NONE;
+  GrallocImpl const *dev = GRALLOC_IMPL(device);
+  return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
 }
 
 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
                                                       gralloc1_buffer_descriptor_t descriptor) {
-  gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
-  if (status == GRALLOC1_ERROR_NONE) {
-    delete reinterpret_cast<BufferDescriptor *>(descriptor);
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
-
-  return status;
+  GrallocImpl const *dev = GRALLOC_IMPL(device);
+  return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
 }
 
 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
                                                gralloc1_buffer_descriptor_t descriptor,
                                                gralloc1_consumer_usage_t usage) {
-  gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
-  if (status == GRALLOC1_ERROR_NONE) {
-    BUF_DESCRIPTOR(descriptor)->SetConsumerUsage(usage);
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetConsumerUsage, usage);
   }
-
-  return status;
 }
 
 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
                                                   gralloc1_buffer_descriptor_t descriptor,
                                                   uint32_t width, uint32_t height) {
-  gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
-  if (status == GRALLOC1_ERROR_NONE) {
-    BUF_DESCRIPTOR(descriptor)->SetDimensions(INT(width), INT(height));
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetDimensions,
+                                                       INT(width), INT(height));
   }
-
-  return status;
 }
 
 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
                                              gralloc1_buffer_descriptor_t descriptor,
                                              int32_t format) {
-  gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
-  if (status == GRALLOC1_ERROR_NONE) {
-    BUF_DESCRIPTOR(descriptor)->SetColorFormat(format);
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetColorFormat, format);
   }
-
-  return status;
 }
 
 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
                                                gralloc1_buffer_descriptor_t descriptor,
                                                gralloc1_producer_usage_t usage) {
-  gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
-  if (status == GRALLOC1_ERROR_NONE) {
-    BUF_DESCRIPTOR(descriptor)->SetProducerUsage(usage);
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetProducerUsage, usage);
   }
-
-  return status;
 }
 
 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
@@ -336,16 +331,16 @@
   return status;
 }
 
-gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_dptors,
-                                              const gralloc1_buffer_descriptor_t *dptors,
-                                              buffer_handle_t *outBuffers) {
-  if (!num_dptors || !dptors) {
+gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
+                                              const gralloc1_buffer_descriptor_t *descriptors,
+                                              buffer_handle_t *out_buffers) {
+  if (!num_descriptors || !descriptors) {
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
 
   GrallocImpl const *dev = GRALLOC_IMPL(device);
-  const BufferDescriptor *descriptors = reinterpret_cast<const BufferDescriptor *>(dptors);
-  gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_dptors, descriptors, outBuffers);
+  gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
+                                                           out_buffers);
 
   return status;
 }
@@ -372,6 +367,17 @@
   return status;
 }
 
+gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
+                                               uint32_t *out_num_planes) {
+  gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
+  if (status == GRALLOC1_ERROR_NONE) {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
+    status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
+  }
+  return status;
+}
+
 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
                                          gralloc1_producer_usage_t prod_usage,
                                          gralloc1_consumer_usage_t cons_usage,
@@ -392,14 +398,16 @@
   // Either producer usage or consumer usage must be *_USAGE_NONE
   if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
       (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
-    return GRALLOC1_ERROR_BAD_VALUE;
+    // Current gralloc1 clients do not satisfy this restriction.
+    // See b/33588773 for details
+    // return GRALLOC1_ERROR_BAD_VALUE;
   }
 
   // currently we ignore the region/rect client wants to lock
   if (region == NULL) {
     return GRALLOC1_ERROR_BAD_VALUE;
   }
-
+  // TODO(user): Need to check if buffer was allocated with the same flags
   status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
 
   *out_data = reinterpret_cast<void *>(hnd->base);
@@ -407,27 +415,24 @@
   return status;
 }
 
-/*  TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure definition is not known yet.
- *  Need to implement after clarification from Google.
-gralloc1_error_t GrallocImpl::LockYCbCrBuffer(gralloc1_device_t* device, buffer_handle_t buffer,
-    gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
-    const gralloc1_rect_t* region, struct android_ycbcr* outYCbCr, int32_t* outAcquireFence) {
-  gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
-
-  if (status == GRALLOC1_ERROR_NONE) {
-    void **outData = 0;
-    status = LockBuffer(device, buffer, prod_usage, cons_usage, region, outData, outAcquireFence);
+gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
+                                       gralloc1_producer_usage_t prod_usage,
+                                       gralloc1_consumer_usage_t cons_usage,
+                                       const gralloc1_rect_t *region,
+                                       struct android_flex_layout *out_flex_layout,
+                                       int32_t acquire_fence) {
+  void *out_data;
+  gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
+                                                    &out_data, acquire_fence);
+  if (status != GRALLOC1_ERROR_NONE) {
+    return status;
   }
 
-  if (status == GRALLOC1_ERROR_NONE) {
-    const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
-    GrallocImpl const *dev = GRALLOC_IMPL(device);
-    dev->allocator_->GetYUVPlaneInfo(hnd, outYCbCr);
-  }
-
+  GrallocImpl const *dev = GRALLOC_IMPL(device);
+  const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
+  dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
   return status;
 }
- */
 
 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
                                            int32_t *release_fence) {
diff --git a/msm8998/libgralloc1/gr_device_impl.h b/msm8998/libgralloc1/gr_device_impl.h
index 4fc8e3c..80c3182 100644
--- a/msm8998/libgralloc1/gr_device_impl.h
+++ b/msm8998/libgralloc1/gr_device_impl.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-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
@@ -44,7 +44,6 @@
 
 class GrallocImpl : public gralloc1_device_t {
  public:
-  explicit GrallocImpl(const private_module_t *module);
   ~GrallocImpl();
   bool Init();
   static int CloseDevice(hw_device_t *device);
@@ -53,9 +52,14 @@
   static gralloc1_function_pointer_t GetFunction(
       struct gralloc1_device *device, int32_t /*gralloc1_function_descriptor_t*/ descriptor);
 
+  static GrallocImpl* GetInstance(const struct hw_module_t *module) {
+    static GrallocImpl *instance = new GrallocImpl(module);
+    return instance;
+  }
+
  private:
-  static inline gralloc1_error_t CheckDeviceAndDescriptor(gralloc1_device_t *device,
-                                                          gralloc1_buffer_descriptor_t descriptor);
+  static inline gralloc1_error_t Dump(gralloc1_device_t *device, uint32_t *out_size,
+                                      char *out_buffer);
   static inline gralloc1_error_t CheckDeviceAndHandle(gralloc1_device_t *device,
                                                       buffer_handle_t buffer);
   static gralloc1_error_t CreateBufferDescriptor(gralloc1_device_t *device,
@@ -90,29 +94,25 @@
                                           buffer_handle_t *out_buffers);
   static gralloc1_error_t RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer);
   static gralloc1_error_t ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer);
-  static gralloc1_error_t getNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
+  static gralloc1_error_t GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
                                            uint32_t *out_num_planes);
   static gralloc1_error_t LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
                                      gralloc1_producer_usage_t prod_usage,
                                      gralloc1_consumer_usage_t cons_usage,
-                                     const gralloc1_rect_t *access_region, void **out_data,
+                                     const gralloc1_rect_t *region, void **out_data,
                                      int32_t acquire_fence);
   static gralloc1_error_t LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
                                    gralloc1_producer_usage_t prod_usage,
                                    gralloc1_consumer_usage_t cons_usage,
-                                   const gralloc1_rect_t *access_region,
+                                   const gralloc1_rect_t *region,
                                    struct android_flex_layout *out_flex_layout,
-                                   int32_t acquireFence);
-  /*  TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure is not known yet.
-   *  Need to implement after clarification from Google.
-  static gralloc1_error_t LockYCbCrBuffer(gralloc1_device_t* device, buffer_handle_t buffer,
-      gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage,
-      const gralloc1_rect_t* Region, struct android_ycbcr* outYCbCr, int32_t* outAcquireFence);
-   */
+                                   int32_t acquire_fence);
+
   static gralloc1_error_t UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
                                        int32_t *release_fence);
   static gralloc1_error_t Gralloc1Perform(gralloc1_device_t *device, int operation, ...);
 
+  explicit GrallocImpl(const hw_module_t *module);
   BufferManager *buf_mgr_ = NULL;
 };
 
diff --git a/msm8998/libgralloc1/gr_ion_alloc.cpp b/msm8998/libgralloc1/gr_ion_alloc.cpp
index ca93a63..b76fa9f 100644
--- a/msm8998/libgralloc1/gr_ion_alloc.cpp
+++ b/msm8998/libgralloc1/gr_ion_alloc.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-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
@@ -106,21 +106,26 @@
 
   data->base = base;
   data->fd = fd_data.fd;
-  ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
-  ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d", data->base, ion_alloc_data.len,
-           data->fd);
+  data->ion_handle = handle_data.handle;
+  ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d handle:0x%x", data->base,
+           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 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", base, size, fd);
+  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);
   }
+  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;
diff --git a/msm8998/libgralloc1/gr_ion_alloc.h b/msm8998/libgralloc1/gr_ion_alloc.h
index baef8aa..68f453c 100644
--- a/msm8998/libgralloc1/gr_ion_alloc.h
+++ b/msm8998/libgralloc1/gr_ion_alloc.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-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
@@ -45,6 +45,7 @@
 struct AllocData {
   void *base = NULL;
   int fd = -1;
+  int ion_handle = -1;
   unsigned int offset = 0;
   unsigned int size = 0;
   unsigned int align = 1;
@@ -63,7 +64,7 @@
 
   bool Init();
   int AllocBuffer(AllocData *data);
-  int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd);
+  int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int ion_handle);
   int MapBuffer(void **base, unsigned int size, unsigned int offset, int fd);
   int UnmapBuffer(void *base, unsigned int size, unsigned int offset);
   int CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op);
diff --git a/msm8998/libgralloc1/gr_priv_handle.h b/msm8998/libgralloc1/gr_priv_handle.h
index ee38b4d..9a7e7b6 100644
--- a/msm8998/libgralloc1/gr_priv_handle.h
+++ b/msm8998/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,35 +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)));
-
-  // 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)));
 
@@ -95,57 +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) {
+        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) {
@@ -175,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;
   }
 
@@ -184,7 +175,7 @@
 
   gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; }
 
-  BackStoreFd GetBackingstore() const { return fd; }
+  uint64_t GetBackingstore() const { return id; }
 };
 
 #endif  // __GR_PRIV_HANDLE_H__
diff --git a/msm8998/libgralloc1/gralloc_priv.h b/msm8998/libgralloc1/gralloc_priv.h
index 0695a60..fa04b1b 100644
--- a/msm8998/libgralloc1/gralloc_priv.h
+++ b/msm8998/libgralloc1/gralloc_priv.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
@@ -24,50 +24,64 @@
 #include "gr_priv_handle.h"
 
 #define ROUND_UP_PAGESIZE(x) roundUpToPageSize(x)
-inline unsigned int roundUpToPageSize(unsigned int x) {
+inline int roundUpToPageSize(int x) {
     return (x + (getpagesize()-1)) & ~(getpagesize()-1);
 }
 
 /* Gralloc usage bits indicating the type of allocation that should be used */
 /* Refer gralloc1_producer_usage_t & gralloc1_consumer_usage-t in gralloc1.h */
 
-/* GRALLOC_USAGE_PRIVATE_0 is unused */
-
+/* Producer flags */
 /* Non linear, Universal Bandwidth Compression */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC GRALLOC1_PRODUCER_USAGE_PRIVATE_1
-
-/* IOMMU heap comes from manually allocated pages, can be cached/uncached, is not secured */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_IOMMU_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_2
-
-/* MM heap is a carveout heap for video, can be secured */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_3
-
-/* ADSP heap is a carveout heap, is not secured */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_4
-
-/* CAMERA heap is a carveout heap for camera, is not secured */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_5
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC  GRALLOC1_PRODUCER_USAGE_PRIVATE_0
 
 /* Set this for allocating uncached memory (using O_DSYNC),
  * cannot be used with noncontiguous heaps */
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED GRALLOC1_PRODUCER_USAGE_PRIVATE_6
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED    GRALLOC1_PRODUCER_USAGE_PRIVATE_1
 
-#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_ZSL GRALLOC1_PRODUCER_USAGE_PRIVATE_7
+/* CAMERA heap is a carveout heap for camera, is not secured */
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_2
 
-/* Buffer content should be displayed on a primary display only */
-#define GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY GRALLOC1_CONSUMER_USAGE_PRIVATE_1
+/* ADSP heap is a carveout heap, is not secured */
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP   GRALLOC1_PRODUCER_USAGE_PRIVATE_3
 
-/* Buffer content should be displayed on an external display only */
-#define GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY GRALLOC1_CONSUMER_USAGE_PRIVATE_2
+/* IOMMU heap comes from manually allocated pages, can be cached/uncached, is not secured */
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_IOMMU_HEAP  GRALLOC1_PRODUCER_USAGE_PRIVATE_4
 
+/* MM heap is a carveout heap for video, can be secured */
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP     GRALLOC1_PRODUCER_USAGE_PRIVATE_5
+
+/* Use legacy ZSL definition until we know the correct usage on gralloc1 */
+#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_ZSL  GRALLOC_USAGE_HW_CAMERA_ZSL
+
+
+/* Consumer flags */
+/* TODO(user): Fix when producer and consumer flags are actually separated */
 /* This flag is set for WFD usecase */
-#define GRALLOC1_CONSUMER_USAGE_PRIVATE_WFD GRALLOC1_CONSUMER_USAGE_PRIVATE_3
+#define GRALLOC1_CONSUMER_USAGE_PRIVATE_WFD            0x00200000
 
 /* This flag is used for SECURE display usecase */
-#define GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY GRALLOC1_CONSUMER_USAGE_PRIVATE_4
+#define GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY 0x00800000
+
+/* Buffer content should be displayed on a primary display only */
+#define GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY  0x04000000
+
+/* Buffer content should be displayed on an external display only */
+#define GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY  0x08000000
+
+
+/* Legacy gralloc0.x definitions */
+/* Some clients may still be using the old flags */
+#define GRALLOC_USAGE_PRIVATE_ALLOC_UBWC GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC
+#define GRALLOC_USAGE_PRIVATE_UNCACHED GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED
+#define GRALLOC_USAGE_PRIVATE_IOMMU_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_IOMMU_HEAP
+#define GRALLOC_USAGE_PRIVATE_WFD GRALLOC1_CONSUMER_USAGE_PRIVATE_WFD
+#define GRALLOC_USAGE_PRIVATE_CAMERA_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP
+#define GRALLOC_USAGE_PRIVATE_MM_HEAP 0x0
+
+
 
 // for PERFORM API :
-// TODO(user): Move it to enum if it's ookay for gfx
 #define GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER 1
 #define GRALLOC_MODULE_PERFORM_GET_STRIDE 2
 #define GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE 3
@@ -81,6 +95,8 @@
 #define GRALLOC_MODULE_PERFORM_GET_IGC 11
 #define GRALLOC_MODULE_PERFORM_SET_IGC 12
 #define GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE 13
+#define GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS 14
+#define GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER 15
 
 // OEM specific HAL formats
 #define HAL_PIXEL_FORMAT_RGBA_5551 6
diff --git a/msm8998/liblight/Android.mk b/msm8998/liblight/Android.mk
index 36f13a6..6e342ff 100644
--- a/msm8998/liblight/Android.mk
+++ b/msm8998/liblight/Android.mk
@@ -25,5 +25,7 @@
 LOCAL_CLANG  := true
 LOCAL_MODULE := lights.$(TARGET_BOARD_PLATFORM)
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/msm8998/libmemtrack/kgsl.c b/msm8998/libmemtrack/kgsl.c
index 877b54e..3f36b8d 100644
--- a/msm8998/libmemtrack/kgsl.c
+++ b/msm8998/libmemtrack/kgsl.c
@@ -74,7 +74,7 @@
     while (1) {
         unsigned long size, mapsize;
         char line_type[7];
-        char flags[9];
+        char flags[10];
         char line_usage[19];
         int ret, egl_surface_count = 0, egl_image_count = 0;
 
@@ -86,9 +86,9 @@
          *  gpuaddr useraddr     size    id flags       type            usage sglen mapsize eglsrf eglimg
          * 545ba000 545ba000     4096     1 -----pY     gpumem      arraybuffer     1  4096      0      0
          */
-        ret = sscanf(line, "%*x %*x %lu %*d %8s %6s %18s %*d %lu %6d %6d\n",
-			&size, flags, line_type, line_usage, &mapsize,
-			&egl_surface_count, &egl_image_count);
+        ret = sscanf(line, "%*x %*x %lu %*d %9s %6s %18s %*d %lu %6d %6d\n",
+                       &size, flags, line_type, line_usage, &mapsize,
+                       &egl_surface_count, &egl_image_count);
         if (ret != 7) {
             continue;
         }
diff --git a/msm8998/libqdutils/Android.mk b/msm8998/libqdutils/Android.mk
index cc0b013..8a52d4b 100644
--- a/msm8998/libqdutils/Android.mk
+++ b/msm8998/libqdutils/Android.mk
@@ -3,6 +3,8 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libqdutils
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 LOCAL_MODULE_TAGS             := optional
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libbinder libqservice
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
@@ -28,5 +30,7 @@
 
 LOCAL_MODULE_TAGS               := optional
 LOCAL_MODULE                    := libqdMetaData
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/msm8998/libqdutils/profiler.cpp b/msm8998/libqdutils/profiler.cpp
index b183e6d..810b019 100644
--- a/msm8998/libqdutils/profiler.cpp
+++ b/msm8998/libqdutils/profiler.cpp
@@ -28,6 +28,7 @@
  */
 
 #define LOG_NDDEBUG 0
+#define __STDC_FORMAT_MACROS 1
 #include <inttypes.h>
 
 #include "profiler.h"
diff --git a/msm8998/libqdutils/qdMetaData.cpp b/msm8998/libqdutils/qdMetaData.cpp
index f6f57cb..c888baa 100644
--- a/msm8998/libqdutils/qdMetaData.cpp
+++ b/msm8998/libqdutils/qdMetaData.cpp
@@ -32,6 +32,7 @@
 #include <sys/mman.h>
 #include <cutils/log.h>
 #include <gralloc_priv.h>
+#define __STDC_FORMAT_MACROS 1
 #include <inttypes.h>
 #include "qdMetaData.h"
 
diff --git a/msm8998/libqdutils/qd_utils.cpp b/msm8998/libqdutils/qd_utils.cpp
index 170b1d8..10ac90b 100644
--- a/msm8998/libqdutils/qd_utils.cpp
+++ b/msm8998/libqdutils/qd_utils.cpp
@@ -28,6 +28,7 @@
  */
 
 #include <unistd.h>
+#include <gralloc_priv.h>
 #include "qd_utils.h"
 
 namespace qdutils {
@@ -281,4 +282,79 @@
     return access(fb_caps, F_OK) ? DriverType::DRM : DriverType::FB;
 }
 
+const char *GetHALPixelFormatString(int format) {
+  switch (format) {
+  case HAL_PIXEL_FORMAT_RGBA_8888:
+    return "RGBA_8888";
+  case HAL_PIXEL_FORMAT_RGBX_8888:
+    return "RGBX_8888";
+  case HAL_PIXEL_FORMAT_RGB_888:
+    return "RGB_888";
+  case HAL_PIXEL_FORMAT_RGB_565:
+    return "RGB_565";
+  case HAL_PIXEL_FORMAT_BGR_565:
+    return "BGR_565";
+  case HAL_PIXEL_FORMAT_BGRA_8888:
+    return "BGRA_8888";
+  case HAL_PIXEL_FORMAT_RGBA_5551:
+    return "RGBA_5551";
+  case HAL_PIXEL_FORMAT_RGBA_4444:
+    return "RGBA_4444";
+  case HAL_PIXEL_FORMAT_YV12:
+    return "YV12";
+  case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+    return "YCbCr_422_SP_NV16";
+  case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+    return "YCrCb_420_SP_NV21";
+  case HAL_PIXEL_FORMAT_YCbCr_422_I:
+    return "YCbCr_422_I_YUY2";
+  case HAL_PIXEL_FORMAT_YCrCb_422_I:
+    return "YCrCb_422_I_YVYU";
+  case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+    return "NV12_ENCODEABLE";
+  case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
+    return "YCbCr_420_SP_TILED_TILE_4x2";
+  case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+    return "YCbCr_420_SP";
+  case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
+    return "YCrCb_420_SP_ADRENO";
+  case HAL_PIXEL_FORMAT_YCrCb_422_SP:
+    return "YCrCb_422_SP";
+  case HAL_PIXEL_FORMAT_R_8:
+    return "R_8";
+  case HAL_PIXEL_FORMAT_RG_88:
+    return "RG_88";
+  case HAL_PIXEL_FORMAT_INTERLACE:
+    return "INTERLACE";
+  case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+    return "YCbCr_420_SP_VENUS";
+  case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+    return "YCrCb_420_SP_VENUS";
+  case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+    return "YCbCr_420_SP_VENUS_UBWC";
+  case HAL_PIXEL_FORMAT_RGBA_1010102:
+    return "RGBA_1010102";
+  case HAL_PIXEL_FORMAT_ARGB_2101010:
+    return "ARGB_2101010";
+  case HAL_PIXEL_FORMAT_RGBX_1010102:
+    return "RGBX_1010102";
+  case HAL_PIXEL_FORMAT_XRGB_2101010:
+    return "XRGB_2101010";
+  case HAL_PIXEL_FORMAT_BGRA_1010102:
+    return "BGRA_1010102";
+  case HAL_PIXEL_FORMAT_ABGR_2101010:
+    return "ABGR_2101010";
+  case HAL_PIXEL_FORMAT_BGRX_1010102:
+    return "BGRX_1010102";
+  case HAL_PIXEL_FORMAT_XBGR_2101010:
+    return "XBGR_2101010";
+  case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+    return "YCbCr_420_P010";
+  case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+    return "YCbCr_420_TP10_UBWC";
+  default:
+    return "Unknown_format";
+  }
+}
+
 }; //namespace qdutils
diff --git a/msm8998/libqdutils/qd_utils.h b/msm8998/libqdutils/qd_utils.h
index 7b3ae40..f3b0c04 100644
--- a/msm8998/libqdutils/qd_utils.h
+++ b/msm8998/libqdutils/qd_utils.h
@@ -69,6 +69,7 @@
     DRM,
 };
 DriverType getDriverType();
+const char *GetHALPixelFormatString(int format);
 
 }; //namespace qdutils
 #endif
diff --git a/msm8998/libqservice/Android.mk b/msm8998/libqservice/Android.mk
index ea11180..9cc1a0b 100644
--- a/msm8998/libqservice/Android.mk
+++ b/msm8998/libqservice/Android.mk
@@ -3,6 +3,8 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libqservice
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libbinder
diff --git a/msm8998/sdm/include/core/layer_buffer.h b/msm8998/sdm/include/core/layer_buffer.h
index 0a8a473..636982c 100644
--- a/msm8998/sdm/include/core/layer_buffer.h
+++ b/msm8998/sdm/include/core/layer_buffer.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014, 2016, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014, 2016-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:
@@ -266,6 +266,7 @@
                                 //!< could be modified by both client and SDM.
   uint64_t buffer_id __attribute__((aligned(8))) = 0;
                                 //!< Specifies the buffer id.
+  uint32_t fb_id = 0;  // DRM f/w registered framebuffer id
 };
 
 // This enum represents buffer layout types.
diff --git a/msm8998/sdm/include/private/extension_interface.h b/msm8998/sdm/include/private/extension_interface.h
index c5e6dcb..a10a2c8 100644
--- a/msm8998/sdm/include/private/extension_interface.h
+++ b/msm8998/sdm/include/private/extension_interface.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved.
+* 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:
@@ -56,6 +56,7 @@
                                            const HWPanelInfo &hw_panel_info,
                                            const HWMixerAttributes &mixer_attributes,
                                            const HWDisplayAttributes &display_attributes,
+                                           const DisplayConfigVariableInfo &fb_config,
                                            PartialUpdateInterface **interface) = 0;
   virtual DisplayError DestroyPartialUpdate(PartialUpdateInterface *interface) = 0;
 
diff --git a/msm8998/sdm/include/private/hw_info_types.h b/msm8998/sdm/include/private/hw_info_types.h
index 657d4ac..03b5eab 100644
--- a/msm8998/sdm/include/private/hw_info_types.h
+++ b/msm8998/sdm/include/private/hw_info_types.h
@@ -483,6 +483,7 @@
   uint32_t v_back_porch = 0;   //!< Vertical back porch of panel
   uint32_t v_pulse_width = 0;  //!< Vertical pulse width of panel
   uint32_t h_total = 0;        //!< Total width of panel (hActive + hFP + hBP + hPulseWidth)
+  uint32_t v_total = 0;        //!< Total height of panel (vActive + vFP + vBP + vPulseWidth)
   std::bitset<32> s3d_config;  //!< Stores the bit mask of S3D modes
 
   void Reset() { *this = HWDisplayAttributes(); }
@@ -498,6 +499,7 @@
             (v_front_porch != display_attributes.v_front_porch) ||
             (v_back_porch != display_attributes.v_back_porch) ||
             (v_pulse_width != display_attributes.v_pulse_width) ||
+            (h_total != display_attributes.h_total) ||
             (is_yuv != display_attributes.is_yuv));
   }
 
diff --git a/msm8998/sdm/include/private/rotator_interface.h b/msm8998/sdm/include/private/rotator_interface.h
deleted file mode 100644
index 5432ac2..0000000
--- a/msm8998/sdm/include/private/rotator_interface.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
-* Copyright (c) 2015, 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.
-*/
-
-#ifndef __ROTATOR_INTERFACE_H__
-#define __ROTATOR_INTERFACE_H__
-
-#include <core/display_interface.h>
-#include <core/buffer_allocator.h>
-#include <core/buffer_sync_handler.h>
-
-#include "hw_info_types.h"
-
-namespace sdm {
-
-class RotatorInterface {
- public:
-  virtual DisplayError RegisterDisplay(DisplayType type, Handle *display_ctx) = 0;
-  virtual void UnregisterDisplay(Handle display_ctx) = 0;
-  virtual DisplayError Prepare(Handle display_ctx, HWLayers *hw_layers) = 0;
-  virtual DisplayError Commit(Handle display_ctx, HWLayers *hw_layers) = 0;
-  virtual DisplayError PostCommit(Handle display_ctx, HWLayers *hw_layers) = 0;
-  virtual DisplayError Purge(Handle display_ctx) = 0;
-
- protected:
-  virtual ~RotatorInterface() { }
-};
-
-}  // namespace sdm
-
-#endif  // __ROTATOR_INTERFACE_H__
-
diff --git a/msm8998/sdm/include/utils/utils.h b/msm8998/sdm/include/utils/utils.h
index ed2c6ae..b1c55c4 100644
--- a/msm8998/sdm/include/utils/utils.h
+++ b/msm8998/sdm/include/utils/utils.h
@@ -36,6 +36,13 @@
 float lcm(float a, float b);
 void CloseFd(int *fd);
 
+enum class DriverType {
+    FB = 0,
+    DRM,
+};
+
+DriverType GetDriverType();
+
 }  // namespace sdm
 
 #endif  // __UTILS_H__
diff --git a/msm8998/sdm/libs/core/Android.mk b/msm8998/sdm/libs/core/Android.mk
index 50f087d..7cfa8a3 100644
--- a/msm8998/sdm/libs/core/Android.mk
+++ b/msm8998/sdm/libs/core/Android.mk
@@ -3,11 +3,24 @@
 include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmcore
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes) $(common_header_export_path)
-LOCAL_CFLAGS                  := -Wno-unused-parameter -DLOG_TAG=\"SDM\" $(common_flags)
-LOCAL_HW_INTF_PATH            := fb
+LOCAL_CFLAGS                  := -Wno-unused-parameter -DLOG_TAG=\"SDM\" \
+                                 $(common_flags)
+ifeq ($(use_hwc2),false)
+  LOCAL_CFLAGS += -DUSE_SPECULATIVE_FENCES
+endif
+LOCAL_HW_INTF_PATH_1          := fb
 LOCAL_SHARED_LIBRARIES        := libdl libsdmutils
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+    LOCAL_CFLAGS              += -isystem external/libdrm
+    LOCAL_SHARED_LIBRARIES    += libdrm libdrmutils
+    LOCAL_HW_INTF_PATH_2      := drm
+endif
+
 LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps) $(kernel_deps)
 LOCAL_SRC_FILES               := core_interface.cpp \
                                  core_impl.cpp \
@@ -20,14 +33,23 @@
                                  resource_default.cpp \
                                  dump_impl.cpp \
                                  color_manager.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_info.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_device.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_primary.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_hdmi.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_virtual.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_color_manager.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_scale.cpp \
-                                 $(LOCAL_HW_INTF_PATH)/hw_events.cpp
+                                 hw_events_interface.cpp \
+                                 hw_info_interface.cpp \
+                                 hw_interface.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_info.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_device.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_primary.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_hdmi.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_virtual.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_color_manager.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_scale.cpp \
+                                 $(LOCAL_HW_INTF_PATH_1)/hw_events.cpp
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+    LOCAL_SRC_FILES           += $(LOCAL_HW_INTF_PATH_2)/hw_info_drm.cpp \
+                                 $(LOCAL_HW_INTF_PATH_2)/hw_device_drm.cpp \
+                                 $(LOCAL_HW_INTF_PATH_2)/hw_events_drm.cpp
+endif
 
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/msm8998/sdm/libs/core/Makefile.am b/msm8998/sdm/libs/core/Makefile.am
index 9041394..2b45d8e 100644
--- a/msm8998/sdm/libs/core/Makefile.am
+++ b/msm8998/sdm/libs/core/Makefile.am
@@ -11,6 +11,9 @@
             resource_default.cpp \
             dump_impl.cpp \
             color_manager.cpp \
+            hw_interface.cpp \
+            hw_info_interface.cpp \
+            hw_events_interface.cpp \
             fb/hw_info.cpp \
             fb/hw_device.cpp \
             fb/hw_primary.cpp \
@@ -41,4 +44,4 @@
 libsdmcore_la_CFLAGS = $(COMMON_CFLAGS) -DLOG_TAG=\"SDM\"
 libsdmcore_la_CPPFLAGS = $(AM_CPPFLAGS)
 libsdmcore_la_LIBADD = ../utils/libsdmutils.la
-libsdmcore_la_LDFLAGS = -shared -avoid-version
\ No newline at end of file
+libsdmcore_la_LDFLAGS = -shared -avoid-version
diff --git a/msm8998/sdm/libs/core/comp_manager.cpp b/msm8998/sdm/libs/core/comp_manager.cpp
index e770684..b070c41 100644
--- a/msm8998/sdm/libs/core/comp_manager.cpp
+++ b/msm8998/sdm/libs/core/comp_manager.cpp
@@ -489,8 +489,9 @@
     return supported;
   }
   Layer *cursor_layer = layer_stack->layers.at(UINT32(gpu_index) - 1);
-  if (cursor_layer->flags.cursor && resource_intf_->ValidateCursorConfig(display_resource_ctx,
-                                    cursor_layer, true) == kErrorNone) {
+  if (cursor_layer->flags.cursor && !cursor_layer->flags.skip &&
+      resource_intf_->ValidateCursorConfig(display_resource_ctx,
+                                           cursor_layer, true) == kErrorNone) {
     supported = true;
   }
 
diff --git a/msm8998/sdm/libs/core/display_base.cpp b/msm8998/sdm/libs/core/display_base.cpp
index 4a31a0f..64cebc1 100644
--- a/msm8998/sdm/libs/core/display_base.cpp
+++ b/msm8998/sdm/libs/core/display_base.cpp
@@ -448,6 +448,10 @@
 
   case kStateDozeSuspend:
     error = hw_intf_->DozeSuspend();
+    if (display_type_ != kPrimary) {
+      active = true;
+    }
+
     break;
 
   case kStateStandby:
@@ -1102,6 +1106,7 @@
     hw_layer.input_buffer.planes[0].stride = sdm_layer->input_buffer.planes[0].stride;
     hw_layer.input_buffer.size = sdm_layer->input_buffer.size;
     hw_layer.input_buffer.acquire_fence_fd = sdm_layer->input_buffer.acquire_fence_fd;
+    hw_layer.input_buffer.fb_id = sdm_layer->input_buffer.fb_id;
   }
 
   return;
diff --git a/msm8998/sdm/libs/core/display_hdmi.cpp b/msm8998/sdm/libs/core/display_hdmi.cpp
index 46d1a20..783cad9 100644
--- a/msm8998/sdm/libs/core/display_hdmi.cpp
+++ b/msm8998/sdm/libs/core/display_hdmi.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 met:
@@ -86,7 +86,7 @@
   s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode>
                             (kS3dFormatFramePacking, kS3DModeFP));
 
-  error = HWEventsInterface::Create(INT(display_type_), this, &event_list_, &hw_events_intf_);
+  error = HWEventsInterface::Create(INT(display_type_), this, event_list_, &hw_events_intf_);
   if (error != kErrorNone) {
     DisplayBase::Deinit();
     HWInterface::Destroy(hw_intf_);
diff --git a/msm8998/sdm/libs/core/display_hdmi.h b/msm8998/sdm/libs/core/display_hdmi.h
index 868b141..6db2304 100644
--- a/msm8998/sdm/libs/core/display_hdmi.h
+++ b/msm8998/sdm/libs/core/display_hdmi.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.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
@@ -30,6 +30,7 @@
 
 #include "display_base.h"
 #include "dump_impl.h"
+#include "hw_events_interface.h"
 
 namespace sdm {
 
@@ -61,11 +62,10 @@
   bool underscan_supported_ = false;
   HWScanSupport scan_support_;
   std::map<LayerBufferS3DFormat, HWS3DMode> s3d_format_to_mode_;
-  std::vector<const char *> event_list_ = {"vsync_event", "idle_notify", "cec/rd_msg",
-                                           "thread_exit"};
+  std::vector<HWEvent> event_list_ = { HWEvent::VSYNC, HWEvent::IDLE_NOTIFY, HWEvent::EXIT,
+    HWEvent::CEC_READ_MESSAGE };
 };
 
 }  // namespace sdm
 
 #endif  // __DISPLAY_HDMI_H__
-
diff --git a/msm8998/sdm/libs/core/display_primary.cpp b/msm8998/sdm/libs/core/display_primary.cpp
index 99aa3d5..3a1541e 100644
--- a/msm8998/sdm/libs/core/display_primary.cpp
+++ b/msm8998/sdm/libs/core/display_primary.cpp
@@ -71,7 +71,7 @@
 
   avr_prop_disabled_ = Debug::IsAVRDisabled();
 
-  error = HWEventsInterface::Create(INT(display_type_), this, &event_list_, &hw_events_intf_);
+  error = HWEventsInterface::Create(INT(display_type_), this, event_list_, &hw_events_intf_);
   if (error != kErrorNone) {
     DLOGE("Failed to create hardware events interface. Error = %d", error);
     DisplayBase::Deinit();
diff --git a/msm8998/sdm/libs/core/display_primary.h b/msm8998/sdm/libs/core/display_primary.h
index 7471899..70c938e 100644
--- a/msm8998/sdm/libs/core/display_primary.h
+++ b/msm8998/sdm/libs/core/display_primary.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.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
@@ -29,6 +29,7 @@
 
 #include "display_base.h"
 #include "dump_impl.h"
+#include "hw_events_interface.h"
 
 namespace sdm {
 
@@ -62,8 +63,8 @@
   bool NeedsAVREnable();
 
   uint32_t idle_timeout_ms_ = 0;
-  std::vector<const char *> event_list_ = {"vsync_event", "show_blank_event", "idle_notify",
-                                           "msm_fb_thermal_level", "thread_exit"};
+  std::vector<HWEvent> event_list_ = { HWEvent::VSYNC, HWEvent::EXIT, HWEvent::IDLE_NOTIFY,
+      HWEvent::SHOW_BLANK_EVENT, HWEvent::THERMAL_LEVEL };
   bool avr_prop_disabled_ = false;
   bool switch_to_cmd_ = false;
 };
diff --git a/msm8998/sdm/libs/core/display_virtual.cpp b/msm8998/sdm/libs/core/display_virtual.cpp
index 04922c4..6baa3ed 100644
--- a/msm8998/sdm/libs/core/display_virtual.cpp
+++ b/msm8998/sdm/libs/core/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 met:
@@ -86,7 +86,6 @@
   DisplayError error = kErrorNone;
   HWDisplayAttributes display_attributes;
   HWMixerAttributes mixer_attributes;
-  DisplayConfigVariableInfo fb_config = *variable_info;
 
   display_attributes.x_pixels = variable_info->x_pixels;
   display_attributes.y_pixels = variable_info->y_pixels;
@@ -106,10 +105,6 @@
     return error;
   }
 
-  // Override x_pixels and y_pixels of frame buffer with mixer width and height
-  fb_config.x_pixels = mixer_attributes.width;
-  fb_config.y_pixels = mixer_attributes.height;
-
   // if display is already connected, unregister display from composition manager and register
   // the display with new configuration.
   if (display_comp_ctx_) {
@@ -117,14 +112,16 @@
   }
 
   error = comp_manager_->RegisterDisplay(display_type_, display_attributes, hw_panel_info_,
-                                         mixer_attributes, fb_config, &display_comp_ctx_);
+                                         mixer_attributes, fb_config_, &display_comp_ctx_);
   if (error != kErrorNone) {
     return error;
   }
 
   display_attributes_ = display_attributes;
   mixer_attributes_ = mixer_attributes;
-  fb_config_ = fb_config;
+
+  DLOGI("Virtual display resolution changed to[%dx%d]", display_attributes_.x_pixels,
+        display_attributes_.y_pixels);
 
   return kErrorNone;
 }
diff --git a/msm8998/sdm/libs/core/drm/hw_device_drm.cpp b/msm8998/sdm/libs/core/drm/hw_device_drm.cpp
new file mode 100644
index 0000000..a72aae7
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_device_drm.cpp
@@ -0,0 +1,701 @@
+/*
+* Copyright (c) 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.
+*/
+
+#define __STDC_FORMAT_MACROS
+
+#include <ctype.h>
+#include <drm_lib_loader.h>
+#include <drm_master.h>
+#include <drm_res_mgr.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <linux/fb.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+#include <utils/sys.h>
+
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "hw_device_drm.h"
+#include "hw_info_interface.h"
+
+#define __CLASS__ "HWDeviceDRM"
+
+using std::string;
+using std::to_string;
+using std::fstream;
+using drm_utils::DRMMaster;
+using drm_utils::DRMResMgr;
+using drm_utils::DRMLibLoader;
+using sde_drm::GetDRMManager;
+using sde_drm::DestroyDRMManager;
+using sde_drm::DRMDisplayType;
+using sde_drm::DRMDisplayToken;
+using sde_drm::DRMConnectorInfo;
+using sde_drm::DRMRect;
+using sde_drm::DRMBlendType;
+using sde_drm::DRMOps;
+using sde_drm::DRMTopology;
+
+namespace sdm {
+
+HWDeviceDRM::HWDeviceDRM(BufferSyncHandler *buffer_sync_handler, HWInfoInterface *hw_info_intf)
+    : hw_info_intf_(hw_info_intf), buffer_sync_handler_(buffer_sync_handler) {
+  device_type_ = kDevicePrimary;
+  device_name_ = "Peripheral Display";
+  hw_info_intf_ = hw_info_intf;
+}
+
+DisplayError HWDeviceDRM::Init() {
+  default_mode_ = (DRMLibLoader::GetInstance()->IsLoaded() == false);
+
+  if (!default_mode_) {
+    DRMMaster *drm_master = {};
+    int dev_fd = -1;
+    DRMMaster::GetInstance(&drm_master);
+    drm_master->GetHandle(&dev_fd);
+    DRMLibLoader::GetInstance()->FuncGetDRMManager()(dev_fd, &drm_mgr_intf_);
+    if (drm_mgr_intf_->RegisterDisplay(DRMDisplayType::PERIPHERAL, &token_)) {
+      DLOGE("RegisterDisplay failed");
+      return kErrorResources;
+    }
+
+    drm_mgr_intf_->CreateAtomicReq(token_, &drm_atomic_intf_);
+    drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+    InitializeConfigs();
+    drm_atomic_intf_->Perform(DRMOps::CRTC_SET_MODE, token_.crtc_id, &current_mode_);
+#ifdef USE_SPECULATIVE_FENCES
+    drm_atomic_intf_->Perform(DRMOps::CRTC_SET_OUTPUT_FENCE_OFFSET, token_.crtc_id, 1);
+#endif
+    // TODO(user): Enable this and remove the one in SetupAtomic() onces underruns are fixed
+    // drm_atomic_intf_->Perform(DRMOps::CRTC_SET_ACTIVE, token_.crtc_id, 1);
+    // Commit to setup pipeline with mode, which then tells us the topology etc
+    if (drm_atomic_intf_->Commit(true /* synchronous */)) {
+      DLOGE("Setting up CRTC %d, Connector %d for %s failed", token_.crtc_id, token_.conn_id,
+            device_name_);
+      return kErrorResources;
+    }
+
+    // Reload connector info for updated info after 1st commit
+    drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+    DLOGI("Setup CRTC %d, Connector %d for %s", token_.crtc_id, token_.conn_id, device_name_);
+  }
+
+  PopulateDisplayAttributes();
+  PopulateHWPanelInfo();
+  UpdateMixerAttributes();
+  hw_info_intf_->GetHWResourceInfo(&hw_resource_);
+
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::Deinit() {
+  drm_mgr_intf_->DestroyAtomicReq(drm_atomic_intf_);
+  drm_atomic_intf_ = {};
+  drm_mgr_intf_->UnregisterDisplay(token_);
+  return kErrorNone;
+}
+
+void HWDeviceDRM::InitializeConfigs() {
+  // TODO(user): Update modes
+  current_mode_ = connector_info_.modes[0];
+}
+
+DisplayError HWDeviceDRM::PopulateDisplayAttributes() {
+  drmModeModeInfo mode = {};
+  uint32_t mm_width = 0;
+  uint32_t mm_height = 0;
+  DRMTopology topology = DRMTopology::SINGLE_LM;
+
+  if (default_mode_) {
+    DRMResMgr *res_mgr = nullptr;
+    int ret = DRMResMgr::GetInstance(&res_mgr);
+    if (ret < 0) {
+      DLOGE("Failed to acquire DRMResMgr instance");
+      return kErrorResources;
+    }
+
+    res_mgr->GetMode(&mode);
+    res_mgr->GetDisplayDimInMM(&mm_width, &mm_height);
+  } else {
+    mode = current_mode_;
+    mm_width = connector_info_.mmWidth;
+    mm_height = connector_info_.mmHeight;
+    topology = connector_info_.topology;
+  }
+
+  display_attributes_.x_pixels = mode.hdisplay;
+  display_attributes_.y_pixels = mode.vdisplay;
+  display_attributes_.fps = mode.vrefresh;
+  display_attributes_.vsync_period_ns = UINT32(1000000000L / display_attributes_.fps);
+
+  /*
+              Active                 Front           Sync           Back
+              Region                 Porch                          Porch
+     <-----------------------><----------------><-------------><-------------->
+     <----- [hv]display ----->
+     <------------- [hv]sync_start ------------>
+     <--------------------- [hv]sync_end --------------------->
+     <-------------------------------- [hv]total ----------------------------->
+   */
+
+  display_attributes_.v_front_porch = mode.vsync_start - mode.vdisplay;
+  display_attributes_.v_pulse_width = mode.vsync_end - mode.vsync_start;
+  display_attributes_.v_back_porch = mode.vtotal - mode.vsync_end;
+  display_attributes_.v_total = mode.vtotal;
+
+  display_attributes_.h_total = mode.htotal;
+  uint32_t h_blanking = mode.htotal - mode.hdisplay;
+  display_attributes_.is_device_split =
+      (topology == DRMTopology::DUAL_LM || topology == DRMTopology::DUAL_LM_MERGE);
+  display_attributes_.h_total += display_attributes_.is_device_split ? h_blanking : 0;
+
+  display_attributes_.x_dpi = (FLOAT(mode.hdisplay) * 25.4f) / FLOAT(mm_width);
+  display_attributes_.y_dpi = (FLOAT(mode.vdisplay) * 25.4f) / FLOAT(mm_height);
+
+  return kErrorNone;
+}
+
+void HWDeviceDRM::PopulateHWPanelInfo() {
+  hw_panel_info_ = {};
+
+  snprintf(hw_panel_info_.panel_name, sizeof(hw_panel_info_.panel_name), "%s",
+           connector_info_.panel_name.c_str());
+  hw_panel_info_.split_info.left_split = display_attributes_.x_pixels;
+  if (display_attributes_.is_device_split) {
+    hw_panel_info_.split_info.left_split = hw_panel_info_.split_info.right_split =
+        display_attributes_.x_pixels / 2;
+  }
+
+  hw_panel_info_.partial_update = 0;
+  hw_panel_info_.left_align = 0;
+  hw_panel_info_.width_align = 0;
+  hw_panel_info_.top_align = 0;
+  hw_panel_info_.height_align = 0;
+  hw_panel_info_.min_roi_width = 0;
+  hw_panel_info_.min_roi_height = 0;
+  hw_panel_info_.needs_roi_merge = 0;
+  hw_panel_info_.dynamic_fps = connector_info_.dynamic_fps;
+  hw_panel_info_.min_fps = 60;
+  hw_panel_info_.max_fps = 60;
+  hw_panel_info_.is_primary_panel = connector_info_.is_primary;
+  hw_panel_info_.is_pluggable = 0;
+
+  if (!default_mode_) {
+    hw_panel_info_.needs_roi_merge = (connector_info_.topology == DRMTopology::DUAL_LM_MERGE);
+  }
+
+  GetHWDisplayPortAndMode();
+  GetHWPanelMaxBrightness();
+
+  DLOGI("%s, Panel Interface = %s, Panel Mode = %s, Is Primary = %d", device_name_,
+        interface_str_.c_str(), hw_panel_info_.mode == kModeVideo ? "Video" : "Command",
+        hw_panel_info_.is_primary_panel);
+  DLOGI("Partial Update = %d, Dynamic FPS = %d", hw_panel_info_.partial_update,
+        hw_panel_info_.dynamic_fps);
+  DLOGI("Align: left = %d, width = %d, top = %d, height = %d", hw_panel_info_.left_align,
+        hw_panel_info_.width_align, hw_panel_info_.top_align, hw_panel_info_.height_align);
+  DLOGI("ROI: min_width = %d, min_height = %d, need_merge = %d", hw_panel_info_.min_roi_width,
+        hw_panel_info_.min_roi_height, hw_panel_info_.needs_roi_merge);
+  DLOGI("FPS: min = %d, max =%d", hw_panel_info_.min_fps, hw_panel_info_.max_fps);
+  DLOGI("Left Split = %d, Right Split = %d", hw_panel_info_.split_info.left_split,
+        hw_panel_info_.split_info.right_split);
+}
+
+void HWDeviceDRM::GetHWDisplayPortAndMode() {
+  hw_panel_info_.port = kPortDefault;
+  hw_panel_info_.mode =
+      (connector_info_.panel_mode == sde_drm::DRMPanelMode::VIDEO) ? kModeVideo : kModeCommand;
+
+  if (default_mode_) {
+    return;
+  }
+
+  switch (connector_info_.type) {
+    case DRM_MODE_CONNECTOR_DSI:
+      hw_panel_info_.port = kPortDSI;
+      interface_str_ = "DSI";
+      break;
+    case DRM_MODE_CONNECTOR_LVDS:
+      hw_panel_info_.port = kPortLVDS;
+      interface_str_ = "LVDS";
+      break;
+    case DRM_MODE_CONNECTOR_eDP:
+      hw_panel_info_.port = kPortEDP;
+      interface_str_ = "EDP";
+      break;
+    case DRM_MODE_CONNECTOR_TV:
+    case DRM_MODE_CONNECTOR_HDMIA:
+    case DRM_MODE_CONNECTOR_HDMIB:
+      hw_panel_info_.port = kPortDTV;
+      interface_str_ = "HDMI";
+      break;
+    case DRM_MODE_CONNECTOR_VIRTUAL:
+      hw_panel_info_.port = kPortWriteBack;
+      interface_str_ = "Virtual";
+      break;
+    case DRM_MODE_CONNECTOR_DisplayPort:
+      // TODO(user): Add when available
+      interface_str_ = "DisplayPort";
+      break;
+  }
+
+  return;
+}
+
+void HWDeviceDRM::GetHWPanelMaxBrightness() {
+  char brightness[kMaxStringLength] = {0};
+  char kMaxBrightnessNode[64] = {0};
+
+  snprintf(kMaxBrightnessNode, sizeof(kMaxBrightnessNode), "%s",
+           "/sys/class/leds/lcd-backlight/max_brightness");
+
+  hw_panel_info_.panel_max_brightness = 255;
+  int fd = Sys::open_(kMaxBrightnessNode, O_RDONLY);
+  if (fd < 0) {
+    DLOGW("Failed to open max brightness node = %s, error = %s", kMaxBrightnessNode,
+          strerror(errno));
+    return;
+  }
+
+  if (Sys::pread_(fd, brightness, sizeof(brightness), 0) > 0) {
+    hw_panel_info_.panel_max_brightness = atoi(brightness);
+    DLOGI("Max brightness level = %d", hw_panel_info_.panel_max_brightness);
+  } else {
+    DLOGW("Failed to read max brightness level. error = %s", strerror(errno));
+  }
+
+  Sys::close_(fd);
+}
+
+DisplayError HWDeviceDRM::GetActiveConfig(uint32_t *active_config) {
+  *active_config = 0;
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::GetNumDisplayAttributes(uint32_t *count) {
+  *count = 1;
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::GetDisplayAttributes(uint32_t index,
+                                            HWDisplayAttributes *display_attributes) {
+  *display_attributes = display_attributes_;
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::GetHWPanelInfo(HWPanelInfo *panel_info) {
+  *panel_info = hw_panel_info_;
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::SetDisplayAttributes(uint32_t index) {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::SetDisplayAttributes(const HWDisplayAttributes &display_attributes) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::GetConfigIndex(uint32_t mode, uint32_t *index) {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::PowerOn() {
+  DTRACE_SCOPED();
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::PowerOff() {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::Doze() {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::DozeSuspend() {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::Standby() {
+  return kErrorNone;
+}
+
+void HWDeviceDRM::SetupAtomic(HWLayers *hw_layers, bool validate) {
+  if (default_mode_) {
+    return;
+  }
+
+  HWLayersInfo &hw_layer_info = hw_layers->info;
+  uint32_t hw_layer_count = UINT32(hw_layer_info.hw_layers.size());
+
+  for (uint32_t i = 0; i < hw_layer_count; i++) {
+    Layer &layer = hw_layer_info.hw_layers.at(i);
+    LayerBuffer &input_buffer = layer.input_buffer;
+    HWPipeInfo *left_pipe = &hw_layers->config[i].left_pipe;
+    HWPipeInfo *right_pipe = &hw_layers->config[i].right_pipe;
+
+    // TODO(user): Add support for solid fill
+    if (layer.flags.solid_fill) {
+      continue;
+    }
+
+    for (uint32_t count = 0; count < 2; count++) {
+      HWPipeInfo *pipe_info = (count == 0) ? left_pipe : right_pipe;
+      if (pipe_info->valid) {
+        uint32_t pipe_id = pipe_info->pipe_id;
+        if (input_buffer.fb_id == 0) {
+          // We set these to 0 to clear any previous cycle's state from another buffer.
+          // Unfortunately this layer will be skipped from validation because it's dimensions are
+          // tied to fb_id which is not available yet.
+          drm_atomic_intf_->Perform(DRMOps::PLANE_SET_FB_ID, pipe_id, 0);
+          drm_atomic_intf_->Perform(DRMOps::PLANE_SET_CRTC, pipe_id, 0);
+          continue;
+        }
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_ALPHA, pipe_id, layer.plane_alpha);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_ZORDER, pipe_id, pipe_info->z_order);
+        DRMBlendType blending = {};
+        SetBlending(layer.blending, &blending);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_BLEND_TYPE, pipe_id, blending);
+        DRMRect src = {};
+        SetRect(pipe_info->src_roi, &src);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_SRC_RECT, pipe_id, src);
+        DRMRect dst = {};
+        SetRect(pipe_info->dst_roi, &dst);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_DST_RECT, pipe_id, dst);
+        uint32_t rot_bit_mask = 0;
+        if (layer.transform.flip_horizontal) {
+          rot_bit_mask |= 1 << DRM_REFLECT_X;
+        }
+        if (layer.transform.flip_vertical) {
+          rot_bit_mask |= 1 << DRM_REFLECT_Y;
+        }
+
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_H_DECIMATION, pipe_id,
+                                  pipe_info->horizontal_decimation);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_V_DECIMATION, pipe_id,
+                                  pipe_info->vertical_decimation);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_ROTATION, pipe_id, rot_bit_mask);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_FB_ID, pipe_id, input_buffer.fb_id);
+        drm_atomic_intf_->Perform(DRMOps::PLANE_SET_CRTC, pipe_id, token_.crtc_id);
+        if (!validate && input_buffer.acquire_fence_fd >= 0) {
+          drm_atomic_intf_->Perform(DRMOps::PLANE_SET_INPUT_FENCE, pipe_id,
+                                    input_buffer.acquire_fence_fd);
+        }
+      }
+    }
+
+    // TODO(user): Remove this and enable the one in Init() onces underruns are fixed
+    drm_atomic_intf_->Perform(DRMOps::CRTC_SET_ACTIVE, token_.crtc_id, 1);
+  }
+}
+
+DisplayError HWDeviceDRM::Validate(HWLayers *hw_layers) {
+  DTRACE_SCOPED();
+  SetupAtomic(hw_layers, true /* validate */);
+
+  int ret = drm_atomic_intf_->Validate();
+  if (ret) {
+    DLOGE("%s failed with error %d", __FUNCTION__, ret);
+    return kErrorHardware;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::Commit(HWLayers *hw_layers) {
+  DTRACE_SCOPED();
+  if (default_mode_) {
+    return DefaultCommit(hw_layers);
+  }
+
+  return AtomicCommit(hw_layers);
+}
+
+DisplayError HWDeviceDRM::DefaultCommit(HWLayers *hw_layers) {
+  DTRACE_SCOPED();
+
+  HWLayersInfo &hw_layer_info = hw_layers->info;
+  LayerStack *stack = hw_layer_info.stack;
+
+  stack->retire_fence_fd = -1;
+  for (Layer &layer : hw_layer_info.hw_layers) {
+    layer.input_buffer.release_fence_fd = -1;
+  }
+
+  DRMMaster *master = nullptr;
+  int ret = DRMMaster::GetInstance(&master);
+  if (ret < 0) {
+    DLOGE("Failed to acquire DRMMaster instance");
+    return kErrorResources;
+  }
+
+  DRMResMgr *res_mgr = nullptr;
+  ret = DRMResMgr::GetInstance(&res_mgr);
+  if (ret < 0) {
+    DLOGE("Failed to acquire DRMResMgr instance");
+    return kErrorResources;
+  }
+
+  int dev_fd = -1;
+  master->GetHandle(&dev_fd);
+
+  uint32_t connector_id = 0;
+  res_mgr->GetConnectorId(&connector_id);
+
+  uint32_t crtc_id = 0;
+  res_mgr->GetCrtcId(&crtc_id);
+
+  drmModeModeInfo mode;
+  res_mgr->GetMode(&mode);
+
+  LayerBuffer &input_buffer = hw_layer_info.hw_layers.at(0).input_buffer;
+  ret = drmModeSetCrtc(dev_fd, crtc_id, input_buffer.fb_id, 0 /* x */, 0 /* y */, &connector_id,
+                       1 /* num_connectors */, &mode);
+  if (ret < 0) {
+    DLOGE("drmModeSetCrtc failed dev fd %d, fb_id %d, crtc id %d, connector id %d, %s", dev_fd,
+          input_buffer.fb_id, crtc_id, connector_id, strerror(errno));
+    return kErrorHardware;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::AtomicCommit(HWLayers *hw_layers) {
+  DTRACE_SCOPED();
+  SetupAtomic(hw_layers, false /* validate */);
+
+  int ret = drm_atomic_intf_->Commit(false /* synchronous */);
+  if (ret) {
+    DLOGE("%s failed with error %d", __FUNCTION__, ret);
+    return kErrorHardware;
+  }
+
+  int release_fence = -1;
+  int retire_fence = -1;
+
+  drm_atomic_intf_->Perform(DRMOps::CRTC_GET_RELEASE_FENCE, token_.crtc_id, &release_fence);
+  drm_atomic_intf_->Perform(DRMOps::CONNECTOR_GET_RETIRE_FENCE, token_.conn_id, &retire_fence);
+
+  HWLayersInfo &hw_layer_info = hw_layers->info;
+  LayerStack *stack = hw_layer_info.stack;
+  stack->retire_fence_fd = retire_fence;
+
+  for (Layer &layer : hw_layer_info.hw_layers) {
+    layer.input_buffer.release_fence_fd = Sys::dup_(release_fence);
+  }
+
+  hw_layer_info.sync_handle = release_fence;
+
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::Flush() {
+  return kErrorNone;
+}
+
+void HWDeviceDRM::SetBlending(const LayerBlending &source, DRMBlendType *target) {
+  switch (source) {
+    case kBlendingPremultiplied:
+      *target = DRMBlendType::PREMULTIPLIED;
+      break;
+    case kBlendingOpaque:
+      *target = DRMBlendType::OPAQUE;
+      break;
+    case kBlendingCoverage:
+      *target = DRMBlendType::COVERAGE;
+      break;
+    default:
+      *target = DRMBlendType::UNDEFINED;
+  }
+}
+
+void HWDeviceDRM::SetRect(const LayerRect &source, DRMRect *target) {
+  target->left = UINT32(source.left);
+  target->top = UINT32(source.top);
+  target->right = UINT32(source.right);
+  target->bottom = UINT32(source.bottom);
+}
+
+bool HWDeviceDRM::EnableHotPlugDetection(int enable) {
+  return true;
+}
+
+void HWDeviceDRM::ResetDisplayParams() {}
+
+DisplayError HWDeviceDRM::SetCursorPosition(HWLayers *hw_layers, int x, int y) {
+  DTRACE_SCOPED();
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::GetPPFeaturesVersion(PPFeatureVersion *vers) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetPPFeatures(PPFeaturesConfig *feature_list) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetVSyncState(bool enable) {
+  return kErrorNone;
+}
+
+void HWDeviceDRM::SetIdleTimeoutMs(uint32_t timeout_ms) {}
+
+DisplayError HWDeviceDRM::SetDisplayMode(const HWDisplayMode hw_display_mode) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetRefreshRate(uint32_t refresh_rate) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetPanelBrightness(int level) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::GetHWScanInfo(HWScanInfo *scan_info) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::GetVideoFormat(uint32_t config_index, uint32_t *video_format) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::GetMaxCEAFormat(uint32_t *max_cea_format) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::GetPanelBrightness(int *level) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetS3DMode(HWS3DMode s3d_mode) {
+  return kErrorNotSupported;
+}
+
+DisplayError HWDeviceDRM::SetScaleLutConfig(HWScaleLutInfo *lut_info) {
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::SetMixerAttributes(const HWMixerAttributes &mixer_attributes) {
+  if (!hw_resource_.hw_dest_scalar_info.count) {
+    return kErrorNotSupported;
+  }
+
+  if (mixer_attributes.width > display_attributes_.x_pixels ||
+      mixer_attributes.height > display_attributes_.y_pixels) {
+    DLOGW("Input resolution exceeds display resolution! input: res %dx%d display: res %dx%d",
+          mixer_attributes.width, mixer_attributes.height, display_attributes_.x_pixels,
+          display_attributes_.y_pixels);
+    return kErrorNotSupported;
+  }
+
+  uint32_t max_input_width = hw_resource_.hw_dest_scalar_info.max_input_width;
+  if (display_attributes_.is_device_split) {
+    max_input_width *= 2;
+  }
+
+  if (mixer_attributes.width > max_input_width) {
+    DLOGW("Input width exceeds width limit! input_width %d width_limit %d", mixer_attributes.width,
+          max_input_width);
+    return kErrorNotSupported;
+  }
+
+  float mixer_aspect_ratio = FLOAT(mixer_attributes.width) / FLOAT(mixer_attributes.height);
+  float display_aspect_ratio =
+      FLOAT(display_attributes_.x_pixels) / FLOAT(display_attributes_.y_pixels);
+
+  if (display_aspect_ratio != mixer_aspect_ratio) {
+    DLOGW("Aspect ratio mismatch! input: res %dx%d display: res %dx%d", mixer_attributes.width,
+          mixer_attributes.height, display_attributes_.x_pixels, display_attributes_.y_pixels);
+    return kErrorNotSupported;
+  }
+
+  float scale_x = FLOAT(display_attributes_.x_pixels) / FLOAT(mixer_attributes.width);
+  float scale_y = FLOAT(display_attributes_.y_pixels) / FLOAT(mixer_attributes.height);
+  float max_scale_up = hw_resource_.hw_dest_scalar_info.max_scale_up;
+  if (scale_x > max_scale_up || scale_y > max_scale_up) {
+    DLOGW(
+        "Up scaling ratio exceeds for destination scalar upscale limit scale_x %f scale_y %f "
+        "max_scale_up %f",
+        scale_x, scale_y, max_scale_up);
+    return kErrorNotSupported;
+  }
+
+  float mixer_split_ratio = FLOAT(mixer_attributes_.split_left) / FLOAT(mixer_attributes_.width);
+
+  mixer_attributes_ = mixer_attributes;
+  mixer_attributes_.split_left = mixer_attributes_.width;
+  if (display_attributes_.is_device_split) {
+    mixer_attributes_.split_left = UINT32(FLOAT(mixer_attributes.width) * mixer_split_ratio);
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWDeviceDRM::GetMixerAttributes(HWMixerAttributes *mixer_attributes) {
+  if (!mixer_attributes) {
+    return kErrorParameters;
+  }
+
+  mixer_attributes_.width = display_attributes_.x_pixels;
+  mixer_attributes_.height = display_attributes_.y_pixels;
+  mixer_attributes_.split_left = display_attributes_.is_device_split
+                                     ? hw_panel_info_.split_info.left_split
+                                     : mixer_attributes_.width;
+  *mixer_attributes = mixer_attributes_;
+
+  return kErrorNone;
+}
+
+void HWDeviceDRM::UpdateMixerAttributes() {
+  mixer_attributes_.width = display_attributes_.x_pixels;
+  mixer_attributes_.height = display_attributes_.y_pixels;
+  mixer_attributes_.split_left = display_attributes_.is_device_split
+                                     ? hw_panel_info_.split_info.left_split
+                                     : mixer_attributes_.width;
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/drm/hw_device_drm.h b/msm8998/sdm/libs/core/drm/hw_device_drm.h
new file mode 100644
index 0000000..db135a0
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_device_drm.h
@@ -0,0 +1,137 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __HW_DEVICE_DRM_H__
+#define __HW_DEVICE_DRM_H__
+
+#include <drm_interface.h>
+#include <errno.h>
+#include <pthread.h>
+#include <xf86drmMode.h>
+#include <string>
+#include <vector>
+
+#include "hw_interface.h"
+
+#define IOCTL_LOGE(ioctl, type) \
+  DLOGE("ioctl %s, device = %d errno = %d, desc = %s", #ioctl, type, errno, strerror(errno))
+
+namespace sdm {
+class HWInfoInterface;
+
+class HWDeviceDRM : public HWInterface {
+ public:
+  explicit HWDeviceDRM(BufferSyncHandler *buffer_sync_handler, HWInfoInterface *hw_info_intf);
+  virtual ~HWDeviceDRM() {}
+  virtual DisplayError Init();
+  virtual DisplayError Deinit();
+
+ protected:
+  // From HWInterface
+  virtual DisplayError GetActiveConfig(uint32_t *active_config);
+  virtual DisplayError GetNumDisplayAttributes(uint32_t *count);
+  virtual DisplayError GetDisplayAttributes(uint32_t index,
+                                            HWDisplayAttributes *display_attributes);
+  virtual DisplayError GetHWPanelInfo(HWPanelInfo *panel_info);
+  virtual DisplayError SetDisplayAttributes(uint32_t index);
+  virtual DisplayError SetDisplayAttributes(const HWDisplayAttributes &display_attributes);
+  virtual DisplayError GetConfigIndex(uint32_t mode, uint32_t *index);
+  virtual DisplayError PowerOn();
+  virtual DisplayError PowerOff();
+  virtual DisplayError Doze();
+  virtual DisplayError DozeSuspend();
+  virtual DisplayError Standby();
+  virtual DisplayError Validate(HWLayers *hw_layers);
+  virtual DisplayError Commit(HWLayers *hw_layers);
+  virtual DisplayError Flush();
+  virtual DisplayError GetPPFeaturesVersion(PPFeatureVersion *vers);
+  virtual DisplayError SetPPFeatures(PPFeaturesConfig *feature_list);
+  virtual DisplayError SetVSyncState(bool enable);
+  virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
+  virtual DisplayError SetDisplayMode(const HWDisplayMode hw_display_mode);
+  virtual DisplayError SetRefreshRate(uint32_t refresh_rate);
+  virtual DisplayError SetPanelBrightness(int level);
+  virtual DisplayError GetHWScanInfo(HWScanInfo *scan_info);
+  virtual DisplayError GetVideoFormat(uint32_t config_index, uint32_t *video_format);
+  virtual DisplayError GetMaxCEAFormat(uint32_t *max_cea_format);
+  virtual DisplayError SetCursorPosition(HWLayers *hw_layers, int x, int y);
+  virtual DisplayError OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
+  virtual DisplayError GetPanelBrightness(int *level);
+  virtual DisplayError SetAutoRefresh(bool enable) { return kErrorNone; }
+  virtual DisplayError SetS3DMode(HWS3DMode s3d_mode);
+  virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info);
+  virtual DisplayError SetMixerAttributes(const HWMixerAttributes &mixer_attributes);
+  virtual DisplayError GetMixerAttributes(HWMixerAttributes *mixer_attributes);
+
+  enum {
+    kHWEventVSync,
+    kHWEventBlank,
+  };
+
+  static const int kMaxStringLength = 1024;
+  static const int kNumPhysicalDisplays = 2;
+
+  DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target);
+  DisplayError SetStride(HWDeviceType device_type, LayerBufferFormat format, uint32_t width,
+                         uint32_t *target);
+  DisplayError PopulateDisplayAttributes();
+  void PopulateHWPanelInfo();
+  void GetHWDisplayPortAndMode();
+  void GetHWPanelMaxBrightness();
+  void ResetDisplayParams();
+  bool EnableHotPlugDetection(int enable);
+  void UpdateMixerAttributes();
+  void InitializeConfigs();
+  void SetBlending(const LayerBlending &source, sde_drm::DRMBlendType *target);
+  void SetRect(const LayerRect &source, sde_drm::DRMRect *target);
+  DisplayError DefaultCommit(HWLayers *hw_layers);
+  DisplayError AtomicCommit(HWLayers *hw_layers);
+  void SetupAtomic(HWLayers *hw_layers, bool validate);
+
+  HWResourceInfo hw_resource_ = {};
+  HWPanelInfo hw_panel_info_ = {};
+  HWInfoInterface *hw_info_intf_ = {};
+  BufferSyncHandler *buffer_sync_handler_ = {};
+  HWDeviceType device_type_ = {};
+  const char *device_name_ = {};
+  bool synchronous_commit_ = false;
+  HWDisplayAttributes display_attributes_ = {};
+  HWMixerAttributes mixer_attributes_ = {};
+  sde_drm::DRMManagerInterface *drm_mgr_intf_ = {};
+  sde_drm::DRMAtomicReqInterface *drm_atomic_intf_ = {};
+  sde_drm::DRMDisplayToken token_ = {};
+  drmModeModeInfo current_mode_ = {};
+  bool default_mode_ = false;
+  sde_drm::DRMConnectorInfo connector_info_ = {};
+  std::string interface_str_ = "DSI";
+};
+
+}  // namespace sdm
+
+#endif  // __HW_DEVICE_DRM_H__
diff --git a/msm8998/sdm/libs/core/drm/hw_events_drm.cpp b/msm8998/sdm/libs/core/drm/hw_events_drm.cpp
new file mode 100644
index 0000000..81384e4
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_events_drm.cpp
@@ -0,0 +1,298 @@
+/*
+* Copyright (c) 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 <drm_master.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <math.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/prctl.h>
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <utils/debug.h>
+#include <utils/sys.h>
+#include <xf86drm.h>
+
+#include <algorithm>
+#include <map>
+#include <utility>
+#include <vector>
+
+#include "hw_events_drm.h"
+
+#define __CLASS__ "HWEventsDRM"
+
+namespace sdm {
+
+using drm_utils::DRMMaster;
+
+DisplayError HWEventsDRM::InitializePollFd() {
+  for (uint32_t i = 0; i < event_data_list_.size(); i++) {
+    char data[kMaxStringLength]{};
+    HWEventData &event_data = event_data_list_[i];
+    poll_fds_[i].fd = -1;
+
+    switch (event_data.event_type) {
+      case HWEvent::VSYNC: {
+        poll_fds_[i].events = POLLIN | POLLPRI | POLLERR;
+        DRMMaster *master = nullptr;
+        int ret = DRMMaster::GetInstance(&master);
+        if (ret < 0) {
+          DLOGE("Failed to acquire DRMMaster instance");
+          return kErrorNotSupported;
+        }
+        master->GetHandle(&poll_fds_[i].fd);
+        vsync_index_ = i;
+      } break;
+      case HWEvent::EXIT: {
+        // Create an eventfd to be used to unblock the poll system call when
+        // a thread is exiting.
+        poll_fds_[i].fd = Sys::eventfd_(0, 0);
+        poll_fds_[i].events |= POLLIN;
+        // Clear any existing data
+        Sys::pread_(poll_fds_[i].fd, data, kMaxStringLength, 0);
+      } break;
+      case HWEvent::IDLE_NOTIFY:
+      case HWEvent::CEC_READ_MESSAGE:
+      case HWEvent::SHOW_BLANK_EVENT:
+      case HWEvent::THERMAL_LEVEL:
+        break;
+    }
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWEventsDRM::SetEventParser() {
+  DisplayError error = kErrorNone;
+
+  for (auto &event_data : event_data_list_) {
+    switch (event_data.event_type) {
+      case HWEvent::VSYNC:
+        event_data.event_parser = &HWEventsDRM::HandleVSync;
+        break;
+      case HWEvent::IDLE_NOTIFY:
+        event_data.event_parser = &HWEventsDRM::HandleIdleTimeout;
+        break;
+      case HWEvent::CEC_READ_MESSAGE:
+        event_data.event_parser = &HWEventsDRM::HandleCECMessage;
+        break;
+      case HWEvent::EXIT:
+        event_data.event_parser = &HWEventsDRM::HandleThreadExit;
+        break;
+      case HWEvent::SHOW_BLANK_EVENT:
+        event_data.event_parser = &HWEventsDRM::HandleBlank;
+        break;
+      case HWEvent::THERMAL_LEVEL:
+        event_data.event_parser = &HWEventsDRM::HandleThermal;
+        break;
+      default:
+        error = kErrorParameters;
+        break;
+    }
+  }
+
+  return error;
+}
+
+void HWEventsDRM::PopulateHWEventData(const vector<HWEvent> &event_list) {
+  for (auto &event : event_list) {
+    HWEventData event_data;
+    event_data.event_type = event;
+    event_data_list_.push_back(std::move(event_data));
+  }
+
+  SetEventParser();
+  InitializePollFd();
+}
+
+DisplayError HWEventsDRM::Init(int display_type, HWEventHandler *event_handler,
+                               const vector<HWEvent> &event_list) {
+  if (!event_handler)
+    return kErrorParameters;
+
+  event_handler_ = event_handler;
+  poll_fds_.resize(event_list.size());
+  event_thread_name_ += " - " + std::to_string(display_type);
+
+  PopulateHWEventData(event_list);
+
+  if (pthread_create(&event_thread_, NULL, &DisplayEventThread, this) < 0) {
+    DLOGE("Failed to start %s, error = %s", event_thread_name_.c_str());
+    return kErrorResources;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWEventsDRM::Deinit() {
+  exit_threads_ = true;
+  Sys::pthread_cancel_(event_thread_);
+
+  for (uint32_t i = 0; i < event_data_list_.size(); i++) {
+    if (event_data_list_[i].event_type == HWEvent::EXIT) {
+      uint64_t exit_value = 1;
+      ssize_t write_size = Sys::write_(poll_fds_[i].fd, &exit_value, sizeof(uint64_t));
+      if (write_size != sizeof(uint64_t)) {
+        DLOGW("Error triggering exit fd (%d). write size = %d, error = %s", poll_fds_[i].fd,
+              write_size, strerror(errno));
+      }
+    }
+  }
+
+  pthread_join(event_thread_, NULL);
+  CloseFds();
+
+  return kErrorNone;
+}
+
+DisplayError HWEventsDRM::CloseFds() {
+  for (uint32_t i = 0; i < event_data_list_.size(); i++) {
+    switch (event_data_list_[i].event_type) {
+      case HWEvent::VSYNC:
+        poll_fds_[i].fd = -1;
+        break;
+      case HWEvent::EXIT:
+        Sys::close_(poll_fds_[i].fd);
+        poll_fds_[i].fd = -1;
+        break;
+      case HWEvent::IDLE_NOTIFY:
+      case HWEvent::CEC_READ_MESSAGE:
+      case HWEvent::SHOW_BLANK_EVENT:
+      case HWEvent::THERMAL_LEVEL:
+        break;
+      default:
+        return kErrorNotSupported;
+    }
+  }
+
+  return kErrorNone;
+}
+
+void *HWEventsDRM::DisplayEventThread(void *context) {
+  if (context) {
+    return reinterpret_cast<HWEventsDRM *>(context)->DisplayEventHandler();
+  }
+
+  return NULL;
+}
+
+void *HWEventsDRM::DisplayEventHandler() {
+  char data[kMaxStringLength]{};
+
+  prctl(PR_SET_NAME, event_thread_name_.c_str(), 0, 0, 0);
+  setpriority(PRIO_PROCESS, 0, kThreadPriorityUrgent);
+
+  while (!exit_threads_) {
+    if (RegisterVSync() != kErrorNone) {
+      pthread_exit(0);
+      return nullptr;
+    }
+
+    int error = Sys::poll_(poll_fds_.data(), UINT32(poll_fds_.size()), -1);
+    if (error <= 0) {
+      DLOGW("poll failed. error = %s", strerror(errno));
+      continue;
+    }
+
+    for (uint32_t i = 0; i < event_data_list_.size(); i++) {
+      pollfd &poll_fd = poll_fds_[i];
+      switch (event_data_list_[i].event_type) {
+        case HWEvent::VSYNC:
+          (this->*(event_data_list_[i]).event_parser)(nullptr);
+          break;
+        case HWEvent::EXIT:
+          if ((poll_fd.revents & POLLIN) &&
+              (Sys::read_(poll_fd.fd, data, kMaxStringLength) > 0)) {
+            (this->*(event_data_list_[i]).event_parser)(data);
+          }
+          break;
+        case HWEvent::IDLE_NOTIFY:
+        case HWEvent::CEC_READ_MESSAGE:
+        case HWEvent::SHOW_BLANK_EVENT:
+        case HWEvent::THERMAL_LEVEL:
+          if (poll_fd.fd >= 0 && (poll_fd.revents & POLLPRI) &&
+              (Sys::pread_(poll_fd.fd, data, kMaxStringLength, 0) > 0)) {
+            (this->*(event_data_list_[i]).event_parser)(data);
+          }
+          break;
+      }
+    }
+  }
+
+  pthread_exit(0);
+
+  return nullptr;
+}
+
+DisplayError HWEventsDRM::RegisterVSync() {
+  drmVBlank vblank{};
+  vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT);
+  vblank.request.sequence = 1;
+  // DRM hack to pass in context to unused field signal. Driver will write this to the node being
+  // polled on, and will be read as part of drm event handling and sent to handler
+  vblank.request.signal = reinterpret_cast<unsigned long>(this);  // NOLINT
+  int error = drmWaitVBlank(poll_fds_[vsync_index_].fd, &vblank);
+  if (error < 0) {
+    DLOGE("drmWaitVBlank failed with err %d", errno);
+    return kErrorResources;
+  }
+
+  return kErrorNone;
+}
+
+void HWEventsDRM::HandleVSync(char *data) {
+  if (poll_fds_[vsync_index_].revents & (POLLIN | POLLPRI)) {
+    drmEventContext event = {};
+    event.version = DRM_EVENT_CONTEXT_VERSION;
+    event.vblank_handler = &HWEventsDRM::VSyncHandlerCallback;
+    int error = drmHandleEvent(poll_fds_[vsync_index_].fd, &event);
+    if (error != 0) {
+      DLOGE("drmHandleEvent failed: %i", error);
+    }
+  }
+}
+
+void HWEventsDRM::VSyncHandlerCallback(int fd, unsigned int sequence, unsigned int tv_sec,
+                                       unsigned int tv_usec, void *data) {
+  int64_t timestamp = (int64_t)(tv_sec)*1000000000 + (int64_t)(tv_usec)*1000;
+  reinterpret_cast<HWEventsDRM *>(data)->event_handler_->VSync(timestamp);
+}
+
+void HWEventsDRM::HandleIdleTimeout(char *data) {
+  event_handler_->IdleTimeout();
+}
+
+void HWEventsDRM::HandleCECMessage(char *data) {
+  event_handler_->CECMessage(data);
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/drm/hw_events_drm.h b/msm8998/sdm/libs/core/drm/hw_events_drm.h
new file mode 100644
index 0000000..9f8dc38
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_events_drm.h
@@ -0,0 +1,90 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __HW_EVENTS_DRM_H__
+#define __HW_EVENTS_DRM_H__
+
+#include <sys/poll.h>
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "hw_events_interface.h"
+#include "hw_interface.h"
+
+namespace sdm {
+
+using std::vector;
+
+class HWEventsDRM : public HWEventsInterface {
+ public:
+  virtual DisplayError Init(int display_type, HWEventHandler *event_handler,
+                            const vector<HWEvent> &event_list);
+  virtual DisplayError Deinit();
+
+ private:
+  static const int kMaxStringLength = 1024;
+
+  typedef void (HWEventsDRM::*EventParser)(char *);
+
+  struct HWEventData {
+    HWEvent event_type {};
+    EventParser event_parser {};
+  };
+
+  static void *DisplayEventThread(void *context);
+  static void VSyncHandlerCallback(int fd, unsigned int sequence, unsigned int tv_sec,
+                                   unsigned int tv_usec, void *data);
+
+  void *DisplayEventHandler();
+  void HandleVSync(char *data);
+  void HandleIdleTimeout(char *data);
+  void HandleCECMessage(char *data);
+  void HandleThreadExit(char *data) {}
+  void HandleThermal(char *data) {}
+  void HandleBlank(char *data) {}
+  void PopulateHWEventData(const vector<HWEvent> &event_list);
+  DisplayError SetEventParser();
+  DisplayError InitializePollFd();
+  DisplayError CloseFds();
+  DisplayError RegisterVSync();
+
+  HWEventHandler *event_handler_{};
+  vector<HWEventData> event_data_list_{};
+  vector<pollfd> poll_fds_{};
+  pthread_t event_thread_{};
+  std::string event_thread_name_ = "SDM_EventThread";
+  bool exit_threads_ = false;
+  uint32_t vsync_index_ = 0;
+};
+
+}  // namespace sdm
+
+#endif  // __HW_EVENTS_DRM_H__
diff --git a/msm8998/sdm/libs/core/drm/hw_info_drm.cpp b/msm8998/sdm/libs/core/drm/hw_info_drm.cpp
new file mode 100644
index 0000000..4bcd791
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_info_drm.cpp
@@ -0,0 +1,471 @@
+/*
+* Copyright (c) 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 <dlfcn.h>
+#include <drm/drm_fourcc.h>
+#include <drm_lib_loader.h>
+#include <drm_master.h>
+#include <drm_res_mgr.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+#include <utils/sys.h>
+
+#include <algorithm>
+#include <fstream>
+#include <iostream>
+#include <map>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "hw_info_drm.h"
+
+#define __CLASS__ "HWInfoDRM"
+
+using drm_utils::DRMMaster;
+using drm_utils::DRMResMgr;
+using drm_utils::DRMLogger;
+using drm_utils::DRMLibLoader;
+using sde_drm::GetDRMManager;
+using sde_drm::DRMPlanesInfo;
+using sde_drm::DRMCrtcInfo;
+using sde_drm::DRMPlaneType;
+
+using std::vector;
+using std::map;
+using std::string;
+using std::fstream;
+using std::to_string;
+
+namespace sdm {
+
+class DRMLoggerImpl : public DRMLogger {
+ public:
+#define PRINTLOG(method, format, buf)        \
+  va_list list;                              \
+  va_start(list, format);                    \
+  vsnprintf(buf, sizeof(buf), format, list); \
+  va_end(list);                              \
+  Debug::Get()->method(kTagNone, "%s", buf);
+
+  void Error(const char *format, ...) { PRINTLOG(Error, format, buf_); }
+  void Warning(const char *format, ...) { PRINTLOG(Warning, format, buf_); }
+  void Info(const char *format, ...) { PRINTLOG(Info, format, buf_); }
+  void Debug(const char *format, ...) { PRINTLOG(Debug, format, buf_); }
+
+ private:
+  char buf_[1024] = {};
+};
+
+HWResourceInfo *HWInfoDRM::hw_resource_ = nullptr;
+
+HWInfoDRM::HWInfoDRM() {
+  DRMLogger::Set(new DRMLoggerImpl());
+  default_mode_ = (DRMLibLoader::GetInstance()->IsLoaded() == false);
+  if (!default_mode_) {
+    DRMMaster *drm_master = {};
+    int dev_fd = -1;
+    DRMMaster::GetInstance(&drm_master);
+    if (!drm_master) {
+      DLOGE("Failed to acquire DRMMaster instance");
+      return;
+    }
+    drm_master->GetHandle(&dev_fd);
+    DRMLibLoader::GetInstance()->FuncGetDRMManager()(dev_fd, &drm_mgr_intf_);
+  }
+}
+
+HWInfoDRM::~HWInfoDRM() {
+  delete hw_resource_;
+  hw_resource_ = nullptr;
+
+  if (drm_mgr_intf_) {
+    DRMLibLoader::GetInstance()->FuncDestroyDRMManager()();
+    drm_mgr_intf_ = nullptr;
+  }
+}
+
+DisplayError HWInfoDRM::GetDynamicBWLimits(HWResourceInfo *hw_resource) {
+  HWDynBwLimitInfo* bw_info = &hw_resource->dyn_bw_info;
+  for (int index = 0; index < kBwModeMax; index++) {
+    bw_info->total_bw_limit[index] = UINT32(hw_resource->max_bandwidth_low);
+    bw_info->pipe_bw_limit[index] = hw_resource->max_pipe_bw;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWInfoDRM::GetHWResourceInfo(HWResourceInfo *hw_resource) {
+  if (hw_resource_) {
+    *hw_resource = *hw_resource_;
+    return kErrorNone;
+  }
+
+  hw_resource->num_blending_stages = 1;
+  hw_resource->max_pipe_width = 2560;
+  hw_resource->max_cursor_size = 128;
+  hw_resource->max_scale_down = 1;
+  hw_resource->max_scale_up = 1;
+  hw_resource->has_decimation = false;
+  hw_resource->max_bandwidth_low = 9600000;
+  hw_resource->max_bandwidth_high = 9600000;
+  hw_resource->max_pipe_bw = 4500000;
+  hw_resource->max_sde_clk = 412500000;
+  hw_resource->clk_fudge_factor = FLOAT(105) / FLOAT(100);
+  hw_resource->macrotile_nv12_factor = 8;
+  hw_resource->macrotile_factor = 4;
+  hw_resource->linear_factor = 1;
+  hw_resource->scale_factor = 1;
+  hw_resource->extra_fudge_factor = 2;
+  hw_resource->amortizable_threshold = 0;
+  hw_resource->system_overhead_lines = 0;
+  hw_resource->hw_dest_scalar_info.count = 0;
+  hw_resource->hw_dest_scalar_info.max_scale_up = 0;
+  hw_resource->hw_dest_scalar_info.max_input_width = 0;
+  hw_resource->hw_dest_scalar_info.max_output_width = 0;
+  hw_resource->is_src_split = true;
+  hw_resource->perf_calc = false;
+  hw_resource->has_dyn_bw_support = false;
+  hw_resource->has_qseed3 = false;
+  hw_resource->has_concurrent_writeback = false;
+
+  // TODO(user): Deprecate
+  hw_resource->hw_version = kHWMdssVersion5;
+  hw_resource->hw_revision = 0;
+  hw_resource->max_mixer_width = 0;
+  hw_resource->writeback_index = 0;
+  hw_resource->has_bwc = false;
+  hw_resource->has_ubwc = true;
+  hw_resource->has_macrotile = true;
+  hw_resource->separate_rotator = true;
+  hw_resource->has_non_scalar_rgb = false;
+
+  GetSystemInfo(hw_resource);
+  GetHWPlanesInfo(hw_resource);
+  GetWBInfo(hw_resource);
+
+  // Disable destination scalar count to 0 if extension library is not present
+  DynLib extension_lib;
+  if (!extension_lib.Open("libsdmextension.so")) {
+    hw_resource->hw_dest_scalar_info.count = 0;
+  }
+
+  DLOGI("Max plane width = %d", hw_resource->max_pipe_width);
+  DLOGI("Max cursor width = %d", hw_resource->max_cursor_size);
+  DLOGI("Max plane upscale = %d", hw_resource->max_scale_up);
+  DLOGI("Max plane downscale = %d", hw_resource->max_scale_down);
+  DLOGI("Has Decimation = %d", hw_resource->has_decimation);
+  DLOGI("Max Blending Stages = %d", hw_resource->num_blending_stages);
+  DLOGI("Has Source Split = %d", hw_resource->is_src_split);
+  DLOGI("Has QSEED3 = %d", hw_resource->has_qseed3);
+  DLOGI("Has UBWC = %d", hw_resource->has_ubwc);
+  DLOGI("Has Concurrent Writeback = %d", hw_resource->has_concurrent_writeback);
+  DLOGI("Max Low Bw = %" PRIu64 "", hw_resource->max_bandwidth_low);
+  DLOGI("Max High Bw = % " PRIu64 "", hw_resource->max_bandwidth_high);
+  DLOGI("Max Pipe Bw = %" PRIu64 " KBps", hw_resource->max_pipe_bw);
+  DLOGI("MaxSDEClock = % " PRIu64 " Hz", hw_resource->max_sde_clk);
+  DLOGI("Clock Fudge Factor = %f", hw_resource->clk_fudge_factor);
+  DLOGI("Prefill factors:");
+  DLOGI("\tTiled_NV12 = %d", hw_resource->macrotile_nv12_factor);
+  DLOGI("\tTiled = %d", hw_resource->macrotile_factor);
+  DLOGI("\tLinear = %d", hw_resource->linear_factor);
+  DLOGI("\tScale = %d", hw_resource->scale_factor);
+  DLOGI("\tFudge_factor = %d", hw_resource->extra_fudge_factor);
+
+  if (hw_resource->separate_rotator || hw_resource->num_dma_pipe) {
+    GetHWRotatorInfo(hw_resource);
+  }
+
+  if (hw_resource->has_dyn_bw_support) {
+    DisplayError ret = GetDynamicBWLimits(hw_resource);
+    if (ret != kErrorNone) {
+      DLOGE("Failed to read dynamic band width info");
+      return ret;
+    }
+
+    DLOGI("Has Support for multiple bw limits shown below");
+    for (int index = 0; index < kBwModeMax; index++) {
+      DLOGI("Mode-index=%d  total_bw_limit=%d and pipe_bw_limit=%d", index,
+            hw_resource->dyn_bw_info.total_bw_limit[index],
+            hw_resource->dyn_bw_info.pipe_bw_limit[index]);
+    }
+  }
+
+  if (!hw_resource_) {
+    hw_resource_ = new HWResourceInfo();
+    *hw_resource_ = *hw_resource;
+  }
+
+  return kErrorNone;
+}
+
+void HWInfoDRM::GetSystemInfo(HWResourceInfo *hw_resource) {
+  DRMCrtcInfo info;
+  drm_mgr_intf_->GetCrtcInfo(0 /* system_info */, &info);
+  hw_resource->is_src_split = info.has_src_split;
+  hw_resource->has_qseed3 = (info.qseed_version == sde_drm::QSEEDVersion::V3);
+  hw_resource->num_blending_stages = info.max_blend_stages;
+}
+
+void HWInfoDRM::GetHWPlanesInfo(HWResourceInfo *hw_resource) {
+  DRMPlanesInfo info;
+  drm_mgr_intf_->GetPlanesInfo(&info);
+  for (auto &pipe_obj : info.planes) {
+    HWPipeCaps pipe_caps;
+    string name = {};
+    switch (pipe_obj.second) {
+      case DRMPlaneType::RGB:
+        pipe_caps.type = kPipeTypeRGB;
+        hw_resource->num_rgb_pipe++;
+        name = "RGB";
+        break;
+      case DRMPlaneType::VIG:
+        pipe_caps.type = kPipeTypeVIG;
+        hw_resource->num_vig_pipe++;
+        name = "VIG";
+        break;
+      case DRMPlaneType::DMA:
+        pipe_caps.type = kPipeTypeDMA;
+        hw_resource->num_dma_pipe++;
+        name = "DMA";
+        break;
+      case DRMPlaneType::CURSOR:
+        pipe_caps.type = kPipeTypeCursor;
+        hw_resource->num_cursor_pipe++;
+        name = "CURSOR";
+        break;
+      default:
+        break;
+    }
+    pipe_caps.id = pipe_obj.first;
+    pipe_caps.max_rects = 1;
+    DLOGI("%s Pipe : Id %d", name.c_str(), pipe_obj.first);
+    hw_resource->hw_pipes.push_back(std::move(pipe_caps));
+  }
+
+  for (auto &pipe_type : info.types) {
+    vector<LayerBufferFormat> supported_sdm_formats = {};
+    for (auto &fmts : pipe_type.second.formats_supported) {
+      GetSDMFormat(fmts.first, fmts.second, &supported_sdm_formats);
+    }
+
+    HWSubBlockType sub_blk_type = kHWSubBlockMax;
+    switch (pipe_type.first) {
+      case DRMPlaneType::RGB:
+        sub_blk_type = kHWRGBPipe;
+        // These properties are per plane but modeled in SDM as system-wide.
+        hw_resource->max_pipe_width = pipe_type.second.max_linewidth;
+        hw_resource->max_scale_down = pipe_type.second.max_downscale;
+        hw_resource->max_scale_up = pipe_type.second.max_upscale;
+        hw_resource->has_decimation =
+            pipe_type.second.max_horizontal_deci > 1 && pipe_type.second.max_vertical_deci > 1;
+        break;
+      case DRMPlaneType::VIG:
+        sub_blk_type = kHWVIGPipe;
+        // These properties are per plane but modeled in SDM as system-wide.
+        hw_resource->max_pipe_width = pipe_type.second.max_linewidth;
+        hw_resource->max_scale_down = pipe_type.second.max_downscale;
+        hw_resource->max_scale_up = pipe_type.second.max_upscale;
+        hw_resource->has_decimation =
+            pipe_type.second.max_horizontal_deci > 1 && pipe_type.second.max_vertical_deci > 1;
+        break;
+      case DRMPlaneType::DMA:
+        sub_blk_type = kHWDMAPipe;
+        break;
+      case DRMPlaneType::CURSOR:
+        sub_blk_type = kHWCursorPipe;
+        hw_resource->max_cursor_size = pipe_type.second.max_linewidth;
+        break;
+      default:
+        break;
+    }
+
+    if (sub_blk_type != kHWSubBlockMax) {
+      hw_resource->supported_formats_map.erase(sub_blk_type);
+      hw_resource->supported_formats_map.insert(make_pair(sub_blk_type, supported_sdm_formats));
+    }
+  }
+}
+
+void HWInfoDRM::GetWBInfo(HWResourceInfo *hw_resource) {
+  HWSubBlockType sub_blk_type = kHWWBIntfOutput;
+  vector<LayerBufferFormat> supported_sdm_formats = {};
+  sde_drm::DRMDisplayToken token;
+
+  // Fake register
+  if (drm_mgr_intf_->RegisterDisplay(sde_drm::DRMDisplayType::VIRTUAL, &token)) {
+    return;
+  }
+
+  sde_drm::DRMConnectorInfo connector_info;
+  drm_mgr_intf_->GetConnectorInfo(token.conn_id, &connector_info);
+  for (auto &fmts : connector_info.formats_supported) {
+    GetSDMFormat(fmts.first, fmts.second, &supported_sdm_formats);
+  }
+
+  hw_resource->supported_formats_map.erase(sub_blk_type);
+  hw_resource->supported_formats_map.insert(make_pair(sub_blk_type, supported_sdm_formats));
+
+  drm_mgr_intf_->UnregisterDisplay(token);
+}
+
+DisplayError HWInfoDRM::GetHWRotatorInfo(HWResourceInfo *hw_resource) {
+  const uint32_t kMaxV4L2Nodes = 64;
+  bool found = false;
+
+  for (uint32_t i = 0; (i < kMaxV4L2Nodes) && (false == found); i++) {
+    string path = "/sys/class/video4linux/video" + to_string(i) + "/name";
+    Sys::fstream fs(path, fstream::in);
+    if (!fs.is_open()) {
+      continue;
+    }
+
+    string line;
+    if (Sys::getline_(fs, line) && (!strncmp(line.c_str(), "sde_rotator", strlen("sde_rotator")))) {
+      hw_resource->hw_rot_info.device_path = string("/dev/video" + to_string(i));
+      hw_resource->hw_rot_info.num_rotator++;
+      hw_resource->hw_rot_info.type = HWRotatorInfo::ROT_TYPE_V4L2;
+      hw_resource->hw_rot_info.has_downscale = true;
+      // We support only 1 rotator
+      found = true;
+    }
+  }
+
+  DLOGI("V4L2 Rotator: Count = %d, Downscale = %d", hw_resource->hw_rot_info.num_rotator,
+        hw_resource->hw_rot_info.has_downscale);
+
+  return kErrorNone;
+}
+
+void HWInfoDRM::GetSDMFormat(uint32_t drm_format, uint64_t drm_format_modifier,
+                             vector<LayerBufferFormat> *sdm_formats) {
+  vector<LayerBufferFormat> &fmts(*sdm_formats);
+  switch (drm_format) {
+    case DRM_FORMAT_ARGB8888:
+      fmts.push_back(kFormatARGB8888);
+      break;
+    case DRM_FORMAT_RGBA8888:
+      fmts.push_back(drm_format_modifier ? kFormatRGBA8888Ubwc : kFormatRGBA8888);
+      break;
+    case DRM_FORMAT_BGRA8888:
+      fmts.push_back(kFormatBGRA8888);
+      break;
+    case DRM_FORMAT_XRGB8888:
+      fmts.push_back(kFormatXRGB8888);
+      break;
+    case DRM_FORMAT_RGBX8888:
+      fmts.push_back(drm_format_modifier ? kFormatRGBX8888Ubwc : kFormatRGBX8888);
+      break;
+    case DRM_FORMAT_BGRX8888:
+      fmts.push_back(kFormatBGRX8888);
+      break;
+    case DRM_FORMAT_RGBA5551:
+      fmts.push_back(kFormatRGBA5551);
+      break;
+    case DRM_FORMAT_RGBA4444:
+      fmts.push_back(kFormatRGBA4444);
+      break;
+    case DRM_FORMAT_RGB888:
+      fmts.push_back(kFormatRGB888);
+      break;
+    case DRM_FORMAT_BGR888:
+      fmts.push_back(kFormatBGR888);
+      break;
+    case DRM_FORMAT_RGB565:
+      fmts.push_back(drm_format_modifier ? kFormatBGR565Ubwc : kFormatBGR565);
+      break;
+    case DRM_FORMAT_BGR565:
+      fmts.push_back(kFormatBGR565);
+      break;
+    case DRM_FORMAT_RGBA1010102:
+      fmts.push_back(drm_format_modifier ? kFormatRGBA1010102Ubwc : kFormatRGBA1010102);
+      break;
+    case DRM_FORMAT_ARGB2101010:
+      fmts.push_back(kFormatARGB2101010);
+      break;
+    case DRM_FORMAT_RGBX1010102:
+      fmts.push_back(drm_format_modifier ? kFormatRGBX1010102Ubwc : kFormatRGBX1010102);
+      break;
+    case DRM_FORMAT_XRGB2101010:
+      fmts.push_back(kFormatXRGB2101010);
+      break;
+    case DRM_FORMAT_BGRA1010102:
+      fmts.push_back(kFormatBGRA1010102);
+      break;
+    case DRM_FORMAT_ABGR2101010:
+      fmts.push_back(kFormatABGR2101010);
+      break;
+    case DRM_FORMAT_BGRX1010102:
+      fmts.push_back(kFormatBGRX1010102);
+      break;
+    case DRM_FORMAT_XBGR2101010:
+      fmts.push_back(kFormatXBGR2101010);
+      break;
+    /* case DRM_FORMAT_P010:
+         fmts.push_back(drm_format_modifier == (DRM_FORMAT_MOD_QCOM_COMPRESSED |
+       DRM_FORMAT_MOD_QCOM_TIGHT) ?
+         kFormatYCbCr420TP10Ubwc : kFormatYCbCr420P010; */
+    case DRM_FORMAT_YVU420:
+      fmts.push_back(kFormatYCrCb420PlanarStride16);
+      break;
+    case DRM_FORMAT_NV12:
+      if (drm_format_modifier) {
+        fmts.push_back(kFormatYCbCr420SPVenusUbwc);
+      } else {
+        fmts.push_back(kFormatYCbCr420SemiPlanarVenus);
+        fmts.push_back(kFormatYCbCr420SemiPlanar);
+      }
+      break;
+    case DRM_FORMAT_NV21:
+      fmts.push_back(kFormatYCrCb420SemiPlanarVenus);
+      fmts.push_back(kFormatYCrCb420SemiPlanar);
+      break;
+    case DRM_FORMAT_NV16:
+      fmts.push_back(kFormatYCbCr422H2V1SemiPlanar);
+      break;
+    default:
+      break;
+  }
+}
+
+DisplayError HWInfoDRM::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
+  hw_disp_info->type = kPrimary;
+  hw_disp_info->is_connected = true;
+
+  return kErrorNone;
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/drm/hw_info_drm.h b/msm8998/sdm/libs/core/drm/hw_info_drm.h
new file mode 100644
index 0000000..9c9a0e0
--- /dev/null
+++ b/msm8998/sdm/libs/core/drm/hw_info_drm.h
@@ -0,0 +1,71 @@
+/*
+* Copyright (c) 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.
+*/
+
+#ifndef __HW_INFO_DRM_H__
+#define __HW_INFO_DRM_H__
+
+#include <core/core_interface.h>
+#include <core/sdm_types.h>
+#include <drm_interface.h>
+#include <private/hw_info_types.h>
+#include <bitset>
+#include <vector>
+
+#include "hw_info_interface.h"
+
+namespace sdm {
+
+class HWInfoDRM: public HWInfoInterface {
+ public:
+  HWInfoDRM();
+  virtual ~HWInfoDRM();
+  virtual DisplayError GetHWResourceInfo(HWResourceInfo *hw_resource);
+  virtual DisplayError GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info);
+
+ private:
+  DisplayError GetHWRotatorInfo(HWResourceInfo *hw_resource);
+  void GetSystemInfo(HWResourceInfo *hw_resource);
+  void GetHWPlanesInfo(HWResourceInfo *hw_resource);
+  void GetWBInfo(HWResourceInfo *hw_resource);
+  DisplayError GetDynamicBWLimits(HWResourceInfo *hw_resource);
+  void GetSDMFormat(uint32_t drm_format, uint64_t drm_format_modifier,
+                    std::vector<LayerBufferFormat> *sdm_formats);
+
+  sde_drm::DRMManagerInterface *drm_mgr_intf_ = {};
+  bool default_mode_ = false;
+
+  // TODO(user): Read Mdss version from the driver
+  static const int kHWMdssVersion5 = 500;  // MDSS_V5
+  static const int kMaxStringLength = 1024;
+  static HWResourceInfo *hw_resource_;
+};
+
+}  // namespace sdm
+
+#endif  // __HW_INFO_DRM_H__
diff --git a/msm8998/sdm/libs/core/fb/hw_device.cpp b/msm8998/sdm/libs/core/fb/hw_device.cpp
index 6e07853..10ff6fb 100644
--- a/msm8998/sdm/libs/core/fb/hw_device.cpp
+++ b/msm8998/sdm/libs/core/fb/hw_device.cpp
@@ -27,6 +27,8 @@
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#define __STDC_FORMAT_MACROS
+
 #include <stdio.h>
 #include <ctype.h>
 #include <math.h>
@@ -59,46 +61,6 @@
 
 namespace sdm {
 
-DisplayError HWInterface::Create(DisplayType type, HWInfoInterface *hw_info_intf,
-                                        BufferSyncHandler *buffer_sync_handler,
-                                        HWInterface **intf) {
-  DisplayError error = kErrorNone;
-  HWDevice *hw = nullptr;
-
-  switch (type) {
-    case kPrimary:
-      hw = new HWPrimary(buffer_sync_handler, hw_info_intf);
-      break;
-    case kHDMI:
-      hw = new HWHDMI(buffer_sync_handler, hw_info_intf);
-      break;
-    case kVirtual:
-      hw = new HWVirtual(buffer_sync_handler, hw_info_intf);
-      break;
-    default:
-      DLOGE("Undefined display type");
-      return kErrorUndefined;
-  }
-
-  error = hw->Init();
-  if (error != kErrorNone) {
-    delete hw;
-    DLOGE("Init on HW Intf type %d failed", type);
-    return error;
-  }
-  *intf = hw;
-
-  return error;
-}
-
-DisplayError HWInterface::Destroy(HWInterface *intf) {
-  HWDevice *hw = static_cast<HWDevice *>(intf);
-  hw->Deinit();
-  delete hw;
-
-  return kErrorNone;
-}
-
 HWDevice::HWDevice(BufferSyncHandler *buffer_sync_handler)
   : fb_node_index_(-1), fb_path_("/sys/devices/virtual/graphics/fb"),
     buffer_sync_handler_(buffer_sync_handler), synchronous_commit_(false) {
diff --git a/msm8998/sdm/libs/core/fb/hw_events.cpp b/msm8998/sdm/libs/core/fb/hw_events.cpp
index 4015796..ea8034f 100644
--- a/msm8998/sdm/libs/core/fb/hw_events.cpp
+++ b/msm8998/sdm/libs/core/fb/hw_events.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved.
+* 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
@@ -49,55 +49,28 @@
 
 namespace sdm {
 
-DisplayError HWEventsInterface::Create(int fb_num, HWEventHandler *event_handler,
-                                       std::vector<const char *> *event_list,
-                                       HWEventsInterface **intf) {
-  DisplayError error = kErrorNone;
-  HWEvents *hw_events = NULL;
-
-  hw_events = new HWEvents();
-  error = hw_events->Init(fb_num, event_handler, event_list);
-  if (error != kErrorNone) {
-    delete hw_events;
-  } else {
-    *intf = hw_events;
-  }
-
-  return error;
-}
-
-DisplayError HWEventsInterface::Destroy(HWEventsInterface *intf) {
-  HWEvents *hw_events = static_cast<HWEvents *>(intf);
-
-  if (hw_events) {
-    hw_events->Deinit();
-    delete hw_events;
-  }
-
-  return kErrorNone;
-}
-
 pollfd HWEvents::InitializePollFd(HWEventData *event_data) {
   char node_path[kMaxStringLength] = {0};
   char data[kMaxStringLength] = {0};
   pollfd poll_fd = {0};
   poll_fd.fd = -1;
 
-  if (!strncmp(event_data->event_name, "thread_exit", strlen("thread_exit"))) {
+  if (event_data->event_type == HWEvent::EXIT) {
     // Create an eventfd to be used to unblock the poll system call when
     // a thread is exiting.
     poll_fd.fd = Sys::eventfd_(0, 0);
     poll_fd.events |= POLLIN;
     exit_fd_ = poll_fd.fd;
   } else {
-    snprintf(node_path, sizeof(node_path), "%s%d/%s", fb_path_, fb_num_, event_data->event_name);
+    snprintf(node_path, sizeof(node_path), "%s%d/%s", fb_path_, fb_num_,
+             map_event_to_node_[event_data->event_type]);
     poll_fd.fd = Sys::open_(node_path, O_RDONLY);
     poll_fd.events |= POLLPRI | POLLERR;
   }
 
   if (poll_fd.fd < 0) {
-    DLOGW("open failed for display=%d event=%s, error=%s", fb_num_, event_data->event_name,
-          strerror(errno));
+    DLOGW("open failed for display=%d event=%s, error=%s", fb_num_,
+          map_event_to_node_[event_data->event_type], strerror(errno));
     return poll_fd;
   }
 
@@ -107,49 +80,58 @@
   return poll_fd;
 }
 
-DisplayError HWEvents::SetEventParser(const char *event_name, HWEventData *event_data) {
+DisplayError HWEvents::SetEventParser(HWEvent event_type, HWEventData *event_data) {
   DisplayError error = kErrorNone;
-
-  if (!strncmp(event_name, "vsync_event", strlen("vsync_event"))) {
-    event_data->event_parser = &HWEvents::HandleVSync;
-  } else if (!strncmp(event_name, "show_blank_event", strlen("show_blank_event"))) {
-    event_data->event_parser = &HWEvents::HandleBlank;
-  } else if (!strncmp(event_name, "idle_notify", strlen("idle_notify"))) {
-    event_data->event_parser = &HWEvents::HandleIdleTimeout;
-  } else if (!strncmp(event_name, "msm_fb_thermal_level", strlen("msm_fb_thermal_level"))) {
-    event_data->event_parser = &HWEvents::HandleThermal;
-  } else if (!strncmp(event_name, "cec/rd_msg", strlen("cec/rd_msg"))) {
-    event_data->event_parser = &HWEvents::HandleCECMessage;
-  } else if (!strncmp(event_name, "thread_exit", strlen("thread_exit"))) {
-    event_data->event_parser = &HWEvents::HandleThreadExit;
-  } else {
-    error = kErrorParameters;
+  switch (event_type) {
+    case HWEvent::VSYNC:
+      event_data->event_parser = &HWEvents::HandleVSync;
+      break;
+    case HWEvent::IDLE_NOTIFY:
+      event_data->event_parser = &HWEvents::HandleIdleTimeout;
+      break;
+    case HWEvent::CEC_READ_MESSAGE:
+      event_data->event_parser = &HWEvents::HandleCECMessage;
+      break;
+    case HWEvent::EXIT:
+      event_data->event_parser = &HWEvents::HandleThreadExit;
+      break;
+    case HWEvent::SHOW_BLANK_EVENT:
+      event_data->event_parser = &HWEvents::HandleBlank;
+      break;
+    case HWEvent::THERMAL_LEVEL:
+      event_data->event_parser = &HWEvents::HandleThermal;
+      break;
+    default:
+      error = kErrorParameters;
+      break;
   }
 
   return error;
 }
 
 void HWEvents::PopulateHWEventData() {
-  for (uint32_t i = 0; i < event_list_->size(); i++) {
-    const char *event_name = event_list_->at(i);
+  for (uint32_t i = 0; i < event_list_.size(); i++) {
     HWEventData event_data;
-    event_data.event_name = event_name;
-    SetEventParser(event_name, &event_data);
+    event_data.event_type = event_list_[i];
+    SetEventParser(event_list_[i], &event_data);
     poll_fds_[i] = InitializePollFd(&event_data);
     event_data_list_.push_back(event_data);
   }
 }
 
 DisplayError HWEvents::Init(int fb_num, HWEventHandler *event_handler,
-                            vector<const char *> *event_list) {
+                            const vector<HWEvent> &event_list) {
   if (!event_handler)
     return kErrorParameters;
 
   event_handler_ = event_handler;
   fb_num_ = fb_num;
   event_list_ = event_list;
-  poll_fds_.resize(event_list_->size());
+  poll_fds_.resize(event_list_.size());
   event_thread_name_ += " - " + std::to_string(fb_num_);
+  map_event_to_node_ = {{HWEvent::VSYNC, "vsync_event"}, {HWEvent::EXIT, "thread_exit"},
+    {HWEvent::IDLE_NOTIFY, "idle_notify"}, {HWEvent::SHOW_BLANK_EVENT, "show_blank_event"},
+    {HWEvent::CEC_READ_MESSAGE, "cec/rd_msg"}, {HWEvent::THERMAL_LEVEL, "msm_fb_thermal_level"}};
 
   PopulateHWEventData();
 
@@ -173,7 +155,7 @@
 
   pthread_join(event_thread_, NULL);
 
-  for (uint32_t i = 0; i < event_list_->size(); i++) {
+  for (uint32_t i = 0; i < event_list_.size(); i++) {
     Sys::close_(poll_fds_[i].fd);
     poll_fds_[i].fd = -1;
   }
@@ -196,17 +178,17 @@
   setpriority(PRIO_PROCESS, 0, kThreadPriorityUrgent);
 
   while (!exit_threads_) {
-    int error = Sys::poll_(poll_fds_.data(), UINT32(event_list_->size()), -1);
+    int error = Sys::poll_(poll_fds_.data(), UINT32(event_list_.size()), -1);
 
     if (error <= 0) {
       DLOGW("poll failed. error = %s", strerror(errno));
       continue;
     }
 
-    for (uint32_t event = 0; event < event_list_->size(); event++) {
+    for (uint32_t event = 0; event < event_list_.size(); event++) {
       pollfd &poll_fd = poll_fds_[event];
 
-      if (!strncmp(event_list_->at(event), "thread_exit", strlen("thread_exit"))) {
+      if (event_list_.at(event) == HWEvent::EXIT) {
         if ((poll_fd.revents & POLLIN) && (Sys::read_(poll_fd.fd, data, kMaxStringLength) > 0)) {
           (this->*(event_data_list_[event]).event_parser)(data);
         }
@@ -253,4 +235,3 @@
 }
 
 }  // namespace sdm
-
diff --git a/msm8998/sdm/libs/core/fb/hw_events.h b/msm8998/sdm/libs/core/fb/hw_events.h
index bcc5ef6..e024924 100644
--- a/msm8998/sdm/libs/core/fb/hw_events.h
+++ b/msm8998/sdm/libs/core/fb/hw_events.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved.
+* 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:
@@ -37,12 +37,13 @@
 namespace sdm {
 
 using std::vector;
+using std::map;
 
 class HWEvents : public HWEventsInterface {
  public:
-  DisplayError Init(int fb_num, HWEventHandler *event_handler,
-                    vector<const char *> *event_list);
-  DisplayError Deinit();
+  virtual DisplayError Init(int fb_num, HWEventHandler *event_handler,
+                            const vector<HWEvent> &event_list);
+  virtual DisplayError Deinit();
 
  private:
   static const int kMaxStringLength = 1024;
@@ -50,8 +51,8 @@
   typedef void (HWEvents::*EventParser)(char *);
 
   struct HWEventData {
-    const char* event_name = NULL;
-    EventParser event_parser = NULL;
+    HWEvent event_type {};
+    EventParser event_parser {};
   };
 
   static void* DisplayEventThread(void *context);
@@ -63,14 +64,15 @@
   void HandleCECMessage(char *data);
   void HandleThreadExit(char *data) { }
   void PopulateHWEventData();
-  DisplayError SetEventParser(const char *event_name, HWEventData *event_data);
+  DisplayError SetEventParser(HWEvent event_type, HWEventData *event_data);
   pollfd InitializePollFd(HWEventData *event_data);
 
-  HWEventHandler *event_handler_ = NULL;
-  vector<const char *> *event_list_ = NULL;
+  HWEventHandler *event_handler_ = {};
+  vector<HWEvent> event_list_ = {};
   vector<HWEventData> event_data_list_ = {};
-  vector<pollfd> poll_fds_;
-  pthread_t event_thread_;
+  vector<pollfd> poll_fds_ = {};
+  map<HWEvent, const char *> map_event_to_node_ = {};
+  pthread_t event_thread_ = {};
   std::string event_thread_name_ = "SDM_EventThread";
   bool exit_threads_ = false;
   const char* fb_path_ = "/sys/devices/virtual/graphics/fb";
diff --git a/msm8998/sdm/libs/core/fb/hw_info.cpp b/msm8998/sdm/libs/core/fb/hw_info.cpp
index 2931638..d898cd2 100644
--- a/msm8998/sdm/libs/core/fb/hw_info.cpp
+++ b/msm8998/sdm/libs/core/fb/hw_info.cpp
@@ -86,27 +86,6 @@
   return 0;
 }
 
-DisplayError HWInfoInterface::Create(HWInfoInterface **intf) {
-  DisplayError error = kErrorNone;
-  HWInfo *hw_info = NULL;
-
-  hw_info = new HWInfo();
-  if (!hw_info) {
-    error = kErrorMemory;
-  } else {
-    *intf = hw_info;
-  }
-
-  return error;
-}
-
-DisplayError HWInfoInterface::Destroy(HWInfoInterface *intf) {
-  HWInfo *hw_info = static_cast<HWInfo *>(intf);
-  delete hw_info;
-
-  return kErrorNone;
-}
-
 DisplayError HWInfo::GetDynamicBWLimits(HWResourceInfo *hw_resource) {
   Sys::fstream fs(kBWModeBitmap, fstream::in);
   if (!fs.is_open()) {
diff --git a/msm8998/sdm/libs/core/fb/hw_primary.cpp b/msm8998/sdm/libs/core/fb/hw_primary.cpp
index a381d7f..3fdc8b2 100644
--- a/msm8998/sdm/libs/core/fb/hw_primary.cpp
+++ b/msm8998/sdm/libs/core/fb/hw_primary.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved.
+* 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
@@ -250,8 +250,8 @@
       (FLOAT(var_screeninfo.yres) * 25.4f) / FLOAT(var_screeninfo.height);
   display_attributes_.fps = meta_data.data.panel_frame_rate;
   display_attributes_.vsync_period_ns = UINT32(1000000000L / display_attributes_.fps);
-  display_attributes_.is_device_split = (hw_panel_info_.split_info.left_split ||
-      (var_screeninfo.xres > hw_resource_.max_mixer_width)) ? true : false;
+  display_attributes_.is_device_split = (hw_panel_info_.split_info.right_split ||
+      (var_screeninfo.xres > hw_resource_.max_mixer_width));
   display_attributes_.h_total += (display_attributes_.is_device_split ||
     hw_panel_info_.ping_pong_split)? h_blanking : 0;
 
diff --git a/msm8998/sdm/libs/core/hw_events_interface.cpp b/msm8998/sdm/libs/core/hw_events_interface.cpp
new file mode 100644
index 0000000..ed62b86
--- /dev/null
+++ b/msm8998/sdm/libs/core/hw_events_interface.cpp
@@ -0,0 +1,75 @@
+/*
+* Copyright (c) 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 <utils/utils.h>
+#include <vector>
+
+#include "hw_events_interface.h"
+#include "fb/hw_events.h"
+#ifdef COMPILE_DRM
+#include "drm/hw_events_drm.h"
+#endif
+
+#define __CLASS__ "HWEventsInterface"
+
+namespace sdm {
+
+DisplayError HWEventsInterface::Create(int display_type, HWEventHandler *event_handler,
+                                       const std::vector<HWEvent> &event_list,
+                                       HWEventsInterface **intf) {
+  DisplayError error = kErrorNone;
+  HWEventsInterface *hw_events = nullptr;
+  if (GetDriverType() == DriverType::FB) {
+    hw_events = new HWEvents();
+  } else {
+#ifdef COMPILE_DRM
+    hw_events = new HWEventsDRM();
+#endif
+  }
+
+  error = hw_events->Init(display_type, event_handler, event_list);
+  if (error != kErrorNone) {
+    delete hw_events;
+  } else {
+    *intf = hw_events;
+  }
+
+  return error;
+}
+
+DisplayError HWEventsInterface::Destroy(HWEventsInterface *intf) {
+  if (intf) {
+    intf->Deinit();
+    delete intf;
+  }
+
+  return kErrorNone;
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/hw_events_interface.h b/msm8998/sdm/libs/core/hw_events_interface.h
index a133a3a..bcb7d43 100644
--- a/msm8998/sdm/libs/core/hw_events_interface.h
+++ b/msm8998/sdm/libs/core/hw_events_interface.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2016, The Linux Foundation. All rights reserved.
+* Copyright (c) 2016-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:
@@ -32,10 +32,25 @@
 
 namespace sdm {
 
+class HWEventHandler;
+
+enum HWEvent {
+  VSYNC = 0,
+  EXIT,
+  IDLE_NOTIFY,
+  CEC_READ_MESSAGE,
+  SHOW_BLANK_EVENT,
+  THERMAL_LEVEL,
+};
+
 class HWEventsInterface {
  public:
-  static DisplayError Create(int fb_num, HWEventHandler *event_handler,
-                             std::vector<const char *> *event_list, HWEventsInterface **intf);
+  virtual DisplayError Init(int display_type, HWEventHandler *event_handler,
+                            const std::vector<HWEvent> &event_list) = 0;
+  virtual DisplayError Deinit() = 0;
+
+  static DisplayError Create(int display_type, HWEventHandler *event_handler,
+                             const std::vector<HWEvent> &event_list, HWEventsInterface **intf);
   static DisplayError Destroy(HWEventsInterface *intf);
 
  protected:
diff --git a/msm8998/sdm/libs/core/hw_info_interface.cpp b/msm8998/sdm/libs/core/hw_info_interface.cpp
new file mode 100644
index 0000000..1773fe5
--- /dev/null
+++ b/msm8998/sdm/libs/core/hw_info_interface.cpp
@@ -0,0 +1,62 @@
+/*
+* Copyright (c) 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 <utils/utils.h>
+
+#include "hw_info_interface.h"
+#include "fb/hw_info.h"
+#ifdef COMPILE_DRM
+#include "drm/hw_info_drm.h"
+#endif
+
+#define __CLASS__ "HWInfoInterface"
+
+namespace sdm {
+
+DisplayError HWInfoInterface::Create(HWInfoInterface **intf) {
+  if (GetDriverType() == DriverType::FB) {
+    *intf = new HWInfo();
+  } else {
+#ifdef COMPILE_DRM
+    *intf = new HWInfoDRM();
+#endif
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWInfoInterface::Destroy(HWInfoInterface *intf) {
+  if (intf) {
+    delete intf;
+  }
+
+  return kErrorNone;
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/hw_interface.cpp b/msm8998/sdm/libs/core/hw_interface.cpp
new file mode 100644
index 0000000..b328831
--- /dev/null
+++ b/msm8998/sdm/libs/core/hw_interface.cpp
@@ -0,0 +1,102 @@
+/*
+* Copyright (c) 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 <utils/debug.h>
+#include <utils/utils.h>
+
+#include "hw_interface.h"
+#include "fb/hw_device.h"
+#include "fb/hw_primary.h"
+#include "fb/hw_hdmi.h"
+#include "fb/hw_virtual.h"
+#ifdef COMPILE_DRM
+#include "drm/hw_device_drm.h"
+#endif
+
+#define __CLASS__ "HWInterface"
+
+namespace sdm {
+
+DisplayError HWInterface::Create(DisplayType type, HWInfoInterface *hw_info_intf,
+                                 BufferSyncHandler *buffer_sync_handler,
+                                 HWInterface **intf) {
+  DisplayError error = kErrorNone;
+  HWInterface *hw = nullptr;
+  DriverType driver_type = GetDriverType();
+
+  switch (type) {
+    case kPrimary:
+      if (driver_type == DriverType::FB) {
+        hw = new HWPrimary(buffer_sync_handler, hw_info_intf);
+      } else {
+#ifdef COMPILE_DRM
+        hw = new HWDeviceDRM(buffer_sync_handler, hw_info_intf);
+#endif
+      }
+      break;
+    case kHDMI:
+      if (driver_type == DriverType::FB) {
+        hw = new HWHDMI(buffer_sync_handler, hw_info_intf);
+      } else {
+        return kErrorNotSupported;
+      }
+      break;
+    case kVirtual:
+      if (driver_type == DriverType::FB) {
+        hw = new HWVirtual(buffer_sync_handler, hw_info_intf);
+      } else {
+        return kErrorNotSupported;
+      }
+      break;
+    default:
+      DLOGE("Undefined display type");
+      return kErrorUndefined;
+  }
+
+  error = hw->Init();
+  if (error != kErrorNone) {
+    delete hw;
+    DLOGE("Init on HW Intf type %d failed", type);
+    return error;
+  }
+  *intf = hw;
+
+  return error;
+}
+
+DisplayError HWInterface::Destroy(HWInterface *intf) {
+  if (intf) {
+    intf->Deinit();
+    delete intf;
+  }
+
+  return kErrorNone;
+}
+
+}  // namespace sdm
diff --git a/msm8998/sdm/libs/core/hw_interface.h b/msm8998/sdm/libs/core/hw_interface.h
index da3ac4d..2e082ae 100644
--- a/msm8998/sdm/libs/core/hw_interface.h
+++ b/msm8998/sdm/libs/core/hw_interface.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.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
@@ -70,6 +70,8 @@
                              BufferSyncHandler *buffer_sync_handler, HWInterface **intf);
   static DisplayError Destroy(HWInterface *intf);
 
+  virtual DisplayError Init() = 0;
+  virtual DisplayError Deinit() = 0;
   virtual DisplayError GetActiveConfig(uint32_t *active_config) = 0;
   virtual DisplayError GetNumDisplayAttributes(uint32_t *count) = 0;
   virtual DisplayError GetDisplayAttributes(uint32_t index,
diff --git a/msm8998/sdm/libs/core/strategy.cpp b/msm8998/sdm/libs/core/strategy.cpp
index 97a6cc3..a34c40c 100644
--- a/msm8998/sdm/libs/core/strategy.cpp
+++ b/msm8998/sdm/libs/core/strategy.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 met:
@@ -57,7 +57,7 @@
     }
 
     error = extension_intf_->CreatePartialUpdate(display_type_, hw_resource_info_, hw_panel_info_,
-                                                 mixer_attributes_, display_attributes_,
+                                                 mixer_attributes_, display_attributes_, fb_config_,
                                                  &partial_update_intf_);
   }
 
@@ -177,9 +177,7 @@
   float layer_mixer_width = mixer_attributes_.width;
   float layer_mixer_height = mixer_attributes_.height;
 
-  if (!hw_resource_info_.is_src_split &&
-     ((layer_mixer_width > hw_resource_info_.max_mixer_width) ||
-     ((hw_panel_info_.is_primary_panel) && hw_panel_info_.split_info.right_split))) {
+  if (!hw_resource_info_.is_src_split && display_attributes_.is_device_split) {
     split_display = true;
   }
 
@@ -217,7 +215,7 @@
   }
 
   extension_intf_->CreatePartialUpdate(display_type_, hw_resource_info_, hw_panel_info,
-                                       mixer_attributes, display_attributes,
+                                       mixer_attributes, display_attributes, fb_config,
                                        &partial_update_intf_);
 
   error = strategy_intf_->Reconfigure(hw_panel_info, hw_resource_info_, mixer_attributes,
diff --git a/msm8998/sdm/libs/hwc/Android.mk b/msm8998/sdm/libs/hwc/Android.mk
index 2efe584..0527910 100644
--- a/msm8998/sdm/libs/hwc/Android.mk
+++ b/msm8998/sdm/libs/hwc/Android.mk
@@ -8,6 +8,7 @@
 LOCAL_PROPRIETARY_MODULE      := true
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
+LOCAL_C_INCLUDES              += frameworks/native/libs/arect/include
 
 LOCAL_CFLAGS                  := -Wno-missing-field-initializers -Wno-unused-parameter \
                                  -std=c++11 -fcolor-diagnostics\
diff --git a/msm8998/sdm/libs/hwc/hwc_display.cpp b/msm8998/sdm/libs/hwc/hwc_display.cpp
index b52c0cc..074e018 100644
--- a/msm8998/sdm/libs/hwc/hwc_display.cpp
+++ b/msm8998/sdm/libs/hwc/hwc_display.cpp
@@ -39,6 +39,7 @@
 #include <utils/debug.h>
 #include <sync/sync.h>
 #include <cutils/properties.h>
+#include <qd_utils.h>
 #include <map>
 #include <utility>
 #include <vector>
@@ -519,6 +520,7 @@
     layer_buffer.planes[0].offset = pvt_handle->offset;
     layer_buffer.planes[0].stride = UINT32(pvt_handle->width);
     layer_buffer.size = pvt_handle->size;
+    layer_buffer.fb_id = pvt_handle->fb_id;
   }
 
   // if swapinterval property is set to 0 then close and reset the acquireFd
@@ -567,7 +569,7 @@
         bool secure = (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) ||
                 (pvt_handle->flags & private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER) ||
                 (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY);
-        if (NonIntegralSourceCrop && !secure) {
+        if (NonIntegralSourceCrop && (!secure && pvt_handle->bufferType != BUFFER_TYPE_VIDEO)) {
             layer->flags.skip = true;
         }
     }
@@ -1074,7 +1076,7 @@
 
       snprintf(dump_file_name, sizeof(dump_file_name), "%s/input_layer%d_%dx%d_%s_frame%d.raw",
                dir_path, i, pvt_handle->width, pvt_handle->height,
-               GetHALPixelFormatString(pvt_handle->format), dump_frame_index_);
+               qdutils::GetHALPixelFormatString(pvt_handle->format), dump_frame_index_);
 
       FILE* fp = fopen(dump_file_name, "w+");
       if (fp) {
@@ -1130,81 +1132,6 @@
   }
 }
 
-const char *HWCDisplay::GetHALPixelFormatString(int format) {
-  switch (format) {
-  case HAL_PIXEL_FORMAT_RGBA_8888:
-    return "RGBA_8888";
-  case HAL_PIXEL_FORMAT_RGBX_8888:
-    return "RGBX_8888";
-  case HAL_PIXEL_FORMAT_RGB_888:
-    return "RGB_888";
-  case HAL_PIXEL_FORMAT_RGB_565:
-    return "RGB_565";
-  case HAL_PIXEL_FORMAT_BGR_565:
-    return "BGR_565";
-  case HAL_PIXEL_FORMAT_BGRA_8888:
-    return "BGRA_8888";
-  case HAL_PIXEL_FORMAT_RGBA_5551:
-    return "RGBA_5551";
-  case HAL_PIXEL_FORMAT_RGBA_4444:
-    return "RGBA_4444";
-  case HAL_PIXEL_FORMAT_YV12:
-    return "YV12";
-  case HAL_PIXEL_FORMAT_YCbCr_422_SP:
-    return "YCbCr_422_SP_NV16";
-  case HAL_PIXEL_FORMAT_YCrCb_420_SP:
-    return "YCrCb_420_SP_NV21";
-  case HAL_PIXEL_FORMAT_YCbCr_422_I:
-    return "YCbCr_422_I_YUY2";
-  case HAL_PIXEL_FORMAT_YCrCb_422_I:
-    return "YCrCb_422_I_YVYU";
-  case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
-    return "NV12_ENCODEABLE";
-  case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
-    return "YCbCr_420_SP_TILED_TILE_4x2";
-  case HAL_PIXEL_FORMAT_YCbCr_420_SP:
-    return "YCbCr_420_SP";
-  case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
-    return "YCrCb_420_SP_ADRENO";
-  case HAL_PIXEL_FORMAT_YCrCb_422_SP:
-    return "YCrCb_422_SP";
-  case HAL_PIXEL_FORMAT_R_8:
-    return "R_8";
-  case HAL_PIXEL_FORMAT_RG_88:
-    return "RG_88";
-  case HAL_PIXEL_FORMAT_INTERLACE:
-    return "INTERLACE";
-  case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
-    return "YCbCr_420_SP_VENUS";
-  case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
-    return "YCrCb_420_SP_VENUS";
-  case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
-    return "YCbCr_420_SP_VENUS_UBWC";
-  case HAL_PIXEL_FORMAT_RGBA_1010102:
-    return "RGBA_1010102";
-  case HAL_PIXEL_FORMAT_ARGB_2101010:
-    return "ARGB_2101010";
-  case HAL_PIXEL_FORMAT_RGBX_1010102:
-    return "RGBX_1010102";
-  case HAL_PIXEL_FORMAT_XRGB_2101010:
-    return "XRGB_2101010";
-  case HAL_PIXEL_FORMAT_BGRA_1010102:
-    return "BGRA_1010102";
-  case HAL_PIXEL_FORMAT_ABGR_2101010:
-    return "ABGR_2101010";
-  case HAL_PIXEL_FORMAT_BGRX_1010102:
-    return "BGRX_1010102";
-  case HAL_PIXEL_FORMAT_XBGR_2101010:
-    return "XBGR_2101010";
-  case HAL_PIXEL_FORMAT_YCbCr_420_P010:
-    return "YCbCr_420_P010";
-  case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
-    return "YCbCr_420_TP10_UBWC";
-  default:
-    return "Unknown_format";
-  }
-}
-
 const char *HWCDisplay::GetDisplayString() {
   switch (type_) {
   case kPrimary:
diff --git a/msm8998/sdm/libs/hwc/hwc_display.h b/msm8998/sdm/libs/hwc/hwc_display.h
index 8da8516..5652996 100644
--- a/msm8998/sdm/libs/hwc/hwc_display.h
+++ b/msm8998/sdm/libs/hwc/hwc_display.h
@@ -192,7 +192,6 @@
   int SetFormat(const int32_t &source, const int flags, LayerBufferFormat *target);
   void SetLayerS3DMode(const LayerBufferS3DFormat &source, uint32_t *target);
   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
-  const char *GetHALPixelFormatString(int format);
   const char *GetDisplayString();
   void MarkLayersForGPUBypass(hwc_display_contents_1_t *content_list);
   virtual void ApplyScanAdjustment(hwc_rect_t *display_frame);
diff --git a/msm8998/sdm/libs/hwc2/Android.mk b/msm8998/sdm/libs/hwc2/Android.mk
index 00f7819..d547a6e 100644
--- a/msm8998/sdm/libs/hwc2/Android.mk
+++ b/msm8998/sdm/libs/hwc2/Android.mk
@@ -17,23 +17,31 @@
 LOCAL_CLANG                   := true
 
 LOCAL_SHARED_LIBRARIES        := libsdmcore libqservice libbinder libhardware libhardware_legacy \
-                                 libutils libcutils libsync libmemalloc libqdutils libdl \
+                                 libutils libcutils libsync libqdutils libdl \
                                  libpowermanager libsdmutils libc++ liblog
 
+ifneq ($(TARGET_USES_GRALLOC1), true)
+    LOCAL_SHARED_LIBRARIES += libmemalloc
+endif
+
 LOCAL_SRC_FILES               := hwc_session.cpp \
                                  hwc_display.cpp \
                                  hwc_display_primary.cpp \
                                  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/msm8998/sdm/libs/hwc2/hwc_buffer_allocator.cpp b/msm8998/sdm/libs/hwc2/hwc_buffer_allocator.cpp
new file mode 100644
index 0000000..5e3cd86
--- /dev/null
+++ b/msm8998/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/msm8998/sdm/libs/hwc2/hwc_buffer_allocator.h b/msm8998/sdm/libs/hwc2/hwc_buffer_allocator.h
new file mode 100644
index 0000000..c28a94e
--- /dev/null
+++ b/msm8998/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/msm8998/sdm/libs/hwc2/hwc_display.cpp b/msm8998/sdm/libs/hwc2/hwc_display.cpp
index f0f743c..fbcf064 100644
--- a/msm8998/sdm/libs/hwc2/hwc_display.cpp
+++ b/msm8998/sdm/libs/hwc2/hwc_display.cpp
@@ -21,14 +21,13 @@
 #include <sys/stat.h>
 #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>
 #include <utils/debug.h>
 #include <utils/formats.h>
 #include <utils/rect.h>
+#include <qd_utils.h>
 
 #include <algorithm>
 #include <map>
@@ -40,6 +39,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>
@@ -237,21 +239,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_);
@@ -269,10 +264,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_) {
@@ -285,7 +279,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;
@@ -352,7 +346,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
@@ -689,10 +687,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_);
 }
 
@@ -1166,7 +1160,7 @@
 
       snprintf(dump_file_name, sizeof(dump_file_name), "%s/input_layer%d_%dx%d_%s_frame%d.raw",
                dir_path, i, pvt_handle->width, pvt_handle->height,
-               GetHALPixelFormatString(pvt_handle->format), dump_frame_index_);
+               qdutils::GetHALPixelFormatString(pvt_handle->format), dump_frame_index_);
 
       FILE *fp = fopen(dump_file_name, "w+");
       if (fp) {
@@ -1221,81 +1215,6 @@
   }
 }
 
-const char *HWCDisplay::GetHALPixelFormatString(int format) {
-  switch (format) {
-    case HAL_PIXEL_FORMAT_RGBA_8888:
-      return "RGBA_8888";
-    case HAL_PIXEL_FORMAT_RGBX_8888:
-      return "RGBX_8888";
-    case HAL_PIXEL_FORMAT_RGB_888:
-      return "RGB_888";
-    case HAL_PIXEL_FORMAT_RGB_565:
-      return "RGB_565";
-    case HAL_PIXEL_FORMAT_BGR_565:
-      return "BGR_565";
-    case HAL_PIXEL_FORMAT_BGRA_8888:
-      return "BGRA_8888";
-    case HAL_PIXEL_FORMAT_RGBA_5551:
-      return "RGBA_5551";
-    case HAL_PIXEL_FORMAT_RGBA_4444:
-      return "RGBA_4444";
-    case HAL_PIXEL_FORMAT_YV12:
-      return "YV12";
-    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
-      return "YCbCr_422_SP_NV16";
-    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
-      return "YCrCb_420_SP_NV21";
-    case HAL_PIXEL_FORMAT_YCbCr_422_I:
-      return "YCbCr_422_I_YUY2";
-    case HAL_PIXEL_FORMAT_YCrCb_422_I:
-      return "YCrCb_422_I_YVYU";
-    case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
-      return "NV12_ENCODEABLE";
-    case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
-      return "YCbCr_420_SP_TILED_TILE_4x2";
-    case HAL_PIXEL_FORMAT_YCbCr_420_SP:
-      return "YCbCr_420_SP";
-    case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
-      return "YCrCb_420_SP_ADRENO";
-    case HAL_PIXEL_FORMAT_YCrCb_422_SP:
-      return "YCrCb_422_SP";
-    case HAL_PIXEL_FORMAT_R_8:
-      return "R_8";
-    case HAL_PIXEL_FORMAT_RG_88:
-      return "RG_88";
-    case HAL_PIXEL_FORMAT_INTERLACE:
-      return "INTERLACE";
-    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
-      return "YCbCr_420_SP_VENUS";
-    case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
-      return "YCrCb_420_SP_VENUS";
-    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
-      return "YCbCr_420_SP_VENUS_UBWC";
-    case HAL_PIXEL_FORMAT_RGBA_1010102:
-      return "RGBA_1010102";
-    case HAL_PIXEL_FORMAT_ARGB_2101010:
-      return "ARGB_2101010";
-    case HAL_PIXEL_FORMAT_RGBX_1010102:
-      return "RGBX_1010102";
-    case HAL_PIXEL_FORMAT_XRGB_2101010:
-      return "XRGB_2101010";
-    case HAL_PIXEL_FORMAT_BGRA_1010102:
-      return "BGRA_1010102";
-    case HAL_PIXEL_FORMAT_ABGR_2101010:
-      return "ABGR_2101010";
-    case HAL_PIXEL_FORMAT_BGRX_1010102:
-      return "BGRX_1010102";
-    case HAL_PIXEL_FORMAT_XBGR_2101010:
-      return "XBGR_2101010";
-    case HAL_PIXEL_FORMAT_YCbCr_420_P010:
-      return "YCbCr_420_P010";
-    case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
-      return "YCbCr_420_TP10_UBWC";
-    default:
-      return "Unknown_format";
-  }
-}
-
 const char *HWCDisplay::GetDisplayString() {
   switch (type_) {
     case kPrimary:
@@ -1340,7 +1259,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;
@@ -1349,8 +1268,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/msm8998/sdm/libs/hwc2/hwc_display.h b/msm8998/sdm/libs/hwc2/hwc_display.h
index 165e2c3..d8f85bd 100644
--- a/msm8998/sdm/libs/hwc2/hwc_display.h
+++ b/msm8998/sdm/libs/hwc2/hwc_display.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
@@ -32,6 +32,7 @@
 #include <utility>
 #include <vector>
 
+#include "hwc_buffer_allocator.h"
 #include "hwc_callbacks.h"
 #include "hwc_layers.h"
 
@@ -212,7 +213,6 @@
     return kErrorNotSupported;
   }
   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
-  const char *GetHALPixelFormatString(int format);
   const char *GetDisplayString();
   void MarkLayersForGPUBypass(void);
   void MarkLayersForClientComposition(void);
@@ -230,6 +230,7 @@
 
   CoreInterface *core_intf_ = nullptr;
   HWCCallbacks *callbacks_  = nullptr;
+  HWCBufferAllocator *buffer_allocator_ = NULL;
   DisplayType type_;
   hwc2_display_t id_;
   bool needs_blit_ = false;
@@ -270,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/msm8998/sdm/libs/hwc2/hwc_display_virtual.cpp b/msm8998/sdm/libs/hwc2/hwc_display_virtual.cpp
index 1197c7b..46fec4e 100644
--- a/msm8998/sdm/libs/hwc2/hwc_display_virtual.cpp
+++ b/msm8998/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/msm8998/sdm/libs/hwc2/hwc_layers.cpp b/msm8998/sdm/libs/hwc2/hwc_layers.cpp
index 2ed21df..440f047 100644
--- a/msm8998/sdm/libs/hwc2/hwc_layers.cpp
+++ b/msm8998/sdm/libs/hwc2/hwc_layers.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
@@ -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
@@ -143,6 +151,7 @@
   layer_buffer->acquire_fence_fd = acquire_fence;
   layer_buffer->size = handle->size;
   layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
+  layer_buffer->fb_id = handle->fb_id;
 
   return HWC2::Error::None;
 }
diff --git a/msm8998/sdm/libs/hwc2/hwc_layers.h b/msm8998/sdm/libs/hwc2/hwc_layers.h
index 6a80b1b..f9cb23e 100644
--- a/msm8998/sdm/libs/hwc2/hwc_layers.h
+++ b/msm8998/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/msm8998/sdm/libs/hwc2/hwc_session.cpp b/msm8998/sdm/libs/hwc2/hwc_session.cpp
index ee4a6e7..cc780d8 100644
--- a/msm8998/sdm/libs/hwc2/hwc_session.cpp
+++ b/msm8998/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>
diff --git a/msm8998/sdm/libs/utils/Android.mk b/msm8998/sdm/libs/utils/Android.mk
index f7f30a9..215fe68 100644
--- a/msm8998/sdm/libs/utils/Android.mk
+++ b/msm8998/sdm/libs/utils/Android.mk
@@ -3,6 +3,8 @@
 include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmutils
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
 LOCAL_CFLAGS                  := -DLOG_TAG=\"SDM\" $(common_flags)
diff --git a/msm8998/sdm/libs/utils/utils.cpp b/msm8998/sdm/libs/utils/utils.cpp
index 2ca9039..07211a1 100644
--- a/msm8998/sdm/libs/utils/utils.cpp
+++ b/msm8998/sdm/libs/utils/utils.cpp
@@ -27,8 +27,11 @@
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include <utils/sys.h>
+#include <unistd.h>
 #include <math.h>
+#include <utils/sys.h>
+#include <utils/utils.h>
+
 #include <algorithm>
 
 #define __CLASS__ "Utils"
@@ -60,4 +63,9 @@
   }
 }
 
+DriverType GetDriverType() {
+    const char *fb_caps = "/sys/devices/virtual/graphics/fb0/mdp/caps";
+    // 0 - File exists
+    return Sys::access_(fb_caps, F_OK) ? DriverType::DRM : DriverType::FB;
+}
 }  // namespace sdm