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
udmabuftestutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4// Needed for F_ADD_SEALS/F_SEAL_SHRINK (fcntl.h) and memfd_create (sys/mman.h).
5#ifndef _GNU_SOURCE
6#define _GNU_SOURCE
7#endif
8
10
11#include <QtMultimedia/private/qmultimediautils_p.h>
12
13#include <fcntl.h>
14#include <linux/memfd.h>
15#include <linux/udmabuf.h>
16#include <sys/mman.h>
17#include <unistd.h>
18
19QT_BEGIN_NAMESPACE
20
21quint64 udmabufPageAlignedSize(quint64 size)
22{
23 const quint64 pageSize = ::sysconf(_SC_PAGESIZE);
24 return (size + pageSize - 1) / pageSize * pageSize;
25}
26
27QUniqueFileDescriptorHandle createUdmabufTestMemfd(const QByteArray &data, quint64 bufferSize,
28 const char *debugName)
29{
30 QUniqueFileDescriptorHandle memfd{
31 ::memfd_create(debugName, MFD_ALLOW_SEALING),
32 };
33 if (!memfd)
34 return {};
35 if (::ftruncate(memfd.get(), bufferSize) != 0)
36 return {};
37 if (::write(memfd.get(), data.constData(), data.size()) != data.size())
38 return {};
39 if (::fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK) != 0)
40 return {};
41 return memfd;
42}
43
44QUniqueFileDescriptorHandle createUdmabufFd(const QUniqueFileDescriptorHandle &memFd, quint64 size)
45{
46 QUniqueFileDescriptorHandle device{
47 ::open("/dev/udmabuf", O_RDWR),
48 };
49 if (!device)
50 return {};
51
52 udmabuf_create create{
53 .memfd = uint32_t(memFd.get()),
54 .flags = UDMABUF_FLAGS_CLOEXEC,
55 .offset = 0,
56 .size = size,
57 };
58
59 QUniqueFileDescriptorHandle dmabufFd{
60 QtMultimediaPrivate::qt_safe_ioctl(device.get(), UDMABUF_CREATE, &create),
61 };
62 return dmabufFd;
63}
64
66{
67 return ::access("/dev/udmabuf", R_OK | W_OK) == 0;
68}
69
70QT_END_NAMESPACE
#define _GNU_SOURCE
Definition forkfd_qt.c:6
QUniqueFileDescriptorHandle createUdmabufTestMemfd(const QByteArray &data, quint64 bufferSize, const char *debugName)
bool canUseUdmabuf()
QUniqueFileDescriptorHandle createUdmabufFd(const QUniqueFileDescriptorHandle &memFd, quint64 size)