Merge "Revert "gralloc: Return an error if the buffer was not mapped""
diff --git a/common.mk b/common.mk
index 6b3e2e9..ae0a6cc 100644
--- a/common.mk
+++ b/common.mk
@@ -1,9 +1,14 @@
 #Common headers
 display_top := $(call my-dir)
 
+#Common C flags
+common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
+common_flags += -Wconversion -Wall -Werror
+
 use_hwc2 := false
 ifeq ($(TARGET_USES_HWC2), true)
     use_hwc2 := true
+    common_flags += -DVIDEO_MODE_DEFER_RETIRE_FENCE
 endif
 
 common_includes := $(display_top)/libqdutils
@@ -16,9 +21,6 @@
 #Common libraries external to display HAL
 common_libs := liblog libutils libcutils libhardware
 
-#Common C flags
-common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
-common_flags += -Wconversion -Wall -Werror
 ifneq ($(TARGET_USES_GRALLOC1), true)
     common_flags += -isystem $(display_top)/libgralloc
 else
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index e9cdc73..158fb68 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -958,6 +958,11 @@
             return false;
     }
 
+    // Workaround for bug 30191188/ CR 1047578
+    if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
+        return false;
+    }
+
     // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
     // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
     // usage flag and MDP supports the format.
diff --git a/libqdutils/qdMetaData.cpp b/libqdutils/qdMetaData.cpp
index 72edfdf..4ada44b 100644
--- a/libqdutils/qdMetaData.cpp
+++ b/libqdutils/qdMetaData.cpp
@@ -87,6 +87,9 @@
         case SET_SINGLE_BUFFER_MODE:
             data->isSingleBufferMode = *((uint32_t *)param);
             break;
+        case SET_VT_TIMESTAMP:
+            data->vtTimeStamp = *((uint64_t *)param);
+            break;
         default:
             ALOGE("Unknown paramType %d", paramType);
             break;
@@ -149,6 +152,9 @@
         case GET_SINGLE_BUFFER_MODE:
             *((uint32_t *)param) = data->isSingleBufferMode ;
             break;
+        case GET_VT_TIMESTAMP:
+            *((uint64_t *)param) = data->vtTimeStamp;
+            break;
         default:
             ALOGE("Unknown paramType %d", paramType);
             break;
diff --git a/libqdutils/qdMetaData.h b/libqdutils/qdMetaData.h
index fd4f444..deaeedd 100644
--- a/libqdutils/qdMetaData.h
+++ b/libqdutils/qdMetaData.h
@@ -79,36 +79,40 @@
     /* Set by graphics to indicate that this buffer will be written to but not
      * swapped out */
     uint32_t isSingleBufferMode;
+    /* Set by camera to program the VT Timestamp */
+    uint64_t vtTimeStamp;
 };
 
 enum DispParamType {
-    UNUSED0             = 0x0001,
-    UNUSED1             = 0x0002,
-    PP_PARAM_INTERLACED = 0x0004,
-    UNUSED2             = 0x0008,
-    UNUSED3             = 0x0010,
-    UNUSED4             = 0x0020,
-    UNUSED5             = 0x0040,
+    UNUSED0                = 0x0001,
+    UNUSED1                = 0x0002,
+    PP_PARAM_INTERLACED    = 0x0004,
+    UNUSED2                = 0x0008,
+    UNUSED3                = 0x0010,
+    UNUSED4                = 0x0020,
+    UNUSED5                = 0x0040,
     UPDATE_BUFFER_GEOMETRY = 0x0080,
-    UPDATE_REFRESH_RATE = 0x0100,
-    UPDATE_COLOR_SPACE = 0x0200,
-    MAP_SECURE_BUFFER = 0x400,
-    S3D_FORMAT = 0x800,
-    LINEAR_FORMAT = 0x1000,
-    SET_IGC = 0x2000,
+    UPDATE_REFRESH_RATE    = 0x0100,
+    UPDATE_COLOR_SPACE     = 0x0200,
+    MAP_SECURE_BUFFER      = 0x0400,
+    S3D_FORMAT             = 0x0800,
+    LINEAR_FORMAT          = 0x1000,
+    SET_IGC                = 0x2000,
     SET_SINGLE_BUFFER_MODE = 0x4000,
+    SET_VT_TIMESTAMP       = 0x8000,
 };
 
 enum DispFetchParamType {
-    GET_PP_PARAM_INTERLACED = 0x0004,
-    GET_BUFFER_GEOMETRY = 0x0080,
-    GET_REFRESH_RATE = 0x0100,
-    GET_COLOR_SPACE = 0x0200,
-    GET_MAP_SECURE_BUFFER = 0x400,
-    GET_S3D_FORMAT = 0x800,
-    GET_LINEAR_FORMAT = 0x1000,
-    GET_IGC = 0x2000,
-    GET_SINGLE_BUFFER_MODE = 0x4000,
+    GET_PP_PARAM_INTERLACED  = 0x0004,
+    GET_BUFFER_GEOMETRY      = 0x0080,
+    GET_REFRESH_RATE         = 0x0100,
+    GET_COLOR_SPACE          = 0x0200,
+    GET_MAP_SECURE_BUFFER    = 0x0400,
+    GET_S3D_FORMAT           = 0x0800,
+    GET_LINEAR_FORMAT        = 0x1000,
+    GET_IGC                  = 0x2000,
+    GET_SINGLE_BUFFER_MODE   = 0x4000,
+    GET_VT_TIMESTAMP         = 0x8000,
 };
 
 struct private_handle_t;
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index c707847..6cb919d 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -458,7 +458,12 @@
   }
 
   stack->retire_fence_fd = mdp_commit.retire_fence;
