Show a patch.

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

{
    "id": 26991,
    "url": "https://patchwork.libcamera.org/api/patches/26991/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/26991/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20260618123844.656396-28-barnabas.pocze@ideasonboard.com>",
    "date": "2026-06-18T12:38:44",
    "name": "[RFC,v1,27/27] v4l2: v4l2_camera: Provide buffers one by one",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "c359ea33bc9eed15609820849dafb58b60059432",
    "submitter": {
        "id": 216,
        "url": "https://patchwork.libcamera.org/api/people/216/?format=api",
        "name": "Barnabás Pőcze",
        "email": "barnabas.pocze@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/26991/mbox/",
    "series": [
        {
            "id": 6006,
            "url": "https://patchwork.libcamera.org/api/series/6006/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=6006",
            "date": "2026-06-18T12:38:17",
            "name": "Misc. changes before request-buffer split",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/6006/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/26991/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/26991/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 EE0F4C3333\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 18 Jun 2026 12:39:30 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5D32B65721;\n\tThu, 18 Jun 2026 14:39:30 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E9247656F9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Jun 2026 14:38:53 +0200 (CEST)",
            "from pb-laptop.local (185.182.214.63.nat.pool.zt.hu\n\t[185.182.214.63])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 054B72393\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Jun 2026 14:38:18 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"GJLEBwnk\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1781786299;\n\tbh=06tF2XpQR52p8Wyt40okZlVtNLZGHQx5hmAOtCLzikQ=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=GJLEBwnk/JPG60ikRedZTiYSOZ/mdiKeVQmextE72rZK32hEAzM2WWQPes1mD0z99\n\tIPTjBL4pQ9lTZpKFZryznQHlhMNkFF07bv8WjmlmC+s3RDTGiWLUTfvAcrqF13B5aN\n\tKxPjuNp83BAmiPEnXv53cn1SS0OIwufduE18GukM=",
        "From": "=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Subject": "[RFC PATCH v1 27/27] v4l2: v4l2_camera: Provide buffers one by one",
        "Date": "Thu, 18 Jun 2026 14:38:44 +0200",
        "Message-ID": "<20260618123844.656396-28-barnabas.pocze@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.54.0",
        "In-Reply-To": "<20260618123844.656396-1-barnabas.pocze@ideasonboard.com>",
        "References": "<20260618123844.656396-1-barnabas.pocze@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "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": "Instead of processing a set of buffers at a time and cycling through\nthe buffers using `V4L2CameraProxy::currentBuf_`, add a new function\nto query just the least recently completed buffer and use that to\nservice `VIDIOC_DQBUF`. This ensures that the reported index will denote\nthe buffer that was actually completed after the request-buffer split.\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n---\n src/v4l2/v4l2_camera.cpp       | 62 ++++++++++---------------\n src/v4l2/v4l2_camera.h         | 13 ++----\n src/v4l2/v4l2_camera_proxy.cpp | 84 +++++++++++++++++-----------------\n src/v4l2/v4l2_camera_proxy.h   |  2 -\n 4 files changed, 69 insertions(+), 92 deletions(-)",
    "diff": "diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\nindex 8c7de8e92b..90773e00bf 100644\n--- a/src/v4l2/v4l2_camera.cpp\n+++ b/src/v4l2/v4l2_camera.cpp\n@@ -20,7 +20,7 @@ LOG_DECLARE_CATEGORY(V4L2Compat)\n \n V4L2Camera::V4L2Camera(std::shared_ptr<Camera> camera)\n \t: camera_(camera), controls_(controls::controls), isRunning_(false),\n-\t  efd_(-1), bufferAvailableCount_(0)\n+\t  efd_(-1)\n {\n \tcamera_->requestCompleted.connect(this, &V4L2Camera::requestComplete);\n }\n@@ -68,38 +68,25 @@ void V4L2Camera::unbind()\n \tefd_ = -1;\n }\n \n-std::vector<V4L2Camera::CompletedBuffer> V4L2Camera::completedBuffers()\n-{\n-\tMutexLocker lock(bufferLock_);\n-\tstd::vector v(std::move_iterator(completedBuffers_.begin()),\n-\t\t      std::move_iterator(completedBuffers_.end()));\n-\n-\tcompletedBuffers_.clear();\n-\n-\treturn v;\n-}\n-\n void V4L2Camera::requestComplete(Request *request)\n {\n \tif (request->status() == Request::RequestCancelled)\n \t\treturn;\n \n \t/* We only have one stream at the moment. */\n-\tbufferLock_.lock();\n-\tFrameBuffer *buffer = request->buffers().begin()->second;\n-\tcompletedBuffers_.emplace_back(buffer->cookie(), buffer->metadata());\n-\tbufferLock_.unlock();\n-\n-\tuint64_t data = 1;\n-\tint ret = ::write(efd_, &data, sizeof(data));\n-\tif (ret != sizeof(data))\n-\t\tLOG(V4L2Compat, Error) << \"Failed to signal eventfd POLLIN\";\n-\n-\trequest->reuse();\n \t{\n \t\tMutexLocker locker(bufferMutex_);\n-\t\tbufferAvailableCount_++;\n+\t\tFrameBuffer *buffer = request->buffers().begin()->second;\n+\t\tcompletedBuffers_.emplace_back(buffer->cookie(), buffer->metadata());\n+\n+\t\tuint64_t data = 1;\n+\t\tint ret = ::write(efd_, &data, sizeof(data));\n+\t\tif (ret != sizeof(data))\n+\t\t\tLOG(V4L2Compat, Error) << \"Failed to signal eventfd POLLIN\";\n+\n+\t\trequest->reuse();\n \t}\n+\n \tbufferCV_.notify_all();\n }\n \n@@ -280,24 +267,23 @@ int V4L2Camera::qbuf(unsigned int index)\n \treturn 0;\n }\n \n-void V4L2Camera::waitForBufferAvailable()\n+std::optional<V4L2Camera::CompletedBuffer> V4L2Camera::nextBuffer(bool wait)\n {\n \tMutexLocker locker(bufferMutex_);\n-\tbufferCV_.wait(locker, [&]() LIBCAMERA_TSA_REQUIRES(bufferMutex_) {\n-\t\t\t       return bufferAvailableCount_ >= 1 || !isRunning_;\n-\t\t       });\n-\tif (isRunning_)\n-\t\tbufferAvailableCount_--;\n-}\n \n-bool V4L2Camera::isBufferAvailable()\n-{\n-\tMutexLocker locker(bufferMutex_);\n-\tif (bufferAvailableCount_ < 1)\n-\t\treturn false;\n+\tif (wait) {\n+\t\tbufferCV_.wait(locker, [&]() LIBCAMERA_TSA_REQUIRES(bufferMutex_) {\n+\t\t       return !completedBuffers_.empty() || !isRunning_;\n+\t\t});\n+\t}\n+\n+\tif (!isRunning_)\n+\t\treturn {};\n+\n+\tauto buffer = std::move(completedBuffers_.front());\n+\tcompletedBuffers_.pop_front();\n \n-\tbufferAvailableCount_--;\n-\treturn true;\n+\treturn buffer;\n }\n \n bool V4L2Camera::isRunning()\ndiff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\nindex 1d90eb43e4..06e37077b2 100644\n--- a/src/v4l2/v4l2_camera.h\n+++ b/src/v4l2/v4l2_camera.h\n@@ -9,6 +9,7 @@\n \n #include <deque>\n #include <memory>\n+#include <optional>\n #include <vector>\n \n #include <libcamera/base/mutex.h>\n@@ -41,8 +42,6 @@ public:\n \tvoid bind(int efd);\n \tvoid unbind();\n \n-\tstd::vector<CompletedBuffer> completedBuffers() LIBCAMERA_TSA_EXCLUDES(bufferLock_);\n-\n \tint configure(libcamera::StreamConfiguration *streamConfigOut,\n \t\t      const libcamera::Size &size,\n \t\t      const libcamera::PixelFormat &pixelformat,\n@@ -63,14 +62,12 @@ public:\n \n \tint qbuf(unsigned int index);\n \n-\tvoid waitForBufferAvailable() LIBCAMERA_TSA_EXCLUDES(bufferMutex_);\n-\tbool isBufferAvailable() LIBCAMERA_TSA_EXCLUDES(bufferMutex_);\n+\tstd::optional<CompletedBuffer> nextBuffer(bool wait) LIBCAMERA_TSA_EXCLUDES(bufferMutex_);\n \n \tbool isRunning();\n \n private:\n-\tvoid requestComplete(libcamera::Request *request)\n-\t\tLIBCAMERA_TSA_EXCLUDES(bufferLock_);\n+\tvoid requestComplete(libcamera::Request *request);\n \n \tstd::shared_ptr<libcamera::Camera> camera_;\n \tstd::unique_ptr<libcamera::CameraConfiguration> config_;\n@@ -79,18 +76,16 @@ private:\n \n \tbool isRunning_;\n \n-\tlibcamera::Mutex bufferLock_;\n \tstd::unique_ptr<libcamera::FrameBufferAllocator> bufferAllocator_;\n \n \tstd::vector<std::unique_ptr<libcamera::Request>> requestPool_;\n \n \tstd::deque<libcamera::Request *> pendingRequests_;\n \tstd::deque<CompletedBuffer> completedBuffers_\n-\t\tLIBCAMERA_TSA_GUARDED_BY(bufferLock_);\n+\t\tLIBCAMERA_TSA_GUARDED_BY(bufferMutex_);\n \n \tint efd_;\n \n \tlibcamera::Mutex bufferMutex_;\n \tlibcamera::ConditionVariable bufferCV_;\n-\tunsigned int bufferAvailableCount_ LIBCAMERA_TSA_GUARDED_BY(bufferMutex_);\n };\ndiff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\nindex 820a417a9c..3a34e84071 100644\n--- a/src/v4l2/v4l2_camera_proxy.cpp\n+++ b/src/v4l2/v4l2_camera_proxy.cpp\n@@ -39,9 +39,37 @@ using namespace std::literals::chrono_literals;\n \n LOG_DECLARE_CATEGORY(V4L2Compat)\n \n+namespace {\n+\n+void updateBuffer(v4l2_buffer &buf, const FrameMetadata &fmd)\n+{\n+\tswitch (fmd.status) {\n+\tcase FrameMetadata::FrameSuccess:\n+\t\tbuf.bytesused = std::accumulate(fmd.planes().begin(),\n+\t\t\t\t\t\tfmd.planes().end(), 0,\n+\t\t\t\t\t\t[](unsigned int total, const auto &plane) {\n+\t\t\t\t\t\t\treturn total + plane.bytesused;\n+\t\t\t\t\t\t});\n+\t\tbuf.field = V4L2_FIELD_NONE;\n+\t\tbuf.timestamp.tv_sec = fmd.timestamp / 1000000000;\n+\t\tbuf.timestamp.tv_usec = (fmd.timestamp / 1000) % 1000000;\n+\t\tbuf.sequence = fmd.sequence;\n+\n+\t\tbuf.flags |= V4L2_BUF_FLAG_DONE;\n+\t\tbreak;\n+\tcase FrameMetadata::FrameError:\n+\t\tbuf.flags |= V4L2_BUF_FLAG_ERROR;\n+\t\tbreak;\n+\tdefault:\n+\t\tbreak;\n+\t}\n+}\n+\n+} /* namespace */\n+\n V4L2CameraProxy::V4L2CameraProxy(unsigned int index,\n \t\t\t\t std::shared_ptr<Camera> camera)\n-\t: refcount_(0), index_(index), bufferCount_(0), currentBuf_(0),\n+\t: refcount_(0), index_(index), bufferCount_(0),\n \t  vcam_(std::make_unique<V4L2Camera>(camera)), owner_(nullptr)\n {\n \tquerycap(camera);\n@@ -241,36 +269,6 @@ void V4L2CameraProxy::querycap(std::shared_ptr<Camera> camera)\n \tmemset(capabilities_.reserved, 0, sizeof(capabilities_.reserved));\n }\n \n-void V4L2CameraProxy::updateBuffers()\n-{\n-\tstd::vector<V4L2Camera::CompletedBuffer> completedBuffers = vcam_->completedBuffers();\n-\tfor (const V4L2Camera::CompletedBuffer &buffer : completedBuffers) {\n-\t\tconst FrameMetadata &fmd = buffer.data_;\n-\t\tstruct v4l2_buffer &buf = buffers_[buffer.index_];\n-\n-\t\tswitch (fmd.status) {\n-\t\tcase FrameMetadata::FrameSuccess:\n-\t\t\tbuf.bytesused = std::accumulate(fmd.planes().begin(),\n-\t\t\t\t\t\t\tfmd.planes().end(), 0,\n-\t\t\t\t\t\t\t[](unsigned int total, const auto &plane) {\n-\t\t\t\t\t\t\t\treturn total + plane.bytesused;\n-\t\t\t\t\t\t\t});\n-\t\t\tbuf.field = V4L2_FIELD_NONE;\n-\t\t\tbuf.timestamp.tv_sec = fmd.timestamp / 1000000000;\n-\t\t\tbuf.timestamp.tv_usec = (fmd.timestamp / 1000) % 1000000;\n-\t\t\tbuf.sequence = fmd.sequence;\n-\n-\t\t\tbuf.flags |= V4L2_BUF_FLAG_DONE;\n-\t\t\tbreak;\n-\t\tcase FrameMetadata::FrameError:\n-\t\t\tbuf.flags |= V4L2_BUF_FLAG_ERROR;\n-\t\t\tbreak;\n-\t\tdefault:\n-\t\t\tbreak;\n-\t\t}\n-\t}\n-}\n-\n int V4L2CameraProxy::vidioc_querycap(V4L2CameraFile *file, struct v4l2_capability *arg)\n {\n \tLOG(V4L2Compat, Debug)\n@@ -585,8 +583,6 @@ int V4L2CameraProxy::vidioc_querybuf(V4L2CameraFile *file, struct v4l2_buffer *a\n \t    arg->index >= bufferCount_)\n \t\treturn -EINVAL;\n \n-\tupdateBuffers();\n-\n \t*arg = buffers_[arg->index];\n \n \treturn 0;\n@@ -674,30 +670,34 @@ int V4L2CameraProxy::vidioc_dqbuf(V4L2CameraFile *file, struct v4l2_buffer *arg,\n \t    !validateMemoryType(arg->memory))\n \t\treturn -EINVAL;\n \n+\tstd::optional<V4L2Camera::CompletedBuffer> b;\n+\n \tif (!file->nonBlocking()) {\n \t\tlock->unlock();\n-\t\tvcam_->waitForBufferAvailable();\n+\t\tb = vcam_->nextBuffer(true);\n \t\tlock->lock();\n-\t} else if (!vcam_->isBufferAvailable())\n-\t\treturn -EAGAIN;\n+\t} else {\n+\t\tb = vcam_->nextBuffer(false);\n+\t}\n \n \t/*\n \t * We need to check here again in case stream was turned off while we\n-\t * were blocked on waitForBufferAvailable().\n+\t * were blocked in nextBuffer().\n \t */\n \tif (!vcam_->isRunning())\n \t\treturn -EINVAL;\n \n-\tupdateBuffers();\n+\tif (!b)\n+\t\treturn -EAGAIN;\n+\n+\tstruct v4l2_buffer &buf = buffers_[b->index_];\n \n-\tstruct v4l2_buffer &buf = buffers_[currentBuf_];\n+\tupdateBuffer(buf, b->data_);\n \n \tbuf.flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_PREPARED);\n \tbuf.length = sizeimage_;\n \t*arg = buf;\n \n-\tcurrentBuf_ = (currentBuf_ + 1) % bufferCount_;\n-\n \tuint64_t data;\n \tint ret = ::read(file->efd(), &data, sizeof(data));\n \tif (ret != sizeof(data))\n@@ -753,8 +753,6 @@ int V4L2CameraProxy::vidioc_streamon(V4L2CameraFile *file, int *arg)\n \tif (vcam_->isRunning())\n \t\treturn 0;\n \n-\tcurrentBuf_ = 0;\n-\n \treturn vcam_->streamOn();\n }\n \ndiff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h\nindex 5aa352c36f..e8f3583aa6 100644\n--- a/src/v4l2/v4l2_camera_proxy.h\n+++ b/src/v4l2/v4l2_camera_proxy.h\n@@ -44,7 +44,6 @@ private:\n \tvoid querycap(std::shared_ptr<libcamera::Camera> camera);\n \tint tryFormat(struct v4l2_format *arg);\n \tenum v4l2_priority maxPriority();\n-\tvoid updateBuffers();\n \tvoid freeBuffers();\n \n \tint vidioc_querycap(V4L2CameraFile *file, struct v4l2_capability *arg);\n@@ -81,7 +80,6 @@ private:\n \n \tlibcamera::StreamConfiguration streamConfig_;\n \tunsigned int bufferCount_;\n-\tunsigned int currentBuf_;\n \tunsigned int sizeimage_;\n \n \tstruct v4l2_capability capabilities_;\n",
    "prefixes": [
        "RFC",
        "v1",
        "27/27"
    ]
}