Copy QSGEngine functions to QQuickCanvas.
authorGunnar Sletta <gunnar.sletta@nokia.com>
Wed, 16 Nov 2011 14:39:45 +0000 (15:39 +0100)
committerQt by Nokia <qt-info@nokia.com>
Fri, 18 Nov 2011 14:44:20 +0000 (15:44 +0100)
Long term we intend to remove the QSGEngine class
all together so this is the first step. It duplicates
some of the logic but doesn't break anything.

Also including an example on how to use it in
examples/declarative/openglunderqml

Change-Id: I69ed93ec5fa1b5c4c746169306d38f8d6ce80477
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
12 files changed:
examples/declarative/openglunderqml/main.cpp [new file with mode: 0644]
examples/declarative/openglunderqml/main.qml [new file with mode: 0644]
examples/declarative/openglunderqml/openglunderqml.pro [new file with mode: 0644]
examples/declarative/openglunderqml/squircle.cpp [new file with mode: 0644]
examples/declarative/openglunderqml/squircle.h [new file with mode: 0644]
src/declarative/items/qquickcanvas.cpp
src/declarative/items/qquickcanvas.h
src/declarative/items/qquickcanvas_p.h
src/declarative/scenegraph/qsgcontext.cpp
src/declarative/scenegraph/qsgcontext_p.h
src/declarative/scenegraph/util/qsgengine.cpp
src/declarative/scenegraph/util/qsgengine.h

diff --git a/examples/declarative/openglunderqml/main.cpp b/examples/declarative/openglunderqml/main.cpp
new file mode 100644 (file)
index 0000000..44984c5
--- /dev/null
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QGuiApplication>
+
+#include <QQuickView>
+
+#include "squircle.h"
+
+int main(int argc, char **argv)
+{
+    QGuiApplication app(argc, argv);
+
+    qmlRegisterType<Squircle>("QtQuick", 2, 0, "Squircle");
+
+    QQuickView view;
+    view.setVSyncAnimations(true);
+    view.setSource(QUrl("main.qml"));
+    view.show();
+
+    return app.exec();
+
+}
diff --git a/examples/declarative/openglunderqml/main.qml b/examples/declarative/openglunderqml/main.qml
new file mode 100644 (file)
index 0000000..0d2e65d
--- /dev/null
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+Item {
+
+    width: 400
+    height: 300
+
+    Squircle {
+        SequentialAnimation on t {
+            NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
+            NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
+            loops: Animation.Infinite
+            running: true
+        }
+    }
+
+    Rectangle {
+        color: Qt.rgba(1, 1, 1, 0.8);
+        radius: 10
+        border.width: 1
+        border.color: "white"
+        anchors.fill: label
+        anchors.margins: -10
+    }
+
+    Text {
+        id: label
+        color: "black"
+        wrapMode: Text.WordWrap
+        text: "The background here is a squircle rendered with raw OpenGL using the 'beforeRender()' signal in QQuickCanvas. This text label and its border is rendered using QML"
+        anchors.right: parent.right
+        anchors.left: parent.left
+        anchors.bottom: parent.bottom
+        anchors.margins: 20
+    }
+
+}
diff --git a/examples/declarative/openglunderqml/openglunderqml.pro b/examples/declarative/openglunderqml/openglunderqml.pro
new file mode 100644 (file)
index 0000000..3cd1673
--- /dev/null
@@ -0,0 +1,11 @@
+QT += declarative
+
+TEMPLATE = app
+DEPENDPATH += .
+INCLUDEPATH += .
+
+# Input
+HEADERS += squircle.h
+SOURCES += squircle.cpp main.cpp
+
+OTHER_FILES += main.qml
diff --git a/examples/declarative/openglunderqml/squircle.cpp b/examples/declarative/openglunderqml/squircle.cpp
new file mode 100644 (file)
index 0000000..c2d4c0a
--- /dev/null
@@ -0,0 +1,123 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "squircle.h"
+
+#include <qsgengine.h>
+#include <qquickcanvas.h>
+#include <QOpenGLShaderProgram>
+
+Squircle::Squircle()
+    : m_program(0)
+{
+    setFlag(ItemHasContents);
+}
+
+void Squircle::itemChange(ItemChange change, const ItemChangeData &)
+{
+    // The ItemSceneChange event is sent when we are first attached to a canvas.
+    if (change == ItemSceneChange) {
+        QQuickCanvas *c = canvas();
+
+        // Connect our the beforeRendering signal to our paint function.
+        // Since this call is executed on the rendering thread it must be
+        // a Qt::DirectConnection
+        connect(c, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
+
+        // If we allow QML to do the clearing, they would clear what we paint
+        // and nothing would show.
+        c->setClearBeforeRendering(false);
+    }
+}
+
+
+void Squircle::paint()
+{
+    if (!m_program) {
+        m_program = new QOpenGLShaderProgram();
+        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
+                                           "attribute highp vec4 vertices;"
+                                           "varying highp vec2 coords;"
+                                           "void main() {"
+                                           "    gl_Position = vertices;"
+                                           "    coords = vertices.xy;"
+                                           "}");
+        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
+                                           "uniform lowp float t;"
+                                           "varying highp vec2 coords;"
+                                           "void main() {"
+                                           "    lowp float i = 1. - (pow(coords.x, 4.) + pow(coords.y, 4.));"
+                                           "    i = smoothstep(t - 0.3, t + 0.3, i);"
+                                           "    gl_FragColor = vec4(coords / 2. + .5, i, i);"
+                                           "}");
+
+        m_program->bindAttributeLocation("vertices", 0);
+        m_program->link();
+    }
+
+    m_program->bind();
+
+    m_program->enableAttributeArray(0);
+
+    float values[] = {
+        -1, -1,
+        1, -1,
+        -1, 1,
+        1, 1
+    };
+    m_program->setAttributeArray(0, GL_FLOAT, values, 2);
+    m_program->setUniformValue("t", (float) m_t);
+
+    glDisable(GL_DEPTH_TEST);
+
+    glClearColor(0, 0, 0, 1);
+    glClear(GL_COLOR_BUFFER_BIT);
+
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+    m_program->disableAttributeArray(0);
+    m_program->release();
+}
+
+
diff --git a/examples/declarative/openglunderqml/squircle.h b/examples/declarative/openglunderqml/squircle.h
new file mode 100644 (file)
index 0000000..c92c93e
--- /dev/null
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef SQUIRCLE_H
+#define SQUIRCLE_H
+
+#include <QtDeclarative/QQuickItem>
+#include <QtGui/QOpenGLShaderProgram>
+
+class Squircle : public QQuickItem
+{
+    Q_OBJECT
+
+    Q_PROPERTY(qreal t READ t WRITE setT NOTIFY tChanged)
+
+public:
+    Squircle();
+
+    qreal t() const { return m_t; }
+    void setT(qreal t) { m_t = t; emit tChanged(); }
+
+    void itemChange(ItemChange change, const ItemChangeData &);
+
+signals:
+    void tChanged();
+
+public slots:
+    void paint();
+
+private:
+    QOpenGLShaderProgram *m_program;
+
+    qreal m_t;
+    bool m_render_under;
+    bool m_render_over;
+};
+
+#endif // SQUIRCLE_H
index 383b552..765f9ec 100644 (file)
@@ -46,7 +46,9 @@
 #include "qquickitem_p.h"
 
 #include <private/qsgrenderer_p.h>
