From 4fe0786bbd62f17a89d4fb7810be5a4a7f1557c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 23 Dec 2022 10:16:17 +0200 Subject: [PATCH] gtk4: Add support for GL on macOS Part-of: --- video/gtk4/Cargo.toml | 4 ++++ video/gtk4/src/sink/imp.rs | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/video/gtk4/Cargo.toml b/video/gtk4/Cargo.toml index e420f32c..46b8a82e 100644 --- a/video/gtk4/Cargo.toml +++ b/video/gtk4/Cargo.toml @@ -24,6 +24,10 @@ gst_gl_egl = { package = "gstreamer-gl-egl", git = "https://gitlab.freedesktop.o once_cell = "1.0" +[target.'cfg(target_os = "macos")'.dependencies] +gtk = { package = "gtk4", git = "https://github.com/gtk-rs/gtk4-rs", features = ["v4_6"] } +gst_gl = { package = "gstreamer-gl", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_16"] } + [lib] name = "gstgtk4" crate-type = ["cdylib", "rlib"] diff --git a/video/gtk4/src/sink/imp.rs b/video/gtk4/src/sink/imp.rs index 158aff37..3dd4a759 100644 --- a/video/gtk4/src/sink/imp.rs +++ b/video/gtk4/src/sink/imp.rs @@ -598,6 +598,8 @@ impl PaintableSink { "GdkX11GLContextGLX" => (), #[cfg(all(target_os = "linux", feature = "wayland"))] "GdkWaylandGLContext" => (), + #[cfg(all(target_os = "macos", feature = "gst_gl"))] + "GdkMacosGLContext" => (), display => { gst::error!(CAT, imp: self_, "Unsupported GDK display {display} for GL"); return None; @@ -654,6 +656,10 @@ impl PaintableSink { "GdkWaylandGLContext" => { self.initialize_waylandegl(display, &mut display_ctx_guard, &mut app_ctx_guard); } + #[cfg(all(target_os = "macos", feature = "gst_gl"))] + "GdkMacosGLContext" => { + self.initialize_macosgl(display, &mut display_ctx_guard, &mut app_ctx_guard); + } _ => { unreachable!("Unsupported GDK display {display} for GL"); } @@ -851,4 +857,38 @@ impl PaintableSink { app_ctx_guard.replace(gst_app_context.unwrap()); } } + + #[cfg(all(target_os = "macos", feature = "gst_gl"))] + fn initialize_macosgl( + &self, + display: gdk::Display, + display_ctx_guard: &mut Option, + app_ctx_guard: &mut Option, + ) { + gst::info!( + CAT, + imp: self, + "Initializing GL with for macOS backend and display." + ); + + let platform = gst_gl::GLPlatform::CGL; + let (gl_api, _, _) = gst_gl::GLContext::current_gl_api(platform); + let gl_ctx = gst_gl::GLContext::current_gl_context(platform); + + if gl_ctx == 0 { + gst::error!(CAT, imp: self, "Failed to get handle from GdkGLContext",); + return; + } + + let gst_display = gst_gl::GLDisplay::new(); + unsafe { + let gst_app_context = + gst_gl::GLContext::new_wrapped(&gst_display, gl_ctx, platform, gl_api); + + assert!(gst_app_context.is_some()); + + display_ctx_guard.replace(gst_display); + app_ctx_guard.replace(gst_app_context.unwrap()); + } + } }