Skip to content

Commit

Permalink
vulkan/wsi/x11: Implement capture hotkey using the keymap
Browse files Browse the repository at this point in the history
This way, we can avoid opening another connection. The capture key is
changes to F1 because F12 has issues on Wayland. (After pressing F12,
all keys become unresponsive, refocussing the window fixes it)

Fixes: 291fa05  ("vulkan/wsi/x11: Capture traces using a hotkey")
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/9578
Reviewed-by: Konstantin Seurer <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24710>
(cherry picked from commit 8c98814)
  • Loading branch information
KonstantinSeurer authored and dcbaker-intel committed Sep 22, 2023
1 parent 9f8a82f commit 0ee3d2b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .pick_status.json
Original file line number Diff line number Diff line change
Expand Up @@ -10654,7 +10654,7 @@
"description": "vulkan/wsi/x11: Implement capture hotkey using the keymap",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "291fa055453e2832423e6f82914131fa2c748abc",
"notes": null
Expand Down
2 changes: 1 addition & 1 deletion docs/envvars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ Core Mesa environment variables

A comma-separated list of trace types used for offline analysis. The
option names are equal to the file extension. Traces are dumped into ``/tmp``.
Captures can be triggered by pressing ``F12`` with the application window
Captures can be triggered by pressing ``F1`` with the application window
focused (Currently X11 only) or via :envvar:`MESA_VK_TRACE_FRAME` and
:envvar:`MESA_VK_TRACE_TRIGGER`.

Expand Down
2 changes: 2 additions & 0 deletions src/vulkan/wsi/wsi_common_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ struct wsi_swapchain {
VkQueue queue;
} blit;

bool capture_key_pressed;

/* Command pools, one per queue family */
VkCommandPool *cmd_pools;

Expand Down
55 changes: 23 additions & 32 deletions src/vulkan/wsi/wsi_common_x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,6 @@ struct x11_swapchain {
bool has_mit_shm;

xcb_connection_t * conn;
xcb_connection_t * capture_conn;
xcb_window_t window;
xcb_gc_t gc;
uint32_t depth;
Expand Down Expand Up @@ -1687,24 +1686,36 @@ x11_present_to_x11_sw(struct x11_swapchain *chain, uint32_t image_index,
static void
x11_capture_trace(struct x11_swapchain *chain)
{
if (!chain->capture_conn)
#ifdef XCB_KEYSYMS_AVAILABLE
VK_FROM_HANDLE(vk_device, device, chain->base.device);
if (!device->physical->instance->trace_mode)
return;

xcb_generic_event_t *event;
while ((event = xcb_poll_for_event(chain->capture_conn))) {
if ((event->response_type & ~0x80) != XCB_KEY_PRESS) {
free(event);
continue;
}
xcb_query_keymap_cookie_t keys_cookie = xcb_query_keymap(chain->conn);

xcb_generic_error_t *error = NULL;
xcb_query_keymap_reply_t *keys = xcb_query_keymap_reply(chain->conn, keys_cookie, &error);
if (error) {
free(error);
return;
}

VK_FROM_HANDLE(vk_device, device, chain->base.device);
xcb_key_symbols_t *key_symbols = xcb_key_symbols_alloc(chain->conn);
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(key_symbols, XK_F1);
if (keycodes) {
xcb_keycode_t keycode = keycodes[0];
free(keycodes);

simple_mtx_lock(&device->trace_mtx);
device->trace_hotkey_trigger = true;
bool capture_key_pressed = keys->keys[keycode / 8] & (1u << (keycode % 8));
device->trace_hotkey_trigger = capture_key_pressed && (capture_key_pressed != chain->base.capture_key_pressed);
chain->base.capture_key_pressed = capture_key_pressed;
simple_mtx_unlock(&device->trace_mtx);

free(event);
}

xcb_key_symbols_free(key_symbols);
free(keys);
#endif
}

/**
Expand Down Expand Up @@ -2353,8 +2364,6 @@ x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
XCB_PRESENT_EVENT_MASK_NO_EVENT);
xcb_discard_reply(chain->conn, cookie.sequence);

xcb_disconnect(chain->capture_conn);

pthread_mutex_destroy(&chain->present_poll_mutex);
pthread_mutex_destroy(&chain->present_progress_mutex);
pthread_cond_destroy(&chain->present_progress_cond);
Expand Down Expand Up @@ -2638,24 +2647,6 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
if (chain == NULL)
return VK_ERROR_OUT_OF_HOST_MEMORY;

#ifdef XCB_KEYSYMS_AVAILABLE
VK_FROM_HANDLE(vk_device, vk_device, device);
if (vk_device->capture_trace) {
chain->capture_conn = xcb_connect(NULL, NULL);
assert(!xcb_connection_has_error(chain->capture_conn));

xcb_key_symbols_t *key_symbols = xcb_key_symbols_alloc(conn);
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(key_symbols, XK_F12);
if (keycodes) {
xcb_grab_key(chain->capture_conn, 1, window, XCB_MOD_MASK_ANY, keycodes[0],
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
}
xcb_key_symbols_free(key_symbols);

xcb_flush(chain->capture_conn);
}
#endif

int ret = pthread_mutex_init(&chain->present_progress_mutex, NULL);
if (ret != 0) {
vk_free(pAllocator, chain);
Expand Down

0 comments on commit 0ee3d2b

Please sign in to comment.