Show a patch.

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

{
    "id": 24452,
    "url": "https://patchwork.libcamera.org/api/patches/24452/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/24452/",
    "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": "<20250924124713.3361707-7-barnabas.pocze@ideasonboard.com>",
    "date": "2025-09-24T12:47:11",
    "name": "[RFC,v1,6/7] libcamera: camera: Add `applyControls()`",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "ca1d4b2f2b617f54d6dc82344c78f18a134a6398",
    "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/24452/mbox/",
    "series": [
        {
            "id": 5457,
            "url": "https://patchwork.libcamera.org/api/series/5457/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=5457",
            "date": "2025-09-24T12:47:05",
            "name": "libcamera: camera: Add `applyControls()`",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/5457/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/24452/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/24452/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 3AB5ABDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 24 Sep 2025 12:47:42 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 34EB46B614;\n\tWed, 24 Sep 2025 14:47:36 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 307D96B5FB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 24 Sep 2025 14:47:18 +0200 (CEST)",
            "from pb-laptop.local (185.221.140.70.nat.pool.zt.hu\n\t[185.221.140.70])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3D26A1E36;\n\tWed, 24 Sep 2025 14:45:54 +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=\"XHyZP5CK\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1758717954;\n\tbh=rRY8KqO5CkXTsdWJUkqirBQf/6DBZyG/gB8dlDWRQKM=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=XHyZP5CKunzZsY+Bsj1gOsEP8bKMqQamvZv9Hd3ORuCclchRZ7/x3m1mwTZjMP08a\n\tGdrJKs5qqr4Z47d/cBDBIbMNC99QRqQ6hcTjCWeETSJpD68eJbJDOD7dLXHWT5NX2X\n\tbpBn5v/av6lJL4//VTyw8Zlpp+g7Th/QnC5YFK8I=",
        "From": "=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>",
        "Subject": "[RFC PATCH v1 6/7] libcamera: camera: Add `applyControls()`",
        "Date": "Wed, 24 Sep 2025 14:47:11 +0200",
        "Message-ID": "<20250924124713.3361707-7-barnabas.pocze@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.51.0",
        "In-Reply-To": "<20250924124713.3361707-1-barnabas.pocze@ideasonboard.com>",
        "References": "<20250924124713.3361707-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": "Add `Camera::applyControls()` whose purpose is to apply controls as soon\nas possible, without going through `Request::controls()`.\n\nA new virtual function `PipelineHandler::applyControlsDevice()` is provided\nfor pipeline handler to implement fast-tracked application of controls. If\nthe pipeline handler does not implement that functionality, or it fails,\nthen a fallback mechanism is used. The controls will be saved for later,\nand they will be merged into the control list of the next request sent to\nthe pipeline handler (`Camera::Private::waitingRequests_`).\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n---\n include/libcamera/camera.h                    |   1 +\n include/libcamera/internal/camera.h           |   1 +\n include/libcamera/internal/pipeline_handler.h |   7 ++\n src/libcamera/camera.cpp                      |  53 +++++++++\n src/libcamera/pipeline_handler.cpp            | 110 +++++++++++++++++-\n 5 files changed, 171 insertions(+), 1 deletion(-)",
    "diff": "diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h\nindex b24a29740..94f940496 100644\n--- a/include/libcamera/camera.h\n+++ b/include/libcamera/camera.h\n@@ -147,6 +147,7 @@ public:\n \n \tstd::unique_ptr<Request> createRequest(uint64_t cookie = 0);\n \tint queueRequest(Request *request);\n+\tint applyControls(ControlList &&controls);\n \n \tint start(const ControlList *controls = nullptr);\n \tint stop();\ndiff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h\nindex 0f50c14a5..5a77298b3 100644\n--- a/include/libcamera/internal/camera.h\n+++ b/include/libcamera/internal/camera.h\n@@ -38,6 +38,7 @@ public:\n \n \tstd::list<Request *> queuedRequests_;\n \tstd::deque<Request *> waitingRequests_;\n+\tControlList pendingControls_;\n \tControlInfoMap controlInfo_;\n \tControlList properties_;\n \ndiff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h\nindex e89d6a33e..16ff54bab 100644\n--- a/include/libcamera/internal/pipeline_handler.h\n+++ b/include/libcamera/internal/pipeline_handler.h\n@@ -57,6 +57,7 @@ public:\n \n \tvoid registerRequest(Request *request);\n \tvoid queueRequest(Request *request);\n+\tint applyControls(Camera *camera, ControlList &&controls);\n \n \tbool completeBuffer(Request *request, FrameBuffer *buffer);\n \tvoid completeRequest(Request *request);\n@@ -75,6 +76,12 @@ protected:\n \tvoid hotplugMediaDevice(MediaDevice *media);\n \n \tvirtual int queueRequestDevice(Camera *camera, Request *request) = 0;\n+\n+\tvirtual int applyControlsDevice([[maybe_unused]] Camera *camera, [[maybe_unused]] const ControlList &controls)\n+\t{\n+\t\treturn -EOPNOTSUPP;\n+\t}\n+\n \tvirtual void stopDevice(Camera *camera) = 0;\n \n \tvirtual bool acquireDevice(Camera *camera);\ndiff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 2e1e146a2..778445e36 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -637,6 +637,14 @@ Camera::Private::~Private()\n  * queued requests was reached.\n  */\n \n+/**\n+ * \\var Camera::Private::pendingControls_\n+ * \\brief The list of pending controls\n+ *\n+ * This list tracks the controls that need to be applied to the device with the\n+ * next request going to PipelineHandler::queueRequestDevice().\n+ */\n+\n /**\n  * \\var Camera::Private::controlInfo_\n  * \\brief The set of controls supported by the camera\n@@ -1374,6 +1382,51 @@ int Camera::queueRequest(Request *request)\n \treturn 0;\n }\n \n+/**\n+ * \\brief Apply controls immediately\n+ * \\param[in] controls The list of controls to apply\n+ *\n+ * This function tries to ensure that the controls in \\a controls are applied\n+ * to the camera as soon as possible.\n+ *\n+ * The exact guarantees are camera dependent, but it is guaranteed that the\n+ * controls will be applied no later than if they were part of the next \\ref Request \"request\"\n+ * that the application \\ref Camera::queueRequest() \"queues\".\n+ *\n+ * \\context This function is \\threadsafe. It may only be called when the camera\n+ * is in the Running state as defined in \\ref camera_operation.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ * \\retval -ENODEV The camera has been disconnected from the system\n+ * \\retval -EACCES The camera is not running\n+ */\n+int Camera::applyControls(ControlList &&controls)\n+{\n+\tPrivate *const d = _d();\n+\n+\t/*\n+\t * \\todo What to do if not running? Should it be stored and merged into the \"start\" `ControlList`?\n+\t *       If yes, should that list of pending controls be overwritten/updated when stopped?\n+\t *       And should it be cleared in `Camera::release()`?\n+\t */\n+\n+\tint ret = d->isAccessAllowed(Private::CameraRunning);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tif (controls.empty())\n+\t\treturn 0;\n+\n+\tpatchControlList(controls);\n+\n+\t/*\n+\t * \\todo Or `ConnectionTypeBlocking` to get the return value?\n+\t */\n+\td->pipe_->invokeMethod(&PipelineHandler::applyControls, ConnectionTypeQueued, this, std::move(controls));\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Start capture from camera\n  * \\param[in] controls Controls to be applied before starting the Camera\ndiff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\nindex d0d3fbc79..99bb2193f 100644\n--- a/src/libcamera/pipeline_handler.cpp\n+++ b/src/libcamera/pipeline_handler.cpp\n@@ -387,6 +387,12 @@ void PipelineHandler::stop(Camera *camera)\n \tASSERT(data->queuedRequests_.empty());\n \tASSERT(data->waitingRequests_.empty());\n \n+\t/*\n+\t * \\todo Or not? This probably needs discussions regarding\n+\t * the expected state of controls after a stop-start sequence.\n+\t */\n+\tdata->pendingControls_.clear();\n+\n \tdata->requestSequence_ = 0;\n }\n \n@@ -466,6 +472,54 @@ void PipelineHandler::queueRequest(Request *request)\n \trequest->_d()->prepare(300ms);\n }\n \n+/**\n+ * \\brief Apply controls immediately\n+ * \\param[in] camera The camera\n+ * \\param[in] controls The controls to apply\n+ *\n+ * This function tries to apply \\a controls immediately to the device by\n+ * calling applyControlsDevice(). If that fails, then a fallback mechanism\n+ * is used to ensure that \\a controls will be merged into the control list\n+ * of the next request submitted to the pipeline handler.\n+ *\n+ * This function also ensures that requests in Camera::Private::waitingRequests_\n+ * will not override the fast-tracked controls.\n+ *\n+ * \\context This function is called from the CameraManager thread.\n+ */\n+int PipelineHandler::applyControls(Camera *camera, ControlList &&controls)\n+{\n+\tCamera::Private *data = camera->_d();\n+\tint res = applyControlsDevice(camera, controls);\n+\n+\t/*\n+\t * Prevent later requests from overriding the fast-tracked controls.\n+\t * \\todo This is unfortunately slow.\n+\t */\n+\tfor (const auto &[k, v] : controls) {\n+\t\tfor (Request *r : data->waitingRequests_)\n+\t\t\tr->controls().erase(k);\n+\t}\n+\n+\tif (res < 0) {\n+\t\t/*\n+\t\t * \\todo Always fall back or only if EOPNOTSUPP is returned?\n+\t\t *       Possibly there could be some way for the user to influence\n+\t\t *       the fallback logic (e.g. `flags` argument for `Camera::applyControls()`).\n+\t\t */\n+\t\tif (res != -EOPNOTSUPP)\n+\t\t\tLOG(Pipeline, Debug) << \"Fast tracking controls failed: \" << res;\n+\n+\t\t/*\n+\t\t * Fall back to adding the controls to the next request that enters the\n+\t\t * pipeline handler. See PipelineHandler::doQueueRequest().\n+\t\t */\n+\t\tdata->pendingControls_.merge(std::move(controls), ControlList::MergePolicy::OverwriteExisting);\n+\t}\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Queue one requests to the device\n  */\n@@ -484,9 +538,49 @@ void PipelineHandler::doQueueRequest(Request *request)\n \t\treturn;\n \t}\n \n+\tif (!data->pendingControls_.empty()) {\n+\t\t/*\n+\t\t * Note that `ControlList::MergePolicy::KeepExisting` is used. This is\n+\t\t * needed to ensure that if `request` is newer than pendingControls_,\n+\t\t * then its controls take precedence.\n+\t\t *\n+\t\t * For older requests, `PipelineHandler::applyControls()` has already removed\n+\t\t * the controls that should be overridden.\n+\t\t *\n+\t\t * \\todo How to handle (if at all) conflicting controls?\n+\t\t * \\todo How to handle failure of `queueRequestDevice()`?\n+\t\t *\t After the merge it becomes impossible to retrieve the pending controls\n+\t\t *\t from `request->controls()`. Making a copy in such a hot-path is not ideal.\n+\t\t */\n+\t\trequest->controls().merge(std::move(data->pendingControls_), ControlList::MergePolicy::KeepExisting);\n+\t\tdata->pendingControls_.clear();\n+\t}\n+\n \tint ret = queueRequestDevice(camera, request);\n-\tif (ret)\n+\tif (ret) {\n+\t\t/*\n+\t\t * \\todo What to do now?\n+\t\t *\n+\t\t * The original `pendingControls_` is not easily recoverable.\n+\t\t *\n+\t\t * Initially\n+\t\t *     request->controls() = A u B\n+\t\t *     pendingControls_ = A' u C\n+\t\t *\n+\t\t * then after the above merge\n+\t\t *     request->controls() = A u B u C\n+\t\t *     pendingControls_ = A'\n+\t\t *\n+\t\t * so recovering `pendingControls_` without a copy of at least\n+\t\t * `B` or `C` does not appear easy.\n+\t\t *\n+\t\t * But sometimes recovering is not even desirable: assume that the controls\n+\t\t * in `pendingControls_` causes the failure. In that case keeping them around\n+\t\t * will cause every single subsequent request to fail.\n+\t\t */\n+\n \t\tcancelRequest(request);\n+\t}\n }\n \n /**\n@@ -532,6 +626,20 @@ void PipelineHandler::doQueueRequests(Camera *camera)\n  * \\return 0 on success or a negative error code otherwise\n  */\n \n+/**\n+ * \\fn PipelineHandler::applyControlsDevice()\n+ * \\brief Apply controls immediately\n+ * \\param[in] camera The camera\n+ * \\param[in] controls The controls to apply\n+ *\n+ * This function applies \\a controls to \\a camera immediately.\n+ *\n+ * \\context This function is called from the CameraManager thread.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ * \\return -EOPNOTSUPP if fast-tracking controls is not supported\n+ */\n+\n /**\n  * \\brief Complete a buffer for a request\n  * \\param[in] request The request the buffer belongs to\n",
    "prefixes": [
        "RFC",
        "v1",
        "6/7"
    ]
}