diff options
Diffstat (limited to 'examples/wayland/qwindow-compositor')
9 files changed, 1065 insertions, 0 deletions
diff --git a/examples/wayland/qwindow-compositor/compositorwindow.cpp b/examples/wayland/qwindow-compositor/compositorwindow.cpp new file mode 100644 index 000000000..6f8f2321d --- /dev/null +++ b/examples/wayland/qwindow-compositor/compositorwindow.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Wayland module +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "compositorwindow.h" +#include <QTouchEvent> + +CompositorWindow::CompositorWindow(const QSurfaceFormat &format, const QRect &geometry) + : m_format(format) +{ + setSurfaceType(QWindow::OpenGLSurface); + setGeometry(geometry); + setFormat(format); + create(); + m_context = new QOpenGLContext; + m_context->setFormat(format); + m_context->create(); +} + +void CompositorWindow::touchEvent(QTouchEvent *event) +{ + // Do not want any automatically synthesized mouse events + // so make sure the touch is always accepted. + event->accept(); +} diff --git a/examples/wayland/qwindow-compositor/compositorwindow.h b/examples/wayland/qwindow-compositor/compositorwindow.h new file mode 100644 index 000000000..dc87b31a3 --- /dev/null +++ b/examples/wayland/qwindow-compositor/compositorwindow.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Wayland module +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COMPOSITORWINDOW_H +#define COMPOSITORWINDOW_H + +#include <QWindow> +#include <QOpenGLContext> +#include <QSurfaceFormat> + +class CompositorWindow : public QWindow +{ +public: + CompositorWindow(const QSurfaceFormat &format, const QRect &geometry); + QOpenGLContext* context() { return m_context; } + bool makeCurrent() { return m_context->makeCurrent(this); } + void swapBuffers() { m_context->swapBuffers(this); } + +protected: + void touchEvent(QTouchEvent *event); + +private: + QOpenGLContext *m_context; + QSurfaceFormat m_format; +}; + +#endif // COMPOSITORWINDOW_H diff --git a/examples/wayland/qwindow-compositor/main.cpp b/examples/wayland/qwindow-compositor/main.cpp new file mode 100644 index 000000000..a0288982e --- /dev/null +++ b/examples/wayland/qwindow-compositor/main.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Compositor. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "compositorwindow.h" +#include "qwindowcompositor.h" + +#include <QGuiApplication> +#include <QStringList> +#include <QScreen> +#include <QSurfaceFormat> + +int main(int argc, char *argv[]) +{ + // Enable the following to have touch events generated from mouse events. + // Very handy for testing touch event delivery without a real touch device. + // QGuiApplication::setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents, true); + + QGuiApplication app(argc, argv); + QScreen *screen = QGuiApplication::primaryScreen(); + QRect screenGeometry = screen->availableGeometry(); + + QSurfaceFormat format; + format.setDepthBufferSize(16); + format.setStencilBufferSize(8); + + QRect geom = screenGeometry; + if (QCoreApplication::arguments().contains(QLatin1String("-nofullscreen"))) + geom = QRect(screenGeometry.width() / 4, screenGeometry.height() / 4, + screenGeometry.width() / 2, screenGeometry.height() / 2); + + CompositorWindow window(format, geom); + QWindowCompositor compositor(&window); + + window.show(); + + return app.exec(); +} diff --git a/examples/wayland/qwindow-compositor/qwindow-compositor.pro b/examples/wayland/qwindow-compositor/qwindow-compositor.pro new file mode 100644 index 000000000..4bab000b5 --- /dev/null +++ b/examples/wayland/qwindow-compositor/qwindow-compositor.pro @@ -0,0 +1,29 @@ +QT += gui gui-private core-private compositor + +LIBS += -L ../../lib +#include (../../src/qt-compositor/qt-compositor.pri) + +HEADERS += \ + compositorwindow.h \ + qwindowcompositor.h \ + textureblitter.h + +SOURCES += main.cpp \ + compositorwindow.cpp \ + qwindowcompositor.cpp \ + textureblitter.cpp + +# to make QtCompositor/... style includes working without installing +INCLUDEPATH += $$PWD/../../include + +# if you want to compile QtCompositor as part of the application +# instead of linking to it, remove the QT += compositor and uncomment +# the following line +#include(../../src/compositor/compositor.pri) + +RESOURCES += qwindow-compositor.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/wayland/qwindow-compositor +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qwindow-compositor.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/wayland/qwindow-compositor +INSTALLS += target sources diff --git a/examples/wayland/qwindow-compositor/qwindow-compositor.qrc b/examples/wayland/qwindow-compositor/qwindow-compositor.qrc new file mode 100644 index 000000000..20dd10a53 --- /dev/null +++ b/examples/wayland/qwindow-compositor/qwindow-compositor.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file alias="background.jpg">../qml-compositor/background.jpg</file> + </qresource> +</RCC> diff --git a/examples/wayland/qwindow-compositor/qwindowcompositor.cpp b/examples/wayland/qwindow-compositor/qwindowcompositor.cpp new file mode 100644 index 000000000..c933152d8 --- /dev/null +++ b/examples/wayland/qwindow-compositor/qwindowcompositor.cpp @@ -0,0 +1,478 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Compositor. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwindowcompositor.h" + +#include <QMouseEvent> +#include <QKeyEvent> +#include <QTouchEvent> +#include <QOpenGLFunctions> +#include <QOpenGLTexture> +#include <QGuiApplication> +#include <QCursor> +#include <QPixmap> +#include <QLinkedList> +#include <QScreen> +#include <QPainter> + +#include <QtCompositor/qwaylandinput.h> +#include <QtCompositor/qwaylandbufferref.h> +#include <QtCompositor/qwaylandsurfaceview.h> + +QT_BEGIN_NAMESPACE + +class BufferAttacher : public QWaylandBufferAttacher +{ +public: + BufferAttacher() + : QWaylandBufferAttacher() + , shmTex(0) + { + } + + ~BufferAttacher() + { + delete shmTex; + } + + void attach(const QWaylandBufferRef &ref) Q_DECL_OVERRIDE + { + if (bufferRef) { + if (bufferRef.isShm()) { + delete shmTex; + shmTex = 0; + } else { + bufferRef.destroyTexture(); + } + } + + bufferRef = ref; + + if (bufferRef) { + if (bufferRef.isShm()) { + shmTex = new QOpenGLTexture(bufferRef.image(), QOpenGLTexture::DontGenerateMipMaps); + texture = shmTex->textureId(); + } else { + texture = bufferRef.createTexture(); + } + } + } + + QImage image() const + { + if (!bufferRef || !bufferRef.isShm()) + return QImage(); + return bufferRef.image(); + } + + QOpenGLTexture *shmTex; + QWaylandBufferRef bufferRef; + GLuint texture; +}; + +QWindowCompositor::QWindowCompositor(CompositorWindow *window) + : QWaylandCompositor(window, 0, DefaultExtensions | SubSurfaceExtension) + , m_window(window) + , m_backgroundTexture(0) + , m_textureBlitter(0) + , m_renderScheduler(this) + , m_draggingWindow(0) + , m_dragKeyIsPressed(false) + , m_cursorSurface(0) + , m_cursorHotspotX(0) + , m_cursorHotspotY(0) + , m_modifiers(Qt::NoModifier) +{ + m_window->makeCurrent(); + + m_textureBlitter = new TextureBlitter(); + m_backgroundImage = makeBackgroundImage(QLatin1String(":/background.jpg")); + m_renderScheduler.setSingleShot(true); + connect(&m_renderScheduler,SIGNAL(timeout()),this,SLOT(render())); + + QOpenGLFunctions *functions = m_window->context()->functions(); + functions->glGenFramebuffers(1, &m_surface_fbo); + + window->installEventFilter(this); + + setRetainedSelectionEnabled(true); + + setOutputGeometry(QRect(QPoint(0, 0), window->size())); + setOutputRefreshRate(qRound(qGuiApp->primaryScreen()->refreshRate() * 1000.0)); + addDefaultShell(); +} + +QWindowCompositor::~QWindowCompositor() +{ + delete m_textureBlitter; +} + + +QImage QWindowCompositor::makeBackgroundImage(const QString &fileName) +{ + Q_ASSERT(m_window); + + int width = m_window->width(); + int height = m_window->height(); + QImage baseImage(fileName); + QImage patternedBackground(width, height, baseImage.format()); + QPainter painter(&patternedBackground); + + QSize imageSize = baseImage.size(); + for (int y = 0; y < height; y += imageSize.height()) { + for (int x = 0; x < width; x += imageSize.width()) { + painter.drawImage(x, y, baseImage); + } + } + + return patternedBackground; +} + +void QWindowCompositor::ensureKeyboardFocusSurface(QWaylandSurface *oldSurface) +{ + QWaylandSurface *kbdFocus = defaultInputDevice()->keyboardFocus(); + if (kbdFocus == oldSurface || !kbdFocus) + defaultInputDevice()->setKeyboardFocus(m_surfaces.isEmpty() ? 0 : m_surfaces.last()); +} + +void QWindowCompositor::surfaceDestroyed() +{ + QWaylandSurface *surface = static_cast<QWaylandSurface *>(sender()); + m_surfaces.removeOne(surface); + ensureKeyboardFocusSurface(surface); + m_renderScheduler.start(0); +} + +void QWindowCompositor::surfaceMapped() +{ + QWaylandSurface *surface = qobject_cast<QWaylandSurface *>(sender()); + QPoint pos; + if (!m_surfaces.contains(surface)) { + if (surface->windowType() != QWaylandSurface::Popup) { + uint px = 0; + uint py = 0; + if (!QCoreApplication::arguments().contains(QLatin1String("-stickytopleft"))) { + px = 1 + (qrand() % (m_window->width() - surface->size().width() - 2)); + py = 1 + (qrand() % (m_window->height() - surface->size().height() - 2)); + } + pos = QPoint(px, py); + QWaylandSurfaceView *view = surface->views().first(); + view->setPos(pos); + } + } else { + m_surfaces.removeOne(surface); + } + + if (surface->windowType() == QWaylandSurface::Popup) { + QWaylandSurfaceView *view = surface->views().first(); + view->setPos(surface->transientParent()->views().first()->pos() + surface->transientOffset()); + } + + m_surfaces.append(surface); + defaultInputDevice()->setKeyboardFocus(surface); + + m_renderScheduler.start(0); +} + +void QWindowCompositor::surfaceUnmapped() +{ + QWaylandSurface *surface = qobject_cast<QWaylandSurface *>(sender()); + if (m_surfaces.removeOne(surface)) + m_surfaces.insert(0, surface); + + ensureKeyboardFocusSurface(surface); + m_renderScheduler.start(0); +} + +void QWindowCompositor::surfaceCommitted() +{ + QWaylandSurface *surface = qobject_cast<QWaylandSurface *>(sender()); + surfaceCommitted(surface); +} + +void QWindowCompositor::surfacePosChanged() +{ + m_renderScheduler.start(0); +} + +void QWindowCompositor::surfaceCommitted(QWaylandSurface *surface) +{ + Q_UNUSED(surface) + m_renderScheduler.start(0); +} + +void QWindowCompositor::surfaceCreated(QWaylandSurface *surface) +{ + connect(surface, SIGNAL(surfaceDestroyed()), this, SLOT(surfaceDestroyed())); + connect(surface, SIGNAL(mapped()), this, SLOT(surfaceMapped())); + connect(surface, SIGNAL(unmapped()), this, SLOT(surfaceUnmapped())); + connect(surface, SIGNAL(redraw()), this, SLOT(surfaceCommitted())); + connect(surface, SIGNAL(extendedSurfaceReady()), this, SLOT(sendExpose())); + m_renderScheduler.start(0); + + surface->setBufferAttacher(new BufferAttacher); +} + +void QWindowCompositor::sendExpose() +{ + QWaylandSurface *surface = qobject_cast<QWaylandSurface *>(sender()); + surface->sendOnScreenVisibilityChange(true); +} + +void QWindowCompositor::updateCursor(bool hasBuffer) +{ + Q_UNUSED(hasBuffer) + if (!m_cursorSurface) + return; + + QImage image = static_cast<BufferAttacher *>(m_cursorSurface->bufferAttacher())->image(); + + QCursor cursor(QPixmap::fromImage(image), m_cursorHotspotX, m_cursorHotspotY); + static bool cursorIsSet = false; + if (cursorIsSet) { + QGuiApplication::changeOverrideCursor(cursor); + } else { + QGuiApplication::setOverrideCursor(cursor); + cursorIsSet = true; + } +} + +QPointF QWindowCompositor::toView(QWaylandSurfaceView *view, const QPointF &pos) const +{ + return pos - view->pos(); +} + +void QWindowCompositor::setCursorSurface(QWaylandSurface *surface, int hotspotX, int hotspotY) +{ + if ((m_cursorSurface != surface) && surface) + connect(surface, SIGNAL(configure(bool)), this, SLOT(updateCursor(bool))); + + m_cursorSurface = surface; + m_cursorHotspotX = hotspotX; + m_cursorHotspotY = hotspotY; + if (m_cursorSurface && !m_cursorSurface->bufferAttacher()) + m_cursorSurface->setBufferAttacher(new BufferAttacher); +} + +QWaylandSurfaceView *QWindowCompositor::viewAt(const QPointF &point, QPointF *local) +{ + for (int i = m_surfaces.size() - 1; i >= 0; --i) { + QWaylandSurface *surface = m_surfaces.at(i); + foreach (QWaylandSurfaceView *view, surface->views()) { + QRectF geo(view->pos(), surface->size()); + if (geo.contains(point)) { + if (local) + *local = toView(view, point); + return view; + } + } + } + return 0; +} + +void QWindowCompositor::render() +{ + m_window->makeCurrent(); + frameStarted(); + + cleanupGraphicsResources(); + + if (!m_backgroundTexture) + m_backgroundTexture = new QOpenGLTexture(m_backgroundImage, QOpenGLTexture::DontGenerateMipMaps); + + m_textureBlitter->bind(); + // Draw the background image texture + m_textureBlitter->drawTexture(m_backgroundTexture->textureId(), + QRect(QPoint(0, 0), m_backgroundImage.size()), + window()->size(), + 0, false, true); + + foreach (QWaylandSurface *surface, m_surfaces) { + if (!surface->visible()) + continue; + GLuint texture = static_cast<BufferAttacher *>(surface->bufferAttacher())->texture; + foreach (QWaylandSurfaceView *view, surface->views()) { + QRect geo(view->pos().toPoint(),surface->size()); + m_textureBlitter->drawTexture(texture,geo,m_window->size(),0,false,surface->isYInverted()); + foreach (QWaylandSurface *child, surface->subSurfaces()) { + drawSubSurface(view->pos().toPoint(), child); + } + } + } + + m_textureBlitter->release(); + sendFrameCallbacks(surfaces()); + + // N.B. Never call glFinish() here as the busylooping with vsync 'feature' of the nvidia binary driver is not desirable. + m_window->swapBuffers(); +} + +void QWindowCompositor::drawSubSurface(const QPoint &offset, QWaylandSurface *surface) +{ + GLuint texture = static_cast<BufferAttacher *>(surface->bufferAttacher())->texture; + QWaylandSurfaceView *view = surface->views().first(); + QPoint pos = view->pos().toPoint() + offset; + QRect geo(pos, surface->size()); + m_textureBlitter->drawTexture(texture, geo, m_window->size(), 0, false, surface->isYInverted()); + foreach (QWaylandSurface *child, surface->subSurfaces()) { + drawSubSurface(pos, child); + } +} + +bool QWindowCompositor::eventFilter(QObject *obj, QEvent *event) +{ + if (obj != m_window) + return false; + + QWaylandInputDevice *input = defaultInputDevice(); + + switch (event->type()) { + case QEvent::Expose: + m_renderScheduler.start(0); + if (m_window->isExposed()) { + // Alt-tabbing away normally results in the alt remaining in + // pressed state in the clients xkb state. Prevent this by sending + // a release. This is not an issue in a "real" compositor but + // is very annoying when running in a regular window on xcb. + Qt::KeyboardModifiers mods = QGuiApplication::queryKeyboardModifiers(); + if (m_modifiers != mods && input->keyboardFocus()) { + Qt::KeyboardModifiers stuckMods = m_modifiers ^ mods; + if (stuckMods & Qt::AltModifier) + input->sendKeyReleaseEvent(64); // native scancode for left alt + m_modifiers = mods; + } + } + break; + case QEvent::MouseButtonPress: { + QPointF local; + QMouseEvent *me = static_cast<QMouseEvent *>(event); + QWaylandSurfaceView *target = viewAt(me->localPos(), &local); + if (m_dragKeyIsPressed && target) { + m_draggingWindow = target; + m_drag_diff = local; + } else { + if (target && input->keyboardFocus() != target->surface()) { + input->setKeyboardFocus(target->surface()); + m_surfaces.removeOne(target->surface()); + m_surfaces.append(target->surface()); + m_renderScheduler.start(0); + } + input->sendMousePressEvent(me->button(), local, me->localPos()); + } + return true; + } + case QEvent::MouseButtonRelease: { + QWaylandSurfaceView *target = input->mouseFocus(); + if (m_draggingWindow) { + m_draggingWindow = 0; + m_drag_diff = QPointF(); + } else { + QMouseEvent *me = static_cast<QMouseEvent *>(event); + QPointF localPos; + if (target) + localPos = toView(target, me->localPos()); + input->sendMouseReleaseEvent(me->button(), localPos, me->localPos()); + } + return true; + } + case QEvent::MouseMove: { + QMouseEvent *me = static_cast<QMouseEvent *>(event); + if (m_draggingWindow) { + m_draggingWindow->setPos(me->localPos() - m_drag_diff); + m_renderScheduler.start(0); + } else { + QPointF local; + QWaylandSurfaceView *target = viewAt(me->localPos(), &local); + input->sendMouseMoveEvent(target, local, me->localPos()); + } + break; + } + case QEvent::Wheel: { + QWheelEvent *we = static_cast<QWheelEvent *>(event); + input->sendMouseWheelEvent(we->orientation(), we->delta()); + break; + } + case QEvent::KeyPress: { + QKeyEvent *ke = static_cast<QKeyEvent *>(event); + if (ke->key() == Qt::Key_Meta || ke->key() == Qt::Key_Super_L) { + m_dragKeyIsPressed = true; + } + m_modifiers = ke->modifiers(); + QWaylandSurface *targetSurface = input->keyboardFocus(); + if (targetSurface) + input->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; + } + m_modifiers = ke->modifiers(); + QWaylandSurface *targetSurface = input->keyboardFocus(); + if (targetSurface) + input->sendKeyReleaseEvent(ke->nativeScanCode()); + break; + } + case QEvent::TouchBegin: + case QEvent::TouchUpdate: + case QEvent::TouchEnd: + { + QWaylandSurfaceView *target = 0; + QTouchEvent *te = static_cast<QTouchEvent *>(event); + QList<QTouchEvent::TouchPoint> points = te->touchPoints(); + QPoint pointPos; + if (!points.isEmpty()) { + pointPos = points.at(0).pos().toPoint(); + target = viewAt(pointPos); + } + if (target && target != input->mouseFocus()) + input->setMouseFocus(target, pointPos, pointPos); + if (input->mouseFocus()) + input->sendFullTouchEvent(te); + break; + } + default: + break; + } + return false; +} + +QT_END_NAMESPACE diff --git a/examples/wayland/qwindow-compositor/qwindowcompositor.h b/examples/wayland/qwindow-compositor/qwindowcompositor.h new file mode 100644 index 000000000..57f3e8536 --- /dev/null +++ b/examples/wayland/qwindow-compositor/qwindowcompositor.h @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Compositor. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWINDOWCOMPOSITOR_H +#define QWINDOWCOMPOSITOR_H + +#include "qwaylandcompositor.h" +#include "qwaylandsurface.h" +#include "textureblitter.h" +#include "compositorwindow.h" + +#include <QtGui/private/qopengltexturecache_p.h> +#include <QObject> +#include <QTimer> + +QT_BEGIN_NAMESPACE + +class QWaylandSurfaceView; +class QOpenGLTexture; + +class QWindowCompositor : public QObject, public QWaylandCompositor +{ + Q_OBJECT +public: + QWindowCompositor(CompositorWindow *window); + ~QWindowCompositor(); + +private slots: + void surfaceDestroyed(); + void surfaceMapped(); + void surfaceUnmapped(); + void surfaceCommitted(); + void surfacePosChanged(); + + void render(); +protected: + void surfaceCommitted(QWaylandSurface *surface); + void surfaceCreated(QWaylandSurface *surface); + + QWaylandSurfaceView* viewAt(const QPointF &point, QPointF *local = 0); + + bool eventFilter(QObject *obj, QEvent *event); + QPointF toView(QWaylandSurfaceView *view, const QPointF &pos) const; + + void setCursorSurface(QWaylandSurface *surface, int hotspotX, int hotspotY); + + void ensureKeyboardFocusSurface(QWaylandSurface *oldSurface); + QImage makeBackgroundImage(const QString &fileName); + +private slots: + void sendExpose(); + void updateCursor(bool hasBuffer); + +private: + void drawSubSurface(const QPoint &offset, QWaylandSurface *surface); + + CompositorWindow *m_window; + QImage m_backgroundImage; + QOpenGLTexture *m_backgroundTexture; + QList<QWaylandSurface *> m_surfaces; + TextureBlitter *m_textureBlitter; + GLuint m_surface_fbo; + QTimer m_renderScheduler; + + //Dragging windows around + QWaylandSurfaceView *m_draggingWindow; + bool m_dragKeyIsPressed; + QPointF m_drag_diff; + + //Cursor + QWaylandSurface *m_cursorSurface; + int m_cursorHotspotX; + int m_cursorHotspotY; + + Qt::KeyboardModifiers m_modifiers; +}; + +QT_END_NAMESPACE + +#endif // QWINDOWCOMPOSITOR_H diff --git a/examples/wayland/qwindow-compositor/textureblitter.cpp b/examples/wayland/qwindow-compositor/textureblitter.cpp new file mode 100644 index 000000000..813fd8966 --- /dev/null +++ b/examples/wayland/qwindow-compositor/textureblitter.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Compositor. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "textureblitter.h" + +#include <QtGui/QOpenGLShaderProgram> +#include <QtGui/QOpenGLContext> +#include <QtGui/QOpenGLFunctions> + +QT_BEGIN_NAMESPACE + +TextureBlitter::TextureBlitter() + : m_shaderProgram(new QOpenGLShaderProgram) +{ + static const char *textureVertexProgram = + "uniform highp mat4 matrix;\n" + "attribute highp vec3 vertexCoordEntry;\n" + "attribute highp vec2 textureCoordEntry;\n" + "varying highp vec2 textureCoord;\n" + "void main() {\n" + " textureCoord = textureCoordEntry;\n" + " gl_Position = matrix * vec4(vertexCoordEntry, 1);\n" + "}\n"; + + static const char *textureFragmentProgram = + "uniform sampler2D texture;\n" + "varying highp vec2 textureCoord;\n" + "void main() {\n" + " gl_FragColor = texture2D(texture, textureCoord);\n" + "}\n"; + + //Enable transparent windows + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram); + m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram); + m_shaderProgram->link(); +} + +TextureBlitter::~TextureBlitter() +{ + delete m_shaderProgram; +} + +void TextureBlitter::bind() +{ + + m_shaderProgram->bind(); + + m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry"); + m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry"); + m_matrixLocation = m_shaderProgram->uniformLocation("matrix"); +} + +void TextureBlitter::release() +{ + m_shaderProgram->release(); +} + +void TextureBlitter::drawTexture(int textureId, const QRectF &targetRect, const QSize &targetSize, int depth, bool targethasInvertedY, bool sourceHasInvertedY) +{ + + glViewport(0,0,targetSize.width(),targetSize.height()); + GLfloat zValue = depth / 1000.0f; + //Set Texture and Vertex coordinates + const GLfloat textureCoordinates[] = { + 0, 0, + 1, 0, + 1, 1, + 0, 1 + }; + + GLfloat x1 = targetRect.left(); + GLfloat x2 = targetRect.right(); + GLfloat y1, y2; + if (targethasInvertedY) { + if (sourceHasInvertedY) { + y1 = targetRect.top(); + y2 = targetRect.bottom(); + } else { + y1 = targetRect.bottom(); + y2 = targetRect.top(); + } + } else { + if (sourceHasInvertedY) { + y1 = targetSize.height() - targetRect.top(); + y2 = targetSize.height() - targetRect.bottom(); + } else { + y1 = targetSize.height() - targetRect.bottom(); + y2 = targetSize.height() - targetRect.top(); + } + } + + const GLfloat vertexCoordinates[] = { + GLfloat(x1), GLfloat(y1), zValue, + GLfloat(x2), GLfloat(y1), zValue, + GLfloat(x2), GLfloat(y2), zValue, + GLfloat(x1), GLfloat(y2), zValue + }; + + //Set matrix to transfrom geometry values into gl coordinate space. + m_transformMatrix.setToIdentity(); + m_transformMatrix.scale( 2.0f / targetSize.width(), 2.0f / targetSize.height() ); + m_transformMatrix.translate(-targetSize.width() / 2.0f, -targetSize.height() / 2.0f); + + //attach the data! + QOpenGLContext *currentContext = QOpenGLContext::currentContext(); + currentContext->functions()->glEnableVertexAttribArray(m_vertexCoordEntry); + currentContext->functions()->glEnableVertexAttribArray(m_textureCoordEntry); + + currentContext->functions()->glVertexAttribPointer(m_vertexCoordEntry, 3, GL_FLOAT, GL_FALSE, 0, vertexCoordinates); + currentContext->functions()->glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates); + m_shaderProgram->setUniformValue(m_matrixLocation, m_transformMatrix); + + glBindTexture(GL_TEXTURE_2D, textureId); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + glBindTexture(GL_TEXTURE_2D, 0); + + currentContext->functions()->glDisableVertexAttribArray(m_vertexCoordEntry); + currentContext->functions()->glDisableVertexAttribArray(m_textureCoordEntry); +} + +QT_END_NAMESPACE diff --git a/examples/wayland/qwindow-compositor/textureblitter.h b/examples/wayland/qwindow-compositor/textureblitter.h new file mode 100644 index 000000000..7426c6f29 --- /dev/null +++ b/examples/wayland/qwindow-compositor/textureblitter.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Compositor. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEXTUREBLITTER_H +#define TEXTUREBLITTER_H + +#include <QtGui/QMatrix4x4> + +QT_BEGIN_NAMESPACE + +class QOpenGLShaderProgram; +class TextureBlitter +{ +public: + TextureBlitter(); + ~TextureBlitter(); + void bind(); + void release(); + void drawTexture(int textureId, const QRectF &sourceGeometry, + const QSize &targetRect, int depth, + bool targethasInvertedY, bool sourceHasInvertedY); + +private: + QOpenGLShaderProgram *m_shaderProgram; + QMatrix4x4 m_transformMatrix; + + int m_matrixLocation; + int m_vertexCoordEntry; + int m_textureCoordEntry; +}; + +QT_END_NAMESPACE + +#endif // TEXTUREBLITTER_H |