Cocoa: reimplement QPlatformBackingStore::scroll()
[profile/ivi/qtbase.git] / src / plugins / platforms / xcb / qxcbintegration.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 plugins 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 "qxcbintegration.h"
43 #include "qxcbconnection.h"
44 #include "qxcbscreen.h"
45 #include "qxcbwindow.h"
46 #include "qxcbbackingstore.h"
47 #include "qxcbnativeinterface.h"
48 #include "qxcbclipboard.h"
49 #include "qxcbdrag.h"
50 #include "qxcbsharedgraphicscache.h"
51
52 #include <xcb/xcb.h>
53
54 #include <QtPlatformSupport/private/qgenericunixeventdispatcher_p.h>
55 #include <QtPlatformSupport/private/qgenericunixfontdatabase_p.h>
56 #include <QtPlatformSupport/private/qgenericunixfontdatabase_p.h>
57 #include <QtPlatformSupport/private/qgenericunixservices_p.h>
58
59 #include <stdio.h>
60
61 //this has to be included before egl, since egl pulls in X headers
62 #include <QtGui/private/qguiapplication_p.h>
63
64 #ifdef XCB_USE_EGL
65 #include <EGL/egl.h>
66 #endif
67
68 #ifdef XCB_USE_XLIB
69 #include <X11/Xlib.h>
70 #endif
71
72 #include <private/qplatforminputcontextfactory_qpa_p.h>
73 #include <private/qgenericunixthemes_p.h>
74 #include <qplatforminputcontext_qpa.h>
75
76 #if defined(XCB_USE_GLX)
77 #include "qglxintegration.h"
78 #elif defined(XCB_USE_EGL)
79 #include "qxcbeglsurface.h"
80 #include <QtPlatformSupport/private/qeglplatformcontext_p.h>
81 #endif
82
83 #include <QtGui/QOpenGLContext>
84 #include <QtGui/QScreen>
85 #ifndef QT_NO_ACCESSIBILITY
86 #include <QtGui/QPlatformAccessibility>
87 #endif
88
89 QT_BEGIN_NAMESPACE
90
91 QXcbIntegration::QXcbIntegration(const QStringList &parameters)
92     : m_eventDispatcher(createUnixEventDispatcher()),
93       m_services(new QGenericUnixServices)
94 {
95     QGuiApplicationPrivate::instance()->setEventDispatcher(m_eventDispatcher);
96
97 #ifdef XCB_USE_XLIB
98     XInitThreads();
99 #endif
100     m_nativeInterface.reset(new QXcbNativeInterface);
101
102     m_connections << new QXcbConnection(m_nativeInterface.data());
103
104     for (int i = 0; i < parameters.size() - 1; i += 2) {
105 #ifdef Q_XCB_DEBUG
106         qDebug() << "QXcbIntegration: Connecting to additional display: " << parameters.at(i) << parameters.at(i+1);
107 #endif
108         QString display = parameters.at(i) + ':' + parameters.at(i+1);
109         m_connections << new QXcbConnection(m_nativeInterface.data(), display.toAscii().constData());
110     }
111
112     foreach (QXcbConnection *connection, m_connections)
113         foreach (QXcbScreen *screen, connection->screens())
114             screenAdded(screen);
115
116     m_fontDatabase.reset(new QGenericUnixFontDatabase());
117     m_inputContext.reset(QPlatformInputContextFactory::create());
118 #ifndef QT_NO_ACCESSIBILITY
119     m_accessibility.reset(new QPlatformAccessibility());
120 #endif
121
122 #if defined(QT_USE_XCB_SHARED_GRAPHICS_CACHE)
123     m_sharedGraphicsCache.reset(new QXcbSharedGraphicsCache);
124 #endif
125 }
126
127 QXcbIntegration::~QXcbIntegration()
128 {
129     qDeleteAll(m_connections);
130 }
131
132 QPlatformWindow *QXcbIntegration::createPlatformWindow(QWindow *window) const
133 {
134     return new QXcbWindow(window);
135 }
136
137 #if defined(XCB_USE_EGL)
138 class QEGLXcbPlatformContext : public QEGLPlatformContext
139 {
140 public:
141     QEGLXcbPlatformContext(const QSurfaceFormat &glFormat, QPlatformOpenGLContext *share,
142                            EGLDisplay display, QXcbConnection *c)
143         : QEGLPlatformContext(glFormat, share, display)
144         , m_connection(c)
145     {
146         Q_XCB_NOOP(m_connection);
147     }
148
149     void swapBuffers(QPlatformSurface *surface)
150     {
151         Q_XCB_NOOP(m_connection);
152         QEGLPlatformContext::swapBuffers(surface);
153         Q_XCB_NOOP(m_connection);
154     }
155
156     bool makeCurrent(QPlatformSurface *surface)
157     {
158         Q_XCB_NOOP(m_connection);
159         bool ret = QEGLPlatformContext::makeCurrent(surface);
160         Q_XCB_NOOP(m_connection);
161         return ret;
162     }
163
164     void doneCurrent()
165     {
166         Q_XCB_NOOP(m_connection);
167         QEGLPlatformContext::doneCurrent();
168         Q_XCB_NOOP(m_connection);
169     }
170
171     EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface)
172     {
173         return static_cast<QXcbWindow *>(surface)->eglSurface()->surface();
174     }
175
176 private:
177     QXcbConnection *m_connection;
178 };
179 #endif
180
181 #ifndef QT_NO_OPENGL
182 QPlatformOpenGLContext *QXcbIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
183 {
184     QXcbScreen *screen = static_cast<QXcbScreen *>(context->screen()->handle());
185 #if defined(XCB_USE_GLX)
186     return new QGLXContext(screen, context->format(), context->shareHandle());
187 #elif defined(XCB_USE_EGL)
188     return new QEGLXcbPlatformContext(context->format(), context->shareHandle(),
189         screen->connection()->egl_display(), screen->connection());
190 #elif defined(XCB_USE_DRI2)
191     return new QDri2Context(context->format(), context->shareHandle());
192 #endif
193     qWarning("QXcbIntegration: Cannot create platform OpenGL context, none of GLX, EGL, or DRI2 are enabled");
194     return 0;
195 }
196 #endif
197
198 QPlatformBackingStore *QXcbIntegration::createPlatformBackingStore(QWindow *window) const
199 {
200     return new QXcbBackingStore(window);
201 }
202
203 bool QXcbIntegration::hasCapability(QPlatformIntegration::Capability cap) const
204 {
205     switch (cap) {
206 #if defined(QT_USE_XCB_SHARED_GRAPHICS_CACHE)
207     case SharedGraphicsCache: return true;
208 #endif
209
210     case ThreadedPixmaps: return true;
211     case OpenGL: return true;
212     case ThreadedOpenGL: return false;
213     default: return QPlatformIntegration::hasCapability(cap);
214     }
215 }
216
217 QAbstractEventDispatcher *QXcbIntegration::guiThreadEventDispatcher() const
218 {
219     return m_eventDispatcher;
220 }
221
222 void QXcbIntegration::moveToScreen(QWindow *window, int screen)
223 {
224     Q_UNUSED(window);
225     Q_UNUSED(screen);
226 }
227
228 QPlatformFontDatabase *QXcbIntegration::fontDatabase() const
229 {
230     return m_fontDatabase.data();
231 }
232
233 QPlatformNativeInterface * QXcbIntegration::nativeInterface() const
234 {
235     return m_nativeInterface.data();
236 }
237
238 QPlatformClipboard *QXcbIntegration::clipboard() const
239 {
240     return m_connections.at(0)->clipboard();
241 }
242
243 QPlatformDrag *QXcbIntegration::drag() const
244 {
245     return m_connections.at(0)->drag();
246 }
247
248 QPlatformInputContext *QXcbIntegration::inputContext() const
249 {
250     return m_inputContext.data();
251 }
252
253 #ifndef QT_NO_ACCESSIBILITY
254 QPlatformAccessibility *QXcbIntegration::accessibility() const
255 {
256     return m_accessibility.data();
257 }
258 #endif
259
260 #if defined(QT_USE_XCB_SHARED_GRAPHICS_CACHE)
261 static bool sharedGraphicsCacheDisabled()
262 {
263     static const char *environmentVariable = "QT_DISABLE_SHARED_CACHE";
264     static bool cacheDisabled = !qgetenv(environmentVariable).isEmpty()
265             && qgetenv(environmentVariable).toInt() != 0;
266     return cacheDisabled;
267 }
268
269 QPlatformSharedGraphicsCache *QXcbIntegration::createPlatformSharedGraphicsCache(const char *cacheId) const
270 {
271     Q_UNUSED(cacheId);
272
273     if (sharedGraphicsCacheDisabled())
274         return 0;
275
276     return m_sharedGraphicsCache.data();
277 }
278 #endif
279
280 QPlatformServices *QXcbIntegration::services() const
281 {
282     return m_services.data();
283 }
284
285 QStringList QXcbIntegration::themeNames() const
286 {
287     return QGenericUnixTheme::themeNames();
288 }
289
290 QPlatformTheme *QXcbIntegration::createPlatformTheme(const QString &name) const
291 {
292     return QGenericUnixTheme::createUnixTheme(name);
293 }
294
295 QT_END_NAMESPACE