forge
Explore
Log in

Don't you dare go Hollow

pw_utils: change early return to match in modifier negotiation

Browse files at this commit

3644e03d2fb229994b324e3a248d2359a2ccdde6wrvsrxParent: aa1f64c
This commit is preparation for supporting shared memory sharing
src/screencasting/pw_utils.rsmodified+218 -202
@@ -448,150 +448,53 @@ impl PipeWire {
448 );448 );
449 inner.min_time_between_frames = min_frame_time;449 inner.min_time_between_frames = min_frame_time;
450 450
451+ // We have following cases when param_changed:
452+ //
453+ // 1. Modifier exists and its flags contain DONT_FIXATE
454+ //
455+ // Do test allocation, set CastState to ConfirmationPending and send param
456+ // again.
457+ //
458+ // 2. Modifier exists and it doesn't need fixation
459+ //
460+ // Do test allocation to ensure the modifier work, then set CastState to
461+ // Ready. Then set buffer to DMA.
462+ //
463+ // 3. Modifier doesn't exist
464+ //
465+ // TODO: set CastState to Ready and set buffer to SHM.
466+
451 let object = pod.as_object().unwrap();467 let object = pod.as_object().unwrap();
452- let Some(prop_modifier) =468+ let maybe_prop_modifier = object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0));
453- object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0))
454- else {
455- warn!("modifier prop missing");
456- stop_cast();
457- return;
458- };
459 469
460- if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) {470+ match maybe_prop_modifier {
461- debug!("fixating the modifier");471+ Some(prop_modifier) if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => {
472+ debug!("fixating the modifier");
462 473
463- let pod_modifier = prop_modifier.value();474+ let pod_modifier = prop_modifier.value();
464- let Ok((_, modifiers)) = PodDeserializer::deserialize_from::<Choice<i64>>(475+ let Ok((_, modifiers)) = PodDeserializer::deserialize_from::<Choice<i64>>(
465- pod_modifier.as_bytes(),476+ pod_modifier.as_bytes(),
466- ) else {477+ ) else {
467- warn!("wrong modifier property type");478+ warn!("wrong modifier property type");
468- stop_cast();
469- return;
470- };
471-
472- let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else {
473- warn!("wrong modifier choice type");
474- stop_cast();
475- return;
476- };
477-
478- let (modifier, plane_count) = match find_preferred_modifier(
479- &gbm,
480- format_size,
481- fourcc,
482- alternatives,
483- ) {
484- Ok(x) => x,
485- Err(err) => {
486- warn!("couldn't find preferred modifier: {err:?}");
487 stop_cast();479 stop_cast();
488 return;480 return;
489- }481+ };
490- };
491-
492- debug!(
493- "allocation successful \
494- (modifier={modifier:?}, plane_count={plane_count}), \
495- moving to confirmation pending"
496- );
497-
498- *state = CastState::ConfirmationPending {
499- size: format_size,
500- alpha: format_has_alpha,
501- dma_negotiation: DmaNegotiation {
502- modifier,
503- plane_count: plane_count as i32,
504- },
505- };
506-
507- let fixated_format = FormatSet::from_iter([Format {
508- code: fourcc,
509- modifier,
510- }]);
511-
512- let mut b1 = Vec::new();
513- let mut b2 = Vec::new();
514-
515- let o1 = make_video_params(
516- &fixated_format,
517- format_size,
518- inner.refresh,
519- format_has_alpha,
520- );
521- let pod1 = make_pod(&mut b1, o1);
522-
523- let o2 = make_video_params(
524- &formats,
525- format_size,
526- inner.refresh,
527- format_has_alpha,
528- );
529- let mut params = [pod1, make_pod(&mut b2, o2)];
530-
531- if let Err(err) = stream.update_params(&mut params) {
532- warn!("error updating stream params: {err:?}");
533- stop_cast();
534- }
535-
536- return;
537- }
538 482
539- // Verify that alpha and modifier didn't change.483+ let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else {
540- let plane_count = match &*state {484+ warn!("wrong modifier choice type");
541- CastState::ConfirmationPending {485+ stop_cast();
542- size,486+ return;
543- alpha,
544- dma_negotiation,
545- }
546- | CastState::Ready {
547- size,
548- alpha,
549- dma_negotiation,
550- ..
551- } if *alpha == format_has_alpha
552- && dma_negotiation.modifier == Modifier::from(format.modifier()) =>
553- {
554- let size = *size;
555- let alpha = *alpha;
556- let dma_negotiation = *dma_negotiation;
557-
558- let (damage_tracker, cursor_damage_tracker) =
559- if let CastState::Ready {
560- damage_tracker,
561- cursor_damage_tracker,
562- ..
563- } = &mut *state
564- {
565- (damage_tracker.take(), cursor_damage_tracker.take())
566- } else {
567- (None, None)
568- };
569-
570- debug!("moving to ready state");
571-
572- *state = CastState::Ready {
573- size,
574- alpha,
575- dma_negotiation,
576- damage_tracker,
577- cursor_damage_tracker,
578- last_cursor_location: None,
579 };487 };
580 488
581- dma_negotiation.plane_count
582- }
583- _ => {
584- // We're negotiating a single modifier, or alpha or modifier changed,
585- // so we need to do a test allocation.
586 let (modifier, plane_count) = match find_preferred_modifier(489 let (modifier, plane_count) = match find_preferred_modifier(
587 &gbm,490 &gbm,
588 format_size,491 format_size,
589 fourcc,492 fourcc,
590- vec![format.modifier() as i64],493+ alternatives,
591 ) {494 ) {
592 Ok(x) => x,495 Ok(x) => x,
593 Err(err) => {496 Err(err) => {
594- warn!("test allocation failed: {err:?}");497+ warn!("couldn't find preferred modifier: {err:?}");
595 stop_cast();498 stop_cast();
596 return;499 return;
597 }500 }
@@ -600,93 +503,206 @@ impl PipeWire {
600 debug!(503 debug!(
601 "allocation successful \504 "allocation successful \
602 (modifier={modifier:?}, plane_count={plane_count}), \505 (modifier={modifier:?}, plane_count={plane_count}), \
603- moving to ready"506+ moving to confirmation pending"
604 );507 );
605 508
606- *state = CastState::Ready {509+ *state = CastState::ConfirmationPending {
607 size: format_size,510 size: format_size,
608 alpha: format_has_alpha,511 alpha: format_has_alpha,
609 dma_negotiation: DmaNegotiation {512 dma_negotiation: DmaNegotiation {
610 modifier,513 modifier,
611 plane_count: plane_count as i32,514 plane_count: plane_count as i32,
612 },515 },
613- damage_tracker: None,
614- cursor_damage_tracker: None,
615- last_cursor_location: None,
616 };516 };
617 517
618- plane_count as i32518+ let fixated_format = FormatSet::from_iter([Format {
619- }519+ code: fourcc,
620- };520+ modifier,
521+ }]);
522+
523+ let mut b1 = Vec::new();
524+ let mut b2 = Vec::new();
525+
526+ let o1 = make_video_params(
527+ &fixated_format,
528+ format_size,
529+ inner.refresh,
530+ format_has_alpha,
531+ );
532+ let pod1 = make_pod(&mut b1, o1);
533+
534+ let o2 = make_video_params(
535+ &formats,
536+ format_size,
537+ inner.refresh,
538+ format_has_alpha,
539+ );
540+ let mut params = [pod1, make_pod(&mut b2, o2)];
621 541
622- // const BPP: u32 = 4;542+ if let Err(err) = stream.update_params(&mut params) {
623- // let stride = format.size().width * BPP;543+ warn!("error updating stream params: {err:?}");
624- // let size = stride * format.size().height;544+ stop_cast();
625-545+ }
626- let o1 = pod::object!(546+ }
627- SpaTypes::ObjectParamBuffers,547+ Some(_) => {
628- ParamType::Buffers,548+ // Verify that alpha and modifier didn't change.
629- Property::new(549+ let plane_count = match &*state {
630- SPA_PARAM_BUFFERS_buffers,550+ CastState::ConfirmationPending {
631- pod::Value::Choice(ChoiceValue::Int(Choice(551+ size,
632- ChoiceFlags::empty(),552+ alpha,
633- ChoiceEnum::Range {553+ dma_negotiation,
634- default: 8,
635- min: 2,
636- max: 16
637 }554 }
638- ))),555+ | CastState::Ready {
639- ),556+ size,
640- Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)),557+ alpha,
641- Property::new(558+ dma_negotiation,
642- SPA_PARAM_BUFFERS_dataType,559+ ..
643- pod::Value::Choice(ChoiceValue::Int(Choice(560+ } if *alpha == format_has_alpha
644- ChoiceFlags::empty(),561+ && dma_negotiation.modifier
645- ChoiceEnum::Flags {562+ == Modifier::from(format.modifier()) =>
646- default: 1 << DataType::DmaBuf.as_raw(),563+ {
647- flags: vec![1 << DataType::DmaBuf.as_raw()],564+ let size = *size;
648- },565+ let alpha = *alpha;
649- ))),566+ let dma_negotiation = *dma_negotiation;
650- ),567+
651- );568+ let (damage_tracker, cursor_damage_tracker) =
569+ if let CastState::Ready {
570+ damage_tracker,
571+ cursor_damage_tracker,
572+ ..
573+ } = &mut *state
574+ {
575+ (damage_tracker.take(), cursor_damage_tracker.take())
576+ } else {
577+ (None, None)
578+ };
579+
580+ debug!("moving to ready state");
581+
582+ *state = CastState::Ready {
583+ size,
584+ alpha,
585+ dma_negotiation,
586+ damage_tracker,
587+ cursor_damage_tracker,
588+ last_cursor_location: None,
589+ };
590+
591+ dma_negotiation.plane_count
592+ }
593+ _ => {
594+ // We're negotiating a single modifier, or alpha or modifier changed,
595+ // so we need to do a test allocation.
596+ let (modifier, plane_count) = match find_preferred_modifier(
597+ &gbm,
598+ format_size,
599+ fourcc,
600+ vec![format.modifier() as i64],
601+ ) {
602+ Ok(x) => x,
603+ Err(err) => {
604+ warn!("test allocation failed: {err:?}");
605+ stop_cast();
606+ return;
607+ }
608+ };
609+
610+ debug!(
611+ "allocation successful \
612+ (modifier={modifier:?}, plane_count={plane_count}), \
613+ moving to ready"
614+ );
615+
616+ *state = CastState::Ready {
617+ size: format_size,
618+ alpha: format_has_alpha,
619+ dma_negotiation: DmaNegotiation {
620+ modifier,
621+ plane_count: plane_count as i32,
622+ },
623+ damage_tracker: None,
624+ cursor_damage_tracker: None,
625+ last_cursor_location: None,
626+ };
627+
628+ plane_count as i32
629+ }
630+ };
652 631
653- let o2 = pod::object!(632+ // const BPP: u32 = 4;
654- SpaTypes::ObjectParamMeta,633+ // let stride = format.size().width * BPP;
655- ParamType::Meta,634+ // let size = stride * format.size().height;
656- Property::new(635+
657- SPA_PARAM_META_type,636+ let o1 = pod::object!(
658- pod::Value::Id(spa::utils::Id(SPA_META_Header))637+ SpaTypes::ObjectParamBuffers,
659- ),638+ ParamType::Buffers,
660- Property::new(639+ Property::new(
661- SPA_PARAM_META_size,640+ SPA_PARAM_BUFFERS_buffers,
662- pod::Value::Int(size_of::<spa_meta_header>() as i32)641+ pod::Value::Choice(ChoiceValue::Int(Choice(
663- ),642+ ChoiceFlags::empty(),
664- );643+ ChoiceEnum::Range {
665- let mut b1 = vec![];644+ default: 8,
666- let mut b2 = vec![];645+ min: 2,
667- let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)];646+ max: 16
668-647+ }
669- let mut b_cursor = vec![];648+ ))),
670- if cursor_mode == CursorMode::Metadata {649+ ),
671- let o_cursor = pod::object!(650+ Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)),
672- SpaTypes::ObjectParamMeta,651+ Property::new(
673- ParamType::Meta,652+ SPA_PARAM_BUFFERS_dataType,
674- Property::new(653+ pod::Value::Choice(ChoiceValue::Int(Choice(
675- SPA_PARAM_META_type,654+ ChoiceFlags::empty(),
676- pod::Value::Id(spa::utils::Id(SPA_META_Cursor))655+ ChoiceEnum::Flags {
677- ),656+ default: 1 << DataType::DmaBuf.as_raw(),
678- Property::new(657+ flags: vec![1 << DataType::DmaBuf.as_raw()],
679- SPA_PARAM_META_size,658+ },
680- pod::Value::Int(CURSOR_META_SIZE as i32)659+ ))),
681- ),660+ ),
682- );661+ );
683- params.push(make_pod(&mut b_cursor, o_cursor));
684- }
685 662
686- if let Err(err) = stream.update_params(&mut params) {663+ let o2 = pod::object!(
687- warn!("error updating stream params: {err:?}");664+ SpaTypes::ObjectParamMeta,
688- stop_cast();665+ ParamType::Meta,
689- }666+ Property::new(
667+ SPA_PARAM_META_type,
668+ pod::Value::Id(spa::utils::Id(SPA_META_Header))
669+ ),
670+ Property::new(
671+ SPA_PARAM_META_size,
672+ pod::Value::Int(size_of::<spa_meta_header>() as i32)
673+ ),
674+ );
675+ let mut b1 = vec![];
676+ let mut b2 = vec![];
677+ let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)];
678+
679+ let mut b_cursor = vec![];
680+ if cursor_mode == CursorMode::Metadata {
681+ let o_cursor = pod::object!(
682+ SpaTypes::ObjectParamMeta,
683+ ParamType::Meta,
684+ Property::new(
685+ SPA_PARAM_META_type,
686+ pod::Value::Id(spa::utils::Id(SPA_META_Cursor))
687+ ),
688+ Property::new(
689+ SPA_PARAM_META_size,
690+ pod::Value::Int(CURSOR_META_SIZE as i32)
691+ ),
692+ );
693+ params.push(make_pod(&mut b_cursor, o_cursor));
694+ }
695+
696+ if let Err(err) = stream.update_params(&mut params) {
697+ warn!("error updating stream params: {err:?}");
698+ stop_cast();
699+ }
700+ }
701+ None => {
702+ warn!("pw stream: modifier prop missing and we don't support shared memory sharing currently");
703+ stop_cast();
704+ }
705+ };
690 }706 }
691 })707 })
692 .add_buffer({708 .add_buffer({