[PATCH v8 1/8] libcamera: add DmaBufAllocator::exportBuffers()
Harvey Yang
chenghaoyang at chromium.org
Mon Aug 5 15:48:32 CEST 2024
Add a helper function exportBuffers in DmaBufAllocator to make it easier
to use.
It'll be used in Virtual Pipeline Handler and SoftwareIsp.
Signed-off-by: Harvey Yang <chenghaoyang at chromium.org>
---
.../libcamera/internal/dma_buf_allocator.h | 12 ++++
src/libcamera/dma_buf_allocator.cpp | 55 +++++++++++++++++++
2 files changed, 67 insertions(+)
diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h
index 36ec1696..3a9b56b1 100644
--- a/include/libcamera/internal/dma_buf_allocator.h
+++ b/include/libcamera/internal/dma_buf_allocator.h
@@ -8,12 +8,16 @@
#pragma once
#include <stddef.h>
+#include <vector>
#include <libcamera/base/flags.h>
#include <libcamera/base/unique_fd.h>
namespace libcamera {
+class FrameBuffer;
+struct StreamConfiguration;
+
class DmaBufAllocator
{
public:
@@ -30,7 +34,15 @@ public:
bool isValid() const { return providerHandle_.isValid(); }
UniqueFD alloc(const char *name, std::size_t size);
+ int exportBuffers(
+ std::size_t count,
+ std::vector<std::size_t> frameSize,
+ std::vector<std::unique_ptr<FrameBuffer>> *buffers);
+
private:
+ std::unique_ptr<FrameBuffer> createBuffer(
+ std::string name, std::vector<std::size_t> frameSizes);
+
UniqueFD allocFromHeap(const char *name, std::size_t size);
UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);
UniqueFD providerHandle_;
diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp
index c06eca7d..a2df1730 100644
--- a/src/libcamera/dma_buf_allocator.cpp
+++ b/src/libcamera/dma_buf_allocator.cpp
@@ -23,6 +23,11 @@
#include <libcamera/base/log.h>
+#include <libcamera/framebuffer.h>
+#include <libcamera/stream.h>
+
+#include "libcamera/internal/formats.h"
+
/**
* \file dma_buf_allocator.cpp
* \brief dma-buf allocator
@@ -243,4 +248,54 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)
return allocFromHeap(name, size);
}
+/**
+ * \brief Allocate and export buffers for \a stream from the DmaBufAllocator
+ * \param[in] count The number of FrameBuffers required
+ * \param[in] frameSizes The sizes of planes in the FrameBuffer
+ * \param[out] buffers Array of buffers successfully allocated
+ *
+ * \return The number of allocated buffers on success or a negative error code
+ * otherwise
+ */
+int DmaBufAllocator::exportBuffers(
+ std::size_t count,
+ std::vector<std::size_t> frameSizes,
+ std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+{
+ for (unsigned i = 0; i < count; ++i) {
+ std::unique_ptr<FrameBuffer> buffer =
+ createBuffer("frame-" + std::to_string(i), frameSizes);
+ if (!buffer) {
+ LOG(DmaBufAllocator, Error) << "Unable to create buffer";
+
+ buffers->clear();
+ return -EINVAL;
+ }
+
+ buffers->push_back(std::move(buffer));
+ }
+
+ return count;
+}
+
+std::unique_ptr<FrameBuffer> DmaBufAllocator::createBuffer(
+ std::string name, std::vector<std::size_t> frameSizes)
+{
+ std::vector<FrameBuffer::Plane> planes;
+
+ for (auto frameSize : frameSizes) {
+ UniqueFD fd = alloc(name.c_str(), frameSize);
+ if (!fd.isValid())
+ return nullptr;
+
+ FrameBuffer::Plane plane;
+ plane.fd = SharedFD(std::move(fd));
+ plane.offset = 0;
+ plane.length = frameSize;
+ planes.push_back(std::move(plane));
+ }
+
+ return std::make_unique<FrameBuffer>(planes);
+}
+
} /* namespace libcamera */
--
2.46.0.rc2.264.g509ed76dc8-goog
More information about the libcamera-devel
mailing list