From patchwork Thu Apr 1 03:46:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 11805 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2C805C0DA3 for ; Thu, 1 Apr 2021 03:48:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8081E60517; Thu, 1 Apr 2021 05:48:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="nel92CdJ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 37535602D2 for ; Thu, 1 Apr 2021 05:48:03 +0200 (CEST) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 748A8F7; Thu, 1 Apr 2021 05:48:01 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1617248882; bh=+hQgYehDOuvzIjJH9GixvuUGRe8PHgnclfsALsdws5s=; h=From:To:Cc:Subject:Date:From; b=nel92CdJn8AvUSXSTo6Nx4BkndalEfXiaadaLPGQbdrG7OVzDjyafcjK7ALf3Xq3D wqo4fowh8CQkVV0RyQXebH7aLG+t60SNuuD6Z0FyR/sUiL0yoWBy2iZgLKKhZ9PIYK JK9u0GW82CmRBqehl9+O7tO1+RXcILz3xmG9b3SY= From: Paul Elder 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From the perspective of a Buffer, a Request can suddenly become nullptr in the middle of a stream. As Requests are supposed to be atomic, and they are the nucleus of the libcamera camera streaming API, it would be convenient if we could set the rate of decay of Requests. Add a half life member variable to Request, and a setHalfLife() member function to set this. This will affect whether the Request is reusable or not, so Request::reuse() can now fail. Signed-off-by: Paul Elder --- TODO: change the applications to reuse the request if reuse() fails --- include/libcamera/request.h | 8 +++++++- src/libcamera/request.cpp | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/include/libcamera/request.h b/include/libcamera/request.h index 4cf5ff3f..45c707f3 100644 --- a/include/libcamera/request.h +++ b/include/libcamera/request.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_REQUEST_H__ #define __LIBCAMERA_REQUEST_H__ +#include #include #include #include @@ -43,7 +44,7 @@ public: Request(Camera *camera, uint64_t cookie = 0); ~Request(); - void reuse(ReuseFlag flags = Default); + bool reuse(ReuseFlag flags = Default); ControlList &controls() { return *controls_; } ControlList &metadata() { return *metadata_; } @@ -57,6 +58,8 @@ public: bool hasPendingBuffers() const { return !pending_.empty(); } + void setHalfLife(std::chrono::duration hl); + std::string toString() const; private: @@ -79,6 +82,9 @@ private: const uint64_t cookie_; Status status_; bool cancelled_; + + std::chrono::steady_clock::time_point birthTime_; + std::chrono::duration halfLife_; }; } /* namespace libcamera */ diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp index ce2dd7b1..0543abaf 100644 --- a/src/libcamera/request.cpp +++ b/src/libcamera/request.cpp @@ -7,7 +7,9 @@ #include +#include #include +#include #include #include @@ -74,7 +76,9 @@ LOG_DEFINE_CATEGORY(Request) */ Request::Request(Camera *camera, uint64_t cookie) : camera_(camera), sequence_(0), cookie_(cookie), - status_(RequestPending), cancelled_(false) + status_(RequestPending), cancelled_(false), + birthTime_(std::chrono::steady_clock::now()), + halfLife_(std::chrono::seconds(10)) { /** * \todo Should the Camera expose a validator instance, to avoid @@ -111,11 +115,28 @@ Request::~Request() * prior to queueing the request to the camera, in lieu of constructing a new * request. The application can reuse the buffers that were previously added * to the request via addBuffer() by setting \a flags to ReuseBuffers. + * + * The request for reuse may fail, as Requests can decay based on the half + * life. + * + * \return True on success, false otherwise */ -void Request::reuse(ReuseFlag flags) +bool Request::reuse(ReuseFlag flags) { LIBCAMERA_TRACEPOINT(request_reuse, this); + std::chrono::steady_clock::time_point end = + std::chrono::steady_clock::now(); + std::chrono::duration diff = end - birthTime_; + + std::random_device rd{}; + std::mt19937 gen{ rd() }; + double hlns = + std::chrono::duration_cast(halfLife_).count(); + std::normal_distribution<> dist{ hlns, hlns / 2 }; + if (dist(gen) > hlns) + return false; + pending_.clear(); if (flags & ReuseBuffers) { for (auto pair : bufferMap_) { @@ -133,6 +154,8 @@ void Request::reuse(ReuseFlag flags) controls_->clear(); metadata_->clear(); + + return true; } /** @@ -320,6 +343,15 @@ bool Request::completeBuffer(FrameBuffer *buffer) return !hasPendingBuffers(); } +/** + * \brief Set the half life of the request + * \param[in] hl Half-life of the request + */ +void Request::setHalfLife(std::chrono::duration hl) +{ + halfLife_ = hl; +} + /** * \brief Generate a string representation of the Request internals *