display: Update to AU160 proprietary drop

Update to AU_LINUX_ANDROID_LA.HB.1.3.9.06.00.01.213.160

display_hal:
b92e73f hwc: HWC 2.0 implementation
c14b208 sdm: Re-define MultiRect MDP flags
34bad08 display: Disable QTI_BSP
728f253 Merge remote-tracking branch 'quic/display.lnx.3.0-dev' into LA.HB.1.3.9
6f12fbc sdm: Use vector for layers and regions
ff96e87 sdm: Move blit/hybrid property check to HWCDisplay
8bf69fb sdm: Define feature flags for pipe

Change-Id: I643c3f3a9606e205b76a60afb89ebbd525d647f2
diff --git a/msm8996/Android.mk b/msm8996/Android.mk
index 58146e1..1c16b90 100644
--- a/msm8996/Android.mk
+++ b/msm8996/Android.mk
@@ -2,7 +2,7 @@
 display-hals += hdmi_cec
 
 sdm-libs := sdm/libs
-display-hals += $(sdm-libs)/utils $(sdm-libs)/core $(sdm-libs)/hwc
+display-hals += $(sdm-libs)/utils $(sdm-libs)/core $(sdm-libs)/hwc $(sdm-libs)/hwc2
 
 ifeq ($(call is-vendor-board-platform,QCOM),true)
     include $(call all-named-subdir-makefiles,$(display-hals))
diff --git a/msm8996/common.mk b/msm8996/common.mk
index f674ac4..62bd128 100644
--- a/msm8996/common.mk
+++ b/msm8996/common.mk
@@ -1,6 +1,8 @@
 #Common headers
 display_top := $(call my-dir)
 
+use_hwc2 := false
+
 common_includes := $(display_top)/libqdutils
 common_includes += $(display_top)/libqservice
 common_includes += $(display_top)/libcopybit
@@ -37,13 +39,10 @@
 kernel_includes :=
 
 # Executed only on QCOM BSPs
-ifeq ($(TARGET_USES_QCOM_BSP),true)
+#ifeq ($(TARGET_USES_QCOM_BSP),true)
 # Enable QCOM Display features
-    common_flags += -DQCOM_BSP
-endif
-ifneq ($(call is-platform-sdk-version-at-least,18),true)
-    common_flags += -DANDROID_JELLYBEAN_MR1=1
-endif
+#    common_flags += -DQTI_BSP
+#endif
 
 ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
 # This check is to pick the kernel headers from the right location.
diff --git a/msm8996/hdmi_cec/qhdmi_cec.cpp b/msm8996/hdmi_cec/qhdmi_cec.cpp
index 1bc7df6..f84cf80 100644
--- a/msm8996/hdmi_cec/qhdmi_cec.cpp
+++ b/msm8996/hdmi_cec/qhdmi_cec.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 The Linux Foundation. All rights reserved.
+* Copyright (c) 2014, 2016, 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
@@ -58,7 +58,7 @@
     CEC_OFFSET_RECEIVER_ID,
     CEC_OFFSET_OPCODE,
     CEC_OFFSET_OPERAND,
-    CEC_OFFSET_FRAME_LENGTH = 18,
+    CEC_OFFSET_FRAME_LENGTH = 17,
     CEC_OFFSET_RETRANSMIT,
 };
 
@@ -413,8 +413,7 @@
 {
     ssize_t err;
     // Enable CEC
-    // TODO: Set to 0x3 to enable CEC wakeup once driver has support
-    int value = enable ? 0x1 : 0x0;
+    int value = enable ? 0x3 : 0x0;
     err = write_int_to_node(ctx, "cec/enable", value);
     if(err < 0) {
         ALOGE("%s: Failed to toggle CEC: enable: %d",
diff --git a/msm8996/libqdutils/display_config.h b/msm8996/libqdutils/display_config.h
index 9ba60f2..05a7f29 100644
--- a/msm8996/libqdutils/display_config.h
+++ b/msm8996/libqdutils/display_config.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013 - 2016 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
@@ -47,10 +47,12 @@
 
 // Use this enum to specify the dpy parameters where needed
 enum {
-    DISPLAY_PRIMARY = 0,
-    DISPLAY_EXTERNAL,
-    DISPLAY_TERTIARY,
-    DISPLAY_VIRTUAL,
+    DISPLAY_PRIMARY = HWC_DISPLAY_PRIMARY,
+    DISPLAY_EXTERNAL = HWC_DISPLAY_EXTERNAL,
+#ifdef QTI_BSP
+    DISPLAY_TERTIARY = HWC_DISPLAY_TERTIARY,
+#endif
+    DISPLAY_VIRTUAL = HWC_DISPLAY_VIRTUAL,
 };
 
 // External Display states - used in setSecondaryDisplayStatus()
diff --git a/msm8996/libqservice/QService.cpp b/msm8996/libqservice/QService.cpp
index ddb4b18..546ad7e 100644
--- a/msm8996/libqservice/QService.cpp
+++ b/msm8996/libqservice/QService.cpp
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
+ *  Copyright (c) 2012-2014, 2016, 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
@@ -55,7 +55,7 @@
 }
 
 void QService::connect(const sp<qClient::IQHDMIClient>& client) {
-    ALOGD_IF(QSERVICE_DEBUG,"HWC client connected");
+    ALOGD_IF(QSERVICE_DEBUG,"HDMI client connected");
     mHDMIClient = client;
 }
 
@@ -82,7 +82,7 @@
         ALOGD_IF(QSERVICE_DEBUG, "%s: HDMI hotplug", __FUNCTION__);
         mHDMIClient->onHdmiHotplug(connected);
     } else {
-        ALOGE("%s: Failed to get a valid HDMI client", __FUNCTION__);
+        ALOGW("%s: Failed to get a valid HDMI client", __FUNCTION__);
     }
 }
 
@@ -91,7 +91,7 @@
         ALOGD_IF(QSERVICE_DEBUG, "%s: CEC message received", __FUNCTION__);
         mHDMIClient->onCECMessageRecieved(msg, len);
     } else {
-        ALOGE("%s: Failed to get a valid HDMI client", __FUNCTION__);
+        ALOGW("%s: Failed to get a valid HDMI client", __FUNCTION__);
     }
 }
 
diff --git a/msm8996/libqservice/QService.h b/msm8996/libqservice/QService.h
index 719c9b7..bd130fa 100644
--- a/msm8996/libqservice/QService.h
+++ b/msm8996/libqservice/QService.h
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
+ *  Copyright (c) 2012-2013, 2016, 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
@@ -50,8 +50,8 @@
     virtual android::status_t dispatch(uint32_t command,
             const android::Parcel* data,
             android::Parcel* reply);
-    void onHdmiHotplug(int connected);
-    void onCECMessageReceived(char *msg, ssize_t len);
+    virtual void onHdmiHotplug(int connected);
+    virtual void onCECMessageReceived(char *msg, ssize_t len);
     static void init();
 private:
     QService();
diff --git a/msm8996/sdm/.clang-format b/msm8996/sdm/.clang-format
new file mode 100644
index 0000000..9082c40
--- /dev/null
+++ b/msm8996/sdm/.clang-format
@@ -0,0 +1,13 @@
+---
+Language:        Cpp
+BasedOnStyle:  Google
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortBlocksOnASingleLine: false
+ColumnLimit:     100
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+ConstructorInitializerIndentWidth: 4
+DerivePointerAlignment: false
+PointerAlignment: Right
+#ReflowComments: false
diff --git a/msm8996/sdm/include/core/core_interface.h b/msm8996/sdm/include/core/core_interface.h
index 582222f..8f97cb4 100644
--- a/msm8996/sdm/include/core/core_interface.h
+++ b/msm8996/sdm/include/core/core_interface.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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
@@ -88,6 +88,18 @@
   kBwModeMax,      //!< Limiter for maximum available bandwidth modes.
 };
 
+
+/*! @brief Information on hardware for the first display
+
+  @details This structure returns the display type of the first display on the device
+  (internal display or HDMI etc) and whether it is currently connected,
+
+*/
+struct HWDisplayInterfaceInfo {
+  DisplayType type;
+  bool is_connected;
+};
+
 /*! @brief Display core interface.
 
   @details This class defines display core interfaces. It contains methods which client shall use
@@ -172,6 +184,18 @@
    */
     virtual DisplayError SetMaxBandwidthMode(HWBwModes mode) = 0;
 
+  /*! @brief Method to get characteristics of the first display.
+
+    @details Client shall use this method to determine if the first display is HDMI, and whether
+    it is currently connected.
+
+    @param[in] hw_disp_info structure that this method will fill up with info.
+
+    @return \link DisplayError \endlink
+
+   */
+    virtual DisplayError GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) = 0;
+
 
  protected:
   virtual ~CoreInterface() { }
diff --git a/msm8996/sdm/include/core/display_interface.h b/msm8996/sdm/include/core/display_interface.h
index 0416daa..a74b34b 100644
--- a/msm8996/sdm/include/core/display_interface.h
+++ b/msm8996/sdm/include/core/display_interface.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -144,6 +144,16 @@
   */
   virtual DisplayError Refresh() = 0;
 
+  /*! @brief Event handler for CEC messages.
+
+    @details This event is dispatched to send CEC messages to the CEC HAL.
+
+    @param[in] message message to be sent
+
+    @return \link DisplayError \endlink
+  */
+  virtual DisplayError CECMessage(char *message) = 0;
+
  protected:
   virtual ~DisplayEventHandler() { }
 };
diff --git a/msm8996/sdm/include/core/layer_buffer.h b/msm8996/sdm/include/core/layer_buffer.h
index 47ebf92..7eba4e3 100644
--- a/msm8996/sdm/include/core/layer_buffer.h
+++ b/msm8996/sdm/include/core/layer_buffer.h
@@ -206,8 +206,9 @@
   uint32_t height = 0;          //!< Actual height of the Layer that this buffer is for.
   uint32_t size = 0;            //!< Size of a single buffer (even if multiple clubbed together)
   LayerBufferFormat format = kFormatRGBA8888;     //!< Format of the buffer content.
-  LayerBufferPlane planes[4];   //!< Array of planes that this buffer contains. RGB buffer formats
-                                //!< have 1 plane whereas YUV buffer formats may have upto 4 planes.
+  LayerBufferPlane planes[4] = {};
+                                //!< Array of planes that this buffer contains. RGB buffer formats
+                                //!< have 1 plane whereas YUV buffer formats may have upto 4 planes
                                 //!< Total number of planes for the buffer will be interpreted based
                                 //!< on the buffer format specified.
 
diff --git a/msm8996/sdm/include/core/layer_stack.h b/msm8996/sdm/include/core/layer_stack.h
index 746d831..3d86fbe 100644
--- a/msm8996/sdm/include/core/layer_stack.h
+++ b/msm8996/sdm/include/core/layer_stack.h
@@ -34,6 +34,8 @@
 #include <stdint.h>
 #include <utils/constants.h>
 
+#include <vector>
+
 #include "layer_buffer.h"
 #include "sdm_types.h"
 
@@ -203,6 +205,9 @@
       uint32_t s3d_mode_present : 1;  //!< This flag will be set to true, if the current layer
                                       //!< stack contains s3d layer, and the layer stack can enter
                                       //!< s3d mode.
+
+      uint32_t post_processed_output : 1;  // If output_buffer should contain post processed output
+                                           // This applies only to primary displays currently
     };
 
     uint32_t flags = 0;               //!< For initialization purpose only.
@@ -261,23 +266,23 @@
                                                    //!< should be preserved between Prepare() and
                                                    //!< Commit() calls.
 
-  LayerRect src_rect;                              //!< Rectangular area of the layer buffer to
+  LayerRect src_rect = {};                         //!< Rectangular area of the layer buffer to
                                                    //!< consider for composition.
 
-  LayerRect dst_rect;                              //!< The target position where the frame will be
+  LayerRect dst_rect = {};                         //!< The target position where the frame will be
                                                    //!< displayed. Cropping rectangle is scaled to
                                                    //!< fit into this rectangle. The origin is the
                                                    //!< top-left corner of the screen.
 
-  LayerRectArray visible_regions;                  //!< Visible rectangular areas in screen space.
+  std::vector<LayerRect> visible_regions = {};     //!< Visible rectangular areas in screen space.
                                                    //!< The visible region includes areas overlapped
                                                    //!< by a translucent layer.
 
-  LayerRectArray dirty_regions;                    //!< Rectangular areas in the current frames
+  std::vector<LayerRect> dirty_regions = {};       //!< Rectangular areas in the current frames
                                                    //!< that have changed in comparison to
                                                    //!< previous frame.
 
-  LayerRectArray blit_regions;                     //!< Rectangular areas of this layer which need
+  std::vector<LayerRect> blit_regions = {};        //!< Rectangular areas of this layer which need
                                                    //!< to be composed to blit target. Display
                                                    //!< device will update blit rectangles if a
                                                    //!< layer composition is set as hybrid. Nth blit
@@ -288,7 +293,7 @@
                                                     //!< applied on the layer buffer during
                                                     //!< composition.
 
-  LayerTransform transform;                        //!< Rotation/Flip operations which need to be
+  LayerTransform transform = {};                   //!< Rotation/Flip operations which need to be
                                                    //!< applied to the layer buffer during
                                                    //!< composition.
 
@@ -319,8 +324,7 @@
   @sa DisplayInterface::Commit
 */
 struct LayerStack {
-  Layer *layers = NULL;                //!< Array of layers.
-  uint32_t layer_count = 0;            //!< Total number of layers.
+  std::vector<Layer *> layers = {};    //!< Vector of layer pointers.
 
   int retire_fence_fd = -1;            //!< File descriptor referring to a sync fence object which
                                        //!< will be signaled when this composited frame has been
diff --git a/msm8996/sdm/include/private/hw_info_types.h b/msm8996/sdm/include/private/hw_info_types.h
index 148de7c..38ccf21 100644
--- a/msm8996/sdm/include/private/hw_info_types.h
+++ b/msm8996/sdm/include/private/hw_info_types.h
@@ -112,6 +112,12 @@
   kBlendFilterMax,
 };
 
+enum HWPipeFlags {
+  kIGC = 0x01,
+  kMultiRect = 0x02,
+  kMultiRectParallelMode = 0x04,
+};
+
 typedef std::map<HWSubBlockType, std::vector<LayerBufferFormat>> FormatsMap;
 
 struct HWDynBwLimitInfo {
@@ -164,6 +170,8 @@
   uint32_t linear_factor = 0;
   uint32_t scale_factor = 0;
   uint32_t extra_fudge_factor = 0;
+  uint32_t amortizable_threshold = 0;
+  uint32_t system_overhead_lines = 0;
   bool has_bwc = false;
   bool has_ubwc = false;
   bool has_decimation = false;
@@ -174,6 +182,8 @@
   bool has_dyn_bw_support = false;
   bool separate_rotator = false;
   bool has_qseed3 = false;
+  bool has_concurrent_writeback = false;
+  uint32_t writeback_index = kHWBlockMax;
   HWDynBwLimitInfo dyn_bw_info;
   std::vector<HWPipeCaps> hw_pipes;
   FormatsMap supported_formats_map;
@@ -385,7 +395,7 @@
   uint8_t vertical_decimation = 0;
   HWScaleData scale_data;
   uint32_t z_order = 0;
-  bool set_igc = false;
+  uint8_t flags = 0;
   bool valid = false;
 
   void Reset() { *this = HWPipeInfo(); }
diff --git a/msm8996/sdm/include/private/resource_interface.h b/msm8996/sdm/include/private/resource_interface.h
index c350460..21652f5 100644
--- a/msm8996/sdm/include/private/resource_interface.h
+++ b/msm8996/sdm/include/private/resource_interface.h
@@ -47,7 +47,7 @@
   virtual DisplayError ValidateScaling(const LayerRect &crop, const LayerRect &dst,
                                        bool rotate90, bool ubwc_tiled,
                                        bool use_rotator_downscale) = 0;
-  virtual DisplayError ValidateCursorConfig(Handle display_ctx, const Layer &layer,
+  virtual DisplayError ValidateCursorConfig(Handle display_ctx, const Layer *layer,
                                             bool is_top) = 0;
   virtual DisplayError ValidateCursorPosition(Handle display_ctx, HWLayers *hw_layers,
                                               int x, int y) = 0;
diff --git a/msm8996/sdm/include/utils/debug.h b/msm8996/sdm/include/utils/debug.h
index 5ac2319..dfb10c7 100644
--- a/msm8996/sdm/include/utils/debug.h
+++ b/msm8996/sdm/include/utils/debug.h
@@ -45,6 +45,7 @@
 #define DLOGV_IF(tag, format, ...) DLOG(tag, Verbose, format, ##__VA_ARGS__)
 
 #define DLOGE(format, ...) DLOGE_IF(kTagNone, format, ##__VA_ARGS__)
+#define DLOGD(format, ...) DLOGD_IF(kTagNone, format, ##__VA_ARGS__)
 #define DLOGW(format, ...) DLOGW_IF(kTagNone, format, ##__VA_ARGS__)
 #define DLOGI(format, ...) DLOGI_IF(kTagNone, format, ##__VA_ARGS__)
 #define DLOGV(format, ...) DLOGV_IF(kTagNone, format, ##__VA_ARGS__)
diff --git a/msm8996/sdm/include/utils/formats.h b/msm8996/sdm/include/utils/formats.h
index 57825dd..67548f3 100644
--- a/msm8996/sdm/include/utils/formats.h
+++ b/msm8996/sdm/include/utils/formats.h
@@ -36,6 +36,7 @@
 
 bool IsUBWCFormat(LayerBufferFormat format);
 bool Is10BitFormat(LayerBufferFormat format);
+const char *GetFormatString(const LayerBufferFormat &format);
 
 }  // namespace sdm
 
diff --git a/msm8996/sdm/include/utils/sys.h b/msm8996/sdm/include/utils/sys.h
index dc45696..ed9b75c 100644
--- a/msm8996/sdm/include/utils/sys.h
+++ b/msm8996/sdm/include/utils/sys.h
@@ -25,6 +25,8 @@
 #ifndef __SYS_H__
 #define __SYS_H__
 
+#include <sys/eventfd.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <poll.h>
@@ -46,6 +48,9 @@
   typedef ssize_t (*getline)(char **lineptr, size_t *linelen, FILE *stream);
   typedef int (*pthread_cancel)(pthread_t thread);
   typedef int (*dup)(int fd);
+  typedef ssize_t (*read)(int, void *, size_t);
+  typedef ssize_t (*write)(int, const void *, size_t);
+  typedef int (*eventfd)(unsigned int, int);
 
   static ioctl ioctl_;
   static open open_;
@@ -58,6 +63,9 @@
   static getline getline_;
   static pthread_cancel pthread_cancel_;
   static dup dup_;
+  static read read_;
+  static write write_;
+  static eventfd eventfd_;
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/core/Android.mk b/msm8996/sdm/libs/core/Android.mk
index 218ff98..5e88683 100644
--- a/msm8996/sdm/libs/core/Android.mk
+++ b/msm8996/sdm/libs/core/Android.mk
@@ -29,6 +29,7 @@
                                  $(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_scale.cpp \
+                                 $(LOCAL_HW_INTF_PATH)/hw_events.cpp
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/msm8996/sdm/libs/core/comp_manager.cpp b/msm8996/sdm/libs/core/comp_manager.cpp
index a7f137a..4bd73d4 100644
--- a/msm8996/sdm/libs/core/comp_manager.cpp
+++ b/msm8996/sdm/libs/core/comp_manager.cpp
@@ -219,7 +219,7 @@
 
   // Avoid idle fallback, if there is only one app layer.
   // TODO(user): App layer count will change for hybrid composition
-  uint32_t app_layer_count = hw_layers->info.stack->layer_count - 1;
+  uint32_t app_layer_count = UINT32(hw_layers->info.stack->layers.size()) - 1;
   if ((app_layer_count > 1 && display_comp_ctx->idle_fallback) || display_comp_ctx->fallback_) {
     // Handle the idle timeout by falling back
     constraints->safe_mode = true;
@@ -433,9 +433,9 @@
     return supported;
   }
 
-  for (int32_t i = INT32(layer_stack->layer_count - 1); i >= 0; i--) {
-    Layer &layer = layer_stack->layers[i];
-    if (layer.composition == kCompositionGPUTarget) {
+  for (int32_t i = INT32(layer_stack->layers.size() - 1); i >= 0; i--) {
+    Layer *layer = layer_stack->layers.at(UINT32(i));
+    if (layer->composition == kCompositionGPUTarget) {
       gpu_index = i;
       break;
     }
@@ -443,9 +443,9 @@
   if (gpu_index <= 0) {
     return supported;
   }
-  Layer &cursor_layer = layer_stack->layers[gpu_index - 1];
-  if (cursor_layer.flags.cursor && resource_intf_->ValidateCursorConfig(display_resource_ctx,
-                                   cursor_layer, true) == kErrorNone) {
+  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) {
     supported = true;
   }
 
diff --git a/msm8996/sdm/libs/core/core_impl.cpp b/msm8996/sdm/libs/core/core_impl.cpp
index 424be43..82f578e 100644
--- a/msm8996/sdm/libs/core/core_impl.cpp
+++ b/msm8996/sdm/libs/core/core_impl.cpp
@@ -212,5 +212,9 @@
   return comp_mgr_.SetMaxBandwidthMode(mode);
 }
 
+DisplayError CoreImpl::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
+  return hw_info_intf_->GetFirstDisplayInterfaceType(hw_disp_info);
+}
+
 }  // namespace sdm
 
diff --git a/msm8996/sdm/libs/core/core_impl.h b/msm8996/sdm/libs/core/core_impl.h
index 06ca3d9..97e8655 100644
--- a/msm8996/sdm/libs/core/core_impl.h
+++ b/msm8996/sdm/libs/core/core_impl.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -58,6 +58,7 @@
                                      DisplayInterface **intf);
   virtual DisplayError DestroyDisplay(DisplayInterface *intf);
   virtual DisplayError SetMaxBandwidthMode(HWBwModes mode);
+  virtual DisplayError GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info);
 
  protected:
   Locker locker_;
diff --git a/msm8996/sdm/libs/core/display_base.cpp b/msm8996/sdm/libs/core/display_base.cpp
index 5a155b7..33ee08d 100644
--- a/msm8996/sdm/libs/core/display_base.cpp
+++ b/msm8996/sdm/libs/core/display_base.cpp
@@ -25,8 +25,11 @@
 #include <stdio.h>
 #include <utils/constants.h>
 #include <utils/debug.h>
+#include <utils/formats.h>
 #include <utils/rect.h>
 
+#include <vector>
+
 #include "display_base.h"
 #include "hw_info_interface.h"
 
@@ -116,23 +119,25 @@
 
   comp_manager_->UnregisterDisplay(display_comp_ctx_);
 
+  HWEventsInterface::Destroy(hw_events_intf_);
+
   return kErrorNone;
 }
 
 DisplayError DisplayBase::ValidateGPUTarget(LayerStack *layer_stack) {
   uint32_t i = 0;
-  Layer *layers = layer_stack->layers;
+  std::vector<Layer *>layers = layer_stack->layers;
 
   // TODO(user): Remove this check once we have query display attributes on virtual display
   if (display_type_ == kVirtual) {
     return kErrorNone;
   }
-
-  while (i < layer_stack->layer_count && (layers[i].composition != kCompositionGPUTarget)) {
+  uint32_t layer_count = UINT32(layers.size());
+  while ((i < layer_count) && (layers.at(i)->composition != kCompositionGPUTarget)) {
     i++;
   }
 
-  if (i >= layer_stack->layer_count) {
+  if (i >= layer_count) {
     DLOGE("Either layer count is zero or GPU target layer is not present");
     return kErrorParameters;
   }
@@ -140,20 +145,20 @@
   uint32_t gpu_target_index = i;
 
   // Check GPU target layer
-  Layer &gpu_target_layer = layer_stack->layers[gpu_target_index];
+  Layer *gpu_target_layer = layers.at(gpu_target_index);
 
-  if (!IsValid(gpu_target_layer.src_rect)) {
+  if (!IsValid(gpu_target_layer->src_rect)) {
     DLOGE("Invalid src rect for GPU target layer");
     return kErrorParameters;
   }
 
-  if (!IsValid(gpu_target_layer.dst_rect)) {
+  if (!IsValid(gpu_target_layer->dst_rect)) {
     DLOGE("Invalid dst rect for GPU target layer");
     return kErrorParameters;
   }
 
-  auto gpu_target_layer_dst_xpixels = gpu_target_layer.dst_rect.right;
-  auto gpu_target_layer_dst_ypixels = gpu_target_layer.dst_rect.bottom;
+  auto gpu_target_layer_dst_xpixels = gpu_target_layer->dst_rect.right;
+  auto gpu_target_layer_dst_ypixels = gpu_target_layer->dst_rect.bottom;
 
   HWDisplayAttributes display_attrib;
   uint32_t active_index = 0;
@@ -560,7 +565,7 @@
   LayerBuffer *out_buffer = hw_layers_.info.stack->output_buffer;
   if (out_buffer) {
     DumpImpl::AppendString(buffer, length, "\nres:%u x %u format: %s", out_buffer->width,
-                           out_buffer->height, GetName(out_buffer->format));
+                           out_buffer->height, GetFormatString(out_buffer->format));
   } else {
     DumpImpl::AppendString(buffer, length, "\nres:%u x %u, dpi:%.2f x %.2f, fps:%u,"
                            "vsync period: %u", info.x_pixels, info.y_pixels, info.x_dpi,
@@ -591,14 +596,14 @@
 
   for (uint32_t i = 0; i < num_hw_layers; i++) {
     uint32_t layer_index = hw_layers_.info.index[i];
-    Layer &layer = hw_layers_.info.stack->layers[layer_index];
-    LayerBuffer *input_buffer = layer.input_buffer;
+    Layer *layer = hw_layers_.info.stack->layers.at(layer_index);
+    LayerBuffer *input_buffer = layer->input_buffer;
     HWLayerConfig &layer_config = hw_layers_.config[i];
     HWRotatorSession &hw_rotator_session = layer_config.hw_rotator_session;
 
     char idx[8] = { 0 };
-    const char *comp_type = GetName(layer.composition);
-    const char *buffer_format = GetName(input_buffer->format);
+    const char *comp_type = GetName(layer->composition);
+    const char *buffer_format = GetFormatString(input_buffer->format);
     const char *rotate_split[2] = { "Rot-1", "Rot-2" };
     const char *comp_split[2] = { "Comp-1", "Comp-2" };
 
@@ -626,7 +631,7 @@
 
     if (hw_rotator_session.hw_block_count > 0) {
       input_buffer = &hw_rotator_session.output_buffer;
-      buffer_format = GetName(input_buffer->format);
+      buffer_format = GetFormatString(input_buffer->format);
     }
 
     for (uint32_t count = 0; count < 2; count++) {
@@ -645,10 +650,10 @@
       LayerRect &dst_roi = pipe.dst_roi;
 
       snprintf(z_order, sizeof(z_order), "%d", pipe.z_order);
-      snprintf(flags, sizeof(flags), "0x%08x", layer.flags.flags);
+      snprintf(flags, sizeof(flags), "0x%08x", layer->flags.flags);
       snprintf(decimation, sizeof(decimation), "%3d x %3d", pipe.horizontal_decimation,
                pipe.vertical_decimation);
-      snprintf(csc, sizeof(csc), "%d", layer.csc);
+      snprintf(csc, sizeof(csc), "%d", layer->csc);
 
       DumpImpl::AppendString(buffer, length, format, idx, comp_type, comp_split[count],
                              "-", pipe.pipe_id, input_buffer->width, input_buffer->height,
@@ -693,52 +698,6 @@
   }
 }
 
-const char * DisplayBase::GetName(const LayerBufferFormat &format) {
-  switch (format) {
-  case kFormatARGB8888:                 return "ARGB_8888";
-  case kFormatRGBA8888:                 return "RGBA_8888";
-  case kFormatBGRA8888:                 return "BGRA_8888";
-  case kFormatXRGB8888:                 return "XRGB_8888";
-  case kFormatRGBX8888:                 return "RGBX_8888";
-  case kFormatBGRX8888:                 return "BGRX_8888";
-  case kFormatRGBA5551:                 return "RGBA_5551";
-  case kFormatRGBA4444:                 return "RGBA_4444";
-  case kFormatRGB888:                   return "RGB_888";
-  case kFormatBGR888:                   return "BGR_888";
-  case kFormatRGB565:                   return "RGB_565";
-  case kFormatBGR565:                   return "BGR_565";
-  case kFormatRGBA8888Ubwc:             return "RGBA_8888_UBWC";
-  case kFormatRGBX8888Ubwc:             return "RGBX_8888_UBWC";
-  case kFormatBGR565Ubwc:               return "BGR_565_UBWC";
-  case kFormatYCbCr420Planar:           return "Y_CB_CR_420";
-  case kFormatYCrCb420Planar:           return "Y_CR_CB_420";
-  case kFormatYCrCb420PlanarStride16:   return "Y_CR_CB_420_STRIDE16";
-  case kFormatYCbCr420SemiPlanar:       return "Y_CBCR_420";
-  case kFormatYCrCb420SemiPlanar:       return "Y_CRCB_420";
-  case kFormatYCbCr420SemiPlanarVenus:  return "Y_CBCR_420_VENUS";
-  case kFormatYCrCb420SemiPlanarVenus:  return "Y_CRCB_420_VENUS";
-  case kFormatYCbCr422H1V2SemiPlanar:   return "Y_CBCR_422_H1V2";
-  case kFormatYCrCb422H1V2SemiPlanar:   return "Y_CRCB_422_H1V2";
-  case kFormatYCbCr422H2V1SemiPlanar:   return "Y_CBCR_422_H2V1";
-  case kFormatYCrCb422H2V1SemiPlanar:   return "Y_CRCB_422_H2V2";
-  case kFormatYCbCr420SPVenusUbwc:      return "Y_CBCR_420_VENUS_UBWC";
-  case kFormatYCbCr422H2V1Packed:       return "YCBYCR_422_H2V1";
-  case kFormatRGBA1010102:              return "RGBA_1010102";
-  case kFormatARGB2101010:              return "ARGB_2101010";
-  case kFormatRGBX1010102:              return "RGBX_1010102";
-  case kFormatXRGB2101010:              return "XRGB_2101010";
-  case kFormatBGRA1010102:              return "BGRA_1010102";
-  case kFormatABGR2101010:              return "ABGR_2101010";
-  case kFormatBGRX1010102:              return "BGRX_1010102";
-  case kFormatXBGR2101010:              return "XBGR_2101010";
-  case kFormatRGBA1010102Ubwc:          return "RGBA_1010102_UBWC";
-  case kFormatRGBX1010102Ubwc:          return "RGBX_1010102_UBWC";
-  case kFormatYCbCr420P010:             return "Y_CBCR_420_P010";
-  case kFormatYCbCr420TP10Ubwc:         return "Y_CBCR_420_TP10_UBWC";
-  default:                              return "UNKNOWN";
-  }
-}
-
 DisplayError DisplayBase::ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload,
                                                PPDisplayAPIPayload *out_payload,
                                                PPPendingParams *pending_action) {
@@ -790,4 +749,15 @@
   return kErrorNotSupported;
 }
 
+DisplayError DisplayBase::SetVSyncState(bool enable) {
+  DisplayError error = kErrorNone;
+  if (vsync_enable_ != enable) {
+    error = hw_intf_->SetVSyncState(enable);
+    if (error == kErrorNone) {
+      vsync_enable_ = enable;
+    }
+  }
+  return error;
+}
+
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/core/display_base.h b/msm8996/sdm/libs/core/display_base.h
index 436af4f..7242156 100644
--- a/msm8996/sdm/libs/core/display_base.h
+++ b/msm8996/sdm/libs/core/display_base.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014-2016, 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:
@@ -34,6 +34,7 @@
 #include "hw_interface.h"
 #include "comp_manager.h"
 #include "color_manager.h"
+#include "hw_events_interface.h"
 
 namespace sdm {
 
@@ -73,6 +74,7 @@
   virtual DisplayError SetCursorPosition(int x, int y);
   virtual DisplayError GetRefreshRateRange(uint32_t *min_refresh_rate, uint32_t *max_refresh_rate);
   virtual DisplayError GetPanelBrightness(int *level);
+  virtual DisplayError SetVSyncState(bool enable);
 
  protected:
   // DumpImpl method
@@ -80,7 +82,6 @@
 
   bool IsRotationRequired(HWLayers *hw_layers);
   const char *GetName(const LayerComposition &composition);
-  const char *GetName(const LayerBufferFormat &format);
   DisplayError ValidateGPUTarget(LayerStack *layer_stack);
 
   DisplayType display_type_;
@@ -104,6 +105,7 @@
   HWInfoInterface *hw_info_intf_ = NULL;
   ColorManagerProxy *color_mgr_ = NULL;  // each display object owns its ColorManagerProxy
   bool partial_update_control_ = true;
+  HWEventsInterface *hw_events_intf_ = NULL;
 
  private:
   // Unused
diff --git a/msm8996/sdm/libs/core/display_hdmi.cpp b/msm8996/sdm/libs/core/display_hdmi.cpp
index 773016a..6522377 100644
--- a/msm8996/sdm/libs/core/display_hdmi.cpp
+++ b/msm8996/sdm/libs/core/display_hdmi.cpp
@@ -70,6 +70,7 @@
   error = DisplayBase::Init();
   if (error != kErrorNone) {
     HWHDMI::Destroy(hw_intf_);
+    return error;
   }
 
   GetScanSupport();
@@ -85,6 +86,14 @@
                             (kS3dFormatTopBottom, kS3DModeTB));
   s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode>
                             (kS3dFormatFramePacking, kS3DModeFP));
+
+  error = HWEventsInterface::Create(INT(display_type_), this, &event_list_, &hw_events_intf_);
+  if (error != kErrorNone) {
+    DisplayBase::Deinit();
+    HWHDMI::Destroy(hw_intf_);
+    DLOGE("Failed to create hardware events interface. Error = %d", error);
+  }
+
   return error;
 }
 
@@ -157,7 +166,7 @@
 
 DisplayError DisplayHDMI::SetVSyncState(bool enable) {
   SCOPE_LOCK(locker_);
-  return kErrorNotSupported;
+  return DisplayBase::SetVSyncState(enable);
 }
 
 void DisplayHDMI::SetIdleTimeoutMs(uint32_t timeout_ms) { }
@@ -311,18 +320,18 @@
   HWPanelInfo panel_info;
   HWDisplayAttributes display_attributes;
   uint32_t active_index = 0;
-  uint32_t layer_count = layer_stack->layer_count;
+  uint32_t layer_count = UINT32(layer_stack->layers.size());
 
   // S3D mode is supported for the following scenarios:
   // 1. Layer stack containing only one s3d layer which is not skip
   // 2. Layer stack containing only one secure layer along with one s3d layer
   for (uint32_t i = 0; i < layer_count; i++) {
-    Layer &layer = layer_stack->layers[i];
-    LayerBuffer *layer_buffer = layer.input_buffer;
+    Layer *layer = layer_stack->layers.at(i);
+    LayerBuffer *layer_buffer = layer->input_buffer;
 
     if (layer_buffer->s3d_format != kS3dFormatNone) {
       s3d_layer_count++;
-      if (s3d_layer_count > 1 || layer.flags.skip) {
+      if (s3d_layer_count > 1 || layer->flags.skip) {
         s3d_mode = kS3DModeNone;
         break;
       }
@@ -355,5 +364,19 @@
   }
 }
 
+void DisplayHDMI::CECMessage(char *message) {
+  event_handler_->CECMessage(message);
+}
+
+DisplayError DisplayHDMI::VSync(int64_t timestamp) {
+  if (vsync_enable_) {
+    DisplayEventVSync vsync;
+    vsync.timestamp = timestamp;
+    event_handler_->VSync(vsync);
+  }
+
+  return kErrorNone;
+}
+
 }  // namespace sdm
 
diff --git a/msm8996/sdm/libs/core/display_hdmi.h b/msm8996/sdm/libs/core/display_hdmi.h
index ca33035..88e553d 100644
--- a/msm8996/sdm/libs/core/display_hdmi.h
+++ b/msm8996/sdm/libs/core/display_hdmi.h
@@ -25,6 +25,7 @@
 #ifndef __DISPLAY_HDMI_H__
 #define __DISPLAY_HDMI_H__
 
+#include <vector>
 #include <map>
 
 #include "display_base.h"
@@ -34,7 +35,7 @@
 
 class HWHDMIInterface;
 
-class DisplayHDMI : public DisplayBase, DumpImpl {
+class DisplayHDMI : public DisplayBase, DumpImpl, HWEventHandler {
  public:
   DisplayHDMI(DisplayEventHandler *event_handler, HWInfoInterface *hw_info_intf,
               BufferSyncHandler *buffer_sync_handler, CompManager *comp_manager,
@@ -65,6 +66,13 @@
   virtual void AppendDump(char *buffer, uint32_t length);
   virtual DisplayError SetCursorPosition(int x, int y);
 
+  // Implement the HWEventHandlers
+  virtual DisplayError VSync(int64_t timestamp);
+  virtual DisplayError Blank(bool blank) { return kErrorNone; }
+  virtual void IdleTimeout() { }
+  virtual void ThermalEvent(int64_t thermal_level) { }
+  virtual void CECMessage(char *message);
+
  private:
   virtual uint32_t GetBestConfig(HWS3DMode s3d_mode);
   virtual void GetScanSupport();
@@ -73,6 +81,8 @@
   Locker locker_;
   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"};
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/core/display_primary.cpp b/msm8996/sdm/libs/core/display_primary.cpp
index e824dbe..e5751df 100644
--- a/msm8996/sdm/libs/core/display_primary.cpp
+++ b/msm8996/sdm/libs/core/display_primary.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -45,7 +45,7 @@
   SCOPE_LOCK(locker_);
 
   DisplayError error = HWPrimary::Create(&hw_intf_, hw_info_intf_,
-                                         DisplayBase::buffer_sync_handler_, this);
+                                         DisplayBase::buffer_sync_handler_);
 
   if (error != kErrorNone) {
     return error;
@@ -67,6 +67,13 @@
     }
   }
 
+  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();
+    HWPrimary::Destroy(hw_intf_);
+  }
+
   return error;
 }
 
@@ -186,15 +193,7 @@
 
 DisplayError DisplayPrimary::SetVSyncState(bool enable) {
   SCOPE_LOCK(locker_);
-  DisplayError error = kErrorNone;
-  if (vsync_enable_ != enable) {
-    error = hw_intf_->SetVSyncState(enable);
-    if (error == kErrorNone) {
-      vsync_enable_ = enable;
-    }
-  }
-
-  return error;
+  return DisplayBase::SetVSyncState(enable);
 }
 
 void DisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
diff --git a/msm8996/sdm/libs/core/display_primary.h b/msm8996/sdm/libs/core/display_primary.h
index b0f2934..d3a37cd 100644
--- a/msm8996/sdm/libs/core/display_primary.h
+++ b/msm8996/sdm/libs/core/display_primary.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -25,6 +25,8 @@
 #ifndef __DISPLAY_PRIMARY_H__
 #define __DISPLAY_PRIMARY_H__
 
+#include <vector>
+
 #include "display_base.h"
 #include "dump_impl.h"
 
@@ -68,10 +70,13 @@
   virtual DisplayError Blank(bool blank);
   virtual void IdleTimeout();
   virtual void ThermalEvent(int64_t thermal_level);
+  virtual void CECMessage(char *message) { }
 
  private:
   Locker locker_;
   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"};
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/core/fb/hw_device.cpp b/msm8996/sdm/libs/core/fb/hw_device.cpp
index fe157a2..546490a 100644
--- a/msm8996/sdm/libs/core/fb/hw_device.cpp
+++ b/msm8996/sdm/libs/core/fb/hw_device.cpp
@@ -58,11 +58,9 @@
     buffer_sync_handler_(buffer_sync_handler), synchronous_commit_(false) {
 }
 
-DisplayError HWDevice::Init(HWEventHandler *eventhandler) {
+DisplayError HWDevice::Init() {
   char device_name[64] = {0};
 
-  event_handler_ = eventhandler;
-
   // Read the fb node index
   fb_node_index_ = GetFBNodeIndex(device_type_);
   if (fb_node_index_ == -1) {
@@ -183,13 +181,13 @@
 
   for (uint32_t i = 0; i < hw_layer_info.count; i++) {
     uint32_t layer_index = hw_layer_info.index[i];
-    Layer &layer = stack->layers[layer_index];
-    LayerBuffer *input_buffer = layer.input_buffer;
+    Layer *layer = stack->layers.at(layer_index);
+    LayerBuffer *input_buffer = layer->input_buffer;
     HWPipeInfo *left_pipe = &hw_layers->config[i].left_pipe;
     HWPipeInfo *right_pipe = &hw_layers->config[i].right_pipe;
     HWRotatorSession *hw_rotator_session = &hw_layers->config[i].hw_rotator_session;
     bool is_rotator_used = (hw_rotator_session->hw_block_count != 0);
-    bool is_cursor_pipe_used = (hw_layer_info.use_hw_cursor & layer.flags.cursor);
+    bool is_cursor_pipe_used = (hw_layer_info.use_hw_cursor & layer->flags.cursor);
 
     for (uint32_t count = 0; count < 2; count++) {
       HWPipeInfo *pipe_info = (count == 0) ? left_pipe : right_pipe;
@@ -208,7 +206,7 @@
         mdp_buffer.comp_ratio.denom = 1000;
         mdp_buffer.comp_ratio.numer = UINT32(hw_layers->config[i].compression * 1000);
 
-        if (layer.flags.solid_fill) {
+        if (layer->flags.solid_fill) {
           mdp_buffer.format = MDP_ARGB_8888;
         } else {
           error = SetFormat(input_buffer->format, &mdp_buffer.format);
@@ -216,10 +214,10 @@
             return error;
           }
         }
-        mdp_layer.alpha = layer.plane_alpha;
+        mdp_layer.alpha = layer->plane_alpha;
         mdp_layer.z_order = UINT16(pipe_info->z_order);
         mdp_layer.transp_mask = 0xffffffff;
-        SetBlending(layer.blending, &mdp_layer.blend_op);
+        SetBlending(layer->blending, &mdp_layer.blend_op);
         mdp_layer.pipe_ndx = pipe_info->pipe_id;
         mdp_layer.horz_deci = pipe_info->horizontal_decimation;
         mdp_layer.vert_deci = pipe_info->vertical_decimation;
@@ -227,11 +225,17 @@
         SetRect(pipe_info->src_roi, &mdp_layer.src_rect);
         SetRect(pipe_info->dst_roi, &mdp_layer.dst_rect);
         SetMDPFlags(layer, is_rotator_used, is_cursor_pipe_used, &mdp_layer.flags);
-        SetCSC(layer.csc, &mdp_layer.color_space);
-        if (pipe_info->set_igc) {
+        SetCSC(layer->csc, &mdp_layer.color_space);
+        if (pipe_info->flags & kIGC) {
           SetIGC(layer, mdp_layer_count);
         }
-        mdp_layer.bg_color = layer.solid_fill_color;
+        if (pipe_info->flags & kMultiRect) {
+          mdp_layer.flags |= MDP_LAYER_MULTIRECT_ENABLE;
+          if (pipe_info->flags & kMultiRectParallelMode) {
+            mdp_layer.flags |= MDP_LAYER_MULTIRECT_PARALLEL_MODE;
+          }
+        }
+        mdp_layer.bg_color = layer->solid_fill_color;
 
         // HWScaleData to MDP driver
         hw_scale_->SetHWScaleData(pipe_info->scale_data, mdp_layer_count, &mdp_layer);
@@ -256,12 +260,10 @@
     }
   }
 
+  // TODO(user): This block should move to the derived class
   if (device_type_ == kDeviceVirtual) {
     LayerBuffer *output_buffer = hw_layers->info.stack->output_buffer;
-    // Fill WB index for virtual based on number of rotator WB blocks present in the HW.
-    // Eg: If 2 WB rotator blocks available, the WB index for virtual will be 2, as the
-    // indexing of WB blocks start from 0.
-    mdp_out_layer_.writeback_ndx = hw_resource_.hw_rot_info.num_rotator;
+    mdp_out_layer_.writeback_ndx = hw_resource_.writeback_index;
     mdp_out_layer_.buffer.width = output_buffer->width;
     mdp_out_layer_.buffer.height = output_buffer->height;
     if (output_buffer->flags.secure) {
@@ -329,7 +331,7 @@
 
   for (uint32_t i = 0; i < hw_layer_info.count; i++) {
     uint32_t layer_index = hw_layer_info.index[i];
-    LayerBuffer *input_buffer = stack->layers[layer_index].input_buffer;
+    LayerBuffer *input_buffer = stack->layers.at(layer_index)->input_buffer;
     HWPipeInfo *left_pipe = &hw_layers->config[i].left_pipe;
     HWPipeInfo *right_pipe = &hw_layers->config[i].right_pipe;
     HWRotatorSession *hw_rotator_session = &hw_layers->config[i].hw_rotator_session;
@@ -372,6 +374,7 @@
     }
   }
 
+  // TODO(user): Move to derived class
   if (device_type_ == kDeviceVirtual) {
     LayerBuffer *output_buffer = hw_layers->info.stack->output_buffer;
 
@@ -421,7 +424,7 @@
 
   for (uint32_t i = 0; i < hw_layer_info.count; i++) {
     uint32_t layer_index = hw_layer_info.index[i];
-    LayerBuffer *input_buffer = stack->layers[layer_index].input_buffer;
+    LayerBuffer *input_buffer = stack->layers.at(layer_index)->input_buffer;
     HWRotatorSession *hw_rotator_session = &hw_layers->config[i].hw_rotator_session;
 
     if (hw_rotator_session->hw_block_count) {
@@ -607,18 +610,18 @@
   target->h = UINT32(source.bottom) - target->y;
 }
 
-void HWDevice::SetMDPFlags(const Layer &layer, const bool &is_rotator_used,
+void HWDevice::SetMDPFlags(const Layer *layer, const bool &is_rotator_used,
                            bool is_cursor_pipe_used, uint32_t *mdp_flags) {
-  LayerBuffer *input_buffer = layer.input_buffer;
+  const LayerBuffer *input_buffer = layer->input_buffer;
 
   // Flips will be taken care by rotator, if layer uses rotator for downscale/rotation. So ignore
   // flip flags for MDP.
   if (!is_rotator_used) {
-    if (layer.transform.flip_vertical) {
+    if (layer->transform.flip_vertical) {
       *mdp_flags |= MDP_LAYER_FLIP_UD;
     }
 
-    if (layer.transform.flip_horizontal) {
+    if (layer->transform.flip_horizontal) {
       *mdp_flags |= MDP_LAYER_FLIP_LR;
     }
 
@@ -635,11 +638,11 @@
     *mdp_flags |= MDP_LAYER_SECURE_DISPLAY_SESSION;
   }
 
-  if (layer.flags.solid_fill) {
+  if (layer->flags.solid_fill) {
     *mdp_flags |= MDP_LAYER_SOLID_FILL;
   }
 
-  if (hw_panel_info_.mode != kModeCommand && layer.flags.cursor && is_cursor_pipe_used) {
+  if (hw_panel_info_.mode != kModeCommand && layer->flags.cursor && is_cursor_pipe_used) {
     // command mode panels does not support async position update
     *mdp_flags |= MDP_LAYER_ASYNC;
   }
@@ -945,6 +948,7 @@
   memset(&mdp_disp_commit_, 0, sizeof(mdp_disp_commit_));
   memset(&mdp_in_layers_, 0, sizeof(mdp_in_layers_));
   memset(&mdp_out_layer_, 0, sizeof(mdp_out_layer_));
+  mdp_out_layer_.buffer.fence = -1;
   hw_scale_->ResetScaleParams();
   memset(&pp_params_, 0, sizeof(pp_params_));
   memset(&igc_lut_data_, 0, sizeof(igc_lut_data_));
@@ -968,12 +972,12 @@
   }
 }
 
-void HWDevice::SetIGC(const Layer &layer, uint32_t index) {
+void HWDevice::SetIGC(const Layer *layer, uint32_t index) {
   mdp_input_layer &mdp_layer = mdp_in_layers_[index];
   mdp_overlay_pp_params &pp_params = pp_params_[index];
   mdp_igc_lut_data_v1_7 &igc_lut_data = igc_lut_data_[index];
 
-  switch (layer.igc) {
+  switch (layer->igc) {
   case kIGCsRGB:
     igc_lut_data.table_fmt = mdp_igc_srgb;
     pp_params.igc_cfg.ops = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
@@ -1032,7 +1036,12 @@
 }
 
 DisplayError HWDevice::SetVSyncState(bool enable) {
-  return kErrorNotSupported;
+  int vsync_on = enable ? 1 : 0;
+  if (Sys::ioctl_(device_fd_, MSMFB_OVERLAY_VSYNC_CTRL, &vsync_on) < 0) {
+    IOCTL_LOGE(MSMFB_OVERLAY_VSYNC_CTRL, device_type_);
+    return kErrorHardware;
+  }
+  return kErrorNone;
 }
 
 void HWDevice::SetIdleTimeoutMs(uint32_t timeout_ms) {
diff --git a/msm8996/sdm/libs/core/fb/hw_device.h b/msm8996/sdm/libs/core/fb/hw_device.h
index 82a9ee6..7311271 100644
--- a/msm8996/sdm/libs/core/fb/hw_device.h
+++ b/msm8996/sdm/libs/core/fb/hw_device.h
@@ -36,6 +36,14 @@
 #define IOCTL_LOGE(ioctl, type) DLOGE("ioctl %s, device = %d errno = %d, desc = %s", #ioctl, \
                                       type, errno, strerror(errno))
 
+#ifndef MDP_LAYER_MULTIRECT_ENABLE
+#define MDP_LAYER_MULTIRECT_ENABLE 0
+#endif
+
+#ifndef MDP_LAYER_MULTIRECT_PARALLEL_MODE
+#define MDP_LAYER_MULTIRECT_PARALLEL_MODE 0
+#endif
+
 namespace sdm {
 class HWInfoInterface;
 
@@ -78,7 +86,7 @@
   virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info);
 
   // For HWDevice derivatives
-  virtual DisplayError Init(HWEventHandler *eventhandler);
+  virtual DisplayError Init();
   virtual DisplayError Deinit();
 
   enum {
@@ -88,7 +96,6 @@
 
   static const int kMaxStringLength = 1024;
   static const int kNumPhysicalDisplays = 2;
-  static const int kNumDisplayEvents = 4;
 
   void DumpLayerCommit(const mdp_layer_commit &layer_commit);
   DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target);
@@ -96,7 +103,7 @@
                          uint32_t width, uint32_t *target);
   void SetBlending(const LayerBlending &source, mdss_mdp_blend_op *target);
   void SetRect(const LayerRect &source, mdp_rect *target);
-  void SetMDPFlags(const Layer &layer, const bool &is_rotator_used,
+  void SetMDPFlags(const Layer *layer, const bool &is_rotator_used,
                    bool is_cursor_pipe_used, uint32_t *mdp_flags);
   // Retrieves HW FrameBuffer Node Index
   int GetFBNodeIndex(HWDeviceType device_type);
@@ -111,14 +118,12 @@
   int ParseLine(char *input, const char *delim, char *tokens[],
                 const uint32_t max_token, uint32_t *count);
   void ResetDisplayParams();
-  void SetCSC(LayerCSC source, mdp_color_space *color_space);
-  void SetIGC(const Layer &layer, uint32_t index);
+  void SetCSC(const LayerCSC source, mdp_color_space *color_space);
+  void SetIGC(const Layer *layer, uint32_t index);
 
   bool EnableHotPlugDetection(int enable);
   ssize_t SysFsWrite(const char* file_node, const char* value, ssize_t length);
 
-  // Store the Device EventHandler - used for callback
-  HWEventHandler *event_handler_;
   HWResourceInfo hw_resource_;
   HWPanelInfo hw_panel_info_;
   HWInfoInterface *hw_info_intf_;
diff --git a/msm8996/sdm/libs/core/fb/hw_events.cpp b/msm8996/sdm/libs/core/fb/hw_events.cpp
new file mode 100755
index 0000000..5a656cc
--- /dev/null
+++ b/msm8996/sdm/libs/core/fb/hw_events.cpp
@@ -0,0 +1,260 @@
+/*
+* Copyright (c) 2015 - 2016, 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 <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/prctl.h>
+#include <utils/debug.h>
+#include <utils/sys.h>
+#include <pthread.h>
+#include <algorithm>
+#include <vector>
+#include <map>
+#include <utility>
+
+#include "hw_events.h"
+
+#define __CLASS__ "HWEvents"
+
+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;
+  poll_fd.fd = -1;
+
+  if (!strncmp(event_data->event_name, "thread_exit", strlen("thread_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);
+    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));
+    return poll_fd;
+  }
+
+  // Read once on all fds to clear data on all fds.
+  Sys::pread_(poll_fd.fd, data , kMaxStringLength, 0);
+
+  return poll_fd;
+}
+
+DisplayError HWEvents::SetEventParser(const char *event_name, 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;
+  }
+
+  return error;
+}
+
+void HWEvents::PopulateHWEventData() {
+  for (uint32_t i = 0; i < event_list_->size(); i++) {
+    const char *event_name = event_list_->at(i);
+    HWEventData event_data;
+    event_data.event_name = event_name;
+    SetEventParser(event_name, &event_data);
+    poll_fds_[i] = InitializePollFd(&event_data);
+    event_data_list_.push_back(event_data);
+  }
+}
+
+DisplayError HWEvents::Init(int fb_num, HWEventHandler *event_handler,
+                            std::vector<const char *> *event_list) {
+  if (!event_handler)
+    return kErrorParameters;
+
+  event_handler_ = event_handler;
+  fb_num_ = fb_num;
+  event_list_ = event_list;
+  poll_fds_ = new pollfd[event_list_->size()];
+  event_thread_name_ += " - " + std::to_string(fb_num_);
+
+  PopulateHWEventData();
+
+  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 HWEvents::Deinit() {
+  exit_threads_ = true;
+  Sys::pthread_cancel_(event_thread_);
+
+  uint64_t exit_value = 1;
+  ssize_t write_size = Sys::write_(exit_fd_, &exit_value, sizeof(uint64_t));
+  if (write_size != sizeof(uint64_t))
+    DLOGW("Error triggering exit_fd_ (%d). write size = %d, error = %s", exit_fd_, write_size,
+          strerror(errno));
+
+  pthread_join(event_thread_, NULL);
+
+  for (uint32_t i = 0; i < event_list_->size(); i++) {
+    Sys::close_(poll_fds_[i].fd);
+    poll_fds_[i].fd = -1;
+  }
+
+  delete [] poll_fds_;
+
+  poll_fds_ = 0;
+
+  return kErrorNone;
+}
+
+void* HWEvents::DisplayEventThread(void *context) {
+  if (context) {
+    return reinterpret_cast<HWEvents *>(context)->DisplayEventHandler();
+  }
+
+  return NULL;
+}
+
+void* HWEvents::DisplayEventHandler() {
+  char data[kMaxStringLength] = {0};
+
+  prctl(PR_SET_NAME, event_thread_name_.c_str(), 0, 0, 0);
+  setpriority(PRIO_PROCESS, 0, kThreadPriorityUrgent);
+
+  while (!exit_threads_) {
+    int error = Sys::poll_(poll_fds_, 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++) {
+      pollfd &poll_fd = poll_fds_[event];
+
+      if (!strncmp(event_list_->at(event), "thread_exit", strlen("thread_exit"))) {
+        if ((poll_fd.revents & POLLIN) && (Sys::read_(poll_fd.fd, data, kMaxStringLength) > 0)) {
+          (this->*(event_data_list_[event]).event_parser)(data);
+        }
+      } else {
+        if ((poll_fd.revents & POLLPRI) &&
+                (Sys::pread_(poll_fd.fd, data, kMaxStringLength, 0) > 0)) {
+          (this->*(event_data_list_[event]).event_parser)(data);
+        }
+      }
+    }
+  }
+
+  pthread_exit(0);
+
+  return NULL;
+}
+
+void HWEvents::HandleVSync(char *data) {
+  int64_t timestamp = 0;
+  if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
+    timestamp = strtoll(data + strlen("VSYNC="), NULL, 0);
+  }
+
+  event_handler_->VSync(timestamp);
+}
+
+void HWEvents::HandleIdleTimeout(char *data) {
+  event_handler_->IdleTimeout();
+}
+
+void HWEvents::HandleThermal(char *data) {
+  int64_t thermal_level = 0;
+  if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) {
+    thermal_level = strtoll(data + strlen("thermal_level="), NULL, 0);
+  }
+
+  DLOGI("Received thermal notification with thermal level = %d", thermal_level);
+
+  event_handler_->ThermalEvent(thermal_level);
+}
+
+void HWEvents::HandleCECMessage(char *data) {
+  event_handler_->CECMessage(data);
+}
+
+}  // namespace sdm
+
diff --git a/msm8996/sdm/libs/core/fb/hw_events.h b/msm8996/sdm/libs/core/fb/hw_events.h
new file mode 100755
index 0000000..5d7d23d
--- /dev/null
+++ b/msm8996/sdm/libs/core/fb/hw_events.h
@@ -0,0 +1,82 @@
+/*
+* Copyright (c) 2015 - 2016, 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_H__
+#define __HW_EVENTS_H__
+
+#include <sys/poll.h>
+#include <string>
+#include <vector>
+#include <map>
+#include <utility>
+
+#include "hw_interface.h"
+#include "hw_events_interface.h"
+
+namespace sdm {
+
+class HWEvents : public HWEventsInterface {
+ public:
+  DisplayError Init(int fb_num, HWEventHandler *event_handler,
+                    std::vector<const char *> *event_list);
+  DisplayError Deinit();
+
+ private:
+  static const int kMaxStringLength = 1024;
+
+  typedef void (HWEvents::*EventParser)(char *);
+
+  struct HWEventData {
+    const char* event_name = NULL;
+    EventParser event_parser = NULL;
+  };
+
+  static void* DisplayEventThread(void *context);
+  void* DisplayEventHandler();
+  void HandleVSync(char *data);
+  void HandleBlank(char *data) { }
+  void HandleIdleTimeout(char *data);
+  void HandleThermal(char *data);
+  void HandleCECMessage(char *data);
+  void HandleThreadExit(char *data) { }
+  void PopulateHWEventData();
+  DisplayError SetEventParser(const char *event_name, HWEventData *event_data);
+  pollfd InitializePollFd(HWEventData *event_data);
+
+  HWEventHandler *event_handler_ = NULL;
+  std::vector<const char *> *event_list_ = NULL;
+  std::vector<HWEventData> event_data_list_ = {};
+  pollfd *poll_fds_ = NULL;
+  pthread_t event_thread_;
+  std::string event_thread_name_ = "SDM_EventThread";
+  bool exit_threads_ = false;
+  const char* fb_path_ = "/sys/devices/virtual/graphics/fb";
+  int fb_num_ = -1;
+  int exit_fd_ = -1;
+};
+
+}  // namespace sdm
+
+#endif  // __HW_EVENTS_H__
+
diff --git a/msm8996/sdm/libs/core/fb/hw_hdmi.cpp b/msm8996/sdm/libs/core/fb/hw_hdmi.cpp
index 9f878f3..73a6a3c 100644
--- a/msm8996/sdm/libs/core/fb/hw_hdmi.cpp
+++ b/msm8996/sdm/libs/core/fb/hw_hdmi.cpp
@@ -84,7 +84,7 @@
   HWHDMI *hw_fb_hdmi = NULL;
 
   hw_fb_hdmi = new HWHDMI(buffer_sync_handler, hw_info_intf);
-  error = hw_fb_hdmi->Init(NULL);
+  error = hw_fb_hdmi->Init();
   if (error != kErrorNone) {
     delete hw_fb_hdmi;
   } else {
@@ -108,13 +108,13 @@
   HWDevice::hw_info_intf_ = hw_info_intf;
 }
 
-DisplayError HWHDMI::Init(HWEventHandler *eventhandler) {
+DisplayError HWHDMI::Init() {
   DisplayError error = kErrorNone;
 
   SetSourceProductInformation("vendor_name", "ro.product.manufacturer");
   SetSourceProductInformation("product_description", "ro.product.name");
 
-  error = HWDevice::Init(eventhandler);
+  error = HWDevice::Init();
   if (error != kErrorNone) {
     return error;
   }
diff --git a/msm8996/sdm/libs/core/fb/hw_hdmi.h b/msm8996/sdm/libs/core/fb/hw_hdmi.h
index 09eab23..50bbbaa 100644
--- a/msm8996/sdm/libs/core/fb/hw_hdmi.h
+++ b/msm8996/sdm/libs/core/fb/hw_hdmi.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015 - 2016, 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:
@@ -41,7 +41,7 @@
 
  protected:
   HWHDMI(BufferSyncHandler *buffer_sync_handler, HWInfoInterface *hw_info_intf);
-  virtual DisplayError Init(HWEventHandler *eventhandler);
+  virtual DisplayError Init();
   virtual DisplayError Deinit();
   virtual DisplayError GetNumDisplayAttributes(uint32_t *count);
   // Requirement to call this only after the first config has been explicitly set by client
diff --git a/msm8996/sdm/libs/core/fb/hw_info.cpp b/msm8996/sdm/libs/core/fb/hw_info.cpp
index 7b707c1..f6f664a 100644
--- a/msm8996/sdm/libs/core/fb/hw_info.cpp
+++ b/msm8996/sdm/libs/core/fb/hw_info.cpp
@@ -235,6 +235,12 @@
         hw_resource->scale_factor = UINT32(atoi(tokens[1]));
       } else if (!strncmp(tokens[0], "xtra_ff_factor", strlen("xtra_ff_factor"))) {
         hw_resource->extra_fudge_factor = UINT32(atoi(tokens[1]));
+      } else if (!strncmp(tokens[0], "amortizable_threshold", strlen("amortizable_threshold"))) {
+        hw_resource->amortizable_threshold = UINT32(atoi(tokens[1]));
+      } else if (!strncmp(tokens[0], "system_overhead_lines", strlen("system_overhead_lines"))) {
+        hw_resource->system_overhead_lines = UINT32(atoi(tokens[1]));
+      } else if (!strncmp(tokens[0], "wb_intf_index", strlen("wb_intf_index"))) {
+        hw_resource->writeback_index = UINT32(atoi(tokens[1]));
       } else if (!strncmp(tokens[0], "features", strlen("features"))) {
         for (uint32_t i = 0; i < token_count; i++) {
           if (!strncmp(tokens[i], "bwc", strlen("bwc"))) {
@@ -257,6 +263,8 @@
             hw_resource->separate_rotator = true;
           } else if (!strncmp(tokens[i], "qseed3", strlen("qseed3"))) {
             hw_resource->has_qseed3 = true;
+          } else if (!strncmp(tokens[i], "concurrent_writeback", strlen("concurrent_writeback"))) {
+            hw_resource->has_concurrent_writeback = true;
           }
         }
       } else if (!strncmp(tokens[0], "pipe_count", strlen("pipe_count"))) {
@@ -315,9 +323,10 @@
         hw_resource->num_vig_pipe, hw_resource->num_dma_pipe, hw_resource->num_cursor_pipe);
   DLOGI("Upscale Ratio = %d, Downscale Ratio = %d, Blending Stages = %d", hw_resource->max_scale_up,
         hw_resource->max_scale_down, hw_resource->num_blending_stages);
-  DLOGI("BWC = %d, UBWC = %d, Decimation = %d, Tile Format = %d", hw_resource->has_bwc,
-        hw_resource->has_ubwc, hw_resource->has_decimation, hw_resource->has_macrotile);
   DLOGI("SourceSplit = %d QSEED3 = %d", hw_resource->is_src_split, hw_resource->has_qseed3);
+  DLOGI("BWC = %d, UBWC = %d, Decimation = %d, Tile Format = %d Concurrent Writeback = %d",
+        hw_resource->has_bwc, hw_resource->has_ubwc, hw_resource->has_decimation,
+        hw_resource->has_macrotile, hw_resource->has_concurrent_writeback);
   DLOGI("MaxLowBw = %" PRIu64 " , MaxHighBw = % " PRIu64 "", hw_resource->max_bandwidth_low,
         hw_resource->max_bandwidth_high);
   DLOGI("MaxPipeBw = %" PRIu64 " KBps, MaxSDEClock = % " PRIu64 " Hz, ClockFudgeFactor = %f",
@@ -330,6 +339,12 @@
     GetHWRotatorInfo(hw_resource);
   }
 
+  // If the driver doesn't spell out the wb index, assume it to be the number of rotators,
+  // based on legacy implementation.
+  if (hw_resource->writeback_index == kHWBlockMax) {
+    hw_resource->writeback_index = hw_resource->hw_rot_info.num_rotator;
+  }
+
   if (hw_resource->has_dyn_bw_support) {
     DisplayError ret = GetDynamicBWLimits(hw_resource);
     if (ret != kErrorNone) {
@@ -509,5 +524,57 @@
   hw_resource->supported_formats_map.insert(make_pair(sub_blk_type, supported_sdm_formats));
 }
 
+DisplayError HWInfo::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
+  char *stringbuffer = reinterpret_cast<char *>(malloc(kMaxStringLength));
+  if (stringbuffer == NULL) {
+    DLOGE("Failed to allocate Stringbuffer");
+    return kErrorMemory;
+  }
+
+  char *line = stringbuffer;
+  size_t len = kMaxStringLength;
+  ssize_t read;
+
+  FILE *fileptr = Sys::fopen_("/sys/class/graphics/fb0/msm_fb_type", "r");
+  if (!fileptr) {
+    free(stringbuffer);
+    return kErrorHardware;
+  }
+
+  if ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
+    if (!strncmp(line, "dtv panel", strlen("dtv panel"))) {
+      hw_disp_info->type = kHDMI;
+      DLOGI("First display is HDMI");
+    } else {
+      hw_disp_info->type = kPrimary;
+      DLOGI("First display is internal display");
+    }
+  } else {
+    free(stringbuffer);
+    fclose(fileptr);
+    return kErrorHardware;
+  }
+
+  fclose(fileptr);
+
+  fileptr = Sys::fopen_("/sys/class/graphics/fb0/connected", "r");
+  if (!fileptr) {
+    // If fb0 is for a DSI/connected panel, then connected node will not exist
+    hw_disp_info->is_connected = true;
+  } else {
+    if ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
+        hw_disp_info->is_connected =  (!strncmp(line, "1", strlen("1")));
+    } else {
+        fclose(fileptr);
+        free(stringbuffer);
+        return kErrorHardware;
+    }
+    fclose(fileptr);
+  }
+
+  free(stringbuffer);
+  return kErrorNone;
+}
+
 }  // namespace sdm
 
diff --git a/msm8996/sdm/libs/core/fb/hw_info.h b/msm8996/sdm/libs/core/fb/hw_info.h
index ba98f87..f22acb0 100644
--- a/msm8996/sdm/libs/core/fb/hw_info.h
+++ b/msm8996/sdm/libs/core/fb/hw_info.h
@@ -26,6 +26,7 @@
 #define __HW_INFO_H__
 
 #include <core/sdm_types.h>
+#include <core/core_interface.h>
 #include <private/hw_info_types.h>
 #include <linux/msm_mdp.h>
 #include "hw_info_interface.h"
@@ -39,6 +40,7 @@
 class HWInfo: public HWInfoInterface {
  public:
   virtual DisplayError GetHWResourceInfo(HWResourceInfo *hw_resource);
+  virtual DisplayError GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info);
 
  private:
   virtual DisplayError GetHWRotatorInfo(HWResourceInfo *hw_resource);
diff --git a/msm8996/sdm/libs/core/fb/hw_primary.cpp b/msm8996/sdm/libs/core/fb/hw_primary.cpp
index e8ab7cf..84eea2a 100644
--- a/msm8996/sdm/libs/core/fb/hw_primary.cpp
+++ b/msm8996/sdm/libs/core/fb/hw_primary.cpp
@@ -39,6 +39,7 @@
 #include <utils/debug.h>
 #include <utils/sys.h>
 #include <core/display_interface.h>
+#include <linux/msm_mdp_ext.h>
 
 #include <string>
 
@@ -47,18 +48,25 @@
 
 #define __CLASS__ "HWPrimary"
 
+#ifndef MDP_COMMIT_CWB_EN
+#define MDP_COMMIT_CWB_EN 0x800
+#endif
+
+#ifndef MDP_COMMIT_CWB_DSPP
+#define MDP_COMMIT_CWB_DSPP 0x1000
+#endif
+
 namespace sdm {
 
 using std::string;
 
 DisplayError HWPrimary::Create(HWInterface **intf, HWInfoInterface *hw_info_intf,
-                               BufferSyncHandler *buffer_sync_handler,
-                               HWEventHandler *eventhandler) {
+                               BufferSyncHandler *buffer_sync_handler) {
   DisplayError error = kErrorNone;
   HWPrimary *hw_primary = NULL;
 
   hw_primary = new HWPrimary(buffer_sync_handler, hw_info_intf);
-  error = hw_primary->Init(eventhandler);
+  error = hw_primary->Init();
   if (error != kErrorNone) {
     delete hw_primary;
   } else {
@@ -83,53 +91,17 @@
   HWDevice::hw_info_intf_ = hw_info_intf;
 }
 
-DisplayError HWPrimary::Init(HWEventHandler *eventhandler) {
+DisplayError HWPrimary::Init() {
   DisplayError error = kErrorNone;
-  char node_path[kMaxStringLength] = { 0 };
-  char data[kMaxStringLength] = { 0 };
-  const char *event_name[kNumDisplayEvents] = {"vsync_event", "show_blank_event", "idle_notify",
-                                               "msm_fb_thermal_level"};
 
-  error = HWDevice::Init(eventhandler);
+  error = HWDevice::Init();
   if (error != kErrorNone) {
-    goto CleanupOnError;
+    return error;
   }
 
   error = PopulateDisplayAttributes();
   if (error != kErrorNone) {
-    goto CleanupOnError;
-  }
-
-  // Open nodes for polling
-  for (int event = 0; event < kNumDisplayEvents; event++) {
-    poll_fds_[event].fd = -1;
-  }
-
-  if (!fake_vsync_) {
-    for (int event = 0; event < kNumDisplayEvents; event++) {
-      pollfd &poll_fd = poll_fds_[event];
-
-      snprintf(node_path, sizeof(node_path), "%s%d/%s", fb_path_, fb_node_index_,
-               event_name[event]);
-
-      poll_fd.fd = Sys::open_(node_path, O_RDONLY);
-      if (poll_fd.fd < 0) {
-        DLOGE("open failed for event=%d, error=%s", event, strerror(errno));
-        error = kErrorHardware;
-        goto CleanupOnError;
-      }
-
-      // Read once on all fds to clear data on all fds.
-      Sys::pread_(poll_fd.fd, data , kMaxStringLength, 0);
-      poll_fd.events = POLLPRI | POLLERR;
-    }
-  }
-
-  // Start the Event thread
-  if (pthread_create(&event_thread_, NULL, &DisplayEventThread, this) < 0) {
-    DLOGE("Failed to start %s, error = %s", event_thread_name_);
-    error = kErrorResources;
-    goto CleanupOnError;
+    return error;
   }
 
   // Disable HPD at start if HDMI is external, it will be enabled later when the display powers on
@@ -137,17 +109,6 @@
   EnableHotPlugDetection(0);
   InitializeConfigs();
 
-  return kErrorNone;
-
-CleanupOnError:
-  // Close all poll fds
-  for (int event = 0; event < kNumDisplayEvents; event++) {
-    int &fd = poll_fds_[event].fd;
-    if (fd >= 0) {
-      Sys::close_(fd);
-    }
-  }
-
   return error;
 }
 
@@ -238,17 +199,6 @@
 }
 
 DisplayError HWPrimary::Deinit() {
-  exit_threads_ = true;
-  Sys::pthread_cancel_(event_thread_);
-  pthread_join(event_thread_, NULL);
-
-  for (int event = 0; event < kNumDisplayEvents; event++) {
-    int &fd = poll_fds_[event].fd;
-    if (fd >= 0) {
-      Sys::close_(fd);
-    }
-  }
-
   return HWDevice::Deinit();
 }
 
@@ -432,6 +382,8 @@
 }
 
 DisplayError HWPrimary::Validate(HWLayers *hw_layers) {
+  LayerStack *stack = hw_layers->info.stack;
+
   HWDevice::ResetDisplayParams();
 
   mdp_layer_commit_v1 &mdp_commit = mdp_disp_commit_.commit_v1;
@@ -453,96 +405,58 @@
     mdp_commit.right_roi.h = UINT32(right_roi.bottom - right_roi.top);
   }
 
+  if (stack->output_buffer && hw_resource_.has_concurrent_writeback) {
+    LayerBuffer *output_buffer = stack->output_buffer;
+    mdp_out_layer_.writeback_ndx = hw_resource_.writeback_index;
+    mdp_out_layer_.buffer.width = output_buffer->width;
+    mdp_out_layer_.buffer.height = output_buffer->height;
+    mdp_out_layer_.buffer.comp_ratio.denom = 1000;
+    mdp_out_layer_.buffer.comp_ratio.numer = UINT32(hw_layers->output_compression * 1000);
+    mdp_out_layer_.buffer.fence = -1;
+    SetFormat(output_buffer->format, &mdp_out_layer_.buffer.format);
+    mdp_commit.flags |= MDP_COMMIT_CWB_EN;
+    mdp_commit.flags |= (stack->flags.post_processed_output) ? MDP_COMMIT_CWB_DSPP : 0;
+    DLOGI_IF(kTagDriverConfig, "****************** Conc WB Output buffer Info ******************");
+    DLOGI_IF(kTagDriverConfig, "out_w %d, out_h %d, out_f %d, wb_id %d DSPP output %d",
+             mdp_out_layer_.buffer.width, mdp_out_layer_.buffer.height,
+             mdp_out_layer_.buffer.format, mdp_out_layer_.writeback_ndx,
+             stack->flags.post_processed_output);
+    DLOGI_IF(kTagDriverConfig, "****************************************************************");
+  }
+
   return HWDevice::Validate(hw_layers);
 }
 
-void* HWPrimary::DisplayEventThread(void *context) {
-  if (context) {
-    return reinterpret_cast<HWPrimary *>(context)->DisplayEventThreadHandler();
-  }
+DisplayError HWPrimary::Commit(HWLayers *hw_layers) {
+  LayerBuffer *output_buffer = hw_layers->info.stack->output_buffer;
 
-  return NULL;
-}
+  if (hw_resource_.has_concurrent_writeback && output_buffer) {
+    if (output_buffer->planes[0].fd >= 0) {
+      mdp_out_layer_.buffer.planes[0].fd = output_buffer->planes[0].fd;
+      mdp_out_layer_.buffer.planes[0].offset = output_buffer->planes[0].offset;
+      SetStride(device_type_, output_buffer->format, output_buffer->planes[0].stride,
+                &mdp_out_layer_.buffer.planes[0].stride);
+      mdp_out_layer_.buffer.plane_count = 1;
+      mdp_out_layer_.buffer.fence = -1;
 
-void* HWPrimary::DisplayEventThreadHandler() {
-  char data[kMaxStringLength] = {0};
-
-  prctl(PR_SET_NAME, event_thread_name_, 0, 0, 0);
-  setpriority(PRIO_PROCESS, 0, kThreadPriorityUrgent);
-
-  if (fake_vsync_) {
-    while (!exit_threads_) {
-      // Fake vsync is used only when set explicitly through a property(todo) or when
-      // the vsync timestamp node cannot be opened at bootup. There is no
-      // fallback to fake vsync from the true vsync loop, ever, as the
-      // condition can easily escape detection.
-      // Also, fake vsync is delivered only for the primary display.
-      usleep(16666);
-      STRUCT_VAR(timeval, time_now);
-      gettimeofday(&time_now, NULL);
-      int64_t ts = int64_t(time_now.tv_sec)*1000000000LL +int64_t(time_now.tv_usec)*1000LL;
-
-      // Send Vsync event for primary display(0)
-      event_handler_->VSync(ts);
-    }
-
-    pthread_exit(0);
-  }
-
-  typedef void (HWPrimary::*EventHandler)(char*);
-  EventHandler event_handler[kNumDisplayEvents] = { &HWPrimary::HandleVSync,
-                                                    &HWPrimary::HandleBlank,
-                                                    &HWPrimary::HandleIdleTimeout,
-                                                    &HWPrimary::HandleThermal };
-
-  while (!exit_threads_) {
-    int error = Sys::poll_(poll_fds_, kNumDisplayEvents, -1);
-    if (error <= 0) {
-      DLOGW("poll failed. error = %s", strerror(errno));
-      continue;
-    }
-
-    for (int event = 0; event < kNumDisplayEvents; event++) {
-      pollfd &poll_fd = poll_fds_[event];
-
-      if (poll_fd.revents & POLLPRI) {
-        if (Sys::pread_(poll_fd.fd, data, kMaxStringLength, 0) > 0) {
-          (this->*event_handler[event])(data);
-        }
-      }
+      DLOGI_IF(kTagDriverConfig, "****************** Conc WB Output buffer Info ****************");
+      DLOGI_IF(kTagDriverConfig, "out_fd %d, out_offset %d, out_stride %d",
+               mdp_out_layer_.buffer.planes[0].fd, mdp_out_layer_.buffer.planes[0].offset,
+               mdp_out_layer_.buffer.planes[0].stride);
+      DLOGI_IF(kTagDriverConfig, "**************************************************************");
+    } else {
+      DLOGE("Invalid output buffer fd");
+      return kErrorParameters;
     }
   }
 
-  pthread_exit(0);
+  DisplayError ret = HWDevice::Commit(hw_layers);
 
-  return NULL;
-}
-
-void HWPrimary::HandleVSync(char *data) {
-  int64_t timestamp = 0;
-  if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
-    timestamp = strtoll(data + strlen("VSYNC="), NULL, 0);
-  }
-  event_handler_->VSync(timestamp);
-}
-
-void HWPrimary::HandleBlank(char *data) {
-  // TODO(user): Need to send blank Event
-}
-
-void HWPrimary::HandleIdleTimeout(char *data) {
-  event_handler_->IdleTimeout();
-}
-
-void HWPrimary::HandleThermal(char *data) {
-  int64_t thermal_level = 0;
-  if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) {
-    thermal_level = strtoll(data + strlen("thermal_level="), NULL, 0);
+  if (ret == kErrorNone && hw_resource_.has_concurrent_writeback && output_buffer) {
+    output_buffer->release_fence_fd = mdp_out_layer_.buffer.fence;
   }
 
-  DLOGI("Received thermal notification with thermal level = %d", thermal_level);
-
-  event_handler_->ThermalEvent(thermal_level);
+  return ret;
 }
 
 void HWPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
@@ -573,14 +487,7 @@
 
 DisplayError HWPrimary::SetVSyncState(bool enable) {
   DTRACE_SCOPED();
-
-  int vsync_on = enable ? 1 : 0;
-  if (Sys::ioctl_(device_fd_, MSMFB_OVERLAY_VSYNC_CTRL, &vsync_on) < 0) {
-    IOCTL_LOGE(MSMFB_OVERLAY_VSYNC_CTRL, device_type_);
-    return kErrorHardware;
-  }
-
-  return kErrorNone;
+  return HWDevice::SetVSyncState(enable);
 }
 
 DisplayError HWPrimary::SetDisplayMode(const HWDisplayMode hw_display_mode) {
diff --git a/msm8996/sdm/libs/core/fb/hw_primary.h b/msm8996/sdm/libs/core/fb/hw_primary.h
index 9e2cea3..999c41a 100644
--- a/msm8996/sdm/libs/core/fb/hw_primary.h
+++ b/msm8996/sdm/libs/core/fb/hw_primary.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015-2016, 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:
@@ -38,12 +38,12 @@
 class HWPrimary : public HWDevice {
  public:
   static DisplayError Create(HWInterface **intf, HWInfoInterface *hw_info_intf,
-                             BufferSyncHandler *buffer_sync_handler, HWEventHandler *eventhandler);
+                             BufferSyncHandler *buffer_sync_handler);
   static DisplayError Destroy(HWInterface *intf);
 
  protected:
   HWPrimary(BufferSyncHandler *buffer_sync_handler, HWInfoInterface *hw_info_intf);
-  virtual DisplayError Init(HWEventHandler *eventhandler);
+  virtual DisplayError Init();
   virtual DisplayError Deinit();
   virtual DisplayError GetNumDisplayAttributes(uint32_t *count);
   virtual DisplayError GetActiveConfig(uint32_t *active_config);
@@ -55,6 +55,7 @@
   virtual DisplayError Doze();
   virtual DisplayError DozeSuspend();
   virtual DisplayError Validate(HWLayers *hw_layers);
+  virtual DisplayError Commit(HWLayers *hw_layers);
   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
   virtual DisplayError SetVSyncState(bool enable);
   virtual DisplayError SetDisplayMode(const HWDisplayMode hw_display_mode);
@@ -72,24 +73,11 @@
     kModeLPMCommand,
   };
 
-  // Event Thread to receive vsync/blank events
-  static void* DisplayEventThread(void *context);
-  void* DisplayEventThreadHandler();
-
-  void HandleVSync(char *data);
-  void HandleBlank(char *data);
-  void HandleIdleTimeout(char *data);
-  void HandleThermal(char *data);
   DisplayError PopulateDisplayAttributes();
   void InitializeConfigs();
   bool IsResolutionSwitchEnabled() { return !display_configs_.empty(); }
   bool GetCurrentModeFromSysfs(size_t *curr_x_pixels, size_t *curr_y_pixels);
 
-  pollfd poll_fds_[kNumDisplayEvents];
-  pthread_t event_thread_;
-  const char *event_thread_name_ = "SDM_EventThread";
-  bool fake_vsync_ = false;
-  bool exit_threads_ = false;
   HWDisplayAttributes display_attributes_;
   std::vector<DisplayConfigVariableInfo> display_configs_;
   std::vector<std::string> display_config_strings_;
diff --git a/msm8996/sdm/libs/core/fb/hw_virtual.cpp b/msm8996/sdm/libs/core/fb/hw_virtual.cpp
index 033159f..662c458 100644
--- a/msm8996/sdm/libs/core/fb/hw_virtual.cpp
+++ b/msm8996/sdm/libs/core/fb/hw_virtual.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015 - 2016, 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
@@ -40,7 +40,7 @@
   HWVirtual *hw_virtual = NULL;
 
   hw_virtual = new HWVirtual(buffer_sync_handler, hw_info_intf);
-  error = hw_virtual->Init(NULL);
+  error = hw_virtual->Init();
   if (error != kErrorNone) {
     delete hw_virtual;
   } else {
@@ -65,8 +65,8 @@
   HWDevice::hw_info_intf_ = hw_info_intf;
 }
 
-DisplayError HWVirtual::Init(HWEventHandler *eventhandler) {
-  return HWDevice::Init(eventhandler);
+DisplayError HWVirtual::Init() {
+  return HWDevice::Init();
 }
 
 DisplayError HWVirtual::Validate(HWLayers *hw_layers) {
diff --git a/msm8996/sdm/libs/core/fb/hw_virtual.h b/msm8996/sdm/libs/core/fb/hw_virtual.h
index 2b740d5..330a067 100644
--- a/msm8996/sdm/libs/core/fb/hw_virtual.h
+++ b/msm8996/sdm/libs/core/fb/hw_virtual.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015 - 2016, 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:
@@ -34,10 +34,11 @@
   static DisplayError Create(HWInterface **intf, HWInfoInterface *hw_info_intf,
                              BufferSyncHandler *buffer_sync_handler);
   static DisplayError Destroy(HWInterface *intf);
+  virtual DisplayError SetVSyncState(bool enable) { return kErrorNotSupported; }
 
  protected:
   HWVirtual(BufferSyncHandler *buffer_sync_handler, HWInfoInterface *hw_info_intf);
-  virtual DisplayError Init(HWEventHandler *eventhandler);
+  virtual DisplayError Init();
   virtual DisplayError Validate(HWLayers *hw_layers);
 };
 
diff --git a/msm8996/sdm/libs/core/hw_events_interface.h b/msm8996/sdm/libs/core/hw_events_interface.h
new file mode 100644
index 0000000..a133a3a
--- /dev/null
+++ b/msm8996/sdm/libs/core/hw_events_interface.h
@@ -0,0 +1,48 @@
+/*
+* Copyright (c) 2016, 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_INTERFACE_H__
+#define __HW_EVENTS_INTERFACE_H__
+
+#include <private/hw_info_types.h>
+#include <inttypes.h>
+#include <utility>
+#include <vector>
+
+namespace sdm {
+
+class HWEventsInterface {
+ public:
+  static DisplayError Create(int fb_num, HWEventHandler *event_handler,
+                             std::vector<const char *> *event_list, HWEventsInterface **intf);
+  static DisplayError Destroy(HWEventsInterface *intf);
+
+ protected:
+  virtual ~HWEventsInterface() { }
+};
+
+}  // namespace sdm
+
+#endif  // __HW_EVENTS_INTERFACE_H__
+
diff --git a/msm8996/sdm/libs/core/hw_info_interface.h b/msm8996/sdm/libs/core/hw_info_interface.h
index 014ab0d..401c8bf 100644
--- a/msm8996/sdm/libs/core/hw_info_interface.h
+++ b/msm8996/sdm/libs/core/hw_info_interface.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -26,6 +26,7 @@
 #define __HW_INFO_INTERFACE_H__
 
 #include <inttypes.h>
+#include <core/core_interface.h>
 #include <private/hw_info_types.h>
 
 namespace sdm {
@@ -35,6 +36,7 @@
   static DisplayError Create(HWInfoInterface **intf);
   static DisplayError Destroy(HWInfoInterface *intf);
   virtual DisplayError GetHWResourceInfo(HWResourceInfo *hw_resource) = 0;
+  virtual DisplayError GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) = 0;
 
  protected:
   virtual ~HWInfoInterface() { }
diff --git a/msm8996/sdm/libs/core/hw_interface.h b/msm8996/sdm/libs/core/hw_interface.h
index 7779350..75d92b9 100644
--- a/msm8996/sdm/libs/core/hw_interface.h
+++ b/msm8996/sdm/libs/core/hw_interface.h
@@ -56,6 +56,8 @@
   virtual DisplayError Blank(bool blank) = 0;
   virtual void IdleTimeout() = 0;
   virtual void ThermalEvent(int64_t thermal_level) = 0;
+  virtual void CECMessage(char *message) = 0;
+
  protected:
   virtual ~HWEventHandler() { }
 };
diff --git a/msm8996/sdm/libs/core/resource_default.cpp b/msm8996/sdm/libs/core/resource_default.cpp
index 9a6eb52..e13150b 100644
--- a/msm8996/sdm/libs/core/resource_default.cpp
+++ b/msm8996/sdm/libs/core/resource_default.cpp
@@ -27,6 +27,7 @@
 #include <utils/debug.h>
 #include <utils/rect.h>
 #include <utils/formats.h>
+#include <utils/sys.h>
 #include <dlfcn.h>
 
 #include "resource_default.h"
@@ -198,9 +199,9 @@
     return kErrorResources;
   }
 
-  Layer &layer = layer_info.stack->layers[layer_info.index[0]];
+  Layer *layer = layer_info.stack->layers.at(layer_info.index[0]);
 
-  if (layer.composition != kCompositionGPUTarget) {
+  if (layer->composition != kCompositionGPUTarget) {
     DLOGV_IF(kTagResources, "Not an FB layer");
     return kErrorParameters;
   }
@@ -311,6 +312,9 @@
     }
   }
 
+  if (hw_layers->info.sync_handle >= 0)
+    Sys::close_(hw_layers->info.sync_handle);
+
   display_resource_ctx->frame_count++;
 
   return kErrorNone;
@@ -495,7 +499,7 @@
                                 HWLayers *hw_layers) {
   HWLayersInfo &layer_info = hw_layers->info;
   DisplayError error = kErrorNone;
-  Layer& layer = layer_info.stack->layers[layer_info.index[0]];
+  Layer *layer = layer_info.stack->layers.at(layer_info.index[0]);
 
   error = ValidateLayerParams(layer);
   if (error != kErrorNone) {
@@ -506,15 +510,15 @@
   HWPipeInfo &left_pipe = layer_config->left_pipe;
   HWPipeInfo &right_pipe = layer_config->right_pipe;
 
-  LayerRect src_rect = layer.src_rect;
-  LayerRect dst_rect = layer.dst_rect;
+  LayerRect src_rect = layer->src_rect;
+  LayerRect dst_rect = layer->dst_rect;
 
   error = ValidateDimensions(src_rect, dst_rect);
   if (error != kErrorNone) {
     return error;
   }
 
-  bool ubwc_tiled = IsUBWCFormat(layer.input_buffer->format);
+  bool ubwc_tiled = IsUBWCFormat(layer->input_buffer->format);
   error = ValidateScaling(src_rect, dst_rect, false /*rotated90 */, ubwc_tiled,
                           false /* use_rotator_downscale */);
   if (error != kErrorNone) {
@@ -540,8 +544,8 @@
   left_pipe.z_order = 0;
 
   DLOGV_IF(kTagResources, "==== FB layer Config ====");
-  Log(kTagResources, "input layer src_rect", layer.src_rect);
-  Log(kTagResources, "input layer dst_rect", layer.dst_rect);
+  Log(kTagResources, "input layer src_rect", layer->src_rect);
+  Log(kTagResources, "input layer dst_rect", layer->dst_rect);
   Log(kTagResources, "cropped src_rect", src_rect);
   Log(kTagResources, "cropped dst_rect", dst_rect);
   Log(kTagResources, "left pipe src", layer_config->left_pipe.src_roi);
@@ -618,10 +622,10 @@
     return false;
 }
 
-DisplayError ResourceDefault::ValidateLayerParams(const Layer &layer) {
-  const LayerRect &src = layer.src_rect;
-  const LayerRect &dst = layer.dst_rect;
-  LayerBuffer *input_buffer = layer.input_buffer;
+DisplayError ResourceDefault::ValidateLayerParams(const Layer *layer) {
+  const LayerRect &src = layer->src_rect;
+  const LayerRect &dst = layer->dst_rect;
+  const LayerBuffer *input_buffer = layer->input_buffer;
 
   if (input_buffer->format == kFormatInvalid) {
     DLOGV_IF(kTagResources, "Invalid input buffer format %d", input_buffer->format);
@@ -832,15 +836,15 @@
   dst_right->right = dst_rect.right;
 }
 
-DisplayError ResourceDefault::AlignPipeConfig(const Layer &layer, HWPipeInfo *left_pipe,
-                                         HWPipeInfo *right_pipe) {
+DisplayError ResourceDefault::AlignPipeConfig(const Layer *layer, HWPipeInfo *left_pipe,
+                                              HWPipeInfo *right_pipe) {
   DisplayError error = kErrorNone;
   if (!left_pipe->valid) {
     DLOGE_IF(kTagResources, "left_pipe should not be invalid");
     return kErrorNotSupported;
   }
 
-  bool ubwc_tiled = IsUBWCFormat(layer.input_buffer->format);
+  bool ubwc_tiled = IsUBWCFormat(layer->input_buffer->format);
   error = ValidatePipeParams(left_pipe, ubwc_tiled);
   if (error != kErrorNone) {
     goto PipeConfigExit;
@@ -879,7 +883,7 @@
   return kErrorNone;
 }
 
-DisplayError ResourceDefault::ValidateCursorConfig(Handle display_ctx, const Layer& layer,
+DisplayError ResourceDefault::ValidateCursorConfig(Handle display_ctx, const Layer *layer,
                                                    bool is_top) {
   return kErrorNotSupported;
 }
diff --git a/msm8996/sdm/libs/core/resource_default.h b/msm8996/sdm/libs/core/resource_default.h
index 23d7dfa..ce6dd5f 100644
--- a/msm8996/sdm/libs/core/resource_default.h
+++ b/msm8996/sdm/libs/core/resource_default.h
@@ -51,7 +51,7 @@
   virtual DisplayError SetMaxMixerStages(Handle display_ctx, uint32_t max_mixer_stages);
   virtual DisplayError ValidateScaling(const LayerRect &crop, const LayerRect &dst,
                                        bool rotate90, bool ubwc_tiled, bool use_rotator_downscale);
-  DisplayError ValidateCursorConfig(Handle display_ctx, const Layer& layer, bool is_top);
+  DisplayError ValidateCursorConfig(Handle display_ctx, const Layer *layer, bool is_top);
   DisplayError ValidateCursorPosition(Handle display_ctx, HWLayers *hw_layers, int x, int y);
   DisplayError SetMaxBandwidthMode(HWBwModes mode);
 
@@ -105,7 +105,7 @@
                              const LayerRect &src_rect, const LayerRect &dst_rect,
                              HWLayerConfig *layer_config);
   bool CalculateCropRects(const LayerRect &scissor, LayerRect *crop, LayerRect *dst);
-  DisplayError ValidateLayerParams(const Layer &layer);
+  DisplayError ValidateLayerParams(const Layer *layer);
   DisplayError ValidateDimensions(const LayerRect &crop, const LayerRect &dst);
   DisplayError ValidatePipeParams(HWPipeInfo *pipe_info, bool ubwc_tiled);
   DisplayError ValidateDownScaling(float scale_x, float scale_y, bool ubwc_tiled);
@@ -115,7 +115,8 @@
   DisplayError SetDecimationFactor(HWPipeInfo *pipe);
   void SplitRect(const LayerRect &src_rect, const LayerRect &dst_rect, LayerRect *src_left,
                 LayerRect *dst_left, LayerRect *src_right, LayerRect *dst_right);
-  DisplayError AlignPipeConfig(const Layer &layer, HWPipeInfo *left_pipe, HWPipeInfo *right_pipe);
+  DisplayError AlignPipeConfig(const Layer *layer, HWPipeInfo *left_pipe,
+                               HWPipeInfo *right_pipe);
   void ResourceStateLog(void);
   DisplayError CalculateDecimation(float downscale, uint8_t *decimation);
   DisplayError GetScaleLutConfig(HWScaleLutInfo *lut_info);
diff --git a/msm8996/sdm/libs/core/strategy.cpp b/msm8996/sdm/libs/core/strategy.cpp
index 924047a..0531693 100644
--- a/msm8996/sdm/libs/core/strategy.cpp
+++ b/msm8996/sdm/libs/core/strategy.cpp
@@ -78,14 +78,16 @@
 
   uint32_t i = 0;
   LayerStack *layer_stack = hw_layers_info_->stack;
-  for (; i < layer_stack->layer_count; i++) {
-    if (layer_stack->layers[i].composition == kCompositionGPUTarget) {
+  uint32_t layer_count = UINT32(layer_stack->layers.size());
+
+  for (; i < layer_count; i++) {
+    if (layer_stack->layers.at(i)->composition == kCompositionGPUTarget) {
       fb_layer_index_ = i;
       break;
     }
   }
 
-  if (i == layer_stack->layer_count) {
+  if (i == layer_count) {
     return kErrorUndefined;
   }
 
@@ -136,11 +138,12 @@
   uint32_t &hw_layer_count = hw_layers_info_->count;
   hw_layer_count = 0;
 
-  for (uint32_t i = 0; i < layer_stack->layer_count; i++) {
-    LayerComposition &composition = layer_stack->layers[i].composition;
+  for (uint32_t i = 0; i < layer_stack->layers.size(); i++) {
+    Layer *layer = layer_stack->layers.at(i);
+    LayerComposition &composition = layer->composition;
     if (composition == kCompositionGPUTarget) {
-      hw_layers_info_->updated_src_rect[hw_layer_count] = layer_stack->layers[i].src_rect;
-      hw_layers_info_->updated_dst_rect[hw_layer_count] = layer_stack->layers[i].dst_rect;
+      hw_layers_info_->updated_src_rect[hw_layer_count] = layer->src_rect;
+      hw_layers_info_->updated_dst_rect[hw_layer_count] = layer->dst_rect;
       hw_layers_info_->index[hw_layer_count++] = i;
     } else if (composition != kCompositionBlitTarget) {
       composition = kCompositionGPU;
diff --git a/msm8996/sdm/libs/hwc/Android.mk b/msm8996/sdm/libs/hwc/Android.mk
index 8ce3757..259a727 100644
--- a/msm8996/sdm/libs/hwc/Android.mk
+++ b/msm8996/sdm/libs/hwc/Android.mk
@@ -1,6 +1,7 @@
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 include $(LOCAL_PATH)/../../../common.mk
+ifeq ($(use_hwc2),false)
 
 LOCAL_MODULE                  := hwcomposer.$(TARGET_BOARD_PLATFORM)
 LOCAL_MODULE_RELATIVE_PATH    := hw
@@ -18,6 +19,7 @@
 
 LOCAL_SRC_FILES               := hwc_session.cpp \
                                  hwc_display.cpp \
+                                 hwc_display_null.cpp \
                                  hwc_display_primary.cpp \
                                  hwc_display_external.cpp \
                                  hwc_display_virtual.cpp \
@@ -29,3 +31,4 @@
                                  cpuhint.cpp
 
 include $(BUILD_SHARED_LIBRARY)
+endif
diff --git a/msm8996/sdm/libs/hwc/blit_engine_c2d.cpp b/msm8996/sdm/libs/hwc/blit_engine_c2d.cpp
index e855524..b55b06b 100644
--- a/msm8996/sdm/libs/hwc/blit_engine_c2d.cpp
+++ b/msm8996/sdm/libs/hwc/blit_engine_c2d.cpp
@@ -101,8 +101,6 @@
     blit_target_buffer_[i] = NULL;
     release_fence_fd_[i] = -1;
   }
-
-  HWCDebugHandler::Get()->GetProperty("persist.hwc.blit.comp", &blit_supported_);
 }
 
 BlitEngineC2d::~BlitEngineC2d() {
@@ -114,10 +112,6 @@
 }
 
 int BlitEngineC2d::Init() {
-  if (!blit_supported_) {
-    return -1;
-  }
-
   hw_module_t const *module;
   if (hw_get_module("copybit", &module) == 0) {
     if (copybit_open(module, &blit_engine_c2d_) < 0) {
@@ -211,17 +205,18 @@
   int fd = -1;
 
   for (uint32_t i = blit_target_start_index_-2; (i > 0) && (count < num_blit_target_); i--) {
-    Layer &layer = layer_stack->layers[i];
-    LayerBuffer *layer_buffer = layer.input_buffer;
-    if (layer.composition == kCompositionBlit) {
+    Layer *layer = layer_stack->layers.at(i);
+    LayerBuffer *layer_buffer = layer->input_buffer;
+    if (layer->composition == kCompositionBlit) {
       int index = blit_target_start_index_ + count;
-      layer_buffer->release_fence_fd = layer_stack->layers[index].input_buffer->release_fence_fd;
+      layer_buffer->release_fence_fd =
+        layer_stack->layers.at(index)->input_buffer->release_fence_fd;
       fence_fd = layer_buffer->release_fence_fd;
       close(layer_buffer->acquire_fence_fd);
       layer_buffer->acquire_fence_fd = -1;
-      layer_stack->layers[index].input_buffer->release_fence_fd = -1;
-      fd = layer_stack->layers[index].input_buffer->acquire_fence_fd;
-      layer_stack->layers[index].input_buffer->acquire_fence_fd = -1;
+      layer_stack->layers.at(index)->input_buffer->release_fence_fd = -1;
+      fd = layer_stack->layers.at(index)->input_buffer->acquire_fence_fd;
+      layer_stack->layers.at(index)->input_buffer->acquire_fence_fd = -1;
       count++;
     }
   }
@@ -258,36 +253,34 @@
 int BlitEngineC2d::Prepare(LayerStack *layer_stack) {
   blit_target_start_index_ = 0;
 
-  uint32_t gpu_target_index = layer_stack->layer_count-1;
-  uint32_t i = INT(layer_stack->layer_count-1);
+  uint32_t layer_count = UINT32(layer_stack->layers.size());
+  uint32_t gpu_target_index = layer_count - 1;  // default assumption
+  uint32_t i = 0;
 
-  for (i = 0; i < layer_stack->layer_count; i++) {
-    Layer &layer = layer_stack->layers[i];
-    if (!blit_supported_) {
-      return -1;
-    }
+  for (; i < layer_count; i++) {
+    Layer *layer = layer_stack->layers.at(i);
 
     // No 10 bit support for C2D
-    if (Is10BitFormat(layer.input_buffer->format)) {
+    if (Is10BitFormat(layer->input_buffer->format)) {
       return -1;
     }
 
-    if (layer.composition == kCompositionGPUTarget) {
+    if (layer->composition == kCompositionGPUTarget) {
       // Need FBT size for allocating buffers
       gpu_target_index = i;
       break;
     }
   }
 
-  if ((layer_stack->layer_count-1) == gpu_target_index) {
+  if ((layer_count - 1) == gpu_target_index) {
     // No blit target layer
     return -1;
   }
 
   blit_target_start_index_ = ++i;
-  num_blit_target_ = layer_stack->layer_count - blit_target_start_index_;
+  num_blit_target_ = layer_count - blit_target_start_index_;
 
-  LayerBuffer *layer_buffer = layer_stack->layers[gpu_target_index].input_buffer;
+  LayerBuffer *layer_buffer = layer_stack->layers.at(gpu_target_index)->input_buffer;
   int fbwidth = INT(layer_buffer->width);
   int fbheight = INT(layer_buffer->height);
   if ((fbwidth < 0) || (fbheight < 0)) {
@@ -298,17 +291,17 @@
   int k = blit_target_start_index_;
 
   for (uint32_t j = 0; j < num_blit_target_; j++, k++) {
-    Layer &layer = layer_stack->layers[k];
-    LayerBuffer *layer_buffer = layer.input_buffer;
+    Layer *layer = layer_stack->layers.at(k);
+    LayerBuffer *layer_buffer = layer->input_buffer;
 
     // Set the buffer height and width
     layer_buffer->width = fbwidth;
     layer_buffer->height = fbheight/3;
 
-    layer.plane_alpha = 0xFF;
-    layer.blending = kBlendingOpaque;
-    layer.composition = kCompositionBlitTarget;
-    layer.frame_rate = layer_stack->layers[gpu_target_index].frame_rate;
+    layer->plane_alpha = 0xFF;
+    layer->blending = kBlendingOpaque;
+    layer->composition = kCompositionBlitTarget;
+    layer->frame_rate = layer_stack->layers.at(gpu_target_index)->frame_rate;
   }
 
   return 0;
@@ -329,17 +322,17 @@
   }
 
   for (uint32_t i = num_app_layers-1; (i > 0) && (processed_blit < num_blit_target_); i--) {
-    Layer &layer = layer_stack->layers[i];
-    if (layer.composition != kCompositionBlit) {
+    Layer *layer = layer_stack->layers.at(i);
+    if (layer->composition != kCompositionBlit) {
       continue;
     }
     blit_needed = true;
     layer_stack->flags.attributes_changed = true;
 
-    Layer &blit_layer = layer_stack->layers[blit_target_start_index_ + processed_blit];
-    LayerRect &blit_src_rect = blit_layer.src_rect;
-    int width = INT(layer.dst_rect.right - layer.dst_rect.left);
-    int height = INT(layer.dst_rect.bottom - layer.dst_rect.top);
+    Layer *blit_layer = layer_stack->layers.at(blit_target_start_index_ + processed_blit);
+    LayerRect &blit_src_rect = blit_layer->src_rect;
+    int width = INT(layer->dst_rect.right - layer->dst_rect.left);
+    int height = INT(layer->dst_rect.bottom - layer->dst_rect.top);
     usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_HW_TEXTURE;
     if (blit_engine_c2d_->get(blit_engine_c2d_, COPYBIT_UBWC_SUPPORT) > 0) {
       usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
@@ -354,9 +347,9 @@
     // Left will be zero always
     dst_rects[processed_blit].top = FLOAT(target_height - height);
     dst_rects[processed_blit].right = dst_rects[processed_blit].left +
-                                      (layer.dst_rect.right - layer.dst_rect.left);
+                                      (layer->dst_rect.right - layer->dst_rect.left);
     dst_rects[processed_blit].bottom = (dst_rects[processed_blit].top +
-                                      (layer.dst_rect.bottom - layer.dst_rect.top));
+                                      (layer->dst_rect.bottom - layer->dst_rect.top));
     blit_src_rect = dst_rects[processed_blit];
     processed_blit++;
   }
@@ -370,19 +363,17 @@
 
   if (blit_needed) {
     for (uint32_t j = 0; j < num_blit_target_; j++) {
-      Layer &layer = layer_stack->layers[j + content_list->numHwLayers];
+      Layer *layer = layer_stack->layers.at(j + content_list->numHwLayers);
       private_handle_t *target_buffer = blit_target_buffer_[current_blit_target_index_];
       // Set the fd information
-      if (layer.input_buffer) {
-        layer.input_buffer->width = target_width;
-        layer.input_buffer->height = target_height;
-        if (target_buffer->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
-          layer.input_buffer->format = kFormatRGBA8888Ubwc;
-        }
-        layer.input_buffer->planes[0].fd = target_buffer->fd;
-        layer.input_buffer->planes[0].offset = 0;
-        layer.input_buffer->planes[0].stride = target_buffer->width;
+        layer->input_buffer->width = target_width;
+        layer->input_buffer->height = target_height;
+      if (target_buffer->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
+          layer->input_buffer->format = kFormatRGBA8888Ubwc;
       }
+      layer->input_buffer->planes[0].fd = target_buffer->fd;
+      layer->input_buffer->planes[0].offset = 0;
+      layer->input_buffer->planes[0].stride = target_buffer->width;
     }
   }
 
@@ -403,8 +394,8 @@
 
   // if not Blit Targets return
   for (uint32_t i = 0; i < num_app_layers; i++) {
-    Layer &layer = layer_stack->layers[i];
-    if (layer.composition == kCompositionHybrid || layer.composition == kCompositionBlit) {
+    Layer *layer = layer_stack->layers.at(i);
+    if (layer->composition == kCompositionHybrid || layer->composition == kCompositionBlit) {
       hybrid_present = true;
     }
   }
@@ -425,22 +416,22 @@
   uint32_t processed_blit = 0;
   for (uint32_t i = num_app_layers-1; (i > 0) && (processed_blit < num_blit_target_) &&
       (status == 0); i--) {
-    Layer &layer = layer_stack->layers[i];
-    if (layer.composition != kCompositionBlit) {
+    Layer *layer = layer_stack->layers.at(i);
+    if (layer->composition != kCompositionBlit) {
       continue;
     }
 
     for (uint32_t k = 0; k <= i; k++) {
-      Layer &bottom_layer = layer_stack->layers[k];
-      LayerBuffer *layer_buffer = bottom_layer.input_buffer;
+      Layer *bottom_layer = layer_stack->layers.at(k);
+      LayerBuffer *layer_buffer = bottom_layer->input_buffer;
       // if layer below the blit layer does not intersect, ignore that layer
-      LayerRect inter_sect = Intersection(layer.dst_rect, bottom_layer.dst_rect);
-      if (bottom_layer.composition != kCompositionHybrid && !IsValid(inter_sect)) {
+      LayerRect inter_sect = Intersection(layer->dst_rect, bottom_layer->dst_rect);
+      if (bottom_layer->composition != kCompositionHybrid && !IsValid(inter_sect)) {
         continue;
       }
-      if (bottom_layer.composition == kCompositionGPU ||
-          bottom_layer.composition == kCompositionSDE ||
-          bottom_layer.composition == kCompositionGPUTarget) {
+      if (bottom_layer->composition == kCompositionGPU ||
+          bottom_layer->composition == kCompositionSDE ||
+          bottom_layer->composition == kCompositionGPUTarget) {
         continue;
       }
 
@@ -453,10 +444,10 @@
         layer_buffer->acquire_fence_fd = -1;
       }
       hwc_layer_1_t *hwc_layer = &content_list->hwLayers[k];
-      LayerRect src_rect = bottom_layer.blit_regions.rect[processed_blit];
-      Layer &blit_layer = layer_stack->layers[blit_target_start_index_ + processed_blit];
-      LayerRect dest_rect = blit_layer.src_rect;
-      int ret_val = DrawRectUsingCopybit(hwc_layer, &bottom_layer, src_rect, dest_rect);
+      LayerRect &src_rect = bottom_layer->blit_regions.at(processed_blit);
+      Layer *blit_layer = layer_stack->layers.at(blit_target_start_index_ + processed_blit);
+      LayerRect dest_rect = blit_layer->src_rect;
+      int ret_val = DrawRectUsingCopybit(hwc_layer, bottom_layer, src_rect, dest_rect);
       copybit_layer_count++;
       if (ret_val < 0) {
         copybit_layer_count = 0;
@@ -478,9 +469,10 @@
     DumpBlitTargetBuffer(fd);
 
     // Set the fd to the LayerStack BlitTargets fd
-    for (uint32_t k = blit_target_start_index_; k < layer_stack->layer_count; k++) {
-      Layer &layer = layer_stack->layers[k];
-      LayerBuffer *layer_buffer = layer.input_buffer;
+    uint32_t layer_count = UINT32(layer_stack->layers.size());
+    for (uint32_t k = blit_target_start_index_; k < layer_count; k++) {
+      Layer *layer = layer_stack->layers.at(k);
+      LayerBuffer *layer_buffer = layer->input_buffer;
       layer_buffer->acquire_fence_fd = fd;
     }
   }
diff --git a/msm8996/sdm/libs/hwc/blit_engine_c2d.h b/msm8996/sdm/libs/hwc/blit_engine_c2d.h
index 69b7299..6536d44 100644
--- a/msm8996/sdm/libs/hwc/blit_engine_c2d.h
+++ b/msm8996/sdm/libs/hwc/blit_engine_c2d.h
@@ -114,7 +114,6 @@
   bool blit_active_ = false;
   uint32_t dump_frame_count_ = 0;
   uint32_t dump_frame_index_ = 0;
-  int blit_supported_ = 0;
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc/hwc_display.cpp b/msm8996/sdm/libs/hwc/hwc_display.cpp
index 71df14b..ab9b4a7 100644
--- a/msm8996/sdm/libs/hwc/hwc_display.cpp
+++ b/msm8996/sdm/libs/hwc/hwc_display.cpp
@@ -32,6 +32,7 @@
 #include <gralloc_priv.h>
 #include <gr.h>
 #include <utils/constants.h>
+#include <utils/formats.h>
 #include <utils/rect.h>
 #include <utils/debug.h>
 #include <sync/sync.h>
@@ -51,18 +52,6 @@
 
 namespace sdm {
 
-static void AssignLayerRegionsAddress(LayerRectArray *region, uint32_t rect_count,
-                                      uint8_t **base_address) {
-  if (rect_count) {
-    region->rect = reinterpret_cast<LayerRect *>(*base_address);
-    for (uint32_t i = 0; i < rect_count; i++) {
-      region->rect[i] = LayerRect();
-    }
-    *base_address += rect_count * sizeof(LayerRect);
-  }
-  region->count = rect_count;
-}
-
 static void ApplyDeInterlaceAdjustment(Layer *layer) {
   // De-interlacing adjustment
   if (layer->input_buffer->flags.interlace) {
@@ -73,8 +62,10 @@
 }
 
 HWCDisplay::HWCDisplay(CoreInterface *core_intf, hwc_procs_t const **hwc_procs, DisplayType type,
-                       int id, bool needs_blit)
-  : core_intf_(core_intf), hwc_procs_(hwc_procs), type_(type), id_(id), needs_blit_(needs_blit) {
+                       int id, bool needs_blit, qService::QService *qservice,
+                       DisplayClass display_class)
+  : core_intf_(core_intf), hwc_procs_(hwc_procs), type_(type), id_(id), needs_blit_(needs_blit),
+    qservice_(qservice), display_class_(display_class) {
 }
 
 int HWCDisplay::Init() {
@@ -98,7 +89,9 @@
     return -EINVAL;
   }
 
-  if (needs_blit_) {
+  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");
@@ -132,11 +125,6 @@
     return -EINVAL;
   }
 
-  if (layer_stack_memory_.raw) {
-    delete[] layer_stack_memory_.raw;
-    layer_stack_memory_.raw = NULL;
-  }
-
   delete framebuffer_config_;
 
   if (blit_engine_) {
@@ -304,6 +292,16 @@
   return kErrorNotSupported;
 }
 
+DisplayError HWCDisplay::CECMessage(char *message) {
+  if (qservice_) {
+    qservice_->onCECMessageReceived(message, 0);
+  } else {
+    DLOGW("Qservice instance not available.");
+  }
+
+  return kErrorNone;
+}
+
 int HWCDisplay::AllocateLayerStack(hwc_display_contents_1_t *content_list) {
   if (!content_list || !content_list->numHwLayers) {
     DLOGW("Invalid content list");
@@ -313,89 +311,31 @@
   size_t num_hw_layers = content_list->numHwLayers;
   uint32_t blit_target_count = 0;
 
-  if (needs_blit_ && blit_engine_) {
+  if (blit_engine_) {
     blit_target_count = kMaxBlitTargetLayers;
   }
 
-  // Allocate memory for
-  //  a) total number of layers
-  //  b) buffer handle for each layer
-  //  c) number of visible rectangles in each layer
-  //  d) number of dirty rectangles in each layer
-  //  e) number of blit rectangles in each layer
-  size_t required_size = (num_hw_layers + blit_target_count) *
-                         (sizeof(Layer) + sizeof(LayerBuffer));
+  FreeLayerStack();
 
   for (size_t i = 0; i < num_hw_layers + blit_target_count; i++) {
-    uint32_t num_visible_rects = 0;
-    uint32_t num_dirty_rects = 0;
-
-    if (i < num_hw_layers) {
-      hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
-      num_visible_rects = UINT32(hwc_layer.visibleRegionScreen.numRects);
-      num_dirty_rects = UINT32(hwc_layer.surfaceDamage.numRects);
-    }
-
-    // visible rectangles + dirty rectangles + blit rectangle
-    size_t num_rects = num_visible_rects + num_dirty_rects + blit_target_count;
-    required_size += num_rects * sizeof(LayerRect);
-  }
-
-  // Layer array may be large enough to hold current number of layers.
-  // If not, re-allocate it now.
-  if (layer_stack_memory_.size < required_size) {
-    if (layer_stack_memory_.raw) {
-      delete[] layer_stack_memory_.raw;
-      layer_stack_memory_.size = 0;
-    }
-
-    // Allocate in multiple of kSizeSteps.
-    required_size = ROUND_UP(required_size, layer_stack_memory_.kSizeSteps);
-    layer_stack_memory_.raw = new uint8_t[required_size];
-    if (!layer_stack_memory_.raw) {
-      return -ENOMEM;
-    }
-
-    layer_stack_memory_.size = required_size;
-  }
-
-  // Assign memory addresses now.
-  uint8_t *current_address = layer_stack_memory_.raw;
-
-  // Layer array address
-  layer_stack_ = LayerStack();
-  layer_stack_.layers = reinterpret_cast<Layer *>(current_address);
-  layer_stack_.layer_count = UINT32(num_hw_layers + blit_target_count);
-  current_address += (num_hw_layers + blit_target_count) * sizeof(Layer);
-
-  for (size_t i = 0; i < num_hw_layers + blit_target_count; i++) {
-    uint32_t num_visible_rects = 0;
-    uint32_t num_dirty_rects = 0;
-
-    if (i < num_hw_layers) {
-      hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
-      num_visible_rects = UINT32(hwc_layer.visibleRegionScreen.numRects);
-      num_dirty_rects = UINT32(hwc_layer.surfaceDamage.numRects);
-    }
-
-    Layer &layer = layer_stack_.layers[i];
-    layer = Layer();
-
-    // Layer buffer handle address
-    layer.input_buffer = reinterpret_cast<LayerBuffer *>(current_address);
-    *layer.input_buffer = LayerBuffer();
-    current_address += sizeof(LayerBuffer);
-
-    // Visible/Dirty/Blit rectangle address
-    AssignLayerRegionsAddress(&layer.visible_regions, num_visible_rects, &current_address);
-    AssignLayerRegionsAddress(&layer.dirty_regions, num_dirty_rects, &current_address);
-    AssignLayerRegionsAddress(&layer.blit_regions, blit_target_count, &current_address);
+    Layer *layer = new Layer();
+    LayerBuffer *layer_buffer = new LayerBuffer();
+    layer->input_buffer = layer_buffer;
+    layer_stack_.layers.push_back(layer);
   }
 
   return 0;
 }
 
-int HWCDisplay::PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer) {
+void HWCDisplay::FreeLayerStack() {
+  for (Layer *layer : layer_stack_.layers) {
+    delete layer->input_buffer;
+    delete layer;
+  }
+  layer_stack_ = {};
+}
+
+int HWCDisplay::PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer* layer) {
   const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer->handle);
 
   LayerBuffer *layer_buffer = layer->input_buffer;
@@ -495,41 +435,54 @@
   for (size_t i = 0; i < num_hw_layers; i++) {
     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
 
-    Layer &layer = layer_stack_.layers[i];
-
-    int ret = PrepareLayerParams(&content_list->hwLayers[i], &layer_stack_.layers[i]);
+    Layer *layer = layer_stack_.layers.at(i);
+    int ret = PrepareLayerParams(&content_list->hwLayers[i], layer);
 
     if (ret != kErrorNone) {
       return ret;
     }
 
-    layer.flags.skip = ((hwc_layer.flags & HWC_SKIP_LAYER) > 0);
-    layer.flags.solid_fill = (hwc_layer.flags & kDimLayer) || solid_fill_enable_;
-    if (layer.flags.skip || layer.flags.solid_fill) {
-      layer.dirty_regions.count = 0;
+    layer->flags.skip = ((hwc_layer.flags & HWC_SKIP_LAYER) > 0);
+    layer->flags.solid_fill = (hwc_layer.flags & kDimLayer) || solid_fill_enable_;
+    if (layer->flags.skip || layer->flags.solid_fill) {
+      layer->dirty_regions.clear();
     }
 
     hwc_rect_t scaled_display_frame = hwc_layer.displayFrame;
     ScaleDisplayFrame(&scaled_display_frame);
     ApplyScanAdjustment(&scaled_display_frame);
 
-    SetRect(scaled_display_frame, &layer.dst_rect);
-    SetRect(hwc_layer.sourceCropf, &layer.src_rect);
-    ApplyDeInterlaceAdjustment(&layer);
+    SetRect(scaled_display_frame, &layer->dst_rect);
+    SetRect(hwc_layer.sourceCropf, &layer->src_rect);
+    ApplyDeInterlaceAdjustment(layer);
 
-    for (uint32_t j = 0; j < layer.visible_regions.count; j++) {
-      SetRect(hwc_layer.visibleRegionScreen.rects[j], &layer.visible_regions.rect[j]);
-    }
-    for (uint32_t j = 0; j < layer.dirty_regions.count; j++) {
-      SetRect(hwc_layer.surfaceDamage.rects[j], &layer.dirty_regions.rect[j]);
-    }
-    SetComposition(hwc_layer.compositionType, &layer.composition);
+    uint32_t num_visible_rects = UINT32(hwc_layer.visibleRegionScreen.numRects);
+    uint32_t num_dirty_rects = UINT32(hwc_layer.surfaceDamage.numRects);
 
+    for (uint32_t j = 0; j < num_visible_rects; j++) {
+      LayerRect visible_rect = {};
+      SetRect(hwc_layer.visibleRegionScreen.rects[j], &visible_rect);
+      layer->visible_regions.push_back(visible_rect);
+    }
+
+    for (uint32_t j = 0; j < num_dirty_rects; j++) {
+      LayerRect dirty_rect = {};
+      SetRect(hwc_layer.surfaceDamage.rects[j], &dirty_rect);
+      layer->dirty_regions.push_back(dirty_rect);
+    }
+
+    if (blit_engine_) {
+      for (uint32_t j = 0; j < kMaxBlitTargetLayers; j++) {
+        LayerRect blit_rect = {};
+        layer->blit_regions.push_back(blit_rect);
+      }
+    }
+
+    SetComposition(hwc_layer.compositionType, &layer->composition);
     if (hwc_layer.compositionType != HWC_FRAMEBUFFER_TARGET) {
-      display_rect_ = Union(display_rect_, layer.dst_rect);
+      display_rect_ = Union(display_rect_, layer->dst_rect);
     }
 
-
     // For dim layers, SurfaceFlinger
     //    - converts planeAlpha to per pixel alpha,
     //    - sets RGB color to 000,
@@ -540,12 +493,12 @@
     //    - incoming planeAlpha,
     //    - blending to Coverage.
     if (hwc_layer.flags & kDimLayer) {
-      layer.input_buffer->format = kFormatARGB8888;
-      layer.solid_fill_color = 0xff000000;
-      SetBlending(HWC_BLENDING_COVERAGE, &layer.blending);
+      layer->input_buffer->format = kFormatARGB8888;
+      layer->solid_fill_color = 0xff000000;
+      SetBlending(HWC_BLENDING_COVERAGE, &layer->blending);
     } else {
-      SetBlending(hwc_layer.blending, &layer.blending);
-      LayerTransform &layer_transform = layer.transform;
+      SetBlending(hwc_layer.blending, &layer->blending);
+      LayerTransform &layer_transform = layer->transform;
       uint32_t &hwc_transform = hwc_layer.transform;
       layer_transform.flip_horizontal = ((hwc_transform & HWC_TRANSFORM_FLIP_H) > 0);
       layer_transform.flip_vertical = ((hwc_transform & HWC_TRANSFORM_FLIP_V) > 0);
@@ -554,47 +507,49 @@
 
     // TODO(user): Remove below block.
     // For solid fill, only dest rect need to be specified.
-    if (layer.flags.solid_fill) {
-      LayerBuffer *input_buffer = layer.input_buffer;
-      input_buffer->width = UINT32(layer.dst_rect.right - layer.dst_rect.left);
-      input_buffer->height = UINT32(layer.dst_rect.bottom - layer.dst_rect.top);
-      layer.src_rect.left = 0;
-      layer.src_rect.top = 0;
-      layer.src_rect.right = input_buffer->width;
-      layer.src_rect.bottom = input_buffer->height;
+    if (layer->flags.solid_fill) {
+      LayerBuffer *input_buffer = layer->input_buffer;
+      input_buffer->width = UINT32(layer->dst_rect.right - layer->dst_rect.left);
+      input_buffer->height = UINT32(layer->dst_rect.bottom - layer->dst_rect.top);
+      layer->src_rect.left = 0;
+      layer->src_rect.top = 0;
+      layer->src_rect.right = input_buffer->width;
+      layer->src_rect.bottom = input_buffer->height;
     }
 
-    layer.plane_alpha = hwc_layer.planeAlpha;
-    layer.flags.cursor = ((hwc_layer.flags & HWC_IS_CURSOR_LAYER) > 0);
-    layer.flags.updating = true;
+    layer->plane_alpha = hwc_layer.planeAlpha;
+    layer->flags.cursor = ((hwc_layer.flags & HWC_IS_CURSOR_LAYER) > 0);
+    layer->flags.updating = true;
 
     if (num_hw_layers <= kMaxLayerCount) {
-      layer.flags.updating = IsLayerUpdating(content_list, INT32(i));
+      layer->flags.updating = IsLayerUpdating(content_list, INT32(i));
     }
 #ifdef QTI_BSP
     if (hwc_layer.flags & HWC_SCREENSHOT_ANIMATOR_LAYER) {
       layer_stack_.flags.animating = true;
     }
 #endif
-    if (layer.flags.skip) {
+    if (layer->flags.skip) {
       layer_stack_.flags.skip_present = true;
     }
 
-    if (layer.flags.cursor) {
+    if (layer->flags.cursor) {
       layer_stack_.flags.cursor_present = true;
     }
 
-    if (layer.frame_rate > metadata_refresh_rate_) {
-      metadata_refresh_rate_ = SanitizeRefreshRate(layer.frame_rate);
+    if (layer->frame_rate > metadata_refresh_rate_) {
+      metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate);
     } else {
-      layer.frame_rate = current_refresh_rate_;
+      layer->frame_rate = current_refresh_rate_;
     }
 
-    layer.input_buffer->buffer_id = reinterpret_cast<uint64_t>(hwc_layer.handle);
+    layer->input_buffer->buffer_id = reinterpret_cast<uint64_t>(hwc_layer.handle);
   }
 
   // Prepare the Blit Target
   if (blit_engine_) {
+  // TODO(user): Fix this to enable BLIT
+#if 0
     int ret = blit_engine_->Prepare(&layer_stack_);
     if (ret) {
       // Blit engine cannot handle this layer stack, hence set the layer stack
@@ -603,6 +558,7 @@
     } else {
       use_blit_comp_ = true;
     }
+#endif
   }
 
   // Configure layer stack
@@ -650,8 +606,8 @@
 
   for (size_t i = 0; i < num_hw_layers; i++) {
     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
-    Layer &layer = layer_stack_.layers[i];
-    LayerComposition composition = layer.composition;
+    Layer *layer = layer_stack_.layers.at(i);
+    LayerComposition composition = layer->composition;
 
     if ((composition == kCompositionSDE) || (composition == kCompositionHybrid) ||
         (composition == kCompositionBlit)) {
@@ -688,7 +644,7 @@
 
   if (!flush_) {
     for (size_t i = 0; i < num_hw_layers; i++) {
-      CommitLayerParams(&content_list->hwLayers[i], &layer_stack_.layers[i]);
+      CommitLayerParams(&content_list->hwLayers[i], layer_stack_.layers.at(i));
     }
 
     if (use_blit_comp_) {
@@ -742,17 +698,17 @@
 
   for (size_t i = 0; i < num_hw_layers; i++) {
     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
-    Layer &layer = layer_stack_.layers[i];
-    LayerBuffer *layer_buffer = layer_stack_.layers[i].input_buffer;
+    Layer *layer = layer_stack_.layers.at(i);
+    LayerBuffer *layer_buffer = layer->input_buffer;
 
     if (!flush_) {
       // If swapinterval property is set to 0 or for single buffer layers, do not update f/w
       // release fences and discard fences from driver
-      if (swap_interval_zero_ || layer.flags.single_buffer) {
+      if (swap_interval_zero_ || layer->flags.single_buffer) {
         hwc_layer.releaseFenceFd = -1;
         close(layer_buffer->release_fence_fd);
         layer_buffer->release_fence_fd = -1;
-      } else if (layer.composition != kCompositionGPU) {
+      } else if (layer->composition != kCompositionGPU) {
         hwc_layer.releaseFenceFd = layer_buffer->release_fence_fd;
       }
 
@@ -760,7 +716,7 @@
       // framebuffer layer throughout animation and do not allow framework to do eglswapbuffer on
       // framebuffer target. So graphics doesn't close the release fence fd of framebuffer target,
       // Hence close the release fencefd of framebuffer target here.
-      if (layer.composition == kCompositionGPUTarget && layer_stack_cache_.animating) {
+      if (layer->composition == kCompositionGPUTarget && layer_stack_cache_.animating) {
         close(hwc_layer.releaseFenceFd);
         hwc_layer.releaseFenceFd = -1;
       }
@@ -795,7 +751,7 @@
 
 
 bool HWCDisplay::NeedsFrameBufferRefresh(hwc_display_contents_1_t *content_list) {
-  uint32_t layer_count = layer_stack_.layer_count;
+  uint32_t num_hw_layers = UINT32(content_list->numHwLayers);
 
   // Handle ongoing animation and end here, start is handled below
   if (layer_stack_cache_.animating) {
@@ -817,24 +773,25 @@
     return true;
   }
 
-  for (uint32_t i = 0; i < layer_count; i++) {
-    Layer &layer = layer_stack_.layers[i];
+  for (uint32_t i = 0; i < num_hw_layers; i++) {
+    Layer *layer = layer_stack_.layers.at(i);
     LayerCache &layer_cache = layer_stack_cache_.layer_cache[i];
 
     // need FB refresh for s3d case
-    if (layer.input_buffer->s3d_format != kS3dFormatNone) {
+    if (layer->input_buffer->s3d_format != kS3dFormatNone) {
         return true;
     }
 
-    if (layer.composition == kCompositionGPUTarget) {
+    if (layer->composition == kCompositionGPUTarget ||
+        layer->composition == kCompositionBlitTarget) {
       continue;
     }
 
-    if (layer_cache.composition != layer.composition) {
+    if (layer_cache.composition != layer->composition) {
       return true;
     }
 
-    if ((layer.composition == kCompositionGPU) && IsLayerUpdating(content_list, INT32(i))) {
+    if ((layer->composition == kCompositionGPU) && IsLayerUpdating(content_list, INT32(i))) {
       return true;
     }
   }
@@ -863,7 +820,7 @@
 }
 
 void HWCDisplay::CacheLayerStackInfo(hwc_display_contents_1_t *content_list) {
-  uint32_t layer_count = layer_stack_.layer_count;
+  uint32_t layer_count = UINT32(layer_stack_.layers.size());
 
   if (layer_count > kMaxLayerCount || layer_stack_.flags.animating) {
     ResetLayerCacheStack();
@@ -871,16 +828,16 @@
   }
 
   for (uint32_t i = 0; i < layer_count; i++) {
-    Layer &layer = layer_stack_.layers[i];
-    if (layer.composition == kCompositionGPUTarget ||
-        layer.composition == kCompositionBlitTarget) {
+    Layer *layer = layer_stack_.layers.at(i);
+    if (layer->composition == kCompositionGPUTarget ||
+        layer->composition == kCompositionBlitTarget) {
       continue;
     }
 
     LayerCache &layer_cache = layer_stack_cache_.layer_cache[i];
     layer_cache.handle = content_list->hwLayers[i].handle;
     layer_cache.plane_alpha = content_list->hwLayers[i].planeAlpha;
-    layer_cache.composition = layer.composition;
+    layer_cache.composition = layer->composition;
   }
 
   layer_stack_cache_.layer_count = layer_count;
@@ -1057,6 +1014,48 @@
   }
 }
 
+void HWCDisplay::DumpOutputBuffer(const BufferInfo& buffer_info, void *base, int fence) {
+  char dir_path[PATH_MAX];
+
+  snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
+
+  if (mkdir(dir_path, 777) != 0 && errno != EEXIST) {
+    DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
+    return;
+  }
+
+  // if directory exists already, need to explicitly change the permission.
+  if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
+    DLOGW("Failed to change permissions on %s directory", dir_path);
+    return;
+  }
+
+  if (base) {
+    char dump_file_name[PATH_MAX];
+    size_t result = 0;
+
+    if (fence >= 0) {
+      int error = sync_wait(fence, 1000);
+      if (error < 0) {
+        DLOGW("sync_wait error errno = %d, desc = %s", errno,  strerror(errno));
+        return;
+      }
+    }
+
+    snprintf(dump_file_name, sizeof(dump_file_name), "%s/output_layer_%dx%d_%s_frame%d.raw",
+             dir_path, buffer_info.buffer_config.width, buffer_info.buffer_config.height,
+             GetFormatString(buffer_info.buffer_config.format), dump_frame_index_);
+
+    FILE* fp = fopen(dump_file_name, "w+");
+    if (fp) {
+      result = fwrite(base, buffer_info.alloc_buffer_info.size, 1, fp);
+      fclose(fp);
+    }
+
+    DLOGI("Frame Dump of %s is %s", dump_file_name, result ? "Successful" : "Failed");
+  }
+}
+
 const char *HWCDisplay::GetHALPixelFormatString(int format) {
   switch (format) {
   case HAL_PIXEL_FORMAT_RGBA_8888:
@@ -1128,7 +1127,7 @@
   case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
     return "YCbCr_420_TP10_UBWC";
   default:
-    return "Unknown pixel format";
+    return "Unknown_format";
   }
 }
 
@@ -1502,8 +1501,8 @@
   uint32_t updating_count = 0;
 
   for (uint i = 0; i < app_layer_count; i++) {
-    Layer &layer = layer_stack_.layers[i];
-    if (layer.flags.updating) {
+    Layer *layer = layer_stack_.layers.at(i);
+    if (layer->flags.updating) {
       updating_count++;
     }
   }
@@ -1527,4 +1526,8 @@
   return refresh_rate;
 }
 
+DisplayClass HWCDisplay::GetDisplayClass() {
+  return display_class_;
+}
+
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc/hwc_display.h b/msm8996/sdm/libs/hwc/hwc_display.h
index 21c87ab..7b14f10 100644
--- a/msm8996/sdm/libs/hwc/hwc_display.h
+++ b/msm8996/sdm/libs/hwc/hwc_display.h
@@ -28,6 +28,7 @@
 #include <hardware/hwcomposer.h>
 #include <core/core_interface.h>
 #include <qdMetaData.h>
+#include <QService.h>
 #include <private/color_params.h>
 #include <map>
 
@@ -35,6 +36,16 @@
 
 class BlitEngine;
 
+// Subclasses set this to their type. This has to be different from DisplayType.
+// This is to avoid RTTI and dynamic_cast
+enum DisplayClass {
+  DISPLAY_CLASS_PRIMARY,
+  DISPLAY_CLASS_EXTERNAL,
+  DISPLAY_CLASS_VIRTUAL,
+  DISPLAY_CLASS_NULL
+};
+
+
 class HWCDisplay : public DisplayEventHandler {
  public:
   virtual ~HWCDisplay() { }
@@ -65,6 +76,18 @@
   virtual int SetCursorPosition(int x, int y);
   virtual void SetSecureDisplay(bool secure_display_active);
 
+  // Captures frame output in the buffer specified by output_buffer_info. The API is
+  // non-blocking and the client is expected to check operation status later on.
+  // Returns -1 if the input is invalid.
+  virtual int FrameCaptureAsync(const BufferInfo& output_buffer_info, bool post_processed) {
+    return -1;
+  }
+  // Returns the status of frame capture operation requested with FrameCaptureAsync().
+  // -EAGAIN : No status obtain yet, call API again after another frame.
+  // < 0 : Operation happened but failed.
+  // 0 : Success.
+  virtual int GetFrameCaptureStatus() { return -EAGAIN; }
+
   // Display Configurations
   virtual int SetActiveDisplayConfig(int config);
   virtual int GetActiveDisplayConfig(uint32_t *config);
@@ -78,6 +101,7 @@
                            PPDisplayAPIPayload *out_payload,
                            PPPendingParams *pending_action);
   int GetVisibleDisplayRect(hwc_rect_t* rect);
+  DisplayClass GetDisplayClass();
 
  protected:
   enum DisplayStatus {
@@ -93,15 +117,6 @@
   // Maximum number of layers supported by display manager.
   static const uint32_t kMaxLayerCount = 32;
 
-  // Structure to track memory allocation for layer stack (layers, rectangles) object.
-  struct LayerStackMemory {
-    static const size_t kSizeSteps = 4096;  // Default memory allocation.
-    uint8_t *raw;  // Pointer to byte array.
-    size_t size;  // Current number of allocated bytes.
-
-    LayerStackMemory() : raw(NULL), size(0) { }
-  };
-
   struct LayerCache {
     buffer_handle_t handle;
     uint8_t plane_alpha;
@@ -120,17 +135,20 @@
   };
 
   HWCDisplay(CoreInterface *core_intf, hwc_procs_t const **hwc_procs, DisplayType type, int id,
-             bool needs_blit);
+             bool needs_blit, qService::QService *qservice, DisplayClass display_class);
 
   // DisplayEventHandler methods
   virtual DisplayError VSync(const DisplayEventVSync &vsync);
   virtual DisplayError Refresh();
+  virtual DisplayError CECMessage(char *message);
 
-  virtual int AllocateLayerStack(hwc_display_contents_1_t *content_list);
+  int AllocateLayerStack(hwc_display_contents_1_t *content_list);
+  void FreeLayerStack();
   virtual int PrePrepareLayerStack(hwc_display_contents_1_t *content_list);
   virtual int PrepareLayerStack(hwc_display_contents_1_t *content_list);
   virtual int CommitLayerStack(hwc_display_contents_1_t *content_list);
   virtual int PostCommitLayerStack(hwc_display_contents_1_t *content_list);
+  virtual void DumpOutputBuffer(const BufferInfo& buffer_info, void *base, int fence);
   inline void SetRect(const hwc_rect_t &source, LayerRect *target);
   inline void SetRect(const hwc_frect_t &source, LayerRect *target);
   inline void SetComposition(const int32_t &source, LayerComposition *target);
@@ -162,9 +180,8 @@
   hwc_procs_t const **hwc_procs_;
   DisplayType type_;
   int id_;
-  bool needs_blit_;
+  bool needs_blit_ = false;
   DisplayInterface *display_intf_ = NULL;
-  LayerStackMemory layer_stack_memory_;
   LayerStack layer_stack_;
   LayerStackCache layer_stack_cache_;
   bool flush_on_error_ = false;
@@ -199,6 +216,8 @@
   void CommitLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer);
   void ResetLayerCacheStack();
   BlitEngine *blit_engine_ = NULL;
+  qService::QService *qservice_ = NULL;
+  DisplayClass display_class_;
 };
 
 inline int HWCDisplay::Perform(uint32_t operation, ...) {
diff --git a/msm8996/sdm/libs/hwc/hwc_display_external.cpp b/msm8996/sdm/libs/hwc/hwc_display_external.cpp
index 86b36d8..813b123 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_external.cpp
+++ b/msm8996/sdm/libs/hwc/hwc_display_external.cpp
@@ -39,12 +39,18 @@
 namespace sdm {
 
 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                               qService::QService *qservice, HWCDisplay **hwc_display) {
+  return Create(core_intf, hwc_procs, 0, 0, qservice, false, hwc_display);
+}
+
+int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
                                uint32_t primary_width, uint32_t primary_height,
+                               qService::QService *qservice, bool use_primary_res,
                                HWCDisplay **hwc_display) {
   uint32_t external_width = 0;
   uint32_t external_height = 0;
 
-  HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, hwc_procs);
+  HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, hwc_procs, qservice);
   int status = hwc_display_external->Init();
   if (status) {
     delete hwc_display_external;
@@ -53,10 +59,19 @@
 
   hwc_display_external->GetPanelResolution(&external_width, &external_height);
 
-  int downscale_enabled = 0;
-  HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
-  if (downscale_enabled) {
-    GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
+  if (primary_width && primary_height) {
+    // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
+    // provided primary_width and primary_height
+    if (use_primary_res) {
+      external_width = primary_width;
+      external_height = primary_height;
+    } else {
+      int downscale_enabled = 0;
+      HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
+      if (downscale_enabled) {
+        GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
+      }
+    }
   }
 
   status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
@@ -75,8 +90,10 @@
   delete hwc_display;
 }
 
-HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
-  : HWCDisplay(core_intf, hwc_procs, kHDMI, HWC_DISPLAY_EXTERNAL, false) {
+HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                                       qService::QService *qservice)
+  : HWCDisplay(core_intf, hwc_procs, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
+               DISPLAY_CLASS_EXTERNAL) {
 }
 
 int HWCDisplayExternal::Prepare(hwc_display_contents_1_t *content_list) {
@@ -155,8 +172,8 @@
     return;
   }
 
-  uint32_t new_panel_width = panel_width * UINT32(1.0f - width_ratio);
-  uint32_t new_panel_height = panel_height * UINT32(1.0f - height_ratio);
+  uint32_t new_panel_width = UINT32(panel_width * FLOAT(1.0f - width_ratio));
+  uint32_t new_panel_height = UINT32(panel_height * FLOAT(1.0f - height_ratio));
 
   int x_offset = INT((FLOAT(panel_width) * width_ratio) / 2.0f);
   int y_offset = INT((FLOAT(panel_height) * height_ratio) / 2.0f);
diff --git a/msm8996/sdm/libs/hwc/hwc_display_external.h b/msm8996/sdm/libs/hwc/hwc_display_external.h
index ba00d4d..436190d 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_external.h
+++ b/msm8996/sdm/libs/hwc/hwc_display_external.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014, 2016, 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,14 +32,18 @@
 class HWCDisplayExternal : public HWCDisplay {
  public:
   static int Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs, uint32_t primary_width,
-                    uint32_t primary_height, HWCDisplay **hwc_display);
+                    uint32_t primary_height, qService::QService *qservice, bool use_primary_res,
+                    HWCDisplay **hwc_display);
+  static int Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                    qService::QService *qservice, HWCDisplay **hwc_display);
   static void Destroy(HWCDisplay *hwc_display);
   virtual int Prepare(hwc_display_contents_1_t *content_list);
   virtual int Commit(hwc_display_contents_1_t *content_list);
   virtual void SetSecureDisplay(bool secure_display_active);
 
  private:
-  HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs);
+  HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                     qService::QService *qservice);
   void ApplyScanAdjustment(hwc_rect_t *display_frame);
   static void GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
                                      uint32_t *virtual_width, uint32_t *virtual_height);
diff --git a/msm8996/sdm/libs/hwc/hwc_display_null.cpp b/msm8996/sdm/libs/hwc/hwc_display_null.cpp
new file mode 100644
index 0000000..11cf47a
--- /dev/null
+++ b/msm8996/sdm/libs/hwc/hwc_display_null.cpp
@@ -0,0 +1,120 @@
+/*
+* Copyright (c) 2016, 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 <hardware/hwcomposer_defs.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+#include "hwc_display_null.h"
+
+#define __CLASS__ "HWCDisplayNull"
+
+namespace sdm {
+
+int HWCDisplayNull::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                           HWCDisplay **hwc_display) {
+  int status;
+
+  DLOGI("Null display is being created");
+  HWCDisplayNull *hwc_display_null = new HWCDisplayNull(core_intf, hwc_procs);
+
+  status = hwc_display_null->Init();
+  if (status) {
+    delete hwc_display_null;
+    return status;
+  }
+
+  *hwc_display = hwc_display_null;
+
+  return 0;
+}
+
+void HWCDisplayNull::Destroy(HWCDisplay *hwc_display) {
+  DLOGI("Null display is being destroyed");
+  hwc_display->Deinit();
+  delete hwc_display;
+}
+
+// We pass the display type as HWC_DISPLAY_PRIMARY to HWCDisplay, but since we override
+// and don't chain to HWCDisplay::Init(), that type does not actually get used.
+HWCDisplayNull::HWCDisplayNull(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
+  : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, false, NULL,
+               DISPLAY_CLASS_NULL) {
+}
+
+int HWCDisplayNull::Init() {
+  // Don't call HWCDisplay::Init() for null display, we don't want the chain of
+  // DisplayPrimary / HWPrimary etc objects to be created.
+  return 0;
+}
+
+int HWCDisplayNull::Deinit() {
+  return 0;
+}
+
+int HWCDisplayNull::Prepare(hwc_display_contents_1_t *content_list) {
+  for (size_t i = 0; i < content_list->numHwLayers; i++) {
+    if (content_list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET ||
+        content_list->hwLayers[i].compositionType == HWC_BACKGROUND) {
+      continue;
+    }
+
+    content_list->hwLayers[i].compositionType = HWC_OVERLAY;
+  }
+
+  return 0;
+}
+
+int HWCDisplayNull::Commit(hwc_display_contents_1_t *content_list) {
+  // HWCSession::Commit (from where this is called) already closes all the acquire
+  // fences once we return from here. So no need to close acquire fences here.
+  for (size_t i = 0; i < content_list->numHwLayers; i++) {
+    content_list->hwLayers[i].releaseFenceFd = -1;
+  }
+
+  return 0;
+}
+
+#define NULL_DISPLAY_FPS 60
+
+int HWCDisplayNull::GetDisplayAttributes(uint32_t config, const uint32_t *attributes,
+                                         int32_t *values) {
+  for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
+    // We fake display resolution as 1080P by default, though it can be overriden through a call to
+    // SetResolution(), and DPI as 160, though what the DPI value does is not clear
+    switch (attributes[i]) {
+    case HWC_DISPLAY_VSYNC_PERIOD:
+      values[i] = INT32(1000000000L / NULL_DISPLAY_FPS);
+      break;
+    case HWC_DISPLAY_WIDTH:
+      values[i] = static_cast<int32_t>(x_res_);
+      break;
+    case HWC_DISPLAY_HEIGHT:
+      values[i] = static_cast<int32_t>(y_res_);
+      break;
+    }
+  }
+  return 0;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc/hwc_display_null.h b/msm8996/sdm/libs/hwc/hwc_display_null.h
new file mode 100644
index 0000000..2f6438a
--- /dev/null
+++ b/msm8996/sdm/libs/hwc/hwc_display_null.h
@@ -0,0 +1,104 @@
+/*
+* Copyright (c) 2016, 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 __HWC_DISPLAY_NULL_H__
+#define __HWC_DISPLAY_NULL_H__
+
+#include <qdMetaData.h>
+#include <gralloc_priv.h>
+#include "hwc_display.h"
+
+namespace sdm {
+
+class HWCDisplayNull : public HWCDisplay {
+ public:
+  static int Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+                    HWCDisplay **hwc_display);
+  static void Destroy(HWCDisplay *hwc_display);
+  virtual int Init();
+  virtual int Deinit();
+  virtual int Prepare(hwc_display_contents_1_t *content_list);
+  virtual int Commit(hwc_display_contents_1_t *content_list);
+  virtual int EventControl(int event, int enable) { return 0; }
+  virtual int SetPowerMode(int mode) { return 0; }
+
+  // Framebuffer configurations
+  virtual int GetDisplayConfigs(uint32_t *configs, size_t *num_configs) {
+    return HWCDisplay::GetDisplayConfigs(configs, num_configs);
+  }
+
+  virtual int GetDisplayAttributes(uint32_t config, const uint32_t *attributes, int32_t *values);
+  virtual int GetActiveConfig() { return 0; }
+  virtual int SetActiveConfig(int index) { return -1; }
+
+  virtual void SetIdleTimeoutMs(uint32_t timeout_ms) { return; }
+  virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) { return; }
+  virtual DisplayError SetMaxMixerStages(uint32_t max_mixer_stages) { return kErrorNone; }
+  virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending) { return kErrorNone; }
+  virtual uint32_t GetLastPowerMode() { return 0; }
+  virtual int SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels) { return 0; }
+
+  virtual void GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
+    *x_pixels = x_res_;
+    *y_pixels = y_res_;
+  }
+
+  virtual void GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
+    *x_pixels = x_res_;
+    *y_pixels = y_res_;
+  }
+
+  virtual int SetDisplayStatus(uint32_t display_status) { return 0; }
+  virtual int OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) { return 0; }
+  virtual int Perform(uint32_t operation, ...) { return 0; }
+  virtual int SetCursorPosition(int x, int y) { return 0; }
+  virtual void SetSecureDisplay(bool secure_display_active) { return; }
+
+  // Display Configurations
+  virtual int SetActiveDisplayConfig(int config) { return 0; }
+  virtual int GetActiveDisplayConfig(uint32_t *config) { return -1; }
+  virtual int GetDisplayConfigCount(uint32_t *count) { return -1; }
+  virtual int GetDisplayAttributesForConfig(int config, DisplayConfigVariableInfo *attributes) {
+    return -1;
+  }
+  virtual bool IsValidContentList(hwc_display_contents_1_t *content_list) {
+    return true;
+  }
+
+  void SetResolution(uint32_t x_res, uint32_t y_res) {
+    x_res_ = x_res;
+    y_res_ = y_res;
+  }
+
+
+ private:
+  HWCDisplayNull(CoreInterface *core_intf, hwc_procs_t const **hwc_procs);
+  uint32_t x_res_ = 1920;
+  uint32_t y_res_ = 1080;
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_DISPLAY_NULL_H__
+
diff --git a/msm8996/sdm/libs/hwc/hwc_display_primary.cpp b/msm8996/sdm/libs/hwc/hwc_display_primary.cpp
index e19184f..32ff73b 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_primary.cpp
+++ b/msm8996/sdm/libs/hwc/hwc_display_primary.cpp
@@ -28,9 +28,12 @@
 */
 
 #include <cutils/properties.h>
+#include <sync/sync.h>
 #include <utils/constants.h>
 #include <utils/debug.h>
 #include <stdarg.h>
+#include <sys/mman.h>
+
 #include "hwc_display_primary.h"
 #include "hwc_debugger.h"
 
@@ -38,13 +41,15 @@
 
 namespace sdm {
 
-int HWCDisplayPrimary::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                              hwc_procs_t const **hwc_procs, qService::QService *qservice,
                               HWCDisplay **hwc_display) {
   int status = 0;
   uint32_t primary_width = 0;
   uint32_t primary_height = 0;
 
-  HWCDisplay *hwc_display_primary = new HWCDisplayPrimary(core_intf, hwc_procs);
+  HWCDisplay *hwc_display_primary = new HWCDisplayPrimary(core_intf, buffer_allocator,
+                                                          hwc_procs, qservice);
   status = hwc_display_primary->Init();
   if (status) {
     delete hwc_display_primary;
@@ -76,8 +81,12 @@
   delete hwc_display;
 }
 
-HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
-  : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, true), cpu_hint_(NULL) {
+HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf,
+                                     BufferAllocator *buffer_allocator,
+                                     hwc_procs_t const **hwc_procs,
+                                     qService::QService *qservice)
+  : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice,
+               DISPLAY_CLASS_PRIMARY), buffer_allocator_(buffer_allocator), cpu_hint_(NULL) {
 }
 
 int HWCDisplayPrimary::Init() {
@@ -153,6 +162,15 @@
     return status;
   }
 
+  bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
+
+  if (frame_capture_buffer_queued_ || pending_output_dump) {
+    // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
+    // here in a subsequent draw round.
+    layer_stack_.output_buffer = &output_buffer_;
+    layer_stack_.flags.post_processed_output = post_processed_output_;
+  }
+
   bool one_updating_layer = SingleLayerUpdating(UINT32(content_list->numHwLayers - 1));
   ToggleCPUHint(one_updating_layer);
 
@@ -206,6 +224,8 @@
     return status;
   }
 
+  HandleFrameOutput();
+
   status = HWCDisplay::PostCommitLayerStack(content_list);
   if (status) {
     return status;
@@ -336,5 +356,145 @@
   display_intf_->SetIdleTimeoutMs(timeout_ms);
 }
 
+static void SetLayerBuffer(const BufferInfo& output_buffer_info, LayerBuffer *output_buffer) {
+  output_buffer->width = output_buffer_info.buffer_config.width;
+  output_buffer->height = output_buffer_info.buffer_config.height;
+  output_buffer->format = output_buffer_info.buffer_config.format;
+  output_buffer->planes[0].fd = output_buffer_info.alloc_buffer_info.fd;
+  output_buffer->planes[0].stride = output_buffer_info.alloc_buffer_info.stride;
+}
+
+void HWCDisplayPrimary::HandleFrameOutput() {
+  if (frame_capture_buffer_queued_) {
+    HandleFrameCapture();
+  } else if (dump_output_to_file_) {
+    HandleFrameDump();
+  }
+}
+
+void HWCDisplayPrimary::HandleFrameCapture() {
+  if (output_buffer_.release_fence_fd >= 0) {
+    frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
+    ::close(output_buffer_.release_fence_fd);
+    output_buffer_.release_fence_fd = -1;
+  }
+
+  frame_capture_buffer_queued_ = false;
+  post_processed_output_ = false;
+  output_buffer_ = {};
+
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(true  /* enable */, &pending);
+}
+
+void HWCDisplayPrimary::HandleFrameDump() {
+  if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) {
+    int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
+    ::close(output_buffer_.release_fence_fd);
+    output_buffer_.release_fence_fd = -1;
+    if (ret < 0) {
+      DLOGE("sync_wait error errno = %d, desc = %s", errno,  strerror(errno));
+    } else {
+      DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
+    }
+  }
+
+  if (0 == dump_frame_count_) {
+    dump_output_to_file_ = false;
+    // Unmap and Free buffer
+    if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
+      DLOGE("unmap failed with err %d", errno);
+    }
+    if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
+      DLOGE("FreeBuffer failed");
+    }
+
+    post_processed_output_ = false;
+    output_buffer_ = {};
+    output_buffer_info_ = {};
+    output_buffer_base_ = nullptr;
+
+    uint32_t pending = 0;  // Just a temporary to satisfy the API
+    ControlPartialUpdate(true  /* enable */, &pending);
+  }
+}
+
+void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
+  HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
+  dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
+  DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
+
+  if (!count || !dump_output_to_file_) {
+    return;
+  }
+
+  // Allocate and map output buffer
+  output_buffer_info_ = {};
+  // Since we dump DSPP output use Panel resolution.
+  GetPanelResolution(&output_buffer_info_.buffer_config.width,
+                     &output_buffer_info_.buffer_config.height);
+  output_buffer_info_.buffer_config.format = kFormatRGB888;
+  output_buffer_info_.buffer_config.buffer_count = 1;
+  if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
+    DLOGE("Buffer allocation failed");
+    output_buffer_info_ = {};
+    return;
+  }
+
+  void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size,
+                      PROT_READ | PROT_WRITE,
+                      MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
+
+  if (buffer == MAP_FAILED) {
+    DLOGE("mmap failed with err %d", errno);
+    buffer_allocator_->FreeBuffer(&output_buffer_info_);
+    output_buffer_info_ = {};
+    return;
+  }
+
+  output_buffer_base_ = buffer;
+  post_processed_output_ = true;
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(false  /* enable */, &pending);
+}
+
+int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo& output_buffer_info,
+                                         bool post_processed_output) {
+  // Note: This function is called in context of a binder thread and a lock is already held
+  if (output_buffer_info.alloc_buffer_info.fd < 0) {
+    DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
+    return -1;
+  }
+
+  auto panel_width = 0u;
+  auto panel_height = 0u;
+  auto fb_width = 0u;
+  auto fb_height = 0u;
+
+  GetPanelResolution(&panel_width, &panel_height);
+  GetFrameBufferResolution(&fb_width, &fb_height);
+
+  if (post_processed_output && (output_buffer_info_.buffer_config.width < panel_width ||
+                                output_buffer_info_.buffer_config.height < panel_height)) {
+    DLOGE("Buffer dimensions should not be less than panel resolution");
+    return -1;
+  } else if (!post_processed_output && (output_buffer_info_.buffer_config.width < fb_width ||
+                                        output_buffer_info_.buffer_config.height < fb_height)) {
+    DLOGE("Buffer dimensions should not be less than FB resolution");
+    return -1;
+  }
+
+  SetLayerBuffer(output_buffer_info, &output_buffer_);
+  post_processed_output_ = post_processed_output;
+  frame_capture_buffer_queued_ = true;
+  // Status is only cleared on a new call to dump and remains valid otherwise
+  frame_capture_status_ = -EAGAIN;
+
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(false  /* enable */, &pending);
+
+  return 0;
+}
+
 }  // namespace sdm
 
diff --git a/msm8996/sdm/libs/hwc/hwc_display_primary.h b/msm8996/sdm/libs/hwc/hwc_display_primary.h
index 3773717..8c02731 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_primary.h
+++ b/msm8996/sdm/libs/hwc/hwc_display_primary.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014-2016, 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:
@@ -40,7 +40,8 @@
     UNSET_QDCM_SOLID_FILL_INFO,
   };
 
-  static int Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
+  static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                    hwc_procs_t const **hwc_procs, qService::QService *qservice,
                     HWCDisplay **hwc_display);
   static void Destroy(HWCDisplay *hwc_display);
   virtual int Init();
@@ -50,9 +51,13 @@
   virtual void SetSecureDisplay(bool secure_display_active);
   virtual DisplayError Refresh();
   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
+  virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
+  virtual int FrameCaptureAsync(const BufferInfo& output_buffer_info, bool post_processed);
+  virtual int GetFrameCaptureStatus() { return frame_capture_status_; }
 
  private:
-  HWCDisplayPrimary(CoreInterface *core_intf, hwc_procs_t const **hwc_procs);
+  HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                    hwc_procs_t const **hwc_procs, qService::QService *qservice);
   void SetMetaDataRefreshRateFlag(bool enable);
   virtual DisplayError SetDisplayMode(uint32_t mode);
   void ProcessBootAnimCompleted(hwc_display_contents_1_t *content_list);
@@ -60,9 +65,26 @@
   void ToggleCPUHint(bool set);
   void ForceRefreshRate(uint32_t refresh_rate);
   uint32_t GetOptimalRefreshRate(bool one_updating_layer);
+  void HandleFrameOutput();
+  void HandleFrameCapture();
+  void HandleFrameDump();
 
-  CPUHint *cpu_hint_;
+  BufferAllocator *buffer_allocator_ = nullptr;
+  CPUHint *cpu_hint_ = nullptr;
   bool handle_idle_timeout_ = false;
+
+  // Primary output buffer configuration
+  LayerBuffer output_buffer_ = {};
+  bool post_processed_output_ = false;
+
+  // Members for 1 frame capture in a client provided buffer
+  bool frame_capture_buffer_queued_ = false;
+  int frame_capture_status_ = -EAGAIN;
+
+  // Members for N frame output dump to file
+  bool dump_output_to_file_ = false;
+  BufferInfo output_buffer_info_ = {};
+  void *output_buffer_base_ = nullptr;
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc/hwc_display_virtual.cpp b/msm8996/sdm/libs/hwc/hwc_display_virtual.cpp
index b405d91..ef7198c 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_virtual.cpp
+++ b/msm8996/sdm/libs/hwc/hwc_display_virtual.cpp
@@ -96,8 +96,8 @@
 }
 
 HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
-  : HWCDisplay(core_intf, hwc_procs, kVirtual, HWC_DISPLAY_VIRTUAL, false),
-    dump_output_layer_(false), output_buffer_(NULL) {
+  : HWCDisplay(core_intf, hwc_procs, kVirtual, HWC_DISPLAY_VIRTUAL, false, NULL,
+               DISPLAY_CLASS_VIRTUAL) {
 }
 
 int HWCDisplayVirtual::Init() {
@@ -119,6 +119,7 @@
 
   if (output_buffer_) {
     delete output_buffer_;
+    output_buffer_ = NULL;
   }
 
   return status;
@@ -175,7 +176,18 @@
     return status;
   }
 
-  DumpOutputBuffer(content_list);
+  if (dump_frame_count_ && !flush_ && dump_output_layer_) {
+    const private_handle_t *output_handle = (const private_handle_t *)(content_list->outbuf);
+    if (output_handle && output_handle->base) {
+      BufferInfo buffer_info;
+      buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
+      buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
+      buffer_info.buffer_config.format = GetSDMFormat(output_handle->format, output_handle->flags);
+      buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
+      DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
+                       layer_stack_.retire_fence_fd);
+    }
+  }
 
   status = HWCDisplay::PostCommitLayerStack(content_list);
   if (status) {
@@ -293,53 +305,6 @@
   return status;
 }
 
-void HWCDisplayVirtual::DumpOutputBuffer(hwc_display_contents_1_t *content_list) {
-  const private_handle_t *output_handle = (const private_handle_t *)(content_list->outbuf);
-  char dir_path[PATH_MAX];
-
-  if (!dump_frame_count_ || flush_ || !dump_output_layer_) {
-    return;
-  }
-
-  snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
-
-  if (mkdir(dir_path, 777) != 0 && errno != EEXIST) {
-    DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
-    return;
-  }
-
-  // if directory exists already, need to explicitly change the permission.
-  if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
-    DLOGW("Failed to change permissions on %s directory", dir_path);
-    return;
-  }
-
-  if (output_handle && output_handle->base) {
-    char dump_file_name[PATH_MAX];
-    size_t result = 0;
-
-    if (content_list->outbufAcquireFenceFd >= 0) {
-      int error = sync_wait(content_list->outbufAcquireFenceFd, 1000);
-      if (error < 0) {
-        DLOGW("sync_wait error errno = %d, desc = %s", errno,  strerror(errno));
-        return;
-      }
-    }
-
-    snprintf(dump_file_name, sizeof(dump_file_name), "%s/output_layer_%dx%d_%s_frame%d.raw",
-             dir_path, output_handle->width, output_handle->height,
-             GetHALPixelFormatString(output_handle->format), dump_frame_index_);
-
-    FILE* fp = fopen(dump_file_name, "w+");
-    if (fp) {
-      result = fwrite(reinterpret_cast<void *>(output_handle->base), output_handle->size, 1, fp);
-      fclose(fp);
-    }
-
-    DLOGI("Frame Dump of %s is %s", dump_file_name, result ? "Successful" : "Failed");
-  }
-}
-
 void HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
   HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
   dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);
diff --git a/msm8996/sdm/libs/hwc/hwc_display_virtual.h b/msm8996/sdm/libs/hwc/hwc_display_virtual.h
index afa1fcd..c383809 100644
--- a/msm8996/sdm/libs/hwc/hwc_display_virtual.h
+++ b/msm8996/sdm/libs/hwc/hwc_display_virtual.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014,2016 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:
@@ -49,10 +49,9 @@
  private:
   HWCDisplayVirtual(CoreInterface *core_intf, hwc_procs_t const **hwc_procs);
   int SetOutputBuffer(hwc_display_contents_1_t *content_list);
-  void DumpOutputBuffer(hwc_display_contents_1_t *content_list);
 
-  bool dump_output_layer_;
-  LayerBuffer *output_buffer_;
+  bool dump_output_layer_ = false;
+  LayerBuffer *output_buffer_ = NULL;
 };
 
 inline bool HWCDisplayVirtual::IsValidContentList(hwc_display_contents_1_t *content_list) {
diff --git a/msm8996/sdm/libs/hwc/hwc_session.cpp b/msm8996/sdm/libs/hwc/hwc_session.cpp
index 129b605..7928765 100644
--- a/msm8996/sdm/libs/hwc/hwc_session.cpp
+++ b/msm8996/sdm/libs/hwc/hwc_session.cpp
@@ -49,6 +49,7 @@
 #include "hwc_buffer_sync_handler.h"
 #include "hwc_session.h"
 #include "hwc_debugger.h"
+#include "hwc_display_null.h"
 #include "hwc_display_primary.h"
 #include "hwc_display_virtual.h"
 
@@ -116,11 +117,12 @@
 
   // Start QService and connect to it.
   qService::QService::init();
-  android::sp<qService::IQService> qservice = android::interface_cast<qService::IQService>(
+  android::sp<qService::IQService> iqservice = android::interface_cast<qService::IQService>(
                 android::defaultServiceManager()->getService(android::String16(qservice_name)));
 
-  if (qservice.get()) {
-    qservice->connect(android::sp<qClient::IQClient>(this));
+  if (iqservice.get()) {
+    iqservice->connect(android::sp<qClient::IQClient>(this));
+    qservice_ = reinterpret_cast<qService::QService* >(iqservice.get());
   } else {
     DLOGE("Failed to acquire %s", qservice_name);
     return -EINVAL;
@@ -145,9 +147,33 @@
     return -EINVAL;
   }
 
-  // Create and power on primary display
-  status = HWCDisplayPrimary::Create(core_intf_, &hwc_procs_,
-                                     &hwc_display_[HWC_DISPLAY_PRIMARY]);
+  // Read which display is first, and create it and store it in primary slot
+  HWDisplayInterfaceInfo hw_disp_info;
+  error = core_intf_->GetFirstDisplayInterfaceType(&hw_disp_info);
+  if (error == kErrorNone) {
+    if (hw_disp_info.type == kHDMI) {
+      // HDMI is primary display. If already connected, then create it and store in
+      // primary display slot. If not connected, create a NULL display for now.
+      if (hw_disp_info.is_connected) {
+        status = HWCDisplayExternal::Create(core_intf_, &hwc_procs_, qservice_,
+                                            &hwc_display_[HWC_DISPLAY_PRIMARY]);
+      } else {
+        // NullDisplay simply closes all its fences, and advertizes a standard
+        // resolution to SurfaceFlinger
+        status = HWCDisplayNull::Create(core_intf_, &hwc_procs_,
+                                        &hwc_display_[HWC_DISPLAY_PRIMARY]);
+      }
+    } else {
+        // Create and power on primary display
+        status = HWCDisplayPrimary::Create(core_intf_, buffer_allocator_, &hwc_procs_, qservice_,
+                                           &hwc_display_[HWC_DISPLAY_PRIMARY]);
+    }
+  } else {
+        // Create and power on primary display
+        status = HWCDisplayPrimary::Create(core_intf_, buffer_allocator_, &hwc_procs_, qservice_,
+                                           &hwc_display_[HWC_DISPLAY_PRIMARY]);
+  }
+
   if (status) {
     CoreInterface::DestroyCore();
     return status;
@@ -596,7 +622,7 @@
 
   if (disp == HWC_DISPLAY_EXTERNAL) {
     status = HWCDisplayExternal::Create(core_intf_, &hwc_procs_, primary_width, primary_height,
-                                        &hwc_display_[disp]);
+                                        qservice_, false, &hwc_display_[disp]);
   } else if (disp == HWC_DISPLAY_VIRTUAL) {
     status = HWCDisplayVirtual::Create(core_intf_, &hwc_procs_, primary_width, primary_height,
                                        content_list, &hwc_display_[disp]);
@@ -1306,6 +1332,7 @@
 int HWCSession::HotPlugHandler(bool connected) {
   int status = 0;
   bool notify_hotplug = false;
+  bool hdmi_primary = false;
 
   // To prevent sending events to client while a lock is held, acquire scope locks only within
   // below scope so that those get automatically unlocked after the scope ends.
@@ -1317,36 +1344,112 @@
       return -1;
     }
 
-    if (connected) {
-      if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
-        DLOGE("HDMI is already connected");
-        return -1;
-      }
 
-      // Connect external display if virtual display is not connected.
-      // Else, defer external display connection and process it when virtual display
-      // tears down; Do not notify SurfaceFlinger since connection is deferred now.
-      if (!hwc_display_[HWC_DISPLAY_VIRTUAL]) {
-        status = ConnectDisplay(HWC_DISPLAY_EXTERNAL, NULL);
+    HWCDisplay *primary_display = hwc_display_[HWC_DISPLAY_PRIMARY];
+    HWCDisplay *external_display = NULL;
+    HWCDisplay *null_display = NULL;
+
+    if (primary_display->GetDisplayClass() == DISPLAY_CLASS_EXTERNAL) {
+        external_display = static_cast<HWCDisplayExternal *>(hwc_display_[HWC_DISPLAY_PRIMARY]);
+    } else if (primary_display->GetDisplayClass() == DISPLAY_CLASS_NULL) {
+        null_display = static_cast<HWCDisplayNull *>(hwc_display_[HWC_DISPLAY_PRIMARY]);
+    }
+
+    if (external_display || null_display) {
+      hdmi_primary = true;
+    }
+
+    // If primary display connected is a NULL display, then replace it with the external display
+    if (connected) {
+      // If we are in HDMI as primary and the primary display just got plugged in
+      if (null_display) {
+        assert(hdmi_primary);
+        uint32_t primary_width, primary_height;
+        null_display->GetFrameBufferResolution(&primary_width, &primary_height);
+        delete null_display;
+        hwc_display_[HWC_DISPLAY_PRIMARY] = NULL;
+
+        // Create external display with a forced framebuffer resolution to that of what the NULL
+        // display had. This is necessary because SurfaceFlinger does not dynamically update
+        // framebuffer resolution once it reads it at bootup. So we always have to have the NULL
+        // display/external display both at the bootup resolution.
+        int status = HWCDisplayExternal::Create(core_intf_, &hwc_procs_, primary_width,
+                                                primary_height, qservice_, true,
+                                                &hwc_display_[HWC_DISPLAY_PRIMARY]);
         if (status) {
-          return status;
+          DLOGE("Could not create external display");
+          return -1;
         }
-        notify_hotplug = true;
+
+        // Next, go ahead and enable vsync on external display. This is expliclity required
+        // because in HDMI as primary case, SurfaceFlinger may not be aware of underlying
+        // changing display. and thus may not explicitly enable vsync
+
+        status = hwc_display_[HWC_DISPLAY_PRIMARY]->EventControl(HWC_EVENT_VSYNC, true);
+        if (status) {
+          DLOGE("Error enabling vsync for HDMI as primary case");
+        }
+        // Don't do hotplug notification for HDMI as primary case for now
+        notify_hotplug = false;
       } else {
-        DLOGI("Virtual display is connected, pending connection");
-        external_pending_connect_ = true;
+        if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+            DLOGE("HDMI is already connected");
+            return -1;
+        }
+
+        // Connect external display if virtual display is not connected.
+        // Else, defer external display connection and process it when virtual display
+        // tears down; Do not notify SurfaceFlinger since connection is deferred now.
+        if (!hwc_display_[HWC_DISPLAY_VIRTUAL]) {
+            status = ConnectDisplay(HWC_DISPLAY_EXTERNAL, NULL);
+            if (status) {
+            return status;
+            }
+            notify_hotplug = true;
+        } else {
+            DLOGI("Virtual display is connected, pending connection");
+            external_pending_connect_ = true;
+        }
       }
     } else {
       // Do not return error if external display is not in connected status.
       // Due to virtual display concurrency, external display connection might be still pending
       // but hdmi got disconnected before pending connection could be processed.
-      if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
-        status = DisconnectDisplay(HWC_DISPLAY_EXTERNAL);
-        notify_hotplug = true;
+
+      if (hdmi_primary) {
+        assert(external_display != NULL);
+        uint32_t x_res, y_res;
+        external_display->GetFrameBufferResolution(&x_res, &y_res);
+        // Need to manually disable VSYNC as SF is not aware of connect/disconnect cases
+        // for HDMI as primary
+        external_display->EventControl(HWC_EVENT_VSYNC, false);
+        HWCDisplayExternal::Destroy(external_display);
+
+        HWCDisplayNull *null_display;
+
+        int status = HWCDisplayNull::Create(core_intf_, &hwc_procs_,
+                                            reinterpret_cast<HWCDisplay **>(&null_display));
+
+        if (status) {
+          DLOGE("Could not create Null display when primary got disconnected");
+          return -1;
+        }
+
+        null_display->SetResolution(x_res, y_res);
+        hwc_display_[HWC_DISPLAY_PRIMARY] = null_display;
+
+        // Don't do hotplug notification for HDMI as primary case for now
+        notify_hotplug = false;
+      } else {
+        if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+            status = DisconnectDisplay(HWC_DISPLAY_EXTERNAL);
+            notify_hotplug = true;
+        }
+        external_pending_connect_ = false;
       }
-      external_pending_connect_ = false;
     }
   }
+
   if (connected && notify_hotplug) {
     // trigger screen refresh to ensure sufficient resources are available to process new
     // new display connection.
@@ -1359,6 +1462,8 @@
     hwc_procs_->hotplug(hwc_procs_, HWC_DISPLAY_EXTERNAL, connected);
   }
 
+  qservice_->onHdmiHotplug(INT(connected));
+
   return 0;
 }
 
diff --git a/msm8996/sdm/libs/hwc/hwc_session.h b/msm8996/sdm/libs/hwc/hwc_session.h
index 561de35..270751a 100644
--- a/msm8996/sdm/libs/hwc/hwc_session.h
+++ b/msm8996/sdm/libs/hwc/hwc_session.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2014 - 2016, 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:
@@ -28,7 +28,6 @@
 #include <hardware/hwcomposer.h>
 #include <core/core_interface.h>
 #include <utils/locker.h>
-#include <IQClient.h>
 
 #include "hwc_display_primary.h"
 #include "hwc_display_external.h"
@@ -140,6 +139,7 @@
   bool new_bw_mode_ = false;
   bool need_invalidate_ = false;
   int bw_mode_release_fd_ = -1;
+  qService::QService *qservice_ = NULL;
 };
 
 }  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/Android.mk b/msm8996/sdm/libs/hwc2/Android.mk
new file mode 100644
index 0000000..f633d7e
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/Android.mk
@@ -0,0 +1,37 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+include $(LOCAL_PATH)/../../../common.mk
+
+ifeq ($(use_hwc2),true)
+
+LOCAL_MODULE                  := hwcomposer.$(TARGET_BOARD_PLATFORM)
+LOCAL_MODULE_RELATIVE_PATH    := hw
+LOCAL_MODULE_TAGS             := optional
+LOCAL_C_INCLUDES              := $(common_includes)
+
+LOCAL_CFLAGS                  := -Wno-missing-field-initializers -Wno-unused-parameter \
+                                 -std=c++11 -fcolor-diagnostics\
+                                 -DLOG_TAG=\"SDM\" $(common_flags) \
+                                 -I $(display_top)/sdm/libs/hwc
+LOCAL_CLANG                   := true
+
+LOCAL_SHARED_LIBRARIES        := libsdmcore libqservice libbinder libhardware libhardware_legacy \
+                                 libutils libcutils libsync libmemalloc libqdutils libdl \
+                                 libpowermanager libsdmutils libc++
+
+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
+
+include $(BUILD_SHARED_LIBRARY)
+endif
diff --git a/msm8996/sdm/libs/hwc2/hwc_callbacks.cpp b/msm8996/sdm/libs/hwc2/hwc_callbacks.cpp
new file mode 100644
index 0000000..48ae398
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_callbacks.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2016, 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/constants.h>
+#include <utils/debug.h>
+#include "hwc_callbacks.h"
+
+#define __CLASS__ "HWCCallbacks"
+
+namespace sdm {
+
+void HWCCallbacks::Hotplug(hwc2_display_t display, HWC2::Connection state) {
+  if (hotplug_) {
+    hotplug_(hotplug_data_, display, INT32(state));
+  }
+}
+
+void HWCCallbacks::Refresh(hwc2_display_t display) {
+  if (refresh_) {
+    refresh_(refresh_data_, display);
+  }
+}
+
+void HWCCallbacks::Vsync(hwc2_display_t display, int64_t timestamp) {
+  if (vsync_) {
+    vsync_(vsync_data_, display, timestamp);
+  }
+}
+
+HWC2::Error HWCCallbacks::Register(HWC2::Callback descriptor, hwc2_callback_data_t callback_data,
+                                   hwc2_function_pointer_t pointer) {
+  switch (descriptor) {
+    case HWC2::Callback::Hotplug:
+      hotplug_data_ = callback_data;
+      hotplug_ = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer);
+      break;
+    case HWC2::Callback::Refresh:
+      refresh_data_ = callback_data;
+      refresh_ = reinterpret_cast<HWC2_PFN_REFRESH>(pointer);
+      break;
+    case HWC2::Callback::Vsync:
+      vsync_data_ = callback_data;
+      vsync_ = reinterpret_cast<HWC2_PFN_VSYNC>(pointer);
+      break;
+    default:
+      return HWC2::Error::BadParameter;
+  }
+  return HWC2::Error::None;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_callbacks.h b/msm8996/sdm/libs/hwc2/hwc_callbacks.h
new file mode 100644
index 0000000..835a06a
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_callbacks.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016, 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 __HWC_CALLBACKS_H__
+#define __HWC_CALLBACKS_H__
+
+#define HWC2_INCLUDE_STRINGIFICATION
+#define HWC2_USE_CPP11
+#include <hardware/hwcomposer2.h>
+#undef HWC2_INCLUDE_STRINGIFICATION
+#undef HWC2_USE_CPP11
+
+namespace sdm {
+
+class HWCCallbacks {
+ public:
+  void Hotplug(hwc2_display_t display, HWC2::Connection state);
+  void Refresh(hwc2_display_t display);
+  void Vsync(hwc2_display_t display, int64_t timestamp);
+  HWC2::Error Register(HWC2::Callback, hwc2_callback_data_t callback_data,
+                       hwc2_function_pointer_t pointer);
+
+ private:
+  hwc2_callback_data_t hotplug_data_ = nullptr;
+  hwc2_callback_data_t refresh_data_ = nullptr;
+  hwc2_callback_data_t vsync_data_ = nullptr;
+
+  HWC2_PFN_HOTPLUG hotplug_ = nullptr;
+  HWC2_PFN_REFRESH refresh_ = nullptr;
+  HWC2_PFN_VSYNC vsync_ = nullptr;
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_CALLBACKS_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_color_manager.cpp b/msm8996/sdm/libs/hwc2/hwc_color_manager.cpp
new file mode 100644
index 0000000..0be9724
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_color_manager.cpp
@@ -0,0 +1,564 @@
+/*
+* Copyright (c) 2015 - 2016, 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 <powermanager/IPowerManager.h>
+#include <cutils/sockets.h>
+#include <cutils/native_handle.h>
+#include <utils/String16.h>
+#include <binder/Parcel.h>
+#include <gralloc_priv.h>
+#include <hardware/hwcomposer.h>
+#include <hardware/hwcomposer_defs.h>
+#include <QService.h>
+
+#include <core/dump_interface.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+#include <core/buffer_allocator.h>
+#include <private/color_params.h>
+#include "hwc_buffer_allocator.h"
+#include "hwc_buffer_sync_handler.h"
+#include "hwc_session.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "HWCColorManager"
+
+namespace sdm {
+
+uint32_t HWCColorManager::Get8BitsARGBColorValue(const PPColorFillParams &params) {
+  uint32_t argb_color = ((params.color.r << 16) & 0xff0000) | ((params.color.g) & 0xff) |
+                        ((params.color.b << 8) & 0xff00);
+  return argb_color;
+}
+
+int HWCColorManager::CreatePayloadFromParcel(const android::Parcel &in, uint32_t *disp_id,
+                                             PPDisplayAPIPayload *sink) {
+  int ret = 0;
+  uint32_t id(0);
+  uint32_t size(0);
+
+  id = UINT32(in.readInt32());
+  size = UINT32(in.readInt32());
+  if (size > 0 && size == in.dataAvail()) {
+    const void *data = in.readInplace(size);
+    const uint8_t *temp = reinterpret_cast<const uint8_t *>(data);
+
+    sink->size = size;
+    sink->payload = const_cast<uint8_t *>(temp);
+    *disp_id = id;
+  } else {
+    DLOGW("Failing size checking, size = %d", size);
+    ret = -EINVAL;
+  }
+
+  return ret;
+}
+
+void HWCColorManager::MarshallStructIntoParcel(const PPDisplayAPIPayload &data,
+                                               android::Parcel *out_parcel) {
+  out_parcel->writeInt32(INT32(data.size));
+  if (data.payload)
+    out_parcel->write(data.payload, data.size);
+}
+
+HWCColorManager *HWCColorManager::CreateColorManager() {
+  HWCColorManager *color_mgr = new HWCColorManager();
+
+  if (color_mgr) {
+    void *&color_lib = color_mgr->color_apis_lib_;
+    // Load display API interface library. And retrieve color API function tables.
+    color_lib = ::dlopen(DISPLAY_API_INTERFACE_LIBRARY_NAME, RTLD_NOW);
+    if (color_lib) {
+      color_mgr->color_apis_ = ::dlsym(color_lib, DISPLAY_API_FUNC_TABLES);
+      if (!color_mgr->color_apis_) {
+        DLOGE("Fail to retrieve = %s from %s", DISPLAY_API_FUNC_TABLES,
+              DISPLAY_API_INTERFACE_LIBRARY_NAME);
+        ::dlclose(color_lib);
+        delete color_mgr;
+        return NULL;
+      }
+    } else {
+      DLOGW("Unable to load = %s", DISPLAY_API_INTERFACE_LIBRARY_NAME);
+      delete color_mgr;
+      return NULL;
+    }
+    DLOGI("Successfully loaded %s", DISPLAY_API_INTERFACE_LIBRARY_NAME);
+
+    // Load diagclient library and invokes its entry point to pass in display APIs.
+    void *&diag_lib = color_mgr->diag_client_lib_;
+    diag_lib = ::dlopen(QDCM_DIAG_CLIENT_LIBRARY_NAME, RTLD_NOW);
+    if (diag_lib) {
+      *(reinterpret_cast<void **>(&color_mgr->qdcm_diag_init_)) =
+          ::dlsym(diag_lib, INIT_QDCM_DIAG_CLIENT_NAME);
+      *(reinterpret_cast<void **>(&color_mgr->qdcm_diag_deinit_)) =
+          ::dlsym(diag_lib, DEINIT_QDCM_DIAG_CLIENT_NAME);
+
+      if (!color_mgr->qdcm_diag_init_ || !color_mgr->qdcm_diag_deinit_) {
+        DLOGE("Fail to retrieve = %s from %s", INIT_QDCM_DIAG_CLIENT_NAME,
+              QDCM_DIAG_CLIENT_LIBRARY_NAME);
+        ::dlclose(diag_lib);
+      } else {
+        // invoke Diag Client entry point to initialize.
+        color_mgr->qdcm_diag_init_(color_mgr->color_apis_);
+        DLOGI("Successfully loaded %s and %s and diag_init'ed", DISPLAY_API_INTERFACE_LIBRARY_NAME,
+              QDCM_DIAG_CLIENT_LIBRARY_NAME);
+      }
+    } else {
+      DLOGW("Unable to load = %s", QDCM_DIAG_CLIENT_LIBRARY_NAME);
+      // only QDCM Diag client failed to be loaded and system still should function.
+    }
+  } else {
+    DLOGE("Unable to create HWCColorManager");
+    return NULL;
+  }
+
+  return color_mgr;
+}
+
+HWCColorManager::~HWCColorManager() {
+}
+
+void HWCColorManager::DestroyColorManager() {
+  if (qdcm_mode_mgr_) {
+    delete qdcm_mode_mgr_;
+  }
+  if (qdcm_diag_deinit_) {
+    qdcm_diag_deinit_();
+  }
+  if (diag_client_lib_) {
+    ::dlclose(diag_client_lib_);
+  }
+  if (color_apis_lib_) {
+    ::dlclose(color_apis_lib_);
+  }
+  delete this;
+}
+
+int HWCColorManager::EnableQDCMMode(bool enable, HWCDisplay *hwc_display) {
+  int ret = 0;
+
+  if (!qdcm_mode_mgr_) {
+    qdcm_mode_mgr_ = HWCQDCMModeManager::CreateQDCMModeMgr();
+    if (!qdcm_mode_mgr_) {
+      DLOGE("Unable to create QDCM operating mode manager.");
+      ret = -EFAULT;
+    }
+  }
+
+  if (qdcm_mode_mgr_) {
+    ret = qdcm_mode_mgr_->EnableQDCMMode(enable, hwc_display);
+  }
+
+  return ret;
+}
+
+bool HWCColorManager::SolidFillLayersPrepare(hwc_display_contents_1_t **displays,
+                                             HWCDisplay *hwc_display) {
+  SCOPE_LOCK(locker_);
+
+  // Query HWCColorManager if QDCM tool requesting SOLID_FILL mode.
+  uint32_t solid_fill_color = Get8BitsARGBColorValue(solid_fill_params_);
+  hwc_display_contents_1_t *layer_list = displays[HWC_DISPLAY_PRIMARY];
+
+  if (solid_fill_enable_ && solid_fill_layers_ && layer_list) {
+    // 1. shallow copy HWC_FRAMEBUFFER_TARGET layer info solid fill layer list.
+    solid_fill_layers_->hwLayers[1] = layer_list->hwLayers[layer_list->numHwLayers - 1];
+
+    // 2. continue the prepare<> on solid_fill_layers.
+    hwc_display->Perform(HWCDisplayPrimary::SET_QDCM_SOLID_FILL_INFO, solid_fill_color);
+    // TODO(user): Remove the display_contents generated here and
+    // use the solid fill layer support in HWC2 to set this up
+    // hwc_display->Prepare(solid_fill_layers_);  // RECT info included.
+
+    // 3. Set HWC_OVERLAY to all SF layers before returning to framework.
+    for (size_t i = 0; i < (layer_list->numHwLayers - 1); i++) {
+      hwc_layer_1_t *layer = &layer_list->hwLayers[i];
+      layer->compositionType = HWC_OVERLAY;
+    }
+
+    return true;
+  } else if (!solid_fill_enable_) {
+    hwc_display->Perform(HWCDisplayPrimary::UNSET_QDCM_SOLID_FILL_INFO, 0);
+  }
+
+  return false;
+}
+
+bool HWCColorManager::SolidFillLayersSet(hwc_display_contents_1_t **displays,
+                                         HWCDisplay *hwc_display) {
+  // Query HWCColorManager if QDCM tool requesting SOLID_FILL mode.
+  SCOPE_LOCK(locker_);
+  hwc_display_contents_1_t *layer_list = displays[HWC_DISPLAY_PRIMARY];
+  if (solid_fill_enable_ && solid_fill_layers_ && layer_list) {
+    // TODO(user): Present solid fill
+    // hwc_display->Commit(solid_fill_layers_);
+
+    // SurfaceFlinger layer stack is dropped in solid fill case and replaced with local layer stack
+    // Close acquire fence fds associated with SF layer stack
+    // Close release/retire fence fds returned along with local layer stack
+    for (size_t i = 0; i < (layer_list->numHwLayers - 1); i++) {
+      int &fence_fd = layer_list->hwLayers[i].acquireFenceFd;
+      if (fence_fd >= 0) {
+        close(fence_fd);
+        fence_fd = -1;
+      }
+    }
+
+    for (size_t i = 0; i < (solid_fill_layers_->numHwLayers - 1); i++) {
+      int &fence_fd = solid_fill_layers_->hwLayers[i].releaseFenceFd;
+      if (fence_fd >= 0) {
+        close(fence_fd);
+        fence_fd = -1;
+      }
+    }
+    if (solid_fill_layers_->retireFenceFd >= 0) {
+      close(solid_fill_layers_->retireFenceFd);
+      solid_fill_layers_->retireFenceFd = -1;
+    }
+
+    return true;
+  }
+
+  return false;
+}
+
+int HWCColorManager::CreateSolidFillLayers(HWCDisplay *hwc_display) {
+  int ret = 0;
+
+  if (!solid_fill_layers_) {
+    uint32_t size = sizeof(hwc_display_contents_1) + kNumSolidFillLayers * sizeof(hwc_layer_1_t);
+    uint32_t primary_width = 0;
+    uint32_t primary_height = 0;
+
+    hwc_display->GetPanelResolution(&primary_width, &primary_height);
+    uint8_t *buf = new uint8_t[size]();
+    // handle for solid fill layer with fd = -1.
+    private_handle_t *handle = new private_handle_t(-1, 0, private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
+                                                    BUFFER_TYPE_UI, HAL_PIXEL_FORMAT_RGBA_8888,
+                                                    INT32(primary_width), INT32(primary_height));
+
+    if (!buf || !handle) {
+      DLOGE("Failed to allocate memory.");
+      if (buf)
+        delete[] buf;
+      if (handle)
+        delete handle;
+
+      return -ENOMEM;
+    }
+
+    solid_fill_layers_ = reinterpret_cast<hwc_display_contents_1 *>(buf);
+    hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0];
+    layer.handle = handle;
+  }
+
+  solid_fill_layers_->flags = HWC_GEOMETRY_CHANGED;
+  solid_fill_layers_->numHwLayers = kNumSolidFillLayers;
+  solid_fill_layers_->retireFenceFd = -1;
+  solid_fill_layers_->outbuf = NULL;
+  solid_fill_layers_->outbufAcquireFenceFd = -1;
+
+  hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0];
+  hwc_rect_t solid_fill_rect = {
+      INT(solid_fill_params_.rect.x), INT(solid_fill_params_.rect.y),
+      solid_fill_params_.rect.x + INT(solid_fill_params_.rect.width),
+      solid_fill_params_.rect.y + INT(solid_fill_params_.rect.height),
+  };
+
+  layer.compositionType = HWC_FRAMEBUFFER;
+  layer.blending = HWC_BLENDING_PREMULT;
+  layer.sourceCropf.left = solid_fill_params_.rect.x;
+  layer.sourceCropf.top = solid_fill_params_.rect.y;
+  layer.sourceCropf.right = UINT32(solid_fill_params_.rect.x) + solid_fill_params_.rect.width;
+  layer.sourceCropf.bottom = UINT32(solid_fill_params_.rect.y) + solid_fill_params_.rect.height;
+  layer.acquireFenceFd = -1;
+  layer.releaseFenceFd = -1;
+  layer.flags = 0;
+  layer.transform = 0;
+  layer.hints = 0;
+  layer.planeAlpha = 0xff;
+  layer.displayFrame = solid_fill_rect;
+  layer.visibleRegionScreen.numRects = 1;
+  layer.visibleRegionScreen.rects = &layer.displayFrame;
+  layer.surfaceDamage.numRects = 0;
+
+  return ret;
+}
+
+void HWCColorManager::DestroySolidFillLayers() {
+  if (solid_fill_layers_) {
+    hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0];
+    uint8_t *buf = reinterpret_cast<uint8_t *>(solid_fill_layers_);
+    private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(layer.handle);
+
+    if (hnd)
+      delete hnd;
+
+    if (buf)
+      delete[] buf;
+
+    solid_fill_layers_ = NULL;
+  }
+}
+
+int HWCColorManager::SetSolidFill(const void *params, bool enable, HWCDisplay *hwc_display) {
+  SCOPE_LOCK(locker_);
+  int ret = 0;
+
+  if (params) {
+    solid_fill_params_ = *reinterpret_cast<const PPColorFillParams *>(params);
+  } else {
+    solid_fill_params_ = PPColorFillParams();
+  }
+
+  if (enable) {
+    // will create solid fill layers for rendering if not present.
+    ret = CreateSolidFillLayers(hwc_display);
+  } else {
+    DestroySolidFillLayers();
+  }
+  solid_fill_enable_ = enable;
+
+  return ret;
+}
+
+int HWCColorManager::SetFrameCapture(void *params, bool enable, HWCDisplay *hwc_display) {
+  SCOPE_LOCK(locker_);
+  int ret = 0;
+
+  PPFrameCaptureData *frame_capture_data = reinterpret_cast<PPFrameCaptureData *>(params);
+
+  if (enable) {
+    std::memset(&buffer_info, 0x00, sizeof(buffer_info));
+    hwc_display->GetFrameBufferResolution(&buffer_info.buffer_config.width,
+                                          &buffer_info.buffer_config.height);
+    if (frame_capture_data->input_params.out_pix_format == PP_PIXEL_FORMAT_RGB_888) {
+      buffer_info.buffer_config.format = kFormatRGB888;
+    } else if (frame_capture_data->input_params.out_pix_format == PP_PIXEL_FORMAT_RGB_2101010) {
+      // TODO(user): Complete the implementation
+      DLOGE("RGB 10-bit format NOT supported");
+      return -EFAULT;
+    } else {
+      DLOGE("Pixel-format: %d NOT support.", frame_capture_data->input_params.out_pix_format);
+      return -EFAULT;
+    }
+
+    buffer_info.buffer_config.buffer_count = 1;
+    buffer_info.alloc_buffer_info.fd = -1;
+    buffer_info.alloc_buffer_info.stride = 0;
+    buffer_info.alloc_buffer_info.size = 0;
+
+    buffer_allocator_ = new HWCBufferAllocator();
+    if (buffer_allocator_ == NULL) {
+      DLOGE("Memory allocation for buffer_allocator_ FAILED");
+      return -ENOMEM;
+    }
+
+    ret = buffer_allocator_->AllocateBuffer(&buffer_info);
+    if (ret != 0) {
+      DLOGE("Buffer allocation failed. ret: %d", ret);
+      delete[] buffer_allocator_;
+      buffer_allocator_ = NULL;
+      return -ENOMEM;
+    } else {
+      void *buffer = mmap(NULL, buffer_info.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
+                          MAP_SHARED, buffer_info.alloc_buffer_info.fd, 0);
+
+      if (buffer == MAP_FAILED) {
+        DLOGE("mmap failed. err = %d", errno);
+        frame_capture_data->buffer = NULL;
+        ret = buffer_allocator_->FreeBuffer(&buffer_info);
+        delete[] buffer_allocator_;
+        buffer_allocator_ = NULL;
+        return -EFAULT;
+      } else {
+        frame_capture_data->buffer = reinterpret_cast<uint8_t *>(buffer);
+        frame_capture_data->buffer_stride = buffer_info.alloc_buffer_info.stride;
+        frame_capture_data->buffer_size = buffer_info.alloc_buffer_info.size;
+      }
+      // TODO(user): Call HWC interface to provide the buffer and rectangle information
+    }
+  } else {
+    if (frame_capture_data->buffer != NULL) {
+      if (munmap(frame_capture_data->buffer, buffer_info.alloc_buffer_info.size) != 0) {
+        DLOGE("munmap failed. err = %d", errno);
+      }
+    }
+    if (buffer_allocator_ != NULL) {
+      std::memset(frame_capture_data, 0x00, sizeof(PPFrameCaptureData));
+      ret = buffer_allocator_->FreeBuffer(&buffer_info);
+      if (ret != 0) {
+        DLOGE("FreeBuffer failed. ret = %d", ret);
+      }
+      delete[] buffer_allocator_;
+      buffer_allocator_ = NULL;
+    }
+  }
+  return ret;
+}
+
+const HWCQDCMModeManager::ActiveFeatureCMD HWCQDCMModeManager::kActiveFeatureCMD[] = {
+    HWCQDCMModeManager::ActiveFeatureCMD("cabl:on", "cabl:off", "cabl:status", "running"),
+    HWCQDCMModeManager::ActiveFeatureCMD("ad:on", "ad:off", "ad:query:status", "running"),
+    HWCQDCMModeManager::ActiveFeatureCMD("svi:on", "svi:off", "svi:status", "running"),
+};
+
+const char *const HWCQDCMModeManager::kSocketName = "pps";
+const char *const HWCQDCMModeManager::kTagName = "surfaceflinger";
+const char *const HWCQDCMModeManager::kPackageName = "colormanager";
+
+HWCQDCMModeManager *HWCQDCMModeManager::CreateQDCMModeMgr() {
+  HWCQDCMModeManager *mode_mgr = new HWCQDCMModeManager();
+
+  if (!mode_mgr) {
+    DLOGW("No memory to create HWCQDCMModeManager.");
+    return NULL;
+  } else {
+    mode_mgr->socket_fd_ =
+        ::socket_local_client(kSocketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
+    if (mode_mgr->socket_fd_ < 0) {
+      // it should not be disastrous and we still can grab wakelock in QDCM mode.
+      DLOGW("Unable to connect to dpps socket!");
+    }
+
+    // retrieve system GPU idle timeout value for later to recover.
+    mode_mgr->entry_timeout_ = UINT32(HWCDebugHandler::GetIdleTimeoutMs());
+
+    // acquire the binder handle to Android system PowerManager for later use.
+    android::sp<android::IBinder> binder =
+        android::defaultServiceManager()->checkService(android::String16("power"));
+    if (binder == NULL) {
+      DLOGW("Application can't connect to  power manager service");
+      delete mode_mgr;
+      mode_mgr = NULL;
+    } else {
+      mode_mgr->power_mgr_ = android::interface_cast<android::IPowerManager>(binder);
+    }
+  }
+
+  return mode_mgr;
+}
+
+HWCQDCMModeManager::~HWCQDCMModeManager() {
+  if (socket_fd_ >= 0)
+    ::close(socket_fd_);
+}
+
+int HWCQDCMModeManager::AcquireAndroidWakeLock(bool enable) {
+  int ret = 0;
+
+  if (enable) {
+    if (wakelock_token_ == NULL) {
+      android::sp<android::IBinder> binder = new android::BBinder();
+      android::status_t status = power_mgr_->acquireWakeLock(
+          (kFullWakeLock | kAcquireCauseWakeup | kONAfterRelease), binder,
+          android::String16(kTagName), android::String16(kPackageName));
+      if (status == android::NO_ERROR) {
+        wakelock_token_ = binder;
+      }
+    }
+  } else {
+    if (wakelock_token_ != NULL && power_mgr_ != NULL) {
+      power_mgr_->releaseWakeLock(wakelock_token_, 0);
+      wakelock_token_.clear();
+      wakelock_token_ = NULL;
+    }
+  }
+
+  return ret;
+}
+
+int HWCQDCMModeManager::EnableActiveFeatures(bool enable,
+                                             const HWCQDCMModeManager::ActiveFeatureCMD &cmds,
+                                             bool *was_running) {
+  int ret = 0;
+  ssize_t size = 0;
+  char response[kSocketCMDMaxLength] = {
+      0,
+  };
+
+  if (socket_fd_ < 0) {
+    DLOGW("No socket connection available!");
+    return -EFAULT;
+  }
+
+  if (!enable) {  // if client requesting to disable it.
+    // query CABL status, if off, no action. keep the status.
+    size = ::write(socket_fd_, cmds.cmd_query_status, strlen(cmds.cmd_query_status));
+    if (size < 0) {
+      DLOGW("Unable to send data over socket %s", ::strerror(errno));
+      ret = -EFAULT;
+    } else {
+      size = ::read(socket_fd_, response, kSocketCMDMaxLength);
+      if (size < 0) {
+        DLOGW("Unable to read data over socket %s", ::strerror(errno));
+        ret = -EFAULT;
+      } else if (!strncmp(response, cmds.running, strlen(cmds.running))) {
+        *was_running = true;
+      }
+    }
+
+    if (*was_running) {  // if was running, it's requested to disable it.
+      size = ::write(socket_fd_, cmds.cmd_off, strlen(cmds.cmd_off));
+      if (size < 0) {
+        DLOGW("Unable to send data over socket %s", ::strerror(errno));
+        ret = -EFAULT;
+      }
+    }
+  } else {  // if was running, need enable it back.
+    if (*was_running) {
+      size = ::write(socket_fd_, cmds.cmd_on, strlen(cmds.cmd_on));
+      if (size < 0) {
+        DLOGW("Unable to send data over socket %s", ::strerror(errno));
+        ret = -EFAULT;
+      }
+    }
+  }
+
+  return ret;
+}
+
+int HWCQDCMModeManager::EnableQDCMMode(bool enable, HWCDisplay *hwc_display) {
+  int ret = 0;
+
+  ret = EnableActiveFeatures((enable ? false : true), kActiveFeatureCMD[kCABLFeature],
+                             &cabl_was_running_);
+  ret = AcquireAndroidWakeLock(enable);
+
+  // if enter QDCM mode, disable GPU fallback idle timeout.
+  if (hwc_display) {
+    uint32_t timeout = enable ? 0 : entry_timeout_;
+    hwc_display->SetIdleTimeoutMs(timeout);
+  }
+
+  return ret;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_display.cpp b/msm8996/sdm/libs/hwc2/hwc_display.cpp
new file mode 100644
index 0000000..d9b3c73
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display.cpp
@@ -0,0 +1,1370 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <math.h>
+#include <errno.h>
+#include <gralloc_priv.h>
+#include <gr.h>
+#include <utils/constants.h>
+#include <utils/formats.h>
+#include <utils/rect.h>
+#include <utils/debug.h>
+#include <sync/sync.h>
+#include <cutils/properties.h>
+#include <map>
+#include <string>
+#include <utility>
+
+#include "hwc_display.h"
+#include "hwc_debugger.h"
+#include "blit_engine_c2d.h"
+
+#ifdef QTI_BSP
+#include <hardware/display_defs.h>
+#endif
+
+#define __CLASS__ "HWCDisplay"
+
+namespace sdm {
+
+static void ApplyDeInterlaceAdjustment(Layer *layer) {
+  // De-interlacing adjustment
+  if (layer->input_buffer->flags.interlace) {
+    float height = (layer->src_rect.bottom - layer->src_rect.top) / 2.0f;
+    layer->src_rect.top = ROUND_UP_ALIGN_DOWN(layer->src_rect.top / 2.0f, 2);
+    layer->src_rect.bottom = layer->src_rect.top + floorf(height);
+  }
+}
+
+HWCDisplay::HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type,
+                       hwc2_display_t id, bool needs_blit, qService::QService *qservice,
+                       DisplayClass display_class)
+    : core_intf_(core_intf),
+      callbacks_(callbacks),
+      type_(type),
+      id_(id),
+      needs_blit_(needs_blit),
+      qservice_(qservice),
+      display_class_(display_class) {
+}
+
+int HWCDisplay::Init() {
+  DisplayError error = core_intf_->CreateDisplay(type_, this, &display_intf_);
+  if (error != kErrorNone) {
+    DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p", error,
+          type_, this, &display_intf_);
+    return -EINVAL;
+  }
+
+  int property_swap_interval = 1;
+  HWCDebugHandler::Get()->GetProperty("debug.egl.swapinterval", &property_swap_interval);
+  if (property_swap_interval == 0) {
+    swap_interval_zero_ = true;
+  }
+
+  framebuffer_config_ = new DisplayConfigVariableInfo();
+  if (!framebuffer_config_) {
+    DLOGV("Failed to allocate memory for custom framebuffer config.");
+    core_intf_->DestroyDisplay(display_intf_);
+    return -EINVAL;
+  }
+
+  client_target_ = new HWCLayer(id_);
+  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;
+      }
+    }
+  }
+
+  display_intf_->GetRefreshRateRange(&min_refresh_rate_, &max_refresh_rate_);
+  current_refresh_rate_ = max_refresh_rate_;
+  DLOGI("Display created with id: %d", id_);
+  return 0;
+}
+
+int HWCDisplay::Deinit() {
+  DisplayError error = core_intf_->DestroyDisplay(display_intf_);
+  if (error != kErrorNone) {
+    DLOGE("Display destroy failed. Error = %d", error);
+    return -EINVAL;
+  }
+
+  delete framebuffer_config_;
+  delete client_target_;
+
+  if (blit_engine_) {
+    blit_engine_->DeInit();
+    delete blit_engine_;
+    blit_engine_ = NULL;
+  }
+
+  return 0;
+}
+
+// LayerStack operations
+HWC2::Error HWCDisplay::CreateLayer(hwc2_layer_t *out_layer_id) {
+  auto layer = *layer_set_.emplace(new HWCLayer(id_));
+  layer_map_.emplace(std::make_pair(layer->GetId(), layer));
+  *out_layer_id = layer->GetId();
+  geometry_changes_ = GeometryChanges::kAdded;
+  return HWC2::Error::None;
+}
+
+HWCLayer *HWCDisplay::GetHWCLayer(hwc2_layer_t layer_id) {
+  const auto map_layer = layer_map_.find(layer_id);
+  if (map_layer == layer_map_.end()) {
+    DLOGE("[%" PRIu64 "] GetLayer(%" PRIu64 ") failed: no such layer", id_, layer_id);
+    return nullptr;
+  } else {
+    return map_layer->second;
+  }
+}
+
+HWC2::Error HWCDisplay::DestroyLayer(hwc2_layer_t layer_id) {
+  const auto map_layer = layer_map_.find(layer_id);
+  if (map_layer == layer_map_.end()) {
+    DLOGE("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer", id_, layer_id);
+    return HWC2::Error::BadLayer;
+  }
+  const auto layer = map_layer->second;
+  layer_map_.erase(map_layer);
+  const auto z_range = layer_set_.equal_range(layer);
+  for (auto current = z_range.first; current != z_range.second; ++current) {
+    if (*current == layer) {
+      current = layer_set_.erase(current);
+      break;
+    }
+  }
+
+  geometry_changes_ = GeometryChanges::kRemoved;
+  return HWC2::Error::None;
+}
+
+void HWCDisplay::BuildLayerStack() {
+  // TODO(user): Validate
+  validated_ = true;
+  layer_stack_ = LayerStack();
+  display_rect_ = LayerRect();
+  metadata_refresh_rate_ = 0;
+
+  // Add one layer for fb target
+  // TODO(user): Add blit target layers
+  for (auto hwc_layer : layer_set_) {
+    Layer *layer = hwc_layer->GetSDMLayer();
+
+    if (swap_interval_zero_) {
+      if (layer->input_buffer->acquire_fence_fd >= 0) {
+        close(layer->input_buffer->acquire_fence_fd);
+        layer->input_buffer->acquire_fence_fd = -1;
+      }
+    }
+
+    const private_handle_t *handle =
+        reinterpret_cast<const private_handle_t *>(layer->input_buffer->buffer_id);
+    if (handle) {
+      if (handle->bufferType == BUFFER_TYPE_VIDEO) {
+        layer_stack_.flags.video_present = true;
+      }
+      // TZ Protected Buffer - L1
+      if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
+        layer_stack_.flags.secure_present = true;
+      }
+      // Gralloc Usage Protected Buffer - L3 - which needs to be treated as Secure & avoid fallback
+      if (handle->flags & private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER) {
+        layer_stack_.flags.secure_present = true;
+      }
+    }
+
+    if (layer->flags.skip) {
+      layer_stack_.flags.skip_present = true;
+    }
+
+    if (layer->flags.cursor) {
+      layer_stack_.flags.cursor_present = true;
+    }
+    // TODO(user): Move to a getter if this is needed at other places
+    hwc_rect_t scaled_display_frame = {INT(layer->dst_rect.left), INT(layer->dst_rect.top),
+                                       INT(layer->dst_rect.right), INT(layer->dst_rect.bottom)};
+    ScaleDisplayFrame(&scaled_display_frame);
+    ApplyScanAdjustment(&scaled_display_frame);
+    hwc_layer->SetLayerDisplayFrame(scaled_display_frame);
+    ApplyDeInterlaceAdjustment(layer);
+    // TODO(user): Verify if we still need to configure the solid fill layerbuffer,
+    // it should already have a valid dst_rect by this point
+
+    if (layer->frame_rate > metadata_refresh_rate_) {
+      metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate);
+    } else {
+      layer->frame_rate = current_refresh_rate_;
+    }
+    display_rect_ = Union(display_rect_, layer->dst_rect);
+    // TODO(user): Set correctly when implementing caching
+    layer->flags.updating = true;
+    geometry_changes_ |= hwc_layer->GetGeometryChanges();
+
+    layer_stack_.layers.push_back(layer);
+  }
+  layer_stack_.flags.geometry_changed = UINT32(geometry_changes_ > 0);
+  // Append client target to the layer stack
+  layer_stack_.layers.push_back(client_target_->GetSDMLayer());
+}
+
+HWC2::Error HWCDisplay::SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z) {
+  const auto map_layer = layer_map_.find(layer_id);
+  if (map_layer == layer_map_.end()) {
+    DLOGE("[%" PRIu64 "] updateLayerZ failed to find layer", id_);
+    return HWC2::Error::BadLayer;
+  }
+
+  const auto layer = map_layer->second;
+  const auto z_range = layer_set_.equal_range(layer);
+  bool layer_on_display = false;
+  for (auto current = z_range.first; current != z_range.second; ++current) {
+    if (*current == layer) {
+      if ((*current)->GetZ() == z) {
+        // Don't change anything if the Z hasn't changed
+        return HWC2::Error::None;
+      }
+      current = layer_set_.erase(current);
+      layer_on_display = true;
+      break;
+    }
+  }
+
+  if (!layer_on_display) {
+    DLOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display", id_);
+    return HWC2::Error::BadLayer;
+  }
+
+  layer->SetLayerZOrder(z);
+  layer_set_.emplace(layer);
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::SetVsyncEnabled(HWC2::Vsync enabled) {
+  DLOGV("Display ID: %d enabled: %s", id_, to_string(enabled).c_str());
+  DisplayError error = kErrorNone;
+
+  if (shutdown_pending_) {
+    return HWC2::Error::None;
+  }
+
+  bool state;
+  if (enabled == HWC2::Vsync::Enable)
+    state = true;
+  else if (enabled == HWC2::Vsync::Disable)
+    state = false;
+  else
+    return HWC2::Error::BadParameter;
+
+  error = display_intf_->SetVSyncState(state);
+
+  if (error != kErrorNone) {
+    if (error == kErrorShutDown) {
+      shutdown_pending_ = true;
+      return HWC2::Error::None;
+    }
+    DLOGE("Failed. enabled = %s, error = %d", to_string(enabled).c_str(), error);
+    return HWC2::Error::BadDisplay;
+  }
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::SetPowerMode(HWC2::PowerMode mode) {
+  DLOGV("display = %d, mode = %s", id_, to_string(mode).c_str());
+  DisplayState state = kStateOff;
+  bool flush_on_error = flush_on_error_;
+
+  if (shutdown_pending_) {
+    return HWC2::Error::None;
+  }
+
+  switch (mode) {
+    case HWC2::PowerMode::Off:
+      // During power off, all of the buffers are released.
+      // Do not flush until a buffer is successfully submitted again.
+      flush_on_error = false;
+      state = kStateOff;
+      break;
+    case HWC2::PowerMode::On:
+      state = kStateOn;
+      last_power_mode_ = HWC2::PowerMode::On;
+      break;
+    case HWC2::PowerMode::Doze:
+      state = kStateDoze;
+      last_power_mode_ = HWC2::PowerMode::Doze;
+      break;
+    case HWC2::PowerMode::DozeSuspend:
+      state = kStateDozeSuspend;
+      last_power_mode_ = HWC2::PowerMode::DozeSuspend;
+      break;
+    default:
+      return HWC2::Error::BadParameter;
+  }
+
+  DisplayError error = display_intf_->SetDisplayState(state);
+  if (error == kErrorNone) {
+    flush_on_error_ = flush_on_error;
+  } else {
+    if (error == kErrorShutDown) {
+      shutdown_pending_ = true;
+      return HWC2::Error::None;
+    }
+    DLOGE("Set state failed. Error = %d", error);
+    return HWC2::Error::BadParameter;
+  }
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format,
+                                               int32_t dataspace) {
+  // TODO(user): Support scaled configurations, other formats and other dataspaces
+  if (format != HAL_PIXEL_FORMAT_RGBA_8888 || dataspace != HAL_DATASPACE_UNKNOWN ||
+      width != framebuffer_config_->x_pixels || height != framebuffer_config_->y_pixels) {
+    return HWC2::Error::Unsupported;
+  } else {
+    return HWC2::Error::None;
+  }
+}
+
+HWC2::Error HWCDisplay::GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs) {
+  // TODO(user): Actually handle multiple configs
+  if (out_configs == nullptr) {
+    *out_num_configs = 1;
+  } else {
+    *out_num_configs = 1;
+    out_configs[0] = 0;
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute,
+                                            int32_t *out_value) {
+  DisplayConfigVariableInfo variable_config = *framebuffer_config_;
+
+  switch (attribute) {
+    case HWC2::Attribute::VsyncPeriod:
+      *out_value = INT32(variable_config.vsync_period_ns);
+      break;
+    case HWC2::Attribute::Width:
+      *out_value = INT32(variable_config.x_pixels);
+      break;
+    case HWC2::Attribute::Height:
+      *out_value = INT32(variable_config.y_pixels);
+      break;
+    case HWC2::Attribute::DpiX:
+      *out_value = INT32(variable_config.x_dpi * 1000.0f);
+      break;
+    case HWC2::Attribute::DpiY:
+      *out_value = INT32(variable_config.y_dpi * 1000.0f);
+      break;
+    default:
+      DLOGW("Spurious attribute type = %s", to_string(attribute).c_str());
+      return HWC2::Error::BadConfig;
+  }
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetDisplayName(uint32_t *out_size, char *out_name) {
+  // TODO(user): Get panel name and EDID name and populate it here
+  if (out_name == nullptr) {
+    *out_size = 32;
+  } else {
+    std::string name;
+    switch (id_) {
+      case HWC_DISPLAY_PRIMARY:
+        name = "Primary Display";
+        break;
+      case HWC_DISPLAY_EXTERNAL:
+        name = "External Display";
+        break;
+      case HWC_DISPLAY_VIRTUAL:
+        name = "Virtual Display";
+        break;
+      default:
+        name = "Unknown";
+        break;
+    }
+    std::strncpy(out_name, name.c_str(), name.size());
+    *out_size = UINT32(name.size());
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetDisplayType(int32_t *out_type) {
+  if (out_type != nullptr) {
+    if (id_ == HWC_DISPLAY_VIRTUAL) {
+      *out_type = HWC2_DISPLAY_TYPE_VIRTUAL;
+    } else {
+      *out_type = HWC2_DISPLAY_TYPE_PHYSICAL;
+    }
+    return HWC2::Error::None;
+  } else {
+    return HWC2::Error::BadParameter;
+  }
+}
+
+// TODO(user): Store configurations and hook them up here
+HWC2::Error HWCDisplay::GetActiveConfig(hwc2_config_t *out_config) {
+  if (out_config != nullptr) {
+    *out_config = 0;
+    return HWC2::Error::None;
+  } else {
+    return HWC2::Error::BadParameter;
+  }
+}
+
+HWC2::Error HWCDisplay::SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
+                                        int32_t dataspace) {
+  client_target_->SetLayerBuffer(target, acquire_fence);
+  // Ignoring dataspace for now
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::SetActiveConfig(hwc2_config_t config) {
+  // We have only one config right now - do nothing
+  return HWC2::Error::None;
+}
+
+void HWCDisplay::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
+  dump_frame_count_ = count;
+  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_);
+}
+
+HWC2::PowerMode HWCDisplay::GetLastPowerMode() {
+  return last_power_mode_;
+}
+
+DisplayError HWCDisplay::VSync(const DisplayEventVSync &vsync) {
+  callbacks_->Vsync(id_, vsync.timestamp);
+  return kErrorNone;
+}
+
+DisplayError HWCDisplay::Refresh() {
+  return kErrorNotSupported;
+}
+
+DisplayError HWCDisplay::CECMessage(char *message) {
+  if (qservice_) {
+    qservice_->onCECMessageReceived(message, 0);
+  } else {
+    DLOGW("Qservice instance not available.");
+  }
+
+  return kErrorNone;
+}
+
+HWC2::Error HWCDisplay::PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests) {
+  layer_changes_.clear();
+  layer_requests_.clear();
+  if (shutdown_pending_) {
+    return HWC2::Error::BadDisplay;
+  }
+
+  if (!skip_prepare_) {
+    DisplayError error = display_intf_->Prepare(&layer_stack_);
+    if (error != kErrorNone) {
+      if (error == kErrorShutDown) {
+        shutdown_pending_ = true;
+      } else if (error != kErrorPermission) {
+        DLOGE("Prepare failed. Error = %d", error);
+        // To prevent surfaceflinger infinite wait, flush the previous frame during Commit()
+        // so that previous buffer and fences are released, and override the error.
+        flush_ = true;
+      }
+      return HWC2::Error::BadDisplay;
+    }
+  } else {
+    // Skip is not set
+    MarkLayersForGPUBypass();
+    skip_prepare_ = false;
+    DLOGI("SecureDisplay %s, Skip Prepare/Commit and Flush",
+          secure_display_active_ ? "Starting" : "Stopping");
+    flush_ = true;
+  }
+
+  // If current draw cycle has different set of layers updating in comparison to previous cycle,
+  // cache content using GPU again.
+  // If set of updating layers remains same, use cached buffer and replace layers marked for GPU
+  // composition with SDE so that SurfaceFlinger does not compose them. Set cache inuse here.
+  bool needs_fb_refresh = NeedsFrameBufferRefresh();
+  for (auto hwc_layer : layer_set_) {
+    Layer *layer = hwc_layer->GetSDMLayer();
+    LayerComposition &composition = layer->composition;
+
+    if ((composition == kCompositionSDE) || (composition == kCompositionHybrid) ||
+        (composition == kCompositionBlit)) {
+      layer_requests_[hwc_layer->GetId()] = HWC2::LayerRequest::ClearClientTarget;
+    }
+
+    if (!needs_fb_refresh && composition == kCompositionGPU) {
+      composition = kCompositionSDE;
+    }
+    hwc_layer->SetComposition(composition);
+    if (hwc_layer->CompositionChanged()) {
+      layer_changes_[hwc_layer->GetId()] = hwc_layer->GetCompositionType();
+    }
+  }
+  *out_num_types = UINT32(layer_changes_.size());
+  *out_num_requests = UINT32(layer_requests_.size());
+  validated_ = true;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::AcceptDisplayChanges() {
+  if (!validated_) {
+    return HWC2::Error::NotValidated;
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetChangedCompositionTypes(uint32_t *out_num_elements,
+                                                   hwc2_layer_t *out_layers, int32_t *out_types) {
+  *out_num_elements = UINT32(layer_changes_.size());
+  if (out_layers != nullptr && out_types != nullptr) {
+    int i = 0;
+    for (auto change : layer_changes_) {
+      out_layers[i] = change.first;
+      out_types[i] = INT32(change.second);
+      i++;
+    }
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers,
+                                         int32_t *out_fences) {
+  if (out_layers != nullptr && out_fences != nullptr) {
+    int i = 0;
+    for (auto hwc_layer : layer_set_) {
+      out_layers[i] = hwc_layer->GetId();
+      out_fences[i] = hwc_layer->PopReleaseFence();
+      i++;
+    }
+  }
+  *out_num_elements = UINT32(layer_set_.size());
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::GetDisplayRequests(int32_t *out_display_requests,
+                                           uint32_t *out_num_elements, hwc2_layer_t *out_layers,
+                                           int32_t *out_layer_requests) {
+  // No display requests for now
+  // Use for sharing blit buffers and
+  // writing wfd buffer directly to output if there is full GPU composition
+  // and no color conversion needed
+  *out_display_requests = 0;
+  *out_num_elements = UINT32(layer_requests_.size());
+  if (out_layers != nullptr && out_layer_requests != nullptr) {
+    int i = 0;
+    for (auto &request : layer_requests_) {
+      out_layers[i] = request.first;
+      out_layer_requests[i] = INT32(request.second);
+      i++;
+    }
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::CommitLayerStack(void) {
+  if (shutdown_pending_) {
+    return HWC2::Error::None;
+  }
+
+  if (!validated_) {
+    DLOGW("Display is not validated");
+    return HWC2::Error::NotValidated;
+  }
+
+  DumpInputBuffers();
+
+  if (!flush_) {
+    DisplayError error = kErrorUndefined;
+    error = display_intf_->Commit(&layer_stack_);
+
+    if (error == kErrorNone) {
+      // A commit is successfully submitted, start flushing on failure now onwards.
+      flush_on_error_ = true;
+    } else {
+      if (error == kErrorShutDown) {
+        shutdown_pending_ = true;
+        return HWC2::Error::Unsupported;
+      } else if (error != kErrorPermission) {
+        DLOGE("Commit failed. Error = %d", error);
+        // To prevent surfaceflinger infinite wait, flush the previous frame during Commit()
+        // so that previous buffer and fences are released, and override the error.
+        flush_ = true;
+      }
+    }
+  }
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCDisplay::PostCommitLayerStack(int32_t *out_retire_fence) {
+  auto status = HWC2::Error::None;
+
+  // Do no call flush on errors, if a successful buffer is never submitted.
+  if (flush_ && flush_on_error_) {
+    display_intf_->Flush();
+  }
+
+  // TODO(user): No way to set the client target release fence on SF
+  int32_t &client_target_release_fence =
+      client_target_->GetSDMLayer()->input_buffer->release_fence_fd;
+  if (client_target_release_fence >= 0) {
+    close(client_target_release_fence);
+    client_target_release_fence = -1;
+  }
+
+  for (auto hwc_layer : layer_set_) {
+    hwc_layer->ResetGeometryChanges();
+    Layer *layer = hwc_layer->GetSDMLayer();
+    LayerBuffer *layer_buffer = layer->input_buffer;
+
+    if (!flush_) {
+      // If swapinterval property is set to 0 or for single buffer layers, do not update f/w
+      // release fences and discard fences from driver
+      if (swap_interval_zero_ || layer->flags.single_buffer) {
+        close(layer_buffer->release_fence_fd);
+        layer_buffer->release_fence_fd = -1;
+      } else if (layer->composition != kCompositionGPU) {
+        hwc_layer->PushReleaseFence(layer_buffer->release_fence_fd);
+      }
+    }
+
+    if (layer_buffer->acquire_fence_fd >= 0) {
+      close(layer_buffer->acquire_fence_fd);
+      layer_buffer->acquire_fence_fd = -1;
+    }
+  }
+
+  if (!flush_) {
+    // if swapinterval property is set to 0 then close and reset the list retire fence
+    if (swap_interval_zero_) {
+      close(layer_stack_.retire_fence_fd);
+      layer_stack_.retire_fence_fd = -1;
+    }
+    *out_retire_fence = stored_retire_fence_;
+    stored_retire_fence_ = layer_stack_.retire_fence_fd;
+
+    if (dump_frame_count_) {
+      dump_frame_count_--;
+      dump_frame_index_++;
+    }
+  }
+  geometry_changes_ = GeometryChanges::kNone;
+  flush_ = false;
+
+  return status;
+}
+
+bool HWCDisplay::NeedsFrameBufferRefresh(void) {
+  // Frame buffer needs to be refreshed for the following reasons:
+  // 1. Any layer is marked skip in the current layer stack.
+  // 2. Any layer is added/removed/layer properties changes in the current layer stack.
+  // 3. Any layer handle is changed and it is marked for GPU composition
+  // 4. Any layer's current composition is different from previous composition.
+  if (layer_stack_.flags.skip_present || layer_stack_.flags.geometry_changed) {
+    return true;
+  }
+
+  for (auto layer : layer_stack_.layers) {
+    // need FB refresh for s3d case
+    if (layer->input_buffer->s3d_format != kS3dFormatNone) {
+      return true;
+    }
+
+    if (layer->composition == kCompositionGPUTarget ||
+        layer->composition == kCompositionBlitTarget) {
+      continue;
+    }
+  }
+
+  return false;
+}
+
+void HWCDisplay::SetIdleTimeoutMs(uint32_t timeout_ms) {
+  return;
+}
+
+DisplayError HWCDisplay::SetMaxMixerStages(uint32_t max_mixer_stages) {
+  DisplayError error = kErrorNone;
+
+  if (display_intf_) {
+    error = display_intf_->SetMaxMixerStages(max_mixer_stages);
+  }
+
+  return error;
+}
+
+DisplayError HWCDisplay::ControlPartialUpdate(bool enable, uint32_t *pending) {
+  DisplayError error = kErrorNone;
+
+  if (display_intf_) {
+    error = display_intf_->ControlPartialUpdate(enable, pending);
+  }
+
+  return error;
+}
+
+LayerBufferFormat HWCDisplay::GetSDMFormat(const int32_t &source, const int flags) {
+  LayerBufferFormat format = kFormatInvalid;
+  if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
+    switch (source) {
+      case HAL_PIXEL_FORMAT_RGBA_8888:
+        format = kFormatRGBA8888Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_RGBX_8888:
+        format = kFormatRGBX8888Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_BGR_565:
+        format = kFormatBGR565Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+      case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+      case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+        format = kFormatYCbCr420SPVenusUbwc;
+        break;
+      case HAL_PIXEL_FORMAT_RGBA_1010102:
+        format = kFormatRGBA1010102Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_RGBX_1010102:
+        format = kFormatRGBX1010102Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+        format = kFormatYCbCr420TP10Ubwc;
+        break;
+      default:
+        DLOGE("Unsupported format type for UBWC %d", source);
+        return kFormatInvalid;
+    }
+    return format;
+  }
+
+  switch (source) {
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+      format = kFormatRGBA8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGBA_5551:
+      format = kFormatRGBA5551;
+      break;
+    case HAL_PIXEL_FORMAT_RGBA_4444:
+      format = kFormatRGBA4444;
+      break;
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+      format = kFormatBGRA8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+      format = kFormatRGBX8888;
+      break;
+    case HAL_PIXEL_FORMAT_BGRX_8888:
+      format = kFormatBGRX8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGB_888:
+      format = kFormatRGB888;
+      break;
+    case HAL_PIXEL_FORMAT_RGB_565:
+      format = kFormatRGB565;
+      break;
+    case HAL_PIXEL_FORMAT_BGR_565:
+      format = kFormatBGR565;
+      break;
+    case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+      format = kFormatYCbCr420SemiPlanarVenus;
+      break;
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+      format = kFormatYCrCb420SemiPlanarVenus;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+      format = kFormatYCbCr420SPVenusUbwc;
+      break;
+    case HAL_PIXEL_FORMAT_YV12:
+      format = kFormatYCrCb420PlanarStride16;
+      break;
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+      format = kFormatYCrCb420SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+      format = kFormatYCbCr420SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+      format = kFormatYCbCr422H2V1SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_422_I:
+      format = kFormatYCbCr422H2V1Packed;
+      break;
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+      format = kFormatRGBA1010102;
+      break;
+    case HAL_PIXEL_FORMAT_ARGB_2101010:
+      format = kFormatARGB2101010;
+      break;
+    case HAL_PIXEL_FORMAT_RGBX_1010102:
+      format = kFormatRGBX1010102;
+      break;
+    case HAL_PIXEL_FORMAT_XRGB_2101010:
+      format = kFormatXRGB2101010;
+      break;
+    case HAL_PIXEL_FORMAT_BGRA_1010102:
+      format = kFormatBGRA1010102;
+      break;
+    case HAL_PIXEL_FORMAT_ABGR_2101010:
+      format = kFormatABGR2101010;
+      break;
+    case HAL_PIXEL_FORMAT_BGRX_1010102:
+      format = kFormatBGRX1010102;
+      break;
+    case HAL_PIXEL_FORMAT_XBGR_2101010:
+      format = kFormatXBGR2101010;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+      format = kFormatYCbCr420P010;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+      format = kFormatYCbCr420TP10Ubwc;
+      break;
+    default:
+      DLOGW("Unsupported format type = %d", source);
+      return kFormatInvalid;
+  }
+
+  return format;
+}
+
+void HWCDisplay::DumpInputBuffers() {
+  char dir_path[PATH_MAX];
+
+  if (!dump_frame_count_ || flush_ || !dump_input_layers_) {
+    return;
+  }
+
+  snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
+
+  if (mkdir(dir_path, 0777) != 0 && errno != EEXIST) {
+    DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
+    return;
+  }
+
+  // if directory exists already, need to explicitly change the permission.
+  if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
+    DLOGW("Failed to change permissions on %s directory", dir_path);
+    return;
+  }
+
+  for (uint32_t i = 0; i < layer_stack_.layers.size(); i++) {
+    auto layer = layer_stack_.layers.at(i);
+    const private_handle_t *pvt_handle =
+        reinterpret_cast<const private_handle_t *>(layer->input_buffer->buffer_id);
+    auto acquire_fence_fd = layer->input_buffer->acquire_fence_fd;
+
+    if (acquire_fence_fd >= 0) {
+      int error = sync_wait(acquire_fence_fd, 1000);
+      if (error < 0) {
+        DLOGW("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
+        return;
+      }
+    }
+
+    if (pvt_handle && pvt_handle->base) {
+      char dump_file_name[PATH_MAX];
+      size_t result = 0;
+
+      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_);
+
+      FILE *fp = fopen(dump_file_name, "w+");
+      if (fp) {
+        result = fwrite(reinterpret_cast<void *>(pvt_handle->base), pvt_handle->size, 1, fp);
+        fclose(fp);
+      }
+
+      DLOGI("Frame Dump %s: is %s", dump_file_name, result ? "Successful" : "Failed");
+    }
+  }
+}
+
+void HWCDisplay::DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence) {
+  char dir_path[PATH_MAX];
+
+  snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
+
+  if (mkdir(dir_path, 777) != 0 && errno != EEXIST) {
+    DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
+    return;
+  }
+
+  // if directory exists already, need to explicitly change the permission.
+  if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
+    DLOGW("Failed to change permissions on %s directory", dir_path);
+    return;
+  }
+
+  if (base) {
+    char dump_file_name[PATH_MAX];
+    size_t result = 0;
+
+    if (fence >= 0) {
+      int error = sync_wait(fence, 1000);
+      if (error < 0) {
+        DLOGW("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
+        return;
+      }
+    }
+
+    snprintf(dump_file_name, sizeof(dump_file_name), "%s/output_layer_%dx%d_%s_frame%d.raw",
+             dir_path, buffer_info.buffer_config.width, buffer_info.buffer_config.height,
+             GetFormatString(buffer_info.buffer_config.format), dump_frame_index_);
+
+    FILE *fp = fopen(dump_file_name, "w+");
+    if (fp) {
+      result = fwrite(base, buffer_info.alloc_buffer_info.size, 1, fp);
+      fclose(fp);
+    }
+
+    DLOGI("Frame Dump of %s is %s", dump_file_name, result ? "Successful" : "Failed");
+  }
+}
+
+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:
+      return "primary";
+    case kHDMI:
+      return "hdmi";
+    case kVirtual:
+      return "virtual";
+    default:
+      return "invalid";
+  }
+}
+
+int HWCDisplay::SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels) {
+  if (x_pixels <= 0 || y_pixels <= 0) {
+    DLOGV("Unsupported config: x_pixels=%d, y_pixels=%d", x_pixels, y_pixels);
+    return -EINVAL;
+  }
+
+  if (framebuffer_config_->x_pixels == x_pixels && framebuffer_config_->y_pixels == y_pixels) {
+    return 0;
+  }
+
+  DisplayConfigVariableInfo active_config;
+  uint32_t active_config_index = 0;
+  display_intf_->GetActiveConfig(&active_config_index);
+  DisplayError error = display_intf_->GetConfig(active_config_index, &active_config);
+  if (error != kErrorNone) {
+    DLOGV("GetConfig variable info failed. Error = %d", error);
+    return -EINVAL;
+  }
+
+  if (active_config.x_pixels <= 0 || active_config.y_pixels <= 0) {
+    DLOGV("Invalid panel resolution (%dx%d)", active_config.x_pixels, active_config.y_pixels);
+    return -EINVAL;
+  }
+
+  // Create rects to represent the new source and destination crops
+  LayerRect crop = LayerRect(0, 0, FLOAT(x_pixels), FLOAT(y_pixels));
+  LayerRect dst = LayerRect(0, 0, FLOAT(active_config.x_pixels), FLOAT(active_config.y_pixels));
+  // Set rotate90 to false since this is taken care of during regular composition.
+  bool rotate90 = false;
+  error = display_intf_->IsScalingValid(crop, dst, rotate90);
+  if (error != kErrorNone) {
+    DLOGV("Unsupported resolution: (%dx%d)", x_pixels, y_pixels);
+    return -EINVAL;
+  }
+
+  framebuffer_config_->x_pixels = x_pixels;
+  framebuffer_config_->y_pixels = y_pixels;
+  framebuffer_config_->vsync_period_ns = active_config.vsync_period_ns;
+  framebuffer_config_->x_dpi = active_config.x_dpi;
+  framebuffer_config_->y_dpi = active_config.y_dpi;
+
+  auto client_target_layer = client_target_->GetSDMLayer();
+  client_target_layer->src_rect = crop;
+  client_target_layer->dst_rect = dst;
+
+  int aligned_width;
+  int aligned_height;
+  int usage = GRALLOC_USAGE_HW_FB;
+  int format = HAL_PIXEL_FORMAT_RGBA_8888;
+  int ubwc_enabled = 0;
+  int flags = 0;
+  HWCDebugHandler::Get()->GetProperty("debug.gralloc.enable_fb_ubwc", &ubwc_enabled);
+  if (ubwc_enabled == 1) {
+    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);
+
+  // TODO(user): How does the dirty region get set on the client target? File bug on Google
+  client_target_layer->composition = kCompositionGPUTarget;
+  client_target_layer->input_buffer->format = GetSDMFormat(format, flags);
+  client_target_layer->input_buffer->width = UINT32(aligned_width);
+  client_target_layer->input_buffer->height = UINT32(aligned_height);
+  client_target_layer->plane_alpha = 255;
+
+  DLOGI("New framebuffer resolution (%dx%d)", framebuffer_config_->x_pixels,
+        framebuffer_config_->y_pixels);
+
+  return 0;
+}
+
+void HWCDisplay::GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
+  *x_pixels = framebuffer_config_->x_pixels;
+  *y_pixels = framebuffer_config_->y_pixels;
+}
+
+void HWCDisplay::ScaleDisplayFrame(hwc_rect_t *display_frame) {
+  if (!IsFrameBufferScaled()) {
+    return;
+  }
+
+  uint32_t active_config_index = 0;
+  display_intf_->GetActiveConfig(&active_config_index);
+  DisplayConfigVariableInfo active_config;
+  DisplayError error = display_intf_->GetConfig(active_config_index, &active_config);
+  if (error != kErrorNone) {
+    DLOGE("GetConfig variable info failed. Error = %d", error);
+    return;
+  }
+
+  float custom_x_pixels = FLOAT(framebuffer_config_->x_pixels);
+  float custom_y_pixels = FLOAT(framebuffer_config_->y_pixels);
+  float active_x_pixels = FLOAT(active_config.x_pixels);
+  float active_y_pixels = FLOAT(active_config.y_pixels);
+  float x_pixels_ratio = active_x_pixels / custom_x_pixels;
+  float y_pixels_ratio = active_y_pixels / custom_y_pixels;
+  float layer_width = FLOAT(display_frame->right - display_frame->left);
+  float layer_height = FLOAT(display_frame->bottom - display_frame->top);
+
+  display_frame->left = INT(x_pixels_ratio * FLOAT(display_frame->left));
+  display_frame->top = INT(y_pixels_ratio * FLOAT(display_frame->top));
+  display_frame->right = INT(FLOAT(display_frame->left) + layer_width * x_pixels_ratio);
+  display_frame->bottom = INT(FLOAT(display_frame->top) + layer_height * y_pixels_ratio);
+}
+
+bool HWCDisplay::IsFrameBufferScaled() {
+  if (framebuffer_config_->x_pixels == 0 || framebuffer_config_->y_pixels == 0) {
+    return false;
+  }
+  uint32_t panel_x_pixels = 0;
+  uint32_t panel_y_pixels = 0;
+  GetPanelResolution(&panel_x_pixels, &panel_y_pixels);
+  return (framebuffer_config_->x_pixels != panel_x_pixels) ||
+         (framebuffer_config_->y_pixels != panel_y_pixels);
+}
+
+void HWCDisplay::GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
+  DisplayConfigVariableInfo active_config;
+  uint32_t active_config_index = 0;
+  display_intf_->GetActiveConfig(&active_config_index);
+  DisplayError error = display_intf_->GetConfig(active_config_index, &active_config);
+  if (error != kErrorNone) {
+    DLOGE("GetConfig variable info failed. Error = %d", error);
+    return;
+  }
+  *x_pixels = active_config.x_pixels;
+  *y_pixels = active_config.y_pixels;
+}
+
+int HWCDisplay::SetDisplayStatus(uint32_t display_status) {
+  int status = 0;
+
+  switch (display_status) {
+    case kDisplayStatusResume:
+      display_paused_ = false;
+    case kDisplayStatusOnline:
+      status = INT32(SetPowerMode(HWC2::PowerMode::On));
+      break;
+    case kDisplayStatusPause:
+      display_paused_ = true;
+    case kDisplayStatusOffline:
+      status = INT32(SetPowerMode(HWC2::PowerMode::Off));
+      break;
+    default:
+      DLOGW("Invalid display status %d", display_status);
+      return -EINVAL;
+  }
+
+  if (display_status == kDisplayStatusResume || display_status == kDisplayStatusPause) {
+    callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
+  }
+
+  return status;
+}
+
+HWC2::Error HWCDisplay::SetCursorPosition(hwc2_layer_t layer, int x, int y) {
+  if (shutdown_pending_) {
+    return HWC2::Error::None;
+  }
+
+  // TODO(user): Validate layer
+  // TODO(user): Check if we're in a validate/present cycle
+
+  auto error = display_intf_->SetCursorPosition(x, y);
+  if (error != kErrorNone) {
+    if (error == kErrorShutDown) {
+      shutdown_pending_ = true;
+      return HWC2::Error::None;
+    }
+
+    DLOGE("Failed for x = %d y = %d, Error = %d", x, y, error);
+    return HWC2::Error::BadDisplay;
+  }
+
+  return HWC2::Error::None;
+}
+
+int HWCDisplay::OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) {
+  DisplayError error = display_intf_->OnMinHdcpEncryptionLevelChange(min_enc_level);
+  if (error != kErrorNone) {
+    DLOGE("Failed. Error = %d", error);
+    return -1;
+  }
+
+  return 0;
+}
+
+void HWCDisplay::MarkLayersForGPUBypass() {
+  for (auto hwc_layer : layer_set_) {
+    auto layer = hwc_layer->GetSDMLayer();
+    layer->composition = kCompositionSDE;
+  }
+}
+
+void HWCDisplay::ApplyScanAdjustment(hwc_rect_t *display_frame) {
+}
+
+int HWCDisplay::SetPanelBrightness(int level) {
+  int ret = 0;
+  if (display_intf_)
+    ret = display_intf_->SetPanelBrightness(level);
+  else
+    ret = -EINVAL;
+
+  return ret;
+}
+
+int HWCDisplay::GetPanelBrightness(int *level) {
+  return display_intf_->GetPanelBrightness(level);
+}
+
+int HWCDisplay::ToggleScreenUpdates(bool enable) {
+  display_paused_ = enable ? false : true;
+  callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
+  return 0;
+}
+
+int HWCDisplay::ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload,
+                                     PPDisplayAPIPayload *out_payload,
+                                     PPPendingParams *pending_action) {
+  int ret = 0;
+
+  if (display_intf_)
+    ret = display_intf_->ColorSVCRequestRoute(in_payload, out_payload, pending_action);
+  else
+    ret = -EINVAL;
+
+  return ret;
+}
+
+int HWCDisplay::GetVisibleDisplayRect(hwc_rect_t *visible_rect) {
+  if (!IsValid(display_rect_)) {
+    return -EINVAL;
+  }
+
+  visible_rect->left = INT(display_rect_.left);
+  visible_rect->top = INT(display_rect_.top);
+  visible_rect->right = INT(display_rect_.right);
+  visible_rect->bottom = INT(display_rect_.bottom);
+  DLOGI("Dpy = %d Visible Display Rect(%d %d %d %d)", visible_rect->left, visible_rect->top,
+        visible_rect->right, visible_rect->bottom);
+
+  return 0;
+}
+
+void HWCDisplay::SetSecureDisplay(bool secure_display_active) {
+  secure_display_active_ = secure_display_active;
+  return;
+}
+
+int HWCDisplay::SetActiveDisplayConfig(int config) {
+  return display_intf_->SetActiveConfig(UINT32(config)) == kErrorNone ? 0 : -1;
+}
+
+int HWCDisplay::GetActiveDisplayConfig(uint32_t *config) {
+  return display_intf_->GetActiveConfig(config) == kErrorNone ? 0 : -1;
+}
+
+int HWCDisplay::GetDisplayConfigCount(uint32_t *count) {
+  return display_intf_->GetNumVariableInfoConfigs(count) == kErrorNone ? 0 : -1;
+}
+
+int HWCDisplay::GetDisplayAttributesForConfig(int config, DisplayConfigVariableInfo *attributes) {
+  return display_intf_->GetConfig(UINT32(config), attributes) == kErrorNone ? 0 : -1;
+}
+
+bool HWCDisplay::SingleLayerUpdating(void) {
+  uint32_t updating_count = 0;
+
+  for (uint i = 0; i < layer_set_.size(); i++) {
+    auto layer = layer_stack_.layers.at(i);
+    if (layer->flags.updating) {
+      updating_count++;
+    }
+  }
+
+  return (updating_count == 1);
+}
+
+uint32_t HWCDisplay::SanitizeRefreshRate(uint32_t req_refresh_rate) {
+  uint32_t refresh_rate = req_refresh_rate;
+
+  if (refresh_rate < min_refresh_rate_) {
+    // Pick the next multiple of request which is within the range
+    refresh_rate =
+        (((min_refresh_rate_ / refresh_rate) + ((min_refresh_rate_ % refresh_rate) ? 1 : 0)) *
+         refresh_rate);
+  }
+
+  if (refresh_rate > max_refresh_rate_) {
+    refresh_rate = max_refresh_rate_;
+  }
+
+  return refresh_rate;
+}
+
+DisplayClass HWCDisplay::GetDisplayClass() {
+  return display_class_;
+}
+
+void HWCDisplay::CloseAcquireFds() {
+  for (auto hwc_layer : layer_set_) {
+    auto layer = hwc_layer->GetSDMLayer();
+    if (layer->input_buffer->acquire_fence_fd >= 0) {
+      close(layer->input_buffer->acquire_fence_fd);
+    }
+  }
+  int32_t &client_target_acquire_fence =
+      client_target_->GetSDMLayer()->input_buffer->acquire_fence_fd;
+  if (client_target_acquire_fence >= 0) {
+    close(client_target_acquire_fence);
+    client_target_acquire_fence = -1;
+  }
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_display.h b/msm8996/sdm/libs/hwc2/hwc_display.h
new file mode 100644
index 0000000..6430538
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display.h
@@ -0,0 +1,215 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __HWC_DISPLAY_H__
+#define __HWC_DISPLAY_H__
+
+#include <hardware/hwcomposer.h>
+#include <core/core_interface.h>
+#include <qdMetaData.h>
+#include <QService.h>
+#include <private/color_params.h>
+#include <map>
+#include <set>
+#include <queue>
+#include <utility>
+#include "hwc_callbacks.h"
+#include "hwc_layers.h"
+
+namespace sdm {
+
+class BlitEngine;
+
+// Subclasses set this to their type. This has to be different from DisplayType.
+// This is to avoid RTTI and dynamic_cast
+enum DisplayClass {
+  DISPLAY_CLASS_PRIMARY,
+  DISPLAY_CLASS_EXTERNAL,
+  DISPLAY_CLASS_VIRTUAL,
+  DISPLAY_CLASS_NULL
+};
+
+class HWCDisplay : public DisplayEventHandler {
+ public:
+  virtual ~HWCDisplay() {}
+  virtual int Init();
+  virtual int Deinit();
+
+  // Framebuffer configurations
+  virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
+  virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
+  virtual DisplayError SetMaxMixerStages(uint32_t max_mixer_stages);
+  virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending);
+  virtual HWC2::PowerMode GetLastPowerMode();
+  virtual int SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels);
+  virtual void GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels);
+  virtual void GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels);
+  virtual int SetDisplayStatus(uint32_t display_status);
+  virtual int OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
+  virtual int Perform(uint32_t operation, ...);
+  virtual void SetSecureDisplay(bool secure_display_active);
+
+  // Captures frame output in the buffer specified by output_buffer_info. The API is
+  // non-blocking and the client is expected to check operation status later on.
+  // Returns -1 if the input is invalid.
+  virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed) {
+    return -1;
+  }
+  // Returns the status of frame capture operation requested with FrameCaptureAsync().
+  // -EAGAIN : No status obtain yet, call API again after another frame.
+  // < 0 : Operation happened but failed.
+  // 0 : Success.
+  virtual int GetFrameCaptureStatus() { return -EAGAIN; }
+
+  // Display Configurations
+  virtual int SetActiveDisplayConfig(int config);
+  virtual int GetActiveDisplayConfig(uint32_t *config);
+  virtual int GetDisplayConfigCount(uint32_t *count);
+  virtual int GetDisplayAttributesForConfig(int config, DisplayConfigVariableInfo *attributes);
+
+  int SetPanelBrightness(int level);
+  int GetPanelBrightness(int *level);
+  int ToggleScreenUpdates(bool enable);
+  int ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload, PPDisplayAPIPayload *out_payload,
+                           PPPendingParams *pending_action);
+  DisplayClass GetDisplayClass();
+  int GetVisibleDisplayRect(hwc_rect_t *rect);
+  void BuildLayerStack(void);
+  HWCLayer *GetHWCLayer(hwc2_layer_t layer);
+
+  // HWC2 APIs
+  virtual HWC2::Error AcceptDisplayChanges(void);
+  virtual HWC2::Error GetActiveConfig(hwc2_config_t *out_config);
+  virtual HWC2::Error SetActiveConfig(hwc2_config_t config);
+  virtual HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
+                                      int32_t dataspace);
+  virtual HWC2::Error GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs);
+  virtual HWC2::Error GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute,
+                                          int32_t *out_value);
+  virtual HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format,
+                                             int32_t dataspace);
+  virtual HWC2::Error GetChangedCompositionTypes(uint32_t *out_num_elements,
+                                                 hwc2_layer_t *out_layers, int32_t *out_types);
+  virtual HWC2::Error GetDisplayRequests(int32_t *out_display_requests, uint32_t *out_num_elements,
+                                         hwc2_layer_t *out_layers, int32_t *out_layer_requests);
+  virtual HWC2::Error GetDisplayName(uint32_t *out_size, char *out_name);
+  virtual HWC2::Error GetDisplayType(int32_t *out_type);
+  virtual HWC2::Error SetCursorPosition(hwc2_layer_t layer, int x, int y);
+  virtual HWC2::Error SetVsyncEnabled(HWC2::Vsync enabled);
+  virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode);
+  virtual HWC2::Error CreateLayer(hwc2_layer_t *out_layer_id);
+  virtual HWC2::Error DestroyLayer(hwc2_layer_t layer_id);
+  virtual HWC2::Error SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z);
+  virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests) = 0;
+  virtual HWC2::Error GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers,
+                                       int32_t *out_fences);
+  virtual HWC2::Error Present(int32_t *out_retire_fence) = 0;
+
+ protected:
+  enum DisplayStatus {
+    kDisplayStatusOffline = 0,
+    kDisplayStatusOnline,
+    kDisplayStatusPause,
+    kDisplayStatusResume,
+  };
+
+  // Maximum number of layers supported by display manager.
+  static const uint32_t kMaxLayerCount = 32;
+
+  HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type, hwc2_display_t id,
+             bool needs_blit, qService::QService *qservice, DisplayClass display_class);
+
+  // DisplayEventHandler methods
+  virtual DisplayError VSync(const DisplayEventVSync &vsync);
+  virtual DisplayError Refresh();
+  virtual DisplayError CECMessage(char *message);
+  virtual void DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence);
+  virtual HWC2::Error PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests);
+  virtual HWC2::Error CommitLayerStack(void);
+  virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence);
+  LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
+  const char *GetHALPixelFormatString(int format);
+  const char *GetDisplayString();
+  void ScaleDisplayFrame(hwc_rect_t *display_frame);
+  void MarkLayersForGPUBypass(void);
+  virtual void ApplyScanAdjustment(hwc_rect_t *display_frame);
+  bool NeedsFrameBufferRefresh(void);
+  bool SingleLayerUpdating(void);
+  uint32_t SanitizeRefreshRate(uint32_t req_refresh_rate);
+  virtual void CloseAcquireFds();
+
+  enum {
+    INPUT_LAYER_DUMP,
+    OUTPUT_LAYER_DUMP,
+  };
+
+  CoreInterface *core_intf_;
+  HWCCallbacks *callbacks_;
+  DisplayType type_;
+  hwc2_display_t id_;
+  bool needs_blit_ = false;
+  DisplayInterface *display_intf_ = NULL;
+  LayerStack layer_stack_;
+  HWCLayer *client_target_;                             // Also known as framebuffer target
+  std::map<hwc2_layer_t, HWCLayer *> layer_map_;        // Look up by Id - TODO
+  std::multiset<HWCLayer *, SortLayersByZ> layer_set_;  // Maintain a set sorted by Z
+  std::map<hwc2_layer_t, HWC2::Composition> layer_changes_;
+  std::map<hwc2_layer_t, HWC2::LayerRequest> layer_requests_;
+  bool flush_on_error_ = false;
+  bool flush_ = false;
+  uint32_t dump_frame_count_ = 0;
+  uint32_t dump_frame_index_ = 0;
+  bool dump_input_layers_ = false;
+  HWC2::PowerMode last_power_mode_;
+  bool swap_interval_zero_ = false;
+  DisplayConfigVariableInfo *framebuffer_config_ = NULL;
+  bool display_paused_ = false;
+  uint32_t min_refresh_rate_ = 0;
+  uint32_t max_refresh_rate_ = 0;
+  uint32_t current_refresh_rate_ = 0;
+  bool use_metadata_refresh_rate_ = false;
+  uint32_t metadata_refresh_rate_ = 0;
+  uint32_t force_refresh_rate_ = 0;
+  bool boot_animation_completed_ = false;
+  bool shutdown_pending_ = false;
+  bool use_blit_comp_ = false;
+  bool secure_display_active_ = false;
+  bool skip_prepare_ = false;
+  bool solid_fill_enable_ = false;
+  uint32_t solid_fill_color_ = 0;
+  LayerRect display_rect_;
+  bool validated_ = false;
+
+ private:
+  bool IsFrameBufferScaled();
+  void DumpInputBuffers(void);
+  BlitEngine *blit_engine_ = NULL;
+  qService::QService *qservice_ = NULL;
+  DisplayClass display_class_;
+  int32_t stored_retire_fence_;
+  uint32_t geometry_changes_ = GeometryChanges::kNone;
+};
+
+inline int HWCDisplay::Perform(uint32_t operation, ...) {
+  return 0;
+}
+
+}  // namespace sdm
+
+#endif  // __HWC_DISPLAY_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_external.cpp b/msm8996/sdm/libs/hwc2/hwc_display_external.cpp
new file mode 100644
index 0000000..73ed0e8
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_external.cpp
@@ -0,0 +1,206 @@
+/*
+* Copyright (c) 2014 - 2016, 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 <cutils/properties.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+
+#include "hwc_display_external.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "HWCDisplayExternal"
+
+namespace sdm {
+
+int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks,
+                               qService::QService *qservice, HWCDisplay **hwc_display) {
+  return Create(core_intf, callbacks, 0, 0, qservice, false, hwc_display);
+}
+
+int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks,
+                               uint32_t primary_width, uint32_t primary_height,
+                               qService::QService *qservice, bool use_primary_res,
+                               HWCDisplay **hwc_display) {
+  uint32_t external_width = 0;
+  uint32_t external_height = 0;
+
+  HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, callbacks, qservice);
+  int status = hwc_display_external->Init();
+  if (status) {
+    delete hwc_display_external;
+    return status;
+  }
+
+  hwc_display_external->GetPanelResolution(&external_width, &external_height);
+
+  if (primary_width && primary_height) {
+    // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
+    // provided primary_width and primary_height
+    if (use_primary_res) {
+      external_width = primary_width;
+      external_height = primary_height;
+    } else {
+      int downscale_enabled = 0;
+      HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
+      if (downscale_enabled) {
+        GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
+      }
+    }
+  }
+
+  status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
+  if (status) {
+    Destroy(hwc_display_external);
+    return status;
+  }
+
+  *hwc_display = hwc_display_external;
+
+  return status;
+}
+
+void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) {
+  hwc_display->Deinit();
+  delete hwc_display;
+}
+
+HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, HWCCallbacks *callbacks,
+                                       qService::QService *qservice)
+    : HWCDisplay(core_intf, callbacks, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
+                 DISPLAY_CLASS_EXTERNAL) {
+}
+
+HWC2::Error HWCDisplayExternal::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
+  auto status = HWC2::Error::None;
+
+  if (secure_display_active_) {
+    MarkLayersForGPUBypass();
+    return status;
+  }
+
+  BuildLayerStack();
+
+  if (layer_set_.empty()) {
+    flush_ = true;
+    return status;
+  }
+
+  status = PrepareLayerStack(out_num_types, out_num_requests);
+  return status;
+}
+
+HWC2::Error HWCDisplayExternal::Present(int32_t *out_retire_fence) {
+  auto status = HWC2::Error::None;
+
+  if (!secure_display_active_) {
+    status = HWCDisplay::CommitLayerStack();
+    if (status == HWC2::Error::None) {
+      status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
+    }
+  }
+  CloseAcquireFds();
+  return status;
+}
+
+void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
+  if (display_intf_->IsUnderscanSupported()) {
+    return;
+  }
+
+  // Read user defined width and height ratio
+  int width = 0, height = 0;
+  HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width);
+  float width_ratio = FLOAT(width) / 100.0f;
+  HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height);
+  float height_ratio = FLOAT(height) / 100.0f;
+
+  if (width_ratio == 0.0f || height_ratio == 0.0f) {
+    return;
+  }
+
+  uint32_t panel_width = 0;
+  uint32_t panel_height = 0;
+  GetPanelResolution(&panel_width, &panel_height);
+
+  if (panel_width == 0 || panel_height == 0) {
+    DLOGV("Invalid panel dimensions (%d, %d)", panel_width, panel_height);
+    return;
+  }
+
+  uint32_t new_panel_width = UINT32(panel_width * FLOAT(1.0f - width_ratio));
+  uint32_t new_panel_height = UINT32(panel_height * FLOAT(1.0f - height_ratio));
+
+  int x_offset = INT((FLOAT(panel_width) * width_ratio) / 2.0f);
+  int y_offset = INT((FLOAT(panel_height) * height_ratio) / 2.0f);
+
+  display_frame->left =
+      (display_frame->left * INT32(new_panel_width) / INT32(panel_width)) + x_offset;
+  display_frame->top =
+      (display_frame->top * INT32(new_panel_height) / INT32(panel_height)) + y_offset;
+  display_frame->right =
+      ((display_frame->right * INT32(new_panel_width)) / INT32(panel_width)) + x_offset;
+  display_frame->bottom =
+      ((display_frame->bottom * INT32(new_panel_height)) / INT32(panel_height)) + y_offset;
+}
+
+void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) {
+  if (secure_display_active_ != secure_display_active) {
+    secure_display_active_ = secure_display_active;
+
+    if (secure_display_active_) {
+      DisplayError error = display_intf_->Flush();
+      if (error != kErrorNone) {
+        DLOGE("Flush failed. Error = %d", error);
+      }
+    }
+  }
+  return;
+}
+
+static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
+                                   uint32_t *src_height) {
+  *src_height = (dst_width * (*src_height)) / (*src_width);
+  *src_width = dst_width;
+}
+
+void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
+                                                uint32_t *non_primary_width,
+                                                uint32_t *non_primary_height) {
+  uint32_t primary_area = primary_width * primary_height;
+  uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
+
+  if (primary_area > non_primary_area) {
+    if (primary_height > primary_width) {
+      Swap(primary_height, primary_width);
+    }
+    AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
+  }
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_external.h b/msm8996/sdm/libs/hwc2/hwc_display_external.h
new file mode 100644
index 0000000..ce7547c
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_external.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014-2016, 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 __HWC_DISPLAY_EXTERNAL_H__
+#define __HWC_DISPLAY_EXTERNAL_H__
+
+#include "hwc_display.h"
+
+namespace sdm {
+
+class HWCDisplayExternal : public HWCDisplay {
+ public:
+  static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t primary_width,
+                    uint32_t primary_height, qService::QService *qservice, bool use_primary_res,
+                    HWCDisplay **hwc_display);
+  static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, qService::QService *qservice,
+                    HWCDisplay **hwc_display);
+  static void Destroy(HWCDisplay *hwc_display);
+  virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
+  virtual HWC2::Error Present(int32_t *out_retire_fence);
+  virtual void SetSecureDisplay(bool secure_display_active);
+
+ private:
+  HWCDisplayExternal(CoreInterface *core_intf, HWCCallbacks *callbacks,
+                     qService::QService *qservice);
+  void ApplyScanAdjustment(hwc_rect_t *display_frame);
+  static void GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
+                                     uint32_t *virtual_width, uint32_t *virtual_height);
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_DISPLAY_EXTERNAL_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_primary.cpp b/msm8996/sdm/libs/hwc2/hwc_display_primary.cpp
new file mode 100644
index 0000000..40ac723
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_primary.cpp
@@ -0,0 +1,471 @@
+/*
+* Copyright (c) 2014 - 2016, 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 <cutils/properties.h>
+#include <sync/sync.h>
+#include <utils/constants.h>
+#include <utils/debug.h>
+#include <stdarg.h>
+#include <sys/mman.h>
+
+#include "hwc_display_primary.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "HWCDisplayPrimary"
+
+namespace sdm {
+
+int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                              HWCCallbacks *callbacks, qService::QService *qservice,
+                              HWCDisplay **hwc_display) {
+  int status = 0;
+  uint32_t primary_width = 0;
+  uint32_t primary_height = 0;
+
+  HWCDisplay *hwc_display_primary =
+      new HWCDisplayPrimary(core_intf, buffer_allocator, callbacks, qservice);
+  status = hwc_display_primary->Init();
+  if (status) {
+    delete hwc_display_primary;
+    return status;
+  }
+
+  hwc_display_primary->GetPanelResolution(&primary_width, &primary_height);
+  int width = 0, height = 0;
+  HWCDebugHandler::Get()->GetProperty("sdm.fb_size_width", &width);
+  HWCDebugHandler::Get()->GetProperty("sdm.fb_size_height", &height);
+  if (width > 0 && height > 0) {
+    primary_width = UINT32(width);
+    primary_height = UINT32(height);
+  }
+
+  status = hwc_display_primary->SetFrameBufferResolution(primary_width, primary_height);
+  if (status) {
+    Destroy(hwc_display_primary);
+    return status;
+  }
+
+  *hwc_display = hwc_display_primary;
+
+  return status;
+}
+
+void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) {
+  hwc_display->Deinit();
+  delete hwc_display;
+}
+
+HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                                     HWCCallbacks *callbacks, qService::QService *qservice)
+    : HWCDisplay(core_intf, callbacks, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice,
+                 DISPLAY_CLASS_PRIMARY),
+      buffer_allocator_(buffer_allocator),
+      cpu_hint_(NULL) {
+}
+
+int HWCDisplayPrimary::Init() {
+  cpu_hint_ = new CPUHint();
+  if (cpu_hint_->Init(static_cast<HWCDebugHandler *>(HWCDebugHandler::Get())) != kErrorNone) {
+    delete cpu_hint_;
+    cpu_hint_ = NULL;
+  }
+
+  use_metadata_refresh_rate_ = true;
+  int disable_metadata_dynfps = 0;
+  HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
+  if (disable_metadata_dynfps) {
+    use_metadata_refresh_rate_ = false;
+  }
+
+  return HWCDisplay::Init();
+}
+
+void HWCDisplayPrimary::ProcessBootAnimCompleted() {
+  uint32_t numBootUpLayers = 0;
+  // TODO(user): Remove this hack
+
+  numBootUpLayers = static_cast<uint32_t>(Debug::GetBootAnimLayerCount());
+
+  if (numBootUpLayers == 0) {
+    numBootUpLayers = 2;
+  }
+  /* All other checks namely "init.svc.bootanim" or
+  * HWC_GEOMETRY_CHANGED fail in correctly identifying the
+  * exact bootup transition to homescreen
+  */
+  char cryptoState[PROPERTY_VALUE_MAX];
+  char voldDecryptState[PROPERTY_VALUE_MAX];
+  bool isEncrypted = false;
+  bool main_class_services_started = false;
+  if (property_get("ro.crypto.state", cryptoState, "unencrypted")) {
+    if (!strcmp(cryptoState, "encrypted")) {
+      isEncrypted = true;
+      if (property_get("vold.decrypt", voldDecryptState, "") &&
+          !strcmp(voldDecryptState, "trigger_restart_framework"))
+        main_class_services_started = true;
+    }
+  }
+  if ((!isEncrypted || (isEncrypted && main_class_services_started)) &&
+      (layer_set_.size() > numBootUpLayers)) {
+    boot_animation_completed_ = true;
+    // Applying default mode after bootanimation is finished And
+    // If Data is Encrypted, it is ready for access.
+    if (display_intf_)
+      display_intf_->ApplyDefaultDisplayMode();
+  }
+}
+
+HWC2::Error HWCDisplayPrimary::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
+  auto status = HWC2::Error::None;
+  DisplayError error = kErrorNone;
+
+  if (!boot_animation_completed_)
+    ProcessBootAnimCompleted();
+
+  if (display_paused_) {
+    MarkLayersForGPUBypass();
+    return status;
+  }
+
+  // Fill in the remaining blanks in the layers and add them to the SDM layerstack
+  BuildLayerStack();
+
+  bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
+
+  if (frame_capture_buffer_queued_ || pending_output_dump) {
+    // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
+    // here in a subsequent draw round.
+    layer_stack_.output_buffer = &output_buffer_;
+    layer_stack_.flags.post_processed_output = post_processed_output_;
+  }
+
+  bool one_updating_layer = SingleLayerUpdating();
+  ToggleCPUHint(one_updating_layer);
+
+  uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
+  if (current_refresh_rate_ != refresh_rate) {
+    error = display_intf_->SetRefreshRate(refresh_rate);
+  }
+
+  if (error == kErrorNone) {
+    // On success, set current refresh rate to new refresh rate
+    current_refresh_rate_ = refresh_rate;
+  }
+
+  if (handle_idle_timeout_) {
+    handle_idle_timeout_ = false;
+  }
+
+  // TODO(user): Validate this
+  if (layer_set_.empty()) {
+    flush_ = true;
+    return status;
+  }
+
+  status = PrepareLayerStack(out_num_types, out_num_requests);
+  return status;
+}
+
+HWC2::Error HWCDisplayPrimary::Present(int32_t *out_retire_fence) {
+  auto status = HWC2::Error::None;
+  if (display_paused_) {
+    // TODO(user): From old HWC implementation
+    // If we do not handle the frame set retireFenceFd to outbufAcquireFenceFd
+    // Revisit this when validating display_paused
+    DisplayError error = display_intf_->Flush();
+    if (error != kErrorNone) {
+      DLOGE("Flush failed. Error = %d", error);
+    }
+  } else {
+    status = HWCDisplay::CommitLayerStack();
+    if (status == HWC2::Error::None) {
+      HandleFrameOutput();
+      status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
+    }
+  }
+  CloseAcquireFds();
+  return status;
+}
+
+int HWCDisplayPrimary::Perform(uint32_t operation, ...) {
+  va_list args;
+  va_start(args, operation);
+  int val = va_arg(args, int32_t);
+  va_end(args);
+  switch (operation) {
+    case SET_METADATA_DYN_REFRESH_RATE:
+      SetMetaDataRefreshRateFlag(val);
+      break;
+    case SET_BINDER_DYN_REFRESH_RATE:
+      ForceRefreshRate(UINT32(val));
+      break;
+    case SET_DISPLAY_MODE:
+      SetDisplayMode(UINT32(val));
+      break;
+    case SET_QDCM_SOLID_FILL_INFO:
+      SetQDCMSolidFillInfo(true, UINT32(val));
+      break;
+    case UNSET_QDCM_SOLID_FILL_INFO:
+      SetQDCMSolidFillInfo(false, UINT32(val));
+      break;
+    default:
+      DLOGW("Invalid operation %d", operation);
+      return -EINVAL;
+  }
+
+  return 0;
+}
+
+DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) {
+  DisplayError error = kErrorNone;
+
+  if (display_intf_) {
+    error = display_intf_->SetDisplayMode(mode);
+  }
+
+  return error;
+}
+
+void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) {
+  int disable_metadata_dynfps = 0;
+
+  HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
+  if (disable_metadata_dynfps) {
+    return;
+  }
+  use_metadata_refresh_rate_ = enable;
+}
+
+void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, uint32_t color) {
+  solid_fill_enable_ = enable;
+  solid_fill_color_ = color;
+}
+
+void HWCDisplayPrimary::ToggleCPUHint(bool set) {
+  if (!cpu_hint_) {
+    return;
+  }
+
+  if (set) {
+    cpu_hint_->Set();
+  } else {
+    cpu_hint_->Reset();
+  }
+}
+
+void HWCDisplayPrimary::SetSecureDisplay(bool secure_display_active) {
+  if (secure_display_active_ != secure_display_active) {
+    // Skip Prepare and call Flush for null commit
+    DLOGI("SecureDisplay state changed from %d to %d Needs Flush!!", secure_display_active_,
+          secure_display_active);
+    secure_display_active_ = secure_display_active;
+    skip_prepare_ = true;
+  }
+  return;
+}
+
+void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) {
+  if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
+      force_refresh_rate_ == refresh_rate) {
+    // Cannot honor force refresh rate, as its beyond the range or new request is same
+    return;
+  }
+
+  force_refresh_rate_ = refresh_rate;
+
+  callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
+
+  return;
+}
+
+uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) {
+  if (force_refresh_rate_) {
+    return force_refresh_rate_;
+  } else if (handle_idle_timeout_) {
+    return min_refresh_rate_;
+  } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
+    return metadata_refresh_rate_;
+  }
+
+  return max_refresh_rate_;
+}
+
+DisplayError HWCDisplayPrimary::Refresh() {
+  DisplayError error = kErrorNone;
+
+  callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
+  handle_idle_timeout_ = true;
+
+  return error;
+}
+
+void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
+  display_intf_->SetIdleTimeoutMs(timeout_ms);
+}
+
+static void SetLayerBuffer(const BufferInfo &output_buffer_info, LayerBuffer *output_buffer) {
+  output_buffer->width = output_buffer_info.buffer_config.width;
+  output_buffer->height = output_buffer_info.buffer_config.height;
+  output_buffer->format = output_buffer_info.buffer_config.format;
+  output_buffer->planes[0].fd = output_buffer_info.alloc_buffer_info.fd;
+  output_buffer->planes[0].stride = output_buffer_info.alloc_buffer_info.stride;
+}
+
+void HWCDisplayPrimary::HandleFrameOutput() {
+  if (frame_capture_buffer_queued_) {
+    HandleFrameCapture();
+  } else if (dump_output_to_file_) {
+    HandleFrameDump();
+  }
+}
+
+void HWCDisplayPrimary::HandleFrameCapture() {
+  if (output_buffer_.release_fence_fd >= 0) {
+    frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
+    ::close(output_buffer_.release_fence_fd);
+    output_buffer_.release_fence_fd = -1;
+  }
+
+  frame_capture_buffer_queued_ = false;
+  post_processed_output_ = false;
+  output_buffer_ = {};
+
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(true /* enable */, &pending);
+}
+
+void HWCDisplayPrimary::HandleFrameDump() {
+  if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) {
+    int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
+    ::close(output_buffer_.release_fence_fd);
+    output_buffer_.release_fence_fd = -1;
+    if (ret < 0) {
+      DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
+    } else {
+      DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
+    }
+  }
+
+  if (0 == dump_frame_count_) {
+    dump_output_to_file_ = false;
+    // Unmap and Free buffer
+    if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
+      DLOGE("unmap failed with err %d", errno);
+    }
+    if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
+      DLOGE("FreeBuffer failed");
+    }
+
+    post_processed_output_ = false;
+    output_buffer_ = {};
+    output_buffer_info_ = {};
+    output_buffer_base_ = nullptr;
+
+    uint32_t pending = 0;  // Just a temporary to satisfy the API
+    ControlPartialUpdate(true /* enable */, &pending);
+  }
+}
+
+void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
+  HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
+  dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
+  DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
+
+  if (!count || !dump_output_to_file_) {
+    return;
+  }
+
+  // Allocate and map output buffer
+  output_buffer_info_ = {};
+  // Since we dump DSPP output use Panel resolution.
+  GetPanelResolution(&output_buffer_info_.buffer_config.width,
+                     &output_buffer_info_.buffer_config.height);
+  output_buffer_info_.buffer_config.format = kFormatRGB888;
+  output_buffer_info_.buffer_config.buffer_count = 1;
+  if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
+    DLOGE("Buffer allocation failed");
+    output_buffer_info_ = {};
+    return;
+  }
+
+  void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
+                      MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
+
+  if (buffer == MAP_FAILED) {
+    DLOGE("mmap failed with err %d", errno);
+    buffer_allocator_->FreeBuffer(&output_buffer_info_);
+    output_buffer_info_ = {};
+    return;
+  }
+
+  output_buffer_base_ = buffer;
+  post_processed_output_ = true;
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(false /* enable */, &pending);
+}
+
+int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo &output_buffer_info,
+                                         bool post_processed_output) {
+  // Note: This function is called in context of a binder thread and a lock is already held
+  if (output_buffer_info.alloc_buffer_info.fd < 0) {
+    DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
+    return -1;
+  }
+
+  auto panel_width = 0u;
+  auto panel_height = 0u;
+  auto fb_width = 0u;
+  auto fb_height = 0u;
+
+  GetPanelResolution(&panel_width, &panel_height);
+  GetFrameBufferResolution(&fb_width, &fb_height);
+
+  if (post_processed_output && (output_buffer_info_.buffer_config.width < panel_width ||
+                                output_buffer_info_.buffer_config.height < panel_height)) {
+    DLOGE("Buffer dimensions should not be less than panel resolution");
+    return -1;
+  } else if (!post_processed_output && (output_buffer_info_.buffer_config.width < fb_width ||
+                                        output_buffer_info_.buffer_config.height < fb_height)) {
+    DLOGE("Buffer dimensions should not be less than FB resolution");
+    return -1;
+  }
+
+  SetLayerBuffer(output_buffer_info, &output_buffer_);
+  post_processed_output_ = post_processed_output;
+  frame_capture_buffer_queued_ = true;
+  // Status is only cleared on a new call to dump and remains valid otherwise
+  frame_capture_status_ = -EAGAIN;
+
+  uint32_t pending = 0;  // Just a temporary to satisfy the API
+  ControlPartialUpdate(false /* enable */, &pending);
+
+  return 0;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_primary.h b/msm8996/sdm/libs/hwc2/hwc_display_primary.h
new file mode 100644
index 0000000..2779f1b
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_primary.h
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2014-2016, 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 __HWC_DISPLAY_PRIMARY_H__
+#define __HWC_DISPLAY_PRIMARY_H__
+
+#include "cpuhint.h"
+#include "hwc_display.h"
+
+namespace sdm {
+
+class HWCDisplayPrimary : public HWCDisplay {
+ public:
+  enum {
+    SET_METADATA_DYN_REFRESH_RATE,
+    SET_BINDER_DYN_REFRESH_RATE,
+    SET_DISPLAY_MODE,
+    SET_QDCM_SOLID_FILL_INFO,
+    UNSET_QDCM_SOLID_FILL_INFO,
+  };
+
+  static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                    HWCCallbacks *callbacks, qService::QService *qservice,
+                    HWCDisplay **hwc_display);
+  static void Destroy(HWCDisplay *hwc_display);
+  virtual int Init();
+  virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
+  virtual HWC2::Error Present(int32_t *out_retire_fence);
+  virtual int Perform(uint32_t operation, ...);
+  virtual void SetSecureDisplay(bool secure_display_active);
+  virtual DisplayError Refresh();
+  virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
+  virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
+  virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed);
+  virtual int GetFrameCaptureStatus() { return frame_capture_status_; }
+
+ private:
+  HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
+                    HWCCallbacks *callbacks, qService::QService *qservice);
+  void SetMetaDataRefreshRateFlag(bool enable);
+  virtual DisplayError SetDisplayMode(uint32_t mode);
+  void ProcessBootAnimCompleted(void);
+  void SetQDCMSolidFillInfo(bool enable, uint32_t color);
+  void ToggleCPUHint(bool set);
+  void ForceRefreshRate(uint32_t refresh_rate);
+  uint32_t GetOptimalRefreshRate(bool one_updating_layer);
+  void HandleFrameOutput();
+  void HandleFrameCapture();
+  void HandleFrameDump();
+
+  BufferAllocator *buffer_allocator_ = nullptr;
+  CPUHint *cpu_hint_ = nullptr;
+  bool handle_idle_timeout_ = false;
+
+  // Primary output buffer configuration
+  LayerBuffer output_buffer_ = {};
+  bool post_processed_output_ = false;
+
+  // Members for 1 frame capture in a client provided buffer
+  bool frame_capture_buffer_queued_ = false;
+  int frame_capture_status_ = -EAGAIN;
+
+  // Members for N frame output dump to file
+  bool dump_output_to_file_ = false;
+  BufferInfo output_buffer_info_ = {};
+  void *output_buffer_base_ = nullptr;
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_DISPLAY_PRIMARY_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_virtual.cpp b/msm8996/sdm/libs/hwc2/hwc_display_virtual.cpp
new file mode 100644
index 0000000..6c7edfb
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_virtual.cpp
@@ -0,0 +1,194 @@
+/*
+* Copyright (c) 2014 - 2016, 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/constants.h>
+#include <utils/debug.h>
+#include <sync/sync.h>
+#include <stdarg.h>
+#include <gr.h>
+
+#include "hwc_display_virtual.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "HWCDisplayVirtual"
+
+namespace sdm {
+
+int HWCDisplayVirtual::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t width,
+                              uint32_t height, HWCDisplay **hwc_display) {
+  int status = 0;
+  HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, callbacks);
+  uint32_t virtual_width = 0, virtual_height = 0;
+
+  status = hwc_display_virtual->Init();
+  if (status) {
+    delete hwc_display_virtual;
+    return status;
+  }
+
+  status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On));
+  if (status) {
+    Destroy(hwc_display_virtual);
+    return status;
+  }
+
+  hwc_display_virtual->GetPanelResolution(&virtual_width, &virtual_height);
+
+  status = hwc_display_virtual->SetFrameBufferResolution(width, height);
+
+  if (status) {
+    Destroy(hwc_display_virtual);
+    return status;
+  }
+
+  *hwc_display = static_cast<HWCDisplay *>(hwc_display_virtual);
+
+  return 0;
+}
+
+void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) {
+  hwc_display->Deinit();
+  delete hwc_display;
+}
+
+HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCCallbacks *callbacks)
+    : HWCDisplay(core_intf, callbacks, kVirtual, HWC_DISPLAY_VIRTUAL, false, NULL,
+                 DISPLAY_CLASS_VIRTUAL) {
+}
+
+int HWCDisplayVirtual::Init() {
+  return HWCDisplay::Init();
+}
+
+int HWCDisplayVirtual::Deinit() {
+  int status = 0;
+
+  status = HWCDisplay::Deinit();
+
+  return status;
+}
+
+HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
+  auto status = HWC2::Error::None;
+
+  if (display_paused_) {
+    MarkLayersForGPUBypass();
+    return status;
+  }
+
+  BuildLayerStack();
+  status = PrepareLayerStack(out_num_types, out_num_requests);
+  return status;
+}
+
+HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) {
+  auto status = HWC2::Error::None;
+  if (display_paused_) {
+    DisplayError error = display_intf_->Flush();
+    if (error != kErrorNone) {
+      DLOGE("Flush failed. Error = %d", error);
+    }
+  } else {
+    status = HWCDisplay::CommitLayerStack();
+    if (status == HWC2::Error::None) {
+      if (dump_frame_count_ && !flush_ && dump_output_layer_) {
+        if (output_handle_ && output_handle_->base) {
+          BufferInfo buffer_info;
+          const private_handle_t *output_handle =
+              reinterpret_cast<const private_handle_t *>(output_buffer_->buffer_id);
+          buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
+          buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
+          buffer_info.buffer_config.format =
+              GetSDMFormat(output_handle->format, output_handle->flags);
+          buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
+          DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
+                           layer_stack_.retire_fence_fd);
+        }
+      }
+
+      status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
+    }
+  }
+  CloseAcquireFds();
+  return status;
+}
+
+HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) {
+  const private_handle_t *output_handle = static_cast<const private_handle_t *>(buf);
+
+  // Fill output buffer parameters (width, height, format, plane information, fence)
+  output_buffer_->acquire_fence_fd = dup(release_fence);
+
+  if (output_handle) {
+    output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
+    int output_handle_format = output_handle->format;
+
+    if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) {
+      output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888;
+    }
+
+    output_buffer_->format = GetSDMFormat(output_handle_format, output_handle->flags);
+
+    if (output_buffer_->format == kFormatInvalid) {
+      return HWC2::Error::BadParameter;
+    }
+
+    int output_buffer_width, output_buffer_height;
+    AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(output_handle, output_buffer_width,
+                                                          output_buffer_height);
+
+    output_buffer_->width = UINT32(output_buffer_width);
+    output_buffer_->height = UINT32(output_buffer_height);
+    // TODO(mkavm): Handle DRC and metadata changes
+    output_buffer_->flags.secure = 0;
+    output_buffer_->flags.video = 0;
+
+    // TZ Protected Buffer - L1
+    if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
+      output_buffer_->flags.secure = 1;
+    }
+
+    // ToDo: Need to extend for non-RGB formats
+    output_buffer_->planes[0].fd = output_handle->fd;
+    output_buffer_->planes[0].offset = output_handle->offset;
+    output_buffer_->planes[0].stride = UINT32(output_handle->width);
+  }
+
+  layer_stack_.output_buffer = output_buffer_;
+  return HWC2::Error::None;
+}
+
+void HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
+  HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
+  dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);
+
+  DLOGI("output_layer_dump_enable %d", dump_output_layer_);
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_display_virtual.h b/msm8996/sdm/libs/hwc2/hwc_display_virtual.h
new file mode 100644
index 0000000..271d15d
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_display_virtual.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2014-2016, 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 __HWC_DISPLAY_VIRTUAL_H__
+#define __HWC_DISPLAY_VIRTUAL_H__
+
+#include <qdMetaData.h>
+#include <gralloc_priv.h>
+#include "hwc_display.h"
+
+namespace sdm {
+
+class HWCDisplayVirtual : public HWCDisplay {
+ public:
+  static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t primary_width,
+                    uint32_t primary_height, HWCDisplay **hwc_display);
+  static void Destroy(HWCDisplay *hwc_display);
+  virtual int Init();
+  virtual int Deinit();
+  virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
+  virtual HWC2::Error Present(int32_t *out_retire_fence);
+  virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
+  HWC2::Error SetOutputBuffer(buffer_handle_t buf, int32_t release_fence);
+
+ private:
+  HWCDisplayVirtual(CoreInterface *core_intf, HWCCallbacks *callbacks);
+
+  bool dump_output_layer_ = false;
+  LayerBuffer *output_buffer_ = NULL;
+  const private_handle_t *output_handle_ = nullptr;
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_DISPLAY_VIRTUAL_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_layers.cpp b/msm8996/sdm/libs/hwc2/hwc_layers.cpp
new file mode 100644
index 0000000..9be2317
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_layers.cpp
@@ -0,0 +1,500 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "hwc_layers.h"
+#include <gr.h>
+#include <utils/debug.h>
+#include <cmath>
+
+#define __CLASS__ "HWCLayer"
+
+namespace sdm {
+
+bool SortLayersByZ::operator()(const HWCLayer *lhs, const HWCLayer *rhs) {
+  return lhs->GetZ() < rhs->GetZ();
+}
+
+std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
+
+// Layer operations
+HWCLayer::HWCLayer(hwc2_display_t display_id) : id_(next_id_++), display_id_(display_id) {
+  layer_ = new Layer();
+  layer_->input_buffer = new LayerBuffer();
+  // Fences are deferred, so the first time this layer is presented, return -1
+  // TODO(user): Verify that fences are properly obtained on suspend/resume
+  release_fences_.push(-1);
+}
+
+HWCLayer::~HWCLayer() {
+  // Close any fences left for this layer
+  while (!release_fences_.empty()) {
+    close(release_fences_.front());
+    release_fences_.pop();
+  }
+
+  if (layer_) {
+    if (layer_->input_buffer) {
+      delete (layer_->input_buffer);
+    }
+    delete layer_;
+  }
+}
+
+HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) {
+  if (!buffer) {
+    DLOGE("Invalid buffer handle: %p on layer: %d", buffer, id_);
+    return HWC2::Error::BadParameter;
+  }
+
+  const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
+  LayerBuffer *layer_buffer = layer_->input_buffer;
+  layer_buffer->width = UINT32(handle->width);
+  layer_buffer->height = UINT32(handle->height);
+  layer_buffer->format = GetSDMFormat(handle->format, handle->flags);
+  if (SetMetaData(handle, layer_) != kErrorNone) {
+    return HWC2::Error::BadLayer;
+  }
+
+  if (handle->bufferType == BUFFER_TYPE_VIDEO) {
+    layer_buffer->flags.video = true;
+  }
+  // TZ Protected Buffer - L1
+  if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
+    layer_buffer->flags.secure = true;
+  }
+  if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
+    layer_buffer->flags.secure_display = true;
+  }
+
+  layer_buffer->planes[0].fd = handle->fd;
+  layer_buffer->planes[0].offset = handle->offset;
+  layer_buffer->planes[0].stride = UINT32(handle->width);
+  layer_buffer->acquire_fence_fd = acquire_fence;
+  layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
+  auto num_dirty_rects = damage.numRects;
+  if (num_dirty_rects > 0) {
+    for (uint32_t i = 0; i <= damage.numRects; i++) {
+      LayerRect rect;
+      SetRect(damage.rects[i], &rect);
+      layer_->dirty_regions.push_back(rect);
+    }
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
+  switch (mode) {
+    case HWC2::BlendMode::Coverage:
+      layer_->blending = kBlendingCoverage;
+      break;
+    case HWC2::BlendMode::Premultiplied:
+      layer_->blending = kBlendingPremultiplied;
+      break;
+    case HWC2::BlendMode::None:
+      layer_->blending = kBlendingOpaque;
+      break;
+    default:
+      return HWC2::Error::BadParameter;
+  }
+  geometry_changes_ |= kBlendMode;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
+  layer_->solid_fill_color = GetUint32Color(color);
+  layer_->input_buffer->format = kFormatARGB8888;
+  DLOGD("Layer color set to: %u", layer_->solid_fill_color);
+  DLOGD("[%" PRIu64 "][%" PRIu64 "] Layer color set to %u  %" PRIu64, display_id_, id_,
+        layer_->solid_fill_color);
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
+  layer_->flags = {};   // Reset earlier flags
+  composition_ = type;  // Used to compute changes
+  switch (type) {
+    case HWC2::Composition::Client:
+      layer_->flags.skip = true;
+      break;
+    case HWC2::Composition::Device:
+      // We try and default to this in SDM
+      break;
+    case HWC2::Composition::SolidColor:
+      layer_->flags.solid_fill = true;
+      break;
+    case HWC2::Composition::Cursor:
+      layer_->flags.cursor = true;
+      break;
+    case HWC2::Composition::Invalid:
+      return HWC2::Error::BadParameter;
+    default:
+      return HWC2::Error::Unsupported;
+  }
+  // TODO(user): Check if this should be set here or somewhere else
+  layer_->flags.updating = true;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
+  // TODO(user): Implement later
+  geometry_changes_ |= kDataspace;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
+  SetRect(frame, &layer_->dst_rect);
+  geometry_changes_ |= kDisplayFrame;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
+  // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
+  layer_->plane_alpha = static_cast<uint8_t>(255.0f * alpha + 0.5f);
+  geometry_changes_ |= kPlaneAlpha;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
+  SetRect(crop, &layer_->src_rect);
+  geometry_changes_ |= kSourceCrop;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
+  switch (transform) {
+    case HWC2::Transform::FlipH:
+      layer_->transform.flip_horizontal = true;
+      break;
+    case HWC2::Transform::FlipV:
+      layer_->transform.flip_vertical = true;
+      break;
+    case HWC2::Transform::Rotate90:
+      layer_->transform.rotation = 90.0f;
+      break;
+    case HWC2::Transform::Rotate180:
+      layer_->transform.rotation = 180.0f;
+      break;
+    case HWC2::Transform::Rotate270:
+      layer_->transform.rotation = 270.0f;
+      break;
+    case HWC2::Transform::FlipHRotate90:
+      layer_->transform.rotation = 90.0f;
+      layer_->transform.flip_horizontal = true;
+      break;
+    case HWC2::Transform::FlipVRotate90:
+      layer_->transform.rotation = 90.0f;
+      layer_->transform.flip_vertical = true;
+      break;
+    default:
+      layer_->transform.rotation = 0.0f;
+      layer_->transform.flip_horizontal = false;
+      layer_->transform.flip_vertical = false;
+  }
+  geometry_changes_ |= kTransform;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
+  auto num_dirty_rects = visible.numRects;
+  if (num_dirty_rects > 0) {
+    for (uint32_t i = 0; i <= visible.numRects; i++) {
+      LayerRect rect;
+      SetRect(visible.rects[i], &rect);
+      layer_->visible_regions.push_back(rect);
+    }
+  }
+
+  return HWC2::Error::None;
+}
+
+HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
+  z_ = z;
+  geometry_changes_ |= kZOrder;
+  return HWC2::Error::None;
+}
+
+void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
+  target->left = FLOAT(source.left);
+  target->top = FLOAT(source.top);
+  target->right = FLOAT(source.right);
+  target->bottom = FLOAT(source.bottom);
+}
+
+void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
+  // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
+  target->left = std::ceil(source.left);
+  target->top = std::ceil(source.top);
+  target->right = std::floor(source.right);
+  target->bottom = std::floor(source.bottom);
+}
+
+uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
+  // Returns 32 bit ARGB
+  uint32_t a = UINT32(source.a) << 24;
+  uint32_t r = UINT32(source.r) << 16;
+  uint32_t g = UINT32(source.g) << 8;
+  uint32_t b = UINT32(source.b);
+  uint32_t color = a & r & g & b;
+  return color;
+}
+
+LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
+  LayerBufferFormat format = kFormatInvalid;
+  if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
+    switch (source) {
+      case HAL_PIXEL_FORMAT_RGBA_8888:
+        format = kFormatRGBA8888Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_RGBX_8888:
+        format = kFormatRGBX8888Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_BGR_565:
+        format = kFormatBGR565Ubwc;
+        break;
+      case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+      case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+      case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+        format = kFormatYCbCr420SPVenusUbwc;
+        break;
+      default:
+        DLOGE("Unsupported format type for UBWC %d", source);
+        return kFormatInvalid;
+    }
+    return format;
+  }
+
+  switch (source) {
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+      format = kFormatRGBA8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGBA_5551:
+      format = kFormatRGBA5551;
+      break;
+    case HAL_PIXEL_FORMAT_RGBA_4444:
+      format = kFormatRGBA4444;
+      break;
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+      format = kFormatBGRA8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+      format = kFormatRGBX8888;
+      break;
+    case HAL_PIXEL_FORMAT_BGRX_8888:
+      format = kFormatBGRX8888;
+      break;
+    case HAL_PIXEL_FORMAT_RGB_888:
+      format = kFormatRGB888;
+      break;
+    case HAL_PIXEL_FORMAT_RGB_565:
+      format = kFormatRGB565;
+      break;
+    case HAL_PIXEL_FORMAT_BGR_565:
+      format = kFormatBGR565;
+      break;
+    case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+      format = kFormatYCbCr420SemiPlanarVenus;
+      break;
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+      format = kFormatYCrCb420SemiPlanarVenus;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
+      format = kFormatYCbCr420SPVenusUbwc;
+      break;
+    case HAL_PIXEL_FORMAT_YV12:
+      format = kFormatYCrCb420PlanarStride16;
+      break;
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+      format = kFormatYCrCb420SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+      format = kFormatYCbCr420SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+      format = kFormatYCbCr422H2V1SemiPlanar;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_422_I:
+      format = kFormatYCbCr422H2V1Packed;
+      break;
+    default:
+      DLOGW("Unsupported format type = %d", source);
+      return kFormatInvalid;
+  }
+
+  return format;
+}
+
+LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
+  LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
+  switch (s3d_format) {
+    case HAL_NO_3D:
+      sdm_s3d_format = kS3dFormatNone;
+      break;
+    case HAL_3D_SIDE_BY_SIDE_L_R:
+      sdm_s3d_format = kS3dFormatLeftRight;
+      break;
+    case HAL_3D_SIDE_BY_SIDE_R_L:
+      sdm_s3d_format = kS3dFormatRightLeft;
+      break;
+    case HAL_3D_TOP_BOTTOM:
+      sdm_s3d_format = kS3dFormatTopBottom;
+      break;
+    default:
+      DLOGW("Invalid S3D format %d", s3d_format);
+  }
+  return sdm_s3d_format;
+}
+
+DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
+  const MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
+  LayerBuffer *layer_buffer = layer->input_buffer;
+
+  if (!meta_data) {
+    return kErrorNone;
+  }
+
+  if (meta_data->operation & UPDATE_COLOR_SPACE) {
+    if (SetCSC(meta_data->colorSpace, &layer->csc) != kErrorNone) {
+      return kErrorNotSupported;
+    }
+  }
+
+  if (meta_data->operation & SET_IGC) {
+    if (SetIGC(meta_data->igc, &layer->igc) != kErrorNone) {
+      return kErrorNotSupported;
+    }
+  }
+
+  if (meta_data->operation & UPDATE_REFRESH_RATE) {
+    layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate);
+  }
+
+  if ((meta_data->operation & PP_PARAM_INTERLACED) && meta_data->interlaced) {
+    layer_buffer->flags.interlace = true;
+  }
+
+  if (meta_data->operation & LINEAR_FORMAT) {
+    layer_buffer->format = GetSDMFormat(INT32(meta_data->linearFormat), 0);
+  }
+
+  if (meta_data->operation & UPDATE_BUFFER_GEOMETRY) {
+    int actual_width = pvt_handle->width;
+    int actual_height = pvt_handle->height;
+    AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(pvt_handle, actual_width, actual_height);
+    layer_buffer->width = UINT32(actual_width);
+    layer_buffer->height = UINT32(actual_height);
+  }
+
+  if (meta_data->operation & S3D_FORMAT) {
+    layer_buffer->s3d_format = GetS3DFormat(meta_data->s3dFormat);
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWCLayer::SetCSC(ColorSpace_t source, LayerCSC *target) {
+  switch (source) {
+    case ITU_R_601:
+      *target = kCSCLimitedRange601;
+      break;
+    case ITU_R_601_FR:
+      *target = kCSCFullRange601;
+      break;
+    case ITU_R_709:
+      *target = kCSCLimitedRange709;
+      break;
+    default:
+      DLOGE("Unsupported CSC: %d", source);
+      return kErrorNotSupported;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
+  switch (source) {
+    case IGC_NotSpecified:
+      *target = kIGCNotSpecified;
+      break;
+    case IGC_sRGB:
+      *target = kIGCsRGB;
+      break;
+    default:
+      DLOGE("Unsupported IGC: %d", source);
+      return kErrorNotSupported;
+  }
+
+  return kErrorNone;
+}
+
+uint32_t HWCLayer::RoundToStandardFPS(uint32_t fps) {
+  static const uint32_t standard_fps[4] = {30, 24, 48, 60};
+
+  int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
+  for (int i = 0; i < count; i++) {
+    if ((standard_fps[i] - fps) < 2) {
+      // Most likely used for video, the fps can fluctuate
+      // Ex: b/w 29 and 30 for 30 fps clip
+      return standard_fps[i];
+    }
+  }
+
+  return fps;
+}
+
+void HWCLayer::SetComposition(const LayerComposition &source) {
+  auto composition = HWC2::Composition::Invalid;
+  switch (source) {
+    case kCompositionGPU:
+      composition = HWC2::Composition::Client;
+      break;
+    case kCompositionHWCursor:
+      composition = HWC2::Composition::Cursor;
+      break;
+    default:
+      composition = HWC2::Composition::Device;
+      break;
+  }
+  // Update solid fill composition
+  if (layer_->composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
+    composition = HWC2::Composition::SolidColor;
+  }
+  if (composition != composition_) {
+    // Composition changed for this layer
+    composition_changed_ = true;
+    composition_ = composition;
+  }
+}
+void HWCLayer::PushReleaseFence(int32_t fence) {
+  release_fences_.push(fence);
+}
+int32_t HWCLayer::PopReleaseFence(void) {
+  if (release_fences_.empty())
+    return -1;
+  auto fence = release_fences_.front();
+  release_fences_.pop();
+  return fence;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_layers.h b/msm8996/sdm/libs/hwc2/hwc_layers.h
new file mode 100644
index 0000000..de3b7bb
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_layers.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __HWC_LAYERS_H__
+#define __HWC_LAYERS_H__
+
+/* This class translates HWC2 Layer functions to the SDM LayerStack
+ */
+
+#include <gralloc_priv.h>
+#include <qdMetaData.h>
+#include <core/layer_stack.h>
+#define HWC2_INCLUDE_STRINGIFICATION
+#define HWC2_USE_CPP11
+#include <hardware/hwcomposer2.h>
+#undef HWC2_INCLUDE_STRINGIFICATION
+#undef HWC2_USE_CPP11
+#include <set>
+#include <map>
+#include <queue>
+
+namespace sdm {
+
+enum GeometryChanges {
+  kNone = 0x00,
+  kBlendMode = 0x01,
+  kDataspace = 0x02,
+  kDisplayFrame = 0x04,
+  kPlaneAlpha = 0x08,
+  kSourceCrop = 0x0A,
+  kTransform = 0x10,
+  kZOrder = 0x12,
+  kAdded = 0x14,
+  kRemoved = 0x18,
+};
+
+class HWCLayer {
+ public:
+  explicit HWCLayer(hwc2_display_t display_id);
+  ~HWCLayer();
+  uint32_t GetZ() const { return z_; }
+  hwc2_layer_t GetId() const { return id_; }
+  Layer *GetSDMLayer() { return layer_; }
+
+  HWC2::Error SetLayerBlendMode(HWC2::BlendMode mode);
+  HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
+  HWC2::Error SetLayerColor(hwc_color_t color);
+  HWC2::Error SetLayerCompositionType(HWC2::Composition type);
+  HWC2::Error SetLayerDataspace(int32_t dataspace);
+  HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
+  HWC2::Error SetLayerPlaneAlpha(float alpha);
+  HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
+  HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
+  HWC2::Error SetLayerTransform(HWC2::Transform transform);
+  HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
+  HWC2::Error SetLayerZOrder(uint32_t z);
+  void SetComposition(const LayerComposition &source);
+  bool CompositionChanged(void) { return composition_changed_; }
+  HWC2::Composition GetCompositionType() { return composition_; }
+  uint32_t GetGeometryChanges() { return geometry_changes_; }
+  void ResetGeometryChanges() { geometry_changes_ = GeometryChanges::kNone; }
+  void PushReleaseFence(int32_t fence);
+  int32_t PopReleaseFence(void);
+
+ private:
+  Layer *layer_ = nullptr;
+  uint32_t z_ = 0;
+  const hwc2_layer_t id_;
+  const hwc2_display_t display_id_;
+  static std::atomic<hwc2_layer_t> next_id_;
+  std::queue<int32_t> release_fences_;
+
+  HWC2::Composition composition_ = HWC2::Composition::Device;
+  bool composition_changed_ = false;
+  uint32_t geometry_changes_ = GeometryChanges::kNone;
+
+  void SetRect(const hwc_rect_t &source, LayerRect *target);
+  void SetRect(const hwc_frect_t &source, LayerRect *target);
+  uint32_t GetUint32Color(const hwc_color_t &source);
+  LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
+  LayerBufferS3DFormat GetS3DFormat(uint32_t s3d_format);
+  DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer);
+  DisplayError SetCSC(ColorSpace_t source, LayerCSC *target);
+  DisplayError SetIGC(IGC_t source, LayerIGC *target);
+  uint32_t RoundToStandardFPS(uint32_t fps);
+};
+
+struct SortLayersByZ {
+  bool operator()(const HWCLayer *lhs, const HWCLayer *rhs);
+};
+
+}  // namespace sdm
+#endif  // __HWC_LAYERS_H__
diff --git a/msm8996/sdm/libs/hwc2/hwc_session.cpp b/msm8996/sdm/libs/hwc2/hwc_session.cpp
new file mode 100644
index 0000000..a7d2042
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_session.cpp
@@ -0,0 +1,1487 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <core/dump_interface.h>
+#include <core/buffer_allocator.h>
+#include <private/color_params.h>
+#include <utils/constants.h>
+#include <utils/String16.h>
+#include <cutils/properties.h>
+#include <hardware_legacy/uevent.h>
+#include <sys/resource.h>
+#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>
+#include <profiler.h>
+
+#include "hwc_buffer_allocator.h"
+#include "hwc_buffer_sync_handler.h"
+#include "hwc_session.h"
+#include "hwc_debugger.h"
+#include "hwc_display_primary.h"
+#include "hwc_display_virtual.h"
+
+#define __CLASS__ "HWCSession"
+
+#define HWC_UEVENT_SWITCH_HDMI "change@/devices/virtual/switch/hdmi"
+#define HWC_UEVENT_GRAPHICS_FB0 "change@/devices/virtual/graphics/fb0"
+
+static sdm::HWCSession::HWCModuleMethods g_hwc_module_methods;
+
+hwc_module_t HAL_MODULE_INFO_SYM = {
+  .common = {
+    .tag = HARDWARE_MODULE_TAG,
+    .version_major = 3,
+    .version_minor = 0,
+    .id = HWC_HARDWARE_MODULE_ID,
+    .name = "QTI Hardware Composer Module",
+    .author = "CodeAurora Forum",
+    .methods = &g_hwc_module_methods,
+    .dso = 0,
+    .reserved = {0},
+  }
+};
+
+namespace sdm {
+Locker HWCSession::locker_;
+
+HWCSession::HWCSession(const hw_module_t *module) {
+  hwc2_device_t::common.tag = HARDWARE_DEVICE_TAG;
+  hwc2_device_t::common.version = HWC_DEVICE_API_VERSION_2_0;
+  hwc2_device_t::common.module = const_cast<hw_module_t *>(module);
+  hwc2_device_t::common.close = Close;
+  hwc2_device_t::getCapabilities = GetCapabilities;
+  hwc2_device_t::getFunction = GetFunction;
+}
+
+int HWCSession::Init() {
+  int status = -EINVAL;
+  const char *qservice_name = "display.qservice";
+
+  // Start QService and connect to it.
+  qService::QService::init();
+  android::sp<qService::IQService> iqservice = android::interface_cast<qService::IQService>(
+      android::defaultServiceManager()->getService(android::String16(qservice_name)));
+
+  if (iqservice.get()) {
+    iqservice->connect(android::sp<qClient::IQClient>(this));
+    qservice_ = reinterpret_cast<qService::QService *>(iqservice.get());
+  } else {
+    DLOGE("Failed to acquire %s", qservice_name);
+    return -EINVAL;
+  }
+
+  buffer_allocator_ = new HWCBufferAllocator();
+  if (buffer_allocator_ == NULL) {
+    DLOGE("Display core initialization failed due to no memory");
+    return -ENOMEM;
+  }
+
+  buffer_sync_handler_ = new HWCBufferSyncHandler();
+  if (buffer_sync_handler_ == NULL) {
+    DLOGE("Display core initialization failed due to no memory");
+    return -ENOMEM;
+  }
+
+  DisplayError error = CoreInterface::CreateCore(HWCDebugHandler::Get(), buffer_allocator_,
+                                                 buffer_sync_handler_, &core_intf_);
+  if (error != kErrorNone) {
+    DLOGE("Display core initialization failed. Error = %d", error);
+    return -EINVAL;
+  }
+
+  // Read which display is first, and create it and store it in primary slot
+  // TODO(user): This will need to be redone for HWC2 - right now we validate only
+  // the primary physical path
+  HWDisplayInterfaceInfo hw_disp_info;
+  error = core_intf_->GetFirstDisplayInterfaceType(&hw_disp_info);
+  if (error == kErrorNone && hw_disp_info.type == kHDMI && hw_disp_info.is_connected) {
+    // HDMI is primary display. If already connected, then create it and store in
+    // primary display slot. If not connected, create a NULL display for now.
+    status = HWCDisplayExternal::Create(core_intf_, &callbacks_, qservice_,
+                                        &hwc_display_[HWC_DISPLAY_PRIMARY]);
+  } else {
+    // Create and power on primary display
+    status = HWCDisplayPrimary::Create(core_intf_, buffer_allocator_, &callbacks_, qservice_,
+                                       &hwc_display_[HWC_DISPLAY_PRIMARY]);
+  }
+
+  if (status) {
+    CoreInterface::DestroyCore();
+    return status;
+  }
+
+  color_mgr_ = HWCColorManager::CreateColorManager();
+  if (!color_mgr_) {
+    DLOGW("Failed to load HWCColorManager.");
+  }
+
+  if (pthread_create(&uevent_thread_, NULL, &HWCUeventThread, this) < 0) {
+    DLOGE("Failed to start = %s, error = %s", uevent_thread_name_, strerror(errno));
+    HWCDisplayPrimary::Destroy(hwc_display_[HWC_DISPLAY_PRIMARY]);
+    hwc_display_[HWC_DISPLAY_PRIMARY] = 0;
+    CoreInterface::DestroyCore();
+    return -errno;
+  }
+
+  return 0;
+}
+
+int HWCSession::Deinit() {
+  HWCDisplayPrimary::Destroy(hwc_display_[HWC_DISPLAY_PRIMARY]);
+  hwc_display_[HWC_DISPLAY_PRIMARY] = 0;
+  if (color_mgr_) {
+    color_mgr_->DestroyColorManager();
+  }
+  uevent_thread_exit_ = true;
+  pthread_join(uevent_thread_, NULL);
+
+  DisplayError error = CoreInterface::DestroyCore();
+  if (error != kErrorNone) {
+    DLOGE("Display core de-initialization failed. Error = %d", error);
+  }
+
+  return 0;
+}
+
+int HWCSession::Open(const hw_module_t *module, const char *name, hw_device_t **device) {
+  SEQUENCE_WAIT_SCOPE_LOCK(locker_);
+
+  if (!module || !name || !device) {
+    DLOGE("Invalid parameters.");
+    return -EINVAL;
+  }
+
+  if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
+    HWCSession *hwc_session = new HWCSession(module);
+    if (!hwc_session) {
+      return -ENOMEM;
+    }
+
+    int status = hwc_session->Init();
+    if (status != 0) {
+      delete hwc_session;
+      return status;
+    }
+
+    hwc2_device_t *composer_device = hwc_session;
+    *device = reinterpret_cast<hw_device_t *>(composer_device);
+  }
+
+  return 0;
+}
+
+int HWCSession::Close(hw_device_t *device) {
+  SEQUENCE_WAIT_SCOPE_LOCK(locker_);
+
+  if (!device) {
+    return -EINVAL;
+  }
+
+  hwc2_device_t *composer_device = reinterpret_cast<hwc2_device_t *>(device);
+  HWCSession *hwc_session = static_cast<HWCSession *>(composer_device);
+
+  hwc_session->Deinit();
+  delete hwc_session;
+
+  return 0;
+}
+
+void HWCSession::GetCapabilities(struct hwc2_device *device, uint32_t *outCount,
+                                 int32_t *outCapabilities) {
+  if (outCapabilities == NULL) {
+    *outCount = 0;
+  }
+}
+
+template <typename PFN, typename T>
+static hwc2_function_pointer_t AsFP(T function) {
+  static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer");
+  return reinterpret_cast<hwc2_function_pointer_t>(function);
+}
+
+// HWC2 functions returned in GetFunction
+// Defined in the same order as in the HWC2 header
+
+static int32_t AcceptDisplayChanges(hwc2_device_t *device, hwc2_display_t display) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::AcceptDisplayChanges);
+}
+
+static int32_t CreateLayer(hwc2_device_t *device, hwc2_display_t display,
+                           hwc2_layer_t *out_layer_id) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::CreateLayer, out_layer_id);
+}
+
+int32_t HWCSession::CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height,
+                                         hwc2_display_t *out_display_id) {
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+
+  HWCSession *hwc_session = static_cast<HWCSession *>(device);
+  auto status = hwc_session->CreateVirtualDisplayObject(width, height);
+  if (status == HWC2::Error::None)
+    *out_display_id = HWC_DISPLAY_VIRTUAL;
+  return INT32(status);
+}
+
+static int32_t DestroyLayer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::DestroyLayer, layer);
+}
+
+int32_t HWCSession::DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display) {
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+
+  auto *hwc_session = static_cast<HWCSession *>(device);
+
+  if (hwc_session->hwc_display_[display]) {
+    HWCDisplayVirtual::Destroy(hwc_session->hwc_display_[display]);
+    return HWC2_ERROR_NONE;
+  } else {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+}
+
+static void Dump(hwc2_device_t *device, uint32_t *out_size, char *out_buffer) {
+  if (!device) {
+    return;
+  }
+
+  if (out_buffer == nullptr) {
+    *out_size = 4096;  // TODO(user): Adjust required dump size
+  } else {
+    DumpInterface::GetDump(out_buffer, 4096);  // TODO(user): Fix this workaround
+    *out_size = sizeof(out_buffer);
+  }
+}
+
+static int32_t GetActiveConfig(hwc2_device_t *device, hwc2_display_t display,
+                               hwc2_config_t *out_config) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetActiveConfig, out_config);
+}
+
+static int32_t GetChangedCompositionTypes(hwc2_device_t *device, hwc2_display_t display,
+                                          uint32_t *out_num_elements, hwc2_layer_t *out_layers,
+                                          int32_t *out_types) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetChangedCompositionTypes,
+                                         out_num_elements, out_layers, out_types);
+}
+
+static int32_t GetClientTargetSupport(hwc2_device_t *device, hwc2_display_t display, uint32_t width,
+                                      uint32_t height, int32_t format, int32_t dataspace) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetClientTargetSupport,
+                                         width, height, format, dataspace);
+}
+
+// TODO(user): GetColorModes
+
+static int32_t GetDisplayAttribute(hwc2_device_t *device, hwc2_display_t display,
+                                   hwc2_config_t config, int32_t int_attribute,
+                                   int32_t *out_value) {
+  auto attribute = static_cast<HWC2::Attribute>(int_attribute);
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayAttribute, config,
+                                         attribute, out_value);
+}
+
+static int32_t GetDisplayConfigs(hwc2_device_t *device, hwc2_display_t display,
+                                 uint32_t *out_num_configs, hwc2_config_t *out_configs) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayConfigs,
+                                         out_num_configs, out_configs);
+}
+
+static int32_t GetDisplayName(hwc2_device_t *device, hwc2_display_t display, uint32_t *out_size,
+                              char *out_name) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayName, out_size,
+                                         out_name);
+}
+
+static int32_t GetDisplayRequests(hwc2_device_t *device, hwc2_display_t display,
+                                  int32_t *out_display_requests, uint32_t *out_num_elements,
+                                  hwc2_layer_t *out_layers, int32_t *out_layer_requests) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayRequests,
+                                         out_display_requests, out_num_elements, out_layers,
+                                         out_layer_requests);
+}
+
+static int32_t GetDisplayType(hwc2_device_t *device, hwc2_display_t display, int32_t *out_type) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayType, out_type);
+}
+
+static int32_t GetDozeSupport(hwc2_device_t *device, hwc2_display_t display, int32_t *out_support) {
+  // TODO(user): Check if it is an HDMI as primary display and disable support for it
+  if (display == HWC_DISPLAY_PRIMARY) {
+    *out_support = 1;
+  } else {
+    *out_support = 0;
+  }
+  return HWC2_ERROR_NONE;
+}
+
+static uint32_t GetMaxVirtualDisplayCount(hwc2_device_t *device) {
+  return 1;
+}
+
+static int32_t GetReleaseFences(hwc2_device_t *device, hwc2_display_t display,
+                                uint32_t *out_num_elements, hwc2_layer_t *out_layers,
+                                int32_t *out_fences) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetReleaseFences,
+                                         out_num_elements, out_layers, out_fences);
+}
+
+int32_t HWCSession::PresentDisplay(hwc2_device_t *device, hwc2_display_t display,
+                                   int32_t *out_retire_fence) {
+  HWCSession *hwc_session = static_cast<HWCSession *>(device);
+  DTRACE_SCOPED();
+  SEQUENCE_EXIT_SCOPE_LOCK(locker_);
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+  // TODO(user): Handle solid fill layers
+  auto status = HWC2::Error::BadDisplay;
+  // TODO(user): Handle virtual display/HDMI concurrency
+
+  if (hwc_session->hwc_display_[display]) {
+    status = hwc_session->hwc_display_[display]->Present(out_retire_fence);
+    // This is only indicative of how many times SurfaceFlinger posts
+    // frames to the display.
+    CALC_FPS();
+  }
+
+  return INT32(status);
+}
+
+int32_t HWCSession::RegisterCallback(hwc2_device_t *device, int32_t descriptor,
+                                     hwc2_callback_data_t callback_data,
+                                     hwc2_function_pointer_t pointer) {
+  HWCSession *hwc_session = static_cast<HWCSession *>(device);
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+  auto desc = static_cast<HWC2::Callback>(descriptor);
+  auto error = hwc_session->callbacks_.Register(desc, callback_data, pointer);
+  DLOGD("Registering callback: %s", to_string(desc).c_str());
+  // TODO(user): The hotplug should only be called when the HOTPLUG callback is registered
+  // However, this causes SurfaceFlinger to behave weirdly - investigate further.
+  hwc_session->callbacks_.Hotplug(HWC_DISPLAY_PRIMARY, HWC2::Connection::Connected);
+  return INT32(error);
+}
+
+static int32_t SetActiveConfig(hwc2_device_t *device, hwc2_display_t display,
+                               hwc2_config_t config) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetActiveConfig, config);
+}
+
+static int32_t SetClientTarget(hwc2_device_t *device, hwc2_display_t display,
+                               buffer_handle_t target, int32_t acquire_fence, int32_t dataspace) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetClientTarget, target,
+                                         acquire_fence, dataspace);
+}
+
+// TODO(user): SetColorMode, SetColorTransform
+
+static int32_t SetCursorPosition(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                 int32_t x, int32_t y) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetCursorPosition, layer, x,
+                                         y);
+}
+
+static int32_t SetLayerBlendMode(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                 int32_t int_mode) {
+  auto mode = static_cast<HWC2::BlendMode>(int_mode);
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerBlendMode, mode);
+}
+
+static int32_t SetLayerBuffer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                              buffer_handle_t buffer, int32_t acquire_fence) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerBuffer, buffer,
+                                       acquire_fence);
+}
+
+static int32_t SetLayerColor(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                             hwc_color_t color) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerColor, color);
+}
+
+static int32_t SetLayerCompositionType(hwc2_device_t *device, hwc2_display_t display,
+                                       hwc2_layer_t layer, int32_t int_type) {
+  auto type = static_cast<HWC2::Composition>(int_type);
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerCompositionType,
+                                       type);
+}
+
+static int32_t SetLayerDataspace(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                 int32_t dataspace) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerDataspace,
+                                       dataspace);
+}
+
+static int32_t SetLayerDisplayFrame(hwc2_device_t *device, hwc2_display_t display,
+                                    hwc2_layer_t layer, hwc_rect_t frame) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerDisplayFrame,
+                                       frame);
+}
+
+static int32_t SetLayerPlaneAlpha(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                  float alpha) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerPlaneAlpha,
+                                       alpha);
+}
+
+static int32_t SetLayerSourceCrop(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                  hwc_frect_t crop) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerSourceCrop, crop);
+}
+
+static int32_t SetLayerSurfaceDamage(hwc2_device_t *device, hwc2_display_t display,
+                                     hwc2_layer_t layer, hwc_region_t damage) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerSurfaceDamage,
+                                       damage);
+}
+
+static int32_t SetLayerTransform(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                                 int32_t int_transform) {
+  auto transform = static_cast<HWC2::Transform>(int_transform);
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerTransform,
+                                       transform);
+}
+
+static int32_t SetLayerVisibleRegion(hwc2_device_t *device, hwc2_display_t display,
+                                     hwc2_layer_t layer, hwc_region_t visible) {
+  return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerVisibleRegion,
+                                       visible);
+}
+
+static int32_t SetLayerZOrder(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
+                              uint32_t z) {
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetLayerZOrder, layer, z);
+}
+
+int32_t HWCSession::SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display,
+                                    buffer_handle_t buffer, int32_t releaseFence) {
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+
+  auto *hwc_session = static_cast<HWCSession *>(device);
+  if (display == HWC_DISPLAY_VIRTUAL && hwc_session->hwc_display_[display]) {
+    auto vds = reinterpret_cast<HWCDisplayVirtual *>(hwc_session->hwc_display_[display]);
+    auto status = vds->SetOutputBuffer(buffer, releaseFence);
+    return INT32(status);
+  } else {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+}
+
+static int32_t SetPowerMode(hwc2_device_t *device, hwc2_display_t display, int32_t int_mode) {
+  auto mode = static_cast<HWC2::PowerMode>(int_mode);
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetPowerMode, mode);
+}
+
+static int32_t SetVsyncEnabled(hwc2_device_t *device, hwc2_display_t display, int32_t int_enabled) {
+  auto enabled = static_cast<HWC2::Vsync>(int_enabled);
+  return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetVsyncEnabled, enabled);
+}
+
+int32_t HWCSession::ValidateDisplay(hwc2_device_t *device, hwc2_display_t display,
+                                    uint32_t *out_num_types, uint32_t *out_num_requests) {
+  DTRACE_SCOPED();
+  HWCSession *hwc_session = static_cast<HWCSession *>(device);
+  if (!device) {
+    return HWC2_ERROR_BAD_DISPLAY;
+  }
+
+  // TODO(user): Handle secure session, handle QDCM solid fill
+  // Handle external_pending_connect_ in CreateVirtualDisplay
+  auto status = HWC2::Error::BadDisplay;
+  if (hwc_session->hwc_display_[display]) {
+    SEQUENCE_ENTRY_SCOPE_LOCK(locker_);
+    if (display == HWC_DISPLAY_PRIMARY) {
+      // TODO(user): This can be moved to HWCDisplayPrimary
+      if (hwc_session->reset_panel_) {
+        DLOGW("panel is in bad state, resetting the panel");
+        hwc_session->ResetPanel();
+      }
+
+      if (hwc_session->need_invalidate_) {
+        hwc_session->callbacks_.Refresh(display);
+      }
+    }
+
+    status = hwc_session->hwc_display_[display]->Validate(out_num_types, out_num_requests);
+  }
+  return INT32(status);
+}
+
+hwc2_function_pointer_t HWCSession::GetFunction(struct hwc2_device *device,
+                                                int32_t int_descriptor) {
+  auto descriptor = static_cast<HWC2::FunctionDescriptor>(int_descriptor);
+
+  switch (descriptor) {
+    case HWC2::FunctionDescriptor::AcceptDisplayChanges:
+      return AsFP<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(AcceptDisplayChanges);
+    case HWC2::FunctionDescriptor::CreateLayer:
+      return AsFP<HWC2_PFN_CREATE_LAYER>(CreateLayer);
+    case HWC2::FunctionDescriptor::CreateVirtualDisplay:
+      return AsFP<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(HWCSession::CreateVirtualDisplay);
+    case HWC2::FunctionDescriptor::DestroyLayer:
+      return AsFP<HWC2_PFN_DESTROY_LAYER>(DestroyLayer);
+    case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
+      return AsFP<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(HWCSession::DestroyVirtualDisplay);
+    case HWC2::FunctionDescriptor::Dump:
+      return AsFP<HWC2_PFN_DUMP>(Dump);
+    case HWC2::FunctionDescriptor::GetActiveConfig:
+      return AsFP<HWC2_PFN_GET_ACTIVE_CONFIG>(GetActiveConfig);
+    case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
+      return AsFP<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(GetChangedCompositionTypes);
+    case HWC2::FunctionDescriptor::GetClientTargetSupport:
+      return AsFP<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(GetClientTargetSupport);
+    // case HWC2::FunctionDescriptor::GetColorModes:
+    // TODO(user): Support later
+    case HWC2::FunctionDescriptor::GetDisplayAttribute:
+      return AsFP<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(GetDisplayAttribute);
+    case HWC2::FunctionDescriptor::GetDisplayConfigs:
+      return AsFP<HWC2_PFN_GET_DISPLAY_CONFIGS>(GetDisplayConfigs);
+    case HWC2::FunctionDescriptor::GetDisplayName:
+      return AsFP<HWC2_PFN_GET_DISPLAY_NAME>(GetDisplayName);
+    case HWC2::FunctionDescriptor::GetDisplayRequests:
+      return AsFP<HWC2_PFN_GET_DISPLAY_REQUESTS>(GetDisplayRequests);
+    case HWC2::FunctionDescriptor::GetDisplayType:
+      return AsFP<HWC2_PFN_GET_DISPLAY_TYPE>(GetDisplayType);
+    case HWC2::FunctionDescriptor::GetDozeSupport:
+      return AsFP<HWC2_PFN_GET_DOZE_SUPPORT>(GetDozeSupport);
+    case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
+      return AsFP<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(GetMaxVirtualDisplayCount);
+    case HWC2::FunctionDescriptor::GetReleaseFences:
+      return AsFP<HWC2_PFN_GET_RELEASE_FENCES>(GetReleaseFences);
+    case HWC2::FunctionDescriptor::PresentDisplay:
+      return AsFP<HWC2_PFN_PRESENT_DISPLAY>(PresentDisplay);
+    case HWC2::FunctionDescriptor::RegisterCallback:
+      return AsFP<HWC2_PFN_REGISTER_CALLBACK>(RegisterCallback);
+    case HWC2::FunctionDescriptor::SetActiveConfig:
+      return AsFP<HWC2_PFN_SET_ACTIVE_CONFIG>(SetActiveConfig);
+    case HWC2::FunctionDescriptor::SetClientTarget:
+      return AsFP<HWC2_PFN_SET_CLIENT_TARGET>(SetClientTarget);
+    // TODO(user): Support later
+    // case HWC2::FunctionDescriptor::SetColorMode:
+    // case HWC2::FunctionDescriptor::SetColorTransform:
+    // break;
+    case HWC2::FunctionDescriptor::SetCursorPosition:
+      return AsFP<HWC2_PFN_SET_CURSOR_POSITION>(SetCursorPosition);
+    case HWC2::FunctionDescriptor::SetLayerBlendMode:
+      return AsFP<HWC2_PFN_SET_LAYER_BLEND_MODE>(SetLayerBlendMode);
+    case HWC2::FunctionDescriptor::SetLayerBuffer:
+      return AsFP<HWC2_PFN_SET_LAYER_BUFFER>(SetLayerBuffer);
+    case HWC2::FunctionDescriptor::SetLayerColor:
+      return AsFP<HWC2_PFN_SET_LAYER_COLOR>(SetLayerColor);
+    case HWC2::FunctionDescriptor::SetLayerCompositionType:
+      return AsFP<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(SetLayerCompositionType);
+    case HWC2::FunctionDescriptor::SetLayerDataspace:
+      return AsFP<HWC2_PFN_SET_LAYER_DATASPACE>(SetLayerDataspace);
+    case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
+      return AsFP<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(SetLayerDisplayFrame);
+    case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
+      return AsFP<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(SetLayerPlaneAlpha);
+    // Sideband stream is not supported
+    // case HWC2::FunctionDescriptor::SetLayerSidebandStream:
+    case HWC2::FunctionDescriptor::SetLayerSourceCrop:
+      return AsFP<HWC2_PFN_SET_LAYER_SOURCE_CROP>(SetLayerSourceCrop);
+    case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
+      return AsFP<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(SetLayerSurfaceDamage);
+    case HWC2::FunctionDescriptor::SetLayerTransform:
+      return AsFP<HWC2_PFN_SET_LAYER_TRANSFORM>(SetLayerTransform);
+    case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
+      return AsFP<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(SetLayerVisibleRegion);
+    case HWC2::FunctionDescriptor::SetLayerZOrder:
+      return AsFP<HWC2_PFN_SET_LAYER_Z_ORDER>(SetLayerZOrder);
+    case HWC2::FunctionDescriptor::SetOutputBuffer:
+      return AsFP<HWC2_PFN_SET_OUTPUT_BUFFER>(SetOutputBuffer);
+    case HWC2::FunctionDescriptor::SetPowerMode:
+      return AsFP<HWC2_PFN_SET_POWER_MODE>(SetPowerMode);
+    case HWC2::FunctionDescriptor::SetVsyncEnabled:
+      return AsFP<HWC2_PFN_SET_VSYNC_ENABLED>(SetVsyncEnabled);
+    case HWC2::FunctionDescriptor::ValidateDisplay:
+      return AsFP<HWC2_PFN_VALIDATE_DISPLAY>(HWCSession::ValidateDisplay);
+    default:
+      DLOGD("Unknown/Unimplemented function descriptor: %d (%s)", int_descriptor,
+            to_string(descriptor).c_str());
+      return nullptr;
+  }
+  return nullptr;
+}
+
+// TODO(user): handle locking
+
+HWC2::Error HWCSession::CreateVirtualDisplayObject(uint32_t width, uint32_t height) {
+  if (hwc_display_[HWC_DISPLAY_VIRTUAL]) {
+    return HWC2::Error::NoResources;
+  }
+  auto status = HWCDisplayVirtual::Create(core_intf_, &callbacks_, width, height,
+                                          &hwc_display_[HWC_DISPLAY_VIRTUAL]);
+  // TODO(user): validate width and height support
+  if (status)
+    return HWC2::Error::Unsupported;
+
+  return HWC2::Error::None;
+}
+
+int32_t HWCSession::ConnectDisplay(int disp) {
+  DLOGI("Display = %d", disp);
+
+  int status = 0;
+  uint32_t primary_width = 0;
+  uint32_t primary_height = 0;
+
+  hwc_display_[HWC_DISPLAY_PRIMARY]->GetFrameBufferResolution(&primary_width, &primary_height);
+
+  if (disp == HWC_DISPLAY_EXTERNAL) {
+    status = HWCDisplayExternal::Create(core_intf_, &callbacks_, primary_width, primary_height,
+                                        qservice_, false, &hwc_display_[disp]);
+  } else {
+    DLOGE("Invalid display type");
+    return -1;
+  }
+
+  if (!status) {
+    hwc_display_[disp]->SetSecureDisplay(secure_display_active_);
+  }
+
+  return status;
+}
+
+int HWCSession::DisconnectDisplay(int disp) {
+  DLOGI("Display = %d", disp);
+
+  if (disp == HWC_DISPLAY_EXTERNAL) {
+    HWCDisplayExternal::Destroy(hwc_display_[disp]);
+  } else if (disp == HWC_DISPLAY_VIRTUAL) {
+    HWCDisplayVirtual::Destroy(hwc_display_[disp]);
+  } else {
+    DLOGE("Invalid display type");
+    return -1;
+  }
+
+  hwc_display_[disp] = NULL;
+
+  return 0;
+}
+
+// Qclient methods
+android::status_t HWCSession::notifyCallback(uint32_t command, const android::Parcel *input_parcel,
+                                             android::Parcel *output_parcel) {
+  SEQUENCE_WAIT_SCOPE_LOCK(locker_);
+
+  android::status_t status = 0;
+
+  switch (command) {
+    case qService::IQService::DYNAMIC_DEBUG:
+      DynamicDebug(input_parcel);
+      break;
+
+    case qService::IQService::SCREEN_REFRESH:
+      callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+      break;
+
+    case qService::IQService::SET_IDLE_TIMEOUT:
+      if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+        uint32_t timeout = UINT32(input_parcel->readInt32());
+        hwc_display_[HWC_DISPLAY_PRIMARY]->SetIdleTimeoutMs(timeout);
+      }
+      break;
+
+    case qService::IQService::SET_FRAME_DUMP_CONFIG:
+      SetFrameDumpConfig(input_parcel);
+      break;
+
+    case qService::IQService::SET_MAX_PIPES_PER_MIXER:
+      status = SetMaxMixerStages(input_parcel);
+      break;
+
+    case qService::IQService::SET_DISPLAY_MODE:
+      status = SetDisplayMode(input_parcel);
+      break;
+
+    case qService::IQService::SET_SECONDARY_DISPLAY_STATUS:
+      status = SetSecondaryDisplayStatus(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::CONFIGURE_DYN_REFRESH_RATE:
+      status = ConfigureRefreshRate(input_parcel);
+      break;
+
+    case qService::IQService::SET_VIEW_FRAME:
+      break;
+
+    case qService::IQService::TOGGLE_SCREEN_UPDATES:
+      status = ToggleScreenUpdates(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::QDCM_SVC_CMDS:
+      status = QdcmCMDHandler(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::MIN_HDCP_ENCRYPTION_LEVEL_CHANGED:
+      status = OnMinHdcpEncryptionLevelChange(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::CONTROL_PARTIAL_UPDATE:
+      status = ControlPartialUpdate(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::SET_ACTIVE_CONFIG:
+      status = HandleSetActiveDisplayConfig(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_ACTIVE_CONFIG:
+      status = HandleGetActiveDisplayConfig(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_CONFIG_COUNT:
+      status = HandleGetDisplayConfigCount(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_DISPLAY_ATTRIBUTES_FOR_CONFIG:
+      status = HandleGetDisplayAttributesForConfig(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_PANEL_BRIGHTNESS:
+      status = GetPanelBrightness(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::SET_PANEL_BRIGHTNESS:
+      status = SetPanelBrightness(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_DISPLAY_VISIBLE_REGION:
+      status = GetVisibleDisplayRect(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::SET_CAMERA_STATUS:
+      status = SetDynamicBWForCamera(input_parcel, output_parcel);
+      break;
+
+    case qService::IQService::GET_BW_TRANSACTION_STATUS:
+      status = GetBWTransactionStatus(input_parcel, output_parcel);
+      break;
+
+    default:
+      DLOGW("QService command = %d is not supported", command);
+      return -EINVAL;
+  }
+
+  return status;
+}
+
+android::status_t HWCSession::ToggleScreenUpdates(const android::Parcel *input_parcel,
+                                                  android::Parcel *output_parcel) {
+  int input = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+
+  if (hwc_display_[HWC_DISPLAY_PRIMARY] && (input <= 1) && (input >= 0)) {
+    error = hwc_display_[HWC_DISPLAY_PRIMARY]->ToggleScreenUpdates(input == 1);
+    if (error != 0) {
+      DLOGE("Failed to toggle screen updates = %d. Error = %d", input, error);
+    }
+  }
+  output_parcel->writeInt32(error);
+
+  return error;
+}
+
+android::status_t HWCSession::SetPanelBrightness(const android::Parcel *input_parcel,
+                                                 android::Parcel *output_parcel) {
+  int level = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+
+  if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+    error = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPanelBrightness(level);
+    if (error != 0) {
+      DLOGE("Failed to set the panel brightness = %d. Error = %d", level, error);
+    }
+  }
+  output_parcel->writeInt32(error);
+
+  return error;
+}
+
+android::status_t HWCSession::GetPanelBrightness(const android::Parcel *input_parcel,
+                                                 android::Parcel *output_parcel) {
+  int error = android::BAD_VALUE;
+  int ret = error;
+
+  if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+    error = hwc_display_[HWC_DISPLAY_PRIMARY]->GetPanelBrightness(&ret);
+    if (error != 0) {
+      ret = error;
+      DLOGE("Failed to get the panel brightness. Error = %d", error);
+    }
+  }
+  output_parcel->writeInt32(ret);
+
+  return error;
+}
+
+android::status_t HWCSession::ControlPartialUpdate(const android::Parcel *input_parcel,
+                                                   android::Parcel *out) {
+  DisplayError error = kErrorNone;
+  int ret = 0;
+  uint32_t disp_id = UINT32(input_parcel->readInt32());
+  uint32_t enable = UINT32(input_parcel->readInt32());
+
+  if (disp_id != HWC_DISPLAY_PRIMARY) {
+    DLOGW("CONTROL_PARTIAL_UPDATE is not applicable for display = %d", disp_id);
+    ret = -EINVAL;
+    out->writeInt32(ret);
+    return ret;
+  }
+
+  if (!hwc_display_[HWC_DISPLAY_PRIMARY]) {
+    DLOGE("primary display object is not instantiated");
+    ret = -EINVAL;
+    out->writeInt32(ret);
+    return ret;
+  }
+
+  uint32_t pending = 0;
+  error = hwc_display_[HWC_DISPLAY_PRIMARY]->ControlPartialUpdate(enable, &pending);
+
+  if (error == kErrorNone) {
+    if (!pending) {
+      out->writeInt32(ret);
+      return ret;
+    }
+  } else if (error == kErrorNotSupported) {
+    out->writeInt32(ret);
+    return ret;
+  } else {
+    ret = -EINVAL;
+    out->writeInt32(ret);
+    return ret;
+  }
+
+  // Todo(user): Unlock it before sending events to client. It may cause deadlocks in future.
+  callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+
+  // Wait until partial update control is complete
+  ret = locker_.WaitFinite(kPartialUpdateControlTimeoutMs);
+
+  out->writeInt32(ret);
+
+  return ret;
+}
+
+android::status_t HWCSession::HandleSetActiveDisplayConfig(const android::Parcel *input_parcel,
+                                                           android::Parcel *output_parcel) {
+  int config = input_parcel->readInt32();
+  int dpy = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+
+  if (dpy > HWC_DISPLAY_VIRTUAL) {
+    return android::BAD_VALUE;
+  }
+
+  if (hwc_display_[dpy]) {
+    error = hwc_display_[dpy]->SetActiveDisplayConfig(config);
+    if (error == 0) {
+      callbacks_.Refresh(0);
+    }
+  }
+
+  return error;
+}
+
+android::status_t HWCSession::HandleGetActiveDisplayConfig(const android::Parcel *input_parcel,
+                                                           android::Parcel *output_parcel) {
+  int dpy = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+
+  if (dpy > HWC_DISPLAY_VIRTUAL) {
+    return android::BAD_VALUE;
+  }
+
+  if (hwc_display_[dpy]) {
+    uint32_t config = 0;
+    error = hwc_display_[dpy]->GetActiveDisplayConfig(&config);
+    if (error == 0) {
+      output_parcel->writeInt32(INT(config));
+    }
+  }
+
+  return error;
+}
+
+android::status_t HWCSession::HandleGetDisplayConfigCount(const android::Parcel *input_parcel,
+                                                          android::Parcel *output_parcel) {
+  int dpy = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+
+  if (dpy > HWC_DISPLAY_VIRTUAL) {
+    return android::BAD_VALUE;
+  }
+
+  uint32_t count = 0;
+  if (hwc_display_[dpy]) {
+    error = hwc_display_[dpy]->GetDisplayConfigCount(&count);
+    if (error == 0) {
+      output_parcel->writeInt32(INT(count));
+    }
+  }
+
+  return error;
+}
+
+android::status_t HWCSession::HandleGetDisplayAttributesForConfig(
+    const android::Parcel *input_parcel, android::Parcel *output_parcel) {
+  int config = input_parcel->readInt32();
+  int dpy = input_parcel->readInt32();
+  int error = android::BAD_VALUE;
+  DisplayConfigVariableInfo attributes;
+
+  if (dpy > HWC_DISPLAY_VIRTUAL) {
+    return android::BAD_VALUE;
+  }
+
+  if (hwc_display_[dpy]) {
+    error = hwc_display_[dpy]->GetDisplayAttributesForConfig(config, &attributes);
+    if (error == 0) {
+      output_parcel->writeInt32(INT(attributes.vsync_period_ns));
+      output_parcel->writeInt32(INT(attributes.x_pixels));
+      output_parcel->writeInt32(INT(attributes.y_pixels));
+      output_parcel->writeFloat(attributes.x_dpi);
+      output_parcel->writeFloat(attributes.y_dpi);
+      output_parcel->writeInt32(0);  // Panel type, unsupported.
+    }
+  }
+
+  return error;
+}
+
+android::status_t HWCSession::SetSecondaryDisplayStatus(const android::Parcel *input_parcel,
+                                                        android::Parcel *output_parcel) {
+  int ret = -EINVAL;
+
+  uint32_t display_id = UINT32(input_parcel->readInt32());
+  uint32_t display_status = UINT32(input_parcel->readInt32());
+
+  DLOGI("Display = %d, Status = %d", display_id, display_status);
+
+  if (display_id >= HWC_NUM_DISPLAY_TYPES) {
+    DLOGE("Invalid display_id");
+  } else if (display_id == HWC_DISPLAY_PRIMARY) {
+    DLOGE("Not supported for this display");
+  } else if (!hwc_display_[display_id]) {
+    DLOGW("Display is not connected");
+  } else {
+    ret = hwc_display_[display_id]->SetDisplayStatus(display_status);
+  }
+
+  output_parcel->writeInt32(ret);
+
+  return ret;
+}
+
+android::status_t HWCSession::ConfigureRefreshRate(const android::Parcel *input_parcel) {
+  uint32_t operation = UINT32(input_parcel->readInt32());
+  switch (operation) {
+    case qdutils::DISABLE_METADATA_DYN_REFRESH_RATE:
+      return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(
+          HWCDisplayPrimary::SET_METADATA_DYN_REFRESH_RATE, false);
+    case qdutils::ENABLE_METADATA_DYN_REFRESH_RATE:
+      return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(
+          HWCDisplayPrimary::SET_METADATA_DYN_REFRESH_RATE, true);
+    case qdutils::SET_BINDER_DYN_REFRESH_RATE: {
+      uint32_t refresh_rate = UINT32(input_parcel->readInt32());
+      return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(
+          HWCDisplayPrimary::SET_BINDER_DYN_REFRESH_RATE, refresh_rate);
+    }
+    default:
+      DLOGW("Invalid operation %d", operation);
+      return -EINVAL;
+  }
+
+  return 0;
+}
+
+android::status_t HWCSession::SetDisplayMode(const android::Parcel *input_parcel) {
+  uint32_t mode = UINT32(input_parcel->readInt32());
+  return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(HWCDisplayPrimary::SET_DISPLAY_MODE, mode);
+}
+
+android::status_t HWCSession::SetMaxMixerStages(const android::Parcel *input_parcel) {
+  DisplayError error = kErrorNone;
+  uint32_t bit_mask_display_type = UINT32(input_parcel->readInt32());
+  uint32_t max_mixer_stages = UINT32(input_parcel->readInt32());
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_PRIMARY)) {
+    if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+      error = hwc_display_[HWC_DISPLAY_PRIMARY]->SetMaxMixerStages(max_mixer_stages);
+      if (error != kErrorNone) {
+        return -EINVAL;
+      }
+    }
+  }
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_EXTERNAL)) {
+    if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+      error = hwc_display_[HWC_DISPLAY_EXTERNAL]->SetMaxMixerStages(max_mixer_stages);
+      if (error != kErrorNone) {
+        return -EINVAL;
+      }
+    }
+  }
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_VIRTUAL)) {
+    if (hwc_display_[HWC_DISPLAY_VIRTUAL]) {
+      error = hwc_display_[HWC_DISPLAY_VIRTUAL]->SetMaxMixerStages(max_mixer_stages);
+      if (error != kErrorNone) {
+        return -EINVAL;
+      }
+    }
+  }
+
+  return 0;
+}
+
+android::status_t HWCSession::SetDynamicBWForCamera(const android::Parcel *input_parcel,
+                                                    android::Parcel *output_parcel) {
+  DisplayError error = kErrorNone;
+  uint32_t camera_status = UINT32(input_parcel->readInt32());
+  HWBwModes mode = camera_status > 0 ? kBwCamera : kBwDefault;
+
+  // trigger invalidate to apply new bw caps.
+  callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+
+  error = core_intf_->SetMaxBandwidthMode(mode);
+  if (error != kErrorNone) {
+    return -EINVAL;
+  }
+
+  new_bw_mode_ = true;
+  need_invalidate_ = true;
+
+  return 0;
+}
+
+android::status_t HWCSession::GetBWTransactionStatus(const android::Parcel *input_parcel,
+                                                     android::Parcel *output_parcel) {
+  bool state = true;
+
+  if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+    if (sync_wait(bw_mode_release_fd_, 0) < 0) {
+      DLOGI("bw_transaction_release_fd is not yet signalled: err= %s", strerror(errno));
+      state = false;
+    }
+    output_parcel->writeInt32(state);
+  }
+
+  return 0;
+}
+
+void HWCSession::SetFrameDumpConfig(const android::Parcel *input_parcel) {
+  uint32_t frame_dump_count = UINT32(input_parcel->readInt32());
+  uint32_t bit_mask_display_type = UINT32(input_parcel->readInt32());
+  uint32_t bit_mask_layer_type = UINT32(input_parcel->readInt32());
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_PRIMARY)) {
+    if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
+      hwc_display_[HWC_DISPLAY_PRIMARY]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type);
+    }
+  }
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_EXTERNAL)) {
+    if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+      hwc_display_[HWC_DISPLAY_EXTERNAL]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type);
+    }
+  }
+
+  if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_VIRTUAL)) {
+    if (hwc_display_[HWC_DISPLAY_VIRTUAL]) {
+      hwc_display_[HWC_DISPLAY_VIRTUAL]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type);
+    }
+  }
+}
+
+void HWCSession::DynamicDebug(const android::Parcel *input_parcel) {
+  int type = input_parcel->readInt32();
+  bool enable = (input_parcel->readInt32() > 0);
+  DLOGI("type = %d enable = %d", type, enable);
+  int verbose_level = input_parcel->readInt32();
+
+  switch (type) {
+    case qService::IQService::DEBUG_ALL:
+      HWCDebugHandler::DebugAll(enable, verbose_level);
+      break;
+
+    case qService::IQService::DEBUG_MDPCOMP:
+      HWCDebugHandler::DebugStrategy(enable, verbose_level);
+      HWCDebugHandler::DebugCompManager(enable, verbose_level);
+      break;
+
+    case qService::IQService::DEBUG_PIPE_LIFECYCLE:
+      HWCDebugHandler::DebugResources(enable, verbose_level);
+      break;
+
+    case qService::IQService::DEBUG_DRIVER_CONFIG:
+      HWCDebugHandler::DebugDriverConfig(enable, verbose_level);
+      break;
+
+    case qService::IQService::DEBUG_ROTATOR:
+      HWCDebugHandler::DebugResources(enable, verbose_level);
+      HWCDebugHandler::DebugDriverConfig(enable, verbose_level);
+      HWCDebugHandler::DebugRotator(enable, verbose_level);
+      break;
+
+    case qService::IQService::DEBUG_QDCM:
+      HWCDebugHandler::DebugQdcm(enable, verbose_level);
+      break;
+
+    default:
+      DLOGW("type = %d is not supported", type);
+  }
+}
+
+android::status_t HWCSession::QdcmCMDHandler(const android::Parcel *input_parcel,
+                                             android::Parcel *output_parcel) {
+  int ret = 0;
+  int32_t *brightness_value = NULL;
+  uint32_t display_id(0);
+  PPPendingParams pending_action;
+  PPDisplayAPIPayload resp_payload, req_payload;
+
+  if (!color_mgr_) {
+    return -1;
+  }
+
+  pending_action.action = kNoAction;
+  pending_action.params = NULL;
+
+  // Read display_id, payload_size and payload from in_parcel.
+  ret = HWCColorManager::CreatePayloadFromParcel(*input_parcel, &display_id, &req_payload);
+  if (!ret) {
+    if (HWC_DISPLAY_PRIMARY == display_id && hwc_display_[HWC_DISPLAY_PRIMARY])
+      ret = hwc_display_[HWC_DISPLAY_PRIMARY]->ColorSVCRequestRoute(req_payload, &resp_payload,
+                                                                    &pending_action);
+
+    if (HWC_DISPLAY_EXTERNAL == display_id && hwc_display_[HWC_DISPLAY_EXTERNAL])
+      ret = hwc_display_[HWC_DISPLAY_EXTERNAL]->ColorSVCRequestRoute(req_payload, &resp_payload,
+                                                                     &pending_action);
+  }
+
+  if (ret) {
+    output_parcel->writeInt32(ret);  // first field in out parcel indicates return code.
+    req_payload.DestroyPayload();
+    resp_payload.DestroyPayload();
+    return ret;
+  }
+
+  switch (pending_action.action) {
+    case kInvalidating:
+      callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+      break;
+    case kEnterQDCMMode:
+      ret = color_mgr_->EnableQDCMMode(true, hwc_display_[HWC_DISPLAY_PRIMARY]);
+      break;
+    case kExitQDCMMode:
+      ret = color_mgr_->EnableQDCMMode(false, hwc_display_[HWC_DISPLAY_PRIMARY]);
+      break;
+    case kApplySolidFill:
+      ret =
+          color_mgr_->SetSolidFill(pending_action.params, true, hwc_display_[HWC_DISPLAY_PRIMARY]);
+      callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+      break;
+    case kDisableSolidFill:
+      ret =
+          color_mgr_->SetSolidFill(pending_action.params, false, hwc_display_[HWC_DISPLAY_PRIMARY]);
+      callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+      break;
+    case kSetPanelBrightness:
+      brightness_value = reinterpret_cast<int32_t *>(resp_payload.payload);
+      if (brightness_value == NULL) {
+        DLOGE("Brightness value is Null");
+        return -EINVAL;
+      }
+      if (HWC_DISPLAY_PRIMARY == display_id)
+        ret = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPanelBrightness(*brightness_value);
+      break;
+    case kEnableFrameCapture:
+      ret = color_mgr_->SetFrameCapture(pending_action.params, true,
+                                        hwc_display_[HWC_DISPLAY_PRIMARY]);
+      callbacks_.Refresh(HWC_DISPLAY_PRIMARY);
+      break;
+    case kDisableFrameCapture:
+      ret = color_mgr_->SetFrameCapture(pending_action.params, false,
+                                        hwc_display_[HWC_DISPLAY_PRIMARY]);
+      break;
+    case kNoAction:
+      break;
+    default:
+      DLOGW("Invalid pending action = %d!", pending_action.action);
+      break;
+  }
+
+  // for display API getter case, marshall returned params into out_parcel.
+  output_parcel->writeInt32(ret);
+  HWCColorManager::MarshallStructIntoParcel(resp_payload, output_parcel);
+  req_payload.DestroyPayload();
+  resp_payload.DestroyPayload();
+
+  return (ret ? -EINVAL : 0);
+}
+
+android::status_t HWCSession::OnMinHdcpEncryptionLevelChange(const android::Parcel *input_parcel,
+                                                             android::Parcel *output_parcel) {
+  int ret = -EINVAL;
+  uint32_t display_id = UINT32(input_parcel->readInt32());
+  uint32_t min_enc_level = UINT32(input_parcel->readInt32());
+
+  DLOGI("Display %d", display_id);
+
+  if (display_id >= HWC_NUM_DISPLAY_TYPES) {
+    DLOGE("Invalid display_id");
+  } else if (display_id != HWC_DISPLAY_EXTERNAL) {
+    DLOGE("Not supported for display");
+  } else if (!hwc_display_[display_id]) {
+    DLOGW("Display is not connected");
+  } else {
+    ret = hwc_display_[display_id]->OnMinHdcpEncryptionLevelChange(min_enc_level);
+  }
+
+  output_parcel->writeInt32(ret);
+
+  return ret;
+}
+
+void *HWCSession::HWCUeventThread(void *context) {
+  if (context) {
+    return reinterpret_cast<HWCSession *>(context)->HWCUeventThreadHandler();
+  }
+
+  return NULL;
+}
+
+void *HWCSession::HWCUeventThreadHandler() {
+  static char uevent_data[PAGE_SIZE];
+  int length = 0;
+  prctl(PR_SET_NAME, uevent_thread_name_, 0, 0, 0);
+  setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
+  if (!uevent_init()) {
+    DLOGE("Failed to init uevent");
+    pthread_exit(0);
+    return NULL;
+  }
+
+  while (!uevent_thread_exit_) {
+    // keep last 2 zeroes to ensure double 0 termination
+    length = uevent_next_event(uevent_data, INT32(sizeof(uevent_data)) - 2);
+
+    if (strcasestr(HWC_UEVENT_SWITCH_HDMI, uevent_data)) {
+      DLOGI("Uevent HDMI = %s", uevent_data);
+      int connected = GetEventValue(uevent_data, length, "SWITCH_STATE=");
+      if (connected >= 0) {
+        DLOGI("HDMI = %s", connected ? "connected" : "disconnected");
+        if (HotPlugHandler(connected) == -1) {
+          DLOGE("Failed handling Hotplug = %s", connected ? "connected" : "disconnected");
+        }
+      }
+    } else if (strcasestr(HWC_UEVENT_GRAPHICS_FB0, uevent_data)) {
+      DLOGI("Uevent FB0 = %s", uevent_data);
+      int panel_reset = GetEventValue(uevent_data, length, "PANEL_ALIVE=");
+      if (panel_reset == 0) {
+        callbacks_.Refresh(0);
+        reset_panel_ = true;
+      }
+    }
+  }
+  pthread_exit(0);
+
+  return NULL;
+}
+
+int HWCSession::GetEventValue(const char *uevent_data, int length, const char *event_info) {
+  const char *iterator_str = uevent_data;
+  while (((iterator_str - uevent_data) <= length) && (*iterator_str)) {
+    char *pstr = strstr(iterator_str, event_info);
+    if (pstr != NULL) {
+      return (atoi(iterator_str + strlen(event_info)));
+    }
+    iterator_str += strlen(iterator_str) + 1;
+  }
+
+  return -1;
+}
+
+void HWCSession::ResetPanel() {
+  HWC2::Error status;
+
+  DLOGI("Powering off primary");
+  status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPowerMode(HWC2::PowerMode::Off);
+  if (status != HWC2::Error::None) {
+    DLOGE("power-off on primary failed with error = %d", status);
+  }
+
+  DLOGI("Restoring power mode on primary");
+  HWC2::PowerMode mode = hwc_display_[HWC_DISPLAY_PRIMARY]->GetLastPowerMode();
+  status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPowerMode(mode);
+  if (status != HWC2::Error::None) {
+    DLOGE("Setting power mode = %d on primary failed with error = %d", mode, status);
+  }
+
+  status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetVsyncEnabled(HWC2::Vsync::Enable);
+  if (status != HWC2::Error::None) {
+    DLOGE("enabling vsync failed for primary with error = %d", status);
+  }
+
+  reset_panel_ = false;
+}
+
+int HWCSession::HotPlugHandler(bool connected) {
+  int status = 0;
+  bool notify_hotplug = false;
+  bool hdmi_primary = false;
+
+  // To prevent sending events to client while a lock is held, acquire scope locks only within
+  // below scope so that those get automatically unlocked after the scope ends.
+  {
+    SEQUENCE_WAIT_SCOPE_LOCK(locker_);
+
+    if (!hwc_display_[HWC_DISPLAY_PRIMARY]) {
+      DLOGE("Primary display is not connected.");
+      return -1;
+    }
+
+    HWCDisplay *primary_display = hwc_display_[HWC_DISPLAY_PRIMARY];
+    HWCDisplay *external_display = NULL;
+
+    if (primary_display->GetDisplayClass() == DISPLAY_CLASS_EXTERNAL) {
+      external_display = static_cast<HWCDisplayExternal *>(hwc_display_[HWC_DISPLAY_PRIMARY]);
+      hdmi_primary = true;
+    }
+
+    // If primary display connected is a NULL display, then replace it with the external display
+    if (connected) {
+      // If we are in HDMI as primary and the primary display just got plugged in
+      if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+        DLOGE("HDMI is already connected");
+        return -1;
+      }
+
+      // Connect external display if virtual display is not connected.
+      // Else, defer external display connection and process it when virtual display
+      // tears down; Do not notify SurfaceFlinger since connection is deferred now.
+      if (!hwc_display_[HWC_DISPLAY_VIRTUAL]) {
+        status = ConnectDisplay(HWC_DISPLAY_EXTERNAL);
+        if (status) {
+          return status;
+        }
+        notify_hotplug = true;
+      } else {
+        DLOGI("Virtual display is connected, pending connection");
+        external_pending_connect_ = true;
+      }
+    } else {
+      // Do not return error if external display is not in connected status.
+      // Due to virtual display concurrency, external display connection might be still pending
+      // but hdmi got disconnected before pending connection could be processed.
+
+      if (hdmi_primary) {
+        assert(external_display != NULL);
+        uint32_t x_res, y_res;
+        external_display->GetFrameBufferResolution(&x_res, &y_res);
+        // Need to manually disable VSYNC as SF is not aware of connect/disconnect cases
+        // for HDMI as primary
+        external_display->SetVsyncEnabled(HWC2::Vsync::Disable);
+        HWCDisplayExternal::Destroy(external_display);
+
+        // In HWC2, primary displays can be hotplugged out
+        notify_hotplug = true;
+      } else {
+        if (hwc_display_[HWC_DISPLAY_EXTERNAL]) {
+          status = DisconnectDisplay(HWC_DISPLAY_EXTERNAL);
+          notify_hotplug = true;
+        }
+        external_pending_connect_ = false;
+      }
+    }
+  }
+
+  if (connected && notify_hotplug) {
+    // trigger screen refresh to ensure sufficient resources are available to process new
+    // new display connection.
+    callbacks_.Refresh(0);
+    uint32_t vsync_period = UINT32(GetVsyncPeriod(HWC_DISPLAY_PRIMARY));
+    usleep(vsync_period * 2 / 1000);
+  }
+  // notify client
+  // Handle HDMI as primary here
+  if (notify_hotplug) {
+    callbacks_.Hotplug(HWC_DISPLAY_EXTERNAL,
+                       connected ? HWC2::Connection::Connected : HWC2::Connection::Disconnected);
+  }
+
+  qservice_->onHdmiHotplug(INT(connected));
+
+  return 0;
+}
+
+int HWCSession::GetVsyncPeriod(int disp) {
+  SCOPE_LOCK(locker_);
+  // default value
+  int32_t vsync_period = 1000000000l / 60;
+  auto attribute = HWC2::Attribute::VsyncPeriod;
+
+  if (hwc_display_[disp]) {
+    hwc_display_[disp]->GetDisplayAttribute(0, attribute, &vsync_period);
+  }
+
+  return vsync_period;
+}
+
+android::status_t HWCSession::GetVisibleDisplayRect(const android::Parcel *input_parcel,
+                                                    android::Parcel *output_parcel) {
+  int dpy = input_parcel->readInt32();
+
+  if (dpy < HWC_DISPLAY_PRIMARY || dpy > HWC_DISPLAY_VIRTUAL) {
+    return android::BAD_VALUE;
+  }
+
+  if (!hwc_display_[dpy]) {
+    return android::NO_INIT;
+  }
+
+  hwc_rect_t visible_rect = {0, 0, 0, 0};
+  int error = hwc_display_[dpy]->GetVisibleDisplayRect(&visible_rect);
+  if (error < 0) {
+    return error;
+  }
+
+  output_parcel->writeInt32(visible_rect.left);
+  output_parcel->writeInt32(visible_rect.top);
+  output_parcel->writeInt32(visible_rect.right);
+  output_parcel->writeInt32(visible_rect.bottom);
+
+  return android::NO_ERROR;
+}
+
+}  // namespace sdm
diff --git a/msm8996/sdm/libs/hwc2/hwc_session.h b/msm8996/sdm/libs/hwc2/hwc_session.h
new file mode 100644
index 0000000..d52afda
--- /dev/null
+++ b/msm8996/sdm/libs/hwc2/hwc_session.h
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
+ * Not a Contribution.
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __HWC_SESSION_H__
+#define __HWC_SESSION_H__
+
+#include <core/core_interface.h>
+#include <utils/locker.h>
+
+#include "hwc_callbacks.h"
+#include "hwc_layers.h"
+#include "hwc_display.h"
+#include "hwc_display_primary.h"
+#include "hwc_display_external.h"
+#include "hwc_display_virtual.h"
+#include "hwc_color_manager.h"
+
+namespace sdm {
+
+class HWCSession : hwc2_device_t, public qClient::BnQClient {
+ public:
+  struct HWCModuleMethods : public hw_module_methods_t {
+    HWCModuleMethods() { hw_module_methods_t::open = HWCSession::Open; }
+  };
+
+  explicit HWCSession(const hw_module_t *module);
+  int Init();
+  int Deinit();
+  HWC2::Error CreateVirtualDisplayObject(uint32_t width, uint32_t height);
+
+  template <typename... Args>
+  static int32_t CallDisplayFunction(hwc2_device_t *device, hwc2_display_t display,
+                                     HWC2::Error (HWCDisplay::*member)(Args...), Args... args) {
+    if (!device) {
+      return HWC2_ERROR_BAD_DISPLAY;
+    }
+
+    HWCSession *hwc_session = static_cast<HWCSession *>(device);
+    auto status = HWC2::Error::BadDisplay;
+    if (hwc_session->hwc_display_[display]) {
+      auto hwc_display = hwc_session->hwc_display_[display];
+      status = (hwc_display->*member)(std::forward<Args>(args)...);
+    }
+    return INT32(status);
+  }
+
+  template <typename... Args>
+  static int32_t CallLayerFunction(hwc2_device_t *device, hwc2_display_t display,
+                                   hwc2_layer_t layer, HWC2::Error (HWCLayer::*member)(Args...),
+                                   Args... args) {
+    if (!device) {
+      return HWC2_ERROR_BAD_DISPLAY;
+    }
+
+    HWCSession *hwc_session = static_cast<HWCSession *>(device);
+    auto status = HWC2::Error::BadDisplay;
+    if (hwc_session->hwc_display_[display]) {
+      status = HWC2::Error::BadLayer;
+      auto hwc_layer = hwc_session->hwc_display_[display]->GetHWCLayer(layer);
+      if (hwc_layer != nullptr) {
+        status = (hwc_layer->*member)(std::forward<Args>(args)...);
+      }
+    }
+    return INT32(status);
+  }
+
+  // HWC2 Functions that require a concrete implementation in hwc session
+  // and hence need to be member functions
+  static int32_t CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height,
+                                      hwc2_display_t *out_display_id);
+  static int32_t DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display);
+  static int32_t PresentDisplay(hwc2_device_t *device, hwc2_display_t display,
+                                int32_t *out_retire_fence);
+  static int32_t RegisterCallback(hwc2_device_t *device, int32_t descriptor,
+                                  hwc2_callback_data_t callback_data,
+                                  hwc2_function_pointer_t pointer);
+  static int32_t SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display,
+                                 buffer_handle_t buffer, int32_t releaseFence);
+  static int32_t ValidateDisplay(hwc2_device_t *device, hwc2_display_t display,
+                                 uint32_t *out_num_types, uint32_t *out_num_requests);
+
+ private:
+  static const int kExternalConnectionTimeoutMs = 500;
+  static const int kPartialUpdateControlTimeoutMs = 100;
+
+  // hwc methods
+  static int Open(const hw_module_t *module, const char *name, hw_device_t **device);
+  static int Close(hw_device_t *device);
+  static void GetCapabilities(struct hwc2_device *device, uint32_t *outCount,
+                              int32_t *outCapabilities);
+  static hwc2_function_pointer_t GetFunction(struct hwc2_device *device, int32_t descriptor);
+
+  // Uevent thread
+  static void *HWCUeventThread(void *context);
+  void *HWCUeventThreadHandler();
+  int GetEventValue(const char *uevent_data, int length, const char *event_info);
+  int HotPlugHandler(bool connected);
+  void ResetPanel();
+  int32_t ConnectDisplay(int disp);
+  int DisconnectDisplay(int disp);
+  int GetVsyncPeriod(int disp);
+
+  // QClient methods
+  virtual android::status_t notifyCallback(uint32_t command, const android::Parcel *input_parcel,
+                                           android::Parcel *output_parcel);
+  void DynamicDebug(const android::Parcel *input_parcel);
+  void SetFrameDumpConfig(const android::Parcel *input_parcel);
+  android::status_t SetMaxMixerStages(const android::Parcel *input_parcel);
+  android::status_t SetDisplayMode(const android::Parcel *input_parcel);
+  android::status_t SetSecondaryDisplayStatus(const android::Parcel *input_parcel,
+                                              android::Parcel *output_parcel);
+  android::status_t ToggleScreenUpdates(const android::Parcel *input_parcel,
+                                        android::Parcel *output_parcel);
+  android::status_t ConfigureRefreshRate(const android::Parcel *input_parcel);
+  android::status_t QdcmCMDHandler(const android::Parcel *input_parcel,
+                                   android::Parcel *output_parcel);
+  android::status_t ControlPartialUpdate(const android::Parcel *input_parcel, android::Parcel *out);
+  android::status_t OnMinHdcpEncryptionLevelChange(const android::Parcel *input_parcel,
+                                                   android::Parcel *output_parcel);
+  android::status_t SetPanelBrightness(const android::Parcel *input_parcel,
+                                       android::Parcel *output_parcel);
+  android::status_t GetPanelBrightness(const android::Parcel *input_parcel,
+                                       android::Parcel *output_parcel);
+  // These functions return the actual display config info as opposed to FB
+  android::status_t HandleSetActiveDisplayConfig(const android::Parcel *input_parcel,
+                                                 android::Parcel *output_parcel);
+  android::status_t HandleGetActiveDisplayConfig(const android::Parcel *input_parcel,
+                                                 android::Parcel *output_parcel);
+  android::status_t HandleGetDisplayConfigCount(const android::Parcel *input_parcel,
+                                                android::Parcel *output_parcel);
+  android::status_t HandleGetDisplayAttributesForConfig(const android::Parcel *input_parcel,
+                                                        android::Parcel *output_parcel);
+  android::status_t GetVisibleDisplayRect(const android::Parcel *input_parcel,
+                                          android::Parcel *output_parcel);
+
+  android::status_t SetDynamicBWForCamera(const android::Parcel *input_parcel,
+                                          android::Parcel *output_parcel);
+  android::status_t GetBWTransactionStatus(const android::Parcel *input_parcel,
+                                           android::Parcel *output_parcel);
+  static Locker locker_;
+  CoreInterface *core_intf_ = NULL;
+  HWCDisplay *hwc_display_[HWC_NUM_DISPLAY_TYPES] = {NULL};
+  HWCCallbacks callbacks_;
+  pthread_t uevent_thread_;
+  bool uevent_thread_exit_ = false;
+  const char *uevent_thread_name_ = "HWC_UeventThread";
+  HWCBufferAllocator *buffer_allocator_ = NULL;
+  HWCBufferSyncHandler *buffer_sync_handler_ = NULL;
+  HWCColorManager *color_mgr_ = NULL;
+  bool reset_panel_ = false;
+  bool secure_display_active_ = false;
+  bool external_pending_connect_ = false;
+  bool new_bw_mode_ = false;
+  bool need_invalidate_ = false;
+  int bw_mode_release_fd_ = -1;
+  qService::QService *qservice_ = NULL;
+};
+
+}  // namespace sdm
+
+#endif  // __HWC_SESSION_H__
diff --git a/msm8996/sdm/libs/utils/formats.cpp b/msm8996/sdm/libs/utils/formats.cpp
index 512f9ee..546c0c7 100644
--- a/msm8996/sdm/libs/utils/formats.cpp
+++ b/msm8996/sdm/libs/utils/formats.cpp
@@ -68,5 +68,51 @@
   }
 }
 
+const char *GetFormatString(const LayerBufferFormat &format) {
+  switch (format) {
+  case kFormatARGB8888:                 return "ARGB_8888";
+  case kFormatRGBA8888:                 return "RGBA_8888";
+  case kFormatBGRA8888:                 return "BGRA_8888";
+  case kFormatXRGB8888:                 return "XRGB_8888";
+  case kFormatRGBX8888:                 return "RGBX_8888";
+  case kFormatBGRX8888:                 return "BGRX_8888";
+  case kFormatRGBA5551:                 return "RGBA_5551";
+  case kFormatRGBA4444:                 return "RGBA_4444";
+  case kFormatRGB888:                   return "RGB_888";
+  case kFormatBGR888:                   return "BGR_888";
+  case kFormatRGB565:                   return "RGB_565";
+  case kFormatBGR565:                   return "BGR_565";
+  case kFormatRGBA8888Ubwc:             return "RGBA_8888_UBWC";
+  case kFormatRGBX8888Ubwc:             return "RGBX_8888_UBWC";
+  case kFormatBGR565Ubwc:               return "BGR_565_UBWC";
+  case kFormatYCbCr420Planar:           return "Y_CB_CR_420";
+  case kFormatYCrCb420Planar:           return "Y_CR_CB_420";
+  case kFormatYCrCb420PlanarStride16:   return "Y_CR_CB_420_STRIDE16";
+  case kFormatYCbCr420SemiPlanar:       return "Y_CBCR_420";
+  case kFormatYCrCb420SemiPlanar:       return "Y_CRCB_420";
+  case kFormatYCbCr420SemiPlanarVenus:  return "Y_CBCR_420_VENUS";
+  case kFormatYCrCb420SemiPlanarVenus:  return "Y_CRCB_420_VENUS";
+  case kFormatYCbCr422H1V2SemiPlanar:   return "Y_CBCR_422_H1V2";
+  case kFormatYCrCb422H1V2SemiPlanar:   return "Y_CRCB_422_H1V2";
+  case kFormatYCbCr422H2V1SemiPlanar:   return "Y_CBCR_422_H2V1";
+  case kFormatYCrCb422H2V1SemiPlanar:   return "Y_CRCB_422_H2V2";
+  case kFormatYCbCr420SPVenusUbwc:      return "Y_CBCR_420_VENUS_UBWC";
+  case kFormatYCbCr422H2V1Packed:       return "YCBYCR_422_H2V1";
+  case kFormatRGBA1010102:              return "RGBA_1010102";
+  case kFormatARGB2101010:              return "ARGB_2101010";
+  case kFormatRGBX1010102:              return "RGBX_1010102";
+  case kFormatXRGB2101010:              return "XRGB_2101010";
+  case kFormatBGRA1010102:              return "BGRA_1010102";
+  case kFormatABGR2101010:              return "ABGR_2101010";
+  case kFormatBGRX1010102:              return "BGRX_1010102";
+  case kFormatXBGR2101010:              return "XBGR_2101010";
+  case kFormatRGBA1010102Ubwc:          return "RGBA_1010102_UBWC";
+  case kFormatRGBX1010102Ubwc:          return "RGBX_1010102_UBWC";
+  case kFormatYCbCr420P010:             return "Y_CBCR_420_P010";
+  case kFormatYCbCr420TP10Ubwc:         return "Y_CBCR_420_TP10_UBWC";
+  default:                              return "UNKNOWN";
+  }
+}
+
 }  // namespace sdm
 
diff --git a/msm8996/sdm/libs/utils/sys.cpp b/msm8996/sdm/libs/utils/sys.cpp
index ff66fcf..a622b9e 100644
--- a/msm8996/sdm/libs/utils/sys.cpp
+++ b/msm8996/sdm/libs/utils/sys.cpp
@@ -54,6 +54,9 @@
 Sys::getline Sys::getline_ = ::getline;
 Sys::pthread_cancel Sys::pthread_cancel_ = PthreadCancel;
 Sys::dup Sys::dup_ = ::dup;
+Sys::read Sys::read_ = ::read;
+Sys::write Sys::write_ = ::write;
+Sys::eventfd Sys::eventfd_ = ::eventfd;
 
 #else
 
@@ -68,6 +71,9 @@
 extern int virtual_fclose(FILE* fileptr);
 extern ssize_t virtual_getline(char **lineptr, size_t *linelen, FILE *stream);
 extern int virtual_dup(int fd);
+extern ssize_t virtual_read(int fd, void *data, size_t count);
+extern ssize_t virtual_write(int fd, const void *data, size_t count);
+extern int virtual_eventfd(unsigned int initval, int flags);
 
 Sys::ioctl Sys::ioctl_ = virtual_ioctl;
 Sys::open Sys::open_ = virtual_open;
@@ -80,6 +86,9 @@
 Sys::getline Sys::getline_ = virtual_getline;
 Sys::pthread_cancel Sys::pthread_cancel_ = ::pthread_cancel;
 Sys::dup Sys::dup_ = virtual_dup;
+Sys::read Sys::read_ = virtual_read;
+Sys::write Sys::write_ = virtual_write;
+Sys::eventfd Sys::eventfd_ = virtual_eventfd;
 
 #endif  // SDM_VIRTUAL_DRIVER