diff --git a/src/platformsupport/platformcompositor/qopenglcompositor.cpp b/src/platformsupport/platformcompositor/qopenglcompositor.cpp
index 2e386532e28ce7768630041d962ff23939bec64e..7e0973040ac1fb4f173137f98148a560abf36f52 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 07a1e77c3a9a1193d746563547e8029374089add..83b551557f1bd84d262f52989674afb5fa8ded5f 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 2086ce56e232298e5221bad731d6a91d959ee424..35b27cba0bb771b34d9b780925f64b87292d8f3b 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 8301be8c17f868b52fd5bec88b8e9f301fe410ad..84856831c3938f2e5eb38d2a62d0c0a885861199 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);
+        }
     }
 }