+#include <private/qsgtexture_p.h>
 #include <private/qsgflashnode_p.h>
+#include <qsgengine.h>
 
 #include <private/qguiapplication_p.h>
 #include <QtGui/QInputPanel>
@@ -412,6 +414,9 @@ void QQuickCanvasPrivate::initializeSceneGraph()
         context->rootNode()->appendChildNode(QQuickItemPrivate::get(rootItem)->itemNode());
     }
 
+    engine = new QSGEngine();
+    engine->setCanvas(q);
+
     emit q_func()->sceneGraphInitialized();
 }
 
@@ -431,16 +436,26 @@ void QQuickCanvasPrivate::polishItems()
 void QQuickCanvasPrivate::syncSceneGraph()
 {
     updateDirtyNodes();
+
+    // Copy the current state of clearing from canvas into renderer.
+    context->renderer()->setClearColor(clearColor);
+    QSGRenderer::ClearMode mode = QSGRenderer::ClearStencilBuffer | QSGRenderer::ClearDepthBuffer;
+    if (clearBeforeRendering)
+        mode |= QSGRenderer::ClearColorBuffer;
+    context->renderer()->setClearMode(mode);
 }
 
 
 void QQuickCanvasPrivate::renderSceneGraph(const QSize &size)
 {
+    Q_Q(QQuickCanvas);
     context->renderer()->setDeviceRect(QRect(QPoint(0, 0), size));
     context->renderer()->setViewportRect(QRect(QPoint(0, 0), renderTarget ? renderTarget->size() : size));
     context->renderer()->setProjectionMatrixToDeviceRect();
 
+    emit q->beforeRendering();
     context->renderNextFrame(renderTarget);
+    emit q->afterRendering();
 
 #ifdef FRAME_TIMING
     sceneGraphRenderTime = frameTimer.elapsed();
@@ -467,7 +482,9 @@ QQuickCanvasPrivate::QQuickCanvasPrivate()
     , mouseGrabberItem(0)
     , dirtyItemList(0)
     , context(0)
+    , clearColor(Qt::white)
     , vsyncAnimations(false)
+    , clearBeforeRendering(true)
     , thread(0)
     , animationDriver(0)
     , renderTarget(0)
@@ -1937,13 +1954,16 @@ void QQuickCanvas::maybeUpdate()
     The engine will only be available once the scene graph has been
     initialized. Register for the sceneGraphEngine() signal to get
     notification about this.
+
+    \deprecated
  */
 
 QSGEngine *QQuickCanvas::sceneGraphEngine() const
 {
     Q_D(const QQuickCanvas);
+    qWarning("QQuickCanvas::sceneGraphEngine() is deprecated, use members of QQuickCanvas instead");
     if (d->context && d->context->isReady())
-        return d->context->engine();
+        return d->engine;
     return 0;
 }
 
@@ -2019,6 +2039,166 @@ QDeclarativeIncubationController *QQuickCanvas::incubationController() const
 }
 
 
