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',