[libcamera-devel,v2,1/5] libcamera: pipeline: vimc: Add internal request queue
diff mbox

Message ID 20210831223705.1928000-2-nfraprado@collabora.com
State New
Headers 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 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>

---

Changes in v2:
- Moved processControls() from PipelineHandlerVimc to VimcCameraData
- Moved cancellation of pending requests to after video devices stop
- Added a counter to keep track of the number of available buffer slots
- Added error log on failure to process controls

 src/libcamera/pipeline/vimc/vimc.cpp | 167 +++++++++++++++++++--------
 1 file changed, 116 insertions(+), 51 deletions(-)

Patch
diff mbox

diff --git a/src/libcamera/pipeline/vimc/vimc.cpp b/src/libcamera/pipeline/vimc/vimc.cpp
index ad71bfc67228..d8f88000d9d7 100644
--- a/src/libcamera/pipeline/vimc/vimc.cpp
+++ b/src/libcamera/pipeline/vimc/vimc.cpp
@@ -9,6 +9,7 @@ 
 #include <iomanip>
 #include <map>
 #include <math.h>
+#include <queue>
 #include <tuple>
 
 #include <linux/media-bus-format.h>
@@ -56,6 +57,11 @@  public:
 	void bufferReady(FrameBuffer *buffer);
 	void paramsFilled(unsigned int id);
 
+	void queuePendingRequests();
+	void cancelPendingRequests();
+
+	void setAvailableBufferSlotCount(unsigned int count) { availableBufferSlotCount_ = count; }
+
 	MediaDevice *media_;
 	std::unique_ptr<CameraSensor> sensor_;
 	std::unique_ptr<V4L2Subdevice> debayer_;
@@ -66,6 +72,13 @@  public:
 
 	std::unique_ptr<ipa::vimc::IPAProxyVimc> ipa_;
 	std::vector<std::unique_ptr<FrameBuffer>> mockIPABufs_;
+
+	std::queue<Request *> pendingRequests_;
+
+private:
+	int processControls(Request *request);
+
+	unsigned int availableBufferSlotCount_;
 };
 
 class VimcCameraConfiguration : public CameraConfiguration
@@ -99,8 +112,6 @@  public:
 	bool match(DeviceEnumerator *enumerator) override;
 
 private:
