From 242e465d423525cb844fcf73b723864c23b5a630 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 25 Sep 2025 19:20:29 -0400 Subject: [PATCH] fix(corner-radius): use cached state --- src/shell/element/surface.rs | 33 +++++--------- src/wayland/protocols/corner_radius.rs | 61 +++++++++++++++++++++----- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/shell/element/surface.rs b/src/shell/element/surface.rs index c85390e0..d8ce0e17 100644 --- a/src/shell/element/surface.rs +++ b/src/shell/element/surface.rs @@ -1,5 +1,4 @@ -use crate::wayland::protocols::corner_radius::CornerRadiusData; -use smithay::reexports::wayland_server::Resource; +use crate::wayland::protocols::corner_radius::CacheableCorners; use std::{ borrow::Cow, sync::{ @@ -9,7 +8,6 @@ use std::{ time::Duration, }; -use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1; use smithay::{ backend::renderer::{ element::{ @@ -37,7 +35,7 @@ use smithay::{ }, }, 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::{ 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) -> Option<[u8; 4]> { self.wl_surface().and_then(|surface| { with_states(&surface, |states| { - let d = states - .data_map - .get::>>()?; - let guard = d.lock().unwrap(); - - let weak_data = guard.upgrade().ok()?; - - let corners = weak_data.data::()?; - - let guard = corners.lock().unwrap(); + let mut guard = states.cached_state.get::(); // guard against corner radius being too large, potentially disconnecting the outline let half_min_dim = u8::try_from(geometry_size.w.min(geometry_size.h) / 2).unwrap_or(u8::MAX); - guard.corners.map(|corners| { - [ - corners.bottom_right.min(half_min_dim), - corners.top_right.min(half_min_dim), - corners.bottom_left.min(half_min_dim), - corners.top_left.min(half_min_dim), - ] - }) + let corners = guard.current().0?; + + Some([ + corners.bottom_right.min(half_min_dim), + corners.top_right.min(half_min_dim), + corners.bottom_left.min(half_min_dim), + corners.top_left.min(half_min_dim), + ]) }) }) } diff --git a/src/wayland/protocols/corner_radius.rs b/src/wayland/protocols/corner_radius.rs index be0937d5..77d18e40 100644 --- a/src/wayland/protocols/corner_radius.rs +++ b/src/wayland/protocols/corner_radius.rs @@ -6,6 +6,7 @@ use smithay::reexports::{ wayland_server::{Client, Dispatch, DisplayHandle, GlobalDispatch, Resource, Weak}, }; use smithay::wayland::compositor::with_states; +use smithay::wayland::compositor::Cacheable; use smithay::wayland::shell::xdg::ToplevelSurface; use std::sync::Mutex; use wayland_backend::server::GlobalId; @@ -110,17 +111,7 @@ where toplevel: toplevel.downgrade(), corners: None, }); - let instance = 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(); - }); - } + let _ = data_init.init(id, data); } _ => unimplemented!(), } @@ -160,6 +151,18 @@ where cosmic_corner_radius_toplevel_v1::Request::Destroy => { let mut guard = data.lock().unwrap(); 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::(); + let pending = cached.pending(); + *pending = CacheableCorners(None); + }); + } drop(guard); state.unset_corner_radius(resource, data); @@ -172,6 +175,18 @@ where } => { let mut guard = data.lock().unwrap(); 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::(); + let pending = cached.pending(); + *pending = CacheableCorners(guard.corners); + }); + } drop(guard); state.set_corner_radius(resource, data); @@ -179,6 +194,18 @@ where cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => { let mut guard = data.lock().unwrap(); 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::(); + let pending = cached.pending(); + *pending = CacheableCorners(None); + }); + } drop(guard); state.unset_corner_radius(resource, data); @@ -204,6 +231,18 @@ pub struct Corners { pub bottom_left: u8, } +#[derive(Default, Debug, Copy, Clone)] +pub struct CacheableCorners(pub Option); + +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 { fn set_corner_radius( &mut self,