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
qopenglvideobuffer.cpp
Go to the documentation of this file.
1
// Copyright (C) 2024 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
"qopenglvideobuffer_p.h"
5
6
#
include
<
qoffscreensurface
.
h
>
7
#
include
<
qthread
.
h
>
8
#
include
<
private
/
qimagevideobuffer_p
.
h
>
9
10
#
include
<
QtOpenGL
/
private
/
qopenglcompositor_p
.
h
>
11
#
include
<
QtOpenGL
/
private
/
qopenglframebufferobject_p
.
h
>
12
13
QT_BEGIN_NAMESPACE
14
15
static
QOpenGLContext
*
createContext
(QOpenGLContext *shareContext)
16
{
17
// Create an OpenGL context for the current thread. The lifetime of the context is tied to the
18
// lifetime of the current thread.
19
auto
context = std::make_unique<QOpenGLContext>();
20
context->setShareContext(shareContext);
21
22
if
(!context->create()) {
23
qWarning() <<
"Couldn't create an OpenGL context for QOpenGLVideoBuffer"
;
24
return
nullptr
;
25
}
26
27
QObject::connect(QThread::currentThread(), &QThread::finished,
28
context.get(), &QOpenGLContext::deleteLater);
29
return
context.release();
30
}
31
32
static
bool
setCurrentOpenGLContext
()
33
{
34
auto
compositorContext = QOpenGLCompositor::instance()->context();
35
36
// A thread-local variable is used to avoid creating a new context if we're called on the same
37
// thread. The context lifetime is tied to the current thread lifetime (see createContext()).
38
static
thread_local
QOpenGLContext *context =
nullptr
;
39
static
thread_local
QOffscreenSurface *surface =
nullptr
;
40
41
if
(!context) {
42
context = compositorContext->thread()->isCurrentThread() ? compositorContext
43
: createContext(compositorContext);
44
45
if
(!context)
46
return
false
;
47
48
surface =
new
QOffscreenSurface(
nullptr
, context);
49
surface->setFormat(context->format());
50
surface->create();
51
}
52
53
return
context->makeCurrent(surface);
54
}
55
56
QOpenGLVideoBuffer
::
QOpenGLVideoBuffer
(std::unique_ptr<QOpenGLFramebufferObject> fbo)
57
:
m_fbo
(
std
::
move
(
fbo
))
58
{
59
Q_ASSERT(m_fbo);
60
}
61
62
QOpenGLVideoBuffer
::~
QOpenGLVideoBuffer
() =
default
;
63
64
QAbstractVideoBuffer
::
MapData
QOpenGLVideoBuffer
::
map
(QVideoFrame::MapMode mode)
65
{
66
return
ensureImageBuffer
(
)
.map(mode);
67
}
68
69
void
QOpenGLVideoBuffer
::
unmap
()
70
{
71
if
(m_imageBuffer)
72
m_imageBuffer->unmap();
73
}
74
75
quint64
QOpenGLVideoBuffer
::
textureHandle
(QRhi &,
int
plane)
76
{
77
Q_UNUSED(plane);
78
return
m_fbo->texture();
79
}
80
81
QImageVideoBuffer &
QOpenGLVideoBuffer
::
ensureImageBuffer
()
82
{
83
// Create image buffer if not yet created.
84
// This is protected by mapMutex in QVideoFrame::map.
85
if
(!m_imageBuffer) {
86
if
(!
setCurrentOpenGLContext
(
)
)
87
qWarning() <<
"Failed to set current OpenGL context"
;
88
89
m_imageBuffer = std::make_unique<QImageVideoBuffer>(m_fbo->toImage(
false
));
90
}
91
92
return
*m_imageBuffer;
93
}
94
95
QT_END_NAMESPACE
QOpenGLVideoBuffer
Definition
qopenglvideobuffer_p.h:26
QOpenGLVideoBuffer::ensureImageBuffer
QImageVideoBuffer & ensureImageBuffer()
Definition
qopenglvideobuffer.cpp:81
QOpenGLVideoBuffer::unmap
void unmap() override
Releases the memory mapped by the map() function.
Definition
qopenglvideobuffer.cpp:69
QOpenGLVideoBuffer::QOpenGLVideoBuffer
QOpenGLVideoBuffer(std::unique_ptr< QOpenGLFramebufferObject > fbo)
Definition
qopenglvideobuffer.cpp:56
QOpenGLVideoBuffer::textureHandle
quint64 textureHandle(QRhi &, int plane) override
Definition
qopenglvideobuffer.cpp:75
QOpenGLVideoBuffer::~QOpenGLVideoBuffer
~QOpenGLVideoBuffer() override
QOpenGLVideoBuffer::map
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
Definition
qopenglvideobuffer.cpp:64
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
createContext
static QT_BEGIN_NAMESPACE QOpenGLContext * createContext(QOpenGLContext *shareContext)
Definition
qopenglvideobuffer.cpp:15
setCurrentOpenGLContext
static bool setCurrentOpenGLContext()
Definition
qopenglvideobuffer.cpp:32
qtmultimedia
src
plugins
multimedia
ffmpeg
qopenglvideobuffer.cpp
Generated on
for Qt by
1.18.0