Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
14
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
29 return context.release();
30}
31
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() == QThread::currentThread())
43 ? compositorContext
44 : createContext(compositorContext);
45
46 if (!context)
47 return false;
48
49 surface = new QOffscreenSurface(nullptr, context);
50 surface->setFormat(context->format());
51 surface->create();
52 }
53
54 return context->makeCurrent(surface);
55}
56
57QOpenGLVideoBuffer::QOpenGLVideoBuffer(std::unique_ptr<QOpenGLFramebufferObject> fbo)
58 : QAbstractVideoBuffer(QVideoFrame::RhiTextureHandle), m_fbo(std::move(fbo))
59{
60 Q_ASSERT(m_fbo);
61}
62
64
66{
67 return m_imageBuffer ? m_imageBuffer->mapMode() : QVideoFrame::NotMapped;
68}
69
74
76{
77 if (m_imageBuffer)
78 m_imageBuffer->unmap();
79}
80
82{
83 Q_UNUSED(plane);
84 return m_fbo->texture();
85}
86
88{
89 // Create image buffer if not yet created.
90 // This is protected by mapMutex in QVideoFrame::map.
91 if (!m_imageBuffer) {
93 qWarning() << "Failed to set current OpenGL context";
94
95 m_imageBuffer = std::make_unique<QImageVideoBuffer>(m_fbo->toImage(false));
96 }
97
98 return *m_imageBuffer;
99}
100
The QAbstractVideoBuffer class is an abstraction for video data. \inmodule QtMultimedia.
MapData map(QVideoFrame::MapMode mode) override
Independently maps the planes of a video buffer to memory.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtGui
void create()
Allocates the platform resources associated with the offscreen surface.
void setFormat(const QSurfaceFormat &format)
Sets the offscreen surface format.
static QOpenGLCompositor * instance()
\inmodule QtGui
quint64 textureHandle(QRhi *, int plane) const override
Returns a texture handle to the data buffer.
QImageVideoBuffer & ensureImageBuffer()
void unmap() override
Releases the memory mapped by the map() function.
QOpenGLVideoBuffer(std::unique_ptr< QOpenGLFramebufferObject > fbo)
QVideoFrame::MapMode mapMode() const override
MapData map(QVideoFrame::MapMode mode) override
Independently maps the planes of a video buffer to memory.
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
static QThread * currentThread()
Definition qthread.cpp:1039
void finished(QPrivateSignal)
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:27
MapMode
Enumerates how a video buffer's data is mapped to system memory.
Definition qvideoframe.h:37
Combined button and popup list for selecting options.
static void * context
#define qWarning
Definition qlogging.h:166
GLenum mode
static QT_BEGIN_NAMESPACE QOpenGLContext * createContext(QOpenGLContext *shareContext)
static bool setCurrentOpenGLContext()
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
unsigned long long quint64
Definition qtypes.h:61