Implement blur background effect
Browse files at this commit73c0ce75d87f7537a48bfbaf5a2ee6ffc6ac1a53Parent: 1b1715f17 changed files +674 -18
- niri-config/src/appearance.rs +59 -1
- niri-config/src/lib.rs +11 -0
- src/handlers/compositor.rs +4 -1
- src/layer/mapped.rs +7 -0
- src/layout/mod.rs +7 -0
- src/layout/tile.rs +2 -0
- src/niri.rs +4 -0
- src/render_helpers/background_effect.rs +39 -6
- src/render_helpers/blur.rs +355 -0
- src/render_helpers/effect_buffer.rs +98 -6
- src/render_helpers/mod.rs +1 -0
- src/render_helpers/shaders/blur.vert +11 -0
- src/render_helpers/shaders/blur_down.frag +21 -0
- src/render_helpers/shaders/blur_up.frag +29 -0
- src/render_helpers/shaders/mod.rs +9 -0
- src/render_helpers/xray.rs +7 -3
- src/window/mapped.rs +10 -1
niri-config/src/appearance.rsmodified+59 -1
| @@ -1006,11 +1006,62 @@ where | |||
| 1006 | } | 1006 | } |
| 1007 | } | 1007 | } |
| 1008 | 1008 | ||
| 1009 | +#[derive(Debug, Clone, Copy, PartialEq)] | ||
| 1010 | +pub struct Blur { | ||
| 1011 | + pub off: bool, | ||
| 1012 | + pub passes: u8, | ||
| 1013 | + pub offset: f64, | ||
| 1014 | + pub noise: f64, | ||
| 1015 | + pub saturation: f64, | ||
| 1016 | +} | ||
| 1017 | + | ||
| 1018 | +impl Default for Blur { | ||
| 1019 | + fn default() -> Self { | ||
| 1020 | + Self { | ||
| 1021 | + off: false, | ||
| 1022 | + passes: 3, | ||
| 1023 | + offset: 3., | ||
| 1024 | + noise: 0.02, | ||
| 1025 | + saturation: 1.5, | ||
| 1026 | + } | ||
| 1027 | + } | ||
| 1028 | +} | ||
| 1029 | + | ||
| 1030 | +#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] | ||
| 1031 | +pub struct BlurPart { | ||
| 1032 | + #[knuffel(child)] | ||
| 1033 | + pub off: bool, | ||
| 1034 | + #[knuffel(child)] | ||
| 1035 | + pub on: bool, | ||
| 1036 | + #[knuffel(child, unwrap(argument))] | ||
| 1037 | + pub passes: Option<u8>, | ||
| 1038 | + #[knuffel(child, unwrap(argument))] | ||
| 1039 | + pub offset: Option<FloatOrInt<0, 100>>, | ||
| 1040 | + #[knuffel(child, unwrap(argument))] | ||
| 1041 | + pub noise: Option<FloatOrInt<0, 1000>>, | ||
| 1042 | + #[knuffel(child, unwrap(argument))] | ||
| 1043 | + pub saturation: Option<FloatOrInt<0, 1000>>, | ||
| 1044 | +} | ||
| 1045 | + | ||
| 1046 | +impl MergeWith<BlurPart> for Blur { | ||
| 1047 | + fn merge_with(&mut self, part: &BlurPart) { | ||
| 1048 | + self.off |= part.off; | ||
| 1049 | + if part.on { | ||
| 1050 | + self.off = false; | ||
| 1051 | + } | ||
| 1052 | + | ||
| 1053 | + merge_clone!((self, part), passes); | ||
| 1054 | + merge!((self, part), offset, noise, saturation); | ||
| 1055 | + } | ||
| 1056 | +} | ||
| 1057 | + | ||
| 1009 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] | 1058 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] |
| 1010 | pub struct BackgroundEffectRule { | 1059 | pub struct BackgroundEffectRule { |
| 1011 | #[knuffel(child, unwrap(argument))] | 1060 | #[knuffel(child, unwrap(argument))] |
| 1012 | pub xray: Option<bool>, | 1061 | pub xray: Option<bool>, |
| 1013 | #[knuffel(child, unwrap(argument))] | 1062 | #[knuffel(child, unwrap(argument))] |
| 1063 | + pub blur: Option<bool>, | ||
| 1064 | + #[knuffel(child, unwrap(argument))] | ||
| 1014 | pub noise: Option<FloatOrInt<0, 1000>>, | 1065 | pub noise: Option<FloatOrInt<0, 1000>>, |
| 1015 | #[knuffel(child, unwrap(argument))] | 1066 | #[knuffel(child, unwrap(argument))] |
| 1016 | pub saturation: Option<FloatOrInt<0, 1000>>, | 1067 | pub saturation: Option<FloatOrInt<0, 1000>>, |
| @@ -1026,13 +1077,20 @@ pub struct BackgroundEffect { | |||
| 1026 | /// - `Some(true)`: xray even if no other background effect is active | 1077 | /// - `Some(true)`: xray even if no other background effect is active |
| 1027 | pub xray: Option<bool>, | 1078 | pub xray: Option<bool>, |
| 1028 | 1079 | ||
| 1080 | + /// Whether to blur the background. | ||
| 1081 | + /// | ||
| 1082 | + /// - `None`: no blur | ||
| 1083 | + /// - `Some(false)`: never blur | ||
| 1084 | + /// - `Some(true)`: always blur | ||
| 1085 | + pub blur: Option<bool>, | ||
| 1086 | + | ||
| 1029 | pub noise: Option<f64>, | 1087 | pub noise: Option<f64>, |
| 1030 | pub saturation: Option<f64>, | 1088 | pub saturation: Option<f64>, |
| 1031 | } | 1089 | } |
| 1032 | 1090 | ||
| 1033 | impl MergeWith<BackgroundEffectRule> for BackgroundEffect { | 1091 | impl MergeWith<BackgroundEffectRule> for BackgroundEffect { |
| 1034 | fn merge_with(&mut self, part: &BackgroundEffectRule) { | 1092 | fn merge_with(&mut self, part: &BackgroundEffectRule) { |
| 1035 | - merge_clone_opt!((self, part), xray); | 1093 | + merge_clone_opt!((self, part), xray, blur); |
| 1036 | 1094 | ||
| 1037 | if let Some(x) = part.noise { | 1095 | if let Some(x) = part.noise { |
| 1038 | self.noise = Some(x.0); | 1096 | self.noise = Some(x.0); |
niri-config/src/lib.rsmodified+11 -0
| @@ -78,6 +78,7 @@ pub struct Config { | |||
| 78 | pub hotkey_overlay: HotkeyOverlay, | 78 | pub hotkey_overlay: HotkeyOverlay, |
| 79 | pub config_notification: ConfigNotification, | 79 | pub config_notification: ConfigNotification, |
| 80 | pub animations: Animations, | 80 | pub animations: Animations, |
| 81 | + pub blur: Blur, | ||
| 81 | pub gestures: Gestures, | 82 | pub gestures: Gestures, |
| 82 | pub overview: Overview, | 83 | pub overview: Overview, |
| 83 | pub environment: Environment, | 84 | pub environment: Environment, |
| @@ -194,6 +195,7 @@ where | |||
| 194 | "hotkey-overlay" => m_merge!(hotkey_overlay), | 195 | "hotkey-overlay" => m_merge!(hotkey_overlay), |
| 195 | "config-notification" => m_merge!(config_notification), | 196 | "config-notification" => m_merge!(config_notification), |
| 196 | "animations" => m_merge!(animations), | 197 | "animations" => m_merge!(animations), |
| 198 | + "blur" => m_merge!(blur), | ||
| 197 | "gestures" => m_merge!(gestures), | 199 | "gestures" => m_merge!(gestures), |
| 198 | "overview" => m_merge!(overview), | 200 | "overview" => m_merge!(overview), |
| 199 | "xwayland-satellite" => m_merge!(xwayland_satellite), | 201 | "xwayland-satellite" => m_merge!(xwayland_satellite), |
| @@ -1616,6 +1618,13 @@ mod tests { | |||
| 1616 | }, | 1618 | }, |
| 1617 | ), | 1619 | ), |
| 1618 | }, | 1620 | }, |
| 1621 | + blur: Blur { | ||
| 1622 | + off: false, | ||
| 1623 | + passes: 3, | ||
| 1624 | + offset: 3.0, | ||
| 1625 | + noise: 0.02, | ||
| 1626 | + saturation: 1.5, | ||
| 1627 | + }, | ||
| 1619 | gestures: Gestures { | 1628 | gestures: Gestures { |
| 1620 | dnd_edge_view_scroll: DndEdgeViewScroll { | 1629 | dnd_edge_view_scroll: DndEdgeViewScroll { |
| 1621 | trigger_width: 10.0, | 1630 | trigger_width: 10.0, |
| @@ -1847,6 +1856,7 @@ mod tests { | |||
| 1847 | tiled_state: None, | 1856 | tiled_state: None, |
| 1848 | background_effect: BackgroundEffectRule { | 1857 | background_effect: BackgroundEffectRule { |
| 1849 | xray: None, | 1858 | xray: None, |
| 1859 | + blur: None, | ||
| 1850 | noise: None, | 1860 | noise: None, |
| 1851 | saturation: None, | 1861 | saturation: None, |
| 1852 | }, | 1862 | }, |
| @@ -1887,6 +1897,7 @@ mod tests { | |||
| 1887 | baba_is_float: None, | 1897 | baba_is_float: None, |
| 1888 | background_effect: BackgroundEffectRule { | 1898 | background_effect: BackgroundEffectRule { |
| 1889 | xray: None, | 1899 | xray: None, |
| 1900 | + blur: None, | ||
| 1890 | noise: None, | 1901 | noise: None, |
| 1891 | saturation: None, | 1902 | saturation: None, |
| 1892 | }, | 1903 | }, |
src/handlers/compositor.rsmodified+4 -1
| @@ -195,7 +195,10 @@ impl CompositorHandler for State { | |||
| 195 | // The mapped pre-commit hook deals with dma-bufs on its own. | 195 | // The mapped pre-commit hook deals with dma-bufs on its own. |
| 196 | self.remove_default_dmabuf_pre_commit_hook(surface); | 196 | self.remove_default_dmabuf_pre_commit_hook(surface); |
| 197 | let hook = add_mapped_toplevel_pre_commit_hook(toplevel); | 197 | let hook = add_mapped_toplevel_pre_commit_hook(toplevel); |
| 198 | - let mapped = Mapped::new(window, rules, hook); | 198 | + let mapped = { |
| 199 | + let config = self.niri.config.borrow(); | ||
| 200 | + Mapped::new(window, rules, hook, &config) | ||
| 201 | + }; | ||
| 199 | let window = mapped.window.clone(); | 202 | let window = mapped.window.clone(); |
| 200 | 203 | ||
| 201 | let target = if let Some(p) = &parent { | 204 | let target = if let Some(p) = &parent { |
src/layer/mapped.rsmodified+7 -0
| @@ -42,6 +42,9 @@ pub struct MappedLayer { | |||
| 42 | /// The shadow around the surface. | 42 | /// The shadow around the surface. |
| 43 | shadow: Shadow, | 43 | shadow: Shadow, |
| 44 | 44 | ||
| 45 | + /// The blur config, passed for background effect rendering. | ||
| 46 | + blur_config: niri_config::Blur, | ||
| 47 | + | ||
| 45 | /// The view size for the layer surface's output. | 48 | /// The view size for the layer surface's output. |
| 46 | view_size: Size<f64, Logical>, | 49 | view_size: Size<f64, Logical>, |
| 47 | 50 | ||
| @@ -85,6 +88,7 @@ impl MappedLayer { | |||
| 85 | view_size, | 88 | view_size, |
| 86 | scale, | 89 | scale, |
| 87 | shadow: Shadow::new(shadow_config), | 90 | shadow: Shadow::new(shadow_config), |
| 91 | + blur_config: config.blur, | ||
| 88 | clock, | 92 | clock, |
| 89 | } | 93 | } |
| 90 | } | 94 | } |
| @@ -95,6 +99,8 @@ impl MappedLayer { | |||
| 95 | shadow_config.on = false; | 99 | shadow_config.on = false; |
| 96 | shadow_config.merge_with(&self.rules.shadow); | 100 | shadow_config.merge_with(&self.rules.shadow); |
| 97 | self.shadow.update_config(shadow_config); | 101 | self.shadow.update_config(shadow_config); |
| 102 | + | ||
| 103 | + self.blur_config = config.blur; | ||
| 98 | } | 104 | } |
| 99 | 105 | ||
| 100 | pub fn update_shaders(&mut self) { | 106 | pub fn update_shaders(&mut self) { |
| @@ -234,6 +240,7 @@ impl MappedLayer { | |||
| 234 | self.scale, | 240 | self.scale, |
| 235 | false, | 241 | false, |
| 236 | surface, | 242 | surface, |
| 243 | + self.blur_config, | ||
| 237 | radius, | 244 | radius, |
| 238 | self.rules.background_effect, | 245 | self.rules.background_effect, |
| 239 | xray_pos, | 246 | xray_pos, |
src/layout/mod.rsmodified+7 -0
| @@ -134,6 +134,11 @@ pub trait LayoutElement { | |||
| 134 | /// Unique ID of this element. | 134 | /// Unique ID of this element. |
| 135 | fn id(&self) -> &Self::Id; | 135 | fn id(&self) -> &Self::Id; |
| 136 | 136 | ||
| 137 | + /// Updates the config for the element. | ||
| 138 | + fn update_config(&mut self, blur_config: niri_config::Blur) { | ||
| 139 | + let _ = blur_config; | ||
| 140 | + } | ||
| 141 | + | ||
| 137 | /// Visual size of the element. | 142 | /// Visual size of the element. |
| 138 | /// | 143 | /// |
| 139 | /// This is what the user would consider the size, i.e. excluding CSD shadows and whatnot. | 144 | /// This is what the user would consider the size, i.e. excluding CSD shadows and whatnot. |
| @@ -364,6 +369,7 @@ pub struct Options { | |||
| 364 | pub animations: niri_config::Animations, | 369 | pub animations: niri_config::Animations, |
| 365 | pub gestures: niri_config::Gestures, | 370 | pub gestures: niri_config::Gestures, |
| 366 | pub overview: niri_config::Overview, | 371 | pub overview: niri_config::Overview, |
| 372 | + pub blur: niri_config::Blur, | ||
| 367 | // Debug flags. | 373 | // Debug flags. |
| 368 | pub disable_resize_throttling: bool, | 374 | pub disable_resize_throttling: bool, |
| 369 | pub disable_transactions: bool, | 375 | pub disable_transactions: bool, |
| @@ -624,6 +630,7 @@ impl Options { | |||
| 624 | animations: config.animations.clone(), | 630 | animations: config.animations.clone(), |
| 625 | gestures: config.gestures, | 631 | gestures: config.gestures, |
| 626 | overview: config.overview, | 632 | overview: config.overview, |
| 633 | + blur: config.blur, | ||
| 627 | disable_resize_throttling: config.debug.disable_resize_throttling, | 634 | disable_resize_throttling: config.debug.disable_resize_throttling, |
| 628 | disable_transactions: config.debug.disable_transactions, | 635 | disable_transactions: config.debug.disable_transactions, |
| 629 | deactivate_unfocused_windows: config.debug.deactivate_unfocused_windows, | 636 | deactivate_unfocused_windows: config.debug.deactivate_unfocused_windows, |
src/layout/tile.rsmodified+2 -0
| @@ -251,6 +251,8 @@ impl<W: LayoutElement> Tile<W> { | |||
| 251 | 251 | ||
| 252 | let shadow_config = self.options.layout.shadow.merged_with(&rules.shadow); | 252 | let shadow_config = self.options.layout.shadow.merged_with(&rules.shadow); |
| 253 | self.shadow.update_config(shadow_config); | 253 | self.shadow.update_config(shadow_config); |
| 254 | + | ||
| 255 | + self.window.update_config(self.options.blur); | ||
| 254 | } | 256 | } |
| 255 | 257 | ||
| 256 | pub fn update_shaders(&mut self) { | 258 | pub fn update_shaders(&mut self) { |
src/niri.rsmodified+4 -0
| @@ -149,6 +149,7 @@ use crate::protocols::mutter_x11_interop::MutterX11InteropManagerState; | |||
| 149 | use crate::protocols::output_management::OutputManagementManagerState; | 149 | use crate::protocols::output_management::OutputManagementManagerState; |
| 150 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; | 150 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; |
| 151 | use crate::protocols::virtual_pointer::VirtualPointerManagerState; | 151 | use crate::protocols::virtual_pointer::VirtualPointerManagerState; |
| 152 | +use crate::render_helpers::blur::BlurOptions; | ||
| 152 | use crate::render_helpers::debug::push_opaque_regions; | 153 | use crate::render_helpers::debug::push_opaque_regions; |
| 153 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 154 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 154 | use crate::render_helpers::renderer::NiriRenderer; | 155 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -4077,13 +4078,16 @@ impl Niri { | |||
| 4077 | state.xray.workspaces.push((geo, bg_color)); | 4078 | state.xray.workspaces.push((geo, bg_color)); |
| 4078 | } | 4079 | } |
| 4079 | state.xray.backdrop_color = state.backdrop_buffer.color(); | 4080 | state.xray.backdrop_color = state.backdrop_buffer.color(); |
| 4081 | + let blur_options = BlurOptions::from(self.config.borrow().blur); | ||
| 4080 | for buf in &state.xray.background { | 4082 | for buf in &state.xray.background { |
| 4081 | let mut buffer = buf.borrow_mut(); | 4083 | let mut buffer = buf.borrow_mut(); |
| 4082 | buffer.update_size(size, scale); | 4084 | buffer.update_size(size, scale); |
| 4085 | + buffer.update_blur_options(blur_options); | ||
| 4083 | } | 4086 | } |
| 4084 | for buf in &state.xray.backdrop { | 4087 | for buf in &state.xray.backdrop { |
| 4085 | let mut buffer = buf.borrow_mut(); | 4088 | let mut buffer = buf.borrow_mut(); |
| 4086 | buffer.update_size(size, scale); | 4089 | buffer.update_size(size, scale); |
| 4090 | + buffer.update_blur_options(blur_options); | ||
| 4087 | } | 4091 | } |
| 4088 | 4092 | ||
| 4089 | let layer_map = layer_map_for_output(out); | 4093 | let layer_map = layer_map_for_output(out); |
src/render_helpers/background_effect.rsmodified+39 -6
| @@ -20,11 +20,13 @@ pub struct BackgroundEffect { | |||
| 20 | /// Stored here in addition to `RenderParams` to damage when it changes. | 20 | /// Stored here in addition to `RenderParams` to damage when it changes. |
| 21 | // FIXME: would be good to remove this duplication of radius. | 21 | // FIXME: would be good to remove this duplication of radius. |
| 22 | corner_radius: CornerRadius, | 22 | corner_radius: CornerRadius, |
| 23 | + blur_config: niri_config::Blur, | ||
| 23 | options: Options, | 24 | options: Options, |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Default)] | 27 | #[derive(Debug, Clone, Copy, PartialEq, Default)] |
| 27 | pub struct Options { | 28 | pub struct Options { |
| 29 | + pub blur: bool, | ||
| 28 | pub xray: bool, | 30 | pub xray: bool, |
| 29 | pub noise: Option<f64>, | 31 | pub noise: Option<f64>, |
| 30 | pub saturation: Option<f64>, | 32 | pub saturation: Option<f64>, |
| @@ -32,7 +34,10 @@ pub struct Options { | |||
| 32 | 34 | ||
| 33 | impl Options { | 35 | impl Options { |
| 34 | fn is_visible(&self) -> bool { | 36 | fn is_visible(&self) -> bool { |
| 35 | - self.xray || self.noise.is_some_and(|x| x > 0.) || self.saturation.is_some_and(|x| x != 1.) | 37 | + self.xray |
| 38 | + || self.blur | ||
| 39 | + || self.noise.is_some_and(|x| x > 0.) | ||
| 40 | + || self.saturation.is_some_and(|x| x != 1.) | ||
| 36 | } | 41 | } |
| 37 | } | 42 | } |
| 38 | 43 | ||
| @@ -70,16 +75,27 @@ impl BackgroundEffect { | |||
| 70 | Self { | 75 | Self { |
| 71 | damage: ExtraDamage::new(), | 76 | damage: ExtraDamage::new(), |
| 72 | corner_radius: CornerRadius::default(), | 77 | corner_radius: CornerRadius::default(), |
| 78 | + blur_config: niri_config::Blur::default(), | ||
| 73 | options: Options::default(), | 79 | options: Options::default(), |
| 74 | } | 80 | } |
| 75 | } | 81 | } |
| 76 | 82 | ||
| 83 | + pub fn update_config(&mut self, config: niri_config::Blur) { | ||
| 84 | + if self.blur_config == config { | ||
| 85 | + return; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + self.blur_config = config; | ||
| 89 | + self.damage.damage_all(); | ||
| 90 | + } | ||
| 91 | + | ||
| 77 | pub fn update_render_elements( | 92 | pub fn update_render_elements( |
| 78 | &mut self, | 93 | &mut self, |
| 79 | corner_radius: CornerRadius, | 94 | corner_radius: CornerRadius, |
| 80 | effect: niri_config::BackgroundEffect, | 95 | effect: niri_config::BackgroundEffect, |
| 81 | ) { | 96 | ) { |
| 82 | let mut options = Options { | 97 | let mut options = Options { |
| 98 | + blur: effect.blur == Some(true), | ||
| 83 | xray: effect.xray == Some(true), | 99 | xray: effect.xray == Some(true), |
| 84 | noise: effect.noise, | 100 | noise: effect.noise, |
| 85 | saturation: effect.saturation, | 101 | saturation: effect.saturation, |
| @@ -122,8 +138,17 @@ impl BackgroundEffect { | |||
| 122 | 138 | ||
| 123 | let damage = self.damage.render(params.geometry); | 139 | let damage = self.damage.render(params.geometry); |
| 124 | 140 | ||
| 125 | - let noise = self.options.noise.unwrap_or(0.) as f32; | 141 | + // Use noise/saturation from options, falling back to blur defaults if blurred, and |
| 126 | - let saturation = self.options.saturation.unwrap_or(1.) as f32; | 142 | + // to no effect if not blurred. |
| 143 | + let blur = self.options.blur && !self.blur_config.off; | ||
| 144 | + let noise = if blur { self.blur_config.noise } else { 0. }; | ||
| 145 | + let noise = self.options.noise.unwrap_or(noise) as f32; | ||
| 146 | + let saturation = if blur { | ||
| 147 | + self.blur_config.saturation | ||
| 148 | + } else { | ||
| 149 | + 1. | ||
| 150 | + }; | ||
| 151 | + let saturation = self.options.saturation.unwrap_or(saturation) as f32; | ||
| 127 | 152 | ||
| 128 | if self.options.xray { | 153 | if self.options.xray { |
| 129 | let Some(xray) = ctx.xray else { | 154 | let Some(xray) = ctx.xray else { |
| @@ -131,9 +156,15 @@ impl BackgroundEffect { | |||
| 131 | }; | 156 | }; |
| 132 | 157 | ||
| 133 | push(damage.into()); | 158 | push(damage.into()); |
| 134 | - xray.render(ctx, params, xray_pos, noise, saturation, &mut |elem| { | 159 | + xray.render( |
| 135 | - push(elem.into()) | 160 | + ctx, |
| 136 | - }); | 161 | + params, |
| 162 | + xray_pos, | ||
| 163 | + blur, | ||
| 164 | + noise, | ||
| 165 | + saturation, | ||
| 166 | + &mut |elem| push(elem.into()), | ||
| 167 | + ); | ||
| 137 | } else { | 168 | } else { |
| 138 | // Render non-xray effect. | 169 | // Render non-xray effect. |
| 139 | } | 170 | } |
| @@ -160,6 +191,7 @@ pub fn render_for_tile( | |||
| 160 | scale: f64, | 191 | scale: f64, |
| 161 | clip_to_geometry: bool, | 192 | clip_to_geometry: bool, |
| 162 | surface: &WlSurface, | 193 | surface: &WlSurface, |
| 194 | + blur_config: niri_config::Blur, | ||
| 163 | radius: CornerRadius, | 195 | radius: CornerRadius, |
| 164 | effect: niri_config::BackgroundEffect, | 196 | effect: niri_config::BackgroundEffect, |
| 165 | xray_pos: XrayPos, | 197 | xray_pos: XrayPos, |
| @@ -169,6 +201,7 @@ pub fn render_for_tile( | |||
| 169 | let background_effect = SurfaceBackgroundEffect::get(states); | 201 | let background_effect = SurfaceBackgroundEffect::get(states); |
| 170 | let mut background_effect = background_effect.0.lock().unwrap(); | 202 | let mut background_effect = background_effect.0.lock().unwrap(); |
| 171 | 203 | ||
| 204 | + background_effect.update_config(blur_config); | ||
| 172 | background_effect.update_render_elements(radius, effect); | 205 | background_effect.update_render_elements(radius, effect); |
| 173 | 206 | ||
| 174 | if !background_effect.is_visible() { | 207 | if !background_effect.is_visible() { |
src/render_helpers/blur.rsadded+355 -0
| @@ -0,0 +1,355 @@ | |||
| 1 | +use std::cmp::max; | ||
| 2 | +use std::iter::{once, zip}; | ||
| 3 | +use std::rc::Rc; | ||
| 4 | + | ||
| 5 | +use anyhow::{ensure, Context as _}; | ||
| 6 | +use smithay::backend::allocator::Fourcc; | ||
| 7 | +use smithay::backend::renderer::gles::{ | ||
| 8 | + ffi, link_program, GlesError, GlesFrame, GlesRenderer, GlesTexture, | ||
| 9 | +}; | ||
| 10 | +use smithay::backend::renderer::{ContextId, Frame as _, Renderer as _, Texture as _}; | ||
| 11 | +use smithay::gpu_span_location; | ||
| 12 | +use smithay::utils::{Buffer, Size}; | ||
| 13 | + | ||
| 14 | +use crate::render_helpers::shaders::Shaders; | ||
| 15 | + | ||
| 16 | +#[derive(Debug)] | ||
| 17 | +pub struct Blur { | ||
| 18 | + program: BlurProgram, | ||
| 19 | + /// Context ID of the renderer that created the program and the textures. | ||
| 20 | + renderer_context_id: ContextId<GlesTexture>, | ||
| 21 | + /// Output texture followed by intermediate textures, large to small. | ||
| 22 | + /// | ||
| 23 | + /// Created lazily and stored here to avoid recreating blur textures frequently. | ||
| 24 | + textures: Vec<GlesTexture>, | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +#[derive(Debug, Default, Clone, Copy, PartialEq)] | ||
| 28 | +pub struct BlurOptions { | ||
| 29 | + pub passes: u8, | ||
| 30 | + pub offset: f64, | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +impl From<niri_config::Blur> for BlurOptions { | ||
| 34 | + fn from(config: niri_config::Blur) -> Self { | ||
| 35 | + Self { | ||
| 36 | + passes: config.passes, | ||
| 37 | + offset: config.offset, | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +#[derive(Debug, Clone)] | ||
| 43 | +pub struct BlurProgram(Rc<BlurProgramInner>); | ||
| 44 | + | ||
| 45 | +#[derive(Debug)] | ||
| 46 | +struct BlurProgramInner { | ||
| 47 | + down: BlurProgramInternal, | ||
| 48 | + up: BlurProgramInternal, | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +#[derive(Debug)] | ||
| 52 | +struct BlurProgramInternal { | ||
| 53 | + program: ffi::types::GLuint, | ||
| 54 | + uniform_tex: ffi::types::GLint, | ||
| 55 | + uniform_half_pixel: ffi::types::GLint, | ||
| 56 | + uniform_offset: ffi::types::GLint, | ||
| 57 | + attrib_vert: ffi::types::GLint, | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +unsafe fn compile_program(gl: &ffi::Gles2, src: &str) -> Result<BlurProgramInternal, GlesError> { | ||
| 61 | + let program = unsafe { link_program(gl, include_str!("shaders/blur.vert"), src)? }; | ||
| 62 | + | ||
| 63 | + let vert = c"vert"; | ||
| 64 | + let tex = c"tex"; | ||
| 65 | + let half_pixel = c"half_pixel"; | ||
| 66 | + let offset = c"offset"; | ||
| 67 | + | ||
| 68 | + Ok(BlurProgramInternal { | ||
| 69 | + program, | ||
| 70 | + uniform_tex: gl.GetUniformLocation(program, tex.as_ptr()), | ||
| 71 | + uniform_half_pixel: gl.GetUniformLocation(program, half_pixel.as_ptr()), | ||
| 72 | + uniform_offset: gl.GetUniformLocation(program, offset.as_ptr()), | ||
| 73 | + attrib_vert: gl.GetAttribLocation(program, vert.as_ptr()), | ||
| 74 | + }) | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +impl BlurProgram { | ||
| 78 | + pub fn compile(renderer: &mut GlesRenderer) -> anyhow::Result<Self> { | ||
| 79 | + renderer | ||
| 80 | + .with_context(move |gl| unsafe { | ||
| 81 | + let down = compile_program(gl, include_str!("shaders/blur_down.frag")) | ||
| 82 | + .context("error compiling blur_down shader")?; | ||
| 83 | + let up = compile_program(gl, include_str!("shaders/blur_up.frag")) | ||
| 84 | + .context("error compiling blur_up shader")?; | ||
| 85 | + Ok(Self(Rc::new(BlurProgramInner { down, up }))) | ||
| 86 | + }) | ||
| 87 | + .context("error making GL context current")? | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + pub fn destroy(self, renderer: &mut GlesRenderer) -> Result<(), GlesError> { | ||
| 91 | + renderer.with_context(move |gl| unsafe { | ||
| 92 | + gl.DeleteProgram(self.0.down.program); | ||
| 93 | + gl.DeleteProgram(self.0.up.program); | ||
| 94 | + }) | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +impl Blur { | ||
| 99 | + pub fn new(renderer: &mut GlesRenderer) -> Option<Self> { | ||
| 100 | + let program = Shaders::get(renderer).blur.clone()?; | ||
| 101 | + Some(Self { | ||
| 102 | + program, | ||
| 103 | + renderer_context_id: renderer.context_id(), | ||
| 104 | + textures: Vec::new(), | ||
| 105 | + }) | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + pub fn context_id(&self) -> ContextId<GlesTexture> { | ||
| 109 | + self.renderer_context_id.clone() | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + pub fn prepare_textures( | ||
| 113 | + &mut self, | ||
| 114 | + mut create_texture: impl FnMut(Fourcc, Size<i32, Buffer>) -> Result<GlesTexture, GlesError>, | ||
| 115 | + source: &GlesTexture, | ||
| 116 | + options: BlurOptions, | ||
| 117 | + ) -> anyhow::Result<()> { | ||
| 118 | + let _span = tracy_client::span!("Blur::prepare_textures"); | ||
| 119 | + | ||
| 120 | + let passes = options.passes.clamp(1, 31) as usize; | ||
| 121 | + let size = source.size(); | ||
| 122 | + | ||
| 123 | + if let Some(output) = self.textures.first_mut() { | ||
| 124 | + let old_size = output.size(); | ||
| 125 | + if old_size != size { | ||
| 126 | + trace!( | ||
| 127 | + "recreating textures: output size changed from {} × {} to {} × {}", | ||
| 128 | + old_size.w, | ||
| 129 | + old_size.h, | ||
| 130 | + size.w, | ||
| 131 | + size.h | ||
| 132 | + ); | ||
| 133 | + self.textures.clear(); | ||
| 134 | + } else if !output.is_unique_reference() { | ||
| 135 | + debug!("recreating textures: not unique",); | ||
| 136 | + // We only need to recreate the output texture here, but this case shouldn't really | ||
| 137 | + // happen anyway, and this is simpler. | ||
| 138 | + self.textures.clear(); | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + // Create any missing textures. | ||
| 143 | + let mut w = size.w; | ||
| 144 | + let mut h = size.h; | ||
| 145 | + for i in 0..=passes { | ||
| 146 | + let size = Size::new(w, h); | ||
| 147 | + w = max(1, w / 2); | ||
| 148 | + h = max(1, h / 2); | ||
| 149 | + | ||
| 150 | + if self.textures.len() > i { | ||
| 151 | + // This texture already exists. | ||
| 152 | + continue; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + // debug!("creating texture for step {i} sized {w} × {h}"); | ||
| 156 | + | ||
| 157 | + let texture: GlesTexture = | ||
| 158 | + create_texture(Fourcc::Abgr8888, size).context("error creating texture")?; | ||
| 159 | + self.textures.push(texture); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + // Drop any no longer needed textures. | ||
| 163 | + self.textures.drain(passes + 1..); | ||
| 164 | + | ||
| 165 | + Ok(()) | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + pub fn render( | ||
| 169 | + &mut self, | ||
| 170 | + frame: &mut GlesFrame, | ||
| 171 | + source: &GlesTexture, | ||
| 172 | + options: BlurOptions, | ||
| 173 | + ) -> anyhow::Result<GlesTexture> { | ||
| 174 | + let _span = tracy_client::span!("Blur::render"); | ||
| 175 | + trace!("rendering blur"); | ||
| 176 | + | ||
| 177 | + ensure!( | ||
| 178 | + frame.context_id() == self.renderer_context_id, | ||
| 179 | + "wrong renderer" | ||
| 180 | + ); | ||
| 181 | + | ||
| 182 | + let passes = options.passes.clamp(1, 31) as usize; | ||
| 183 | + let size = source.size(); | ||
| 184 | + | ||
| 185 | + ensure!( | ||
| 186 | + self.textures.len() == passes + 1, | ||
| 187 | + "wrong textures len: expected {}, got {}", | ||
| 188 | + passes + 1, | ||
| 189 | + self.textures.len() | ||
| 190 | + ); | ||
| 191 | + | ||
| 192 | + let output = &mut self.textures[0]; | ||
| 193 | + ensure!( | ||
| 194 | + output.size() == size, | ||
| 195 | + "wrong output texture size: expected {size:?}, got {:?}", | ||
| 196 | + output.size() | ||
| 197 | + ); | ||
| 198 | + | ||
| 199 | + ensure!( | ||
| 200 | + output.is_unique_reference(), | ||
| 201 | + "output texture has a non-unique reference" | ||
| 202 | + ); | ||
| 203 | + | ||
| 204 | + frame.with_profiled_context(gpu_span_location!("Blur::render"), |gl| unsafe { | ||
| 205 | + while gl.GetError() != ffi::NO_ERROR {} | ||
| 206 | + | ||
| 207 | + let mut current_fbo = 0i32; | ||
| 208 | + let mut viewport = [0i32; 4]; | ||
| 209 | + gl.GetIntegerv(ffi::FRAMEBUFFER_BINDING, &mut current_fbo as *mut _); | ||
| 210 | + gl.GetIntegerv(ffi::VIEWPORT, viewport.as_mut_ptr()); | ||
| 211 | + | ||
| 212 | + gl.Disable(ffi::BLEND); | ||
| 213 | + gl.Disable(ffi::SCISSOR_TEST); | ||
| 214 | + | ||
| 215 | + gl.ActiveTexture(ffi::TEXTURE0); | ||
| 216 | + | ||
| 217 | + let mut fbos = [0; 2]; | ||
| 218 | + gl.GenFramebuffers(fbos.len() as _, fbos.as_mut_ptr()); | ||
| 219 | + gl.BindFramebuffer(ffi::DRAW_FRAMEBUFFER, fbos[0]); | ||
| 220 | + | ||
| 221 | + let program = &self.program.0.down; | ||
| 222 | + gl.UseProgram(program.program); | ||
| 223 | + gl.Uniform1i(program.uniform_tex, 0); | ||
| 224 | + gl.Uniform1f(program.uniform_offset, options.offset as f32); | ||
| 225 | + | ||
| 226 | + let vertices: [f32; 12] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]; | ||
| 227 | + gl.EnableVertexAttribArray(program.attrib_vert as u32); | ||
| 228 | + gl.BindBuffer(ffi::ARRAY_BUFFER, 0); | ||
| 229 | + gl.VertexAttribPointer( | ||
| 230 | + program.attrib_vert as u32, | ||
| 231 | + 2, | ||
| 232 | + ffi::FLOAT, | ||
| 233 | + ffi::FALSE, | ||
| 234 | + 0, | ||
| 235 | + vertices.as_ptr().cast(), | ||
| 236 | + ); | ||
| 237 | + | ||
| 238 | + let src = once(source).chain(&self.textures[1..]); | ||
| 239 | + let dst = &self.textures[1..]; | ||
| 240 | + for (src, dst) in zip(src, dst) { | ||
| 241 | + let dst_size = dst.size(); | ||
| 242 | + let w = dst_size.w; | ||
| 243 | + let h = dst_size.h; | ||
| 244 | + gl.Viewport(0, 0, w, h); | ||
| 245 | + | ||
| 246 | + // During downsampling, half_pixel is half of the destination pixel. | ||
| 247 | + gl.Uniform2f(program.uniform_half_pixel, 0.5 / w as f32, 0.5 / h as f32); | ||
| 248 | + | ||
| 249 | + let src = src.tex_id(); | ||
| 250 | + let dst = dst.tex_id(); | ||
| 251 | + | ||
| 252 | + trace!("drawing down {src} to {dst}"); | ||
| 253 | + gl.FramebufferTexture2D( | ||
| 254 | + ffi::DRAW_FRAMEBUFFER, | ||
| 255 | + ffi::COLOR_ATTACHMENT0, | ||
| 256 | + ffi::TEXTURE_2D, | ||
| 257 | + dst, | ||
| 258 | + 0, | ||
| 259 | + ); | ||
| 260 | + | ||
| 261 | + gl.BindTexture(ffi::TEXTURE_2D, src); | ||
| 262 | + gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MIN_FILTER, ffi::LINEAR as i32); | ||
| 263 | + gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MAG_FILTER, ffi::LINEAR as i32); | ||
| 264 | + gl.TexParameteri( | ||
| 265 | + ffi::TEXTURE_2D, | ||
| 266 | + ffi::TEXTURE_WRAP_S, | ||
| 267 | + ffi::CLAMP_TO_EDGE as i32, | ||
| 268 | + ); | ||
| 269 | + gl.TexParameteri( | ||
| 270 | + ffi::TEXTURE_2D, | ||
| 271 | + ffi::TEXTURE_WRAP_T, | ||
| 272 | + ffi::CLAMP_TO_EDGE as i32, | ||
| 273 | + ); | ||
| 274 | + | ||
| 275 | + gl.DrawArrays(ffi::TRIANGLES, 0, 6); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + gl.DisableVertexAttribArray(program.attrib_vert as u32); | ||
| 279 | + | ||
| 280 | + // Up | ||
| 281 | + let program = &self.program.0.up; | ||
| 282 | + gl.UseProgram(program.program); | ||
| 283 | + gl.Uniform1i(program.uniform_tex, 0); | ||
| 284 | + gl.Uniform1f(program.uniform_offset, options.offset as f32); | ||
| 285 | + | ||
| 286 | + let vertices: [f32; 12] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]; | ||
| 287 | + gl.EnableVertexAttribArray(program.attrib_vert as u32); | ||
| 288 | + gl.BindBuffer(ffi::ARRAY_BUFFER, 0); | ||
| 289 | + gl.VertexAttribPointer( | ||
| 290 | + program.attrib_vert as u32, | ||
| 291 | + 2, | ||
| 292 | + ffi::FLOAT, | ||
| 293 | + ffi::FALSE, | ||
| 294 | + 0, | ||
| 295 | + vertices.as_ptr().cast(), | ||
| 296 | + ); | ||
| 297 | + | ||
| 298 | + let src = self.textures.iter().rev(); | ||
| 299 | + let dst = self.textures.iter().rev().skip(1); | ||
| 300 | + for (src, dst) in zip(src, dst) { | ||
| 301 | + let dst_size = dst.size(); | ||
| 302 | + let w = dst_size.w; | ||
| 303 | + let h = dst_size.h; | ||
| 304 | + gl.Viewport(0, 0, w, h); | ||
| 305 | + | ||
| 306 | + // During upsampling, half_pixel is half of the source pixel. | ||
| 307 | + let src_size = src.size(); | ||
| 308 | + let src_w = src_size.w as f32; | ||
| 309 | + let src_h = src_size.h as f32; | ||
| 310 | + gl.Uniform2f(program.uniform_half_pixel, 0.5 / src_w, 0.5 / src_h); | ||
| 311 | + | ||
| 312 | + let src = src.tex_id(); | ||
| 313 | + let dst = dst.tex_id(); | ||
| 314 | + | ||
| 315 | + trace!("drawing up {src} to {dst}"); | ||
| 316 | + gl.FramebufferTexture2D( | ||
| 317 | + ffi::DRAW_FRAMEBUFFER, | ||
| 318 | + ffi::COLOR_ATTACHMENT0, | ||
| 319 | + ffi::TEXTURE_2D, | ||
| 320 | + dst, | ||
| 321 | + 0, | ||
| 322 | + ); | ||
| 323 | + | ||
| 324 | + gl.BindTexture(ffi::TEXTURE_2D, src); | ||
| 325 | + gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MIN_FILTER, ffi::LINEAR as i32); | ||
| 326 | + gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MAG_FILTER, ffi::LINEAR as i32); | ||
| 327 | + gl.TexParameteri( | ||
| 328 | + ffi::TEXTURE_2D, | ||
| 329 | + ffi::TEXTURE_WRAP_S, | ||
| 330 | + ffi::CLAMP_TO_EDGE as i32, | ||
| 331 | + ); | ||
| 332 | + gl.TexParameteri( | ||
| 333 | + ffi::TEXTURE_2D, | ||
| 334 | + ffi::TEXTURE_WRAP_T, | ||
| 335 | + ffi::CLAMP_TO_EDGE as i32, | ||
| 336 | + ); | ||
| 337 | + | ||
| 338 | + gl.DrawArrays(ffi::TRIANGLES, 0, 6); | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + gl.DisableVertexAttribArray(program.attrib_vert as u32); | ||
| 342 | + | ||
| 343 | + gl.BindFramebuffer(ffi::FRAMEBUFFER, 0); | ||
| 344 | + gl.DeleteFramebuffers(fbos.len() as _, fbos.as_ptr()); | ||
| 345 | + | ||
| 346 | + // Restore state set by GlesFrame that we just modified. | ||
| 347 | + gl.Enable(ffi::BLEND); | ||
| 348 | + gl.Enable(ffi::SCISSOR_TEST); | ||
| 349 | + gl.BindFramebuffer(ffi::FRAMEBUFFER, current_fbo as u32); | ||
| 350 | + gl.Viewport(viewport[0], viewport[1], viewport[2], viewport[3]); | ||
| 351 | + })?; | ||
| 352 | + | ||
| 353 | + Ok(self.textures[0].clone()) | ||
| 354 | + } | ||
| 355 | +} | ||
src/render_helpers/effect_buffer.rsmodified+98 -6
| @@ -1,10 +1,10 @@ | |||
| 1 | use std::mem; | 1 | use std::mem; |
| 2 | 2 | ||
| 3 | -use anyhow::Context as _; | 3 | +use anyhow::{ensure, Context as _}; |
| 4 | use smithay::backend::allocator::Fourcc; | 4 | use smithay::backend::allocator::Fourcc; |
| 5 | use smithay::backend::renderer::damage::OutputDamageTracker; | 5 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 6 | use smithay::backend::renderer::element::{Id, RenderElementStates}; | 6 | use smithay::backend::renderer::element::{Id, RenderElementStates}; |
| 7 | -use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 7 | +use smithay::backend::renderer::gles::{GlesFrame, GlesRenderer, GlesTexture}; |
| 8 | use smithay::backend::renderer::utils::CommitCounter; | 8 | use smithay::backend::renderer::utils::CommitCounter; |
| 9 | use smithay::backend::renderer::{ | 9 | use smithay::backend::renderer::{ |
| 10 | Bind as _, Color32F, ContextId, Offscreen as _, Renderer as _, Texture, | 10 | Bind as _, Color32F, ContextId, Offscreen as _, Renderer as _, Texture, |
| @@ -12,6 +12,7 @@ use smithay::backend::renderer::{ | |||
| 12 | use smithay::utils::{Buffer, Logical, Physical, Scale, Size, Transform}; | 12 | use smithay::utils::{Buffer, Logical, Physical, Scale, Size, Transform}; |
| 13 | 13 | ||
| 14 | use crate::niri::OutputRenderElements; | 14 | use crate::niri::OutputRenderElements; |
| 15 | +use crate::render_helpers::blur::{Blur, BlurOptions}; | ||
| 15 | 16 | ||
| 16 | #[derive(Debug)] | 17 | #[derive(Debug)] |
| 17 | pub struct EffectBuffer { | 18 | pub struct EffectBuffer { |
| @@ -22,13 +23,17 @@ pub struct EffectBuffer { | |||
| 22 | size: Size<i32, Buffer>, | 23 | size: Size<i32, Buffer>, |
| 23 | /// Scale of the effect buffer. | 24 | /// Scale of the effect buffer. |
| 24 | scale: Scale<f64>, | 25 | scale: Scale<f64>, |
| 26 | + /// Options for blurring. | ||
| 27 | + blur_options: BlurOptions, | ||
| 25 | 28 | ||
| 26 | /// Elements to be rendered on demand. | 29 | /// Elements to be rendered on demand. |
| 27 | elements: Elements, | 30 | elements: Elements, |
| 28 | /// Offscreen buffer where elements get rendered. | 31 | /// Offscreen buffer where elements get rendered. |
| 29 | offscreen: Option<Offscreen>, | 32 | offscreen: Option<Offscreen>, |
| 33 | + /// Blurring program, if available. | ||
| 34 | + blur: Option<Blur>, | ||
| 30 | 35 | ||
| 31 | - /// Commit counter for the offscreen texture. | 36 | + /// Commit counter that takes into account both original and blurred texture changes. |
| 32 | commit_counter: CommitCounter, | 37 | commit_counter: CommitCounter, |
| 33 | } | 38 | } |
| 34 | 39 | ||
| @@ -55,6 +60,10 @@ struct Offscreen { | |||
| 55 | damage: OutputDamageTracker, | 60 | damage: OutputDamageTracker, |
| 56 | /// Render element states from the last render into the offscreen. | 61 | /// Render element states from the last render into the offscreen. |
| 57 | states: RenderElementStates, | 62 | states: RenderElementStates, |
| 63 | + /// Rendered blurred version of the texture. | ||
| 64 | + /// | ||
| 65 | + /// When texture needs to be reblurred, this field must be reset to `None`. | ||
| 66 | + blurred: Option<GlesTexture>, | ||
| 58 | } | 67 | } |
| 59 | 68 | ||
| 60 | impl Default for Elements { | 69 | impl Default for Elements { |
| @@ -69,8 +78,10 @@ impl EffectBuffer { | |||
| 69 | id: Id::new(), | 78 | id: Id::new(), |
| 70 | size: Size::default(), | 79 | size: Size::default(), |
| 71 | scale: Scale::from(1.), | 80 | scale: Scale::from(1.), |
| 81 | + blur_options: BlurOptions::default(), | ||
| 72 | elements: Elements::default(), | 82 | elements: Elements::default(), |
| 73 | offscreen: None, | 83 | offscreen: None, |
| 84 | + blur: None, | ||
| 74 | commit_counter: CommitCounter::default(), | 85 | commit_counter: CommitCounter::default(), |
| 75 | } | 86 | } |
| 76 | } | 87 | } |
| @@ -100,6 +111,21 @@ impl EffectBuffer { | |||
| 100 | self.scale = scale; | 111 | self.scale = scale; |
| 101 | } | 112 | } |
| 102 | 113 | ||
| 114 | + pub fn update_blur_options(&mut self, options: BlurOptions) { | ||
| 115 | + if self.blur_options == options { | ||
| 116 | + return; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + self.blur_options = options; | ||
| 120 | + | ||
| 121 | + if let Some(offscreen) = &mut self.offscreen { | ||
| 122 | + if offscreen.blurred.is_some() { | ||
| 123 | + offscreen.blurred = None; | ||
| 124 | + self.commit_counter.increment(); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 103 | pub fn elements(&mut self) -> &mut Vec<OutputRenderElements<GlesRenderer>> { | 129 | pub fn elements(&mut self) -> &mut Vec<OutputRenderElements<GlesRenderer>> { |
| 104 | // Assume we're going to insert new elements, switch to New. | 130 | // Assume we're going to insert new elements, switch to New. |
| 105 | match mem::take(&mut self.elements) { | 131 | match mem::take(&mut self.elements) { |
| @@ -113,12 +139,19 @@ impl EffectBuffer { | |||
| 113 | elements | 139 | elements |
| 114 | } | 140 | } |
| 115 | 141 | ||
| 116 | - pub fn prepare(&mut self, renderer: &mut GlesRenderer) -> bool { | 142 | + pub fn prepare(&mut self, renderer: &mut GlesRenderer, blur: bool) -> bool { |
| 117 | if let Err(err) = self.prepare_offscreen(renderer) { | 143 | if let Err(err) = self.prepare_offscreen(renderer) { |
| 118 | warn!("error preparing offscreen: {err:?}"); | 144 | warn!("error preparing offscreen: {err:?}"); |
| 119 | return false; | 145 | return false; |
| 120 | }; | 146 | }; |
| 121 | 147 | ||
| 148 | + if blur { | ||
| 149 | + if let Err(err) = self.prepare_blur(renderer) { | ||
| 150 | + warn!("error preparing blur: {err:?}"); | ||
| 151 | + return false; | ||
| 152 | + } | ||
| 153 | + } | ||
| 154 | + | ||
| 122 | true | 155 | true |
| 123 | } | 156 | } |
| 124 | 157 | ||
| @@ -176,6 +209,7 @@ impl EffectBuffer { | |||
| 176 | scale: self.scale, | 209 | scale: self.scale, |
| 177 | damage, | 210 | damage, |
| 178 | states: RenderElementStates::default(), | 211 | states: RenderElementStates::default(), |
| 212 | + blurred: None, | ||
| 179 | }) | 213 | }) |
| 180 | }; | 214 | }; |
| 181 | 215 | ||
| @@ -189,6 +223,7 @@ impl EffectBuffer { | |||
| 189 | offscreen.damage = OutputDamageTracker::new(buffer_size, self.scale, Transform::Normal); | 223 | offscreen.damage = OutputDamageTracker::new(buffer_size, self.scale, Transform::Normal); |
| 190 | 224 | ||
| 191 | self.commit_counter.increment(); | 225 | self.commit_counter.increment(); |
| 226 | + offscreen.blurred = None; | ||
| 192 | } | 227 | } |
| 193 | 228 | ||
| 194 | // Render the elements if any. | 229 | // Render the elements if any. |
| @@ -215,6 +250,9 @@ impl EffectBuffer { | |||
| 215 | 250 | ||
| 216 | if res.damage.is_some() { | 251 | if res.damage.is_some() { |
| 217 | self.commit_counter.increment(); | 252 | self.commit_counter.increment(); |
| 253 | + | ||
| 254 | + // Original texture changed; reset the blurred texture. | ||
| 255 | + offscreen.blurred = None; | ||
| 218 | } | 256 | } |
| 219 | 257 | ||
| 220 | // Clear and put the storage back. | 258 | // Clear and put the storage back. |
| @@ -224,8 +262,62 @@ impl EffectBuffer { | |||
| 224 | Ok(()) | 262 | Ok(()) |
| 225 | } | 263 | } |
| 226 | 264 | ||
| 227 | - pub fn render(&mut self) -> anyhow::Result<GlesTexture> { | 265 | + fn prepare_blur(&mut self, renderer: &mut GlesRenderer) -> anyhow::Result<()> { |
| 266 | + let offscreen = self.offscreen.as_mut().context("missing offscreen")?; | ||
| 267 | + if offscreen.blurred.is_some() { | ||
| 268 | + // Already rendered. | ||
| 269 | + return Ok(()); | ||
| 270 | + } | ||
| 271 | + | ||
| 272 | + if let Some(blur) = &self.blur { | ||
| 273 | + if blur.context_id() != renderer.context_id() { | ||
| 274 | + debug!("recreating blur: renderer changed"); | ||
| 275 | + self.blur = None; | ||
| 276 | + } | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + let blur = if let Some(blur) = &mut self.blur { | ||
| 280 | + blur | ||
| 281 | + } else { | ||
| 282 | + let Some(blur) = Blur::new(renderer) else { | ||
| 283 | + // Missing blur shader. | ||
| 284 | + return Ok(()); | ||
| 285 | + }; | ||
| 286 | + self.blur.insert(blur) | ||
| 287 | + }; | ||
| 288 | + | ||
| 289 | + ensure!( | ||
| 290 | + offscreen.renderer_context_id == renderer.context_id(), | ||
| 291 | + "wrong renderer context id" | ||
| 292 | + ); | ||
| 293 | + | ||
| 294 | + blur.prepare_textures( | ||
| 295 | + |fourcc, size| renderer.create_buffer(fourcc, size), | ||
| 296 | + &offscreen.texture, | ||
| 297 | + self.blur_options, | ||
| 298 | + ) | ||
| 299 | + .context("error preparing blur textures")?; | ||
| 300 | + | ||
| 301 | + Ok(()) | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + pub fn render(&mut self, frame: &mut GlesFrame, blur: bool) -> anyhow::Result<GlesTexture> { | ||
| 228 | let offscreen = self.offscreen.as_mut().context("offscreen is missing")?; | 305 | let offscreen = self.offscreen.as_mut().context("offscreen is missing")?; |
| 229 | - Ok(offscreen.texture.clone()) | 306 | + |
| 307 | + if !blur { | ||
| 308 | + return Ok(offscreen.texture.clone()); | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + let texture = if let Some(texture) = &offscreen.blurred { | ||
| 312 | + texture.clone() | ||
| 313 | + } else { | ||
| 314 | + let blur = self.blur.as_mut().context("blur is missing")?; | ||
| 315 | + let blurred = blur | ||
| 316 | + .render(frame, &offscreen.texture, self.blur_options) | ||
| 317 | + .context("error rendering blur")?; | ||
| 318 | + offscreen.blurred.insert(blurred).clone() | ||
| 319 | + }; | ||
| 320 | + | ||
| 321 | + Ok(texture) | ||
| 230 | } | 322 | } |
| 231 | } | 323 | } |
src/render_helpers/mod.rsmodified+1 -0
| @@ -25,6 +25,7 @@ use crate::render_helpers::renderer::AsGlesRenderer; | |||
| 25 | use crate::render_helpers::xray::Xray; | 25 | use crate::render_helpers::xray::Xray; |
| 26 | 26 | ||
| 27 | pub mod background_effect; | 27 | pub mod background_effect; |
| 28 | +pub mod blur; | ||
| 28 | pub mod border; | 29 | pub mod border; |
| 29 | pub mod clipped_surface; | 30 | pub mod clipped_surface; |
| 30 | pub mod damage; | 31 | pub mod damage; |
src/render_helpers/shaders/blur.vertadded+11 -0
| @@ -0,0 +1,11 @@ | |||
| 1 | +#version 100 | ||
| 2 | + | ||
| 3 | +attribute vec2 vert; | ||
| 4 | +varying vec2 v_coords; | ||
| 5 | + | ||
| 6 | +void main() { | ||
| 7 | + v_coords = vert; | ||
| 8 | + // vert goes from 0 to 1; position must be from -1 to 1. | ||
| 9 | + vec2 position = vert * 2.0 - 1.0; | ||
| 10 | + gl_Position = vec4(position, 1.0, 1.0); | ||
| 11 | +} | ||
src/render_helpers/shaders/blur_down.fragadded+21 -0
| @@ -0,0 +1,21 @@ | |||
| 1 | +#version 100 | ||
| 2 | + | ||
| 3 | +precision highp float; | ||
| 4 | + | ||
| 5 | +varying vec2 v_coords; | ||
| 6 | + | ||
| 7 | +uniform sampler2D tex; | ||
| 8 | +uniform vec2 half_pixel; | ||
| 9 | +uniform float offset; | ||
| 10 | + | ||
| 11 | +void main() { | ||
| 12 | + vec2 o = half_pixel * offset; | ||
| 13 | + | ||
| 14 | + vec4 sum = texture2D(tex, v_coords) * 4.0; | ||
| 15 | + sum += texture2D(tex, v_coords + vec2(-o.x, -o.y)); | ||
| 16 | + sum += texture2D(tex, v_coords + vec2( o.x, -o.y)); | ||
| 17 | + sum += texture2D(tex, v_coords + vec2(-o.x, o.y)); | ||
| 18 | + sum += texture2D(tex, v_coords + vec2( o.x, o.y)); | ||
| 19 | + | ||
| 20 | + gl_FragColor = sum / 8.0; | ||
| 21 | +} | ||
src/render_helpers/shaders/blur_up.fragadded+29 -0
| @@ -0,0 +1,29 @@ | |||
| 1 | +#version 100 | ||
| 2 | + | ||
| 3 | +precision highp float; | ||
| 4 | + | ||
| 5 | +varying vec2 v_coords; | ||
| 6 | + | ||
| 7 | +uniform sampler2D tex; | ||
| 8 | +uniform vec2 half_pixel; | ||
| 9 | +uniform float offset; | ||
| 10 | + | ||
| 11 | +void main() { | ||
| 12 | + vec2 o = half_pixel * offset; | ||
| 13 | + | ||
| 14 | + vec4 sum = vec4(0.0); | ||
| 15 | + | ||
| 16 | + // Four edge centers | ||
| 17 | + sum += texture2D(tex, v_coords + vec2(-o.x * 2.0, 0.0)); | ||
| 18 | + sum += texture2D(tex, v_coords + vec2( o.x * 2.0, 0.0)); | ||
| 19 | + sum += texture2D(tex, v_coords + vec2(0.0, -o.y * 2.0)); | ||
| 20 | + sum += texture2D(tex, v_coords + vec2(0.0, o.y * 2.0)); | ||
| 21 | + | ||
| 22 | + // Four diagonal corners | ||
| 23 | + sum += texture2D(tex, v_coords + vec2(-o.x, o.y)) * 2.0; | ||
| 24 | + sum += texture2D(tex, v_coords + vec2( o.x, o.y)) * 2.0; | ||
| 25 | + sum += texture2D(tex, v_coords + vec2(-o.x, -o.y)) * 2.0; | ||
| 26 | + sum += texture2D(tex, v_coords + vec2( o.x, -o.y)) * 2.0; | ||
| 27 | + | ||
| 28 | + gl_FragColor = sum / 12.0; | ||
| 29 | +} | ||
src/render_helpers/shaders/mod.rsmodified+9 -0
| @@ -8,6 +8,7 @@ use smithay::backend::renderer::gles::{ | |||
| 8 | 8 | ||
| 9 | use super::renderer::NiriRenderer; | 9 | use super::renderer::NiriRenderer; |
| 10 | use super::shader_element::ShaderProgram; | 10 | use super::shader_element::ShaderProgram; |
| 11 | +use crate::render_helpers::blur::BlurProgram; | ||
| 11 | 12 | ||
| 12 | pub struct Shaders { | 13 | pub struct Shaders { |
| 13 | pub border: Option<ShaderProgram>, | 14 | pub border: Option<ShaderProgram>, |
| @@ -16,6 +17,7 @@ pub struct Shaders { | |||
| 16 | pub postprocess_and_clip: Option<GlesTexProgram>, | 17 | pub postprocess_and_clip: Option<GlesTexProgram>, |
| 17 | pub resize: Option<ShaderProgram>, | 18 | pub resize: Option<ShaderProgram>, |
| 18 | pub gradient_fade: Option<GlesTexProgram>, | 19 | pub gradient_fade: Option<GlesTexProgram>, |
| 20 | + pub blur: Option<BlurProgram>, | ||
| 19 | pub custom_resize: RefCell<Option<ShaderProgram>>, | 21 | pub custom_resize: RefCell<Option<ShaderProgram>>, |
| 20 | pub custom_close: RefCell<Option<ShaderProgram>>, | 22 | pub custom_close: RefCell<Option<ShaderProgram>>, |
| 21 | pub custom_open: RefCell<Option<ShaderProgram>>, | 23 | pub custom_open: RefCell<Option<ShaderProgram>>, |
| @@ -140,6 +142,12 @@ impl Shaders { | |||
| 140 | }) | 142 | }) |
| 141 | .ok(); | 143 | .ok(); |
| 142 | 144 | ||
| 145 | + let blur = BlurProgram::compile(renderer) | ||
| 146 | + .map_err(|err| { | ||
| 147 | + warn!("error compiling blur shaders: {err:?}"); | ||
| 148 | + }) | ||
| 149 | + .ok(); | ||
| 150 | + | ||
| 143 | Self { | 151 | Self { |
| 144 | border, | 152 | border, |
| 145 | shadow, | 153 | shadow, |
| @@ -147,6 +155,7 @@ impl Shaders { | |||
| 147 | postprocess_and_clip, | 155 | postprocess_and_clip, |
| 148 | resize, | 156 | resize, |
| 149 | gradient_fade, | 157 | gradient_fade, |
| 158 | + blur, | ||
| 150 | custom_resize: RefCell::new(None), | 159 | custom_resize: RefCell::new(None), |
| 151 | custom_close: RefCell::new(None), | 160 | custom_close: RefCell::new(None), |
| 152 | custom_open: RefCell::new(None), | 161 | custom_open: RefCell::new(None), |
src/render_helpers/xray.rsmodified+7 -3
| @@ -73,6 +73,7 @@ pub struct XrayElement { | |||
| 73 | clip_geo_size: Vec2, | 73 | clip_geo_size: Vec2, |
| 74 | corner_radius: CornerRadius, | 74 | corner_radius: CornerRadius, |
| 75 | scale: f32, | 75 | scale: f32, |
| 76 | + blur: bool, | ||
| 76 | noise: f32, | 77 | noise: f32, |
| 77 | saturation: f32, | 78 | saturation: f32, |
| 78 | bg_color: Color32F, | 79 | bg_color: Color32F, |
| @@ -95,6 +96,7 @@ impl Xray { | |||
| 95 | ctx: RenderCtx<GlesRenderer>, | 96 | ctx: RenderCtx<GlesRenderer>, |
| 96 | params: RenderParams, | 97 | params: RenderParams, |
| 97 | xray_pos: XrayPos, | 98 | xray_pos: XrayPos, |
| 99 | + blur: bool, | ||
| 98 | noise: f32, | 100 | noise: f32, |
| 99 | saturation: f32, | 101 | saturation: f32, |
| 100 | push: &mut dyn FnMut(XrayElement), | 102 | push: &mut dyn FnMut(XrayElement), |
| @@ -121,7 +123,7 @@ impl Xray { | |||
| 121 | 123 | ||
| 122 | let mut background = self.background[ctx.target as usize].borrow_mut(); | 124 | let mut background = self.background[ctx.target as usize].borrow_mut(); |
| 123 | let prev = background.commit(); | 125 | let prev = background.commit(); |
| 124 | - if background.prepare(ctx.renderer) { | 126 | + if background.prepare(ctx.renderer, blur) { |
| 125 | if background.commit() != prev { | 127 | if background.commit() != prev { |
| 126 | trace!("background damaged"); | 128 | trace!("background damaged"); |
| 127 | } | 129 | } |
| @@ -191,6 +193,7 @@ impl Xray { | |||
| 191 | clip_geo_size, | 193 | clip_geo_size, |
| 192 | corner_radius, | 194 | corner_radius, |
| 193 | scale: params.scale as f32, | 195 | scale: params.scale as f32, |
| 196 | + blur, | ||
| 194 | noise, | 197 | noise, |
| 195 | saturation, | 198 | saturation, |
| 196 | bg_color: *bg_color, | 199 | bg_color: *bg_color, |
| @@ -206,7 +209,7 @@ impl Xray { | |||
| 206 | } | 209 | } |
| 207 | 210 | ||
| 208 | let prev = backdrop.commit(); | 211 | let prev = backdrop.commit(); |
| 209 | - if backdrop.prepare(ctx.renderer) { | 212 | + if backdrop.prepare(ctx.renderer, blur) { |
| 210 | if backdrop.commit() != prev { | 213 | if backdrop.commit() != prev { |
| 211 | trace!("backdrop damaged"); | 214 | trace!("backdrop damaged"); |
| 212 | } | 215 | } |
| @@ -239,6 +242,7 @@ impl Xray { | |||
| 239 | clip_geo_size, | 242 | clip_geo_size, |
| 240 | corner_radius: corner_radius.scaled_by(zoom as f32), | 243 | corner_radius: corner_radius.scaled_by(zoom as f32), |
| 241 | scale: params.scale as f32, | 244 | scale: params.scale as f32, |
| 245 | + blur, | ||
| 242 | noise, | 246 | noise, |
| 243 | saturation, | 247 | saturation, |
| 244 | bg_color: self.backdrop_color, | 248 | bg_color: self.backdrop_color, |
| @@ -297,7 +301,7 @@ impl RenderElement<GlesRenderer> for XrayElement { | |||
| 297 | _opaque_regions: &[Rectangle<i32, Physical>], | 301 | _opaque_regions: &[Rectangle<i32, Physical>], |
| 298 | ) -> Result<(), GlesError> { | 302 | ) -> Result<(), GlesError> { |
| 299 | let mut buffer = self.buffer.borrow_mut(); | 303 | let mut buffer = self.buffer.borrow_mut(); |
| 300 | - let texture = match buffer.render() { | 304 | + let texture = match buffer.render(frame, self.blur) { |
| 301 | Ok(x) => x, | 305 | Ok(x) => x, |
| 302 | Err(err) => { | 306 | Err(err) => { |
| 303 | warn!("error rendering effect buffer: {err:?}"); | 307 | warn!("error rendering effect buffer: {err:?}"); |
src/window/mapped.rsmodified+10 -1
| @@ -106,6 +106,9 @@ pub struct Mapped { | |||
| 106 | /// Buffer to draw instead of the window when it should be blocked out. | 106 | /// Buffer to draw instead of the window when it should be blocked out. |
| 107 | block_out_buffer: RefCell<SolidColorBuffer>, | 107 | block_out_buffer: RefCell<SolidColorBuffer>, |
| 108 | 108 | ||
| 109 | + /// The blur config, passed for background effect rendering. | ||
| 110 | + blur_config: niri_config::Blur, | ||
| 111 | + | ||
| 109 | /// Whether the next configure should be animated, if the configured state changed. | 112 | /// Whether the next configure should be animated, if the configured state changed. |
| 110 | animate_next_configure: bool, | 113 | animate_next_configure: bool, |
| 111 | 114 | ||
| @@ -251,7 +254,7 @@ enum RequestSizeOnce { | |||
| 251 | } | 254 | } |
| 252 | 255 | ||
| 253 | impl Mapped { | 256 | impl Mapped { |
| 254 | - pub fn new(window: Window, rules: ResolvedWindowRules, hook: HookId) -> Self { | 257 | + pub fn new(window: Window, rules: ResolvedWindowRules, hook: HookId, config: &Config) -> Self { |
| 255 | let surface = window.wl_surface().expect("no X11 support"); | 258 | let surface = window.wl_surface().expect("no X11 support"); |
| 256 | let credentials = get_credentials_for_surface(&surface); | 259 | let credentials = get_credentials_for_surface(&surface); |
| 257 | let mut rv = Self { | 260 | let mut rv = Self { |
| @@ -271,6 +274,7 @@ impl Mapped { | |||
| 271 | is_window_cast_target: false, | 274 | is_window_cast_target: false, |
| 272 | ignore_opacity_window_rule: false, | 275 | ignore_opacity_window_rule: false, |
| 273 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.])), | 276 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.])), |
| 277 | + blur_config: config.blur, | ||
| 274 | animate_next_configure: false, | 278 | animate_next_configure: false, |
| 275 | animate_serials: Vec::new(), | 279 | animate_serials: Vec::new(), |
| 276 | animation_snapshot: None, | 280 | animation_snapshot: None, |
| @@ -604,6 +608,10 @@ impl LayoutElement for Mapped { | |||
| 604 | &self.window | 608 | &self.window |
| 605 | } | 609 | } |
| 606 | 610 | ||
| 611 | + fn update_config(&mut self, blur_config: niri_config::Blur) { | ||
| 612 | + self.blur_config = blur_config; | ||
| 613 | + } | ||
| 614 | + | ||
| 607 | fn size(&self) -> Size<i32, Logical> { | 615 | fn size(&self) -> Size<i32, Logical> { |
| 608 | self.window.geometry().size | 616 | self.window.geometry().size |
| 609 | } | 617 | } |
| @@ -691,6 +699,7 @@ impl LayoutElement for Mapped { | |||
| 691 | scale, | 699 | scale, |
| 692 | clip_to_geometry, | 700 | clip_to_geometry, |
| 693 | self.toplevel().wl_surface(), | 701 | self.toplevel().wl_surface(), |
| 702 | + self.blur_config, | ||
| 694 | radius, | 703 | radius, |
| 695 | self.rules.background_effect, | 704 | self.rules.background_effect, |
| 696 | xray_pos, | 705 | xray_pos, |