From 3d6d6594b0dd2a8860b8fddd5a58f86c4ebd255c Mon Sep 17 00:00:00 2001
From: Simon Hausmann <simon.hausmann@digia.com>
Date: Wed, 26 Mar 2014 12:21:25 +0100
Subject: [PATCH] Centralize OpenGL initialization

We now require the user to use QWebEngine::initialize() in main (preferably) and
print out an error message if this wasn't set up accordingly. This limits the use
of private scene graph API to inside QWebEngine and offers public API for users
of the API.

Change-Id: I787c176a85ab7784dbc8787d9876960b4872959e
Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
---
 examples/webengine/quicknanobrowser/main.cpp  |  9 +--
 .../quicknanobrowser/quicknanobrowser.pro     |  3 +-
 src/core/content_browser_client_qt.cpp        |  4 +-
 src/webengine/api/qtwebengineglobal.cpp       | 75 +++++++++++++++++++
 src/webengine/api/qtwebengineglobal.h         |  6 ++
 src/webengine/webengine.pro                   |  1 +
 tests/auto/quick/qmltests/qmltests.pro        |  1 -
 .../tst_qquickwebviewgraphics.cpp             | 10 +--
 .../quick/shared/qt_webengine_quicktest.h     |  7 +-
 tests/auto/quick/tests.pri                    |  3 +-
 tests/quicktestbrowser/main.cpp               |  9 +--
 tests/quicktestbrowser/quicktestbrowser.pro   |  3 +-
 12 files changed, 97 insertions(+), 34 deletions(-)
 create mode 100644 src/webengine/api/qtwebengineglobal.cpp

diff --git a/examples/webengine/quicknanobrowser/main.cpp b/examples/webengine/quicknanobrowser/main.cpp
index 6c2f91de6..1e592f3e1 100644
--- a/examples/webengine/quicknanobrowser/main.cpp
+++ b/examples/webengine/quicknanobrowser/main.cpp
@@ -47,18 +47,13 @@ typedef QApplication Application;
 #include <QtGui/QGuiApplication>
 typedef QGuiApplication Application;
 #endif
