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 "qsgcontextplugin_p.h"
41#include <QtQuick/private/qsgcontext_p.h>
42#include <QtGui/qguiapplication.h>
43#include <QtCore/private/qfactoryloader_p.h>
44#include <QtCore/qlibraryinfo.h>
45
46// Built-in adaptations
47#include <QtQuick/private/qsgsoftwareadaptation_p.h>
48#if QT_CONFIG(opengl)
49#include <QtQuick/private/qsgdefaultcontext_p.h>
50#endif
51
52#include <QtGui/private/qguiapplication_p.h>
53#include <QtGui/qpa/qplatformintegration.h>
54
55QT_BEGIN_NAMESPACE
56
57Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_INFO)
58
59QSGContextPlugin::QSGContextPlugin(QObject *parent)
60 : QObject(parent)
61{
62}
63
64QSGContextPlugin::~QSGContextPlugin()
65{
66}
67
68#if QT_CONFIG(library)
69Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
70 (QSGContextFactoryInterface_iid, QLatin1String("/scenegraph")))
71#endif
72
73struct QSGAdaptationBackendData
74{
75 QSGAdaptationBackendData();
76 ~QSGAdaptationBackendData();
77 Q_DISABLE_COPY(QSGAdaptationBackendData)
78
79 bool tried = false;
80 QSGContextFactoryInterface *factory = nullptr;
81 QString name;
82 QSGContextFactoryInterface::Flags flags;
83
84 QVector<QSGContextFactoryInterface *> builtIns;
85
86 QString quickWindowBackendRequest;
87};
88
89QSGAdaptationBackendData::QSGAdaptationBackendData()
90 : flags(nullptr)
91{
92 // Fill in the table with the built-in adaptations.
93 builtIns.append(new QSGSoftwareAdaptation);
94}
95
96QSGAdaptationBackendData::~QSGAdaptationBackendData()
97{
98 qDeleteAll(builtIns);
99}
100
101Q_GLOBAL_STATIC(QSGAdaptationBackendData, qsg_adaptation_data)
102
103// This only works when the backend is loaded (contextFactory() was called),
104// otherwise the return value is 0.
105//
106// Note that the default (OpenGL) implementation always results in 0, custom flags
107// can only be returned from the other (either compiled-in or plugin-based) backends.
108QSGContextFactoryInterface::Flags qsg_backend_flags()
109{
110 return qsg_adaptation_data()->flags;
111}
112
113QSGAdaptationBackendData *contextFactory()
114{
115 QSGAdaptationBackendData *backendData = qsg_adaptation_data();
116
117 if (!backendData->tried) {
118 backendData->tried = true;
119
120 const QStringList args = QGuiApplication::arguments();
121 QString requestedBackend = backendData->quickWindowBackendRequest; // empty or set via QQuickWindow::setBackend()
122
123 for (int index = 0; index < args.count(); ++index) {
124 if (args.at(index).startsWith(QLatin1String("--device="))) {
125 requestedBackend = args.at(index).mid(9);
126 break;
127 }
128 }
129
130 if (requestedBackend.isEmpty())
131 requestedBackend = qEnvironmentVariable("QMLSCENE_DEVICE");
132
133 // A modern alternative. Scenegraph adaptations can represent backends
134 // for different graphics APIs as well, instead of being specific to
135 // some device or platform.
136 if (requestedBackend.isEmpty())
137 requestedBackend = qEnvironmentVariable("QT_QUICK_BACKEND");
138
139 // If this platform does not support OpenGL, and no backend has been set
140 // default to the software renderer
141 if (requestedBackend.isEmpty()
142 && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)) {
143 requestedBackend = QString::fromLocal8Bit("software");
144 }
145
146 if (!requestedBackend.isEmpty()) {
147 qCDebug(QSG_LOG_INFO, "Loading backend %s", qUtf8Printable(requestedBackend));
148
149 // First look for a built-in adaptation.
150 for (QSGContextFactoryInterface *builtInBackend : qAsConst(backendData->builtIns)) {
151 if (builtInBackend->keys().contains(requestedBackend)) {
152 backendData->factory = builtInBackend;
153 backendData->name = requestedBackend;
154 backendData->flags = backendData->factory->flags(requestedBackend);
155 break;
156 }
157 }
158
159#if QT_CONFIG(library)
160 // Then try the plugins.
161 if (!backendData->factory) {
162 const int index = loader()->indexOf(requestedBackend);
163 if (index != -1)
164 backendData->factory = qobject_cast<QSGContextFactoryInterface*>(loader()->instance(index));
165 if (backendData->factory) {
166 backendData->name = requestedBackend;
167 backendData->flags = backendData->factory->flags(requestedBackend);
168 }
169 if (!backendData->factory) {
170 qWarning("Could not create scene graph context for backend '%s'"
171 " - check that plugins are installed correctly in %s",
172 qPrintable(requestedBackend),
173 qPrintable(QLibraryInfo::location(QLibraryInfo::PluginsPath)));
174 }
175 }
176#endif // library
177 }
178 }
179
180 return backendData;
181}
182
183
184
185/*!
186 \fn QSGContext *QSGContext::createDefaultContext()
187
188 Creates a default scene graph context for the current hardware.
189 This may load a device-specific plugin.
190*/
191QSGContext *QSGContext::createDefaultContext()
192{
193 QSGAdaptationBackendData *backendData = contextFactory();
194 if (backendData->factory)
195 return backendData->factory->create(backendData->name);
196#if QT_CONFIG(opengl)
197 return new QSGDefaultContext();
198#else
199 return nullptr;
200#endif
201}
202
203
204
205/*!
206 Calls into the scene graph adaptation if available and creates a texture
207 factory. The primary purpose of this function is to reimplement hardware
208 specific asynchronous texture frameskip-less uploads that can happen on
209 the image providers thread.
210 */
211
212QQuickTextureFactory *QSGContext::createTextureFactoryFromImage(const QImage &image)
213{
214 QSGAdaptationBackendData *backendData = contextFactory();
215 if (backendData->factory)
216 return backendData->factory->createTextureFactoryFromImage(image);
217 return nullptr;
218}
219
220
221/*!
222 Calls into the scene graph adaptation if available and creates a hardware
223 specific window manager.
224 */
225
226QSGRenderLoop *QSGContext::createWindowManager()
227{
228 QSGAdaptationBackendData *backendData = contextFactory();
229 if (backendData->factory)
230 return backendData->factory->createWindowManager();
231 return nullptr;
232}
233
234void QSGContext::setBackend(const QString &backend)
235{
236 QSGAdaptationBackendData *backendData = qsg_adaptation_data();
237 if (backendData->tried)
238 qWarning("Scenegraph already initialized, setBackend() request ignored");
239
240 backendData->quickWindowBackendRequest = backend;
241}
242
243QString QSGContext::backend()
244{
245 QSGAdaptationBackendData *backendData = qsg_adaptation_data();
246 if (backendData->tried)
247 return backendData->name;
248
249 return backendData->quickWindowBackendRequest;
250}
251
252QT_END_NAMESPACE
253
254#include "moc_qsgcontextplugin_p.cpp"
255