{"id":10002,"url":"https://patchwork.libcamera.org/api/1.1/patches/10002/?format=json","web_url":"https://patchwork.libcamera.org/patch/10002/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20201007101745.15104-11-jacopo@jmondi.org>","date":"2020-10-07T10:17:42","name":"[libcamera-devel,v3,10/13] android: camera_stream: Create buffer pool","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"a66c47210e0e2a2cd4ca6e3c01d51932385a3120","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10002/mbox/","series":[{"id":1361,"url":"https://patchwork.libcamera.org/api/1.1/series/1361/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1361","date":"2020-10-07T10:17:32","name":"android: CameraStream rework","version":3,"mbox":"https://patchwork.libcamera.org/series/1361/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10002/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10002/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 16DFFBEEDF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  7 Oct 2020 10:14:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E0CF760455;\n\tWed,  7 Oct 2020 12:14:05 +0200 (CEST)","from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net\n\t[217.70.183.195])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 847FB60396\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  7 Oct 2020 12:14:03 +0200 (CEST)","from uno.lan (93-34-118-233.ip49.fastwebnet.it [93.34.118.233])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 9727660005;\n\tWed,  7 Oct 2020 10:14:02 +0000 (UTC)"],"X-Originating-IP":"93.34.118.233","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  7 Oct 2020 12:17:42 +0200","Message-Id":"<20201007101745.15104-11-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.28.0","In-Reply-To":"<20201007101745.15104-1-jacopo@jmondi.org>","References":"<20201007101745.15104-1-jacopo@jmondi.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v3 10/13] android: camera_stream: Create\n\tbuffer pool","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a FrameBufferAllocator class member to the CameraStream class.\nThe allocator is constructed for CameraStream instances that needs\ninternal allocation and automatically deleted.\n\nAllocate FrameBuffers using the allocator_ class member in the\nCameraStream class at CameraStream::configure() time and add two\nmethods to the CameraStream class to get and put FrameBuffer pointers\nfrom the pool of allocated buffers. As buffer allocation can take place\nonly after the Camera has been configured, move the CameraStream\nconfiguration loop in the CameraDevice class after camera_->configure()\ncall.\n\nThe newly created pool will be used to provide buffers to CameraStream\nthat need to provide memory to libcamera where to deliver frames.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/android/camera_device.cpp | 22 +++++++++---------\n src/android/camera_stream.cpp | 43 +++++++++++++++++++++++++++++++++++\n src/android/camera_stream.h   |  9 ++++++++\n 3 files changed, 63 insertions(+), 11 deletions(-)","diff":"diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 1e2cbeeb92d1..ecdc0922e90b 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -1282,6 +1282,17 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t\treturn -EINVAL;\n \t}\n \n+\t/*\n+\t * Once the CameraConfiguration has been adjusted/validated\n+\t * it can be applied to the camera.\n+\t */\n+\tint ret = camera_->configure(config_.get());\n+\tif (ret) {\n+\t\tLOG(HAL, Error) << \"Failed to configure camera '\"\n+\t\t\t\t<< camera_->id() << \"'\";\n+\t\treturn ret;\n+\t}\n+\n \t/*\n \t * Configure the HAL CameraStream instances using the associated\n \t * StreamConfiguration and set the number of required buffers in\n@@ -1295,17 +1306,6 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t\t}\n \t}\n \n-\t/*\n-\t * Once the CameraConfiguration has been adjusted/validated\n-\t * it can be applied to the camera.\n-\t */\n-\tint ret = camera_->configure(config_.get());\n-\tif (ret) {\n-\t\tLOG(HAL, Error) << \"Failed to configure camera '\"\n-\t\t\t\t<< camera_->id() << \"'\";\n-\t\treturn ret;\n-\t}\n-\n \treturn 0;\n }\n \ndiff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp\nindex f899be4fe007..eac1480530f8 100644\n--- a/src/android/camera_stream.cpp\n+++ b/src/android/camera_stream.cpp\n@@ -26,6 +26,11 @@ CameraStream::CameraStream(CameraDevice *cameraDevice, Type type,\n \n \tif (type_ == Type::Internal || type_ == Type::Mapped)\n \t\tencoder_ = std::make_unique<EncoderLibJpeg>();\n+\n+\tif (type == Type::Internal) {\n+\t\tallocator_ = std::make_unique<FrameBufferAllocator>(cameraDevice_->camera());\n+\t\tmutex_ = std::make_unique<std::mutex>();\n+\t}\n }\n \n const StreamConfiguration &CameraStream::configuration() const\n@@ -46,6 +51,16 @@ int CameraStream::configure()\n \t\t\treturn ret;\n \t}\n \n+\tif (allocator_) {\n+\t\tint ret = allocator_->allocate(stream());\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\n+\t\t/* Save a pointer to the reserved frame buffers */\n+\t\tfor (const auto &frameBuffer : allocator_->buffers(stream()))\n+\t\t\tbuffers_.push_back(frameBuffer.get());\n+\t}\n+\n \tcamera3Stream_->max_buffers = configuration().bufferCount;\n \n \treturn 0;\n@@ -109,3 +124,31 @@ int CameraStream::process(const libcamera::FrameBuffer &source,\n \n \treturn 0;\n }\n+\n+FrameBuffer *CameraStream::getBuffer()\n+{\n+\tif (!allocator_)\n+\t\treturn nullptr;\n+\n+\tstd::lock_guard<std::mutex> locker(*mutex_);\n+\n+\tif (buffers_.empty()) {\n+\t\tLOG(HAL, Error) << \"Buffer underrun\";\n+\t\treturn nullptr;\n+\t}\n+\n+\tFrameBuffer *buffer = buffers_.back();\n+\tbuffers_.pop_back();\n+\n+\treturn buffer;\n+}\n+\n+void CameraStream::putBuffer(libcamera::FrameBuffer *buffer)\n+{\n+\tif (!allocator_)\n+\t\treturn;\n+\n+\tstd::lock_guard<std::mutex> locker(*mutex_);\n+\n+\tbuffers_.push_back(buffer);\n+}\ndiff --git a/src/android/camera_stream.h b/src/android/camera_stream.h\nindex 4c51f0fb3393..f929e8260ae3 100644\n--- a/src/android/camera_stream.h\n+++ b/src/android/camera_stream.h\n@@ -8,11 +8,14 @@\n #define __ANDROID_CAMERA_STREAM_H__\n \n #include <memory>\n+#include <mutex>\n+#include <vector>\n \n #include <hardware/camera3.h>\n \n #include <libcamera/buffer.h>\n #include <libcamera/camera.h>\n+#include <libcamera/framebuffer_allocator.h>\n #include <libcamera/geometry.h>\n #include <libcamera/pixel_format.h>\n \n@@ -117,6 +120,8 @@ public:\n \tint configure();\n \tint process(const libcamera::FrameBuffer &source,\n \t\t    MappedCamera3Buffer *dest, CameraMetadata *metadata);\n+\tlibcamera::FrameBuffer *getBuffer();\n+\tvoid putBuffer(libcamera::FrameBuffer *buffer);\n \n private:\n \tCameraDevice *cameraDevice_;\n@@ -129,7 +134,11 @@ private:\n \t * one or more streams to the Android framework.\n \t */\n \tunsigned int index_;\n+\n \tstd::unique_ptr<Encoder> encoder_;\n+\tstd::unique_ptr<libcamera::FrameBufferAllocator> allocator_;\n+\tstd::vector<libcamera::FrameBuffer *> buffers_;\n+\tstd::unique_ptr<std::mutex> mutex_;\n };\n \n #endif /* __ANDROID_CAMERA_STREAM__ */\n","prefixes":["libcamera-devel","v3","10/13"]}