[libcamera-devel,v3,3/3] libcamera: Add exportFrameBuffers in HeapAllocator
diff mbox series

Message ID 20230302081349.3334290-4-chenghaoyang@google.com
State Superseded
Headers show
Series
  • Add HeapAllocator
Related show

Commit Message

Harvey Yang March 2, 2023, 8:13 a.m. UTC
Add a helper function exportFrameBuffers in HeapAllocator to make it
easier to use.

Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
---
 include/libcamera/heap_allocator.h | 10 +++++++
 src/libcamera/heap_allocator.cpp   | 44 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h
index cd7ed1a3..138596de 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,10 @@ 
 
 namespace libcamera {
 
+class Camera;
+class Stream;
+class FrameBuffer;
+
 class HeapAllocator
 {
 public:
@@ -23,7 +28,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(std::size_t size);
+
 	std::unique_ptr<Heap> heap_;
 };
 
diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp
index 179c2c21..1faa5585 100644
--- a/src/libcamera/heap_allocator.cpp
+++ b/src/libcamera/heap_allocator.cpp
@@ -17,7 +17,10 @@ 
 
 #include <libcamera/base/log.h>
 
+#include <libcamera/camera.h>
 #include <libcamera/dma_heap.h>
+#include <libcamera/framebuffer.h>
+#include <libcamera/stream.h>
 #include <libcamera/udma_heap.h>
 
 namespace libcamera {
@@ -43,4 +46,45 @@  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 */
+	size_t size = stream->configuration().frameSize;
+
+	for (unsigned i = 0; i < count; ++i) {
+		std::unique_ptr<FrameBuffer> buffer = createBuffer(size);
+		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(std::size_t size)
+{
+	std::vector<FrameBuffer::Plane> planes;
+
+	UniqueFD fd = alloc("Buffer", size);
+	if (!fd.isValid())
+		return nullptr;
+
+	FrameBuffer::Plane plane;
+	plane.fd = SharedFD(std::move(fd));
+	plane.offset = 0;
+	plane.length = size;
+
+	planes.push_back(std::move(plane));
+
+	return std::make_unique<FrameBuffer>(planes);
+}
+
 } /* namespace libcamera */