libcamera  v0.4.0
Supporting cameras in Linux since 2019
dma_buf_allocator.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2020, Raspberry Pi Ltd
4  *
5  * Helper class for dma-buf allocations.
6  */
7 
8 #pragma once
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include <libcamera/base/flags.h>
17 
18 namespace libcamera {
19 
20 class FrameBuffer;
21 
23 {
24 public:
25  enum class DmaBufAllocatorFlag {
26  CmaHeap = 1 << 0,
27  SystemHeap = 1 << 1,
28  UDmaBuf = 1 << 2,
29  };
30 
32 
35  bool isValid() const { return providerHandle_.isValid(); }
36  UniqueFD alloc(const char *name, std::size_t size);
37 
38  int exportBuffers(unsigned int count,
39  const std::vector<unsigned int> &planeSizes,
40  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
41 
42 private:
43  std::unique_ptr<FrameBuffer> createBuffer(
44  std::string name, const std::vector<unsigned int> &planeSizes);
45 
46  UniqueFD allocFromHeap(const char *name, std::size_t size);
47  UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);
48  UniqueFD providerHandle_;
49  DmaBufAllocatorFlag type_;
50 };
51 
52 class DmaSyncer final
53 {
54 public:
55  enum class SyncType {
56  Read = 0,
57  Write,
58  ReadWrite,
59  };
60 
61  explicit DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite);
62 
63  ~DmaSyncer();
64 
65 private:
66  void sync(uint64_t step);
67 
68  SharedFD fd_;
69  uint64_t flags_ = 0;
70 };
71 
73 
74 } /* namespace libcamera */
RAII-style wrapper for file descriptors.
Definition: shared_fd.h:16
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
Allocate using a memfd + /dev/udmabuf.
File descriptor wrapper.
Helper class for dma-buf allocations.
Definition: dma_buf_allocator.h:22
File descriptor wrapper that owns a file descriptor.
DmaBufAllocator(DmaBufAllocatorFlags flags=DmaBufAllocatorFlag::CmaHeap)
Construct a DmaBufAllocator of a given type.
Definition: dma_buf_allocator.cpp:97
Top-level libcamera namespace.
Definition: backtrace.h:17
SyncType
Read and/or write access via the CPU map.
Definition: dma_buf_allocator.h:55
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:17
~DmaBufAllocator()
Destroy the DmaBufAllocator instance.
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
Enable bitwise operations on the enum enumeration.
Allocate from the system dma-heap, using the page allocator.
Enum-based bit fields.
Allocate from a CMA dma-heap, providing physically-contiguous memory.
Type-safe container for enum-based bitfields.
Definition: flags.h:15
UniqueFD alloc(const char *name, std::size_t size)
Allocate a dma-buf from the DmaBufAllocator.
Definition: dma_buf_allocator.cpp:200
bool isValid() const
Check if the DmaBufAllocator instance is valid.
Definition: dma_buf_allocator.h:35
DmaBufAllocatorFlag
Type of the dma-buf provider.
Definition: dma_buf_allocator.h:25
Helper class for dma-buf&#39;s synchronization.
Definition: dma_buf_allocator.h:52