Merge "sdm: drm: add interlaced content support"
diff --git a/common.mk b/common.mk
index 3a55cca..941b0a4 100644
--- a/common.mk
+++ b/common.mk
@@ -6,59 +6,17 @@
 common_flags += -Wconversion -Wall -Werror -std=c++11
 ifneq ($(TARGET_IS_HEADLESS), true)
     common_flags += -DCOMPILE_DRM
+else
+    common_flags += -DTARGET_HEADLESS
+    LOCAL_CLANG := false
 endif
 
 ifeq ($(TARGET_USES_COLOR_METADATA), true)
-common_flags += -DUSE_COLOR_METADATA
+    common_flags += -DUSE_COLOR_METADATA
 endif
 
-CHECK_VERSION_LE = $(shell if [ $(1) -le $(2) ] ; then echo true ; else echo false ; fi)
-PLATFORM_SDK_NOUGAT = 25
-ifeq "REL" "$(PLATFORM_VERSION_CODENAME)"
-ifeq ($(call CHECK_VERSION_LE, $(PLATFORM_SDK_VERSION), $(PLATFORM_SDK_NOUGAT)), true)
-version_flag := -D__NOUGAT__
-endif
-endif
-
-use_hwc2 := false
-ifeq ($(TARGET_USES_HWC2), true)
-    use_hwc2 := true
-    common_flags += -DVIDEO_MODE_DEFER_RETIRE_FENCE
-endif
-
-common_includes := $(display_top)/libqdutils
-common_includes += $(display_top)/libqservice
-common_includes += $(display_top)/gpu_tonemapper
-ifneq ($(TARGET_IS_HEADLESS), true)
-    common_includes += $(display_top)/libcopybit
-    common_includes += $(display_top)/libdrmutils
-endif
-
-common_includes += $(display_top)/include
-common_includes += $(display_top)/sdm/include
-common_includes += system/core/base/include
-
-common_header_export_path := qcom/display
-
-#Common libraries external to display HAL
-common_libs := liblog libutils libcutils libhardware
-
-ifeq ($(TARGET_IS_HEADLESS), true)
-    LOCAL_CLANG := false
-else
-    LOCAL_CLANG := true
-endif
-
-ifneq ($(TARGET_USES_GRALLOC1), true)
-    common_flags += -isystem $(display_top)/libgralloc
-else
-    common_flags += -isystem $(display_top)/libgralloc1
-    common_flags += -DUSE_GRALLOC1
-endif
-
-ifeq ($(TARGET_USES_POST_PROCESSING),true)
-    common_flags     += -DUSES_POST_PROCESSING
-    common_includes  += $(TARGET_OUT_HEADERS)/pp/inc
+ifeq ($(TARGET_USES_QCOM_BSP),true)
+    common_flags += -DQTI_BSP
 endif
 
 ifeq ($(ARCH_ARM_HAVE_NEON),true)
@@ -69,19 +27,45 @@
     common_flags += -DMASTER_SIDE_CP
 endif
 
+use_hwc2 := false
+ifeq ($(TARGET_USES_HWC2), true)
+    use_hwc2 := true
+    common_flags += -DVIDEO_MODE_DEFER_RETIRE_FENCE
+endif
+
+ifeq ($(TARGET_USES_GRALLOC1), true)
+    common_flags += -DUSE_GRALLOC1
+endif
+
+common_includes := system/core/base/include
+CHECK_VERSION_LE = $(shell if [ $(1) -le $(2) ] ; then echo true ; else echo false ; fi)
+PLATFORM_SDK_NOUGAT = 25
+ifeq "REL" "$(PLATFORM_VERSION_CODENAME)"
+ifeq ($(call CHECK_VERSION_LE, $(PLATFORM_SDK_VERSION), $(PLATFORM_SDK_NOUGAT)), true)
+version_flag := -D__NOUGAT__
+
+# These include paths are deprecated post N
+common_includes += $(display_top)/libqdutils
+common_includes += $(display_top)/libqservice
+common_includes += $(display_top)/gpu_tonemapper
+ifneq ($(TARGET_IS_HEADLESS), true)
+    common_includes += $(display_top)/libcopybit
+    common_includes += $(display_top)/libdrmutils
+endif
+
+common_includes += $(display_top)/include
+common_includes += $(display_top)/sdm/include
+common_flags += -isystem $(TARGET_OUT_HEADERS)/qcom/display
+endif
+endif
+
+common_header_export_path := qcom/display
+
+#Common libraries external to display HAL
+common_libs := liblog libutils libcutils libhardware
 common_deps  :=
 kernel_includes :=
 
-# Executed only on QCOM BSPs
-ifeq ($(TARGET_USES_QCOM_BSP),true)
-# Enable QCOM Display features
-   common_flags += -DQTI_BSP
-endif
-
-ifeq ($(TARGET_IS_HEADLESS),true)
-    common_flags += -DTARGET_HEADLESS
-endif
-
 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
diff --git a/gpu_tonemapper/Android.mk b/gpu_tonemapper/Android.mk
index 5d1f9a9..2d10319 100644
--- a/gpu_tonemapper/Android.mk
+++ b/gpu_tonemapper/Android.mk
@@ -1,16 +1,23 @@
 LOCAL_PATH := $(call my-dir)
 include $(LOCAL_PATH)/../common.mk
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_COPY_HEADERS_TO     := $(common_header_export_path)
 LOCAL_COPY_HEADERS        := TonemapFactory.h Tonemapper.h
-LOCAL_SHARED_LIBRARIES    := libEGL libGLESv2 libui libutils liblog
 include $(BUILD_COPY_HEADERS)
 
+include $(CLEAR_VARS)
 LOCAL_MODULE              := libgpu_tonemapper
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32      := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64      := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS         := optional
 LOCAL_C_INCLUDES          := $(TARGET_OUT_HEADERS)/qcom/display/
 LOCAL_C_INCLUDES          += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_SHARED_LIBRARIES    := libEGL libGLESv2 libui libutils liblog
 
 LOCAL_CFLAGS              := $(version_flag) -Wno-missing-field-initializers -Wall \
                              -Wno-unused-parameter -std=c++11 -DLOG_TAG=\"GPU_TONEMAPPER\"
