Loading...
Searching...
No Matches
dma_buf_allocator.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Raspberry Pi Ltd
*
* Helper class for dma-buf allocations.
*/
#pragma once
#include <memory>
#include <stdint.h>
#include <string>
#include <vector>
#include <libcamera/base/flags.h>
#include <libcamera/base/shared_fd.h>
#include <libcamera/base/unique_fd.h>
namespace libcamera {
class FrameBuffer;
class DmaBufAllocator
{
public:
enum class DmaBufAllocatorFlag {
CmaHeap = 1 << 0,
SystemHeap = 1 << 1,
UDmaBuf = 1 << 2,
};
using DmaBufAllocatorFlags = Flags<DmaBufAllocatorFlag>;
DmaBufAllocator(DmaBufAllocatorFlags flags = DmaBufAllocatorFlag::CmaHeap);
~DmaBufAllocator();
bool isValid() const { return providerHandle_.isValid(); }
UniqueFD alloc(const char *name, std::size_t size);
int exportBuffers(unsigned int count,
const std::vector<unsigned int> &planeSizes,
std::vector<std::unique_ptr<FrameBuffer>> *buffers);
private:
std::unique_ptr<FrameBuffer> createBuffer(
std::string name, const std::vector<unsigned int> &planeSizes);
UniqueFD allocFromHeap(const char *name, std::size_t size);
UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);
UniqueFD providerHandle_;
DmaBufAllocatorFlag type_;
};
class DmaSyncer final
{
public:
enum class SyncType {
Read = 0,
Write,
ReadWrite,
};
explicit DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite);
DmaSyncer(DmaSyncer &&other) = default;
DmaSyncer &operator=(DmaSyncer &&other) = default;
~DmaSyncer();
private:
LIBCAMERA_DISABLE_COPY(DmaSyncer)
void sync(uint64_t step);
SharedFD fd_;
uint64_t flags_ = 0;
};
LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag)
} /* namespace libcamera */
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
libcamera::DmaBufAllocator::isValid
bool isValid() const
Check if the DmaBufAllocator instance is valid.
Definition dma_buf_allocator.h:36
libcamera::DmaBufAllocator::exportBuffers
int exportBuffers(unsigned int count, const std::vector< unsigned int > &planeSizes, std::vector< std::unique_ptr< FrameBuffer > > *buffers)
Allocate and export buffers from the DmaBufAllocator.
Definition dma_buf_allocator.cpp:223
libcamera::DmaBufAllocator::DmaBufAllocator
DmaBufAllocator(DmaBufAllocatorFlags flags=DmaBufAllocatorFlag::CmaHeap)
Construct a DmaBufAllocator of a given type.
Definition dma_buf_allocator.cpp:97
libcamera::DmaBufAllocator::DmaBufAllocatorFlag
DmaBufAllocatorFlag
Type of the dma-buf provider.
Definition dma_buf_allocator.h:26
libcamera::DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap
@ CmaHeap
Allocate from a CMA dma-heap, providing physically-contiguous memory.
Definition dma_buf_allocator.h:27
libcamera::DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap
@ SystemHeap
Allocate from the system dma-heap, using the page allocator.
Definition dma_buf_allocator.h:28
libcamera::DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf
@ UDmaBuf
Allocate using a memfd + /dev/udmabuf.
Definition dma_buf_allocator.h:29
libcamera::DmaBufAllocator::DmaBufAllocatorFlags
Flags< DmaBufAllocatorFlag > DmaBufAllocatorFlags
A bitwise combination of DmaBufAllocator::DmaBufAllocatorFlag values.
Definition dma_buf_allocator.h:32
libcamera::DmaBufAllocator::~DmaBufAllocator
~DmaBufAllocator()
Destroy the DmaBufAllocator instance.
libcamera::DmaBufAllocator::alloc
UniqueFD alloc(const char *name, std::size_t size)
Allocate a dma-buf from the DmaBufAllocator.
Definition dma_buf_allocator.cpp:200
libcamera::DmaSyncer::SyncType
SyncType
Read and/or write access via the CPU map.
Definition dma_buf_allocator.h:56
libcamera::DmaSyncer::SyncType::Write
@ Write
Indicates that the mapped dm-buf will be written by the client via the CPU map.
Definition dma_buf_allocator.h:58
libcamera::DmaSyncer::SyncType::ReadWrite
@ ReadWrite
Indicates that the mapped dma-buf will be read and written by the client via the CPU map.
Definition dma_buf_allocator.h:59
libcamera::DmaSyncer::SyncType::Read
@ Read
Indicates that the mapped dma-buf will be read by the client via the CPU map.
Definition dma_buf_allocator.h:57
libcamera::DmaSyncer::DmaSyncer
DmaSyncer(DmaSyncer &&other)=default
Enable move on class DmaSyncer.
libcamera::DmaSyncer::DmaSyncer
DmaSyncer(SharedFD fd, SyncType type=SyncType::ReadWrite)
Construct a DmaSyncer with a dma-buf's fd and the access type.
Definition dma_buf_allocator.cpp:296
libcamera::DmaSyncer::operator=
DmaSyncer & operator=(DmaSyncer &&other)=default
Enable move on class DmaSyncer.
libcamera::Flags
Type-safe container for enum-based bitfields.
Definition flags.h:16
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
flags.h
Enum-based bit fields.
LIBCAMERA_FLAGS_ENABLE_OPERATORS
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
Enable bitwise operations on the enum enumeration.
Definition flags.h:189
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
shared_fd.h
File descriptor wrapper.
unique_fd.h
File descriptor wrapper that owns a file descriptor.