+
+/*!
+    \enum QQuickCanvas::CreateTextureOption
+
+    The CreateTextureOption enums are used to customize a texture is wrapped.
+
+    \value TextureHasAlphaChannel The texture has an alpha channel and should
+    be drawn using blending.
+
+    \value TextureHasMipmaps The texture has mipmaps and can be drawn with
+    mipmapping enabled.
+
+    \value TextureOwnsGLTexture The texture object owns the texture id and
+    will delete the GL texture when the texture object is deleted.
+ */
+
+/*!
+    \fn void QQuickCanvas::beforeRendering()
+
+    This signal is emitted before the scene starts rendering.
+
+    Combined with the modes for clearing the background, this option
+    can be used to paint using raw GL under QML content.
+
+    The GL context used for rendering the scene graph will be bound
+    at this point.
+
+    Since this signal is emitted from the scene graph rendering thread, the receiver should
+    be on the scene graph thread or the connection should be Qt::DirectConnection.
+
+*/
+
+/*!
+    \fn void QQuickCanvas::afterRendering()
+
+    This signal is emitted after the scene has completed rendering, before swapbuffers is called.
+
+    This signal can be used to paint using raw GL on top of QML content,
+    or to do screen scraping of the current frame buffer.
+
+    The GL context used for rendering the scene graph will be bound at this point.
+
+    Since this signal is emitted from the scene graph rendering thread, the receiver should
+    be on the scene graph thread or the connection should be Qt::DirectConnection.
+ */
+
+
+
+/*!
+    Sets weither the scene graph rendering of QML should clear the color buffer
+    before it starts rendering to \a enbled.
+
+    By disabling clearing of the color buffer, it is possible to do GL painting
+    under the scene graph.
+
+    The color buffer is cleared by default.
+
+    \sa beforeRendering()
+ */
+
+void QQuickCanvas::setClearBeforeRendering(bool enabled)
+{
+    Q_D(QQuickCanvas);
+    d->clearBeforeRendering = enabled;
+}
+
+
+
+/*!
+    Returns weither clearing of the color buffer is done before rendering or not.
+ */
+
+bool QQuickCanvas::clearBeforeRendering() const
+{
+    Q_D(const QQuickCanvas);
+    return d->clearBeforeRendering;
+}
+
+
+
+/*!
+    Creates a new QSGTexture from the supplied \a image. If the image has an
+    alpha channel, the corresponding texture will have an alpha channel.
+
+    The caller of the function is responsible for deleting the returned texture.
+    The actual GL texture will be deleted when the texture object is deleted.
+
+    \warning This function will return 0 if the scene graph has not yet been
+    initialized.
+
+    This function can be called both from the GUI thread and the rendering thread.
+
+    \sa sceneGraphInitialized()
+ */
+
+QSGTexture *QQuickCanvas::createTextureFromImage(const QImage &image) const
+{
+    Q_D(const QQuickCanvas);
+    if (d->context)
+        return d->context->createTexture(image);
+    else
+        return 0;
+}
+
+
+
+/*!
+    Creates a new QSGTexture object from an existing GL texture \a id.
+
+    The caller of the function is responsible for deleting the returned texture.
+
+    Use \a options to customize the texture attributes.
+
+    \warning This function will return 0 if the scenegraph has not yet been
+    initialized.
+
+    \sa sceneGraphInitialized()
+ */
+QSGTexture *QQuickCanvas::createTextureFromId(uint id, const QSize &size, CreateTextureOptions options) const
+{
+    Q_D(const QQuickCanvas);
+    if (d->context) {
+        QSGPlainTexture *texture = new QSGPlainTexture();
+        texture->setTextureId(id);
+        texture->setHasAlphaChannel(options & TextureHasAlphaChannel);
+        texture->setHasMipmaps(options & TextureHasMipmaps);
+        texture->setOwnsTexture(options & TextureOwnsGLTexture);
+        texture->setTextureSize(size);
+        return texture;
+    }
+    return 0;
+}
+
+
+/*!
+    Sets the color used to clear the opengl context to \a color.
+
+    Setting the clear color has no effect when clearing is disabled.
+
+    \sa setClearBeforeRendering()
+ */
+
+void QQuickCanvas::setClearColor(const QColor &color)
+{
+    d_func()->clearColor = color;
+}
+
+
+
+/*!
+    Returns the color used to clear the opengl context.
+ */
+
+QColor QQuickCanvas::clearColor() const
+{
+    return d_func()->clearColor;
+}
+
+
+
 void QQuickCanvasRenderLoop::createGLContext()
 {
     gl = new QOpenGLContext();
index e9b1b60..847a6fb 100644 (file)
@@ -54,6 +54,7 @@ QT_MODULE(Declarative)
 
 class QQuickItem;
 class QSGEngine;
+class QSGTexture;
 class QQuickCanvasPrivate;
 class QOpenGLFramebufferObject;
 class QDeclarativeIncubationController;
@@ -63,6 +64,14 @@ class Q_DECLARATIVE_EXPORT QQuickCanvas : public QWindow
 Q_OBJECT
 Q_DECLARE_PRIVATE(QQuickCanvas)
 public:
+    enum CreateTextureOption {
+        TextureHasAlphaChannel  = 0x0001,
+        TextureHasMipmaps       = 0x0002,
+        TextureOwnsGLTexture    = 0x0004
+    };
+
+    Q_DECLARE_FLAGS(CreateTextureOptions, CreateTextureOption)
+
     QQuickCanvas(QWindow *parent = 0);
 
     virtual ~QQuickCanvas();
@@ -88,9 +97,23 @@ public:
 
     QDeclarativeIncubationController *incubationController() const;
 
+    // Scene graph specific functions
+    QSGTexture *createTextureFromImage(const QImage &image) const;
+    QSGTexture *createTextureFromId(uint id, const QSize &size, CreateTextureOptions options = CreateTextureOption(0)) const;
+
+    void setClearBeforeRendering(bool enabled);
+    bool clearBeforeRendering() const;
+
+    void setClearColor(const QColor &color);
+    QColor clearColor() const;
+
+    QOpenGLContext *openglContext() const;
+
 Q_SIGNALS:
     void frameSwapped();
     void sceneGraphInitialized();
+    void beforeRendering();
+    void afterRendering();
 
 protected:
     QQuickCanvas(QQuickCanvasPrivate &dd, QWindow *parent = 0);
index 702234d..fdfe091 100644 (file)
@@ -157,9 +157,12 @@ public:
     void updateEffectiveOpacityRoot(QQuickItem *, qreal);
     void updateDirtyNode(QQuickItem *);
 
+    QSGEngine *engine;
     QSGContext *context;
+    QColor clearColor;
 
     uint vsyncAnimations : 1;
+    uint clearBeforeRendering : 1;
 
     QQuickCanvasRenderLoop *thread;
     QSize widgetSize;
index 87beb72..50af0da 100644 (file)
@@ -53,8 +53,6 @@
 #include <private/qsgdistancefieldglyphnode_p.h>
 
 #include <private/qsgtexture_p.h>
-#include <qsgengine.h>
-
 #include <QGuiApplication>
 #include <QOpenGLContext>
 
@@ -108,8 +106,6 @@ public:
 
     QOpenGLContext *gl;
 
-    QSGEngine engine;
-
     QHash<QSGMaterialType *, QSGMaterialShader *> materials;
 
     QSGDistanceFieldGlyphCacheManager *distanceFieldCacheManager;
@@ -138,7 +134,6 @@ QSGContext::QSGContext(QObject *parent) :
     QObject(*(new QSGContextPrivate), parent)
 {
     Q_D(QSGContext);
-    d->engine.setContext(this);
 }
 
 
@@ -153,18 +148,6 @@ QSGContext::~QSGContext()
 }
 
 /*!
-    Returns the scene graph engine for this context.
-
-    The main purpose of the QSGEngine is to serve as a public API
-    to the QSGContext.
-
- */
-QSGEngine *QSGContext::engine() const
-{
-    return const_cast<QSGEngine *>(&d_func()->engine);
-}
-
-/*!
     Schedules the texture to be cleaned up on the rendering thread
     at a later time.
 
@@ -258,8 +241,6 @@ void QSGContext::renderNextFrame(QOpenGLFramebufferObject *fbo)
 {
     Q_D(QSGContext);
 
-    emit d->engine.beforeRendering();
-
     cleanupTextures();
 
     if (fbo) {
@@ -269,8 +250,6 @@ void QSGContext::renderNextFrame(QOpenGLFramebufferObject *fbo)
         d->renderer->renderScene();
     }
 
-    emit d->engine.afterRendering();
-
 }
 
 /*!
index 328b85e..41a9a4a 100644 (file)
@@ -89,7 +89,6 @@ public:
     void setRootNode(QSGRootNode *node);
     QSGRootNode *rootNode() const;
 
-    QSGEngine *engine() const;
     QOpenGLContext *glContext() const;
 
     bool isReady() const;
index 2b7e5be..88eeebc 100644 (file)
 
 #include "qsgengine.h"
 
-#include <private/qsgtexture_p.h>
-#include <private/qsgrenderer_p.h>
+#include <qquickcanvas.h>
+
+#include <private/qobject_p.h>
+#include <QtGui/QColor>
 
 QT_BEGIN_NAMESPACE
 
@@ -50,33 +52,18 @@ class QSGEnginePrivate : public QObjectPrivate
 {
 public:
     QSGEnginePrivate()
-        : context(0)
-        , clearBeforeRender(true)
+        : canvas(0)
     {
     }
 
-    QSGContext *context;
-
-    bool clearBeforeRender;
+    QQuickCanvas *canvas;
 };
 
-
 /*!
     \class QSGEngine
-    \brief The QSGEngine class serves as a generic entry point into scene graph specific APIs.
-
-    The QSGEngine class provides factory functions for creating textures. Though the user
-    can implement any type of texture using the abstract QSGTexture class, the QSGEngine
-    class provides some convenience for the default usecases. This also allows the scene
-    graph to apply hardware specific optimzations.
-
+    \deprecated
  */
 
