From patchwork Tue Sep 22 09:47:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9690 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 6C45ABF01C for ; Tue, 22 Sep 2020 09:44:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4955C62FD4; Tue, 22 Sep 2020 11:44:35 +0200 (CEST) Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BE60D60364 for ; Tue, 22 Sep 2020 11:44:33 +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 relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 5BF86E0003; Tue, 22 Sep 2020 09:44:26 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:36 +0200 Message-Id: <20200922094738.5327-7-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200922094738.5327-1-jacopo@jmondi.org> References: <20200922094738.5327-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 6/8] android: camera_device: Add methods to 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 two methods to the CameraDevice class to retrieve and return frame buffers associated to a stream from the memory pool reserved in libcamera. Protect accessing the vector of FrameBufer pointers with a per-pool mutex in the get and return buffer methods. Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 34 +++++++++++++++++++++++++++++++++- src/android/camera_device.h | 11 +++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index f96ea7321a67..6ed56ff57dab 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -8,6 +8,7 @@ #include "camera_device.h" #include "camera_ops.h" +#include #include #include #include @@ -1400,11 +1401,42 @@ int CameraDevice::allocateBuffersPool(Stream *stream) * the HAL. */ for (const auto &frameBuffer : allocator_.buffers(stream)) - bufferPool_[stream].push_back(frameBuffer.get()); + bufferPool_[stream].buffers.push_back(frameBuffer.get()); return 0; } +libcamera::FrameBuffer *CameraDevice::getBuffer(libcamera::Stream *stream) +{ + if (bufferPool_.find(stream) == bufferPool_.end()) + return nullptr; + + BufferPool *pool = &bufferPool_[stream]; + std::lock_guard locker(pool->mutex); + + if (pool->buffers.empty()) { + LOG(HAL, Error) << "Buffer underrun"; + return nullptr; + } + + FrameBuffer *buffer = pool->buffers.front(); + pool->buffers.erase(pool->buffers.begin()); + + return buffer; +} + +void CameraDevice::returnBuffer(libcamera::Stream *stream, + libcamera::FrameBuffer *buffer) +{ + if (bufferPool_.find(stream) == bufferPool_.end()) + return; + + BufferPool *pool = &bufferPool_[stream]; + std::lock_guard locker(pool->mutex); + + pool->buffers.push_back(buffer); +} + int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request) { if (!camera3Request->num_output_buffers) { diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 4cef34c01a49..5addffdc070a 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -166,8 +167,11 @@ protected: std::string logPrefix() const override; private: - using FrameBufferPool = std::map>; + struct BufferPool { + std::mutex mutex; + std::vector buffers; + }; + using FrameBufferPool = std::map; CameraDevice(unsigned int id, const std::shared_ptr &camera); @@ -198,6 +202,9 @@ private: std::tuple calculateStaticMetadataSize(); libcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer); int allocateBuffersPool(libcamera::Stream *stream); + libcamera::FrameBuffer *getBuffer(libcamera::Stream *stream); + void returnBuffer(libcamera::Stream *stream, + libcamera::FrameBuffer *buffer); void notifyShutter(uint32_t frameNumber, uint64_t timestamp); void notifyError(uint32_t frameNumber, camera3_stream_t *stream);