libcamera  v0.5.0+49-e5442c31
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <stdint.h>
12 #include <string>
13 #include <vector>
14 
15 #include <libcamera/base/flags.h>
18 
19 namespace libcamera {
20 
21 class FrameBuffer;
22 
24 {
25 public:
26  enum class DmaBufAllocatorFlag {
27  CmaHeap = 1 << 0,
28  SystemHeap = 1 << 1,
29  UDmaBuf = 1 << 2,
30  };
31 
33 
36  bool isValid() const { return providerHandle_.isValid(); }
37  UniqueFD alloc(const char *name, std::size_t size);
38 
39  int exportBuffers(unsigned int count,
40  const std::vector<unsigned int> &planeSizes,
41  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
42 
43 private:
44  std::unique_ptr<FrameBuffer> createBuffer(
45  std::string name, const std::vector<unsigned int> &planeSizes);
46 
47  UniqueFD allocFromHeap(const char *name, std::size_t size);
48  UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);
49  UniqueFD providerHandle_;
50  DmaBufAllocatorFlag type_;
51 };
52 
53 class DmaSyncer final
54 {
55 public:
56  enum class SyncType {
57  Read = 0,
58  Write,
59  ReadWrite,
60  };
61 
62  explicit DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite);
63 
64  DmaSyncer(DmaSyncer &&other) = default;
65  DmaSyncer &operator=(DmaSyncer &&other) = default;
66 
67  ~DmaSyncer();
68 
69 private:
71 
72  void sync(uint64_t step);
73 
74  SharedFD fd_;
75  uint64_t flags_ = 0;
76 };
77 
79 
80 } /* 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:23
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
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
SyncType
Read and/or write access via the CPU map.
Definition: dma_buf_allocator.h:56
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:16
~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:36
DmaBufAllocatorFlag
Type of the dma-buf provider.
Definition: dma_buf_allocator.h:26
Helper class for dma-buf&#39;s synchronization.
Definition: dma_buf_allocator.h:53