summaryrefslogtreecommitdiffstats
path: root/examples/qwindow-compositor/qwindowcompositor.cpp
blob: b6ad9ca33578c66773074603760c52a85235aa9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "qwindowcompositor.h"
#include <QMouseEvent>
#include <QKeyEvent>
#include <QTouchEvent>

QWindowCompositor::QWindowCompositor(QOpenGLWindow *window)
    : WaylandCompositor(window)
    , m_window(window)
    , m_textureBlitter(0)
    , m_renderScheduler(this)
    , m_draggingWindow(0)
    , m_dragKeyIsPressed(false)
{
    enableSubSurfaceExtension();
    m_window->makeCurrent();

    m_textureCache = new QOpenGLTextureCache(m_window->context());
    m_textureBlitter = new TextureBlitter();
    m_backgroundImage = QImage(QLatin1String(":/background.jpg"));
    m_renderScheduler.setSingleShot(true);
    connect(&m_renderScheduler,SIGNAL(timeout()),this,SLOT(render()));



    glGenFramebuffers(1,&m_surface_fbo);

    window->installEventFilter(this);

    setRetainedSelectionEnabled(true);

    m_renderScheduler.start(0);
}

void QWindowCompositor::surfaceDestroyed(QObject *object)
{
    WaylandSurface *surface = static_cast<WaylandSurface *>(object);
    m_surfaces.removeOne(surface);
    if (inputFocus() == surface || !inputFocus()) // typically reset to 0 already in Compositor::surfaceDestroyed()
        setInputFocus(m_surfaces.isEmpty() ? 0 : m_surfaces.last());
    m_renderScheduler.start(0);
}

void QWindowCompositor::surfaceMapped()
{
    WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
    QPoint pos;
    if (!m_surfaces.contains(surface)) {
        uint px = 1 + (qrand() % (m_window->width() - surface->geometry().size().width() - 2));
        uint py = 1 + (qrand() % (m_window->height() - surface->geometry().size().height() - 2));
        pos = QPoint(px, py);
        surface->setGeometry(QRect(pos, surface->geometry().size()));
    } else {
        surface->setGeometry(QRect(window()->geometry().topLeft(),surface->geometry().size()));
        m_surfaces.removeOne(surface);
    }
    m_surfaces.append(surface);
    setInputFocus(surface);
    m_renderScheduler.start(0);
}

void QWindowCompositor::surfaceDamaged(const QRect &rect)
{
    WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
    surfaceDamaged(surface, rect);
}

void QWindowCompositor::surfaceDamaged(WaylandSurface *surface, const QRect &rect)
{
    Q_UNUSED(surface)
    Q_UNUSED(rect)
    m_renderScheduler.start(0);
}

void QWindowCompositor::surfaceCreated(WaylandSurface *surface)
{
    connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
    connect(surface, SIGNAL(mapped()), this, SLOT(surfaceMapped()));
    connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
    m_renderScheduler.start(0);
}

QPointF QWindowCompositor::toSurface(WaylandSurface *surface, const QPointF &pos) const
{
    return pos - surface->geometry().topLeft();
}

WaylandSurface *QWindowCompositor::surfaceAt(const QPoint &point, QPoint *local)
{
    for (int i = m_surfaces.size() - 1; i >= 0; --i) {
        if (m_surfaces.at(i)->geometry().contains(point)) {
            if (local)
                *local = toSurface(m_surfaces.at(i), point).toPoint();
            return m_surfaces.at(i);
        }
    }
    return 0;
}

GLuint QWindowCompositor::composeSurface(WaylandSurface *surface) {
    GLuint texture = 0;

    glBindFramebuffer(GL_FRAMEBUFFER, m_surface_fbo);

    if (surface->type() == WaylandSurface::Shm) {
        texture = m_textureCache->bindTexture(QOpenGLContext::currentContext(),surface->image());
    } else {
        texture = surface->texture(QOpenGLContext::currentContext());
    }
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                           GL_TEXTURE_2D, texture, 0);
    paintChildren(surface,surface);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                           GL_TEXTURE_2D,0, 0);

    glBindFramebuffer(GL_FRAMEBUFFER,0);
    return texture;
}

void QWindowCompositor::paintChildren(WaylandSurface *surface, WaylandSurface *window) {

    if (surface->subSurfaces().size() == 0)
        return;

    QLinkedListIterator<WaylandSurface *> i(surface->subSurfaces());
    while (i.hasNext()) {
        WaylandSurface *subSurface = i.next();
        QPoint p = subSurface->mapTo(window,QPoint(0,0));
        QRect geo = subSurface->geometry();
        geo.moveTo(p);
        if (geo.isValid()) {
            GLuint texture = 0;
            if (subSurface->type() == WaylandSurface::Texture) {
                texture = subSurface->texture(QOpenGLContext::currentContext());
            } else if (surface->type() == WaylandSurface::Shm ) {
                texture = m_textureCache->bindTexture(QOpenGLContext::currentContext(),surface->image());
            }
            qDebug() << "window geo is" << window->geometry().size();
            m_textureBlitter->drawTexture(texture,geo,window->geometry().size(),0,window->isYInverted(),subSurface->isYInverted());
        }
        paintChildren(subSurface,window);
    }
}


