From patchwork Sun Feb 3 10:55:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 482 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 A66A560DB8 for ; Sun, 3 Feb 2019 11:55:21 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 165145AA; Sun, 3 Feb 2019 11:55:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191321; bh=bSL8a/jHoYJV+bzkEdKJN3UZTxmPQYH+CXnPiRKgpdw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m0R621QVlzo7jTpl/NyGu8mgg3Na4v6cwkNSmBND0+6om2De03MzJqVtX2uToPN5D V8DkZRYb0nD+EUS73Gk6S0q0FV0bPJ08mzpB9At83hpi/i2AsiEW72xyDcDD8nygzx twa64NBg4A1Dd1ZWKfkTY/AeOHu5zDy6Xs+t5K1g= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 11:55:13 +0100 Message-Id: <20190203105517.5355-2-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> References: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/5] libcamera: Add Buffer and BufferPool classes 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: Sun, 03 Feb 2019 10:55:22 -0000 From: Laurent Pinchart Provide classes that represent frame buffers and pools of frame buffers. Signed-off-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- Kieran - Fix checkstyle.py diff --- include/libcamera/buffer.h | 55 ++++++++++++++++++ include/libcamera/libcamera.h | 1 + include/libcamera/meson.build | 1 + src/libcamera/buffer.cpp | 102 ++++++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 5 files changed, 160 insertions(+) create mode 100644 include/libcamera/buffer.h create mode 100644 src/libcamera/buffer.cpp diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h new file mode 100644 index 000000000000..97c8025d9e77 --- /dev/null +++ b/include/libcamera/buffer.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * buffer.h - Buffer handling + */ +#ifndef __LIBCAMERA_BUFFER_H__ +#define __LIBCAMERA_BUFFER_H__ + +#include + +namespace libcamera { + +class Buffer +{ +public: + Buffer(); + ~Buffer(); + + int dmabuf() const { return fd_; }; + int setDmabuf(int fd); + + int mmap(); + void munmap(); + +private: + int fd_; +}; + +class BufferPool +{ +public: + enum Memory { + Internal, + External, + }; + + BufferPool(Memory memory); + virtual ~BufferPool(); + + int allocate(unsigned int count); + void free(); + + unsigned int count() const { return buffers_.size(); }; + +private: + virtual int allocateMemory() = 0; + + BufferPool::Memory memory_; + std::vector buffers_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BUFFER_H__ */ diff --git a/include/libcamera/libcamera.h b/include/libcamera/libcamera.h index 272dfd5e4a67..8167e8099ac0 100644 --- a/include/libcamera/libcamera.h +++ b/include/libcamera/libcamera.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_LIBCAMERA_H__ #define __LIBCAMERA_LIBCAMERA_H__ +#include #include #include #include diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 54a680787e5c..8c14423bc444 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -1,4 +1,5 @@ libcamera_api = files([ + 'buffer.h', 'camera.h', 'camera_manager.h', 'event_dispatcher.h', diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp new file mode 100644 index 000000000000..6dfebfc6bb28 --- /dev/null +++ b/src/libcamera/buffer.cpp @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * buffer.cpp - Buffer handling + */ + +#include +#include + +#include + +/** + * \file buffer.h + * \brief Buffer handling + */ + +namespace libcamera { + +/** + * \class Buffer + * \brief A memory buffer to store a frame + * + * The Buffer class represents a memory buffer used to store a frame. + */ +Buffer::Buffer() + : fd_(-1) +{ +} + +Buffer::~Buffer() +{ + if (fd_ != -1) + close(fd_); +} + +/** + * \fn Buffer::dmabuf() + * \brief Get the dmabuf file handle backing the buffer + */ + +/** + * \brief Set the dmabuf file handle backing the buffer + * + * The \a fd dmabuf file handle is duplicated and stored. + */ +int Buffer::setDmabuf(int fd) +{ + if (fd_ != -1) { + close(fd_); + fd_ = -1; + } + + if (fd != -1) + return 0; + + fd_ = dup(fd); + if (fd_ == -1) + return -errno; + + return 0; +} + +/** + * \class BufferPool + * \brief A pool of buffers + */ +BufferPool::BufferPool(BufferPool::Memory memory) + : memory_(memory) +{ +} + +BufferPool::~BufferPool() +{ + free(); +} + +int BufferPool::allocate(unsigned int count) +{ + for (unsigned int i = 0; i < count; ++i) { + Buffer *buffer = new Buffer(); + buffers_.push_back(buffer); + } + + if (memory_ == Memory::Internal) { + int ret = allocateMemory(); + if (ret < 0) + return ret; + } + + return 0; +} + +void BufferPool::free() +{ + for (Buffer *buffer : buffers_) + delete buffer; + + buffers_.clear(); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 9f6ff99eebe2..a4e9cc8f936c 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -1,4 +1,5 @@ libcamera_sources = files([ + 'buffer.cpp', 'camera.cpp', 'camera_manager.cpp', 'device_enumerator.cpp', From patchwork Sun Feb 3 10:55:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 483 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 B8A3060DB5 for ; Sun, 3 Feb 2019 11:55:21 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 53D7CD4A; Sun, 3 Feb 2019 11:55:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191321; bh=cTDa7n0mr9Kx1YntMoNasC58iWTcQWo6rIPhFluuJ7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SZN14kJgvvTvIiBRV5CjYkjOSO/Dxq9n3oo7Hk+asgsHkATzyZf6FsuLNP7fsXF5g p6lxTwEYY94UsVxcwaQldJsfS9On0AanjTsyhS/hQRLMw975SA06zd6L9cxV/GzFD5 eZogvbQwxNQ9ZSq/M5aM3mdZfqNucy6O9y5zT1ec= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 11:55:14 +0100 Message-Id: <20190203105517.5355-3-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> References: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/5] libcamera: buffer: Fix setDmabuf operations 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: Sun, 03 Feb 2019 10:55:22 -0000 The setDmabuf validation for the input FD was incorrect. Fix this, and update the documentation and error reporting. Signed-off-by: Kieran Bingham --- src/libcamera/buffer.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 6dfebfc6bb28..c86847daf193 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -43,6 +43,8 @@ Buffer::~Buffer() * \brief Set the dmabuf file handle backing the buffer * * The \a fd dmabuf file handle is duplicated and stored. + * + * \return 0 on success or a negative error value otherwise. */ int Buffer::setDmabuf(int fd) { @@ -51,12 +53,19 @@ int Buffer::setDmabuf(int fd) fd_ = -1; } - if (fd != -1) - return 0; + if (fd < 0) { + LOG(Buffer, Error) << "Invalid DMABuf FD provided"; + return -EINVAL; + } fd_ = dup(fd); - if (fd_ == -1) - return -errno; + if (fd_ == -1) { + int ret = -errno; + LOG(Buffer, Error) + << "Failed to duplicate Dmabuf: " << strerror(-ret); + + return ret; + } return 0; } From patchwork Sun Feb 3 10:55:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 484 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EB62960DBB for ; Sun, 3 Feb 2019 11:55:21 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 88322D4B; Sun, 3 Feb 2019 11:55:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191321; bh=35bii0CJhMK4PSa+eowHSiDQzo7TB3r3oh9XcibmUC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LT6D/iK4lhWmHL6KAeEKr8rzrSS71b6syyZ3A4hDLJrlWcehMFBU/BLhhT+a7v5Tu sMmbwMZF3is+ZgDl2vE8WAmM/dqjiU4U/uLdW5R6DegGmDDO4sUAzyRKgqihjlCTUS puNTrZS9Snn0uuhPPEWybNFJa/09VW7E77WqpPkU= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 11:55:15 +0100 Message-Id: <20190203105517.5355-4-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> References: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/5] libcamera: buffer: Document the BufferPool 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: Sun, 03 Feb 2019 10:55:22 -0000 Add Doxygen documentation for the existing code in the BufferPool class. Signed-off-by: Kieran Bingham --- src/libcamera/buffer.cpp | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index c86847daf193..5abc2a68e978 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -73,6 +73,37 @@ int Buffer::setDmabuf(int fd) /** * \class BufferPool * \brief A pool of buffers + * + * The BufferPool class groups together a collection of Buffers for passing + * between devices. The buffers must be exported by a device before they can be + * imported into another device for further use. + */ + +/** + * \enum BufferPool::Memory + * \brief Identify the memory type used in the BufferPool + */ + +/** + * \var BufferPool::Internal + * \brief The memory for the Buffers in this pool is allocated internally by the + * device associated with the pool + */ + +/** + * \var BufferPool::External + * \brief The memory for the Buffers in this pool is allocated externally by + * another device and can be imported for use by other devices. + * + * The buffers in the pool have been exported to make them available for sharing + * with other devices, and can be managed using their own file descriptors. + */ + +/** + * \brief Construct a buffer pool. The \a memory argument declares the intent of + * the pool to be either internal or external to a device. + * + * \sa BufferPool::Memory */ BufferPool::BufferPool(BufferPool::Memory memory) : memory_(memory) @@ -84,6 +115,9 @@ BufferPool::~BufferPool() free(); } +/** + * \brief Allocate buffers in a pool + */ int BufferPool::allocate(unsigned int count) { for (unsigned int i = 0; i < count; ++i) { @@ -100,6 +134,9 @@ int BufferPool::allocate(unsigned int count) return 0; } +/** + * \brief Release all buffers from pool. + */ void BufferPool::free() { for (Buffer *buffer : buffers_) @@ -108,4 +145,9 @@ void BufferPool::free() buffers_.clear(); } +/** + * \fn BufferPool::count() + * \brief Get the number of Buffers contained within this pool + */ + } /* namespace libcamera */ From patchwork Sun Feb 3 10:55:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 485 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2F10E60DB5 for ; Sun, 3 Feb 2019 11:55:22 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C01CDE08; Sun, 3 Feb 2019 11:55:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191321; bh=0J1VS/+qBua/3pFS5Nfzf2Oh384fiN+pESxb3aLGGdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZCWppbrfPwZKKNZhEH/rxQdUzncGH8vx3BSzjzB/61GdsiKn5z3DD81ne8p0JYZQv Op5vV36hQHWqoXBakhkAOMrCD6gQ0CDTnU0aGXFHoRaEAKREWXzIjvFUuKW9aMCBVu PQ55T97KYZZeVomE+d1sAipprxI5yszmLqYuSGgM= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 11:55:16 +0100 Message-Id: <20190203105517.5355-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> References: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/5] libcamera: buffer: Provide access to the Buffer vector 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: Sun, 03 Feb 2019 10:55:22 -0000 Extend the BufferPool to support retrieving Buffers by receiving a reference to the buffer vector. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- include/libcamera/buffer.h | 1 + src/libcamera/buffer.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index 97c8025d9e77..dda5075f2879 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -42,6 +42,7 @@ public: void free(); unsigned int count() const { return buffers_.size(); }; + const std::vector &buffers() { return buffers_; }; private: virtual int allocateMemory() = 0; diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 5abc2a68e978..08deaa97e4af 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -150,4 +150,10 @@ void BufferPool::free() * \brief Get the number of Buffers contained within this pool */ +/** + * \fn BufferPool::buffers() + * \brief Retrieve all the buffers in the pool + * \return A vector containing all the buffers in the pool. + */ + } /* namespace libcamera */ From patchwork Sun Feb 3 10:55:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 486 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 6440D60DB5 for ; Sun, 3 Feb 2019 11:55:22 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1118A41; Sun, 3 Feb 2019 11:55:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191322; bh=ucXzhou69UrroknYx5pOV3IP4jtEFpQzVog2HmdzNFk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qxXEyG4/0sg0qWLjBvRB6Pbe0dYnSZ+3l906m2jicvV4JPr0RcwXR/nqbzG4Xcx8z RFSOnMIIrSoRFAqk3XFbn3f2qyKpLX+6xubzwCFM3lXHbs4eYejFX5O5UZHPN3ByHB 3dOMCkPVoih1LF97qcCcIZgmxJ/dNN9zq/SQdH5Y= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 11:55:17 +0100 Message-Id: <20190203105517.5355-6-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> References: <20190203105517.5355-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 5/5] libcamera: buffer: Provide Buffer Planes 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: Sun, 03 Feb 2019 10:55:22 -0000 Extend the Buffer management to support multi-planar formats. An image within the system may use one or more Plane objects to track each plane in the case of multi-planar image formats. The Buffer class manages all of the data required to render or interpret the raw image data. Signed-off-by: Kieran Bingham --- include/libcamera/buffer.h | 37 +++++++- src/libcamera/buffer.cpp | 187 +++++++++++++++++++++++++++++++++++-- 2 files changed, 212 insertions(+), 12 deletions(-) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index dda5075f2879..afe99d5c4ab0 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -11,20 +11,49 @@ namespace libcamera { -class Buffer +class Plane { public: - Buffer(); - ~Buffer(); + Plane(); + Plane(unsigned int length, unsigned int offset); + ~Plane(); int dmabuf() const { return fd_; }; int setDmabuf(int fd); int mmap(); - void munmap(); + int munmap(); + + unsigned int length() const { return length_; }; + void *mem() const { return mem_; }; private: int fd_; + unsigned int length_; + unsigned int offset_; + void *mem_; +}; + +class Buffer +{ +public: + Buffer(); + virtual ~Buffer(); + + int mmap(); + int munmap(); + + unsigned int index() const { return index_; }; + const std::vector &planes() { return planes_; }; + +private: + unsigned int index_; + + unsigned int format_; + unsigned int width_; + unsigned int height_; + + std::vector planes_; }; class BufferPool diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 08deaa97e4af..887863b2af2e 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -6,10 +6,14 @@ */ #include +#include +#include #include #include +#include "log.h" + /** * \file buffer.h * \brief Buffer handling @@ -17,25 +21,52 @@ namespace libcamera { +LOG_DEFINE_CATEGORY(Buffer) + /** - * \class Buffer - * \brief A memory buffer to store a frame + * \class Plane + * \brief A memory region to store a single plane of a frame + * + * Image pixel formats may require more than one memory region to store separate + * planar data. These memory regions may be different sizes depending upon the + * pixel formats, and may be backed against different dmabuf handles when + * working with the V4L2 MPLANE api. * - * The Buffer class represents a memory buffer used to store a frame. + * The Plane class tracks the specific details of a memory region used to store + * a single plane for a given image and provides the means to map and access the + * data from an application. + * + * A Buffer may contain multiple Plane instances for multi-planar image formats. */ -Buffer::Buffer() - : fd_(-1) + +Plane::Plane() + : fd_(-1), length_(0), offset_(0), mem_(0) { } -Buffer::~Buffer() +/** + * \brief Construct a Plane memory region for CPU mappings + * \param[in] length The size of the memory region which should be mapped + * \param[in] offset The offset into the file descriptor base at which the + * buffer commences + * + * \sa mmap() + */ +Plane::Plane(unsigned int length, unsigned int offset) + : fd_(-1), length_(length), offset_(offset), mem_(0) { +} + +Plane::~Plane() +{ + munmap(); + if (fd_ != -1) close(fd_); } /** - * \fn Buffer::dmabuf() + * \fn Plane::dmabuf() * \brief Get the dmabuf file handle backing the buffer */ @@ -46,7 +77,7 @@ Buffer::~Buffer() * * \return 0 on success or a negative error value otherwise. */ -int Buffer::setDmabuf(int fd) +int Plane::setDmabuf(int fd) { if (fd_ != -1) { close(fd_); @@ -70,6 +101,146 @@ int Buffer::setDmabuf(int fd) return 0; } + +/** + * \brief Map the plane memory data to a CPU accessible address + * + * The file descriptor to map the memory from must be set by a call to + * setDmaBuf before calling this function. + * + * \sa setDmaBuf + * + * \return 0 on success or a negative error value otherwise. + */ +int Plane::mmap() +{ + void *map; + + if (mem_) + return 0; + + map = ::mmap(NULL, length_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, + offset_); + if (map == reinterpret_cast(-1)) { + int ret = -errno; + LOG(Buffer, Error) + << "Failed to mmap buffer: " << strerror(-ret); + return ret; + } + + mem_ = map; + + return 0; +} + +/** + * \brief Unmap any existing CPU accessible mapping + * + * Unmap the memory mapped by an earlier call to mmap. + * + * \return 0 on success or a negative error value otherwise. + */ +int Plane::munmap() +{ + int ret = 0; + + if (mem_) + ret = ::munmap(mem_, length_); + + if (ret) { + ret = -errno; + LOG(Buffer, Warning) + << "Failed to unmap buffer: " << strerror(ret); + } else { + mem_ = 0; + } + + return ret; +} + +/** + * \fn Plane::length() + * \brief Get the length of the memory buffer + */ + +/** + * \fn Plane::mem() + * \brief Get the CPU accessible memory address if mapped + */ + +/** + * \class Buffer + * \brief A memory buffer to store an image + * + * The Buffer class represents the memory buffers used to store a + * full frame image, which may contain multiple separate memory Plane + * objects if the image format is multi-planar. + */ + +Buffer::Buffer() + : index_(-1), format_(0), width_(0), height_(0) +{ +} + +Buffer::~Buffer() +{ + for (Plane *plane : planes_) + delete plane; + + planes_.clear(); +} + +/** + * \brief Map each buffer plane to a CPU accessible address + * + * Each buffer plane must have had it's dmabuf handle set before attempting to + * mmap. + * + * \return 0 on success or a negative error value otherwise. + */ +int Buffer::mmap() +{ + int ret; + + for (Plane *plane : planes_) { + ret = plane->mmap(); + if (ret) + return ret; + } + + return 0; +} + +/** + * \brief Unmap any existing CPU accessible mapping for each plane + * + * Unmap the memory mapped by an earlier call to mmap. + * + * \return 0 on success or a negative error value otherwise. + */ +int Buffer::munmap() +{ + int ret; + + for (Plane *plane : planes_) { + ret = plane->munmap(); + if (ret) + return ret; + } + + return 0; +} + +/** + * \fn Buffer::index() + * \brief Get the Buffer index value + */ + +/** + * \fn Buffer::planes() + * \brief Return a reference to the vector holding all Planes within the buffer + */ + /** * \class BufferPool * \brief A pool of buffers