1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsgengine_p.h"
41
42#include <QtQuick/qsgtexture.h>
43#include <private/qsgcontext_p.h>
44#include <private/qsgrenderer_p.h>
45#include <private/qsgplaintexture_p.h>
46
47#if QT_CONFIG(opengl)
48# include <QtGui/QOpenGLContext>
49# include <private/qsgdefaultrendercontext_p.h>
50#endif
51
52QT_BEGIN_NAMESPACE
53
54
55/*!
56 \class QSGEngine
57 \brief The QSGEngine class allows low level rendering of a scene graph.
58 \inmodule QtQuick
59 \since 5.4
60
61 A QSGEngine can be used to render a tree of QSGNode directly on a QWindow
62 or QOpenGLFramebufferObject without any integration with QML, QQuickWindow
63 or QQuickItem and the convenience that they provide.
64
65 This means that you must handle event propagation, animation timing,
66 and node lifetime yourself.
67
68 \note This class is for very low level access to an independent scene graph.
69 Most of the time you will instead want to subclass QQuickItem and insert
70 your QSGNode in a normal QtQuick scene by overriding QQuickItem::updatePaintNode().
71
72 \sa QSGAbstractRenderer
73 */
74
75/*!
76 \enum QSGEngine::CreateTextureOption
77
78 The CreateTextureOption enums are used to customize how a texture is wrapped.
79
80 \value TextureHasAlphaChannel The texture has an alpha channel and should
81 be drawn using blending.
82
83 \value TextureOwnsGLTexture The texture object owns the texture id and
84 will delete the GL texture when the texture object is deleted.
85
86 \value TextureCanUseAtlas The image can be uploaded into a texture atlas.
87
88 \value TextureIsOpaque The texture object is opaque.
89 */
90
91QSGEnginePrivate::QSGEnginePrivate()
92 : sgContext(QSGContext::createDefaultContext())
93 , sgRenderContext(sgContext.data()->createRenderContext())
94{
95}
96
97/*!
98 Constructs a new QSGEngine with its \a parent
99 */
100QSGEngine::QSGEngine(QObject *parent)
101 : QObject(*(new QSGEnginePrivate), parent)
102{
103}
104
105/*!
106 Destroys the engine
107 */
108QSGEngine::~QSGEngine()
109{
110}
111
112/*!
113 Initialize the engine with \a context.
114
115 \warning You have to make sure that you call
116 QOpenGLContext::makeCurrent() on \a context before calling this.
117 */
118void QSGEngine::initialize(QOpenGLContext *context)
119{
120 Q_D(QSGEngine);
121#if QT_CONFIG(opengl)
122 if (context && QOpenGLContext::currentContext() != context) {
123 qWarning("WARNING: The context must be current before calling QSGEngine::initialize.");
124 return;
125 }
126#endif
127 if (d->sgRenderContext && !d->sgRenderContext->isValid()) {
128 d->sgRenderContext->setAttachToGraphicsContext(false);
129#if QT_CONFIG(opengl)
130 QSGDefaultRenderContext *rc = qobject_cast<QSGDefaultRenderContext *>(d->sgRenderContext.data());
131 if (rc) {
132 QSGDefaultRenderContext::InitParams params;
133 params.sampleCount = qMax(1, context->format().samples());
134 params.openGLContext = context;
135 // leave the size hint and surface unset, we do not know, that's fine
136 rc->initialize(&params);
137 } else {
138 d->sgRenderContext->initialize(nullptr);
139 }
140#else
141 d->sgRenderContext->initialize(nullptr);
142#endif
143#if QT_CONFIG(opengl)
144 if (context)
145 connect(context, &QOpenGLContext::aboutToBeDestroyed, this, &QSGEngine::invalidate);
146#endif
147 }
148
149#if !QT_CONFIG(opengl)
150 Q_UNUSED(context);
151#endif
152}
153
154/*!
155 Invalidate the engine releasing its resources
156
157 You will have to call initialize() and createRenderer() if you
158 want to use it again.
159 */
160void QSGEngine::invalidate()
161{
162 Q_D(QSGEngine);
163 d->sgRenderContext->invalidate();
164}
165
166/*!
167 Returns a renderer that can be used to render a QSGNode tree
168
169 You call initialize() first with the QOpenGLContext that you
170 want to use with this renderer. This will return a null
171 renderer otherwise.
172 */
173QSGAbstractRenderer *QSGEngine::createRenderer() const
174{
175 Q_D(const QSGEngine);
176 if (!d->sgRenderContext->isValid())
177 return nullptr;
178
179 QSGRenderer *renderer = d->sgRenderContext->createRenderer();
180 renderer->setCustomRenderMode(qgetenv("QSG_VISUALIZE"));
181 return renderer;
182}
183
184/*!
185 Creates a texture using the data of \a image
186
187 Valid \a options are TextureCanUseAtlas and TextureIsOpaque.
188
189 The caller takes ownership of the texture and the
190 texture should only be used with this engine.
191
192 \sa createTextureFromId(), QSGSimpleTextureNode::setOwnsTexture(), QQuickWindow::createTextureFromImage()
193 */
194QSGTexture *QSGEngine::createTextureFromImage(const QImage &image, CreateTextureOptions options) const
195{
196 Q_D(const QSGEngine);
197 if (!d->sgRenderContext->isValid())
198 return nullptr;
199 uint flags = 0;
200 if (options & TextureCanUseAtlas) flags |= QSGRenderContext::CreateTexture_Atlas;
201 if (!(options & TextureIsOpaque)) flags |= QSGRenderContext::CreateTexture_Alpha;
202 return d->sgRenderContext->createTexture(image, flags);
203}
204
205/*!
206 Creates a texture object that wraps the GL texture \a id uploaded with \a size
207
208 Valid \a options are TextureHasAlphaChannel and TextureOwnsGLTexture
209
210 The caller takes ownership of the texture object and the
211 texture should only be used with this engine.
212
213 \sa createTextureFromImage(), QSGSimpleTextureNode::setOwnsTexture(), QQuickWindow::createTextureFromId()
214 */
215QSGTexture *QSGEngine::createTextureFromId(uint id, const QSize &size, CreateTextureOptions options) const
216{
217 Q_D(const QSGEngine);
218 if (d->sgRenderContext->isValid()) {
219 QSGPlainTexture *texture = new QSGPlainTexture();
220 texture->setTextureId(id);
221 texture->setHasAlphaChannel(options & TextureHasAlphaChannel);
222 texture->setOwnsTexture(options & TextureOwnsGLTexture);
223 texture->setTextureSize(size);
224 return texture;
225 }
226 return nullptr;
227}
228
229/*!
230 Returns the current renderer interface if there is one. Otherwise null is returned.
231
232 \sa QSGRenderNode, QSGRendererInterface
233 \since 5.8
234 */
235QSGRendererInterface *QSGEngine::rendererInterface() const
236{
237 Q_D(const QSGEngine);
238 return d->sgRenderContext->isValid()
239 ? d->sgRenderContext->sceneGraphContext()->rendererInterface(d->sgRenderContext.data())
240 : nullptr;
241}
242
243/*!
244 Creates a simple rectangle node. When the scenegraph is not initialized, the return value is null.
245
246 This is cross-backend alternative to constructing a QSGSimpleRectNode directly.
247
248 \since 5.8
249 \sa QSGRectangleNode
250 */
251QSGRectangleNode *QSGEngine::createRectangleNode() const
252{
253 Q_D(const QSGEngine);
254 return d->sgRenderContext->isValid() ? d->sgRenderContext->sceneGraphContext()->createRectangleNode() : nullptr;
255}
256
257/*!
258 Creates a simple image node. When the scenegraph is not initialized, the return value is null.
259
260 This is cross-backend alternative to constructing a QSGSimpleTextureNode directly.
261
262 \since 5.8
263 \sa QSGImageNode
264 */
265
266QSGImageNode *QSGEngine::createImageNode() const
267{
268 Q_D(const QSGEngine);
269 return d->sgRenderContext->isValid() ? d->sgRenderContext->sceneGraphContext()->createImageNode() : nullptr;
270}
271
272/*!
273 Creates a nine patch node. When the scenegraph is not initialized, the return value is null.
274
275 \since 5.8
276 */
277
278QSGNinePatchNode *QSGEngine::createNinePatchNode() const
279{
280 Q_D(const QSGEngine);
281 return d->sgRenderContext->isValid() ? d->sgRenderContext->sceneGraphContext()->createNinePatchNode() : nullptr;
282}
283
284QT_END_NAMESPACE
285
286#include "moc_qsgengine.cpp"
287