diff --git a/hdmi_cec/Android.mk b/hdmi_cec/Android.mk
index 3b794e3..6cfb856 100644
--- a/hdmi_cec/Android.mk
+++ b/hdmi_cec/Android.mk
@@ -3,9 +3,16 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := hdmi_cec.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
+LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libqservice libbinder libqdutils
 
 LOCAL_CFLAGS                  := $(common_flags) -DLOG_TAG=\"qdhdmi_cec\" -Wno-sign-conversion
diff --git a/include/Android.mk b/include/Android.mk
index 4d0d9f7..95c5d25 100644
--- a/include/Android.mk
+++ b/include/Android.mk
@@ -7,3 +7,19 @@
 
 include $(BUILD_COPY_HEADERS)
 
+include $(CLEAR_VARS)
+#TODO move all exported headers to this directory
+LOCAL_MODULE := display_headers
+LOCAL_EXPORT_C_INCLUDE_DIRS   := $(LOCAL_PATH) \
+                                 $(display_top)/libcopybit \
+                                 $(display_top)/libdrmutils \
+                                 $(display_top)/libqdutils \
+                                 $(display_top)/libqservice \
+                                 $(display_top)/sdm/include
+
+ifeq ($(TARGET_USES_GRALLOC1), true)
+    LOCAL_EXPORT_C_INCLUDE_DIRS += $(display_top)/libgralloc1
+else
+    LOCAL_EXPORT_C_INCLUDE_DIRS += $(display_top)/libgralloc
+endif
+include $(BUILD_HEADER_LIBRARY)
diff --git a/libcopybit/Android.mk b/libcopybit/Android.mk
index 3e6d8fe..0bce602 100644
--- a/libcopybit/Android.mk
+++ b/libcopybit/Android.mk
@@ -21,8 +21,15 @@
 #Copy the headers regardless of whether copybit is built
 include $(BUILD_COPY_HEADERS)
 
+include $(CLEAR_VARS)
 ifneq ($(TARGET_USES_GRALLOC1), true)
 LOCAL_MODULE                  := copybit.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
diff --git a/libdrmutils/Android.mk b/libdrmutils/Android.mk
index 15102e4..36beea2 100644
--- a/libdrmutils/Android.mk
+++ b/libdrmutils/Android.mk
@@ -2,6 +2,12 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libdrmutils
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := external/libdrm \
                                  $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
diff --git a/libgralloc/Android.mk b/libgralloc/Android.mk
index 6a0f868..8d5dfd3 100644
--- a/libgralloc/Android.mk
+++ b/libgralloc/Android.mk
@@ -18,6 +18,12 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := gralloc.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
@@ -37,6 +43,12 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libmemalloc
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libqdutils libdl
diff --git a/libgralloc1/Android.mk b/libgralloc1/Android.mk
index fb8c74a..0afe5f9 100644
--- a/libgralloc1/Android.mk
+++ b/libgralloc1/Android.mk
@@ -4,11 +4,18 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := gralloc.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) \
                                  external/libcxx/include/
 
+LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libqdMetaData libsync libqdutils
 ifneq ($(TARGET_IS_HEADLESS), true)
 LOCAL_SHARED_LIBRARIES        += libdrmutils
diff --git a/libgralloc1/gr_buf_descriptor.h b/libgralloc1/gr_buf_descriptor.h
index 95386fa..1f86867 100644
--- a/libgralloc1/gr_buf_descriptor.h
+++ b/libgralloc1/gr_buf_descriptor.h
@@ -65,6 +65,8 @@
 
   void SetColorFormat(int format) { format_ = format; }
 
+  void SetLayerCount(uint32_t layer_count) { layer_count_ = layer_count; }
+
   gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage_; }
 
   gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage_; }
@@ -75,12 +77,15 @@
 
   int GetFormat() const { return format_; }
 
+  uint32_t GetLayerCount() const { return layer_count_; }
+
   gralloc1_buffer_descriptor_t GetId() const { return id_; }
 
  private:
   int width_ = -1;
   int height_ = -1;
   int format_ = -1;
+  uint32_t layer_count_ = 1;
   gralloc1_producer_usage_t producer_usage_ = GRALLOC1_PRODUCER_USAGE_NONE;
   gralloc1_consumer_usage_t consumer_usage_ = GRALLOC1_CONSUMER_USAGE_NONE;
   const gralloc1_buffer_descriptor_t id_;
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 6432820..d3a307b 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -189,6 +189,7 @@
 
 gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
   auto hnd = buf->handle;
+  ALOGD_IF(DEBUG, "FreeBuffer handle:%p id: %" PRIu64, hnd, hnd->id);
   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
                              hnd->fd, buf->ion_handle_main) != 0) {
     return GRALLOC1_ERROR_BAD_HANDLE;
@@ -216,6 +217,7 @@
 }
 
 gralloc1_error_t BufferManager::ImportHandle(private_handle_t* hnd) {
+  ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
   int ion_handle = allocator_->ImportBuffer(hnd->fd);
   if (ion_handle < 0) {
     ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
@@ -246,9 +248,11 @@
 
 gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
   private_handle_t *hnd = const_cast<private_handle_t *>(handle);
+  ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
 
   hnd->base = 0;
   hnd->base_metadata = 0;
+
   if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
                             hnd->fd) != 0) {
     return GRALLOC1_ERROR_BAD_HANDLE;
@@ -274,7 +278,10 @@
     private_handle_t *handle = const_cast<private_handle_t *>(hnd);
     err = ImportHandle(handle);
     if (err == GRALLOC1_ERROR_NONE) {
-      // TODO(user): Do not map here, map should be in lock()
+      // TODO(user): See bug 35955598
+      if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
+        return GRALLOC1_ERROR_NONE;  // Don't map secure buffer
+      }
       err = MapBuffer(hnd);
     }
   }
