Merge "gralloc: Validate heap assign failure while buffer allocation"
diff --git a/composer/hwc_display.cpp b/composer/hwc_display.cpp
index b992a04..8b3c97c 100755
--- a/composer/hwc_display.cpp
+++ b/composer/hwc_display.cpp
@@ -3151,6 +3151,9 @@
 DisplayError HWCDisplay::PostHandleSecureEvent(SecureEvent secure_event) {
   DisplayError err = display_intf_->PostHandleSecureEvent(secure_event);
   if (err == kErrorNone) {
+    if (secure_event == kTUITransitionEnd || secure_event == kTUITransitionUnPrepare) {
+      return kErrorNone;
+    }
     secure_event_ = secure_event;
   }
   return err;
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 870c1b5..0e65788 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -4270,10 +4270,10 @@
 int HWCSession::WaitForCommitDone(hwc2_display_t display, int client_id) {
   shared_ptr<Fence> retire_fence = nullptr;
   int timeout_ms = -1;
+  callbacks_.Refresh(display);
   {
     SEQUENCE_WAIT_SCOPE_LOCK(locker_[display]);
     DLOGI("Acquired lock for client %d display %" PRIu64, client_id, display);
-    callbacks_.Refresh(display);
     clients_waiting_for_commit_[display].set(client_id);
     locker_[display].Wait();
     if (commit_error_[display] != 0) {
@@ -4454,7 +4454,6 @@
       DLOGW("Target display %d is not ready", disp_id);
       return -ENODEV;
     }
-    tui_state_transition_[disp_id] = true;
   }
 
   return 0;
@@ -4474,11 +4473,6 @@
     return -ENOTSUP;
   }
 
-  if (!tui_state_transition_[disp_id]) {
-    DLOGE("Display %d tui transition state is not valid.", disp_id);
-    return -EINVAL;
-  }
-
   {
     SEQUENCE_WAIT_SCOPE_LOCK(locker_[target_display]);
     hwc_display_[target_display]->SetIdleTimeoutMs(idle_time_active_ms_, idle_time_inactive_ms_);
@@ -4513,7 +4507,6 @@
       DLOGW("Target display %d is not ready", disp_id);
       return -ENODEV;
     }
-    tui_state_transition_[disp_id] = false;
   }
 
   return TUITransitionUnPrepare(disp_id);
diff --git a/composer/hwc_session.h b/composer/hwc_session.h
index bfd72e8..658ef21 100644
--- a/composer/hwc_session.h
+++ b/composer/hwc_session.h
@@ -720,7 +720,6 @@
   bool async_power_mode_triggered_ = false;
   bool async_vds_creation_ = false;
   bool power_state_transition_[HWCCallbacks::kNumDisplays] = {};
-  bool tui_state_transition_[HWCCallbacks::kNumDisplays] = {};
   std::bitset<HWCCallbacks::kNumDisplays> display_ready_;
   bool secure_session_active_ = false;
   bool is_client_up_ = false;
diff --git a/include/display_properties.h b/include/display_properties.h
index cfc1508..95deb7a 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")
@@ -160,6 +162,7 @@
 #define ANTI_AGING_RECALIB_TIMER             DISPLAY_PROP("demura_recalib_timer")
 #define ANTI_AGING_RECORD_TIMER              DISPLAY_PROP("demura_record_timer")
 #define ANTI_AGING_IDLE_TIMER                DISPLAY_PROP("demura_idle_timer")
+#define ANTI_AGING_MEMORY_SIZE               DISPLAY_PROP("demura_memory_size")
 
 // PERF hint properties
 #define ENABLE_PERF_HINT_LARGE_COMP_CYCLE    DISPLAY_PROP("enable_perf_hint_large_comp_cycle")
diff --git a/sdm/include/private/demuratn_core_uvm_intf.h b/sdm/include/private/demuratn_core_uvm_intf.h
index 1639ea7..78b44cd 100644
--- a/sdm/include/private/demuratn_core_uvm_intf.h
+++ b/sdm/include/private/demuratn_core_uvm_intf.h
@@ -11,8 +11,15 @@
 
 namespace sdm {
 
+enum DemuraTnCoreState {
+  kDemuraTnCoreNotReady,
+  kDemuraTnCoreReady,
+  kDemuraTnCoreError,
+  kDemuraTnCoreStateMax,
+};
+
 enum DemuraTnCoreUvmParams {
-  /* Getter: bool */
+  /* Getter: DemuraTnCoreState */
   kDemuraTnCoreUvmParamInitReady,
   /* Getter/Setter: bool */
   kDemuraTnCoreUvmParamEnable,
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..5402d8e 100644
--- a/sdm/libs/core/display_builtin.cpp
+++ b/sdm/libs/core/display_builtin.cpp
@@ -778,9 +778,9 @@
   *en = enable;
 
   if (enable) {  // make sure init is ready before enabling
-    bool *init_ready = nullptr;
+    DemuraTnCoreState *init_ready = nullptr;
     GenericPayload ready_pl;
-    ret = ready_pl.CreatePayload<bool>(init_ready);
+    ret = ready_pl.CreatePayload<DemuraTnCoreState>(init_ready);
     if (ret) {
       DLOGE("failed to create the payload. Error:%d", ret);
       return kErrorUndefined;
@@ -791,16 +791,25 @@
       DLOGE("GetParameter for InitReady failed ret %d", ret);
       return kErrorUndefined;
     }
-    if (!(*init_ready)) {
+    if (*init_ready == kDemuraTnCoreNotReady) {
       return kErrorNone;
-    }
-
-    ret = demuratn_->SetParameter(kDemuraTnCoreUvmParamEnable, payload);
-    if (ret) {
-      DLOGE("SetParameter for enable failed ret %d", ret);
+    } else if (*init_ready == kDemuraTnCoreError) {
+      DLOGE("DemuraTn init ready state returns error");
+      int rc = demuratn_->Deinit();
+      if (rc)
+        DLOGE("Failed to deinit DemuraTn ret %d", rc);
+      demuratn_factory_ = nullptr;
+      demuratn_.reset();
+      demuratn_ = nullptr;
       return kErrorUndefined;
+    } else if (*init_ready == kDemuraTnCoreReady) {
+      ret = demuratn_->SetParameter(kDemuraTnCoreUvmParamEnable, payload);
+      if (ret) {
+        DLOGE("SetParameter for enable failed ret %d", ret);
+        return kErrorUndefined;
+      }
+      demuratn_enabled_ = true;
     }
-    demuratn_enabled_ = true;
   } else {
     ret = demuratn_->SetParameter(kDemuraTnCoreUvmParamEnable, payload);
     if (ret) {
@@ -2394,7 +2403,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_;
 };