camera: intel: ipu6: Fix flush timeout issue when close camera

[Issues]
Camera error happens after switch camera multiple times

[Root Cause]
Flush request timeout when close camera, it causes open it
failed in next time

[Changes]
1. Don't wait sof event if thread exit
2. Handle the latest frame if lose sof event for long time
3. Clear the buffer pool in executor unit to make sure don't destory
   user buffer in psys

BUG=b:149068439, b:149068672
TEST=Full tested pass for camera functions.

Change-Id: I92d3b92c79f764019eb2019be4498a4ce9fe2773
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2600369
Tested-by: Zong Li <zong.li@intel.com>
Reviewed-by: Ren-Pei Zeng <kamesan@chromium.org>
Commit-Queue: Ren-Pei Zeng <kamesan@chromium.org>
diff --git a/camera/hal/intel/ipu6/src/core/PSysProcessor.cpp b/camera/hal/intel/ipu6/src/core/PSysProcessor.cpp
index a34c85c..b165209 100644
--- a/camera/hal/intel/ipu6/src/core/PSysProcessor.cpp
+++ b/camera/hal/intel/ipu6/src/core/PSysProcessor.cpp
@@ -475,17 +475,17 @@
         ret = prepareTask(&srcBuffers, &dstBuffers);
         CheckError(ret != OK, UNKNOWN_ERROR, "%s, Failed to process frame", __func__);
     } else {
+        timeval curTime;
+        int64_t sofInterval = 0;
         {
             ConditionLock lock(mSofLock);
 
-            timeval curTime;
             gettimeofday(&curTime, nullptr);
-            int64_t sofInterval = TIMEVAL2NSECS(curTime) - TIMEVAL2NSECS(mSofTimestamp);
-
+            sofInterval = TIMEVAL2NSECS(curTime) - TIMEVAL2NSECS(mSofTimestamp);
             // Wait next sof event when missing last one for a long time
             if (sofInterval > SOF_EVENT_MARGIN && sofInterval < SOF_EVENT_MAX_MARGIN) {
                 LOG2("%s, need to wait next sof event. sofInterval: %ld", __func__, sofInterval);
-                ret = mSofCondition.waitRelative(lock, kWaitDuration * SLOWLY_MULTIPLIER);
+                ret = mSofCondition.waitRelative(lock, SOF_EVENT_MAX_MARGIN * SLOWLY_MULTIPLIER);
 
                 // Already stopped
                 if (!mThreadRunning) return -1;
@@ -504,20 +504,25 @@
                 ConditionLock lock(mBufferQueueLock);
                 ret = waitFreeBuffersInQueue(lock, srcBuffers, dstBuffers, SOF_EVENT_MARGIN);
 
+                // Already stopped
+                if (!mThreadRunning) return -1;
+
                 // Return to wait next sof event if there isn't pending buffer.
-                if (ret != OK) {
-                    LOG1("%s, cameraId: %d, there isn't pending buffer, recovery",
-                         __func__, mCameraId);
-                    return OK;
-                }
+                if (ret != OK) return OK;
             }
 
             {
                 AutoMutex l(mSofLock);
                 if (srcBuffers.begin()->second->getSequence() >= mSofSequence) {
-                    LOG2("%s, run the frame in next sof: buffer sequence: %ld, sof sequence: %ld",
-                         __func__, srcBuffers.begin()->second->getSequence(), mSofSequence);
-                    return OK;
+                    gettimeofday(&curTime, nullptr);
+                    sofInterval = TIMEVAL2NSECS(curTime) - TIMEVAL2NSECS(mSofTimestamp);
+
+                    // Handle the frame of sof(N) on sof(N + 1) when the sof event is continuously
+                    if (sofInterval < SOF_EVENT_MAX_MARGIN) {
+                        return OK;
+                    }
+                    LOG2("%s, sof event lost for long time, skip wating. sofInterval: %ld",
+                         __func__, sofInterval);
                 }
             }
 
diff --git a/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.cpp b/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.cpp
index c7d5f7d..9a260a8 100644
--- a/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.cpp
+++ b/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.cpp
@@ -398,6 +398,12 @@
     // Thread is not running. It is safe to clear the Queue
     clearBufferQueues();
     delete mProcessThread;
+
+    // Clear the buffer pool of pg Uint
+    for (auto& unit : mPGExecutors) {
+        unit.inputBuffers.clear();
+        unit.outputBuffers.clear();
+    }
 }
 
 void PipeLiteExecutor::notifyStop()
diff --git a/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.h b/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.h
index 16452c6..fa38a39 100644
--- a/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.h
+++ b/camera/hal/intel/ipu6/src/core/psysprocessor/PipeLiteExecutor.h
@@ -117,6 +117,8 @@
         // Initialized during buffer allocation
         std::map<ia_uid, std::shared_ptr<CameraBuffer>> inputBuffers;
         std::map<ia_uid, std::shared_ptr<CameraBuffer>> outputBuffers;
+
+        ExecutorUnit() { pgId = -1; stageId = 0; }
     };
 
  protected: