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
|
|
|
|
*/
|
|
|
|
|
2023-07-03 19:28:19 +00:00
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTest>
|
2020-05-24 10:46:27 +00:00
|
|
|
#include <QThread>
|
|
|
|
|
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/surface.h"
|
|
|
|
#include "wayland/viewporter.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"
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2020-05-24 10:46:27 +00:00
|
|
|
|
|
|
|
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;
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::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);
|
2023-12-20 20:15:38 +00:00
|
|
|
QSignalSpy bufferSourceBoxChangedSpy(serverSurface, &SurfaceInterface::bufferSourceBoxChanged);
|
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());
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(bufferSourceBoxChangedSpy.count(), 1);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(100, 50));
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(serverSurface->bufferSourceBox(), QRectF(0, 0, 200, 100));
|
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());
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(bufferSourceBoxChangedSpy.count(), 2);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(30, 20));
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(serverSurface->bufferSourceBox(), QRectF(20, 20, 60, 40));
|
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());
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(bufferSourceBoxChangedSpy.count(), 2);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(500, 250));
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(serverSurface->bufferSourceBox(), QRectF(20, 20, 60, 40));
|
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());
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(bufferSourceBoxChangedSpy.count(), 3);
|
2020-05-24 10:46:27 +00:00
|
|
|
QCOMPARE(serverSurface->size(), QSize(100, 50));
|
2023-12-20 20:15:38 +00:00
|
|
|
QCOMPARE(serverSurface->bufferSourceBox(), QRectF(0, 0, 200, 100));
|
2020-05-24 10:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestViewporterInterface)
|
|
|
|
|
|
|
|
#include "test_viewporter_interface.moc"
|