pw_utils: Use early return in modifier fixation
Browse files at this commitb754f26c3d8fea7203c3d5f2f258df927c98a828Parent: 94b7dcd1 changed file +192 -201
- src/screencasting/pw_utils.rs +192 -201
src/screencasting/pw_utils.rsmodified+192 -201
| @@ -587,10 +587,10 @@ impl PipeWire { | |||
| 587 | // Set CastState to Ready and set buffer to SHM. | 587 | // Set CastState to Ready and set buffer to SHM. |
| 588 | 588 | ||
| 589 | let object = pod.as_object().unwrap(); | 589 | let object = pod.as_object().unwrap(); |
| 590 | - let maybe_prop_modifier = | 590 | + let prop_modifier = |
| 591 | object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); | 591 | object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); |
| 592 | 592 | ||
| 593 | - match maybe_prop_modifier { | 593 | + match prop_modifier { |
| 594 | Some(prop_modifier) | 594 | Some(prop_modifier) |
| 595 | if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => | 595 | if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => |
| 596 | { | 596 | { |
| @@ -664,214 +664,205 @@ impl PipeWire { | |||
| 664 | warn!("error updating stream params: {err:?}"); | 664 | warn!("error updating stream params: {err:?}"); |
| 665 | stop_cast(); | 665 | stop_cast(); |
| 666 | } | 666 | } |
| 667 | + | ||
| 668 | + return; | ||
| 667 | } | 669 | } |
| 668 | - _ => { | 670 | + _ => (), |
| 669 | - let o1 = match maybe_prop_modifier { | 671 | + } |
| 670 | - Some(_) => { | ||
| 671 | - // Verify that alpha and modifier didn't change. | ||
| 672 | - let plane_count = match &*state { | ||
| 673 | - CastState::ConfirmationPending { | ||
| 674 | - size, | ||
| 675 | - alpha, | ||
| 676 | - dma_negotiation: Some(dma_negotiation), | ||
| 677 | - } | ||
| 678 | - | CastState::Ready { | ||
| 679 | - size, | ||
| 680 | - alpha, | ||
| 681 | - dma_negotiation: Some(dma_negotiation), | ||
| 682 | - .. | ||
| 683 | - } if *alpha == format_has_alpha | ||
| 684 | - && dma_negotiation.modifier | ||
| 685 | - == Modifier::from(format.modifier()) => | ||
| 686 | - { | ||
| 687 | - let size = *size; | ||
| 688 | - let alpha = *alpha; | ||
| 689 | - let dma_negotiation = *dma_negotiation; | ||
| 690 | - | ||
| 691 | - let (damage_tracker, cursor_damage_tracker) = | ||
| 692 | - if let CastState::Ready { | ||
| 693 | - damage_tracker, | ||
| 694 | - cursor_damage_tracker, | ||
| 695 | - .. | ||
| 696 | - } = &mut *state | ||
| 697 | - { | ||
| 698 | - ( | ||
| 699 | - damage_tracker.take(), | ||
| 700 | - cursor_damage_tracker.take(), | ||
| 701 | - ) | ||
| 702 | - } else { | ||
| 703 | - (None, None) | ||
| 704 | - }; | ||
| 705 | - | ||
| 706 | - debug!("moving to ready state"); | ||
| 707 | - | ||
| 708 | - *state = CastState::Ready { | ||
| 709 | - size, | ||
| 710 | - alpha, | ||
| 711 | - dma_negotiation: Some(dma_negotiation), | ||
| 712 | - damage_tracker, | ||
| 713 | - cursor_damage_tracker, | ||
| 714 | - last_cursor_location: None, | ||
| 715 | - }; | ||
| 716 | - | ||
| 717 | - dma_negotiation.plane_count | ||
| 718 | - } | ||
| 719 | - _ => { | ||
| 720 | - // We're negotiating a single modifier, or alpha or | ||
| 721 | - // modifier changed, so we need to do a test | ||
| 722 | - // allocation. | ||
| 723 | - let (modifier, plane_count) = | ||
| 724 | - match find_preferred_modifier( | ||
| 725 | - &gbm, | ||
| 726 | - format_size, | ||
| 727 | - fourcc, | ||
| 728 | - vec![format.modifier() as i64], | ||
| 729 | - ) { | ||
| 730 | - Ok(x) => x, | ||
| 731 | - Err(err) => { | ||
| 732 | - warn!( | ||
| 733 | - "test allocation failed: {err:?}" | ||
| 734 | - ); | ||
| 735 | - stop_cast(); | ||
| 736 | - return; | ||
| 737 | - } | ||
| 738 | - }; | ||
| 739 | - | ||
| 740 | - debug!( | ||
| 741 | - "allocation successful \ | ||
| 742 | - (modifier={modifier:?}, \ | ||
| 743 | - plane_count={plane_count}), \ | ||
| 744 | - moving to ready" | ||
| 745 | - ); | ||
| 746 | - | ||
| 747 | - *state = CastState::Ready { | ||
| 748 | - size: format_size, | ||
| 749 | - alpha: format_has_alpha, | ||
| 750 | - dma_negotiation: Some(DmaNegotiation { | ||
| 751 | - modifier, | ||
| 752 | - plane_count: plane_count as i32, | ||
| 753 | - }), | ||
| 754 | - damage_tracker: None, | ||
| 755 | - cursor_damage_tracker: None, | ||
| 756 | - last_cursor_location: None, | ||
| 757 | - }; | ||
| 758 | - | ||
| 759 | - plane_count as i32 | ||
| 760 | - } | ||
| 761 | - }; | ||
| 762 | 672 | ||
| 763 | - pod::object!( | 673 | + let o1 = if prop_modifier.is_some() { |
| 764 | - SpaTypes::ObjectParamBuffers, | 674 | + // Verify that alpha and modifier didn't change. |
| 765 | - ParamType::Buffers, | 675 | + let plane_count = match &*state { |
| 766 | - Property::new( | 676 | + CastState::ConfirmationPending { |
| 767 | - SPA_PARAM_BUFFERS_buffers, | 677 | + size, |
| 768 | - pod::Value::Choice(ChoiceValue::Int(Choice( | 678 | + alpha, |
| 769 | - ChoiceFlags::empty(), | 679 | + dma_negotiation: Some(dma_negotiation), |
| 770 | - ChoiceEnum::Range { | 680 | + } |
| 771 | - default: 8, | 681 | + | CastState::Ready { |
| 772 | - min: 2, | 682 | + size, |
| 773 | - max: 16 | 683 | + alpha, |
| 774 | - } | 684 | + dma_negotiation: Some(dma_negotiation), |
| 775 | - ))), | 685 | + .. |
| 776 | - ), | 686 | + } if *alpha == format_has_alpha |
| 777 | - Property::new( | 687 | + && dma_negotiation.modifier |
| 778 | - SPA_PARAM_BUFFERS_blocks, | 688 | + == Modifier::from(format.modifier()) => |
| 779 | - pod::Value::Int(plane_count) | 689 | + { |
| 780 | - ), | 690 | + let size = *size; |
| 781 | - Property::new( | 691 | + let alpha = *alpha; |
| 782 | - SPA_PARAM_BUFFERS_dataType, | 692 | + let dma_negotiation = *dma_negotiation; |
| 783 | - pod::Value::Choice(ChoiceValue::Int(Choice( | 693 | + |
| 784 | - ChoiceFlags::empty(), | 694 | + let (damage_tracker, cursor_damage_tracker) = |
| 785 | - ChoiceEnum::Flags { | 695 | + if let CastState::Ready { |
| 786 | - default: 1 << DataType::DmaBuf.as_raw(), | 696 | + damage_tracker, |
| 787 | - flags: vec![1 << DataType::DmaBuf.as_raw()], | 697 | + cursor_damage_tracker, |
| 788 | - }, | 698 | + .. |
| 789 | - ))), | 699 | + } = &mut *state |
| 790 | - ), | 700 | + { |
| 791 | - ) | 701 | + (damage_tracker.take(), cursor_damage_tracker.take()) |
| 792 | - } | 702 | + } else { |
| 793 | - None => { | 703 | + (None, None) |
| 794 | - *state = CastState::Ready { | ||
| 795 | - size: format_size, | ||
| 796 | - alpha: format_has_alpha, | ||
| 797 | - dma_negotiation: None, | ||
| 798 | - damage_tracker: None, | ||
| 799 | - cursor_damage_tracker: None, | ||
| 800 | - last_cursor_location: None, | ||
| 801 | }; | 704 | }; |
| 802 | - pod::object!( | ||
| 803 | - SpaTypes::ObjectParamBuffers, | ||
| 804 | - ParamType::Buffers, | ||
| 805 | - Property::new( | ||
| 806 | - SPA_PARAM_BUFFERS_buffers, | ||
| 807 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 808 | - ChoiceFlags::empty(), | ||
| 809 | - ChoiceEnum::Range { | ||
| 810 | - default: 8, | ||
| 811 | - min: 2, | ||
| 812 | - max: 16 | ||
| 813 | - } | ||
| 814 | - ))), | ||
| 815 | - ), | ||
| 816 | - Property::new( | ||
| 817 | - SPA_PARAM_BUFFERS_blocks, | ||
| 818 | - pod::Value::Int(SHM_BLOCKS as i32), | ||
| 819 | - ), | ||
| 820 | - Property::new( | ||
| 821 | - SPA_PARAM_BUFFERS_dataType, | ||
| 822 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 823 | - ChoiceFlags::empty(), | ||
| 824 | - ChoiceEnum::Flags { | ||
| 825 | - default: 1 << DataType::MemFd.as_raw(), | ||
| 826 | - flags: vec![1 << DataType::MemFd.as_raw()], | ||
| 827 | - }, | ||
| 828 | - ))), | ||
| 829 | - ), | ||
| 830 | - ) | ||
| 831 | - } | ||
| 832 | - }; | ||
| 833 | 705 | ||
| 834 | - let o2 = pod::object!( | 706 | + debug!("moving to ready state"); |
| 835 | - SpaTypes::ObjectParamMeta, | ||
| 836 | - ParamType::Meta, | ||
| 837 | - Property::new( | ||
| 838 | - SPA_PARAM_META_type, | ||
| 839 | - pod::Value::Id(spa::utils::Id(SPA_META_Header)) | ||
| 840 | - ), | ||
| 841 | - Property::new( | ||
| 842 | - SPA_PARAM_META_size, | ||
| 843 | - pod::Value::Int(size_of::<spa_meta_header>() as i32) | ||
| 844 | - ), | ||
| 845 | - ); | ||
| 846 | 707 | ||
| 847 | - let mut b1 = vec![]; | 708 | + *state = CastState::Ready { |
| 848 | - let mut b2 = vec![]; | 709 | + size, |
| 849 | - | 710 | + alpha, |
| 850 | - let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; | 711 | + dma_negotiation: Some(dma_negotiation), |
| 851 | - | 712 | + damage_tracker, |
| 852 | - let mut b_cursor = vec![]; | 713 | + cursor_damage_tracker, |
| 853 | - if cursor_mode == CursorMode::Metadata { | 714 | + last_cursor_location: None, |
| 854 | - let o_cursor = pod::object!( | 715 | + }; |
| 855 | - SpaTypes::ObjectParamMeta, | 716 | + |
| 856 | - ParamType::Meta, | 717 | + dma_negotiation.plane_count |
| 857 | - Property::new( | ||
| 858 | - SPA_PARAM_META_type, | ||
| 859 | - pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) | ||
| 860 | - ), | ||
| 861 | - Property::new( | ||
| 862 | - SPA_PARAM_META_size, | ||
| 863 | - pod::Value::Int(CURSOR_META_SIZE as i32) | ||
| 864 | - ), | ||
| 865 | - ); | ||
| 866 | - params.push(make_pod(&mut b_cursor, o_cursor)); | ||
| 867 | } | 718 | } |
| 719 | + _ => { | ||
| 720 | + // We're negotiating a single modifier, or alpha or modifier | ||
| 721 | + // changed, so we need to do a test allocation. | ||
| 722 | + let (modifier, plane_count) = match find_preferred_modifier( | ||
| 723 | + &gbm, | ||
| 724 | + format_size, | ||
| 725 | + fourcc, | ||
| 726 | + vec![format.modifier() as i64], | ||
| 727 | + ) { | ||
| 728 | + Ok(x) => x, | ||
| 729 | + Err(err) => { | ||
| 730 | + warn!("test allocation failed: {err:?}"); | ||
| 731 | + stop_cast(); | ||
| 732 | + return; | ||
| 733 | + } | ||
| 734 | + }; | ||
| 868 | 735 | ||
| 869 | - if let Err(err) = stream.update_params(&mut params) { | 736 | + debug!( |
| 870 | - warn!("error updating stream params: {err:?}"); | 737 | + "allocation successful \ |
| 871 | - stop_cast(); | 738 | + (modifier={modifier:?}, plane_count={plane_count}), \ |
| 739 | + moving to ready" | ||
| 740 | + ); | ||
| 741 | + | ||
| 742 | + *state = CastState::Ready { | ||
| 743 | + size: format_size, | ||
| 744 | + alpha: format_has_alpha, | ||
| 745 | + dma_negotiation: Some(DmaNegotiation { | ||
| 746 | + modifier, | ||
| 747 | + plane_count: plane_count as i32, | ||
| 748 | + }), | ||
| 749 | + damage_tracker: None, | ||
| 750 | + cursor_damage_tracker: None, | ||
| 751 | + last_cursor_location: None, | ||
| 752 | + }; | ||
| 753 | + | ||
| 754 | + plane_count as i32 | ||
| 872 | } | 755 | } |
| 873 | - } | 756 | + }; |
| 757 | + | ||
| 758 | + pod::object!( | ||
| 759 | + SpaTypes::ObjectParamBuffers, | ||
| 760 | + ParamType::Buffers, | ||
| 761 | + Property::new( | ||
| 762 | + SPA_PARAM_BUFFERS_buffers, | ||
| 763 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 764 | + ChoiceFlags::empty(), | ||
| 765 | + ChoiceEnum::Range { | ||
| 766 | + default: 8, | ||
| 767 | + min: 2, | ||
| 768 | + max: 16 | ||
| 769 | + } | ||
| 770 | + ))), | ||
| 771 | + ), | ||
| 772 | + Property::new( | ||
| 773 | + SPA_PARAM_BUFFERS_blocks, | ||
| 774 | + pod::Value::Int(plane_count) | ||
| 775 | + ), | ||
| 776 | + Property::new( | ||
| 777 | + SPA_PARAM_BUFFERS_dataType, | ||
| 778 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 779 | + ChoiceFlags::empty(), | ||
| 780 | + ChoiceEnum::Flags { | ||
| 781 | + default: 1 << DataType::DmaBuf.as_raw(), | ||
| 782 | + flags: vec![1 << DataType::DmaBuf.as_raw()], | ||
| 783 | + }, | ||
| 784 | + ))), | ||
| 785 | + ), | ||
| 786 | + ) | ||
| 787 | + } else { | ||
| 788 | + *state = CastState::Ready { | ||
| 789 | + size: format_size, | ||
| 790 | + alpha: format_has_alpha, | ||
| 791 | + dma_negotiation: None, | ||
| 792 | + damage_tracker: None, | ||
| 793 | + cursor_damage_tracker: None, | ||
| 794 | + last_cursor_location: None, | ||
| 795 | + }; | ||
| 796 | + pod::object!( | ||
| 797 | + SpaTypes::ObjectParamBuffers, | ||
| 798 | + ParamType::Buffers, | ||
| 799 | + Property::new( | ||
| 800 | + SPA_PARAM_BUFFERS_buffers, | ||
| 801 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 802 | + ChoiceFlags::empty(), | ||
| 803 | + ChoiceEnum::Range { | ||
| 804 | + default: 8, | ||
| 805 | + min: 2, | ||
| 806 | + max: 16 | ||
| 807 | + } | ||
| 808 | + ))), | ||
| 809 | + ), | ||
| 810 | + Property::new( | ||
| 811 | + SPA_PARAM_BUFFERS_blocks, | ||
| 812 | + pod::Value::Int(SHM_BLOCKS as i32), | ||
| 813 | + ), | ||
| 814 | + Property::new( | ||
| 815 | + SPA_PARAM_BUFFERS_dataType, | ||
| 816 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 817 | + ChoiceFlags::empty(), | ||
| 818 | + ChoiceEnum::Flags { | ||
| 819 | + default: 1 << DataType::MemFd.as_raw(), | ||
| 820 | + flags: vec![1 << DataType::MemFd.as_raw()], | ||
| 821 | + }, | ||
| 822 | + ))), | ||
| 823 | + ), | ||
| 824 | + ) | ||
| 874 | }; | 825 | }; |
| 826 | + | ||
| 827 | + let o2 = pod::object!( | ||
| 828 | + SpaTypes::ObjectParamMeta, | ||
| 829 | + ParamType::Meta, | ||
| 830 | + Property::new( | ||
| 831 | + SPA_PARAM_META_type, | ||
| 832 | + pod::Value::Id(spa::utils::Id(SPA_META_Header)) | ||
| 833 | + ), | ||
| 834 | + Property::new( | ||
| 835 | + SPA_PARAM_META_size, | ||
| 836 | + pod::Value::Int(size_of::<spa_meta_header>() as i32) | ||
| 837 | + ), | ||
| 838 | + ); | ||
| 839 | + | ||
| 840 | + let mut b1 = vec![]; | ||
| 841 | + let mut b2 = vec![]; | ||
| 842 | + | ||
| 843 | + let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; | ||
| 844 | + | ||
| 845 | + let mut b_cursor = vec![]; | ||
| 846 | + if cursor_mode == CursorMode::Metadata { | ||
| 847 | + let o_cursor = pod::object!( | ||
| 848 | + SpaTypes::ObjectParamMeta, | ||
| 849 | + ParamType::Meta, | ||
| 850 | + Property::new( | ||
| 851 | + SPA_PARAM_META_type, | ||
| 852 | + pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) | ||
| 853 | + ), | ||
| 854 | + Property::new( | ||
| 855 | + SPA_PARAM_META_size, | ||
| 856 | + pod::Value::Int(CURSOR_META_SIZE as i32) | ||
| 857 | + ), | ||
| 858 | + ); | ||
| 859 | + params.push(make_pod(&mut b_cursor, o_cursor)); | ||
| 860 | + } | ||
| 861 | + | ||
| 862 | + if let Err(err) = stream.update_params(&mut params) { | ||
| 863 | + warn!("error updating stream params: {err:?}"); | ||
| 864 | + stop_cast(); | ||
| 865 | + } | ||
| 875 | } | 866 | } |
| 876 | }) | 867 | }) |
| 877 | .add_buffer({ | 868 | .add_buffer({ |