From 611f403ec6d28744be9a8c82f45eabb7e7fce4b6 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 04:46:02 +0200 Subject: [PATCH 01/22] glue: create one shared render factory per application Follow-up to ruis issue #190: each platform glue now creates a single shared render factory (the shared/headless render context) in the application constructor and passes it into every per-window context and the resource loader. The shared-context member (resource_loader_ruis_rendering_context) is renamed to render_factory (shared_ref) and the common_shaders type becomes factory::shaders. --- src/ruisapp/glue/android/application.cxx | 10 ++++--- src/ruisapp/glue/ios/application.cxx | 9 +++--- src/ruisapp/glue/ios/glue.mm | 1 + .../glue/linux/wayland/application.cxx | 6 ++-- .../glue/linux/wayland/application.hxx | 28 +++++++++++------ src/ruisapp/glue/linux/xorg/glue.cxx | 24 ++++++++------- src/ruisapp/glue/macos/application.cxx | 15 +++++----- src/ruisapp/glue/macos/application.hxx | 4 +-- src/ruisapp/glue/sdl/application.cxx | 30 ++++++++++--------- src/ruisapp/glue/sdl/application.hxx | 4 +-- src/ruisapp/glue/windows/application.cxx | 20 ++++++------- src/ruisapp/glue/windows/application.hxx | 4 +-- src/ruisapp/glue/windows/glue.cxx | 2 ++ tests/app/src/cube_widget.cpp | 16 ++++++---- 14 files changed, 98 insertions(+), 75 deletions(-) diff --git a/src/ruisapp/glue/android/application.cxx b/src/ruisapp/glue/android/application.cxx index 7fe3659..056c441 100644 --- a/src/ruisapp/glue/android/application.cxx +++ b/src/ruisapp/glue/android/application.cxx @@ -22,6 +22,7 @@ along with this program. If not, see . #include "application.hxx" #include +#include #include #include @@ -107,13 +108,14 @@ app_window& application_glue::make_window(ruisapp::window_parameters window_para // status bar and the navigation bar. ruis_native_window.get().set_fullscreen(fullscreen); - auto rendering_context = utki::make_shared(ruis_native_window); + auto rendering_factory = utki::make_shared(ruis_native_window); + auto rendering_context = utki::make_shared(ruis_native_window, rendering_factory); - auto common_render_objects = utki::make_shared(rendering_context); - auto common_shaders = rendering_context.get().make_shaders(); + auto common_render_objects = utki::make_shared(rendering_factory.get()); + auto common_shaders = rendering_factory.get().make_shaders(); auto ruis_resource_loader = utki::make_shared( - rendering_context, // + rendering_factory, // common_render_objects ); diff --git a/src/ruisapp/glue/ios/application.cxx b/src/ruisapp/glue/ios/application.cxx index 6dced28..6b311e9 100644 --- a/src/ruisapp/glue/ios/application.cxx +++ b/src/ruisapp/glue/ios/application.cxx @@ -85,13 +85,14 @@ app_window& application_glue::make_window(ruisapp::window_parameters window_para std::move(window_params) ); - auto rendering_context = utki::make_shared(ruis_native_window); + auto rendering_factory = utki::make_shared(ruis_native_window); + auto rendering_context = utki::make_shared(ruis_native_window, rendering_factory); - auto common_render_objects = utki::make_shared(rendering_context); - auto common_shaders = rendering_context.get().make_shaders(); + auto common_render_objects = utki::make_shared(rendering_factory.get()); + auto common_shaders = rendering_factory.get().make_shaders(); auto ruis_resource_loader = utki::make_shared( - rendering_context, // + rendering_factory, // common_render_objects ); diff --git a/src/ruisapp/glue/ios/glue.mm b/src/ruisapp/glue/ios/glue.mm index 4ed5c49..97d4563 100644 --- a/src/ruisapp/glue/ios/glue.mm +++ b/src/ruisapp/glue/ios/glue.mm @@ -32,6 +32,7 @@ #include #include +#include // include implementations #include "application.cxx" diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index ad0e418..7ee0f83 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -25,9 +25,11 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL # include +# include #elif defined(RUISAPP_RENDER_OPENGLES) # include +# include #else # error "Unknown graphics API" @@ -215,9 +217,9 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window .updater = this->updater, .renderer = utki::make_shared( #ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #else # error "Unknown graphics API" #endif diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 617df43..2186b81 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -30,6 +30,16 @@ along with this program. If not, see . #include "../../../window.hpp" #include "../../unix_common.hxx" +#ifdef RUISAPP_RENDER_OPENGL +# include +# include +#elif defined(RUISAPP_RENDER_OPENGLES) +# include +# include +#else +# error "Unknown graphics API" +#endif + #include "window.hxx" namespace { @@ -127,8 +137,8 @@ public: // TODO: make windowless shared egl context const utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref resource_loader_ruis_rendering_context; - const utki::shared_ref common_shaders; + const utki::shared_ref render_factory; + const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; const utki::shared_ref ruis_style_provider; @@ -168,27 +178,27 @@ public: nullptr // no shared gl context ) ), - resource_loader_ruis_rendering_context( + render_factory( #ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) #elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) #else # error "Unknown graphics API" #endif ), common_shaders( // [&]() { - utki::assert(this->resource_loader_ruis_rendering_context.to_shared_ptr(), SL); - return this->resource_loader_ruis_rendering_context.get().make_shaders(); + utki::assert(this->render_factory.to_shared_ptr(), SL); + return this->render_factory.get().make_shaders(); }() ), common_render_objects( // - utki::make_shared(this->resource_loader_ruis_rendering_context) + utki::make_shared(this->render_factory.get()) ), ruis_resource_loader( // utki::make_shared( - this->resource_loader_ruis_rendering_context, // + this->render_factory, // this->common_render_objects ) ), diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index c5240a3..f762a58 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -29,9 +29,11 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL # include +# include #elif defined(RUISAPP_RENDER_OPENGLES) # include +# include #else # error "Unknown graphics API" @@ -87,8 +89,8 @@ class application_glue : public utki::destructable utki::version_duplet gl_version; utki::shared_ref shared_gl_context_native_window; - utki::shared_ref resource_loader_ruis_rendering_context; - utki::shared_ref common_shaders; + utki::shared_ref render_factory; + utki::shared_ref common_shaders; utki::shared_ref common_render_objects; utki::shared_ref ruis_resource_loader; @@ -124,27 +126,27 @@ class application_glue : public utki::destructable nullptr ) ), - resource_loader_ruis_rendering_context( + render_factory( #ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) #elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) #else # error "Unknown graphics API" #endif ), common_shaders( // [&]() { - utki::assert(this->resource_loader_ruis_rendering_context.to_shared_ptr(), SL); - return this->resource_loader_ruis_rendering_context.get().make_shaders(); + utki::assert(this->render_factory.to_shared_ptr(), SL); + return this->render_factory.get().make_shaders(); }() ), common_render_objects( // - utki::make_shared(this->resource_loader_ruis_rendering_context) + utki::make_shared(this->render_factory.get()) ), ruis_resource_loader( // utki::make_shared( - this->resource_loader_ruis_rendering_context, // + this->render_factory, // this->common_render_objects ) ), @@ -176,9 +178,9 @@ class application_glue : public utki::destructable .updater = this->updater, .renderer = utki::make_shared( #ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #else # error "Unknown graphics API" #endif diff --git a/src/ruisapp/glue/macos/application.cxx b/src/ruisapp/glue/macos/application.cxx index 7182d1b..13f804a 100644 --- a/src/ruisapp/glue/macos/application.cxx +++ b/src/ruisapp/glue/macos/application.cxx @@ -22,6 +22,7 @@ along with this program. If not, see . #include "application.hxx" #include +#include #include namespace { @@ -94,19 +95,17 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : }, nullptr // no shared gl context )), - resource_loader_ruis_rendering_context([&]() { + render_factory([&]() { utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n'); - auto c = utki::make_shared(this->shared_gl_context_native_window); + auto c = utki::make_shared(this->shared_gl_context_native_window); utki::logcat_debug("application_glue::application_glue(): shared gl context created", '\n'); return c; }()), - common_shaders(this->resource_loader_ruis_rendering_context.get().make_shaders()), - common_render_objects( - utki::make_shared(this->resource_loader_ruis_rendering_context) - ), + common_shaders(this->render_factory.get().make_shaders()), + common_render_objects(utki::make_shared(this->render_factory.get())), ruis_resource_loader( // utki::make_shared( - this->resource_loader_ruis_rendering_context, // + this->render_factory, // this->common_render_objects ) ), @@ -134,7 +133,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window }, .updater = this->updater, .renderer = utki::make_shared( - utki::make_shared(ruis_native_window), // + utki::make_shared(ruis_native_window, this->render_factory), // this->common_shaders, this->common_render_objects ), diff --git a/src/ruisapp/glue/macos/application.hxx b/src/ruisapp/glue/macos/application.hxx index ebcbfaa..71b532e 100644 --- a/src/ruisapp/glue/macos/application.hxx +++ b/src/ruisapp/glue/macos/application.hxx @@ -65,8 +65,8 @@ class application_glue : public utki::destructable const utki::version_duplet gl_version; const utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref resource_loader_ruis_rendering_context; - const utki::shared_ref common_shaders; + const utki::shared_ref render_factory; + const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; diff --git a/src/ruisapp/glue/sdl/application.cxx b/src/ruisapp/glue/sdl/application.cxx index af02932..f786047 100644 --- a/src/ruisapp/glue/sdl/application.cxx +++ b/src/ruisapp/glue/sdl/application.cxx @@ -26,9 +26,11 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL // # include # include +# include #elif defined(RUISAPP_RENDER_OPENGLES) // # include # include +# include #else # error "Unknown graphics API" #endif @@ -51,27 +53,27 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : nullptr // no shared gl context ) ), - resource_loader_ruis_rendering_context( + render_factory( # ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) # elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(this->shared_gl_context_native_window) + utki::make_shared(this->shared_gl_context_native_window) # else # error "Unknown graphics API" # endif ), common_shaders( // [&]() { - utki::assert(this->resource_loader_ruis_rendering_context.to_shared_ptr(), SL); - return this->resource_loader_ruis_rendering_context.get().make_shaders(); + utki::assert(this->render_factory.to_shared_ptr(), SL); + return this->render_factory.get().make_shaders(); }() ), common_render_objects( // - utki::make_shared(this->resource_loader_ruis_rendering_context) + utki::make_shared(this->render_factory.get()) ), ruis_resource_loader( // utki::make_shared( - this->resource_loader_ruis_rendering_context, // + this->render_factory, // this->common_render_objects ) ), @@ -146,13 +148,13 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ); #if CFG_OS_NAME == CFG_OS_NAME_EMSCRIPTEN - auto rendering_context = utki::make_shared(ruis_native_window); + auto render_factory = utki::make_shared(ruis_native_window); - auto common_render_objects = utki::make_shared(rendering_context); - auto common_shaders = rendering_context.get().make_shaders(); + auto common_render_objects = utki::make_shared(*render_factory); + auto common_shaders = render_factory.get().make_shaders(); auto ruis_resource_loader = utki::make_shared( - rendering_context, // + render_factory, // common_render_objects ); @@ -171,16 +173,16 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window .renderer = #if CFG_OS_NAME == CFG_OS_NAME_EMSCRIPTEN utki::make_shared( - std::move(rendering_context), + utki::make_shared(ruis_native_window, render_factory), std::move(common_shaders), std::move(common_render_objects) ), #else utki::make_shared( # ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), # elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), # else # error "Unknown graphics API" # endif diff --git a/src/ruisapp/glue/sdl/application.hxx b/src/ruisapp/glue/sdl/application.hxx index 5395b4e..0cd12cd 100644 --- a/src/ruisapp/glue/sdl/application.hxx +++ b/src/ruisapp/glue/sdl/application.hxx @@ -69,8 +69,8 @@ public: // only one window allowed on emscripten, so we don't create hidden window for shared GL context #if CFG_OS_NAME != CFG_OS_NAME_EMSCRIPTEN const utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref resource_loader_ruis_rendering_context; - const utki::shared_ref common_shaders; + const utki::shared_ref render_factory; + const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; const utki::shared_ref ruis_style_provider; diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index c5f1a1b..6c20deb 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -110,26 +110,24 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : }, nullptr // no shared gl context )), - resource_loader_ruis_rendering_context([&]() { + render_factory([&]() { utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n'); #ifdef RUISAPP_RENDER_OPENGL - using context_type = ruis::render::opengl::context; + using factory_type = ruis::render::opengl::factory; #elif defined(RUISAPP_RENDER_OPENGLES) - using context_type = ruis::render::opengles::context; + using factory_type = ruis::render::opengles::factory; #else # error "Unknown graphics API" #endif - auto c = utki::make_shared(this->shared_gl_context_native_window); + auto c = utki::make_shared(this->shared_gl_context_native_window); utki::logcat_debug("application_glue::application_glue(): shared gl context created", '\n'); return c; }()), - common_shaders(this->resource_loader_ruis_rendering_context.get().make_shaders()), - common_render_objects( - utki::make_shared(this->resource_loader_ruis_rendering_context) - ), + common_shaders(this->render_factory.get().make_shaders()), + common_render_objects(utki::make_shared(this->render_factory.get())), ruis_resource_loader( // utki::make_shared( - this->resource_loader_ruis_rendering_context, // + this->render_factory, // this->common_render_objects ) ), @@ -187,9 +185,9 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window .updater = this->updater, .renderer = utki::make_shared( #ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window), + utki::make_shared(ruis_native_window, this->render_factory), #else # error "Unknown graphics API" #endif diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 8aa3d35..2f82ff9 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -75,8 +75,8 @@ class application_glue : public utki::destructable utki::shared_ref display = utki::make_shared(); const utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref resource_loader_ruis_rendering_context; - const utki::shared_ref common_shaders; + const utki::shared_ref render_factory; + const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; diff --git a/src/ruisapp/glue/windows/glue.cxx b/src/ruisapp/glue/windows/glue.cxx index 6e1a1f2..16793ef 100644 --- a/src/ruisapp/glue/windows/glue.cxx +++ b/src/ruisapp/glue/windows/glue.cxx @@ -29,9 +29,11 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL # include +# include #elif defined(RUISAPP_RENDER_OPENGLES) # include # include +# include #else # error "Unknown graphics API" #endif diff --git a/tests/app/src/cube_widget.cpp b/tests/app/src/cube_widget.cpp index dae17bd..df500ff 100644 --- a/tests/app/src/cube_widget.cpp +++ b/tests/app/src/cube_widget.cpp @@ -23,8 +23,9 @@ cube_widget::cube_widget( ruis::vec3(-1, -1, 1), ruis::vec3(-1, -1, 1), ruis::vec3(1, -1, -1), ruis::vec3(1, -1, 1)} }; - auto pos_vbo = - this->context.get().renderer.get().rendering_context.get().make_vertex_buffer(utki::make_span(cube_pos)); + auto pos_vbo = this->context.get().renderer.get().rendering_context.get().factory.get().make_vertex_buffer( + utki::make_span(cube_pos) + ); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) std::array cube_tex = { @@ -36,8 +37,9 @@ cube_widget::cube_widget( ruis::vec2(0, 0), ruis::vec2(1, 0), ruis::vec2(0, 1), ruis::vec2(1, 0), ruis::vec2(1, 1), ruis::vec2(0, 1)} }; - auto tex_vbo = - this->context.get().renderer.get().rendering_context.get().make_vertex_buffer(utki::make_span(cube_tex)); + auto tex_vbo = this->context.get().renderer.get().rendering_context.get().factory.get().make_vertex_buffer( + utki::make_span(cube_tex) + ); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) std::array indices = { @@ -47,13 +49,15 @@ cube_widget::cube_widget( } }; - auto cube_indices = - this->context.get().renderer.get().rendering_context.get().make_index_buffer(utki::make_span(indices)); + auto cube_indices = this->context.get().renderer.get().rendering_context.get().factory.get().make_index_buffer( + utki::make_span(indices) + ); this->cubeVAO = this->context.get() .renderer.get() .rendering_context.get() + .factory.get() .make_vertex_array({pos_vbo, tex_vbo}, cube_indices, ruis::render::vertex_array::mode::triangles) .to_shared_ptr(); From 2d95c1857a39de40b2455ad30c88fd17569271f4 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 14:42:45 +0200 Subject: [PATCH 02/22] test app: use context.factory() accessor instead of the removed factory member Follows the ruis context::factory member -> factory() method change. Refs cppfw/ruis#190 --- tests/app/src/cube_widget.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/app/src/cube_widget.cpp b/tests/app/src/cube_widget.cpp index df500ff..2b3a7e6 100644 --- a/tests/app/src/cube_widget.cpp +++ b/tests/app/src/cube_widget.cpp @@ -23,7 +23,7 @@ cube_widget::cube_widget( ruis::vec3(-1, -1, 1), ruis::vec3(-1, -1, 1), ruis::vec3(1, -1, -1), ruis::vec3(1, -1, 1)} }; - auto pos_vbo = this->context.get().renderer.get().rendering_context.get().factory.get().make_vertex_buffer( + auto pos_vbo = this->context.get().renderer.get().rendering_context.get().factory().make_vertex_buffer( utki::make_span(cube_pos) ); @@ -37,7 +37,7 @@ cube_widget::cube_widget( ruis::vec2(0, 0), ruis::vec2(1, 0), ruis::vec2(0, 1), ruis::vec2(1, 0), ruis::vec2(1, 1), ruis::vec2(0, 1)} }; - auto tex_vbo = this->context.get().renderer.get().rendering_context.get().factory.get().make_vertex_buffer( + auto tex_vbo = this->context.get().renderer.get().rendering_context.get().factory().make_vertex_buffer( utki::make_span(cube_tex) ); @@ -49,7 +49,7 @@ cube_widget::cube_widget( } }; - auto cube_indices = this->context.get().renderer.get().rendering_context.get().factory.get().make_index_buffer( + auto cube_indices = this->context.get().renderer.get().rendering_context.get().factory().make_index_buffer( utki::make_span(indices) ); @@ -57,7 +57,7 @@ cube_widget::cube_widget( this->context.get() .renderer.get() .rendering_context.get() - .factory.get() + .factory() .make_vertex_array({pos_vbo, tex_vbo}, cube_indices, ruis::render::vertex_array::mode::triangles) .to_shared_ptr(); From 622b89adc83803fc718abc1c9789bd30040ba838 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 15:05:36 +0200 Subject: [PATCH 03/22] test app: use context.fac() accessor (renamed from factory()) --- tests/app/src/cube_widget.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/app/src/cube_widget.cpp b/tests/app/src/cube_widget.cpp index 2b3a7e6..3381320 100644 --- a/tests/app/src/cube_widget.cpp +++ b/tests/app/src/cube_widget.cpp @@ -23,7 +23,7 @@ cube_widget::cube_widget( ruis::vec3(-1, -1, 1), ruis::vec3(-1, -1, 1), ruis::vec3(1, -1, -1), ruis::vec3(1, -1, 1)} }; - auto pos_vbo = this->context.get().renderer.get().rendering_context.get().factory().make_vertex_buffer( + auto pos_vbo = this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer( utki::make_span(cube_pos) ); @@ -37,7 +37,7 @@ cube_widget::cube_widget( ruis::vec2(0, 0), ruis::vec2(1, 0), ruis::vec2(0, 1), ruis::vec2(1, 0), ruis::vec2(1, 1), ruis::vec2(0, 1)} }; - auto tex_vbo = this->context.get().renderer.get().rendering_context.get().factory().make_vertex_buffer( + auto tex_vbo = this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer( utki::make_span(cube_tex) ); @@ -49,7 +49,7 @@ cube_widget::cube_widget( } }; - auto cube_indices = this->context.get().renderer.get().rendering_context.get().factory().make_index_buffer( + auto cube_indices = this->context.get().renderer.get().rendering_context.get().fac().make_index_buffer( utki::make_span(indices) ); @@ -57,7 +57,7 @@ cube_widget::cube_widget( this->context.get() .renderer.get() .rendering_context.get() - .factory() + .fac() .make_vertex_array({pos_vbo, tex_vbo}, cube_indices, ruis::render::vertex_array::mode::triangles) .to_shared_ptr(); From 594877bb8054d02eeb92aef67df5a22d016841e8 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 15:07:52 +0200 Subject: [PATCH 04/22] format --- tests/app/src/cube_widget.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/app/src/cube_widget.cpp b/tests/app/src/cube_widget.cpp index 3381320..1a860be 100644 --- a/tests/app/src/cube_widget.cpp +++ b/tests/app/src/cube_widget.cpp @@ -23,9 +23,8 @@ cube_widget::cube_widget( ruis::vec3(-1, -1, 1), ruis::vec3(-1, -1, 1), ruis::vec3(1, -1, -1), ruis::vec3(1, -1, 1)} }; - auto pos_vbo = this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer( - utki::make_span(cube_pos) - ); + auto pos_vbo = + this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer(utki::make_span(cube_pos)); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) std::array cube_tex = { @@ -37,9 +36,8 @@ cube_widget::cube_widget( ruis::vec2(0, 0), ruis::vec2(1, 0), ruis::vec2(0, 1), ruis::vec2(1, 0), ruis::vec2(1, 1), ruis::vec2(0, 1)} }; - auto tex_vbo = this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer( - utki::make_span(cube_tex) - ); + auto tex_vbo = + this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer(utki::make_span(cube_tex)); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) std::array indices = { @@ -49,9 +47,8 @@ cube_widget::cube_widget( } }; - auto cube_indices = this->context.get().renderer.get().rendering_context.get().fac().make_index_buffer( - utki::make_span(indices) - ); + auto cube_indices = + this->context.get().renderer.get().rendering_context.get().fac().make_index_buffer(utki::make_span(indices)); this->cubeVAO = this->context.get() From ad91cc9cef69729d14fd65ce136f86056f77a95c Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 20 Nov 2026 14:12:09 +0200 Subject: [PATCH 05/22] Integrate the Vulkan renderer (#28) Wire up ruis-render-vulkan as a selectable renderer backend for the Linux Xorg and Wayland backends, built on the shared render factory introduced by the factory refactor (this is the Vulkan integration rebased from the pre-factory 'vulkan' branch onto 'ren_factory'). - Add Vulkan variants of each Linux ruisapp library (ruisapp-vulkan-xorg, ruisapp-vulkan-wayland), built only when the ruis-render-vulkan package is available so the rest of the package keeps working until it is released. - A single shared ruis::render::vulkan::factory (created from a headless surface provider, since Vulkan has no 'current context' and all GPU resources live on the VkDevice) is used for resource loading, shaders and common render objects. - Each window creates its own VkSurface (Xlib or Wayland) plus a per-window vulkan::context bound to that surface and the shared factory. - Add a Vulkan-native window implementation (vulkan_window.hxx) with proper WM_DELETE_WINDOW handling and an XFlush on destroy. - Harden window lifecycle: destroy_window() no longer asserts when the window is already gone, and mouse button events send a move first so the GUI knows the pointer position. Adapted to the factory API: contexts now take the shared factory, and common_shaders/common_render_objects come from the factory. --- build/cmake/CMakeLists.txt | 43 ++- .../glue/linux/wayland/application.cxx | 38 ++- .../glue/linux/wayland/application.hxx | 30 +- src/ruisapp/glue/linux/wayland/glue.cxx | 8 + src/ruisapp/glue/linux/wayland/window.hxx | 61 +++- .../glue/linux/wayland/xdg_surface.cxx | 9 + src/ruisapp/glue/linux/xorg/display.hxx | 6 + src/ruisapp/glue/linux/xorg/glue.cxx | 113 ++++++-- src/ruisapp/glue/linux/xorg/vulkan_window.hxx | 272 ++++++++++++++++++ src/ruisapp/glue/linux/xorg/window.hxx | 21 ++ tests/app/src/new_native_window.cpp | 87 ++++++ tests/app/src/new_native_window.hpp | 9 + tests/app/src/text_input_window.cpp | 47 +++ 13 files changed, 705 insertions(+), 39 deletions(-) create mode 100644 src/ruisapp/glue/linux/xorg/vulkan_window.hxx create mode 100644 tests/app/src/new_native_window.cpp create mode 100644 tests/app/src/new_native_window.hpp diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 73592d7..83ddf4f 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -170,6 +170,19 @@ if(LINUX) endif() endfunction() + # The Vulkan variants require the ruis-render-vulkan package. In a monorepo + # build it is a regular target (already added before this project), in a + # package-manager build (vcpkg, ...) it is only found when it is already + # published. Build the Vulkan variants only when the package is available, + # so the rest of the package keeps working until ruis-render-vulkan is released. + set(ruisapp_build_vulkan_variant True) + if(NOT TARGET ruis-render-vulkan::ruis-render-vulkan) + find_package(ruis-render-vulkan CONFIG QUIET) + if(NOT ruis-render-vulkan_FOUND) + set(ruisapp_build_vulkan_variant False) + endif() + endif() + ruisapp_declare_variant(opengl xorg INSTALL_HEADERS) ruisapp_declare_variant(opengles xorg) ruisapp_declare_variant(opengl wayland) @@ -177,6 +190,11 @@ if(LINUX) ruisapp_declare_variant(opengl sdl) ruisapp_declare_variant(opengles sdl) + if(ruisapp_build_vulkan_variant) + ruisapp_declare_variant(vulkan xorg) + ruisapp_declare_variant(vulkan wayland) + endif() + # Compatibility aliases: for now ruisapp-opengl / ruisapp-opengles refer to the xorg variants. # Note: the namespaced alias refers to the plain (non-namespaced) target, the same # convention that myci_declare_library() uses for the ${PROJECT_NAME}:: alias. @@ -275,14 +293,23 @@ endif() # === export === if(LINUX) + set(ruisapp_export_targets + ${name}-opengl-xorg + ${name}-opengles-xorg + ${name}-opengl-wayland + ${name}-opengles-wayland + ${name}-opengl-sdl + ${name}-opengles-sdl + ) + if(ruisapp_build_vulkan_variant) + list(APPEND ruisapp_export_targets + ${name}-vulkan-xorg + ${name}-vulkan-wayland + ) + endif() myci_export( TARGETS - ${name}-opengl-xorg - ${name}-opengles-xorg - ${name}-opengl-wayland - ${name}-opengles-wayland - ${name}-opengl-sdl - ${name}-opengles-sdl + ${ruisapp_export_targets} ) elseif(ruisapp_build_opengl_variant AND ruisapp_build_opengles_variant) myci_export( @@ -333,6 +360,10 @@ if(LINUX) ruisapp_declare_test_app(${renderer} ${backend}) endforeach() endforeach() + if(ruisapp_build_vulkan_variant) + ruisapp_declare_test_app(vulkan xorg) + ruisapp_declare_test_app(vulkan wayland) + endif() else() if(ruisapp_build_opengl_variant) myci_declare_application(${name}-opengl-test diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 7ee0f83..ba0a30f 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -25,12 +25,12 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL # include -# include #elif defined(RUISAPP_RENDER_OPENGLES) # include -# include +#elif defined(RUISAPP_RENDER_VULKAN) +# include #else # error "Unknown graphics API" #endif @@ -220,6 +220,22 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), +#elif defined(RUISAPP_RENDER_VULKAN) + [&](auto&& win_ref) -> utki::shared_ref { + auto wl_surf = win_ref.get().get_wl_surface(); + auto wl_disp = win_ref.get().get_wl_display(); + auto& vdev = *this->vulkan_device; + VkWaylandSurfaceCreateInfoKHR sci{}; + sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; + sci.display = wl_disp; + sci.surface = wl_surf; + VkSurfaceKHR vk_surf = VK_NULL_HANDLE; + vkCreateWaylandSurfaceKHR(vdev.get_instance(), &sci, nullptr, &vk_surf); + auto sp = utki::make_shared(vdev, vk_surf); + auto vulkan_factory_ref = + utki::dynamic_reference_cast(this->render_factory); + return utki::make_shared(win_ref, sp, vulkan_factory_ref); + }(ruis_native_window), #else # error "Unknown graphics API" #endif @@ -246,6 +262,19 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window std::move(ruis_native_window) ); +#ifdef RUISAPP_RENDER_VULKAN + { + // use a raw pointer to avoid a circular shared_ref that would + // prevent the app_window from being destroyed + auto* win_ptr = &ruisapp_window.get(); + ruisapp_window.get().ruis_native_window.get().present_func = [win_ptr]() { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") + auto& ctx = static_cast(win_ptr->gui.context.get().ren().ctx()); + ctx.present(); + }; + } +#endif + ruisapp_window.get().gui.set_viewport( // ruis::rect( 0, // @@ -265,6 +294,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window auto& ret_win = res.first->second.get(); +#ifndef RUISAPP_RENDER_VULKAN // EGL surface for the window is not created yet at this point. // In Wayland the EGL surface is created asynchronously. // Some further calls (e.g. set_vsync_enabled()) rely on the EGL surface to be created, @@ -280,6 +310,10 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window } utki::assert(ret_win.ruis_native_window.get().is_egl_surface_created(), SL); +#else + // For Vulkan, do a roundtrip to make sure the window is mapped before proceeding. + wl_display_roundtrip(this->display.get().wayland_display.display); +#endif return ret_win; } diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 2186b81..38d2d79 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -30,16 +30,6 @@ along with this program. If not, see . #include "../../../window.hpp" #include "../../unix_common.hxx" -#ifdef RUISAPP_RENDER_OPENGL -# include -# include -#elif defined(RUISAPP_RENDER_OPENGLES) -# include -# include -#else -# error "Unknown graphics API" -#endif - #include "window.hxx" namespace { @@ -137,6 +127,10 @@ public: // TODO: make windowless shared egl context const utki::shared_ref shared_gl_context_native_window; +#ifdef RUISAPP_RENDER_VULKAN + std::unique_ptr vulkan_device; + const utki::shared_ref vulkan_surface_provider; +#endif const utki::shared_ref render_factory; const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; @@ -178,8 +172,22 @@ public: nullptr // no shared gl context ) ), +#ifdef RUISAPP_RENDER_VULKAN + vulkan_device(ruis::render::vulkan::vulkan_device::make( + {std::string(VK_KHR_SURFACE_EXTENSION_NAME), std::string(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) + }, // instance extensions + {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // device extensions + )), + // Vulkan has no "current context" — no surface needed for resource loading + vulkan_surface_provider(utki::make_shared( + *this->vulkan_device, // + VK_NULL_HANDLE + )), +#endif render_factory( -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN + utki::make_shared(this->vulkan_surface_provider) +#elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(this->shared_gl_context_native_window) diff --git a/src/ruisapp/glue/linux/wayland/glue.cxx b/src/ruisapp/glue/linux/wayland/glue.cxx index f6dacbc..ef5b73d 100644 --- a/src/ruisapp/glue/linux/wayland/glue.cxx +++ b/src/ruisapp/glue/linux/wayland/glue.cxx @@ -32,11 +32,19 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_OPENGL # include # include +# include + +#elif defined(RUISAPP_RENDER_VULKAN) +# include +# include +# include +# include #elif defined(RUISAPP_RENDER_OPENGLES) # include # include # include +# include #else # error "Unknown graphics API" diff --git a/src/ruisapp/glue/linux/wayland/window.hxx b/src/ruisapp/glue/linux/wayland/window.hxx index fd353aa..c8b3c6b 100644 --- a/src/ruisapp/glue/linux/wayland/window.hxx +++ b/src/ruisapp/glue/linux/wayland/window.hxx @@ -22,9 +22,15 @@ along with this program. If not, see . #pragma once #include -#include // Wayland EGL MUST be included before EGL headers -#include "../../egl_utils.hxx" +#ifdef RUISAPP_RENDER_VULKAN +# include +# include +#else +# include // Wayland EGL MUST be included before EGL headers + +# include "../../egl_utils.hxx" +#endif #include "display.hxx" #include "wayland_egl_window.hxx" @@ -42,11 +48,13 @@ class native_window : public ruis::render::native_window xdg_toplevel_wrapper xdg_toplevel; wayland_egl_window_wrapper wayland_egl_window; +#ifndef RUISAPP_RENDER_VULKAN egl_config_wrapper egl_config; egl_context_wrapper egl_context; std::optional egl_dummy_surface; std::optional egl_surface; +#endif wayland_surface_wrapper::scale_and_dpi scale_and_dpi; @@ -88,6 +96,7 @@ public: this->wayland_surface, // window_params.dims ), +#ifndef RUISAPP_RENDER_VULKAN egl_config( this->display.get().egl_display, // gl_version, @@ -99,6 +108,7 @@ public: this->egl_config, shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT ), +#endif cur_window_dims(window_params.dims) { utki::log_debug([](auto& o) { @@ -123,6 +133,19 @@ public: return this->wayland_surface.surface; } + // get the raw wl_surface pointer for Vulkan surface creation + wl_surface* get_wl_surface() const noexcept + { + return this->wayland_surface.surface; + } + + // get the raw wl_display pointer for Vulkan surface creation + wl_display* get_wl_display() const noexcept + { + return this->display.get().wayland_display.display; + } + +#ifndef RUISAPP_RENDER_VULKAN void create_egl_surface() { utki::assert(!this->is_egl_surface_created(), [](auto& o) { @@ -140,16 +163,34 @@ public: { return this->egl_surface.has_value(); } +#endif void swap_frame_buffers() override { +#ifdef RUISAPP_RENDER_VULKAN + // For Vulkan, the present is handled by the Vulkan context + // through the present_func callback. + if (this->present_func) { + this->present_func(); + } +#else if (this->egl_surface.has_value()) { this->egl_surface.value().swap_frame_buffers(); } +#endif + } + + r4::vector2 get_dims() const noexcept override + { + return this->cur_window_dims * this->scale_and_dpi.scale; } void bind_rendering_context() override { +#ifdef RUISAPP_RENDER_VULKAN + // For Vulkan, there's no concept of binding a rendering context. + // The Vulkan context is thread-safe and doesn't need to be made current. +#else auto& egl_display = this->display.get().egl_display; if (this->egl_surface.has_value()) { @@ -186,11 +227,17 @@ public: ); } } +#endif } bool is_rendering_context_bound() const noexcept override { +#ifdef RUISAPP_RENDER_VULKAN + // For Vulkan, the context is always "bound" (thread-safe) + return true; +#else return eglGetCurrentContext() == this->egl_context.context; +#endif } void set_fullscreen_internal(bool enable) override @@ -214,6 +261,11 @@ private: bool mouse_cursor_visible = true; public: +#ifdef RUISAPP_RENDER_VULKAN + // callback for presenting the Vulkan frame + std::function present_func; +#endif + void update_mouse_cursor() { auto& wayland_pointer = this->display.get().wayland_seat.wayland_pointer; @@ -239,6 +291,10 @@ public: void set_vsync_enabled_internal(bool enabled) override { +#ifdef RUISAPP_RENDER_VULKAN + (void)enabled; + // VSync is handled by the Vulkan present mode +#else utki::assert( [this]() { return this->is_rendering_context_bound(); @@ -247,6 +303,7 @@ public: ); this->egl_context.set_vsync_enabled(enabled); +#endif } wl_callback* make_frame_callback() diff --git a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx index 71ee05c..0e70ad4 100644 --- a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx +++ b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx @@ -90,6 +90,7 @@ void xdg_surface_wrapper::xdg_surface_configure( auto& win = *window; auto& natwin = win.ruis_native_window.get(); +#ifndef RUISAPP_RENDER_VULKAN // The configure event can come several times for the same xdg_surface // because we do several surface commits during the initial configuration, // and each commit can trigger a configure event. @@ -107,6 +108,14 @@ void xdg_surface_wrapper::xdg_surface_configure( win.gui.context.get().ren().ctx().apply([&]() { natwin.swap_frame_buffers(); }); +#else + // For Vulkan, the initial surface commit (with a buffer) is done by presenting a frame. + // Without this the frame callback would never fire (the compositor only fires it after + // compositing a surface that has a buffer), so the window would never become visible. + win.gui.context.get().ren().ctx().apply([&]() { + natwin.swap_frame_buffers(); + }); +#endif // on some Wayland implementations just swapping EGL buffers is not enough and surface commit is needed self.wayland_surface.commit(); diff --git a/src/ruisapp/glue/linux/xorg/display.hxx b/src/ruisapp/glue/linux/xorg/display.hxx index 0033907..371df67 100644 --- a/src/ruisapp/glue/linux/xorg/display.hxx +++ b/src/ruisapp/glue/linux/xorg/display.hxx @@ -103,6 +103,12 @@ public: auto surf = gdk_surface_new_toplevel(disp); utki::assert(surf, SL); auto mon = gdk_display_get_monitor_at_surface(disp, surf); + if (!mon) { + // Fallback: use the first monitor + auto monitors = gdk_display_get_monitors(disp); + mon = GDK_MONITOR(g_list_model_get_item(monitors, 0)); + g_object_unref(monitors); + } utki::assert(mon, SL); int sf = gdk_monitor_get_scale_factor(mon); diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index f762a58..51efa79 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -35,6 +35,14 @@ along with this program. If not, see . # include # include +#elif defined(RUISAPP_RENDER_VULKAN) +# include +# include +# include +# include +# include +# include + #else # error "Unknown graphics API" #endif @@ -45,7 +53,11 @@ along with this program. If not, see . #include "cursor.hxx" #include "display.hxx" #include "key_code_map.hxx" -#include "window.hxx" +#ifdef RUISAPP_RENDER_VULKAN +# include "vulkan_window.hxx" +#else +# include "window.hxx" +#endif using namespace std::string_literals; using namespace std::string_view_literals; @@ -86,11 +98,14 @@ class application_glue : public utki::destructable const utki::shared_ref display = utki::make_shared(); private: +#ifdef RUISAPP_RENDER_VULKAN + std::unique_ptr vulkan_device; +#endif utki::version_duplet gl_version; utki::shared_ref shared_gl_context_native_window; - utki::shared_ref render_factory; - utki::shared_ref common_shaders; + const utki::shared_ref render_factory; + const utki::shared_ref common_shaders; utki::shared_ref common_render_objects; utki::shared_ref ruis_resource_loader; @@ -113,21 +128,33 @@ class application_glue : public utki::destructable std::vector> windows_to_destroy; application_glue(const utki::version_duplet& gl_version) : +#ifdef RUISAPP_RENDER_VULKAN + vulkan_device(ruis::render::vulkan::vulkan_device::make( + { + std::string(VK_KHR_SURFACE_EXTENSION_NAME), + std::string(VK_KHR_XLIB_SURFACE_EXTENSION_NAME) + }, // + {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // + )), +#endif gl_version(gl_version), shared_gl_context_native_window( // utki::make_shared( this->display, // this->gl_version, - ruisapp::window_parameters{ - .dims = {1, 1}, - .title = {}, - .fullscreen = false - }, + ruisapp::window_parameters{.dims = {1, 1}, .title = {}, .fullscreen = false, .taskbar = false}, nullptr ) ), + // one shared render factory per application render_factory( -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN + // Vulkan has no "current context" — all GPU resources live on the VkDevice and + // are inherently shareable. No surface is needed for the shared factory. + utki::make_shared( + utki::make_shared(*this->vulkan_device, VK_NULL_HANDLE) + ) +#elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(this->shared_gl_context_native_window) @@ -170,6 +197,21 @@ class application_glue : public utki::destructable &this->shared_gl_context_native_window.get() ); +#ifdef RUISAPP_RENDER_VULKAN + auto win_surface_provider = + utki::make_shared(*this->vulkan_device, [&]() { + VkXlibSurfaceCreateInfoKHR ci{}; + ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; + ci.dpy = this->display.get().xorg_display.display; + ci.window = ruis_native_window.get().get_id(); + VkSurfaceKHR surface = VK_NULL_HANDLE; + VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device->get_instance(), &ci, nullptr, &surface)); + return surface; + }()); + auto vulkan_factory_ref = + utki::dynamic_reference_cast(this->render_factory); +#endif + auto ruis_context = utki::make_shared(ruis::context::parameters{ .post_to_ui_thread_function = [this](std::function proc) { @@ -181,8 +223,12 @@ class application_glue : public utki::destructable utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), -#else -# error "Unknown graphics API" +#elif defined(RUISAPP_RENDER_VULKAN) + utki::make_shared( + ruis_native_window, // + win_surface_provider, // + vulkan_factory_ref + ), #endif this->common_shaders, this->common_render_objects @@ -215,6 +261,22 @@ class application_glue : public utki::destructable std::move(ruis_native_window) ); + // save a pointer to the native_window before the move + auto& nw = ruisapp_window.get().ruis_native_window.get(); + + { +#ifdef RUISAPP_RENDER_VULKAN + // use a raw pointer to avoid a circular shared_ref that would + // prevent the app_window from being destroyed + auto* win_ptr = &ruisapp_window.get(); + nw.present_func = [win_ptr]() { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") + auto& ctx = static_cast(win_ptr->gui.context.get().ren().ctx()); + ctx.present(); + }; +#endif + } + ruisapp_window.get().gui.set_viewport( // ruis::rect( 0, // @@ -226,7 +288,7 @@ class application_glue : public utki::destructable auto res = this->windows.insert( // std::make_pair( - ruisapp_window.get().ruis_native_window.get().get_id(), // + nw.get_id(), // std::move(ruisapp_window) ) ); @@ -237,8 +299,11 @@ class application_glue : public utki::destructable void destroy_window(app_window& w) { - auto i = this->windows.find(w.ruis_native_window.get().get_id()); - utki::assert(i != this->windows.end(), SL); + auto id = w.ruis_native_window.get().get_id(); + auto i = this->windows.find(id); + if (i == this->windows.end()) { + return; + } // Defer actual window object destruction until next main loop cycle, // for that put the window to the list of windows to destroy. @@ -487,10 +552,12 @@ int main(int argc, const char** argv) w.render(); break; case ConfigureNotify: - // squash all window resize events into one, for that store the new - // window dimensions and update the viewport later only once - w.new_win_dims.x() = ruis::real(event.xconfigure.width); - w.new_win_dims.y() = ruis::real(event.xconfigure.height); + { + // squash all window resize events into one, for that store the new + // window dimensions and update the viewport later only once + w.new_win_dims.x() = ruis::real(event.xconfigure.width); + w.new_win_dims.y() = ruis::real(event.xconfigure.height); + } break; case KeyPress: { @@ -554,6 +621,11 @@ int main(int argc, const char** argv) } break; case ButtonPress: + // send mouse move first so the GUI knows the pointer position + w.gui.send_mouse_move( + ruis::vec2(event.xbutton.x, event.xbutton.y), + 0 // pointer_id + ); w.gui.send_mouse_button( ruis::button_action::press, // ruis::vec2(event.xbutton.x, event.xbutton.y), @@ -562,6 +634,11 @@ int main(int argc, const char** argv) ); break; case ButtonRelease: + // send mouse move first so the GUI knows the pointer position + w.gui.send_mouse_move( + ruis::vec2(event.xbutton.x, event.xbutton.y), + 0 // pointer_id + ); w.gui.send_mouse_button( ruis::button_action::release, // ruis::vec2(event.xbutton.x, event.xbutton.y), diff --git a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx new file mode 100644 index 0000000..7ae6f05 --- /dev/null +++ b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx @@ -0,0 +1,272 @@ +/* +ruisapp - ruis GUI adaptation layer + +Copyright (C) 2016-2025 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +// A minimal X11 native window for the Vulkan renderer. +// Unlike the OpenGL window (window.hxx) this one does not create any GL context, +// it only creates a plain X11 window. The Vulkan surface is created by the glue +// from the X11 window and the shared Vulkan instance. + +#include +#include +#include + +#include +#include +#include +#include + +#include "cursor.hxx" +#include "xorg_display_wrapper.hxx" + +namespace { + +// new windows are cascaded to prevent overlap +constexpr unsigned cascade_max_columns = 15; +constexpr int cascade_step_px = 60; +constexpr int cascade_hint_step_px = 40; + +// the cascade offset (in pixels) for the window at the given index +inline int cascade_offset(unsigned index, int step_px) +{ + return int(index % cascade_max_columns) * step_px; +} + +class native_window : public ruis::render::native_window +{ +public: + // a callback that presents the frame; set by the glue to call the Vulkan + // context's present() + std::function present_func; + + // counter for cascading new windows to prevent overlap + static inline unsigned s_window_count = 0; + + native_window( + const utki::shared_ref& display, + const utki::version_duplet&, // the GL version is not used by Vulkan + const ruisapp::window_parameters& window_params, + const native_window* // the shared GL context is not used by Vulkan + ) : + display(display), + window([&]() { + auto screen = this->display.get().xorg_display.get_default_screen(); + auto visual = DefaultVisual(this->display.get().xorg_display.display, screen); + auto depth = DefaultDepth(this->display.get().xorg_display.display, screen); + + XSetWindowAttributes attr; + attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | ButtonMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask; + + auto dims = (this->display.get().scale_factor * window_params.dims.to()).to(); + + // cascade new windows to prevent overlap + int cascade = cascade_offset(s_window_count, cascade_step_px); + s_window_count++; + + auto w = XCreateWindow( + this->display.get().xorg_display.display, + this->display.get().xorg_display.get_root_window(screen), + cascade, // x + cascade, // y + dims.x(), + dims.y(), + 0, // border width + depth, + InputOutput, + visual, + CWEventMask, + &attr + ); + if (!w) { + throw std::runtime_error("Failed to create X11 window"); + } + this->current_dims = dims; + return w; + }()) + { + { // handle WM_DELETE_WINDOW to know when the window is closed + Atom a = XInternAtom(this->display.get().xorg_display.display, "WM_DELETE_WINDOW", True); + XSetWMProtocols(this->display.get().xorg_display.display, this->window, &a, 1); + } + + { + // set the window position hint so the WM respects the cascading position + auto* hints = XAllocSizeHints(); + hints->flags = USPosition; + hints->x = cascade_offset(s_window_count - 1, cascade_hint_step_px); + hints->y = cascade_offset(s_window_count - 1, cascade_hint_step_px); + XSetWMNormalHints(this->display.get().xorg_display.display, this->window, hints); + XFree(hints); + } + + if (!window_params.taskbar) { + Atom wm_state = XInternAtom(this->display.get().xorg_display.display, "_NET_WM_STATE", False); + Atom skip_taskbar = + XInternAtom(this->display.get().xorg_display.display, "_NET_WM_STATE_SKIP_TASKBAR", False); + static_assert(sizeof(skip_taskbar) >= 4, "Atom must be of at least 32-bit size"); + XChangeProperty( + this->display.get().xorg_display.display, + this->window, + wm_state, + XA_ATOM, + 4 * utki::byte_bits, + PropModeReplace, + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast, "using C API") + reinterpret_cast(&skip_taskbar), + 1 + ); + } + + XMapWindow(this->display.get().xorg_display.display, this->window); + + // force the cascading position (WM may ignore position hints) + { + const int cascade = cascade_offset(s_window_count - 1, cascade_step_px); + XMoveWindow(this->display.get().xorg_display.display, this->window, cascade, cascade); + } + + this->display.get().xorg_display.flush(); + + if (!window_params.title.empty()) { + XStoreName(this->display.get().xorg_display.display, this->window, window_params.title.c_str()); + } + } + + native_window(const native_window&) = delete; + native_window& operator=(const native_window&) = delete; + + native_window(native_window&&) = delete; + native_window& operator=(native_window&&) = delete; + + ~native_window() override + { + this->destroy_x11_window(); + } + + void destroy_x11_window() + { + if (!this->x11_window_destroyed) { + XDestroyWindow(this->display.get().xorg_display.display, this->window); + XFlush(this->display.get().xorg_display.display); + this->x11_window_destroyed = true; + } + } + + void swap_frame_buffers() override + { + if (this->present_func) { + this->present_func(); + } + } + + r4::vector2 get_dims() const noexcept override + { + return this->current_dims; + } + + void set_dims(const r4::vector2 dims) const noexcept override + { + XResizeWindow(this->display.get().xorg_display.display, this->window, dims.x(), dims.y()); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + const_cast(this)->current_dims = dims; + } + + // the X11 window id + static_assert(std::is_integral_v<::Window>, "xorg lib's Window type is unexpectedly not integral"); + using window_id_type = ::Window; + + window_id_type get_id() const noexcept + { + return this->window; + } + + std::u32string get_string(XKeyEvent& event) const + { + constexpr auto static_buf_size = 32; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array static_buf; + std::vector arr; + auto buf = utki::make_span(static_buf); + XComposeStatus status; + int size = XLookupString(&event, buf.data(), int(buf.size() - 1), nullptr, &status); + std::vector ret; + for (int i = 0; i < size; ++i) { + ret.push_back(uint32_t(buf[i])); + } + return {ret.begin(), ret.end()}; + } + + void set_fullscreen_internal(bool enable) override + { + Atom state_atom = XInternAtom( + this->display.get().xorg_display.display, // + "_NET_WM_STATE", + False + ); + Atom atom = XInternAtom( + this->display.get().xorg_display.display, // + "_NET_WM_STATE_FULLSCREEN", + False + ); + + XEvent event; + event.xclient.type = ClientMessage; + event.xclient.serial = 0; + event.xclient.send_event = True; + event.xclient.window = this->window; + event.xclient.message_type = state_atom; + event.xclient.format = utki::byte_bits * 4; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[0] = enable ? 1 : 0; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[1] = long(atom); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[2] = 0; + + XSendEvent( + this->display.get().xorg_display.display, // + this->display.get().xorg_display.get_root_window(this->display.get().xorg_display.get_default_screen()), // + False, // propagate + SubstructureRedirectMask | SubstructureNotifyMask, + &event + ); + this->display.get().xorg_display.flush(); + } + + void set_mouse_cursor_visible(bool visible) override + { + this->cursor_visible = visible; + auto& c = this->display.get().get_cursor(visible ? ruis::mouse_cursor::arrow : ruis::mouse_cursor::none); + XDefineCursor( + this->display.get().xorg_display.display, // + this->window, + c.cursor + ); + } + +private: + const utki::shared_ref display; + mutable r4::vector2 current_dims = {0, 0}; + bool x11_window_destroyed = false; + const ::Window window; + bool cursor_visible = true; +}; +} // namespace diff --git a/src/ruisapp/glue/linux/xorg/window.hxx b/src/ruisapp/glue/linux/xorg/window.hxx index 14e8848..2ce4195 100644 --- a/src/ruisapp/glue/linux/xorg/window.hxx +++ b/src/ruisapp/glue/linux/xorg/window.hxx @@ -382,6 +382,7 @@ class native_window : public ruis::render::native_window this->display.xorg_display.display, // this->window ); + XFlush(this->display.xorg_display.display); } } xorg_window; @@ -689,6 +690,25 @@ public: ~native_window() override = default; + void destroy_x11_window() + { + if (!this->x11_window_destroyed) { +#ifdef RUISAPP_RENDER_OPENGL + // unbind the GLX context from the current thread; + // do NOT destroy it — all windows share the same GLX context + glXMakeCurrent(this->display.get().xorg_display.display, GLX_NONE, nullptr); +#elif defined(RUISAPP_RENDER_OPENGLES) + eglMakeCurrent(this->display.get().egl_display.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); +#endif + XDestroyWindow( + this->display.get().xorg_display.display, // + this->xorg_window.window + ); + XFlush(this->display.get().xorg_display.display); + this->x11_window_destroyed = true; + } + } + void set_vsync_enabled_internal(bool enable) override { utki::assert( @@ -932,6 +952,7 @@ private: // NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive") cursor_wrapper* cur_cursor = nullptr; bool cursor_visible = true; + bool x11_window_destroyed = false; void apply_cursor(cursor_wrapper& c) { diff --git a/tests/app/src/new_native_window.cpp b/tests/app/src/new_native_window.cpp new file mode 100644 index 0000000..ba29f7a --- /dev/null +++ b/tests/app/src/new_native_window.cpp @@ -0,0 +1,87 @@ +#include "new_native_window.hpp" + +#include +#include +#include +#include +#include +#include + +#include "style.hpp" + +using namespace std::string_literals; +using namespace std::string_view_literals; + +utki::shared_ref make_new_native_window_root_widget( + const utki::shared_ref& c, // + ruisapp::window& win +) +{ + // clang-format off + auto button = m::push_button(c, + { + .layout_params{ + .dims{ruis::dim::fill, ruis::dim::fill}, + .weight = 1 + } + }, + { + m::text(c, {}, U"the button"s) + } + ); + // clang-format on + + button.get().click_handler = [](ruis::push_button& b) { + utki::logcat("the button clicked", '\n'); + }; + + // clang-format off + auto close_button = m::push_button(c, + {}, + { + m::text(c, {}, U"X"s) + } + ); + // clang-format on + + auto* win_ptr = &win; + close_button.get().click_handler = [win_ptr](auto& b) { + ruisapp::inst().destroy_window(*win_ptr); + }; + + // clang-format off + return m::column(c, + {}, + { + m::row(c, + { + .layout_params{ + .align = {ruis::align::front, ruis::align::center} + } + }, + { + m::text(c, {}, U"Native window"s), + std::move(close_button) + } + ), + m::text_field(c, + { + .layout_params{ + .dims{ruis::dim::max, ruis::dim::min} + }, + .widget{ + .id = "text_input"s + }, + .text_input{ + .text{ + .color = c.get().style().get_color_text() + } + } + }, + U"Hello Wrodl!!!"s + ), + std::move(button) + } + ); + // clang-format on +} diff --git a/tests/app/src/new_native_window.hpp b/tests/app/src/new_native_window.hpp new file mode 100644 index 0000000..32a4ff2 --- /dev/null +++ b/tests/app/src/new_native_window.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +utki::shared_ref make_new_native_window_root_widget( + const utki::shared_ref& c, // + ruisapp::window& win +); diff --git a/tests/app/src/text_input_window.cpp b/tests/app/src/text_input_window.cpp index b9cdb58..924da04 100644 --- a/tests/app/src/text_input_window.cpp +++ b/tests/app/src/text_input_window.cpp @@ -7,7 +7,9 @@ #include #include #include +#include +#include "new_native_window.hpp" #include "style.hpp" using namespace std::string_literals; @@ -55,6 +57,41 @@ utki::shared_ref make_text_input_window(const utki::shared_ref make_text_input_window(const utki::shared_ref Date: Fri, 20 Nov 2026 14:17:37 +0200 Subject: [PATCH 06/22] Refactor Xorg window hierarchy and fix Wayland EGL surface bind - Extract graphics-agnostic X11 window base class (xorg_window.hxx): window creation, input, cursor management, display handling. - GL leaf (window.hxx) now uses multiple inheritance: gl_visual_info (FBConfig + visual) + xorg_window (X11 base). - Vulkan leaf (vulkan_window.hxx) simplified to ~80 lines: inherits xorg_window, uses default visual, adds present_func. - Fix Wayland: create_egl_surface() now calls bind_rendering_context() so the EGL surface is bound even when the context is already current. --- src/ruisapp/glue/linux/wayland/window.hxx | 7 + src/ruisapp/glue/linux/xorg/vulkan_window.hxx | 239 +--- src/ruisapp/glue/linux/xorg/window.hxx | 1179 ++++++----------- src/ruisapp/glue/linux/xorg/xorg_window.hxx | 514 +++++++ 4 files changed, 949 insertions(+), 990 deletions(-) create mode 100644 src/ruisapp/glue/linux/xorg/xorg_window.hxx diff --git a/src/ruisapp/glue/linux/wayland/window.hxx b/src/ruisapp/glue/linux/wayland/window.hxx index c8b3c6b..3ea35a7 100644 --- a/src/ruisapp/glue/linux/wayland/window.hxx +++ b/src/ruisapp/glue/linux/wayland/window.hxx @@ -157,6 +157,13 @@ public: this->egl_config, this->wayland_egl_window.window ); + + // The rendering context was made current with EGL_NO_SURFACE in its constructor, + // before the surface existed. Now that the surface exists, re-bind the context to + // it, so that rendering (and the initial eglSwapBuffers) actually reaches the + // window's buffer. Without this, context::apply() would skip the bind (the context + // is already "current" on the context stack) and the swap would fail. + this->bind_rendering_context(); } bool is_egl_surface_created() const noexcept diff --git a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx index 7ae6f05..3a1e311 100644 --- a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx @@ -19,136 +19,50 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ -// A minimal X11 native window for the Vulkan renderer. -// Unlike the OpenGL window (window.hxx) this one does not create any GL context, -// it only creates a plain X11 window. The Vulkan surface is created by the glue +// The Vulkan native window. Inherits from xorg_window (the graphics-agnostic +// base) and adds the Vulkan-specific parts: the present callback. Unlike the +// OpenGL window it uses the default X11 visual (not an FBConfig-based one) and +// does not create any GL context. The Vulkan surface is created by the glue // from the X11 window and the shared Vulkan instance. #include -#include -#include -#include #include #include -#include -#include "cursor.hxx" -#include "xorg_display_wrapper.hxx" +#include "xorg_window.hxx" namespace { -// new windows are cascaded to prevent overlap -constexpr unsigned cascade_max_columns = 15; -constexpr int cascade_step_px = 60; -constexpr int cascade_hint_step_px = 40; - -// the cascade offset (in pixels) for the window at the given index -inline int cascade_offset(unsigned index, int step_px) -{ - return int(index % cascade_max_columns) * step_px; -} - -class native_window : public ruis::render::native_window +class native_window : public xorg_window { public: // a callback that presents the frame; set by the glue to call the Vulkan // context's present() std::function present_func; - // counter for cascading new windows to prevent overlap - static inline unsigned s_window_count = 0; - native_window( - const utki::shared_ref& display, + utki::shared_ref display, // const utki::version_duplet&, // the GL version is not used by Vulkan const ruisapp::window_parameters& window_params, - const native_window* // the shared GL context is not used by Vulkan + const native_window* shared ) : - display(display), - window([&]() { - auto screen = this->display.get().xorg_display.get_default_screen(); - auto visual = DefaultVisual(this->display.get().xorg_display.display, screen); - auto depth = DefaultDepth(this->display.get().xorg_display.display, screen); - - XSetWindowAttributes attr; - attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - PointerMotionMask | ButtonMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask; - - auto dims = (this->display.get().scale_factor * window_params.dims.to()).to(); - - // cascade new windows to prevent overlap - int cascade = cascade_offset(s_window_count, cascade_step_px); - s_window_count++; - - auto w = XCreateWindow( - this->display.get().xorg_display.display, - this->display.get().xorg_display.get_root_window(screen), - cascade, // x - cascade, // y - dims.x(), - dims.y(), - 0, // border width - depth, - InputOutput, - visual, - CWEventMask, - &attr - ); - if (!w) { - throw std::runtime_error("Failed to create X11 window"); + xorg_window( + display, // + window_params, + shared, + xorg_visual{ + .visual = DefaultVisual( + display.get().xorg_display.display, // + display.get().xorg_display.get_default_screen() + ), + .depth = DefaultDepth( + display.get().xorg_display.display, // + display.get().xorg_display.get_default_screen() + ) } - this->current_dims = dims; - return w; - }()) - { - { // handle WM_DELETE_WINDOW to know when the window is closed - Atom a = XInternAtom(this->display.get().xorg_display.display, "WM_DELETE_WINDOW", True); - XSetWMProtocols(this->display.get().xorg_display.display, this->window, &a, 1); - } - - { - // set the window position hint so the WM respects the cascading position - auto* hints = XAllocSizeHints(); - hints->flags = USPosition; - hints->x = cascade_offset(s_window_count - 1, cascade_hint_step_px); - hints->y = cascade_offset(s_window_count - 1, cascade_hint_step_px); - XSetWMNormalHints(this->display.get().xorg_display.display, this->window, hints); - XFree(hints); - } - - if (!window_params.taskbar) { - Atom wm_state = XInternAtom(this->display.get().xorg_display.display, "_NET_WM_STATE", False); - Atom skip_taskbar = - XInternAtom(this->display.get().xorg_display.display, "_NET_WM_STATE_SKIP_TASKBAR", False); - static_assert(sizeof(skip_taskbar) >= 4, "Atom must be of at least 32-bit size"); - XChangeProperty( - this->display.get().xorg_display.display, - this->window, - wm_state, - XA_ATOM, - 4 * utki::byte_bits, - PropModeReplace, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast, "using C API") - reinterpret_cast(&skip_taskbar), - 1 - ); - } - - XMapWindow(this->display.get().xorg_display.display, this->window); - - // force the cascading position (WM may ignore position hints) - { - const int cascade = cascade_offset(s_window_count - 1, cascade_step_px); - XMoveWindow(this->display.get().xorg_display.display, this->window, cascade, cascade); - } - - this->display.get().xorg_display.flush(); - - if (!window_params.title.empty()) { - XStoreName(this->display.get().xorg_display.display, this->window, window_params.title.c_str()); - } - } + ) + {} native_window(const native_window&) = delete; native_window& operator=(const native_window&) = delete; @@ -156,19 +70,7 @@ public: native_window(native_window&&) = delete; native_window& operator=(native_window&&) = delete; - ~native_window() override - { - this->destroy_x11_window(); - } - - void destroy_x11_window() - { - if (!this->x11_window_destroyed) { - XDestroyWindow(this->display.get().xorg_display.display, this->window); - XFlush(this->display.get().xorg_display.display); - this->x11_window_destroyed = true; - } - } + ~native_window() override = default; void swap_frame_buffers() override { @@ -176,97 +78,6 @@ public: this->present_func(); } } - - r4::vector2 get_dims() const noexcept override - { - return this->current_dims; - } - - void set_dims(const r4::vector2 dims) const noexcept override - { - XResizeWindow(this->display.get().xorg_display.display, this->window, dims.x(), dims.y()); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) - const_cast(this)->current_dims = dims; - } - - // the X11 window id - static_assert(std::is_integral_v<::Window>, "xorg lib's Window type is unexpectedly not integral"); - using window_id_type = ::Window; - - window_id_type get_id() const noexcept - { - return this->window; - } - - std::u32string get_string(XKeyEvent& event) const - { - constexpr auto static_buf_size = 32; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array static_buf; - std::vector arr; - auto buf = utki::make_span(static_buf); - XComposeStatus status; - int size = XLookupString(&event, buf.data(), int(buf.size() - 1), nullptr, &status); - std::vector ret; - for (int i = 0; i < size; ++i) { - ret.push_back(uint32_t(buf[i])); - } - return {ret.begin(), ret.end()}; - } - - void set_fullscreen_internal(bool enable) override - { - Atom state_atom = XInternAtom( - this->display.get().xorg_display.display, // - "_NET_WM_STATE", - False - ); - Atom atom = XInternAtom( - this->display.get().xorg_display.display, // - "_NET_WM_STATE_FULLSCREEN", - False - ); - - XEvent event; - event.xclient.type = ClientMessage; - event.xclient.serial = 0; - event.xclient.send_event = True; - event.xclient.window = this->window; - event.xclient.message_type = state_atom; - event.xclient.format = utki::byte_bits * 4; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[0] = enable ? 1 : 0; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[1] = long(atom); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[2] = 0; - - XSendEvent( - this->display.get().xorg_display.display, // - this->display.get().xorg_display.get_root_window(this->display.get().xorg_display.get_default_screen()), // - False, // propagate - SubstructureRedirectMask | SubstructureNotifyMask, - &event - ); - this->display.get().xorg_display.flush(); - } - - void set_mouse_cursor_visible(bool visible) override - { - this->cursor_visible = visible; - auto& c = this->display.get().get_cursor(visible ? ruis::mouse_cursor::arrow : ruis::mouse_cursor::none); - XDefineCursor( - this->display.get().xorg_display.display, // - this->window, - c.cursor - ); - } - -private: - const utki::shared_ref display; - mutable r4::vector2 current_dims = {0, 0}; - bool x11_window_destroyed = false; - const ::Window window; - bool cursor_visible = true; }; + } // namespace diff --git a/src/ruisapp/glue/linux/xorg/window.hxx b/src/ruisapp/glue/linux/xorg/window.hxx index 2ce4195..0627c5a 100644 --- a/src/ruisapp/glue/linux/xorg/window.hxx +++ b/src/ruisapp/glue/linux/xorg/window.hxx @@ -19,659 +19,457 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ -#pragma once +// The OpenGL/OpenGL ES native window. Inherits from xorg_window (the +// graphics-agnostic base) and adds the GL-specific parts: FBConfig/visual +// selection, GL context creation, swap_frame_buffers, bind_rendering_context, +// etc. +// +// The gl_visual_info base class holds the computed FBConfig + X11 visual. It is +// inherited before xorg_window so that the base class can use the visual when +// creating the X11 window (base classes are initialized before members, so the +// visual cannot be a member of the derived class). #include #include -#ifdef RUISAPP_RENDER_OPENGL +#if defined(RUISAPP_RENDER_OPENGL) # include - #elif defined(RUISAPP_RENDER_OPENGLES) # include # include "../../egl_utils.hxx" - -#else -# error "Unknown graphics API" #endif -#include "display.hxx" +#include "xorg_window.hxx" namespace { -class native_window : public ruis::render::native_window +// Holds the computed FBConfig + X11 visual. This is a base class of native_window +// (inherited before xorg_window) so that the visual is available when the +// xorg_window base creates the X11 window. +class gl_visual_info { - utki::shared_ref display; - -#ifdef RUISAPP_RENDER_OPENGL - struct fb_config_wrapper { - GLXFBConfig config; - - fb_config_wrapper( - display_wrapper& display, // - const utki::version_duplet&, - const ruisapp::window_parameters& window_params - ) : - config([&]() { - GLXFBConfig best_fb_config = nullptr; - std::vector visual_attribs; - visual_attribs.push_back(GLX_X_RENDERABLE); - visual_attribs.push_back(True); - visual_attribs.push_back(GLX_X_VISUAL_TYPE); - visual_attribs.push_back(GLX_TRUE_COLOR); - visual_attribs.push_back(GLX_DRAWABLE_TYPE); - visual_attribs.push_back(GLX_WINDOW_BIT); - visual_attribs.push_back(GLX_RENDER_TYPE); - visual_attribs.push_back(GLX_RGBA_BIT); - visual_attribs.push_back(GLX_DOUBLEBUFFER); - visual_attribs.push_back(True); - visual_attribs.push_back(GLX_RED_SIZE); - visual_attribs.push_back(utki::byte_bits); - visual_attribs.push_back(GLX_GREEN_SIZE); - visual_attribs.push_back(utki::byte_bits); - visual_attribs.push_back(GLX_BLUE_SIZE); - visual_attribs.push_back(utki::byte_bits); - visual_attribs.push_back(GLX_ALPHA_SIZE); +public: +#if defined(RUISAPP_RENDER_OPENGL) + GLXFBConfig fb_config; + XVisualInfo* const visual_info; + xorg_visual xvisual; + + gl_visual_info( + display_wrapper& display, // + const utki::version_duplet&, + const ruisapp::window_parameters& window_params + ) : + fb_config([&]() { + GLXFBConfig best_fb_config = nullptr; + std::vector visual_attribs; + visual_attribs.push_back(GLX_X_RENDERABLE); + visual_attribs.push_back(True); + visual_attribs.push_back(GLX_X_VISUAL_TYPE); + visual_attribs.push_back(GLX_TRUE_COLOR); + visual_attribs.push_back(GLX_DRAWABLE_TYPE); + visual_attribs.push_back(GLX_WINDOW_BIT); + visual_attribs.push_back(GLX_RENDER_TYPE); + visual_attribs.push_back(GLX_RGBA_BIT); + visual_attribs.push_back(GLX_DOUBLEBUFFER); + visual_attribs.push_back(True); + visual_attribs.push_back(GLX_RED_SIZE); + visual_attribs.push_back(utki::byte_bits); + visual_attribs.push_back(GLX_GREEN_SIZE); + visual_attribs.push_back(utki::byte_bits); + visual_attribs.push_back(GLX_BLUE_SIZE); + visual_attribs.push_back(utki::byte_bits); + visual_attribs.push_back(GLX_ALPHA_SIZE); + visual_attribs.push_back(utki::byte_bits); + + if (window_params.buffers.get(ruisapp::buffer::depth)) { + visual_attribs.push_back(GLX_DEPTH_SIZE); + visual_attribs.push_back(utki::byte_bits * 3); // 24 bits per pixel for depth buffer + } + if (window_params.buffers.get(ruisapp::buffer::stencil)) { + visual_attribs.push_back(GLX_STENCIL_SIZE); visual_attribs.push_back(utki::byte_bits); + } - if (window_params.buffers.get(ruisapp::buffer::depth)) { - visual_attribs.push_back(GLX_DEPTH_SIZE); - visual_attribs.push_back(utki::byte_bits * 3); // 24 bits per pixel for depth buffer - } - if (window_params.buffers.get(ruisapp::buffer::stencil)) { - visual_attribs.push_back(GLX_STENCIL_SIZE); - visual_attribs.push_back(utki::byte_bits); - } - - visual_attribs.push_back(None); - - utki::span fb_configs = [&]() { - int fbcount = 0; - GLXFBConfig* fb_configs = glXChooseFBConfig( - display.xorg_display.display, - display.xorg_display.get_default_screen(), - visual_attribs.data(), - &fbcount - ); - if (!fb_configs) { - throw std::runtime_error("glXChooseFBConfig() returned empty list"); - } - return utki::make_span(fb_configs, fbcount); - }(); - - utki::scope_exit scope_exit_fbc([&fb_configs]() { - // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion) - XFree(fb_configs.data()); - }); - - GLXFBConfig worst_fb_config = nullptr; - int best_num_samp = -1; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) - int worst_num_samp = 999; - - for (auto fb_config : fb_configs) { - XVisualInfo* vi = glXGetVisualFromFBConfig( - display.xorg_display.display, // - fb_config - ); - if (!vi) { - continue; - } - - // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - int samp_buf; - glXGetFBConfigAttrib( - display.xorg_display.display, // - fb_config, - GLX_SAMPLE_BUFFERS, - &samp_buf - ); - - // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - int samples; - glXGetFBConfigAttrib( - display.xorg_display.display, // - fb_config, - GLX_SAMPLES, - &samples - ); - - if (!best_fb_config || (samp_buf && samples > best_num_samp)) { - best_fb_config = fb_config; - best_num_samp = samples; - } - if (!worst_fb_config || !samp_buf || samples < worst_num_samp) { - worst_fb_config = fb_config; - worst_num_samp = samples; - } + visual_attribs.push_back(None); - XFree(vi); + utki::span fb_configs = [&]() { + int fbcount = 0; + GLXFBConfig* fb_configs = glXChooseFBConfig( + display.xorg_display.display, + display.xorg_display.get_default_screen(), + visual_attribs.data(), + &fbcount + ); + if (!fb_configs) { + throw std::runtime_error("glXChooseFBConfig() returned empty list"); } + return utki::make_span(fb_configs, fbcount); + }(); - utki::assert(best_fb_config, SL); - return best_fb_config; - }()) - {} - - fb_config_wrapper(const fb_config_wrapper&) = delete; - fb_config_wrapper& operator=(const fb_config_wrapper&) = delete; - - fb_config_wrapper(fb_config_wrapper&&) = delete; - fb_config_wrapper& operator=(fb_config_wrapper&&) = delete; - - // no need to free fb config - ~fb_config_wrapper() = default; - } fb_config; -#elif defined(RUISAPP_RENDER_OPENGLES) - using fb_config_wrapper = egl_config_wrapper; - fb_config_wrapper fb_config; -#endif + utki::scope_exit scope_exit_fbc([&fb_configs]() { + // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion) + XFree(fb_configs.data()); + }); - struct xorg_visual_info_wrapper { - XVisualInfo* const visual_info; + GLXFBConfig worst_fb_config = nullptr; + int best_num_samp = -1; + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) + int worst_num_samp = 999; - xorg_visual_info_wrapper( - display_wrapper& display, // - fb_config_wrapper& fb_config - ) : - visual_info([&]() { -#ifdef RUISAPP_RENDER_OPENGL - auto visual_info = glXGetVisualFromFBConfig( + for (auto fb_config : fb_configs) { + XVisualInfo* vi = glXGetVisualFromFBConfig( display.xorg_display.display, // - fb_config.config + fb_config ); - if (!visual_info) { - throw std::runtime_error("glXGetVisualFromFBConfig() failed"); - } - return visual_info; -#elif defined(RUISAPP_RENDER_OPENGLES) - EGLint vid = 0; - - if (!eglGetConfigAttrib( - display.egl_display.display, // - fb_config.config, - EGL_NATIVE_VISUAL_ID, - &vid - )) - { - throw std::runtime_error("eglGetConfigAttrib() failed"); + if (!vi) { + continue; } - int num_visuals = 0; - XVisualInfo vis_template; - vis_template.visualid = vid; - auto visual_info = XGetVisualInfo( + // NOLINTNEXTLINE(cppcoreguidelines-init-variables) + int samp_buf; + glXGetFBConfigAttrib( display.xorg_display.display, // - VisualIDMask, - &vis_template, - &num_visuals + fb_config, + GLX_SAMPLE_BUFFERS, + &samp_buf ); - if (!visual_info) { - throw std::runtime_error("XGetVisualInfo() failed"); - } - return visual_info; -#else -# error "Unknown graphics API" -#endif - }()) - {} - xorg_visual_info_wrapper(const xorg_visual_info_wrapper&) = delete; - xorg_visual_info_wrapper& operator=(const xorg_visual_info_wrapper&) = delete; - - xorg_visual_info_wrapper(xorg_visual_info_wrapper&&) = delete; - xorg_visual_info_wrapper& operator=(xorg_visual_info_wrapper&&) = delete; - - ~xorg_visual_info_wrapper() - { - XFree(this->visual_info); - } - } xorg_visual_info; - - struct xorg_color_map_wrapper { - display_wrapper& display; - - const Colormap color_map; - - xorg_color_map_wrapper( - display_wrapper& display, // - xorg_visual_info_wrapper& visual_info - ) : - display(display), - color_map([&]() { - auto cm = XCreateColormap( - this->display.xorg_display.display, - this->display.xorg_display.get_root_window(visual_info.visual_info->screen), - visual_info.visual_info->visual, - AllocNone + // NOLINTNEXTLINE(cppcoreguidelines-init-variables) + int samples; + glXGetFBConfigAttrib( + display.xorg_display.display, // + fb_config, + GLX_SAMPLES, + &samples ); - if (cm == None) { - // TODO: use XSetErrorHandler() to get error code - throw std::runtime_error("XCreateColormap(): failed"); - } - return cm; - }()) - {} - xorg_color_map_wrapper(const xorg_color_map_wrapper&) = delete; - xorg_color_map_wrapper& operator=(const xorg_color_map_wrapper&) = delete; + if (!best_fb_config || (samp_buf && samples > best_num_samp)) { + best_fb_config = fb_config; + best_num_samp = samples; + } + if (!worst_fb_config || !samp_buf || samples < worst_num_samp) { + worst_fb_config = fb_config; + worst_num_samp = samples; + } - xorg_color_map_wrapper(xorg_color_map_wrapper&&) = delete; - xorg_color_map_wrapper& operator=(xorg_color_map_wrapper&&) = delete; + XFree(vi); + } - ~xorg_color_map_wrapper() - { - XFreeColormap( + utki::assert(best_fb_config, SL); + return best_fb_config; + }()), + visual_info([&]() { + auto visual_info = glXGetVisualFromFBConfig( display.xorg_display.display, // - this->color_map + this->fb_config ); - } - } xorg_color_map; - - struct xorg_window_wrapper { - display_wrapper& display; - - const ::Window window; - - xorg_window_wrapper( - display_wrapper& display, // - const ruisapp::window_parameters& window_params, - xorg_color_map_wrapper& color_map, - xorg_visual_info_wrapper& visual_info, - bool visible - ) : - display(display), - window([&]() { - XSetWindowAttributes attr; - attr.colormap = color_map.color_map; - attr.border_pixel = 0; - attr.background_pixmap = None; - attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - PointerMotionMask | ButtonMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask; - unsigned long fields = CWBorderPixel | CWColormap | CWEventMask | CWBackPixmap; - - auto dims = (this->display.scale_factor * window_params.dims.to()).to(); - - auto w = XCreateWindow( - this->display.xorg_display.display, - this->display.xorg_display.get_root_window(visual_info.visual_info->screen), // parent window - 0, // x position - 0, // y position - dims.x(), // width - dims.y(), // height - 0, // border width - visual_info.visual_info->depth, // window's depth - InputOutput, // window's class - visual_info.visual_info->visual, - fields, // defined attributes - &attr - ); - if (!w) { - throw std::runtime_error("Failed to create window"); - } - return w; - }()) - { - { // we want to handle WM_DELETE_WINDOW event to know when window is closed - Atom a = XInternAtom( - this->display.xorg_display.display, // - "WM_DELETE_WINDOW", - True - ); - XSetWMProtocols( - this->display.xorg_display.display, // - this->window, - &a, - 1 - ); + if (!visual_info) { + throw std::runtime_error("glXGetVisualFromFBConfig() failed"); } + return visual_info; + }()), + xvisual([this]() { + return xorg_visual{ + .visual = this->visual_info->visual, // + .depth = unsigned(this->visual_info->depth) + }; + }()) + {} + + gl_visual_info(const gl_visual_info&) = delete; + gl_visual_info& operator=(const gl_visual_info&) = delete; + + gl_visual_info(gl_visual_info&&) = delete; + gl_visual_info& operator=(gl_visual_info&&) = delete; + + ~gl_visual_info() + { + XFree(this->visual_info); + } - if (!window_params.taskbar) { - Atom wm_state = XInternAtom( - this->display.xorg_display.display, // - "_NET_WM_STATE", - False - ); - Atom skip_taskbar = XInternAtom( - this->display.xorg_display.display, // - "_NET_WM_STATE_SKIP_TASKBAR", - False - ); +#elif defined(RUISAPP_RENDER_OPENGLES) + const egl_config_wrapper egl_config; + XVisualInfo* const visual_info; + xorg_visual xvisual; - static_assert(sizeof(skip_taskbar) >= 4, "Atom must be of at least 32-bit size"); - XChangeProperty( - this->display.xorg_display.display, // - this->window, - wm_state, - XA_ATOM, // type of data - 4 * utki::byte_bits, // data is a list of 32-bit values - PropModeReplace, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast, "using C API") - reinterpret_cast(&skip_taskbar), // data - 1 // only one value in the data - ); + gl_visual_info( + display_wrapper& display, // + const utki::version_duplet& gl_version, + const ruisapp::window_parameters& window_params + ) : + egl_config( + display.egl_display, // + gl_version, + window_params + ), + visual_info([&]() { + EGLint vid = 0; + + if (!eglGetConfigAttrib( + display.egl_display.display, // + this->egl_config.config, + EGL_NATIVE_VISUAL_ID, + &vid + )) + { + throw std::runtime_error("eglGetConfigAttrib() failed"); } - if (visible) { - XMapWindow( - this->display.xorg_display.display, // - this->window - ); + int num_visuals = 0; + XVisualInfo vis_template; + vis_template.visualid = vid; + auto visual_info = XGetVisualInfo( + display.xorg_display.display, // + VisualIDMask, + &vis_template, + &num_visuals + ); + if (!visual_info) { + throw std::runtime_error("XGetVisualInfo() failed"); } + return visual_info; + }()), + xvisual([this]() { + return xorg_visual{ + .visual = this->visual_info->visual, // + .depth = unsigned(this->visual_info->depth) + }; + }()) + {} + + gl_visual_info(const gl_visual_info&) = delete; + gl_visual_info& operator=(const gl_visual_info&) = delete; + + gl_visual_info(gl_visual_info&&) = delete; + gl_visual_info& operator=(gl_visual_info&&) = delete; + + ~gl_visual_info() + { + XFree(this->visual_info); + } +#endif +}; - this->display.xorg_display.flush(); +#if defined(RUISAPP_RENDER_OPENGL) +// Creates the GLX context +struct glx_context_wrapper { + display_wrapper& display; - if (!window_params.title.empty()) { - XStoreName( - this->display.xorg_display.display, // - this->window, - window_params.title.c_str() - ); - } - } + enum class glx_extension { + glx_arb_create_context, + glx_ext_swap_control, + glx_mesa_swap_control, - xorg_window_wrapper(const xorg_window_wrapper&) = delete; - xorg_window_wrapper& operator=(const xorg_window_wrapper&) = delete; + enum_size + }; - xorg_window_wrapper(xorg_window_wrapper&&) = delete; - xorg_window_wrapper& operator=(xorg_window_wrapper&&) = delete; + // glXGetProcAddressARB() will return non-null pointer even if extension is + // not supported, so we need to explicitly check for supported extensions. + // SOURCE: + // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ + const utki::flags supported_extensions; - ~xorg_window_wrapper() - { - XDestroyWindow( + const GLXContext context; + + glx_context_wrapper( + display_wrapper& display, // + XVisualInfo* visual_info, + const utki::version_duplet& gl_version, + GLXFBConfig fb_config, + GLXContext shared_glx_context + ) : + display(display), + supported_extensions([&]() { + utki::flags supported = false; + + auto glx_extensions_string = std::string_view(glXQueryExtensionsString( this->display.xorg_display.display, // - this->window - ); - XFlush(this->display.xorg_display.display); - } - } xorg_window; + visual_info->screen + )); + utki::log_debug([&](auto& o) { + o << "glx_extensions_string = " << glx_extensions_string << std::endl; + }); -#ifdef RUISAPP_RENDER_OPENGL - struct glx_context_wrapper { - display_wrapper& display; - - enum class glx_extension { - glx_arb_create_context, - glx_ext_swap_control, - glx_mesa_swap_control, - - enum_size - }; - - // glXGetProcAddressARB() will return non-null pointer even if extension is - // not supported, so we need to explicitly check for supported extensions. - // SOURCE: - // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ - const utki::flags supported_extensions; - - const GLXContext context; - - glx_context_wrapper( - display_wrapper& display, // - const xorg_visual_info_wrapper& visual_info, - const utki::version_duplet& gl_version, - const fb_config_wrapper& fb_config, - native_window* shared_gl_context_native_window - ) : - display(display), - supported_extensions([&]() { - utki::flags supported = false; - - auto glx_extensions_string = std::string_view(glXQueryExtensionsString( - this->display.xorg_display.display, // - visual_info.visual_info->screen - )); - utki::log_debug([&](auto& o) { - o << "glx_extensions_string = " << glx_extensions_string << std::endl; - }); + auto glx_extensions = utki::split(glx_extensions_string); - auto glx_extensions = utki::split(glx_extensions_string); + using namespace std::string_view_literals; - using namespace std::string_view_literals; + if (std::ranges::find( + glx_extensions, // + "GLX_ARB_create_context"sv + ) != glx_extensions.end()) + { + supported.set(glx_extension::glx_arb_create_context); + } - if (std::ranges::find( - glx_extensions, // - "GLX_ARB_create_context"sv - ) != glx_extensions.end()) - { - supported.set(glx_extension::glx_arb_create_context); - } + if (std::ranges::find( + glx_extensions, // + "GLX_EXT_swap_control"sv + ) != glx_extensions.end()) + { + supported.set(glx_extension::glx_ext_swap_control); + } - if (std::ranges::find( - glx_extensions, // - "GLX_EXT_swap_control"sv - ) != glx_extensions.end()) - { - supported.set(glx_extension::glx_ext_swap_control); - } + if (std::ranges::find( // + glx_extensions, + "GLX_MESA_swap_control"sv + ) != glx_extensions.end()) + { + supported.set(glx_extension::glx_mesa_swap_control); + } - if (std::ranges::find( // - glx_extensions, - "GLX_MESA_swap_control"sv - ) != glx_extensions.end()) - { - supported.set(glx_extension::glx_mesa_swap_control); - } + return supported; + }()), + context([&]() { + GLXContext gl_context = nullptr; - return supported; - }()), - context([&]() { - GLXContext gl_context = nullptr; + if (this->supported_extensions.get(glx_extension::glx_arb_create_context)) { + // GLX_ARB_create_context is supported - GLXContext shared_glx_context = [&]() -> GLXContext { - if (shared_gl_context_native_window) { - return shared_gl_context_native_window->glx_context.context; - } - return nullptr; - }(); + // NOTE: glXGetProcAddressARB() is guaranteed to be present in all GLX + // versions. + // glXGetProcAddress() is not guaranteed. + // SOURCE: + // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ - if (this->supported_extensions.get(glx_extension::glx_arb_create_context)) { - // GLX_ARB_create_context is supported + auto glx_create_context_attribs_arb = PFNGLXCREATECONTEXTATTRIBSARBPROC(glXGetProcAddressARB( + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + reinterpret_cast("glXCreateContextAttribsARB") + )); - // NOTE: glXGetProcAddressARB() is guaranteed to be present in all GLX - // versions. - // glXGetProcAddress() is not guaranteed. - // SOURCE: + if (!glx_create_context_attribs_arb) { + // this should not happen since we checked extension presence, and + // anyway, glXGetProcAddressARB() never returns nullptr according to // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ + // so, this check for null is just in case future version of GLX may + // return null + throw std::runtime_error("glXCreateContextAttribsARB() not found"); + } - auto glx_create_context_attribs_arb = PFNGLXCREATECONTEXTATTRIBSARBPROC(glXGetProcAddressARB( - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast("glXCreateContextAttribsARB") - )); - - if (!glx_create_context_attribs_arb) { - // this should not happen since we checked extension presence, and - // anyway, glXGetProcAddressARB() never returns nullptr according to - // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ - // so, this check for null is just in case future version of GLX may - // return null - throw std::runtime_error("glXCreateContextAttribsARB() not found"); + auto graphics_api_version = [&ver = gl_version]() { + if (ver.to_uint32_t() == 0) { + // default OpenGL version is 2.0 + return utki::version_duplet{ + .major = 2, // + .minor = 0 + }; } + if (ver.major < 2) { + throw std::invalid_argument( + utki::cat("minimal supported OpenGL version is 2.0, requested: ", ver) + ); + } + return ver; + }(); - auto graphics_api_version = [&ver = gl_version]() { - if (ver.to_uint32_t() == 0) { - // default OpenGL version is 2.0 - return utki::version_duplet{ - .major = 2, // - .minor = 0 - }; - } - if (ver.major < 2) { - throw std::invalid_argument( - utki::cat("minimal supported OpenGL version is 2.0, requested: ", ver) - ); - } - return ver; - }(); - - static const std::array context_attribs = { - GLX_CONTEXT_MAJOR_VERSION_ARB, - graphics_api_version.major, - GLX_CONTEXT_MINOR_VERSION_ARB, - graphics_api_version.minor, - GLX_CONTEXT_PROFILE_MASK_ARB, - // we don't need compatibility context - GLX_CONTEXT_CORE_PROFILE_BIT_ARB, - None - }; - - gl_context = glx_create_context_attribs_arb( - this->display.xorg_display.display, // - fb_config.config, - shared_glx_context, - GL_TRUE, - context_attribs.data() - ); - } else { - // GLX_ARB_create_context is not supported - gl_context = glXCreateContext( - this->display.xorg_display.display, // - visual_info.visual_info, - shared_glx_context, - GL_TRUE - ); - } - - // sync to ensure any errors generated are processed - XSync( + static const std::array context_attribs = { + GLX_CONTEXT_MAJOR_VERSION_ARB, + graphics_api_version.major, + GLX_CONTEXT_MINOR_VERSION_ARB, + graphics_api_version.minor, + GLX_CONTEXT_PROFILE_MASK_ARB, + // we don't need compatibility context + GLX_CONTEXT_CORE_PROFILE_BIT_ARB, + None + }; + + gl_context = glx_create_context_attribs_arb( this->display.xorg_display.display, // - False + fb_config, + shared_glx_context, + GL_TRUE, + context_attribs.data() ); - - if (gl_context == nullptr) { - throw std::runtime_error("glXCreateContext() failed"); - } - - return gl_context; - }()) - {} - - glx_context_wrapper(const glx_context_wrapper&) = delete; - glx_context_wrapper& operator=(const glx_context_wrapper&) = delete; - - glx_context_wrapper(glx_context_wrapper&&) = delete; - glx_context_wrapper& operator=(glx_context_wrapper&&) = delete; - - ~glx_context_wrapper() - { - if (glXGetCurrentContext() == this->context) { - glXMakeCurrent( + } else { + // GLX_ARB_create_context is not supported + gl_context = glXCreateContext( this->display.xorg_display.display, // - None, - nullptr + visual_info, + shared_glx_context, + GL_TRUE ); } - glXDestroyContext( + + // sync to ensure any errors generated are processed + XSync( this->display.xorg_display.display, // - this->context + False ); - } - } glx_context; -#elif defined(RUISAPP_RENDER_OPENGLES) - egl_surface_wrapper egl_surface; - egl_context_wrapper egl_context; -#endif + if (gl_context == nullptr) { + throw std::runtime_error("glXCreateContext() failed"); + } - struct xorg_input_context_wrapper { - const XIC input_context; - - xorg_input_context_wrapper( - const display_wrapper& display, // - const xorg_window_wrapper& window - ) : - input_context([&]() { - auto ic = - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, "using C API") - XCreateIC( - display.xorg_input_method.xim, - XNClientWindow, - window.window, - XNFocusWindow, - window.window, - XNInputStyle, - XIMPreeditNothing | XIMStatusNothing, - nullptr - ); - if (ic == nullptr) { - throw std::runtime_error("XCreateIC() failed"); - } - return ic; - }()) - {} + return gl_context; + }()) + {} - xorg_input_context_wrapper(const xorg_input_context_wrapper&) = delete; - xorg_input_context_wrapper& operator=(const xorg_input_context_wrapper&) = delete; + glx_context_wrapper(const glx_context_wrapper&) = delete; + glx_context_wrapper& operator=(const glx_context_wrapper&) = delete; - xorg_input_context_wrapper(xorg_input_context_wrapper&&) = delete; - xorg_input_context_wrapper& operator=(xorg_input_context_wrapper&&) = delete; + glx_context_wrapper(glx_context_wrapper&&) = delete; + glx_context_wrapper& operator=(glx_context_wrapper&&) = delete; - ~xorg_input_context_wrapper() - { - XUnsetICFocus(this->input_context); - XDestroyIC(this->input_context); + ~glx_context_wrapper() + { + if (glXGetCurrentContext() == this->context) { + glXMakeCurrent( + this->display.xorg_display.display, // + None, + nullptr + ); } - } xorg_input_context; + glXDestroyContext( + this->display.xorg_display.display, // + this->context + ); + } +}; +#endif +class native_window : public gl_visual_info, public xorg_window +{ public: native_window( utki::shared_ref display, // const utki::version_duplet& gl_version, const ruisapp::window_parameters& window_params, - native_window* shared_gl_context_native_window + const native_window* shared_gl_context_native_window ) : - display(std::move(display)), - fb_config( -#ifdef RUISAPP_RENDER_OPENGL - this->display, -#elif defined(RUISAPP_RENDER_OPENGLES) - this->display.get().egl_display, -#endif + gl_visual_info( + display.get(), // gl_version, window_params ), - xorg_visual_info( - this->display, // - this->fb_config - ), - xorg_color_map( - this->display, // - this->xorg_visual_info - ), xorg_window( - this->display, // + display, // window_params, - this->xorg_color_map, - this->xorg_visual_info, - shared_gl_context_native_window != nullptr + shared_gl_context_native_window, + this->xvisual ), -#ifdef RUISAPP_RENDER_OPENGL +#if defined(RUISAPP_RENDER_OPENGL) glx_context( - this->display, // - this->xorg_visual_info, + this->display.get(), // + this->visual_info, gl_version, this->fb_config, - shared_gl_context_native_window - ), + shared_gl_context_native_window ? shared_gl_context_native_window->glx_context.context : nullptr + ) #elif defined(RUISAPP_RENDER_OPENGLES) egl_surface( this->display.get().egl_display, // - this->fb_config, - this->xorg_window.window + this->egl_config, + this->window.window ), egl_context( this->display.get().egl_display, // gl_version, - this->fb_config, + this->egl_config, shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT - ), -#endif - xorg_input_context( - this->display, // - this->xorg_window ) +#endif { -#ifdef RUISAPP_RENDER_OPENGL +#if defined(RUISAPP_RENDER_OPENGL) // if there is no any GL context current, then set this one before calling glewInit() if (glXGetCurrentContext() == nullptr) { this->bind_rendering_context(); @@ -690,23 +488,57 @@ public: ~native_window() override = default; - void destroy_x11_window() + void bind_rendering_context() override { - if (!this->x11_window_destroyed) { #ifdef RUISAPP_RENDER_OPENGL - // unbind the GLX context from the current thread; - // do NOT destroy it — all windows share the same GLX context - glXMakeCurrent(this->display.get().xorg_display.display, GLX_NONE, nullptr); + glXMakeCurrent( + this->display.get().xorg_display.display, // + this->window.window, + this->glx_context.context + ); #elif defined(RUISAPP_RENDER_OPENGLES) - eglMakeCurrent(this->display.get().egl_display.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + if (eglMakeCurrent( + this->display.get().egl_display.display, + this->egl_surface.surface, + this->egl_surface.surface, + this->egl_context.context + ) == EGL_FALSE) + { + throw std::runtime_error("eglMakeCurrent() failed"); + } #endif - XDestroyWindow( - this->display.get().xorg_display.display, // - this->xorg_window.window - ); - XFlush(this->display.get().xorg_display.display); - this->x11_window_destroyed = true; + } + + bool is_rendering_context_bound() const noexcept override + { +#ifdef RUISAPP_RENDER_OPENGL + return glXGetCurrentContext() == this->glx_context.context; +#elif defined(RUISAPP_RENDER_OPENGLES) + return eglGetCurrentContext() == this->egl_context.context; +#endif + } + + void swap_frame_buffers() override + { +#ifdef RUISAPP_RENDER_OPENGL + glXSwapBuffers( + this->display.get().xorg_display.display, // + this->window.window + ); + if (this->is_vsync_enabled()) { + // With glFinish() call here it works better when VSYNC is enabled. + // This is noticable when an app renders a mouse cursor itself, + // the cursor moves with less latency. + // For some reason, the effect is only present if calling the glFinish() + // right after swapping buffers, if calling it before swapping buffers, + // the effect is not present. + glFinish(); } +#elif defined(RUISAPP_RENDER_OPENGLES) + this->egl_surface.swap_frame_buffers(); +#else +# error "Unknown graphics API" +#endif } void set_vsync_enabled_internal(bool enable) override @@ -735,7 +567,7 @@ public: // disable v-sync glx_swap_interval_ext( this->display.get().xorg_display.display, // - this->xorg_window.window, + this->window.window, enable ? 1 : 0 // swap interval in vsync frames ); } else if (this->glx_context.supported_extensions.get(glx_context_wrapper::glx_extension::glx_mesa_swap_control @@ -781,218 +613,13 @@ public: #endif } - void swap_frame_buffers() override - { -#ifdef RUISAPP_RENDER_OPENGL - glXSwapBuffers( - this->display.get().xorg_display.display, // - this->xorg_window.window - ); - if (this->is_vsync_enabled()) { - // With glFinish() call here it works better when VSYNC is enabled. - // This is noticable when an app renders a mouse cursor itself, - // the cursor moves with less latency. - // For some reason, the effect is only present if calling the glFinish() - // right after swapping buffers, if calling it before swapping buffers, - // the effect is not present. - glFinish(); - } -#elif defined(RUISAPP_RENDER_OPENGLES) - this->egl_surface.swap_frame_buffers(); -#else -# error "Unknown graphics API" -#endif - } - - static_assert(std::is_integral_v<::Window>, "xorg lib's Window type is unexpectedly not integral"); - using window_id_type = ::Window; - - window_id_type get_id() const noexcept - { - return this->xorg_window.window; - } - - void set_fullscreen_internal(bool enable) override - { - Atom state_atom = XInternAtom( - this->display.get().xorg_display.display, // - "_NET_WM_STATE", - False - ); - Atom atom = XInternAtom( - this->display.get().xorg_display.display, // - "_NET_WM_STATE_FULLSCREEN", - False - ); - - XEvent event; - event.xclient.type = ClientMessage; - event.xclient.serial = 0; - event.xclient.send_event = True; - event.xclient.window = this->xorg_window.window; - event.xclient.message_type = state_atom; - - // data should be viewed as list of longs - event.xclient.format = utki::byte_bits * 4; - - // use union from third-party code - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[0] = enable ? 1 : 0; - - // use union from third-party code - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[1] = long(atom); - - // use union from third-party code - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - event.xclient.data.l[2] = 0; - - XSendEvent( - this->display.get().xorg_display.display, // - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic) - this->display.get().xorg_display.get_default_root_window(), - False, - SubstructureRedirectMask | SubstructureNotifyMask, - &event - ); - - this->display.get().xorg_display.flush(); - } - - void set_mouse_cursor(ruis::mouse_cursor c) override - { - this->cur_cursor = &this->display.get().get_cursor(c); - - if (this->cursor_visible) { - this->apply_cursor(*this->cur_cursor); - } - } - - void set_mouse_cursor_visible(bool visible) override - { - this->cursor_visible = visible; - if (visible) { - if (this->cur_cursor) { - this->apply_cursor(*this->cur_cursor); - } else { - XUndefineCursor( - this->display.get().xorg_display.display, // - this->xorg_window.window - ); - } - } else { - this->apply_cursor(this->display.get().get_cursor(ruis::mouse_cursor::none)); - } - } - - std::u32string get_string(XKeyEvent& event) const - { -#ifndef X_HAVE_UTF8_STRING -# error "no Xutf8stringlookup()" -#endif - constexpr auto static_buf_size = 32; - - // the array is used to get data, so no need to initialize it - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array static_buf; - - std::vector arr; - auto buf = utki::make_span(static_buf); - - // the variable is initialized via output argument, so no need to initialize - // it here NOLINTNEXTLINE(cppcoreguidelines-init-variables) - Status status; - - int size = Xutf8LookupString( - this->xorg_input_context.input_context, // - &event, - buf.data(), - int(buf.size() - 1), - nullptr, - &status - ); - if (status == XBufferOverflow) { - // allocate enough memory - arr.resize(size + 1); - buf = utki::make_span(arr); - size = Xutf8LookupString( - this->xorg_input_context.input_context, // - &event, - buf.data(), - int(buf.size() - 1), - nullptr, - &status - ); - } - utki::assert(size >= 0, SL); - utki::assert(buf.size() != 0, SL); - utki::assert(buf.size() > unsigned(size), SL); - - buf[size] = 0; // null-terminate - - switch (status) { - case XLookupChars: - case XLookupBoth: - if (size == 0) { - return {}; - } - return utki::to_utf32(buf.data()); - default: - case XBufferOverflow: - utki::assert(false, SL); - case XLookupKeySym: - case XLookupNone: - break; - } - - return {}; - } - private: - // NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive") - cursor_wrapper* cur_cursor = nullptr; - bool cursor_visible = true; - bool x11_window_destroyed = false; - - void apply_cursor(cursor_wrapper& c) - { - // set the cursor to xorg window - XDefineCursor( - this->display.get().xorg_display.display, // - this->xorg_window.window, - c.cursor - ); - } - - void bind_rendering_context() override - { -#ifdef RUISAPP_RENDER_OPENGL - glXMakeCurrent( - this->display.get().xorg_display.display, // - this->xorg_window.window, - this->glx_context.context - ); +#if defined(RUISAPP_RENDER_OPENGL) + const glx_context_wrapper glx_context; #elif defined(RUISAPP_RENDER_OPENGLES) - if (eglMakeCurrent( - this->display.get().egl_display.display, - this->egl_surface.surface, - this->egl_surface.surface, - this->egl_context.context - ) == EGL_FALSE) - { - throw std::runtime_error("eglMakeCurrent() failed"); - } -#endif - } - - bool is_rendering_context_bound() const noexcept override - { -#ifdef RUISAPP_RENDER_OPENGL - return glXGetCurrentContext() == this->glx_context.context; -#elif defined(RUISAPP_RENDER_OPENGLES) - return eglGetCurrentContext() == this->egl_context.context; + egl_surface_wrapper egl_surface; + egl_context_wrapper egl_context; #endif - } }; } // namespace diff --git a/src/ruisapp/glue/linux/xorg/xorg_window.hxx b/src/ruisapp/glue/linux/xorg/xorg_window.hxx new file mode 100644 index 0000000..29fd0a1 --- /dev/null +++ b/src/ruisapp/glue/linux/xorg/xorg_window.hxx @@ -0,0 +1,514 @@ +/* +ruisapp - ruis GUI adaptation layer + +Copyright (C) 2016-2025 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +// A base X11 native window that implements everything that is not related to +// the graphics API: X11 window creation, keyboard input (via XIM/IME), mouse +// cursor handling, fullscreen, cascading, etc. +// +// The graphics-specific parts (visual selection, graphics context creation, +// swap_frame_buffers, bind_rendering_context) are implemented by the derived +// classes (see window.hxx for OpenGL/OpenGL ES and vulkan_window.hxx for +// Vulkan). + +#pragma once + +#include +#include +#include +#include + +#include "display.hxx" + +namespace { + +// new windows are cascaded to prevent overlap +constexpr unsigned cascade_max_columns = 15; +constexpr int cascade_step_px = 60; +constexpr int cascade_hint_step_px = 40; + +// the cascade offset (in pixels) for the window at the given index +inline int cascade_offset(unsigned index, int step_px) +{ + return int(index % cascade_max_columns) * step_px; +} + +// the X11 visual info needed to create a window. The graphics API determines +// which visual to use (OpenGL/OpenGL ES pick an FBConfig-based visual, Vulkan +// uses the default visual) +struct xorg_visual { + Visual* visual; + unsigned depth; +}; + +class xorg_window : public ruis::render::native_window +{ + struct xorg_color_map_wrapper { + display_wrapper& display; + + const Colormap color_map; + + xorg_color_map_wrapper( + display_wrapper& display, // + Visual* visual + ) : + display(display), + color_map([&]() { + auto cm = XCreateColormap( + this->display.xorg_display.display, + this->display.xorg_display.get_root_window(this->display.xorg_display.get_default_screen()), + visual, + AllocNone + ); + if (cm == None) { + // TODO: use XSetErrorHandler() to get error code + throw std::runtime_error("XCreateColormap(): failed"); + } + return cm; + }()) + {} + + xorg_color_map_wrapper(const xorg_color_map_wrapper&) = delete; + xorg_color_map_wrapper& operator=(const xorg_color_map_wrapper&) = delete; + + xorg_color_map_wrapper(xorg_color_map_wrapper&&) = delete; + xorg_color_map_wrapper& operator=(const xorg_color_map_wrapper&&) = delete; + + ~xorg_color_map_wrapper() + { + XFreeColormap( + this->display.xorg_display.display, // + this->color_map + ); + } + }; + + struct xorg_window_wrapper { + display_wrapper& display; + + const ::Window window; + mutable bool destroyed = false; + + xorg_window_wrapper( + display_wrapper& display, // + const ruisapp::window_parameters& window_params, + const xorg_color_map_wrapper& color_map, + Visual* visual, + unsigned depth, + bool visible, + int cascade_px, + int hint_px + ) : + display(display), + window([&]() { + XSetWindowAttributes attr; + attr.colormap = color_map.color_map; + attr.border_pixel = 0; + attr.background_pixmap = None; + attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | ButtonMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask; + unsigned long fields = CWBorderPixel | CWColormap | CWEventMask | CWBackPixmap; + + auto dims = (this->display.scale_factor * window_params.dims.to()).to(); + + auto w = XCreateWindow( + this->display.xorg_display.display, + this->display.xorg_display.get_root_window(this->display.xorg_display.get_default_screen() + ), // parent + cascade_px, // x position + cascade_px, // y position + dims.x(), // width + dims.y(), // height + 0, // border width + depth, // window's depth + InputOutput, // window's class + visual, + fields, // defined attributes + &attr + ); + if (!w) { + throw std::runtime_error("Failed to create window"); + } + return w; + }()) + { + { // we want to handle WM_DELETE_WINDOW event to know when window is closed + Atom a = XInternAtom( + this->display.xorg_display.display, // + "WM_DELETE_WINDOW", + True + ); + XSetWMProtocols( + this->display.xorg_display.display, // + this->window, + &a, + 1 + ); + } + + { + // set the window position hint so the WM respects the cascading position + auto* hints = XAllocSizeHints(); + hints->flags = USPosition; + hints->x = hint_px; + hints->y = hint_px; + XSetWMNormalHints(this->display.xorg_display.display, this->window, hints); + XFree(hints); + } + + if (!window_params.taskbar) { + Atom wm_state = XInternAtom( + this->display.xorg_display.display, // + "_NET_WM_STATE", + False + ); + Atom skip_taskbar = + XInternAtom(this->display.xorg_display.display, "_NET_WM_STATE_SKIP_TASKBAR", False); + + static_assert(sizeof(skip_taskbar) >= 4, "Atom must be of at least 32-bit size"); + XChangeProperty( + this->display.xorg_display.display, // + this->window, + wm_state, + XA_ATOM, // type of data + 4 * utki::byte_bits, // data is a list of 32-bit values + PropModeReplace, + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast, "using C API") + reinterpret_cast(&skip_taskbar), // data + 1 // only one value in the data + ); + } + + if (visible) { + XMapWindow( + this->display.xorg_display.display, // + this->window + ); + } + + this->display.xorg_display.flush(); + + if (!window_params.title.empty()) { + XStoreName( + this->display.xorg_display.display, // + this->window, + window_params.title.c_str() + ); + } + + // force the cascading position (WM may ignore position hints) + XMoveWindow( + this->display.xorg_display.display, // + this->window, + cascade_px, + cascade_px + ); + } + + xorg_window_wrapper(const xorg_window_wrapper&) = delete; + xorg_window_wrapper& operator=(const xorg_window_wrapper&) = delete; + + xorg_window_wrapper(xorg_window_wrapper&&) = delete; + xorg_window_wrapper& operator=(const xorg_window_wrapper&&) = delete; + + ~xorg_window_wrapper() + { + this->destroy(); + } + + void destroy() const + { + if (!this->destroyed) { + XDestroyWindow( + this->display.xorg_display.display, // + this->window + ); + XFlush(this->display.xorg_display.display); + this->destroyed = true; + } + } + }; + + struct xorg_input_context_wrapper { + const XIC input_context; + + xorg_input_context_wrapper( + const display_wrapper& display, // + const xorg_window_wrapper& window + ) : + input_context([&]() { + auto ic = + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, "using C API") + XCreateIC( + display.xorg_input_method.xim, + XNClientWindow, + window.window, + XNFocusWindow, + window.window, + XNInputStyle, + XIMPreeditNothing | XIMStatusNothing, + nullptr + ); + if (ic == nullptr) { + throw std::runtime_error("XCreateIC() failed"); + } + return ic; + }()) + {} + + xorg_input_context_wrapper(const xorg_input_context_wrapper&) = delete; + xorg_input_context_wrapper& operator=(const xorg_input_context_wrapper&) = delete; + + xorg_input_context_wrapper(xorg_input_context_wrapper&&) = delete; + xorg_input_context_wrapper& operator=(const xorg_input_context_wrapper&&) = delete; + + ~xorg_input_context_wrapper() + { + XUnsetICFocus(this->input_context); + XDestroyIC(this->input_context); + } + }; + +public: + // counter for cascading new windows to prevent overlap + static inline unsigned s_window_count = 0; + + xorg_window( + utki::shared_ref display, // + const ruisapp::window_parameters& window_params, + const xorg_window* shared, + const xorg_visual& visual + ) : + display(std::move(display)), + colormap( + this->display, // + visual.visual + ), + window( + this->display, // + window_params, + this->colormap, + visual.visual, + visual.depth, + shared != nullptr, // visible + cascade_offset(s_window_count, cascade_step_px), + cascade_offset(s_window_count, cascade_hint_step_px) + ), + input_context( + this->display, // + this->window + ), + current_dims((this->display.get().scale_factor * window_params.dims.to()).to()) + { + s_window_count++; + } + + xorg_window(const xorg_window&) = delete; + xorg_window& operator=(const xorg_window&) = delete; + + xorg_window(xorg_window&&) = delete; + xorg_window& operator=(const xorg_window&&) = delete; + + ~xorg_window() override = default; + + void destroy_x11_window() + { + this->window.destroy(); + } + + static_assert(std::is_integral_v<::Window>, "xorg lib's Window type is unexpectedly not integral"); + using window_id_type = ::Window; + + window_id_type get_id() const noexcept + { + return this->window.window; + } + + r4::vector2 get_dims() const noexcept override + { + return this->current_dims; + } + + void set_dims(const r4::vector2 dims) const noexcept override + { + XResizeWindow(this->display.get().xorg_display.display, this->window.window, dims.x(), dims.y()); + this->current_dims = dims; + } + + void set_fullscreen_internal(bool enable) override + { + Atom state_atom = XInternAtom( + this->display.get().xorg_display.display, // + "_NET_WM_STATE", + False + ); + Atom atom = XInternAtom( + this->display.get().xorg_display.display, // + "_NET_WM_STATE_FULLSCREEN", + False + ); + + XEvent event; + event.xclient.type = ClientMessage; + event.xclient.serial = 0; + event.xclient.send_event = True; + event.xclient.window = this->window.window; + event.xclient.message_type = state_atom; + + // data should be viewed as list of longs + event.xclient.format = utki::byte_bits * 4; + + // use union from third-party code + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[0] = enable ? 1 : 0; + + // use union from third-party code + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[1] = long(atom); + + // use union from third-party code + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + event.xclient.data.l[2] = 0; + + XSendEvent( + this->display.get().xorg_display.display, // + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic) + this->display.get().xorg_display.get_default_root_window(), + False, + SubstructureRedirectMask | SubstructureNotifyMask, + &event + ); + + this->display.get().xorg_display.flush(); + } + + void set_mouse_cursor(ruis::mouse_cursor c) override + { + this->cur_cursor = &this->display.get().get_cursor(c); + + if (this->cursor_visible) { + this->apply_cursor(*this->cur_cursor); + } + } + + void set_mouse_cursor_visible(bool visible) override + { + this->cursor_visible = visible; + if (visible) { + if (this->cur_cursor) { + this->apply_cursor(*this->cur_cursor); + } else { + XUndefineCursor( + this->display.get().xorg_display.display, // + this->window.window + ); + } + } else { + this->apply_cursor(this->display.get().get_cursor(ruis::mouse_cursor::none)); + } + } + + std::u32string get_string(XKeyEvent& event) const + { +#ifndef X_HAVE_UTF8_STRING +# error "no Xutf8stringlookup()" +#endif + constexpr auto static_buf_size = 32; + + // the array is used to get data, so no need to initialize it + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array static_buf; + + std::vector arr; + auto buf = utki::make_span(static_buf); + + // the variable is initialized via output argument, so no need to initialize + // it here NOLINTNEXTLINE(cppcoreguidelines-init-variables) + Status status; + + int size = Xutf8LookupString( + this->input_context.input_context, // + &event, + buf.data(), + int(buf.size() - 1), + nullptr, + &status + ); + if (status == XBufferOverflow) { + // allocate enough memory + arr.resize(size + 1); + buf = utki::make_span(arr); + size = Xutf8LookupString( + this->input_context.input_context, // + &event, + buf.data(), + int(buf.size() - 1), + nullptr, + &status + ); + } + utki::assert(size >= 0, SL); + utki::assert(buf.size() != 0, SL); + utki::assert(buf.size() > unsigned(size), SL); + + buf[size] = 0; // null-terminate + + switch (status) { + case XLookupChars: + case XLookupBoth: + if (size == 0) { + return {}; + } + return utki::to_utf32(buf.data()); + default: + case XBufferOverflow: + utki::assert(false, SL); + case XLookupKeySym: + case XLookupNone: + break; + } + + return {}; + } + +protected: + const utki::shared_ref display; + const xorg_color_map_wrapper colormap; + const xorg_window_wrapper window; + const xorg_input_context_wrapper input_context; + mutable r4::vector2 current_dims; + +private: + // NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive") + cursor_wrapper* cur_cursor = nullptr; + bool cursor_visible = true; + + void apply_cursor(cursor_wrapper& c) + { + // set the cursor to xorg window + XDefineCursor( + this->display.get().xorg_display.display, // + this->window.window, + c.cursor + ); + } +}; + +} // namespace From 98de76e120a187a1103c50ffb7cd5a9ffc162a24 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 20 Nov 2026 23:06:59 +0200 Subject: [PATCH 07/22] Address PR review comments - Remove Vulkan build gating from CMake (always build/export/declare vulkan variants) - Change vulkan_device from unique_ptr to shared_ref (wayland + xorg) - Remove vulkan_surface_provider member, construct null surface locally for factory - Rename window.hxx to opengl_window.hxx - Change negative #ifndef RUISAPP_RENDER_VULKAN to positive conditions - Add explanatory comments (present_func, surface extensions, VU_CHECK, concrete factory, window mapped roundtrip, egl_dummy_surface) - Remove unnecessary curly brackets in ConfigureNotify case - Remove unnecessary send_mouse_move calls in ButtonPress/ButtonRelease - Remove unused destroy_x11_window() from xorg_window - Use ctx().ren().ctx().fac() shortcuts in test app --- build/cmake/CMakeLists.txt | 36 ++++------------- .../glue/linux/wayland/application.cxx | 14 ++++--- .../glue/linux/wayland/application.hxx | 19 +++++---- src/ruisapp/glue/linux/wayland/window.hxx | 12 +++--- .../glue/linux/wayland/xdg_surface.cxx | 2 +- src/ruisapp/glue/linux/xorg/glue.cxx | 40 ++++++++----------- .../xorg/{window.hxx => opengl_window.hxx} | 0 src/ruisapp/glue/linux/xorg/xorg_window.hxx | 5 --- tests/app/src/cube_widget.cpp | 3 +- 9 files changed, 51 insertions(+), 80 deletions(-) rename src/ruisapp/glue/linux/xorg/{window.hxx => opengl_window.hxx} (100%) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 83ddf4f..b089342 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -170,30 +170,14 @@ if(LINUX) endif() endfunction() - # The Vulkan variants require the ruis-render-vulkan package. In a monorepo - # build it is a regular target (already added before this project), in a - # package-manager build (vcpkg, ...) it is only found when it is already - # published. Build the Vulkan variants only when the package is available, - # so the rest of the package keeps working until ruis-render-vulkan is released. - set(ruisapp_build_vulkan_variant True) - if(NOT TARGET ruis-render-vulkan::ruis-render-vulkan) - find_package(ruis-render-vulkan CONFIG QUIET) - if(NOT ruis-render-vulkan_FOUND) - set(ruisapp_build_vulkan_variant False) - endif() - endif() - ruisapp_declare_variant(opengl xorg INSTALL_HEADERS) ruisapp_declare_variant(opengles xorg) ruisapp_declare_variant(opengl wayland) ruisapp_declare_variant(opengles wayland) ruisapp_declare_variant(opengl sdl) ruisapp_declare_variant(opengles sdl) - - if(ruisapp_build_vulkan_variant) - ruisapp_declare_variant(vulkan xorg) - ruisapp_declare_variant(vulkan wayland) - endif() + ruisapp_declare_variant(vulkan xorg) + ruisapp_declare_variant(vulkan wayland) # Compatibility aliases: for now ruisapp-opengl / ruisapp-opengles refer to the xorg variants. # Note: the namespaced alias refers to the plain (non-namespaced) target, the same @@ -301,12 +285,10 @@ if(LINUX) ${name}-opengl-sdl ${name}-opengles-sdl ) - if(ruisapp_build_vulkan_variant) - list(APPEND ruisapp_export_targets - ${name}-vulkan-xorg - ${name}-vulkan-wayland - ) - endif() + list(APPEND ruisapp_export_targets + ${name}-vulkan-xorg + ${name}-vulkan-wayland + ) myci_export( TARGETS ${ruisapp_export_targets} @@ -360,10 +342,8 @@ if(LINUX) ruisapp_declare_test_app(${renderer} ${backend}) endforeach() endforeach() - if(ruisapp_build_vulkan_variant) - ruisapp_declare_test_app(vulkan xorg) - ruisapp_declare_test_app(vulkan wayland) - endif() + ruisapp_declare_test_app(vulkan xorg) + ruisapp_declare_test_app(vulkan wayland) else() if(ruisapp_build_opengl_variant) myci_declare_application(${name}-opengl-test diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index ba0a30f..0391e96 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -224,7 +224,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window [&](auto&& win_ref) -> utki::shared_ref { auto wl_surf = win_ref.get().get_wl_surface(); auto wl_disp = win_ref.get().get_wl_display(); - auto& vdev = *this->vulkan_device; + auto& vdev = this->vulkan_device.get(); VkWaylandSurfaceCreateInfoKHR sci{}; sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; sci.display = wl_disp; @@ -264,8 +264,9 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window #ifdef RUISAPP_RENDER_VULKAN { - // use a raw pointer to avoid a circular shared_ref that would - // prevent the app_window from being destroyed + // The Vulkan window presents frames via a callback (present_func) set here, + // because the native window is graphics-agnostic and does not know about the renderer. + // Use a raw pointer to avoid a circular shared_ref that would prevent destruction. auto* win_ptr = &ruisapp_window.get(); ruisapp_window.get().ruis_native_window.get().present_func = [win_ptr]() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") @@ -294,7 +295,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window auto& ret_win = res.first->second.get(); -#ifndef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) // EGL surface for the window is not created yet at this point. // In Wayland the EGL surface is created asynchronously. // Some further calls (e.g. set_vsync_enabled()) rely on the EGL surface to be created, @@ -310,8 +311,9 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window } utki::assert(ret_win.ruis_native_window.get().is_egl_surface_created(), SL); -#else - // For Vulkan, do a roundtrip to make sure the window is mapped before proceeding. +#elif defined(RUISAPP_RENDER_VULKAN) + // A roundtrip ensures the compositor has processed the toplevel and the + // surface is mapped, so the Vulkan surface created from it is valid. wl_display_roundtrip(this->display.get().wayland_display.display); #endif diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 38d2d79..943481b 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -128,8 +128,9 @@ public: // TODO: make windowless shared egl context const utki::shared_ref shared_gl_context_native_window; #ifdef RUISAPP_RENDER_VULKAN - std::unique_ptr vulkan_device; - const utki::shared_ref vulkan_surface_provider; + // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). + // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. + const utki::shared_ref vulkan_device; #endif const utki::shared_ref render_factory; const utki::shared_ref common_shaders; @@ -173,20 +174,18 @@ public: ) ), #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(ruis::render::vulkan::vulkan_device::make( + vulkan_device(std::shared_ptr(ruis::render::vulkan::vulkan_device::make( {std::string(VK_KHR_SURFACE_EXTENSION_NAME), std::string(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) }, // instance extensions {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // device extensions - )), - // Vulkan has no "current context" — no surface needed for resource loading - vulkan_surface_provider(utki::make_shared( - *this->vulkan_device, // - VK_NULL_HANDLE - )), + ))), #endif render_factory( #ifdef RUISAPP_RENDER_VULKAN - utki::make_shared(this->vulkan_surface_provider) + // Vulkan has no "current context" — a null surface is sufficient for the factory (resource creation) + utki::make_shared( + utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) + ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) #elif defined(RUISAPP_RENDER_OPENGLES) diff --git a/src/ruisapp/glue/linux/wayland/window.hxx b/src/ruisapp/glue/linux/wayland/window.hxx index 3ea35a7..acf3f86 100644 --- a/src/ruisapp/glue/linux/wayland/window.hxx +++ b/src/ruisapp/glue/linux/wayland/window.hxx @@ -48,10 +48,11 @@ class native_window : public ruis::render::native_window xdg_toplevel_wrapper xdg_toplevel; wayland_egl_window_wrapper wayland_egl_window; -#ifndef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) egl_config_wrapper egl_config; egl_context_wrapper egl_context; - + // An offscreen pbuffer surface used as a fallback for the shared GL context + // when no real window surface is available (e.g. during resource loading). std::optional egl_dummy_surface; std::optional egl_surface; #endif @@ -96,7 +97,7 @@ public: this->wayland_surface, // window_params.dims ), -#ifndef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) egl_config( this->display.get().egl_display, // gl_version, @@ -269,7 +270,8 @@ private: public: #ifdef RUISAPP_RENDER_VULKAN - // callback for presenting the Vulkan frame + // Callback for presenting the Vulkan frame. Set by the application glue + // (see application.cxx make_window) because the native window is graphics-agnostic. std::function present_func; #endif @@ -301,7 +303,7 @@ public: #ifdef RUISAPP_RENDER_VULKAN (void)enabled; // VSync is handled by the Vulkan present mode -#else +#elif defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) utki::assert( [this]() { return this->is_rendering_context_bound(); diff --git a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx index 0e70ad4..1242374 100644 --- a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx +++ b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx @@ -90,7 +90,7 @@ void xdg_surface_wrapper::xdg_surface_configure( auto& win = *window; auto& natwin = win.ruis_native_window.get(); -#ifndef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) // The configure event can come several times for the same xdg_surface // because we do several surface commits during the initial configuration, // and each commit can trigger a configure event. diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 51efa79..7e751d2 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -56,7 +56,7 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_VULKAN # include "vulkan_window.hxx" #else -# include "window.hxx" +# include "opengl_window.hxx" #endif using namespace std::string_literals; @@ -99,7 +99,9 @@ class application_glue : public utki::destructable private: #ifdef RUISAPP_RENDER_VULKAN - std::unique_ptr vulkan_device; + // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). + // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. + const utki::shared_ref vulkan_device; #endif utki::version_duplet gl_version; @@ -129,13 +131,13 @@ class application_glue : public utki::destructable application_glue(const utki::version_duplet& gl_version) : #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(ruis::render::vulkan::vulkan_device::make( + vulkan_device(std::shared_ptr(ruis::render::vulkan::vulkan_device::make( { std::string(VK_KHR_SURFACE_EXTENSION_NAME), std::string(VK_KHR_XLIB_SURFACE_EXTENSION_NAME) }, // {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // - )), + ))), #endif gl_version(gl_version), shared_gl_context_native_window( // @@ -152,7 +154,7 @@ class application_glue : public utki::destructable // Vulkan has no "current context" — all GPU resources live on the VkDevice and // are inherently shareable. No surface is needed for the shared factory. utki::make_shared( - utki::make_shared(*this->vulkan_device, VK_NULL_HANDLE) + utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) @@ -199,13 +201,14 @@ class application_glue : public utki::destructable #ifdef RUISAPP_RENDER_VULKAN auto win_surface_provider = - utki::make_shared(*this->vulkan_device, [&]() { + utki::make_shared(this->vulkan_device.get(), [&]() { VkXlibSurfaceCreateInfoKHR ci{}; ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; ci.dpy = this->display.get().xorg_display.display; ci.window = ruis_native_window.get().get_id(); VkSurfaceKHR surface = VK_NULL_HANDLE; - VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device->get_instance(), &ci, nullptr, &surface)); + // VU_CHECK: Vulkan error checking macro from ruis/render/vulkan/vulkan.hpp + VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device.get().get_instance(), &ci, nullptr, &surface)); return surface; }()); auto vulkan_factory_ref = @@ -224,6 +227,8 @@ class application_glue : public utki::destructable #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_VULKAN) + // The Vulkan context needs the concrete factory (not the abstract one) + // because it accesses Vulkan-specific members (e.g. all_textures, pipeline caches). utki::make_shared( ruis_native_window, // win_surface_provider, // @@ -552,12 +557,11 @@ int main(int argc, const char** argv) w.render(); break; case ConfigureNotify: - { - // squash all window resize events into one, for that store the new - // window dimensions and update the viewport later only once - w.new_win_dims.x() = ruis::real(event.xconfigure.width); - w.new_win_dims.y() = ruis::real(event.xconfigure.height); - } + // squash all window resize events into one, for that store the new + // window dimensions and update the viewport later only once + w.new_win_dims.x() = ruis::real(event.xconfigure.width); + w.new_win_dims.y() = ruis::real(event.xconfigure.height); + break; break; case KeyPress: { @@ -621,11 +625,6 @@ int main(int argc, const char** argv) } break; case ButtonPress: - // send mouse move first so the GUI knows the pointer position - w.gui.send_mouse_move( - ruis::vec2(event.xbutton.x, event.xbutton.y), - 0 // pointer_id - ); w.gui.send_mouse_button( ruis::button_action::press, // ruis::vec2(event.xbutton.x, event.xbutton.y), @@ -634,11 +633,6 @@ int main(int argc, const char** argv) ); break; case ButtonRelease: - // send mouse move first so the GUI knows the pointer position - w.gui.send_mouse_move( - ruis::vec2(event.xbutton.x, event.xbutton.y), - 0 // pointer_id - ); w.gui.send_mouse_button( ruis::button_action::release, // ruis::vec2(event.xbutton.x, event.xbutton.y), diff --git a/src/ruisapp/glue/linux/xorg/window.hxx b/src/ruisapp/glue/linux/xorg/opengl_window.hxx similarity index 100% rename from src/ruisapp/glue/linux/xorg/window.hxx rename to src/ruisapp/glue/linux/xorg/opengl_window.hxx diff --git a/src/ruisapp/glue/linux/xorg/xorg_window.hxx b/src/ruisapp/glue/linux/xorg/xorg_window.hxx index 29fd0a1..34be86f 100644 --- a/src/ruisapp/glue/linux/xorg/xorg_window.hxx +++ b/src/ruisapp/glue/linux/xorg/xorg_window.hxx @@ -328,11 +328,6 @@ public: ~xorg_window() override = default; - void destroy_x11_window() - { - this->window.destroy(); - } - static_assert(std::is_integral_v<::Window>, "xorg lib's Window type is unexpectedly not integral"); using window_id_type = ::Window; diff --git a/tests/app/src/cube_widget.cpp b/tests/app/src/cube_widget.cpp index 1a860be..8c3ebd5 100644 --- a/tests/app/src/cube_widget.cpp +++ b/tests/app/src/cube_widget.cpp @@ -23,8 +23,7 @@ cube_widget::cube_widget( ruis::vec3(-1, -1, 1), ruis::vec3(-1, -1, 1), ruis::vec3(1, -1, -1), ruis::vec3(1, -1, 1)} }; - auto pos_vbo = - this->context.get().renderer.get().rendering_context.get().fac().make_vertex_buffer(utki::make_span(cube_pos)); + auto pos_vbo = this->ctx().ren().ctx().fac().make_vertex_buffer(utki::make_span(cube_pos)); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) std::array cube_tex = { From 213d2a062e12119d6d00c671aaedafe2ff8e8e93 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 20 Nov 2026 23:25:29 +0200 Subject: [PATCH 08/22] Address follow-up PR review comments - Remove ruisapp_export_targets list, pass targets directly to myci_export() - Capture window by reference instead of raw pointer in close_handler --- build/cmake/CMakeLists.txt | 21 ++++++++------------- tests/app/src/text_input_window.cpp | 5 ++--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index b089342..0f19d2e 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -277,21 +277,16 @@ endif() # === export === if(LINUX) - set(ruisapp_export_targets - ${name}-opengl-xorg - ${name}-opengles-xorg - ${name}-opengl-wayland - ${name}-opengles-wayland - ${name}-opengl-sdl - ${name}-opengles-sdl - ) - list(APPEND ruisapp_export_targets - ${name}-vulkan-xorg - ${name}-vulkan-wayland - ) myci_export( TARGETS - ${ruisapp_export_targets} + ${name}-opengl-xorg + ${name}-opengles-xorg + ${name}-opengl-wayland + ${name}-opengles-wayland + ${name}-opengl-sdl + ${name}-opengles-sdl + ${name}-vulkan-xorg + ${name}-vulkan-wayland ) elseif(ruisapp_build_opengl_variant AND ruisapp_build_opengles_variant) myci_export( diff --git a/tests/app/src/text_input_window.cpp b/tests/app/src/text_input_window.cpp index 924da04..c8720e7 100644 --- a/tests/app/src/text_input_window.cpp +++ b/tests/app/src/text_input_window.cpp @@ -85,9 +85,8 @@ utki::shared_ref make_text_input_window(const utki::shared_ref Date: Fri, 20 Nov 2026 23:43:49 +0200 Subject: [PATCH 09/22] Update for surface_provider rename and vulkan_device public ctor - Use make_shared for vulkan_device (ctor is now public) - Update surface_provider references to surface - Extract make_vulkan_device() helper (make_shared doesn't work with brace-enclosed initializer lists) --- .../glue/linux/wayland/application.cxx | 2 +- .../glue/linux/wayland/application.hxx | 23 ++++++-- src/ruisapp/glue/linux/xorg/glue.cxx | 56 +++++++++++-------- 3 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 0391e96..18cc83a 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -231,7 +231,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window sci.surface = wl_surf; VkSurfaceKHR vk_surf = VK_NULL_HANDLE; vkCreateWaylandSurfaceKHR(vdev.get_instance(), &sci, nullptr, &vk_surf); - auto sp = utki::make_shared(vdev, vk_surf); + auto sp = utki::make_shared(vdev, vk_surf); auto vulkan_factory_ref = utki::dynamic_reference_cast(this->render_factory); return utki::make_shared(win_ref, sp, vulkan_factory_ref); diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 943481b..32c5e5b 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -100,6 +100,21 @@ private: } // namespace namespace { +#ifdef RUISAPP_RENDER_VULKAN +const utki::shared_ref make_vulkan_device() +{ + std::vector instance_extensions = { + VK_KHR_SURFACE_EXTENSION_NAME, + VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME + }; + std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + return utki::make_shared( + instance_extensions, // + device_extensions // + ); +} +#endif + class application_glue : public utki::destructable { public: @@ -174,17 +189,13 @@ public: ) ), #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(std::shared_ptr(ruis::render::vulkan::vulkan_device::make( - {std::string(VK_KHR_SURFACE_EXTENSION_NAME), std::string(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) - }, // instance extensions - {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // device extensions - ))), + vulkan_device(make_vulkan_device()), #endif render_factory( #ifdef RUISAPP_RENDER_VULKAN // Vulkan has no "current context" — a null surface is sufficient for the factory (resource creation) utki::make_shared( - utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) + utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 7e751d2..62a1296 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -41,7 +41,7 @@ along with this program. If not, see . # include # include # include -# include +# include #else # error "Unknown graphics API" @@ -64,6 +64,20 @@ using namespace std::string_view_literals; using namespace ruisapp; +#ifdef RUISAPP_RENDER_VULKAN +namespace { +const utki::shared_ref make_vulkan_device() +{ + std::vector instance_extensions = {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_XLIB_SURFACE_EXTENSION_NAME}; + std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + return utki::make_shared( + instance_extensions, // + device_extensions // + ); +} +} // namespace +#endif + namespace { class app_window : public ruisapp::window { @@ -131,20 +145,19 @@ class application_glue : public utki::destructable application_glue(const utki::version_duplet& gl_version) : #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(std::shared_ptr(ruis::render::vulkan::vulkan_device::make( - { - std::string(VK_KHR_SURFACE_EXTENSION_NAME), - std::string(VK_KHR_XLIB_SURFACE_EXTENSION_NAME) - }, // - {std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME)} // - ))), + vulkan_device(make_vulkan_device()), #endif gl_version(gl_version), shared_gl_context_native_window( // utki::make_shared( this->display, // this->gl_version, - ruisapp::window_parameters{.dims = {1, 1}, .title = {}, .fullscreen = false, .taskbar = false}, + ruisapp::window_parameters{ + .dims = {1, 1}, + .title = {}, + .fullscreen = false, + .taskbar = false + }, nullptr ) ), @@ -154,7 +167,7 @@ class application_glue : public utki::destructable // Vulkan has no "current context" — all GPU resources live on the VkDevice and // are inherently shareable. No surface is needed for the shared factory. utki::make_shared( - utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) + utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) @@ -200,17 +213,16 @@ class application_glue : public utki::destructable ); #ifdef RUISAPP_RENDER_VULKAN - auto win_surface_provider = - utki::make_shared(this->vulkan_device.get(), [&]() { - VkXlibSurfaceCreateInfoKHR ci{}; - ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; - ci.dpy = this->display.get().xorg_display.display; - ci.window = ruis_native_window.get().get_id(); - VkSurfaceKHR surface = VK_NULL_HANDLE; - // VU_CHECK: Vulkan error checking macro from ruis/render/vulkan/vulkan.hpp - VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device.get().get_instance(), &ci, nullptr, &surface)); - return surface; - }()); + auto win_surface = utki::make_shared(this->vulkan_device.get(), [&]() { + VkXlibSurfaceCreateInfoKHR ci{}; + ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; + ci.dpy = this->display.get().xorg_display.display; + ci.window = ruis_native_window.get().get_id(); + VkSurfaceKHR surface = VK_NULL_HANDLE; + // VU_CHECK: Vulkan error checking macro from ruis/render/vulkan/vulkan.hpp + VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device.get().get_instance(), &ci, nullptr, &surface)); + return surface; + }()); auto vulkan_factory_ref = utki::dynamic_reference_cast(this->render_factory); #endif @@ -231,7 +243,7 @@ class application_glue : public utki::destructable // because it accesses Vulkan-specific members (e.g. all_textures, pipeline caches). utki::make_shared( ruis_native_window, // - win_surface_provider, // + win_surface, // vulkan_factory_ref ), #endif From 7b975a03a959d245421c1b0d43fc8e6f94be5e09 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 00:15:13 +0200 Subject: [PATCH 10/22] Update for vulkan_device to device rename --- src/ruisapp/glue/linux/wayland/application.cxx | 2 +- src/ruisapp/glue/linux/wayland/application.hxx | 10 +++++----- src/ruisapp/glue/linux/wayland/glue.cxx | 2 +- src/ruisapp/glue/linux/xorg/glue.cxx | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 18cc83a..58e480b 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -224,7 +224,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window [&](auto&& win_ref) -> utki::shared_ref { auto wl_surf = win_ref.get().get_wl_surface(); auto wl_disp = win_ref.get().get_wl_display(); - auto& vdev = this->vulkan_device.get(); + auto& vdev = this->device.get(); VkWaylandSurfaceCreateInfoKHR sci{}; sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; sci.display = wl_disp; diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 32c5e5b..041735f 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -101,14 +101,14 @@ private: namespace { #ifdef RUISAPP_RENDER_VULKAN -const utki::shared_ref make_vulkan_device() +const utki::shared_ref make_device() { std::vector instance_extensions = { VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME }; std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; - return utki::make_shared( + return utki::make_shared( instance_extensions, // device_extensions // ); @@ -145,7 +145,7 @@ public: #ifdef RUISAPP_RENDER_VULKAN // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. - const utki::shared_ref vulkan_device; + const utki::shared_ref device; #endif const utki::shared_ref render_factory; const utki::shared_ref common_shaders; @@ -189,13 +189,13 @@ public: ) ), #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(make_vulkan_device()), + device(make_device()), #endif render_factory( #ifdef RUISAPP_RENDER_VULKAN // Vulkan has no "current context" — a null surface is sufficient for the factory (resource creation) utki::make_shared( - utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) + utki::make_shared(this->device.get(), VK_NULL_HANDLE) ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) diff --git a/src/ruisapp/glue/linux/wayland/glue.cxx b/src/ruisapp/glue/linux/wayland/glue.cxx index ef5b73d..5806b15 100644 --- a/src/ruisapp/glue/linux/wayland/glue.cxx +++ b/src/ruisapp/glue/linux/wayland/glue.cxx @@ -38,7 +38,7 @@ along with this program. If not, see . # include # include # include -# include +# include #elif defined(RUISAPP_RENDER_OPENGLES) # include diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 62a1296..711cdba 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -40,7 +40,7 @@ along with this program. If not, see . # include # include # include -# include +# include # include #else @@ -66,11 +66,11 @@ using namespace ruisapp; #ifdef RUISAPP_RENDER_VULKAN namespace { -const utki::shared_ref make_vulkan_device() +const utki::shared_ref make_device() { std::vector instance_extensions = {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_XLIB_SURFACE_EXTENSION_NAME}; std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; - return utki::make_shared( + return utki::make_shared( instance_extensions, // device_extensions // ); @@ -115,7 +115,7 @@ class application_glue : public utki::destructable #ifdef RUISAPP_RENDER_VULKAN // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. - const utki::shared_ref vulkan_device; + const utki::shared_ref device; #endif utki::version_duplet gl_version; @@ -145,7 +145,7 @@ class application_glue : public utki::destructable application_glue(const utki::version_duplet& gl_version) : #ifdef RUISAPP_RENDER_VULKAN - vulkan_device(make_vulkan_device()), + device(make_device()), #endif gl_version(gl_version), shared_gl_context_native_window( // @@ -167,7 +167,7 @@ class application_glue : public utki::destructable // Vulkan has no "current context" — all GPU resources live on the VkDevice and // are inherently shareable. No surface is needed for the shared factory. utki::make_shared( - utki::make_shared(this->vulkan_device.get(), VK_NULL_HANDLE) + utki::make_shared(this->device.get(), VK_NULL_HANDLE) ) #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(this->shared_gl_context_native_window) @@ -213,14 +213,14 @@ class application_glue : public utki::destructable ); #ifdef RUISAPP_RENDER_VULKAN - auto win_surface = utki::make_shared(this->vulkan_device.get(), [&]() { + auto win_surface = utki::make_shared(this->device.get(), [&]() { VkXlibSurfaceCreateInfoKHR ci{}; ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; ci.dpy = this->display.get().xorg_display.display; ci.window = ruis_native_window.get().get_id(); VkSurfaceKHR surface = VK_NULL_HANDLE; // VU_CHECK: Vulkan error checking macro from ruis/render/vulkan/vulkan.hpp - VU_CHECK(vkCreateXlibSurfaceKHR(this->vulkan_device.get().get_instance(), &ci, nullptr, &surface)); + VU_CHECK(vkCreateXlibSurfaceKHR(this->device.get().get_instance(), &ci, nullptr, &surface)); return surface; }()); auto vulkan_factory_ref = From fd112820f91ec266f364f3fb1691468c6007ecea Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 00:39:32 +0200 Subject: [PATCH 11/22] Replace VU_CHECK macro with check_vk_result direct call --- src/ruisapp/glue/linux/xorg/glue.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 711cdba..5ff6fd7 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -219,8 +219,8 @@ class application_glue : public utki::destructable ci.dpy = this->display.get().xorg_display.display; ci.window = ruis_native_window.get().get_id(); VkSurfaceKHR surface = VK_NULL_HANDLE; - // VU_CHECK: Vulkan error checking macro from ruis/render/vulkan/vulkan.hpp - VU_CHECK(vkCreateXlibSurfaceKHR(this->device.get().get_instance(), &ci, nullptr, &surface)); + + ::ruis::render::vulkan::check_vk_result(vkCreateXlibSurfaceKHR(this->device.get().get_instance(), &ci, nullptr, &surface), "vkCreateXlibSurfaceKHR"); return surface; }()); auto vulkan_factory_ref = From 302270754e4db60e78dbbad4c7ea42710c8aaaf1 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 00:52:58 +0200 Subject: [PATCH 12/22] Split opengl_window.hxx into separate OpenGL and GLES files; refactor Wayland window to base + per-API leaves - Split opengl_window.hxx into opengl_window.hxx (GLX) and opengles_window.hxx (EGL) - Create wayland_window.hxx (base), opengles_window.hxx (GLES leaf), vulkan_window.hxx (Vulkan leaf) - Update includes and constructor signatures --- .../glue/linux/wayland/application.cxx | 7 + .../glue/linux/wayland/application.hxx | 12 +- .../glue/linux/wayland/opengles_window.hxx | 154 +++++++++++++++ .../glue/linux/wayland/vulkan_window.hxx | 75 ++++++++ .../glue/linux/wayland/wayland_window.hxx | 171 +++++++++++++++++ src/ruisapp/glue/linux/wayland/window.cxx | 4 +- src/ruisapp/glue/linux/xorg/glue.cxx | 11 +- src/ruisapp/glue/linux/xorg/opengl_window.hxx | 167 +--------------- .../glue/linux/xorg/opengles_window.hxx | 178 ++++++++++++++++++ 9 files changed, 614 insertions(+), 165 deletions(-) create mode 100644 src/ruisapp/glue/linux/wayland/opengles_window.hxx create mode 100644 src/ruisapp/glue/linux/wayland/vulkan_window.hxx create mode 100644 src/ruisapp/glue/linux/wayland/wayland_window.hxx create mode 100644 src/ruisapp/glue/linux/xorg/opengles_window.hxx diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 58e480b..aced038 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -202,12 +202,19 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window { utki::logcat_debug("application_glue::make_window(): enter", '\n'); +#ifdef RUISAPP_RENDER_VULKAN + auto ruis_native_window = utki::make_shared( + this->display, // + window_params + ); +#else auto ruis_native_window = utki::make_shared( this->display, // this->gl_version, window_params, &this->shared_gl_context_native_window.get() ); +#endif auto ruis_context = utki::make_shared(ruis::context::parameters{ .post_to_ui_thread_function = diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 041735f..f2d3769 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -30,7 +30,11 @@ along with this program. If not, see . #include "../../../window.hpp" #include "../../unix_common.hxx" -#include "window.hxx" +#ifdef RUISAPP_RENDER_VULKAN +# include "vulkan_window.hxx" +#else +# include "opengles_window.hxx" +#endif namespace { class app_window : public ruisapp::window @@ -177,6 +181,9 @@ public: waitable(this->display.get().wayland_display), gl_version(gl_version), shared_gl_context_native_window( // +#ifdef RUISAPP_RENDER_VULKAN + nullptr // Vulkan doesn't need a shared GL context window +#else utki::make_shared( this->display, // this->gl_version, @@ -184,9 +191,10 @@ public: .dims = {1, 1}, .title = {}, .fullscreen = false - }, +}, nullptr // no shared gl context ) +#endif ), #ifdef RUISAPP_RENDER_VULKAN device(make_device()), diff --git a/src/ruisapp/glue/linux/wayland/opengles_window.hxx b/src/ruisapp/glue/linux/wayland/opengles_window.hxx new file mode 100644 index 0000000..de332fa --- /dev/null +++ b/src/ruisapp/glue/linux/wayland/opengles_window.hxx @@ -0,0 +1,154 @@ +/* +ruisapp - ruis GUI adaptation layer + +Copyright (C) 2016-2025 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +#pragma once + +#include // Wayland EGL MUST be included before EGL headers + +#include "../../egl_utils.hxx" + +#include "wayland_window.hxx" + +namespace { +// OpenGL ES-specific native window for Wayland. +class native_window : public wayland_window +{ + egl_config_wrapper egl_config; + egl_context_wrapper egl_context; + // An offscreen pbuffer surface used as a fallback for the shared GL context + // when no real window surface is available (e.g. during resource loading). + std::optional egl_dummy_surface; + std::optional egl_surface; + +public: + native_window( + utki::shared_ref display, + const utki::version_duplet& gl_version, + const ruisapp::window_parameters& window_params, + native_window* shared_gl_context_native_window + ) : + wayland_window( + std::move(display), // + window_params + ), + egl_config( + this->display.get().egl_display, // + gl_version, + window_params + ), + egl_context( + this->display.get().egl_display, // + gl_version, + this->egl_config, + shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT + ) + {} + + void create_egl_surface() + { + utki::assert(!this->is_egl_surface_created(), [](auto& o) { + o << "create_egl_surface() must not be called more than once"; + }); + + this->egl_surface.emplace( + this->display.get().egl_display, // + this->egl_config, + this->wayland_egl_window.window + ); + + // The rendering context was made current with EGL_NO_SURFACE in its constructor, + // before the surface existed. Now that the surface exists, re-bind the context to + // it, so that rendering (and the initial eglSwapBuffers) actually reaches the + // window's buffer. Without this, context::apply() would skip the bind (the context + // is already "current" on the context stack) and the swap would fail. + this->bind_rendering_context(); + } + + bool is_egl_surface_created() const noexcept + { + return this->egl_surface.has_value(); + } + + void swap_frame_buffers() override + { + if (this->egl_surface.has_value()) { + this->egl_surface.value().swap_frame_buffers(); + } + } + + void bind_rendering_context() override + { + auto& egl_display = this->display.get().egl_display; + + if (this->egl_surface.has_value()) { + if (eglMakeCurrent( + egl_display.display, + this->egl_surface.value().surface, + this->egl_surface.value().surface, + this->egl_context.context + ) == EGL_FALSE) + { + throw std::runtime_error("eglMakeCurrent() failed"); + } + } else { + if (egl_display.extensions.get(egl::extension::khr_surfaceless_context)) { + eglMakeCurrent( + egl_display.display, // + EGL_NO_SURFACE, + EGL_NO_SURFACE, + this->egl_context.context + ); + } else { + // KHR_surfaceless_context EGL extension is not available, create a dummy pbuffer surface to make the context current + if (!this->egl_dummy_surface.has_value()) { + this->egl_dummy_surface.emplace( + egl_display, // + this->egl_config + ); + } + eglMakeCurrent( + egl_display.display, // + this->egl_dummy_surface.value().surface, + this->egl_dummy_surface.value().surface, + this->egl_context.context + ); + } + } + } + + bool is_rendering_context_bound() const noexcept override + { + return eglGetCurrentContext() == this->egl_context.context; + } + + void set_vsync_enabled_internal(bool enabled) override + { + utki::assert( + [this]() { + return this->is_rendering_context_bound(); + }, + SL + ); + + this->egl_context.set_vsync_enabled(enabled); + } +}; +} // namespace diff --git a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx new file mode 100644 index 0000000..5b230f4 --- /dev/null +++ b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx @@ -0,0 +1,75 @@ +/* +ruisapp - ruis GUI adaptation layer + +Copyright (C) 2016-2025 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +#pragma once + +#include +#include + +#include "wayland_window.hxx" + +namespace { +// Vulkan-specific native window for Wayland. +class native_window : public wayland_window +{ +public: + // Callback for presenting the Vulkan frame. Set by the application glue + // (see application.cxx make_window) because the native window is graphics-agnostic. + std::function present_func; + + native_window( + utki::shared_ref display, + const ruisapp::window_parameters& window_params + ) : + wayland_window( + std::move(display), // + window_params + ) + {} + + void swap_frame_buffers() override + { + // For Vulkan, the present is handled by the Vulkan context + // through the present_func callback. + if (this->present_func) { + this->present_func(); + } + } + + void bind_rendering_context() override + { + // For Vulkan, there's no concept of binding a rendering context. + // The Vulkan context is thread-safe and doesn't need to be made current. + } + + bool is_rendering_context_bound() const noexcept override + { + // For Vulkan, the context is always "bound" (thread-safe) + return true; + } + + void set_vsync_enabled_internal(bool enabled) override + { + (void)enabled; + // VSync is handled by the Vulkan present mode + } +}; +} // namespace diff --git a/src/ruisapp/glue/linux/wayland/wayland_window.hxx b/src/ruisapp/glue/linux/wayland/wayland_window.hxx new file mode 100644 index 0000000..26a7b9a --- /dev/null +++ b/src/ruisapp/glue/linux/wayland/wayland_window.hxx @@ -0,0 +1,171 @@ +/* +ruisapp - ruis GUI adaptation layer + +Copyright (C) 2016-2025 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +#pragma once + +#include + +#include "display.hxx" +#include "wayland_egl_window.hxx" +#include "wayland_surface.hxx" +#include "xdg_surface.hxx" +#include "xdg_toplevel.hxx" + +namespace { +// Base class for the Wayland native window. Contains the common Wayland +// surface, xdg-toplevel, and cursor handling. The graphics-specific code +// (EGL or Vulkan) is in the derived leaf classes. +class wayland_window : public ruis::render::native_window +{ +protected: + const utki::shared_ref display; + wayland_surface_wrapper wayland_surface; + xdg_surface_wrapper xdg_surface; + xdg_toplevel_wrapper xdg_toplevel; + wayland_egl_window_wrapper wayland_egl_window; + wayland_surface_wrapper::scale_and_dpi scale_and_dpi; + ruis::mouse_cursor cur_mouse_cursor = ruis::mouse_cursor::arrow; + bool mouse_cursor_visible = true; + +public: + // keep track of current window dimensions for restoring them after fullscreen mode + r4::vector2 cur_window_dims; + + // save window dimentions before makeing it fullscreen to restore the dimentsions + // when window is made non-fullscreen again + r4::vector2 pre_fullscreen_win_dims = this->cur_window_dims; + + using window_id_type = const wl_surface*; + + const unsigned sequence_number = []() { + static unsigned next_sequence_number = 0; + auto ret = next_sequence_number; + ++next_sequence_number; + return ret; + }(); + + wayland_window(utki::shared_ref display, const ruisapp::window_parameters& window_params) : + display(std::move(display)), + wayland_surface(this->display.get().wayland_compositor), + xdg_surface( + this->wayland_surface, // + this->display.get().xdg_wm_base + ), + xdg_toplevel( + this->wayland_surface, // + this->xdg_surface, + window_params + ), + wayland_egl_window( + this->wayland_surface, // + window_params.dims + ), + cur_window_dims(window_params.dims) + { + utki::log_debug([](auto& o) { + o << "wayland_window constructed" << std::endl; + }); + } + + void resize(const r4::vector2& dims); + + ruis::real get_scale() const noexcept + { + return ruis::real(this->scale_and_dpi.scale); + } + + ruis::real get_dpi() const noexcept + { + return this->scale_and_dpi.dpi; + } + + window_id_type get_id() const noexcept + { + return this->wayland_surface.surface; + } + + // get the raw wl_surface pointer for Vulkan surface creation + wl_surface* get_wl_surface() const noexcept + { + return this->wayland_surface.surface; + } + + // get the raw wl_display pointer for Vulkan surface creation + wl_display* get_wl_display() const noexcept + { + return this->display.get().wayland_display.display; + } + + r4::vector2 get_dims() const noexcept override + { + return this->cur_window_dims * this->scale_and_dpi.scale; + } + + void set_fullscreen_internal(bool enable) override + { + if (enable) { + this->pre_fullscreen_win_dims = this->cur_window_dims; + utki::log_debug([&](auto& o) { + o << " old win dims = " << std::dec << this->pre_fullscreen_win_dims << std::endl; + }); + xdg_toplevel_set_fullscreen( + this->xdg_toplevel.toplevel, // + nullptr // output + ); + } else { + xdg_toplevel_unset_fullscreen(this->xdg_toplevel.toplevel); + } + } + + void update_mouse_cursor() + { + auto& wayland_pointer = this->display.get().wayland_seat.wayland_pointer; + + if (this->mouse_cursor_visible) { + wayland_pointer.set_cursor(this->cur_mouse_cursor); + } else { + wayland_pointer.set_cursor(ruis::mouse_cursor::none); + } + } + + void set_mouse_cursor_visible(bool visible) override + { + this->mouse_cursor_visible = visible; + this->update_mouse_cursor(); + } + + void set_mouse_cursor(ruis::mouse_cursor cursor) override + { + this->cur_mouse_cursor = cursor; + this->update_mouse_cursor(); + } + + wl_callback* make_frame_callback() + { + return wl_surface_frame(this->wayland_surface.surface); + } + + void mark_dirty() + { + this->wayland_surface.damage(this->cur_window_dims.to()); + } +}; +} // namespace diff --git a/src/ruisapp/glue/linux/wayland/window.cxx b/src/ruisapp/glue/linux/wayland/window.cxx index 3b9cd20..a7f4d64 100644 --- a/src/ruisapp/glue/linux/wayland/window.cxx +++ b/src/ruisapp/glue/linux/wayland/window.cxx @@ -19,9 +19,9 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ -#include "window.hxx" +#include "wayland_window.hxx" -void native_window::resize(const r4::vector2& dims) +void wayland_window::resize(const r4::vector2& dims) { utki::logcat_debug("native_window::resize(): dims = ", dims, '\n'); this->cur_window_dims = dims; diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 5ff6fd7..a2b1938 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -55,8 +55,10 @@ along with this program. If not, see . #include "key_code_map.hxx" #ifdef RUISAPP_RENDER_VULKAN # include "vulkan_window.hxx" -#else +#elif defined(RUISAPP_RENDER_OPENGL) # include "opengl_window.hxx" +#elif defined(RUISAPP_RENDER_OPENGLES) +# include "opengles_window.hxx" #endif using namespace std::string_literals; @@ -219,8 +221,11 @@ class application_glue : public utki::destructable ci.dpy = this->display.get().xorg_display.display; ci.window = ruis_native_window.get().get_id(); VkSurfaceKHR surface = VK_NULL_HANDLE; - - ::ruis::render::vulkan::check_vk_result(vkCreateXlibSurfaceKHR(this->device.get().get_instance(), &ci, nullptr, &surface), "vkCreateXlibSurfaceKHR"); + + ::ruis::render::vulkan::check_vk_result( + vkCreateXlibSurfaceKHR(this->device.get().get_instance(), &ci, nullptr, &surface), + "vkCreateXlibSurfaceKHR" + ); return surface; }()); auto vulkan_factory_ref = diff --git a/src/ruisapp/glue/linux/xorg/opengl_window.hxx b/src/ruisapp/glue/linux/xorg/opengl_window.hxx index 0627c5a..f8c99a6 100644 --- a/src/ruisapp/glue/linux/xorg/opengl_window.hxx +++ b/src/ruisapp/glue/linux/xorg/opengl_window.hxx @@ -1,55 +1,29 @@ -/* -ruisapp - ruis GUI adaptation layer - -Copyright (C) 2016-2025 Ivan Gagis - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -/* ================ LICENSE END ================ */ - -// The OpenGL/OpenGL ES native window. Inherits from xorg_window (the -// graphics-agnostic base) and adds the GL-specific parts: FBConfig/visual -// selection, GL context creation, swap_frame_buffers, bind_rendering_context, -// etc. +// this file is part of ruisapp application framework // -// The gl_visual_info base class holds the computed FBConfig + X11 visual. It is +// this file is licensed under the terms of the MIT license, which can be found in the LICENSE file +// +// SPDX-License-Identifier: MIT + +// OpenGL-specific native window implementation. +// The gl_visual_info base class holds the computed GLXFBConfig + X11 visual. It is // inherited before xorg_window so that the base class can use the visual when // creating the X11 window (base classes are initialized before members, so the // visual cannot be a member of the derived class). +#include #include #include -#if defined(RUISAPP_RENDER_OPENGL) -# include -#elif defined(RUISAPP_RENDER_OPENGLES) -# include -# include "../../egl_utils.hxx" -#endif - #include "xorg_window.hxx" namespace { -// Holds the computed FBConfig + X11 visual. This is a base class of native_window +// Holds the computed GLXFBConfig + X11 visual. This is a base class of native_window // (inherited before xorg_window) so that the visual is available when the // xorg_window base creates the X11 window. class gl_visual_info { public: -#if defined(RUISAPP_RENDER_OPENGL) GLXFBConfig fb_config; XVisualInfo* const visual_info; xorg_visual xvisual; @@ -186,71 +160,8 @@ public: { XFree(this->visual_info); } - -#elif defined(RUISAPP_RENDER_OPENGLES) - const egl_config_wrapper egl_config; - XVisualInfo* const visual_info; - xorg_visual xvisual; - - gl_visual_info( - display_wrapper& display, // - const utki::version_duplet& gl_version, - const ruisapp::window_parameters& window_params - ) : - egl_config( - display.egl_display, // - gl_version, - window_params - ), - visual_info([&]() { - EGLint vid = 0; - - if (!eglGetConfigAttrib( - display.egl_display.display, // - this->egl_config.config, - EGL_NATIVE_VISUAL_ID, - &vid - )) - { - throw std::runtime_error("eglGetConfigAttrib() failed"); - } - - int num_visuals = 0; - XVisualInfo vis_template; - vis_template.visualid = vid; - auto visual_info = XGetVisualInfo( - display.xorg_display.display, // - VisualIDMask, - &vis_template, - &num_visuals - ); - if (!visual_info) { - throw std::runtime_error("XGetVisualInfo() failed"); - } - return visual_info; - }()), - xvisual([this]() { - return xorg_visual{ - .visual = this->visual_info->visual, // - .depth = unsigned(this->visual_info->depth) - }; - }()) - {} - - gl_visual_info(const gl_visual_info&) = delete; - gl_visual_info& operator=(const gl_visual_info&) = delete; - - gl_visual_info(gl_visual_info&&) = delete; - gl_visual_info& operator=(gl_visual_info&&) = delete; - - ~gl_visual_info() - { - XFree(this->visual_info); - } -#endif }; -#if defined(RUISAPP_RENDER_OPENGL) // Creates the GLX context struct glx_context_wrapper { display_wrapper& display; @@ -338,11 +249,6 @@ struct glx_context_wrapper { )); if (!glx_create_context_attribs_arb) { - // this should not happen since we checked extension presence, and - // anyway, glXGetProcAddressARB() never returns nullptr according to - // https://dri.freedesktop.org/wiki/glXGetProcAddressNeverReturnsNULL/ - // so, this check for null is just in case future version of GLX may - // return null throw std::runtime_error("glXCreateContextAttribsARB() not found"); } @@ -425,7 +331,6 @@ struct glx_context_wrapper { ); } }; -#endif class native_window : public gl_visual_info, public xorg_window { @@ -447,7 +352,6 @@ public: shared_gl_context_native_window, this->xvisual ), -#if defined(RUISAPP_RENDER_OPENGL) glx_context( this->display.get(), // this->visual_info, @@ -455,21 +359,7 @@ public: this->fb_config, shared_gl_context_native_window ? shared_gl_context_native_window->glx_context.context : nullptr ) -#elif defined(RUISAPP_RENDER_OPENGLES) - egl_surface( - this->display.get().egl_display, // - this->egl_config, - this->window.window - ), - egl_context( - this->display.get().egl_display, // - gl_version, - this->egl_config, - shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT - ) -#endif { -#if defined(RUISAPP_RENDER_OPENGL) // if there is no any GL context current, then set this one before calling glewInit() if (glXGetCurrentContext() == nullptr) { this->bind_rendering_context(); @@ -477,7 +367,6 @@ public: if (glewInit() != GLEW_OK) { throw std::runtime_error("GLEW initialization failed"); } -#endif } native_window(const native_window&) = delete; @@ -490,37 +379,20 @@ public: void bind_rendering_context() override { -#ifdef RUISAPP_RENDER_OPENGL glXMakeCurrent( this->display.get().xorg_display.display, // this->window.window, this->glx_context.context ); -#elif defined(RUISAPP_RENDER_OPENGLES) - if (eglMakeCurrent( - this->display.get().egl_display.display, - this->egl_surface.surface, - this->egl_surface.surface, - this->egl_context.context - ) == EGL_FALSE) - { - throw std::runtime_error("eglMakeCurrent() failed"); - } -#endif } bool is_rendering_context_bound() const noexcept override { -#ifdef RUISAPP_RENDER_OPENGL return glXGetCurrentContext() == this->glx_context.context; -#elif defined(RUISAPP_RENDER_OPENGLES) - return eglGetCurrentContext() == this->egl_context.context; -#endif } void swap_frame_buffers() override { -#ifdef RUISAPP_RENDER_OPENGL glXSwapBuffers( this->display.get().xorg_display.display, // this->window.window @@ -534,11 +406,6 @@ public: // the effect is not present. glFinish(); } -#elif defined(RUISAPP_RENDER_OPENGLES) - this->egl_surface.swap_frame_buffers(); -#else -# error "Unknown graphics API" -#endif } void set_vsync_enabled_internal(bool enable) override @@ -550,7 +417,6 @@ public: SL ); -#ifdef RUISAPP_RENDER_OPENGL // disable v-sync via swap control extension if (this->glx_context.supported_extensions.get(glx_context_wrapper::glx_extension::glx_ext_swap_control)) { utki::log_debug([](auto& o) { @@ -601,25 +467,10 @@ public: this->display.get().xorg_display.display, // False ); -#elif defined(RUISAPP_RENDER_OPENGLES) - utki::logcat_debug("eglSwapInterval(", enable ? "true" : "false", ")\n"); - if (eglSwapInterval( - this->display.get().egl_display.display, // - enable ? 1 : 0 // swap interval in vsync frames - ) != EGL_TRUE) - { - utki::logcat("WARNING: eglSwapInterval(", enable, ") failed"); - } -#endif } private: -#if defined(RUISAPP_RENDER_OPENGL) const glx_context_wrapper glx_context; -#elif defined(RUISAPP_RENDER_OPENGLES) - egl_surface_wrapper egl_surface; - egl_context_wrapper egl_context; -#endif }; } // namespace diff --git a/src/ruisapp/glue/linux/xorg/opengles_window.hxx b/src/ruisapp/glue/linux/xorg/opengles_window.hxx new file mode 100644 index 0000000..99e0652 --- /dev/null +++ b/src/ruisapp/glue/linux/xorg/opengles_window.hxx @@ -0,0 +1,178 @@ +// this file is part of ruisapp application framework +// +// this file is licensed under the terms of the MIT license, which can be found in the LICENSE file +// +// SPDX-License-Identifier: MIT + +// OpenGL ES-specific native window implementation. +// The gl_visual_info base class holds the computed EGLConfig + X11 visual. It is +// inherited before xorg_window so that the base class can use the visual when +// creating the X11 window (base classes are initialized before members, so the +// visual cannot be a member of the derived class). + +#include +#include +#include + +#include "../../egl_utils.hxx" + +#include "xorg_window.hxx" + +namespace { + +// Holds the computed EGLConfig + X11 visual. This is a base class of native_window +// (inherited before xorg_window) so that the visual is available when the +// xorg_window base creates the X11 window. +class gl_visual_info +{ +public: + const egl_config_wrapper egl_config; + XVisualInfo* const visual_info; + xorg_visual xvisual; + + gl_visual_info( + display_wrapper& display, // + const utki::version_duplet& gl_version, + const ruisapp::window_parameters& window_params + ) : + egl_config( + display.egl_display, // + gl_version, + window_params + ), + visual_info([&]() { + EGLint vid = 0; + + if (!eglGetConfigAttrib( + display.egl_display.display, // + this->egl_config.config, + EGL_NATIVE_VISUAL_ID, + &vid + )) + { + throw std::runtime_error("eglGetConfigAttrib() failed"); + } + + int num_visuals = 0; + XVisualInfo vis_template; + vis_template.visualid = vid; + auto visual_info = XGetVisualInfo( + display.xorg_display.display, // + VisualIDMask, + &vis_template, + &num_visuals + ); + if (!visual_info) { + throw std::runtime_error("XGetVisualInfo() failed"); + } + return visual_info; + }()), + xvisual([this]() { + return xorg_visual{ + .visual = this->visual_info->visual, // + .depth = unsigned(this->visual_info->depth) + }; + }()) + {} + + gl_visual_info(const gl_visual_info&) = delete; + gl_visual_info& operator=(const gl_visual_info&) = delete; + + gl_visual_info(gl_visual_info&&) = delete; + gl_visual_info& operator=(gl_visual_info&&) = delete; + + ~gl_visual_info() + { + XFree(this->visual_info); + } +}; + +class native_window : public gl_visual_info, public xorg_window +{ +public: + native_window( + utki::shared_ref display, // + const utki::version_duplet& gl_version, + const ruisapp::window_parameters& window_params, + const native_window* shared_gl_context_native_window + ) : + gl_visual_info( + display.get(), // + gl_version, + window_params + ), + xorg_window( + display, // + window_params, + shared_gl_context_native_window, + this->xvisual + ), + egl_surface( + this->display.get().egl_display, // + this->egl_config, + this->window.window + ), + egl_context( + this->display.get().egl_display, // + gl_version, + this->egl_config, + shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT + ) + {} + + native_window(const native_window&) = delete; + native_window& operator=(const native_window&) = delete; + + native_window(native_window&&) = delete; + native_window& operator=(native_window&&) = delete; + + ~native_window() override = default; + + void bind_rendering_context() override + { + if (eglMakeCurrent( + this->display.get().egl_display.display, + this->egl_surface.surface, + this->egl_surface.surface, + this->egl_context.context + ) == EGL_FALSE) + { + throw std::runtime_error("eglMakeCurrent() failed"); + } + } + + bool is_rendering_context_bound() const noexcept override + { + return eglGetCurrentContext() == this->egl_context.context; + } + + void swap_frame_buffers() override + { + this->egl_surface.swap_frame_buffers(); + } + + void set_vsync_enabled_internal(bool enable) override + { + utki::assert( + [this]() { + return this->is_rendering_context_bound(); + }, + SL + ); + + utki::logcat_debug("eglSwapInterval(", enable ? "true" : "false", ")\n"); + if (eglSwapInterval( + this->display.get().egl_display.display, // + enable ? 1 : 0 // swap interval in vsync frames + ) != EGL_TRUE) + { + utki::logcat("WARNING: eglSwapInterval(", enable, ") failed"); + } + } + +private: + egl_surface_wrapper egl_surface; + egl_context_wrapper egl_context; +}; + +} // namespace From 2c2fa80d5a03c003e0fe1a088a2f7f55c8867c9f Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:19:17 +0200 Subject: [PATCH 13/22] Address review comments: rename native_window classes, use context* instead of present_func --- .../glue/linux/wayland/application.cxx | 17 +- .../glue/linux/wayland/application.hxx | 16 +- .../glue/linux/wayland/opengles_window.hxx | 6 +- .../glue/linux/wayland/vulkan_window.hxx | 16 +- src/ruisapp/glue/linux/wayland/window.hxx | 328 ------------------ src/ruisapp/glue/linux/xorg/glue.cxx | 38 +- src/ruisapp/glue/linux/xorg/opengl_window.hxx | 17 +- .../glue/linux/xorg/opengles_window.hxx | 16 +- src/ruisapp/glue/linux/xorg/vulkan_window.hxx | 26 +- 9 files changed, 76 insertions(+), 404 deletions(-) delete mode 100644 src/ruisapp/glue/linux/wayland/window.hxx diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index aced038..92a08ab 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -203,12 +203,12 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window utki::logcat_debug("application_glue::make_window(): enter", '\n'); #ifdef RUISAPP_RENDER_VULKAN - auto ruis_native_window = utki::make_shared( + auto ruis_native_window = utki::make_shared( this->display, // window_params ); #else - auto ruis_native_window = utki::make_shared( + auto ruis_native_window = utki::make_shared( this->display, // this->gl_version, window_params, @@ -271,15 +271,10 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window #ifdef RUISAPP_RENDER_VULKAN { - // The Vulkan window presents frames via a callback (present_func) set here, - // because the native window is graphics-agnostic and does not know about the renderer. - // Use a raw pointer to avoid a circular shared_ref that would prevent destruction. - auto* win_ptr = &ruisapp_window.get(); - ruisapp_window.get().ruis_native_window.get().present_func = [win_ptr]() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") - auto& ctx = static_cast(win_ptr->gui.context.get().ren().ctx()); - ctx.present(); - }; + // set the owning Vulkan context so that swap_frame_buffers() can call present() + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") + ruisapp_window.get().ruis_native_window.get().owner_context = + &static_cast(ruisapp_window.get().gui.context.get().ren().ctx()); } #endif diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index f2d3769..28d632f 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -32,8 +32,10 @@ along with this program. If not, see . #ifdef RUISAPP_RENDER_VULKAN # include "vulkan_window.hxx" +using native_window_type = vulkan_native_window; #else # include "opengles_window.hxx" +using native_window_type = opengles_native_window; #endif namespace { @@ -48,11 +50,11 @@ public: bool activated = false; } actual_state; - utki::shared_ref ruis_native_window; + utki::shared_ref ruis_native_window; app_window( utki::shared_ref ruis_context, // - utki::shared_ref ruis_native_window + utki::shared_ref ruis_native_window ) : ruisapp::window(std::move(ruis_context)), ruis_native_window(std::move(ruis_native_window)) @@ -145,7 +147,7 @@ public: const utki::version_duplet gl_version; // TODO: make windowless shared egl context - const utki::shared_ref shared_gl_context_native_window; + const utki::shared_ref shared_gl_context_native_window; #ifdef RUISAPP_RENDER_VULKAN // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. @@ -159,7 +161,7 @@ public: private: std::map< - native_window::window_id_type, // + native_window_type::window_id_type, // utki::shared_ref // > windows; @@ -172,7 +174,7 @@ public: return this->windows; } - native_window::window_id_type get_shared_gl_context_window_id() const noexcept + native_window_type::window_id_type get_shared_gl_context_window_id() const noexcept { return this->shared_gl_context_native_window.get().get_id(); } @@ -184,7 +186,7 @@ public: #ifdef RUISAPP_RENDER_VULKAN nullptr // Vulkan doesn't need a shared GL context window #else - utki::make_shared( + utki::make_shared( this->display, // this->gl_version, ruisapp::window_parameters{ @@ -236,7 +238,7 @@ public: ruisapp::window& make_window(ruisapp::window_parameters window_params); void destroy_window(app_window& w); - app_window* get_window(native_window::window_id_type id) + app_window* get_window(native_window_type::window_id_type id) { auto i = this->windows.find(id); if (i == this->windows.end()) { diff --git a/src/ruisapp/glue/linux/wayland/opengles_window.hxx b/src/ruisapp/glue/linux/wayland/opengles_window.hxx index de332fa..9ddd671 100644 --- a/src/ruisapp/glue/linux/wayland/opengles_window.hxx +++ b/src/ruisapp/glue/linux/wayland/opengles_window.hxx @@ -29,7 +29,7 @@ along with this program. If not, see . namespace { // OpenGL ES-specific native window for Wayland. -class native_window : public wayland_window +class opengles_native_window : public wayland_window { egl_config_wrapper egl_config; egl_context_wrapper egl_context; @@ -39,11 +39,11 @@ class native_window : public wayland_window std::optional egl_surface; public: - native_window( + opengles_native_window( utki::shared_ref display, const utki::version_duplet& gl_version, const ruisapp::window_parameters& window_params, - native_window* shared_gl_context_native_window + opengles_native_window* shared_gl_context_native_window ) : wayland_window( std::move(display), // diff --git a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx index 5b230f4..e61de51 100644 --- a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx @@ -28,14 +28,14 @@ along with this program. If not, see . namespace { // Vulkan-specific native window for Wayland. -class native_window : public wayland_window +class vulkan_native_window : public wayland_window { public: - // Callback for presenting the Vulkan frame. Set by the application glue - // (see application.cxx make_window) because the native window is graphics-agnostic. - std::function present_func; + // pointer to the owning Vulkan context; set by the glue after the context + // is created. swap_frame_buffers() calls present() on it if non-null. + ruis::render::vulkan::context* owner_context = nullptr; - native_window( + vulkan_native_window( utki::shared_ref display, const ruisapp::window_parameters& window_params ) : @@ -47,10 +47,8 @@ public: void swap_frame_buffers() override { - // For Vulkan, the present is handled by the Vulkan context - // through the present_func callback. - if (this->present_func) { - this->present_func(); + if (this->owner_context) { + this->owner_context->present(); } } diff --git a/src/ruisapp/glue/linux/wayland/window.hxx b/src/ruisapp/glue/linux/wayland/window.hxx deleted file mode 100644 index acf3f86..0000000 --- a/src/ruisapp/glue/linux/wayland/window.hxx +++ /dev/null @@ -1,328 +0,0 @@ -/* -ruisapp - ruis GUI adaptation layer - -Copyright (C) 2016-2025 Ivan Gagis - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -/* ================ LICENSE END ================ */ - -#pragma once - -#include - -#ifdef RUISAPP_RENDER_VULKAN -# include -# include -#else -# include // Wayland EGL MUST be included before EGL headers - -# include "../../egl_utils.hxx" -#endif - -#include "display.hxx" -#include "wayland_egl_window.hxx" -#include "wayland_surface.hxx" -#include "xdg_surface.hxx" -#include "xdg_toplevel.hxx" - -namespace { -class native_window : public ruis::render::native_window -{ - const utki::shared_ref display; - - wayland_surface_wrapper wayland_surface; - xdg_surface_wrapper xdg_surface; - xdg_toplevel_wrapper xdg_toplevel; - wayland_egl_window_wrapper wayland_egl_window; - -#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) - egl_config_wrapper egl_config; - egl_context_wrapper egl_context; - // An offscreen pbuffer surface used as a fallback for the shared GL context - // when no real window surface is available (e.g. during resource loading). - std::optional egl_dummy_surface; - std::optional egl_surface; -#endif - - wayland_surface_wrapper::scale_and_dpi scale_and_dpi; - -public: - const unsigned sequence_number = []() { - static unsigned next_sequence_number = 0; - auto ret = next_sequence_number; - ++next_sequence_number; - return ret; - }(); - - // keep track of current window dimensions for restoring them after fullscreen mode - r4::vector2 cur_window_dims; - - // save window dimentions before makeing it fullscreen to restore the dimentsions - // when window is made non-fullscreen again - r4::vector2 pre_fullscreen_win_dims = this->cur_window_dims; - - using window_id_type = const wl_surface*; - - native_window( - utki::shared_ref display, - const utki::version_duplet& gl_version, - const ruisapp::window_parameters& window_params, - native_window* shared_gl_context_native_window - ) : - display(std::move(display)), - wayland_surface(this->display.get().wayland_compositor), - xdg_surface( - this->wayland_surface, // - this->display.get().xdg_wm_base - ), - xdg_toplevel( - this->wayland_surface, // - this->xdg_surface, - window_params - ), - wayland_egl_window( - this->wayland_surface, // - window_params.dims - ), -#if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) - egl_config( - this->display.get().egl_display, // - gl_version, - window_params - ), - egl_context( - this->display.get().egl_display, // - gl_version, - this->egl_config, - shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT - ), -#endif - cur_window_dims(window_params.dims) - { - utki::log_debug([](auto& o) { - o << "native_window constructed" << std::endl; - }); - } - - void resize(const r4::vector2& dims); - - ruis::real get_scale() const noexcept - { - return ruis::real(this->scale_and_dpi.scale); - } - - ruis::real get_dpi() const noexcept - { - return this->scale_and_dpi.dpi; - } - - window_id_type get_id() const noexcept - { - return this->wayland_surface.surface; - } - - // get the raw wl_surface pointer for Vulkan surface creation - wl_surface* get_wl_surface() const noexcept - { - return this->wayland_surface.surface; - } - - // get the raw wl_display pointer for Vulkan surface creation - wl_display* get_wl_display() const noexcept - { - return this->display.get().wayland_display.display; - } - -#ifndef RUISAPP_RENDER_VULKAN - void create_egl_surface() - { - utki::assert(!this->is_egl_surface_created(), [](auto& o) { - o << "create_egl_surface() must not be called more than once"; - }); - - this->egl_surface.emplace( - this->display.get().egl_display, // - this->egl_config, - this->wayland_egl_window.window - ); - - // The rendering context was made current with EGL_NO_SURFACE in its constructor, - // before the surface existed. Now that the surface exists, re-bind the context to - // it, so that rendering (and the initial eglSwapBuffers) actually reaches the - // window's buffer. Without this, context::apply() would skip the bind (the context - // is already "current" on the context stack) and the swap would fail. - this->bind_rendering_context(); - } - - bool is_egl_surface_created() const noexcept - { - return this->egl_surface.has_value(); - } -#endif - - void swap_frame_buffers() override - { -#ifdef RUISAPP_RENDER_VULKAN - // For Vulkan, the present is handled by the Vulkan context - // through the present_func callback. - if (this->present_func) { - this->present_func(); - } -#else - if (this->egl_surface.has_value()) { - this->egl_surface.value().swap_frame_buffers(); - } -#endif - } - - r4::vector2 get_dims() const noexcept override - { - return this->cur_window_dims * this->scale_and_dpi.scale; - } - - void bind_rendering_context() override - { -#ifdef RUISAPP_RENDER_VULKAN - // For Vulkan, there's no concept of binding a rendering context. - // The Vulkan context is thread-safe and doesn't need to be made current. -#else - auto& egl_display = this->display.get().egl_display; - - if (this->egl_surface.has_value()) { - if (eglMakeCurrent( - egl_display.display, - this->egl_surface.value().surface, - this->egl_surface.value().surface, - this->egl_context.context - ) == EGL_FALSE) - { - throw std::runtime_error("eglMakeCurrent() failed"); - } - } else { - if (egl_display.extensions.get(egl::extension::khr_surfaceless_context)) { - eglMakeCurrent( - egl_display.display, // - EGL_NO_SURFACE, - EGL_NO_SURFACE, - this->egl_context.context - ); - } else { - // KHR_surfaceless_context EGL extension is not available, create a dummy pbuffer surface to make the context current - if (!this->egl_dummy_surface.has_value()) { - this->egl_dummy_surface.emplace( - egl_display, // - this->egl_config - ); - } - eglMakeCurrent( - egl_display.display, // - this->egl_dummy_surface.value().surface, - this->egl_dummy_surface.value().surface, - this->egl_context.context - ); - } - } -#endif - } - - bool is_rendering_context_bound() const noexcept override - { -#ifdef RUISAPP_RENDER_VULKAN - // For Vulkan, the context is always "bound" (thread-safe) - return true; -#else - return eglGetCurrentContext() == this->egl_context.context; -#endif - } - - void set_fullscreen_internal(bool enable) override - { - if (enable) { - this->pre_fullscreen_win_dims = this->cur_window_dims; - utki::log_debug([&](auto& o) { - o << " old win dims = " << std::dec << this->pre_fullscreen_win_dims << std::endl; - }); - xdg_toplevel_set_fullscreen( - this->xdg_toplevel.toplevel, // - nullptr // output - ); - } else { - xdg_toplevel_unset_fullscreen(this->xdg_toplevel.toplevel); - } - } - -private: - ruis::mouse_cursor cur_mouse_cursor = ruis::mouse_cursor::arrow; - bool mouse_cursor_visible = true; - -public: -#ifdef RUISAPP_RENDER_VULKAN - // Callback for presenting the Vulkan frame. Set by the application glue - // (see application.cxx make_window) because the native window is graphics-agnostic. - std::function present_func; -#endif - - void update_mouse_cursor() - { - auto& wayland_pointer = this->display.get().wayland_seat.wayland_pointer; - - if (this->mouse_cursor_visible) { - wayland_pointer.set_cursor(this->cur_mouse_cursor); - } else { - wayland_pointer.set_cursor(ruis::mouse_cursor::none); - } - } - - void set_mouse_cursor_visible(bool visible) override - { - this->mouse_cursor_visible = visible; - this->update_mouse_cursor(); - } - - void set_mouse_cursor(ruis::mouse_cursor cursor) override - { - this->cur_mouse_cursor = cursor; - this->update_mouse_cursor(); - } - - void set_vsync_enabled_internal(bool enabled) override - { -#ifdef RUISAPP_RENDER_VULKAN - (void)enabled; - // VSync is handled by the Vulkan present mode -#elif defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) - utki::assert( - [this]() { - return this->is_rendering_context_bound(); - }, - SL - ); - - this->egl_context.set_vsync_enabled(enabled); -#endif - } - - wl_callback* make_frame_callback() - { - return wl_surface_frame(this->wayland_surface.surface); - } - - void mark_dirty() - { - this->wayland_surface.damage(this->cur_window_dims.to()); - } -}; -} // namespace diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index a2b1938..cba8239 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -66,6 +66,14 @@ using namespace std::string_view_literals; using namespace ruisapp; +#ifdef RUISAPP_RENDER_VULKAN +using native_window_type = vulkan_native_window; +#elif defined(RUISAPP_RENDER_OPENGL) +using native_window_type = opengl_native_window; +#elif defined(RUISAPP_RENDER_OPENGLES) +using native_window_type = opengles_native_window; +#endif + #ifdef RUISAPP_RENDER_VULKAN namespace { const utki::shared_ref make_device() @@ -84,11 +92,11 @@ namespace { class app_window : public ruisapp::window { public: - utki::shared_ref ruis_native_window; + utki::shared_ref ruis_native_window; app_window( utki::shared_ref ruis_context, // - utki::shared_ref ruis_native_window + utki::shared_ref ruis_native_window ) : ruisapp::window(std::move(ruis_context)), ruis_native_window(std::move(ruis_native_window)) @@ -121,7 +129,7 @@ class application_glue : public utki::destructable #endif utki::version_duplet gl_version; - utki::shared_ref shared_gl_context_native_window; + utki::shared_ref shared_gl_context_native_window; const utki::shared_ref render_factory; const utki::shared_ref common_shaders; utki::shared_ref common_render_objects; @@ -132,7 +140,7 @@ class application_glue : public utki::destructable private: std::map< - native_window::window_id_type, // + native_window_type::window_id_type, // utki::shared_ref // > windows; @@ -151,7 +159,7 @@ class application_glue : public utki::destructable #endif gl_version(gl_version), shared_gl_context_native_window( // - utki::make_shared( + utki::make_shared( this->display, // this->gl_version, ruisapp::window_parameters{ @@ -207,7 +215,7 @@ class application_glue : public utki::destructable app_window& make_window(ruisapp::window_parameters window_params) { - auto ruis_native_window = utki::make_shared( + auto ruis_native_window = utki::make_shared( this->display, // this->gl_version, window_params, @@ -288,14 +296,10 @@ class application_glue : public utki::destructable { #ifdef RUISAPP_RENDER_VULKAN - // use a raw pointer to avoid a circular shared_ref that would - // prevent the app_window from being destroyed - auto* win_ptr = &ruisapp_window.get(); - nw.present_func = [win_ptr]() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") - auto& ctx = static_cast(win_ptr->gui.context.get().ren().ctx()); - ctx.present(); - }; + // set the owning Vulkan context so that swap_frame_buffers() can call present() + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") + nw.owner_context = + &static_cast(ruisapp_window.get().gui.context.get().ren().ctx()); #endif } @@ -334,7 +338,7 @@ class application_glue : public utki::destructable this->windows.erase(i); } - app_window* get_window(native_window::window_id_type id) + app_window* get_window(native_window_type::window_id_type id) { auto i = this->windows.find(id); if (i == this->windows.end()) { @@ -412,13 +416,13 @@ ruis::mouse_button button_number_to_enum(unsigned number) class key_event_unicode_provider : public ruis::gui::input_string_provider { - const native_window& window; + const native_window_type& window; // NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive") XKeyEvent& event; public: key_event_unicode_provider( - const native_window& window, // + const native_window_type& window, // XKeyEvent& event ) : window(window), diff --git a/src/ruisapp/glue/linux/xorg/opengl_window.hxx b/src/ruisapp/glue/linux/xorg/opengl_window.hxx index f8c99a6..272f65c 100644 --- a/src/ruisapp/glue/linux/xorg/opengl_window.hxx +++ b/src/ruisapp/glue/linux/xorg/opengl_window.hxx @@ -332,14 +332,15 @@ struct glx_context_wrapper { } }; -class native_window : public gl_visual_info, public xorg_window +// review: rename to opengl_native_window, do simnilar with opengles_native_window and vulkan_native_window, also in wayland implementation +class opengl_native_window : public gl_visual_info, public xorg_window { public: - native_window( + opengl_native_window( utki::shared_ref display, // const utki::version_duplet& gl_version, const ruisapp::window_parameters& window_params, - const native_window* shared_gl_context_native_window + opengl_native_window* shared_gl_context_native_window ) : gl_visual_info( display.get(), // @@ -369,13 +370,13 @@ public: } } - native_window(const native_window&) = delete; - native_window& operator=(const native_window&) = delete; + opengl_native_window(const opengl_native_window&) = delete; + opengl_native_window& operator=(const opengl_native_window&) = delete; - native_window(native_window&&) = delete; - native_window& operator=(native_window&&) = delete; + opengl_native_window(opengl_native_window&&) = delete; + opengl_native_window& operator=(opengl_native_window&&) = delete; - ~native_window() override = default; + ~opengl_native_window() override = default; void bind_rendering_context() override { diff --git a/src/ruisapp/glue/linux/xorg/opengles_window.hxx b/src/ruisapp/glue/linux/xorg/opengles_window.hxx index 99e0652..497149d 100644 --- a/src/ruisapp/glue/linux/xorg/opengles_window.hxx +++ b/src/ruisapp/glue/linux/xorg/opengles_window.hxx @@ -87,14 +87,14 @@ public: } }; -class native_window : public gl_visual_info, public xorg_window +class opengles_native_window : public gl_visual_info, public xorg_window { public: - native_window( + opengles_native_window( utki::shared_ref display, // const utki::version_duplet& gl_version, const ruisapp::window_parameters& window_params, - const native_window* shared_gl_context_native_window + opengles_native_window* shared_gl_context_native_window ) : gl_visual_info( display.get(), // @@ -120,13 +120,13 @@ public: ) {} - native_window(const native_window&) = delete; - native_window& operator=(const native_window&) = delete; + opengles_native_window(const opengles_native_window&) = delete; + opengles_native_window& operator=(const opengles_native_window&) = delete; - native_window(native_window&&) = delete; - native_window& operator=(native_window&&) = delete; + opengles_native_window(opengles_native_window&&) = delete; + opengles_native_window& operator=(opengles_native_window&&) = delete; - ~native_window() override = default; + ~opengles_native_window() override = default; void bind_rendering_context() override { diff --git a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx index 3a1e311..20db89d 100644 --- a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx @@ -34,18 +34,18 @@ along with this program. If not, see . namespace { -class native_window : public xorg_window +class vulkan_native_window : public xorg_window { public: - // a callback that presents the frame; set by the glue to call the Vulkan - // context's present() - std::function present_func; + // pointer to the owning Vulkan context; set by the glue after the context + // is created. swap_frame_buffers() calls present() on it if non-null. + ruis::render::vulkan::context* owner_context = nullptr; - native_window( + vulkan_native_window( utki::shared_ref display, // const utki::version_duplet&, // the GL version is not used by Vulkan const ruisapp::window_parameters& window_params, - const native_window* shared + vulkan_native_window* shared ) : xorg_window( display, // @@ -64,18 +64,18 @@ public: ) {} - native_window(const native_window&) = delete; - native_window& operator=(const native_window&) = delete; + vulkan_native_window(const vulkan_native_window&) = delete; + vulkan_native_window& operator=(const vulkan_native_window&) = delete; - native_window(native_window&&) = delete; - native_window& operator=(native_window&&) = delete; + vulkan_native_window(vulkan_native_window&&) = delete; + vulkan_native_window& operator=(vulkan_native_window&&) = delete; - ~native_window() override = default; + ~vulkan_native_window() override = default; void swap_frame_buffers() override { - if (this->present_func) { - this->present_func(); + if (this->owner_context) { + this->owner_context->present(); } } }; From 236d0f2b4fa75f4230c07685230c53778179c204 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:19:37 +0200 Subject: [PATCH 14/22] Remove addressed review comment --- src/ruisapp/glue/linux/xorg/opengl_window.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ruisapp/glue/linux/xorg/opengl_window.hxx b/src/ruisapp/glue/linux/xorg/opengl_window.hxx index 272f65c..cba6b71 100644 --- a/src/ruisapp/glue/linux/xorg/opengl_window.hxx +++ b/src/ruisapp/glue/linux/xorg/opengl_window.hxx @@ -332,7 +332,7 @@ struct glx_context_wrapper { } }; -// review: rename to opengl_native_window, do simnilar with opengles_native_window and vulkan_native_window, also in wayland implementation + class opengl_native_window : public gl_visual_info, public xorg_window { public: From 4a4d169a2a52e82399ba560cb2d8e4623da23ea7 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:38:43 +0200 Subject: [PATCH 15/22] Fix Vulkan Wayland crash: create dummy native_window for shared_gl_context The shared_gl_context_native_window member is a shared_ref (cannot be null). For Vulkan, it was being set to nullptr, causing an assertion failure. Now a 1x1 hidden window is created for all APIs. --- .../glue/linux/wayland/application.hxx | 19 +++++++++++++++---- src/ruisapp/glue/linux/xorg/opengl_window.hxx | 1 - 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 28d632f..422605f 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -182,9 +182,19 @@ public: application_glue(const utki::version_duplet& gl_version) : waitable(this->display.get().wayland_display), gl_version(gl_version), - shared_gl_context_native_window( // + shared_gl_context_native_window( + // create a 1x1 hidden window; for Vulkan it is not used by the factory, + // but the member is a shared_ref which cannot be null #ifdef RUISAPP_RENDER_VULKAN - nullptr // Vulkan doesn't need a shared GL context window + utki::make_shared( + this->display, // + ruisapp::window_parameters{ + .dims = {1, 1}, + .title = {}, + .fullscreen = false, + .taskbar = false + } + ) #else utki::make_shared( this->display, // @@ -192,9 +202,10 @@ public: ruisapp::window_parameters{ .dims = {1, 1}, .title = {}, - .fullscreen = false + .fullscreen = false, + .taskbar = false }, - nullptr // no shared gl context + nullptr ) #endif ), diff --git a/src/ruisapp/glue/linux/xorg/opengl_window.hxx b/src/ruisapp/glue/linux/xorg/opengl_window.hxx index cba6b71..578a1d2 100644 --- a/src/ruisapp/glue/linux/xorg/opengl_window.hxx +++ b/src/ruisapp/glue/linux/xorg/opengl_window.hxx @@ -332,7 +332,6 @@ struct glx_context_wrapper { } }; - class opengl_native_window : public gl_visual_info, public xorg_window { public: From 4704fa336ad90148813a8331a28569152eba9ddb Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:51:42 +0200 Subject: [PATCH 16/22] Address review: create vulkan::context before ruis::context, no static_cast Create the Vulkan render context before the ruis::context so we can save a pointer to it directly (owner_context) without a static_cast downcast. --- .../glue/linux/wayland/application.cxx | 44 ++++++++++--------- src/ruisapp/glue/linux/xorg/glue.cxx | 20 ++++----- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 92a08ab..50eaabe 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -216,6 +216,26 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ); #endif + // for Vulkan, create the context before the ruis::context so we can save a + // pointer to it without a static_cast later +#ifdef RUISAPP_RENDER_VULKAN + auto vulkan_ctx = [this](auto&& win_ref) -> utki::shared_ref { + auto wl_surf = win_ref.get().get_wl_surface(); + auto wl_disp = win_ref.get().get_wl_display(); + auto& vdev = this->device.get(); + VkWaylandSurfaceCreateInfoKHR sci{}; + sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; + sci.display = wl_disp; + sci.surface = wl_surf; + VkSurfaceKHR vk_surf = VK_NULL_HANDLE; + vkCreateWaylandSurfaceKHR(vdev.get_instance(), &sci, nullptr, &vk_surf); + auto sp = utki::make_shared(vdev, vk_surf); + auto vulkan_factory_ref = + utki::dynamic_reference_cast(this->render_factory); + return utki::make_shared(win_ref, sp, vulkan_factory_ref); + }(ruis_native_window); +#endif + auto ruis_context = utki::make_shared(ruis::context::parameters{ .post_to_ui_thread_function = [this](std::function proc) { @@ -228,21 +248,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_VULKAN) - [&](auto&& win_ref) -> utki::shared_ref { - auto wl_surf = win_ref.get().get_wl_surface(); - auto wl_disp = win_ref.get().get_wl_display(); - auto& vdev = this->device.get(); - VkWaylandSurfaceCreateInfoKHR sci{}; - sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; - sci.display = wl_disp; - sci.surface = wl_surf; - VkSurfaceKHR vk_surf = VK_NULL_HANDLE; - vkCreateWaylandSurfaceKHR(vdev.get_instance(), &sci, nullptr, &vk_surf); - auto sp = utki::make_shared(vdev, vk_surf); - auto vulkan_factory_ref = - utki::dynamic_reference_cast(this->render_factory); - return utki::make_shared(win_ref, sp, vulkan_factory_ref); - }(ruis_native_window), + vulkan_ctx, #else # error "Unknown graphics API" #endif @@ -270,12 +276,8 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ); #ifdef RUISAPP_RENDER_VULKAN - { - // set the owning Vulkan context so that swap_frame_buffers() can call present() - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") - ruisapp_window.get().ruis_native_window.get().owner_context = - &static_cast(ruisapp_window.get().gui.context.get().ren().ctx()); - } + // set the owning Vulkan context so that swap_frame_buffers() can call present() + ruisapp_window.get().ruis_native_window.get().owner_context = &vulkan_ctx.get(); #endif ruisapp_window.get().gui.set_viewport( // diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index cba8239..8464178 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -238,6 +238,14 @@ class application_glue : public utki::destructable }()); auto vulkan_factory_ref = utki::dynamic_reference_cast(this->render_factory); + + // create the Vulkan context before the ruis::context so we can save a pointer + // to it without a static_cast later + auto vulkan_ctx = utki::make_shared( + ruis_native_window, // + win_surface, // + vulkan_factory_ref + ); #endif auto ruis_context = utki::make_shared(ruis::context::parameters{ @@ -252,13 +260,7 @@ class application_glue : public utki::destructable #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_VULKAN) - // The Vulkan context needs the concrete factory (not the abstract one) - // because it accesses Vulkan-specific members (e.g. all_textures, pipeline caches). - utki::make_shared( - ruis_native_window, // - win_surface, // - vulkan_factory_ref - ), + vulkan_ctx, #endif this->common_shaders, this->common_render_objects @@ -297,9 +299,7 @@ class application_glue : public utki::destructable { #ifdef RUISAPP_RENDER_VULKAN // set the owning Vulkan context so that swap_frame_buffers() can call present() - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer is the Vulkan renderer in this build variant") - nw.owner_context = - &static_cast(ruisapp_window.get().gui.context.get().ren().ctx()); + nw.owner_context = &vulkan_ctx.get(); #endif } From c1ab784ef30e45ffcd360e397a9c8f7720acef37 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:01:05 +0200 Subject: [PATCH 17/22] Address remaining review comments - Store concrete factory type (no dynamic_cast needed) - Create render context before ruis::context for all APIs - Rename vulkan_ctx to render_context - Move owner_context assignment to right after render context creation - Remove answered review comments --- .../glue/linux/wayland/application.cxx | 37 +++++++------- .../glue/linux/wayland/application.hxx | 9 +++- src/ruisapp/glue/linux/xorg/glue.cxx | 50 ++++++++++--------- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 50eaabe..e46d595 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -216,10 +216,10 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ); #endif - // for Vulkan, create the context before the ruis::context so we can save a - // pointer to it without a static_cast later + // create the render context before the ruis::context so we can save a pointer + // to it without a static_cast later (needed for Vulkan swap_frame_buffers) #ifdef RUISAPP_RENDER_VULKAN - auto vulkan_ctx = [this](auto&& win_ref) -> utki::shared_ref { + auto render_context = [this](auto&& win_ref) -> utki::shared_ref { auto wl_surf = win_ref.get().get_wl_surface(); auto wl_disp = win_ref.get().get_wl_display(); auto& vdev = this->device.get(); @@ -230,10 +230,20 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window VkSurfaceKHR vk_surf = VK_NULL_HANDLE; vkCreateWaylandSurfaceKHR(vdev.get_instance(), &sci, nullptr, &vk_surf); auto sp = utki::make_shared(vdev, vk_surf); - auto vulkan_factory_ref = - utki::dynamic_reference_cast(this->render_factory); - return utki::make_shared(win_ref, sp, vulkan_factory_ref); + return utki::make_shared(win_ref, sp, this->render_factory); }(ruis_native_window); + // set the owning Vulkan context so that swap_frame_buffers() can call present() + ruis_native_window.get().owner_context = &render_context.get(); +#elif defined(RUISAPP_RENDER_OPENGL) + auto render_context = utki::make_shared( + ruis_native_window, // + this->render_factory + ); +#else // RUISAPP_RENDER_OPENGLES + auto render_context = utki::make_shared( + ruis_native_window, // + this->render_factory + ); #endif auto ruis_context = utki::make_shared(ruis::context::parameters{ @@ -243,15 +253,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window }, .updater = this->updater, .renderer = utki::make_shared( -#ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window, this->render_factory), -#elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window, this->render_factory), -#elif defined(RUISAPP_RENDER_VULKAN) - vulkan_ctx, -#else -# error "Unknown graphics API" -#endif + render_context, this->common_shaders, this->common_render_objects ), @@ -275,11 +277,6 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window std::move(ruis_native_window) ); -#ifdef RUISAPP_RENDER_VULKAN - // set the owning Vulkan context so that swap_frame_buffers() can call present() - ruisapp_window.get().ruis_native_window.get().owner_context = &vulkan_ctx.get(); -#endif - ruisapp_window.get().gui.set_viewport( // ruis::rect( 0, // diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 422605f..680d8f3 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -153,7 +153,14 @@ public: // Surface extensions are needed for creating Vulkan surfaces (one per window) for presenting frames. const utki::shared_ref device; #endif - const utki::shared_ref render_factory; + // concrete factory type, #ifdef by renderer +#ifdef RUISAPP_RENDER_VULKAN + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGL) + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGLES) + const utki::shared_ref render_factory; +#endif const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 8464178..a9a2444 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -130,7 +130,16 @@ class application_glue : public utki::destructable utki::version_duplet gl_version; utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref render_factory; + + // concrete factory type, #ifdef by renderer +#ifdef RUISAPP_RENDER_VULKAN + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGL) + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGLES) + const utki::shared_ref render_factory; +#endif + const utki::shared_ref common_shaders; utki::shared_ref common_render_objects; utki::shared_ref ruis_resource_loader; @@ -222,6 +231,8 @@ class application_glue : public utki::destructable &this->shared_gl_context_native_window.get() ); + // create the render context before the ruis::context so we can save a pointer + // to it without a static_cast later (needed for Vulkan swap_frame_buffers) #ifdef RUISAPP_RENDER_VULKAN auto win_surface = utki::make_shared(this->device.get(), [&]() { VkXlibSurfaceCreateInfoKHR ci{}; @@ -236,15 +247,22 @@ class application_glue : public utki::destructable ); return surface; }()); - auto vulkan_factory_ref = - utki::dynamic_reference_cast(this->render_factory); - - // create the Vulkan context before the ruis::context so we can save a pointer - // to it without a static_cast later - auto vulkan_ctx = utki::make_shared( + auto render_context = utki::make_shared( ruis_native_window, // win_surface, // - vulkan_factory_ref + this->render_factory + ); + // set the owning Vulkan context so that swap_frame_buffers() can call present() + ruis_native_window.get().owner_context = &render_context.get(); +#elif defined(RUISAPP_RENDER_OPENGL) + auto render_context = utki::make_shared( + ruis_native_window, // + this->render_factory + ); +#else // RUISAPP_RENDER_OPENGLES + auto render_context = utki::make_shared( + ruis_native_window, // + this->render_factory ); #endif @@ -255,13 +273,7 @@ class application_glue : public utki::destructable }, .updater = this->updater, .renderer = utki::make_shared( -#ifdef RUISAPP_RENDER_OPENGL - utki::make_shared(ruis_native_window, this->render_factory), -#elif defined(RUISAPP_RENDER_OPENGLES) - utki::make_shared(ruis_native_window, this->render_factory), -#elif defined(RUISAPP_RENDER_VULKAN) - vulkan_ctx, -#endif + render_context, this->common_shaders, this->common_render_objects ), @@ -293,16 +305,8 @@ class application_glue : public utki::destructable std::move(ruis_native_window) ); - // save a pointer to the native_window before the move auto& nw = ruisapp_window.get().ruis_native_window.get(); - { -#ifdef RUISAPP_RENDER_VULKAN - // set the owning Vulkan context so that swap_frame_buffers() can call present() - nw.owner_context = &vulkan_ctx.get(); -#endif - } - ruisapp_window.get().gui.set_viewport( // ruis::rect( 0, // From 902915daa06da83487959ea92f21db044bbc2744 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:05:33 +0200 Subject: [PATCH 18/22] Remove remaining review comments, use #elif defined + #error for unknown API --- src/ruisapp/glue/linux/wayland/application.cxx | 4 +++- src/ruisapp/glue/linux/xorg/glue.cxx | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index e46d595..8c53c8c 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -239,11 +239,13 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ruis_native_window, // this->render_factory ); -#else // RUISAPP_RENDER_OPENGLES +#elif defined(RUISAPP_RENDER_OPENGLES) auto render_context = utki::make_shared( ruis_native_window, // this->render_factory ); +#else +# error "Unknown graphics API" #endif auto ruis_context = utki::make_shared(ruis::context::parameters{ diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index a9a2444..adc9d32 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -259,11 +259,13 @@ class application_glue : public utki::destructable ruis_native_window, // this->render_factory ); -#else // RUISAPP_RENDER_OPENGLES +#elif defined(RUISAPP_RENDER_OPENGLES) auto render_context = utki::make_shared( ruis_native_window, // this->render_factory ); +#else +# error "Unknown graphics API" #endif auto ruis_context = utki::make_shared(ruis::context::parameters{ From eab0cda1b51b8bf8ea9e0f8b81cf10bad111d7d7 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:12:51 +0200 Subject: [PATCH 19/22] Use ctx() shorthand, std::move for shared_ref args --- src/ruisapp/application.cpp | 6 +++--- src/ruisapp/glue/linux/wayland/application.cxx | 2 +- src/ruisapp/glue/linux/xorg/glue.cxx | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ruisapp/application.cpp b/src/ruisapp/application.cpp index 9b9274e..4fd1e64 100644 --- a/src/ruisapp/application.cpp +++ b/src/ruisapp/application.cpp @@ -132,7 +132,7 @@ ruisapp::window& application::make_window(window_parameters window_params) // By choice, the VSYNC is enabled by default. // This call will synchornize the is_vsync_enabled_v field of the native window // with the actual VSYNC state of the newly created window. - win.gui.context.get().ren().ctx().set_vsync_enabled(true); + win.gui.ctx().ren().ctx().set_vsync_enabled(true); if (!this->get_shared_style_provider()) { // The platform does not support shared style_provider. @@ -140,7 +140,7 @@ ruisapp::window& application::make_window(window_parameters window_params) // The platform should support only one window which has just been created. utki::assert(this->get_windows().size() == size_t(1)); - auto& sp = win.gui.context.get().style(); + auto& sp = win.gui.ctx().style(); if (this->mount_ruis_res_pack) { // The platform doesn't have shared style provider, so we need to @@ -165,7 +165,7 @@ const std::shared_ptr& application::get_default_style_prov auto windows = this->get_windows(); if (!windows.empty()) { - return windows.front().get().gui.context.get().style_provider.to_shared_ptr(); + return windows.front().get().gui.ctx().style_provider.to_shared_ptr(); } // return nullptr because shared style provider is nullptr diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 8c53c8c..f2ba2c8 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -255,7 +255,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window }, .updater = this->updater, .renderer = utki::make_shared( - render_context, + std::move(render_context), this->common_shaders, this->common_render_objects ), diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index adc9d32..334b1f6 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -249,7 +249,7 @@ class application_glue : public utki::destructable }()); auto render_context = utki::make_shared( ruis_native_window, // - win_surface, // + std::move(win_surface), // this->render_factory ); // set the owning Vulkan context so that swap_frame_buffers() can call present() @@ -275,7 +275,7 @@ class application_glue : public utki::destructable }, .updater = this->updater, .renderer = utki::make_shared( - render_context, + std::move(render_context), this->common_shaders, this->common_render_objects ), From 13e03b4addeac06f9873a927acf3bd4f45c4738b Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:28:40 +0200 Subject: [PATCH 20/22] Use context::swap_frame_buffers(), remove owner_context from Vulkan windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - window.cpp calls render context's swap_frame_buffers() instead of native window's - Removed owner_context pointer and swap_frame_buffers() override from Vulkan native windows (Xorg + Wayland) — the context handles presentation - xdg_surface.cxx: moved swap_frame_buffers() call out of #ifdef since it is now identical for GL and Vulkan paths --- .../glue/linux/wayland/application.cxx | 2 -- .../glue/linux/wayland/vulkan_window.hxx | 11 ----------- .../glue/linux/wayland/xdg_surface.cxx | 19 ++++++------------- src/ruisapp/glue/linux/xorg/glue.cxx | 2 -- src/ruisapp/glue/linux/xorg/vulkan_window.hxx | 11 ----------- src/ruisapp/window.cpp | 2 +- 6 files changed, 7 insertions(+), 40 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index f2ba2c8..94ddab3 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -232,8 +232,6 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window auto sp = utki::make_shared(vdev, vk_surf); return utki::make_shared(win_ref, sp, this->render_factory); }(ruis_native_window); - // set the owning Vulkan context so that swap_frame_buffers() can call present() - ruis_native_window.get().owner_context = &render_context.get(); #elif defined(RUISAPP_RENDER_OPENGL) auto render_context = utki::make_shared( ruis_native_window, // diff --git a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx index e61de51..5fecfe9 100644 --- a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx @@ -31,10 +31,6 @@ namespace { class vulkan_native_window : public wayland_window { public: - // pointer to the owning Vulkan context; set by the glue after the context - // is created. swap_frame_buffers() calls present() on it if non-null. - ruis::render::vulkan::context* owner_context = nullptr; - vulkan_native_window( utki::shared_ref display, const ruisapp::window_parameters& window_params @@ -45,13 +41,6 @@ public: ) {} - void swap_frame_buffers() override - { - if (this->owner_context) { - this->owner_context->present(); - } - } - void bind_rendering_context() override { // For Vulkan, there's no concept of binding a rendering context. diff --git a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx index 1242374..95c04f4 100644 --- a/src/ruisapp/glue/linux/wayland/xdg_surface.cxx +++ b/src/ruisapp/glue/linux/wayland/xdg_surface.cxx @@ -88,9 +88,9 @@ void xdg_surface_wrapper::xdg_surface_configure( } auto& win = *window; - auto& natwin = win.ruis_native_window.get(); #if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) + auto& natwin = win.ruis_native_window.get(); // The configure event can come several times for the same xdg_surface // because we do several surface commits during the initial configuration, // and each commit can trigger a configure event. @@ -99,23 +99,16 @@ void xdg_surface_wrapper::xdg_surface_configure( if (!natwin.is_egl_surface_created()) { natwin.create_egl_surface(); } +#endif // After initial configure event we need to do the initial surface commit, // otherwise the surface will not be mapped to the screen. - // In case of EGL we have to call eglSwapBuffers(), which will do the surface commit for us. - // This makes the surface to be mapped to the screen. - // swap EGL frame buffers with the window's EGL context made current + // For EGL this calls eglSwapBuffers() which does the surface commit for us. + // For Vulkan this presents a frame (the compositor fires the frame callback + // only after compositing a surface that has a buffer). win.gui.context.get().ren().ctx().apply([&]() { - natwin.swap_frame_buffers(); + win.gui.context.get().ren().ctx().swap_frame_buffers(); }); -#else - // For Vulkan, the initial surface commit (with a buffer) is done by presenting a frame. - // Without this the frame callback would never fire (the compositor only fires it after - // compositing a surface that has a buffer), so the window would never become visible. - win.gui.context.get().ren().ctx().apply([&]() { - natwin.swap_frame_buffers(); - }); -#endif // on some Wayland implementations just swapping EGL buffers is not enough and surface commit is needed self.wayland_surface.commit(); diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index 334b1f6..c17db4b 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -252,8 +252,6 @@ class application_glue : public utki::destructable std::move(win_surface), // this->render_factory ); - // set the owning Vulkan context so that swap_frame_buffers() can call present() - ruis_native_window.get().owner_context = &render_context.get(); #elif defined(RUISAPP_RENDER_OPENGL) auto render_context = utki::make_shared( ruis_native_window, // diff --git a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx index 20db89d..03911e6 100644 --- a/src/ruisapp/glue/linux/xorg/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/xorg/vulkan_window.hxx @@ -37,10 +37,6 @@ namespace { class vulkan_native_window : public xorg_window { public: - // pointer to the owning Vulkan context; set by the glue after the context - // is created. swap_frame_buffers() calls present() on it if non-null. - ruis::render::vulkan::context* owner_context = nullptr; - vulkan_native_window( utki::shared_ref display, // const utki::version_duplet&, // the GL version is not used by Vulkan @@ -71,13 +67,6 @@ public: vulkan_native_window& operator=(vulkan_native_window&&) = delete; ~vulkan_native_window() override = default; - - void swap_frame_buffers() override - { - if (this->owner_context) { - this->owner_context->present(); - } - } }; } // namespace diff --git a/src/ruisapp/window.cpp b/src/ruisapp/window.cpp index 55b7b6e..d4a453a 100644 --- a/src/ruisapp/window.cpp +++ b/src/ruisapp/window.cpp @@ -38,7 +38,7 @@ void window::render() this->gui.render(this->gui.context.get().ren().ctx().initial_matrix); // std::cout << "swap frame buffers" << std::endl; - this->gui.context.get().window().swap_frame_buffers(); + this->gui.ctx().ren().ctx().swap_frame_buffers(); // std::cout << "swapped" << std::endl; }); } From 34a0a480e8f571654e50df137c3ff85109c2dca9 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:39:35 +0200 Subject: [PATCH 21/22] Point to Vulkan pick_present_mode() for vsync handling --- src/ruisapp/glue/linux/wayland/vulkan_window.hxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx index 5fecfe9..edecc4c 100644 --- a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx @@ -56,7 +56,8 @@ public: void set_vsync_enabled_internal(bool enabled) override { (void)enabled; - // VSync is handled by the Vulkan present mode + // VSync is handled by the Vulkan present mode, + // see pick_present_mode() in ruis-render-vulkan/src/ruis/render/vulkan/context.cpp } }; } // namespace From 35eefda6606da5bcd3cb1bb7db3fa263cab00487 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:44:09 +0200 Subject: [PATCH 22/22] Improve vsync comment in vulkan_window.hxx --- src/ruisapp/glue/linux/wayland/vulkan_window.hxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx index edecc4c..811aeeb 100644 --- a/src/ruisapp/glue/linux/wayland/vulkan_window.hxx +++ b/src/ruisapp/glue/linux/wayland/vulkan_window.hxx @@ -56,8 +56,10 @@ public: void set_vsync_enabled_internal(bool enabled) override { (void)enabled; - // VSync is handled by the Vulkan present mode, - // see pick_present_mode() in ruis-render-vulkan/src/ruis/render/vulkan/context.cpp + // VSync is handled by the Vulkan present mode (a swapchain creation parameter, + // not a runtime setting). ensure_swapchain_valid() is called every frame and if + // it detects a change in is_vsync_enabled() it re-creates the swapchain with + // the actual vsync state. See ruis-render-vulkan/src/ruis/render/vulkan/context.cpp } }; } // namespace