Cocoa: Fix qmlscene flicker on startup.
[profile/ivi/qtbase.git] / src / plugins / platforms / wayland / qwaylandwindow.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 config.tests 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 "qwaylandwindow.h"
43
44 #include "qwaylandbuffer.h"
45 #include "qwaylanddisplay.h"
46 #include "qwaylandinputdevice.h"
47 #include "qwaylandscreen.h"
48
49 #include <QtGui/QWindow>
50
51 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
52 #include "windowmanager_integration/qwaylandwindowmanagerintegration.h"
53 #endif
54
55 #include <QCoreApplication>
56 #include <QtGui/QWindowSystemInterface>
57
58 #include <QDebug>
59
60 QWaylandWindow::QWaylandWindow(QWindow *window)
61     : QPlatformWindow(window)
62     , mSurface(0)
63     , mDisplay(QWaylandScreen::waylandScreenFromWindow(window)->display())
64     , mBuffer(0)
65     , mWaitingForFrameSync(false)
66 {
67     static WId id = 1;
68     mWindowId = id++;
69
70 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
71         mDisplay->windowManagerIntegration()->mapClientToProcess(qApp->applicationPid());
72         mDisplay->windowManagerIntegration()->authenticateWithToken();
73 #endif
74 }
75
76 QWaylandWindow::~QWaylandWindow()
77 {
78     if (mSurface)
79         wl_surface_destroy(mSurface);
80
81     QList<QWaylandInputDevice *> inputDevices = mDisplay->inputDevices();
82     for (int i = 0; i < inputDevices.size(); ++i)
83         inputDevices.at(i)->handleWindowDestroyed(this);
84 }
85
86 WId QWaylandWindow::winId() const
87 {
88     return mWindowId;
89 }
90
91 void QWaylandWindow::setParent(const QPlatformWindow *parent)
92 {
93     Q_UNUSED(parent);
94     qWarning("Sub window is not supported");
95 }
96
97 void QWaylandWindow::setVisible(bool visible)
98 {
99     if (!mSurface && visible) {
100         mSurface = mDisplay->createSurface(this);
101         newSurfaceCreated();
102     }
103
104     if (!visible) {
105         wl_surface_destroy(mSurface);
106         mSurface = NULL;
107     }
108 }
109
110 void QWaylandWindow::configure(uint32_t time, uint32_t edges,
111                                int32_t x, int32_t y,
112                                int32_t width, int32_t height)
113 {
114     Q_UNUSED(time);
115     Q_UNUSED(edges);
116
117     QRect geometry = QRect(x, y, width, height);
118     setGeometry(geometry);
119
120     QWindowSystemInterface::handleGeometryChange(window(), geometry);
121 }
122
123 void QWaylandWindow::attach(QWaylandBuffer *buffer)
124 {
125     mBuffer = buffer;
126     if (mSurface) {
127         wl_surface_attach(mSurface, buffer->buffer(),0,0);
128         QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
129     }
130 }
131
132 void QWaylandWindow::damage(const QRect &rect)
133 {
134     //We have to do sync stuff before calling damage, or we might
135     //get a frame callback before we get the timestamp
136     if (!mWaitingForFrameSync) {
137         mDisplay->frameCallback(QWaylandWindow::frameCallback, mSurface, this);
138         mWaitingForFrameSync = true;
139     }
140
141     wl_surface_damage(mSurface,
142                       rect.x(), rect.y(), rect.width(), rect.height());
143 }
144
145 void QWaylandWindow::newSurfaceCreated()
146 {
147     if (mBuffer) {
148         wl_surface_attach(mSurface,mBuffer->buffer(),0,0);
149         // do not damage the surface here, as this leads to graphical corruptions in the compositor until
150         // the first frame has been rendered
151     }
152 }
153
154 void QWaylandWindow::frameCallback(struct wl_surface *surface, void *data, uint32_t time)
155 {
156     Q_UNUSED(time);
157     Q_UNUSED(surface);
158     QWaylandWindow *self = static_cast<QWaylandWindow*>(data);
159     self->mWaitingForFrameSync = false;
160 }
161
162 void QWaylandWindow::waitForFrameSync()
163 {
164     mDisplay->flushRequests();
165     while (mWaitingForFrameSync)
166         mDisplay->blockingReadEvents();
167 }