| 1 | use std::sync::{Arc, Mutex}; |
| 2 | |
| 3 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 4 | use smithay::utils::{Logical, Rectangle}; |
| 5 | use smithay::wayland::background_effect::{ |
| 6 | self, BackgroundEffectSurfaceCachedState, ExtBackgroundEffectHandler, |
| 7 | }; |
| 8 | use smithay::wayland::compositor::{ |
| 9 | add_post_commit_hook, with_states, RegionAttributes, SurfaceData, |
| 10 | }; |
| 11 | |
| 12 | use crate::niri::State; |
| 13 | use crate::utils::region::region_to_non_overlapping_rects; |
| 14 | |
| 15 | |
| 16 | #[derive(Default)] |
| 17 | struct CachedBlurRegionUserData(Mutex<CachedBlurRegionInner>); |
| 18 | |
| 19 | #[derive(Default)] |
| 20 | struct CachedBlurRegionInner { |
| 21 | |
| 22 | pending_dirty: bool, |
| 23 | |
| 24 | dirty: bool, |
| 25 | |
| 26 | hook_registered: bool, |
| 27 | |
| 28 | |
| 29 | |
| 30 | rects: Option<Arc<Vec<Rectangle<i32, Logical>>>>, |
| 31 | } |
| 32 | |
| 33 | |
| 34 | pub fn get_cached_blur_region(states: &SurfaceData) -> Option<Arc<Vec<Rectangle<i32, Logical>>>> { |
| 35 | let cache = states |
| 36 | .data_map |
| 37 | .get_or_insert_threadsafe(CachedBlurRegionUserData::default); |
| 38 | let mut guard = cache.0.lock().unwrap(); |
| 39 | |
| 40 | if guard.dirty { |
| 41 | guard.dirty = false; |
| 42 | recompute_blur_region(states, &mut guard); |
| 43 | } |
| 44 | |
| 45 | guard.rects.clone() |
| 46 | } |
| 47 | |
| 48 | fn recompute_blur_region(states: &SurfaceData, inner: &mut CachedBlurRegionInner) { |
| 49 | let cached = &states.cached_state; |
| 50 | |
| 51 | let rects = if let Some(arc) = &mut inner.rects { |
| 52 | if Arc::strong_count(arc) > 1 { |
| 53 | debug!("cloning rects due to non-unique reference"); |
| 54 | } |
| 55 | arc |
| 56 | } else { |
| 57 | inner.rects.insert(Arc::new(Vec::new())) |
| 58 | }; |
| 59 | let rects = Arc::make_mut(rects); |
| 60 | |
| 61 | if cached.has::<BackgroundEffectSurfaceCachedState>() { |
| 62 | let mut guard = cached.get::<BackgroundEffectSurfaceCachedState>(); |
| 63 | if let Some(region) = &guard.current().blur_region { |
| 64 | region_to_non_overlapping_rects(region, rects); |
| 65 | } else { |
| 66 | inner.rects = None; |
| 67 | } |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | inner.rects = None; |
| 72 | } |
| 73 | |
| 74 | fn mark_blur_region_pending_dirty(wl_surface: &WlSurface) { |
| 75 | let register_hook = with_states(wl_surface, |states| { |
| 76 | let cache = states |
| 77 | .data_map |
| 78 | .get_or_insert_threadsafe(CachedBlurRegionUserData::default); |
| 79 | let mut guard = cache.0.lock().unwrap(); |
| 80 | guard.pending_dirty = true; |
| 81 | |
| 82 | if guard.hook_registered { |
| 83 | false |
| 84 | } else { |
| 85 | guard.hook_registered = true; |
| 86 | true |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | if register_hook { |
| 91 | add_post_commit_hook::<State, _>(wl_surface, |_state, _dh, surface| { |
| 92 | with_states(surface, |states| { |
| 93 | if let Some(cache) = states.data_map.get::<CachedBlurRegionUserData>() { |
| 94 | let mut guard = cache.0.lock().unwrap(); |
| 95 | if guard.pending_dirty { |
| 96 | guard.pending_dirty = false; |
| 97 | guard.dirty = true; |
| 98 | |
| 99 | crate::render_helpers::background_effect::damage_surface(states); |
| 100 | } |
| 101 | } else { |
| 102 | error!("unexpected missing CachedBlurRegionUserData"); |
| 103 | } |
| 104 | }); |
| 105 | }); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl ExtBackgroundEffectHandler for State { |
| 110 | fn capabilities(&self) -> background_effect::Capability { |
| 111 | background_effect::Capability::Blur |
| 112 | } |
| 113 | |
| 114 | fn set_blur_region(&mut self, wl_surface: WlSurface, _region: RegionAttributes) { |
| 115 | mark_blur_region_pending_dirty(&wl_surface); |
| 116 | } |
| 117 | |
| 118 | fn unset_blur_region(&mut self, wl_surface: WlSurface) { |
| 119 | mark_blur_region_pending_dirty(&wl_surface); |
| 120 | } |
| 121 | } |