Track API change in QPlatformNativeInterface
[profile/ivi/qtbase.git] / src / plugins / gfxdrivers / eglnullws / eglnullwsscreen.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "eglnullwsscreen.h"
43 #include "eglnullwswindowsurface.h"
44 #include "eglnullwsscreenplugin.h"
45
46 #include <QHash>
47 #include <QDebug>
48
49 namespace
50 {
51     class EGLNullWSScreenSurfaceFunctions : public QGLScreenSurfaceFunctions
52     {
53     public:
54         virtual bool createNativeWindow(QWidget *, EGLNativeWindowType *native)
55             { *native = 0; return true; }
56     };
57 }
58
59 EGLNullWSScreen::EGLNullWSScreen(int displayId) : QGLScreen(displayId) {}
60
61 EGLNullWSScreen::~EGLNullWSScreen() {}
62
63 bool EGLNullWSScreen::initDevice()
64 {
65     setSurfaceFunctions(new EGLNullWSScreenSurfaceFunctions);
66     return true;
67 }
68
69 static const QHash<QString, QImage::Format> formatDictionary()
70 {
71     QHash<QString, QImage::Format> dictionary;
72     dictionary["rgb32"]     = QImage::Format_RGB32;
73     dictionary["argb32"]    = QImage::Format_ARGB32;
74     dictionary["rgb16"]     = QImage::Format_RGB16;
75     dictionary["rgb666"]    = QImage::Format_RGB666;
76     dictionary["rgb555"]    = QImage::Format_RGB555;
77     dictionary["rgb888"]    = QImage::Format_RGB888;
78     dictionary["rgb444"]    = QImage::Format_RGB444;
79     return dictionary;
80 }
81
82 static int depthForFormat(QImage::Format format)
83 {
84     switch (format) {
85     case QImage::Format_RGB32:  return 32;
86     case QImage::Format_ARGB32: return 32;
87     case QImage::Format_RGB16:  return 16;
88     case QImage::Format_RGB666: return 24;
89     case QImage::Format_RGB555: return 16;
90     case QImage::Format_RGB888: return 24;
91     case QImage::Format_RGB444: return 16;
92     default:
93         Q_ASSERT_X(false, "EGLNullWSScreen", "Unknown format");
94         return -1;
95     }
96 }
97
98 static void printHelp(const QHash<QString, QImage::Format> &formatDictionary)
99 {
100     QByteArray formatsBuf;
101     QTextStream(&formatsBuf) << QStringList(formatDictionary.keys()).join(", ");
102     qWarning(
103         "%s: Valid options are:\n"
104         "size=WIDTHxHEIGHT   Screen size reported by this driver\n"
105         "format=FORMAT       Screen format, where FORMAT is one of the following:\n"
106         "                      %s\n",
107         PluginName,
108         formatsBuf.constData());
109 }
110
111 bool EGLNullWSScreen::connect(const QString &displaySpec)
112 {
113     const QStringList args = displaySpec.section(':', 1).split(':', QString::SkipEmptyParts);
114     const QHash<QString, QImage::Format> formatDict = formatDictionary();
115     Q_FOREACH(const QString arg, args) {
116         const QString optionName = arg.section('=', 0, 0);
117         const QString optionArg = arg.section('=', 1);
118         if (optionName == QLatin1String("size")) {
119             w = optionArg.section('x', 0, 0).toInt();
120             h = optionArg.section('x', 1, 1).toInt();
121         } else if (optionName == QLatin1String("format")) {
122             if (formatDict.contains(optionArg))
123                 setPixelFormat(formatDict.value(optionArg));
124             else
125                 printHelp(formatDict);
126         } else {
127             printHelp(formatDict);
128         }
129     }
130
131     if (w == 0 || h == 0) {
132         w = 640;
133         h = 480;
134         qWarning("%s: Using default screen size %dx%d", PluginName, w, h);
135     }
136     dw = w;
137     dh = h;
138
139     if (pixelFormat() == QImage::Format_Invalid) {
140         qWarning("%s: Using default screen format argb32", PluginName);
141         setPixelFormat(QImage::Format_ARGB32);
142     }
143     d = depthForFormat(pixelFormat());
144
145     static const int Dpi = 120;
146     static const qreal ScalingFactor = static_cast<qreal>(25.4) / Dpi;
147     physWidth = qRound(dw * ScalingFactor);
148     physHeight = qRound(dh * ScalingFactor);
149
150     return true;
151 }
152
153 void EGLNullWSScreen::disconnect() {}
154
155 void EGLNullWSScreen::shutdownDevice() {}
156
157 void EGLNullWSScreen::setMode(int /*width*/, int /*height*/, int /*depth*/) {}
158
159 void EGLNullWSScreen::blank(bool /*on*/) {}
160
161 void EGLNullWSScreen::exposeRegion(QRegion /*r*/, int /*changing*/) {}
162
163 QWSWindowSurface* EGLNullWSScreen::createSurface(QWidget *widget) const
164 {
165     if (qobject_cast<QGLWidget*>(widget)) {
166         return new EGLNullWSWindowSurface(widget);
167     } else {
168         qWarning("%s: Creating non-GL surface", PluginName);
169         return QScreen::createSurface(widget);
170     }
171 }
172
173 QWSWindowSurface* EGLNullWSScreen::createSurface(const QString &key) const
174 {
175     if (key == QLatin1String("eglnullws")) {
176         return new EGLNullWSWindowSurface;
177     } else {
178         qWarning("%s: Creating non-GL surface", PluginName);
179         return QScreen::createSurface(key);
180     }
181 }