[libcamera-devel,v2,7/7] android: function flush() waits for all requests completed before return
diff mbox series

Message ID 20220812090838.1784703-8-hanlinchen@chromium.org
State New
Headers show
Series
  • Implement Android Partial Result Featrue
Related show

Commit Message

Hanlin Chen Aug. 12, 2022, 9:08 a.m. UTC
Android requires that flush() only returns when there are no more outstanding
buffers or requests left in the HAL. Add a contition variable to wait for all
pending requests are completed before returning the call.

Signed-off-by: Han-Lin Chen <hanlinchen@chromium.org>
---
 src/android/camera_device.cpp | 23 ++++++++++++++++++++---
 src/android/camera_device.h   |  2 ++
 2 files changed, 22 insertions(+), 3 deletions(-)

Patch
diff mbox series

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index c999b198..2672a5eb 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -428,8 +428,20 @@  void CameraDevice::flush()
 
 	camera_->stop();
 
-	MutexLocker stateLock(stateMutex_);
-	state_ = State::Stopped;
+	{
+		MutexLocker locker(pendingRequestMutex_);
+		pendingRequestsCv_.wait(
+			locker,
+			[&]() LIBCAMERA_TSA_REQUIRES(pendingRequestMutex_) {
+				return pendingRequests_.empty();
+			});
+		ASSERT(pendingRequests_.empty());
+	}
+
+	{
+		MutexLocker stateLock(stateMutex_);
+		state_ = State::Stopped;
+	}
 }
 
 void CameraDevice::stop()
@@ -1480,7 +1492,6 @@  void CameraDevice::completeResultDescriptor(Camera3ResultDescriptor *result)
 	completeRequestDescriptor(result->request_);
 }
 
-
 /**
  * \brief Complete the Camera3RequestDescriptor
  * \param[in] descriptor The Camera3RequestDescriptor
@@ -1526,6 +1537,12 @@  void CameraDevice::completeRequestDescriptor(Camera3RequestDescriptor *request)
 
 		pendingRequests_.pop_front();
 	}
+
+	if (pendingRequests_.empty()) {
+		locker.unlock();
+		pendingRequestsCv_.notify_one();
+		return;
+	}
 }
 
 void CameraDevice::setBufferStatus(StreamBuffer &streamBuffer,
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index 613e419f..aa976db2 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -135,6 +135,8 @@  private:
 	std::map<CameraStream *, std::list<StreamBuffer *>> pendingStreamBuffers_
 		LIBCAMERA_TSA_GUARDED_BY(pendingRequestMutex_);
 
+	libcamera::ConditionVariable pendingRequestsCv_;
+
 	std::string maker_;
 	std::string model_;