Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgcontextplugin.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgcontextplugin_p.h"
43 #include <QtQuick/private/qsgcontext_p.h>
44 #include <QtGui/qguiapplication.h>
45 #include <QtCore/private/qfactoryloader_p.h>
46 #include <QtCore/qlibraryinfo.h>
47
48 QT_BEGIN_NAMESPACE
49
50 QSGContextPlugin::QSGContextPlugin(QObject *parent)
51     : QObject(parent)
52 {
53 }
54
55 QSGContextPlugin::~QSGContextPlugin()
56 {
57 }
58
59 #ifndef QT_NO_LIBRARY
60 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
61     (QSGContextFactoryInterface_iid, QLatin1String("/scenegraph")))
62 #endif
63
64 struct QSGAdaptionPluginData
65 {
66     QSGAdaptionPluginData()
67         : tried(false)
68         , factory(0)
69     {
70     }
71
72     ~QSGAdaptionPluginData()
73     {
74     }
75
76     bool tried;
77     QSGContextFactoryInterface *factory;
78     QString deviceName;
79 };
80
81 Q_GLOBAL_STATIC(QSGAdaptionPluginData, qsg_adaptation_data)
82
83 QSGAdaptionPluginData *contextFactory()
84 {
85     QSGAdaptionPluginData *plugin = qsg_adaptation_data();
86     if (!plugin->tried) {
87
88         plugin->tried = true;
89         const QStringList args = QGuiApplication::arguments();
90         QString device;
91         for (int index = 0; index < args.count(); ++index) {
92             if (args.at(index).startsWith(QLatin1String("--device="))) {
93                 device = args.at(index).mid(9);
94                 break;
95             }
96         }
97         if (device.isEmpty())
98             device = QString::fromLocal8Bit(qgetenv("QMLSCENE_DEVICE"));
99
100 #ifndef QT_NO_LIBRARY
101         if (!device.isEmpty()) {
102             const int index = loader()->indexOf(device);
103             if (index != -1)
104                 plugin->factory = qobject_cast<QSGContextFactoryInterface*>(loader()->instance(index));
105             plugin->deviceName = device;
106 #ifndef QT_NO_DEBUG
107             if (!plugin->factory) {
108                 qWarning("Could not create scene graph context for device '%s'"
109                          " - check that plugins are installed correctly in %s",
110                          qPrintable(device),
111                          qPrintable(QLibraryInfo::location(QLibraryInfo::PluginsPath)));
112             }
113 #endif
114         }
115
116 #endif // QT_NO_LIBRARY
117     }
118     return plugin;
119 }
120
121
122
123 /*!
124     \fn QSGContext *QSGContext::createDefaultContext()
125
126     Creates a default scene graph context for the current hardware.
127     This may load a device-specific plugin.
128 */
129 QSGContext *QSGContext::createDefaultContext()
130 {
131     QSGAdaptionPluginData *plugin = contextFactory();
132     if (plugin->factory)
133         return plugin->factory->create(plugin->deviceName);
134     return new QSGContext();
135 }
136
137
138
139 /*!
140     \fn QQuickTextureFactory *createTextureFactoryFromImage(const QImage &image)
141
142     Calls into the scene graph adaptation if available and creates a texture
143     factory. The primary purpose of this function is to reimplement hardware
144     specific asynchronous texture frameskip-less uploads that can happen on
145     the image providers thread.
146  */
147
148 QQuickTextureFactory *QSGContext::createTextureFactoryFromImage(const QImage &image)
149 {
150     QSGAdaptionPluginData *plugin = contextFactory();
151     if (plugin->factory)
152         return plugin->factory->createTextureFactoryFromImage(image);
153     return 0;
154 }
155
156
157 /*!
158     \fn QQuickWindowManager *createWindowManager()
159
160     Calls into the scene graph adaptation if available and creates a hardware
161     specific window manager.
162  */
163
164 QQuickWindowManager *QSGContext::createWindowManager()
165 {
166     QSGAdaptionPluginData *plugin = contextFactory();
167     if (plugin->factory)
168         return plugin->factory->createWindowManager();
169     return 0;
170 }
171
172
173
174
175
176 QT_END_NAMESPACE