diff --git a/autotests/client/test_wayland_subsurface.cpp b/autotests/client/test_wayland_subsurface.cpp index cd2c0c4..40bbad5 100644 --- a/autotests/client/test_wayland_subsurface.cpp +++ b/autotests/client/test_wayland_subsurface.cpp @@ -1,918 +1,924 @@ /******************************************************************** Copyright 2014 Martin Gräßlin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ // Qt #include // KWin #include "../../src/client/compositor.h" #include "../../src/client/connection_thread.h" #include "../../src/client/event_queue.h" #include "../../src/client/registry.h" #include "../../src/client/shm_pool.h" #include "../../src/client/subcompositor.h" #include "../../src/client/subsurface.h" #include "../../src/client/surface.h" #include "../../src/server/buffer_interface.h" #include "../../src/server/display.h" #include "../../src/server/compositor_interface.h" #include "../../src/server/subcompositor_interface.h" #include "../../src/server/surface_interface.h" // Wayland #include class TestSubSurface : public QObject { Q_OBJECT public: explicit TestSubSurface(QObject *parent = nullptr); private Q_SLOTS: void init(); void cleanup(); void testCreate(); void testMode(); void testPosition(); void testPlaceAbove(); void testPlaceBelow(); void testDestroy(); void testCast(); void testSyncMode(); void testDeSyncMode(); void testMainSurfaceFromTree(); void testRemoveSurface(); void testMappingOfSurfaceTree(); private: KWayland::Server::Display *m_display; KWayland::Server::CompositorInterface *m_compositorInterface; KWayland::Server::SubCompositorInterface *m_subcompositorInterface; KWayland::Client::ConnectionThread *m_connection; KWayland::Client::Compositor *m_compositor; KWayland::Client::ShmPool *m_shm; KWayland::Client::SubCompositor *m_subCompositor; KWayland::Client::EventQueue *m_queue; QThread *m_thread; }; static const QString s_socketName = QStringLiteral("kwayland-test-wayland-subsurface-0"); TestSubSurface::TestSubSurface(QObject *parent) : QObject(parent) , m_display(nullptr) , m_compositorInterface(nullptr) , m_subcompositorInterface(nullptr) , m_connection(nullptr) , m_compositor(nullptr) , m_shm(nullptr) , m_subCompositor(nullptr) , m_queue(nullptr) , m_thread(nullptr) { } void TestSubSurface::init() { using namespace KWayland::Server; delete m_display; m_display = new Display(this); m_display->setSocketName(s_socketName); m_display->start(); QVERIFY(m_display->isRunning()); m_display->createShm(); // setup connection m_connection = new KWayland::Client::ConnectionThread; QSignalSpy connectedSpy(m_connection, SIGNAL(connected())); m_connection->setSocketName(s_socketName); m_thread = new QThread(this); m_connection->moveToThread(m_thread); m_thread->start(); m_connection->initConnection(); QVERIFY(connectedSpy.wait()); m_queue = new KWayland::Client::EventQueue(this); QVERIFY(!m_queue->isValid()); m_queue->setup(m_connection); QVERIFY(m_queue->isValid()); KWayland::Client::Registry registry; QSignalSpy compositorSpy(®istry, SIGNAL(compositorAnnounced(quint32,quint32))); QVERIFY(compositorSpy.isValid()); QSignalSpy subCompositorSpy(®istry, SIGNAL(subCompositorAnnounced(quint32,quint32))); QVERIFY(subCompositorSpy.isValid()); QVERIFY(!registry.eventQueue()); registry.setEventQueue(m_queue); QCOMPARE(registry.eventQueue(), m_queue); registry.create(m_connection->display()); QVERIFY(registry.isValid()); registry.setup(); m_compositorInterface = m_display->createCompositor(m_display); m_compositorInterface->create(); QVERIFY(m_compositorInterface->isValid()); m_subcompositorInterface = m_display->createSubCompositor(m_display); QVERIFY(m_subcompositorInterface); m_subcompositorInterface->create(); QVERIFY(m_subcompositorInterface->isValid()); QVERIFY(subCompositorSpy.wait()); m_subCompositor = registry.createSubCompositor(subCompositorSpy.first().first().value(), subCompositorSpy.first().last().value(), this); if (compositorSpy.isEmpty()) { QVERIFY(compositorSpy.wait()); } m_compositor = registry.createCompositor(compositorSpy.first().first().value(), compositorSpy.first().last().value(), this); m_shm = registry.createShmPool(registry.interface(KWayland::Client::Registry::Interface::Shm).name, registry.interface(KWayland::Client::Registry::Interface::Shm).version, this); QVERIFY(m_shm->isValid()); } void TestSubSurface::cleanup() { if (m_shm) { delete m_shm; m_shm = nullptr; } if (m_subCompositor) { delete m_subCompositor; m_subCompositor = nullptr; } if (m_compositor) { delete m_compositor; m_compositor = nullptr; } if (m_queue) { delete m_queue; m_queue = nullptr; } if (m_thread) { m_thread->quit(); m_thread->wait(); delete m_thread; m_thread = nullptr; } delete m_connection; m_connection = nullptr; delete m_display; m_display = nullptr; } void TestSubSurface::testCreate() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, SIGNAL(surfaceCreated(KWayland::Server::SurfaceInterface*))); QVERIFY(surfaceCreatedSpy.isValid()); // create two Surfaces QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); SurfaceInterface *serverSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(serverSurface); surfaceCreatedSpy.clear(); QScopedPointer parent(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); SurfaceInterface *serverParentSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(serverParentSurface); QSignalSpy subSurfaceCreatedSpy(m_subcompositorInterface, SIGNAL(subSurfaceCreated(KWayland::Server::SubSurfaceInterface*))); QVERIFY(subSurfaceCreatedSpy.isValid()); // create subSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface); QVERIFY(serverSubSurface->parentSurface()); QCOMPARE(serverSubSurface->parentSurface().data(), serverParentSurface); QCOMPARE(serverSubSurface->surface().data(), serverSurface); QCOMPARE(serverSurface->subSurface().data(), serverSubSurface); QCOMPARE(serverSubSurface->mainSurface().data(), serverParentSurface); // children are only added after committing the surface QEXPECT_FAIL("", "Incorrect adding of child windows to workaround QtWayland behavior", Continue); QCOMPARE(serverParentSurface->childSubSurfaces().count(), 0); // so let's commit the surface, to apply the stacking change parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverParentSurface->childSubSurfaces().count(), 1); QCOMPARE(serverParentSurface->childSubSurfaces().first().data(), serverSubSurface); // and let's destroy it again QSignalSpy destroyedSpy(serverSubSurface, SIGNAL(destroyed(QObject*))); QVERIFY(destroyedSpy.isValid()); subSurface.reset(); QVERIFY(destroyedSpy.wait()); QCOMPARE(serverSurface->subSurface(), QPointer()); // only applied after next commit QEXPECT_FAIL("", "Incorrect removing of child windows to workaround QtWayland behavior", Continue); QCOMPARE(serverParentSurface->childSubSurfaces().count(), 1); // but the surface should be invalid if (!serverParentSurface->childSubSurfaces().isEmpty()) { QVERIFY(serverParentSurface->childSubSurfaces().first().isNull()); } // committing the state should solve it parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverParentSurface->childSubSurfaces().count(), 0); } void TestSubSurface::testMode() { using namespace KWayland::Client; using namespace KWayland::Server; // create two Surface QScopedPointer surface(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); QSignalSpy subSurfaceCreatedSpy(m_subcompositorInterface, SIGNAL(subSurfaceCreated(KWayland::Server::SubSurfaceInterface*))); QVERIFY(subSurfaceCreatedSpy.isValid()); // create the SubSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface); // both client and server subsurface should be in synchronized mode QCOMPARE(subSurface->mode(), SubSurface::Mode::Synchronized); QCOMPARE(serverSubSurface->mode(), SubSurfaceInterface::Mode::Synchronized); // verify that we can change to desynchronized QSignalSpy modeChangedSpy(serverSubSurface, SIGNAL(modeChanged(KWayland::Server::SubSurfaceInterface::Mode))); QVERIFY(modeChangedSpy.isValid()); subSurface->setMode(SubSurface::Mode::Desynchronized); QCOMPARE(subSurface->mode(), SubSurface::Mode::Desynchronized); QVERIFY(modeChangedSpy.wait()); QCOMPARE(modeChangedSpy.first().first().value(), SubSurfaceInterface::Mode::Desynchronized); QCOMPARE(serverSubSurface->mode(), SubSurfaceInterface::Mode::Desynchronized); // setting the same again won't change subSurface->setMode(SubSurface::Mode::Desynchronized); QCOMPARE(subSurface->mode(), SubSurface::Mode::Desynchronized); // not testing the signal, we do that after changing to synchronized // and change back to synchronized subSurface->setMode(SubSurface::Mode::Synchronized); QCOMPARE(subSurface->mode(), SubSurface::Mode::Synchronized); QVERIFY(modeChangedSpy.wait()); QCOMPARE(modeChangedSpy.count(), 2); QCOMPARE(modeChangedSpy.first().first().value(), SubSurfaceInterface::Mode::Desynchronized); QCOMPARE(modeChangedSpy.last().first().value(), SubSurfaceInterface::Mode::Synchronized); QCOMPARE(serverSubSurface->mode(), SubSurfaceInterface::Mode::Synchronized); } void TestSubSurface::testPosition() { using namespace KWayland::Client; using namespace KWayland::Server; // create two Surface QScopedPointer surface(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); QSignalSpy subSurfaceCreatedSpy(m_subcompositorInterface, SIGNAL(subSurfaceCreated(KWayland::Server::SubSurfaceInterface*))); QVERIFY(subSurfaceCreatedSpy.isValid()); // create the SubSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface); // create a signalspy QSignalSpy subsurfaceTreeChanged(serverSubSurface->parentSurface().data(), &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subsurfaceTreeChanged.isValid()); // both client and server should have a default position QCOMPARE(subSurface->position(), QPoint()); QCOMPARE(serverSubSurface->position(), QPoint()); QSignalSpy positionChangedSpy(serverSubSurface, SIGNAL(positionChanged(QPoint))); QVERIFY(positionChangedSpy.isValid()); // changing the position should not trigger a direct update on server side subSurface->setPosition(QPoint(10, 20)); QCOMPARE(subSurface->position(), QPoint(10, 20)); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface->position(), QPoint()); // changing once more subSurface->setPosition(QPoint(20, 30)); QCOMPARE(subSurface->position(), QPoint(20, 30)); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface->position(), QPoint()); // committing the parent surface should update the position parent->commit(Surface::CommitFlag::None); QCOMPARE(subsurfaceTreeChanged.count(), 0); QVERIFY(positionChangedSpy.wait()); QCOMPARE(positionChangedSpy.count(), 1); QCOMPARE(positionChangedSpy.first().first().toPoint(), QPoint(20, 30)); QCOMPARE(serverSubSurface->position(), QPoint(20, 30)); QCOMPARE(subsurfaceTreeChanged.count(), 1); } void TestSubSurface::testPlaceAbove() { using namespace KWayland::Client; using namespace KWayland::Server; // create needed Surfaces (one parent, three client QScopedPointer surface1(m_compositor->createSurface()); QScopedPointer surface2(m_compositor->createSurface()); QScopedPointer surface3(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); QSignalSpy subSurfaceCreatedSpy(m_subcompositorInterface, SIGNAL(subSurfaceCreated(KWayland::Server::SubSurfaceInterface*))); QVERIFY(subSurfaceCreatedSpy.isValid()); // create the SubSurfaces for surface of parent QScopedPointer subSurface1(m_subCompositor->createSubSurface(QPointer(surface1.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface1 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface1); subSurfaceCreatedSpy.clear(); QScopedPointer subSurface2(m_subCompositor->createSubSurface(QPointer(surface2.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface2 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface2); subSurfaceCreatedSpy.clear(); QScopedPointer subSurface3(m_subCompositor->createSubSurface(QPointer(surface3.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface3 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface3); subSurfaceCreatedSpy.clear(); // so far the stacking order should still be empty QEXPECT_FAIL("", "Incorrect adding of child windows to workaround QtWayland behavior", Continue); QVERIFY(serverSubSurface1->parentSurface()->childSubSurfaces().isEmpty()); // committing the parent should create the stacking order parent->commit(Surface::CommitFlag::None); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface3); // raising subsurface1 should place it to top of stack subSurface1->raise(); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); // but as long as parent is not committed it shouldn't change on server side QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); // after commit it's changed parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface1); // try placing 3 above 1, should result in 2, 1, 3 subSurface3->placeAbove(QPointer(subSurface1.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface3); // try placing 3 above 2, should result in 2, 3, 1 subSurface3->placeAbove(QPointer(subSurface2.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface1); // try placing 1 above 3 - shouldn't change subSurface1->placeAbove(QPointer(subSurface3.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface1); // and 2 above 3 - > 3, 2, 1 subSurface2->placeAbove(QPointer(subSurface3.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface1); } void TestSubSurface::testPlaceBelow() { using namespace KWayland::Client; using namespace KWayland::Server; // create needed Surfaces (one parent, three client QScopedPointer surface1(m_compositor->createSurface()); QScopedPointer surface2(m_compositor->createSurface()); QScopedPointer surface3(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); QSignalSpy subSurfaceCreatedSpy(m_subcompositorInterface, SIGNAL(subSurfaceCreated(KWayland::Server::SubSurfaceInterface*))); QVERIFY(subSurfaceCreatedSpy.isValid()); // create the SubSurfaces for surface of parent QScopedPointer subSurface1(m_subCompositor->createSubSurface(QPointer(surface1.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface1 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface1); subSurfaceCreatedSpy.clear(); QScopedPointer subSurface2(m_subCompositor->createSubSurface(QPointer(surface2.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface2 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface2); subSurfaceCreatedSpy.clear(); QScopedPointer subSurface3(m_subCompositor->createSubSurface(QPointer(surface3.data()), QPointer(parent.data()))); QVERIFY(subSurfaceCreatedSpy.wait()); SubSurfaceInterface *serverSubSurface3 = subSurfaceCreatedSpy.first().first().value(); QVERIFY(serverSubSurface3); subSurfaceCreatedSpy.clear(); // so far the stacking order should still be empty QEXPECT_FAIL("", "Incorrect adding of child windows to workaround QtWayland behavior", Continue); QVERIFY(serverSubSurface1->parentSurface()->childSubSurfaces().isEmpty()); // committing the parent should create the stacking order parent->commit(Surface::CommitFlag::None); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface3); // lowering subsurface3 should place it to the bottom of stack subSurface3->lower(); // ensure it's processed on server side wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); // but as long as parent is not committed it shouldn't change on server side QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); // after commit it's changed parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface2); // place 1 below 3 -> 1, 3, 2 subSurface1->placeBelow(QPointer(subSurface3.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface2); // 2 below 3 -> 1, 2, 3 subSurface2->placeBelow(QPointer(subSurface3.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface3); // 1 below 2 -> shouldn't change subSurface1->placeBelow(QPointer(subSurface2.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface2); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface3); // and 3 below 1 -> 3, 1, 2 subSurface3->placeBelow(QPointer(subSurface1.data())); parent->commit(Surface::CommitFlag::None); wl_display_flush(m_connection->display()); QCoreApplication::processEvents(); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().count(), 3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(0).data(), serverSubSurface3); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(1).data(), serverSubSurface1); QCOMPARE(serverSubSurface1->parentSurface()->childSubSurfaces().at(2).data(), serverSubSurface2); } void TestSubSurface::testDestroy() { using namespace KWayland::Client; // create two Surfaces QScopedPointer surface(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); // create subSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); connect(m_connection, &ConnectionThread::connectionDied, m_compositor, &Compositor::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_subCompositor, &SubCompositor::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_shm, &ShmPool::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_queue, &EventQueue::destroy); connect(m_connection, &ConnectionThread::connectionDied, surface.data(), &Surface::destroy); connect(m_connection, &ConnectionThread::connectionDied, parent.data(), &Surface::destroy); connect(m_connection, &ConnectionThread::connectionDied, subSurface.data(), &SubSurface::destroy); QVERIFY(subSurface->isValid()); QSignalSpy connectionDiedSpy(m_connection, SIGNAL(connectionDied())); QVERIFY(connectionDiedSpy.isValid()); delete m_display; m_display = nullptr; QVERIFY(connectionDiedSpy.wait()); // now the pool should be destroyed; QVERIFY(!subSurface->isValid()); // calling destroy again should not fail subSurface->destroy(); } void TestSubSurface::testCast() { using namespace KWayland::Client; // create two Surfaces QScopedPointer surface(m_compositor->createSurface()); QScopedPointer parent(m_compositor->createSurface()); // create subSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QCOMPARE(SubSurface::get(*(subSurface.data())), QPointer(subSurface.data())); } void TestSubSurface::testSyncMode() { // this test verifies that state is only applied when the parent surface commits its pending state using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(childSurface); QScopedPointer parent(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto parentSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(parentSurface); QSignalSpy subSurfaceTreeChangedSpy(parentSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subSurfaceTreeChangedSpy.isValid()); // create subSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(subSurfaceTreeChangedSpy.count(), 1); // let's damage the child surface QSignalSpy childDamagedSpy(childSurface, &SurfaceInterface::damaged); QVERIFY(childDamagedSpy.isValid()); QImage image(QSize(200, 200), QImage::Format_ARGB32); image.fill(Qt::black); surface->attachBuffer(m_shm->createBuffer(image)); surface->damage(QRect(0, 0, 200, 200)); surface->commit(); // state should be applied when the parent surface's state gets applied QVERIFY(!childDamagedSpy.wait(100)); QVERIFY(!childSurface->buffer()); QVERIFY(!childSurface->isMapped()); QVERIFY(!parentSurface->isMapped()); QImage image2(QSize(400, 400), QImage::Format_ARGB32); image2.fill(Qt::red); parent->attachBuffer(m_shm->createBuffer(image2)); parent->damage(QRect(0, 0, 400, 400)); parent->commit(); QVERIFY(childDamagedSpy.wait()); QCOMPARE(childDamagedSpy.count(), 1); QCOMPARE(subSurfaceTreeChangedSpy.count(), 2); QCOMPARE(childSurface->buffer()->data(), image); QCOMPARE(parentSurface->buffer()->data(), image2); QVERIFY(childSurface->isMapped()); QVERIFY(parentSurface->isMapped()); // sending frame rendered to parent should also send it to child QSignalSpy frameRenderedSpy(surface.data(), &Surface::frameRendered); QVERIFY(frameRenderedSpy.isValid()); parentSurface->frameRendered(100); QVERIFY(frameRenderedSpy.wait()); } void TestSubSurface::testDeSyncMode() { // this test verifies that state gets applied immediately in desync mode using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(childSurface); QScopedPointer parent(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto parentSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(parentSurface); QSignalSpy subSurfaceTreeChangedSpy(parentSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subSurfaceTreeChangedSpy.isValid()); // create subSurface for surface of parent QScopedPointer subSurface(m_subCompositor->createSubSurface(QPointer(surface.data()), QPointer(parent.data()))); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(subSurfaceTreeChangedSpy.count(), 1); // let's damage the child surface QSignalSpy childDamagedSpy(childSurface, &SurfaceInterface::damaged); QVERIFY(childDamagedSpy.isValid()); QImage image(QSize(200, 200), QImage::Format_ARGB32); image.fill(Qt::black); surface->attachBuffer(m_shm->createBuffer(image)); surface->damage(QRect(0, 0, 200, 200)); surface->commit(Surface::CommitFlag::None); // state should be applied when the parent surface's state gets applied or when the subsurface switches to desync QVERIFY(!childDamagedSpy.wait(100)); QVERIFY(!childSurface->isMapped()); QVERIFY(!parentSurface->isMapped()); // setting to desync should apply the state directly subSurface->setMode(SubSurface::Mode::Desynchronized); QVERIFY(childDamagedSpy.wait()); QCOMPARE(subSurfaceTreeChangedSpy.count(), 2); QCOMPARE(childSurface->buffer()->data(), image); QVERIFY(!childSurface->isMapped()); QVERIFY(!parentSurface->isMapped()); // and damaging again, should directly be applied image.fill(Qt::red); surface->attachBuffer(m_shm->createBuffer(image)); surface->damage(QRect(0, 0, 200, 200)); surface->commit(Surface::CommitFlag::None); QVERIFY(childDamagedSpy.wait()); QCOMPARE(subSurfaceTreeChangedSpy.count(), 3); QCOMPARE(childSurface->buffer()->data(), image); } void TestSubSurface::testMainSurfaceFromTree() { // this test verifies that in a tree of surfaces every surface has the same main surface using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer parentSurface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto parentServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(parentServerSurface); QScopedPointer childLevel1Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel1ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel1ServerSurface); QScopedPointer childLevel2Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel2ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel2ServerSurface); QScopedPointer childLevel3Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel3ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel3ServerSurface); QSignalSpy subSurfaceTreeChangedSpy(parentServerSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subSurfaceTreeChangedSpy.isValid()); m_subCompositor->createSubSurface(childLevel1Surface.data(), parentSurface.data()); m_subCompositor->createSubSurface(childLevel2Surface.data(), childLevel1Surface.data()); m_subCompositor->createSubSurface(childLevel3Surface.data(), childLevel2Surface.data()); parentSurface->commit(Surface::CommitFlag::None); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(parentServerSurface->childSubSurfaces().count(), 1); auto child = parentServerSurface->childSubSurfaces().first(); QCOMPARE(child->parentSurface().data(), parentServerSurface); QCOMPARE(child->mainSurface().data(), parentServerSurface); QCOMPARE(child->surface()->childSubSurfaces().count(), 1); auto child2 = child->surface()->childSubSurfaces().first(); QCOMPARE(child2->parentSurface().data(), child->surface().data()); QCOMPARE(child2->mainSurface().data(), parentServerSurface); QCOMPARE(child2->surface()->childSubSurfaces().count(), 1); auto child3 = child2->surface()->childSubSurfaces().first(); QCOMPARE(child3->parentSurface().data(), child2->surface().data()); QCOMPARE(child3->mainSurface().data(), parentServerSurface); QCOMPARE(child3->surface()->childSubSurfaces().count(), 0); } void TestSubSurface::testRemoveSurface() { // this test verifies that removing the surface also removes the sub-surface from the parent using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer parentSurface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto parentServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(parentServerSurface); QScopedPointer childSurface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childServerSurface); QSignalSpy subSurfaceTreeChangedSpy(parentServerSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subSurfaceTreeChangedSpy.isValid()); m_subCompositor->createSubSurface(childSurface.data(), parentSurface.data()); parentSurface->commit(Surface::CommitFlag::None); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(parentServerSurface->childSubSurfaces().count(), 1); // destroy surface, takes place immediately childSurface.reset(); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(parentServerSurface->childSubSurfaces().count(), 0); } void TestSubSurface::testMappingOfSurfaceTree() { // this test verifies mapping and unmapping of a sub-surface tree using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer parentSurface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto parentServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(parentServerSurface); QScopedPointer childLevel1Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel1ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel1ServerSurface); QScopedPointer childLevel2Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel2ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel2ServerSurface); QScopedPointer childLevel3Surface(m_compositor->createSurface()); QVERIFY(surfaceCreatedSpy.wait()); auto childLevel3ServerSurface = surfaceCreatedSpy.last().first().value(); QVERIFY(childLevel3ServerSurface); QSignalSpy subSurfaceTreeChangedSpy(parentServerSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(subSurfaceTreeChangedSpy.isValid()); auto subSurfaceLevel1 = m_subCompositor->createSubSurface(childLevel1Surface.data(), parentSurface.data()); auto subSurfaceLevel2 = m_subCompositor->createSubSurface(childLevel2Surface.data(), childLevel1Surface.data()); auto subSurfaceLevel3 = m_subCompositor->createSubSurface(childLevel3Surface.data(), childLevel2Surface.data()); parentSurface->commit(Surface::CommitFlag::None); QVERIFY(subSurfaceTreeChangedSpy.wait()); QCOMPARE(parentServerSurface->childSubSurfaces().count(), 1); auto child = parentServerSurface->childSubSurfaces().first(); QCOMPARE(child->surface()->childSubSurfaces().count(), 1); auto child2 = child->surface()->childSubSurfaces().first(); QCOMPARE(child2->surface()->childSubSurfaces().count(), 1); auto child3 = child2->surface()->childSubSurfaces().first(); QCOMPARE(child3->parentSurface().data(), child2->surface().data()); QCOMPARE(child3->mainSurface().data(), parentServerSurface); QCOMPARE(child3->surface()->childSubSurfaces().count(), 0); // so far no surface is mapped QVERIFY(!parentServerSurface->isMapped()); QVERIFY(!child->surface()->isMapped()); QVERIFY(!child2->surface()->isMapped()); QVERIFY(!child3->surface()->isMapped()); // first set all subsurfaces to desync, to simplify subSurfaceLevel1->setMode(SubSurface::Mode::Desynchronized); subSurfaceLevel2->setMode(SubSurface::Mode::Desynchronized); subSurfaceLevel3->setMode(SubSurface::Mode::Desynchronized); // first map the child, should not map it QSignalSpy child3DamageSpy(child3->surface().data(), &SurfaceInterface::damaged); QVERIFY(child3DamageSpy.isValid()); QImage image(QSize(200, 200), QImage::Format_ARGB32); image.fill(Qt::black); childLevel3Surface->attachBuffer(m_shm->createBuffer(image)); childLevel3Surface->damage(QRect(0, 0, 200, 200)); childLevel3Surface->commit(Surface::CommitFlag::None); QVERIFY(child3DamageSpy.wait()); QVERIFY(child3->surface()->buffer()); QVERIFY(!child3->surface()->isMapped()); // let's map the top level QSignalSpy parentSpy(parentServerSurface, &SurfaceInterface::damaged); QVERIFY(parentSpy.isValid()); parentSurface->attachBuffer(m_shm->createBuffer(image)); parentSurface->damage(QRect(0, 0, 200, 200)); parentSurface->commit(Surface::CommitFlag::None); QVERIFY(parentSpy.wait()); QVERIFY(parentServerSurface->isMapped()); // children should not yet be mapped + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child->surface()->isMapped()); + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child2->surface()->isMapped()); + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child3->surface()->isMapped()); // next level QSignalSpy child2DamageSpy(child2->surface().data(), &SurfaceInterface::damaged); QVERIFY(child2DamageSpy.isValid()); childLevel2Surface->attachBuffer(m_shm->createBuffer(image)); childLevel2Surface->damage(QRect(0, 0, 200, 200)); childLevel2Surface->commit(Surface::CommitFlag::None); QVERIFY(child2DamageSpy.wait()); QVERIFY(parentServerSurface->isMapped()); // children should not yet be mapped + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child->surface()->isMapped()); + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child2->surface()->isMapped()); + QEXPECT_FAIL("", "Workaround for QtWayland bug https://bugreports.qt.io/browse/QTBUG-52192", Continue); QVERIFY(!child3->surface()->isMapped()); // last but not least the first child level, which should map all our subsurfaces QSignalSpy child1DamageSpy(child->surface().data(), &SurfaceInterface::damaged); QVERIFY(child1DamageSpy.isValid()); childLevel1Surface->attachBuffer(m_shm->createBuffer(image)); childLevel1Surface->damage(QRect(0, 0, 200, 200)); childLevel1Surface->commit(Surface::CommitFlag::None); QVERIFY(child1DamageSpy.wait()); // everything is mapped QVERIFY(parentServerSurface->isMapped()); QVERIFY(child->surface()->isMapped()); QVERIFY(child2->surface()->isMapped()); QVERIFY(child3->surface()->isMapped()); // unmapping a parent should unmap the complete tree QSignalSpy unmappedSpy(child->surface().data(), &SurfaceInterface::unmapped); QVERIFY(unmappedSpy.isValid()); childLevel1Surface->attachBuffer(Buffer::Ptr()); childLevel1Surface->damage(QRect(0, 0, 200, 200)); childLevel1Surface->commit(Surface::CommitFlag::None); QVERIFY(unmappedSpy.wait()); QVERIFY(parentServerSurface->isMapped()); QVERIFY(!child->surface()->isMapped()); QVERIFY(!child2->surface()->isMapped()); QVERIFY(!child3->surface()->isMapped()); } QTEST_GUILESS_MAIN(TestSubSurface) #include "test_wayland_subsurface.moc" diff --git a/src/server/surface_interface.cpp b/src/server/surface_interface.cpp index 56fc18f..8791f6f 100644 --- a/src/server/surface_interface.cpp +++ b/src/server/surface_interface.cpp @@ -1,686 +1,688 @@ /******************************************************************** Copyright 2014 Martin Gräßlin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #include "surface_interface.h" #include "surface_interface_p.h" #include "buffer_interface.h" #include "clientconnection.h" #include "compositor_interface.h" #include "region_interface.h" #include "subcompositor_interface.h" #include "subsurface_interface_p.h" // Wayland #include // std #include namespace KWayland { namespace Server { SurfaceInterface::Private::Private(SurfaceInterface *q, CompositorInterface *c, wl_resource *parentResource) : Resource::Private(q, c, parentResource, &wl_surface_interface, &s_interface) { } SurfaceInterface::Private::~Private() { destroy(); } void SurfaceInterface::Private::addChild(QPointer< SubSurfaceInterface > child) { // protocol is not precise on how to handle the addition of new sub surfaces pending.children.append(child); subSurfacePending.children.append(child); current.children.append(child); Q_Q(SurfaceInterface); emit q->subSurfaceTreeChanged(); QObject::connect(child.data(), &SubSurfaceInterface::positionChanged, q, &SurfaceInterface::subSurfaceTreeChanged); QObject::connect(child->surface().data(), &SurfaceInterface::damaged, q, &SurfaceInterface::subSurfaceTreeChanged); QObject::connect(child->surface().data(), &SurfaceInterface::unmapped, q, &SurfaceInterface::subSurfaceTreeChanged); QObject::connect(child->surface().data(), &SurfaceInterface::subSurfaceTreeChanged, q, &SurfaceInterface::subSurfaceTreeChanged); } void SurfaceInterface::Private::removeChild(QPointer< SubSurfaceInterface > child) { // protocol is not precise on how to handle the addition of new sub surfaces pending.children.removeAll(child); subSurfacePending.children.removeAll(child); current.children.removeAll(child); Q_Q(SurfaceInterface); emit q->subSurfaceTreeChanged(); QObject::disconnect(child.data(), &SubSurfaceInterface::positionChanged, q, &SurfaceInterface::subSurfaceTreeChanged); if (!child->surface().isNull()) { QObject::disconnect(child->surface().data(), &SurfaceInterface::damaged, q, &SurfaceInterface::subSurfaceTreeChanged); QObject::disconnect(child->surface().data(), &SurfaceInterface::unmapped, q, &SurfaceInterface::subSurfaceTreeChanged); QObject::disconnect(child->surface().data(), &SurfaceInterface::subSurfaceTreeChanged, q, &SurfaceInterface::subSurfaceTreeChanged); } } bool SurfaceInterface::Private::raiseChild(QPointer subsurface, SurfaceInterface *sibling) { Q_Q(SurfaceInterface); auto it = std::find(pending.children.begin(), pending.children.end(), subsurface); if (it == pending.children.end()) { return false; } if (pending.children.count() == 1) { // nothing to do return true; } if (sibling == q) { // it's to the parent, so needs to become last item pending.children.append(*it); pending.children.erase(it); pending.childrenChanged = true; return true; } if (!sibling->subSurface()) { // not a sub surface return false; } auto siblingIt = std::find(pending.children.begin(), pending.children.end(), sibling->subSurface()); if (siblingIt == pending.children.end() || siblingIt == it) { // not a sibling return false; } auto value = (*it); pending.children.erase(it); // find the iterator again siblingIt = std::find(pending.children.begin(), pending.children.end(), sibling->subSurface()); pending.children.insert(++siblingIt, value); pending.childrenChanged = true; return true; } bool SurfaceInterface::Private::lowerChild(QPointer subsurface, SurfaceInterface *sibling) { Q_Q(SurfaceInterface); auto it = std::find(pending.children.begin(), pending.children.end(), subsurface); if (it == pending.children.end()) { return false; } if (pending.children.count() == 1) { // nothing to do return true; } if (sibling == q) { // it's to the parent, so needs to become first item auto value = *it; pending.children.erase(it); pending.children.prepend(value); pending.childrenChanged = true; return true; } if (!sibling->subSurface()) { // not a sub surface return false; } auto siblingIt = std::find(pending.children.begin(), pending.children.end(), sibling->subSurface()); if (siblingIt == pending.children.end() || siblingIt == it) { // not a sibling return false; } auto value = (*it); pending.children.erase(it); // find the iterator again siblingIt = std::find(pending.children.begin(), pending.children.end(), sibling->subSurface()); pending.children.insert(siblingIt, value); pending.childrenChanged = true; return true; } void SurfaceInterface::Private::setShadow(const QPointer &shadow) { pending.shadow = shadow; pending.shadowIsSet = true; } void SurfaceInterface::Private::setBlur(const QPointer &blur) { pending.blur = blur; pending.blurIsSet = true; } void SurfaceInterface::Private::setSlide(const QPointer &slide) { pending.slide = slide; pending.slideIsSet = true; } void SurfaceInterface::Private::setContrast(const QPointer &contrast) { pending.contrast = contrast; pending.contrastIsSet = true; } #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_surface_interface SurfaceInterface::Private::s_interface = { destroyCallback, attachCallback, damageCallback, frameCallaback, opaqueRegionCallback, inputRegionCallback, commitCallback, bufferTransformCallback, bufferScaleCallback }; #endif SurfaceInterface::SurfaceInterface(CompositorInterface *parent, wl_resource *parentResource) : Resource(new Private(this, parent, parentResource)) { } SurfaceInterface::~SurfaceInterface() = default; void SurfaceInterface::frameRendered(quint32 msec) { Q_D(); // notify all callbacks const bool needsFlush = !d->current.callbacks.isEmpty(); while (!d->current.callbacks.isEmpty()) { wl_resource *r = d->current.callbacks.takeFirst(); wl_callback_send_done(r, msec); wl_resource_destroy(r); } for (auto it = d->current.children.constBegin(); it != d->current.children.constEnd(); ++it) { const auto &subSurface = *it; if (subSurface.isNull() || subSurface->d_func()->surface.isNull()) { continue; } subSurface->d_func()->surface->frameRendered(msec); } if (needsFlush) { client()->flush(); } } void SurfaceInterface::Private::destroy() { for (wl_resource *c : current.callbacks) { wl_resource_destroy(c); } for (wl_resource *c : pending.callbacks) { wl_resource_destroy(c); } for (wl_resource *c : subSurfacePending.callbacks) { wl_resource_destroy(c); } if (current.buffer) { current.buffer->unref(); } if (resource) { wl_resource_destroy(resource); resource = nullptr; } } void SurfaceInterface::Private::swapStates(State *source, State *target, bool emitChanged) { Q_Q(SurfaceInterface); bool bufferChanged = source->bufferIsSet; const bool opaqueRegionChanged = source->opaqueIsSet; const bool inputRegionChanged = source->inputIsSet; const bool scaleFactorChanged = source->scaleIsSet && (target->scale != source->scale); const bool transformChanged = source->transformIsSet && (target->transform != source->transform); const bool shadowChanged = source->shadowIsSet; const bool blurChanged = source->blurIsSet; const bool contrastChanged = source->contrastIsSet; const bool slideChanged = source->slideIsSet; const bool childrenChanged = source->childrenChanged; bool sizeChanged = false; auto buffer = target->buffer; if (bufferChanged) { // TODO: is the reffing correct for subsurfaces? QSize oldSize; if (target->buffer) { oldSize = target->buffer->size(); if (emitChanged) { target->buffer->unref(); QObject::disconnect(target->buffer, &BufferInterface::sizeChanged, q, &SurfaceInterface::sizeChanged); } else { delete target->buffer; target->buffer = nullptr; } } if (source->buffer) { if (emitChanged) { source->buffer->ref(); QObject::connect(source->buffer, &BufferInterface::sizeChanged, q, &SurfaceInterface::sizeChanged); } const QSize newSize = source->buffer->size(); sizeChanged = newSize.isValid() && newSize != oldSize; } if (!target->buffer && !source->buffer && emitChanged) { // null buffer set on a not mapped surface, don't emit unmapped bufferChanged = false; } buffer = source->buffer; } // copy values if (bufferChanged) { target->buffer = buffer; target->damage = source->damage; target->bufferIsSet = source->bufferIsSet; } if (childrenChanged) { target->childrenChanged = source->childrenChanged; target->children = source->children; } target->callbacks.append(source->callbacks); if (shadowChanged) { target->shadow = source->shadow; target->shadowIsSet = true; } if (blurChanged) { target->blur = source->blur; target->blurIsSet = true; } if (contrastChanged) { target->contrast = source->contrast; target->contrastIsSet = true; } if (slideChanged) { target->slide = source->slide; target->slideIsSet = true; } if (inputRegionChanged) { target->input = source->input; target->inputIsInfinite = source->inputIsInfinite; target->inputIsSet = true; } if (opaqueRegionChanged) { target->opaque = source->opaque; target->opaqueIsSet = true; } if (scaleFactorChanged) { target->scale = source->scale; target->scaleIsSet = true; } if (transformChanged) { target->transform = source->transform; target->transformIsSet = true; } *source = State{}; source->children = target->children; if (opaqueRegionChanged) { emit q->opaqueChanged(target->opaque); } if (inputRegionChanged) { emit q->inputChanged(target->input); } if (scaleFactorChanged) { emit q->scaleChanged(target->scale); } if (transformChanged) { emit q->transformChanged(target->transform); } if (bufferChanged && emitChanged) { if (!target->damage.isEmpty()) { const QRegion windowRegion = QRegion(0, 0, q->size().width(), q->size().height()); if (!windowRegion.isEmpty()) { target->damage = windowRegion.intersected(target->damage); if (emitChanged) { + subSurfaceIsMapped = true; emit q->damaged(target->damage); // workaround for https://bugreports.qt.io/browse/QTBUG-52092 // if the surface is a sub-surface, but the main surface is not yet mapped, fake frame rendered if (subSurface && !subSurface->mainSurface()->buffer()) { q->frameRendered(0); } } } } else if (!target->buffer && emitChanged) { + subSurfaceIsMapped = false; emit q->unmapped(); } } if (!emitChanged) { return; } if (sizeChanged) { emit q->sizeChanged(); } if (shadowChanged) { emit q->shadowChanged(); } if (blurChanged) { emit q->blurChanged(); } if (contrastChanged) { emit q->contrastChanged(); } if (slideChanged) { emit q->slideOnShowHideChanged(); } if (childrenChanged) { emit q->subSurfaceTreeChanged(); } } void SurfaceInterface::Private::commit() { if (!subSurface.isNull() && subSurface->isSynchronized()) { swapStates(&pending, &subSurfacePending, false); } else { swapStates(&pending, ¤t, true); if (!subSurface.isNull()) { subSurface->d_func()->commit(); } // commit all subSurfaces to apply position changes // "The cached state is applied to the sub-surface immediately after the parent surface's state is applied" for (auto it = current.children.constBegin(); it != current.children.constEnd(); ++it) { const auto &subSurface = *it; if (subSurface.isNull()) { continue; } subSurface->d_func()->commit(); } } } void SurfaceInterface::Private::commitSubSurface() { if (subSurface.isNull() || !subSurface->isSynchronized()) { return; } swapStates(&subSurfacePending, ¤t, true); // "The cached state is applied to the sub-surface immediately after the parent surface's state is applied" for (auto it = current.children.constBegin(); it != current.children.constEnd(); ++it) { const auto &subSurface = *it; if (subSurface.isNull() || !subSurface->isSynchronized()) { continue; } subSurface->d_func()->commit(); } } void SurfaceInterface::Private::damage(const QRect &rect) { if (!pending.bufferIsSet || (pending.bufferIsSet && !pending.buffer)) { // TODO: should we send an error? return; } pending.damage = pending.damage.united(rect); } void SurfaceInterface::Private::setScale(qint32 scale) { pending.scale = scale; pending.scaleIsSet = true; } void SurfaceInterface::Private::setTransform(OutputInterface::Transform transform) { pending.transform = transform; } void SurfaceInterface::Private::addFrameCallback(uint32_t callback) { wl_resource *r = client->createResource(&wl_callback_interface, 1, callback); if (!r) { wl_resource_post_no_memory(resource); return; } wl_resource_set_implementation(r, nullptr, this, destroyFrameCallback); pending.callbacks << r; } void SurfaceInterface::Private::attachBuffer(wl_resource *buffer, const QPoint &offset) { pending.bufferIsSet = true; pending.offset = offset; if (pending.buffer) { delete pending.buffer; } if (!buffer) { // got a null buffer, deletes content in next frame pending.buffer = nullptr; pending.damage = QRegion(); return; } Q_Q(SurfaceInterface); pending.buffer = new BufferInterface(buffer, q); QObject::connect(pending.buffer, &BufferInterface::aboutToBeDestroyed, q, [this](BufferInterface *buffer) { if (pending.buffer == buffer) { pending.buffer = nullptr; } if (subSurfacePending.buffer == buffer) { subSurfacePending.buffer = nullptr; } if (current.buffer == buffer) { current.buffer->unref(); current.buffer = nullptr; } } ); } void SurfaceInterface::Private::destroyFrameCallback(wl_resource *r) { auto s = cast(r); s->current.callbacks.removeAll(r); s->pending.callbacks.removeAll(r); s->subSurfacePending.callbacks.removeAll(r); } void SurfaceInterface::Private::destroyCallback(wl_client *client, wl_resource *resource) { Q_UNUSED(client) wl_resource_destroy(resource); } void SurfaceInterface::Private::attachCallback(wl_client *client, wl_resource *resource, wl_resource *buffer, int32_t sx, int32_t sy) { Q_UNUSED(client) cast(resource)->attachBuffer(buffer, QPoint(sx, sy)); } void SurfaceInterface::Private::damageCallback(wl_client *client, wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) { Q_UNUSED(client) cast(resource)->damage(QRect(x, y, width, height)); } void SurfaceInterface::Private::frameCallaback(wl_client *client, wl_resource *resource, uint32_t callback) { auto s = cast(resource); Q_ASSERT(client == *s->client); s->addFrameCallback(callback); } void SurfaceInterface::Private::opaqueRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region) { auto s = cast(resource); Q_ASSERT(client == *s->client); auto r = RegionInterface::get(region); s->setOpaque(r ? r->region() : QRegion()); } void SurfaceInterface::Private::setOpaque(const QRegion ®ion) { pending.opaqueIsSet = true; pending.opaque = region; } void SurfaceInterface::Private::inputRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region) { auto s = cast(resource); Q_ASSERT(client == *s->client); auto r = RegionInterface::get(region); s->setInput(r ? r->region() : QRegion(), !r); } void SurfaceInterface::Private::setInput(const QRegion ®ion, bool isInfinite) { pending.inputIsSet = true; pending.inputIsInfinite = isInfinite; pending.input = region; } void SurfaceInterface::Private::commitCallback(wl_client *client, wl_resource *resource) { Q_UNUSED(client) cast(resource)->commit(); } void SurfaceInterface::Private::bufferTransformCallback(wl_client *client, wl_resource *resource, int32_t transform) { Q_UNUSED(client) cast(resource)->setTransform(OutputInterface::Transform(transform)); } void SurfaceInterface::Private::bufferScaleCallback(wl_client *client, wl_resource *resource, int32_t scale) { Q_UNUSED(client) cast(resource)->setScale(scale); } QRegion SurfaceInterface::damage() const { Q_D(); return d->current.damage; } QRegion SurfaceInterface::opaque() const { Q_D(); return d->current.opaque; } QRegion SurfaceInterface::input() const { Q_D(); return d->current.input; } bool SurfaceInterface::inputIsInfitine() const { return inputIsInfinite(); } bool SurfaceInterface::inputIsInfinite() const { Q_D(); return d->current.inputIsInfinite; } qint32 SurfaceInterface::scale() const { Q_D(); return d->current.scale; } OutputInterface::Transform SurfaceInterface::transform() const { Q_D(); return d->current.transform; } BufferInterface *SurfaceInterface::buffer() { Q_D(); return d->current.buffer; } QPoint SurfaceInterface::offset() const { Q_D(); return d->current.offset; } SurfaceInterface *SurfaceInterface::get(wl_resource *native) { return Private::get(native); } SurfaceInterface *SurfaceInterface::get(quint32 id, const ClientConnection *client) { return Private::get(id, client); } QList< QPointer< SubSurfaceInterface > > SurfaceInterface::childSubSurfaces() const { Q_D(); return d->current.children; } QPointer< SubSurfaceInterface > SurfaceInterface::subSurface() const { Q_D(); return d->subSurface; } QSize SurfaceInterface::size() const { Q_D(); // TODO: apply transform and scale to the buffer size if (d->current.buffer) { return d->current.buffer->size(); } return QSize(); } QPointer< ShadowInterface > SurfaceInterface::shadow() const { Q_D(); return d->current.shadow; } QPointer< BlurInterface > SurfaceInterface::blur() const { Q_D(); return d->current.blur; } QPointer< ContrastInterface > SurfaceInterface::contrast() const { Q_D(); return d->current.contrast; } QPointer< SlideInterface > SurfaceInterface::slideOnShowHide() const { Q_D(); return d->current.slide; } bool SurfaceInterface::isMapped() const { Q_D(); if (d->subSurface) { // from spec: // "A sub-surface becomes mapped, when a non-NULL wl_buffer is applied and the parent surface is mapped." - return d->current.buffer && !d->subSurface->parentSurface().isNull() && d->subSurface->parentSurface()->isMapped(); + return d->subSurfaceIsMapped && !d->subSurface->parentSurface().isNull() && d->subSurface->parentSurface()->isMapped(); } return d->current.buffer != nullptr; } SurfaceInterface::Private *SurfaceInterface::d_func() const { return reinterpret_cast(d.data()); } } } diff --git a/src/server/surface_interface_p.h b/src/server/surface_interface_p.h index 301248a..86e8186 100644 --- a/src/server/surface_interface_p.h +++ b/src/server/surface_interface_p.h @@ -1,118 +1,124 @@ /******************************************************************** Copyright 2014 Martin Gräßlin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #ifndef WAYLAND_SERVER_SURFACE_INTERFACE_P_H #define WAYLAND_SERVER_SURFACE_INTERFACE_P_H #include "surface_interface.h" #include "resource_p.h" // Wayland #include namespace KWayland { namespace Server { class SurfaceInterface::Private : public Resource::Private { public: struct State { QRegion damage = QRegion(); QRegion opaque = QRegion(); QRegion input = QRegion(); bool inputIsSet = false; bool opaqueIsSet = false; bool bufferIsSet = false; bool shadowIsSet = false; bool blurIsSet = false; bool contrastIsSet = false; bool slideIsSet = false; bool inputIsInfinite = true; bool childrenChanged = false; bool scaleIsSet = false; bool transformIsSet = false; qint32 scale = 1; OutputInterface::Transform transform = OutputInterface::Transform::Normal; QList callbacks = QList(); QPoint offset = QPoint(); BufferInterface *buffer = nullptr; // stacking order: bottom (first) -> top (last) QList> children; QPointer shadow; QPointer blur; QPointer contrast; QPointer slide; }; Private(SurfaceInterface *q, CompositorInterface *c, wl_resource *parentResource); ~Private(); void destroy(); void addChild(QPointer subsurface); void removeChild(QPointer subsurface); bool raiseChild(QPointer subsurface, SurfaceInterface *sibling); bool lowerChild(QPointer subsurface, SurfaceInterface *sibling); void setShadow(const QPointer &shadow); void setBlur(const QPointer &blur); void setContrast(const QPointer &contrast); void setSlide(const QPointer &slide); void commitSubSurface(); void commit(); State current; State pending; State subSurfacePending; QPointer subSurface; + // workaround for https://bugreports.qt.io/browse/QTBUG-52192 + // A subsurface needs to be considered mapped even if it doesn't have a buffer attached + // Otherwise Qt's sub-surfaces will never be visible and the client will freeze due to + // waiting on the frame callback of the never visible surface + bool subSurfaceIsMapped = true; + private: SurfaceInterface *q_func() { return reinterpret_cast(q); } void swapStates(State *source, State *target, bool emitChanged); void damage(const QRect &rect); void setScale(qint32 scale); void setTransform(OutputInterface::Transform transform); void addFrameCallback(uint32_t callback); void attachBuffer(wl_resource *buffer, const QPoint &offset); void setOpaque(const QRegion ®ion); void setInput(const QRegion ®ion, bool isInfinite); static void destroyFrameCallback(wl_resource *r); static void destroyCallback(wl_client *client, wl_resource *resource); static void attachCallback(wl_client *client, wl_resource *resource, wl_resource *buffer, int32_t sx, int32_t sy); static void damageCallback(wl_client *client, wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height); static void frameCallaback(wl_client *client, wl_resource *resource, uint32_t callback); static void opaqueRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region); static void inputRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region); static void commitCallback(wl_client *client, wl_resource *resource); // since version 2 static void bufferTransformCallback(wl_client *client, wl_resource *resource, int32_t transform); // since version 3 static void bufferScaleCallback(wl_client *client, wl_resource *resource, int32_t scale); static const struct wl_surface_interface s_interface; }; } } #endif