Merge remote-tracking branch 'quic/display.lnx.3.0-dev' into LA.HB.1.3.9

Change-Id: Ic494c2f6350da6fb5d03fbfe1f9fcfebd8b270f0
diff --git a/common.mk b/common.mk
index 0142a68..76c392a 100644
--- a/common.mk
+++ b/common.mk
@@ -1,8 +1,10 @@
 #Common headers
-common_includes := $(LOCAL_PATH)/../libgralloc
-common_includes += $(LOCAL_PATH)/../libcopybit
-common_includes += $(LOCAL_PATH)/../libqdutils
-common_includes += $(LOCAL_PATH)/../libqservice
+display_top := $(call my-dir)
+
+common_includes := $(display_top)/libqdutils
+common_includes += $(display_top)/libqservice
+common_includes += $(display_top)/libcopybit
+common_includes += $(display_top)/sdm/include
 
 common_header_export_path := qcom/display
 
@@ -12,6 +14,7 @@
 #Common C flags
 common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
 common_flags += -Wconversion -Wall -Werror
+common_flags += -isystem $(display_top)/libgralloc
 
 ifeq ($(TARGET_USES_POST_PROCESSING),true)
     common_flags     += -DUSES_POST_PROCESSING
@@ -36,12 +39,12 @@
 # Executed only on QCOM BSPs
 ifeq ($(TARGET_USES_QCOM_BSP),true)
 # Enable QCOM Display features
-    common_flags += -DQCOM_BSP
+    common_flags += -DQTI_BSP
 endif
 ifneq ($(call is-platform-sdk-version-at-least,18),true)
     common_flags += -DANDROID_JELLYBEAN_MR1=1
 endif
-ifeq ($(call is-vendor-board-platform,QCOM),true)
+ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
 # This check is to pick the kernel headers from the right location.
 # If the macro above is defined, we make the assumption that we have the kernel
 # available in the build tree.
diff --git a/hdmi_cec/qhdmi_cec.cpp b/hdmi_cec/qhdmi_cec.cpp
index 1bc7df6..f84cf80 100644
--- a/hdmi_cec/qhdmi_cec.cpp
+++ b/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/libqdutils/display_config.h b/libqdutils/display_config.h
index 9ba60f2..05a7f29 100644
--- a/libqdutils/display_config.h
+++ b/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/libqservice/QService.cpp b/libqservice/QService.cpp
index ddb4b18..546ad7e 100644
--- a/libqservice/QService.cpp
+++ b/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/libqservice/QService.h b/libqservice/QService.h
index 719c9b7..bd130fa 100644
--- a/libqservice/QService.h
+++ b/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/sdm/include/core/core_interface.h b/sdm/include/core/core_interface.h
index 582222f..8f97cb4 100644
--- a/sdm/include/core/core_interface.h
+++ b/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/sdm/include/core/display_interface.h b/sdm/include/core/display_interface.h
index 0416daa..a74b34b 100644
--- a/sdm/include/core/display_interface.h
+++ b/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/sdm/include/core/layer_buffer.h b/sdm/include/core/layer_buffer.h
index 47ebf92..7eba4e3 100644
--- a/sdm/include/core/layer_buffer.h
+++ b/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/sdm/include/core/layer_stack.h b/sdm/include/core/layer_stack.h
index 746d831..3d86fbe 100644
--- a/sdm/include/core/layer_stack.h
+++ b/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/sdm/include/private/hw_info_types.h b/sdm/include/private/hw_info_types.h
index 148de7c..38ccf21 100644
--- a/sdm/include/private/hw_info_types.h
+++ b/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/sdm/include/private/resource_interface.h b/sdm/include/private/resource_interface.h
index c350460..21652f5 100644
--- a/sdm/include/private/resource_interface.h
+++ b/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/sdm/include/utils/formats.h b/sdm/include/utils/formats.h
index 57825dd..67548f3 100644
--- a/sdm/include/utils/formats.h
+++ b/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/sdm/include/utils/sys.h b/sdm/include/utils/sys.h
index dc45696..ed9b75c 100644
--- a/sdm/include/utils/sys.h
+++ b/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/sdm/libs/core/Android.mk b/sdm/libs/core/Android.mk
index c468420..5e88683 100644
--- a/sdm/libs/core/Android.mk
+++ b/sdm/libs/core/Android.mk
@@ -1,18 +1,17 @@
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
+include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmcore
 LOCAL_MODULE_TAGS             := optional