-
-
-/*!
-    Constructs a new QSGengine
- */
 QSGEngine::QSGEngine(QObject *parent) :
     QObject(*(new QSGEnginePrivate), parent)
 {
@@ -87,165 +74,42 @@ QSGEngine::~QSGEngine()
 {
 }
 
-/*!
-    \enum TextureOption
-
-    The TextureOption enums are used to customize a texture is wrapped.
-
-    \value TextureHasAlphaChannel The texture has an alpha channel and should
-    be drawn using blending.
-
-    \value TextureHasMipmaps The texture has mipmaps and can be drawn with
-    mipmapping enabled.
-
-    \value TextureOwnsGLTexture The texture object owns the texture id and
-    will delete the GL texture when the texture object is deleted.
-
- */
-
-/*!
-    \fn void QSGEngine::beforeRendering()
-
-    This signal is emitted before the scene starts rendering.
-
-    Combined with the modes for clearing the background, this option
-    can be used to paint using raw GL under QML content.
 
-    The GL context used for rendering the scene graph will be bound
-    at this point.
-
-    Since this signal is emitted from the scene graph rendering thread, the receiver should
-    be on the scene graph thread or the connection should be Qt::DirectConnection.
-
-*/
-
-/*!
-    \fn void QSGEngine::afterRendering()
-
-    This signal is emitted after the scene has completed rendering, before swapbuffers is called.
-
-    This signal can be used to paint using raw GL on top of QML content,
-    or to do screen scraping of the current frame buffer.
-
-    The GL context used for rendering the scene graph will be bound at this point.
-
-    Since this signal is emitted from the scene graph rendering thread, the receiver should
-    be on the scene graph thread or the connection should be Qt::DirectConnection.
- */
-
-
-
-/*!
-    Sets weither the scene graph rendering of QML should clear the color buffer
-    before it starts rendering to \a enbled.
-
-    By disabling clearing of the color buffer, it is possible to do GL painting
-    under the scene graph.
-
-    The color buffer is cleared by default.
- */
+void QSGEngine::setCanvas(QQuickCanvas *canvas)
+{
+    d_func()->canvas = canvas;
+    connect(canvas, SIGNAL(afterRendering()), this, SIGNAL(afterRendering()));
+    connect(canvas, SIGNAL(beforeRendering()), this, SIGNAL(beforeRendering()));
+}
 
 void QSGEngine::setClearBeforeRendering(bool enabled)
 {
-    Q_D(QSGEngine);
-    d->clearBeforeRender = enabled;
-    if (d->clearBeforeRender) {
-        d->context->renderer()->setClearMode(QSGRenderer::ClearDepthBuffer
-                                             | QSGRenderer::ClearColorBuffer);
-    } else {
-        d->context->renderer()->setClearMode(QSGRenderer::ClearDepthBuffer);
-    }
+    d_func()->canvas->setClearBeforeRendering(enabled);
 }
 
-
-
-/*!
-    Returns weither clearing of the color buffer is done before rendering or not.
- */
-
 bool QSGEngine::clearBeforeRendering() const
 {
-    Q_D(const QSGEngine);
-    return d->clearBeforeRender;
+    return d_func()->canvas->clearBeforeRendering();
 }
 
-
-
-/*!
-    Creates a new QSGTexture from the supplied \a image. If the image has an
-    alpha channel, the corresponding texture will have an alpha channel.
-
-    The caller of the function is responsible for deleting the returned texture.
-
-    The actual GL texture will be deleted when the texture object is deleted.
- */
-
 QSGTexture *QSGEngine::createTextureFromImage(const QImage &image) const
 {
-    Q_D(const QSGEngine);
-    return d->context->createTexture(image);
+    return d_func()->canvas->createTextureFromImage(image);
 }
 
-
-
-/*!
-    Creates a new QSGTexture object from an existing GL texture \a id.
-
-    The caller of the function is responsible for deleting the returned texture.
-
-    Use \a options to customize the texture attributes.
- */
 QSGTexture *QSGEngine::createTextureFromId(uint id, const QSize &size, TextureOptions options) const
 {
-    QSGPlainTexture *texture = new QSGPlainTexture();
-    texture->setTextureId(id);
-    texture->setHasAlphaChannel(options & TextureHasAlphaChannel);
-    texture->setHasMipmaps(options & TextureHasMipmaps);
-    texture->setOwnsTexture(options & TextureOwnsGLTexture);
-    texture->setTextureSize(size);
-    return texture;
+    return d_func()->canvas->createTextureFromId(id, size, QQuickCanvas::CreateTextureOptions((int) options));
 }
 
-
-
-/*!
-    \internal
-
-    Sets the scene graph context of this engine to context.
-
-    The context will be set by the QSGcontext::initialize() function,
-    as part of constructing the engine object.
- */
-
-void QSGEngine::setContext(QSGContext *context)
-{
-    Q_D(QSGEngine);
-    d->context = context;
-}
-
-
-
-/*!
-    Sets the background color of the scene graph to \a color.
-
-    Changing the clear color has no effect when clearing before rendering is
-    disabled.
- */
-
 void QSGEngine::setClearColor(const QColor &color)
 {
-    d_func()->context->renderer()->setClearColor(color);
+    d_func()->canvas->setClearColor(color);
 }
 
-
-
-/*!
-    Returns the background color of the scene graph
- */
-
 QColor QSGEngine::clearColor() const
 {
-    return d_func()->context->renderer()->clearColor();
+    return d_func()->canvas->clearColor();
 }
 
 QT_END_NAMESPACE
index f9a0ea1..0cbbcdd 100644 (file)
@@ -54,7 +54,8 @@ QT_MODULE(Declarative)
 
 class QSGEnginePrivate;
 
-class QSGContext;
+class QQuickCanvas;
+
 class Q_DECLARATIVE_EXPORT QSGEngine : public QObject
 {
     Q_OBJECT
@@ -89,7 +90,8 @@ private:
 
     friend class QSGContext;
     friend class QSGContextPrivate;
-    void setContext(QSGContext *context);
+    friend class QQuickCanvasPrivate;
+    void setCanvas(QQuickCanvas *canvas);
 
 };