diff --git a/autotests/client/test_wayland_subsurface.cpp b/autotests/client/test_wayland_subsurface.cpp index 42c326a..dc53a1b 100644 --- a/autotests/client/test_wayland_subsurface.cpp +++ b/autotests/client/test_wayland_subsurface.cpp @@ -1,1045 +1,1092 @@ /******************************************************************** 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(); void testSurfaceAt(); void testDestroyAttachedBuffer(); + void testDestroyParentSurface(); 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_Premultiplied); 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_Premultiplied); 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_Premultiplied); 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_Premultiplied); 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()); } void TestSubSurface::testSurfaceAt() { // this test verifies that the correct surface is picked in a sub-surface tree using namespace KWayland::Client; using namespace KWayland::Server; // first create a parent surface and map it QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(serverSurfaceCreated.isValid()); QScopedPointer parent(m_compositor->createSurface()); QImage image(QSize(100, 100), QImage::Format_ARGB32_Premultiplied); image.fill(Qt::red); parent->attachBuffer(m_shm->createBuffer(image)); parent->damage(QRect(0, 0, 100, 100)); parent->commit(Surface::CommitFlag::None); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *parentServerSurface = serverSurfaceCreated.last().first().value(); // create two child sub surfaces, those won't be mapped, just added to the parent // this is to simulate the behavior of QtWayland QScopedPointer directChild1(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *directChild1ServerSurface = serverSurfaceCreated.last().first().value(); QVERIFY(directChild1ServerSurface); QScopedPointer directChild2(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *directChild2ServerSurface = serverSurfaceCreated.last().first().value(); QVERIFY(directChild2ServerSurface); // create the sub surfaces for them QScopedPointer directChild1SubSurface(m_subCompositor->createSubSurface(directChild1.data(), parent.data())); directChild1SubSurface->setMode(SubSurface::Mode::Desynchronized); QScopedPointer directChild2SubSurface(m_subCompositor->createSubSurface(directChild2.data(), parent.data())); directChild2SubSurface->setMode(SubSurface::Mode::Desynchronized); // each of the children gets a child QScopedPointer childFor1(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *childFor1ServerSurface = serverSurfaceCreated.last().first().value(); QScopedPointer childFor2(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *childFor2ServerSurface = serverSurfaceCreated.last().first().value(); // create sub surfaces for them QScopedPointer childFor1SubSurface(m_subCompositor->createSubSurface(childFor1.data(), directChild1.data())); childFor1SubSurface->setMode(SubSurface::Mode::Desynchronized); QScopedPointer childFor2SubSurface(m_subCompositor->createSubSurface(childFor2.data(), directChild2.data())); childFor2SubSurface->setMode(SubSurface::Mode::Desynchronized); // both get a quarter of the grand-parent surface childFor2SubSurface->setPosition(QPoint(50, 50)); childFor2->commit(Surface::CommitFlag::None); directChild2->commit(Surface::CommitFlag::None); parent->commit(Surface::CommitFlag::None); // now let's render both grand children QImage partImage(QSize(50, 50), QImage::Format_ARGB32_Premultiplied); partImage.fill(Qt::green); childFor1->attachBuffer(m_shm->createBuffer(partImage)); childFor1->damage(QRect(0, 0, 50, 50)); childFor1->commit(Surface::CommitFlag::None); partImage.fill(Qt::blue); childFor2->attachBuffer(m_shm->createBuffer(partImage)); childFor2->damage(QRect(0, 0, 50, 50)); childFor2->commit(Surface::CommitFlag::None); QSignalSpy treeChangedSpy(parentServerSurface, &SurfaceInterface::subSurfaceTreeChanged); QVERIFY(treeChangedSpy.isValid()); QVERIFY(treeChangedSpy.wait()); QCOMPARE(directChild1ServerSurface->subSurface()->parentSurface().data(), parentServerSurface); QCOMPARE(directChild2ServerSurface->subSurface()->parentSurface().data(), parentServerSurface); QCOMPARE(childFor1ServerSurface->subSurface()->parentSurface().data(), directChild1ServerSurface); QCOMPARE(childFor2ServerSurface->subSurface()->parentSurface().data(), directChild2ServerSurface); // now let's test a few positions QCOMPARE(parentServerSurface->surfaceAt(QPointF(0, 0)), childFor1ServerSurface); QCOMPARE(parentServerSurface->surfaceAt(QPointF(49, 49)), childFor1ServerSurface); QCOMPARE(parentServerSurface->surfaceAt(QPointF(50, 50)), childFor2ServerSurface); QCOMPARE(parentServerSurface->surfaceAt(QPointF(100, 100)), childFor2ServerSurface); QCOMPARE(parentServerSurface->surfaceAt(QPointF(25, 75)), parentServerSurface); QCOMPARE(parentServerSurface->surfaceAt(QPointF(75, 25)), parentServerSurface); // outside the geometries should be no surface QVERIFY(!parentServerSurface->surfaceAt(QPointF(-1, -1))); QVERIFY(!parentServerSurface->surfaceAt(QPointF(101, 101))); } void TestSubSurface::testDestroyAttachedBuffer() { // this test verifies that destroying of a buffer attached to a sub-surface works using namespace KWayland::Client; using namespace KWayland::Server; // create surface QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(serverSurfaceCreated.isValid()); QScopedPointer parent(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); QScopedPointer child(m_compositor->createSurface()); QVERIFY(serverSurfaceCreated.wait()); SurfaceInterface *serverChildSurface = serverSurfaceCreated.last().first().value(); // create sub-surface m_subCompositor->createSubSurface(child.data(), parent.data()); // let's damage this surface, will be in sub-surface pending state QImage image(QSize(100, 100), QImage::Format_ARGB32_Premultiplied); image.fill(Qt::red); child->attachBuffer(m_shm->createBuffer(image)); child->damage(QRect(0, 0, 100, 100)); child->commit(Surface::CommitFlag::None); m_connection->flush(); // Let's try to destroy it QSignalSpy destroySpy(serverChildSurface, &QObject::destroyed); QVERIFY(destroySpy.isValid()); delete m_shm; m_shm = nullptr; child.reset(); QVERIFY(destroySpy.wait()); } +void TestSubSurface::testDestroyParentSurface() +{ + // this test verifies that destroying a parent surface does not create problems + // see BUG 389231 + using namespace KWayland::Client; + using namespace KWayland::Server; + // create surface + QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated); + QVERIFY(serverSurfaceCreated.isValid()); + QScopedPointer parent(m_compositor->createSurface()); + QVERIFY(serverSurfaceCreated.wait()); + SurfaceInterface *serverParentSurface = serverSurfaceCreated.last().first().value(); + QScopedPointer child(m_compositor->createSurface()); + QVERIFY(serverSurfaceCreated.wait()); + SurfaceInterface *serverChildSurface = serverSurfaceCreated.last().first().value(); + QScopedPointer grandChild(m_compositor->createSurface()); + QVERIFY(serverSurfaceCreated.wait()); + SurfaceInterface *serverGrandChildSurface = serverSurfaceCreated.last().first().value(); + // create sub-surface in desynchronized mode as Qt uses them + auto sub1 = m_subCompositor->createSubSurface(child.data(), parent.data()); + sub1->setMode(SubSurface::Mode::Desynchronized); + auto sub2 = m_subCompositor->createSubSurface(grandChild.data(), child.data()); + sub2->setMode(SubSurface::Mode::Desynchronized); + + // let's damage this surface + // and at the same time delete the parent surface + parent.reset(); + QSignalSpy parentDestroyedSpy(serverParentSurface, &QObject::destroyed); + QVERIFY(parentDestroyedSpy.isValid()); + QVERIFY(parentDestroyedSpy.wait()); + QImage image(QSize(100, 100), QImage::Format_ARGB32_Premultiplied); + image.fill(Qt::red); + grandChild->attachBuffer(m_shm->createBuffer(image)); + grandChild->damage(QRect(0, 0, 100, 100)); + grandChild->commit(Surface::CommitFlag::None); + QSignalSpy damagedSpy(serverGrandChildSurface, &SurfaceInterface::damaged); + QVERIFY(damagedSpy.isValid()); + QVERIFY(damagedSpy.wait()); + + // Let's try to destroy it + QSignalSpy destroySpy(serverChildSurface, &QObject::destroyed); + QVERIFY(destroySpy.isValid()); + child.reset(); + QVERIFY(destroySpy.wait()); +} + QTEST_GUILESS_MAIN(TestSubSurface) #include "test_wayland_subsurface.moc" diff --git a/src/server/subcompositor_interface.cpp b/src/server/subcompositor_interface.cpp index f829ab2..f2b6de9 100644 --- a/src/server/subcompositor_interface.cpp +++ b/src/server/subcompositor_interface.cpp @@ -1,375 +1,378 @@ /******************************************************************** 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 "subcompositor_interface.h" #include "subsurface_interface_p.h" #include "global_p.h" #include "display.h" #include "surface_interface_p.h" // Wayland #include namespace KWayland { namespace Server { class SubCompositorInterface::Private : public Global::Private { public: Private(SubCompositorInterface *q, Display *d); private: void bind(wl_client *client, uint32_t version, uint32_t id) override; void subsurface(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface, wl_resource *parent); static void unbind(wl_resource *resource); static void destroyCallback(wl_client *client, wl_resource *resource); static void subsurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface, wl_resource *parent); static Private *cast(wl_resource *r) { return reinterpret_cast(wl_resource_get_user_data(r)); } SubCompositorInterface *q; static const struct wl_subcompositor_interface s_interface; static const quint32 s_version; }; const quint32 SubCompositorInterface::Private::s_version = 1; #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_subcompositor_interface SubCompositorInterface::Private::s_interface = { destroyCallback, subsurfaceCallback }; #endif SubCompositorInterface::Private::Private(SubCompositorInterface *q, Display *d) : Global::Private(d, &wl_subcompositor_interface, s_version) , q(q) { } void SubCompositorInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id) { auto c = display->getConnection(client); wl_resource *resource = c->createResource(&wl_subcompositor_interface, qMin(version, s_version), id); if (!resource) { wl_client_post_no_memory(client); return; } wl_resource_set_implementation(resource, &s_interface, this, unbind); } void SubCompositorInterface::Private::unbind(wl_resource *resource) { Q_UNUSED(resource) } void SubCompositorInterface::Private::destroyCallback(wl_client *client, wl_resource *resource) { Q_UNUSED(client) Q_UNUSED(resource) wl_resource_destroy(resource); } void SubCompositorInterface::Private::subsurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface, wl_resource *sparent) { cast(resource)->subsurface(client, resource, id, surface, sparent); } void SubCompositorInterface::Private::subsurface(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *nativeSurface, wl_resource *nativeParentSurface) { Q_UNUSED(client) SurfaceInterface *surface = SurfaceInterface::get(nativeSurface); SurfaceInterface *parentSurface = SurfaceInterface::get(nativeParentSurface); if (!surface || !parentSurface) { wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "Surface or parent surface not found"); return; } if (surface == parentSurface) { wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "Cannot become sub composite to same surface"); return; } // TODO: add check that surface is not already used in an interface (e.g. Shell) // TODO: add check that parentSurface is not a child of surface SubSurfaceInterface *s = new SubSurfaceInterface(q, resource); s->d_func()->create(display->getConnection(client), wl_resource_get_version(resource), id, surface, parentSurface); if (!s->resource()) { wl_resource_post_no_memory(resource); delete s; return; } emit q->subSurfaceCreated(s); } SubCompositorInterface::SubCompositorInterface(Display *display, QObject *parent) : Global(new Private(this, display), parent) { } SubCompositorInterface::~SubCompositorInterface() = default; #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_subsurface_interface SubSurfaceInterface::Private::s_interface = { resourceDestroyedCallback, setPositionCallback, placeAboveCallback, placeBelowCallback, setSyncCallback, setDeSyncCallback }; #endif SubSurfaceInterface::Private::Private(SubSurfaceInterface *q, SubCompositorInterface *compositor, wl_resource *parentResource) : Resource::Private(q, compositor, parentResource, &wl_subsurface_interface, &s_interface) { } SubSurfaceInterface::Private::~Private() { // no need to notify the surface as it's tracking a QPointer which will be reset automatically if (parent) { Q_Q(SubSurfaceInterface); reinterpret_cast(parent->d.data())->removeChild(QPointer(q)); } } void SubSurfaceInterface::Private::create(ClientConnection *client, quint32 version, quint32 id, SurfaceInterface *s, SurfaceInterface *p) { create(client, version, id); if (!resource) { return; } surface = s; parent = p; Q_Q(SubSurfaceInterface); surface->d_func()->subSurface = QPointer(q); // copy current state to subSurfacePending state // it's the reference for all new pending state which needs to be committed surface->d_func()->subSurfacePending = surface->d_func()->current; surface->d_func()->subSurfacePending.blurIsSet = false; surface->d_func()->subSurfacePending.bufferIsSet = false; surface->d_func()->subSurfacePending.childrenChanged = false; surface->d_func()->subSurfacePending.contrastIsSet = false; surface->d_func()->subSurfacePending.callbacks.clear(); surface->d_func()->subSurfacePending.inputIsSet = false; surface->d_func()->subSurfacePending.inputIsInfinite = true; surface->d_func()->subSurfacePending.opaqueIsSet = false; surface->d_func()->subSurfacePending.shadowIsSet = false; surface->d_func()->subSurfacePending.slideIsSet = false; parent->d_func()->addChild(QPointer(q)); QObject::connect(surface.data(), &QObject::destroyed, q, [this] { // from spec: "If the wl_surface associated with the wl_subsurface is destroyed, // the wl_subsurface object becomes inert. Note, that destroying either object // takes effect immediately." if (parent) { Q_Q(SubSurfaceInterface); reinterpret_cast(parent->d.data())->removeChild(QPointer(q)); } } ); } void SubSurfaceInterface::Private::commit() { if (scheduledPosChange) { scheduledPosChange = false; pos = scheduledPos; scheduledPos = QPoint(); Q_Q(SubSurfaceInterface); emit q->positionChanged(pos); } if (surface) { surface->d_func()->commitSubSurface(); } } void SubSurfaceInterface::Private::setPositionCallback(wl_client *client, wl_resource *resource, int32_t x, int32_t y) { Q_UNUSED(client) // TODO: is this a fixed position? cast(resource)->setPosition(QPoint(x, y)); } void SubSurfaceInterface::Private::setPosition(const QPoint &p) { Q_Q(SubSurfaceInterface); if (!q->isSynchronized()) { // workaround for https://bugreports.qt.io/browse/QTBUG-52118 // apply directly as Qt doesn't commit the parent surface pos = p; emit q->positionChanged(pos); return; } if (scheduledPos == p) { return; } scheduledPos = p; scheduledPosChange = true; } void SubSurfaceInterface::Private::placeAboveCallback(wl_client *client, wl_resource *resource, wl_resource *sibling) { Q_UNUSED(client) cast(resource)->placeAbove(SurfaceInterface::get(sibling)); } void SubSurfaceInterface::Private::placeAbove(SurfaceInterface *sibling) { if (parent.isNull()) { // TODO: raise error return; } Q_Q(SubSurfaceInterface); if (!parent->d_func()->raiseChild(QPointer(q), sibling)) { wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "Incorrect sibling"); } } void SubSurfaceInterface::Private::placeBelowCallback(wl_client *client, wl_resource *resource, wl_resource *sibling) { Q_UNUSED(client) cast(resource)->placeBelow(SurfaceInterface::get(sibling)); } void SubSurfaceInterface::Private::placeBelow(SurfaceInterface *sibling) { if (parent.isNull()) { // TODO: raise error return; } Q_Q(SubSurfaceInterface); if (!parent->d_func()->lowerChild(QPointer(q), sibling)) { wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "Incorrect sibling"); } } void SubSurfaceInterface::Private::setSyncCallback(wl_client *client, wl_resource *resource) { Q_UNUSED(client) cast(resource)->setMode(Mode::Synchronized); } void SubSurfaceInterface::Private::setDeSyncCallback(wl_client *client, wl_resource *resource) { Q_UNUSED(client) cast(resource)->setMode(Mode::Desynchronized); } void SubSurfaceInterface::Private::setMode(Mode m) { if (mode == m) { return; } if (m == Mode::Desynchronized && (!parent->subSurface() || !parent->subSurface()->isSynchronized())) { // no longer synchronized, this is like calling commit if (surface) { surface->d_func()->commit(); surface->d_func()->commitSubSurface(); } } mode = m; Q_Q(SubSurfaceInterface); emit q->modeChanged(m); } SubSurfaceInterface::SubSurfaceInterface(SubCompositorInterface *parent, wl_resource *parentResource) : Resource(new Private(this, parent, parentResource)) { Q_UNUSED(parent) } SubSurfaceInterface::~SubSurfaceInterface() = default; QPoint SubSurfaceInterface::position() const { Q_D(); return d->pos; } QPointer SubSurfaceInterface::surface() { // TODO: remove with ABI break (KF6) Q_D(); return d->surface; } QPointer SubSurfaceInterface::surface() const { Q_D(); return d->surface; } QPointer SubSurfaceInterface::parentSurface() { // TODO: remove with ABI break (KF6) Q_D(); return d->parent; } QPointer SubSurfaceInterface::parentSurface() const { Q_D(); return d->parent; } SubSurfaceInterface::Mode SubSurfaceInterface::mode() const { Q_D(); return d->mode; } bool SubSurfaceInterface::isSynchronized() const { Q_D(); if (d->mode == Mode::Synchronized) { return true; } if (d->parent.isNull()) { // that shouldn't happen, but let's assume false return false; } if (!d->parent->subSurface().isNull()) { // follow parent's mode return d->parent->subSurface()->isSynchronized(); } // parent is no subsurface, thus parent is in desync mode and this surface is in desync mode return false; } QPointer SubSurfaceInterface::mainSurface() const { Q_D(); + if (!d->parent) { + return QPointer(); + } if (d->parent->d_func()->subSurface) { return d->parent->d_func()->subSurface->mainSurface(); } return d->parent; } SubSurfaceInterface::Private *SubSurfaceInterface::d_func() const { return reinterpret_cast(d.data()); } } } diff --git a/src/server/surface_interface.cpp b/src/server/surface_interface.cpp index 5102271..3787794 100644 --- a/src/server/surface_interface.cpp +++ b/src/server/surface_interface.cpp @@ -1,888 +1,891 @@ /******************************************************************** 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 "idleinhibit_interface_p.h" #include "pointerconstraints_interface_p.h" #include "region_interface.h" #include "subcompositor_interface.h" #include "subsurface_interface_p.h" // Qt #include // 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; } void SurfaceInterface::Private::installPointerConstraint(LockedPointerInterface *lock) { Q_ASSERT(lockedPointer.isNull()); Q_ASSERT(confinedPointer.isNull()); lockedPointer = QPointer(lock); if (lock->lifeTime() == LockedPointerInterface::LifeTime::OneShot) { constrainsOneShotConnection = QObject::connect(lock, &LockedPointerInterface::lockedChanged, q_func(), [this] { if (lockedPointer.isNull()) { return; } if (!lockedPointer->isLocked()) { lockedPointer.clear(); disconnect(constrainsOneShotConnection); constrainsOneShotConnection = QMetaObject::Connection(); disconnect(constrainsUnboundConnection); constrainsUnboundConnection = QMetaObject::Connection(); emit q_func()->pointerConstraintsChanged(); } } ); } constrainsUnboundConnection = QObject::connect(lock, &LockedPointerInterface::unbound, q_func(), [this] { if (lockedPointer.isNull()) { return; } lockedPointer.clear(); disconnect(constrainsOneShotConnection); constrainsOneShotConnection = QMetaObject::Connection(); disconnect(constrainsUnboundConnection); constrainsUnboundConnection = QMetaObject::Connection(); emit q_func()->pointerConstraintsChanged(); } ); emit q_func()->pointerConstraintsChanged(); } void SurfaceInterface::Private::installIdleInhibitor(IdleInhibitorInterface *inhibitor) { idleInhibitors << inhibitor; QObject::connect(inhibitor, &IdleInhibitorInterface::aboutToBeUnbound, q, [this, inhibitor] { idleInhibitors.removeOne(inhibitor); if (idleInhibitors.isEmpty()) { emit q_func()->inhibitsIdleChanged(); } } ); if (idleInhibitors.count() == 1) { emit q_func()->inhibitsIdleChanged(); } } void SurfaceInterface::Private::installPointerConstraint(ConfinedPointerInterface *confinement) { Q_ASSERT(lockedPointer.isNull()); Q_ASSERT(confinedPointer.isNull()); confinedPointer = QPointer(confinement); if (confinement->lifeTime() == ConfinedPointerInterface::LifeTime::OneShot) { constrainsOneShotConnection = QObject::connect(confinement, &ConfinedPointerInterface::confinedChanged, q_func(), [this] { if (confinedPointer.isNull()) { return; } if (!confinedPointer->isConfined()) { confinedPointer.clear(); disconnect(constrainsOneShotConnection); constrainsOneShotConnection = QMetaObject::Connection(); disconnect(constrainsUnboundConnection); constrainsUnboundConnection = QMetaObject::Connection(); emit q_func()->pointerConstraintsChanged(); } } ); } constrainsUnboundConnection = QObject::connect(confinement, &ConfinedPointerInterface::unbound, q_func(), [this] { if (confinedPointer.isNull()) { return; } confinedPointer.clear(); disconnect(constrainsOneShotConnection); constrainsOneShotConnection = QMetaObject::Connection(); disconnect(constrainsUnboundConnection); constrainsUnboundConnection = QMetaObject::Connection(); emit q_func()->pointerConstraintsChanged(); } ); emit q_func()->pointerConstraintsChanged(); } #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_surface_interface SurfaceInterface::Private::s_interface = { resourceDestroyedCallback, attachCallback, damageCallback, frameCallback, 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() { // copy all existing callbacks to new list and clear existing lists // the wl_resource_destroy on the callback resource goes into destroyFrameCallback // which would modify the list we are iterating on QList callbacksToDestroy; callbacksToDestroy << current.callbacks; current.callbacks.clear(); callbacksToDestroy << pending.callbacks; pending.callbacks.clear(); callbacksToDestroy << subSurfacePending.callbacks; subSurfacePending.callbacks.clear(); for (auto it = callbacksToDestroy.constBegin(), end = callbacksToDestroy.constEnd(); it != end; it++) { wl_resource_destroy(*it); } if (current.buffer) { current.buffer->unref(); } } 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; } if (!lockedPointer.isNull()) { lockedPointer->d_func()->commit(); } if (!confinedPointer.isNull()) { confinedPointer->d_func()->commit(); } *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 (buffer && !sizeChanged) { emit q->sizeChanged(); } } 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; trackedDamage = trackedDamage.united(target->damage); 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); + if (subSurface) { + const auto mainSurface = subSurface->mainSurface(); + if (!mainSurface || !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::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::frameCallback(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 to the buffer size if (d->current.buffer) { return d->current.buffer->size() / scale(); } 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->subSurfaceIsMapped && !d->subSurface->parentSurface().isNull() && d->subSurface->parentSurface()->isMapped(); } return d->current.buffer != nullptr; } QRegion SurfaceInterface::trackedDamage() const { Q_D(); return d->trackedDamage; } void SurfaceInterface::resetTrackedDamage() { Q_D(); d->trackedDamage = QRegion(); } QVector SurfaceInterface::outputs() const { Q_D(); return d->outputs; } void SurfaceInterface::setOutputs(const QVector &outputs) { Q_D(); QVector removedOutputs = d->outputs; for (auto it = outputs.constBegin(), end = outputs.constEnd(); it != end; ++it) { const auto o = *it; removedOutputs.removeOne(o); } for (auto it = removedOutputs.constBegin(), end = removedOutputs.constEnd(); it != end; ++it) { const auto resources = (*it)->clientResources(client()); for (wl_resource *r : resources) { wl_surface_send_leave(d->resource, r); } disconnect(d->outputDestroyedConnections.take(*it)); } QVector addedOutputsOutputs = outputs; for (auto it = d->outputs.constBegin(), end = d->outputs.constEnd(); it != end; ++it) { const auto o = *it; addedOutputsOutputs.removeOne(o); } for (auto it = addedOutputsOutputs.constBegin(), end = addedOutputsOutputs.constEnd(); it != end; ++it) { const auto o = *it; const auto resources = o->clientResources(client()); for (wl_resource *r : resources) { wl_surface_send_enter(d->resource, r); } d->outputDestroyedConnections[o] = connect(o, &Global::aboutToDestroyGlobal, this, [this, o] { Q_D(); auto outputs = d->outputs; if (outputs.removeOne(o)) { setOutputs(outputs); }}); } // TODO: send enter when the client binds the OutputInterface another time d->outputs = outputs; } SurfaceInterface *SurfaceInterface::surfaceAt(const QPointF &position) { if (!isMapped()) { return nullptr; } Q_D(); // go from top to bottom. Top most child is last in list QListIterator> it(d->current.children); it.toBack(); while (it.hasPrevious()) { const auto ¤t = it.previous(); auto surface = current->surface(); if (surface.isNull()) { continue; } if (auto s = surface->surfaceAt(position - current->position())) { return s; } } // check whether the geometry contains the pos if (!size().isEmpty() && QRectF(QPoint(0, 0), size()).contains(position)) { return this; } return nullptr; } QPointer SurfaceInterface::lockedPointer() const { Q_D(); return d->lockedPointer; } QPointer SurfaceInterface::confinedPointer() const { Q_D(); return d->confinedPointer; } bool SurfaceInterface::inhibitsIdle() const { Q_D(); return !d->idleInhibitors.isEmpty(); } SurfaceInterface::Private *SurfaceInterface::d_func() const { return reinterpret_cast(d.data()); } } }