From 742a7a0bd18d4ec23288b132a0e190235bff0547 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 18 Dec 2013 12:24:56 +0100 Subject: Introducing QQuickWidget Renders into an FBO, and provides a texture that is composed by the QPA/widget kernel compositor. Also introducing QQuickRenderControl, which is private API for now. Change-Id: I710c16e1506124a17f91e87344496471803a448b Reviewed-by: Gunnar Sletta --- src/quick/items/qquickrendercontrol.cpp | 224 ++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 src/quick/items/qquickrendercontrol.cpp (limited to 'src/quick/items/qquickrendercontrol.cpp') diff --git a/src/quick/items/qquickrendercontrol.cpp b/src/quick/items/qquickrendercontrol.cpp new file mode 100644 index 0000000000..7e4335ba63 --- /dev/null +++ b/src/quick/items/qquickrendercontrol.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQuick 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 "qquickrendercontrol_p.h" + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha); + +class QQuickRenderControlPrivate : public QObjectPrivate +{ +public: + QQuickRenderControlPrivate() + : window(0) + { + sg = QSGContext::createDefaultContext(); + rc = new QSGRenderContext(sg); + } + + ~QQuickRenderControlPrivate() + { + delete rc; + delete sg; + } + + QQuickWindow *window; + QSGContext *sg; + QSGRenderContext *rc; +}; + +/*! + \class QQuickRenderControl + \brief The QQuickRenderControl class provides a mechanism for rendering the Qt Quick scenegraph. + + \internal + + \inmodule QtQuick +*/ + +QQuickRenderControl::QQuickRenderControl() + : QObject(*(new QQuickRenderControlPrivate), 0) +{ +} + +QQuickRenderControl::~QQuickRenderControl() +{ +} + +void QQuickRenderControl::windowDestroyed() +{ + Q_D(QQuickRenderControl); + if (d->window == 0) { + d->rc->invalidate(); + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + } +} + +void QQuickRenderControl::initialize(QOpenGLContext *gl) +{ + Q_D(QQuickRenderControl); + if (!d->window) + return; + bool current = gl->makeCurrent(d->window); + if (current) + QQuickWindowPrivate::get(d->window)->context->initialize(gl); +} + +void QQuickRenderControl::invalidate() +{ + Q_D(QQuickRenderControl); + QQuickWindowPrivate::get(d->window)->context->invalidate(); +} + +/*! + This function should be called as late as possible before + sync(). In a threaded scenario, rendering can happen in parallel with this function. + */ +void QQuickRenderControl::polishItems() +{ + Q_D(QQuickRenderControl); + if (!d->window || !QQuickWindowPrivate::get(d->window)->isRenderable()) + return; + + QQuickWindowPrivate *cd = QQuickWindowPrivate::get(d->window); + cd->polishItems(); +} + +/*! + Synchronize GUI and scenegraph. Returns true if the scene graph was changed. + + This function is a synchronization point. Rendering can not happen in parallel. + */ +bool QQuickRenderControl::sync() +{ + Q_D(QQuickRenderControl); + if (!d->window || !QQuickWindowPrivate::get(d->window)->isRenderable()) + return false; + + QQuickWindowPrivate *cd = QQuickWindowPrivate::get(d->window); + cd->syncSceneGraph(); + + // TODO: find out if the sync actually caused a scenegraph update. + return true; +} + +/*! + Render the scenegraph using the current context. + */ +void QQuickRenderControl::render() +{ + Q_D(QQuickRenderControl); + if (!d->window || !QQuickWindowPrivate::get(d->window)->isRenderable()) + return; + + QQuickWindowPrivate *cd = QQuickWindowPrivate::get(d->window); + cd->renderSceneGraph(d->window->size()); +} + + +/*! + \fn void QQuickRenderControl::renderRequested() + + This signal is emitted when the scene graph needs to be rendered. It is not necessary to call sync(). +*/ + +/*! + \fn void QQuickRenderControl::sceneChanged() + + This signal is emitted when the scene graph is updated, meaning that + polishItems() and sync() needs to be called. If sync() returns + true, then render() needs to be called. +*/ + + +QImage QQuickRenderControl::grab() +{ + Q_D(QQuickRenderControl); + if (!d->window) + return QImage(); + + render(); + QImage grabContent = qt_gl_read_framebuffer(d->window->size(), false, false); + return grabContent; +} + +QSGContext *QQuickRenderControl::sceneGraphContext() const +{ + Q_D(const QQuickRenderControl); + return d->sg; +} + +QSGRenderContext *QQuickRenderControl::renderContext(QSGContext *) const +{ + Q_D(const QQuickRenderControl); + return d->rc; +} + +void QQuickRenderControl::setWindow(QQuickWindow *window) +{ + Q_D(QQuickRenderControl); + d->window = window; +} + +QQuickWindow *QQuickRenderControl::window() const +{ + Q_D(const QQuickRenderControl); + return d->window; +} + +QT_END_NAMESPACE -- cgit v1.2.3