sdm: Handle pending vsync enable after commit for Doze

Doze is being deferred to next commit, Handling of pending vsync
enable also deferred after next commit.

Change-Id: I18feaeacd85acb4c8a699aef1555c2f4a44edd65
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index ae88e42..a13445d 100755
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -435,7 +435,8 @@
 
   DLOGI_IF(kTagDisplay, "Exiting commit for display: %d-%d", display_id_, display_type_);
 
-  return kErrorNone;
+  // Handle pending vsync enable if any after the commit
+  return HandlePendingVSyncEnable(layer_stack->retire_fence_fd);
 }
 
 DisplayError DisplayBase::Flush(LayerStack *layer_stack) {
@@ -553,10 +554,9 @@
 
   // If vsync is enabled, disable vsync before power off/Doze suspend
   if (vsync_enable_ && (state == kStateOff || state == kStateDozeSuspend)) {
-    error = SetVSyncState(false);
+    error = SetVSyncState(false /* enable */);
     if (error == kErrorNone) {
-      vsync_state_change_pending_ = true;
-      requested_vsync_state_ = true;
+      vsync_enable_pending_ = true;
     }
   }
 
@@ -614,12 +614,10 @@
     comp_manager_->SetDisplayState(display_comp_ctx_, state, release_fence ? *release_fence : -1);
   }
 
-  if (vsync_state_change_pending_ && (state_ == kStateOn || state_ == kStateDoze)) {
-    error = SetVSyncState(requested_vsync_state_);
-    if (error != kErrorNone) {
-      return error;
-    }
-    vsync_state_change_pending_ = false;
+  // Handle vsync pending on resume, Since the power on commit is synchronous we pass -1 as retire
+  // fence otherwise pass valid retire fence
+  if (state_ == kStateOn) {
+    return HandlePendingVSyncEnable(-1 /* retire fence */);
   }
 
   return error;
@@ -1135,14 +1133,27 @@
   return error;
 }
 
+DisplayError DisplayBase::HandlePendingVSyncEnable(int32_t retire_fence) {
+  if (vsync_enable_pending_) {
+    // Retire fence signalling confirms that CRTC enabled, hence wait for retire fence before
+    // we enable vsync
+    buffer_sync_handler_->SyncWait(retire_fence);
+
+    DisplayError error = SetVSyncState(true /* enable */);
+    if (error != kErrorNone) {
+      return error;
+    }
+    vsync_enable_pending_ = false;
+  }
+  return kErrorNone;
+}
+
 DisplayError DisplayBase::SetVSyncState(bool enable) {
   lock_guard<recursive_mutex> obj(recursive_mutex_);
-  if (state_ == kStateOff) {
-    DLOGW("Can't %s vsync when power state is off for display %d-%d," \
-          "Defer it when display is active", enable ? "enable":"disable",
-          display_id_, display_type_);
-    vsync_state_change_pending_ = true;
-    requested_vsync_state_ = enable;
+  if (state_ == kStateOff && enable) {
+    DLOGW("Can't enable vsync when power state is off for display %d-%d," \
+          "Defer it when display is active", display_id_, display_type_);
+    vsync_enable_pending_ = true;
     return kErrorNone;
   }
   DisplayError error = kErrorNone;
@@ -1159,6 +1170,7 @@
       vsync_enable_ = enable;
     }
   }
+  vsync_enable_pending_ = !enable ? false : vsync_enable_pending_;
 
   return error;
 }
diff --git a/sdm/libs/core/display_base.h b/sdm/libs/core/display_base.h
index eb1c045..ea407bb 100644
--- a/sdm/libs/core/display_base.h
+++ b/sdm/libs/core/display_base.h
@@ -183,6 +183,7 @@
   PrimariesTransfer GetBlendSpaceFromColorMode();
   bool IsHdrMode(const AttrVal &attr);
   void InsertBT2020PqHlgModes();
+  DisplayError HandlePendingVSyncEnable(int32_t retire_fence);
 
   recursive_mutex recursive_mutex_;
   int32_t display_id_ = -1;
@@ -233,9 +234,7 @@
   bool drop_skewed_vsync_ = false;
   bool custom_mixer_resolution_ = false;
   DisplayState power_state_pending_ = kStateOff;
-  bool vsync_state_change_pending_ = false;
-  // requested_vsync_state: true -> enable vsync, false -> disable vsync
-  bool requested_vsync_state_ = false;
+  bool vsync_enable_pending_ = false;
   bool defer_power_state_ = false;
   QSyncMode qsync_mode_ = kQSyncModeNone;
   bool needs_avr_update_ = false;