Show a patch.

GET /api/1.1/patches/23288/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 23288,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/23288/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/23288/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20250428090413.38234-17-s.pueschel@pengutronix.de>",
    "date": "2025-04-28T09:02:41",
    "name": "[v11,16/19] libcamera: pipeline: uvcvideo: Add internal request queue",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "b9ae81289ee47b7fa2810dc2f41b69b92fcc438b",
    "submitter": {
        "id": 225,
        "url": "https://patchwork.libcamera.org/api/1.1/people/225/?format=api",
        "name": "Sven Püschel",
        "email": "s.pueschel@pengutronix.de"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/23288/mbox/",
    "series": [
        {
            "id": 5148,
            "url": "https://patchwork.libcamera.org/api/1.1/series/5148/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=5148",
            "date": "2025-04-28T09:02:25",
            "name": "lc-compliance: Add test to queue more requests than hardware depth",
            "version": 11,
            "mbox": "https://patchwork.libcamera.org/series/5148/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/23288/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/23288/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 731EEC331E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 28 Apr 2025 09:05:39 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9752768B31;\n\tMon, 28 Apr 2025 11:05:38 +0200 (CEST)",
            "from metis.whiteo.stw.pengutronix.de\n\t(metis.whiteo.stw.pengutronix.de [IPv6:2a0a:edc0:2:b01:1d::104])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E6F2568ACF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Apr 2025 11:05:08 +0200 (CEST)",
            "from ptz.office.stw.pengutronix.de ([2a0a:edc0:0:900:1d::77]\n\thelo=peter.guest.stw.pengutronix.de)\n\tby metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92)\n\t(envelope-from <s.pueschel@pengutronix.de>)\n\tid 1u9KQG-0001au-Fs; Mon, 28 Apr 2025 11:05:08 +0200"
        ],
        "From": "=?utf-8?q?Sven_P=C3=BCschel?= <s.pueschel@pengutronix.de>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "=?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= <nfraprado@collabora.com>, \n\t=?utf-8?q?Sven_P=C3=BCschel?= <s.pueschel@pengutronix.de>",
        "Subject": "[PATCH v11 16/19] libcamera: pipeline: uvcvideo: Add internal\n\trequest queue",
        "Date": "Mon, 28 Apr 2025 11:02:41 +0200",
        "Message-ID": "<20250428090413.38234-17-s.pueschel@pengutronix.de>",
        "X-Mailer": "git-send-email 2.49.0",
        "In-Reply-To": "<20250428090413.38234-1-s.pueschel@pengutronix.de>",
        "References": "<20250428090413.38234-1-s.pueschel@pengutronix.de>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "X-SA-Exim-Connect-IP": "2a0a:edc0:0:900:1d::77",
        "X-SA-Exim-Mail-From": "s.pueschel@pengutronix.de",
        "X-SA-Exim-Scanned": "No (on metis.whiteo.stw.pengutronix.de);\n\tSAEximRunCond expanded to false",
        "X-PTX-Original-Recipient": "libcamera-devel@lists.libcamera.org",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Nícolas F. R. A. Prado <nfraprado@collabora.com>\n\nAdd an internal queue that stores requests until there are V4L2 buffer\nslots available. This avoids the need to cancel requests when there is a\nshortage of said buffers.\n\nSigned-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>\nSigned-off-by: Sven Püschel <s.pueschel@pengutronix.de>\n\n---\nChanges in v11:\n- Added from https://lists.libcamera.org/pipermail/libcamera-devel/2021-September/024121.html\n---\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 103 +++++++++++++++----\n 1 file changed, 82 insertions(+), 21 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex c3b718f9..52659fa2 100644\n--- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n+++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n@@ -12,6 +12,7 @@\n #include <map>\n #include <memory>\n #include <optional>\n+#include <queue>\n #include <set>\n #include <string>\n #include <vector>\n@@ -29,6 +30,7 @@\n \n #include \"libcamera/internal/camera.h\"\n #include \"libcamera/internal/device_enumerator.h\"\n+#include \"libcamera/internal/framebuffer.h\"\n #include \"libcamera/internal/media_device.h\"\n #include \"libcamera/internal/pipeline_handler.h\"\n #include \"libcamera/internal/sysfs.h\"\n@@ -53,18 +55,28 @@ public:\n \n \tconst std::string &id() const { return id_; }\n \n+\tvoid queuePendingRequests();\n+\tvoid cancelPendingRequests();\n+\n+\tvoid setAvailableBufferSlotCount(unsigned int count) { availableBufferSlotCount_ = count; }\n+\n \tMutex openLock_;\n \tstd::unique_ptr<V4L2VideoDevice> video_;\n \tStream stream_;\n \tstd::map<PixelFormat, std::vector<SizeRange>> formats_;\n+\tstd::queue<Request *> pendingRequests_;\n \n \tstd::optional<v4l2_exposure_auto_type> autoExposureMode_;\n \tstd::optional<v4l2_exposure_auto_type> manualExposureMode_;\n \n private:\n \tbool generateId();\n+\tint processControl(ControlList *controls, unsigned int id,\n+\t\t\t   const ControlValue &value);\n+\tint processControls(Request *request);\n \n \tstd::string id_;\n+\tunsigned int availableBufferSlotCount_;\n };\n \n class UVCCameraConfiguration : public CameraConfiguration\n@@ -98,10 +110,6 @@ public:\n \tbool match(DeviceEnumerator *enumerator) override;\n \n private:\n-\tint processControl(const UVCCameraData *data, ControlList *controls,\n-\t\t\t   unsigned int id, const ControlValue &value);\n-\tint processControls(UVCCameraData *data, Request *request);\n-\n \tbool acquireDevice(Camera *camera) override;\n \tvoid releaseDevice(Camera *camera) override;\n \n@@ -295,6 +303,8 @@ int PipelineHandlerUVC::start(Camera *camera, [[maybe_unused]] const ControlList\n \tif (ret < 0)\n \t\treturn ret;\n \n+\tdata->setAvailableBufferSlotCount(kUVCBufferSlotCount);\n+\n \tret = data->video_->streamOn();\n \tif (ret < 0) {\n \t\tdata->video_->releaseBuffers();\n@@ -308,11 +318,12 @@ void PipelineHandlerUVC::stopDevice(Camera *camera)\n {\n \tUVCCameraData *data = cameraData(camera);\n \tdata->video_->streamOff();\n+\tdata->cancelPendingRequests();\n \tdata->video_->releaseBuffers();\n }\n \n-int PipelineHandlerUVC::processControl(const UVCCameraData *data, ControlList *controls,\n-\t\t\t\t       unsigned int id, const ControlValue &value)\n+int UVCCameraData::processControl(ControlList *controls, unsigned int id,\n+\t\t\t\t  const ControlValue &value)\n {\n \tuint32_t cid;\n \n@@ -362,10 +373,10 @@ int PipelineHandlerUVC::processControl(const UVCCameraData *data, ControlList *c\n \n \t\tswitch (value.get<int32_t>()) {\n \t\tcase controls::ExposureTimeModeAuto:\n-\t\t\tmode = data->autoExposureMode_;\n+\t\t\tmode = autoExposureMode_;\n \t\t\tbreak;\n \t\tcase controls::ExposureTimeModeManual:\n-\t\t\tmode = data->manualExposureMode_;\n+\t\t\tmode = manualExposureMode_;\n \t\t\tbreak;\n \t\t}\n \n@@ -409,19 +420,19 @@ int PipelineHandlerUVC::processControl(const UVCCameraData *data, ControlList *c\n \treturn 0;\n }\n \n-int PipelineHandlerUVC::processControls(UVCCameraData *data, Request *request)\n+int UVCCameraData::processControls(Request *request)\n {\n-\tControlList controls(data->video_->controls());\n+\tControlList controls(video_->controls());\n \n \tfor (const auto &[id, value] : request->controls())\n-\t\tprocessControl(data, &controls, id, value);\n+\t\tprocessControl(&controls, id, value);\n \n \tfor (const auto &ctrl : controls)\n \t\tLOG(UVC, Debug)\n \t\t\t<< \"Setting control \" << utils::hex(ctrl.first)\n \t\t\t<< \" to \" << ctrl.second.toString();\n \n-\tint ret = data->video_->setControls(&controls);\n+\tint ret = video_->setControls(&controls);\n \tif (ret) {\n \t\tLOG(UVC, Error) << \"Failed to set controls: \" << ret;\n \t\treturn ret < 0 ? ret : -EINVAL;\n@@ -433,21 +444,16 @@ int PipelineHandlerUVC::processControls(UVCCameraData *data, Request *request)\n int PipelineHandlerUVC::queueRequestDevice(Camera *camera, Request *request)\n {\n \tUVCCameraData *data = cameraData(camera);\n-\tFrameBuffer *buffer = request->findBuffer(&data->stream_);\n-\tif (!buffer) {\n+\n+\tif (!request->findBuffer(&data->stream_)) {\n \t\tLOG(UVC, Error)\n \t\t\t<< \"Attempt to queue request with invalid stream\";\n \n \t\treturn -ENOENT;\n \t}\n \n-\tint ret = processControls(data, request);\n-\tif (ret < 0)\n-\t\treturn ret;\n-\n-\tret = data->video_->queueBuffer(buffer);\n-\tif (ret < 0)\n-\t\treturn ret;\n+\tdata->pendingRequests_.push(request);\n+\tdata->queuePendingRequests();\n \n \treturn 0;\n }\n@@ -882,6 +888,61 @@ void UVCCameraData::imageBufferReady(FrameBuffer *buffer)\n \n \tpipe()->completeBuffer(request, buffer);\n \tpipe()->completeRequest(request);\n+\n+\tavailableBufferSlotCount_++;\n+\n+\tqueuePendingRequests();\n+}\n+\n+void UVCCameraData::queuePendingRequests()\n+{\n+\twhile (!pendingRequests_.empty() && availableBufferSlotCount_) {\n+\t\tRequest *request = pendingRequests_.front();\n+\t\tFrameBuffer *buffer = request->findBuffer(&stream_);\n+\n+\t\tint ret = processControls(request);\n+\t\tif (ret < 0) {\n+\t\t\tLOG(UVC, Error) << \"Failed to process controls with\"\n+\t\t\t\t\t<< \" error \" << ret << \". Cancelling\"\n+\t\t\t\t\t<< \" buffer.\";\n+\t\t\tbuffer->_d()->cancel();\n+\t\t\tpipe()->completeBuffer(request, buffer);\n+\t\t\tpipe()->completeRequest(request);\n+\t\t\tpendingRequests_.pop();\n+\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tret = video_->queueBuffer(buffer);\n+\t\tif (ret < 0) {\n+\t\t\tLOG(UVC, Error) << \"Failed to queue buffer with error \"\n+\t\t\t\t\t<< ret << \". Cancelling buffer.\";\n+\t\t\tbuffer->_d()->cancel();\n+\t\t\tpipe()->completeBuffer(request, buffer);\n+\t\t\tpipe()->completeRequest(request);\n+\t\t\tpendingRequests_.pop();\n+\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tavailableBufferSlotCount_--;\n+\n+\t\tpendingRequests_.pop();\n+\t}\n+}\n+\n+void UVCCameraData::cancelPendingRequests()\n+{\n+\twhile (!pendingRequests_.empty()) {\n+\t\tRequest *request = pendingRequests_.front();\n+\t\tFrameBuffer *buffer = request->findBuffer(&stream_);\n+\n+\t\tbuffer->_d()->cancel();\n+\t\tpipe()->completeBuffer(request, buffer);\n+\t\tpipe()->completeRequest(request);\n+\n+\t\tpendingRequests_.pop();\n+\t}\n }\n \n REGISTER_PIPELINE_HANDLER(PipelineHandlerUVC, \"uvcvideo\")\n",
    "prefixes": [
        "v11",
        "16/19"
    ]
}