2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
|
2016-04-13 07:58:09 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2016-04-13 07:58:09 +00:00
|
|
|
// Qt
|
2023-07-03 19:28:19 +00:00
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTest>
|
2016-04-13 07:58:09 +00:00
|
|
|
// client
|
2020-04-29 13:59:23 +00:00
|
|
|
#include "KWayland/Client/compositor.h"
|
2021-08-29 05:11:06 +00:00
|
|
|
#include "KWayland/Client/connection_thread.h"
|
2020-04-29 13:59:23 +00:00
|
|
|
#include "KWayland/Client/event_queue.h"
|
|
|
|
#include "KWayland/Client/registry.h"
|
|
|
|
#include "KWayland/Client/shadow.h"
|
|
|
|
#include "KWayland/Client/shm_pool.h"
|
|
|
|
#include "KWayland/Client/surface.h"
|
2016-04-13 07:58:09 +00:00
|
|
|
// server
|
2023-05-25 11:15:57 +00:00
|
|
|
#include "core/graphicsbufferview.h"
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "wayland/compositor.h"
|
2022-04-22 09:27:33 +00:00
|
|
|
#include "wayland/display.h"
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "wayland/shadow.h"
|
2016-04-13 07:58:09 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2016-04-13 07:58:09 +00:00
|
|
|
|
|
|
|
class ShadowTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
void testCreateShadow();
|
|
|
|
void testShadowElements();
|
2016-05-30 09:21:12 +00:00
|
|
|
void testSurfaceDestroy();
|
2016-04-13 07:58:09 +00:00
|
|
|
|
|
|
|
private:
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display *m_display = nullptr;
|
2016-04-13 07:58:09 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
KWayland::Client::ConnectionThread *m_connection = nullptr;
|
2016-04-13 07:58:09 +00:00
|
|
|
CompositorInterface *m_compositorInterface = nullptr;
|
|
|
|
ShadowManagerInterface *m_shadowInterface = nullptr;
|
|
|
|
QThread *m_thread = nullptr;
|
2022-11-08 21:15:17 +00:00
|
|
|
KWayland::Client::EventQueue *m_queue = nullptr;
|
|
|
|
KWayland::Client::ShmPool *m_shm = nullptr;
|
|
|
|
KWayland::Client::Compositor *m_compositor = nullptr;
|
|
|
|
KWayland::Client::ShadowManager *m_shadow = nullptr;
|
2016-04-13 07:58:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const QString s_socketName = QStringLiteral("kwayland-test-shadow-0");
|
|
|
|
|
|
|
|
void ShadowTest::init()
|
|
|
|
{
|
|
|
|
delete m_display;
|
2023-09-13 17:59:29 +00:00
|
|
|
m_display = new KWin::Display(this);
|
2020-10-19 15:52:56 +00:00
|
|
|
m_display->addSocketName(s_socketName);
|
2016-04-13 07:58:09 +00:00
|
|
|
m_display->start();
|
|
|
|
QVERIFY(m_display->isRunning());
|
|
|
|
m_display->createShm();
|
2020-12-09 20:13:19 +00:00
|
|
|
m_compositorInterface = new CompositorInterface(m_display, m_display);
|
|
|
|
m_shadowInterface = new ShadowManagerInterface(m_display, m_display);
|
2016-04-13 07:58:09 +00:00
|
|
|
|
|
|
|
// setup connection
|
|
|
|
m_connection = new KWayland::Client::ConnectionThread;
|
2022-11-08 21:15:17 +00:00
|
|
|
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
|
2016-04-13 07:58:09 +00:00
|
|
|
m_connection->setSocketName(s_socketName);
|
|
|
|
|
|
|
|
m_thread = new QThread(this);
|
|
|
|
m_connection->moveToThread(m_thread);
|
|
|
|
m_thread->start();
|
|
|
|
|
|
|
|
m_connection->initConnection();
|
|
|
|
QVERIFY(connectedSpy.wait());
|
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
m_queue = new KWayland::Client::EventQueue(this);
|
2016-04-13 07:58:09 +00:00
|
|
|
m_queue->setup(m_connection);
|
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
KWayland::Client::Registry registry;
|
|
|
|
QSignalSpy interfacesAnnouncedSpy(®istry, &KWayland::Client::Registry::interfacesAnnounced);
|
2016-04-13 07:58:09 +00:00
|
|
|
registry.setEventQueue(m_queue);
|
|
|
|
registry.create(m_connection);
|
|
|
|
QVERIFY(registry.isValid());
|
|
|
|
registry.setup();
|
|
|
|
QVERIFY(interfacesAnnouncedSpy.wait());
|
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
m_shm = registry.createShmPool(registry.interface(KWayland::Client::Registry::Interface::Shm).name, registry.interface(KWayland::Client::Registry::Interface::Shm).version, this);
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(m_shm->isValid());
|
2021-08-29 05:11:06 +00:00
|
|
|
m_compositor =
|
2022-11-08 21:15:17 +00:00
|
|
|
registry.createCompositor(registry.interface(KWayland::Client::Registry::Interface::Compositor).name, registry.interface(KWayland::Client::Registry::Interface::Compositor).version, this);
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(m_compositor->isValid());
|
2021-08-29 05:11:06 +00:00
|
|
|
m_shadow =
|
2022-11-08 21:15:17 +00:00
|
|
|
registry.createShadowManager(registry.interface(KWayland::Client::Registry::Interface::Shadow).name, registry.interface(KWayland::Client::Registry::Interface::Shadow).version, this);
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(m_shadow->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShadowTest::cleanup()
|
|
|
|
{
|
2022-04-19 10:14:26 +00:00
|
|
|
#define CLEANUP(variable) \
|
|
|
|
if (variable) { \
|
|
|
|
delete variable; \
|
|
|
|
variable = nullptr; \
|
2016-04-13 07:58:09 +00:00
|
|
|
}
|
|
|
|
CLEANUP(m_shm)
|
|
|
|
CLEANUP(m_compositor)
|
|
|
|
CLEANUP(m_shadow)
|
|
|
|
CLEANUP(m_queue)
|
|
|
|
if (m_connection) {
|
|
|
|
m_connection->deleteLater();
|
|
|
|
m_connection = nullptr;
|
|
|
|
}
|
|
|
|
if (m_thread) {
|
|
|
|
m_thread->quit();
|
|
|
|
m_thread->wait();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP(m_display)
|
|
|
|
#undef CLEANUP
|
2020-10-30 13:36:24 +00:00
|
|
|
|
|
|
|
// these are the children of the display
|
|
|
|
m_compositorInterface = nullptr;
|
|
|
|
m_shadowInterface = nullptr;
|
2016-04-13 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShadowTest::testCreateShadow()
|
|
|
|
{
|
|
|
|
// this test verifies the basic shadow behavior, create for surface, commit it, etc.
|
|
|
|
QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated);
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(surfaceCreatedSpy.wait());
|
2021-08-29 05:11:06 +00:00
|
|
|
auto serverSurface = surfaceCreatedSpy.first().first().value<SurfaceInterface *>();
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(serverSurface);
|
|
|
|
// a surface without anything should not have a Shadow
|
|
|
|
QVERIFY(!serverSurface->shadow());
|
|
|
|
QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
|
|
|
|
|
|
|
|
// let's create a shadow for the Surface
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
|
2016-04-13 07:58:09 +00:00
|
|
|
// that should not have triggered the shadowChangedSpy)
|
|
|
|
QVERIFY(!shadowChangedSpy.wait(100));
|
|
|
|
|
|
|
|
// now let's commit the surface, that should trigger the shadow changed
|
2022-11-08 21:15:17 +00:00
|
|
|
surface->commit(KWayland::Client::Surface::CommitFlag::None);
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(shadowChangedSpy.wait());
|
|
|
|
QCOMPARE(shadowChangedSpy.count(), 1);
|
|
|
|
|
|
|
|
// we didn't set anything on the shadow, so it should be all default values
|
|
|
|
auto serverShadow = serverSurface->shadow();
|
|
|
|
QVERIFY(serverShadow);
|
|
|
|
QCOMPARE(serverShadow->offset(), QMarginsF());
|
|
|
|
QVERIFY(!serverShadow->topLeft());
|
|
|
|
QVERIFY(!serverShadow->top());
|
|
|
|
QVERIFY(!serverShadow->topRight());
|
|
|
|
QVERIFY(!serverShadow->right());
|
|
|
|
QVERIFY(!serverShadow->bottomRight());
|
|
|
|
QVERIFY(!serverShadow->bottom());
|
|
|
|
QVERIFY(!serverShadow->bottomLeft());
|
|
|
|
QVERIFY(!serverShadow->left());
|
|
|
|
|
|
|
|
// now let's remove the shadow
|
2022-08-01 21:29:02 +00:00
|
|
|
m_shadow->removeShadow(surface.get());
|
2016-04-13 07:58:09 +00:00
|
|
|
// just removing should not remove it yet, surface needs to be committed
|
|
|
|
QVERIFY(!shadowChangedSpy.wait(100));
|
2022-11-08 21:15:17 +00:00
|
|
|
surface->commit(KWayland::Client::Surface::CommitFlag::None);
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(shadowChangedSpy.wait());
|
|
|
|
QCOMPARE(shadowChangedSpy.count(), 2);
|
|
|
|
QVERIFY(!serverSurface->shadow());
|
|
|
|
}
|
|
|
|
|
2023-04-28 07:30:18 +00:00
|
|
|
static QImage bufferToImage(KWin::GraphicsBuffer *clientBuffer)
|
2021-07-20 17:02:13 +00:00
|
|
|
{
|
2023-05-25 11:15:57 +00:00
|
|
|
if (clientBuffer) {
|
|
|
|
KWin::GraphicsBufferView view(clientBuffer);
|
|
|
|
if (QImage *image = view.image()) {
|
|
|
|
return image->copy();
|
|
|
|
}
|
2021-07-20 17:02:13 +00:00
|
|
|
}
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:58:09 +00:00
|
|
|
void ShadowTest::testShadowElements()
|
|
|
|
{
|
|
|
|
// this test verifies that all shadow elements are correctly passed to the server
|
|
|
|
// first create surface
|
|
|
|
QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated);
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(surfaceCreatedSpy.wait());
|
2021-08-29 05:11:06 +00:00
|
|
|
auto serverSurface = surfaceCreatedSpy.first().first().value<SurfaceInterface *>();
|
2016-04-13 07:58:09 +00:00
|
|
|
QVERIFY(serverSurface);
|
|
|
|
QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
|
|
|
|
|
|
|
|
// now create the shadow
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage topLeftImage(QSize(10, 10), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
topLeftImage.fill(Qt::white);
|
|
|
|
shadow->attachTopLeft(m_shm->createBuffer(topLeftImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage topImage(QSize(11, 11), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
topImage.fill(Qt::black);
|
|
|
|
shadow->attachTop(m_shm->createBuffer(topImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage topRightImage(QSize(12, 12), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
topRightImage.fill(Qt::red);
|
|
|
|
shadow->attachTopRight(m_shm->createBuffer(topRightImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage rightImage(QSize(13, 13), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
rightImage.fill(Qt::darkRed);
|
|
|
|
shadow->attachRight(m_shm->createBuffer(rightImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage bottomRightImage(QSize(14, 14), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
bottomRightImage.fill(Qt::green);
|
|
|
|
shadow->attachBottomRight(m_shm->createBuffer(bottomRightImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage bottomImage(QSize(15, 15), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
bottomImage.fill(Qt::darkGreen);
|
|
|
|
shadow->attachBottom(m_shm->createBuffer(bottomImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage bottomLeftImage(QSize(16, 16), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
bottomLeftImage.fill(Qt::blue);
|
|
|
|
shadow->attachBottomLeft(m_shm->createBuffer(bottomLeftImage));
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage leftImage(QSize(17, 17), QImage::Format_ARGB32_Premultiplied);
|
2016-04-13 07:58:09 +00:00
|
|
|
leftImage.fill(Qt::darkBlue);
|
|
|
|
shadow->attachLeft(m_shm->createBuffer(leftImage));
|
|
|
|
shadow->setOffsets(QMarginsF(1, 2, 3, 4));
|
|
|
|
shadow->commit();
|
2022-11-08 21:15:17 +00:00
|
|
|
surface->commit(KWayland::Client::Surface::CommitFlag::None);
|
2016-04-13 07:58:09 +00:00
|
|
|
|
|
|
|
QVERIFY(shadowChangedSpy.wait());
|
|
|
|
auto serverShadow = serverSurface->shadow();
|
|
|
|
QVERIFY(serverShadow);
|
|
|
|
QCOMPARE(serverShadow->offset(), QMarginsF(1, 2, 3, 4));
|
2021-07-20 17:02:13 +00:00
|
|
|
QCOMPARE(bufferToImage(serverShadow->topLeft()), topLeftImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->top()), topImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->topRight()), topRightImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->right()), rightImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->bottomRight()), bottomRightImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->bottom()), bottomImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->bottomLeft()), bottomLeftImage);
|
|
|
|
QCOMPARE(bufferToImage(serverShadow->left()), leftImage);
|
2016-04-13 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 09:21:12 +00:00
|
|
|
void ShadowTest::testSurfaceDestroy()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2016-05-30 09:21:12 +00:00
|
|
|
QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated);
|
|
|
|
|
2022-08-01 21:29:02 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
|
2016-05-30 09:21:12 +00:00
|
|
|
QVERIFY(serverSurfaceCreated.wait());
|
2021-08-29 05:11:06 +00:00
|
|
|
auto serverSurface = serverSurfaceCreated.first().first().value<SurfaceInterface *>();
|
2016-05-30 09:21:12 +00:00
|
|
|
QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
|
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
|
2016-05-30 09:21:12 +00:00
|
|
|
shadow->commit();
|
2022-11-08 21:15:17 +00:00
|
|
|
surface->commit(KWayland::Client::Surface::CommitFlag::None);
|
2016-05-30 09:21:12 +00:00
|
|
|
QVERIFY(shadowChangedSpy.wait());
|
|
|
|
auto serverShadow = serverSurface->shadow();
|
|
|
|
QVERIFY(serverShadow);
|
|
|
|
|
|
|
|
// destroy the parent surface
|
|
|
|
QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
|
2023-09-07 10:56:54 +00:00
|
|
|
QSignalSpy shadowDestroyedSpy(serverShadow, &QObject::destroyed);
|
2016-05-30 09:21:12 +00:00
|
|
|
surface.reset();
|
|
|
|
QVERIFY(surfaceDestroyedSpy.wait());
|
|
|
|
QVERIFY(shadowDestroyedSpy.isEmpty());
|
|
|
|
// destroy the shadow
|
|
|
|
shadow.reset();
|
2016-06-07 10:58:58 +00:00
|
|
|
QVERIFY(shadowDestroyedSpy.wait());
|
2016-05-30 09:21:12 +00:00
|
|
|
QCOMPARE(shadowDestroyedSpy.count(), 1);
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:58:09 +00:00
|
|
|
QTEST_GUILESS_MAIN(ShadowTest)
|
|
|
|
#include "test_shadow.moc"
|