rhi: gl: Allow passing in a custom shareContext

We already honor AA_ShareOpenGLContexts and pass in the QGuiApp's
context as the shareContext for the QRhi's QOpenGLContext. Extend this
to also allow specifying a QOpenGLContext in the init params struct.

A good example of this is the backingstore compositor that serves
QQuickWidget and co. If one wanted to implement that with (an
OpenGL-based) QRhi, instead of direct OpenGL calls, then the ability
to create a QRhi that uses a context that shares resources with a
given other context becomes essential.

Pick-to: 6.2
Change-Id: I6bc5ff8e803d467f8795197ac1f12fdc0f73bbd1
Reviewed-by: Christian Strømme <christian.stromme@qt.io>
This commit is contained in:
Laszlo Agocs 2021-08-24 15:09:57 +02:00
parent 7729989648
commit afa0ce9045
3 changed files with 14 additions and 3 deletions

View File

@ -107,8 +107,9 @@ QT_BEGIN_NAMESPACE
of the returned QOffscreenSurface is transferred to the caller and the QRhi
will not destroy it.
\note QRhiSwapChain can only target QWindow instances that have their
surface type set to QSurface::OpenGLSurface.
\note With the OpenGL backend, QRhiSwapChain can only target QWindow
instances that have their surface type set to QSurface::OpenGLSurface or
QSurface::RasterGLSurface.
\note \l window is optional. It is recommended to specify it whenever
possible, in order to avoid problems on multi-adapter and multi-screen
@ -117,6 +118,10 @@ QT_BEGIN_NAMESPACE
an invisible window on some platforms (for example, Windows) and that may
trigger unexpected problems in some cases.
In case resource sharing with an existing QOpenGLContext is desired, \l
shareContext can be set to an existing QOpenGLContext. Alternatively,
Qt::AA_ShareOpenGLContexts is honored as well, when enabled.
\section2 Working with existing OpenGL contexts
When interoperating with another graphics engine, it may be necessary to
@ -424,6 +429,7 @@ QRhiGles2::QRhiGles2(QRhiGles2InitParams *params, QRhiGles2NativeHandles *import
requestedFormat = QRhiGles2InitParams::adjustedFormat(params->format);
fallbackSurface = params->fallbackSurface;
maybeWindow = params->window; // may be null
maybeShareContext = params->shareContext; // may be null
importedContext = importDevice != nullptr;
if (importedContext) {
@ -472,7 +478,10 @@ bool QRhiGles2::create(QRhi::Flags flags)
if (!importedContext) {
ctx = new QOpenGLContext;
ctx->setFormat(requestedFormat);
if (QOpenGLContext *shareContext = qt_gl_global_share_context()) {
if (maybeShareContext) {
ctx->setShareContext(maybeShareContext);
ctx->setScreen(maybeShareContext->screen());
} else if (QOpenGLContext *shareContext = qt_gl_global_share_context()) {
ctx->setShareContext(shareContext);
ctx->setScreen(shareContext->screen());
}

View File

@ -67,6 +67,7 @@ struct Q_GUI_EXPORT QRhiGles2InitParams : public QRhiInitParams
QSurfaceFormat format;
QOffscreenSurface *fallbackSurface = nullptr;
QWindow *window = nullptr;
QOpenGLContext *shareContext = nullptr;
static QOffscreenSurface *newFallbackSurface(const QSurfaceFormat &format = QSurfaceFormat::defaultFormat());
static QSurfaceFormat adjustedFormat(const QSurfaceFormat &format = QSurfaceFormat::defaultFormat());

View File

@ -897,6 +897,7 @@ public:
QSurfaceFormat requestedFormat;
QSurface *fallbackSurface = nullptr;
QWindow *maybeWindow = nullptr;
QOpenGLContext *maybeShareContext = nullptr;
mutable bool needsMakeCurrent = false;
QOpenGLExtensions *f = nullptr;
uint vao = 0;