-#include <QtQuick/private/qsgcontext_p.h>
+#include <qtwebengineglobal.h>
 
 int main(int argc, char **argv)
 {
     Application app(argc, argv);
 
-    // This is currently needed by all QtWebEngine application using the HW accelerated QQuickWebView.
-    // It enables sharing between the QOpenGLContext of all QQuickWindows of the application.
-    // We have to do so until we expose a public API for it, or chose enable it by default in Qt 5.3.0.
-    QOpenGLContext shareContext;
-    shareContext.create();
-    QSGContext::setSharedOpenGLContext(&shareContext);
+    QWebEngine::initialize();
 
     ApplicationEngine appEngine;
 
diff --git a/examples/webengine/quicknanobrowser/quicknanobrowser.pro b/examples/webengine/quicknanobrowser/quicknanobrowser.pro
index f1b665932..3628b817e 100644
--- a/examples/webengine/quicknanobrowser/quicknanobrowser.pro
+++ b/examples/webengine/quicknanobrowser/quicknanobrowser.pro
@@ -10,8 +10,7 @@ OTHER_FILES += quickwindow.qml
 
 RESOURCES += resources.qrc
 
-QT += qml quick
-QT_PRIVATE += quick-private gui-private core-private
+QT += qml quick webengine
 
 qtHaveModule(widgets) {
     QT += widgets # QApplication is required to get native styling with QtQuickControls
diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp
index 99862235f..628aa6ed2 100644
--- a/src/core/content_browser_client_qt.cpp
+++ b/src/core/content_browser_client_qt.cpp
@@ -249,7 +249,9 @@ void ShareGroupQtQuick::AboutToAddFirstContext()
 {
     // This currently has to be setup by ::main in all applications using QQuickWebEngineView with delegated rendering.
     QOpenGLContext *shareContext = QSGContext::sharedOpenGLContext();
-    Q_ASSERT(shareContext);
+    if (!shareContext) {
+        qFatal("QWebEngine: OpenGL resource sharing is not set up in QtQuick. Please make sure to call QWebEngine::initialize() in your main() function.");
+    }
     m_shareContextQtQuick = make_scoped_refptr(new QtShareGLContext(shareContext));
 }
 
diff --git a/src/webengine/api/qtwebengineglobal.cpp b/src/webengine/api/qtwebengineglobal.cpp
new file mode 100644
index 000000000..4aa72ba6f
--- /dev/null
+++ b/src/webengine/api/qtwebengineglobal.cpp
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebEngine module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qtwebengineglobal.h"
+
+#include <private/qsgcontext_p.h>
+#include <QGuiApplication>
+
+static QOpenGLContext *shareContext;
+
+static void deleteShareContext()
+{
+    delete shareContext;
+    shareContext = 0;
+}
+
+void QWebEngine::initialize()
+{
+    QCoreApplication *app = QCoreApplication::instance();
+    if (!app) {
+        qFatal("QWebEngine::initialize() must be called after the construction of the application object.");
+        return;
+    }
+    if (app->thread() != QThread::currentThread()) {
+        qFatal("QWebEngine::initialize() must be called from the Qt gui thread.");
+        return;
+    }
+
+    if (shareContext)
+        return;
+
+    shareContext = new QOpenGLContext;
+    shareContext->create();
+    qAddPostRoutine(deleteShareContext);
+    QSGContext::setSharedOpenGLContext(shareContext);
+}
+
diff --git a/src/webengine/api/qtwebengineglobal.h b/src/webengine/api/qtwebengineglobal.h
index 46f77c55f..f5fa479d9 100644
--- a/src/webengine/api/qtwebengineglobal.h
+++ b/src/webengine/api/qtwebengineglobal.h
@@ -55,6 +55,12 @@ QT_BEGIN_NAMESPACE
 #  define Q_WEBENGINE_EXPORT
 #endif
 
+class Q_WEBENGINE_EXPORT QWebEngine
+{
+public:
+    static void initialize();
+};
+
 QT_END_NAMESPACE
 
 #endif // QTWEBENGINEGLOBAL_H
diff --git a/src/webengine/webengine.pro b/src/webengine/webengine.pro
index 42602bbc6..ad1a31b54 100644
--- a/src/webengine/webengine.pro
+++ b/src/webengine/webengine.pro
@@ -15,6 +15,7 @@ SOURCES = \
         api/qquickwebengineloadrequest.cpp \
         api/qquickwebenginenewviewrequest.cpp \
         api/qquickwebengineview.cpp \
+        api/qtwebengineglobal.cpp \
         render_widget_host_view_qt_delegate_quick.cpp \
         ui_delegates_manager.cpp
 
diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro
index 5883574d3..98d836a10 100644
--- a/tests/auto/quick/qmltests/qmltests.pro
+++ b/tests/auto/quick/qmltests/qmltests.pro
@@ -1,7 +1,6 @@
 include(../tests.pri)
 
 QT += qmltest
-QT_PRIVATE += quick-private
 
 IMPORTPATH += $$PWD/data
 
diff --git a/tests/auto/quick/qquickwebviewgraphics/tst_qquickwebviewgraphics.cpp b/tests/auto/quick/qquickwebviewgraphics/tst_qquickwebviewgraphics.cpp
index 4411107c4..213420b13 100644
--- a/tests/auto/quick/qquickwebviewgraphics/tst_qquickwebviewgraphics.cpp
+++ b/tests/auto/quick/qquickwebviewgraphics/tst_qquickwebviewgraphics.cpp
@@ -43,7 +43,8 @@
 #include <QQmlContext>
 #include <QQuickView>
 #include <QQuickItem>
-#include <QtQuick/private/qsgcontext_p.h>
+#include <QPainter>
+#include <qtwebengineglobal.h>
 
 class TestView : public QQuickView {
     Q_OBJECT
@@ -111,12 +112,7 @@ void tst_QQuickWebViewGraphics::initTestCase()
 #if defined(TST_QQUICKWEBVIEWGRAPHICS_SOFTWARE)
     qApp->setProperty("QQuickWebEngineView_DisableHardwareAcceleration", QVariant(true));
 #else
-    // This is currently needed by all QtWebEngine application using the HW accelerated QQuickWebView.
-    // It enables sharing between the QOpenGLContext of all QQuickWindows of the application.
-    // We have to do so until we expose a public API for it, or chose enable it by default in Qt 5.3.0.
-    QOpenGLContext *shareContext = new QOpenGLContext;
-    shareContext->create();
-    QSGContext::setSharedOpenGLContext(shareContext);
+    QWebEngine::initialize();
 #endif
 }
 
diff --git a/tests/auto/quick/shared/qt_webengine_quicktest.h b/tests/auto/quick/shared/qt_webengine_quicktest.h
index 92e9889ef..275e40dd2 100644
--- a/tests/auto/quick/shared/qt_webengine_quicktest.h
+++ b/tests/auto/quick/shared/qt_webengine_quicktest.h
@@ -51,7 +51,7 @@
 #endif
 
 #include "qopenglcontext.h"
-#include <QtQuick/private/qsgcontext_p.h>
+#include <qtwebengineglobal.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -72,11 +72,8 @@ QT_BEGIN_NAMESPACE
         qputenv("QTWEBENGINEPROCESS_PATH", QWP_PATH); \
         if (!QCoreApplication::instance()) \
             app = new Application(argc, argv); \
-        QOpenGLContext *shareContext = new QOpenGLContext(); \
-        shareContext->create(); \
-        QSGContext::setSharedOpenGLContext(shareContext); \
+        QWebEngine::initialize(); \
         int i = quick_test_main(argc, argv, #name, QUICK_TEST_SOURCE_DIR); \
-        delete shareContext; \
         delete app; \
         return i; \
     }
diff --git a/tests/auto/quick/tests.pri b/tests/auto/quick/tests.pri
index c2e47c653..932407e66 100644
--- a/tests/auto/quick/tests.pri
+++ b/tests/auto/quick/tests.pri
@@ -9,8 +9,7 @@ TARGET = tst_$$TARGET
 SOURCES += $${TARGET}.cpp
 INCLUDEPATH += $$PWD
 
-QT += testlib network quick
-QT_PRIVATE += quick-private gui-private core-private
+QT += testlib network quick webengine
 
 macx: CONFIG -= app_bundle
 
diff --git a/tests/quicktestbrowser/main.cpp b/tests/quicktestbrowser/main.cpp
index 6c2f91de6..1e592f3e1 100644
--- a/tests/quicktestbrowser/main.cpp
+++ b/tests/quicktestbrowser/main.cpp
@@ -47,18 +47,13 @@ typedef QApplication Application;
 #include <QtGui/QGuiApplication>
 typedef QGuiApplication Application;
 #endif
-#include <QtQuick/private/qsgcontext_p.h>
+#include <qtwebengineglobal.h>
 
 int main(int argc, char **argv)
 {
     Application app(argc, argv);
 
-    // This is currently needed by all QtWebEngine application using the HW accelerated QQuickWebView.
-    // It enables sharing between the QOpenGLContext of all QQuickWindows of the application.
-    // We have to do so until we expose a public API for it, or chose enable it by default in Qt 5.3.0.
-    QOpenGLContext shareContext;
-    shareContext.create();
-    QSGContext::setSharedOpenGLContext(&shareContext);
+    QWebEngine::initialize();
 
     ApplicationEngine appEngine;
 
diff --git a/tests/quicktestbrowser/quicktestbrowser.pro b/tests/quicktestbrowser/quicktestbrowser.pro
index 83baa73f1..b0146a689 100644
--- a/tests/quicktestbrowser/quicktestbrowser.pro
+++ b/tests/quicktestbrowser/quicktestbrowser.pro
@@ -13,8 +13,7 @@ OTHER_FILES += ContextMenuExtras.qml \
 
 RESOURCES += resources.qrc
 
-QT += qml quick
-QT_PRIVATE += quick-private gui-private core-private
+QT += qml quick webengine
 
 qtHaveModule(widgets) {
     QT += widgets # QApplication is required to get native styling with QtQuickControls
-- 
GitLab