@@ -303,6 +310,7 @@
                                            gralloc1_consumer_usage_t cons_usage) {
   std::lock_guard<std::mutex> lock(locker_);
   gralloc1_error_t err = GRALLOC1_ERROR_NONE;
+  ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
 
   // If buffer is not meant for CPU return err
   if (!CpuCanAccess(prod_usage, cons_usage)) {
@@ -443,17 +451,43 @@
   return flags;
 }
 
-int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
-                                  int unaligned_h, int format, int bufferType,
-                                  gralloc1_producer_usage_t prod_usage,
-                                  gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) {
-  auto page_size = UINT(getpagesize());
+int BufferManager::GetBufferType(int inputFormat) {
+  int buffer_type = BUFFER_TYPE_VIDEO;
+  if (IsUncompressedRGBFormat(inputFormat)) {
+    // RGB formats
+    buffer_type = BUFFER_TYPE_UI;
+  }
+
+  return buffer_type;
+}
+
+int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
+                                  unsigned int bufferSize) {
+  if (!handle)
+    return -EINVAL;
+
+  int format = descriptor.GetFormat();
+  gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
+  gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
+  uint32_t layer_count = descriptor.GetLayerCount();
+
+  // Get implementation defined format
+  int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
+
+  unsigned int size;
+  unsigned int alignedw, alignedh;
+  int buffer_type = GetBufferType(gralloc_format);
+  allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
+  size = (bufferSize >= size) ? bufferSize : size;
+  size = size * layer_count;
+
   int err = 0;
   int flags = 0;
+  auto page_size = UINT(getpagesize());
   AllocData data;
   data.align = GetDataAlignment(format, prod_usage, cons_usage);
   data.size = ALIGN(size, data.align);
-  data.handle = (uintptr_t)handle;
+  data.handle = (uintptr_t) handle;
   data.uncached = allocator_->UseUncached(prod_usage);
 
   // Allocate buffer memory
@@ -483,19 +517,20 @@
   private_handle_t *hnd = new private_handle_t(data.fd,
                                                e_data.fd,
                                                flags,
-                                               aligned_w,
-                                               aligned_h,
-                                               unaligned_w,
-                                               unaligned_h,
+                                               INT(alignedw),
+                                               INT(alignedh),
+                                               descriptor.GetWidth(),
+                                               descriptor.GetHeight(),
                                                format,
-                                               bufferType,
+                                               buffer_type,
                                                size,
                                                prod_usage,
                                                cons_usage);
 
   hnd->id = ++next_id_;
-  hnd->base = reinterpret_cast<uint64_t >(data.base);
-  hnd->base_metadata = reinterpret_cast<uint64_t >(e_data.base);
+  hnd->base = 0;
+  hnd->base_metadata = 0;
+  hnd->layer_count = layer_count;
 
   ColorSpace_t colorSpace = ITU_R_601;
   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
@@ -508,61 +543,6 @@
   return err;
 }
 
-int BufferManager::GetBufferType(int inputFormat) {
-  int buffer_type = BUFFER_TYPE_VIDEO;
-  if (IsUncompressedRGBFormat(inputFormat)) {
-    // RGB formats
-    buffer_type = BUFFER_TYPE_UI;
-  }
-
-  return buffer_type;
-}
-
-int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
-                                  unsigned int bufferSize) {
-  if (!handle)
-    return -EINVAL;
-
-  int format = descriptor.GetFormat();
-  gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
-  gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
-
-  // Get implementation defined format
-  int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
-
-  bool use_fb_mem = false;
-  if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && map_fb_mem_) {
-    use_fb_mem = true;
-  }
-
-  if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && ubwc_for_fb_) {
-    prod_usage =
-        (gralloc1_producer_usage_t)(prod_usage | GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC);
-  }
-
-  unsigned int size;
-  unsigned int alignedw, alignedh;
-  int buffer_type = GetBufferType(gralloc_format);
-  allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
-
-  size = (bufferSize >= size) ? bufferSize : size;
-
-  int err = 0;
-  if (use_fb_mem) {
-    // TODO(user): TBD Framebuffer specific implementation in a seperate file/class
-  } else {
-    err = AllocateBuffer(size, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
-                         descriptor.GetHeight(), format, buffer_type, descriptor.GetProducerUsage(),
-                         descriptor.GetConsumerUsage(), handle);
-  }
-
-  if (err < 0) {
-    return err;
-  }
-
-  return 0;
-}
-
 gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
   switch (operation) {
     case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
@@ -614,9 +594,9 @@
         return GRALLOC1_ERROR_BAD_HANDLE;
       }
 
-      MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
-      if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
-        *stride = metadata->bufferDim.sliceWidth;
+      BufferDim_t buffer_dim;
+      if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
+        *stride = buffer_dim.sliceWidth;
       } else {
         *stride = hnd->width;
       }
@@ -631,10 +611,10 @@
         return GRALLOC1_ERROR_BAD_HANDLE;
       }
 