-LOCAL_C_INCLUDES              := hardware/qcom/display/sdm/include/ \
-                                 $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
-                                 external/libcxx/include/
+LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
 LOCAL_CFLAGS                  := -Wno-missing-field-initializers -Wno-unused-parameter \
-                                 -Wall -Werror -Wconversion -std=c++11 -fcolor-diagnostics\
-                                 -DLOG_TAG=\"SDM\"
+                                 -std=c++11 -fcolor-diagnostics\
+                                 -DLOG_TAG=\"SDM\" $(common_flags)
 LOCAL_CLANG                   := true
 LOCAL_HW_INTF_PATH            := fb
 LOCAL_SHARED_LIBRARIES        := libdl libsdmutils libc++
-LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps) $(kernel_deps)
 LOCAL_SRC_FILES               := core_interface.cpp \
                                  core_impl.cpp \
                                  display_base.cpp \
@@ -30,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/sdm/libs/core/comp_manager.cpp b/sdm/libs/core/comp_manager.cpp
index a7f137a..4bd73d4 100644
--- a/sdm/libs/core/comp_manager.cpp
+++ b/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/sdm/libs/core/core_impl.cpp b/sdm/libs/core/core_impl.cpp
index 424be43..82f578e 100644
--- a/sdm/libs/core/core_impl.cpp
+++ b/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/sdm/libs/core/core_impl.h b/sdm/libs/core/core_impl.h
index 06ca3d9..97e8655 100644
--- a/sdm/libs/core/core_impl.h
+++ b/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/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index 5a155b7..33ee08d 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/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/sdm/libs/core/display_base.h b/sdm/libs/core/display_base.h
index 436af4f..7242156 100644
--- a/sdm/libs/core/display_base.h
+++ b/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/sdm/libs/core/display_hdmi.cpp b/sdm/libs/core/display_hdmi.cpp
index 773016a..6522377 100644
--- a/sdm/libs/core/display_hdmi.cpp
+++ b/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/sdm/libs/core/display_hdmi.h b/sdm/libs/core/display_hdmi.h
index ca33035..88e553d 100644
--- a/sdm/libs/core/display_hdmi.h
+++ b/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/sdm/libs/core/display_primary.cpp b/sdm/libs/core/display_primary.cpp
index e824dbe..e5751df 100644
--- a/sdm/libs/core/display_primary.cpp
+++ b/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/sdm/libs/core/display_primary.h b/sdm/libs/core/display_primary.h
index b0f2934..d3a37cd 100644
--- a/sdm/libs/core/display_primary.h
+++ b/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/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index fe157a2..546490a 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/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/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index 82a9ee6..4a997a9 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -78,7 +78,7 @@
   virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info);
 
   // For HWDevice derivatives
