From patchwork Tue Aug 31 22:37:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 23010 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id B7D95C0DA4 for ; Mon, 24 Mar 2025 17:12:47 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 40F6CA8F for ; Mon, 24 Mar 2025 18:11:01 +0100 (CET) Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id 2NvwBTuvLmFucAAA4E0KoQ (envelope-from ) for ; Wed, 01 Sep 2021 00:37:47 +0200 Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by perceval.ideasonboard.com (Postfix) with ESMTPS id 07BAC8F; Wed, 1 Sep 2021 00:37:47 +0200 (CEST) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9BB6069183; Wed, 1 Sep 2021 00:37:46 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 81E6B69166 for ; Wed, 1 Sep 2021 00:37:38 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2434:b693:c9:5cb6:b688]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 8E51C1F4396E; Tue, 31 Aug 2021 23:37:36 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Tue, 31 Aug 2021 19:37:04 -0300 Message-Id: <20210831223705.1928000-5-nfraprado@collabora.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210831223705.1928000-1-nfraprado@collabora.com> References: <20210831223705.1928000-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/5] libcamera: pipeline: simple: Add internal request queue X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" X-TUID: mPWV82qDk+0V Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org 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 --- I wasn't able to test this patch as I don't have any of the target hardware. Changes in v2: - New src/libcamera/pipeline/simple/simple.cpp | 101 ++++++++++++++++++----- 1 file changed, 81 insertions(+), 20 deletions(-) diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp index 6deba5d7dd61..e9abe5e291f4 100644 --- a/src/libcamera/pipeline/simple/simple.cpp +++ b/src/libcamera/pipeline/simple/simple.cpp @@ -167,6 +167,9 @@ public: int setupFormats(V4L2SubdeviceFormat *format, V4L2Subdevice::Whence whence); + void queuePendingRequests(); + void cancelPendingRequests(); + unsigned int streamIndex(const Stream *stream) const { return stream - &streams_.front(); @@ -198,6 +201,10 @@ public: std::queue> converterQueue_; const SimplePipelineInfo *deviceInfo; + + std::queue pendingRequests_; + + unsigned int avaliableBufferSlotCount_; }; class SimpleCameraConfiguration : public CameraConfiguration @@ -546,6 +553,66 @@ int SimpleCameraData::setupFormats(V4L2SubdeviceFormat *format, return 0; } +void SimpleCameraData::queuePendingRequests() +{ + while (!pendingRequests_.empty() && avaliableBufferSlotCount_) { + Request *request = pendingRequests_.front(); + + if (useConverter_) { + /* + * If conversion is needed, push the buffer(s) to the + * converter queue, it will be handed to the converter + * in the capture completion handler. + */ + std::map buffers; + + for (auto &[stream, buffer] : request->buffers()) + buffers.emplace(streamIndex(stream), buffer); + + converterQueue_.push(std::move(buffers)); + } else { + /* + * Since we're not using a converter we can assume + * there's a single stream. + */ + FrameBuffer *buffer = request->buffers().at(0); + + int ret = video_->queueBuffer(buffer); + if (ret < 0) { + LOG(SimplePipeline, Error) + << "Failed to queue buffer with error " + << ret << ". Cancelling buffer."; + buffer->cancel(); + pipe()->completeBuffer(request, buffer); + pipe()->completeRequest(request); + pendingRequests_.pop(); + + continue; + } + } + + avaliableBufferSlotCount_--; + + pendingRequests_.pop(); + } +} + +void SimpleCameraData::cancelPendingRequests() +{ + while (!pendingRequests_.empty()) { + Request *request = pendingRequests_.front(); + + for (auto const &it : request->buffers()) { + it.second->cancel(); + pipe()->completeBuffer(request, it.second); + } + + pipe()->completeRequest(request); + + pendingRequests_.pop(); + } +} + /* ----------------------------------------------------------------------------- * Camera Configuration */ @@ -852,6 +919,8 @@ int SimplePipelineHandler::start(Camera *camera, [[maybe_unused]] const ControlL if (ret < 0) return ret; + data->avaliableBufferSlotCount_ = kSimpleBufferSlotCount; + ret = video->streamOn(); if (ret < 0) { stop(camera); @@ -885,6 +954,7 @@ void SimplePipelineHandler::stop(Camera *camera) converter_->stop(); video->streamOff(); + data->cancelPendingRequests(); video->releaseBuffers(); data->converterBuffers_.clear(); @@ -894,27 +964,9 @@ void SimplePipelineHandler::stop(Camera *camera) int SimplePipelineHandler::queueRequestDevice(Camera *camera, Request *request) { SimpleCameraData *data = cameraData(camera); - int ret; - - std::map buffers; - - for (auto &[stream, buffer] : request->buffers()) { - /* - * If conversion is needed, push the buffer to the converter - * queue, it will be handed to the converter in the capture - * completion handler. - */ - if (data->useConverter_) { - buffers.emplace(data->streamIndex(stream), buffer); - } else { - ret = data->video_->queueBuffer(buffer); - if (ret < 0) - return ret; - } - } - if (data->useConverter_) - data->converterQueue_.push(std::move(buffers)); + data->pendingRequests_.push(request); + data->queuePendingRequests(); return 0; } @@ -1151,6 +1203,8 @@ void SimplePipelineHandler::bufferReady(FrameBuffer *buffer) Request *request = buffer->request(); completeBuffer(request, buffer); completeRequest(request); + data->avaliableBufferSlotCount_++; + data->queuePendingRequests(); return; } @@ -1221,6 +1275,9 @@ void SimplePipelineHandler::bufferReady(FrameBuffer *buffer) /* Otherwise simply complete the request. */ completeBuffer(request, buffer); completeRequest(request); + + data->avaliableBufferSlotCount_++; + data->queuePendingRequests(); } void SimplePipelineHandler::converterInputDone(FrameBuffer *buffer) @@ -1235,11 +1292,15 @@ void SimplePipelineHandler::converterInputDone(FrameBuffer *buffer) void SimplePipelineHandler::converterOutputDone(FrameBuffer *buffer) { ASSERT(activeCamera_); + SimpleCameraData *data = cameraData(activeCamera_); /* Complete the buffer and the request. */ Request *request = buffer->request(); if (completeBuffer(request, buffer)) completeRequest(request); + + data->avaliableBufferSlotCount_++; + data->queuePendingRequests(); } REGISTER_PIPELINE_HANDLER(SimplePipelineHandler)