Skip to content

Commit 242e465

Browse files
wash2Drakulix
authored andcommitted
fix(corner-radius): use cached state
1 parent e476153 commit 242e465

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

src/shell/element/surface.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::wayland::protocols::corner_radius::CornerRadiusData;
2-
use smithay::reexports::wayland_server::Resource;
1+
use crate::wayland::protocols::corner_radius::CacheableCorners;
32
use std::{
43
borrow::Cow,
54
sync::{
@@ -9,7 +8,6 @@ use std::{
98
time::Duration,
109
};
1110

12-
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1;
1311
use smithay::{
1412
backend::renderer::{
1513
element::{
@@ -37,7 +35,7 @@ use smithay::{
3735
},
3836
},
3937
wayland_protocols_misc::server_decoration::server::org_kde_kwin_server_decoration::Mode as KdeMode,
40-
wayland_server::{protocol::wl_surface::WlSurface, Weak},
38+
wayland_server::protocol::wl_surface::WlSurface,
4139
},
4240
utils::{
4341
user_data::UserDataMap, IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size,
@@ -130,29 +128,20 @@ impl CosmicSurface {
130128
pub fn corner_radius(&self, geometry_size: Size<i32, Logical>) -> Option<[u8; 4]> {
131129
self.wl_surface().and_then(|surface| {
132130
with_states(&surface, |states| {
133-
let d = states
134-
.data_map
135-
.get::<Mutex<Weak<CosmicCornerRadiusToplevelV1>>>()?;
136-
let guard = d.lock().unwrap();
137-
138-
let weak_data = guard.upgrade().ok()?;
139-
140-
let corners = weak_data.data::<CornerRadiusData>()?;
141-
142-
let guard = corners.lock().unwrap();
131+
let mut guard = states.cached_state.get::<CacheableCorners>();
143132

144133
// guard against corner radius being too large, potentially disconnecting the outline
145134
let half_min_dim =
146135
u8::try_from(geometry_size.w.min(geometry_size.h) / 2).unwrap_or(u8::MAX);
147136

148-
guard.corners.map(|corners| {
149-
[
150-
corners.bottom_right.min(half_min_dim),
151-
corners.top_right.min(half_min_dim),
152-
corners.bottom_left.min(half_min_dim),
153-
corners.top_left.min(half_min_dim),
154-
]
155-
})
137+
let corners = guard.current().0?;
138+
139+
Some([
140+
corners.bottom_right.min(half_min_dim),
141+
corners.top_right.min(half_min_dim),
142+
corners.bottom_left.min(half_min_dim),
143+
corners.top_left.min(half_min_dim),
144+
])
156145
})
157146
})
158147
}

src/wayland/protocols/corner_radius.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use smithay::reexports::{
66
wayland_server::{Client, Dispatch, DisplayHandle, GlobalDispatch, Resource, Weak},
77
};
88
use smithay::wayland::compositor::with_states;
9+
use smithay::wayland::compositor::Cacheable;
910
use smithay::wayland::shell::xdg::ToplevelSurface;
1011
use std::sync::Mutex;
1112
use wayland_backend::server::GlobalId;
@@ -110,17 +111,7 @@ where
110111
toplevel: toplevel.downgrade(),
111112
corners: None,
112113
});
113-
let instance = data_init.init(id, data);
114-
115-
if let Some(surface) = state.toplevel_from_resource(&toplevel) {
116-
with_states(surface.wl_surface(), |s| {
117-
let data = s
118-
.data_map
119-
.get_or_insert_threadsafe(|| Mutex::new(instance.downgrade()));
120-
let mut guard = data.lock().unwrap();
121-
*guard = instance.downgrade();
122-
});
123-
}
114+
let _ = data_init.init(id, data);
124115
}
125116
_ => unimplemented!(),
126117
}
@@ -160,6 +151,18 @@ where
160151
cosmic_corner_radius_toplevel_v1::Request::Destroy => {
161152
let mut guard = data.lock().unwrap();
162153
guard.corners = None;
154+
if let Some(surface) = guard
155+
.toplevel
156+
.upgrade()
157+
.ok()
158+
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
159+
{
160+
with_states(surface.wl_surface(), |s| {
161+
let mut cached = s.cached_state.get::<CacheableCorners>();
162+
let pending = cached.pending();
163+
*pending = CacheableCorners(None);
164+
});
165+
}
163166
drop(guard);
164167

165168
state.unset_corner_radius(resource, data);
@@ -172,13 +175,37 @@ where
172175
} => {
173176
let mut guard = data.lock().unwrap();
174177
guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left);
178+
if let Some(surface) = guard
179+
.toplevel
180+
.upgrade()
181+
.ok()
182+
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
183+
{
184+
with_states(surface.wl_surface(), |s| {
185+
let mut cached = s.cached_state.get::<CacheableCorners>();
186+
let pending = cached.pending();
187+
*pending = CacheableCorners(guard.corners);
188+
});
189+
}
175190
drop(guard);
176191

177192
state.set_corner_radius(resource, data);
178193
}
179194
cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => {
180195
let mut guard = data.lock().unwrap();
181196
guard.corners = None;
197+
if let Some(surface) = guard
198+
.toplevel
199+
.upgrade()
200+
.ok()
201+
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
202+
{
203+
with_states(surface.wl_surface(), |s| {
204+
let mut cached = s.cached_state.get::<CacheableCorners>();
205+
let pending = cached.pending();
206+
*pending = CacheableCorners(None);
207+
});
208+
}
182209
drop(guard);
183210

184211
state.unset_corner_radius(resource, data);
@@ -204,6 +231,18 @@ pub struct Corners {
204231
pub bottom_left: u8,
205232
}
206233

234+
#[derive(Default, Debug, Copy, Clone)]
235+
pub struct CacheableCorners(pub Option<Corners>);
236+
237+
impl Cacheable for CacheableCorners {
238+
fn commit(&mut self, _dh: &DisplayHandle) -> Self {
239+
*self
240+
}
241+
fn merge_into(self, into: &mut Self, _dh: &DisplayHandle) {
242+
*into = self;
243+
}
244+
}
245+
207246
impl CornerRadiusInternal {
208247
fn set_corner_radius(
209248
&mut self,

0 commit comments

Comments
 (0)