forge
Explore
Log in

Don't you dare go Hollow

Handle dmabuf blocker separately in toplevel pre-commit

Browse files at this commit

a40e7b44707692335910532c33e41cad0023844eIvan MolodetskikhParent: f1894f6
Will be needed for transactions.
src/handlers/compositor.rsmodified+63 -36
@@ -8,8 +8,9 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
8 use smithay::reexports::wayland_server::{Client, Resource};8 use smithay::reexports::wayland_server::{Client, Resource};
9 use smithay::wayland::buffer::BufferHandler;9 use smithay::wayland::buffer::BufferHandler;
10 use smithay::wayland::compositor::{10 use smithay::wayland::compositor::{
11- add_blocker, add_pre_commit_hook, get_parent, is_sync_subsurface, with_states,11+ add_blocker, add_pre_commit_hook, get_parent, is_sync_subsurface, remove_pre_commit_hook,
12- BufferAssignment, CompositorClientState, CompositorHandler, CompositorState, SurfaceAttributes,12+ with_states, BufferAssignment, CompositorClientState, CompositorHandler, CompositorState,
13+ SurfaceAttributes,
13 };14 };
14 use smithay::wayland::dmabuf::get_dmabuf;15 use smithay::wayland::dmabuf::get_dmabuf;
15 use smithay::wayland::shell::xdg::XdgToplevelSurfaceData;16 use smithay::wayland::shell::xdg::XdgToplevelSurfaceData;
@@ -46,40 +47,7 @@ impl CompositorHandler for State {
46 }47 }
47 48
48 fn new_surface(&mut self, surface: &WlSurface) {49 fn new_surface(&mut self, surface: &WlSurface) {
49- add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| {50+ self.add_default_dmabuf_pre_commit_hook(surface);
50- let maybe_dmabuf = with_states(surface, |surface_data| {
51- surface_data
52- .cached_state
53- .get::<SurfaceAttributes>()
54- .pending()
55- .buffer
56- .as_ref()
57- .and_then(|assignment| match assignment {
58- BufferAssignment::NewBuffer(buffer) => get_dmabuf(buffer).cloned().ok(),
59- _ => None,
60- })
61- });
62- if let Some(dmabuf) = maybe_dmabuf {
63- if let Ok((blocker, source)) = dmabuf.generate_blocker(Interest::READ) {
64- if let Some(client) = surface.client() {
65- let res =
66- state
67- .niri
68- .event_loop
69- .insert_source(source, move |_, _, state| {
70- let display_handle = state.niri.display_handle.clone();
71- state
72- .client_compositor_state(&client)
73- .blocker_cleared(state, &display_handle);
74- Ok(())
75- });
76- if res.is_ok() {
77- add_blocker(surface, blocker);
78- }
79- }
80- }
81- }
82- });
83 }51 }
84 52
85 fn commit(&mut self, surface: &WlSurface) {53 fn commit(&mut self, surface: &WlSurface) {
@@ -157,6 +125,8 @@ impl CompositorHandler for State {
157 })125 })
158 .map(|(mapped, _)| mapped.window.clone());126 .map(|(mapped, _)| mapped.window.clone());
159 127
128+ // The mapped pre-commit hook deals with dma-bufs on its own.
129+ self.remove_default_dmabuf_pre_commit_hook(toplevel.wl_surface());
160 let hook = add_mapped_toplevel_pre_commit_hook(toplevel);130 let hook = add_mapped_toplevel_pre_commit_hook(toplevel);
161 let mapped = Mapped::new(window, rules, hook);131 let mapped = Mapped::new(window, rules, hook);
162 let window = mapped.window.clone();132 let window = mapped.window.clone();
@@ -246,6 +216,7 @@ impl CompositorHandler for State {
246 });216 });
247 217
248 self.niri.layout.remove_window(&window);218 self.niri.layout.remove_window(&window);
219+ self.add_default_dmabuf_pre_commit_hook(surface);
249 220
250 if was_active {221 if was_active {
251 self.maybe_warp_cursor_to_focus();222 self.maybe_warp_cursor_to_focus();
@@ -356,6 +327,8 @@ impl CompositorHandler for State {
356 self.niri327 self.niri
357 .root_surface328 .root_surface
358 .retain(|k, v| k != surface && v != surface);329 .retain(|k, v| k != surface && v != surface);
330+
331+ self.niri.dmabuf_pre_commit_hook.remove(surface);
359 }332 }
360 }333 }
361 334
@@ -371,3 +344,57 @@ impl ShmHandler for State {
371 344
372 delegate_compositor!(State);345 delegate_compositor!(State);
373 delegate_shm!(State);346 delegate_shm!(State);
347+
348+impl State {
349+ pub fn add_default_dmabuf_pre_commit_hook(&mut self, surface: &WlSurface) {
350+ let hook = add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| {
351+ let maybe_dmabuf = with_states(surface, |surface_data| {
352+ surface_data
353+ .cached_state
354+ .get::<SurfaceAttributes>()
355+ .pending()
356+ .buffer
357+ .as_ref()
358+ .and_then(|assignment| match assignment {
359+ BufferAssignment::NewBuffer(buffer) => get_dmabuf(buffer).cloned().ok(),
360+ _ => None,
361+ })
362+ });
363+ if let Some(dmabuf) = maybe_dmabuf {
364+ if let Ok((blocker, source)) = dmabuf.generate_blocker(Interest::READ) {
365+ if let Some(client) = surface.client() {
366+ let res =
367+ state
368+ .niri
369+ .event_loop
370+ .insert_source(source, move |_, _, state| {
371+ let display_handle = state.niri.display_handle.clone();
372+ state
373+ .client_compositor_state(&client)
374+ .blocker_cleared(state, &display_handle);
375+ Ok(())
376+ });
377+ if res.is_ok() {
378+ add_blocker(surface, blocker);
379+ trace!("added default dmabuf blocker");
380+ }
381+ }
382+ }
383+ }
384+ });
385+
386+ let s = surface.clone();
387+ if let Some(prev) = self.niri.dmabuf_pre_commit_hook.insert(s, hook) {
388+ error!("tried to add dmabuf pre-commit hook when there was already one");
389+ remove_pre_commit_hook(surface, prev);
390+ }
391+ }
392+
393+ pub fn remove_default_dmabuf_pre_commit_hook(&mut self, surface: &WlSurface) {
394+ if let Some(hook) = self.niri.dmabuf_pre_commit_hook.remove(surface) {
395+ remove_pre_commit_hook(surface, hook);
396+ } else {
397+ error!("tried to remove dmabuf pre-commit hook but there was none");
398+ }
399+ }
400+}
src/handlers/xdg_shell.rsmodified+38 -5
@@ -1,5 +1,6 @@
1 use std::cell::Cell;1 use std::cell::Cell;
2 2
3+use calloop::Interest;
3 use smithay::desktop::{4 use smithay::desktop::{
4 find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, utils, LayerSurface,5 find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, utils, LayerSurface,
5 PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window,6 PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window,
@@ -17,8 +18,10 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
17 use smithay::reexports::wayland_server::{self, Resource, WEnum};18 use smithay::reexports::wayland_server::{self, Resource, WEnum};
18 use smithay::utils::{Logical, Rectangle, Serial};19 use smithay::utils::{Logical, Rectangle, Serial};
19 use smithay::wayland::compositor::{20 use smithay::wayland::compositor::{
20- add_pre_commit_hook, with_states, BufferAssignment, HookId, SurfaceAttributes,21+ add_blocker, add_pre_commit_hook, with_states, BufferAssignment, CompositorHandler as _,
22+ HookId, SurfaceAttributes,
21 };23 };
24+use smithay::wayland::dmabuf::get_dmabuf;
22 use smithay::wayland::input_method::InputMethodSeat;25 use smithay::wayland::input_method::InputMethodSeat;
23 use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState};26 use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState};
24 use smithay::wayland::shell::wlr_layer::{self, Layer};27 use smithay::wayland::shell::wlr_layer::{self, Layer};
@@ -491,6 +494,7 @@ impl XdgShellHandler for State {
491 let was_active = active_window == Some(&window);494 let was_active = active_window == Some(&window);
492 495
493 self.niri.layout.remove_window(&window);496 self.niri.layout.remove_window(&window);
497+ self.add_default_dmabuf_pre_commit_hook(surface.wl_surface());
494 498
495 if was_active {499 if was_active {
496 self.maybe_warp_cursor_to_focus();500 self.maybe_warp_cursor_to_focus();
@@ -1005,10 +1009,17 @@ pub fn add_mapped_toplevel_pre_commit_hook(toplevel: &ToplevelSurface) -> HookId
1005 return;1009 return;
1006 };1010 };
1007 1011
1008- let (got_unmapped, commit_serial) = with_states(surface, |states| {1012+ let (got_unmapped, dmabuf, commit_serial) = with_states(surface, |states| {
1009- let got_unmapped = {1013+ let (got_unmapped, dmabuf) = {
1010 let mut guard = states.cached_state.get::<SurfaceAttributes>();1014 let mut guard = states.cached_state.get::<SurfaceAttributes>();
1011- matches!(guard.pending().buffer, Some(BufferAssignment::Removed))1015+ match guard.pending().buffer.as_ref() {
1016+ Some(BufferAssignment::NewBuffer(buffer)) => {
1017+ let dmabuf = get_dmabuf(buffer).cloned().ok();
1018+ (false, dmabuf)
1019+ }
1020+ Some(BufferAssignment::Removed) => (true, None),
1021+ None => (false, None),
1022+ }
1012 };1023 };
1013 1024
1014 let role = states1025 let role = states
@@ -1018,9 +1029,12 @@ pub fn add_mapped_toplevel_pre_commit_hook(toplevel: &ToplevelSurface) -> HookId
1018 .lock()1029 .lock()
1019 .unwrap();1030 .unwrap();
1020 1031
1021- (got_unmapped, role.configure_serial)1032+ (got_unmapped, dmabuf, role.configure_serial)
1022 });1033 });
1023 1034
1035+ let mut dmabuf_blocker =
1036+ dmabuf.and_then(|dmabuf| dmabuf.generate_blocker(Interest::READ).ok());
1037+
1024 let animate = if let Some(serial) = commit_serial {1038 let animate = if let Some(serial) = commit_serial {
1025 mapped.should_animate_commit(serial)1039 mapped.should_animate_commit(serial)
1026 } else {1040 } else {
@@ -1028,6 +1042,25 @@ pub fn add_mapped_toplevel_pre_commit_hook(toplevel: &ToplevelSurface) -> HookId
1028 false1042 false
1029 };1043 };
1030 1044
1045+ if let Some((blocker, source)) = dmabuf_blocker.take() {
1046+ if let Some(client) = surface.client() {
1047+ let res = state
1048+ .niri
1049+ .event_loop
1050+ .insert_source(source, move |_, _, state| {
1051+ let display_handle = state.niri.display_handle.clone();
1052+ state
1053+ .client_compositor_state(&client)
1054+ .blocker_cleared(state, &display_handle);
1055+ Ok(())
1056+ });
1057+ if res.is_ok() {
1058+ add_blocker(surface, blocker);
1059+ trace!("added toplevel dmabuf blocker");
1060+ }
1061+ }
1062+ }
1063+
1031 let window = mapped.window.clone();1064 let window = mapped.window.clone();
1032 if got_unmapped {1065 if got_unmapped {
1033 state.backend.with_primary_renderer(|renderer| {1066 state.backend.with_primary_renderer(|renderer| {
src/niri.rsmodified+6 -2
@@ -67,8 +67,8 @@ use smithay::utils::{
67 Transform, SERIAL_COUNTER,67 Transform, SERIAL_COUNTER,
68 };68 };
69 use smithay::wayland::compositor::{69 use smithay::wayland::compositor::{
70- with_states, with_surface_tree_downward, CompositorClientState, CompositorState, SurfaceData,70+ with_states, with_surface_tree_downward, CompositorClientState, CompositorState, HookId,
71- TraversalAction,71+ SurfaceData, TraversalAction,
72 };72 };
73 use smithay::wayland::cursor_shape::CursorShapeManagerState;73 use smithay::wayland::cursor_shape::CursorShapeManagerState;
74 use smithay::wayland::dmabuf::DmabufState;74 use smithay::wayland::dmabuf::DmabufState;
@@ -189,6 +189,9 @@ pub struct Niri {
189 // normal get_parent() is cleared out.189 // normal get_parent() is cleared out.
190 pub root_surface: HashMap<WlSurface, WlSurface>,190 pub root_surface: HashMap<WlSurface, WlSurface>,
191 191
192+ // Dmabuf readiness pre-commit hook for a surface.
193+ pub dmabuf_pre_commit_hook: HashMap<WlSurface, HookId>,
194+
192 pub output_state: HashMap<Output, OutputState>,195 pub output_state: HashMap<Output, OutputState>,
193 pub output_by_name: HashMap<String, Output>,196 pub output_by_name: HashMap<String, Output>,
194 197
@@ -1732,6 +1735,7 @@ impl Niri {
1732 output_by_name: HashMap::new(),1735 output_by_name: HashMap::new(),
1733 unmapped_windows: HashMap::new(),1736 unmapped_windows: HashMap::new(),
1734 root_surface: HashMap::new(),1737 root_surface: HashMap::new(),
1738+ dmabuf_pre_commit_hook: HashMap::new(),
1735 monitors_active: true,1739 monitors_active: true,
1736 1740
1737 devices: HashSet::new(),1741 devices: HashSet::new(),