aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgcontextplugin.cpp
blob: 2956c8c438e22f2731f228a5c80b096b20713876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsgcontextplugin_p.h"
#include <QtQuick/private/qsgcontext_p.h>
#include <QtGui/qguiapplication.h>
#include <QtCore/private/qfactoryloader_p.h>
#include <QtCore/qlibraryinfo.h>

// Built-in adaptations
#include <QtQuick/private/qsgsoftwareadaptation_p.h>
#include <QtQuick/private/qsgdefaultcontext_p.h>

#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformintegration.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_INFO)

QSGContextPlugin::QSGContextPlugin(QObject *parent)
    : QObject(parent)
{
}

QSGContextPlugin::~QSGContextPlugin()
{
}

#if QT_CONFIG(library)
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
    (QSGContextFactoryInterface_iid, QLatin1String("/scenegraph")))
#endif

struct QSGAdaptationBackendData
{
    QSGAdaptationBackendData();
    ~QSGAdaptationBackendData();
    Q_DISABLE_COPY(QSGAdaptationBackendData)

    bool tried = false;
    QSGContextFactoryInterface *factory = nullptr;
    QString name;
    QSGContextFactoryInterface::Flags flags;

    QVector<QSGContextFactoryInterface *> builtIns;

    QString quickWindowBackendRequest;
};

QSGAdaptationBackendData::QSGAdaptationBackendData()
{
    // Fill in the table with the built-in adaptations.
    builtIns.append(new QSGSoftwareAdaptation);
}

QSGAdaptationBackendData::~QSGAdaptationBackendData()
{
    qDeleteAll(builtIns);
}

Q_GLOBAL_STATIC(QSGAdaptationBackendData, qsg_adaptation_data)

// This only works when the backend is loaded (contextFactory() was called),
// otherwise the return value is 0.
//
// Note that the default (OpenGL) implementation always results in 0, custom flags
// can only be returned from the other (either compiled-in or plugin-based) backends.
QSGContextFactoryInterface::Flags qsg_backend_flags()
{
    return qsg_adaptation_data()->flags;
}

QSGAdaptationBackendData *contextFactory()
{
    QSGAdaptationBackendData *backendData = qsg_adaptation_data();

    if (!backendData->tried) {
        backendData->tried = true;

        const QStringList args = QGuiApplication::arguments();
        QString requestedBackend = backendData->quickWindowBackendRequest; // empty or set via QQuickWindow::setBackend()

        for (int index = 0; index < args.count(); ++index) {
            if (args.at(index).startsWith(QLatin1String("--device="))) {
                requestedBackend = args.at(index).mid(9);
                break;
            }
        }

        if (requestedBackend.isEmpty())
            requestedBackend = qEnvironmentVariable("QMLSCENE_DEVICE");

        // A modern alternative. Scenegraph adaptations can represent backends
        // for different graphics APIs as well, instead of being specific to
        // some device or platform.
        if (requestedBackend.isEmpty())
            requestedBackend = qEnvironmentVariable("QT_QUICK_BACKEND");

        // If this platform does not support OpenGL, Vulkan, D3D11, or Metal, and no
        // backend has been set, default to the software renderer. We rely on the
        // static, build time flags only. This is to prevent the inevitable confusion
        // caused by run time hocus pocus. If one wants to use the software backend
        // in a GL or Vulkan capable Qt build (or on Windows or Apple platforms), it
        // has to be requested explicitly.
#if !QT_CONFIG(opengl) && !QT_CONFIG(vulkan) && !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) && !defined(Q_OS_IOS)
        if (requestedBackend.isEmpty())
            requestedBackend = QLatin1String("software");
#endif

        // This is handy if some of the logic above goes wrong and we select
        // e.g. the software backend when it is not desired.
        if (requestedBackend == QLatin1String("rhi"))
            requestedBackend.clear(); // empty = no custom backend to load

        if (!requestedBackend.isEmpty()) {
            qCDebug(QSG_LOG_INFO, "Loading backend %s", qUtf8Printable(requestedBackend));

            // First look for a built-in adaptation.
            for (QSGContextFactoryInterface *builtInBackend : qAsConst(backendData->builtIns)) {
                if (builtInBackend->keys().contains(requestedBackend)) {
                    backendData->factory = builtInBackend;
                    backendData->name = requestedBackend;
                    backendData->flags = backendData->factory->flags(requestedBackend);
                    break;
                }
            }

#if QT_CONFIG(library)
            // Then try the plugins.
            if (!backendData->factory) {
                const int index = loader()->indexOf(requestedBackend);
                if (index != -1)
                    backendData->factory = qobject_cast<QSGContextFactoryInterface*>(loader()->instance(index));
                if (backendData->factory) {
                    backendData->name = requestedBackend;
                    backendData->flags = backendData->factory->flags(requestedBackend);
                }
                if (!backendData->factory) {
                    qWarning("Could not create scene graph context for backend '%s'"
                             " - check that plugins are installed correctly in %s",
                             qPrintable(requestedBackend),
                             qPrintable(QLibraryInfo::path(QLibraryInfo::PluginsPath)));
                }
            }
#endif // library
        }
    }

    return backendData;
}



/*!
    \fn QSGContext *QSGContext::createDefaultContext()

    Creates a default scene graph context for the current hardware.
    This may load a device-specific plugin.
*/
QSGContext *QSGContext::createDefaultContext()
{
    QSGAdaptationBackendData *backendData = contextFactory();
    if (backendData->factory)
        return backendData->factory->create(backendData->name);
    return new QSGDefaultContext();
}



/*!
    Calls into the scene graph adaptation if available and creates a texture
    factory. The primary purpose of this function is to reimplement hardware
    specific asynchronous texture frameskip-less uploads that can happen on
    the image providers thread.
 */

QQuickTextureFactory *QSGContext::createTextureFactoryFromImage(const QImage &image)
{
    QSGAdaptationBackendData *backendData = contextFactory();
    if (backendData->factory)
        return backendData->factory->createTextureFactoryFromImage(image);
    return nullptr;
}


/*!
    Calls into the scene graph adaptation if available and creates a hardware
    specific window manager.
 */

QSGRenderLoop *QSGContext::createWindowManager()
{
    QSGAdaptationBackendData *backendData = contextFactory();
    if (backendData->factory)
        return backendData->factory->createWindowManager();
    return nullptr;
}

void QSGContext::setBackend(const QString &backend)
{
    QSGAdaptationBackendData *backendData = qsg_adaptation_data();
    if (backendData->tried)
        qWarning("Scenegraph already initialized, setBackend() request ignored");

    backendData->quickWindowBackendRequest = backend;
}

QString QSGContext::backend()
{
    QSGAdaptationBackendData *backendData = qsg_adaptation_data();
    if (backendData->tried)
        return backendData->name;

    return backendData->quickWindowBackendRequest;
}

QT_END_NAMESPACE

#include "moc_qsgcontextplugin_p.cpp"