forge
Explore
Log in

Don't you dare go Hollow

Move unmapped check to a pre-commit hook

Browse files at this commit

2bb6dd8c48f02b818de6fb0f307704b60a7888a2Ivan MolodetskikhParent: 7319f37
src/handlers/compositor.rsmodified+15 -34
@@ -16,7 +16,7 @@ use smithay::wayland::dmabuf::get_dmabuf;
16 use smithay::wayland::shm::{ShmHandler, ShmState};16 use smithay::wayland::shm::{ShmHandler, ShmState};
17 use smithay::{delegate_compositor, delegate_shm};17 use smithay::{delegate_compositor, delegate_shm};
18 18
19-use crate::layout::LayoutElement;19+use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;
20 use crate::niri::{ClientState, State};20 use crate::niri::{ClientState, State};
21 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped};21 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped};
22 22
@@ -81,6 +81,13 @@ impl CompositorHandler for State {
81 fn commit(&mut self, surface: &WlSurface) {81 fn commit(&mut self, surface: &WlSurface) {
82 let _span = tracy_client::span!("CompositorHandler::commit");82 let _span = tracy_client::span!("CompositorHandler::commit");
83 83
84+ on_commit_buffer_handler::<Self>(surface);
85+ self.backend.early_import(surface);
86+
87+ if is_sync_subsurface(surface) {
88+ return;
89+ }
90+
84 let mut root_surface = surface.clone();91 let mut root_surface = surface.clone();
85 while let Some(parent) = get_parent(&root_surface) {92 while let Some(parent) = get_parent(&root_surface) {
86 root_surface = parent;93 root_surface = parent;
@@ -91,30 +98,6 @@ impl CompositorHandler for State {
91 .root_surface98 .root_surface
92 .insert(surface.clone(), root_surface.clone());99 .insert(surface.clone(), root_surface.clone());
93 100
94- // Check if this root surface got unmapped to snapshot it before on_commit_buffer_handler()
95- // overwrites the buffer.
96- if surface == &root_surface {
97- let got_unmapped = with_states(surface, |states| {
98- let attrs = states.cached_state.current::<SurfaceAttributes>();
99- matches!(attrs.buffer, Some(BufferAssignment::Removed))
100- });
101-
102- if got_unmapped {
103- if let Some((mapped, _)) = self.niri.layout.find_window_and_output(surface) {
104- self.backend.with_primary_renderer(|renderer| {
105- mapped.render_and_store_snapshot(renderer);
106- });
107- }
108- }
109- }
110-
111- on_commit_buffer_handler::<Self>(surface);
112- self.backend.early_import(surface);
113-
114- if is_sync_subsurface(surface) {
115- return;
116- }
117-
118 if surface == &root_surface {101 if surface == &root_surface {
119 // This is a root surface commit. It might have mapped a previously-unmapped toplevel.102 // This is a root surface commit. It might have mapped a previously-unmapped toplevel.
120 if let Entry::Occupied(entry) = self.niri.unmapped_windows.entry(surface.clone()) {103 if let Entry::Occupied(entry) = self.niri.unmapped_windows.entry(surface.clone()) {
@@ -131,6 +114,8 @@ impl CompositorHandler for State {
131 114
132 window.on_commit();115 window.on_commit();
133 116
117+ let toplevel = window.toplevel().expect("no X11 support");
118+
134 let (rules, width, is_full_width, output) =119 let (rules, width, is_full_width, output) =
135 if let InitialConfigureState::Configured {120 if let InitialConfigureState::Configured {
136 rules,121 rules,
@@ -149,9 +134,7 @@ impl CompositorHandler for State {
149 (ResolvedWindowRules::empty(), None, false, None)134 (ResolvedWindowRules::empty(), None, false, None)
150 };135 };
151 136
152- let parent = window137+ let parent = toplevel
153- .toplevel()
154- .expect("no x11 support")
155 .parent()138 .parent()
156 .and_then(|parent| self.niri.layout.find_window_and_output(&parent))139 .and_then(|parent| self.niri.layout.find_window_and_output(&parent))
157 // Only consider the parent if we configured the window for the same140 // Only consider the parent if we configured the window for the same
@@ -165,7 +148,8 @@ impl CompositorHandler for State {
165 })148 })
166 .map(|(mapped, _)| mapped.window.clone());149 .map(|(mapped, _)| mapped.window.clone());
167 150
168- let mapped = Mapped::new(window, rules);151+ let hook = add_mapped_toplevel_pre_commit_hook(toplevel);
152+ let mapped = Mapped::new(window, rules, hook);
169 let window = mapped.window.clone();153 let window = mapped.window.clone();
170 154
171 let output = if let Some(p) = parent {155 let output = if let Some(p) = parent {
@@ -218,11 +202,8 @@ impl CompositorHandler for State {
218 false202 false
219 });203 });
220 204
221- if is_mapped {205+ // Must start the close animation before window.on_commit().
222- // The surface remains mapped; clear any cached render snapshot.206+ if !is_mapped {
223- let _ = mapped.take_last_render();
224- } else {
225- // Must start the close animation before window.on_commit().
226 self.backend.with_primary_renderer(|renderer| {207 self.backend.with_primary_renderer(|renderer| {
227 self.niri208 self.niri
228 .layout209 .layout
src/handlers/xdg_shell.rsmodified+28 -1
@@ -12,7 +12,10 @@ use smithay::reexports::wayland_server::protocol::wl_output;
12 use smithay::reexports::wayland_server::protocol::wl_seat::WlSeat;12 use smithay::reexports::wayland_server::protocol::wl_seat::WlSeat;
13 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;13 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
14 use smithay::utils::{Logical, Rectangle, Serial};14 use smithay::utils::{Logical, Rectangle, Serial};
15-use smithay::wayland::compositor::{send_surface_state, with_states};15+use smithay::wayland::compositor::{
16+ add_pre_commit_hook, send_surface_state, with_states, BufferAssignment, HookId,
17+ SurfaceAttributes,
18+};
16 use smithay::wayland::input_method::InputMethodSeat;19 use smithay::wayland::input_method::InputMethodSeat;
17 use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState};20 use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState};
18 use smithay::wayland::shell::wlr_layer::Layer;21 use smithay::wayland::shell::wlr_layer::Layer;
@@ -27,6 +30,7 @@ use smithay::{
27 };30 };
28 31
29 use crate::layout::workspace::ColumnWidth;32 use crate::layout::workspace::ColumnWidth;
33+use crate::layout::LayoutElement as _;
30 use crate::niri::{PopupGrabState, State};34 use crate::niri::{PopupGrabState, State};
31 use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped, WindowRef};35 use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped, WindowRef};
32 36
@@ -808,3 +812,26 @@ fn unconstrain_with_padding(
808 // Could not unconstrain into the padded target, so resort to the regular one.812 // Could not unconstrain into the padded target, so resort to the regular one.
809 positioner.get_unconstrained_geometry(target)813 positioner.get_unconstrained_geometry(target)
810 }814 }
815+
816+pub fn add_mapped_toplevel_pre_commit_hook(toplevel: &ToplevelSurface) -> HookId {
817+ add_pre_commit_hook::<State, _>(toplevel.wl_surface(), move |state, _dh, surface| {
818+ let Some((mapped, _)) = state.niri.layout.find_window_and_output_mut(surface) else {
819+ error!("pre-commit hook for mapped surfaces must be removed upon unmapping");
820+ return;
821+ };
822+
823+ let got_unmapped = with_states(surface, |states| {
824+ let attrs = states.cached_state.current::<SurfaceAttributes>();
825+ matches!(attrs.buffer, Some(BufferAssignment::Removed))
826+ });
827+
828+ if got_unmapped {
829+ state.backend.with_primary_renderer(|renderer| {
830+ mapped.render_and_store_snapshot(renderer);
831+ });
832+ } else {
833+ // The toplevel remains mapped; clear any cached render snapshot.
834+ let _ = mapped.take_last_render();
835+ }
836+ })
837+}
src/window/mapped.rsmodified+14 -2
@@ -12,7 +12,9 @@ use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_to
12 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;12 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
13 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;13 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
14 use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};14 use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
15-use smithay::wayland::compositor::{send_surface_state, with_states};15+use smithay::wayland::compositor::{
16+ remove_pre_commit_hook, send_surface_state, with_states, HookId,
17+};
16 use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface};18 use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface};
17 19
18 use super::{ResolvedWindowRules, WindowRef};20 use super::{ResolvedWindowRules, WindowRef};
@@ -26,6 +28,9 @@ use crate::render_helpers::{BakedBuffer, RenderSnapshot, RenderTarget};
26 pub struct Mapped {28 pub struct Mapped {
27 pub window: Window,29 pub window: Window,
28 30
31+ /// Pre-commit hook that we have on all mapped toplevel surfaces.
32+ pre_commit_hook: HookId,
33+
29 /// Up-to-date rules.34 /// Up-to-date rules.
30 rules: ResolvedWindowRules,35 rules: ResolvedWindowRules,
31 36
@@ -46,9 +51,10 @@ pub struct Mapped {
46 }51 }
47 52
48 impl Mapped {53 impl Mapped {
49- pub fn new(window: Window, rules: ResolvedWindowRules) -> Self {54+ pub fn new(window: Window, rules: ResolvedWindowRules, hook: HookId) -> Self {
50 Self {55 Self {
51 window,56 window,
57+ pre_commit_hook: hook,
52 rules,58 rules,
53 need_to_recompute_rules: false,59 need_to_recompute_rules: false,
54 is_focused: false,60 is_focused: false,
@@ -132,6 +138,12 @@ impl Mapped {
132 }138 }
133 }139 }
134 140
141+impl Drop for Mapped {
142+ fn drop(&mut self) {
143+ remove_pre_commit_hook(self.toplevel().wl_surface(), self.pre_commit_hook);
144+ }
145+}
146+
135 impl LayoutElement for Mapped {147 impl LayoutElement for Mapped {
136 type Id = Window;148 type Id = Window;
137 149