Project
Loading...
Searching...
No Matches
GPUDisplayFrontendWayland.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
17#include "GPUDisplayGUIWrapper.h" // Destructor of the GUI wrapper unique_ptr member
18#include "GPUDisplay.h"
19#include "GPULogging.h"
20#include <cstdio>
21#include <cstdlib>
22#include <cstring>
23#include <stdexcept>
24#include <cerrno>
25#include <chrono>
26#include <algorithm>
27#include <cmath>
28
29#include <vulkan/vulkan.h>
30#include <vulkan/vulkan_wayland.h>
31
32#include <unistd.h>
33#include <poll.h>
34#include <sys/mman.h>
35#include <wayland-client.h>
36#include "xdg-shell-client-protocol.h"
37#include "xdg-decoration-client-protocol.h"
38#include "viewporter-client-protocol.h"
39#include "cursor-shape-client-protocol.h"
40#include <xkbcommon/xkbcommon.h>
41
42using namespace o2::gpu;
43
45
51
52int32_t GPUDisplayFrontendWayland::GetKey(uint32_t key, uint32_t state)
53{
54 int32_t retVal = 0;
55 if (key == 41) { // evdev KEY_GRAVE, key left of 1: console key (^), independent of the layout (linux/input-event-codes.h must not be included here)
56 return ('^');
57 }
58 if (mXKBkeymap) {
59 xkb_keysym_t sym = xkb_state_key_get_one_sym(mXKBstate, key + 8);
60 if (sym == XKB_KEY_KP_Subtract) {
61 return ('-');
62 } else if (sym == XKB_KEY_KP_Add) {
63 return ('+');
64 } else if (sym == XKB_KEY_Shift_L || sym == XKB_KEY_Shift_R) {
65 return (KEY_SHIFT);
66 } else if (sym == XKB_KEY_Alt_L) {
67 return (KEY_ALT);
68 } else if (sym == XKB_KEY_ISO_Level3_Shift || sym == XKB_KEY_Alt_R) {
69 return (KEY_RALT);
70 } else if (sym == XKB_KEY_Control_L) {
71 return (KEY_CTRL);
72 } else if (sym == XKB_KEY_Control_R) {
73 return (KEY_RCTRL);
74 } else if (sym == XKB_KEY_Up) {
75 return (KEY_UP);
76 } else if (sym == XKB_KEY_Down) {
77 return (KEY_DOWN);
78 } else if (sym == XKB_KEY_Left) {
79 return (KEY_LEFT);
80 } else if (sym == XKB_KEY_Right) {
81 return (KEY_RIGHT);
82 } else if (sym == XKB_KEY_Page_Up) {
83 return (KEY_PAGEUP);
84 } else if (sym == XKB_KEY_Page_Down) {
85 return (KEY_PAGEDOWN);
86 } else if (sym == XKB_KEY_Escape) {
87 return (KEY_ESCAPE);
88 } else if (sym == XKB_KEY_Return) {
89 return (KEY_ENTER);
90 } else if (sym == XKB_KEY_End) {
91 return (KEY_END);
92 } else if (sym == XKB_KEY_Home) {
93 return (KEY_HOME);
94 } else if (sym == XKB_KEY_Insert) {
95 return (KEY_INSERT);
96 } else if (sym == XKB_KEY_BackSpace) {
97 return (KEY_BACKSPACE);
98 } else if (sym == XKB_KEY_Delete) {
99 return (KEY_DELETE);
100 } else if (sym == XKB_KEY_F1) {
101 return (KEY_F1);
102 } else if (sym == XKB_KEY_F2) {
103 return (KEY_F2);
104 } else if (sym == XKB_KEY_F3) {
105 return (KEY_F3);
106 } else if (sym == XKB_KEY_F4) {
107 return (KEY_F4);
108 } else if (sym == XKB_KEY_F5) {
109 return (KEY_F5);
110 } else if (sym == XKB_KEY_F6) {
111 return (KEY_F6);
112 } else if (sym == XKB_KEY_F7) {
113 return (KEY_F7);
114 } else if (sym == XKB_KEY_F8) {
115 return (KEY_F8);
116 } else if (sym == XKB_KEY_F9) {
117 return (KEY_F9);
118 } else if (sym == XKB_KEY_F10) {
119 return (KEY_F10);
120 } else if (sym == XKB_KEY_F11) {
121 return (KEY_F11);
122 } else if (sym == XKB_KEY_F12) {
123 return (KEY_F12);
124 } else if (sym == 32) {
125 return (KEY_SPACE);
126 } else if (sym > 255) {
127 return (0);
128 } else {
129 retVal = xkb_keysym_to_utf32(sym);
130 }
131 }
132 if (retVal > 255) {
133 return (0);
134 }
135 return retVal;
136}
137
138int32_t GPUDisplayFrontendWayland::FrontendMain()
139{
140 if (backend()->backendType() != GPUDisplayBackend::TYPE_VULKAN) {
141 fprintf(stderr, "Only Vulkan backend supported\n");
142 return 1;
143 }
144
145 mXKBcontext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
146 if (mXKBcontext == nullptr) {
147 throw std::runtime_error("Error initializing xkb context");
148 }
149
150 mWayland = wl_display_connect(nullptr);
151 if (mWayland == nullptr) {
152 throw std::runtime_error("Could not connect to wayland display");
153 }
154 mRegistry = wl_display_get_registry(mWayland);
155 if (mRegistry == nullptr) {
156 throw std::runtime_error("Could not create wayland registry");
157 }
158
159 auto pointer_enter = [](void* data, wl_pointer* wl_pointer, uint32_t serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
161 if (!me->mCursorShapeDevice && me->mCursorShapeManager) {
162 me->mCursorShapeDevice = wp_cursor_shape_manager_v1_get_pointer(me->mCursorShapeManager, wl_pointer);
163 }
164 if (me->mCursorShapeDevice) {
165 wp_cursor_shape_device_v1_set_shape(me->mCursorShapeDevice, serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
166 }
167 };
168 auto pointer_leave = [](void* data, wl_pointer* wl_pointer, uint32_t serial, wl_surface* wl_surface) {
170 me->mMouseDn = me->mMouseDnR = me->mMouseDnM = false;
171 };
172 auto pointer_motion = [](void* data, wl_pointer* wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
174 me->mMouseMvX = wl_fixed_to_double(surface_x) * me->mScale;
175 me->mMouseMvY = wl_fixed_to_double(surface_y) * me->mScale;
176 };
177 auto pointer_button = [](void* data, wl_pointer* wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
179 if (state == WL_POINTER_BUTTON_STATE_PRESSED || state == WL_POINTER_BUTTON_STATE_RELEASED) {
180 me->handlePointerButton(button, state == WL_POINTER_BUTTON_STATE_PRESSED);
181 }
182 };
183 auto pointer_frame = [](void* data, wl_pointer* wl_pointer) { ((GPUDisplayFrontendWayland*)data)->applyScroll(); };
184 auto pointer_axis = [](void* data, wl_pointer* wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {
186 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
187 me->mAxisValue += wl_fixed_to_double(value);
188 if (me->mSeatVersion < WL_POINTER_FRAME_SINCE_VERSION) {
189 me->applyScroll();
190 }
191 }
192 };
193 auto pointer_axis_source = [](void* data, wl_pointer* wl_pointer, uint32_t axis_source) {};
194 auto pointer_axis_stop = [](void* data, wl_pointer* wl_pointer, uint32_t time, uint32_t axis) {};
195 auto pointer_axis_discrete = [](void* data, wl_pointer* wl_pointer, uint32_t axis, int32_t discrete) {};
196#ifdef WL_POINTER_AXIS_VALUE120_SINCE_VERSION
197 auto pointer_axis_value120 = [](void* data, wl_pointer* wl_pointer, uint32_t axis, int32_t value120) {
199 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
200 me->mAxisValue120 += value120;
201 me->mHasAxisValue120 = true;
202 }
203 };
204#endif
205#pragma GCC diagnostic push
206#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
207 mPointerListener = wl_pointer_listener{.enter = pointer_enter, .leave = pointer_leave, .motion = pointer_motion, .button = pointer_button, .axis = pointer_axis, .frame = pointer_frame, .axis_source = pointer_axis_source, .axis_stop = pointer_axis_stop, .axis_discrete = pointer_axis_discrete,
208#ifdef WL_POINTER_AXIS_VALUE120_SINCE_VERSION
209 .axis_value120 = pointer_axis_value120
210#endif
211 };
212#pragma GCC diagnostic pop
213 auto keyboard_keymap = [](void* data, wl_keyboard* wl_keyboard, uint32_t format, int32_t fd, uint32_t size) {
215 char* keymap_string = format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 ? (char*)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0) : (char*)MAP_FAILED; // MAP_PRIVATE required since wl_seat version 7
216 close(fd);
217 if (keymap_string == MAP_FAILED) {
218 GPUError("Cannot read Wayland keymap");
219 return;
220 }
221 xkb_keymap* keymap = xkb_keymap_new_from_string(me->mXKBcontext, keymap_string, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
222 munmap(keymap_string, size);
223 xkb_state* state = keymap ? xkb_state_new(keymap) : nullptr;
224 if (!state) { // Keep the previous keymap
225 GPUError("Cannot compile Wayland keymap");
226 xkb_keymap_unref(keymap);
227 return;
228 }
229 if (me->mXKBkeymap) {
230 xkb_state_unref(me->mXKBstate);
231 xkb_keymap_unref(me->mXKBkeymap);
232 }
233 me->mXKBkeymap = keymap;
234 me->mXKBstate = state;
235 };
236 auto keyboard_enter = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, wl_surface* surface, wl_array* keys) {};
237 auto keyboard_leave = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, wl_surface* surface) {
239 me->focusLost();
240 me->mRepeatKeyCode = -1;
241 };
242 auto keyboard_key = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
244 int32_t symbol = me->GetKey(key, state);
245 int32_t keyPress = (symbol >= 'a' && symbol <= 'z') ? symbol + 'A' - 'a' : symbol;
246 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
247 me->mKeys[keyPress] = keyPress == KEY_SHIFT || !me->consoleShown(); // Held keys only without console, SHIFT also with it (scrolling)
248 me->HandleKey(symbol);
249 if (symbol && me->mRepeatRate > 0 && me->mXKBkeymap && xkb_keymap_key_repeats(me->mXKBkeymap, key + 8)) { // Wayland compositors do not repeat keys, the client must do it
250 me->mRepeatKeyCode = key;
251 me->mRepeatSymbol = symbol;
252 me->mRepeatNext = std::chrono::steady_clock::now() + std::chrono::milliseconds(me->mRepeatDelay);
253 }
254 } else {
255 me->mKeys[keyPress] = false;
256 if ((int64_t)key == me->mRepeatKeyCode) {
257 me->mRepeatKeyCode = -1;
258 }
259 }
260 };
261 auto keyboard_modifiers = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
263 xkb_state_update_mask(me->mXKBstate, mods_depressed, mods_latched, mods_locked, 0, 0, group);
264 };
265 auto keyboard_repeat = [](void* data, wl_keyboard* wl_keyboard, int32_t rate, int32_t delay) {
267 me->mRepeatRate = rate;
268 me->mRepeatDelay = delay;
269 if (rate <= 0) {
270 me->mRepeatKeyCode = -1;
271 }
272 };
273 mKeyboardListener = wl_keyboard_listener{.keymap = keyboard_keymap, .enter = keyboard_enter, .leave = keyboard_leave, .key = keyboard_key, .modifiers = keyboard_modifiers, .repeat_info = keyboard_repeat};
274
275 auto xdg_wm_base_ping = [](void* data, struct xdg_wm_base* xdg_wm_base, uint32_t serial) {
276 xdg_wm_base_pong(xdg_wm_base, serial);
277 };
278 mXdgWmBaseListener = xdg_wm_base_listener{
279 .ping = xdg_wm_base_ping,
280 };
281
282 auto seat_capabilities = [](void* data, struct wl_seat* seat, uint32_t capabilities) {
284 if ((capabilities & WL_SEAT_CAPABILITY_POINTER) && !me->mPointer) {
285 me->mPointer = wl_seat_get_pointer(seat);
286 wl_pointer_add_listener(me->mPointer, &me->mPointerListener, me);
287 }
288 if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !me->mKeyboard) {
289 me->mKeyboard = wl_seat_get_keyboard(seat);
290 wl_keyboard_add_listener(me->mKeyboard, &me->mKeyboardListener, me);
291 }
292 };
293 auto seat_name = [](void* data, struct wl_seat* seat, const char* name) {
294 if (((GPUDisplayFrontendWayland*)data)->mDisplay->GetProcessingSettings().debugLevel >= 2) {
295 GPUInfo("Wayland seat: %s", name);
296 }
297 };
298 mSeatListener = wl_seat_listener{
299 .capabilities = seat_capabilities,
300 .name = seat_name,
301 };
302
303 auto output_geometry = [](void* data, wl_output* output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char* make, const char* model, int32_t transform) {};
304 auto output_mode = [](void* data, wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
306 if (auto* o = me->findOutput(output); o && (flags & WL_OUTPUT_MODE_CURRENT)) {
307 o->refresh = refresh;
308 }
309 };
310 auto output_done = [](void* data, wl_output* output) {};
311 auto output_scale = [](void* data, wl_output* output, int32_t factor) {
313 if (auto* o = me->findOutput(output)) {
314 o->scale = factor;
315 me->updateScale();
316 }
317 };
318#ifdef WL_OUTPUT_NAME_SINCE_VERSION
319 auto output_name = [](void* data, wl_output* output, const char* name) {
321 if (auto* o = me->findOutput(output)) {
322 o->name = name;
323 }
324 if (me->mDisplay->GetProcessingSettings().debugLevel >= 2) {
325 GPUInfo("Wayland output: %s", name);
326 }
327 };
328 auto output_description = [](void* data, wl_output* output, const char* description) {};
329#endif
330 mOutputListener = wl_output_listener{
331 .geometry = output_geometry,
332 .mode = output_mode,
333 .done = output_done,
334 .scale = output_scale,
335#ifdef WL_OUTPUT_NAME_SINCE_VERSION
336 .name = output_name,
337 .description = output_description,
338#endif
339 };
340
341 auto registry_global = [](void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
343 if (me->mDisplay->GetProcessingSettings().debugLevel >= 3) {
344 GPUInfo("Available interface %s (version %u)", interface, version);
345 }
346 auto bindVersion = [version](const wl_interface& iface, uint32_t maxVersion) { return std::min<uint32_t>({version, (uint32_t)iface.version, maxVersion}); }; // Highest version supported by compositor, generated headers, and this code
347 if (strcmp(interface, wl_output_interface.name) == 0) {
348 uint32_t v = bindVersion(wl_output_interface, 4);
349 wl_output* output = (wl_output*)wl_registry_bind(registry, name, &wl_output_interface, v);
350 me->mOutputs.emplace_back(OutputInfo{output, name, v, 1, ""});
351 wl_output_add_listener(output, &me->mOutputListener, me);
352 } else if (strcmp(interface, wl_compositor_interface.name) == 0) {
353 me->mCompositor = (wl_compositor*)wl_registry_bind(registry, name, &wl_compositor_interface, bindVersion(wl_compositor_interface, 6));
354 } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
355 me->mXdgBase = (xdg_wm_base*)wl_registry_bind(registry, name, &xdg_wm_base_interface, bindVersion(xdg_wm_base_interface, 6));
356 xdg_wm_base_add_listener(me->mXdgBase, &me->mXdgWmBaseListener, me);
357 } else if (strcmp(interface, wl_seat_interface.name) == 0 && !me->mSeat) {
358 me->mSeatVersion = bindVersion(wl_seat_interface, 8);
359 me->mSeat = (wl_seat*)wl_registry_bind(registry, name, &wl_seat_interface, me->mSeatVersion);
360 wl_seat_add_listener(me->mSeat, &me->mSeatListener, me);
361 } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
362 me->mDecManager = (zxdg_decoration_manager_v1*)wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1);
363 } else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
364 me->mCursorShapeManager = (wp_cursor_shape_manager_v1*)wl_registry_bind(registry, name, &wp_cursor_shape_manager_v1_interface, 1);
365 } else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
366 me->mViewporter = (wp_viewporter*)wl_registry_bind(registry, name, &wp_viewporter_interface, 1);
367 } else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
368 me->mFractionalScaleManager = (wp_fractional_scale_manager_v1*)wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1);
369 }
370 };
371 auto registry_global_remove = [](void* data, wl_registry* registry, uint32_t name) {
373 for (auto it = me->mOutputs.begin(); it != me->mOutputs.end(); it++) {
374 if (it->globalName == name) {
375 std::erase(me->mSurfaceOutputs, it->output);
376 if (it->version >= WL_OUTPUT_RELEASE_SINCE_VERSION) {
377 wl_output_release(it->output);
378 } else {
379 wl_output_destroy(it->output);
380 }
381 me->mOutputs.erase(it);
382 me->updateScale();
383 break;
384 }
385 }
386 };
387 mRegistryListener = wl_registry_listener{.global = registry_global, .global_remove = registry_global_remove};
388
389 wl_registry_add_listener(mRegistry, &mRegistryListener, this);
390 if (wl_display_roundtrip(mWayland) == -1) {
391 reportError("initial roundtrip");
392 throw std::runtime_error("Wayland initialization failed");
393 }
394
395 if (mCompositor == nullptr || mXdgBase == nullptr || mSeat == nullptr) {
396 throw std::runtime_error("Error getting wayland objects");
397 }
398
399 mSurface = wl_compositor_create_surface(mCompositor);
400 if (mSurface == nullptr) {
401 throw std::runtime_error("Error creating wayland surface");
402 }
403
404 auto surface_enter = [](void* data, wl_surface* surface, wl_output* output) {
406 me->mSurfaceOutputs.emplace_back(output);
407 me->updateScale();
408 };
409 auto surface_leave = [](void* data, wl_surface* surface, wl_output* output) {
411 std::erase(me->mSurfaceOutputs, output);
412 me->updateScale();
413 };
414#ifdef WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION
415 auto surface_preferred_buffer_scale = [](void* data, wl_surface* surface, int32_t factor) {
417 me->mPreferredScale = factor;
418 me->updateScale();
419 };
420 auto surface_preferred_buffer_transform = [](void* data, wl_surface* surface, uint32_t transform) {};
421#endif
422 mSurfaceListener = wl_surface_listener{
423 .enter = surface_enter,
424 .leave = surface_leave,
425#ifdef WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION
426 .preferred_buffer_scale = surface_preferred_buffer_scale,
427 .preferred_buffer_transform = surface_preferred_buffer_transform,
428#endif
429 };
430 wl_surface_add_listener(mSurface, &mSurfaceListener, this);
431
432 if (mViewporter && mFractionalScaleManager) {
433 auto fractional_scale_preferred_scale = [](void* data, wp_fractional_scale_v1* fractional_scale, uint32_t scale) {
435 me->mFractionalScale120 = scale;
436 me->updateScale();
437 };
438 mFractionalScaleListener = wp_fractional_scale_v1_listener{.preferred_scale = fractional_scale_preferred_scale};
439 mViewport = wp_viewporter_get_viewport(mViewporter, mSurface);
440 mFractionalScale = wp_fractional_scale_manager_v1_get_fractional_scale(mFractionalScaleManager, mSurface);
441 wp_fractional_scale_v1_add_listener(mFractionalScale, &mFractionalScaleListener, this);
442 }
443
444 mXdgSurface = xdg_wm_base_get_xdg_surface(mXdgBase, mSurface);
445 if (mXdgSurface == nullptr) {
446 throw std::runtime_error("Error creating wayland xdg surface");
447 }
448 mXdgToplevel = xdg_surface_get_toplevel(mXdgSurface);
449
450 auto xdg_toplevel_handle_configure = [](void* data, xdg_toplevel* toplevel, int32_t width, int32_t height, wl_array* states) {
452 if (me->mDisplay->GetProcessingSettings().debugLevel >= 3) {
453 GPUInfo("Wayland surface resized to %d %d", width, height);
454 }
455 me->mWidthRequested = width;
456 me->mHeightRequested = height;
457 bool fullScreen = false, maximized = false;
458 me->mSuspended = false;
459 const uint32_t* state = (const uint32_t*)states->data;
460 for (size_t i = 0; i < states->size / sizeof(uint32_t); i++) {
461 if (state[i] == XDG_TOPLEVEL_STATE_FULLSCREEN) {
462 fullScreen = true;
463 } else if (state[i] == XDG_TOPLEVEL_STATE_MAXIMIZED) {
464 maximized = true;
465#ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
466 } else if (state[i] == XDG_TOPLEVEL_STATE_SUSPENDED) {
467 me->mSuspended = true;
468#endif
469 }
470 }
471 me->setWindowState(fullScreen, maximized);
472 me->mStateFullScreenOrMaximized = fullScreen || maximized;
473 };
474
475 auto xdg_surface_handle_configure = [](void* data, xdg_surface* surface, uint32_t serial) {
477 xdg_surface_ack_configure(me->mXdgSurface, serial);
478 if (me->mWidthRequested && me->mHeightRequested) {
479 me->mWidthLogical = me->mWidthRequested;
480 me->mHeightLogical = me->mHeightRequested;
481 if (!me->mStateFullScreenOrMaximized) {
482 me->mWidthWindowed = me->mWidthRequested;
483 me->mHeightWindowed = me->mHeightRequested;
484 }
485 } else if (!me->mStateFullScreenOrMaximized) {
486 me->mWidthLogical = me->mWidthWindowed;
487 me->mHeightLogical = me->mHeightWindowed;
488 if (me->mBoundsWidth && me->mBoundsHeight) {
489 me->mWidthLogical = std::min(me->mWidthLogical, me->mBoundsWidth);
490 me->mHeightLogical = std::min(me->mHeightLogical, me->mBoundsHeight);
491 }
492 }
493 me->updateSize();
494 me->mWidthRequested = me->mHeightRequested = 0;
495 };
496
497 auto xdg_toplevel_handle_close = [](void* data, xdg_toplevel* toplevel) {
498 ((GPUDisplayFrontendWayland*)data)->mDisplay->mCfgC.displayControl.store(2);
499 };
500#ifdef XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION
501 auto xdg_toplevel_handle_configure_bounds = [](void* data, xdg_toplevel* toplevel, int32_t width, int32_t height) {
503 me->mBoundsWidth = width;
504 me->mBoundsHeight = height;
505 };
506#endif
507#ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
508 auto xdg_toplevel_handle_wm_capabilities = [](void* data, xdg_toplevel* toplevel, wl_array* capabilities) {
510 me->mCanFullscreen = me->mCanMaximize = false;
511 const uint32_t* cap = (const uint32_t*)capabilities->data;
512 for (size_t i = 0; i < capabilities->size / sizeof(uint32_t); i++) {
513 me->mCanFullscreen |= cap[i] == XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN;
514 me->mCanMaximize |= cap[i] == XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE;
515 }
516 };
517#endif
518
519 mXdgSurfaceListener = xdg_surface_listener{
520 .configure = xdg_surface_handle_configure,
521 };
522
523 mXdgToplevelListener = xdg_toplevel_listener{
524 .configure = xdg_toplevel_handle_configure,
525 .close = xdg_toplevel_handle_close,
526#ifdef XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION
527 .configure_bounds = xdg_toplevel_handle_configure_bounds,
528#endif
529#ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
530 .wm_capabilities = xdg_toplevel_handle_wm_capabilities,
531#endif
532 };
533
534 xdg_surface_add_listener(mXdgSurface, &mXdgSurfaceListener, this);
535 xdg_toplevel_add_listener(mXdgToplevel, &mXdgToplevelListener, this);
536
537 xdg_toplevel_set_title(mXdgToplevel, DISPLAY_WINDOW_NAME);
538 xdg_toplevel_set_min_size(mXdgToplevel, MIN_WIDTH, MIN_HEIGHT); // Logical size, avoids degenerate swapchains / projections
539 xdg_toplevel_set_app_id(mXdgToplevel, "o2-gpu-event-display"); // Used by the compositor for window grouping, window rules, and matching a .desktop file
540
541 if (mDecManager) { // Must be requested before the first commit. Without the protocol (e.g. GNOME), the window has no decorations
542 mXdgDecoration = zxdg_decoration_manager_v1_get_toplevel_decoration(mDecManager, mXdgToplevel);
543 zxdg_toplevel_decoration_v1_set_mode(mXdgDecoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
544 } else if (mDisplay->GetProcessingSettings().debugLevel >= 2) {
545 GPUInfo("Wayland compositor does not support server-side decorations");
546 }
547
548 const bool initialFullScreen = mDisplay->cfgR().fullScreen, initialMaximized = mDisplay->cfgR().maximized; // Read before the configure, which syncs the settings to the (windowed) state
549 wl_surface_commit(mSurface); // Initial commit without buffer, the compositor answers with the first configure
550 if (wl_display_roundtrip(mWayland) == -1) {
551 reportError("initial roundtrip");
552 throw std::runtime_error("Wayland initialization failed");
553 }
554 if (initialFullScreen || initialMaximized) { // Only after the windowed initial configure, so that the compositor has a normal (un-maximized) placement, then wait for the new configure before creating the swapchain
555 if (initialFullScreen) { // Directly: the settings were reset by the configure, the next configure syncs them to the actual state
556 requestFullScreen(true);
557 } else {
558 requestMaximized(true);
559 }
560 if (wl_display_roundtrip(mWayland) == -1) {
561 reportError("initial window state roundtrip");
562 throw std::runtime_error("Wayland initialization failed");
563 }
564 }
565
566 if (InitDisplay()) {
567 return (1);
568 }
569
570 while (mDisplay->mCfgC.displayControl.load() != 2) {
571 bool error = false;
572 while (!error && wl_display_prepare_read(mWayland) != 0) {
573 error = wl_display_dispatch_pending(mWayland) == -1;
574 }
575 if (error) {
576 reportError("dispatching events");
577 break;
578 }
579 wl_display_flush(mWayland);
580 pollfd pfd = {wl_display_get_fd(mWayland), POLLIN, 0};
581 if (poll(&pfd, 1, mSuspended ? 100 : 0) > 0) { // Block while suspended (no rendering), otherwise the loop spins at full CPU
582 if (wl_display_read_events(mWayland) == -1) {
583 reportError("reading events");
584 break;
585 }
586 } else {
587 wl_display_cancel_read(mWayland);
588 }
589 if (wl_display_dispatch_pending(mWayland) == -1) {
590 reportError("dispatching events");
591 break;
592 }
593 if (mDisplay->mCfgC.displayControl.load() == 2) {
594 break;
595 }
596 handleKeyRepeat();
597 HandleUpdatesAndDrawGLScene(mSuspended); // Window not visible, compositor would not release swapchain images
599 }
600
601 ExitDisplay();
602
603 if (mXKBkeymap) {
604 xkb_state_unref(mXKBstate);
605 xkb_keymap_unref(mXKBkeymap);
606 mXKBkeymap = nullptr;
607 }
608 xkb_context_unref(mXKBcontext);
609
610 {
611 std::lock_guard lock(mExitStateMutex);
612 mDisplayRunning = false;
613 mDisplayStoppedCV.notify_all();
614 }
615
616 if (mCursorShapeDevice) {
617 wp_cursor_shape_device_v1_destroy(mCursorShapeDevice);
618 mCursorShapeDevice = nullptr;
619 }
620 if (mCursorShapeManager) {
621 wp_cursor_shape_manager_v1_destroy(mCursorShapeManager);
622 mCursorShapeManager = nullptr;
623 }
624 if (mPointer) {
625 mSeatVersion >= WL_POINTER_RELEASE_SINCE_VERSION ? wl_pointer_release(mPointer) : wl_pointer_destroy(mPointer);
626 mPointer = nullptr;
627 }
628 if (mKeyboard) {
629 mSeatVersion >= WL_KEYBOARD_RELEASE_SINCE_VERSION ? wl_keyboard_release(mKeyboard) : wl_keyboard_destroy(mKeyboard);
630 mKeyboard = nullptr;
631 }
632 if (mXdgDecoration) {
633 zxdg_toplevel_decoration_v1_destroy(mXdgDecoration);
634 mXdgDecoration = nullptr;
635 }
636 if (mFractionalScale) {
637 wp_fractional_scale_v1_destroy(mFractionalScale);
638 mFractionalScale = nullptr;
639 }
640 if (mViewport) {
641 wp_viewport_destroy(mViewport);
642 mViewport = nullptr;
643 }
644 xdg_toplevel_destroy(mXdgToplevel);
645 xdg_surface_destroy(mXdgSurface);
646 wl_surface_destroy(mSurface);
647 if (mFractionalScaleManager) {
648 wp_fractional_scale_manager_v1_destroy(mFractionalScaleManager);
649 mFractionalScaleManager = nullptr;
650 }
651 if (mViewporter) {
652 wp_viewporter_destroy(mViewporter);
653 mViewporter = nullptr;
654 }
655 if (mDecManager) {
656 zxdg_decoration_manager_v1_destroy(mDecManager);
657 mDecManager = nullptr;
658 }
659 for (auto& o : mOutputs) {
660 if (o.version >= WL_OUTPUT_RELEASE_SINCE_VERSION) {
661 wl_output_release(o.output);
662 } else {
663 wl_output_destroy(o.output);
664 }
665 }
666 mOutputs.clear();
667 mSurfaceOutputs.clear();
668 if (mSeat) {
669 if (mSeatVersion >= WL_SEAT_RELEASE_SINCE_VERSION) {
670 wl_seat_release(mSeat);
671 } else {
672 wl_seat_destroy(mSeat);
673 }
674 mSeat = nullptr;
675 }
676 xdg_wm_base_destroy(mXdgBase);
677 wl_compositor_destroy(mCompositor);
678 wl_registry_destroy(mRegistry);
679 wl_display_disconnect(mWayland);
680
681 return (0);
682}
683
685{
686 if (!mCanFullscreen) {
687 GPUWarning("Wayland compositor does not support fullscreen");
688 } else if (set) {
689 xdg_toplevel_set_fullscreen(mXdgToplevel, mSurfaceOutputs.size() ? mSurfaceOutputs.back() : nullptr);
690 } else {
691 xdg_toplevel_unset_fullscreen(mXdgToplevel);
692 }
693}
694
696{
697 if (!mCanMaximize) {
698 GPUWarning("Wayland compositor does not support maximizing windows");
699 } else if (set) {
700 xdg_toplevel_set_maximized(mXdgToplevel);
701 } else {
702 xdg_toplevel_unset_maximized(mXdgToplevel);
703 }
704}
705
707{
708 auto* o = mSurfaceOutputs.size() ? findOutput(mSurfaceOutputs.back()) : nullptr;
709 return o ? o->refresh / 1000.f : 0.f;
710}
711
712GPUDisplayFrontendWayland::OutputInfo* GPUDisplayFrontendWayland::findOutput(wl_output* output)
713{
714 auto it = std::find_if(mOutputs.begin(), mOutputs.end(), [output](const auto& o) { return o.output == output; });
715 return it == mOutputs.end() ? nullptr : &*it;
716}
717
718void GPUDisplayFrontendWayland::updateScale()
719{
720 double scale;
721 if (mFractionalScale) {
722 scale = mFractionalScale120 ? mFractionalScale120 / 120. : 1.;
723 } else {
724 int32_t intScale = mPreferredScale;
725 if (intScale == 0) {
726 intScale = 1;
727 for (auto* output : mSurfaceOutputs) {
728 if (auto* o = findOutput(output)) {
729 intScale = std::max(intScale, o->scale);
730 }
731 }
732 }
733 scale = intScale;
734 }
735 if (scale != mScale && mSurface) {
736 mScale = scale;
737 if (!mFractionalScale) {
738 wl_surface_set_buffer_scale(mSurface, (int32_t)mScale); // Applied with the next commit, i.e. the next present, which happens after the swapchain was recreated with the new size by ResizeScene
739 }
740 updateSize();
741 }
742}
743
744void GPUDisplayFrontendWayland::updateSize()
745{
746 if (mViewport) {
747 wp_viewport_set_destination(mViewport, mWidthLogical, mHeightLogical); // Surface size in logical coordinates, independent of the buffer size, applied with the next present
748 }
749 const int32_t width = mFractionalScale ? (int32_t)std::lround(mWidthLogical * mScale) : (mWidthLogical * (int32_t)mScale);
750 const int32_t height = mFractionalScale ? (int32_t)std::lround(mHeightLogical * mScale) : (mHeightLogical * (int32_t)mScale);
752 if (mDisplayRunning) {
753 ResizeScene(width, height); // Swapchain is recreated by the backend, Vulkan WSI commits the new buffer
754 } else {
755 mDisplayWidth = width; // Initial size, used by InitDisplay via getSize()
757 }
758 }
759}
760
761void GPUDisplayFrontendWayland::reportError(const char* where)
762{
763 const int32_t err = wl_display_get_error(mWayland);
764 if (err == EPROTO) {
765 const wl_interface* iface = nullptr;
766 uint32_t id = 0;
767 const uint32_t code = wl_display_get_protocol_error(mWayland, &iface, &id);
768 GPUError("Wayland protocol error %u on %s@%u while %s", code, iface ? iface->name : "unknown interface", id, where);
769 } else {
770 GPUError("Wayland error while %s: %s", where, strerror(err));
771 }
772}
773
774void GPUDisplayFrontendWayland::handleKeyRepeat()
775{
776 if (mRepeatKeyCode < 0) {
777 return;
778 }
779 const auto now = std::chrono::steady_clock::now();
780 const auto period = std::chrono::microseconds(1000000 / mRepeatRate);
781 if (now >= mRepeatNext + period) { // More than one repeat behind (long frame), no burst of repeats
782 mRepeatNext = now;
783 }
784 while (now >= mRepeatNext && mRepeatKeyCode >= 0) {
785 HandleKey(mRepeatSymbol);
786 mRepeatNext += period;
787 }
788}
789
790void GPUDisplayFrontendWayland::applyScroll()
791{
792 mMouseWheel += mHasAxisValue120 ? (mAxisValue120 * -100 / 120) : (int32_t)(mAxisValue * (-100. / 15.));
793 mAxisValue = 0;
794 mAxisValue120 = 0;
795 mHasAxisValue120 = false;
796}
797
801
807
809{
810 VkWaylandSurfaceCreateInfoKHR info{};
811 info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
812 info.flags = 0;
813 info.display = mWayland;
814 info.surface = mSurface;
815 return vkCreateWaylandSurfaceKHR(*(VkInstance*)instance, &info, nullptr, (VkSurfaceKHR*)surface) != VK_SUCCESS;
816}
817
819{
820 static const char* exts[] = {"VK_KHR_surface", "VK_KHR_wayland_surface"};
821 p = exts;
822 return 2;
823}
header::DataDescription description
int16_t time
Definition RawEventData.h:4
unsigned int uint32_t
std::vector< std::string > keys
int32_t i
int32_t retVal
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder int32_t
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by factor
char error[CURL_ERROR_SIZE]
SurfaceTrackState state
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
uint32_t version
Definition RawData.h:8
const Acts::Surface * surface
StringRef key
int32_t getVulkanSurface(void *instance, void *surface) override
void getSize(int32_t &width, int32_t &height) override
uint32_t getReqVulkanExtensions(const char **&p) override
int32_t HandleUpdatesAndDrawGLScene(bool suspended=false)
void ResizeScene(int32_t width, int32_t height)
std::condition_variable mDisplayStoppedCV
static constexpr const char * DISPLAY_WINDOW_NAME
int32_t InitDisplay(bool initFailure=false)
static constexpr int32_t MIN_WIDTH
static constexpr int32_t MIN_HEIGHT
std::atomic< bool > mDisplayRunning
GPUDisplayControl mCfgC
Definition GPUDisplay.h:93
const GPUSettingsDisplayRenderer & cfgR() const
Definition GPUDisplay.h:72
const GPUSettingsProcessing & GetProcessingSettings() const
Definition GPUDisplay.h:88
GLint GLenum GLint x
Definition glcorearb.h:403
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLenum * rate
Definition glcorearb.h:5735
const GLdouble * v
Definition glcorearb.h:832
GLuint const GLchar * name
Definition glcorearb.h:781
GLint GLsizei GLsizei height
Definition glcorearb.h:270
GLint GLsizei width
Definition glcorearb.h:270
GLint y
Definition glcorearb.h:270
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean * data
Definition glcorearb.h:298
GLboolean GLuint group
Definition glcorearb.h:3991
GLbitfield flags
Definition glcorearb.h:1570
GLint GLint GLsizei GLint GLenum format
Definition glcorearb.h:275
GLuint * states
Definition glcorearb.h:4932
uint64_t now() noexcept
Definition Clock.h:69
GPUDisplayFrontend * createGPUDisplayFrontendWayland()
constexpr auto make(F f) -> std::array< int, N >
Definition SMatrixGPU.h:293
Polygon< T > close(Polygon< T > polygon)
Definition Polygon.h:126
constexpr value_T me
Definition TrackUtils.h:128