Merge "composer: Prevents TUI Transition state change in case of End/Unprepare"
diff --git a/include/display_properties.h b/include/display_properties.h
index cfc1508..d948fe5 100644
--- a/include/display_properties.h
+++ b/include/display_properties.h
@@ -133,6 +133,8 @@
 // Disable SDR dimming support
 #define DISABLE_SDR_DIMMING                  DISPLAY_PROP("disable_sdr_dimming")
 #define FORCE_TONEMAPPING                    DISPLAY_PROP("force_tonemapping")
+// Allows color management(tonemapping) in native mode (native mode is considered BT709+sRGB)
+#define ALLOW_TONEMAP_NATIVE                 DISPLAY_PROP("allow_tonemap_native")
 
 // RC
 #define ENABLE_ROUNDED_CORNER                DISPLAY_PROP("enable_rounded_corner")
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index f14d2cb..1d03686 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -217,6 +217,10 @@
   if (Debug::Get()->GetProperty(DISABLE_LLCC_DURING_AOD, &prop) == kErrorNone) {
     disable_llcc_during_aod_ = (prop == 1);
   }
+  prop = 0;
+  if (Debug::Get()->GetProperty(ALLOW_TONEMAP_NATIVE, &prop) == kErrorNone) {
+    allow_tonemap_native_ = (prop == 1);
+  }
 
   SetupPanelFeatureFactory();
 
@@ -3576,7 +3580,8 @@
   } else if (color_gamut == kDcip3) {
     pt.primaries = GetColorPrimariesFromAttribute(color_gamut);
     pt.transfer = Transfer_sRGB;
-  } else if (color_gamut == kNative) {
+  } else if (color_gamut == kNative && !allow_tonemap_native_) {
+    // if allow_tonemap_native_ is set, blend space is defaulted to BT709 + sRGB
     pt.primaries = GetColorPrimariesFromAttribute(color_gamut);
     pt.transfer = Transfer_Max;
   }
diff --git a/sdm/libs/core/display_base.h b/sdm/libs/core/display_base.h
index 3b54419..e53c4ca 100644
--- a/sdm/libs/core/display_base.h
+++ b/sdm/libs/core/display_base.h
@@ -431,6 +431,7 @@
   bool pending_commit_ = false;
   uint32_t active_refresh_rate_ = 0;
   bool disable_cwb_idle_fallback_ = false;
+  bool allow_tonemap_native_ = false;
 
  private:
   // Max tolerable power-state-change wait-times in milliseconds.
diff --git a/sdm/libs/core/display_builtin.cpp b/sdm/libs/core/display_builtin.cpp
index 2a4bc76..914b0a9 100644
--- a/sdm/libs/core/display_builtin.cpp
+++ b/sdm/libs/core/display_builtin.cpp
@@ -2394,7 +2394,9 @@
   }
 
   // Set sRGB as default blend space.
-  if (stc_color_modes_.list.empty()) {
+  bool native_mode = (color_mode.intent == snapdragoncolor::kNative) ||
+                     (color_mode.gamut == ColorPrimaries_Max && color_mode.gamma == Transfer_Max);
+  if (stc_color_modes_.list.empty() || (native_mode && allow_tonemap_native_)) {
     return blend_space;
   }
 
diff --git a/sdm/libs/dal/hw_device_drm.cpp b/sdm/libs/dal/hw_device_drm.cpp
index c70ff12..09b28ec 100644
--- a/sdm/libs/dal/hw_device_drm.cpp
+++ b/sdm/libs/dal/hw_device_drm.cpp
@@ -2024,7 +2024,8 @@
   // for other metadata types we will run into issues.
   bool extended_md_present = input_buffer.extended_content_metadata != nullptr &&
                               input_buffer.extended_content_metadata->size;
-  if (extended_md_present && input_buffer.color_metadata.transfer == Transfer_SMPTE_170M) {
+  if (extended_md_present && (input_buffer.color_metadata.transfer == Transfer_SMPTE_170M
+                          || input_buffer.color_metadata.transfer == Transfer_sRGB)) {
       *type = DRMCscType::kCscYuv2RgbDolbyVisionP5;
       return;
   }
diff --git a/services/config/src/device_impl.cpp b/services/config/src/device_impl.cpp
index 8c32a89..39bb02d 100644
--- a/services/config/src/device_impl.cpp
+++ b/services/config/src/device_impl.cpp
@@ -146,17 +146,9 @@
 
 void DeviceImpl::serviceDied(uint64_t client_handle,
                              const android::wp<::android::hidl::base::V1_0::IBase>& callback) {
-  std::lock_guard<std::shared_mutex> exclusive_lock(shared_mutex_);
   std::lock_guard<std::recursive_mutex> lock(death_service_mutex_);
-  auto itr = display_config_map_.find(client_handle);
-  std::shared_ptr<DeviceClientContext> client = itr->second;
-  if (client != NULL) {
-    ConfigInterface *intf = client->GetDeviceConfigIntf();
-    intf_->UnRegisterClientContext(intf);
-    client.reset();
+  pending_display_config_.push_back(client_handle);
     ALOGW("Client id:%lu service died", client_handle);
-    display_config_map_.erase(itr);
-  }
 }
 
 DeviceImpl::DeviceClientContext::DeviceClientContext(
@@ -899,12 +891,24 @@
 Return<void> DeviceImpl::perform(uint64_t client_handle, uint32_t op_code,
                                  const ByteStream &input_params, const HandleStream &input_handles,
                                  perform_cb _hidl_cb) {
-  std::shared_lock<std::shared_mutex> shared_lock(shared_mutex_);
   int32_t error = 0;
   std::shared_ptr<DeviceClientContext> client = nullptr;
 
   {
     std::lock_guard<std::recursive_mutex> lock(death_service_mutex_);
+    for (auto& pending_client_handle : pending_display_config_) {
+      auto itr = display_config_map_.find(pending_client_handle);
+      std::shared_ptr<DeviceClientContext> pending_client = itr->second;
+      if (pending_client != NULL) {
+        ConfigInterface *pending_intf = pending_client->GetDeviceConfigIntf();
+        intf_->UnRegisterClientContext(pending_intf);
+        pending_client.reset();
+        ALOGI("clear old client id:%lu ", pending_client_handle);
+      }
+      display_config_map_.erase(itr);
+    }
+    pending_display_config_.clear();
+
     auto itr = display_config_map_.find(client_handle);
     if (itr == display_config_map_.end()) {
       error = -EINVAL;
diff --git a/services/config/src/device_impl.h b/services/config/src/device_impl.h
index c89be31..dd0e536 100644
--- a/services/config/src/device_impl.h
+++ b/services/config/src/device_impl.h
@@ -179,9 +179,9 @@
 
   ClientContext *intf_ = nullptr;
   std::map<uint64_t, std::shared_ptr<DeviceClientContext>> display_config_map_;
+  std::vector<uint64_t> pending_display_config_;
   uint64_t client_id_ = 0;
   std::recursive_mutex death_service_mutex_;
-  std::shared_mutex shared_mutex_;
   static DeviceImpl *device_obj_;
   static std::mutex device_lock_;
 };