Show a patch.

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

{
    "id": 15131,
    "url": "https://patchwork.libcamera.org/api/patches/15131/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/15131/",
    "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": "<20211210205239.354901-9-jacopo@jmondi.org>",
    "date": "2021-12-10T20:52:36",
    "name": "[libcamera-devel,v5,08/11] libcamera: request: Add Request::Private::prepare()",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "479432c24069f84e2cdf4b176061cd7c2aa7d3fd",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/15131/mbox/",
    "series": [
        {
            "id": 2837,
            "url": "https://patchwork.libcamera.org/api/series/2837/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2837",
            "date": "2021-12-10T20:52:28",
            "name": "libcamera: Add fence",
            "version": 5,
            "mbox": "https://patchwork.libcamera.org/series/2837/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/15131/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/15131/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 86934C3258\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 10 Dec 2021 20:52:04 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0F1F46089F;\n\tFri, 10 Dec 2021 21:52:04 +0100 (CET)",
            "from relay11.mail.gandi.net (relay11.mail.gandi.net\n\t[217.70.178.231])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6EBF260890\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 10 Dec 2021 21:51:57 +0100 (CET)",
            "(Authenticated sender: jacopo@jmondi.org)\n\tby relay11.mail.gandi.net (Postfix) with ESMTPSA id C3AE1100007;\n\tFri, 10 Dec 2021 20:51:56 +0000 (UTC)"
        ],
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Fri, 10 Dec 2021 21:52:36 +0100",
        "Message-Id": "<20211210205239.354901-9-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.33.1",
        "In-Reply-To": "<20211210205239.354901-1-jacopo@jmondi.org>",
        "References": "<20211210205239.354901-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v5 08/11] libcamera: request: Add\n\tRequest::Private::prepare()",
        "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 a prepare() function to the Private Request representation.\n\nThe prepare() function is used by the PipelineHandler class to\nprepare a Request to be queued to the hardware.\n\nThe current implementation of prepare() handles the fences associated\nwith the Framebuffers part of a Request. The function starts an event\nnotifier for each of those and emits the Request::prepared signal when\nall fences have been signalled or an optional timeout has expired.\n\nThe optional timeout allows to interrupt blocked waits and notify the\nRequest as failed so that it can be cancelled.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/request.h |  16 ++++\n src/libcamera/request.cpp            | 133 +++++++++++++++++++++++++++\n 2 files changed, 149 insertions(+)",
    "diff": "diff --git a/include/libcamera/internal/request.h b/include/libcamera/internal/request.h\nindex 1340ffa2a683..1f2499896e23 100644\n--- a/include/libcamera/internal/request.h\n+++ b/include/libcamera/internal/request.h\n@@ -7,10 +7,17 @@\n #ifndef __LIBCAMERA_INTERNAL_REQUEST_H__\n #define __LIBCAMERA_INTERNAL_REQUEST_H__\n \n+#include <chrono>\n+#include <map>\n #include <memory>\n \n+#include <libcamera/base/event_notifier.h>\n+#include <libcamera/base/timer.h>\n+\n #include <libcamera/request.h>\n \n+using namespace std::chrono_literals;\n+\n namespace libcamera {\n \n class Camera;\n@@ -32,16 +39,25 @@ public:\n \tvoid cancel();\n \tvoid reuse();\n \n+\tvoid prepare(std::chrono::milliseconds timeout = 0ms);\n+\tSignal<> prepared;\n+\n private:\n \tfriend class PipelineHandler;\n \n \tvoid doCancelRequest();\n+\tvoid emitPrepareCompleted();\n+\tvoid notifierActivated(FrameBuffer *buffer);\n+\tvoid timeout();\n \n \tCamera *camera_;\n \tbool cancelled_;\n \tuint32_t sequence_ = 0;\n+\tbool prepared_ = false;\n \n \tstd::unordered_set<FrameBuffer *> pending_;\n+\tstd::map<FrameBuffer *, std::unique_ptr<EventNotifier>> notifiers_;\n+\tstd::unique_ptr<Timer> timer_;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp\nindex 016db9d62340..7e9238dc1eb4 100644\n--- a/src/libcamera/request.cpp\n+++ b/src/libcamera/request.cpp\n@@ -135,6 +135,8 @@ void Request::Private::doCancelRequest()\n \n \tcancelled_ = true;\n \tpending_.clear();\n+\tnotifiers_.clear();\n+\ttimer_.reset();\n }\n \n /**\n@@ -162,7 +164,138 @@ void Request::Private::reuse()\n {\n \tsequence_ = 0;\n \tcancelled_ = false;\n+\tprepared_ = false;\n \tpending_.clear();\n+\tnotifiers_.clear();\n+\ttimer_.reset();\n+}\n+\n+/*\n+ * Helper function to save some lines of code and make sure prepared_ is set\n+ * to true before emitting the signal.\n+ */\n+void Request::Private::emitPrepareCompleted()\n+{\n+\tprepared_ = true;\n+\tprepared.emit();\n+}\n+\n+/**\n+ * \\brief Prepare the Request to be queued to the device\n+ * \\param[in] timeout Optional expiration timeout\n+ *\n+ * Prepare a Request to be queued to the hardware device by ensuring it is\n+ * ready for the incoming memory transfers.\n+ *\n+ * This currently means waiting on each frame buffer acquire fence to be\n+ * signalled. An optional expiration timeout can be specified. If not all the\n+ * fences have been signalled correctly before the timeout expires the Request\n+ * is cancelled.\n+ *\n+ * The function immediately emits the prepared signal if all the prepare\n+ * operations have been completed synchronously. If instead the prepare\n+ * operations require to wait the completion of asynchronous events, such as\n+ * fences notifications or timer expiration, the prepared signal is emitted upon\n+ * the asynchronous event completion.\n+ *\n+ * As we currently only handle fences, the function emits the prepared signal\n+ * immediately if there are no fences to wait on. Otherwise the prepared signal\n+ * is emitted when all fences have been signalled or the optional timeout has\n+ * expired.\n+ *\n+ * If not all the fences have been correctly signalled or the optional timeout\n+ * has expired the Request will be cancelled and the Request::prepared signal\n+ * emitted.\n+ *\n+ * The intended user of this function is the PipelineHandler base class, which\n+ * 'prepares' a Request before queuing it to the hardware device.\n+ */\n+void Request::Private::prepare(std::chrono::milliseconds timeout)\n+{\n+\t/* Create and connect one notifier for each synchronization fence. */\n+\tfor (FrameBuffer *buffer : pending_) {\n+\t\tconst Fence *fence = buffer->_d()->fence();\n+\t\tif (!fence)\n+\t\t\tcontinue;\n+\n+\t\tstd::unique_ptr<EventNotifier> notifier =\n+\t\t\tstd::make_unique<EventNotifier>(fence->fd().get(),\n+\t\t\t\t\t\t\tEventNotifier::Read);\n+\n+\t\tnotifier->activated.connect(this, [this, buffer] {\n+\t\t\t\t\t\t\tnotifierActivated(buffer);\n+\t\t\t\t\t    });\n+\n+\t\tnotifiers_[buffer] = std::move(notifier);\n+\t}\n+\n+\tif (notifiers_.empty()) {\n+\t\temitPrepareCompleted();\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * In case a timeout is specified, create a timer and set it up.\n+\t *\n+\t * The timer must be created here instead of in the Request constructor,\n+\t * in order to be bound to the pipeline handler thread.\n+\t */\n+\tif (timeout == 0ms) {\n+\t\ttimer_ = std::make_unique<Timer>();\n+\t\ttimer_->timeout.connect(this, &Request::Private::timeout);\n+\t\ttimer_->start(timeout);\n+\t}\n+}\n+\n+/**\n+ * \\var Request::Private::prepared\n+ * \\brief Request preparation completed Signal\n+ *\n+ * The signal is emitted once the request preparation has completed and is ready\n+ * to be queued. The Request might complete with errors in which case it is\n+ * cancelled.\n+ *\n+ * The intended slot for this signal is the PipelineHandler::doQueueRequests()\n+ * function which queues Request after they have been prepared or cancel them\n+ * if they have failed preparing.\n+ */\n+\n+void Request::Private::notifierActivated(FrameBuffer *buffer)\n+{\n+\t/* Close the fence if successfully signalled. */\n+\tASSERT(buffer);\n+\tbuffer->releaseFence();\n+\n+\t/* Remove the entry from the map and check if other fences are pending. */\n+\tauto it = notifiers_.find(buffer);\n+\tASSERT(it != notifiers_.end());\n+\tnotifiers_.erase(it);\n+\n+\tRequest *request = _o<Request>();\n+\tLOG(Request, Debug)\n+\t\t<< \"Request \" << request->cookie() << \" buffer \" << buffer\n+\t\t<< \" fence signalled\";\n+\n+\tif (!notifiers_.empty())\n+\t\treturn;\n+\n+\t/* All fences completed, delete the timer and move to state Ready. */\n+\ttimer_.reset();\n+\temitPrepareCompleted();\n+}\n+\n+void Request::Private::timeout()\n+{\n+\t/* A timeout can only happen if there are fences not yet signalled. */\n+\tASSERT(!notifiers_.empty());\n+\tnotifiers_.clear();\n+\n+\tRequest *request = _o<Request>();\n+\tLOG(Request, Debug) << \"Request prepare timeout: \" << request->cookie();\n+\n+\tcancel();\n+\n+\temitPrepareCompleted();\n }\n \n /**\n",
    "prefixes": [
        "libcamera-devel",
        "v5",
        "08/11"
    ]
}