-  virtual DisplayError Init(HWEventHandler *eventhandler);
+  virtual DisplayError Init();
   virtual DisplayError Deinit();
 
   enum {
@@ -88,7 +88,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 +95,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 +110,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/sdm/libs/core/fb/hw_events.cpp b/sdm/libs/core/fb/hw_events.cpp
new file mode 100755
index 0000000..5a656cc
--- /dev/null
+++ b/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/sdm/libs/core/fb/hw_events.h b/sdm/libs/core/fb/hw_events.h
new file mode 100755
index 0000000..5d7d23d
--- /dev/null
+++ b/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/sdm/libs/core/fb/hw_hdmi.cpp b/sdm/libs/core/fb/hw_hdmi.cpp
index 9f878f3..73a6a3c 100644
--- a/sdm/libs/core/fb/hw_hdmi.cpp
+++ b/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/sdm/libs/core/fb/hw_hdmi.h b/sdm/libs/core/fb/hw_hdmi.h
index 09eab23..50bbbaa 100644
--- a/sdm/libs/core/fb/hw_hdmi.h
+++ b/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/sdm/libs/core/fb/hw_info.cpp b/sdm/libs/core/fb/hw_info.cpp
index 7b707c1..f6f664a 100644
--- a/sdm/libs/core/fb/hw_info.cpp
+++ b/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/sdm/libs/core/fb/hw_info.h b/sdm/libs/core/fb/hw_info.h
index ba98f87..f22acb0 100644
--- a/sdm/libs/core/fb/hw_info.h
+++ b/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/sdm/libs/core/fb/hw_primary.cpp b/sdm/libs/core/fb/hw_primary.cpp
index e8ab7cf..84eea2a 100644
--- a/sdm/libs/core/fb/hw_primary.cpp
+++ b/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/sdm/libs/core/fb/hw_primary.h b/sdm/libs/core/fb/hw_primary.h
index 9e2cea3..999c41a 100644
--- a/sdm/libs/core/fb/hw_primary.h
+++ b/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/sdm/libs/core/fb/hw_virtual.cpp b/sdm/libs/core/fb/hw_virtual.cpp
index 033159f..662c458 100644
--- a/sdm/libs/core/fb/hw_virtual.cpp
+++ b/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/sdm/libs/core/fb/hw_virtual.h b/sdm/libs/core/fb/hw_virtual.h
index 2b740d5..330a067 100644
--- a/sdm/libs/core/fb/hw_virtual.h
+++ b/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/sdm/libs/core/hw_events_interface.h b/sdm/libs/core/hw_events_interface.h
new file mode 100644
index 0000000..a133a3a
--- /dev/null
+++ b/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/sdm/libs/core/hw_info_interface.h b/sdm/libs/core/hw_info_interface.h
index 014ab0d..401c8bf 100644
--- a/sdm/libs/core/hw_info_interface.h
+++ b/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/sdm/libs/core/hw_interface.h b/sdm/libs/core/hw_interface.h
index 7779350..75d92b9 100644
--- a/sdm/libs/core/hw_interface.h
+++ b/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/sdm/libs/core/resource_default.cpp b/sdm/libs/core/resource_default.cpp
index 9a6eb52..e13150b 100644
--- a/sdm/libs/core/resource_default.cpp
+++ b/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/sdm/libs/core/resource_default.h b/sdm/libs/core/resource_default.h
index 23d7dfa..ce6dd5f 100644
--- a/sdm/libs/core/resource_default.h
+++ b/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/sdm/libs/core/strategy.cpp b/sdm/libs/core/strategy.cpp
index 924047a..0531693 100644
--- a/sdm/libs/core/strategy.cpp
+++ b/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/sdm/libs/hwc/Android.mk b/sdm/libs/hwc/Android.mk
index 13502f2..795ba39 100644
--- a/sdm/libs/hwc/Android.mk
+++ b/sdm/libs/hwc/Android.mk
@@ -1,38 +1,24 @@
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
+include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := hwcomposer.$(TARGET_BOARD_PLATFORM)
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
-LOCAL_C_INCLUDES              := hardware/qcom/display/sdm/include/ \
-                                 hardware/qcom/display/libqservice/ \
-                                 hardware/qcom/display/libqdutils/ \
-                                 hardware/qcom/display/libcopybit/ \
-                                 external/libcxx/include/
+LOCAL_C_INCLUDES              := $(common_includes)
 
 LOCAL_CFLAGS                  := -Wno-missing-field-initializers -Wno-unused-parameter \
-                                 -Wall -Werror -Wconversion -std=c++11 -fcolor-diagnostics\
-                                 -DLOG_TAG=\"SDM\" -DDEBUG_CALC_FPS
-LOCAL_CFLAGS                  += -isystem hardware/qcom/display/libgralloc
+                                 -std=c++11 -fcolor-diagnostics\
+                                 -DLOG_TAG=\"SDM\" $(common_flags)
 LOCAL_CLANG                   := true
 
-# TODO: Move this to the common makefile
-ifeq ($(call is-board-platform-in-list, $(MASTER_SIDE_CP_TARGET_LIST)), true)
-    LOCAL_CFLAGS += -DMASTER_SIDE_CP
-endif
-
-
-ifeq ($(TARGET_USES_QCOM_BSP),true)
-# Enable QCOM Display features
-#LOCAL_CFLAGS += -DQTI_BSP
-endif
-
 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_null.cpp \
                                  hwc_display_primary.cpp \
                                  hwc_display_external.cpp \
                                  hwc_display_virtual.cpp \
diff --git a/sdm/libs/hwc/blit_engine_c2d.cpp b/sdm/libs/hwc/blit_engine_c2d.cpp
index e855524..b55b06b 100644
--- a/sdm/libs/hwc/blit_engine_c2d.cpp
+++ b/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/sdm/libs/hwc/blit_engine_c2d.h b/sdm/libs/hwc/blit_engine_c2d.h
index 69b7299..6536d44 100644
--- a/sdm/libs/hwc/blit_engine_c2d.h
+++ b/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/sdm/libs/hwc/hwc_display.cpp b/sdm/libs/hwc/hwc_display.cpp
index 71df14b..ab9b4a7 100644
--- a/sdm/libs/hwc/hwc_display.cpp
+++ b/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/sdm/libs/hwc/hwc_display.h b/sdm/libs/hwc/hwc_display.h
index 21c87ab..7b14f10 100644
--- a/sdm/libs/hwc/hwc_display.h
+++ b/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/sdm/libs/hwc/hwc_display_external.cpp b/sdm/libs/hwc/hwc_display_external.cpp
index 86b36d8..813b123 100644
--- a/sdm/libs/hwc/hwc_display_external.cpp
+++ b/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/sdm/libs/hwc/hwc_display_external.h b/sdm/libs/hwc/hwc_display_external.h
index ba00d4d..436190d 100644
--- a/sdm/libs/hwc/hwc_display_external.h
+++ b/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/sdm/libs/hwc/hwc_display_null.cpp b/sdm/libs/hwc/hwc_display_null.cpp
new file mode 100644
index 0000000..11cf47a
--- /dev/null
+++ b/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/sdm/libs/hwc/hwc_display_null.h b/sdm/libs/hwc/hwc_display_null.h
new file mode 100644
index 0000000..2f6438a
--- /dev/null
+++ b/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/sdm/libs/hwc/hwc_display_primary.cpp b/sdm/libs/hwc/hwc_display_primary.cpp
index 0403361..8915bba 100644
--- a/sdm/libs/hwc/hwc_display_primary.cpp
+++ b/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() {
@@ -155,6 +164,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);
 
@@ -208,6 +226,8 @@
     return status;
   }
 
