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
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
4
#
include
"qdmabufvideobuffer_p.h"
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
25
static_assert
(QT_CONFIG(linux_dmabuf));
26
27
QT_BEGIN_NAMESPACE
28
29
Q_STATIC_LOGGING_CATEGORY
(qLcDmaBufVideoBuffer,
"qt.multimedia.dmabufvideobuffer"
);
30
31
namespace
QtMultimediaPrivate
{
32
33
#
if
QT_CONFIG
(
linux_dmabuf_egl
)
34
static
QRhiValueMapper
<
DmaBufEglContext
>
g_eglContexts
;
35
#
endif
36
37
namespace
{
38
39
q23::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
47
q23::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
59
QDmaBufVideoBuffer
::
QDmaBufVideoBuffer
(
const
QVideoFrameFormat
&
format
,
60
QSpan
<
const
DmaBufPlane
>
planes
,
61
std
::
shared_ptr
<
void
>
keepAlive
,
DmaBufImportMode
importMode
)
62
:
QDmaBufVideoBuffer
{
63
format
.
pixelFormat
(),
format
.
frameSize
(),
planes
,
std
::
move
(
keepAlive
),
importMode
,
64
}
65
{
66
}
67
68
QDmaBufVideoBuffer
::
QDmaBufVideoBuffer
(
QVideoFrameFormat
::
PixelFormat
format
,
QSize
size
,
69
QSpan
<
const
DmaBufPlane
>
planes
,
70
std
::
shared_ptr
<
void
>
keepAlive
,
DmaBufImportMode
importMode
)
71
:
m_format
(
format
),
m_size
(
size
),
m_keepAlive
(
std
::
move
(
keepAlive
)),
m_importMode
(
importMode
)
72
{
73
Q_ASSERT
(!
planes
.
empty
() &&
planes
.
size
() <= 4);
74
75
for
(
qsizetype
i
= 0;
i
<
planes
.
size
(); ++
i
) {
76
q23
::
expected
<
DmaBufPlane
,
std
::
error_code
>
plane
=
planeWithDupedFd
(
planes
[
i
]);
77
if
(!
plane
) {
78
qCWarning
(
qLcDmaBufVideoBuffer
)
79
<<
"Failed to dup DMABUF fd for plane"
<<
i
<<
':'
<<
plane
.
error
().
message
();
80
break
;
81
}
82
83
m_planes
.
push_back
(
std
::
move
(*
plane
));
84
}
85
}
86
87
QDmaBufVideoBuffer
::~
QDmaBufVideoBuffer
()
88
{
89
Q_ASSERT
(
m_mapMode
==
QVideoFrame
::
NotMapped
);
90
91
for
(
DmaBufPlane
&
plane
:
m_planes
) {
92
if
(
plane
.
fd
>= 0)
93
qt_safe_close
(
plane
.
fd
);
94
}
95
}
96
97
QVideoFrameTexturesUPtr
QDmaBufVideoBuffer
::
mapTextures
(
QRhi
&
rhi
)
98
{
99
Q_ASSERT
(
rhi
.
thread
()->
isCurrentThread
());
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.
109
DmaBufEglContext
&
eglContext
=
g_eglContexts
.
getOrCreate
(
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
118
auto
handles
=
importDmaBufTextures
(
rhi
,
eglContext
,
m_planes
,
m_format
,
m_size
,
m_keepAlive
,
119
m_importMode
);
120
if
(!
handles
) {
121
if
(
handles
.
error
() ==
FailureSeverity
::
unrecoverable
) {
122
qCWarning
(
qLcDmaBufVideoBuffer
)
123
<<
"Unrecoverable EGL import failure, disabling texture import for this buffer"
;
124
eglContext
.
invalidate
();
125
}
126
return
{};
127
}
128
129
return
QVideoTextureHelper
::
createTexturesFromHandles
(
std
::
move
(*
handles
),
rhi
,
m_format
,
130
m_size
);
131
#
else
// QT_CONFIG(linux_dmabuf_egl)
132
qCDebug
(
qLcDmaBufVideoBuffer
)
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
138
void
QDmaBufVideoBuffer
::
unmapRegions
(
QSpan
<
const
MappedRegion
>
regions
)
139
{
140
for
(
const
MappedRegion
&
region
:
regions
) {
141
dmaBufSyncRead
(
region
.
fd
.
get
(),
DmaBufCpuAccess
::
End
);
142
::
munmap
(
region
.
data
.
data
(),
region
.
data
.
size
());
143
}
144
}
145
146
QAbstractVideoBuffer
::
MapData
QDmaBufVideoBuffer
::
map
(
QVideoFrame
::
MapMode
mode
)
147
{
148
MapData
mapData
;
149
150
// The producer (driver/compositor) owns write access to the DMABUF.
151
if
(
mode
!=
QVideoFrame
::
ReadOnly
||
m_mapMode
!=
QVideoFrame
::
NotMapped
)
152
return
mapData
;
153
154
auto
*
desc
=
QVideoTextureHelper
::
textureDescription
(
m_format
);
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
) {
163
qCWarning
(
qLcDmaBufVideoBuffer
)
164
<<
"Failed to dup DMABUF fd for plane"
<<
i
<<
':'
<<
fd
.
error
().
message
();
165
unmapRegions
(
m_mappedRegions
);
166
m_mappedRegions
.
clear
();
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
) {
174
qCWarning
(
qLcDmaBufVideoBuffer
)
175
<<
"Could not map DMABUF for plane"
<<
i
<<
':'
<<
qt_error_string
(errno);
176
unmapRegions
(
m_mappedRegions
);
177
m_mappedRegions
.
clear
();
178
return
{};
179
}
180
181
dmaBufSyncRead
(
fd
->
get
(),
DmaBufCpuAccess
::
Begin
);
182
const
QSpan
<
std
::
byte
>
data
{
183
static_cast
<
std
::
byte
*>(
mapped
),
184
qsizetype
(
len
),
185
};
186
m_mappedRegions
.
push_back
({
187
std
::
move
(*
fd
),
188
data
,
189
});
190
191
mapData
.
data
[
i
] =
reinterpret_cast
<
uchar
*>(
data
.
data
()) +
plane
.
offset
;
192
mapData
.
bytesPerLine
[
i
] =
int
(
plane
.
pitch
);
193
mapData
.
dataSize
[
i
] =
int
(
plane
.
pitch
) *
desc
->
heightForPlane
(
m_size
.
height
(),
i
);
194
}
195
196
mapData
.
planeCount
=
m_planes
.
size
();
197
198
m_mapMode
=
mode
;
199
return
mapData
;
200
}
201
202
void
QDmaBufVideoBuffer
::
unmap
()
203
{
204
unmapRegions
(
m_mappedRegions
);
205
m_mappedRegions
.
clear
();
206
207
m_mapMode
=
QVideoFrame
::
NotMapped
;
208
}
209
210
}
// namespace QtMultimediaPrivate
211
212
QT_END_NAMESPACE
QtMultimediaPrivate::impl
Definition
qaudiosystem_p.h:257
Q_STATIC_LOGGING_CATEGORY
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
MAP_FAILED
#define MAP_FAILED
Definition
qresource.cpp:1189
qtmultimedia
src
multimedia
video
qdmabufvideobuffer.cpp
Generated on
for Qt by
1.18.0