Merge "gralloc: Fix integer overflow in roundUpToPageSize"
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 870c1b5..555930f 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -2798,10 +2798,13 @@
 }
 
 android::status_t HWCSession::SetDsiClk(const android::Parcel *input_parcel) {
-  int disp_id = input_parcel->readInt32();
+  uint32_t disp_id = UINT32(input_parcel->readInt32());
   uint64_t clk = UINT64(input_parcel->readInt64());
   if (disp_id != HWC_DISPLAY_PRIMARY) {
-    return -EINVAL;
+    if (!std::any_of(map_info_builtin_.begin(), map_info_builtin_.end(),
+                    [&disp_id](auto &i) {return disp_id == i.client_id;})) {
+                      return -EINVAL;
+                   }
   }
 
   SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_id]);
@@ -4270,10 +4273,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 +4457,6 @@
       DLOGW("Target display %d is not ready", disp_id);
       return -ENODEV;
     }
-    tui_state_transition_[disp_id] = true;
   }
 
   return 0;
@@ -4474,11 +4476,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 +4510,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/init/init.qti.display_boot.sh b/init/init.qti.display_boot.sh
index bf65e8f..5baeadd 100644
--- a/init/init.qti.display_boot.sh
+++ b/init/init.qti.display_boot.sh
@@ -57,8 +57,10 @@
     ;;
     "kalama")
     #SOC ID for Kalama is 519
+    #SOC ID for Kalama SG36 is 600
+    #SOC ID for Kalama SG p is 601
     case "$soc_hwid" in
-      519)
+        519|600|601)
         setprop vendor.display.enable_fb_scaling 0
         setprop vendor.display.target.version 4
         setprop vendor.gralloc.use_dma_buf_heaps 1
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 1d03686..75aada6 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -2540,6 +2540,9 @@
   }
 
   LayerRect &roi = cwb_config->cwb_roi;
+  // Set cwb full rect as per window rect.
+  cwb_config->cwb_full_rect.right -= (window_rect_.left + window_rect_.right);
+  cwb_config->cwb_full_rect.bottom -= (window_rect_.top + window_rect_.bottom);
   LayerRect &full_frame = cwb_config->cwb_full_rect;
   uint32_t cwb_roi_supported = 0;  // Check whether CWB ROI is supported.
   IsSupportedOnDisplay(kCwbCrop, &cwb_roi_supported);
@@ -2581,7 +2584,8 @@
     DLOGI_IF(kTagDisplay, "Client provided invalid ROI. Going for Full frame CWB.");
     roi = full_frame;
   }
-
+  // Reposition CWB ROI as per window rect.
+  roi = Reposition(roi, window_rect_.left, window_rect_.top);
   DLOGI_IF(kTagDisplay, "Cwb_config: tap_point %d, CWB ROI Rect(%f %f %f %f), PU_as_CWB_ROI %d",
            tap_point, roi.left, roi.top, roi.right, roi.bottom, pu_as_cwb_roi);
 
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) {