Merge "sdm: Adjust CWB ROI as per Window rect"
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/gralloc/gr_allocator.cpp b/gralloc/gr_allocator.cpp
index feda5b5..b9c3491 100644
--- a/gralloc/gr_allocator.cpp
+++ b/gralloc/gr_allocator.cpp
@@ -62,7 +62,6 @@
 
 int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
   int ret;
-  int err = 0;
   bool is_secure = false;
   alloc_data->uncached = UseUncached(format, usage);
 
@@ -88,15 +87,16 @@
   } else {
     ALOGE("%s: Failed to allocate buffer - heap name: %s flags: 0x%x ret: %d", __FUNCTION__,
           alloc_data->heap_name.c_str(), alloc_data->flags, ret);
+    return ret;
   }
 
   if (!alloc_data->vm_names.empty()) {
-    err = alloc_intf->SecureMemPerms(alloc_data);
+    ret = alloc_intf->SecureMemPerms(alloc_data);
   }
 
-  if (err) {
+  if (ret) {
     ALOGE("%s: Failed to modify secure use permissions - heap name: %s flags: 0x%x, err: %d",
-          __FUNCTION__, alloc_data->heap_name.c_str(), alloc_data->flags, err);
+          __FUNCTION__, alloc_data->heap_name.c_str(), alloc_data->flags, ret);
   }
 
   return ret;
diff --git a/include/display_properties.h b/include/display_properties.h
index d948fe5..95deb7a 100644
--- a/include/display_properties.h
+++ b/include/display_properties.h
@@ -162,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_builtin.cpp b/sdm/libs/core/display_builtin.cpp
index 914b0a9..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) {