{"id":11805,"url":"https://patchwork.libcamera.org/api/patches/11805/?format=json","web_url":"https://patchwork.libcamera.org/patch/11805/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20210401034648.136223-1-paul.elder@ideasonboard.com>","date":"2021-04-01T03:46:48","name":"[libcamera-devel,RFC] libcamera: Request: Add setHalfLife","commit_ref":null,"pull_url":null,"state":"not-applicable","archived":false,"hash":"8ef11de0fe1e8412a10b066401ecdc645a42fa5c","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11805/mbox/","series":[{"id":1877,"url":"https://patchwork.libcamera.org/api/series/1877/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1877","date":"2021-04-01T03:46:48","name":"[libcamera-devel,RFC] libcamera: Request: Add setHalfLife","version":1,"mbox":"https://patchwork.libcamera.org/series/1877/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11805/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11805/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 2C805C0DA3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  1 Apr 2021 03:48:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8081E60517;\n\tThu,  1 Apr 2021 05:48:04 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 37535602D2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  1 Apr 2021 05:48:03 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2400:4051:61:600:2c71:1b79:d06d:5032])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 748A8F7;\n\tThu,  1 Apr 2021 05:48:01 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"nel92CdJ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1617248882;\n\tbh=+hQgYehDOuvzIjJH9GixvuUGRe8PHgnclfsALsdws5s=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=nel92CdJn8AvUSXSTo6Nx4BkndalEfXiaadaLPGQbdrG7OVzDjyafcjK7ALf3Xq3D\n\twqo4fowh8CQkVV0RyQXebH7aLG+t60SNuuD6Z0FyR/sUiL0yoWBy2iZgLKKhZ9PIYK\n\tJK9u0GW82CmRBqehl9+O7tO1+RXcILz3xmG9b3SY=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  1 Apr 2021 12:46:48 +0900","Message-Id":"<20210401034648.136223-1-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH] libcamera: Request: Add setHalfLife","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From the perspective of a Buffer, a Request can suddenly become nullptr\nin the middle of a stream. As Requests are supposed to be atomic, and\nthey are the nucleus of the libcamera camera streaming API, it would be\nconvenient if we could set the rate of decay of Requests.\n\nAdd a half life member variable to Request, and a setHalfLife() member\nfunction to set this. This will affect whether the Request is reusable\nor not, so Request::reuse() can now fail.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nTODO: change the applications to reuse the request if reuse() fails\n---\n include/libcamera/request.h |  8 +++++++-\n src/libcamera/request.cpp   | 36 ++++++++++++++++++++++++++++++++++--\n 2 files changed, 41 insertions(+), 3 deletions(-)","diff":"diff --git a/include/libcamera/request.h b/include/libcamera/request.h\nindex 4cf5ff3f..45c707f3 100644\n--- a/include/libcamera/request.h\n+++ b/include/libcamera/request.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_REQUEST_H__\n #define __LIBCAMERA_REQUEST_H__\n \n+#include <chrono>\n #include <map>\n #include <memory>\n #include <stdint.h>\n@@ -43,7 +44,7 @@ public:\n \tRequest(Camera *camera, uint64_t cookie = 0);\n \t~Request();\n \n-\tvoid reuse(ReuseFlag flags = Default);\n+\tbool reuse(ReuseFlag flags = Default);\n \n \tControlList &controls() { return *controls_; }\n \tControlList &metadata() { return *metadata_; }\n@@ -57,6 +58,8 @@ public:\n \n \tbool hasPendingBuffers() const { return !pending_.empty(); }\n \n+\tvoid setHalfLife(std::chrono::duration<double> hl);\n+\n \tstd::string toString() const;\n \n private:\n@@ -79,6 +82,9 @@ private:\n \tconst uint64_t cookie_;\n \tStatus status_;\n \tbool cancelled_;\n+\n+\tstd::chrono::steady_clock::time_point birthTime_;\n+\tstd::chrono::duration<double> halfLife_;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp\nindex ce2dd7b1..0543abaf 100644\n--- a/src/libcamera/request.cpp\n+++ b/src/libcamera/request.cpp\n@@ -7,7 +7,9 @@\n \n #include <libcamera/request.h>\n \n+#include <chrono>\n #include <map>\n+#include <random>\n #include <sstream>\n \n #include <libcamera/buffer.h>\n@@ -74,7 +76,9 @@ LOG_DEFINE_CATEGORY(Request)\n  */\n Request::Request(Camera *camera, uint64_t cookie)\n \t: camera_(camera), sequence_(0), cookie_(cookie),\n-\t  status_(RequestPending), cancelled_(false)\n+\t  status_(RequestPending), cancelled_(false),\n+\t  birthTime_(std::chrono::steady_clock::now()),\n+\t  halfLife_(std::chrono::seconds(10))\n {\n \t/**\n \t * \\todo Should the Camera expose a validator instance, to avoid\n@@ -111,11 +115,28 @@ Request::~Request()\n  * prior to queueing the request to the camera, in lieu of constructing a new\n  * request. The application can reuse the buffers that were previously added\n  * to the request via addBuffer() by setting \\a flags to ReuseBuffers.\n+ *\n+ * The request for reuse may fail, as Requests can decay based on the half\n+ * life.\n+ *\n+ * \\return True on success, false otherwise\n  */\n-void Request::reuse(ReuseFlag flags)\n+bool Request::reuse(ReuseFlag flags)\n {\n \tLIBCAMERA_TRACEPOINT(request_reuse, this);\n \n+\tstd::chrono::steady_clock::time_point end =\n+\t\tstd::chrono::steady_clock::now();\n+\tstd::chrono::duration<double> diff = end - birthTime_;\n+\n+\tstd::random_device rd{};\n+\tstd::mt19937 gen{ rd() };\n+\tdouble hlns =\n+\t\tstd::chrono::duration_cast<std::chrono::nanoseconds>(halfLife_).count();\n+\tstd::normal_distribution<> dist{ hlns, hlns / 2 };\n+\tif (dist(gen) > hlns)\n+\t\treturn false;\n+\n \tpending_.clear();\n \tif (flags & ReuseBuffers) {\n \t\tfor (auto pair : bufferMap_) {\n@@ -133,6 +154,8 @@ void Request::reuse(ReuseFlag flags)\n \n \tcontrols_->clear();\n \tmetadata_->clear();\n+\n+\treturn true;\n }\n \n /**\n@@ -320,6 +343,15 @@ bool Request::completeBuffer(FrameBuffer *buffer)\n \treturn !hasPendingBuffers();\n }\n \n+/**\n+ * \\brief Set the half life of the request\n+ * \\param[in] hl Half-life of the request\n+ */\n+void Request::setHalfLife(std::chrono::duration<double> hl)\n+{\n+\thalfLife_ = hl;\n+}\n+\n /**\n  * \\brief Generate a string representation of the Request internals\n  *\n","prefixes":["libcamera-devel","RFC"]}