Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlibcamera_dmabufregistry.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtMultimedia/private/qmultimedia_ranges_p.h>
8#include <QtCore/private/qsystemerror_p.h>
9
10#include <libcamera/base/span.h>
11#include <libcamera/camera.h>
12#include <libcamera/framebuffer.h>
13#include <libcamera/request.h>
14#include <libcamera/stream.h>
15
16#include <fcntl.h>
17
18#include <algorithm>
19#include <cerrno>
20#include <cstddef>
21#include <utility>
22
23#if QT_CONFIG(linux_dmabuf)
24
25QT_BEGIN_NAMESPACE
26
27using namespace Qt::StringLiterals;
28
29namespace QLibCamera {
30
31using namespace libcamera;
32
33namespace {
34
35using QtMultimediaPrivate::DmaBufLayout;
36using QtMultimediaPrivate::QDmaBuf;
37
38constexpr std::size_t kMinFreeBuffers = 2;
39
40// Duplicates the fd that all planes share, sized to cover every plane.
41q23::expected<std::shared_ptr<QDmaBuf>, QString>
42dmaBufForPlanes(Span<const FrameBuffer::Plane> planes)
43{
44 if (planes.empty())
45 return q23::unexpected(u"Capture buffer has no planes"_s);
46
47 const SharedFD fd = planes[0].fd;
48 qsizetype size = 0;
49 for (const FrameBuffer::Plane &plane : planes) {
50 if (plane.fd != fd)
51 return q23::unexpected(u"Capture buffer planes are not in one DMA-BUF"_s);
52 if (plane.offset == FrameBuffer::Plane::kInvalidOffset)
53 return q23::unexpected(u"Capture buffer plane has no offset"_s);
54 size = std::max(size, qsizetype(plane.offset) + qsizetype(plane.length));
55 }
56
57 libcamera::UniqueFD dup = fd.dup();
58
59 if (!dup.isValid()) {
60 return q23::unexpected(
61 u"Cannot duplicate capture buffer fd: %1"_s.arg(QSystemError::stdString(errno)));
62 }
63
64 return std::make_shared<QDmaBuf>(QUniqueFileDescriptorHandle{ dup.release() }, size);
65}
66
67// The planes must sit exactly where the layout places them.
68bool planesMatchLayout(Span<const FrameBuffer::Plane> planes, const DmaBufLayout &layout)
69{
70 if (planes.size() != std::size_t(layout.size()))
71 return false;
72
73 for (qsizetype i = 0; i < layout.size(); ++i) {
74 const FrameBuffer::Plane &plane = planes[std::size_t(i)];
75 if (plane.offset != layout[i].offset || qsizetype(plane.length) < layout[i].size)
76 return false;
77 }
78 return true;
79}
80
81} // namespace
82
83std::shared_ptr<QLibCameraDmaBufRegistry>
84QLibCameraDmaBufRegistry::create(std::shared_ptr<Camera> camera,
85 std::shared_ptr<QLibCameraPendingControls> pendingControls,
86 QSpan<const std::unique_ptr<libcamera::Request>> requests,
87 Stream *stream, const QVideoFrameFormat &format, int bytesPerLine)
88{
89 using namespace QtMultimediaPrivate;
90
91 if (requests.empty() || requests.size() > kCaptureBufferCount) {
92 qCDebug(lcLibCamera) << "Cannot share" << requests.size() << "capture buffers as DMA-BUFs";
93 return nullptr;
94 }
95
96 std::vector<Buffer> buffers;
97 std::optional<DmaBufLayout> layout;
98 for (const std::unique_ptr<Request> &request : requests) {
99 FrameBuffer *frameBuffer = request->findBuffer(stream);
100 if (!frameBuffer) {
101 qCDebug(lcLibCamera) << "Capture request has no buffer for the stream";
102 return nullptr;
103 }
104
105 const Span<const FrameBuffer::Plane> planes = frameBuffer->planes();
106 q23::expected<std::shared_ptr<QDmaBuf>, QString> dmaBuf = dmaBufForPlanes(planes);
107 if (!dmaBuf) {
108 qCDebug(lcLibCamera) << "Capture buffer cannot be shared as a DMA-BUF:"
109 << dmaBuf.error();
110 return nullptr;
111 }
112
113 if (buffers.empty()) {
114 layout = dmaBufLayoutForFormat(format.pixelFormat(), format.frameSize(), bytesPerLine,
115 quint32((*dmaBuf)->size()));
116 }
117
118 if (layout && !planesMatchLayout(planes, *layout)) {
119 qCDebug(lcLibCamera) << "Capture buffer planes do not match the zero-copy layout";
120 return nullptr;
121 }
122
123 buffers.push_back(Buffer{ request.get(), std::move(*dmaBuf) });
124 }
125
126 return std::shared_ptr<QLibCameraDmaBufRegistry>(new QLibCameraDmaBufRegistry(
127 std::move(camera), std::move(pendingControls), std::move(buffers), std::move(layout)));
128}
129
130QLibCameraDmaBufRegistry::QLibCameraDmaBufRegistry(
131 std::shared_ptr<Camera> camera, std::shared_ptr<QLibCameraPendingControls> pendingControls,
132 std::vector<Buffer> buffers, std::optional<DmaBufLayout> layout)
133 : CaptureSlotRegistry(buffers.size(), kMinFreeBuffers),
134 m_camera(std::move(camera)),
135 m_pendingControls(std::move(pendingControls)),
136 m_buffers(std::move(buffers)),
137 m_layout(std::move(layout))
138{
139}
140
141std::shared_ptr<QDmaBuf> QLibCameraDmaBufRegistry::dmaBufFor(const Request *request) const
142{
143 namespace ranges = QtMultimediaPrivate::ranges;
144
145 const auto it = ranges::find_if(m_buffers, [&](const Buffer &buffer) {
146 return buffer.request == request;
147 });
148 if (it == m_buffers.end())
149 return nullptr;
150 return it->dmaBuf;
151}
152
153void QLibCameraDmaBufRegistry::recycleSlot(Request *request) const
154{
155 requeueRequest(*m_camera, request, *m_pendingControls);
156}
157
158} // namespace QLibCamera
159
160QT_END_NAMESPACE
161
162#endif // QT_CONFIG(linux_dmabuf)