Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qmultimedia_dmabuf_support.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
4
#
include
"qmultimedia_dmabuf_support_p.h"
5
6
#
include
<
QtMultimedia
/
private
/
qdmabufallocator_p
.
h
>
7
#
include
<
QtMultimedia
/
private
/
qmultimediautils_p
.
h
>
8
#
include
<
QtMultimedia
/
private
/
qtmm_alignment_support_p
.
h
>
9
#
include
<
QtCore
/
qloggingcategory
.
h
>
10
11
#
if
QT_CONFIG
(
linux_dmabuf
)
12
#
include
<
QtCore
/
qscopeguard
.
h
>
13
#
include
<
linux
/
dma
-
buf
.
h
>
14
#
include
<
sys
/
ioctl
.
h
>
15
#
include
<
sys
/
mman
.
h
>
16
#
include
<
unistd
.
h
>
17
#
endif
18
19
#
include
<
cerrno
>
20
21
QT_BEGIN_NAMESPACE
22
23
namespace
QtMultimediaPrivate
{
24
25
#
if
QT_CONFIG
(
linux_dmabuf
)
26
27
Q_STATIC_LOGGING_CATEGORY
(
qLcDmaBufSupport
,
"qt.multimedia.dmabufsupport"
);
28
29
void
dmaBufSyncRead
(
int
fd
,
DmaBufCpuAccess
access
)
30
{
31
const
uint64_t
startOrEnd
=
32
access
==
DmaBufCpuAccess
::
Begin
?
DMA_BUF_SYNC_START
:
DMA_BUF_SYNC_END
;
33
34
dma_buf_sync
sync
= {
35
.
flags
=
startOrEnd
|
DMA_BUF_SYNC_READ
,
36
};
37
if
(
qt_safe_ioctl
(
fd
,
DMA_BUF_IOCTL_SYNC
, &
sync
) < 0)
38
qCDebug
(
qLcDmaBufSupport
) <<
"DMA_BUF_IOCTL_SYNC failed"
<<
qt_error_string
(
errno
);
39
}
40
41
std
::
optional
<
QByteArray
>
copyDmaBufData
(
int
fd
,
off_t
offset
,
size_t
size
,
size_t
bufferSize
)
42
{
43
if
(
offset
< 0 ||
size_t
(
offset
) >
bufferSize
||
size
>
bufferSize
-
size_t
(
offset
)) {
44
qCWarning
(
qLcDmaBufSupport
) <<
"DMA-BUF range lies outside its buffer"
;
45
return
std
::
nullopt
;
46
}
47
48
if
(
size
== 0)
49
return
QByteArray
{};
50
51
// The mapping must start on a page boundary, so map from the page holding offset.
52
const
int
sysconfPageSize
=
sysconf
(
_SC_PAGESIZE
);
53
if
(
Q_UNLIKELY
(
sysconfPageSize
< 0)) {
54
qCWarning
(
qLcDmaBufSupport
) <<
"cannot determine system page size:"
<<
qt_error_string
(
errno
);
55
return
std
::
nullopt
;
56
}
57
const
size_t
pageSize
=
size_t
(
sysconfPageSize
);
58
const
off_t
mapOffset
=
alignDown
(
offset
,
pageSize
);
59
const
size_t
pageOffset
=
size_t
(
offset
-
mapOffset
);
60
61
void
*
mapping
=
mmap
(
nullptr
,
pageOffset
+
size
,
PROT_READ
,
MAP_SHARED
,
fd
,
mapOffset
);
62
if
(
mapping
==
MAP_FAILED
) {
63
qCWarning
(
qLcDmaBufSupport
) <<
"mapping DMA-BUF failed:"
<<
qt_error_string
(
errno
);
64
return
std
::
nullopt
;
65
}
66
auto
unmap
=
qScopeGuard
([&] {
67
munmap
(
mapping
,
pageOffset
+
size
);
68
});
69
70
dmaBufSyncRead
(
fd
,
DmaBufCpuAccess
::
Begin
);
71
auto
syncEnd
=
qScopeGuard
([&] {
72
dmaBufSyncRead
(
fd
,
DmaBufCpuAccess
::
End
);
73
});
74
75
return
QByteArray
(
static_cast
<
const
char
*>(
mapping
) +
pageOffset
,
qsizetype
(
size
));
76
}
77
78
std
::
optional
<
QByteArray
>
copyDmaBufData
(
int
fd
,
off_t
offset
,
size_t
size
)
79
{
80
const
off_t
bufferSize
=
lseek
(
fd
, 0,
SEEK_END
);
81
if
(
bufferSize
< 0) {
82
qCWarning
(
qLcDmaBufSupport
) <<
"cannot determine DMA-BUF size:"
<<
qt_error_string
(
errno
);
83
return
std
::
nullopt
;
84
}
85
86
return
copyDmaBufData
(
fd
,
offset
,
size
,
size_t
(
bufferSize
));
87
}
88
89
std
::
optional
<
QByteArray
>
copyDmaBufData
(
const
QDmaBuf
&
buf
,
off_t
offset
,
size_t
size
)
90
{
91
return
copyDmaBufData
(
buf
.
fd
(),
offset
,
size
,
buf
.
size
());
92
}
93
94
#
endif
// QT_CONFIG(linux_dmabuf)
95
96
}
// namespace QtMultimediaPrivate
97
98
QT_END_NAMESPACE
QtMultimediaPrivate::impl
Definition
qaudiosystem_p.h:257
qtmultimedia
src
multimedia
video
qmultimedia_dmabuf_support.cpp
Generated on
for Qt by
1.18.0