From 96425960f9d57387c33e3ba50785aaa81255bc98 Mon Sep 17 00:00:00 2001
From: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Date: Fri, 28 Mar 2014 17:14:30 +0100
Subject: [PATCH] Render the widgets view using the scene graph into a
 QOpenGLWidget

This means that widgets application now need to setup the GL context
sharing as well. QWebEngineWidgets::initialize() must be called,
which has the same effect as QWebEngine::initialize().
The QtWebEngineWidgets now depends on the QtWebEngine module to make
this happen.

Since QOpenGLWidget is only available in Qt 5.3, this patch also
disables the webenginewidgets module completely when building using
Qt 5.2.

Change-Id: I0e99a779d1eb080f2ccf5a338ff0763ad64e6eba
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
---
 examples/examples.pro                         |  3 +-
 examples/webenginewidgets/browser/main.cpp    |  5 ++
 .../webenginewidgets/fancybrowser/main.cpp    |  3 +
 src/core/content_browser_client_qt.cpp        |  7 +-
 src/core/delegated_frame_node.cpp             | 17 ++--
 src/core/delegated_frame_node.h               |  6 +-
 src/core/render_widget_host_view_qt.cpp       |  4 +-
 src/core/render_widget_host_view_qt.h         |  2 +-
 .../render_widget_host_view_qt_delegate.h     |  4 +-
 src/src.pro                                   |  4 +-
 src/webengine/api/qtwebengineglobal.cpp       | 12 ++-
 ...der_widget_host_view_qt_delegate_quick.cpp |  2 +-
 .../api/qtwebenginewidgetsglobal.cpp          | 49 ++++++++++++
 .../api/qtwebenginewidgetsglobal.h            |  6 ++
 src/webenginewidgets/api/qwebenginepage.cpp   |  4 +-
 ...er_widget_host_view_qt_delegate_widget.cpp | 77 ++++++++++++-------
 ...nder_widget_host_view_qt_delegate_widget.h | 17 ++--
 src/webenginewidgets/webenginewidgets.pro     |  5 +-
 tests/auto/auto.pro                           |  4 +-
 .../qwebengineframe/tst_qwebengineframe.cpp   |  6 ++
 .../tst_qwebenginehistory.cpp                 |  6 ++
 .../tst_qwebenginehistoryinterface.cpp        |  6 ++
 .../qwebenginepage/tst_qwebenginepage.cpp     |  1 +
 .../qwebengineview/tst_qwebengineview.cpp     |  1 +
 24 files changed, 186 insertions(+), 65 deletions(-)
 create mode 100644 src/webenginewidgets/api/qtwebenginewidgetsglobal.cpp

diff --git a/examples/examples.pro b/examples/examples.pro
index 7940e289d..3114e6887 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -1,7 +1,8 @@
 TEMPLATE=subdirs
 
 SUBDIRS += webengine/quicknanobrowser
