guvcview with SDL backend uses GTK in two separate threads which is not...
A simple v4l2 full-featured video grabber
Brought to you by:
assisp
In https://bugzilla.redhat.com/show_bug.cgi?id=2269649 , we found that guvcview in Fedora - which, at the time, was built with the SDL backend - was crashing frequently on GNOME-on-Wayland. The investigation went through various places and wound up in libdecor - https://gitlab.freedesktop.org/libdecor/libdecor/-/issues/72 . But now a GTK dev says the problem is really with guvcview:
"Seems to be guvcview runs SDL in a separate thread, and GTK in the main thread. GTK is single threaded, so that can't work."
That is, guvcview uses GTK in its main thread, but it also runs SDL in a separate thread; SDL also uses GTK, via libdecor, so we have GTK running in two threads, and that won't work.
Hi, I've been running guvcview in wayland with the sdl backend for 2+ years (in Arch) without any issues. The gui and the render run in different threads, this is how guvcview is designed.
I've tested in gnome, plasma and sway and never had any issues.
Could this be related to the way sdl is build?
Last edit: Paulo Assis 2024-11-14
The problem started appearing on Fedora when the whole stack was native Wayland, I think. But according to the GTK devs, running GTK in two threads within the app is just fundamentally wrong and not supportable even if it happened to work in some configurations, they're not going to do anything to fix the crash.
edit: and yeah, I guess SDL build could affect it. For instance, is arch's SDL actually built against libdecor? This bug will only happen with libdecor, as that's what causes SDL to use GTK.
Last edit: Adam Williamson 2024-11-14
This can't be the issue.
Building guvcview with Qt and disabling Gtk also causes the same crash (according to your tests). Disabling Gtk makes sure guvcview isn't even linked against Gtk.
Guvcview only uses Gtk in the main thread. The render thread uses SDL or SFML.
I don't know any distribution in that SDL has a direct dependency on Gtk. This just doesn't make any sense. SDL is supposed to be system independent.
libdecor is NOT a gtk lib, although it has a optional Gtk pluggin (https://gitlab.freedesktop.org/libdecor/libdecor)
In Arch libdecor has Gtk3 as optional (https://gitlab.archlinux.org/archlinux/packaging/packages/libdecor/-/blob/main/PKGBUILD?ref_type=heads)
Last edit: Paulo Assis 2024-11-14
libdecor can be disabled programmatically :
https://wiki.libsdl.org/SDL2/SDL_HINT_VIDEO_WAYLAND_ALLOW_LIBDECOR
just need to add
SDL_SetHint("SDL_HINT_VIDEO_WAYLAND_ALLOW_LIBDECOR", "0");
to render_sdl2.c
just after SDL_SetHint("SDL_HINT_RENDER_SCALE_QUALITY", "1");
around line 84
Checking SDL API I found that libdecor is used over xdg-shell when xdg-decoration protocol is unavailable.
So if xdg-decoration is available native decorations are used instead.
In Arch both SDL and libdecor are build with a dependency on wayland-protocols package that includes xdg-decoration, so in arch at least, both SDL and libdecor use native wayland window decorations instead of the Gtk3 pluggin.
I don't think guvcview needs to or should change, I created a MR to libdecor to not attempt to use the gtk decoration plugin and fall back to the cairo one, if it's not run from the main thread. Cairo doesn't care what thread it is run from. Ideally libdecor can address this in the future to get GTK based decorations for this use case.
Thanks Jonas! Sorry, I misunderstood the PR as meaning "the app will crash intentionally" or something equally bad, not "it'll fall back smoothly to some other decorations".
I guess I'll check if Fedora's SDL can be built with wayland-protocols, too...
Huh, Fedora's SDL2 and libdecor both buildrequire
pkgconfig(wayland-protocols), so I'm not sure why we wind up in GTK3 nevertheless. Will poke into it a bit.this is because mutter doesn't implement xdg-decorations and forces csd (client side decorations), this seems like a lost battle. All wayland compositors, implement ssd (server side decorations) with the exception of gnome and enlighthment.
I think in arch libdecor doesn't force the gtk3 plugin, so SDL in gnome ends up using the default cairo backend.
Last edit: Paulo Assis 2024-11-14