[libcamera-devel] [PATCH v4 3/3] libcamera: Add exportFrameBuffers in HeapAllocator

Harvey Yang chenghaoyang at chromium.org
Tue Mar 28 11:55:34 CEST 2023


From: Cheng-Hao Yang <chenghaoyang at chromium.org>

Add a helper function exportFrameBuffers in HeapAllocator to make it
easier to use.

Signed-off-by: Harvey Yang <chenghaoyang at chromium.org>
---
 include/libcamera/heap_allocator.h | 11 ++++++
 src/libcamera/heap_allocator.cpp   | 56 ++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h
index cd7ed1a3..076f0951 100644
--- a/include/libcamera/heap_allocator.h
+++ b/include/libcamera/heap_allocator.h
@@ -8,6 +8,7 @@
 #pragma once
 
 #include <stddef.h>
+#include <vector>
 
 #include <libcamera/base/unique_fd.h>
 
@@ -15,6 +16,11 @@
 
 namespace libcamera {
 
+class Camera;
+class Stream;
+class FrameBuffer;
+class PixelFormatInfo;
+
 class HeapAllocator
 {
 public:
@@ -23,7 +29,12 @@ public:
 	bool isValid() const { return heap_->isValid(); }
 	UniqueFD alloc(const char *name, std::size_t size);
 
+	int exportFrameBuffers(Camera *camera, Stream *stream,
+			       std::vector<std::unique_ptr<FrameBuffer>> *buffers);
+
 private:
+	std::unique_ptr<FrameBuffer> createBuffer(Stream *stream);
+
 	std::unique_ptr<Heap> heap_;
 };
 
diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp
index 179c2c21..33e9eaca 100644
--- a/src/libcamera/heap_allocator.cpp
+++ b/src/libcamera/heap_allocator.cpp
@@ -17,7 +17,11 @@
 
 #include <libcamera/base/log.h>
 
+#include <libcamera/camera.h>
 #include <libcamera/dma_heap.h>
+#include <libcamera/framebuffer.h>
+#include <libcamera/internal/formats.h>
+#include <libcamera/stream.h>
 #include <libcamera/udma_heap.h>
 
 namespace libcamera {
@@ -43,4 +47,56 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)
 	return heap_->alloc(name, size);
 }
 
+int HeapAllocator::exportFrameBuffers([[maybe_unused]] Camera *camera, Stream *stream,
+				      std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+{
+	unsigned int count = stream->configuration().bufferCount;
+
+	/** \todo: Support multiplanar allocations */
+
+	for (unsigned i = 0; i < count; ++i) {
+		std::unique_ptr<FrameBuffer> buffer = createBuffer(stream);
+		if (!buffer) {
+			LOG(HeapAllocator, Error) << "Unable to create buffer";
+
+			buffers->clear();
+			return -EINVAL;
+		}
+
+		buffers->push_back(std::move(buffer));
+	}
+
+	return count;
+}
+
+std::unique_ptr<FrameBuffer> HeapAllocator::createBuffer(Stream *stream)
+{
+	std::vector<FrameBuffer::Plane> planes;
+
+	int num_page = (stream->configuration().frameSize + getpagesize() - 1) / getpagesize();
+
+	UniqueFD fd = alloc("Buffer", num_page * getpagesize());
+	if (!fd.isValid())
+		return nullptr;
+
+	auto info = PixelFormatInfo::info(stream->configuration().pixelFormat);
+	SharedFD shared_fd(std::move(fd));
+	unsigned int offset = 0;
+	for (long unsigned int i = 0; i < info.planes.size(); ++i) {
+		unsigned int planeSize = info.planeSize(stream->configuration().size, i);
+		if (planeSize == 0)
+			continue;
+
+		FrameBuffer::Plane plane;
+		plane.fd = shared_fd;
+		plane.offset = offset;
+		plane.length = planeSize;
+		planes.push_back(std::move(plane));
+
+		offset += planeSize;
+	}
+
+	return std::make_unique<FrameBuffer>(planes);
+}
+
 } /* namespace libcamera */
-- 
2.40.0



More information about the libcamera-devel mailing list