Make QOpenGLContext::globalShareContext() lazy creation thread-safe
We move the context to the main thread (or the thread of qGuiApp), as that's where it will be deleted. Amends 4a7ccb65f0065e878c5762db05eca9c5cd6731e5. Caught-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> Change-Id: I8e5dc512dce02d22980730a46474b2262684a713 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
parent
11f149d987
commit
e9dbdaa499
@ -968,11 +968,16 @@ bool QOpenGLContext::supportsThreadedOpenGL()
|
||||
QOpenGLContext *QOpenGLContext::globalShareContext()
|
||||
{
|
||||
Q_ASSERT(qGuiApp);
|
||||
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
|
||||
// Lazily create a global share context when enabled unless there is already one
|
||||
if (!qt_gl_global_share_context() && qGuiApp->testAttribute(Qt::AA_ShareOpenGLContexts)) {
|
||||
QOpenGLContext *ctx = new QOpenGLContext;
|
||||
ctx->setFormat(QSurfaceFormat::defaultFormat());
|
||||
ctx->create();
|
||||
ctx->moveToThread(qGuiApp->thread());
|
||||
qt_gl_set_global_share_context(ctx);
|
||||
QGuiApplicationPrivate::instance()->ownGlobalShareContext = true;
|
||||
}
|
||||
|
@ -1292,6 +1292,24 @@ void tst_QGuiApplication::globalShareContext()
|
||||
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
|
||||
ctx = QOpenGLContext::globalShareContext();
|
||||
QVERIFY(ctx);
|
||||
|
||||
// Test that the global share context is lazily created,
|
||||
// even from secondary threads.
|
||||
app.reset();
|
||||
app.reset(new QGuiApplication(argc, argv));
|
||||
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
|
||||
class Thread : public QThread
|
||||
{
|
||||
void run() override
|
||||
{
|
||||
auto *ctx = QOpenGLContext::globalShareContext();
|
||||
QVERIFY(ctx);
|
||||
QCOMPARE(ctx->thread(), qGuiApp->thread());
|
||||
}
|
||||
};
|
||||
Thread thread;
|
||||
thread.start();
|
||||
thread.wait();
|
||||
#else
|
||||
QSKIP("No OpenGL support");
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user