display: Merge changes from display.lnx.4.0

Change-Id: I9cd5aec75486514b6dfb04b6edc58e6eff1ff01b
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 189f19e..815f2b0 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2017 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2018 The Linux Foundation. All rights reserved.
  * Not a Contribution
  *
  * Copyright (C) 2010 The Android Open Source Project
@@ -407,7 +407,7 @@
     flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
   }
 
-  if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
+  if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
     flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
   }
 
diff --git a/libgralloc1/gralloc_priv.h b/libgralloc1/gralloc_priv.h
index fb50965..0a2314b 100644
--- a/libgralloc1/gralloc_priv.h
+++ b/libgralloc1/gralloc_priv.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution
  *
  * Copyright (C) 2008 The Android Open Source Project
@@ -67,7 +67,7 @@
 #define GRALLOC1_CONSUMER_USAGE_PRIVATE_WFD            0x00200000
 
 /* This flag is used for SECURE display usecase */
-#define GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY 0x01000000
+#define GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY 0x02000000
 
 /* This flag is used to indicate P010 format */
 #define GRALLOC1_CONSUMER_USAGE_PRIVATE_10BIT       GRALLOC1_PRODUCER_USAGE_PRIVATE_10BIT
diff --git a/sdm/include/private/hw_info_types.h b/sdm/include/private/hw_info_types.h
index 5d311d9..52bc73e 100644
--- a/sdm/include/private/hw_info_types.h
+++ b/sdm/include/private/hw_info_types.h
@@ -123,6 +123,13 @@
   kPPSplit,
 };
 
+enum HwHdrEotf {
+  kHdrEOTFInvalid = 0,
+  kHdrEOTFSDR = 0x1,
+  kHdrEOTFHdrLumRange = 0x2,
+  kHdrEOTFHDR10 = 0x4,
+  kHdrEOTFHLG = 0x8,
+};
 
 typedef std::map<HWSubBlockType, std::vector<LayerBufferFormat>> FormatsMap;
 typedef std::map<LayerBufferFormat, float> CompRatioMap;
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index b07c8a6..383bdba 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -155,7 +155,7 @@
     hw_layers_info.app_layer_count++;
   }
 
-  DLOGD_IF(kTagNone, "LayerStack layer_count: %d, app_layer_count: %d, gpu_target_index: %d, "
+  DLOGD_IF(kTagDisplay, "LayerStack layer_count: %d, app_layer_count: %d, gpu_target_index: %d, "
            "display type: %d", layers.size(), hw_layers_info.app_layer_count,
            hw_layers_info.gpu_target_index, display_type_);
 
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index 38e57d8..2c529ae 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -116,6 +116,10 @@
     device_fd_ = -1;
   }
 
+  if (stored_retire_fence >= 0) {
+    Sys::close_(stored_retire_fence);
+    stored_retire_fence = -1;
+  }
   return kErrorNone;
 }
 
diff --git a/sdm/libs/hwc2/hwc_display.cpp b/sdm/libs/hwc2/hwc_display.cpp
index 0c48b05..870b8b6 100644
--- a/sdm/libs/hwc2/hwc_display.cpp
+++ b/sdm/libs/hwc2/hwc_display.cpp
@@ -434,6 +434,8 @@
   *out_layer_id = layer->GetId();
   geometry_changes_ |= GeometryChanges::kAdded;
   validated_ = false;
+  layer_stack_invalid_ = true;
+
   return HWC2::Error::None;
 }
 
@@ -466,6 +468,8 @@
 
   geometry_changes_ |= GeometryChanges::kRemoved;
   validated_ = false;
+  layer_stack_invalid_ = true;
+
   return HWC2::Error::None;
 }
 
@@ -633,6 +637,8 @@
   }
   // set secure display
   SetSecureDisplay(secure_display_active);
+
+  layer_stack_invalid_ = false;
 }
 
 void HWCDisplay::BuildSolidFillStack() {
@@ -1214,10 +1220,6 @@
 
 
 HWC2::Error HWCDisplay::CommitLayerStack(void) {
-  if (shutdown_pending_ || layer_set_.empty()) {
-    return HWC2::Error::None;
-  }
-
   if (skip_validate_ && !CanSkipValidate()) {
     validated_ = false;
   }
@@ -1227,6 +1229,10 @@
     return HWC2::Error::NotValidated;
   }
 
+  if (shutdown_pending_ || layer_set_.empty()) {
+    return HWC2::Error::None;
+  }
+
   DumpInputBuffers();
 
   if (!flush_) {
@@ -1316,8 +1322,11 @@
       close(layer_buffer->acquire_fence_fd);
       layer_buffer->acquire_fence_fd = -1;
     }
+
+    layer->request.flags = {};
   }
 
+  client_target_->GetSDMLayer()->request.flags = {};
   *out_retire_fence = -1;
   if (!flush_) {
     // if swapinterval property is set to 0 then close and reset the list retire fence
@@ -1337,8 +1346,6 @@
   geometry_changes_ = GeometryChanges::kNone;
   flush_ = false;
 
-  ClearRequestFlags();
-
   return status;
 }
 
