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 <fcntl.h>
12#include <linux/memfd.h>
13#include <linux/udmabuf.h>
14#include <sys/ioctl.h>
15#include <sys/mman.h>
16#include <unistd.h>
17
18QT_BEGIN_NAMESPACE
19
20quint64 udmabufPageAlignedSize(quint64 size)
21{
22 const quint64 pageSize = ::sysconf(_SC_PAGESIZE);
23 return (size + pageSize - 1) / pageSize * pageSize;
24}
25
26QUniqueFileDescriptorHandle createUdmabufTestMemfd(const QByteArray &data, quint64 bufferSize,
27 const char *debugName)
28{
29 QUniqueFileDescriptorHandle memfd{
30 ::memfd_create(debugName, MFD_ALLOW_SEALING),
31 };
32 if (!memfd)
33 return {};
34 if (::ftruncate(memfd.get(), bufferSize) != 0)
35 return {};
36 if (::write(memfd.get(), data.constData(), data.size()) != data.size())
37 return {};
38 if (::fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK) != 0)
39 return {};
40 return memfd;
41}
42
43QUniqueFileDescriptorHandle createUdmabufFd(const QUniqueFileDescriptorHandle &memFd, quint64 size)
44{
45 QUniqueFileDescriptorHandle device{
46 ::open("/dev/udmabuf", O_RDWR),
47 };
48 if (!device)
49 return {};
50
51 udmabuf_create create{
52 .memfd = uint32_t(memFd.get()),
53 .flags = UDMABUF_FLAGS_CLOEXEC,
54 .offset = 0,
55 .size = size,
56 };
57
58 QUniqueFileDescriptorHandle dmabufFd{
59 ::ioctl(device.get(), UDMABUF_CREATE, &create),
60 };
61 return dmabufFd;
62}
63
65{
66 return ::access("/dev/udmabuf", R_OK | W_OK) == 0;
67}
68
69QT_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)