[libcamera-devel,v2,3/5] libcamera: pipeline: rkisp1: Add internal request queue
diff mbox series

Message ID 20210831223705.1928000-4-nfraprado@collabora.com
State New
Headers show
Series
  • libcamera: pipeline: Add internal request queue
Related show

Commit Message

NĂ­colas F. R. A. Prado Aug. 31, 2021, 10:37 p.m. UTC
Add an internal queue that stores requests until there are internal
buffers and V4L2 buffer slots available. This avoids the need to cancel
requests when there is a shortage of said buffers.

Signed-off-by: NĂ­colas F. R. A. Prado <nfraprado@collabora.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

---

Changes in v2:
- Moved cancellation of pending requests to after video devices stop

 src/libcamera/pipeline/rkisp1/rkisp1.cpp | 73 +++++++++++++++++++-----
 1 file changed, 59 insertions(+), 14 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
index 03a8d1d26e30..6aca4083af72 100644
--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
@@ -91,6 +91,9 @@  public:
 	PipelineHandlerRkISP1 *pipe();
 	int loadIPA(unsigned int hwRevision);
 
+	void queuePendingRequests();
+	void cancelPendingRequests();
+
 	Stream mainPathStream_;
 	Stream selfPathStream_;
 	std::unique_ptr<CameraSensor> sensor_;
@@ -104,6 +107,8 @@  public:
 
 	std::unique_ptr<ipa::rkisp1::IPAProxyRkISP1> ipa_;
 
+	std::queue<Request *> pendingRequests_;
+
 private:
 	void queueFrameAction(unsigned int frame,
 			      const ipa::rkisp1::RkISP1Action &action);
@@ -210,13 +215,13 @@  RkISP1FrameInfo *RkISP1Frames::create(const RkISP1CameraData *data, Request *req
 	unsigned int frame = data->frame_;
 
 	if (pipe_->availableParamBuffers_.empty()) {
-		LOG(RkISP1, Error) << "Parameters buffer underrun";
+		LOG(RkISP1, Debug) << "Parameters buffer underrun";
 		return nullptr;
 	}
 	FrameBuffer *paramBuffer = pipe_->availableParamBuffers_.front();
 
 	if (pipe_->availableStatBuffers_.empty()) {
-		LOG(RkISP1, Error) << "Statisitc buffer underrun";
+		LOG(RkISP1, Debug) << "Statistic buffer underrun";
 		return nullptr;
 	}
 	FrameBuffer *statBuffer = pipe_->availableStatBuffers_.front();
@@ -386,6 +391,52 @@  void RkISP1CameraData::metadataReady(unsigned int frame, const ControlList &meta
 	pipe()->tryCompleteRequest(info->request);
 }
 
+void RkISP1CameraData::queuePendingRequests()
+{
+	while (!pendingRequests_.empty()) {
+		Request *request = pendingRequests_.front();
+
+		/*
+		 * If there aren't internal buffers available, we break and try
+		 * again later. If there are, we're guaranteed to also have V4L2
+		 * buffer slots available to queue the request, since we should
+		 * always have more (or equal) buffer slots than internal
+		 * buffers.
+		 */
+		RkISP1FrameInfo *info = frameInfo_.create(this, request);
+		if (!info)
+			break;
+
+		ipa::rkisp1::RkISP1Event ev;
+		ev.op = ipa::rkisp1::EventQueueRequest;
+		ev.frame = frame_;
+		ev.bufferId = info->paramBuffer->cookie();
+		ev.controls = request->controls();
+		ipa_->processEvent(ev);
+
+		frame_++;
+
+		pendingRequests_.pop();
+	}
+}
+
+void RkISP1CameraData::cancelPendingRequests()
+{
+	while (!pendingRequests_.empty()) {
+		Request *request = pendingRequests_.front();
+
+		for (auto it : request->buffers()) {
+			FrameBuffer *buffer = it.second;
+			buffer->cancel();
+			pipe()->completeBuffer(request, buffer);
+		}
+
+		pipe()->completeRequest(request);
+
+		pendingRequests_.pop();
+	}
+}
+
 RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera,
 						     RkISP1CameraData *data)
 	: CameraConfiguration()
@@ -855,6 +906,8 @@  void PipelineHandlerRkISP1::stop(Camera *camera)
 		LOG(RkISP1, Warning)
 			<< "Failed to stop parameters for " << camera->id();
 
+	data->cancelPendingRequests();
+
 	ASSERT(data->queuedRequests_.empty());
 	data->frameInfo_.clear();
 
@@ -867,18 +920,8 @@  int PipelineHandlerRkISP1::queueRequestDevice(Camera *camera, Request *request)
 {
 	RkISP1CameraData *data = cameraData(camera);
 
-	RkISP1FrameInfo *info = data->frameInfo_.create(data, request);
-	if (!info)
-		return -ENOENT;
-
-	ipa::rkisp1::RkISP1Event ev;
-	ev.op = ipa::rkisp1::EventQueueRequest;
-	ev.frame = data->frame_;
-	ev.bufferId = info->paramBuffer->cookie();
-	ev.controls = request->controls();
-	data->ipa_->processEvent(ev);
-
-	data->frame_++;
+	data->pendingRequests_.push(request);
+	data->queuePendingRequests();
 
 	return 0;
 }
@@ -1077,6 +1120,8 @@  void PipelineHandlerRkISP1::tryCompleteRequest(Request *request)
 	data->frameInfo_.destroy(info->frame);
 
 	completeRequest(request);
+
+	data->queuePendingRequests();
 }
 
 void PipelineHandlerRkISP1::bufferReady(FrameBuffer *buffer)