@@ -9,12 +9,16 @@
#pragma once
#include <stddef.h>
+#include <vector>
#include <libcamera/base/unique_fd.h>
namespace libcamera {
+class Camera;
+class FrameBuffer;
class Heap;
+struct StreamConfiguration;
class HeapAllocator
{
@@ -25,7 +29,13 @@ public:
bool isValid() const;
UniqueFD alloc(const char *name, std::size_t size);
+ int exportFrameBuffers(
+ const StreamConfiguration &config,
+ std::vector<std::unique_ptr<FrameBuffer>> *buffers);
+
private:
+ std::unique_ptr<FrameBuffer> createBuffer(const StreamConfiguration &config);
+
std::unique_ptr<Heap> heap_;
};
@@ -22,6 +22,12 @@
#include <libcamera/base/log.h>
+#include <libcamera/camera.h>
+#include <libcamera/framebuffer.h>
+#include <libcamera/stream.h>
+
+#include "libcamera/internal/formats.h"
+
/**
* \file libcamera/internal/heap_allocator.h
* \brief Internal heap buffer allocator support
@@ -291,7 +297,59 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)
return {};
}
- return heap_->alloc(name, size);
+ std::size_t pageSize = getpagesize();
+ /* Allocation size needs to be a direct multiple of pageSize. */
+ std::size_t numPage = (size + pageSize - 1) / pageSize;
+
+ return heap_->alloc(name, numPage * pageSize);
+}
+
+int HeapAllocator::exportFrameBuffers(
+ const StreamConfiguration &config,
+ std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+{
+ unsigned int count = config.bufferCount;
+
+ for (unsigned i = 0; i < count; ++i) {
+ std::unique_ptr<FrameBuffer> buffer = createBuffer(config);
+ 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(
+ const StreamConfiguration &config)
+{
+ std::vector<FrameBuffer::Plane> planes;
+
+ auto info = PixelFormatInfo::info(config.pixelFormat);
+ for (size_t i = 0; i < info.planes.size(); ++i) {
+ unsigned int planeSize = info.planeSize(config.size, i);
+ if (planeSize == 0)
+ continue;
+
+ UniqueFD fd = alloc("FrameBuffer", planeSize);
+ if (!fd.isValid())
+ return nullptr;
+
+ SharedFD sharedFd(std::move(fd));
+
+ FrameBuffer::Plane plane;
+ plane.fd = sharedFd;
+ plane.offset = 0;
+ plane.length = planeSize;
+ 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/internal/heap_allocator.h | 10 ++++ src/libcamera/heap_allocator.cpp | 60 ++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-)