From patchwork Wed Sep 30 13:26:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9873 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 DDD87C3B5B for ; Wed, 30 Sep 2020 13:23:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BC4276237A; Wed, 30 Sep 2020 15:23:19 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9B3CC60364 for ; Wed, 30 Sep 2020 15:23:18 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 213DFC000E; Wed, 30 Sep 2020 13:23:16 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:26:59 +0200 Message-Id: <20200930132707.19367-2-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 1/9] android: camera_device: Add CameraStream::Type 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" Define the CameraStream::Type enumeration and assign it to each CameraStream instance at construction time. The CameraStream type will be used to decide if memory needs to be allocated on its behalf or if the stream is backed by memory externally allocated by the Android framework. Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 14 ++++-- src/android/camera_device.h | 87 ++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 70d77a17ef43..e4ffbc02c2da 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -169,9 +169,11 @@ MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer, } } -CameraStream::CameraStream(PixelFormat format, Size size, +CameraStream::CameraStream(PixelFormat format, Size size, Type type, unsigned int index, Encoder *encoder) - : format_(format), size_(size), index_(index), encoder_(encoder) + + : format_(format), size_(size), type_(type), index_(index), + encoder_(encoder) { } @@ -1222,12 +1224,14 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) config_->addConfiguration(streamConfiguration); unsigned int index = config_->size() - 1; - streams_.emplace_back(format, size, index); + streams_.emplace_back(format, size, CameraStream::Type::Direct, + index); stream->priv = static_cast(&streams_.back()); } /* Now handle the MJPEG streams, adding a new stream if required. */ if (jpegStream) { + CameraStream::Type type; int index = -1; /* Search for a compatible stream in the non-JPEG ones. */ @@ -1245,6 +1249,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) LOG(HAL, Info) << "Android JPEG stream mapped to libcamera stream " << i; + type = CameraStream::Type::Mapped; index = i; break; } @@ -1269,6 +1274,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) LOG(HAL, Info) << "Adding " << streamConfiguration.toString() << " for MJPEG support"; + type = CameraStream::Type::Internal; config_->addConfiguration(streamConfiguration); index = config_->size() - 1; } @@ -1287,7 +1293,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) return ret; } - streams_.emplace_back(formats::MJPEG, cfg.size, index, encoder); + streams_.emplace_back(formats::MJPEG, cfg.size, type, index, encoder); jpegStream->priv = static_cast(&streams_.back()); } diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 1837748d2efc..17de963f5fc4 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -30,17 +30,102 @@ class CameraMetadata; class CameraStream { public: + /* + * Enumeration of CameraStream types. + * + * A camera stream associates an Android stream to a libcamera stream. + * This enumeration describes how the two streams are associated and how + * and where data produced from libcamera are delivered to the + * Android framework. + * + * Direct: + * + * The Android stream is directly mapped onto a libcamera stream: frames + * are delivered by the library directly in the memory location + * specified by the Android stream (buffer_handle_t->data) and provided + * to the framework as they are. The Android stream characteristics are + * directly translated to the libcamera stream configuration. + * + * +-----+ +-----+ + * | A | | L | + * +-----+ +-----+ + * | | + * V V + * +-----+ +------+ + * | B |<---------------| FB | + * +-----+ +------+ + * + * + * Internal: + * + * Data for the Android stream is produced by processing a libcamera + * stream created by the HAL for that purpose. The libcamera stream + * needs to be supplied with intermediate buffers where the library + * delivers frames to be processed and then provided to the framework. + * The libcamera stream configuration is not a direct translation of the + * Android stream characteristics, but it describes the format and size + * required for the processing procedure to produce frames in the + * Android required format. + * + * +-----+ +-----+ + * | A | | L | + * +-----+ +-----+ + * | | + * V V + * +-----+ +------+ + * | B | | FB | + * +-----+ +------+ + * ^ | + * |-------Processing------| + * + * + * Mapped: + * + * Data for the Android stream is produced by processing a libcamera + * stream associated with another CameraStream. Mapped camera streams do + * not need any memory to be reserved for them as they process data + * produced by libcamera for a different stream whose format and size + * is compatible with the processing procedure requirements to produce + * frames in the Android required format. + * + * +-----+ +-----+ +-----+ + * | A | | A' | | L | + * +-----+ +-----+ +-----+ + * | | | + * V V V + * +-----+ +-----+ +------+ + * | B | | B' |<---------| FB | + * +-----+ +-----+ +------+ + * ^ | + * |--Processing--| + * + * + * -------------------------------------------------------------------- + * A = Android stream + * L = libcamera stream + * B = memory buffer + * FB = libcamera FrameBuffer + * "Processing" = Frame processing procedure (Encoding, scaling etc) + */ + enum class Type { + Direct, + Internal, + Mapped, + }; + CameraStream(libcamera::PixelFormat format, libcamera::Size size, - unsigned int index, Encoder *encoder = nullptr); + Type type, unsigned int index, Encoder *encoder = nullptr); const libcamera::PixelFormat &format() const { return format_; } const libcamera::Size &size() const { return size_; } + Type type() const { return type_; } unsigned int index() const { return index_; } Encoder *encoder() const { return encoder_.get(); } private: libcamera::PixelFormat format_; libcamera::Size size_; + Type type_; /* * The index of the libcamera StreamConfiguration as added during * configureStreams(). A single libcamera Stream may be used to deliver From patchwork Wed Sep 30 13:27:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9874 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 0BD7AC3B5B for ; Wed, 30 Sep 2020 13:23:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DB3736237A; Wed, 30 Sep 2020 15:23:22 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2F165622B5 for ; Wed, 30 Sep 2020 15:23:20 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id BC660C000B; Wed, 30 Sep 2020 13:23:18 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:00 +0200 Message-Id: <20200930132707.19367-3-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 2/9] android: camera_device: Add frame allocator 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 a FrameBufferAllocator to the CameraDevice class to handle any allocations required to satisfy internal stream requirements. Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Hirokazu Honda Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 4 ++-- src/android/camera_device.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index e4ffbc02c2da..d8587647f831 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -211,8 +211,8 @@ CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() */ CameraDevice::CameraDevice(unsigned int id, const std::shared_ptr &camera) - : id_(id), running_(false), camera_(camera), staticMetadata_(nullptr), - facing_(CAMERA_FACING_FRONT), orientation_(0) + : id_(id), running_(false), camera_(camera), allocator_(camera), + staticMetadata_(nullptr), facing_(CAMERA_FACING_FRONT), orientation_(0) { camera_->requestCompleted.connect(this, &CameraDevice::requestComplete); diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 17de963f5fc4..75e3305089d9 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -206,6 +207,7 @@ private: bool running_; std::shared_ptr camera_; std::unique_ptr config_; + libcamera::FrameBufferAllocator allocator_; CameraMetadata *staticMetadata_; std::map requestTemplates_; From patchwork Wed Sep 30 13:27:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9875 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 4E430C3B5E for ; Wed, 30 Sep 2020 13:23:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0ADAE6238E; Wed, 30 Sep 2020 15:23:23 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CDC2760364 for ; Wed, 30 Sep 2020 15:23:21 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 5408FC000A; Wed, 30 Sep 2020 13:23:20 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:01 +0200 Message-Id: <20200930132707.19367-4-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 3/9] libcamera: frame_buffer_allocator: Add freeAll() 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 a freeAll() method to the FrameBufferAllocator class that frees all the buffers previously reserved by the allocator. Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi --- include/libcamera/framebuffer_allocator.h | 1 + src/libcamera/framebuffer_allocator.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/libcamera/framebuffer_allocator.h b/include/libcamera/framebuffer_allocator.h index a96aaeae58ce..3394e9923584 100644 --- a/include/libcamera/framebuffer_allocator.h +++ b/include/libcamera/framebuffer_allocator.h @@ -28,6 +28,7 @@ public: int allocate(Stream *stream); int free(Stream *stream); + void freeAll(); bool allocated() const { return !buffers_.empty(); } const std::vector> &buffers(Stream *stream) const; diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp index 2fbba37a1b0b..8dbe94ad1ce6 100644 --- a/src/libcamera/framebuffer_allocator.cpp +++ b/src/libcamera/framebuffer_allocator.cpp @@ -125,6 +125,14 @@ int FrameBufferAllocator::free(Stream *stream) return 0; } +/** + * \brief Free all the buffers previously allocated + */ +void FrameBufferAllocator::freeAll() +{ + buffers_.clear(); +} + /** * \fn FrameBufferAllocator::allocated() * \brief Check if the allocator has allocated buffers for any stream From patchwork Wed Sep 30 13:27:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9876 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 53302C3B5B for ; Wed, 30 Sep 2020 13:23:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2F3DB6239C; Wed, 30 Sep 2020 15:23:25 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6E73B60364 for ; Wed, 30 Sep 2020 15:23: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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id E9AE9C0016; Wed, 30 Sep 2020 13:23:21 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:02 +0200 Message-Id: <20200930132707.19367-5-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 4/9] android: camera_device: Clear allocator 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" Clear all the allocated buffers at camera close() time and before configuring the camera streams at configureStream() time as the configureStream operation could be called by the Android framework in two successive capture sessions without going through a close(). Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Hirokazu Honda Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index d8587647f831..d73d10d9d449 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -534,6 +534,8 @@ int CameraDevice::open(const hw_module_t *hardwareModule) void CameraDevice::close() { + allocator_.freeAll(); + camera_->stop(); camera_->release(); @@ -1181,12 +1183,13 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) } /* - * Clear and remove any existing configuration from previous calls, and - * ensure the required entries are available without further - * reallocation. + * Clear and remove any existing configuration and memory allocated from + * previous calls, and ensure the required entries are available without + * further reallocation. */ streams_.clear(); streams_.reserve(stream_list->num_streams); + allocator_.freeAll(); /* First handle all non-MJPEG streams. */ camera3_stream_t *jpegStream = nullptr; From patchwork Wed Sep 30 13:27:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9877 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 AD076C3B5B for ; Wed, 30 Sep 2020 13:23:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 70711623B0; Wed, 30 Sep 2020 15:23:27 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B1AD4622EE for ; Wed, 30 Sep 2020 15:23:24 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 9C52EC0015; Wed, 30 Sep 2020 13:23:23 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:03 +0200 Message-Id: <20200930132707.19367-6-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 5/9] android: camera_device: Clear streams_ in close() 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" When closing the CameraDevice, clear the streams_ vector to release all the created CameraStream instances. Suggested-by: Laurent Pinchart Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index d73d10d9d449..8692771a4538 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -534,6 +534,7 @@ int CameraDevice::open(const hw_module_t *hardwareModule) void CameraDevice::close() { + streams_.clear(); allocator_.freeAll(); camera_->stop(); From patchwork Wed Sep 30 13:27:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9878 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 00A30C3B5E for ; Wed, 30 Sep 2020 13:23:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C8D61623BA; Wed, 30 Sep 2020 15:23:27 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EE60560364 for ; Wed, 30 Sep 2020 15:23:25 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id D6C5FC0010; Wed, 30 Sep 2020 13:23:24 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:04 +0200 Message-Id: <20200930132707.19367-7-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 6/9] android: camera_device: Allocate buffer pools 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" After the Camera has been configured, walk the list of collected CameraStream instances and allocate memory for the ones that needs intermediate buffers reserved by the libcamera FrameBufferAllocator. Maintain a map between each Stream and a vector of pointers to the associated buffers. Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 36 +++++++++++++++++++++++++++++++++++ src/android/camera_device.h | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 8692771a4538..07041dd916d5 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1191,6 +1191,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) streams_.clear(); streams_.reserve(stream_list->num_streams); allocator_.freeAll(); + bufferPool_.clear(); /* First handle all non-MJPEG streams. */ camera3_stream_t *jpegStream = nullptr; @@ -1338,6 +1339,25 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) return ret; } + /* + * Now that the camera has been configured allocate buffers for + * the streams that need memory reserved by libcamera. + */ + for (const CameraStream &cameraStream : streams_) { + const StreamConfiguration &cfg = config_->at(cameraStream.index()); + Stream *stream = cfg.stream(); + + if (cameraStream.type() != CameraStream::Type::Internal) + continue; + + ret = allocateBufferPool(stream); + if (ret) { + LOG(HAL, Error) << "Failed to allocate buffers for stream " + << cameraStream.index(); + return ret; + } + } + return 0; } @@ -1371,6 +1391,22 @@ FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer return new FrameBuffer(std::move(planes)); } +int CameraDevice::allocateBufferPool(Stream *stream) +{ + int ret = allocator_.allocate(stream); + if (ret < 0) + return ret; + + /* + * Save a pointer to the reserved frame buffer for usage in + * the HAL. + */ + for (const auto &frameBuffer : allocator_.buffers(stream)) + bufferPool_[stream].push_back(frameBuffer.get()); + + return 0; +} + 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 75e3305089d9..c46610725e12 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -166,6 +166,9 @@ protected: std::string logPrefix() const override; private: + using FrameBufferPool = std::map>; + CameraDevice(unsigned int id, const std::shared_ptr &camera); struct Camera3RequestDescriptor { @@ -194,6 +197,8 @@ private: std::tuple calculateStaticMetadataSize(); libcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer); + int allocateBufferPool(libcamera::Stream *stream); + void notifyShutter(uint32_t frameNumber, uint64_t timestamp); void notifyError(uint32_t frameNumber, camera3_stream_t *stream); CameraMetadata *requestTemplatePreview(); @@ -208,6 +213,7 @@ private: std::shared_ptr camera_; std::unique_ptr config_; libcamera::FrameBufferAllocator allocator_; + FrameBufferPool bufferPool_; CameraMetadata *staticMetadata_; std::map requestTemplates_; From patchwork Wed Sep 30 13:27:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9879 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 72767C3B5B for ; Wed, 30 Sep 2020 13:23:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4CEB2623B0; Wed, 30 Sep 2020 15:23:30 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 34879622B5 for ; Wed, 30 Sep 2020 15:23:27 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 1D189C0007; Wed, 30 Sep 2020 13:23:25 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:05 +0200 Message-Id: <20200930132707.19367-8-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 7/9] 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 FrameBuffer 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 07041dd916d5..2ebc3fcc336e 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 @@ -1402,11 +1403,42 @@ int CameraDevice::allocateBufferPool(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 c46610725e12..c91de367d7bd 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 allocateBufferPool(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); From patchwork Wed Sep 30 13:27:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9880 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 AF92DC3B5E for ; Wed, 30 Sep 2020 13:23:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 81FB1623BA; Wed, 30 Sep 2020 15:23:30 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 82FF4622EE for ; Wed, 30 Sep 2020 15:23:28 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 5AB22C0010; Wed, 30 Sep 2020 13:23:27 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:06 +0200 Message-Id: <20200930132707.19367-9-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 8/9] android: camera_device: Use libcamera buffer pool 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" Now that we have reserved and made available to the camera HAL a pool of libcamera allocated buffers, use them when a CameraStream instance that requires internal allocation is processed. Reviewed-by: Niklas Söderlund Reviewed-by: Hirokazu Honda Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 57 +++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 2ebc3fcc336e..dc19faf8edd7 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1479,6 +1479,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { CameraStream *cameraStream = static_cast(camera3Buffers[i].stream->priv); + const StreamConfiguration &config = config_->at(cameraStream->index()); + Stream *stream = config.stream(); /* * Keep track of which stream the request belongs to and store @@ -1487,27 +1489,49 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques descriptor->buffers[i].stream = camera3Buffers[i].stream; descriptor->buffers[i].buffer = camera3Buffers[i].buffer; - /* Software streams are handled after hardware streams complete. */ - if (cameraStream->format() == formats::MJPEG) - continue; - /* - * Create a libcamera buffer using the dmabuf descriptors of - * the camera3Buffer for each stream. The FrameBuffer is - * directly associated with the Camera3RequestDescriptor for - * lifetime management only. + * Inspect the camera stream type, create buffers opportunely + * and add them to the Request if required. */ - FrameBuffer *buffer = createFrameBuffer(*camera3Buffers[i].buffer); + FrameBuffer *buffer; + switch (cameraStream->type()) { + case CameraStream::Type::Mapped: + /* + * Mapped streams don't need buffers added to the + * Request. + */ + continue; + + case CameraStream::Type::Direct: + /* + * Create a libcamera buffer using the dmabuf + * descriptors of the camera3Buffer for each stream and + * associate it with the Camera3RequestDescriptor for + * lifetime management only. + */ + buffer = createFrameBuffer(*camera3Buffers[i].buffer); + descriptor->frameBuffers.emplace_back(buffer); + break; + + case CameraStream::Type::Internal: + /* + * Get the frame buffer from the CameraStream internal + * buffer pool. The lifetime management of internal + * buffers is connected to the one of the + * FrameBufferAllocator instance. + * + * The retrieved buffer has to be returned to the + * allocator once it has been processed. + */ + buffer = getBuffer(stream); + break; + } if (!buffer) { LOG(HAL, Error) << "Failed to create buffer"; delete request; delete descriptor; return -ENOMEM; } - descriptor->frameBuffers.emplace_back(buffer); - - StreamConfiguration *streamConfiguration = &config_->at(cameraStream->index()); - Stream *stream = streamConfiguration->stream(); request->addBuffer(stream, buffer); } @@ -1634,6 +1658,13 @@ void CameraDevice::requestComplete(Request *request) const uint32_t jpeg_orientation = 0; resultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1); + + /* + * Return the FrameBuffer to the CameraStream now that we're + * done processing it. + */ + if (cameraStream->type() == CameraStream::Type::Internal) + returnBuffer(stream, buffer); } /* Prepare to call back the Android camera stack. */ From patchwork Wed Sep 30 13:27:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9881 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 0C08CC3B5B for ; Wed, 30 Sep 2020 13:23:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D54FB62382; Wed, 30 Sep 2020 15:23:33 +0200 (CEST) Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E97016237A for ; Wed, 30 Sep 2020 15:23:29 +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 relay6-d.mail.gandi.net (Postfix) with ESMTPSA id A4478C0003; Wed, 30 Sep 2020 13:23:28 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Sep 2020 15:27:07 +0200 Message-Id: <20200930132707.19367-10-jacopo@jmondi.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930132707.19367-1-jacopo@jmondi.org> References: <20200930132707.19367-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 9/9] android: camera_device: Add stream mapping log 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" To ease following how Android streams get mapped to libcamera ones add a (quite verbose) printout before queueing a request to libcamera. The output looks like: 0 - (320x240)[0x00000022] -> (320x240)[NV12] (direct) 1 - (640x480)[0x00000021] -> (640x480)[NV12] (internal) Reviewed-by: Laurent Pinchart Reviewed-by: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi --- src/android/camera_device.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index dc19faf8edd7..7383664e2e2f 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1476,9 +1476,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques Request *request = camera_->createRequest(reinterpret_cast(descriptor)); + LOG(HAL, Debug) << "Queueing Request to libcamera with " + << descriptor->numBuffers << " HAL streams"; for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { + camera3_stream *camera3Stream = camera3Buffers[i].stream; CameraStream *cameraStream = - static_cast(camera3Buffers[i].stream->priv); + static_cast(camera3Stream->priv); const StreamConfiguration &config = config_->at(cameraStream->index()); Stream *stream = config.stream(); @@ -1489,6 +1492,13 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques descriptor->buffers[i].stream = camera3Buffers[i].stream; descriptor->buffers[i].buffer = camera3Buffers[i].buffer; + std::stringstream ss; + ss << i << " - (" + << camera3Stream->width << "x" << camera3Stream->height << ")" + << "[" << utils::hex(camera3Stream->format) << "] -> " + << "(" << config.size.toString() << ")[" + << config.pixelFormat.toString() << "]"; + /* * Inspect the camera stream type, create buffers opportunely * and add them to the Request if required. @@ -1500,6 +1510,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * Mapped streams don't need buffers added to the * Request. */ + LOG(HAL, Debug) << ss.str() << " (mapped)"; continue; case CameraStream::Type::Direct: @@ -1511,6 +1522,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques */ buffer = createFrameBuffer(*camera3Buffers[i].buffer); descriptor->frameBuffers.emplace_back(buffer); + LOG(HAL, Debug) << ss.str() << " (direct)"; break; case CameraStream::Type::Internal: @@ -1524,6 +1536,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * allocator once it has been processed. */ buffer = getBuffer(stream); + LOG(HAL, Debug) << ss.str() << " (internal)"; break; } if (!buffer) {