forge
Explore
Log in

Don't you dare go Hollow

pw_utils: change type of `dma_negotiation` to `Option<DmaNegotiation>`

Browse files at this commit

4dc2750becd50dae0ed511a775888c63d4fb8db3wrvsrxParent: 3644e03
src/screencasting/pw_utils.rsmodified+81 -74
@@ -131,18 +131,20 @@ struct DmaNegotiation {
131131 #[allow(clippy::large_enum_variant)]
132132 #[derive(Debug)]
133133 enum CastState {
134+ // dma_negotiation = Some(_) means DMA sharing
135+ // dma_negotiation = None means SHM sharing
134136 ResizePending {
135137 pending_size: Size<u32, Physical>,
136138 },
137139 ConfirmationPending {
138140 size: Size<u32, Physical>,
139141 alpha: bool,
140- dma_negotiation: DmaNegotiation,
142+ dma_negotiation: Option<DmaNegotiation>,
141143 },
142144 Ready {
143145 size: Size<u32, Physical>,
144146 alpha: bool,
145- dma_negotiation: DmaNegotiation,
147+ dma_negotiation: Option<DmaNegotiation>,
146148 // Lazily-initialized to keep the initialization to a single place.
147149 damage_tracker: Option<OutputDamageTracker>,
148150 cursor_damage_tracker: Option<OutputDamageTracker>,
@@ -509,10 +511,10 @@ impl PipeWire {
509511 *state = CastState::ConfirmationPending {
510512 size: format_size,
511513 alpha: format_has_alpha,
512- dma_negotiation: DmaNegotiation {
514+ dma_negotiation: Some(DmaNegotiation {
513515 modifier,
514516 plane_count: plane_count as i32,
515- },
517+ }),
516518 };
517519
518520 let fixated_format = FormatSet::from_iter([Format {
@@ -550,12 +552,12 @@ impl PipeWire {
550552 CastState::ConfirmationPending {
551553 size,
552554 alpha,
553- dma_negotiation,
555+ dma_negotiation: Some(dma_negotiation),
554556 }
555557 | CastState::Ready {
556558 size,
557559 alpha,
558- dma_negotiation,
560+ dma_negotiation: Some(dma_negotiation),
559561 ..
560562 } if *alpha == format_has_alpha
561563 && dma_negotiation.modifier
@@ -582,7 +584,7 @@ impl PipeWire {
582584 *state = CastState::Ready {
583585 size,
584586 alpha,
585- dma_negotiation,
587+ dma_negotiation: Some(dma_negotiation),
586588 damage_tracker,
587589 cursor_damage_tracker,
588590 last_cursor_location: None,
@@ -616,10 +618,10 @@ impl PipeWire {
616618 *state = CastState::Ready {
617619 size: format_size,
618620 alpha: format_has_alpha,
619- dma_negotiation: DmaNegotiation {
621+ dma_negotiation: Some(DmaNegotiation {
620622 modifier,
621623 plane_count: plane_count as i32,
622- },
624+ }),
623625 damage_tracker: None,
624626 cursor_damage_tracker: None,
625627 last_cursor_location: None,
@@ -712,77 +714,82 @@ impl PipeWire {
712714 let _span = debug_span!("add_buffer", %stream_id).entered();
713715 let mut inner = inner.borrow_mut();
714716
715- let (size, alpha, modifier) = if let CastState::Ready {
716- size,
717- alpha,
718- dma_negotiation,
719- ..
720- } = &inner.state
721- {
722- (*size, *alpha, dma_negotiation.modifier)
723- } else {
724- trace!("add_buffer, but not ready yet");
725- return;
726- };
717+ match inner.state {
718+ CastState::Ready {
719+ size,
720+ alpha,
721+ dma_negotiation,
722+ ..
723+ } => {
724+ match dma_negotiation {
725+ Some(DmaNegotiation { modifier, .. }) => {
726+ trace!("size={size:?}, alpha={alpha}, modifier={modifier:?}");
727+ unsafe {
728+ let spa_buffer = (*buffer).buffer;
729+
730+ let fourcc = if alpha {
731+ Fourcc::Argb8888
732+ } else {
733+ Fourcc::Xrgb8888
734+ };
727735
728- trace!("size={size:?}, alpha={alpha}, modifier={modifier:?}");
736+ let dmabuf = match allocate_dmabuf(&gbm, size, fourcc, modifier) {
737+ Ok(dmabuf) => dmabuf,
738+ Err(err) => {
739+ warn!("error allocating dmabuf: {err:?}");
740+ stop_cast();
741+ return;
742+ }
743+ };
729744
730- unsafe {
731- let spa_buffer = (*buffer).buffer;
745+ let plane_count = dmabuf.num_planes();
746+ assert_eq!((*spa_buffer).n_datas as usize, plane_count);
732747
733- let fourcc = if alpha {
734- Fourcc::Argb8888
735- } else {
736- Fourcc::Xrgb8888
737- };
748+ for (i, (fd, (stride, offset))) in
749+ zip(dmabuf.handles(), zip(dmabuf.strides(), dmabuf.offsets()))
750+ .enumerate()
751+ {
752+ let spa_data = (*spa_buffer).datas.add(i);
753+ assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0);
754+
755+ (*spa_data).type_ = DataType::DmaBuf.as_raw();
756+
757+ // With DMA-BUFs, consumers should ignore the maxsize field, and
758+ // producers are allowed to set it to 0.
759+ //
760+ // https://docs.pipewire.org/page_dma_buf.html
761+ (*spa_data).maxsize = 1;
762+ (*spa_data).fd = fd.as_raw_fd() as i64;
763+ (*spa_data).flags = SPA_DATA_FLAG_READWRITE;
764+
765+ let chunk = (*spa_data).chunk;
766+ (*chunk).stride = stride as i32;
767+ (*chunk).offset = offset;
768+
769+ trace!(
770+ "pw buffer plane: fd={}, stride={stride}, offset={offset}",
771+ (*spa_data).fd
772+ );
773+ }
738774
739- let dmabuf = match allocate_dmabuf(&gbm, size, fourcc, modifier) {
740- Ok(dmabuf) => dmabuf,
741- Err(err) => {
742- warn!("error allocating dmabuf: {err:?}");
743- stop_cast();
744- return;
775+ let fd = (*(*spa_buffer).datas).fd;
776+ assert!(inner.dmabufs.insert(fd, dmabuf).is_none());
777+ }
778+
779+ // During size re-negotiation, the stream sometimes just keeps running, in
780+ // which case we may need to force a redraw once we got a newly sized buffer.
781+ if inner.dmabufs.len() == 1 && stream.state() == StreamState::Streaming {
782+ redraw_();
783+ }
784+ },
785+ None => {
786+ warn!("pw stream: shared memory sharing hasn't been implemented")
787+ }
745788 }
746- };
747-
748- let plane_count = dmabuf.num_planes();
749- assert_eq!((*spa_buffer).n_datas as usize, plane_count);
750-
751- for (i, (fd, (stride, offset))) in
752- zip(dmabuf.handles(), zip(dmabuf.strides(), dmabuf.offsets()))
753- .enumerate()
754- {
755- let spa_data = (*spa_buffer).datas.add(i);
756- assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0);
757-
758- (*spa_data).type_ = DataType::DmaBuf.as_raw();
759-
760- // With DMA-BUFs, consumers should ignore the maxsize field, and
761- // producers are allowed to set it to 0.
762- //
763- // https://docs.pipewire.org/page_dma_buf.html
764- (*spa_data).maxsize = 1;
765- (*spa_data).fd = fd.as_raw_fd() as i64;
766- (*spa_data).flags = SPA_DATA_FLAG_READWRITE;
767-
768- let chunk = (*spa_data).chunk;
769- (*chunk).stride = stride as i32;
770- (*chunk).offset = offset;
771-
772- trace!(
773- "pw buffer plane: fd={}, stride={stride}, offset={offset}",
774- (*spa_data).fd
775- );
789+ },
790+ _ => {
791+ trace!("pw stream: add buffer, but not ready yet");
776792 }
777-
778- let fd = (*(*spa_buffer).datas).fd;
779- assert!(inner.dmabufs.insert(fd, dmabuf).is_none());
780- }
781-
782- // During size re-negotiation, the stream sometimes just keeps running, in
783- // which case we may need to force a redraw once we got a newly sized buffer.
784- if inner.dmabufs.len() == 1 && stream.state() == StreamState::Streaming {
785- redraw_();
786793 }
787794 }
788795 })