From patchwork Tue Jan 29 13:53:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 441 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CEE260DB6 for ; Tue, 29 Jan 2019 14:54:02 +0100 (CET) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9887185; Tue, 29 Jan 2019 14:54:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548770041; bh=mpJhigK67X9Dl/OhAeAIAWF/WJ/ksRHJbPvIx1oPqMc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OjSxGv0bWICmo5gj2TG37zW9ePNc/XjPx+BPYsdaxUIdY8MyzPxnu+W3oA3Hg1c3r 9IJrM+FTu8sJBJe2rVZRMT8iq9DIqyLOk22CGSAmxMBwY88YlIrKE/N00+e/lg+d2t f3vMtupo3Wt6zCL31nEcDCMkfek/esiwfnkT4mHY= From: Kieran Bingham To: LibCamera Devel Date: Tue, 29 Jan 2019 13:53:54 +0000 Message-Id: <20190129135357.32339-2-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190129135357.32339-1-kieran.bingham@ideasonboard.com> References: <20190129135357.32339-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/4] 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: Tue, 29 Jan 2019 13:54:02 -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 c0511cf6d662..6619e556ebbd 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 d7cb55ba4a49..8fcbbf7892ea 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 f9f25c0ecf15..b5b25965fd12 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',