-      MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
-      if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
-        *stride = metadata->bufferDim.sliceWidth;
-        *height = metadata->bufferDim.sliceHeight;
+      BufferDim_t buffer_dim;
+      if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
+        *stride = buffer_dim.sliceWidth;
+        *height = buffer_dim.sliceHeight;
       } else {
         *stride = hnd->width;
         *height = hnd->height;
@@ -670,30 +650,30 @@
       if (private_handle_t::validate(hnd) != 0) {
         return GRALLOC1_ERROR_BAD_HANDLE;
       }
-      MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
-      if (!metadata) {
-        return GRALLOC1_ERROR_BAD_HANDLE;
+      *color_space = 0;
 #ifdef USE_COLOR_METADATA
-      } else if (metadata->operation & COLOR_METADATA) {
-        ColorMetaData *colorMetadata = &metadata->color;
-        switch (colorMetadata->colorPrimaries) {
-        case ColorPrimaries_BT709_5:
-          *color_space = HAL_CSC_ITU_R_709;
-          break;
-        case ColorPrimaries_BT601_6_525:
-          *color_space = ((colorMetadata->range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
-           break;
-        case ColorPrimaries_BT2020:
-          *color_space = (colorMetadata->range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
-          break;
-        default:
-          ALOGE("Unknown Color Space = %d", colorMetadata->colorPrimaries);
-          break;
+      ColorMetaData color_metadata;
+      if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
+        switch (color_metadata.colorPrimaries) {
+          case ColorPrimaries_BT709_5:
+            *color_space = HAL_CSC_ITU_R_709;
+            break;
+          case ColorPrimaries_BT601_6_525:
+            *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
+            break;
+          case ColorPrimaries_BT2020:
+            *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
+            break;
+          default:
+            ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
+            break;
         }
-#endif
-      } else if (metadata->operation & UPDATE_COLOR_SPACE) {
-        *color_space = metadata->colorSpace;
+        break;
       }
+      if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
+          *color_space = 0;
+      }
+#endif
     } break;
     case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
       private_handle_t *hnd = va_arg(args, private_handle_t *);
@@ -712,10 +692,8 @@
       if (private_handle_t::validate(hnd) != 0) {
         return GRALLOC1_ERROR_BAD_HANDLE;
       }
-      MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
-      if (metadata && metadata->operation & MAP_SECURE_BUFFER) {
-        *map_secure_buffer = metadata->mapSecureBuffer;
-      } else {
+
+      if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
         *map_secure_buffer = 0;
       }
     } break;
diff --git a/libgralloc1/gr_buf_mgr.h b/libgralloc1/gr_buf_mgr.h
index 0677d96..738385d 100644
--- a/libgralloc1/gr_buf_mgr.h
+++ b/libgralloc1/gr_buf_mgr.h
@@ -75,10 +75,6 @@
   int GetBufferType(int format);
   int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
                      unsigned int bufferSize = 0);
-  int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
-                     int unaligned_h, int format, int bufferType,
-                     gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
-                     buffer_handle_t *handle);
   uint32_t GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
                        gralloc1_consumer_usage_t cons_usage);
   int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp
index 3649111..08173e8 100644
--- a/libgralloc1/gr_device_impl.cpp
+++ b/libgralloc1/gr_device_impl.cpp
@@ -103,10 +103,11 @@
 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
                                   int32_t  /*gralloc1_capability_t*/ *out_capabilities) {
   if (device != nullptr) {
-    if (out_capabilities != nullptr && *out_count > 0) {
+    if (out_capabilities != nullptr && *out_count >= 2) {
       out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
+      out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
     }
-    *out_count = 1;
+    *out_count = 2;
   }
   return;
 }
@@ -129,6 +130,8 @@
       return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
     case GRALLOC1_FUNCTION_SET_FORMAT:
       return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
+    case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
+      return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
     case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
       return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
     case GRALLOC1_FUNCTION_GET_BACKING_STORE:
@@ -139,6 +142,8 @@
       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
     case GRALLOC1_FUNCTION_GET_FORMAT:
       return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
+    case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
+      return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
     case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
       return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
     case GRALLOC1_FUNCTION_GET_STRIDE:
@@ -257,6 +262,19 @@
   }
 }
 
+gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
+                                            gralloc1_buffer_descriptor_t descriptor,
+                                            uint32_t layer_count) {
+  if (!device) {
+    return GRALLOC1_ERROR_BAD_DESCRIPTOR;
+  } else {
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
+                                                       &BufferDescriptor::SetLayerCount,
+                                                       layer_count);
+  }
+}
+
 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
                                                gralloc1_buffer_descriptor_t descriptor,
                                                gralloc1_producer_usage_t usage) {
@@ -313,6 +331,16 @@
   return status;
 }
 
+gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
+                                            uint32_t *outLayerCount) {
+  gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
+  if (status == GRALLOC1_ERROR_NONE) {
+    *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
+  }
+
+  return status;
+}
+
 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
                                                gralloc1_producer_usage_t *outUsage) {
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
diff --git a/libgralloc1/gr_device_impl.h b/libgralloc1/gr_device_impl.h
index 5a3e7e9..55ce44b 100644
--- a/libgralloc1/gr_device_impl.h
+++ b/libgralloc1/gr_device_impl.h
@@ -76,6 +76,9 @@
                                               uint32_t width, uint32_t height);
   static gralloc1_error_t SetColorFormat(gralloc1_device_t *device,
                                          gralloc1_buffer_descriptor_t descriptor, int32_t format);
+  static gralloc1_error_t SetLayerCount(gralloc1_device_t *device,
+                                        gralloc1_buffer_descriptor_t descriptor,
+                                        uint32_t layer_count);
   static gralloc1_error_t SetProducerUsage(gralloc1_device_t *device,
                                            gralloc1_buffer_descriptor_t descriptor,
                                            gralloc1_producer_usage_t usage);
@@ -87,6 +90,8 @@
                                               uint32_t *out_width, uint32_t *out_height);
   static gralloc1_error_t GetColorFormat(gralloc1_device_t *device, buffer_handle_t descriptor,
                                          int32_t *outFormat);
+  static gralloc1_error_t GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
+                                        uint32_t *out_layer_count);
   static gralloc1_error_t GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
                                            gralloc1_producer_usage_t *out_usage);
   static gralloc1_error_t GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
diff --git a/libgralloc1/gr_ion_alloc.cpp b/libgralloc1/gr_ion_alloc.cpp
index 25792e5..72fe995 100644
--- a/libgralloc1/gr_ion_alloc.cpp
+++ b/libgralloc1/gr_ion_alloc.cpp
@@ -71,7 +71,6 @@
   struct ion_handle_data handle_data;
   struct ion_fd_data fd_data;
   struct ion_allocation_data ion_alloc_data;
-  void *base = NULL;
 
   ion_alloc_data.len = data->size;
   ion_alloc_data.align = data->align;
@@ -94,21 +93,10 @@
     return err;
   }
 
