slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.56k stars 604 forks source link

Panic when creating window with femtovg on X11 #2162

Closed mgunyho closed 1 year ago

mgunyho commented 1 year ago

Hi!

I'm trying to run the memory example (or any example really), but I am getting the following error when the program starts:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { raw_code: Some(12291), raw_os_message: None, kind: OutOfMemory }', internal/backends/winit/renderer/femtovg/glcontext.rs:119:82
Full backtrace ``` thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { raw_code: Some(12291), raw_os_message: None, kind: OutOfMemory }', internal/backends/winit/renderer/femtovg/glcontext.rs:119:82 stack backtrace: 0: 0x56476254eac0 - std::backtrace_rs::backtrace::libunwind::trace::h1d00f3fcf4cb5ac4 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x56476254eac0 - std::backtrace_rs::backtrace::trace_unsynchronized::h920a6ff332484ee2 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x56476254eac0 - std::sys_common::backtrace::_print_fmt::hd7323920c925af6d at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:65:5 3: 0x56476254eac0 - ::fmt::h3155a8c966b4beb5 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:44:22 4: 0x564762574dde - core::fmt::write::h062c617411b691df at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/fmt/mod.rs:1209:17 5: 0x564762548335 - std::io::Write::write_fmt::hb61fdf1275c61e1c at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/mod.rs:1682:15 6: 0x56476254e885 - std::sys_common::backtrace::_print::hd1b4d9664ab500e0 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:47:5 7: 0x56476254e885 - std::sys_common::backtrace::print::hca896ae22beb06cb at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:34:9 8: 0x56476254feff - std::panicking::default_hook::{{closure}}::h0b5eeed5cf36ab5f at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:267:22 9: 0x56476254fc3a - std::panicking::default_hook::h8932b573145a321b at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:286:9 10: 0x5647625505f8 - std::panicking::rust_panic_with_hook::h4b1447a24e3e94f8 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:688:13 11: 0x564762550397 - std::panicking::begin_panic_handler::{{closure}}::h8701da9995a3820c at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:579:13 12: 0x56476254ef6c - std::sys_common::backtrace::__rust_end_short_backtrace::hb696c5ed02a01598 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:137:18 13: 0x5647625500b2 - rust_begin_unwind at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:575:5 14: 0x5647615adb83 - core::panicking::panic_fmt::h8aa706a976963c88 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:65:14 15: 0x5647615ade43 - core::result::unwrap_failed::h065c02f906ca4578 at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/result.rs:1791:5 16: 0x5647616cafeb - core::result::Result::unwrap::hcac2ceb68132391c at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/result.rs:1113:23 17: 0x5647616e425d - i_slint_backend_winit::renderer::femtovg::glcontext::OpenGLContext::new_context::he8993b7862e3d89f at .../slint/internal/backends/winit/renderer/femtovg/glcontext.rs:119:26 ```

This doesn't happen on v0.3.2, but on v0.3.3 and later (including master @ f34b1bda2) it does.

I tried to run the basic window example from glutin (0.30.3, which is what Slint is using based on Cargo.lock), and that worked fine. I found the equivalent call to create_window_surface(config, attrs), and I checked that the attrs is the same for both, but config was Glx(...) for the glutin example, but Egl(...) for Slint.

I know basically nothing about graphics/OpenGL, but if I understand correctly, the problem is that I'm on X11, which would need GLX, but Slint is trying to use EGL.

I found a similar issue from egui: https://github.com/emilk/egui/issues/2457 and related PR: https://github.com/emilk/egui/pull/2541.

Here's what the glutin example prints:

Picked a config with 0 samples
Running on Mesa DRI Intel(R) HD Graphics (ILK)
OpenGL Version OpenGL ES 2.0 Mesa 21.2.6
Shaders version on OpenGL ES GLSL ES 1.0.16

I'm using Ubuntu 20.04 on an ancient Thinkpad.

As I said, I don't really know what I'm talking about here, so feel free to update the title if it's incorrect or missing something relevant.

tronical commented 1 year ago

Thank you for the report!