-qtHaveModule(widgets) {
+
+qtHaveModule(widgets):equals(QT_MAJOR_VERSION, 5):greaterThan(QT_MINOR_VERSION, 2) {
     SUBDIRS += \
         webenginewidgets/browser \
         webenginewidgets/fancybrowser
diff --git a/examples/webenginewidgets/browser/main.cpp b/examples/webenginewidgets/browser/main.cpp
index 9baf01f42..d49a920b5 100644
--- a/examples/webenginewidgets/browser/main.cpp
+++ b/examples/webenginewidgets/browser/main.cpp
@@ -41,12 +41,17 @@
 
 #include "browserapplication.h"
 
+#include <qtwebenginewidgetsglobal.h>
+
 int main(int argc, char **argv)
 {
     Q_INIT_RESOURCE(data);
     BrowserApplication application(argc, argv);
     if (!application.isTheOnlyBrowser())
         return 0;
+
+    QWebEngineWidgets::initialize();
+
     application.newMainWindow();
     return application.exec();
 }
diff --git a/examples/webenginewidgets/fancybrowser/main.cpp b/examples/webenginewidgets/fancybrowser/main.cpp
index 451f247f3..c18b98d9d 100644
--- a/examples/webenginewidgets/fancybrowser/main.cpp
+++ b/examples/webenginewidgets/fancybrowser/main.cpp
@@ -40,10 +40,13 @@
 
 #include <QtWidgets>
 #include "mainwindow.h"
+#include <qtwebenginewidgetsglobal.h>
 
 int main(int argc, char * argv[])
 {
     QApplication app(argc, argv);
+    QWebEngineWidgets::initialize();
+
     QUrl url;
     if (argc > 1)
         url = QUrl::fromUserInput(argv[1]);
diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp
index 628aa6ed2..86ff11efb 100644
--- a/src/core/content_browser_client_qt.cpp
+++ b/src/core/content_browser_client_qt.cpp
@@ -58,6 +58,7 @@
 #include "web_contents_view_qt.h"
 
 #include <QGuiApplication>
+#include <QtGui/private/qopenglcontext_p.h>
 #include <QtQuick/private/qsgcontext_p.h>
 #include <qpa/qplatformnativeinterface.h>
 
@@ -248,9 +249,13 @@ private:
 void ShareGroupQtQuick::AboutToAddFirstContext()
 {
     // This currently has to be setup by ::main in all applications using QQuickWebEngineView with delegated rendering.
+#if (QT_VERSION < QT_VERSION_CHECK(5, 3, 0))
     QOpenGLContext *shareContext = QSGContext::sharedOpenGLContext();
+#else
+    QOpenGLContext *shareContext = QOpenGLContextPrivate::globalShareContext();
+#endif
     if (!shareContext) {
-        qFatal("QWebEngine: OpenGL resource sharing is not set up in QtQuick. Please make sure to call QWebEngine::initialize() in your main() function.");
+        qFatal("QWebEngine: OpenGL resource sharing is not set up in QtQuick. Please make sure to call QWebEngine::initialize() or QWebEngineWidgets::initialize() in your main() function.");
     }
     m_shareContextQtQuick = make_scoped_refptr(new QtShareGLContext(shareContext));
 }
diff --git a/src/core/delegated_frame_node.cpp b/src/core/delegated_frame_node.cpp
index 2cfcd4afe..1f7084d18 100644
--- a/src/core/delegated_frame_node.cpp
+++ b/src/core/delegated_frame_node.cpp
@@ -70,7 +70,6 @@
 #include <QSGSimpleTextureNode>
 #include <QSGTexture>
 #include <QtQuick/private/qquickclipnode_p.h>
-#include <QtQuick/private/qquickwindow_p.h>
 #include <QtQuick/private/qsgadaptationlayer_p.h>
 #include <QtQuick/private/qsgcontext_p.h>
 #include <QtQuick/private/qsgrenderer_p.h>
@@ -286,8 +285,8 @@ void MailboxTexture::fetchTexture(gpu::gles2::MailboxManager *mailboxManager)
         m_textureId = service_id(tex);
 }
 
-DelegatedFrameNode::DelegatedFrameNode(QQuickWindow *window)
-    : m_window(window)
+DelegatedFrameNode::DelegatedFrameNode(QSGRenderContext *sgRenderContext)
+    : m_sgRenderContext(sgRenderContext)
     , m_numPendingSyncPoints(0)
 {
     setFlag(UsePreprocess);
@@ -370,10 +369,8 @@ void DelegatedFrameNode::commit(DelegatedFrameNodeData* data, cc::ReturnedResour
         QSGNode *renderPassParent = 0;
         if (pass != rootRenderPass) {
             QSharedPointer<RenderPassTexture> rpTexture = findRenderPassTexture(pass->id, oldRenderPassTextures);
-            if (!rpTexture) {
-                QSGRenderContext *sgrc = QQuickWindowPrivate::get(m_window)->context;
-                rpTexture = QSharedPointer<RenderPassTexture>(new RenderPassTexture(pass->id, sgrc));
-            }
+            if (!rpTexture)
+                rpTexture = QSharedPointer<RenderPassTexture>(new RenderPassTexture(pass->id, m_sgRenderContext));
             m_renderPassTextures.append(rpTexture);
             rpTexture->setDevicePixelRatio(m_data->frameDevicePixelRatio);
             rpTexture->setRect(toQt(pass->output_rect));
@@ -404,8 +401,7 @@ void DelegatedFrameNode::commit(DelegatedFrameNodeData* data, cc::ReturnedResour
             switch (quad->material) {
             case cc::DrawQuad::CHECKERBOARD: {
                 const cc::CheckerboardDrawQuad *cbquad = cc::CheckerboardDrawQuad::MaterialCast(quad);
-                QSGRenderContext *sgrc = QQuickWindowPrivate::get(m_window)->context;
-                QSGRectangleNode *rectangleNode = sgrc->sceneGraphContext()->createRectangleNode();
+                QSGRectangleNode *rectangleNode = m_sgRenderContext->sceneGraphContext()->createRectangleNode();
 
                 rectangleNode->setRect(toQt(quad->rect));
                 rectangleNode->setColor(toQt(cbquad->color));
@@ -450,8 +446,7 @@ void DelegatedFrameNode::commit(DelegatedFrameNodeData* data, cc::ReturnedResour
                 break;
             } case cc::DrawQuad::SOLID_COLOR: {
                 const cc::SolidColorDrawQuad *scquad = cc::SolidColorDrawQuad::MaterialCast(quad);
-                QSGRenderContext *sgrc = QQuickWindowPrivate::get(m_window)->context;
-                QSGRectangleNode *rectangleNode = sgrc->sceneGraphContext()->createRectangleNode();
+                QSGRectangleNode *rectangleNode = m_sgRenderContext->sceneGraphContext()->createRectangleNode();
 
                 // Qt only supports MSAA and this flag shouldn't be needed.
                 // If we ever want to use QSGRectangleNode::setAntialiasing for this we should
diff --git a/src/core/delegated_frame_node.h b/src/core/delegated_frame_node.h
index 71a3e1d25..214894821 100644
--- a/src/core/delegated_frame_node.h
+++ b/src/core/delegated_frame_node.h
@@ -51,7 +51,7 @@
 #include <QWaitCondition>
 
 QT_BEGIN_NAMESPACE
-class QQuickWindow;
+class QSGRenderContext;
 QT_END_NAMESPACE
 
 namespace cc {
@@ -73,14 +73,14 @@ public:
 
 class DelegatedFrameNode : public QSGTransformNode {
 public:
-    DelegatedFrameNode(QQuickWindow *window);
+    DelegatedFrameNode(QSGRenderContext *sgRenderContext);
     ~DelegatedFrameNode();
     void preprocess();
     void commit(DelegatedFrameNodeData* data, cc::ReturnedResourceArray *resourcesToRelease);
 
 private:
     QExplicitlySharedDataPointer<DelegatedFrameNodeData> m_data;
-    QQuickWindow *m_window;
+    QSGRenderContext *m_sgRenderContext;
     QList<QSharedPointer<RenderPassTexture> > m_renderPassTextures;
     int m_numPendingSyncPoints;
     QWaitCondition m_mailboxesFetchedWaitCond;
diff --git a/src/core/render_widget_host_view_qt.cpp b/src/core/render_widget_host_view_qt.cpp
index 7430f6756..7df9ee46a 100644
--- a/src/core/render_widget_host_view_qt.cpp
+++ b/src/core/render_widget_host_view_qt.cpp
@@ -685,11 +685,11 @@ void RenderWidgetHostViewQt::paint(QPainter *painter, const QRectF& boundingRect
         m_backingStore->paintToTarget(painter, boundingRect);
 }
 
-QSGNode *RenderWidgetHostViewQt::updatePaintNode(QSGNode *oldNode, QQuickWindow *window)
+QSGNode *RenderWidgetHostViewQt::updatePaintNode(QSGNode *oldNode, QSGRenderContext *sgRenderContext)
 {
     DelegatedFrameNode *frameNode = static_cast<DelegatedFrameNode *>(oldNode);
     if (!frameNode)
-        frameNode = new DelegatedFrameNode(window);
+        frameNode = new DelegatedFrameNode(sgRenderContext);
 
     frameNode->commit(m_frameNodeData.data(), &m_resourcesToRelease);
 
diff --git a/src/core/render_widget_host_view_qt.h b/src/core/render_widget_host_view_qt.h
index 7ecfe7ecb..208336b8b 100644
--- a/src/core/render_widget_host_view_qt.h
+++ b/src/core/render_widget_host_view_qt.h
@@ -168,7 +168,7 @@ public:
 
     // Overridden from RenderWidgetHostViewQtDelegateClient.
     virtual void paint(QPainter *, const QRectF& boundingRect) Q_DECL_OVERRIDE;
-    virtual QSGNode *updatePaintNode(QSGNode *, QQuickWindow *) Q_DECL_OVERRIDE;
+    virtual QSGNode *updatePaintNode(QSGNode *, QSGRenderContext *) Q_DECL_OVERRIDE;
     virtual void fetchBackingStore() Q_DECL_OVERRIDE;
     virtual void notifyResize() Q_DECL_OVERRIDE;
     virtual bool forwardEvent(QEvent *) Q_DECL_OVERRIDE;
diff --git a/src/core/render_widget_host_view_qt_delegate.h b/src/core/render_widget_host_view_qt_delegate.h
index 08d66745b..ddfa2b3c8 100644
--- a/src/core/render_widget_host_view_qt_delegate.h
+++ b/src/core/render_widget_host_view_qt_delegate.h
@@ -51,8 +51,8 @@ QT_BEGIN_NAMESPACE
 class QCursor;
 class QEvent;
 class QPainter;
-class QQuickWindow;
 class QSGNode;
+class QSGRenderContext;
 class QVariant;
 class QWindow;
 class QInputMethodEvent;
@@ -64,7 +64,7 @@ class QWEBENGINE_EXPORT RenderWidgetHostViewQtDelegateClient {
 public:
     virtual ~RenderWidgetHostViewQtDelegateClient() { }
     virtual void paint(QPainter *, const QRectF& boundingRect) = 0;
-    virtual QSGNode *updatePaintNode(QSGNode *, QQuickWindow *) = 0;
+    virtual QSGNode *updatePaintNode(QSGNode *, QSGRenderContext *) = 0;
     virtual void fetchBackingStore() = 0;
     virtual void notifyResize() = 0;
     virtual bool forwardEvent(QEvent *) = 0;
diff --git a/src/src.pro b/src/src.pro
index 536fcad5c..6d902497f 100644
--- a/src/src.pro
+++ b/src/src.pro
@@ -2,7 +2,7 @@ TEMPLATE = subdirs
 
 process.depends = core
 webengine.depends = core
-webenginewidgets.depends = core
+webenginewidgets.depends = core webengine
 webengine_plugin.subdir = webengine/plugin
 webengine_plugin.target = sub-webengine-plugin
 webengine_plugin.depends = webengine
@@ -21,6 +21,6 @@ SUBDIRS += core \
 # Another example of where this could be necessary is to make it easy to build proprietery codecs support.
 !contains(WEBENGINE_CONFIG, no_ui_delegates): SUBDIRS += webengine/ui
 
-qtHaveModule(widgets) {
+qtHaveModule(widgets):equals(QT_MAJOR_VERSION, 5):greaterThan(QT_MINOR_VERSION, 2) {
     SUBDIRS += webenginewidgets
 }
diff --git a/src/webengine/api/qtwebengineglobal.cpp b/src/webengine/api/qtwebengineglobal.cpp
index 4aa72ba6f..7340d32d1 100644
--- a/src/webengine/api/qtwebengineglobal.cpp
+++ b/src/webengine/api/qtwebengineglobal.cpp
@@ -41,8 +41,10 @@
 
 #include "qtwebengineglobal.h"
 
-#include <private/qsgcontext_p.h>
 #include <QGuiApplication>
+#include <QThread>
+#include <private/qopenglcontext_p.h>
+#include <private/qsgcontext_p.h>
 
 static QOpenGLContext *shareContext;
 
@@ -56,11 +58,11 @@ void QWebEngine::initialize()
 {
     QCoreApplication *app = QCoreApplication::instance();
     if (!app) {
-        qFatal("QWebEngine::initialize() must be called after the construction of the application object.");
+        qFatal("QWebEngine(Widgets)::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.");
+        qFatal("QWebEngine(Widgets)::initialize() must be called from the Qt gui thread.");
         return;
     }
 
@@ -70,6 +72,10 @@ void QWebEngine::initialize()
     shareContext = new QOpenGLContext;
     shareContext->create();
     qAddPostRoutine(deleteShareContext);
+#if (QT_VERSION < QT_VERSION_CHECK(5, 3, 0))
     QSGContext::setSharedOpenGLContext(shareContext);
+#else
+    QOpenGLContextPrivate::setGlobalShareContext(shareContext);
+#endif
 }
 
diff --git a/src/webengine/render_widget_host_view_qt_delegate_quick.cpp b/src/webengine/render_widget_host_view_qt_delegate_quick.cpp
index 693b12e07..e2804ef9b 100644
--- a/src/webengine/render_widget_host_view_qt_delegate_quick.cpp
+++ b/src/webengine/render_widget_host_view_qt_delegate_quick.cpp
@@ -66,7 +66,7 @@ void RenderWidgetHostViewQtDelegateQuick::itemChange(ItemChange change, const It
 
 QSGNode *RenderWidgetHostViewQtDelegateQuick::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
 {
-    return m_client->updatePaintNode(oldNode, QQuickItem::window());
+    return m_client->updatePaintNode(oldNode, QQuickWindowPrivate::get(QQuickItem::window())->context);
 }
 
 RenderWidgetHostViewQtDelegateQuickPainted::RenderWidgetHostViewQtDelegateQuickPainted(RenderWidgetHostViewQtDelegateClient *client, bool isPopup)
diff --git a/src/webenginewidgets/api/qtwebenginewidgetsglobal.cpp b/src/webenginewidgets/api/qtwebenginewidgetsglobal.cpp
new file mode 100644
index 000000000..f3792248e
--- /dev/null
+++ b/src/webenginewidgets/api/qtwebenginewidgetsglobal.cpp
@@ -0,0 +1,49 @@
+/****************************************************************************
+**
+** 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 "qtwebenginewidgetsglobal.h"
+
+#include "qtwebengineglobal.h"
+
+void QWebEngineWidgets::initialize()
+{
+    QWebEngine::initialize();
+}
diff --git a/src/webenginewidgets/api/qtwebenginewidgetsglobal.h b/src/webenginewidgets/api/qtwebenginewidgetsglobal.h
index d875c1cbe..1836de357 100644
--- a/src/webenginewidgets/api/qtwebenginewidgetsglobal.h
+++ b/src/webenginewidgets/api/qtwebenginewidgetsglobal.h
@@ -55,6 +55,12 @@ QT_BEGIN_NAMESPACE
 #  define QWEBENGINEWIDGETS_EXPORT
 #endif
 
+class QWEBENGINEWIDGETS_EXPORT QWebEngineWidgets
+{
+public:
+    static void initialize();
+};
+
 QT_END_NAMESPACE
 
 #endif // QTWEBENGINEWIDGETSGLOBAL_H
diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index a5e5f8320..a879a94ef 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -148,7 +148,7 @@ void CallbackDirectory::CallbackSharedDataPointer::doDeref()
 
 QWebEnginePagePrivate::QWebEnginePagePrivate()
     : QObjectPrivate(QObjectPrivateVersion)
-    , adapter(new WebContentsAdapter(SoftwareRenderingMode))
+    , adapter(new WebContentsAdapter(HardwareAccelerationMode))
     , history(new QWebEngineHistory(new QWebEngineHistoryPrivate(this)))
     , view(0)
 {
@@ -353,7 +353,7 @@ void QWebEnginePagePrivate::_q_webActionTriggered(bool checked)
 
 void QWebEnginePagePrivate::recreateFromSerializedHistory(QDataStream &input)
 {
-    QExplicitlySharedDataPointer<WebContentsAdapter> newWebContents = WebContentsAdapter::createFromSerializedNavigationHistory(input, this, WebContentsAdapterClient::SoftwareRenderingMode);
+    QExplicitlySharedDataPointer<WebContentsAdapter> newWebContents = WebContentsAdapter::createFromSerializedNavigationHistory(input, this, WebContentsAdapterClient::HardwareAccelerationMode);
     if (newWebContents) {
         adapter = newWebContents.data();
         adapter->initialize(this);
diff --git a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
index 71d6e235e..308735f70 100644
--- a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
+++ b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
@@ -41,19 +41,21 @@
 
 #include "render_widget_host_view_qt_delegate_widget.h"
 
-#include "qwebengineview.h"
 #include "qwebenginepage_p.h"
-#include <QtGlobal>
+#include "qwebengineview.h"
 #include <QLayout>
-#include <QResizeEvent>
-#include <QPainter>
-#include <QPaintEvent>
-#include <QWindow>
-#include <QtWidgets/QApplication>
+#include <QSGNode>
+#include <private/qsgadaptationlayer_p.h>
+#include <private/qsgcontext_p.h>
+#include <private/qsgrenderer_p.h>
+#include <private/qwidget_p.h>
 
 RenderWidgetHostViewQtDelegateWidget::RenderWidgetHostViewQtDelegateWidget(RenderWidgetHostViewQtDelegateClient *client, QWidget *parent)
-    : QWidget(parent)
+    : QOpenGLWidget(parent)
     , m_client(client)
+    , sgContext(QSGContext::createDefaultContext())
+    , sgRenderContext(new QSGRenderContext(sgContext.data()))
+    , rootNode(new QSGRootNode)
     , m_isPopup(false)
 {
     setFocusPolicy(Qt::ClickFocus);
@@ -68,7 +70,7 @@ void RenderWidgetHostViewQtDelegateWidget::initAsChild(WebContentsAdapterClient*
     QWebEnginePagePrivate *pagePrivate = static_cast<QWebEnginePagePrivate *>(container);
     if (pagePrivate->view) {
         pagePrivate->view->layout()->addWidget(this);
-        QWidget::show();
+        QOpenGLWidget::show();
     } else
         setParent(0);
 }
@@ -108,38 +110,38 @@ void RenderWidgetHostViewQtDelegateWidget::show()
     // Check if we're attached to a QWebEngineView, we don't
     // want to show anything else than popups as top-level.
     if (parent() || m_isPopup)
-        QWidget::show();
+        QOpenGLWidget::show();
 }
 
 void RenderWidgetHostViewQtDelegateWidget::hide()
 {
-    QWidget::hide();
+    QOpenGLWidget::hide();
 }
 
 bool RenderWidgetHostViewQtDelegateWidget::isVisible() const
 {
-    return QWidget::isVisible();
+    return QOpenGLWidget::isVisible();
 }
 
 QWindow* RenderWidgetHostViewQtDelegateWidget::window() const
 {
-    const QWidget* root = QWidget::window();
+    const QWidget* root = QOpenGLWidget::window();
     return root ? root->windowHandle() : 0;
 }
 
-void RenderWidgetHostViewQtDelegateWidget::update(const QRect& rect)
+void RenderWidgetHostViewQtDelegateWidget::update(const QRect&)
 {
-    QWidget::update(rect);
+    updateGL();
 }
 
 void RenderWidgetHostViewQtDelegateWidget::updateCursor(const QCursor &cursor)
 {
-    QWidget::setCursor(cursor);
+    QOpenGLWidget::setCursor(cursor);
 }
 
 void RenderWidgetHostViewQtDelegateWidget::resize(int width, int height)
 {
-    QWidget::resize(width, height);
+    QOpenGLWidget::resize(width, height);
 }
 
 void RenderWidgetHostViewQtDelegateWidget::move(const QPoint &screenPos)
@@ -153,7 +155,7 @@ void RenderWidgetHostViewQtDelegateWidget::inputMethodStateChanged(bool editorVi
     if (qApp->inputMethod()->isVisible() == editorVisible)
         return;
 
-    QWidget::setAttribute(Qt::WA_InputMethodEnabled, editorVisible);
+    QOpenGLWidget::setAttribute(Qt::WA_InputMethodEnabled, editorVisible);
     qApp->inputMethod()->update(Qt::ImQueryInput | Qt::ImEnabled | Qt::ImHints);
     qApp->inputMethod()->setVisible(editorVisible);
 }
@@ -170,25 +172,44 @@ QVariant RenderWidgetHostViewQtDelegateWidget::inputMethodQuery(Qt::InputMethodQ
 
 bool RenderWidgetHostViewQtDelegateWidget::supportsHardwareAcceleration() const
 {
-    return false;
-}
-
-void RenderWidgetHostViewQtDelegateWidget::paintEvent(QPaintEvent * event)
-{
-    QPainter painter(this);
-    m_client->fetchBackingStore();
-    m_client->paint(&painter, event->rect());
+    return true;
 }
 
 void RenderWidgetHostViewQtDelegateWidget::resizeEvent(QResizeEvent *resizeEvent)
 {
-    Q_UNUSED(resizeEvent);
+    QOpenGLWidget::resizeEvent(resizeEvent);
     m_client->notifyResize();
 }
 
 bool RenderWidgetHostViewQtDelegateWidget::event(QEvent *event)
 {
     if (!m_client->forwardEvent(event))
-        return QWidget::event(event);
+        return QOpenGLWidget::event(event);
     return true;
 }
+
+void RenderWidgetHostViewQtDelegateWidget::initializeGL()
+{
+    sgRenderContext->initialize(QOpenGLContext::currentContext());
+    sgRenderer.reset(sgRenderContext->createRenderer());
+    sgRenderer->setRootNode(rootNode.data());
+}
+
+void RenderWidgetHostViewQtDelegateWidget::paintGL()
+{
+    QSGNode *paintNode = m_client->updatePaintNode(rootNode->firstChild(), sgRenderContext.data());
+    if (paintNode != rootNode->firstChild()) {
+        delete rootNode->firstChild();
+        rootNode->appendChildNode(paintNode);
+    }
+
+    rootNode->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update.
+    sgRenderer->nodeChanged(rootNode.data(), QSGNode::DirtyForceUpdate); // Force render list update.
+
+    sgRenderer->setDeviceRect(size());
+    sgRenderer->setViewportRect(size());
+    sgRenderer->setProjectionMatrixToRect(QRectF(QPointF(), size()));
+    sgRenderer->setClearColor(Qt::transparent);
+
+    sgRenderContext->renderNextFrame(sgRenderer.data(), defaultFramebufferObject());
+}
diff --git a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h
index 77a594d61..6a77d8321 100644
--- a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h
+++ b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h
@@ -45,15 +45,17 @@
 #include "render_widget_host_view_qt_delegate.h"
 #include "web_contents_adapter_client.h"
 
-#include <QWidget>
-
-class BackingStoreQt;
+#include <private/qopenglwidget_p.h>
 
 QT_BEGIN_NAMESPACE
+class QSGContext;
+class QSGRenderContext;
+class QSGRenderer;
+class QSGRootNode;
 class QWindow;
 QT_END_NAMESPACE
 
-class RenderWidgetHostViewQtDelegateWidget : public QWidget, public RenderWidgetHostViewQtDelegate
+class RenderWidgetHostViewQtDelegateWidget : public QOpenGLWidget, public RenderWidgetHostViewQtDelegate
 {
 public:
     RenderWidgetHostViewQtDelegateWidget(RenderWidgetHostViewQtDelegateClient *client, QWidget *parent = 0);
@@ -76,14 +78,19 @@ public:
     virtual void setTooltip(const QString &tooltip) Q_DECL_OVERRIDE;
 
 protected:
-    void paintEvent(QPaintEvent * event);
     bool event(QEvent *event);
     void resizeEvent(QResizeEvent *resizeEvent);
+    void initializeGL() Q_DECL_OVERRIDE;
+    void paintGL() Q_DECL_OVERRIDE;
 
     QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
 
 private:
     RenderWidgetHostViewQtDelegateClient *m_client;
+    QScopedPointer<QSGContext> sgContext;
+    QScopedPointer<QSGRenderContext> sgRenderContext;
+    QScopedPointer<QSGRootNode> rootNode;
+    QScopedPointer<QSGRenderer> sgRenderer;
     bool m_isPopup;
 };
 
diff --git a/src/webenginewidgets/webenginewidgets.pro b/src/webenginewidgets/webenginewidgets.pro
index 182cb5500..1f2eb84b3 100644
--- a/src/webenginewidgets/webenginewidgets.pro
+++ b/src/webenginewidgets/webenginewidgets.pro
@@ -3,14 +3,15 @@ TARGET = QtWebEngineWidgets
 # For our export macros
 DEFINES += QT_BUILD_WEBENGINEWIDGETS_LIB
 
-QT += widgets network
-QT_PRIVATE += webenginecore widgets-private gui-private network-private core-private
+QT += webengine widgets network quick
+QT_PRIVATE += webenginecore widgets-private quick-private gui-private network-private core-private
 
 QMAKE_DOCS = $$PWD/doc/qtwebenginewidgets.qdocconf
 
 INCLUDEPATH += $$PWD api ../core
 
 SOURCES = \
+        api/qtwebenginewidgetsglobal.cpp \
         api/qwebenginehistory.cpp \
         api/qwebenginepage.cpp \
         api/qwebengineview.cpp\
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro
index 359bc3145..4d9f78917 100644
--- a/tests/auto/auto.pro
+++ b/tests/auto/auto.pro
@@ -1,5 +1,7 @@
 TEMPLATE = subdirs
 
 SUBDIRS = quick
-qtHaveModule(widgets): SUBDIRS += widgets
 
+qtHaveModule(widgets):equals(QT_MAJOR_VERSION, 5):greaterThan(QT_MINOR_VERSION, 2) {
+    SUBDIRS += widgets
+}
diff --git a/tests/auto/widgets/qwebengineframe/tst_qwebengineframe.cpp b/tests/auto/widgets/qwebengineframe/tst_qwebengineframe.cpp
index 448f488f6..608b09396 100644
--- a/tests/auto/widgets/qwebengineframe/tst_qwebengineframe.cpp
+++ b/tests/auto/widgets/qwebengineframe/tst_qwebengineframe.cpp
@@ -45,6 +45,7 @@ public:
     bool eventFilter(QObject* watched, QEvent* event);
 
 public Q_SLOTS:
+    void initTestCase();
     void init();
     void cleanup();
 
@@ -115,6 +116,11 @@ bool tst_QWebEngineFrame::eventFilter(QObject* watched, QEvent* event)
     return QObject::eventFilter(watched, event);
 }
 
+void tst_QWebEngineFrame::initTestCase()
+{
+    QWebEngineWidgets::initialize();
+}
+
 void tst_QWebEngineFrame::init()
 {
     m_view = new QWebEngineView();
diff --git a/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp b/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
index a5cbc6103..14008d075 100644
--- a/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
+++ b/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
@@ -42,6 +42,7 @@ protected :
     }
 
 public Q_SLOTS:
+    void initTestCase();
     void init();
     void cleanup();
 
@@ -86,6 +87,11 @@ tst_QWebEngineHistory::~tst_QWebEngineHistory()
 {
 }
 
+void tst_QWebEngineHistory::initTestCase()
+{
+    QWebEngineWidgets::initialize();
+}
+
 void tst_QWebEngineHistory::init()
 {
     page = new QWebEnginePage(this);
diff --git a/tests/auto/widgets/qwebenginehistoryinterface/tst_qwebenginehistoryinterface.cpp b/tests/auto/widgets/qwebenginehistoryinterface/tst_qwebenginehistoryinterface.cpp
index 60c54e93e..180a0f2fe 100644
--- a/tests/auto/widgets/qwebenginehistoryinterface/tst_qwebenginehistoryinterface.cpp
+++ b/tests/auto/widgets/qwebenginehistoryinterface/tst_qwebenginehistoryinterface.cpp
@@ -34,6 +34,7 @@ public:
     virtual ~tst_QWebEngineHistoryInterface();
 
 public Q_SLOTS:
+    void initTestCase();
     void init();
     void cleanup();
 
@@ -56,6 +57,11 @@ tst_QWebEngineHistoryInterface::~tst_QWebEngineHistoryInterface()
 {
 }
 
+void tst_QWebEngineHistoryInterface::initTestCase()
+{
+    QWebEngineWidgets::initialize();
+}
+
 void tst_QWebEngineHistoryInterface::init()
 {
     m_view = new QWebEngineView();
diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
index 9abc3ad0b..3c2bbb67b 100644
--- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
+++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
@@ -228,6 +228,7 @@ void tst_QWebEnginePage::cleanupFiles()
 
 void tst_QWebEnginePage::initTestCase()
 {
+    QWebEngineWidgets::initialize();
     cleanupFiles(); // In case there are old files from previous runs
 }
 
diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
index 4c238a074..a0bf36f60 100644
--- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
+++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
@@ -63,6 +63,7 @@ private Q_SLOTS:
 // It is only called once.
 void tst_QWebEngineView::initTestCase()
 {
+    QWebEngineWidgets::initialize();
 }
 
 // This will be called after the last test function is executed.
-- 
GitLab