Implement PipeWire DMA-BUF modifier negotiation
Browse files at this commit8ff90c4fc2e2aceaf92c7efad8d7248d47100069Parent: 908c8eb4 changed files +422 -143
- Cargo.lock +4 -8
- Cargo.toml +1 -1
- src/niri.rs +8 -0
- src/pw_utils.rs +409 -134
Cargo.lockmodified+4 -8
| @@ -2002,8 +2002,7 @@ dependencies = [ | |||
| 2002 | [[package]] | 2002 | [[package]] |
| 2003 | name = "libspa" | 2003 | name = "libspa" |
| 2004 | version = "0.8.0" | 2004 | version = "0.8.0" |
| 2005 | -source = "registry+https://github.com/rust-lang/crates.io-index" | 2005 | +source = "git+https://gitlab.freedesktop.org/pipewire/pipewire-rs.git#016e554768ae251c8de3724ea4b06f4749a8dd00" |
| 2006 | -checksum = "65f3a4b81b2a2d8c7f300643676202debd1b7c929dbf5c9bb89402ea11d19810" | ||
| 2007 | dependencies = [ | 2006 | dependencies = [ |
| 2008 | "bitflags 2.6.0", | 2007 | "bitflags 2.6.0", |
| 2009 | "cc", | 2008 | "cc", |
| @@ -2019,8 +2018,7 @@ dependencies = [ | |||
| 2019 | [[package]] | 2018 | [[package]] |
| 2020 | name = "libspa-sys" | 2019 | name = "libspa-sys" |
| 2021 | version = "0.8.0" | 2020 | version = "0.8.0" |
| 2022 | -source = "registry+https://github.com/rust-lang/crates.io-index" | 2021 | +source = "git+https://gitlab.freedesktop.org/pipewire/pipewire-rs.git#016e554768ae251c8de3724ea4b06f4749a8dd00" |
| 2023 | -checksum = "bf0d9716420364790e85cbb9d3ac2c950bde16a7dd36f3209b7dfdfc4a24d01f" | ||
| 2024 | dependencies = [ | 2022 | dependencies = [ |
| 2025 | "bindgen", | 2023 | "bindgen", |
| 2026 | "cc", | 2024 | "cc", |
| @@ -2849,8 +2847,7 @@ dependencies = [ | |||
| 2849 | [[package]] | 2847 | [[package]] |
| 2850 | name = "pipewire" | 2848 | name = "pipewire" |
| 2851 | version = "0.8.0" | 2849 | version = "0.8.0" |
| 2852 | -source = "registry+https://github.com/rust-lang/crates.io-index" | 2850 | +source = "git+https://gitlab.freedesktop.org/pipewire/pipewire-rs.git#016e554768ae251c8de3724ea4b06f4749a8dd00" |
| 2853 | -checksum = "08e645ba5c45109106d56610b3ee60eb13a6f2beb8b74f8dc8186cf261788dda" | ||
| 2854 | dependencies = [ | 2851 | dependencies = [ |
| 2855 | "anyhow", | 2852 | "anyhow", |
| 2856 | "bitflags 2.6.0", | 2853 | "bitflags 2.6.0", |
| @@ -2866,8 +2863,7 @@ dependencies = [ | |||
| 2866 | [[package]] | 2863 | [[package]] |
| 2867 | name = "pipewire-sys" | 2864 | name = "pipewire-sys" |
| 2868 | version = "0.8.0" | 2865 | version = "0.8.0" |
| 2869 | -source = "registry+https://github.com/rust-lang/crates.io-index" | 2866 | +source = "git+https://gitlab.freedesktop.org/pipewire/pipewire-rs.git#016e554768ae251c8de3724ea4b06f4749a8dd00" |
| 2870 | -checksum = "849e188f90b1dda88fe2bfe1ad31fe5f158af2c98f80fb5d13726c44f3f01112" | ||
| 2871 | dependencies = [ | 2867 | dependencies = [ |
| 2872 | "bindgen", | 2868 | "bindgen", |
| 2873 | "libspa-sys", | 2869 | "libspa-sys", |
Cargo.tomlmodified+1 -1
| @@ -66,7 +66,7 @@ notify-rust = { version = "~4.10.0", optional = true } | |||
| 66 | ordered-float = "4.2.1" | 66 | ordered-float = "4.2.1" |
| 67 | pango = { version = "0.20.0", features = ["v1_44"] } | 67 | pango = { version = "0.20.0", features = ["v1_44"] } |
| 68 | pangocairo = "0.20.0" | 68 | pangocairo = "0.20.0" |
| 69 | -pipewire = { version = "0.8.0", optional = true } | 69 | +pipewire = { git = "https://gitlab.freedesktop.org/pipewire/pipewire-rs.git", optional = true, features = ["v0_3_33"] } |
| 70 | png = "0.17.13" | 70 | png = "0.17.13" |
| 71 | portable-atomic = { version = "1.7.0", default-features = false, features = ["float"] } | 71 | portable-atomic = { version = "1.7.0", default-features = false, features = ["float"] } |
| 72 | profiling = "1.0.15" | 72 | profiling = "1.0.15" |
src/niri.rsmodified+8 -0
| @@ -1369,8 +1369,16 @@ impl State { | |||
| 1369 | } | 1369 | } |
| 1370 | }; | 1370 | }; |
| 1371 | 1371 | ||
| 1372 | + let render_formats = self | ||
| 1373 | + .backend | ||
| 1374 | + .with_primary_renderer(|renderer| { | ||
| 1375 | + renderer.egl_context().dmabuf_render_formats().clone() | ||
| 1376 | + }) | ||
| 1377 | + .unwrap_or_default(); | ||
| 1378 | + | ||
| 1372 | let res = pw.start_cast( | 1379 | let res = pw.start_cast( |
| 1373 | gbm, | 1380 | gbm, |
| 1381 | + render_formats, | ||
| 1374 | session_id, | 1382 | session_id, |
| 1375 | target, | 1383 | target, |
| 1376 | size, | 1384 | size, |
src/pw_utils.rsmodified+409 -134
| @@ -1,6 +1,7 @@ | |||
| 1 | use std::cell::{Cell, RefCell}; | 1 | use std::cell::{Cell, RefCell}; |
| 2 | use std::collections::HashMap; | 2 | use std::collections::HashMap; |
| 3 | use std::io::Cursor; | 3 | use std::io::Cursor; |
| 4 | +use std::iter::zip; | ||
| 4 | use std::mem; | 5 | use std::mem; |
| 5 | use std::os::fd::{AsFd, AsRawFd, BorrowedFd}; | 6 | use std::os::fd::{AsFd, AsRawFd, BorrowedFd}; |
| 6 | use std::rc::Rc; | 7 | use std::rc::Rc; |
| @@ -16,16 +17,19 @@ use pipewire::spa::param::format::{FormatProperties, MediaSubtype, MediaType}; | |||
| 16 | use pipewire::spa::param::format_utils::parse_format; | 17 | use pipewire::spa::param::format_utils::parse_format; |
| 17 | use pipewire::spa::param::video::{VideoFormat, VideoInfoRaw}; | 18 | use pipewire::spa::param::video::{VideoFormat, VideoInfoRaw}; |
| 18 | use pipewire::spa::param::ParamType; | 19 | use pipewire::spa::param::ParamType; |
| 20 | +use pipewire::spa::pod::deserialize::PodDeserializer; | ||
| 19 | use pipewire::spa::pod::serialize::PodSerializer; | 21 | use pipewire::spa::pod::serialize::PodSerializer; |
| 20 | -use pipewire::spa::pod::{self, ChoiceValue, Pod, Property, PropertyFlags}; | 22 | +use pipewire::spa::pod::{self, ChoiceValue, Pod, PodPropFlags, Property, PropertyFlags}; |
| 21 | use pipewire::spa::sys::*; | 23 | use pipewire::spa::sys::*; |
| 22 | use pipewire::spa::utils::{ | 24 | use pipewire::spa::utils::{ |
| 23 | Choice, ChoiceEnum, ChoiceFlags, Direction, Fraction, Rectangle, SpaTypes, | 25 | Choice, ChoiceEnum, ChoiceFlags, Direction, Fraction, Rectangle, SpaTypes, |
| 24 | }; | 26 | }; |
| 27 | +use pipewire::spa::{self}; | ||
| 25 | use pipewire::stream::{Stream, StreamFlags, StreamListener, StreamState}; | 28 | use pipewire::stream::{Stream, StreamFlags, StreamListener, StreamState}; |
| 26 | use smithay::backend::allocator::dmabuf::{AsDmabuf, Dmabuf}; | 29 | use smithay::backend::allocator::dmabuf::{AsDmabuf, Dmabuf}; |
| 30 | +use smithay::backend::allocator::format::FormatSet; | ||
| 27 | use smithay::backend::allocator::gbm::{GbmBuffer, GbmBufferFlags, GbmDevice}; | 31 | use smithay::backend::allocator::gbm::{GbmBuffer, GbmBufferFlags, GbmDevice}; |
| 28 | -use smithay::backend::allocator::Fourcc; | 32 | +use smithay::backend::allocator::{Format, Fourcc}; |
| 29 | use smithay::backend::drm::DrmDeviceFd; | 33 | use smithay::backend::drm::DrmDeviceFd; |
| 30 | use smithay::backend::renderer::element::RenderElement; | 34 | use smithay::backend::renderer::element::RenderElement; |
| 31 | use smithay::backend::renderer::gles::GlesRenderer; | 35 | use smithay::backend::renderer::gles::GlesRenderer; |
| @@ -57,22 +61,32 @@ pub struct Cast { | |||
| 57 | _listener: StreamListener<()>, | 61 | _listener: StreamListener<()>, |
| 58 | pub is_active: Rc<Cell<bool>>, | 62 | pub is_active: Rc<Cell<bool>>, |
| 59 | pub target: CastTarget, | 63 | pub target: CastTarget, |
| 60 | - pub size: Rc<Cell<CastSize>>, | 64 | + formats: FormatSet, |
| 61 | - pub refresh: u32, | 65 | + state: Rc<RefCell<CastState>>, |
| 66 | + refresh: Rc<Cell<u32>>, | ||
| 62 | offer_alpha: bool, | 67 | offer_alpha: bool, |
| 63 | pub cursor_mode: CursorMode, | 68 | pub cursor_mode: CursorMode, |
| 64 | pub last_frame_time: Duration, | 69 | pub last_frame_time: Duration, |
| 65 | - pub min_time_between_frames: Rc<Cell<Duration>>, | 70 | + min_time_between_frames: Rc<Cell<Duration>>, |
| 66 | - pub dmabufs: Rc<RefCell<HashMap<i32, Dmabuf>>>, | 71 | + dmabufs: Rc<RefCell<HashMap<i64, Dmabuf>>>, |
| 67 | } | 72 | } |
| 68 | 73 | ||
| 69 | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] | 74 | +#[derive(Debug)] |
| 70 | -pub enum CastSize { | 75 | +pub enum CastState { |
| 71 | - InitialPending(Size<i32, Physical>), | 76 | + ResizePending { |
| 72 | - Ready(Size<i32, Physical>), | 77 | + pending_size: Size<u32, Physical>, |
| 73 | - ChangePending { | 78 | + }, |
| 74 | - last_negotiated: Size<i32, Physical>, | 79 | + ConfirmationPending { |
| 75 | - pending: Size<i32, Physical>, | 80 | + size: Size<u32, Physical>, |
| 81 | + alpha: bool, | ||
| 82 | + modifier: Modifier, | ||
| 83 | + plane_count: i32, | ||
| 84 | + }, | ||
| 85 | + Ready { | ||
| 86 | + size: Size<u32, Physical>, | ||
| 87 | + alpha: bool, | ||
| 88 | + modifier: Modifier, | ||
| 89 | + plane_count: i32, | ||
| 76 | }, | 90 | }, |
| 77 | } | 91 | } |
| 78 | 92 | ||
| @@ -89,17 +103,17 @@ pub enum CastTarget { | |||
| 89 | } | 103 | } |
| 90 | 104 | ||
| 91 | macro_rules! make_params { | 105 | macro_rules! make_params { |
| 92 | - ($params:ident, $size:expr, $refresh:expr, $alpha:expr) => { | 106 | + ($params:ident, $formats:expr, $size:expr, $refresh:expr, $alpha:expr) => { |
| 93 | let mut b1 = Vec::new(); | 107 | let mut b1 = Vec::new(); |
| 94 | let mut b2 = Vec::new(); | 108 | let mut b2 = Vec::new(); |
| 95 | 109 | ||
| 96 | - let o1 = make_video_params($size, $refresh, false); | 110 | + let o1 = make_video_params($formats, $size, $refresh, false); |
| 97 | let pod1 = make_pod(&mut b1, o1); | 111 | let pod1 = make_pod(&mut b1, o1); |
| 98 | 112 | ||
| 99 | let mut p1; | 113 | let mut p1; |
| 100 | let mut p2; | 114 | let mut p2; |
| 101 | $params = if $alpha { | 115 | $params = if $alpha { |
| 102 | - let o2 = make_video_params($size, $refresh, true); | 116 | + let o2 = make_video_params($formats, $size, $refresh, true); |
| 103 | p2 = [pod1, make_pod(&mut b2, o2)]; | 117 | p2 = [pod1, make_pod(&mut b2, o2)]; |
| 104 | &mut p2[..] | 118 | &mut p2[..] |
| 105 | } else { | 119 | } else { |
| @@ -157,6 +171,7 @@ impl PipeWire { | |||
| 157 | pub fn start_cast( | 171 | pub fn start_cast( |
| 158 | &self, | 172 | &self, |
| 159 | gbm: GbmDevice<DrmDeviceFd>, | 173 | gbm: GbmDevice<DrmDeviceFd>, |
| 174 | + formats: FormatSet, | ||
| 160 | session_id: usize, | 175 | session_id: usize, |
| 161 | target: CastTarget, | 176 | target: CastTarget, |
| 162 | size: Size<i32, Physical>, | 177 | size: Size<i32, Physical>, |
| @@ -190,10 +205,10 @@ impl PipeWire { | |||
| 190 | let is_active = Rc::new(Cell::new(false)); | 205 | let is_active = Rc::new(Cell::new(false)); |
| 191 | let min_time_between_frames = Rc::new(Cell::new(Duration::ZERO)); | 206 | let min_time_between_frames = Rc::new(Cell::new(Duration::ZERO)); |
| 192 | let dmabufs = Rc::new(RefCell::new(HashMap::new())); | 207 | let dmabufs = Rc::new(RefCell::new(HashMap::new())); |
| 193 | - let negotiated_alpha = Rc::new(Cell::new(false)); | 208 | + let refresh = Rc::new(Cell::new(refresh)); |
| 194 | 209 | ||
| 195 | - let pending_size = size; | 210 | + let pending_size = Size::from((size.w as u32, size.h as u32)); |
| 196 | - let size = Rc::new(Cell::new(CastSize::InitialPending(size))); | 211 | + let state = Rc::new(RefCell::new(CastState::ResizePending { pending_size })); |
| 197 | 212 | ||
| 198 | let listener = stream | 213 | let listener = stream |
| 199 | .add_local_listener_with_user_data(()) | 214 | .add_local_listener_with_user_data(()) |
| @@ -244,8 +259,11 @@ impl PipeWire { | |||
| 244 | }) | 259 | }) |
| 245 | .param_changed({ | 260 | .param_changed({ |
| 246 | let min_time_between_frames = min_time_between_frames.clone(); | 261 | let min_time_between_frames = min_time_between_frames.clone(); |
| 247 | - let size = size.clone(); | 262 | + let stop_cast = stop_cast.clone(); |
| 248 | - let negotiated_alpha = negotiated_alpha.clone(); | 263 | + let state = state.clone(); |
| 264 | + let gbm = gbm.clone(); | ||
| 265 | + let formats = formats.clone(); | ||
| 266 | + let refresh = refresh.clone(); | ||
| 249 | move |stream, (), id, pod| { | 267 | move |stream, (), id, pod| { |
| 250 | let id = ParamType::from_raw(id); | 268 | let id = ParamType::from_raw(id); |
| 251 | trace!(?id, "pw stream: param_changed"); | 269 | trace!(?id, "pw stream: param_changed"); |
| @@ -270,22 +288,28 @@ impl PipeWire { | |||
| 270 | 288 | ||
| 271 | let mut format = VideoInfoRaw::new(); | 289 | let mut format = VideoInfoRaw::new(); |
| 272 | format.parse(pod).unwrap(); | 290 | format.parse(pod).unwrap(); |
| 273 | - trace!("pw stream: got format = {format:?}"); | 291 | + debug!("pw stream: got format = {format:?}"); |
| 274 | 292 | ||
| 275 | - let expected_size = size.get().expected_format_size(); | 293 | + let format_size = Size::from((format.size().width, format.size().height)); |
| 276 | - let format_size = | ||
| 277 | - Size::from((format.size().width as i32, format.size().height as i32)); | ||
| 278 | 294 | ||
| 279 | - if format_size == expected_size { | 295 | + let mut state = state.borrow_mut(); |
| 280 | - size.set(CastSize::Ready(expected_size)); | 296 | + if format_size != state.expected_format_size() { |
| 281 | - } else { | 297 | + if !matches!(&*state, CastState::ResizePending { .. }) { |
| 282 | - size.set(CastSize::ChangePending { | 298 | + warn!("pw stream: wrong size, but we're not resizing"); |
| 283 | - last_negotiated: format_size, | 299 | + stop_cast(); |
| 284 | - pending: expected_size, | 300 | + return; |
| 285 | - }); | 301 | + } |
| 302 | + | ||
| 303 | + debug!("pw stream: wrong size, waiting"); | ||
| 304 | + return; | ||
| 286 | } | 305 | } |
| 287 | 306 | ||
| 288 | - negotiated_alpha.set(format.format() == VideoFormat::BGRA); | 307 | + let format_has_alpha = format.format() == VideoFormat::BGRA; |
| 308 | + let fourcc = if format_has_alpha { | ||
| 309 | + Fourcc::Argb8888 | ||
| 310 | + } else { | ||
| 311 | + Fourcc::Xrgb8888 | ||
| 312 | + }; | ||
| 289 | 313 | ||
| 290 | let max_frame_rate = format.max_framerate(); | 314 | let max_frame_rate = format.max_framerate(); |
| 291 | // Subtract 0.5 ms to improve edge cases when equal to refresh rate. | 315 | // Subtract 0.5 ms to improve edge cases when equal to refresh rate. |
| @@ -294,9 +318,161 @@ impl PipeWire { | |||
| 294 | ) - Duration::from_micros(500); | 318 | ) - Duration::from_micros(500); |
| 295 | min_time_between_frames.set(min_frame_time); | 319 | min_time_between_frames.set(min_frame_time); |
| 296 | 320 | ||
| 297 | - const BPP: u32 = 4; | 321 | + let object = pod.as_object().unwrap(); |
| 298 | - let stride = format.size().width * BPP; | 322 | + let Some(prop_modifier) = |
| 299 | - let size = stride * format.size().height; | 323 | + object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)) |
| 324 | + else { | ||
| 325 | + warn!("pw stream: modifier prop missing"); | ||
| 326 | + stop_cast(); | ||
| 327 | + return; | ||
| 328 | + }; | ||
| 329 | + | ||
| 330 | + if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) { | ||
| 331 | + debug!("pw stream: fixating the modifier"); | ||
| 332 | + | ||
| 333 | + let pod_modifier = prop_modifier.value(); | ||
| 334 | + let Ok((_, modifiers)) = PodDeserializer::deserialize_from::<Choice<i64>>( | ||
| 335 | + pod_modifier.as_bytes(), | ||
| 336 | + ) else { | ||
| 337 | + warn!("pw stream: wrong modifier property type"); | ||
| 338 | + stop_cast(); | ||
| 339 | + return; | ||
| 340 | + }; | ||
| 341 | + | ||
| 342 | + let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else { | ||
| 343 | + warn!("pw stream: wrong modifier choice type"); | ||
| 344 | + stop_cast(); | ||
| 345 | + return; | ||
| 346 | + }; | ||
| 347 | + | ||
| 348 | + let (modifier, plane_count) = match find_preferred_modifier( | ||
| 349 | + &gbm, | ||
| 350 | + format_size, | ||
| 351 | + fourcc, | ||
| 352 | + alternatives, | ||
| 353 | + ) { | ||
| 354 | + Ok(x) => x, | ||
| 355 | + Err(err) => { | ||
| 356 | + warn!("pw stream: couldn't find preferred modifier: {err:?}"); | ||
| 357 | + stop_cast(); | ||
| 358 | + return; | ||
| 359 | + } | ||
| 360 | + }; | ||
| 361 | + | ||
| 362 | + debug!( | ||
| 363 | + "pw stream: allocation successful \ | ||
| 364 | + (modifier={modifier:?}, plane_count={plane_count}), \ | ||
| 365 | + moving to confirmation pending" | ||
| 366 | + ); | ||
| 367 | + | ||
| 368 | + *state = CastState::ConfirmationPending { | ||
| 369 | + size: format_size, | ||
| 370 | + alpha: format_has_alpha, | ||
| 371 | + modifier, | ||
| 372 | + plane_count: plane_count as i32, | ||
| 373 | + }; | ||
| 374 | + | ||
| 375 | + let fixated_format = FormatSet::from_iter([Format { | ||
| 376 | + code: fourcc, | ||
| 377 | + modifier, | ||
| 378 | + }]); | ||
| 379 | + | ||
| 380 | + let mut b1 = Vec::new(); | ||
| 381 | + let mut b2 = Vec::new(); | ||
| 382 | + | ||
| 383 | + let o1 = make_video_params( | ||
| 384 | + &fixated_format, | ||
| 385 | + format_size, | ||
| 386 | + refresh.get(), | ||
| 387 | + format_has_alpha, | ||
| 388 | + ); | ||
| 389 | + let pod1 = make_pod(&mut b1, o1); | ||
| 390 | + | ||
| 391 | + let o2 = make_video_params( | ||
| 392 | + &formats, | ||
| 393 | + format_size, | ||
| 394 | + refresh.get(), | ||
| 395 | + format_has_alpha, | ||
| 396 | + ); | ||
| 397 | + let mut params = [pod1, make_pod(&mut b2, o2)]; | ||
| 398 | + | ||
| 399 | + if let Err(err) = stream.update_params(&mut params) { | ||
| 400 | + warn!("error updating stream params: {err:?}"); | ||
| 401 | + stop_cast(); | ||
| 402 | + } | ||
| 403 | + | ||
| 404 | + return; | ||
| 405 | + } | ||
| 406 | + | ||
| 407 | + // Verify that alpha and modifier didn't change. | ||
| 408 | + let plane_count = match &*state { | ||
| 409 | + CastState::ConfirmationPending { | ||
| 410 | + size, | ||
| 411 | + alpha, | ||
| 412 | + modifier, | ||
| 413 | + plane_count, | ||
| 414 | + } | ||
| 415 | + | CastState::Ready { | ||
| 416 | + size, | ||
| 417 | + alpha, | ||
| 418 | + modifier, | ||
| 419 | + plane_count, | ||
| 420 | + } if *alpha == format_has_alpha | ||
| 421 | + && *modifier == Modifier::from(format.modifier()) => | ||
| 422 | + { | ||
| 423 | + let size = *size; | ||
| 424 | + let alpha = *alpha; | ||
| 425 | + let modifier = *modifier; | ||
| 426 | + let plane_count = *plane_count; | ||
| 427 | + | ||
| 428 | + debug!("pw stream: moving to ready state"); | ||
| 429 | + | ||
| 430 | + *state = CastState::Ready { | ||
| 431 | + size, | ||
| 432 | + alpha, | ||
| 433 | + modifier, | ||
| 434 | + plane_count, | ||
| 435 | + }; | ||
| 436 | + | ||
| 437 | + plane_count | ||
| 438 | + } | ||
| 439 | + _ => { | ||
| 440 | + // We're negotiating a single modifier, or alpha or modifier changed, | ||
| 441 | + // so we need to do a test allocation. | ||
| 442 | + let (modifier, plane_count) = match find_preferred_modifier( | ||
| 443 | + &gbm, | ||
| 444 | + format_size, | ||
| 445 | + fourcc, | ||
| 446 | + vec![format.modifier() as i64], | ||
| 447 | + ) { | ||
| 448 | + Ok(x) => x, | ||
| 449 | + Err(err) => { | ||
| 450 | + warn!("pw stream: test allocation failed: {err:?}"); | ||
| 451 | + stop_cast(); | ||
| 452 | + return; | ||
| 453 | + } | ||
| 454 | + }; | ||
| 455 | + | ||
| 456 | + debug!( | ||
| 457 | + "pw stream: allocation successful \ | ||
| 458 | + (modifier={modifier:?}, plane_count={plane_count}), \ | ||
| 459 | + moving to ready" | ||
| 460 | + ); | ||
| 461 | + | ||
| 462 | + *state = CastState::Ready { | ||
| 463 | + size: format_size, | ||
| 464 | + alpha: format_has_alpha, | ||
| 465 | + modifier, | ||
| 466 | + plane_count: plane_count as i32, | ||
| 467 | + }; | ||
| 468 | + | ||
| 469 | + plane_count as i32 | ||
| 470 | + } | ||
| 471 | + }; | ||
| 472 | + | ||
| 473 | + // const BPP: u32 = 4; | ||
| 474 | + // let stride = format.size().width * BPP; | ||
| 475 | + // let size = stride * format.size().height; | ||
| 300 | 476 | ||
| 301 | let o1 = pod::object!( | 477 | let o1 = pod::object!( |
| 302 | SpaTypes::ObjectParamBuffers, | 478 | SpaTypes::ObjectParamBuffers, |
| @@ -312,10 +488,10 @@ impl PipeWire { | |||
| 312 | } | 488 | } |
| 313 | ))), | 489 | ))), |
| 314 | ), | 490 | ), |
| 315 | - Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(1)), | 491 | + Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), |
| 316 | - Property::new(SPA_PARAM_BUFFERS_size, pod::Value::Int(size as i32)), | 492 | + // Property::new(SPA_PARAM_BUFFERS_size, pod::Value::Int(size as i32)), |
| 317 | - Property::new(SPA_PARAM_BUFFERS_stride, pod::Value::Int(stride as i32)), | 493 | + // Property::new(SPA_PARAM_BUFFERS_stride, pod::Value::Int(stride as i32)), |
| 318 | - Property::new(SPA_PARAM_BUFFERS_align, pod::Value::Int(16)), | 494 | + // Property::new(SPA_PARAM_BUFFERS_align, pod::Value::Int(16)), |
| 319 | Property::new( | 495 | Property::new( |
| 320 | SPA_PARAM_BUFFERS_dataType, | 496 | SPA_PARAM_BUFFERS_dataType, |
| 321 | pod::Value::Choice(ChoiceValue::Int(Choice( | 497 | pod::Value::Choice(ChoiceValue::Int(Choice( |
| @@ -345,25 +521,38 @@ impl PipeWire { | |||
| 345 | let mut params = [ | 521 | let mut params = [ |
| 346 | make_pod(&mut b1, o1), // make_pod(&mut b2, o2) | 522 | make_pod(&mut b1, o1), // make_pod(&mut b2, o2) |
| 347 | ]; | 523 | ]; |
| 348 | - stream.update_params(&mut params).unwrap(); | 524 | + |
| 525 | + if let Err(err) = stream.update_params(&mut params) { | ||
| 526 | + warn!("error updating stream params: {err:?}"); | ||
| 527 | + stop_cast(); | ||
| 528 | + } | ||
| 349 | } | 529 | } |
| 350 | }) | 530 | }) |
| 351 | .add_buffer({ | 531 | .add_buffer({ |
| 352 | let dmabufs = dmabufs.clone(); | 532 | let dmabufs = dmabufs.clone(); |
| 353 | let stop_cast = stop_cast.clone(); | 533 | let stop_cast = stop_cast.clone(); |
| 354 | - let size = size.clone(); | 534 | + let state = state.clone(); |
| 355 | - let negotiated_alpha = negotiated_alpha.clone(); | ||
| 356 | move |stream, (), buffer| { | 535 | move |stream, (), buffer| { |
| 357 | - let size = size.get().negotiated_size(); | 536 | + let (size, alpha, modifier) = if let CastState::Ready { |
| 358 | - let alpha = negotiated_alpha.get(); | 537 | + size, |
| 359 | - trace!("pw stream: add_buffer, size={:?}, alpha={alpha}", size); | 538 | + alpha, |
| 360 | - let size = size.expect("size must be negotiated to allocate buffers"); | 539 | + modifier, |
| 540 | + .. | ||
| 541 | + } = &*state.borrow() | ||
| 542 | + { | ||
| 543 | + (*size, *alpha, *modifier) | ||
| 544 | + } else { | ||
| 545 | + trace!("pw stream: add buffer, but not ready yet"); | ||
| 546 | + return; | ||
| 547 | + }; | ||
| 548 | + | ||
| 549 | + trace!( | ||
| 550 | + "pw stream: add_buffer, size={size:?}, alpha={alpha}, \ | ||
| 551 | + modifier={modifier:?}" | ||
| 552 | + ); | ||
| 361 | 553 | ||
| 362 | unsafe { | 554 | unsafe { |
| 363 | let spa_buffer = (*buffer).buffer; | 555 | let spa_buffer = (*buffer).buffer; |
| 364 | - let spa_data = (*spa_buffer).datas; | ||
| 365 | - assert!((*spa_buffer).n_datas > 0); | ||
| 366 | - assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0); | ||
| 367 | 556 | ||
| 368 | let fourcc = if alpha { | 557 | let fourcc = if alpha { |
| 369 | Fourcc::Argb8888 | 558 | Fourcc::Argb8888 |
| @@ -371,36 +560,29 @@ impl PipeWire { | |||
| 371 | Fourcc::Xrgb8888 | 560 | Fourcc::Xrgb8888 |
| 372 | }; | 561 | }; |
| 373 | 562 | ||
| 374 | - let bo = match gbm.create_buffer_object::<()>( | 563 | + let dmabuf = match allocate_dmabuf(&gbm, size, fourcc, modifier) { |
| 375 | - size.w as u32, | ||
| 376 | - size.h as u32, | ||
| 377 | - fourcc, | ||
| 378 | - GbmBufferFlags::RENDERING, | ||
| 379 | - ) { | ||
| 380 | - Ok(bo) => bo, | ||
| 381 | - Err(err) => { | ||
| 382 | - warn!("error creating GBM buffer object: {err:?}"); | ||
| 383 | - stop_cast(); | ||
| 384 | - return; | ||
| 385 | - } | ||
| 386 | - }; | ||
| 387 | - let buffer = GbmBuffer::from_bo(bo, true); | ||
| 388 | - let dmabuf = match buffer.export() { | ||
| 389 | Ok(dmabuf) => dmabuf, | 564 | Ok(dmabuf) => dmabuf, |
| 390 | Err(err) => { | 565 | Err(err) => { |
| 391 | - warn!("error exporting GBM buffer object as dmabuf: {err:?}"); | 566 | + warn!("error allocating dmabuf: {err:?}"); |
| 392 | stop_cast(); | 567 | stop_cast(); |
| 393 | return; | 568 | return; |
| 394 | } | 569 | } |
| 395 | }; | 570 | }; |
| 396 | 571 | ||
| 397 | - let fd = dmabuf.handles().next().unwrap().as_raw_fd(); | 572 | + let plane_count = dmabuf.num_planes(); |
| 573 | + assert_eq!((*spa_buffer).n_datas as usize, plane_count); | ||
| 574 | + | ||
| 575 | + for (i, fd) in dmabuf.handles().enumerate() { | ||
| 576 | + let spa_data = (*spa_buffer).datas.add(i); | ||
| 577 | + assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0); | ||
| 398 | 578 | ||
| 399 | - (*spa_data).type_ = DataType::DmaBuf.as_raw(); | 579 | + (*spa_data).type_ = DataType::DmaBuf.as_raw(); |
| 400 | - (*spa_data).maxsize = dmabuf.strides().next().unwrap() * size.h as u32; | 580 | + (*spa_data).maxsize = 1; |
| 401 | - (*spa_data).fd = fd as i64; | 581 | + (*spa_data).fd = fd.as_raw_fd() as i64; |
| 402 | - (*spa_data).flags = SPA_DATA_FLAG_READWRITE; | 582 | + (*spa_data).flags = SPA_DATA_FLAG_READWRITE; |
| 583 | + } | ||
| 403 | 584 | ||
| 585 | + let fd = (*(*spa_buffer).datas).fd; | ||
| 404 | assert!(dmabufs.borrow_mut().insert(fd, dmabuf).is_none()); | 586 | assert!(dmabufs.borrow_mut().insert(fd, dmabuf).is_none()); |
| 405 | } | 587 | } |
| 406 | 588 | ||
| @@ -421,7 +603,7 @@ impl PipeWire { | |||
| 421 | let spa_data = (*spa_buffer).datas; | 603 | let spa_data = (*spa_buffer).datas; |
| 422 | assert!((*spa_buffer).n_datas > 0); | 604 | assert!((*spa_buffer).n_datas > 0); |
| 423 | 605 | ||
| 424 | - let fd = (*spa_data).fd as i32; | 606 | + let fd = (*spa_data).fd; |
| 425 | dmabufs.borrow_mut().remove(&fd); | 607 | dmabufs.borrow_mut().remove(&fd); |
| 426 | } | 608 | } |
| 427 | } | 609 | } |
| @@ -429,10 +611,10 @@ impl PipeWire { | |||
| 429 | .register() | 611 | .register() |
| 430 | .unwrap(); | 612 | .unwrap(); |
| 431 | 613 | ||
| 432 | - trace!("starting pw stream with size={pending_size:?}, refresh={refresh}"); | 614 | + trace!("starting pw stream with size={pending_size:?}, refresh={refresh:?}"); |
| 433 | 615 | ||
| 434 | let params; | 616 | let params; |
| 435 | - make_params!(params, pending_size, refresh, alpha); | 617 | + make_params!(params, &formats, pending_size, refresh.get(), alpha); |
| 436 | stream | 618 | stream |
| 437 | .connect( | 619 | .connect( |
| 438 | Direction::Output, | 620 | Direction::Output, |
| @@ -448,7 +630,8 @@ impl PipeWire { | |||
| 448 | _listener: listener, | 630 | _listener: listener, |
| 449 | is_active, | 631 | is_active, |
| 450 | target, | 632 | target, |
| 451 | - size, | 633 | + formats, |
| 634 | + state, | ||
| 452 | refresh, | 635 | refresh, |
| 453 | offer_alpha: alpha, | 636 | offer_alpha: alpha, |
| 454 | cursor_mode, | 637 | cursor_mode, |
| @@ -462,12 +645,14 @@ impl PipeWire { | |||
| 462 | 645 | ||
| 463 | impl Cast { | 646 | impl Cast { |
| 464 | pub fn ensure_size(&self, size: Size<i32, Physical>) -> anyhow::Result<CastSizeChange> { | 647 | pub fn ensure_size(&self, size: Size<i32, Physical>) -> anyhow::Result<CastSizeChange> { |
| 465 | - let current_size = self.size.get(); | 648 | + let new_size = Size::from((size.w as u32, size.h as u32)); |
| 466 | - if current_size == CastSize::Ready(size) { | 649 | + |
| 650 | + let mut state = self.state.borrow_mut(); | ||
| 651 | + if matches!(&*state, CastState::Ready { size, .. } if *size == new_size) { | ||
| 467 | return Ok(CastSizeChange::Ready); | 652 | return Ok(CastSizeChange::Ready); |
| 468 | } | 653 | } |
| 469 | 654 | ||
| 470 | - if current_size.pending_size() == Some(size) { | 655 | + if state.pending_size() == Some(new_size) { |
| 471 | debug!("stream size still hasn't changed, skipping frame"); | 656 | debug!("stream size still hasn't changed, skipping frame"); |
| 472 | return Ok(CastSizeChange::Pending); | 657 | return Ok(CastSizeChange::Pending); |
| 473 | } | 658 | } |
| @@ -475,10 +660,18 @@ impl Cast { | |||
| 475 | let _span = tracy_client::span!("Cast::ensure_size"); | 660 | let _span = tracy_client::span!("Cast::ensure_size"); |
| 476 | debug!("cast size changed, updating stream size"); | 661 | debug!("cast size changed, updating stream size"); |
| 477 | 662 | ||
| 478 | - self.size.set(current_size.with_pending(size)); | 663 | + *state = CastState::ResizePending { |
| 664 | + pending_size: new_size, | ||
| 665 | + }; | ||
| 479 | 666 | ||
| 480 | let params; | 667 | let params; |
| 481 | - make_params!(params, size, self.refresh, self.offer_alpha); | 668 | + make_params!( |
| 669 | + params, | ||
| 670 | + &self.formats, | ||
| 671 | + new_size, | ||
| 672 | + self.refresh.get(), | ||
| 673 | + self.offer_alpha | ||
| 674 | + ); | ||
| 482 | self.stream | 675 | self.stream |
| 483 | .update_params(params) | 676 | .update_params(params) |
| 484 | .context("error updating stream params")?; | 677 | .context("error updating stream params")?; |
| @@ -487,17 +680,17 @@ impl Cast { | |||
| 487 | } | 680 | } |
| 488 | 681 | ||
| 489 | pub fn set_refresh(&mut self, refresh: u32) -> anyhow::Result<()> { | 682 | pub fn set_refresh(&mut self, refresh: u32) -> anyhow::Result<()> { |
| 490 | - if self.refresh == refresh { | 683 | + if self.refresh.get() == refresh { |
| 491 | return Ok(()); | 684 | return Ok(()); |
| 492 | } | 685 | } |
| 493 | 686 | ||
| 494 | let _span = tracy_client::span!("Cast::set_refresh"); | 687 | let _span = tracy_client::span!("Cast::set_refresh"); |
| 495 | debug!("cast FPS changed, updating stream FPS"); | 688 | debug!("cast FPS changed, updating stream FPS"); |
| 496 | - self.refresh = refresh; | 689 | + self.refresh.set(refresh); |
| 497 | 690 | ||
| 498 | - let size = self.size.get().expected_format_size(); | 691 | + let size = self.state.borrow().expected_format_size(); |
| 499 | let params; | 692 | let params; |
| 500 | - make_params!(params, size, self.refresh, self.offer_alpha); | 693 | + make_params!(params, &self.formats, size, refresh, self.offer_alpha); |
| 501 | self.stream | 694 | self.stream |
| 502 | .update_params(params) | 695 | .update_params(params) |
| 503 | .context("error updating stream params")?; | 696 | .context("error updating stream params")?; |
| @@ -552,77 +745,88 @@ impl Cast { | |||
| 552 | } | 745 | } |
| 553 | }; | 746 | }; |
| 554 | 747 | ||
| 555 | - let data = &mut buffer.datas_mut()[0]; | 748 | + let fd = buffer.datas_mut()[0].as_raw().fd; |
| 556 | - let fd = data.as_raw().fd as i32; | 749 | + let dmabuf = &self.dmabufs.borrow()[&fd]; |
| 557 | - let dmabuf = self.dmabufs.borrow()[&fd].clone(); | ||
| 558 | 750 | ||
| 559 | - if let Err(err) = | 751 | + if let Err(err) = render_to_dmabuf( |
| 560 | - render_to_dmabuf(renderer, dmabuf, size, scale, Transform::Normal, elements) | 752 | + renderer, |
| 561 | - { | 753 | + dmabuf.clone(), |
| 754 | + size, | ||
| 755 | + scale, | ||
| 756 | + Transform::Normal, | ||
| 757 | + elements, | ||
| 758 | + ) { | ||
| 562 | warn!("error rendering to dmabuf: {err:?}"); | 759 | warn!("error rendering to dmabuf: {err:?}"); |
| 563 | return false; | 760 | return false; |
| 564 | } | 761 | } |
| 565 | 762 | ||
| 566 | - let maxsize = data.as_raw().maxsize; | 763 | + for (data, (stride, offset)) in |
| 567 | - let chunk = data.chunk_mut(); | 764 | + zip(buffer.datas_mut(), zip(dmabuf.strides(), dmabuf.offsets())) |
| 568 | - *chunk.size_mut() = maxsize; | 765 | + { |
| 569 | - *chunk.stride_mut() = maxsize as i32 / size.h; | 766 | + let chunk = data.chunk_mut(); |
| 570 | - | 767 | + *chunk.size_mut() = 1; |
| 571 | - true | 768 | + *chunk.stride_mut() = stride as i32; |
| 572 | - } | 769 | + *chunk.offset_mut() = offset; |
| 573 | -} | ||
| 574 | 770 | ||
| 575 | -impl CastSize { | 771 | + trace!( |
| 576 | - fn pending_size(self) -> Option<Size<i32, Physical>> { | 772 | + "pw buffer: fd = {}, stride = {stride}, offset = {offset}", |
| 577 | - match self { | 773 | + data.as_raw().fd |
| 578 | - CastSize::InitialPending(pending) => Some(pending), | 774 | + ); |
| 579 | - CastSize::Ready(_) => None, | ||
| 580 | - CastSize::ChangePending { pending, .. } => Some(pending), | ||
| 581 | } | 775 | } |
| 582 | - } | ||
| 583 | 776 | ||
| 584 | - fn negotiated_size(self) -> Option<Size<i32, Physical>> { | 777 | + true |
| 585 | - match self { | ||
| 586 | - CastSize::InitialPending(_) => None, | ||
| 587 | - CastSize::Ready(size) => Some(size), | ||
| 588 | - CastSize::ChangePending { | ||
| 589 | - last_negotiated, .. | ||
| 590 | - } => Some(last_negotiated), | ||
| 591 | - } | ||
| 592 | } | 778 | } |
| 779 | +} | ||
| 593 | 780 | ||
| 594 | - fn expected_format_size(self) -> Size<i32, Physical> { | 781 | +impl CastState { |
| 782 | + fn pending_size(&self) -> Option<Size<u32, Physical>> { | ||
| 595 | match self { | 783 | match self { |
| 596 | - CastSize::InitialPending(pending) => pending, | 784 | + CastState::ResizePending { pending_size } => Some(*pending_size), |
| 597 | - CastSize::Ready(size) => size, | 785 | + CastState::ConfirmationPending { size, .. } => Some(*size), |
| 598 | - CastSize::ChangePending { pending, .. } => pending, | 786 | + CastState::Ready { .. } => None, |
| 599 | } | 787 | } |
| 600 | } | 788 | } |
| 601 | 789 | ||
| 602 | - fn with_pending(self, pending: Size<i32, Physical>) -> Self { | 790 | + fn expected_format_size(&self) -> Size<u32, Physical> { |
| 603 | match self { | 791 | match self { |
| 604 | - CastSize::InitialPending(_) => CastSize::InitialPending(pending), | 792 | + CastState::ResizePending { pending_size } => *pending_size, |
| 605 | - CastSize::Ready(size) => CastSize::ChangePending { | 793 | + CastState::ConfirmationPending { size, .. } => *size, |
| 606 | - last_negotiated: size, | 794 | + CastState::Ready { size, .. } => *size, |
| 607 | - pending, | ||
| 608 | - }, | ||
| 609 | - CastSize::ChangePending { | ||
| 610 | - last_negotiated, .. | ||
| 611 | - } => CastSize::ChangePending { | ||
| 612 | - last_negotiated, | ||
| 613 | - pending, | ||
| 614 | - }, | ||
| 615 | } | 795 | } |
| 616 | } | 796 | } |
| 617 | } | 797 | } |
| 618 | 798 | ||
| 619 | -fn make_video_params(size: Size<i32, Physical>, refresh: u32, alpha: bool) -> pod::Object { | 799 | +fn make_video_params( |
| 800 | + formats: &FormatSet, | ||
| 801 | + size: Size<u32, Physical>, | ||
| 802 | + refresh: u32, | ||
| 803 | + alpha: bool, | ||
| 804 | +) -> pod::Object { | ||
| 620 | let format = if alpha { | 805 | let format = if alpha { |
| 621 | VideoFormat::BGRA | 806 | VideoFormat::BGRA |
| 622 | } else { | 807 | } else { |
| 623 | VideoFormat::BGRx | 808 | VideoFormat::BGRx |
| 624 | }; | 809 | }; |
| 625 | 810 | ||
| 811 | + let fourcc = if alpha { | ||
| 812 | + Fourcc::Argb8888 | ||
| 813 | + } else { | ||
| 814 | + Fourcc::Xrgb8888 | ||
| 815 | + }; | ||
| 816 | + | ||
| 817 | + let formats: Vec<_> = formats | ||
| 818 | + .iter() | ||
| 819 | + .filter_map(|f| (f.code == fourcc).then_some(u64::from(f.modifier) as i64)) | ||
| 820 | + .collect(); | ||
| 821 | + | ||
| 822 | + trace!("offering: {formats:?}"); | ||
| 823 | + | ||
| 824 | + let dont_fixate = if formats.len() > 1 { | ||
| 825 | + PropertyFlags::DONT_FIXATE | ||
| 826 | + } else { | ||
| 827 | + PropertyFlags::empty() | ||
| 828 | + }; | ||
| 829 | + | ||
| 626 | pod::object!( | 830 | pod::object!( |
| 627 | SpaTypes::ObjectParamFormat, | 831 | SpaTypes::ObjectParamFormat, |
| 628 | ParamType::EnumFormat, | 832 | ParamType::EnumFormat, |
| @@ -631,15 +835,21 @@ fn make_video_params(size: Size<i32, Physical>, refresh: u32, alpha: bool) -> po | |||
| 631 | pod::property!(FormatProperties::VideoFormat, Id, format), | 835 | pod::property!(FormatProperties::VideoFormat, Id, format), |
| 632 | Property { | 836 | Property { |
| 633 | key: FormatProperties::VideoModifier.as_raw(), | 837 | key: FormatProperties::VideoModifier.as_raw(), |
| 634 | - value: pod::Value::Long(u64::from(Modifier::Invalid) as i64), | 838 | + flags: PropertyFlags::MANDATORY | dont_fixate, |
| 635 | - flags: PropertyFlags::MANDATORY, | 839 | + value: pod::Value::Choice(ChoiceValue::Long(Choice( |
| 840 | + ChoiceFlags::empty(), | ||
| 841 | + ChoiceEnum::Enum { | ||
| 842 | + default: formats[0], | ||
| 843 | + alternatives: formats, | ||
| 844 | + } | ||
| 845 | + ))) | ||
| 636 | }, | 846 | }, |
| 637 | pod::property!( | 847 | pod::property!( |
| 638 | FormatProperties::VideoSize, | 848 | FormatProperties::VideoSize, |
| 639 | Rectangle, | 849 | Rectangle, |
| 640 | Rectangle { | 850 | Rectangle { |
| 641 | - width: size.w as u32, | 851 | + width: size.w, |
| 642 | - height: size.h as u32, | 852 | + height: size.h, |
| 643 | } | 853 | } |
| 644 | ), | 854 | ), |
| 645 | pod::property!( | 855 | pod::property!( |
| @@ -669,3 +879,68 @@ fn make_pod(buffer: &mut Vec<u8>, object: pod::Object) -> &Pod { | |||
| 669 | PodSerializer::serialize(Cursor::new(&mut *buffer), &pod::Value::Object(object)).unwrap(); | 879 | PodSerializer::serialize(Cursor::new(&mut *buffer), &pod::Value::Object(object)).unwrap(); |
| 670 | Pod::from_bytes(buffer).unwrap() | 880 | Pod::from_bytes(buffer).unwrap() |
| 671 | } | 881 | } |
| 882 | + | ||
| 883 | +fn find_preferred_modifier( | ||
| 884 | + gbm: &GbmDevice<DrmDeviceFd>, | ||
| 885 | + size: Size<u32, Physical>, | ||
| 886 | + fourcc: Fourcc, | ||
| 887 | + modifiers: Vec<i64>, | ||
| 888 | +) -> anyhow::Result<(Modifier, usize)> { | ||
| 889 | + debug!("find_preferred_modifier: size={size:?}, fourcc={fourcc}, modifiers={modifiers:?}"); | ||
| 890 | + | ||
| 891 | + let (buffer, modifier) = allocate_buffer(gbm, size, fourcc, &modifiers)?; | ||
| 892 | + | ||
| 893 | + let dmabuf = buffer | ||
| 894 | + .export() | ||
| 895 | + .context("error exporting GBM buffer object as dmabuf")?; | ||
| 896 | + let plane_count = dmabuf.num_planes(); | ||
| 897 | + | ||
| 898 | + // FIXME: Ideally this also needs to try binding the dmabuf for rendering. | ||
| 899 | + | ||
| 900 | + Ok((modifier, plane_count)) | ||
| 901 | +} | ||
| 902 | + | ||
| 903 | +fn allocate_buffer( | ||
| 904 | + gbm: &GbmDevice<DrmDeviceFd>, | ||
| 905 | + size: Size<u32, Physical>, | ||
| 906 | + fourcc: Fourcc, | ||
| 907 | + modifiers: &[i64], | ||
| 908 | +) -> anyhow::Result<(GbmBuffer, Modifier)> { | ||
| 909 | + let (w, h) = (size.w, size.h); | ||
| 910 | + let flags = GbmBufferFlags::RENDERING; | ||
| 911 | + | ||
| 912 | + if modifiers.len() == 1 && Modifier::from(modifiers[0] as u64) == Modifier::Invalid { | ||
| 913 | + let bo = gbm | ||
| 914 | + .create_buffer_object::<()>(w, h, fourcc, flags) | ||
| 915 | + .context("error creating GBM buffer object")?; | ||
| 916 | + | ||
| 917 | + let buffer = GbmBuffer::from_bo(bo, true); | ||
| 918 | + Ok((buffer, Modifier::Invalid)) | ||
| 919 | + } else { | ||
| 920 | + let modifiers = modifiers | ||
| 921 | + .iter() | ||
| 922 | + .map(|m| Modifier::from(*m as u64)) | ||
| 923 | + .filter(|m| *m != Modifier::Invalid); | ||
| 924 | + | ||
| 925 | + let bo = gbm | ||
| 926 | + .create_buffer_object_with_modifiers2::<()>(w, h, fourcc, modifiers, flags) | ||
| 927 | + .context("error creating GBM buffer object")?; | ||
| 928 | + | ||
| 929 | + let modifier = bo.modifier().unwrap(); | ||
| 930 | + let buffer = GbmBuffer::from_bo(bo, false); | ||
| 931 | + Ok((buffer, modifier)) | ||
| 932 | + } | ||
| 933 | +} | ||
| 934 | + | ||
| 935 | +fn allocate_dmabuf( | ||
| 936 | + gbm: &GbmDevice<DrmDeviceFd>, | ||
| 937 | + size: Size<u32, Physical>, | ||
| 938 | + fourcc: Fourcc, | ||
| 939 | + modifier: Modifier, | ||
| 940 | +) -> anyhow::Result<Dmabuf> { | ||
| 941 | + let (buffer, _modifier) = allocate_buffer(gbm, size, fourcc, &[u64::from(modifier) as i64])?; | ||
| 942 | + let dmabuf = buffer | ||
| 943 | + .export() | ||
| 944 | + .context("error exporting GBM buffer object as dmabuf")?; | ||
| 945 | + Ok(dmabuf) | ||
| 946 | +} | ||