Skip to content

Commit

Permalink
WIP linux-drm-syncobj-v1
Browse files Browse the repository at this point in the history
Store drm device in `DrmTimeline`; destroy timeline on drop

WIP OUT_FENCE_FD; always signalled in fence

DRM git rev

Remove patch for drm

remove OUT_FENCE_PTR

remove comment

release

WIP

fix

destroy syncobj

feature checks

clone_from

WIP
  • Loading branch information
ids1024 committed May 21, 2024
1 parent 8f132ec commit 42ffd4e
Show file tree
Hide file tree
Showing 11 changed files with 621 additions and 23 deletions.
19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ profiling = "1.0"
smallvec = "1.11"
pixman = { version = "0.1.0", features = ["drm-fourcc"], optional = true }

[patch.crates-io]
wayland-egl = { git = "https://github.com/smithay/wayland-rs" }
wayland-protocols = { git = "https://github.com/smithay/wayland-rs" }
wayland-protocols-wlr = { git = "https://github.com/smithay/wayland-rs" }
wayland-protocols-misc = { git = "https://github.com/smithay/wayland-rs" }
wayland-server = { git = "https://github.com/smithay/wayland-rs" }
wayland-client = { git = "https://github.com/smithay/wayland-rs" }
wayland-sys = { git = "https://github.com/smithay/wayland-rs" }
wayland-backend = { git = "https://github.com/smithay/wayland-rs" }
wayland-scanner = { git = "https://github.com/smithay/wayland-rs" }
# wayland-egl = { path = "../wayland-rs/wayland-egl/" }
# wayland-protocols = { path = "../wayland-rs/wayland-protocols/" }
# wayland-protocols-wlr = { path = "../wayland-rs/wayland-protocols-wlr/" }
# wayland-protocols-misc = { path = "../wayland-rs/wayland-protocols-misc/" }
# wayland-server = { path = "../wayland-rs/wayland-server/" }
# wayland-client = { path = "../wayland-rs/wayland-client/" }
# wayland-sys = { path = "../wayland-rs/wayland-sys/" }
# wayland-backend = { path = "../wayland-rs/wayland-backend/" }
# wayland-scanner = { path = "../wayland-rs/wayland-scanner/" }

