pw_utils: change type of `dma_negotiation` to `Option<DmaNegotiation>`
Browse files at this commit4dc2750becd50dae0ed511a775888c63d4fb8db3Parent: 3644e031 changed file +81 -74
- src/screencasting/pw_utils.rs +81 -74
src/screencasting/pw_utils.rsmodified+81 -74
| @@ -131,18 +131,20 @@ struct DmaNegotiation { | |||
| 131 | #[allow(clippy::large_enum_variant)] | 131 | #[allow(clippy::large_enum_variant)] |
| 132 | #[derive(Debug)] | 132 | #[derive(Debug)] |
| 133 | enum CastState { | 133 | enum CastState { |
| 134 | + // dma_negotiation = Some(_) means DMA sharing | ||
| 135 | + // dma_negotiation = None means SHM sharing | ||
| 134 | ResizePending { | 136 | ResizePending { |
| 135 | pending_size: Size<u32, Physical>, | 137 | pending_size: Size<u32, Physical>, |
| 136 | }, | 138 | }, |
| 137 | ConfirmationPending { | 139 | ConfirmationPending { |
| 138 | size: Size<u32, Physical>, | 140 | size: Size<u32, Physical>, |
| 139 | alpha: bool, | 141 | alpha: bool, |
| 140 | - dma_negotiation: DmaNegotiation, | 142 | + dma_negotiation: Option<DmaNegotiation>, |
| 141 | }, | 143 | }, |
| 142 | Ready { | 144 | Ready { |
| 143 | size: Size<u32, Physical>, | 145 | size: Size<u32, Physical>, |
| 144 | alpha: bool, | 146 | alpha: bool, |
| 145 | - dma_negotiation: DmaNegotiation, | 147 | + dma_negotiation: Option<DmaNegotiation>, |
| 146 | // Lazily-initialized to keep the initialization to a single place. | 148 | // Lazily-initialized to keep the initialization to a single place. |
| 147 | damage_tracker: Option<OutputDamageTracker>, | 149 | damage_tracker: Option<OutputDamageTracker>, |
| 148 | cursor_damage_tracker: Option<OutputDamageTracker>, | 150 | cursor_damage_tracker: Option<OutputDamageTracker>, |
| @@ -509,10 +511,10 @@ impl PipeWire { | |||
| 509 | *state = CastState::ConfirmationPending { | 511 | *state = CastState::ConfirmationPending { |
| 510 | size: format_size, | 512 | size: format_size, |
| 511 | alpha: format_has_alpha, | 513 | alpha: format_has_alpha, |
| 512 | - dma_negotiation: DmaNegotiation { | 514 | + dma_negotiation: Some(DmaNegotiation { |
| 513 | modifier, | 515 | modifier, |
| 514 | plane_count: plane_count as i32, | 516 | plane_count: plane_count as i32, |
| 515 | - }, | 517 | + }), |
| 516 | }; | 518 | }; |
| 517 | 519 | ||
| 518 | let fixated_format = FormatSet::from_iter([Format { | 520 | let fixated_format = FormatSet::from_iter([Format { |
| @@ -550,12 +552,12 @@ impl PipeWire { | |||
| 550 | CastState::ConfirmationPending { | 552 | CastState::ConfirmationPending { |
| 551 | size, | 553 | size, |
| 552 | alpha, | 554 | alpha, |
| 553 | - dma_negotiation, | 555 | + dma_negotiation: Some(dma_negotiation), |
| 554 | } | 556 | } |
| 555 | | CastState::Ready { | 557 | | CastState::Ready { |
| 556 | size, | 558 | size, |
| 557 | alpha, | 559 | alpha, |
| 558 | - dma_negotiation, | 560 | + dma_negotiation: Some(dma_negotiation), |
| 559 | .. | 561 | .. |
| 560 | } if *alpha == format_has_alpha | 562 | } if *alpha == format_has_alpha |
| 561 | && dma_negotiation.modifier | 563 | && dma_negotiation.modifier |
| @@ -582,7 +584,7 @@ impl PipeWire { | |||
| 582 | *state = CastState::Ready { | 584 | *state = CastState::Ready { |
| 583 | size, | 585 | size, |
| 584 | alpha, | 586 | alpha, |
| 585 | - dma_negotiation, | 587 | + dma_negotiation: Some(dma_negotiation), |
| 586 | damage_tracker, | 588 | damage_tracker, |
| 587 | cursor_damage_tracker, | 589 | cursor_damage_tracker, |
| 588 | last_cursor_location: None, | 590 | last_cursor_location: None, |
| @@ -616,10 +618,10 @@ impl PipeWire { | |||
| 616 | *state = CastState::Ready { | 618 | *state = CastState::Ready { |
| 617 | size: format_size, | 619 | size: format_size, |
| 618 | alpha: format_has_alpha, | 620 | alpha: format_has_alpha, |
| 619 | - dma_negotiation: DmaNegotiation { | 621 | + dma_negotiation: Some(DmaNegotiation { |
| 620 | modifier, | 622 | modifier, |
| 621 | plane_count: plane_count as i32, | 623 | plane_count: plane_count as i32, |
| 622 | - }, | 624 | + }), |
| 623 | damage_tracker: None, | 625 | damage_tracker: None, |
| 624 | cursor_damage_tracker: None, | 626 | cursor_damage_tracker: None, |
| 625 | last_cursor_location: None, | 627 | last_cursor_location: None, |
| @@ -712,77 +714,82 @@ impl PipeWire { | |||
| 712 | let _span = debug_span!("add_buffer", %stream_id).entered(); | 714 | let _span = debug_span!("add_buffer", %stream_id).entered(); |
| 713 | let mut inner = inner.borrow_mut(); | 715 | let mut inner = inner.borrow_mut(); |
| 714 | 716 | ||
| 715 | - let (size, alpha, modifier) = if let CastState::Ready { | 717 | + match inner.state { |
| 716 | - size, | 718 | + CastState::Ready { |
| 717 | - alpha, | 719 | + size, |
| 718 | - dma_negotiation, | 720 | + alpha, |
| 719 | - .. | 721 | + dma_negotiation, |
| 720 | - } = &inner.state | 722 | + .. |
| 721 | - { | 723 | + } => { |
| 722 | - (*size, *alpha, dma_negotiation.modifier) | 724 | + match dma_negotiation { |
| 723 | - } else { | 725 | + Some(DmaNegotiation { modifier, .. }) => { |
| 724 | - trace!("add_buffer, but not ready yet"); | 726 | + trace!("size={size:?}, alpha={alpha}, modifier={modifier:?}"); |
| 725 | - return; | 727 | + unsafe { |
| 726 | - }; | 728 | + let spa_buffer = (*buffer).buffer; |
| 729 | + | ||
| 730 | + let fourcc = if alpha { | ||
| 731 | + Fourcc::Argb8888 | ||
| 732 | + } else { | ||
| 733 | + Fourcc::Xrgb8888 | ||
| 734 | + }; | ||
| 727 | 735 | ||
| 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 | + }; | ||
| 729 | 744 | ||
| 730 | - unsafe { | 745 | + let plane_count = dmabuf.num_planes(); |
| 731 | - let spa_buffer = (*buffer).buffer; | 746 | + assert_eq!((*spa_buffer).n_datas as usize, plane_count); |
| 732 | 747 | ||
| 733 | - let fourcc = if alpha { | 748 | + for (i, (fd, (stride, offset))) in |
| 734 | - Fourcc::Argb8888 | 749 | + zip(dmabuf.handles(), zip(dmabuf.strides(), dmabuf.offsets())) |
| 735 | - } else { | 750 | + .enumerate() |
| 736 | - Fourcc::Xrgb8888 | 751 | + { |
| 737 | - }; | 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 | + } | ||
| 738 | 774 | ||
| 739 | - let dmabuf = match allocate_dmabuf(&gbm, size, fourcc, modifier) { | 775 | + let fd = (*(*spa_buffer).datas).fd; |
| 740 | - Ok(dmabuf) => dmabuf, | 776 | + assert!(inner.dmabufs.insert(fd, dmabuf).is_none()); |
| 741 | - Err(err) => { | 777 | + } |
| 742 | - warn!("error allocating dmabuf: {err:?}"); | 778 | + |
| 743 | - stop_cast(); | 779 | + // During size re-negotiation, the stream sometimes just keeps running, in |
| 744 | - return; | 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 | + } | ||
| 745 | } | 788 | } |
| 746 | - }; | 789 | + }, |
| 747 | - | 790 | + _ => { |
| 748 | - let plane_count = dmabuf.num_planes(); | 791 | + trace!("pw stream: add buffer, but not ready yet"); |
| 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 | - ); | ||
| 776 | } | 792 | } |
| 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_(); | ||
| 786 | } | 793 | } |
| 787 | } | 794 | } |
| 788 | }) | 795 | }) |