2020-05-24 10:46:27 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
#include <QtTest>
|
|
|
|
|
2022-04-22 09:27:33 +00:00
|
|
|
#include "wayland/compositor_interface.h"
|
|
|
|
#include "wayland/display.h"
|
|
|
|
#include "wayland/surface_interface.h"
|
|
|
|
#include "wayland/viewporter_interface.h"
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
#include "KWayland/Client/compositor.h"
|
|
|
|
#include "KWayland/Client/connection_thread.h"
|
|
|
|
#include "KWayland/Client/event_queue.h"
|
|
|
|
#include "KWayland/Client/registry.h"
|
|
|
|
#include "KWayland/Client/shm_pool.h"
|
|
|
|
#include "KWayland/Client/surface.h"
|
|
|
|
|
|
|
|
#include "qwayland-viewporter.h"
|
|
|
|
|
|
|
|
using namespace KWaylandServer;
|
|
|
|
|
|
|
|
class Viewporter : public QtWayland::wp_viewporter
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class Viewport : public QtWayland::wp_viewport
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestViewporterInterface : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
~TestViewporterInterface() override;
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
void testCropScale();
|
|
|
|
|
|
|
|
private:
|
|
|
|
KWayland::Client::ConnectionThread *m_connection;
|
|
|
|
KWayland::Client::EventQueue *m_queue;
|
|
|
|
KWayland::Client::Compositor *m_clientCompositor;
|
|
|
|
KWayland::Client::ShmPool *m_shm;
|
|
|
|
|
|
|
|
QThread *m_thread;
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display m_display;
|
2020-05-24 10:46:27 +00:00
|
|
|
CompositorInterface *m_serverCompositor;
|
|
|
|
Viewporter *m_viewporter;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const QString s_socketName = QStringLiteral("kwin-wayland-server-viewporter-test-0");
|
|
|
|
|
|
|
|
void TestViewporterInterface::initTestCase()
|
|
|
|
{
|
2020-10-19 15:52:56 +00:00
|
|
|
m_display.addSocketName(s_socketName);
|
2020-05-24 10:46:27 +00:00
|
|
|
m_display.start();
|
|
|
|
QVERIFY(m_display.isRunning());
|
|
|
|
|
|
|
|
m_display.createShm();
|
2020-12-09 20:13:19 +00:00
|
|
|
new ViewporterInterface(&m_display);
|
2020-05-24 10:46:27 +00:00
|
|
|
|
2020-12-09 20:13:19 +00:00
|
|
|
m_serverCompositor = new CompositorInterface(&m_display, this);
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
m_connection = new KWayland::Client::ConnectionThread;
|
|
|
|
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::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());
|
|
|
|
QVERIFY(!m_connection->connections().isEmpty());
|
|
|
|
|
|
|
|
m_queue = new KWayland::Client::EventQueue(this);
|
|
|
|
QVERIFY(!m_queue->isValid());
|
|
|
|
m_queue->setup(m_connection);
|
|
|
|
QVERIFY(m_queue->isValid());
|
|
|
|
|
|
|
|
auto registry = new KWayland::Client::Registry(this);
|
|
|
|
connect(registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, registry](const QByteArray &interface, quint32 id, quint32 version) {
|
|
|
|
if (interface == QByteArrayLiteral("wp_viewporter")) {
|
|
|
|
m_viewporter = new Viewporter();
|
|
|
|
m_viewporter->init(*registry, id, version);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
QSignalSpy allAnnouncedSpy(registry, &KWayland::Client::Registry::interfaceAnnounced);
|
|
|
|
QSignalSpy compositorSpy(registry, &KWayland::Client::Registry::compositorAnnounced);
|
|
|
|
QSignalSpy shmSpy(registry, &KWayland::Client::Registry::shmAnnounced);
|
|
|
|
registry->setEventQueue(m_queue);
|
|
|
|
registry->create(m_connection->display());
|
|
|
|
QVERIFY(registry->isValid());
|
|
|
|
registry->setup();
|
|
|
|
QVERIFY(allAnnouncedSpy.wait());
|
|
|
|
|
2021-08-29 05:11:06 +00:00
|
|
|
m_clientCompositor = registry->createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
|
2020-05-24 10:46:27 +00:00
|
|
|
QVERIFY(m_clientCompositor->isValid());
|
|
|
|
|
2021-08-29 05:11:06 +00:00
|
|
|
m_shm = registry->createShmPool(shmSpy.first().first().value<quint32>(), shmSpy.first().last().value<quint32>(), this);
|
2020-05-24 10:46:27 +00:00
|
|
|
QVERIFY(m_shm->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TestViewporterInterface::~TestViewporterInterface()
|
|
|
|
{
|
|
|
|
if (m_viewporter) {
|
|
|
|
delete m_viewporter;
|
|
|
|
m_viewporter = nullptr;
|
|
|
|
}
|
|
|
|
if (m_shm) {
|
|
|
|
delete m_shm;
|
|
|
|
m_shm = 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;
|
|
|
|
}
|
|
|
|
m_connection->deleteLater();
|
|
|
|
m_connection = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestViewporterInterface::testCropScale()
|
|
|
|
{
|
|
|
|
// Create a test surface.
|
|
|
|
QSignalSpy serverSurfaceCreatedSpy(m_serverCompositor, &CompositorInterface::surfaceCreated);
|
2022-08-01 21:29:02 +00:00
|
|
|
std::unique_ptr<KWayland::Client::Surface> clientSurface(m_clientCompositor->createSurface(this));
|
2020-05-24 10:46:27 +00:00
|
|
|
QVERIFY(serverSurfaceCreatedSpy.wait());
|
|
|
|
SurfaceInterface *serverSurface = serverSurfaceCreatedSpy.first().first().value<SurfaceInterface *>();
|
|
|
|
QVERIFY(serverSurface);
|
|
|
|
|
|
|
|
QSignalSpy serverSurfaceMappedSpy(serverSurface, &SurfaceInterface::mapped);
|
|
|
|
QSignalSpy serverSurfaceSizeChangedSpy(serverSurface, &SurfaceInterface::sizeChanged);
|
2020-06-10 07:42:16 +00:00
|
|
|
QSignalSpy surfaceToBufferMatrixChangedSpy(serverSurface, &SurfaceInterface::surfaceToBufferMatrixChanged);
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
// Map the surface.
|
2020-06-10 07:42:16 +00:00
|
|
|
QImage image(QSize(200, 100), QImage::Format_ARGB32_Premultiplied);
|
2020-05-24 10:46:27 +00:00
|
|
|
image.fill(Qt::black);
|
|
|
|
KWayland::Client::Buffer::Ptr buffer = m_shm->createBuffer(image);
|
|
|
|
clientSurface->attachBuffer(buffer);
|
2020-06-10 07:42:16 +00:00
|
|
|
clientSurface->setScale(2);
|
2020-05-24 10:46:27 +00:00
|
|
|
clientSurface->damage(image.rect());
|
|
|
|
clientSurface->commit(KWayland::Client::Surface::CommitFlag::None);
|
|
|
|
QVERIFY(serverSurfaceMappedSpy.wait());
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(surfaceToBufferMatrixChangedSpy.count(), 1);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(100, 50));
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(serverSurface->mapToBuffer(QPointF(0, 0)), QPointF(0, 0));
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
// Create a viewport for the surface.
|
2022-08-01 21:29:02 +00:00
|
|
|
std::unique_ptr<Viewport> clientViewport(new Viewport);
|
2020-05-24 10:46:27 +00:00
|
|
|
clientViewport->init(m_viewporter->get_viewport(*clientSurface));
|
|
|
|
|
|
|
|
// Crop the surface.
|
2021-08-29 05:11:06 +00:00
|
|
|
clientViewport->set_source(wl_fixed_from_double(10), wl_fixed_from_double(10), wl_fixed_from_double(30), wl_fixed_from_double(20));
|
2020-05-24 10:46:27 +00:00
|
|
|
clientSurface->commit(KWayland::Client::Surface::CommitFlag::None);
|
|
|
|
QVERIFY(serverSurfaceSizeChangedSpy.wait());
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(surfaceToBufferMatrixChangedSpy.count(), 2);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(30, 20));
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(serverSurface->mapToBuffer(QPointF(0, 0)), QPointF(20, 20));
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
// Scale the surface.
|
|
|
|
clientViewport->set_destination(500, 250);
|
|
|
|
clientSurface->commit(KWayland::Client::Surface::CommitFlag::None);
|
|
|
|
QVERIFY(serverSurfaceSizeChangedSpy.wait());
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(surfaceToBufferMatrixChangedSpy.count(), 3);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(500, 250));
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(serverSurface->mapToBuffer(QPointF(0, 0)), QPointF(20, 20));
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
// If the viewport is destroyed, the crop and scale state will be unset on a next commit.
|
|
|
|
clientViewport->destroy();
|
|
|
|
clientSurface->commit(KWayland::Client::Surface::CommitFlag::None);
|
|
|
|
QVERIFY(serverSurfaceSizeChangedSpy.wait());
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(surfaceToBufferMatrixChangedSpy.count(), 4);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(100, 50));
|
2020-06-10 07:42:16 +00:00
|
|
|
QCOMPARE(serverSurface->mapToBuffer(QPointF(0, 0)), QPointF(0, 0));
|
2020-05-24 10:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestViewporterInterface)
|
|
|
|
|
|
|
|
#include "test_viewporter_interface.moc"
|