fix(corner-radius): use cached state

This commit is contained in:
Ashley Wulber 2025-09-25 19:20:29 -04:00 • committed by Victoria Brekenfeld
parent e476153086
commit 242e465d42
2 changed files with 61 additions and 33 deletions

View file

@ -1,5 +1,4 @@
use crate::wayland::protocols::corner_radius::CornerRadiusData; use crate::wayland::protocols::corner_radius::CacheableCorners;
use smithay::reexports::wayland_server::Resource;
use std::{ use std::{
borrow::Cow, borrow::Cow,
sync::{ sync::{
@ -9,7 +8,6 @@ use std::{
time::Duration, time::Duration,
}; };
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1;
use smithay::{ use smithay::{
backend::renderer::{ backend::renderer::{
element::{ element::{
@ -37,7 +35,7 @@ use smithay::{
}, },
}, },
wayland_protocols_misc::server_decoration::server::org_kde_kwin_server_decoration::Mode as KdeMode, wayland_protocols_misc::server_decoration::server::org_kde_kwin_server_decoration::Mode as KdeMode,
wayland_server::{protocol::wl_surface::WlSurface, Weak}, wayland_server::protocol::wl_surface::WlSurface,
}, },
utils::{ utils::{
user_data::UserDataMap, IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size, user_data::UserDataMap, IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size,
@ -130,29 +128,20 @@ impl CosmicSurface {
pub fn corner_radius(&self, geometry_size: Size<i32, Logical>) -> Option<[u8; 4]> { pub fn corner_radius(&self, geometry_size: Size<i32, Logical>) -> Option<[u8; 4]> {
self.wl_surface().and_then(|surface| { self.wl_surface().and_then(|surface| {
with_states(&surface, |states| { with_states(&surface, |states| {
let d = states let mut guard = states.cached_state.get::<CacheableCorners>();
.data_map
.get::<Mutex<Weak<CosmicCornerRadiusToplevelV1>>>()?;
let guard = d.lock().unwrap();
let weak_data = guard.upgrade().ok()?;
let corners = weak_data.data::<CornerRadiusData>()?;
let guard = corners.lock().unwrap();
// guard against corner radius being too large, potentially disconnecting the outline // guard against corner radius being too large, potentially disconnecting the outline
let half_min_dim = let half_min_dim =
u8::try_from(geometry_size.w.min(geometry_size.h) / 2).unwrap_or(u8::MAX); u8::try_from(geometry_size.w.min(geometry_size.h) / 2).unwrap_or(u8::MAX);
guard.corners.map(|corners| { let corners = guard.current().0?;
[
corners.bottom_right.min(half_min_dim), Some([
corners.top_right.min(half_min_dim), corners.bottom_right.min(half_min_dim),
corners.bottom_left.min(half_min_dim), corners.top_right.min(half_min_dim),
corners.top_left.min(half_min_dim), corners.bottom_left.min(half_min_dim),
] corners.top_left.min(half_min_dim),
}) ])
}) })
}) })
} }

View file

@ -6,6 +6,7 @@ use smithay::reexports::{
wayland_server::{Client, Dispatch, DisplayHandle, GlobalDispatch, Resource, Weak}, wayland_server::{Client, Dispatch, DisplayHandle, GlobalDispatch, Resource, Weak},
}; };
use smithay::wayland::compositor::with_states; use smithay::wayland::compositor::with_states;
use smithay::wayland::compositor::Cacheable;
use smithay::wayland::shell::xdg::ToplevelSurface; use smithay::wayland::shell::xdg::ToplevelSurface;
use std::sync::Mutex; use std::sync::Mutex;
use wayland_backend::server::GlobalId; use wayland_backend::server::GlobalId;
@ -110,17 +111,7 @@ where
toplevel: toplevel.downgrade(), toplevel: toplevel.downgrade(),
corners: None, corners: None,
}); });
let instance = data_init.init(id, data); let _ = data_init.init(id, data);
if let Some(surface) = state.toplevel_from_resource(&toplevel) {
with_states(surface.wl_surface(), |s| {
let data = s
.data_map
.get_or_insert_threadsafe(|| Mutex::new(instance.downgrade()));
let mut guard = data.lock().unwrap();
*guard = instance.downgrade();
});
}
} }
_ => unimplemented!(), _ => unimplemented!(),
} }
@ -160,6 +151,18 @@ where
cosmic_corner_radius_toplevel_v1::Request::Destroy => { cosmic_corner_radius_toplevel_v1::Request::Destroy => {
let mut guard = data.lock().unwrap(); let mut guard = data.lock().unwrap();
guard.corners = None; guard.corners = None;
if let Some(surface) = guard
.toplevel
.upgrade()
.ok()
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
{
with_states(surface.wl_surface(), |s| {
let mut cached = s.cached_state.get::<CacheableCorners>();
let pending = cached.pending();
*pending = CacheableCorners(None);
});
}
drop(guard); drop(guard);
state.unset_corner_radius(resource, data); state.unset_corner_radius(resource, data);
@ -172,6 +175,18 @@ where
} => { } => {
let mut guard = data.lock().unwrap(); let mut guard = data.lock().unwrap();
guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left); guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left);
if let Some(surface) = guard
.toplevel
.upgrade()
.ok()
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
{
with_states(surface.wl_surface(), |s| {
let mut cached = s.cached_state.get::<CacheableCorners>();
let pending = cached.pending();
*pending = CacheableCorners(guard.corners);
});
}
drop(guard); drop(guard);
state.set_corner_radius(resource, data); state.set_corner_radius(resource, data);
@ -179,6 +194,18 @@ where
cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => { cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => {
let mut guard = data.lock().unwrap(); let mut guard = data.lock().unwrap();
guard.corners = None; guard.corners = None;
if let Some(surface) = guard
.toplevel
.upgrade()
.ok()
.and_then(|toplevel| state.toplevel_from_resource(&toplevel))
{
with_states(surface.wl_surface(), |s| {
let mut cached = s.cached_state.get::<CacheableCorners>();
let pending = cached.pending();
*pending = CacheableCorners(None);
});
}
drop(guard); drop(guard);
state.unset_corner_radius(resource, data); state.unset_corner_radius(resource, data);
@ -204,6 +231,18 @@ pub struct Corners {
pub bottom_left: u8, pub bottom_left: u8,
} }
#[derive(Default, Debug, Copy, Clone)]
pub struct CacheableCorners(pub Option<Corners>);
impl Cacheable for CacheableCorners {
fn commit(&mut self, _dh: &DisplayHandle) -> Self {
*self
}
fn merge_into(self, into: &mut Self, _dh: &DisplayHandle) {
*into = self;
}
}
impl CornerRadiusInternal { impl CornerRadiusInternal {
fn set_corner_radius( fn set_corner_radius(
&mut self, &mut self,