{"id":14969,"url":"https://patchwork.libcamera.org/api/1.1/patches/14969/?format=json","web_url":"https://patchwork.libcamera.org/patch/14969/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211201142936.107405-10-jacopo@jmondi.org>","date":"2021-12-01T14:29:34","name":"[libcamera-devel,v4,09/11] libcamera: request: Add Request::Private::prepare()","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"470a4f28a12e40358221acaa7f5d31058cb2aae0","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":{"id":15,"url":"https://patchwork.libcamera.org/api/1.1/users/15/?format=json","username":"jmondi","first_name":"Jacopo","last_name":"Mondi","email":"jacopo@jmondi.org"},"mbox":"https://patchwork.libcamera.org/patch/14969/mbox/","series":[{"id":2795,"url":"https://patchwork.libcamera.org/api/1.1/series/2795/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2795","date":"2021-12-01T14:29:25","name":"libcamera: Add support for Fence","version":4,"mbox":"https://patchwork.libcamera.org/series/2795/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14969/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14969/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 EC87EC3251\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  1 Dec 2021 14:28:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8A7ED607C5;\n\tWed,  1 Dec 2021 15:28:59 +0100 (CET)","from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net\n\t[217.70.183.198])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5FDC260743\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  1 Dec 2021 15:28:58 +0100 (CET)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 874D5C0002;\n\tWed,  1 Dec 2021 14:28:57 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  1 Dec 2021 15:29:34 +0100","Message-Id":"<20211201142936.107405-10-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.33.1","In-Reply-To":"<20211201142936.107405-1-jacopo@jmondi.org>","References":"<20211201142936.107405-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 09/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>\n---\n include/libcamera/internal/request.h |  15 +++\n src/libcamera/request.cpp            | 132 ++++++++++++++++++++++++++-\n 2 files changed, 146 insertions(+), 1 deletion(-)","diff":"diff --git a/include/libcamera/internal/request.h b/include/libcamera/internal/request.h\nindex 1340ffa2a683..740ab21ac7e0 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,24 @@ 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 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 5ddb4b0592b3..59fb02b4a0b7 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@@ -163,6 +165,134 @@ void Request::Private::reuse()\n \tsequence_ = 0;\n \tcancelled_ = false;\n \tpending_.clear();\n+\tnotifiers_.clear();\n+\ttimer_.reset();\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+\tprepared_ = false;\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+\n+\t\tif (!fence)\n+\t\t\tcontinue;\n+\n+\t\tnotifiers_[buffer] = std::make_unique<EventNotifier>(fence->fd().get(),\n+\t\t\t\t\t\t\t\t     EventNotifier::Read);\n+\t}\n+\n+\tif (notifiers_.empty()) {\n+\t\tprepared_ = true;\n+\t\tprepared.emit();\n+\t\treturn;\n+\t}\n+\n+\tfor (auto &it : notifiers_) {\n+\t\tFrameBuffer *buffer = it.first;\n+\t\tstd::unique_ptr<EventNotifier> &notifier = it.second;\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+\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 (prepared_\n+ * == true) and is ready for being queued. The Request might complete with\n+ * errors in which case it is 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+\tprepared_ = true;\n+\tprepared.emit();\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+\tprepared_ = true;\n+\tprepared.emit();\n }\n \n /**\n@@ -314,7 +444,7 @@ void Request::reuse(ReuseFlag flags)\n  * responsible for resetting it before associating this buffer with a new\n  * Request by calling this function again.\n  *\n- * \\sa FrameBuffer::resetFence()\n+ * \\sa FrameBuffer::releaseFence()\n  *\n  * \\return 0 on success or a negative error code otherwise\n  * \\retval -EEXIST The request already contains a buffer for the stream\n","prefixes":["libcamera-devel","v4","09/11"]}