Merge "composer: Handle CWB frame dump on Power Down or ESD"
diff --git a/composer/hwc_display.cpp b/composer/hwc_display.cpp
index f745756..1db3c93 100644
--- a/composer/hwc_display.cpp
+++ b/composer/hwc_display.cpp
@@ -3126,6 +3126,10 @@
   if (secure_event == kTUITransitionEnd || secure_event == kTUITransitionUnPrepare) {
     DLOGI("Resume display %d-%d",  sdm_id_, type_);
     display_paused_ = false;
+    if (*needs_refresh == false) {
+      secure_event_ = kSecureEventMax;
+      return kErrorNone;
+    }
   } else if (secure_event == kTUITransitionPrepare || secure_event == kTUITransitionStart) {
     if (*needs_refresh) {
       display_pause_pending_ = true;
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 17e4c78..a5b1c1b 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -3234,10 +3234,6 @@
     available_mixer_count = hwc_display_[active_builtin]->GetAvailableMixerCount();
   }
 
-  if (available_mixer_count < min_mixer_count) {
-    return -EAGAIN;
-  }
-
   for (auto &iter : *hw_displays_info) {
     auto &info = iter.second;
 
@@ -3256,6 +3252,12 @@
       continue;
     }
 
+    if (available_mixer_count < min_mixer_count) {
+      DLOGI("mixers not available: available: %d, min: %d",
+            available_mixer_count, min_mixer_count);
+      return -EAGAIN;
+    }
+
     // Count active pluggable display slots and slots with no commits.
     bool first_commit_pending = false;
     std::for_each(map_info_pluggable_.begin(), map_info_pluggable_.end(),
@@ -3734,13 +3736,17 @@
     return;
   }
 
+  static constexpr uint32_t min_mixer_count = 2;
+  uint32_t available_mixer_count = 0;
   std :: bitset < kSecureMax > secure_sessions = 0;
   if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
     Locker::ScopeLock lock_d(locker_[active_builtin_disp_id]);
     hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
+    available_mixer_count = hwc_display_[active_builtin_disp_id]->GetAvailableMixerCount();
   }
 
-  if (secure_sessions.any() || active_builtin_disp_id >= HWCCallbacks::kNumDisplays) {
+  if (secure_sessions.any() || active_builtin_disp_id >= HWCCallbacks::kNumDisplays ||
+      available_mixer_count < min_mixer_count) {
     return;
   }
 
diff --git a/composer/hwc_session.h b/composer/hwc_session.h
index 0702267..96bd91c 100644
--- a/composer/hwc_session.h
+++ b/composer/hwc_session.h
@@ -434,7 +434,7 @@
     };
 
     struct DisplayCWBSession{
-      std::deque<QueueNode *> queue;
+      std::deque<std::shared_ptr<QueueNode>> queue;
       std::mutex lock;
       std::condition_variable cv;
       std::future<void> future;
@@ -443,7 +443,7 @@
 
     static void AsyncTaskToProcessCWBStatus(CWB *cwb, hwc2_display_t display_type);
     void ProcessCWBStatus(hwc2_display_t display_type);
-    void NotifyCWBStatus(int status, QueueNode *cwb_node);
+    void NotifyCWBStatus(int status, std::shared_ptr<QueueNode> cwb_node);
 
     std::map<hwc2_display_t, DisplayCWBSession> display_cwb_session_map_;
     HWCSession *hwc_session_ = nullptr;
diff --git a/composer/hwc_session_services.cpp b/composer/hwc_session_services.cpp
index e507be3..12ad1ad 100644
--- a/composer/hwc_session_services.cpp
+++ b/composer/hwc_session_services.cpp
@@ -1149,7 +1149,7 @@
                                     hwc2_display_t display_type) {
   HWC2::Error error = HWC2::Error::None;
   auto& session_map = display_cwb_session_map_[display_type];
-  QueueNode *node = nullptr;
+  std::shared_ptr<QueueNode> node = nullptr;
   uint64_t node_handle_id = 0;
   void *hdl = const_cast<native_handle_t *>(buffer);
   auto err = gralloc::GetMetaDataValue(hdl, (int64_t)StandardMetadataType::BUFFER_ID,
@@ -1160,15 +1160,29 @@
   }
 
   if (error == HWC2::Error::None) {
-    node = new QueueNode(callback, cwb_config, buffer, display_type, node_handle_id);
+    node = std::make_shared<QueueNode>(callback, cwb_config, buffer, display_type, node_handle_id);
     if (node) {
       // Keep CWB request handling related resources in a requested display context.
       std::unique_lock<std::mutex> lock(session_map.lock);
+
+      // Iterate over the queue to avoid duplicate node of same buffer, because that
+      // buffer is already present in queue.
+      for (auto& qnode : session_map.queue) {
+        if (qnode->handle_id == node_handle_id) {
+          error = HWC2::Error::BadParameter;
+          DLOGW("CWB Buffer with handle id %lu is already available in Queue for processing!",
+                node_handle_id);
+          break;
+        }
+      }
+
       // Ensure that async task runs only until all queued CWB requests have been fulfilled.
       // If cwb queue is empty, async task has not either started or async task has finished
       // processing previously queued cwb requests. Start new async task on such a case as
       // currently running async task will automatically desolve without processing more requests.
-      session_map.queue.push_back(node);
+      if (error == HWC2::Error::None) {
+        session_map.queue.push_back(node);
+      }
     } else {
       error = HWC2::Error::BadParameter;
       DLOGE("Unable to allocate node for CWB request(handle id: %lu)!", node_handle_id);
@@ -1242,8 +1256,10 @@
           session_map.cv.notify_one();
           return 0;
         } else {
-          //Just skip notifying to client on not matching handle_id.
-          return -1;
+          // Continue to check on not matching handle_id, to update the status of any matching
+          // node, because if notification for particular handle_id skip, then it will not update
+          // again and notification thread will wait for skipped node forever.
+          continue;
         }
       }
     }
