From patchwork Tue Sep 22 09:47:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9685 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 7C519BF01C for ; Tue, 22 Sep 2020 09:44:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 56B5262FD5; Tue, 22 Sep 2020 11:44:02 +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 BD70260364 for ; Tue, 22 Sep 2020 11:44:00 +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 542D9E000E; Tue, 22 Sep 2020 09:43:57 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:31 +0200 Message-Id: <20200922094738.5327-2-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 1/8] 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: Kieran Bingham Reviewed-by: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- 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..9a9406cc257c 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 |--->| FB | + * +-----+ +-----+ +-----+ + * | | | + * V V | + * +-----+ +------+ | + * | B | | B |<----+ + * +-----+ +------+ + * ^ | + * |-------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 Tue Sep 22 09:47:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9686 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 A9F69BF01C for ; Tue, 22 Sep 2020 09:44:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 83C5062FD2; Tue, 22 Sep 2020 11:44:07 +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 AFAF560364 for ; Tue, 22 Sep 2020 11:44:05 +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 0038EE000A; Tue, 22 Sep 2020 09:44:00 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:32 +0200 Message-Id: <20200922094738.5327-3-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 2/8] 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 to the CameraDevice class a FrameBufferAllocator class member. Reviewed-by: Niklas Söderlund Reviewed-by: Hirokazu Honda Signed-off-by: Jacopo Mondi Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 5 +++-- src/android/camera_device.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index e4ffbc02c2da..98b8159ebd8c 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include "libcamera/internal/formats.h" @@ -211,8 +212,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 9a9406cc257c..84f636f7a93c 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 Tue Sep 22 09:47:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9687 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 D3F5ABF01C for ; Tue, 22 Sep 2020 09:44:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B220F62FD4; Tue, 22 Sep 2020 11:44:11 +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 0F86160364 for ; Tue, 22 Sep 2020 11:44:11 +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 743CBE000D; Tue, 22 Sep 2020 09:44:05 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:33 +0200 Message-Id: <20200922094738.5327-4-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 3/8] libcamera: frame_buffer_allocator: Add clear() 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 clear() method to the FrameBufferAllocator class that frees all the buffers previously reserved by the allocator. Reviewed-by: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- 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 78f1353964eb..2a4d538a0cb2 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 clear(); 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..7ed80011c845 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::clear() +{ + buffers_.clear(); +} + /** * \fn FrameBufferAllocator::allocated() * \brief Check if the allocator has allocated buffers for any stream From patchwork Tue Sep 22 09:47:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9688 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 09AC3BF01C for ; Tue, 22 Sep 2020 09:44:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DBCCD62FD4; Tue, 22 Sep 2020 11:44:18 +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 399E160364 for ; Tue, 22 Sep 2020 11:44: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 relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 02AF6E000D; Tue, 22 Sep 2020 09:44:11 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:34 +0200 Message-Id: <20200922094738.5327-5-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 4/8] android: camera_device: Clear allocator at configureStream 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" The configureStream operation might be called by the Android framework in two successive capture session without going through a close(). Clear all the allocated buffers before configuring the camera streams. Reviewed-by: Niklas Söderlund Reviewed-by: Hirokazu Honda Signed-off-by: Jacopo Mondi Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 98b8159ebd8c..42fb9ea4e113 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1182,12 +1182,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_.clear(); /* First handle all non-MJPEG streams. */ camera3_stream_t *jpegStream = nullptr; From patchwork Tue Sep 22 09:47:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9689 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 33F6EBF01C for ; Tue, 22 Sep 2020 09:44:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0775B62FD4; Tue, 22 Sep 2020 11:44:27 +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 34A1A60364 for ; Tue, 22 Sep 2020 11:44:26 +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 36CF7E0008; Tue, 22 Sep 2020 09:44:18 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:35 +0200 Message-Id: <20200922094738.5327-6-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 5/8] 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 --- 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 42fb9ea4e113..f96ea7321a67 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1189,6 +1189,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) streams_.clear(); streams_.reserve(stream_list->num_streams); allocator_.clear(); + bufferPool_.clear(); /* First handle all non-MJPEG streams. */ camera3_stream_t *jpegStream = nullptr; @@ -1336,6 +1337,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 = allocateBuffersPool(stream); + if (ret) { + LOG(HAL, Error) << "Failed to allocate buffers for stream " + << cameraStream.index(); + return ret; + } + } + return 0; } @@ -1369,6 +1389,22 @@ FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer return new FrameBuffer(std::move(planes)); } +int CameraDevice::allocateBuffersPool(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 84f636f7a93c..4cef34c01a49 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 allocateBuffersPool(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 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); From patchwork Tue Sep 22 09:47:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9691 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 D425BBF01C for ; Tue, 22 Sep 2020 09:44:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 74C8E62FDD; Tue, 22 Sep 2020 11:44:42 +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 80B8160364 for ; Tue, 22 Sep 2020 11:44:41 +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 BCC58E0010; Tue, 22 Sep 2020 09:44:33 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:37 +0200 Message-Id: <20200922094738.5327-8-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 7/8] 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 --- 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 6ed56ff57dab..1b35c72a3de6 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1477,6 +1477,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 @@ -1485,27 +1487,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); } @@ -1632,6 +1656,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 Tue Sep 22 09:47:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 9692 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 0716CBF01C for ; Tue, 22 Sep 2020 09:44:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D5DC462FD2; Tue, 22 Sep 2020 11:44:51 +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 315C460364 for ; Tue, 22 Sep 2020 11:44:50 +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 59D28E0009; Tue, 22 Sep 2020 09:44:41 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 11:47:38 +0200 Message-Id: <20200922094738.5327-9-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 8/8] 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 stream gets mapped onto 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: Hirokazu Honda Reviewed-by: Niklas Söderlund Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- 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 1b35c72a3de6..316d8293843f 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1474,9 +1474,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 << " 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(); @@ -1487,6 +1490,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. @@ -1498,6 +1508,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: @@ -1509,6 +1520,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: @@ -1522,6 +1534,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) {