From patchwork Wed Sep 9 15:54:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9562 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 78114BDB1E for ; Wed, 9 Sep 2020 15:51:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4821F62D68; Wed, 9 Sep 2020 17:51:24 +0200 (CEST) Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1055262D04 for ; Wed, 9 Sep 2020 17:51:23 +0200 (CEST) X-Originating-IP: 93.34.118.233 Received: from uno.lan (93-34-118-233.ip49.fastwebnet.it [93.34.118.233]) (Authenticated sender: jacopo@jmondi.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 2FDFF1BF208; Wed, 9 Sep 2020 15:51:22 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 9 Sep 2020 17:54:54 +0200 Message-Id: <20200909155457.153907-6-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200909155457.153907-1-jacopo@jmondi.org> References: <20200909155457.153907-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 5/8] libcamera: framebuffer_allocator: Get and return buffers 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: , Cc: hanlinchen@chromium.org Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add to the FrameBufferAllocator class two methods to get and return buffers from the pool of buffers allocated for a Stream. The two methods return pointer to the allocated buffers without transferring ownership to the caller. Signed-off-by: Jacopo Mondi --- include/libcamera/framebuffer_allocator.h | 5 ++ src/libcamera/framebuffer_allocator.cpp | 60 +++++++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/include/libcamera/framebuffer_allocator.h b/include/libcamera/framebuffer_allocator.h index 2a4d538a0cb2..1f3f10d4ec03 100644 --- a/include/libcamera/framebuffer_allocator.h +++ b/include/libcamera/framebuffer_allocator.h @@ -9,6 +9,7 @@ #include #include +#include #include namespace libcamera { @@ -33,9 +34,13 @@ public: bool allocated() const { return !buffers_.empty(); } const std::vector> &buffers(Stream *stream) const; + FrameBuffer *getBuffer(Stream *stream); + void returnBuffer(Stream *stream, FrameBuffer *buffer); + private: std::shared_ptr camera_; std::map>> buffers_; + std::map> availableBuffers_; }; } /* namespace libcamera */ diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp index 7ed80011c845..7429d6b9edb7 100644 --- a/src/libcamera/framebuffer_allocator.cpp +++ b/src/libcamera/framebuffer_allocator.cpp @@ -64,7 +64,7 @@ FrameBufferAllocator::FrameBufferAllocator(std::shared_ptr camera) FrameBufferAllocator::~FrameBufferAllocator() { - buffers_.clear(); + clear(); } /** @@ -93,11 +93,17 @@ int FrameBufferAllocator::allocate(Stream *stream) } int ret = camera_->exportFrameBuffers(stream, &buffers_[stream]); - if (ret == -EINVAL) + if (ret == -EINVAL) { LOG(Allocator, Error) << "Stream is not part of " << camera_->id() << " active configuration"; - return ret; + return ret; + } + + for (const auto &buffer : buffers_[stream]) + availableBuffers_[stream].push(buffer.get()); + + return 0; } /** @@ -122,6 +128,9 @@ int FrameBufferAllocator::free(Stream *stream) buffers.clear(); buffers_.erase(iter); + availableBuffers_[stream] = {}; + availableBuffers_.erase(availableBuffers_.find(stream)); + return 0; } @@ -131,6 +140,7 @@ int FrameBufferAllocator::free(Stream *stream) void FrameBufferAllocator::clear() { buffers_.clear(); + availableBuffers_.clear(); } /** @@ -162,4 +172,48 @@ FrameBufferAllocator::buffers(Stream *stream) const return iter->second; } +/** + * \brief Get a pointer to a \a buffer for the \a stream + * \param[in] stream The stream to get a buffer for + * + * The method returns a pointer to a FrameBuffer but does transfer the buffer + * ownership to the caller: the returned pointer remains valid until the + * FrameBufferAllocator does not get deleted or the allocated buffers do not get + * released with a call for free() or clear(). + * + * \return A FrameBuffer pointer or nullptr if the no buffers is available + */ +FrameBuffer *FrameBufferAllocator::getBuffer(Stream *stream) +{ + if (!allocated() || buffers_[stream].empty()) + return nullptr; + + FrameBuffer *frameBuffer = availableBuffers_[stream].front(); + availableBuffers_[stream].pop(); + + return frameBuffer; +} + +/** + * \brief Return a \a buffer to the list of buffers available for the a \a stream + * \param[in] stream The stream to return buffer to + * \param[in] buffer The buffer to return + */ +void FrameBufferAllocator::returnBuffer(Stream *stream, FrameBuffer *buffer) +{ + if (!allocated()) + return; + + for (const auto &b : buffers_[stream]) { + /* + * Return the buffer to the available queue only if it was part + * of the vector of buffers allocated for the Stream. + */ + if (b.get() != buffer) + continue; + + availableBuffers_[stream].push(buffer); + } +} + } /* namespace libcamera */