-
+#ifdef VIDEO_MODE_DEFER_RETIRE_FENCE
+  if (hw_panel_info_.mode == kModeVideo) {
+    stack->retire_fence_fd = stored_retire_fence;
+    stored_retire_fence =  mdp_commit.retire_fence;
+  }
+#endif
   // MDP returns only one release fence for the entire layer stack. Duplicate this fence into all
   // layers being composed by MDP.
 
diff --git a/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index 40f4f63..d82a420 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -135,6 +135,7 @@
   const char *fb_path_;
   BufferSyncHandler *buffer_sync_handler_;
   int device_fd_;
+  int stored_retire_fence = -1;
   HWDeviceType device_type_;
   mdp_layer_commit mdp_disp_commit_;
   mdp_input_layer mdp_in_layers_[kMaxSDELayers * 2];   // split panel (left + right)
diff --git a/sdm/libs/hwc2/hwc_display.cpp b/sdm/libs/hwc2/hwc_display.cpp
index bb2bc92..f9bf09b 100644
--- a/sdm/libs/hwc2/hwc_display.cpp
+++ b/sdm/libs/hwc2/hwc_display.cpp
@@ -60,7 +60,7 @@
 
 HWC2::Error HWCColorMode::Init() {
   PopulateColorModes();
-  return HWC2::Error::None;
+  return SetColorMode(HAL_COLOR_MODE_NATIVE);
 }
 
 HWC2::Error HWCColorMode::DeInit() {
@@ -360,9 +360,15 @@
       layer_stack_.flags.skip_present = true;
     }
 
-    if (layer->flags.cursor) {
-      layer_stack_.flags.cursor_present = true;
+
+    if (hwc_layer->GetClientRequestedCompositionType() == HWC2::Composition::Cursor) {
+      // Currently we support only one HWCursor & only at top most z-order
+      if ((*layer_set_.rbegin())->GetId() == hwc_layer->GetId()) {
+        layer->flags.cursor = true;
+        layer_stack_.flags.cursor_present = true;
+      }
     }
+
     // TODO(user): Move to a getter if this is needed at other places
     hwc_rect_t scaled_display_frame = {INT(layer->dst_rect.left), INT(layer->dst_rect.top),
                                        INT(layer->dst_rect.right), INT(layer->dst_rect.bottom)};
diff --git a/sdm/libs/hwc2/hwc_display_virtual.cpp b/sdm/libs/hwc2/hwc_display_virtual.cpp
index 90e3015..787640c 100644
--- a/sdm/libs/hwc2/hwc_display_virtual.cpp
+++ b/sdm/libs/hwc2/hwc_display_virtual.cpp
@@ -174,6 +174,7 @@
   output_buffer_->acquire_fence_fd = dup(release_fence);
 
   if (output_handle) {
+    output_handle_ = output_handle;
     output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
     int output_handle_format = output_handle->format;
 
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 098959b..0f9b6b2 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -147,7 +147,6 @@
       layer_->flags.solid_fill = true;
       break;
     case HWC2::Composition::Cursor:
-      layer_->flags.cursor = true;
       break;
     case HWC2::Composition::Invalid:
       return HWC2::Error::BadParameter;
diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp
index df24098..60db660 100644
--- a/sdm/libs/hwc2/hwc_session.cpp
+++ b/sdm/libs/hwc2/hwc_session.cpp
@@ -200,9 +200,10 @@
 
 void HWCSession::GetCapabilities(struct hwc2_device *device, uint32_t *outCount,
                                  int32_t *outCapabilities) {
-  if (outCapabilities == NULL) {
-    *outCount = 0;
+  if (outCapabilities != nullptr && *outCount >= 1) {
+    outCapabilities[0] = HWC2_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
   }
+  *outCount = 1;
 }
 
 template <typename PFN, typename T>