From f1199a1011a22f00e863473ae241c781f4bd999d Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Wed, 10 Jul 2019 12:07:19 +0200 Subject: libcamera: buffer: Add dmabuf file descriptors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/libcamera/stream.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/libcamera/stream.cpp') diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp index 37a9bc0a..98178e06 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 ExternalMemory type. It + * will return a null pointer when called on streams using the InternalMemory + * 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 -- cgit v1.2.1