[dev-dependencies]
clap = { version = "4", features = ["derive"] }
Expand Down
22 changes: 22 additions & 0 deletions anvil/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use smithay::{
CompositorState, SurfaceAttributes, TraversalAction,
},
dmabuf::get_dmabuf,
drm_syncobj::DrmSyncobjCachedState,
shell::{
wlr_layer::{
Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler,
Expand Down Expand Up @@ -112,7 +113,14 @@ impl<BackendData: Backend> CompositorHandler for AnvilState<BackendData> {

fn new_surface(&mut self, surface: &WlSurface) {
add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| {
let mut acquire_point = None;
let maybe_dmabuf = with_states(surface, |surface_data| {
acquire_point.clone_from(
&surface_data
.cached_state
.pending::<DrmSyncobjCachedState>()
.acquire_point,
);
surface_data
.cached_state
.pending::<SurfaceAttributes>()
Expand All @@ -124,6 +132,20 @@ impl<BackendData: Backend> CompositorHandler for AnvilState<BackendData> {
})
});
if let Some(dmabuf) = maybe_dmabuf {
if let Some(acquire_point) = acquire_point {
if let Ok((blocker, source)) = acquire_point.generate_blocker() {
let client = surface.client().unwrap();
let res = state.handle.insert_source(source, move |_, _, data| {
let dh = data.display_handle.clone();
data.client_compositor_state(&client).blocker_cleared(data, &dh);
Ok(())
});
if res.is_ok() {
add_blocker(surface, blocker);
return;
}
}
}
if let Ok((blocker, source)) = dmabuf.generate_blocker(Interest::READ) {
if let Some(client) = surface.client() {
let res = state.handle.insert_source(source, move |_, _, data| {
Expand Down
35 changes: 29 additions & 6 deletions anvil/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
any::Any,
os::unix::io::OwnedFd,
sync::{atomic::AtomicBool, Arc},
time::Duration,
Expand All @@ -13,12 +14,13 @@ use smithay::{
default_primary_scanout_output_compare, utils::select_dmabuf_feedback, RenderElementStates,
},
},
delegate_compositor, delegate_data_control, delegate_data_device, delegate_fractional_scale,
delegate_input_method_manager, delegate_keyboard_shortcuts_inhibit, delegate_layer_shell,
delegate_output, delegate_pointer_constraints, delegate_pointer_gestures, delegate_presentation,
delegate_primary_selection, delegate_relative_pointer, delegate_seat, delegate_security_context,
delegate_shm, delegate_tablet_manager, delegate_text_input_manager, delegate_viewporter,
delegate_virtual_keyboard_manager, delegate_xdg_activation, delegate_xdg_decoration, delegate_xdg_shell,
delegate_compositor, delegate_data_control, delegate_data_device, delegate_drm_syncobj,
delegate_fractional_scale, delegate_input_method_manager, delegate_keyboard_shortcuts_inhibit,
delegate_layer_shell, delegate_output, delegate_pointer_constraints, delegate_pointer_gestures,
delegate_presentation, delegate_primary_selection, delegate_relative_pointer, delegate_seat,
delegate_security_context, delegate_shm, delegate_tablet_manager, delegate_text_input_manager,
delegate_viewporter, delegate_virtual_keyboard_manager, delegate_xdg_activation, delegate_xdg_decoration,
delegate_xdg_shell,
desktop::{
space::SpaceElement,
utils::{
Expand Down Expand Up @@ -48,6 +50,7 @@ use smithay::{
wayland::{
compositor::{get_parent, with_states, CompositorClientState, CompositorState},
dmabuf::DmabufFeedback,
drm_syncobj::{DrmSyncobjHandler, DrmSyncobjState},
fractional_scale::{with_fractional_scale, FractionalScaleHandler, FractionalScaleManagerState},
input_method::{InputMethodHandler, InputMethodManagerState, PopupSurface},
keyboard_shortcuts_inhibit::{
Expand Down Expand Up @@ -535,6 +538,22 @@ impl<BackendData: Backend> XdgForeignHandler for AnvilState<BackendData> {
}
smithay::delegate_xdg_foreign!(@<BackendData: Backend + 'static> AnvilState<BackendData>);

impl<BackendData: Backend> DrmSyncobjHandler for AnvilState<BackendData> {
fn import_device(&self) -> &smithay::backend::drm::DrmDeviceFd {
let udev_data = &<dyn Any>::downcast_ref::<AnvilState<crate::udev::UdevData>>(self)
.expect("syncobj protocol used on backend other than DRM")
.backend_data;
udev_data.backends[&udev_data
.primary_gpu
.node_with_type(smithay::backend::drm::NodeType::Primary)
.unwrap()
.unwrap()]
.drm
.device_fd()
}
}
delegate_drm_syncobj!(@<BackendData: Backend + 'static> AnvilState<BackendData>);

impl<BackendData: Backend + 'static> AnvilState<BackendData> {
pub fn init(
display: Display<AnvilState<BackendData>>,
Expand Down Expand Up @@ -613,6 +632,10 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
.get_data::<ClientState>()
.map_or(true, |client_state| client_state.security_context.is_none())
});
if <dyn Any>::downcast_ref::<AnvilState<crate::udev::UdevData>>(&backend_data).is_some() {
// TODO only expose if main device supports drm_syncobj_eventfd?
DrmSyncobjState::new::<Self>(&dh);
}

// init input
let seat_name = backend_data.seat_name();
Expand Down
8 changes: 4 additions & 4 deletions anvil/src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ pub struct UdevData {
pub session: LibSeatSession,
dh: DisplayHandle,
dmabuf_state: Option<(DmabufState, DmabufGlobal)>,
primary_gpu: DrmNode,
pub primary_gpu: DrmNode,
gpus: GpuManager<GbmGlesBackend<GlesRenderer, DrmDeviceFd>>,
backends: HashMap<DrmNode, BackendData>,
pub backends: HashMap<DrmNode, BackendData>,
pointer_images: Vec<(xcursor::parser::Image, MemoryRenderBuffer)>,
pointer_element: PointerElement,
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -727,13 +727,13 @@ impl Drop for SurfaceData {
}
}

struct BackendData {
pub struct BackendData {
surfaces: HashMap<crtc::Handle, SurfaceData>,
non_desktop_connectors: Vec<(connector::Handle, crtc::Handle)>,
leasing_global: Option<DrmLeaseState>,
active_leases: Vec<DrmLease>,
gbm: GbmDevice<DrmDeviceFd>,
drm: DrmDevice,
pub drm: DrmDevice,
drm_scanner: DrmScanner,
render_node: DrmNode,
registration_token: RegistrationToken,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/drm/device/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::{error, info, warn};

use crate::utils::{DevPath, DeviceFd};

#[derive(Debug)]
#[derive(Debug, PartialEq)]
struct InternalDrmDeviceFd {
fd: DeviceFd,
privileged: bool,
Expand All @@ -33,7 +33,7 @@ impl BasicDevice for InternalDrmDeviceFd {}
impl ControlDevice for InternalDrmDeviceFd {}

/// Ref-counted file descriptor of an open drm device
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct DrmDeviceFd(Arc<InternalDrmDeviceFd>);

impl AsFd for DrmDeviceFd {
Expand Down
42 changes: 32 additions & 10 deletions src/backend/renderer/utils/wayland.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "backend_drm")]
use crate::wayland::drm_syncobj::{DrmSyncPoint, DrmSyncobjCachedState};
use crate::{
backend::renderer::{buffer_dimensions, buffer_has_alpha, element::RenderElement, ImportAll, Renderer},
utils::{Buffer as BufferCoord, Coordinate, Logical, Physical, Point, Rectangle, Scale, Size, Transform},
Expand Down Expand Up @@ -50,12 +52,22 @@ pub struct RendererSurfaceState {
}

#[derive(Debug)]
struct InnerBuffer(WlBuffer);
struct InnerBuffer {
buffer: WlBuffer,
#[cfg(feature = "backend_drm")]
release_point: Option<DrmSyncPoint>,
}

impl Drop for InnerBuffer {
#[inline]
fn drop(&mut self) {
self.0.release();
self.buffer.release();
#[cfg(feature = "backend_drm")]
if let Some(release_point) = &self.release_point {
if let Err(err) = release_point.signal() {
tracing::error!("Failed to signal syncobj release point: {}", err);
}
}
}
}

Expand All @@ -65,11 +77,14 @@ pub struct Buffer {
inner: Arc<InnerBuffer>,
}

impl From<WlBuffer> for Buffer {
#[inline]
fn from(buffer: WlBuffer) -> Self {
impl Buffer {
fn new(buffer: WlBuffer, #[cfg(feature = "backend_drm")] release_point: Option<DrmSyncPoint>) -> Self {
Buffer {
inner: Arc::new(InnerBuffer(buffer)),
inner: Arc::new(InnerBuffer {
buffer,
#[cfg(feature = "backend_drm")]
release_point,
}),
}
}
}
Expand All @@ -79,27 +94,30 @@ impl std::ops::Deref for Buffer {

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner.0
&self.inner.buffer
}
}

impl PartialEq<WlBuffer> for Buffer {
#[inline]
fn eq(&self, other: &WlBuffer) -> bool {
self.inner.0 == *other
self.inner.buffer == *other
}
}

impl PartialEq<WlBuffer> for &Buffer {
#[inline]
fn eq(&self, other: &WlBuffer) -> bool {
self.inner.0 == *other
self.inner.buffer == *other
}
}

impl RendererSurfaceState {
#[profiling::function]
pub(crate) fn update_buffer(&mut self, states: &SurfaceData) {
#[cfg(feature = "backend_drm")]
let mut syncobj_state = states.cached_state.pending::<DrmSyncobjCachedState>();

let mut attrs = states.cached_state.current::<SurfaceAttributes>();
self.buffer_delta = attrs.buffer_delta.take();

Expand All @@ -122,7 +140,11 @@ impl RendererSurfaceState {
self.buffer_transform = attrs.buffer_transform.into();

if !self.buffer.as_ref().map_or(false, |b| b == buffer) {
self.buffer = Some(Buffer::from(buffer));
self.buffer = Some(Buffer::new(
buffer,
#[cfg(feature = "backend_drm")]
syncobj_state.release_point.take(),
));
}

self.textures.clear();
Expand Down
24 changes: 24 additions & 0 deletions src/wayland/compositor/tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "backend_drm")]
use crate::wayland::drm_syncobj::DrmSyncobjCachedState;
use crate::{utils::Serial, wayland::compositor::SUBSURFACE_ROLE};

use super::{
Expand Down Expand Up @@ -156,6 +158,28 @@ impl PrivateSurfaceData {
{
buffer.release();
};
#[cfg(feature = "backend_drm")]
if let Some(release_point) = &my_data
.public_data
.cached_state
.pending::<DrmSyncobjCachedState>()
.release_point
{
if let Err(err) = release_point.signal() {
tracing::error!("Failed to signal syncobj release point: {}", err);
}
}
#[cfg(feature = "backend_drm")]
if let Some(release_point) = &my_data
.public_data
.cached_state
.current::<DrmSyncobjCachedState>()
.release_point
{
if let Err(err) = release_point.signal() {
tracing::error!("Failed to signal syncobj release point: {}", err);
}
}

let hooks = my_data.destruction_hooks.clone();
// don't hold the mutex while the hooks are invoked
Expand Down
Loading

0 comments on commit 42ffd4e

Please sign in to comment.