gtk4: Reduce number of unwraps during GL context creation and query handling

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1038>
This commit is contained in:
Sebastian Dröge 2022-12-29 10:55:13 +02:00
parent f6b092d2af
commit bb2f632c9c

View file

@ -310,11 +310,10 @@ impl BaseSinkImpl for PaintableSink {
} }
#[cfg(any(target_os = "macos", feature = "gst_gl"))] #[cfg(any(target_os = "macos", feature = "gst_gl"))]
{
{ {
// Early return if there is no context initialized // Early return if there is no context initialized
let gst_context_guard = self.gst_context.lock().unwrap(); let gst_context = match &*self.gst_context.lock().unwrap() {
if gst_context_guard.is_none() { None => {
gst::debug!( gst::debug!(
CAT, CAT,
imp: self, imp: self,
@ -322,7 +321,8 @@ impl BaseSinkImpl for PaintableSink {
); );
return Ok(()); return Ok(());
} }
} Some(gst_context) => gst_context.clone(),
};
// GL specific things // GL specific things
let (caps, need_pool) = query.get_owned(); let (caps, need_pool) = query.get_owned();
@ -347,8 +347,6 @@ impl BaseSinkImpl for PaintableSink {
let size = info.size() as u32; let size = info.size() as u32;
{
let gst_context = { self.gst_context.lock().unwrap().clone().unwrap() };
let buffer_pool = gst_gl::GLBufferPool::new(&gst_context); let buffer_pool = gst_gl::GLBufferPool::new(&gst_context);
if need_pool { if need_pool {
@ -374,7 +372,6 @@ impl BaseSinkImpl for PaintableSink {
{ {
query.add_allocation_meta::<gst_gl::GLSyncMeta>(None) query.add_allocation_meta::<gst_gl::GLSyncMeta>(None)
} }
}
Ok(()) Ok(())
} }
@ -388,23 +385,21 @@ impl BaseSinkImpl for PaintableSink {
gst::QueryViewMut::Context(q) => { gst::QueryViewMut::Context(q) => {
// Avoid holding the locks while we respond to the query // Avoid holding the locks while we respond to the query
// The objects are ref-counted anyway. // The objects are ref-counted anyway.
let gst_display = { self.gst_display.lock().unwrap().clone() }; let (gst_display, app_ctx, gst_ctx) = (
if let Some(display) = gst_display { self.gst_display.lock().unwrap().clone(),
let (app_ctx, gst_ctx) = {
(
self.gst_app_context.lock().unwrap().clone(), self.gst_app_context.lock().unwrap().clone(),
self.gst_context.lock().unwrap().clone(), self.gst_context.lock().unwrap().clone(),
) );
};
assert_ne!(app_ctx, None);
assert_ne!(gst_ctx, None);
if let (Some(gst_display), Some(app_ctx), Some(gst_ctx)) =
(gst_display, app_ctx, gst_ctx)
{
return gst_gl::functions::gl_handle_context_query( return gst_gl::functions::gl_handle_context_query(
&*self.obj(), &*self.obj(),
q, q,
Some(&display), Some(&gst_display),
gst_ctx.as_ref(), Some(&gst_ctx),
app_ctx.as_ref(), Some(&app_ctx),
); );
} }
@ -641,24 +636,24 @@ impl PaintableSink {
ctx.make_current(); ctx.make_current();
let mut app_ctx_guard = self.gst_app_context.lock().unwrap(); let mut app_ctx_guard = self.gst_app_context.lock().unwrap();
let mut display_ctx_guard = self.gst_display.lock().unwrap(); let mut display_guard = self.gst_display.lock().unwrap();
match ctx.type_().name() { match ctx.type_().name() {
#[cfg(all(target_os = "linux", feature = "x11egl"))] #[cfg(all(target_os = "linux", feature = "x11egl"))]
"GdkX11GLContextEGL" => { "GdkX11GLContextEGL" => {
self.initialize_x11egl(display, &mut display_ctx_guard, &mut app_ctx_guard); self.initialize_x11egl(display, &mut display_guard, &mut app_ctx_guard);
} }
#[cfg(all(target_os = "linux", feature = "x11glx"))] #[cfg(all(target_os = "linux", feature = "x11glx"))]
"GdkX11GLContextGLX" => { "GdkX11GLContextGLX" => {
self.initialize_x11glx(display, &mut display_ctx_guard, &mut app_ctx_guard); self.initialize_x11glx(display, &mut display_guard, &mut app_ctx_guard);
} }
#[cfg(all(target_os = "linux", feature = "wayland"))] #[cfg(all(target_os = "linux", feature = "wayland"))]
"GdkWaylandGLContext" => { "GdkWaylandGLContext" => {
self.initialize_waylandegl(display, &mut display_ctx_guard, &mut app_ctx_guard); self.initialize_waylandegl(display, &mut display_guard, &mut app_ctx_guard);
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
"GdkMacosGLContext" => { "GdkMacosGLContext" => {
self.initialize_macosgl(display, &mut display_ctx_guard, &mut app_ctx_guard); self.initialize_macosgl(display, &mut display_guard, &mut app_ctx_guard);
} }
_ => { _ => {
unreachable!("Unsupported GDK display {display} for GL"); unreachable!("Unsupported GDK display {display} for GL");
@ -666,11 +661,20 @@ impl PaintableSink {
}; };
// This should have been initialized once we are done with the platform checks // This should have been initialized once we are done with the platform checks
if app_ctx_guard.is_none() { let app_ctx = match &*app_ctx_guard {
None => {
assert!(display_guard.is_none());
return false; return false;
} }
Some(app_ctx) => app_ctx,
};
match app_ctx_guard.as_ref().unwrap().activate(true) { let display = match &*display_guard {
None => return false,
Some(display) => display,
};
match app_ctx.activate(true) {
Ok(_) => gst::info!(CAT, imp: self, "Successfully activated GL Context."), Ok(_) => gst::info!(CAT, imp: self, "Successfully activated GL Context."),
Err(_) => { Err(_) => {
gst::error!(CAT, imp: self, "Failed to activate GL context",); gst::error!(CAT, imp: self, "Failed to activate GL context",);
@ -678,14 +682,14 @@ impl PaintableSink {
} }
}; };
if let Err(err) = app_ctx_guard.as_ref().unwrap().fill_info() { if let Err(err) = app_ctx.fill_info() {
gst::error!( gst::error!(
CAT, CAT,
imp: self, imp: self,
"Failed to fill info on the GL Context: {err}", "Failed to fill info on the GL Context: {err}",
); );
// Deactivate the context upon failure // Deactivate the context upon failure
if app_ctx_guard.as_ref().unwrap().activate(false).is_err() { if app_ctx.activate(false).is_err() {
gst::error!( gst::error!(
CAT, CAT,
imp: self, imp: self,
@ -696,7 +700,7 @@ impl PaintableSink {
return false; return false;
} }
if app_ctx_guard.as_ref().unwrap().activate(false).is_err() { if app_ctx.activate(false).is_err() {
gst::error!(CAT, imp: self, "Failed to deactivate GL context",); gst::error!(CAT, imp: self, "Failed to deactivate GL context",);
return false; return false;
} }
@ -707,11 +711,7 @@ impl PaintableSink {
"Successfully deactivated GL Context after fill_info" "Successfully deactivated GL Context after fill_info"
); );
match display_ctx_guard match display.create_context(app_ctx) {
.as_ref()
.unwrap()
.create_context(app_ctx_guard.as_ref().unwrap())
{
Ok(gst_context) => { Ok(gst_context) => {
let mut gst_ctx_guard = self.gst_context.lock().unwrap(); let mut gst_ctx_guard = self.gst_context.lock().unwrap();
gst::info!(CAT, imp: self, "Successfully initialized GL Context"); gst::info!(CAT, imp: self, "Successfully initialized GL Context");