Merge branch 'refactor'
[profile/ivi/qtwayland.git] / examples / qwindow-compositor / qwindowcompositor.cpp
1 #include "qwindowcompositor.h"
2
3 QWindowCompositor::QWindowCompositor(QOpenGLWindow *window)
4     : WaylandCompositor(window, window->context())
5     , m_window(window)
6 {
7     m_backgroundImage = QImage(QLatin1String("background.jpg"));
8     m_renderer = new SurfaceRenderer(m_window->context(), m_window);
9     m_backgroundTexture = m_renderer->textureFromImage(m_backgroundImage);
10
11     render();
12 }
13
14 void QWindowCompositor::surfaceDestroyed(QObject *object)
15 {
16     WaylandSurface *surface = static_cast<WaylandSurface *>(object);
17     m_surfaces.removeAll(surface);
18     render();
19 }
20
21 void QWindowCompositor::surfaceMapped(const QSize &size)
22 {
23     WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
24     QPoint pos;
25     if (!m_surfaces.contains(surface)) {
26         uint px = 1 + (qrand() % (m_window->width() - size.width() - 2));
27         uint py = 1 + (qrand() % (m_window->height() - size.height() - 2));
28         pos = QPoint(px, py);
29         surface->setGeometry(QRect(pos, size));
30         m_surfaces.append(surface);
31     } else {
32         surface->setGeometry(QRect(window()->geometry().topLeft(),size));
33     }
34     setInputFocus(surface);
35     render();
36 }
37
38 void QWindowCompositor::surfaceDamaged(const QRect &rect)
39 {
40     WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
41     surfaceDamaged(surface, rect);
42 }
43
44 void QWindowCompositor::surfaceDamaged(WaylandSurface *surface, const QRect &rect)
45 {
46     Q_UNUSED(surface)
47     Q_UNUSED(rect)
48     render();
49 }
50
51 void QWindowCompositor::surfaceCreated(WaylandSurface *surface)
52 {
53     connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
54     connect(surface, SIGNAL(mapped(const QSize &)), this, SLOT(surfaceMapped(const QSize &)));
55     connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
56     render();
57 }
58
59 WaylandSurface * QWindowCompositor::surfaceAt(const QPoint &point, QPoint *local)
60 {
61     for (int i = m_surfaces.size() - 1; i >= 0; --i) {
62         if (m_surfaces.at(i)->geometry().contains(point)) {
63             if (local)
64                 *local = point - m_surfaces.at(i)->geometry().topLeft();
65             return m_surfaces.at(i);
66         }
67     }
68     return 0;
69 }
70
71 void QWindowCompositor::render()
72 {
73     m_window->makeCurrent();
74
75     //Draw the background Image texture
76     m_renderer->drawTexture(m_backgroundTexture, m_window->geometry(), 0);
77
78     //Iterate all surfaces in m_surfaces
79     //If type == WaylandSurface::Texture draw textureId at geometry
80     foreach (WaylandSurface *surface, m_surfaces) {
81         if (surface->type() == WaylandSurface::Texture)
82             m_renderer->drawTexture(surface->texture(), surface->geometry(), 1); //depth argument should be dynamic (focused should be top).
83         else if (surface->type() == WaylandSurface::Shm)
84             m_renderer->drawImage(surface->image(), surface->geometry());
85     }
86     frameFinished();
87     glFinish();
88     m_window->swapBuffers();
89     m_window->context()->doneCurrent();
90 }
91