new file mode 100644
@@ -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 <vector>
+
+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<Buffer *> buffers_;
+};
+
+} /* namespace libcamera */
+
+#endif /* __LIBCAMERA_BUFFER_H__ */
@@ -7,6 +7,7 @@
#ifndef __LIBCAMERA_LIBCAMERA_H__
#define __LIBCAMERA_LIBCAMERA_H__
+#include <libcamera/buffer.h>
#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <libcamera/event_dispatcher.h>
@@ -1,4 +1,5 @@
libcamera_api = files([
+ 'buffer.h',
'camera.h',
'camera_manager.h',
'event_dispatcher.h',
new file mode 100644
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2018, Google Inc.
+ *
+ * buffer.cpp - Buffer handling
+ */
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <libcamera/buffer.h>
+
+/**
+ * \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 */
@@ -1,4 +1,5 @@
libcamera_sources = files([
+ 'buffer.cpp',
'camera.cpp',
'camera_manager.cpp',
'device_enumerator.cpp',