forge
Explore
Log in

Don't you dare go Hollow

Add transparency support to window screencasts

Browse files at this commit

88f7b08e5682b568f710fcb79f6be34848d01beeIvan MolodetskikhParent: dc92d80
Turns out it needed to be in a separate pod.
src/niri.rsmodified+4 -3
@@ -1229,7 +1229,7 @@ impl State {
1229 return;1229 return;
1230 };1230 };
1231 1231
1232- let (target, size, refresh) = match target {1232+ let (target, size, refresh, alpha) = match target {
1233 StreamTargetId::Output { name } => {1233 StreamTargetId::Output { name } => {
1234 let global_space = &self.niri.global_space;1234 let global_space = &self.niri.global_space;
1235 let output = global_space.outputs().find(|out| out.name() == name);1235 let output = global_space.outputs().find(|out| out.name() == name);
@@ -1243,7 +1243,7 @@ impl State {
1243 let transform = output.current_transform();1243 let transform = output.current_transform();
1244 let size = transform.transform_size(mode.size);1244 let size = transform.transform_size(mode.size);
1245 let refresh = mode.refresh as u32;1245 let refresh = mode.refresh as u32;
1246- (CastTarget::Output(output.downgrade()), size, refresh)1246+ (CastTarget::Output(output.downgrade()), size, refresh, false)
1247 }1247 }
1248 StreamTargetId::Window { id } => {1248 StreamTargetId::Window { id } => {
1249 let mut window = None;1249 let mut window = None;
@@ -1274,7 +1274,7 @@ impl State {
1274 let size = bbox.size.to_physical_precise_ceil(scale);1274 let size = bbox.size.to_physical_precise_ceil(scale);
1275 let refresh = output.current_mode().unwrap().refresh as u32;1275 let refresh = output.current_mode().unwrap().refresh as u32;
1276 1276
1277- (CastTarget::Window { id }, size, refresh)1277+ (CastTarget::Window { id }, size, refresh, true)
1278 }1278 }
1279 };1279 };
1280 1280
@@ -1284,6 +1284,7 @@ impl State {
1284 target,1284 target,
1285 size,1285 size,
1286 refresh,1286 refresh,
1287+ alpha,
1287 cursor_mode,1288 cursor_mode,
1288 signal_ctx,1289 signal_ctx,
1289 );1290 );
src/pw_utils.rsmodified+51 -16
@@ -59,6 +59,7 @@ pub struct Cast {
59 pub target: CastTarget,59 pub target: CastTarget,
60 pub size: Rc<Cell<CastSize>>,60 pub size: Rc<Cell<CastSize>>,
61 pub refresh: u32,61 pub refresh: u32,
62+ offer_alpha: bool,
62 pub cursor_mode: CursorMode,63 pub cursor_mode: CursorMode,
63 pub last_frame_time: Duration,64 pub last_frame_time: Duration,
64 pub min_time_between_frames: Rc<Cell<Duration>>,65 pub min_time_between_frames: Rc<Cell<Duration>>,
@@ -87,6 +88,20 @@ pub enum CastTarget {
87 Window { id: u64 },88 Window { id: u64 },
88 }89 }
89 90
91+macro_rules! make_params {
92+ ($size:expr, $refresh:expr, $alpha:expr, $b1:expr, $b2:expr) => {{
93+ let o1 = make_video_params($size, $refresh, false);
94+ let pod1 = make_pod($b1, o1);
95+
96+ if $alpha {
97+ let o2 = make_video_params($size, $refresh, true);
98+ &mut [pod1, make_pod($b2, o2)][..]
99+ } else {
100+ &mut [pod1][..]
101+ }
102+ }};
103+}
104+
90 impl PipeWire {105 impl PipeWire {
91 pub fn new(event_loop: &LoopHandle<'static, State>) -> anyhow::Result<Self> {106 pub fn new(event_loop: &LoopHandle<'static, State>) -> anyhow::Result<Self> {
92 let main_loop = MainLoop::new(None).context("error creating MainLoop")?;107 let main_loop = MainLoop::new(None).context("error creating MainLoop")?;
@@ -139,6 +154,7 @@ impl PipeWire {
139 target: CastTarget,154 target: CastTarget,
140 size: Size<i32, Physical>,155 size: Size<i32, Physical>,
141 refresh: u32,156 refresh: u32,
157+ alpha: bool,
142 cursor_mode: CursorMode,158 cursor_mode: CursorMode,
143 signal_ctx: SignalContext<'static>,159 signal_ctx: SignalContext<'static>,
144 ) -> anyhow::Result<Cast> {160 ) -> anyhow::Result<Cast> {
@@ -167,6 +183,7 @@ impl PipeWire {
167 let is_active = Rc::new(Cell::new(false));183 let is_active = Rc::new(Cell::new(false));
168 let min_time_between_frames = Rc::new(Cell::new(Duration::ZERO));184 let min_time_between_frames = Rc::new(Cell::new(Duration::ZERO));
169 let dmabufs = Rc::new(RefCell::new(HashMap::new()));185 let dmabufs = Rc::new(RefCell::new(HashMap::new()));
186+ let negotiated_alpha = Rc::new(Cell::new(false));
170 187
171 let pending_size = size;188 let pending_size = size;
172 let size = Rc::new(Cell::new(CastSize::InitialPending(size)));189 let size = Rc::new(Cell::new(CastSize::InitialPending(size)));
@@ -221,6 +238,7 @@ impl PipeWire {
221 .param_changed({238 .param_changed({
222 let min_time_between_frames = min_time_between_frames.clone();239 let min_time_between_frames = min_time_between_frames.clone();
223 let size = size.clone();240 let size = size.clone();
241+ let negotiated_alpha = negotiated_alpha.clone();
224 move |stream, (), id, pod| {242 move |stream, (), id, pod| {
225 let id = ParamType::from_raw(id);243 let id = ParamType::from_raw(id);
226 trace!(?id, "pw stream: param_changed");244 trace!(?id, "pw stream: param_changed");
@@ -260,6 +278,8 @@ impl PipeWire {
260 });278 });
261 }279 }
262 280
281+ negotiated_alpha.set(format.format() == VideoFormat::BGRA);
282+
263 let max_frame_rate = format.max_framerate();283 let max_frame_rate = format.max_framerate();
264 // Subtract 0.5 ms to improve edge cases when equal to refresh rate.284 // Subtract 0.5 ms to improve edge cases when equal to refresh rate.
265 let min_frame_time = Duration::from_secs_f64(285 let min_frame_time = Duration::from_secs_f64(
@@ -325,9 +345,11 @@ impl PipeWire {
325 let dmabufs = dmabufs.clone();345 let dmabufs = dmabufs.clone();
326 let stop_cast = stop_cast.clone();346 let stop_cast = stop_cast.clone();
327 let size = size.clone();347 let size = size.clone();
348+ let negotiated_alpha = negotiated_alpha.clone();
328 move |stream, (), buffer| {349 move |stream, (), buffer| {
329 let size = size.get().negotiated_size();350 let size = size.get().negotiated_size();
330- trace!("pw stream: add_buffer, size={:?}", size);351+ let alpha = negotiated_alpha.get();
352+ trace!("pw stream: add_buffer, size={:?}, alpha={alpha}", size);
331 let size = size.expect("size must be negotiated to allocate buffers");353 let size = size.expect("size must be negotiated to allocate buffers");
332 354
333 unsafe {355 unsafe {
@@ -336,10 +358,16 @@ impl PipeWire {
336 assert!((*spa_buffer).n_datas > 0);358 assert!((*spa_buffer).n_datas > 0);
337 assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0);359 assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0);
338 360
361+ let fourcc = if alpha {
362+ Fourcc::Argb8888
363+ } else {
364+ Fourcc::Xrgb8888
365+ };
366+
339 let bo = match gbm.create_buffer_object::<()>(367 let bo = match gbm.create_buffer_object::<()>(
340 size.w as u32,368 size.w as u32,
341 size.h as u32,369 size.h as u32,
342- Fourcc::Xrgb8888,370+ fourcc,
343 GbmBufferFlags::RENDERING | GbmBufferFlags::LINEAR,371 GbmBufferFlags::RENDERING | GbmBufferFlags::LINEAR,
344 ) {372 ) {
345 Ok(bo) => bo,373 Ok(bo) => bo,
@@ -396,15 +424,15 @@ impl PipeWire {
396 424
397 trace!("starting pw stream with size={pending_size:?}, refresh={refresh}");425 trace!("starting pw stream with size={pending_size:?}, refresh={refresh}");
398 426
399- let object = make_video_params(pending_size, refresh);427+ let mut b1 = Vec::new();
400- let mut buffer = vec![];428+ let mut b2 = Vec::new();
401- let mut params = [make_pod(&mut buffer, object)];429+ let params = make_params!(pending_size, refresh, alpha, &mut b1, &mut b2);
402 stream430 stream
403 .connect(431 .connect(
404 Direction::Output,432 Direction::Output,
405 None,433 None,
406 StreamFlags::DRIVER | StreamFlags::ALLOC_BUFFERS,434 StreamFlags::DRIVER | StreamFlags::ALLOC_BUFFERS,
407- &mut params,435+ params,
408 )436 )
409 .context("error connecting stream")?;437 .context("error connecting stream")?;
410 438
@@ -416,6 +444,7 @@ impl PipeWire {
416 target,444 target,
417 size,445 size,
418 refresh,446 refresh,
447+ offer_alpha: alpha,
419 cursor_mode,448 cursor_mode,
420 last_frame_time: Duration::ZERO,449 last_frame_time: Duration::ZERO,
421 min_time_between_frames,450 min_time_between_frames,
@@ -442,11 +471,11 @@ impl Cast {
442 471
443 self.size.set(current_size.with_pending(size));472 self.size.set(current_size.with_pending(size));
444 473
445- let object = make_video_params(size, self.refresh);474+ let mut b1 = Vec::new();
446- let mut buffer = vec![];475+ let mut b2 = Vec::new();
447- let mut params = [make_pod(&mut buffer, object)];476+ let params = make_params!(size, self.refresh, self.offer_alpha, &mut b1, &mut b2);
448 self.stream477 self.stream
449- .update_params(&mut params)478+ .update_params(params)
450 .context("error updating stream params")?;479 .context("error updating stream params")?;
451 480
452 Ok(CastSizeChange::Pending)481 Ok(CastSizeChange::Pending)
@@ -462,11 +491,11 @@ impl Cast {
462 self.refresh = refresh;491 self.refresh = refresh;
463 492
464 let size = self.size.get().expected_format_size();493 let size = self.size.get().expected_format_size();
465- let object = make_video_params(size, self.refresh);494+ let mut b1 = Vec::new();
466- let mut buffer = vec![];495+ let mut b2 = Vec::new();
467- let mut params = [make_pod(&mut buffer, object)];496+ let params = make_params!(size, self.refresh, self.offer_alpha, &mut b1, &mut b2);
468 self.stream497 self.stream
469- .update_params(&mut params)498+ .update_params(params)
470 .context("error updating stream params")?;499 .context("error updating stream params")?;
471 500
472 Ok(())501 Ok(())
@@ -583,13 +612,19 @@ impl CastSize {
583 }612 }
584 }613 }
585 614
586-fn make_video_params(size: Size<i32, Physical>, refresh: u32) -> pod::Object {615+fn make_video_params(size: Size<i32, Physical>, refresh: u32, alpha: bool) -> pod::Object {
616+ let format = if alpha {
617+ VideoFormat::BGRA
618+ } else {
619+ VideoFormat::BGRx
620+ };
621+
587 pod::object!(622 pod::object!(
588 SpaTypes::ObjectParamFormat,623 SpaTypes::ObjectParamFormat,
589 ParamType::EnumFormat,624 ParamType::EnumFormat,
590 pod::property!(FormatProperties::MediaType, Id, MediaType::Video),625 pod::property!(FormatProperties::MediaType, Id, MediaType::Video),
591 pod::property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw),626 pod::property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw),
592- pod::property!(FormatProperties::VideoFormat, Id, VideoFormat::BGRx),627+ pod::property!(FormatProperties::VideoFormat, Id, format),
593 Property {628 Property {
594 key: FormatProperties::VideoModifier.as_raw(),629 key: FormatProperties::VideoModifier.as_raw(),
595 value: pod::Value::Long(u64::from(Modifier::Invalid) as i64),630 value: pod::Value::Long(u64::from(Modifier::Invalid) as i64),