1 /********************************************************************
2 Copyright 2016 Martin Gräßlin <mgraesslin@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include <QtTest>
21
22 #include "../../src/client/compositor.h"
23 #include "../../src/client/connection_thread.h"
24 #include "../../src/client/event_queue.h"
25 #include "../../src/client/registry.h"
26 #include "../../src/client/shadow.h"
27 #include "../../src/client/shm_pool.h"
28 #include "../../src/client/surface.h"
29
30 #include "../../server/buffer.h"
31 #include "../../server/client.h"
32 #include "../../server/compositor.h"
33 #include "../../server/display.h"
34 #include "../../server/shadow.h"
35 #include "../../server/surface.h"
36
37 class ShadowTest : public QObject
38 {
39 Q_OBJECT
40 private Q_SLOTS:
41 void init();
42 void cleanup();
43
44 void testCreateShadow();
45 void testShadowElements();
46 void testSurfaceDestroy();
47
48 private:
49 Wrapland::Server::Display* m_display = nullptr;
50
51 Wrapland::Client::ConnectionThread* m_connection = nullptr;
52 Wrapland::Server::Compositor* m_serverCompositor = nullptr;
53 Wrapland::Server::ShadowManager* m_shadowInterface = nullptr;
54 QThread* m_thread = nullptr;
55 Wrapland::Client::EventQueue* m_queue = nullptr;
56 Wrapland::Client::ShmPool* m_shm = nullptr;
57 Wrapland::Client::Compositor* m_compositor = nullptr;
58 Wrapland::Client::ShadowManager* m_shadow = nullptr;
59 };
60
61 static const QString s_socketName = QStringLiteral("wrapland-test-shadow-0");
62
init()63 void ShadowTest::init()
64 {
65 qRegisterMetaType<Wrapland::Server::Surface*>();
66
67 m_display = new Wrapland::Server::Display(this);
68 m_display->setSocketName(s_socketName);
69 m_display->start();
70
71 m_display->createShm();
72 m_serverCompositor = m_display->createCompositor(m_display);
73 m_shadowInterface = m_display->createShadowManager(m_display);
74
75 // setup connection
76 m_connection = new Wrapland::Client::ConnectionThread;
77 QSignalSpy connectedSpy(m_connection, &Wrapland::Client::ConnectionThread::establishedChanged);
78 QVERIFY(connectedSpy.isValid());
79 m_connection->setSocketName(s_socketName);
80
81 m_thread = new QThread(this);
82 m_connection->moveToThread(m_thread);
83 m_thread->start();
84
85 m_connection->establishConnection();
86 QVERIFY(connectedSpy.count() || connectedSpy.wait());
87 QCOMPARE(connectedSpy.count(), 1);
88
89 m_queue = new Wrapland::Client::EventQueue(this);
90 m_queue->setup(m_connection);
91
92 Wrapland::Client::Registry registry;
93 QSignalSpy interfacesAnnouncedSpy(®istry, &Wrapland::Client::Registry::interfacesAnnounced);
94 QVERIFY(interfacesAnnouncedSpy.isValid());
95 registry.setEventQueue(m_queue);
96 registry.create(m_connection);
97 QVERIFY(registry.isValid());
98 registry.setup();
99 QVERIFY(interfacesAnnouncedSpy.wait());
100
101 m_shm = registry.createShmPool(
102 registry.interface(Wrapland::Client::Registry::Interface::Shm).name,
103 registry.interface(Wrapland::Client::Registry::Interface::Shm).version,
104 this);
105 QVERIFY(m_shm->isValid());
106 m_compositor = registry.createCompositor(
107 registry.interface(Wrapland::Client::Registry::Interface::Compositor).name,
108 registry.interface(Wrapland::Client::Registry::Interface::Compositor).version,
109 this);
110 QVERIFY(m_compositor->isValid());
111 m_shadow = registry.createShadowManager(
112 registry.interface(Wrapland::Client::Registry::Interface::Shadow).name,
113 registry.interface(Wrapland::Client::Registry::Interface::Shadow).version,
114 this);
115 QVERIFY(m_shadow->isValid());
116 }
117
cleanup()118 void ShadowTest::cleanup()
119 {
120 #define CLEANUP(variable) \
121 if (variable) { \
122 delete variable; \
123 variable = nullptr; \
124 }
125 CLEANUP(m_shm)
126 CLEANUP(m_compositor)
127 CLEANUP(m_shadow)
128 CLEANUP(m_queue)
129 if (m_connection) {
130 m_connection->deleteLater();
131 m_connection = nullptr;
132 }
133 if (m_thread) {
134 m_thread->quit();
135 m_thread->wait();
136 delete m_thread;
137 m_thread = nullptr;
138 }
139
140 CLEANUP(m_serverCompositor)
141 CLEANUP(m_shadowInterface)
142 CLEANUP(m_display)
143
144 #undef CLEANUP
145 }
146
testCreateShadow()147 void ShadowTest::testCreateShadow()
148 {
149 // this test verifies the basic shadow behavior, create for surface, commit it, etc.
150 QSignalSpy serverSurfaceCreated(m_serverCompositor,
151 SIGNAL(surfaceCreated(Wrapland::Server::Surface*)));
152 QVERIFY(serverSurfaceCreated.isValid());
153
154 std::unique_ptr<Wrapland::Client::Surface> surface{m_compositor->createSurface()};
155 QVERIFY(serverSurfaceCreated.wait());
156 auto serverSurface = serverSurfaceCreated.first().first().value<Wrapland::Server::Surface*>();
157 QVERIFY(serverSurface);
158
159 // a surface without anything should not have a Shadow
160 QVERIFY(!serverSurface->state().shadow);
161 QSignalSpy commit_spy(serverSurface, &Wrapland::Server::Surface::committed);
162 QVERIFY(commit_spy.isValid());
163
164 // let's create a shadow for the Surface
165 std::unique_ptr<Wrapland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
166 // that should not have triggered the commit_spy)
167 QVERIFY(!commit_spy.wait(100));
168 QCOMPARE(serverSurface->state().updates, Wrapland::Server::surface_change::none);
169
170 // now let's commit the surface, that should trigger the shadow changed
171 surface->commit(Wrapland::Client::Surface::CommitFlag::None);
172 QVERIFY(commit_spy.wait());
173 QCOMPARE(commit_spy.count(), 1);
174 QVERIFY(serverSurface->state().updates & Wrapland::Server::surface_change::shadow);
175
176 // we didn't set anything on the shadow, so it should be all default values
177 auto serverShadow = serverSurface->state().shadow;
178 QVERIFY(serverShadow);
179 QCOMPARE(serverShadow->offset(), QMarginsF());
180 QVERIFY(!serverShadow->topLeft());
181 QVERIFY(!serverShadow->top());
182 QVERIFY(!serverShadow->topRight());
183 QVERIFY(!serverShadow->right());
184 QVERIFY(!serverShadow->bottomRight());
185 QVERIFY(!serverShadow->bottom());
186 QVERIFY(!serverShadow->bottomLeft());
187 QVERIFY(!serverShadow->left());
188
189 // now let's remove the shadow
190 m_shadow->removeShadow(surface.get());
191
192 // just removing should not remove it yet, surface needs to be committed
193 QVERIFY(!commit_spy.wait(100));
194 surface->commit(Wrapland::Client::Surface::CommitFlag::None);
195 QVERIFY(commit_spy.wait());
196 QCOMPARE(commit_spy.count(), 2);
197 QVERIFY(!serverSurface->state().shadow);
198 }
199
testShadowElements()200 void ShadowTest::testShadowElements()
201 {
202
203 // this test verifies that all shadow elements are correctly passed to the server
204 // first create surface
205 QSignalSpy serverSurfaceCreated(m_serverCompositor,
206 SIGNAL(surfaceCreated(Wrapland::Server::Surface*)));
207 QVERIFY(serverSurfaceCreated.isValid());
208
209 std::unique_ptr<Wrapland::Client::Surface> surface{m_compositor->createSurface()};
210 QVERIFY(serverSurfaceCreated.wait());
211 auto serverSurface = serverSurfaceCreated.first().first().value<Wrapland::Server::Surface*>();
212 QVERIFY(serverSurface);
213
214 QSignalSpy commit_spy(serverSurface, &Wrapland::Server::Surface::committed);
215 QVERIFY(commit_spy.isValid());
216
217 // now create the shadow
218 std::unique_ptr<Wrapland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
219 QImage topLeftImage(QSize(10, 10), QImage::Format_ARGB32_Premultiplied);
220 topLeftImage.fill(Qt::white);
221 shadow->attachTopLeft(m_shm->createBuffer(topLeftImage));
222 QImage topImage(QSize(11, 11), QImage::Format_ARGB32_Premultiplied);
223 topImage.fill(Qt::black);
224 shadow->attachTop(m_shm->createBuffer(topImage));
225 QImage topRightImage(QSize(12, 12), QImage::Format_ARGB32_Premultiplied);
226 topRightImage.fill(Qt::red);
227 shadow->attachTopRight(m_shm->createBuffer(topRightImage));
228 QImage rightImage(QSize(13, 13), QImage::Format_ARGB32_Premultiplied);
229 rightImage.fill(Qt::darkRed);
230 shadow->attachRight(m_shm->createBuffer(rightImage));
231 QImage bottomRightImage(QSize(14, 14), QImage::Format_ARGB32_Premultiplied);
232 bottomRightImage.fill(Qt::green);
233 shadow->attachBottomRight(m_shm->createBuffer(bottomRightImage));
234 QImage bottomImage(QSize(15, 15), QImage::Format_ARGB32_Premultiplied);
235 bottomImage.fill(Qt::darkGreen);
236 shadow->attachBottom(m_shm->createBuffer(bottomImage));
237 QImage bottomLeftImage(QSize(16, 16), QImage::Format_ARGB32_Premultiplied);
238 bottomLeftImage.fill(Qt::blue);
239 shadow->attachBottomLeft(m_shm->createBuffer(bottomLeftImage));
240 QImage leftImage(QSize(17, 17), QImage::Format_ARGB32_Premultiplied);
241 leftImage.fill(Qt::darkBlue);
242 shadow->attachLeft(m_shm->createBuffer(leftImage));
243 shadow->setOffsets(QMarginsF(1, 2, 3, 4));
244 shadow->commit();
245 surface->commit(Wrapland::Client::Surface::CommitFlag::None);
246
247 QVERIFY(commit_spy.wait());
248 auto serverShadow = serverSurface->state().shadow;
249 QVERIFY(serverShadow);
250 QCOMPARE(serverShadow->offset(), QMarginsF(1, 2, 3, 4));
251 QCOMPARE(serverShadow->topLeft()->shmImage()->createQImage(), topLeftImage);
252 QCOMPARE(serverShadow->top()->shmImage()->createQImage(), topImage);
253 QCOMPARE(serverShadow->topRight()->shmImage()->createQImage(), topRightImage);
254 QCOMPARE(serverShadow->right()->shmImage()->createQImage(), rightImage);
255 QCOMPARE(serverShadow->bottomRight()->shmImage()->createQImage(), bottomRightImage);
256 QCOMPARE(serverShadow->bottom()->shmImage()->createQImage(), bottomImage);
257 QCOMPARE(serverShadow->bottomLeft()->shmImage()->createQImage(), bottomLeftImage);
258 QCOMPARE(serverShadow->left()->shmImage()->createQImage(), leftImage);
259
260 // try to destroy the buffer
261 // first attach one buffer
262 shadow->attachTopLeft(m_shm->createBuffer(topLeftImage));
263
264 // We need to reference the shared_ptr buffer to guarantee receiving the destroyed signal.
265 auto buf = serverShadow->topLeft();
266 QSignalSpy destroyedSpy(serverShadow->topLeft().get(),
267 &Wrapland::Server::Buffer::resourceDestroyed);
268 QVERIFY(destroyedSpy.isValid());
269
270 delete m_shm;
271 m_shm = nullptr;
272 QVERIFY(destroyedSpy.wait());
273
274 // now all buffers should be gone
275 // TODO: does that need a signal?
276 QVERIFY(!serverShadow->topLeft());
277 QVERIFY(!serverShadow->top());
278 QVERIFY(!serverShadow->topRight());
279 QVERIFY(!serverShadow->right());
280 QVERIFY(!serverShadow->bottomRight());
281 QVERIFY(!serverShadow->bottom());
282 QVERIFY(!serverShadow->bottomLeft());
283 QVERIFY(!serverShadow->left());
284 }
285
testSurfaceDestroy()286 void ShadowTest::testSurfaceDestroy()
287 {
288 QSignalSpy serverSurfaceCreated(m_serverCompositor,
289 SIGNAL(surfaceCreated(Wrapland::Server::Surface*)));
290 QVERIFY(serverSurfaceCreated.isValid());
291
292 std::unique_ptr<Wrapland::Client::Surface> surface{m_compositor->createSurface()};
293 QVERIFY(serverSurfaceCreated.wait());
294 auto serverSurface = serverSurfaceCreated.first().first().value<Wrapland::Server::Surface*>();
295 QVERIFY(serverSurface);
296
297 QSignalSpy commit_spy(serverSurface, &Wrapland::Server::Surface::committed);
298 QVERIFY(commit_spy.isValid());
299 std::unique_ptr<Wrapland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
300 shadow->commit();
301 surface->commit(Wrapland::Client::Surface::CommitFlag::None);
302 QVERIFY(commit_spy.wait());
303 auto serverShadow = serverSurface->state().shadow;
304 QVERIFY(serverShadow);
305
306 // destroy the parent surface
307 QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
308 QVERIFY(surfaceDestroyedSpy.isValid());
309 QSignalSpy shadowDestroyedSpy(serverShadow.data(), &QObject::destroyed);
310 QVERIFY(shadowDestroyedSpy.isValid());
311 surface.reset();
312 QVERIFY(surfaceDestroyedSpy.wait());
313 QVERIFY(shadowDestroyedSpy.isEmpty());
314 // destroy the shadow
315 shadow.reset();
316 QVERIFY(shadowDestroyedSpy.wait());
317 QCOMPARE(shadowDestroyedSpy.count(), 1);
318 }
319
320 QTEST_GUILESS_MAIN(ShadowTest)
321 #include "shadow.moc"
322