forge
Explore
Log in

Don't you dare go Hollow

Make gbm optional for PW casts

Browse files at this commit

bfb5dd51b3bda39206cc3a60f52b533695c964b8Ivan MolodetskikhParent: 572ad73
Without gbm, only shm is advertised.

Enables PW screencasting in winit niri.
src/screencasting/mod.rsmodified+9 -11
@@ -77,12 +77,7 @@ impl Screencasting {
77 }77 }
78 78
79 impl State {79 impl State {
80- fn prepare_pw_cast(&mut self) -> anyhow::Result<(GbmDevice<DrmDeviceFd>, FormatSet)> {80+ fn prepare_pw_cast(&mut self) -> anyhow::Result<Option<(GbmDevice<DrmDeviceFd>, FormatSet)>> {
81- let gbm = self
82- .backend
83- .gbm_device()
84- .context("no GBM device available")?;
85-
86 // Ensure PipeWire is initialized.81 // Ensure PipeWire is initialized.
87 if self.niri.casting.pipewire.is_none() {82 if self.niri.casting.pipewire.is_none() {
88 let pw = PipeWire::new(83 let pw = PipeWire::new(
@@ -93,6 +88,11 @@ impl State {
93 self.niri.casting.pipewire = Some(pw);88 self.niri.casting.pipewire = Some(pw);
94 }89 }
95 90
91+ let Some(gbm) = self.backend.gbm_device() else {
92+ // We will offer shm only.
93+ return Ok(None);
94+ };
95+
96 let mut render_formats = self96 let mut render_formats = self
97 .backend97 .backend
98 .with_primary_renderer(|renderer| {98 .with_primary_renderer(|renderer| {
@@ -110,7 +110,7 @@ impl State {
110 }110 }
111 }111 }
112 112
113- Ok((gbm, render_formats))113+ Ok(Some((gbm, render_formats)))
114 }114 }
115 115
116 pub fn on_pw_msg(&mut self, msg: PwToNiri) {116 pub fn on_pw_msg(&mut self, msg: PwToNiri) {
@@ -332,7 +332,7 @@ impl State {
332 }332 }
333 };333 };
334 334
335- let (gbm, render_formats) = match self.prepare_pw_cast() {335+ let gbm = match self.prepare_pw_cast() {
336 Ok(x) => x,336 Ok(x) => x,
337 Err(err) => {337 Err(err) => {
338 warn!("error starting pending screencasts: {err:?}");338 warn!("error starting pending screencasts: {err:?}");
@@ -356,7 +356,6 @@ impl State {
356 for pending in self.niri.casting.pending_dynamic_casts.drain(..) {356 for pending in self.niri.casting.pending_dynamic_casts.drain(..) {
357 let res = pw.start_cast(357 let res = pw.start_cast(
358 gbm.clone(),358 gbm.clone(),
359- render_formats.clone(),
360 pending.session_id,359 pending.session_id,
361 pending.stream_id,360 pending.stream_id,
362 target.clone(),361 target.clone(),
@@ -430,7 +429,7 @@ impl State {
430 }429 }
431 };430 };
432 431
433- let (gbm, render_formats) = match self.prepare_pw_cast() {432+ let gbm = match self.prepare_pw_cast() {
434 Ok(x) => x,433 Ok(x) => x,
435 Err(err) => {434 Err(err) => {
436 warn!("error starting screencast: {err:?}");435 warn!("error starting screencast: {err:?}");
@@ -442,7 +441,6 @@ impl State {
442 441
443 let res = pw.start_cast(442 let res = pw.start_cast(
444 gbm,443 gbm,
445- render_formats,
446 session_id,444 session_id,
447 stream_id,445 stream_id,
448 target,446 target,
src/screencasting/pw_utils.rsmodified+31 -7
@@ -9,7 +9,7 @@ use std::rc::Rc;
9 use std::time::Duration;9 use std::time::Duration;
10 use std::{mem, slice};10 use std::{mem, slice};
11 11
12-use anyhow::{ensure, Context as _};12+use anyhow::{bail, ensure, Context as _};
13 use calloop::timer::{TimeoutAction, Timer};13 use calloop::timer::{TimeoutAction, Timer};
14 use calloop::RegistrationToken;14 use calloop::RegistrationToken;
15 use pipewire::context::ContextRc;15 use pipewire::context::ContextRc;
@@ -400,8 +400,7 @@ impl PipeWire {
400 #[allow(clippy::too_many_arguments)]400 #[allow(clippy::too_many_arguments)]
401 pub fn start_cast(401 pub fn start_cast(
402 &self,402 &self,
403- gbm: GbmDevice<DrmDeviceFd>,403+ gbm: Option<(GbmDevice<DrmDeviceFd>, FormatSet)>,
404- formats: FormatSet,
405 session_id: CastSessionId,404 session_id: CastSessionId,
406 stream_id: CastStreamId,405 stream_id: CastStreamId,
407 target: CastTarget,406 target: CastTarget,
@@ -445,6 +444,13 @@ impl PipeWire {
445 444
446 let pending_size = Size::from((size.w as u32, size.h as u32));445 let pending_size = Size::from((size.w as u32, size.h as u32));
447 446
447+ let (gbm, formats) = if let Some((gbm, formats)) = gbm {
448+ (Some(gbm), formats)
449+ } else {
450+ debug!("no gbm device; advertising only shm formats");
451+ (None, FormatSet::default())
452+ };
453+
448 // Like in good old wayland-rs times...454 // Like in good old wayland-rs times...
449 let inner = Rc::new(RefCell::new(CastInner {455 let inner = Rc::new(RefCell::new(CastInner {
450 is_active: false,456 is_active: false,
@@ -597,6 +603,12 @@ impl PipeWire {
597 {603 {
598 debug!("fixating the modifier");604 debug!("fixating the modifier");
599 605
606+ let Some(gbm) = &gbm else {
607+ error!("negotiated dmabuf without gbm");
608+ stop_cast();
609+ return;
610+ };
611+
600 let pod_modifier = prop_modifier.value();612 let pod_modifier = prop_modifier.value();
601 let Ok((_, modifiers)) =613 let Ok((_, modifiers)) =
602 PodDeserializer::deserialize_from::<Choice<i64>>(614 PodDeserializer::deserialize_from::<Choice<i64>>(
@@ -615,7 +627,7 @@ impl PipeWire {
615 };627 };
616 628
617 let (modifier, plane_count) = match find_preferred_modifier(629 let (modifier, plane_count) = match find_preferred_modifier(
618- &gbm,630+ gbm,
619 format_size,631 format_size,
620 fourcc,632 fourcc,
621 alternatives,633 alternatives,
@@ -718,10 +730,16 @@ impl PipeWire {
718 dma_negotiation.plane_count730 dma_negotiation.plane_count
719 }731 }
720 _ => {732 _ => {
733+ let Some(gbm) = &gbm else {
734+ error!("negotiated dmabuf without gbm");
735+ stop_cast();
736+ return;
737+ };
738+
721 // We're negotiating a single modifier, or alpha or modifier739 // We're negotiating a single modifier, or alpha or modifier
722 // changed, so we need to do a test allocation.740 // changed, so we need to do a test allocation.
723 let (modifier, plane_count) = match find_preferred_modifier(741 let (modifier, plane_count) = match find_preferred_modifier(
724- &gbm,742+ gbm,
725 format_size,743 format_size,
726 fourcc,744 fourcc,
727 vec![format.modifier() as i64],745 vec![format.modifier() as i64],
@@ -874,7 +892,7 @@ impl PipeWire {
874 move |stream, (), buffer| {892 move |stream, (), buffer| {
875 let _span = debug_span!("add_buffer", %stream_id).entered();893 let _span = debug_span!("add_buffer", %stream_id).entered();
876 894
877- match unsafe { inner.borrow_mut().on_add_buffer(&gbm, buffer) } {895+ match unsafe { inner.borrow_mut().on_add_buffer(gbm.as_ref(), buffer) } {
878 Ok(redraw) => {896 Ok(redraw) => {
879 // During size re-negotiation, the stream sometimes just keeps897 // During size re-negotiation, the stream sometimes just keeps
880 // running, in which case we may need to force a redraw once we got898 // running, in which case we may need to force a redraw once we got
@@ -1372,7 +1390,7 @@ impl Cast {
1372 impl CastInner {1390 impl CastInner {
1373 unsafe fn on_add_buffer(1391 unsafe fn on_add_buffer(
1374 &mut self,1392 &mut self,
1375- gbm: &GbmDevice<DrmDeviceFd>,1393+ gbm: Option<&GbmDevice<DrmDeviceFd>>,
1376 buffer: *mut pw_buffer,1394 buffer: *mut pw_buffer,
1377 ) -> anyhow::Result<bool> {1395 ) -> anyhow::Result<bool> {
1378 let CastState::Ready {1396 let CastState::Ready {
@@ -1392,6 +1410,12 @@ impl CastInner {
1392 "pw stream: add_buffer (dma), size={size:?}, \1410 "pw stream: add_buffer (dma), size={size:?}, \
1393 alpha={alpha}, modifier={modifier:?}"1411 alpha={alpha}, modifier={modifier:?}"
1394 );1412 );
1413+
1414+ let Some(gbm) = gbm else {
1415+ error!("add_buffer(dma) without gbm");
1416+ bail!("missing gbm");
1417+ };
1418+
1395 unsafe {1419 unsafe {
1396 let spa_buffer = (*buffer).buffer;1420 let spa_buffer = (*buffer).buffer;
1397 1421