-  if (!(INT(data->flags) & INT(ION_SECURE))) {
-    base = mmap(0, ion_alloc_data.len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
-    if (base == MAP_FAILED) {
-      err = -errno;
-      ALOGE("%s: Failed to map the allocated memory: %s", __FUNCTION__, strerror(errno));
-      ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
-      return err;
-    }
-  }
-
-  data->base = base;
   data->fd = fd_data.fd;
   data->ion_handle = handle_data.handle;
-  ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d handle:0x%x", data->base,
-           ion_alloc_data.len, data->fd, data->ion_handle);
+  ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x",
+          ion_alloc_data.len, data->fd, data->ion_handle);
 
   return 0;
 }
diff --git a/libgralloc1/gr_priv_handle.h b/libgralloc1/gr_priv_handle.h
index 27d405b..5948aab 100644
--- a/libgralloc1/gr_priv_handle.h
+++ b/libgralloc1/gr_priv_handle.h
@@ -83,6 +83,7 @@
   uint64_t id                              __attribute__((aligned(8)));
   gralloc1_producer_usage_t producer_usage __attribute__((aligned(8)));
   gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8)));
+  unsigned int layer_count;
 
   static const int kNumFds = 2;
   static const int kMagic = 'gmsm';
@@ -123,7 +124,8 @@
         gpuaddr(0),
         id(0),
         producer_usage(prod_usage),
-        consumer_usage(cons_usage) {
+        consumer_usage(cons_usage),
+        layer_count(1) {
     version = static_cast<int>(sizeof(native_handle));
     numInts = NumInts();
     numFds = kNumFds;
@@ -162,11 +164,11 @@
   }
 
   static void Dump(const private_handle_t *hnd) {
-    ALOGD("handle id:%" PRIu64 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x"
-          "prod_usage:0x%" PRIx64" cons_usage:0x%" PRIx64 "format:0x%x",
+    ALOGD("handle id:%" PRIu64 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x "
+          "prod_usage:0x%" PRIx64" cons_usage:0x%" PRIx64 " format:0x%x layer_count: %d",
           hnd->id, hnd->width, hnd->height, hnd->unaligned_width, hnd->unaligned_height, hnd->size,
           hnd->fd, hnd->fd_metadata, hnd->flags, hnd->producer_usage, hnd->consumer_usage,
-          hnd->format);
+          hnd->format, hnd->layer_count);
   }
 
   int GetUnalignedWidth() const { return unaligned_width; }
@@ -175,6 +177,8 @@
 
   int GetColorFormat() const { return format; }
 
+  unsigned int GetLayerCount() const { return layer_count; }
+
   int GetStride() const {
     // In handle we currently store aligned width after allocation.
     return width;
diff --git a/liblight/Android.mk b/liblight/Android.mk
index aa09187..1fa05f7 100644
--- a/liblight/Android.mk
+++ b/liblight/Android.mk
@@ -28,4 +28,9 @@
 LOCAL_MODULE := lights.$(TARGET_BOARD_PLATFORM)
 LOCAL_MODULE_TAGS := optional
 
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 include $(BUILD_SHARED_LIBRARY)
diff --git a/libmemtrack/Android.mk b/libmemtrack/Android.mk
index 4e7a09f..12475c3 100644
--- a/libmemtrack/Android.mk
+++ b/libmemtrack/Android.mk
@@ -19,6 +19,12 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE_RELATIVE_PATH := hw
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_C_INCLUDES += hardware/libhardware/include
 LOCAL_CFLAGS := -Wconversion -Wall -Werror -Wno-sign-conversion
 LOCAL_CLANG  := true
diff --git a/libqdutils/Android.mk b/libqdutils/Android.mk
index cc0b013..c7f428b 100644
--- a/libqdutils/Android.mk
+++ b/libqdutils/Android.mk
@@ -3,9 +3,16 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libqdutils
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libbinder libqservice
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
+LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_CFLAGS                  := $(common_flags) -DLOG_TAG=\"qdutils\" -Wno-sign-conversion
 LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps)
 LOCAL_COPY_HEADERS_TO         := $(common_header_export_path)
@@ -21,6 +28,7 @@
 LOCAL_COPY_HEADERS              := qdMetaData.h
 LOCAL_SHARED_LIBRARIES          := liblog libcutils
 LOCAL_C_INCLUDES                := $(common_includes)
+LOCAL_HEADER_LIBRARIES          := display_headers
 LOCAL_ADDITIONAL_DEPENDENCIES   := $(common_deps)
 LOCAL_SRC_FILES                 := qdMetaData.cpp
 LOCAL_CFLAGS                    := $(common_flags) -Wno-sign-conversion
@@ -28,5 +36,11 @@
 
 LOCAL_MODULE_TAGS               := optional
 LOCAL_MODULE                    := libqdMetaData
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/libqdutils/qdMetaData.cpp b/libqdutils/qdMetaData.cpp
index 8f1e617..af86eee 100644
--- a/libqdutils/qdMetaData.cpp
+++ b/libqdutils/qdMetaData.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -35,28 +35,42 @@
 #include <gralloc_priv.h>
 #include "qdMetaData.h"
 
-int setMetaData(private_handle_t *handle, DispParamType paramType,
-                                                    void *param) {
+static int validateAndMap(private_handle_t* handle) {
     if (private_handle_t::validate(handle)) {
-        ALOGE("%s: Private handle is invalid! handle=%p", __func__, handle);
+        ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
         return -1;
     }
     if (handle->fd_metadata == -1) {
-        ALOGE("%s: Bad fd for extra data!", __func__);
+        ALOGE("%s: Invalid metadata fd - handle:%p fd: %d",
+                __func__, handle, handle->fd_metadata);
         return -1;
     }
-    unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
-    void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
-        handle->fd_metadata, 0);
-    if (base == reinterpret_cast<void*>(MAP_FAILED)) {
-        ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
-        return -1;
+
+    if (!handle->base_metadata) {
+        unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
+        void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
+                handle->fd_metadata, 0);
+        if (base == reinterpret_cast<void*>(MAP_FAILED)) {
+            ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s",
+                __func__, handle, handle->fd_metadata, strerror(errno));
+
+            return -1;
+        }
+        handle->base_metadata = (uintptr_t) base;
     }
-    MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
+    return 0;
+}
+
+int setMetaData(private_handle_t *handle, DispParamType paramType,
+                                                    void *param) {
+    auto err = validateAndMap(handle);
+    if (err != 0)
+        return err;
+
+    MetaData_t *data = reinterpret_cast <MetaData_t *>(handle->base_metadata);
     // If parameter is NULL reset the specific MetaData Key
     if (!param) {
        data->operation &= ~paramType;
-       return munmap(base, size);
     }
 
     data->operation |= paramType;
@@ -103,30 +117,15 @@
             ALOGE("Unknown paramType %d", paramType);
             break;
     }