+  HandleFrameOutput();
+
   status = HWCDisplay::PostCommitLayerStack(content_list);
   if (status) {
     return status;
@@ -338,5 +358,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/sdm/libs/hwc/hwc_display_primary.h b/sdm/libs/hwc/hwc_display_primary.h
index 3773717..8c02731 100644
--- a/sdm/libs/hwc/hwc_display_primary.h
+++ b/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/sdm/libs/hwc/hwc_display_virtual.cpp b/sdm/libs/hwc/hwc_display_virtual.cpp
index b405d91..ef7198c 100644
--- a/sdm/libs/hwc/hwc_display_virtual.cpp
+++ b/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/sdm/libs/hwc/hwc_display_virtual.h b/sdm/libs/hwc/hwc_display_virtual.h
index afa1fcd..c383809 100644
--- a/sdm/libs/hwc/hwc_display_virtual.h
+++ b/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/sdm/libs/hwc/hwc_session.cpp b/sdm/libs/hwc/hwc_session.cpp
index 129b605..7928765 100644
--- a/sdm/libs/hwc/hwc_session.cpp
+++ b/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/sdm/libs/hwc/hwc_session.h b/sdm/libs/hwc/hwc_session.h
index 561de35..270751a 100644
--- a/sdm/libs/hwc/hwc_session.h
+++ b/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/sdm/libs/utils/Android.mk b/sdm/libs/utils/Android.mk
index 8125b49..f482a6d 100644
--- a/sdm/libs/utils/Android.mk
+++ b/sdm/libs/utils/Android.mk
@@ -1,13 +1,13 @@
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
+include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmutils
 LOCAL_MODULE_TAGS             := optional
-LOCAL_C_INCLUDES              := hardware/qcom/display/sdm/include/
+LOCAL_C_INCLUDES              := $(common_includes)
 LOCAL_CFLAGS                  := -Wno-missing-field-initializers \
-                                 -Wall -Werror -Wconversion \
                                  -std=c++11 -fcolor-diagnostics\
-                                 -DLOG_TAG=\"SDM\"
+                                 -DLOG_TAG=\"SDM\" $(common_flags)
 LOCAL_CLANG                   := true
 LOCAL_SRC_FILES               := debug.cpp \
                                  rect.cpp \
diff --git a/sdm/libs/utils/formats.cpp b/sdm/libs/utils/formats.cpp
index 512f9ee..546c0c7 100644
--- a/sdm/libs/utils/formats.cpp
+++ b/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/sdm/libs/utils/sys.cpp b/sdm/libs/utils/sys.cpp
index ff66fcf..a622b9e 100644
--- a/sdm/libs/utils/sys.cpp
+++ b/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