@@ -2016,12 +2023,6 @@
   return display_class_;
 }
 
-void HWCDisplay::ClearRequestFlags() {
-  for (Layer *layer : layer_stack_.layers) {
-    layer->request.flags = {};
-  }
-}
-
 std::string HWCDisplay::Dump() {
   std::ostringstream os;
   os << "\n------------HWC----------------\n";
@@ -2045,6 +2046,11 @@
        << std::endl;
   }
 
+  if (layer_stack_invalid_) {
+    os << "\n Layers added or removed but not reflected to SDM's layer stack yet\n";
+    return os.str();
+  }
+
   if (color_mode_) {
     os << "\n----------Color Modes---------\n";
     color_mode_->Dump(&os);
diff --git a/sdm/libs/hwc2/hwc_display.h b/sdm/libs/hwc2/hwc_display.h
index 263b69f..a5e1457 100644
--- a/sdm/libs/hwc2/hwc_display.h
+++ b/sdm/libs/hwc2/hwc_display.h
@@ -249,7 +249,6 @@
   bool IsSurfaceUpdated(const std::vector<LayerRect> &dirty_regions);
   bool IsLayerUpdating(const Layer *layer);
   uint32_t SanitizeRefreshRate(uint32_t req_refresh_rate);
-  virtual void ClearRequestFlags();
   virtual void GetUnderScanConfig() { }
 
   enum {
@@ -258,6 +257,7 @@
   };
 
   bool validated_ = false;
+  bool layer_stack_invalid_ = true;
   CoreInterface *core_intf_ = nullptr;
   HWCCallbacks *callbacks_  = nullptr;
   HWCBufferAllocator *buffer_allocator_ = NULL;
diff --git a/sdm/libs/hwc2/hwc_display_primary.cpp b/sdm/libs/hwc2/hwc_display_primary.cpp
index ee6f03e..1f2fdf6 100644
--- a/sdm/libs/hwc2/hwc_display_primary.cpp
+++ b/sdm/libs/hwc2/hwc_display_primary.cpp
@@ -207,7 +207,10 @@
   }
 
   if (layer_set_.empty()) {
-    flush_ = true;
+    // Avoid flush for Command mode panel.
+    DisplayConfigFixedInfo display_config;
+    display_intf_->GetConfig(&display_config);
+    flush_ = !display_config.is_cmdmode;
     return status;
   }
 
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 0bcffc6..d611352 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -180,12 +180,15 @@
 HWCLayer::~HWCLayer() {
   // Close any fences left for this layer
   while (!release_fences_.empty()) {
-    close(release_fences_.front());
+    ::close(release_fences_.front());
     release_fences_.pop();
   }
   if (layer_) {
     if (layer_->input_buffer.acquire_fence_fd >= 0) {
-      close(layer_->input_buffer.acquire_fence_fd);
+      ::close(layer_->input_buffer.acquire_fence_fd);
+    }
+    if (buffer_fd_ >= 0) {
+      ::close(buffer_fd_);
     }
     delete layer_;
   }
@@ -257,10 +260,14 @@
   layer_buffer->flags.secure_display = secure_display;
 
   if (layer_buffer->acquire_fence_fd >= 0) {
-    close(layer_buffer->acquire_fence_fd);
+    ::close(layer_buffer->acquire_fence_fd);
   }
   layer_buffer->acquire_fence_fd = acquire_fence;
-  layer_buffer->planes[0].fd = handle->fd;
+  if (buffer_fd_ >= 0) {
+    ::close(buffer_fd_);
+  }
+  buffer_fd_ = ::dup(handle->fd);
+  layer_buffer->planes[0].fd = buffer_fd_;
   layer_buffer->planes[0].offset = handle->offset;
   layer_buffer->planes[0].stride = UINT32(handle->width);
   layer_buffer->size = handle->size;
@@ -728,7 +735,7 @@
   float fps = 0;
   uint32_t frame_rate = layer->frame_rate;
   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
-    frame_rate = RoundToStandardFPS(fps);
+    frame_rate = (fps != 0) ? RoundToStandardFPS(fps) : layer->frame_rate;
   }
 
   int32_t interlaced = 0;
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index 5f5792f..ed03c50 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -111,6 +111,7 @@
   LayerRect dst_rect_ = {};
   bool needs_validate_ = true;
   bool single_buffer_ = false;
+  int buffer_fd_ = -1;
 
   // Composition requested by client(SF)
   HWC2::Composition client_requested_ = HWC2::Composition::Device;
diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp
index fdb8358..36b55d0 100644
--- a/sdm/libs/hwc2/hwc_session.cpp
+++ b/sdm/libs/hwc2/hwc_session.cpp
@@ -804,7 +804,7 @@
   }
 
   // Sequence locking currently begins on Validate, so cancel the sequence lock on failures
-  if (status != HWC2::Error::None) {
+  if (status != HWC2::Error::None && status != HWC2::Error::HasChanges) {
     SEQUENCE_CANCEL_SCOPE_LOCK(locker_[display]);
   }