forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

background_effect.rs332 lines, 10.0 KiBHistoryRawDownload
1use std::sync::{Arc, Mutex};
2
3use niri_config::CornerRadius;
4use smithay::backend::renderer::gles::GlesRenderer;
5use smithay::utils::{Logical, Point, Rectangle, Scale};
6use smithay::wayland::compositor::{with_states, SurfaceData};
7use wayland_server::protocol::wl_surface::WlSurface;
8
9use crate::handlers::background_effect::get_cached_blur_region;
10use crate::niri_render_elements;
11use crate::render_helpers::blur::BlurOptions;
12use crate::render_helpers::damage::ExtraDamage;
13use crate::render_helpers::framebuffer_effect::{FramebufferEffect, FramebufferEffectElement};
14use crate::render_helpers::xray::{XrayElement, XrayPos};
15use crate::render_helpers::RenderCtx;
16use crate::utils::region::TransformedRegion;
17use crate::utils::surface_geo;
18
19#[derive(Debug)]
20pub struct BackgroundEffect {
21 nonxray: FramebufferEffect,
22 /// Damage when options change.
23 damage: ExtraDamage,
24 /// Corner radius for clipping.
25 ///
26 /// Stored here in addition to `RenderParams` to damage when it changes.
27 // FIXME: would be good to remove this duplication of radius.
28 corner_radius: CornerRadius,
29 blur_config: niri_config::Blur,
30 options: Options,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Default)]
34pub struct Options {
35 pub blur: bool,
36 pub xray: bool,
37 pub noise: Option<f64>,
38 pub saturation: Option<f64>,
39}
40
41impl Options {
42 fn is_visible(&self) -> bool {
43 self.xray
44 || self.blur
45 || self.noise.is_some_and(|x| x > 0.)
46 || self.saturation.is_some_and(|x| x != 1.)
47 }
48}
49
50/// Render-time parameters.
51#[derive(Debug)]
52pub struct RenderParams {
53 /// Geometry of the background effect.
54 pub geometry: Rectangle<f64, Logical>,
55 /// Effect subregion, will be clipped to `geometry`.
56 ///
57 /// `subregion.iter()` should return `geometry`-relative rectangles.
58 pub subregion: Option<TransformedRegion>,
59 /// Geometry and radius for clipping in the same coordinate space as `geometry`.
60 pub clip: Option<(Rectangle<f64, Logical>, CornerRadius)>,
61 /// Scale to use for rounding to physical pixels.
62 pub scale: f64,
63}
64
65impl RenderParams {
66 fn fit_clip_radius(&mut self) {
67 if let Some((geo, radius)) = &mut self.clip {
68 // HACK: increase radius to avoid slight bleed on rounded corners.
69 *radius = radius.expanded_by(1.);
70
71 *radius = radius.fit_to(geo.size.w as f32, geo.size.h as f32);
72 }
73 }
74}
75
76niri_render_elements! {
77 BackgroundEffectElement => {
78 FramebufferEffect = FramebufferEffectElement,
79 Xray = XrayElement,
80 ExtraDamage = ExtraDamage,
81 }
82}
83
84impl BackgroundEffect {
85 pub fn new() -> Self {
86 Self {
87 nonxray: FramebufferEffect::new(),
88 damage: ExtraDamage::new(),
89 corner_radius: CornerRadius::default(),
90 blur_config: niri_config::Blur::default(),
91 options: Options::default(),
92 }
93 }
94
95 /// Damage the background effect, for example when a blur subregion changes.
96 pub fn damage(&mut self) {
97 self.damage.damage_all();
98 self.nonxray.damage();
99 }
100
101 pub fn update_config(&mut self, config: niri_config::Blur) {
102 if self.blur_config == config {
103 return;
104 }
105
106 self.blur_config = config;
107 self.damage.damage_all();
108 self.nonxray.damage();
109 }
110
111 pub fn update_render_elements(
112 &mut self,
113 corner_radius: CornerRadius,
114 effect: niri_config::BackgroundEffect,
115 has_blur_region: bool,
116 ) {
117 // If the surface explicitly requests a blur region, default blur to true.
118 let blur = if has_blur_region {
119 effect.blur != Some(false)
120 } else {
121 effect.blur == Some(true)
122 };
123
124 let mut options = Options {
125 blur,
126 xray: effect.xray == Some(true),
127 noise: effect.noise,
128 saturation: effect.saturation,
129 };
130
131 // If we have some background effect but xray wasn't explicitly set, default it to true
132 // since it's cheaper.
133 if options.is_visible() && effect.xray.is_none() {
134 options.xray = true;
135 }
136
137 if self.options == options && self.corner_radius == corner_radius {
138 return;
139 }
140
141 self.options = options;
142 self.corner_radius = corner_radius;
143 self.damage.damage_all();
144 self.nonxray.damage();
145 }
146
147 pub fn is_visible(&self) -> bool {
148 self.options.is_visible()
149 }
150
151 pub fn render(
152 &self,
153 ctx: RenderCtx<GlesRenderer>,
154 ns: Option<usize>,
155 mut params: RenderParams,
156 xray_pos: XrayPos,
157 push: &mut dyn FnMut(BackgroundEffectElement),
158 ) {
159 if !self.is_visible() {
160 return;
161 }
162
163 if let Some(clip) = &mut params.clip {
164 clip.1 = self.corner_radius;
165 }
166 params.fit_clip_radius();
167
168 let damage = self.damage.render(params.geometry);
169
170 // Use noise/saturation from options, falling back to blur defaults if blurred, and
171 // to no effect if not blurred.
172 let blur = self.options.blur && !self.blur_config.off;
173 let blur_options = blur.then_some(BlurOptions::from(self.blur_config));
174 let noise = if blur { self.blur_config.noise } else { 0. };
175 let noise = self.options.noise.unwrap_or(noise) as f32;
176 let saturation = if blur {
177 self.blur_config.saturation
178 } else {
179 1.
180 };
181 let saturation = self.options.saturation.unwrap_or(saturation) as f32;
182
183 if self.options.xray {
184 let Some(xray) = ctx.xray else {
185 return;
186 };
187
188 push(damage.into());
189 xray.render(
190 ctx,
191 params,
192 xray_pos,
193 blur,
194 noise,
195 saturation,
196 &mut |elem| push(elem.into()),
197 );
198 } else {
199 // Render non-xray effect.
200 let elem = self
201 .nonxray
202 .render(ns, params, blur_options, noise, saturation);
203 push(elem.into());
204 }
205 }
206}
207
208fn render_params_for_tile(
209 geometry: Rectangle<f64, Logical>,
210 scale: f64,
211 clip_to_geometry: bool,
212 block_out: bool,
213 blur_region: Option<Arc<Vec<Rectangle<i32, Logical>>>>,
214 surface_geo: Rectangle<f64, Logical>,
215 surface_anim_scale: Scale<f64>,
216) -> Option<RenderParams> {
217 // Effects not requested by the surface itself are drawn to match the geometry.
218 let mut clip = true;
219
220 let mut effect_geometry = geometry;
221 let mut subregion = None;
222 if let Some(rects) = blur_region {
223 if rects.is_empty() {
224 // Surface has a set, but empty blur region.
225 return None;
226 } else {
227 // If the surface itself requests the effects, apply different defaults.
228 clip = clip_to_geometry;
229
230 // Use geometry-shaped blur for blocked-out windows to avoid unintentionally
231 // leaking any surface shapes. We render those windows as geometry-shaped solid
232 // rectangles anyway.
233 if block_out {
234 clip = true;
235 } else {
236 let mut surface_geo = surface_geo.upscale(surface_anim_scale);
237 surface_geo.loc += geometry.loc;
238
239 subregion = Some(TransformedRegion {
240 rects,
241 scale: surface_anim_scale,
242 offset: surface_geo.loc,
243 });
244
245 surface_geo = surface_geo
246 .to_physical_precise_round(scale)
247 .to_logical(scale);
248 effect_geometry = surface_geo;
249 }
250 }
251 }
252
253 // This corner radius is reset to self.corner_radius in render().
254 let clip = clip.then_some((geometry, CornerRadius::default()));
255
256 Some(RenderParams {
257 geometry: effect_geometry,
258 subregion,
259 clip,
260 scale,
261 })
262}
263
264/// Per-surface background effect stored in its data map.
265struct SurfaceBackgroundEffect(Mutex<BackgroundEffect>);
266
267impl SurfaceBackgroundEffect {
268 fn get(states: &SurfaceData) -> &Self {
269 states
270 .data_map
271 .get_or_insert(|| SurfaceBackgroundEffect(Mutex::new(BackgroundEffect::new())))
272 }
273}
274
275pub fn damage_surface(states: &SurfaceData) {
276 if let Some(effect) = states.data_map.get::<SurfaceBackgroundEffect>() {
277 effect.0.lock().unwrap().damage();
278 }
279}
280
281// Silence, Clippy
282// A Smithay user is talking
283#[allow(clippy::too_many_arguments)]
284pub fn render_for_tile(
285 ctx: RenderCtx<GlesRenderer>,
286 ns: Option<usize>,
287 geometry: Rectangle<f64, Logical>,
288 scale: f64,
289 clip_to_geometry: bool,
290 surface: &WlSurface,
291 surface_off: Point<f64, Logical>,
292 surface_anim_scale: Scale<f64>,
293 blur_config: niri_config::Blur,
294 radius: CornerRadius,
295 effect: niri_config::BackgroundEffect,
296 should_block_out: bool,
297 xray_pos: XrayPos,
298 push: &mut dyn FnMut(BackgroundEffectElement),
299) {
300 with_states(surface, |states| {
301 let background_effect = SurfaceBackgroundEffect::get(states);
302 let mut background_effect = background_effect.0.lock().unwrap();
303
304 let blur_region = get_cached_blur_region(states);
305 let has_blur_region = blur_region.as_ref().is_some_and(|r| !r.is_empty());
306
307 background_effect.update_config(blur_config);
308 background_effect.update_render_elements(radius, effect, has_blur_region);
309
310 if !background_effect.is_visible() {
311 return;
312 }
313
314 let mut surface_geo = surface_geo(states).unwrap_or_default().to_f64();
315 surface_geo.loc += surface_off;
316
317 let Some(params) = render_params_for_tile(
318 geometry,
319 scale,
320 clip_to_geometry,
321 should_block_out,
322 blur_region,
323 surface_geo,
324 surface_anim_scale,
325 ) else {
326 return;
327 };
328
329 let xray_pos = xray_pos.offset(params.geometry.loc - geometry.loc);
330 background_effect.render(ctx, ns, params, xray_pos, push);
331 });
332}