7467448dbd0ab966ef68c48ece01c9d973d75fc6
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgcontextplugin.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
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         delete factory;
75     }
76
77     bool tried;
78     QSGContextFactoryInterface *factory;
79     QString deviceName;
80 };
81
82 QThreadStorage<QSGAdaptionPluginData> qsg_plugin_data;
83
84
85 QSGAdaptionPluginData *contextFactory()
86 {
87     QSGAdaptionPluginData &plugin = qsg_plugin_data.localData();
88     if (!plugin.tried) {
89         plugin.tried = true;
90         const QStringList args = QGuiApplication::arguments();
91         QString device;
92         for (int index = 0; index < args.count(); ++index) {
93             if (args.at(index).startsWith(QLatin1String("--device="))) {
94                 device = args.at(index).mid(9);
95                 break;
96             }
97         }
98         if (device.isEmpty())
99             device = QString::fromLocal8Bit(qgetenv("QMLSCENE_DEVICE"));
100
101 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
102         if (!device.isEmpty()) {
103             plugin.factory = qobject_cast<QSGContextFactoryInterface*>(loader()->instance(device));
104             plugin.deviceName = device;
105         }
106 #ifndef QT_NO_DEBUG
107         if (!device.isEmpty()) {
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 #endif // QT_NO_LIBRARY || QT_NO_SETTINGS
116     }
117     return &plugin;
118 }
119
120
121
122 /*!
123     \fn QSGContext *QSGContext::createDefaultContext()
124
125     Creates a default scene graph context for the current hardware.
126     This may load a device-specific plugin.
127 */
128 QSGContext *QSGContext::createDefaultContext()
129 {
130     QSGAdaptionPluginData *plugin = contextFactory();
131     if (plugin->factory)
132         return plugin->factory->create(plugin->deviceName);
133     return new QSGContext();
134 }
135
136
137
138 /*!
139     \fn QDeclarativeTextureFactory *createTextureFactoryFromImage(const QImage &image)
140
141     Calls into the scene graph adaptation if available and creates a texture
142     factory. The primary purpose of this function is to reimplement hardware
143     specific asynchronous texture frameskip-less uploads that can happen on
144     the image providers thread.
145  */
146
147 QDeclarativeTextureFactory *QSGContext::createTextureFactoryFromImage(const QImage &image)
148 {
149     QSGAdaptionPluginData *plugin = contextFactory();
150     if (plugin->factory)
151         return plugin->factory->createTextureFactoryFromImage(image);
152     return 0;
153 }
154
155
156
157 QT_END_NAMESPACE