I know basically nothing about graphics/OpenGL, but if I understand correctly, the problem is that I'm on X11, which would need GLX, but Slint is trying to use EGL.

It’s odd to me that EGL fails, but you’re totally right: we should try GLX in this case.

I can’t quite reproduce this setup myself, but I can prepare a patch to try a fallback. I’ll let you know when I have something:)

tronical commented 1 year ago

@mgunyho I've made a patch at https://github.com/slint-ui/slint/pull/2165 - it's basically a one-liner (or two-liner :). Could you give it a try?

mgunyho commented 1 year ago

Thanks for the quick response and patch! However, with 5f1165b93, it still crashes with the same error, though the stack is slightly different:

$ cargo run -p memory

...

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { raw_code: Some(12291), raw_os_message: None, kind: OutOfMemory }', internal/backends/winit/renderer/femtovg/glcontext.rs:121:82

...

  16:     0x55cfece36dfb - core::result::Result<T,E>::unwrap::h6b4d8991b7c0a9c1
                               at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/result.rs:1113:23
  17:     0x55cfecec8d35 - i_slint_backend_winit::renderer::femtovg::glcontext::OpenGLContext::new_context::h94eaf85356bb03e9
                               at .../slint/internal/backends/winit/renderer/femtovg/glcontext.rs:121:26
  18:     0x55cfece05343 - <i_slint_backend_winit::renderer::femtovg::FemtoVGRenderer as i_slint_backend_winit::renderer::WinitCompatibleRenderer>::show::h808eaa0ab819455f
                               at .../slint/internal/backends/winit/renderer/femtovg.rs:58:30

If I do println!("{:?}", config) just before create_window_surface, it still shows it's using EGL: Egl(Config { inner: Config { raw: EglConfig(0x561145559990), display: EglDisplay(0x561145520d70) } }).

I put a println!("hello"); under the #[cfg(all(feature = "x11"), not(target_family="windows"))] condition which you added, it gets printed.

So looks like glutin is not applying the fallback, even though it should.

tronical commented 1 year ago

Thanks for checking. Ok, that matches also what the egui folks observed then. Tomorrow I'll try to make a patch to use two separated attempts at creating the display instead of asking glutin to do that in one-shot with PreferXThenY.

tronical commented 1 year ago

Apologies for the delay, it took me longer to re-do the patch. I've updated the PR with a new attempt that tries to deal with the EGL surface creation failing at a later stage and then re-try with GLX.

mgunyho commented 1 year ago

Looks like on my system it tried to build with winit version 0.28.1 (I guess a leftover in Cargo.lock from some previous build), so I had to apply

diff --git a/internal/backends/winit/renderer/femtovg/glcontext.rs b/internal/backends/winit/renderer/femtovg/glcontext.rs
index 42743f31e..c51676c6e 100644
--- a/internal/backends/winit/renderer/femtovg/glcontext.rs
+++ b/internal/backends/winit/renderer/femtovg/glcontext.rs
@@ -61,7 +61,7 @@ pub fn new_context(
                 if #[cfg(target_os = "macos")] {
                     let prefs = [glutin::display::DisplayApiPreference::Cgl];
                 } else if #[cfg(all(feature = "x11", not(target_family = "windows")))] {
-                    let prefs = [glutin::display::DisplayApiPreference::Egl, glutin::display::DisplayApiPreference::Glx(Box::new(winit::platform::unix::register_xlib_error_hook))];
+                    let prefs = [glutin::display::DisplayApiPreference::Egl, glutin::display::DisplayApiPreference::Glx(Box::new(winit::platform::x11::register_xlib_error_hook))];
                 } else if #[cfg(not(target_family = "windows"))] {
                     let prefs = [glutin::display::DisplayApiPreference::Egl];
                 } else {

But other than that, it works! Including the latest version, https://github.com/slint-ui/slint/commit/746fb61495b29277c342018645a7cb14ecab56b4

tronical commented 1 year ago

Excellent, thanks a lot for trying!! Yeah, I noticed the same build issue (and a few more).

mgunyho commented 1 year ago

I also tried 3441320, and it works as well.