void QWindowCompositor::render()
{
    m_window->makeCurrent();
    m_backgroundTexture = m_textureCache->bindTexture(QOpenGLContext::currentContext(),m_backgroundImage);

    m_textureBlitter->bind();
    //Draw the background Image texture
    int w = m_window->width();
    int h = m_window->height();
    QSize imageSize = m_backgroundImage.size();
    for (int y = 0; y < h; y += imageSize.height()) {
        for (int x = 0; x < w; x += imageSize.width()) {
            m_textureBlitter->drawTexture(m_backgroundTexture,QRect(QPoint(x, y),imageSize),window()->size(), 0,true,true);
        }
    }

    foreach (WaylandSurface *surface, m_surfaces) {
        GLuint texture = composeSurface(surface);
        m_textureBlitter->drawTexture(texture,surface->geometry(),m_window->size(),0,false,surface->isYInverted());
    }

    m_textureBlitter->release();
    frameFinished();
    glFinish();

    m_window->swapBuffers();
}

bool QWindowCompositor::eventFilter(QObject *obj, QEvent *event)
{
    if (obj != m_window)
        return false;

    switch (event->type()) {
    case QEvent::Expose:
        m_renderScheduler.start(0);
        break;
    case QEvent::MouseButtonPress: {
        QPoint local;
        QMouseEvent *me = static_cast<QMouseEvent *>(event);
        WaylandSurface *targetSurface = surfaceAt(me->pos(), &local);
        if (targetSurface) {
            if (m_dragKeyIsPressed) {
               m_draggingWindow = targetSurface;
               m_drag_diff = local;
            } else {
                if (inputFocus() != targetSurface) {
                    setInputFocus(targetSurface);
                    m_surfaces.removeOne(targetSurface);
                    m_surfaces.append(targetSurface);
                    m_renderScheduler.start(0);
                }
                targetSurface->sendMousePressEvent(local, me->button());
            }
        }
        break;
    }
    case QEvent::MouseButtonRelease: {
        WaylandSurface *targetSurface = inputFocus();
        if (m_draggingWindow) {
            m_draggingWindow = 0;
            m_drag_diff = QPoint();
        } else  if (targetSurface) {
            QMouseEvent *me = static_cast<QMouseEvent *>(event);
            targetSurface->sendMouseReleaseEvent(toSurface(targetSurface, me->pos()).toPoint(), me->button());
        }
        break;
    }
    case QEvent::MouseMove: {
        QMouseEvent *me = static_cast<QMouseEvent *>(event);
        if (m_draggingWindow) {
            QRect geo = m_draggingWindow->geometry();
            geo.moveTopLeft(me->pos() - m_drag_diff);
            m_draggingWindow->setGeometry(geo);
            m_renderScheduler.start(0);
        } else {
            QPoint local;
            WaylandSurface *targetSurface = surfaceAt(me->pos(), &local);
            if (targetSurface) {
                targetSurface->sendMouseMoveEvent(local);
            }
        }
    }
    case QEvent::KeyPress: {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        qDebug() << ke->key();
        if (ke->key() == Qt::Key_Meta || ke->key() == Qt::Key_Super_L) {
            m_dragKeyIsPressed = true;
        }
        WaylandSurface *targetSurface = inputFocus();
        if (targetSurface)
            targetSurface->sendKeyPressEvent(ke->nativeScanCode());
        break;
    }
    case QEvent::KeyRelease: {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Meta || ke->key() == Qt::Key_Super_L) {
            m_dragKeyIsPressed = false;
        }
        WaylandSurface *targetSurface = inputFocus();
        if (targetSurface)
            targetSurface->sendKeyReleaseEvent(ke->nativeScanCode());
        break;
    }
    case QEvent::TouchBegin:
    case QEvent::TouchUpdate:
    case QEvent::TouchEnd:
    {
        QSet<WaylandSurface *> targets;
        QTouchEvent *te = static_cast<QTouchEvent *>(event);
        QList<QTouchEvent::TouchPoint> points = te->touchPoints();
        for (int i = 0; i < points.count(); ++i) {
            const QTouchEvent::TouchPoint &tp(points.at(i));
            QPoint local;
            WaylandSurface *targetSurface = surfaceAt(tp.pos().toPoint(), &local);
            if (targetSurface) {
                targetSurface->sendTouchPointEvent(tp.id(), local.x(), local.y(), tp.state());
                targets.insert(targetSurface);
            }
        }
        foreach (WaylandSurface *surface, targets)
            surface->sendTouchFrameEvent();
        break;
    }
    default:
        break;
    }
    return false;
}