-    if(munmap(base, size))
-        ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
-                                                                        errno);
     return 0;
 }
 
 int clearMetaData(private_handle_t *handle, DispParamType paramType) {
-    if (!handle) {
-        ALOGE("%s: Private handle is null!", __func__);
-        return -1;
-    }
-    if (handle->fd_metadata == -1) {
-        ALOGE("%s: Bad fd for extra data!", __func__);
-        return -1;
-    }
+    auto err = validateAndMap(handle);
+    if (err != 0)
+        return err;
 
-    unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
-    void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
-        handle->fd_metadata, 0);
-    if (base == reinterpret_cast<void*>(MAP_FAILED)) {
-        ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
-        return -1;
-    }
-    MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
+    MetaData_t *data = reinterpret_cast <MetaData_t *>(handle->base_metadata);
     data->operation &= ~paramType;
     switch (paramType) {
         case SET_S3D_COMP:
@@ -137,124 +136,112 @@
             ALOGE("Unknown paramType %d", paramType);
             break;
     }
-    if(munmap(base, size))
-        ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
-                                                                        errno);
     return 0;
 }
 
 int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
                                                     void *param) {
-    if (!handle) {
-        ALOGE("%s: Private handle is null!", __func__);
-        return -1;
-    }
-    if (handle->fd_metadata == -1) {
-        ALOGE("%s: Bad fd for extra data!", __func__);
-        return -1;
-    }
-    if (!param) {
-        ALOGE("%s: input param is null!", __func__);
-        return -1;
-    }
-    unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
-    void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
-        handle->fd_metadata, 0);
-    if (base == reinterpret_cast<void*>(MAP_FAILED)) {
-        ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
-        return -1;
-    }
+    int ret = validateAndMap(handle);
+    if (ret != 0)
+        return ret;
+    MetaData_t *data = reinterpret_cast <MetaData_t *>(handle->base_metadata);
+    // Make sure we send 0 only if the operation queried is present
+    ret = -EINVAL;
 
-    MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
     switch (paramType) {
         case GET_PP_PARAM_INTERLACED:
-            *((int32_t *)param) = data->interlaced;
+            if (data->operation & PP_PARAM_INTERLACED) {
+                *((int32_t *)param) = data->interlaced;
+                ret = 0;
+            }
             break;
         case GET_BUFFER_GEOMETRY:
-            *((BufferDim_t *)param) = data->bufferDim;
+            if (data->operation & UPDATE_BUFFER_GEOMETRY) {
+                *((BufferDim_t *)param) = data->bufferDim;
+                ret = 0;
+            }
             break;
         case GET_REFRESH_RATE:
-            *((float *)param) = data->refreshrate;
+            if (data->operation & UPDATE_REFRESH_RATE) {
+                *((float *)param) = data->refreshrate;
+                ret = 0;
+            }
             break;
         case GET_COLOR_SPACE:
-            *((ColorSpace_t *)param) = data->colorSpace;
+            if (data->operation & UPDATE_COLOR_SPACE) {
+                *((ColorSpace_t *)param) = data->colorSpace;
+                ret = 0;
+            }
             break;
         case GET_MAP_SECURE_BUFFER:
-            *((int32_t *)param) = data->mapSecureBuffer;
+            if (data->operation & MAP_SECURE_BUFFER) {
+                *((int32_t *)param) = data->mapSecureBuffer;
+                ret = 0;
+            }
             break;
         case GET_S3D_FORMAT:
-            *((uint32_t *)param) = data->s3dFormat;
+            if (data->operation & S3D_FORMAT) {
+                *((uint32_t *)param) = data->s3dFormat;
+                ret = 0;
+            }
             break;
         case GET_LINEAR_FORMAT:
-            *((uint32_t *)param) = data->linearFormat;
+            if (data->operation & LINEAR_FORMAT) {
+                *((uint32_t *)param) = data->linearFormat;
+                ret = 0;
+            }
             break;
         case GET_IGC:
-            *((IGC_t *)param) = data->igc;
+            if (data->operation & SET_IGC) {
+                *((IGC_t *)param) = data->igc;
+                ret = 0;
+            }
             break;
         case GET_SINGLE_BUFFER_MODE:
-            *((uint32_t *)param) = data->isSingleBufferMode ;
+            if (data->operation & SET_SINGLE_BUFFER_MODE) {
+                *((uint32_t *)param) = data->isSingleBufferMode;
+                ret = 0;
+            }
             break;
         case GET_S3D_COMP:
-            *((S3DGpuComp_t *)param) = data->s3dComp;
+            if (data->operation & SET_S3D_COMP) {
+                *((S3DGpuComp_t *)param) = data->s3dComp;
+                ret = 0;
+            }
             break;
         case GET_VT_TIMESTAMP:
-            *((uint64_t *)param) = data->vtTimeStamp;
+            if (data->operation & SET_VT_TIMESTAMP) {
+                *((uint64_t *)param) = data->vtTimeStamp;
+                ret = 0;
+            }
             break;
 #ifdef USE_COLOR_METADATA
         case GET_COLOR_METADATA:
-            *((ColorMetaData *)param) = data->color;
+            if (data->operation & COLOR_METADATA) {
+                *((ColorMetaData *)param) = data->color;
+                ret = 0;
+            }
 #endif
             break;
         default:
             ALOGE("Unknown paramType %d", paramType);
             break;
     }