@@ -1259,7 +1275,7 @@
 void HWCSession::CWB::ProcessCWBStatus(hwc2_display_t display_type) {
   auto& session_map = display_cwb_session_map_[display_type];
   while(true) {
-    QueueNode *cwb_node = nullptr;
+    std::shared_ptr<QueueNode> cwb_node = nullptr;
     {
       std::unique_lock<std::mutex> lock(session_map.lock);
       // Exit thread in case of no pending CWB request in queue.
@@ -1276,6 +1292,13 @@
       } else if (cwb_node->notified_status == kCwbNotifiedNone) {
         // Wait for the signal for availability of CWB notified node.
         session_map.cv.wait(lock);
+        if (cwb_node->notified_status == kCwbNotifiedNone) {
+          // If any other node notified before front node, then need to continue to wait
+          // for front node, such that further client notification will be done in sequential
+          // manner.
+          DLOGW("CWB request is notified out of sequence.");
+          continue;
+        }
       }
       session_map.queue.pop_front();
     }
@@ -1283,9 +1306,10 @@
     // Notify to client, when notification is received successfully for expected input buffer.
     NotifyCWBStatus(cwb_node->notified_status , cwb_node);
   }
+  DLOGI("CWB queue is empty. Display: %d", display_type);
 }
 
-void HWCSession::CWB::NotifyCWBStatus(int status, QueueNode *cwb_node) {
+void HWCSession::CWB::NotifyCWBStatus(int status, std::shared_ptr<QueueNode> cwb_node) {
   // Notify client about buffer status and erase the node from pending request queue.
   std::shared_ptr<DisplayConfig::ConfigCallback> callback = cwb_node->callback.lock();
   if (callback) {
@@ -1295,7 +1319,6 @@
 
   native_handle_close(cwb_node->buffer);
   native_handle_delete(const_cast<native_handle_t *>(cwb_node->buffer));
-  delete cwb_node;
 }
 
 int HWCSession::NotifyCwbDone(hwc2_display_t display, int32_t status, uint64_t handle_id) {
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index 03dba0e..ee063c0 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -3837,6 +3837,9 @@
         return err;
       }
     }
+    if (state == kStateOff) {
+      *needs_refresh = false;
+    }
   }
   if (*needs_refresh) {
     validated_ = false;