rewrite built-in wayland compositor egl → vulkan

This commit is contained in:
galister
2025-12-25 21:26:38 +09:00
parent 3b6acb3673
commit 40dc33410d
34 changed files with 923 additions and 3051 deletions

View File

@@ -1,47 +1,6 @@
use std::{fmt::Display, os::fd::RawFd};
use std::os::fd::RawFd;
#[derive(Debug, Clone, Copy, Default)]
pub struct FourCC {
pub value: u32,
}
impl PartialEq for FourCC {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl From<u32> for FourCC {
fn from(value: u32) -> Self {
Self { value }
}
}
impl From<FourCC> for u32 {
fn from(fourcc: FourCC) -> Self {
fourcc.value
}
}
impl Display for FourCC {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for i in 0..4 {
if let Some(c) = char::from_u32((self.value >> (i * 8)) & 0xFF) {
write!(f, "{c}")?
} else {
write!(f, "?")?
}
}
Ok(())
}
}
pub const DRM_FORMAT_ARGB8888: u32 = 0x34325241; // AR24
pub const DRM_FORMAT_ABGR8888: u32 = 0x34324241; // AB24
pub const DRM_FORMAT_XRGB8888: u32 = 0x34325258; // XR24
pub const DRM_FORMAT_XBGR8888: u32 = 0x34324258; // XB24
pub const DRM_FORMAT_ABGR2101010: u32 = 0x30334241; // AB30
pub const DRM_FORMAT_XBGR2101010: u32 = 0x30334258; // XB30
use drm_fourcc::{DrmFormat, DrmModifier};
#[cfg(feature = "egl")]
#[rustfmt::skip]
@@ -73,24 +32,27 @@ pub enum Transform {
Flipped270,
}
#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy)]
pub struct FrameFormat {
pub width: u32,
pub height: u32,
pub fourcc: FourCC,
pub modifier: u64,
pub drm_format: DrmFormat,
pub transform: Transform,
}
impl FrameFormat {
#[must_use]
pub fn get_mod_hi(&self) -> u32 {
(self.modifier >> 32) as _
let m = u64::from(self.drm_format.modifier);
(m >> 32) as _
}
#[must_use]
pub fn get_mod_lo(&self) -> u32 {
(self.modifier & 0xFFFFFFFF) as _
let m = u64::from(self.drm_format.modifier);
(m & 0xFFFFFFFF) as _
}
pub fn set_mod(&mut self, mod_hi: u32, mod_low: u32) {
self.modifier = ((mod_hi as u64) << 32) + mod_low as u64;
self.drm_format.modifier = DrmModifier::from(((mod_hi as u64) << 32) + mod_low as u64);
}
}
@@ -101,13 +63,6 @@ pub struct FramePlane {
pub stride: i32,
}
#[derive(Default, Clone)]
pub struct DrmFormat {
pub fourcc: FourCC,
pub modifiers: Vec<u64>,
}
#[derive(Default)]
pub struct DmabufFrame {
pub format: FrameFormat,
pub num_planes: usize,
@@ -126,7 +81,7 @@ impl DmabufFrame {
0x3056, // HEIGHT
self.format.height as _,
0x3271, // LINUX_DRM_FOURCC_EXT,
self.format.fourcc.value as _,
self.format.drm_format.code as _,
];
for i in 0..self.num_planes {
@@ -162,14 +117,12 @@ impl DmabufFrame {
}
}
#[derive(Default)]
pub struct MemFdFrame {
pub format: FrameFormat,
pub plane: FramePlane,
pub mouse: Option<MouseMeta>,
}
#[derive(Default)]
pub struct MemPtrFrame {
pub format: FrameFormat,
pub ptr: usize,

View File

@@ -1,7 +1,8 @@
#![allow(dead_code)]
#![allow(clippy::expect_fun_call)]
use frame::{DrmFormat, WlxFrame};
pub use drm_fourcc::{DrmFormat, DrmFourcc, DrmModifier};
use frame::WlxFrame;
pub mod frame;

View File

@@ -1,4 +1,5 @@
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;
@@ -12,6 +13,9 @@ use ashpd::desktop::{
pub use ashpd::Error as AshpdError;
use drm_fourcc::DrmFormat;
use drm_fourcc::DrmFourcc;
use drm_fourcc::DrmModifier;
use pipewire as pw;
use pw::spa;
@@ -33,14 +37,6 @@ use spa::utils::ChoiceEnum;
use spa::utils::ChoiceFlags;
use crate::WlxCapture;
use crate::frame::DRM_FORMAT_ABGR8888;
use crate::frame::DRM_FORMAT_ABGR2101010;
use crate::frame::DRM_FORMAT_ARGB8888;
use crate::frame::DRM_FORMAT_XBGR8888;
use crate::frame::DRM_FORMAT_XBGR2101010;
use crate::frame::DRM_FORMAT_XRGB8888;
use crate::frame::DrmFormat;
use crate::frame::FourCC;
use crate::frame::FrameFormat;
use crate::frame::MouseMeta;
use crate::frame::Transform;
@@ -297,7 +293,15 @@ where
)?;
let _listener = stream
.add_local_listener_with_user_data(FrameFormat::default())
.add_local_listener_with_user_data(FrameFormat {
width: 0,
height: 0,
drm_format: DrmFormat {
code: DrmFourcc::Argb8888,
modifier: DrmModifier::Invalid,
},
transform: Transform::Undefined,
})
.state_changed({
let name = name.clone();
move |_, _, old, new| {
@@ -320,10 +324,10 @@ where
format.width = info.size().width;
format.height = info.size().height;
format.fourcc = spa_to_fourcc(info.format());
format.modifier = info.modifier();
format.drm_format.code = spa_to_fourcc(info.format());
format.drm_format.modifier = DrmModifier::from(info.modifier());
let kind = if format.modifier != 0 {
let kind = if info.modifier() != 0 {
"DMA-buf"
} else {
"SHM"
@@ -425,7 +429,7 @@ where
format: *format,
num_planes: planes.len(),
mouse: mouse_meta,
..Default::default()
planes: Default::default(),
};
dmabuf.planes[..planes.len()].copy_from_slice(&planes[..planes.len()]);
@@ -494,7 +498,17 @@ where
})
.register()?;
let mut format_params: Vec<Vec<u8>> = dmabuf_formats
let mut fourcc_mods: HashMap<DrmFourcc, Vec<DrmModifier>> = HashMap::new();
for f in dmabuf_formats.iter() {
if let Some(v) = fourcc_mods.get_mut(&f.code) {
v.push(f.modifier);
} else {
fourcc_mods.insert(f.code, vec![f.modifier]);
}
}
let mut format_params: Vec<Vec<u8>> = fourcc_mods
.iter()
.filter_map(|f| obj_to_bytes(get_format_params(Some(f))).ok())
.collect();
@@ -591,7 +605,7 @@ fn get_meta_object(key: u32, size: usize) -> Object {
)
}
fn get_format_params(fmt: Option<&DrmFormat>) -> Object {
fn get_format_params(fmt: Option<(&DrmFourcc, &Vec<DrmModifier>)>) -> Object {
let mut obj = spa::pod::object!(
spa::utils::SpaTypes::ObjectParamFormat,
spa::param::ParamType::EnumFormat,
@@ -638,7 +652,7 @@ fn get_format_params(fmt: Option<&DrmFormat>) -> Object {
);
if let Some(fmt) = fmt {
let spa_fmt = fourcc_to_spa(fmt.fourcc);
let spa_fmt = fourcc_to_spa(*fmt.0);
let prop = spa::pod::property!(
spa::param::format::FormatProperties::VideoFormat,
@@ -657,8 +671,8 @@ fn get_format_params(fmt: Option<&DrmFormat>) -> Object {
value: Value::Choice(ChoiceValue::Long(Choice(
ChoiceFlags::empty(),
ChoiceEnum::Enum {
default: fmt.modifiers[0] as _,
alternatives: fmt.modifiers.iter().map(|m| *m as _).collect(),
default: u64::from(fmt.1[0]) as _,
alternatives: fmt.1.iter().map(|m| u64::from(*m) as _).collect(),
},
))),
};
@@ -683,27 +697,27 @@ fn get_format_params(fmt: Option<&DrmFormat>) -> Object {
obj
}
fn fourcc_to_spa(fourcc: FourCC) -> VideoFormat {
match fourcc.value {
DRM_FORMAT_ARGB8888 => VideoFormat::BGRA,
DRM_FORMAT_ABGR8888 => VideoFormat::RGBA,
DRM_FORMAT_XRGB8888 => VideoFormat::BGRx,
DRM_FORMAT_XBGR8888 => VideoFormat::RGBx,
DRM_FORMAT_ABGR2101010 => VideoFormat::ABGR_210LE,
DRM_FORMAT_XBGR2101010 => VideoFormat::xBGR_210LE,
fn fourcc_to_spa(fourcc: DrmFourcc) -> VideoFormat {
match fourcc{
DrmFourcc::Argb8888 => VideoFormat::BGRA,
DrmFourcc::Abgr8888 => VideoFormat::RGBA,
DrmFourcc::Xrgb8888 => VideoFormat::BGRx,
DrmFourcc::Xbgr8888 => VideoFormat::RGBx,
DrmFourcc::Abgr2101010 => VideoFormat::ABGR_210LE,
DrmFourcc::Xbgr2101010 => VideoFormat::xBGR_210LE,
_ => panic!("Unsupported format"),
}
}
#[allow(non_upper_case_globals)]
fn spa_to_fourcc(spa: VideoFormat) -> FourCC {
fn spa_to_fourcc(spa: VideoFormat) -> DrmFourcc {
match spa {
VideoFormat::BGRA => DRM_FORMAT_ARGB8888.into(),
VideoFormat::RGBA => DRM_FORMAT_ABGR8888.into(),
VideoFormat::BGRx => DRM_FORMAT_XRGB8888.into(),
VideoFormat::RGBx => DRM_FORMAT_XBGR8888.into(),
VideoFormat::ABGR_210LE => DRM_FORMAT_ABGR2101010.into(),
VideoFormat::xBGR_210LE => DRM_FORMAT_XBGR2101010.into(),
VideoFormat::BGRA => DrmFourcc::Argb8888,
VideoFormat::RGBA => DrmFourcc::Abgr8888,
VideoFormat::BGRx => DrmFourcc::Xrgb8888,
VideoFormat::RGBx => DrmFourcc::Xbgr8888,
VideoFormat::ABGR_210LE => DrmFourcc::Abgr2101010,
VideoFormat::xBGR_210LE => DrmFourcc::Xbgr2101010,
_ => panic!("Unsupported format"),
}
}

View File

@@ -6,12 +6,13 @@ use std::{
thread::JoinHandle,
};
use drm_fourcc::{DrmFormat, DrmFourcc, DrmModifier};
use smithay_client_toolkit::reexports::protocols_wlr::export_dmabuf::v1::client::zwlr_export_dmabuf_frame_v1::{self, ZwlrExportDmabufFrameV1};
use wayland_client::{Connection, QueueHandle, Dispatch, Proxy};
use crate::{
WlxCapture,
frame::{DmabufFrame, DrmFormat, FramePlane, WlxFrame},
frame::{DmabufFrame, FrameFormat, FramePlane, WlxFrame},
wayland::WlxClient,
};
@@ -168,13 +169,27 @@ fn request_dmabuf_frame(
num_objects,
..
} => {
let mut new_frame = DmabufFrame::default();
new_frame.format.width = width;
new_frame.format.height = height;
new_frame.format.fourcc.value = format;
let mut new_frame = DmabufFrame {
format: FrameFormat {
width,
height,
drm_format: DrmFormat {
code: match DrmFourcc::try_from(format) {
Ok(code) => code,
Err(e) => {
log::error!("Unrecognized fourcc: {e:?}");
return;
}
},
modifier: DrmModifier::Invalid,
},
transform,
},
mouse: None,
num_planes: num_objects as _,
planes: Default::default(),
};
new_frame.format.set_mod(mod_high, mod_low);
new_frame.format.transform = transform;
new_frame.num_planes = num_objects as _;
frame = Some(new_frame);
}
zwlr_export_dmabuf_frame_v1::Event::Object {

View File

@@ -1,3 +1,4 @@
use drm_fourcc::{DrmFormat, DrmFourcc, DrmModifier};
use libc::{O_CREAT, O_RDWR, S_IRUSR, S_IWUSR};
use std::{
any::Any,
@@ -18,10 +19,7 @@ use smithay_client_toolkit::reexports::protocols_wlr::screencopy::v1::client::zw
use crate::{
WlxCapture,
frame::{
DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB8888, DrmFormat, FourCC, FrameFormat, FramePlane,
MemFdFrame, WlxFrame,
},
frame::{FrameFormat, FramePlane, MemFdFrame, WlxFrame},
wayland::WlxClient,
};
@@ -44,7 +42,7 @@ impl Drop for BufData {
enum ScreenCopyEvent {
Buffer {
data: BufData,
fourcc: FourCC,
drm_format: DrmFormat,
width: u32,
height: u32,
stride: u32,
@@ -213,7 +211,7 @@ where
match event {
ScreenCopyEvent::Buffer {
data,
fourcc,
drm_format,
width,
height,
stride,
@@ -222,16 +220,15 @@ where
format: FrameFormat {
width,
height,
fourcc,
drm_format,
transform,
..Default::default()
},
plane: FramePlane {
fd: Some(data.fd),
offset: 0,
stride: stride as _,
},
..Default::default()
mouse: None,
};
log::trace!("{}: Received screencopy buffer, copying", name.as_ref());
if wait_for_damage {
@@ -331,7 +328,10 @@ impl Dispatch<ZwlrScreencopyFrameV1, SyncSender<ScreenCopyEvent>> for WlxClient
wl_pool,
fd,
},
fourcc,
drm_format: DrmFormat {
code: fourcc,
modifier: DrmModifier::Invalid,
},
width,
height,
stride,
@@ -346,12 +346,12 @@ impl Dispatch<ZwlrScreencopyFrameV1, SyncSender<ScreenCopyEvent>> for WlxClient
}
}
fn fourcc_from_wlshm(shm_format: Format) -> Option<FourCC> {
fn fourcc_from_wlshm(shm_format: Format) -> Option<DrmFourcc> {
match shm_format {
Format::Argb8888 => Some(FourCC::from(DRM_FORMAT_ARGB8888)),
Format::Xrgb8888 => Some(FourCC::from(DRM_FORMAT_XRGB8888)),
Format::Abgr8888 => Some(FourCC::from(DRM_FORMAT_ARGB8888)),
Format::Xbgr8888 => Some(FourCC::from(DRM_FORMAT_XRGB8888)),
Format::Argb8888 => Some(DrmFourcc::Argb8888),
Format::Xrgb8888 => Some(DrmFourcc::Xrgb8888),
Format::Abgr8888 => Some(DrmFourcc::Abgr8888),
Format::Xbgr8888 => Some(DrmFourcc::Xbgr8888),
_ => None,
}
}

View File

@@ -8,11 +8,12 @@ use std::{
},
};
use drm_fourcc::{DrmFormat, DrmFourcc, DrmModifier};
use rxscreen::monitor::Monitor;
use crate::{
WlxCapture,
frame::{DRM_FORMAT_XRGB8888, DrmFormat, FrameFormat, MemPtrFrame, MouseMeta, WlxFrame},
frame::{FrameFormat, MemPtrFrame, MouseMeta, Transform, WlxFrame},
};
pub struct XshmScreen {
@@ -101,8 +102,11 @@ where
format: FrameFormat {
width: image.width() as _,
height: image.height() as _,
fourcc: DRM_FORMAT_XRGB8888.into(),
..Default::default()
drm_format: DrmFormat {
code: DrmFourcc::Xrgb8888,
modifier: DrmModifier::Invalid,
},
transform: Transform::Normal,
},
ptr: unsafe { image.as_ptr() as _ },
size,