-    if(munmap(base, size))
-        ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
-                                                                        errno);
-    return 0;
+    return ret;
 }
 
 int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
-    if (!src || !dst) {
-        ALOGE("%s: Private handle is null!", __func__);
-        return -1;
-    }
-    if (src->fd_metadata == -1) {
-        ALOGE("%s: Bad fd for src extra data!", __func__);
-        return -1;
-    }
-    if (dst->fd_metadata == -1) {
-        ALOGE("%s: Bad fd for dst extra data!", __func__);
-        return -1;
-    }
+    auto err = validateAndMap(src);
+    if (err != 0)
+        return err;
+
+    err = validateAndMap(dst);
+    if (err != 0)
+        return err;
 
     unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
-
-    void *base_src = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
-        src->fd_metadata, 0);
-    if (base_src == reinterpret_cast<void*>(MAP_FAILED)) {
-        ALOGE("%s: src mmap() failed: error is %s!", __func__, strerror(errno));
-        return -1;
-    }
-
-    void *base_dst = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
-        dst->fd_metadata, 0);
-    if (base_dst == reinterpret_cast<void*>(MAP_FAILED)) {
-        ALOGE("%s: dst mmap() failed: error is %s!", __func__, strerror(errno));
-        if(munmap(base_src, size))
-            ALOGE("%s: failed to unmap src ptr %p, err %d", __func__,
-                                             (void*)base_src, errno);
-        return -1;
-    }
-
-    memcpy(base_dst, base_src, size);
-
-    if(munmap(base_src, size))
-        ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_src,
-                                                                        errno);
-    if(munmap(base_dst, size))
-        ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_dst,
-                                                                        errno);
+    MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
+    MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
+    memcpy(src_data, dst_data, size);
     return 0;
 }
diff --git a/libqservice/Android.mk b/libqservice/Android.mk
index ea11180..0f77d4f 100644
--- a/libqservice/Android.mk
+++ b/libqservice/Android.mk
@@ -3,6 +3,12 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE                  := libqservice
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
 LOCAL_SHARED_LIBRARIES        := $(common_libs) libbinder
diff --git a/sdm/libs/core/Android.mk b/sdm/libs/core/Android.mk
index 114fcaf..874b150 100644
--- a/sdm/libs/core/Android.mk
+++ b/sdm/libs/core/Android.mk
@@ -3,8 +3,15 @@
 include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmcore
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
-LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes) $(common_header_export_path)
+LOCAL_C_INCLUDES              := $(common_includes) $(kernel_includes)
+LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_CFLAGS                  := -Wno-unused-parameter -DLOG_TAG=\"SDM\" \
                                  $(common_flags)
 ifeq ($(use_hwc2),false)
diff --git a/sdm/libs/hwc/Android.mk b/sdm/libs/hwc/Android.mk
index 9c78cd3..049f0f9 100644
--- a/sdm/libs/hwc/Android.mk
+++ b/sdm/libs/hwc/Android.mk
@@ -4,6 +4,12 @@
 ifeq ($(use_hwc2),false)
 
 LOCAL_MODULE                  := hwcomposer.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
diff --git a/sdm/libs/hwc2/Android.mk b/sdm/libs/hwc2/Android.mk
index d4ff53c..ca1ebae 100644
--- a/sdm/libs/hwc2/Android.mk
+++ b/sdm/libs/hwc2/Android.mk
@@ -5,9 +5,16 @@
 ifeq ($(use_hwc2),true)
 
 LOCAL_MODULE                  := hwcomposer.$(TARGET_BOARD_PLATFORM)
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_RELATIVE_PATH    := hw
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
+LOCAL_HEADER_LIBRARIES        := display_headers
 
 LOCAL_CFLAGS                  := -Wno-missing-field-initializers -Wno-unused-parameter \
                                  -std=c++11 -fcolor-diagnostics\
@@ -16,8 +23,8 @@
 LOCAL_CLANG                   := true
 
 LOCAL_SHARED_LIBRARIES        := libsdmcore libqservice libbinder libhardware libhardware_legacy \
-                                 libutils libcutils libsync libqdutils libdl \
-                                 libpowermanager libsdmutils libc++ liblog
+                                 libutils libcutils libsync libqdutils libqdMetaData libdl \
+                                 libpowermanager libsdmutils libc++ liblog libdrmutils
 
 ifneq ($(TARGET_USES_GRALLOC1), true)
     LOCAL_SHARED_LIBRARIES += libmemalloc
diff --git a/sdm/libs/hwc2/hwc_display_virtual.cpp b/sdm/libs/hwc2/hwc_display_virtual.cpp
index 11346ed..f77e9c9 100644
--- a/sdm/libs/hwc2/hwc_display_virtual.cpp
+++ b/sdm/libs/hwc2/hwc_display_virtual.cpp
@@ -208,8 +208,7 @@
     output_buffer_->flags.secure = 0;
     output_buffer_->flags.video = 0;
 