-	int processControls(VimcCameraData *data, Request *request);
-
 	VimcCameraData *cameraData(Camera *camera)
 	{
 		return static_cast<VimcCameraData *>(camera->_d());
@@ -335,6 +346,8 @@  int PipelineHandlerVimc::start(Camera *camera, [[maybe_unused]] const ControlLis
 	if (ret < 0)
 		return ret;
 
+	data->setAvailableBufferSlotCount(kVimcBufferSlotCount);
+
 	/* Map the mock IPA buffers to VIMC IPA to exercise IPC code paths. */
 	std::vector<IPABuffer> ipaBuffers;
 	for (auto [i, buffer] : utils::enumerate(data->mockIPABufs_)) {
@@ -370,67 +383,24 @@  void PipelineHandlerVimc::stop(Camera *camera)
 	data->ipa_->unmapBuffers(ids);
 	data->ipa_->stop();
 
-	data->video_->releaseBuffers();
-}
-
-int PipelineHandlerVimc::processControls(VimcCameraData *data, Request *request)
-{
-	ControlList controls(data->sensor_->controls());
-
-	for (auto it : request->controls()) {
-		unsigned int id = it.first;
-		unsigned int offset;
-		uint32_t cid;
+	data->cancelPendingRequests();
 
-		if (id == controls::Brightness) {
-			cid = V4L2_CID_BRIGHTNESS;
-			offset = 128;
-		} else if (id == controls::Contrast) {
-			cid = V4L2_CID_CONTRAST;
-			offset = 0;
-		} else if (id == controls::Saturation) {
-			cid = V4L2_CID_SATURATION;
-			offset = 0;
-		} else {
-			continue;
-		}
-
-		int32_t value = lroundf(it.second.get<float>() * 128 + offset);
-		controls.set(cid, std::clamp(value, 0, 255));
-	}
-
-	for (const auto &ctrl : controls)
-		LOG(VIMC, Debug)
-			<< "Setting control " << utils::hex(ctrl.first)
-			<< " to " << ctrl.second.toString();
-
-	int ret = data->sensor_->setControls(&controls);
-	if (ret) {
-		LOG(VIMC, Error) << "Failed to set controls: " << ret;
-		return ret < 0 ? ret : -EINVAL;
-	}
-
-	return ret;
+	data->video_->releaseBuffers();
 }
 
 int PipelineHandlerVimc::queueRequestDevice(Camera *camera, Request *request)
 {
 	VimcCameraData *data = cameraData(camera);
-	FrameBuffer *buffer = request->findBuffer(&data->stream_);
-	if (!buffer) {
+
+	if (!request->findBuffer(&data->stream_)) {
 		LOG(VIMC, Error)
 			<< "Attempt to queue request with invalid stream";
 
 		return -ENOENT;
 	}
 
-	int ret = processControls(data, request);
-	if (ret < 0)
-		return ret;
-
-	ret = data->video_->queueBuffer(buffer);
-	if (ret < 0)
-		return ret;
+	data->pendingRequests_.push(request);
+	data->queuePendingRequests();
 
 	data->ipa_->processControls(request->sequence(), request->controls());
 
@@ -565,6 +535,46 @@  int VimcCameraData::init()
 	return 0;
 }
 
+int VimcCameraData::processControls(Request *request)
+{
+	ControlList controls(sensor_->controls());
+
+	for (auto it : request->controls()) {
+		unsigned int id = it.first;
+		unsigned int offset;
+		uint32_t cid;
+
+		if (id == controls::Brightness) {
+			cid = V4L2_CID_BRIGHTNESS;
+			offset = 128;
+		} else if (id == controls::Contrast) {
+			cid = V4L2_CID_CONTRAST;
+			offset = 0;
+		} else if (id == controls::Saturation) {
+			cid = V4L2_CID_SATURATION;
+			offset = 0;
+		} else {
+			continue;
+		}
+
+		int32_t value = lroundf(it.second.get<float>() * 128 + offset);
+		controls.set(cid, std::clamp(value, 0, 255));
+	}
+
+	for (const auto &ctrl : controls)
+		LOG(VIMC, Debug)
+			<< "Setting control " << utils::hex(ctrl.first)
+			<< " to " << ctrl.second.toString();
+
+	int ret = sensor_->setControls(&controls);
+	if (ret) {
+		LOG(VIMC, Error) << "Failed to set controls: " << ret;
+		return ret < 0 ? ret : -EINVAL;
+	}
+
+	return ret;
+}
+
 void VimcCameraData::bufferReady(FrameBuffer *buffer)
 {
 	PipelineHandlerVimc *pipe =
@@ -591,6 +601,10 @@  void VimcCameraData::bufferReady(FrameBuffer *buffer)
 	pipe->completeRequest(request);
 
 	ipa_->fillParams(request->sequence(), mockIPABufs_[0]->cookie());
+
+	availableBufferSlotCount_++;
+
+	queuePendingRequests();
 }
 
 int VimcCameraData::allocateMockIPABuffers()
@@ -612,6 +626,57 @@  void VimcCameraData::paramsFilled([[maybe_unused]] unsigned int id)
 {
 }
 
+void VimcCameraData::queuePendingRequests()
+{
+	while (!pendingRequests_.empty() && availableBufferSlotCount_) {
+		Request *request = pendingRequests_.front();
+		FrameBuffer *buffer = request->findBuffer(&stream_);
+
+		int ret = processControls(request);
+		if (ret < 0) {
+			LOG(VIMC, Error) << "Failed to process controls with"
+					 << " error " << ret << ". Cancelling"
+					 << " buffer.";
+			buffer->cancel();
+			pipe()->completeBuffer(request, buffer);
+			pipe()->completeRequest(request);
+			pendingRequests_.pop();
+
+			continue;
+		}
+
+		ret = video_->queueBuffer(buffer);
+		if (ret < 0) {
+			LOG(VIMC, Error) << "Failed to queue buffer with error "
+					 << ret << ". Cancelling buffer.";
+			buffer->cancel();
+			pipe()->completeBuffer(request, buffer);
+			pipe()->completeRequest(request);
+			pendingRequests_.pop();
+
+			continue;
+		}
+
+		availableBufferSlotCount_--;
+
+		pendingRequests_.pop();
+	}
+}
+
+void VimcCameraData::cancelPendingRequests()
+{
+	while (!pendingRequests_.empty()) {
+		Request *request = pendingRequests_.front();
+		FrameBuffer *buffer = request->findBuffer(&stream_);
+
+		buffer->cancel();
+		pipe()->completeBuffer(request, buffer);
+		pipe()->completeRequest(request);
+
+		pendingRequests_.pop();
+	}
+}
+
 REGISTER_PIPELINE_HANDLER(PipelineHandlerVimc)
 
 } /* namespace libcamera */