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
qdmabufvideobuffer.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
5
6#include <QtMultimedia/private/qmultimedia_dmabuf_support_p.h>
7#include <QtMultimedia/private/qvideotexturehelper_p.h>
8#include <QtMultimedia/private/qrhivaluemapper_p.h>
9#include <QtGui/rhi/qrhi.h>
10#include <QtCore/qloggingcategory.h>
11#include <QtCore/private/qcore_unix_p.h>
12#include <QtCore/private/qexpected_p.h>
13
14#if QT_CONFIG(linux_dmabuf_egl)
15# include <QtMultimedia/private/qdmabuftextureimporter_p.h>
16#endif
17
18#include <cerrno>
19#include <cstring>
20#include <system_error>
21#include <fcntl.h>
22#include <unistd.h>
23#include <sys/mman.h>
24
25static_assert(QT_CONFIG(linux_dmabuf));
26
28
29Q_STATIC_LOGGING_CATEGORY(qLcDmaBufVideoBuffer, "qt.multimedia.dmabufvideobuffer");
30
31namespace QtMultimediaPrivate {
32
33#if QT_CONFIG(linux_dmabuf_egl)
35#endif
36
37namespace {
38
39q23::expected<QUniqueFileDescriptorHandle, std::error_code> dupFd(int fd)
40{
41 QUniqueFileDescriptorHandle duped{ ::fcntl(fd, F_DUPFD_CLOEXEC, 0) };
42 if (!duped)
43 return q23::unexpected(std::error_code(errno, std::generic_category()));
44 return duped;
45}
46
47q23::expected<DmaBufPlane, std::error_code> planeWithDupedFd(DmaBufPlane plane)
48{
49 auto duped = dupFd(plane.fd);
50 if (!duped)
51 return q23::unexpected(duped.error());
52
53 plane.fd = duped->release();
54 return plane;
55}
56
57} // namespace
58
67
86
96
98{
100
101 if (rhi.backend() != QRhi::OpenGLES2)
102 return {};
103
104#if QT_CONFIG(linux_dmabuf_egl)
105 // DmaBufEglContext is bound to the GL context associated with a QRhi;
106 // textures are (re-)imported on the RHI render thread, so this is
107 // created lazily here (rather than in the constructor, which may run on
108 // a different thread) and cached per-rhi.
110 return DmaBufEglContext(&rhi);
111 });
112
113 if (!eglContext.isValid()) {
114 qCDebug(qLcDmaBufVideoBuffer) << "DmaBufEglContext is not valid, falling back to CPU path";
115 return {};
116 }
117
120 if (!handles) {
123 << "Unrecoverable EGL import failure, disabling texture import for this buffer";
125 }
126 return {};
127 }
128
130 m_size);
131#else // QT_CONFIG(linux_dmabuf_egl)
133 << "DMABUF texture import is not supported in this build, falling back to CPU path";
134 return {};
135#endif // QT_CONFIG(linux_dmabuf_egl)
136}
137
139{
140 for (const MappedRegion &region : regions) {
143 }
144}
145
147{
149
150 // The producer (driver/compositor) owns write access to the DMABUF.
152 return mapData;
153
155 if (!desc)
156 return mapData;
157
158 for (qsizetype i = 0; i < m_planes.size(); ++i) {
159 const DmaBufPlane &plane = m_planes[i];
160
161 auto fd = dupFd(plane.fd);
162 if (!fd) {
164 << "Failed to dup DMABUF fd for plane" << i << ':' << fd.error().message();
167 return {};
168 }
169
170 const off_t len = ::lseek(fd->get(), 0, SEEK_END);
171 void *mapped = len > 0 ? ::mmap(nullptr, size_t(len), PROT_READ, MAP_SHARED, fd->get(), 0)
172 : MAP_FAILED;
173 if (mapped == MAP_FAILED) {
175 << "Could not map DMABUF for plane" << i << ':' << qt_error_string(errno);
178 return {};
179 }
180
182 const QSpan<std::byte> data{
183 static_cast<std::byte *>(mapped),
184 qsizetype(len),
185 };
187 std::move(*fd),
188 data,
189 });
190
191 mapData.data[i] = reinterpret_cast<uchar *>(data.data()) + plane.offset;
194 }
195
197
198 m_mapMode = mode;
199 return mapData;
200}
201
209
210} // namespace QtMultimediaPrivate
211
212QT_END_NAMESPACE
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
#define MAP_FAILED