@@ -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_;
};
@@ -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 */
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(+)