-    const MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(output_handle->base_metadata);
-    if (meta_data && SetCSC(meta_data, &output_buffer_->color_metadata) != kErrorNone) {
+    if (sdm::SetCSC(output_handle, &output_buffer_->color_metadata) != kErrorNone) {
       return HWC2::Error::BadParameter;
     }
 
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 4345b30..91fb04b 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -17,6 +17,9 @@
  * limitations under the License.
  */
 
+#include <stdint.h>
+#include <qdMetaData.h>
+
 #include "hwc_layers.h"
 #ifndef USE_GRALLOC1
 #include <gr.h>
@@ -30,34 +33,34 @@
 
 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
 
-DisplayError SetCSC(const MetaData_t *meta_data, ColorMetaData *color_metadata) {
-  if (meta_data->operation & COLOR_METADATA) {
-#ifdef USE_COLOR_METADATA
-    *color_metadata = meta_data->color;
-#endif
-  } else if (meta_data->operation & UPDATE_COLOR_SPACE) {
-    ColorSpace_t csc = meta_data->colorSpace;
-    color_metadata->range = Range_Limited;
+DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata) {
+  if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_METADATA,
+                  color_metadata) != 0) {
+    ColorSpace_t csc = ITU_R_601;
+    if (getMetaData(const_cast<private_handle_t *>(pvt_handle),  GET_COLOR_SPACE,
+                    &csc) == 0) {
+      if (csc == ITU_R_601_FR || csc == ITU_R_2020_FR) {
+        color_metadata->range = Range_Full;
+      }
 
-    if (csc == ITU_R_601_FR || csc == ITU_R_2020_FR) {
-      color_metadata->range = Range_Full;
-    }
-
-    switch (csc) {
-    case ITU_R_601:
-    case ITU_R_601_FR:
-      // video and display driver uses 601_525
-      color_metadata->colorPrimaries = ColorPrimaries_BT601_6_525;
-      break;
-    case ITU_R_709:
-      color_metadata->colorPrimaries = ColorPrimaries_BT709_5;
-      break;
-    case ITU_R_2020:
-    case ITU_R_2020_FR:
+      switch (csc) {
+      case ITU_R_601:
+      case ITU_R_601_FR:
+        // video and display driver uses 601_525
+        color_metadata->colorPrimaries = ColorPrimaries_BT601_6_525;
+        break;
+      case ITU_R_709:
+        color_metadata->colorPrimaries = ColorPrimaries_BT709_5;
+        break;
+      case ITU_R_2020:
+      case ITU_R_2020_FR:
         color_metadata->colorPrimaries = ColorPrimaries_BT2020;
         break;
-    default:
-      DLOGE("Unsupported CSC: %d", csc);
+      default:
+        DLOGE("Unsupported CSC: %d", csc);
+        return kErrorNotSupported;
+      }
+    } else {
       return kErrorNotSupported;
     }
   }
@@ -122,7 +125,7 @@
   layer_buffer->unaligned_height = UINT32(handle->unaligned_height);
 
   layer_buffer->format = GetSDMFormat(handle->format, handle->flags);
-  if (SetMetaData(handle, layer_) != kErrorNone) {
+  if (SetMetaData(const_cast<private_handle_t *>(handle), layer_) != kErrorNone) {
     return HWC2::Error::BadLayer;
   }
 
@@ -482,37 +485,37 @@
 }
 
 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
-  const MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
   LayerBuffer *layer_buffer = &layer->input_buffer;
-
-  if (!meta_data) {
-    return kErrorNone;
-  }
-
-  if (sdm::SetCSC(meta_data, &layer_buffer->color_metadata) != kErrorNone) {
+  if (sdm::SetCSC(pvt_handle, &layer_buffer->color_metadata) != kErrorNone) {
     return kErrorNotSupported;
   }
 
-  if (meta_data->operation & SET_IGC) {
-    if (SetIGC(meta_data->igc, &layer_buffer->igc) != kErrorNone) {
+  private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
+  IGC_t igc = {};
+  if (getMetaData(handle, GET_IGC, &igc) == 0) {
+    if (SetIGC(igc, &layer_buffer->igc) != kErrorNone) {
       return kErrorNotSupported;
     }
   }
 
-  if (meta_data->operation & UPDATE_REFRESH_RATE) {
-    layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate);
+  uint32_t fps = 0;
+  if (getMetaData(handle, GET_REFRESH_RATE  , &fps) == 0) {
+    layer->frame_rate = RoundToStandardFPS(fps);
   }
 
-  if ((meta_data->operation & PP_PARAM_INTERLACED) && meta_data->interlaced) {
-    layer_buffer->flags.interlace = true;
+  int32_t interlaced = 0;
+  if (getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
+    layer_buffer->flags.interlace = interlaced ? true : false;
   }
 
-  if (meta_data->operation & LINEAR_FORMAT) {
-    layer_buffer->format = GetSDMFormat(INT32(meta_data->linearFormat), 0);
+  uint32_t linear_format = 0;
+  if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
+    layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
   }
 
-  if (meta_data->operation & S3D_FORMAT) {
-    layer_buffer->s3d_format = GetS3DFormat(meta_data->s3dFormat);
+  uint32_t s3d = 0;
+  if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
+    layer_buffer->s3d_format = GetS3DFormat(s3d);
   }
 
   return kErrorNone;
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index f9cb23e..58a45ae 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -39,7 +39,7 @@
 
 namespace sdm {
 
-DisplayError SetCSC(const MetaData_t *meta_data, ColorMetaData *color_metadata);
+DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata);
 
 enum GeometryChanges {
   kNone         = 0x000,
@@ -105,7 +105,6 @@
   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
   LayerBufferS3DFormat GetS3DFormat(uint32_t s3d_format);
   DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer);
-  DisplayError SetCSC(const MetaData_t *meta_data, ColorMetaData *color_metadata);
   DisplayError SetIGC(IGC_t source, LayerIGC *target);
   uint32_t RoundToStandardFPS(float fps);
 };
diff --git a/sdm/libs/utils/Android.mk b/sdm/libs/utils/Android.mk
index f7f30a9..36d1148 100644
--- a/sdm/libs/utils/Android.mk
+++ b/sdm/libs/utils/Android.mk
@@ -3,8 +3,15 @@
 include $(LOCAL_PATH)/../../../common.mk
 
 LOCAL_MODULE                  := libsdmutils
+
+ifneq ($(TARGET_IS_HEADLESS), true)
+LOCAL_MODULE_PATH_32          := $(TARGET_OUT_VENDOR)/lib
+LOCAL_MODULE_PATH_64          := $(TARGET_OUT_VENDOR)/lib64
+endif
+
 LOCAL_MODULE_TAGS             := optional
 LOCAL_C_INCLUDES              := $(common_includes)
+LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_CFLAGS                  := -DLOG_TAG=\"SDM\" $(common_flags)
 LOCAL_SRC_FILES               := debug.cpp \
                                  rect.cpp \