1 #include "qwindowcompositor.h"
3 QWindowCompositor::QWindowCompositor(QOpenGLWindow *window)
4 : WaylandCompositor(window, window->context())
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);
14 void QWindowCompositor::surfaceDestroyed(QObject *object)
16 WaylandSurface *surface = static_cast<WaylandSurface *>(object);
17 m_surfaces.removeAll(surface);
21 void QWindowCompositor::surfaceMapped(const QSize &size)
23 WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
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));
29 surface->setGeometry(QRect(pos, size));
30 m_surfaces.append(surface);
32 surface->setGeometry(QRect(window()->geometry().topLeft(),size));
34 setInputFocus(surface);
38 void QWindowCompositor::surfaceDamaged(const QRect &rect)
40 WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
41 surfaceDamaged(surface, rect);
44 void QWindowCompositor::surfaceDamaged(WaylandSurface *surface, const QRect &rect)
51 void QWindowCompositor::surfaceCreated(WaylandSurface *surface)
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 &)));
59 WaylandSurface * QWindowCompositor::surfaceAt(const QPoint &point, QPoint *local)
61 for (int i = m_surfaces.size() - 1; i >= 0; --i) {
62 if (m_surfaces.at(i)->geometry().contains(point)) {
64 *local = point - m_surfaces.at(i)->geometry().topLeft();
65 return m_surfaces.at(i);
71 void QWindowCompositor::render()
73 m_window->makeCurrent();
75 //Draw the background Image texture
76 m_renderer->drawTexture(m_backgroundTexture, m_window->geometry(), 0);
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());
88 m_window->swapBuffers();
89 m_window->context()->doneCurrent();