From patchwork Sat Jul 13 17:23:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1693 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3C9E960C23 for ; Sat, 13 Jul 2019 19:24:45 +0200 (CEST) Received: from pendragon.ideasonboard.com (softbank126209254147.bbtec.net [126.209.254.147]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2C4852B2 for ; Sat, 13 Jul 2019 19:24:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1563038685; bh=TJRuO3cldATEFSTkNSOhV2RNzkwoVuSxkA+kPa9Z7A8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=MQ15meMCVSFvPvijCUnDuVmw76POj1Q3ziPCXrUWYYp5E3Ywd7i146rNZBogUWzUh WKjFHkZgAs886w7ONmWA0V+9jxmokyJ9Z2OzEVF2HhCOdN6N/7eD1KtBPz2jdJASAC /YnemjJ1mkx1zfkk/UqWnXLdv5VYvc7XrC2l4R1I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 13 Jul 2019 20:23:47 +0300 Message-Id: <20190713172351.25452-13-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190713172351.25452-1-laurent.pinchart@ideasonboard.com> References: <20190713172351.25452-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 12/16] libcamera: buffer: Add dmabuf file descriptors X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 13 Jul 2019 17:24:45 -0000 From: Jacopo Mondi In addition to referencing buffer memory by index, add support to referencing it using dmabuf file descriptors. This will be used to reference buffer memory allocated outside of libcamera and import it. The dmabuf file descriptors are stored in an array in the Buffer class, and a new Stream::createBuffer() overload is added to construct a buffer from dmabuf file descriptor. Signed-off-by: Jacopo Mondi Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/buffer.h | 3 +++ include/libcamera/stream.h | 1 + src/libcamera/buffer.cpp | 13 ++++++++++- src/libcamera/request.cpp | 5 ++++ src/libcamera/stream.cpp | 47 +++++++++++++++++++++++++++++++++++++- 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index f5ba6207bcef..f8569a6b67f3 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_BUFFER_H__ #define __LIBCAMERA_BUFFER_H__ +#include #include #include @@ -75,6 +76,7 @@ public: Buffer &operator=(const Buffer &) = delete; unsigned int index() const { return index_; } + const std::array &dmabufs() const { return dmabuf_; } unsigned int bytesused() const { return bytesused_; } uint64_t timestamp() const { return timestamp_; } @@ -95,6 +97,7 @@ private: void setRequest(Request *request) { request_ = request; } unsigned int index_; + std::array dmabuf_; unsigned int bytesused_; uint64_t timestamp_; diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h index 08eb8cc7d5c7..1883d9e9b9c5 100644 --- a/include/libcamera/stream.h +++ b/include/libcamera/stream.h @@ -75,6 +75,7 @@ public: Stream(); std::unique_ptr createBuffer(unsigned int index); + std::unique_ptr createBuffer(const std::array &fds); BufferPool &bufferPool() { return bufferPool_; } std::vector &buffers() { return bufferPool_.buffers(); } diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index ecbf25246a55..99358633a088 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -269,7 +269,8 @@ void BufferPool::destroyBuffers() * for a stream with Stream::createBuffer(). */ Buffer::Buffer(unsigned int index, const Buffer *metadata) - : index_(index), status_(Buffer::BufferSuccess), request_(nullptr), + : index_(index), dmabuf_({ -1, -1, -1 }), + status_(Buffer::BufferSuccess), request_(nullptr), stream_(nullptr) { if (metadata) { @@ -289,6 +290,16 @@ Buffer::Buffer(unsigned int index, const Buffer *metadata) * \return The buffer index */ +/** + * \fn Buffer::dmabufs() + * \brief Retrieve the dmabuf file descriptors for all buffer planes + * + * The dmabufs array contains one dmabuf file descriptor per plane. Unused + * entries are set to -1. + * + * \return The dmabuf file descriptors + */ + /** * \fn Buffer::bytesused() * \brief Retrieve the number of bytes occupied by the data in the buffer diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp index 19131472710b..ee2158fc7a9c 100644 --- a/src/libcamera/request.cpp +++ b/src/libcamera/request.cpp @@ -106,10 +106,15 @@ Request::~Request() * * \return 0 on success or a negative error code otherwise * \retval -EEXIST The request already contains a buffer for the stream + * \retval -EINVAL The buffer does not reference a valid Stream */ int Request::addBuffer(std::unique_ptr buffer) { Stream *stream = buffer->stream(); + if (!stream) { + LOG(Request, Error) << "Invalid stream reference"; + return -EINVAL; + } auto it = bufferMap_.find(stream); if (it != bufferMap_.end()) { diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp index 94aa4810f6b9..e6aa1b643a37 100644 --- a/src/libcamera/stream.cpp +++ b/src/libcamera/stream.cpp @@ -424,17 +424,26 @@ Stream::Stream() } /** - * \brief Create a Buffer instance + * \brief Create a Buffer instance referencing the memory buffer \a index * \param[in] index The desired buffer index * * This method creates a Buffer instance that references a BufferMemory from * the stream's buffers pool by its \a index. The index shall be lower than the * number of buffers in the pool. * + * This method is only valid for streams that use the InternalMemory type. It + * will return a null pointer when called on streams using the ExternalMemory + * type. + * * \return A newly created Buffer on success or nullptr otherwise */ std::unique_ptr Stream::createBuffer(unsigned int index) { + if (memoryType_ != InternalMemory) { + LOG(Stream, Error) << "Invalid stream memory type"; + return nullptr; + } + if (index >= bufferPool_.count()) { LOG(Stream, Error) << "Invalid buffer index " << index; return nullptr; @@ -447,6 +456,42 @@ std::unique_ptr Stream::createBuffer(unsigned int index) return std::unique_ptr(buffer); } +/** + * \brief Create a Buffer instance that represents a memory area identified by + * dmabuf file descriptors + * \param[in] fds The dmabuf file descriptors for each plane + * + * This method creates a Buffer instance that references buffer memory + * allocated outside of libcamera through dmabuf file descriptors. The \a + * dmabuf array shall contain a file descriptor for each plane in the buffer, + * and unused entries shall be set to -1. + * + * The buffer is created without a valid index, as it does not yet map to any of + * the stream's BufferMemory instances. An index will be assigned at the time + * the buffer is queued to the camera in a request. Applications may thus + * create any number of Buffer instances, providing that no more than the + * number of buffers allocated for the stream are queued at any given time. + * + * This method is only valid for streams that use the InternalMemory type. It + * will return a null pointer when called on streams using the ExternalMemory + * type. + * + * \return A newly created Buffer on success or nullptr otherwise + */ +std::unique_ptr Stream::createBuffer(const std::array &fds) +{ + if (memoryType_ != ExternalMemory) { + LOG(Stream, Error) << "Invalid stream memory type"; + return nullptr; + } + + Buffer *buffer = new Buffer(); + buffer->dmabuf_ = fds; + buffer->stream_ = this; + + return std::unique_ptr(buffer); +} + /** * \fn Stream::bufferPool() * \brief Retrieve the buffer pool for the stream