From 09e8d69b7a5c6f394b6a43f6c485d26f74189541 Mon Sep 17 00:00:00 2001
From: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
Date: Wed, 27 Jan 2016 11:53:25 +0100
Subject: [PATCH] eglfs: Sanitize the widget compositor's context handling

createPlatformOpenGLContext() used to silently set the widget
compositor's context as the context to share resources with.

This works mostly, but is the wrong level to enforce the resource
sharing.  For example, QOpenGLContext::shareGroup() becomes
inconsistent since from QOpenGLContext's view there was no
shareContext specified.

The inability to test via shareGroup() is the reason eglfs started to
show warnings when exiting applications. The resource sharing was in
place on EGL level but QOpenGLContext knew nothing about it.

Therefore, let's switch over to the way other components, f.ex. Web
Engine use: set the internal global share context pointer to the
widget compositor's context. This way everything remains consistent:
the widget compositor's context is stored upon creating the main
QEGLFSWindow, QWidget::shareContext() picks this up then, and as a
result we have sharing set up on QOpenGLContext's level instead of
sneaking it in in the QPlatformOpenGLContext implementation.

Task-number: QTBUG-50707
Change-Id: I5fc1dec58c69c46aa83c7b4cab1eadce6fa633ce
Reviewed-by: Paul Olav Tvete <paul.tvete@theqtcompany.com>
---
 .../platformcompositor/qopenglcompositor.cpp              | 2 +-
 .../platformcompositor/qopenglcompositorbackingstore.cpp  | 8 ++++++++
 src/plugins/platforms/eglfs/qeglfsintegration.cpp         | 5 +----
 src/plugins/platforms/eglfs/qeglfswindow.cpp              | 8 ++++++++
 4 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/platformsupport/platformcompositor/qopenglcompositor.cpp b/src/platformsupport/platformcompositor/qopenglcompositor.cpp
index 2e386532e28..7e0973040ac 100644
--- a/src/platformsupport/platformcompositor/qopenglcompositor.cpp
+++ b/src/platformsupport/platformcompositor/qopenglcompositor.cpp
@@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE
 
     It is up to the platform plugin to manage the lifetime of the
     compositor (instance(), destroy()), set the correct destination
-    context and window as early as possible (setTargetWindow()),
+    context and window as early as possible (setTarget()),
     register the composited windows as they are shown, activated,
     raised and lowered (addWindow(), moveToTop(), etc.), and to
     schedule repaints (update()).
diff --git a/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp b/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
index 07a1e77c3a9..83b551557f1 100644
--- a/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
+++ b/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
@@ -184,6 +184,8 @@ void QOpenGLCompositorBackingStore::flush(QWindow *window, const QRegion &region
 
     QOpenGLCompositor *compositor = QOpenGLCompositor::instance();
     QOpenGLContext *dstCtx = compositor->context();
+    Q_ASSERT(dstCtx);
+
     QWindow *dstWin = compositor->targetWindow();
     if (!dstWin)
         return;
@@ -209,6 +211,12 @@ void QOpenGLCompositorBackingStore::composeAndFlush(QWindow *window, const QRegi
 
     QOpenGLCompositor *compositor = QOpenGLCompositor::instance();
     QOpenGLContext *dstCtx = compositor->context();
+    Q_ASSERT(dstCtx); // setTarget() must have been called before, e.g. from QEGLFSWindow
+
+    // The compositor's context and the context to which QOpenGLWidget/QQuickWidget
+    // textures belong are not the same. They share resources, though.
+    Q_ASSERT(context->shareGroup() == dstCtx->shareGroup());
+
     QWindow *dstWin = compositor->targetWindow();
     if (!dstWin)
         return;
diff --git a/src/plugins/platforms/eglfs/qeglfsintegration.cpp b/src/plugins/platforms/eglfs/qeglfsintegration.cpp
index 2086ce56e23..35b27cba0bb 100644
--- a/src/plugins/platforms/eglfs/qeglfsintegration.cpp
+++ b/src/plugins/platforms/eglfs/qeglfsintegration.cpp
@@ -188,11 +188,8 @@ QPlatformWindow *QEglFSIntegration::createPlatformWindow(QWindow *window) const
 
 QPlatformOpenGLContext *QEglFSIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
 {
-    // If there is a "root" window into which raster and QOpenGLWidget content is
-    // composited, all other contexts must share with its context.
-    QOpenGLContext *compositingContext = QOpenGLCompositor::instance()->context();
     EGLDisplay dpy = context->screen() ? static_cast<QEglFSScreen *>(context->screen()->handle())->display() : display();
-    QPlatformOpenGLContext *share = compositingContext ? compositingContext->handle() : context->shareHandle();
+    QPlatformOpenGLContext *share = context->shareHandle();
     QVariant nativeHandle = context->nativeHandle();
 
     QEglFSContext *ctx;
diff --git a/src/plugins/platforms/eglfs/qeglfswindow.cpp b/src/plugins/platforms/eglfs/qeglfswindow.cpp
index 8301be8c17f..84856831c39 100644
--- a/src/plugins/platforms/eglfs/qeglfswindow.cpp
+++ b/src/plugins/platforms/eglfs/qeglfswindow.cpp
@@ -138,6 +138,14 @@ void QEglFSWindow::create()
         if (!context->create())
             qFatal("EGLFS: Failed to create compositing context");
         compositor->setTarget(context, window());
+        // If there is a "root" window into which raster and QOpenGLWidget content is
+        // composited, all other contexts must share with its context.
+        if (!qt_gl_global_share_context()) {
+            qt_gl_set_global_share_context(context);
+            // What we set up here is in effect equivalent to the application setting
+            // AA_ShareOpenGLContexts. Set the attribute to be fully consistent.
+            QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
+        }
     }
 }
 
-- 
GitLab