2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-09-22 06:52:05 +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
|
|
|
|
*/
|
2014-09-22 06:52:05 +00:00
|
|
|
// Qt
|
|
|
|
#include <QImage>
|
2021-08-29 05:11:06 +00:00
|
|
|
#include <QtTest>
|
2014-09-22 06:52:05 +00:00
|
|
|
// KWin
|
2021-08-29 05:11:06 +00:00
|
|
|
#include "../../src/server/compositor_interface.h"
|
|
|
|
#include "../../src/server/display.h"
|
|
|
|
#include "../../src/server/surface_interface.h"
|
2020-04-29 13:59:23 +00:00
|
|
|
#include "KWayland/Client/compositor.h"
|
|
|
|
#include "KWayland/Client/connection_thread.h"
|
|
|
|
#include "KWayland/Client/registry.h"
|
|
|
|
#include "KWayland/Client/shm_pool.h"
|
2021-08-29 05:11:06 +00:00
|
|
|
#include "KWayland/Client/surface.h"
|
2014-09-22 06:52:05 +00:00
|
|
|
|
|
|
|
class TestShmPool : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit TestShmPool(QObject *parent = nullptr);
|
|
|
|
private Q_SLOTS:
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
void testCreateBufferNullImage();
|
|
|
|
void testCreateBufferNullSize();
|
|
|
|
void testCreateBufferInvalidSize();
|
|
|
|
void testCreateBufferFromImage();
|
2017-08-30 08:57:41 +00:00
|
|
|
void testCreateBufferFromImageWithAlpha();
|
2014-09-22 06:52:05 +00:00
|
|
|
void testCreateBufferFromData();
|
|
|
|
void testReuseBuffer();
|
|
|
|
void testDestroy();
|
|
|
|
|
|
|
|
private:
|
2020-04-29 14:56:38 +00:00
|
|
|
KWaylandServer::Display *m_display;
|
2014-09-22 06:52:05 +00:00
|
|
|
KWayland::Client::ConnectionThread *m_connection;
|
|
|
|
KWayland::Client::Compositor *m_compositor;
|
|
|
|
KWayland::Client::ShmPool *m_shmPool;
|
|
|
|
QThread *m_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const QString s_socketName = QStringLiteral("kwin-test-wayland-surface-0");
|
|
|
|
|
|
|
|
TestShmPool::TestShmPool(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_display(nullptr)
|
|
|
|
, m_connection(nullptr)
|
|
|
|
, m_compositor(nullptr)
|
|
|
|
, m_shmPool(nullptr)
|
|
|
|
, m_thread(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::init()
|
|
|
|
{
|
2020-04-29 14:56:38 +00:00
|
|
|
using namespace KWaylandServer;
|
2014-09-22 06:52:05 +00:00
|
|
|
delete m_display;
|
|
|
|
m_display = new Display(this);
|
2020-10-19 15:52:56 +00:00
|
|
|
m_display->addSocketName(s_socketName);
|
2014-09-22 06:52:05 +00:00
|
|
|
m_display->start();
|
|
|
|
QVERIFY(m_display->isRunning());
|
|
|
|
|
|
|
|
// setup connection
|
|
|
|
m_connection = new KWayland::Client::ConnectionThread;
|
2021-02-25 13:48:11 +00:00
|
|
|
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
|
2014-09-22 06:52:05 +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());
|
|
|
|
|
|
|
|
KWayland::Client::Registry registry;
|
2021-02-25 13:48:11 +00:00
|
|
|
QSignalSpy shmSpy(®istry, &KWayland::Client::Registry::shmAnnounced);
|
2014-09-22 06:52:05 +00:00
|
|
|
registry.create(m_connection->display());
|
|
|
|
QVERIFY(registry.isValid());
|
|
|
|
registry.setup();
|
|
|
|
|
|
|
|
// here we need a shm pool
|
|
|
|
m_display->createShm();
|
|
|
|
|
|
|
|
QVERIFY(shmSpy.wait());
|
|
|
|
m_shmPool = registry.createShmPool(shmSpy.first().first().value<quint32>(), shmSpy.first().last().value<quint32>(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::cleanup()
|
|
|
|
{
|
|
|
|
if (m_compositor) {
|
|
|
|
delete m_compositor;
|
|
|
|
m_compositor = nullptr;
|
|
|
|
}
|
|
|
|
if (m_shmPool) {
|
|
|
|
delete m_shmPool;
|
|
|
|
m_shmPool = 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 TestShmPool::testCreateBufferNullImage()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
|
|
|
QImage img;
|
|
|
|
QVERIFY(img.isNull());
|
|
|
|
QVERIFY(!m_shmPool->createBuffer(img));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testCreateBufferNullSize()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
|
|
|
QSize size(0, 0);
|
|
|
|
QVERIFY(size.isNull());
|
|
|
|
QVERIFY(!m_shmPool->createBuffer(size, 0, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testCreateBufferInvalidSize()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
|
|
|
QSize size;
|
|
|
|
QVERIFY(!size.isValid());
|
|
|
|
QVERIFY(!m_shmPool->createBuffer(size, 0, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testCreateBufferFromImage()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage img(24, 24, QImage::Format_RGB32);
|
2014-09-22 06:52:05 +00:00
|
|
|
img.fill(Qt::black);
|
|
|
|
QVERIFY(!img.isNull());
|
2014-09-22 07:54:10 +00:00
|
|
|
auto buffer = m_shmPool->createBuffer(img).toStrongRef();
|
2014-09-22 06:52:05 +00:00
|
|
|
QVERIFY(buffer);
|
|
|
|
QCOMPARE(buffer->size(), img.size());
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage img2(buffer->address(), img.width(), img.height(), QImage::Format_RGB32);
|
|
|
|
QCOMPARE(img2, img);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testCreateBufferFromImageWithAlpha()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
|
|
|
QImage img(24, 24, QImage::Format_ARGB32_Premultiplied);
|
2021-08-29 05:11:06 +00:00
|
|
|
img.fill(QColor(255, 0, 0, 100)); // red with alpha
|
2017-08-30 08:57:41 +00:00
|
|
|
QVERIFY(!img.isNull());
|
|
|
|
auto buffer = m_shmPool->createBuffer(img).toStrongRef();
|
|
|
|
QVERIFY(buffer);
|
|
|
|
QCOMPARE(buffer->size(), img.size());
|
|
|
|
QImage img2(buffer->address(), img.width(), img.height(), QImage::Format_ARGB32_Premultiplied);
|
2014-09-22 06:52:05 +00:00
|
|
|
QCOMPARE(img2, img);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testCreateBufferFromData()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage img(24, 24, QImage::Format_ARGB32_Premultiplied);
|
2014-09-22 06:52:05 +00:00
|
|
|
img.fill(Qt::black);
|
|
|
|
QVERIFY(!img.isNull());
|
2014-09-22 07:54:10 +00:00
|
|
|
auto buffer = m_shmPool->createBuffer(img.size(), img.bytesPerLine(), img.constBits()).toStrongRef();
|
2014-09-22 06:52:05 +00:00
|
|
|
QVERIFY(buffer);
|
|
|
|
QCOMPARE(buffer->size(), img.size());
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage img2(buffer->address(), img.width(), img.height(), QImage::Format_ARGB32_Premultiplied);
|
2014-09-22 06:52:05 +00:00
|
|
|
QCOMPARE(img2, img);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testReuseBuffer()
|
|
|
|
{
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
2017-08-30 08:57:41 +00:00
|
|
|
QImage img(24, 24, QImage::Format_ARGB32_Premultiplied);
|
2014-09-22 06:52:05 +00:00
|
|
|
img.fill(Qt::black);
|
|
|
|
QVERIFY(!img.isNull());
|
2014-09-22 07:54:10 +00:00
|
|
|
auto buffer = m_shmPool->createBuffer(img).toStrongRef();
|
2014-09-22 06:52:05 +00:00
|
|
|
QVERIFY(buffer);
|
|
|
|
buffer->setReleased(true);
|
|
|
|
buffer->setUsed(false);
|
|
|
|
|
|
|
|
// same image should get the same buffer
|
2014-09-22 07:54:10 +00:00
|
|
|
auto buffer2 = m_shmPool->createBuffer(img).toStrongRef();
|
2014-09-22 06:52:05 +00:00
|
|
|
QCOMPARE(buffer, buffer2);
|
|
|
|
buffer2->setReleased(true);
|
|
|
|
buffer2->setUsed(false);
|
|
|
|
|
|
|
|
// image with different size should get us a new buffer
|
|
|
|
auto buffer3 = m_shmPool->getBuffer(QSize(10, 10), 8);
|
|
|
|
QVERIFY(buffer3 != buffer2);
|
|
|
|
|
|
|
|
// image with a different format should get us a new buffer
|
|
|
|
QImage img2(24, 24, QImage::Format_RGB32);
|
|
|
|
img2.fill(Qt::black);
|
|
|
|
QVERIFY(!img2.isNull());
|
2014-09-22 07:54:10 +00:00
|
|
|
auto buffer4 = m_shmPool->createBuffer(img2).toStrongRef();
|
2014-09-22 06:52:05 +00:00
|
|
|
QVERIFY(buffer4);
|
|
|
|
QVERIFY(buffer4 != buffer2);
|
|
|
|
QVERIFY(buffer4 != buffer3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShmPool::testDestroy()
|
|
|
|
{
|
|
|
|
using namespace KWayland::Client;
|
|
|
|
connect(m_connection, &ConnectionThread::connectionDied, m_shmPool, &ShmPool::destroy);
|
|
|
|
QVERIFY(m_shmPool->isValid());
|
|
|
|
|
|
|
|
// let's create one Buffer
|
|
|
|
m_shmPool->getBuffer(QSize(10, 10), 8);
|
|
|
|
|
2021-02-25 13:48:11 +00:00
|
|
|
QSignalSpy connectionDiedSpy(m_connection, &KWayland::Client::ConnectionThread::connectionDied);
|
2014-09-22 06:52:05 +00:00
|
|
|
QVERIFY(connectionDiedSpy.isValid());
|
|
|
|
delete m_display;
|
|
|
|
m_display = nullptr;
|
|
|
|
QVERIFY(connectionDiedSpy.wait());
|
|
|
|
|
|
|
|
// now the pool should be destroyed;
|
|
|
|
QVERIFY(!m_shmPool->isValid());
|
|
|
|
|
|
|
|
// calling destroy again should not fail
|
|
|
|
m_shmPool->destroy();
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:00:17 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestShmPool)
|
2014-09-22 06:52:05 +00:00
|
|
|
#include "test_shm_pool.moc"
|