Implement non-xray background effects
Browse files at this commit66d66d603020163c7c97bb38c32f712533f9f802Parent: 19866f87 changed files +528 -19
- docs/wiki/Window-Effects.md +11 -0
- src/layer/mapped.rs +2 -0
- src/niri.rs +40 -18
- src/render_helpers/background_effect.rs +16 -1
- src/render_helpers/framebuffer_effect.rs +457 -0
- src/render_helpers/mod.rs +1 -0
- src/window/mapped.rs +1 -0
docs/wiki/Window-Effects.mdmodified+11 -0
| @@ -51,3 +51,14 @@ Xray is automatically enabled by default if any other background effect (like bl | |||
| 51 | This is because it's much more efficient: with xray active, niri only needs to blur the background once, and then can reuse this blurred version with no extra work (since the wallpaper changes very rarely). | 51 | This is because it's much more efficient: with xray active, niri only needs to blur the background once, and then can reuse this blurred version with no extra work (since the wallpaper changes very rarely). |
| 52 | 52 | ||
| 53 | If you have an animated wallpaper, xray will still have to recompute blur every frame, but that happens once and shared among all windows, rather than recomputed separately for each window. | 53 | If you have an animated wallpaper, xray will still have to recompute blur every frame, but that happens once and shared among all windows, rather than recomputed separately for each window. |
| 54 | + | ||
| 55 | +#### Non-xray effects (experimental) | ||
| 56 | + | ||
| 57 | +You can disable xray with `xray false` background effect window rule. | ||
| 58 | +This gives you the normal kind of blur where everything below a window is blurred. | ||
| 59 | +Keep in mind that non-xray blur and other non-xray effects are more expensive as niri has to recompute them any time you move the window, or the contents underneath change. | ||
| 60 | + | ||
| 61 | +Non-xray effects are currently experimental because they have some known limitations. | ||
| 62 | + | ||
| 63 | +- They disappear during window open/close animations and while dragging a tiled window. | ||
| 64 | +Fixing this requries a refactor to the niri rendering code to defer offscreen rendering, and possibly other refactors. | ||
src/layer/mapped.rsmodified+2 -0
| @@ -187,6 +187,7 @@ impl MappedLayer { | |||
| 187 | pub fn render_normal<R: NiriRenderer>( | 187 | pub fn render_normal<R: NiriRenderer>( |
| 188 | &self, | 188 | &self, |
| 189 | mut ctx: RenderCtx<R>, | 189 | mut ctx: RenderCtx<R>, |
| 190 | + ns: Option<usize>, | ||
| 190 | location: Point<f64, Logical>, | 191 | location: Point<f64, Logical>, |
| 191 | xray_pos: XrayPos, | 192 | xray_pos: XrayPos, |
| 192 | push: &mut dyn FnMut(LayerSurfaceRenderElement<R>), | 193 | push: &mut dyn FnMut(LayerSurfaceRenderElement<R>), |
| @@ -238,6 +239,7 @@ impl MappedLayer { | |||
| 238 | let radius = self.rules.geometry_corner_radius.unwrap_or_default(); | 239 | let radius = self.rules.geometry_corner_radius.unwrap_or_default(); |
| 239 | background_effect::render_for_tile( | 240 | background_effect::render_for_tile( |
| 240 | ctx.as_gles(), | 241 | ctx.as_gles(), |
| 242 | + ns, | ||
| 241 | geometry, | 243 | geometry, |
| 242 | self.scale, | 244 | self.scale, |
| 243 | false, | 245 | false, |
src/niri.rsmodified+40 -18
| @@ -4271,33 +4271,41 @@ impl Niri { | |||
| 4271 | // We use macros instead of closures to avoid borrowing issues (renderer and push() go | 4271 | // We use macros instead of closures to avoid borrowing issues (renderer and push() go |
| 4272 | // into different functions). | 4272 | // into different functions). |
| 4273 | macro_rules! push_popups_from_layer { | 4273 | macro_rules! push_popups_from_layer { |
| 4274 | - ($layer:expr, $backdrop:expr, $push:expr) => {{ | 4274 | + ($layer:expr, $ns:expr, $backdrop:expr, $push:expr) => {{ |
| 4275 | - self.render_layer_popups(ctx.r(), &layer_map, $layer, $backdrop, $push); | 4275 | + self.render_layer_popups(ctx.r(), $ns, &layer_map, $layer, $backdrop, $push); |
| 4276 | }}; | 4276 | }}; |
| 4277 | ($layer:expr, true) => {{ | 4277 | ($layer:expr, true) => {{ |
| 4278 | - push_popups_from_layer!($layer, true, &mut |elem| push(elem.into())); | 4278 | + push_popups_from_layer!($layer, None, true, &mut |elem| push(elem.into())); |
| 4279 | }}; | 4279 | }}; |
| 4280 | - ($layer:expr, $push:expr) => {{ | 4280 | + ($layer:expr, $ns:expr, $push:expr) => {{ |
| 4281 | - push_popups_from_layer!($layer, false, $push); | 4281 | + push_popups_from_layer!($layer, $ns, false, $push); |
| 4282 | }}; | 4282 | }}; |
| 4283 | ($layer:expr) => {{ | 4283 | ($layer:expr) => {{ |
| 4284 | - push_popups_from_layer!($layer, false, &mut |elem| push(elem.into())); | 4284 | + push_popups_from_layer!($layer, None, false, &mut |elem| push(elem.into())); |
| 4285 | }}; | 4285 | }}; |
| 4286 | } | 4286 | } |
| 4287 | macro_rules! push_normal_from_layer { | 4287 | macro_rules! push_normal_from_layer { |
| 4288 | - ($layer:expr, $xray_pos:expr, $backdrop:expr, $push:expr) => {{ | 4288 | + ($layer:expr, $ns:expr, $xray_pos:expr, $backdrop:expr, $push:expr) => {{ |
| 4289 | - self.render_layer_normal(ctx.r(), &layer_map, $layer, $xray_pos, $backdrop, $push); | 4289 | + self.render_layer_normal( |
| 4290 | + ctx.r(), | ||
| 4291 | + $ns, | ||
| 4292 | + &layer_map, | ||
| 4293 | + $layer, | ||
| 4294 | + $xray_pos, | ||
| 4295 | + $backdrop, | ||
| 4296 | + $push, | ||
| 4297 | + ); | ||
| 4290 | }}; | 4298 | }}; |
| 4291 | ($layer:expr, true) => {{ | 4299 | ($layer:expr, true) => {{ |
| 4292 | - push_normal_from_layer!($layer, XrayPos::default(), true, &mut |elem| { | 4300 | + push_normal_from_layer!($layer, None, XrayPos::default(), true, &mut |elem| { |
| 4293 | push(elem.into()) | 4301 | push(elem.into()) |
| 4294 | }); | 4302 | }); |
| 4295 | }}; | 4303 | }}; |
| 4296 | - ($layer:expr, $xray_pos:expr, $push:expr) => {{ | 4304 | + ($layer:expr, $ns:expr, $xray_pos:expr, $push:expr) => {{ |
| 4297 | - push_normal_from_layer!($layer, $xray_pos, false, $push); | 4305 | + push_normal_from_layer!($layer, $ns, $xray_pos, false, $push); |
| 4298 | }}; | 4306 | }}; |
| 4299 | ($layer:expr) => {{ | 4307 | ($layer:expr) => {{ |
| 4300 | - push_normal_from_layer!($layer, XrayPos::default(), false, &mut |elem| { | 4308 | + push_normal_from_layer!($layer, None, XrayPos::default(), false, &mut |elem| { |
| 4301 | push(elem.into()) | 4309 | push(elem.into()) |
| 4302 | }); | 4310 | }); |
| 4303 | }}; | 4311 | }}; |
| @@ -4349,17 +4357,27 @@ impl Niri { | |||
| 4349 | }}; | 4357 | }}; |
| 4350 | } | 4358 | } |
| 4351 | 4359 | ||
| 4352 | - for (_ws, geo) in mon.workspaces_with_render_geo() { | 4360 | + for (ws, geo) in mon.workspaces_with_render_geo() { |
| 4353 | - push_popups_from_layer!(Layer::Bottom, process!(geo)); | 4361 | + let ns = Some(ws.id().get() as usize); |
| 4354 | - push_popups_from_layer!(Layer::Background, process!(geo)); | 4362 | + push_popups_from_layer!(Layer::Bottom, ns, process!(geo)); |
| 4363 | + push_popups_from_layer!(Layer::Background, ns, process!(geo)); | ||
| 4355 | } | 4364 | } |
| 4356 | 4365 | ||
| 4357 | mon.render_workspaces(ctx.r(), focus_ring, &mut |elem| push(elem.into())); | 4366 | mon.render_workspaces(ctx.r(), focus_ring, &mut |elem| push(elem.into())); |
| 4358 | 4367 | ||
| 4359 | for (ws, geo) in mon.workspaces_with_render_geo() { | 4368 | for (ws, geo) in mon.workspaces_with_render_geo() { |
| 4369 | + // The render element namespace. This will be set to the workspace index for | ||
| 4370 | + // elements duplicated across workspaces (i.e. background and bottom layers) in | ||
| 4371 | + // order to have their non-xray framebuffer effects separated from each other. | ||
| 4372 | + // | ||
| 4373 | + // This doesn't have to correspond exactly to workspace id or idx, the only | ||
| 4374 | + // requirement is that there's only one framebuffer effect element with a given id + | ||
| 4375 | + // namespace on the frame at once. Id + namespace is used as the cache key in the | ||
| 4376 | + // damage tracker. | ||
| 4377 | + let ns = Some(ws.id().get() as usize); | ||
| 4360 | let xray_pos = XrayPos::new(geo.loc, zoom); | 4378 | let xray_pos = XrayPos::new(geo.loc, zoom); |
| 4361 | - push_normal_from_layer!(Layer::Bottom, xray_pos, process!(geo)); | 4379 | + push_normal_from_layer!(Layer::Bottom, ns, xray_pos, process!(geo)); |
| 4362 | - push_normal_from_layer!(Layer::Background, xray_pos, process!(geo)); | 4380 | + push_normal_from_layer!(Layer::Background, ns, xray_pos, process!(geo)); |
| 4363 | 4381 | ||
| 4364 | process!(geo)(ws.render_background()); | 4382 | process!(geo)(ws.render_background()); |
| 4365 | } | 4383 | } |
| @@ -4402,6 +4420,7 @@ impl Niri { | |||
| 4402 | elements.clear(); | 4420 | elements.clear(); |
| 4403 | self.render_layer_normal( | 4421 | self.render_layer_normal( |
| 4404 | ctx.r(), | 4422 | ctx.r(), |
| 4423 | + None, | ||
| 4405 | &layer_map, | 4424 | &layer_map, |
| 4406 | Layer::Background, | 4425 | Layer::Background, |
| 4407 | XrayPos::default(), | 4426 | XrayPos::default(), |
| @@ -4418,6 +4437,7 @@ impl Niri { | |||
| 4418 | elements.clear(); | 4437 | elements.clear(); |
| 4419 | self.render_layer_normal( | 4438 | self.render_layer_normal( |
| 4420 | ctx.r(), | 4439 | ctx.r(), |
| 4440 | + None, | ||
| 4421 | &layer_map, | 4441 | &layer_map, |
| 4422 | Layer::Background, | 4442 | Layer::Background, |
| 4423 | XrayPos::default(), | 4443 | XrayPos::default(), |
| @@ -4481,6 +4501,7 @@ impl Niri { | |||
| 4481 | fn render_layer_normal<R: NiriRenderer>( | 4501 | fn render_layer_normal<R: NiriRenderer>( |
| 4482 | &self, | 4502 | &self, |
| 4483 | mut ctx: RenderCtx<R>, | 4503 | mut ctx: RenderCtx<R>, |
| 4504 | + ns: Option<usize>, | ||
| 4484 | layer_map: &LayerMap, | 4505 | layer_map: &LayerMap, |
| 4485 | layer: Layer, | 4506 | layer: Layer, |
| 4486 | xray_pos: XrayPos, | 4507 | xray_pos: XrayPos, |
| @@ -4490,13 +4511,14 @@ impl Niri { | |||
| 4490 | for (mapped, geo) in self.layers_in_render_order(layer_map, layer, for_backdrop) { | 4511 | for (mapped, geo) in self.layers_in_render_order(layer_map, layer, for_backdrop) { |
| 4491 | let loc = geo.loc.to_f64(); | 4512 | let loc = geo.loc.to_f64(); |
| 4492 | let xray_pos = xray_pos.offset(loc); | 4513 | let xray_pos = xray_pos.offset(loc); |
| 4493 | - mapped.render_normal(ctx.r(), loc, xray_pos, push); | 4514 | + mapped.render_normal(ctx.r(), ns, loc, xray_pos, push); |
| 4494 | } | 4515 | } |
| 4495 | } | 4516 | } |
| 4496 | 4517 | ||
| 4497 | fn render_layer_popups<R: NiriRenderer>( | 4518 | fn render_layer_popups<R: NiriRenderer>( |
| 4498 | &self, | 4519 | &self, |
| 4499 | mut ctx: RenderCtx<R>, | 4520 | mut ctx: RenderCtx<R>, |
| 4521 | + _ns: Option<usize>, | ||
| 4500 | layer_map: &LayerMap, | 4522 | layer_map: &LayerMap, |
| 4501 | layer: Layer, | 4523 | layer: Layer, |
| 4502 | for_backdrop: bool, | 4524 | for_backdrop: bool, |
src/render_helpers/background_effect.rsmodified+16 -1
| @@ -8,7 +8,9 @@ use wayland_server::protocol::wl_surface::WlSurface; | |||
| 8 | 8 | ||
| 9 | use crate::handlers::background_effect::get_cached_blur_region; | 9 | use crate::handlers::background_effect::get_cached_blur_region; |
| 10 | use crate::niri_render_elements; | 10 | use crate::niri_render_elements; |
| 11 | +use crate::render_helpers::blur::BlurOptions; | ||
| 11 | use crate::render_helpers::damage::ExtraDamage; | 12 | use crate::render_helpers::damage::ExtraDamage; |
| 13 | +use crate::render_helpers::framebuffer_effect::{FramebufferEffect, FramebufferEffectElement}; | ||
| 12 | use crate::render_helpers::xray::{XrayElement, XrayPos}; | 14 | use crate::render_helpers::xray::{XrayElement, XrayPos}; |
| 13 | use crate::render_helpers::RenderCtx; | 15 | use crate::render_helpers::RenderCtx; |
| 14 | use crate::utils::region::TransformedRegion; | 16 | use crate::utils::region::TransformedRegion; |
| @@ -16,6 +18,7 @@ use crate::utils::surface_geo; | |||
| 16 | 18 | ||
| 17 | #[derive(Debug)] | 19 | #[derive(Debug)] |
| 18 | pub struct BackgroundEffect { | 20 | pub struct BackgroundEffect { |
| 21 | + nonxray: FramebufferEffect, | ||
| 19 | /// Damage when options change. | 22 | /// Damage when options change. |
| 20 | damage: ExtraDamage, | 23 | damage: ExtraDamage, |
| 21 | /// Corner radius for clipping. | 24 | /// Corner radius for clipping. |
| @@ -72,6 +75,7 @@ impl RenderParams { | |||
| 72 | 75 | ||
| 73 | niri_render_elements! { | 76 | niri_render_elements! { |
| 74 | BackgroundEffectElement => { | 77 | BackgroundEffectElement => { |
| 78 | + FramebufferEffect = FramebufferEffectElement, | ||
| 75 | Xray = XrayElement, | 79 | Xray = XrayElement, |
| 76 | ExtraDamage = ExtraDamage, | 80 | ExtraDamage = ExtraDamage, |
| 77 | } | 81 | } |
| @@ -80,6 +84,7 @@ niri_render_elements! { | |||
| 80 | impl BackgroundEffect { | 84 | impl BackgroundEffect { |
| 81 | pub fn new() -> Self { | 85 | pub fn new() -> Self { |
| 82 | Self { | 86 | Self { |
| 87 | + nonxray: FramebufferEffect::new(), | ||
| 83 | damage: ExtraDamage::new(), | 88 | damage: ExtraDamage::new(), |
| 84 | corner_radius: CornerRadius::default(), | 89 | corner_radius: CornerRadius::default(), |
| 85 | blur_config: niri_config::Blur::default(), | 90 | blur_config: niri_config::Blur::default(), |
| @@ -90,6 +95,7 @@ impl BackgroundEffect { | |||
| 90 | /// Damage the background effect, for example when a blur subregion changes. | 95 | /// Damage the background effect, for example when a blur subregion changes. |
| 91 | pub fn damage(&mut self) { | 96 | pub fn damage(&mut self) { |
| 92 | self.damage.damage_all(); | 97 | self.damage.damage_all(); |
| 98 | + self.nonxray.damage(); | ||
| 93 | } | 99 | } |
| 94 | 100 | ||
| 95 | pub fn update_config(&mut self, config: niri_config::Blur) { | 101 | pub fn update_config(&mut self, config: niri_config::Blur) { |
| @@ -99,6 +105,7 @@ impl BackgroundEffect { | |||
| 99 | 105 | ||
| 100 | self.blur_config = config; | 106 | self.blur_config = config; |
| 101 | self.damage.damage_all(); | 107 | self.damage.damage_all(); |
| 108 | + self.nonxray.damage(); | ||
| 102 | } | 109 | } |
| 103 | 110 | ||
| 104 | pub fn update_render_elements( | 111 | pub fn update_render_elements( |
| @@ -134,6 +141,7 @@ impl BackgroundEffect { | |||
| 134 | self.options = options; | 141 | self.options = options; |
| 135 | self.corner_radius = corner_radius; | 142 | self.corner_radius = corner_radius; |
| 136 | self.damage.damage_all(); | 143 | self.damage.damage_all(); |
| 144 | + self.nonxray.damage(); | ||
| 137 | } | 145 | } |
| 138 | 146 | ||
| 139 | pub fn is_visible(&self) -> bool { | 147 | pub fn is_visible(&self) -> bool { |
| @@ -143,6 +151,7 @@ impl BackgroundEffect { | |||
| 143 | pub fn render( | 151 | pub fn render( |
| 144 | &self, | 152 | &self, |
| 145 | ctx: RenderCtx<GlesRenderer>, | 153 | ctx: RenderCtx<GlesRenderer>, |
| 154 | + ns: Option<usize>, | ||
| 146 | mut params: RenderParams, | 155 | mut params: RenderParams, |
| 147 | xray_pos: XrayPos, | 156 | xray_pos: XrayPos, |
| 148 | push: &mut dyn FnMut(BackgroundEffectElement), | 157 | push: &mut dyn FnMut(BackgroundEffectElement), |
| @@ -161,6 +170,7 @@ impl BackgroundEffect { | |||
| 161 | // Use noise/saturation from options, falling back to blur defaults if blurred, and | 170 | // Use noise/saturation from options, falling back to blur defaults if blurred, and |
| 162 | // to no effect if not blurred. | 171 | // to no effect if not blurred. |
| 163 | let blur = self.options.blur && !self.blur_config.off; | 172 | let blur = self.options.blur && !self.blur_config.off; |
| 173 | + let blur_options = blur.then_some(BlurOptions::from(self.blur_config)); | ||
| 164 | let noise = if blur { self.blur_config.noise } else { 0. }; | 174 | let noise = if blur { self.blur_config.noise } else { 0. }; |
| 165 | let noise = self.options.noise.unwrap_or(noise) as f32; | 175 | let noise = self.options.noise.unwrap_or(noise) as f32; |
| 166 | let saturation = if blur { | 176 | let saturation = if blur { |
| @@ -187,6 +197,10 @@ impl BackgroundEffect { | |||
| 187 | ); | 197 | ); |
| 188 | } else { | 198 | } else { |
| 189 | // Render non-xray effect. | 199 | // Render non-xray effect. |
| 200 | + let elem = self | ||
| 201 | + .nonxray | ||
| 202 | + .render(ns, params, blur_options, noise, saturation); | ||
| 203 | + push(elem.into()); | ||
| 190 | } | 204 | } |
| 191 | } | 205 | } |
| 192 | } | 206 | } |
| @@ -269,6 +283,7 @@ pub fn damage_surface(states: &SurfaceData) { | |||
| 269 | #[allow(clippy::too_many_arguments)] | 283 | #[allow(clippy::too_many_arguments)] |
| 270 | pub fn render_for_tile( | 284 | pub fn render_for_tile( |
| 271 | ctx: RenderCtx<GlesRenderer>, | 285 | ctx: RenderCtx<GlesRenderer>, |
| 286 | + ns: Option<usize>, | ||
| 272 | geometry: Rectangle<f64, Logical>, | 287 | geometry: Rectangle<f64, Logical>, |
| 273 | scale: f64, | 288 | scale: f64, |
| 274 | clip_to_geometry: bool, | 289 | clip_to_geometry: bool, |
| @@ -312,6 +327,6 @@ pub fn render_for_tile( | |||
| 312 | }; | 327 | }; |
| 313 | 328 | ||
| 314 | let xray_pos = xray_pos.offset(params.geometry.loc - geometry.loc); | 329 | let xray_pos = xray_pos.offset(params.geometry.loc - geometry.loc); |
| 315 | - background_effect.render(ctx, params, xray_pos, push); | 330 | + background_effect.render(ctx, ns, params, xray_pos, push); |
| 316 | }); | 331 | }); |
| 317 | } | 332 | } |
src/render_helpers/framebuffer_effect.rsadded+457 -0
| @@ -0,0 +1,457 @@ | |||
| 1 | +use std::cell::RefCell; | ||
| 2 | + | ||
| 3 | +use glam::{Mat3, Vec2}; | ||
| 4 | +use niri_config::CornerRadius; | ||
| 5 | +use smithay::backend::allocator::Fourcc; | ||
| 6 | +use smithay::backend::renderer::element::{Element, Id, RenderElement}; | ||
| 7 | +use smithay::backend::renderer::gles::{ | ||
| 8 | + ffi, GlesError, GlesFrame, GlesRenderer, GlesTexture, Uniform, | ||
| 9 | +}; | ||
| 10 | +use smithay::backend::renderer::utils::CommitCounter; | ||
| 11 | +use smithay::backend::renderer::{Frame as _, FrameContext, Offscreen, Texture as _}; | ||
| 12 | +use smithay::gpu_span_location; | ||
| 13 | +use smithay::utils::user_data::UserDataMap; | ||
| 14 | +use smithay::utils::{Buffer, Logical, Physical, Rectangle, Scale, Transform}; | ||
| 15 | + | ||
| 16 | +use crate::backend::tty::{TtyFrame, TtyRenderer, TtyRendererError}; | ||
| 17 | +use crate::render_helpers::background_effect::RenderParams; | ||
| 18 | +use crate::render_helpers::blur::{Blur, BlurOptions}; | ||
| 19 | +use crate::render_helpers::renderer::AsGlesFrame as _; | ||
| 20 | +use crate::render_helpers::shaders::{mat3_uniform, Shaders}; | ||
| 21 | +use crate::utils::region::TransformedRegion; | ||
| 22 | + | ||
| 23 | +#[derive(Debug)] | ||
| 24 | +pub struct FramebufferEffect { | ||
| 25 | + id: Id, | ||
| 26 | + commit: CommitCounter, | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +#[derive(Debug)] | ||
| 30 | +pub struct FramebufferEffectElement { | ||
| 31 | + id: Id, | ||
| 32 | + commit: CommitCounter, | ||
| 33 | + geometry: Rectangle<f64, Logical>, | ||
| 34 | + clip_geo: Rectangle<f64, Logical>, | ||
| 35 | + corner_radius: CornerRadius, | ||
| 36 | + subregion: Option<TransformedRegion>, | ||
| 37 | + scale: f32, | ||
| 38 | + blur_options: Option<BlurOptions>, | ||
| 39 | + noise: f32, | ||
| 40 | + saturation: f32, | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +#[derive(Debug)] | ||
| 44 | +struct Inner { | ||
| 45 | + framebuffer: Option<GlesTexture>, | ||
| 46 | + blur: Option<Blur>, | ||
| 47 | + intermediate: Option<GlesTexture>, | ||
| 48 | + /// Reusable storage for subregion-filtered damage rects. | ||
| 49 | + subregion_damage: Vec<Rectangle<i32, Physical>>, | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +impl FramebufferEffect { | ||
| 53 | + pub fn new() -> Self { | ||
| 54 | + Self { | ||
| 55 | + id: Id::new(), | ||
| 56 | + commit: CommitCounter::default(), | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + pub fn damage(&mut self) { | ||
| 61 | + self.commit.increment(); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + pub fn render( | ||
| 65 | + &self, | ||
| 66 | + ns: Option<usize>, | ||
| 67 | + params: RenderParams, | ||
| 68 | + blur_options: Option<BlurOptions>, | ||
| 69 | + noise: f32, | ||
| 70 | + saturation: f32, | ||
| 71 | + ) -> FramebufferEffectElement { | ||
| 72 | + let (clip_geo, corner_radius) = params | ||
| 73 | + .clip | ||
| 74 | + .unwrap_or((params.geometry, CornerRadius::default())); | ||
| 75 | + | ||
| 76 | + let mut id = self.id.clone(); | ||
| 77 | + if let Some(ns) = ns { | ||
| 78 | + id = id.namespaced(ns); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + FramebufferEffectElement { | ||
| 82 | + id, | ||
| 83 | + commit: self.commit, | ||
| 84 | + geometry: params.geometry, | ||
| 85 | + clip_geo, | ||
| 86 | + corner_radius, | ||
| 87 | + subregion: params.subregion, | ||
| 88 | + scale: params.scale as f32, | ||
| 89 | + blur_options, | ||
| 90 | + noise, | ||
| 91 | + saturation, | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +impl FramebufferEffectElement { | ||
| 97 | + fn compute_uniforms( | ||
| 98 | + &self, | ||
| 99 | + crop: Rectangle<f64, Logical>, | ||
| 100 | + transform: Transform, | ||
| 101 | + ) -> [Uniform<'static>; 7] { | ||
| 102 | + let offset = crop.loc - (self.clip_geo.loc - self.geometry.loc); | ||
| 103 | + let offset = Vec2::new(offset.x as f32, offset.y as f32); | ||
| 104 | + let crop_size = Vec2::new(crop.size.w as f32, crop.size.h as f32); | ||
| 105 | + let clip_size = Vec2::new(self.clip_geo.size.w as f32, self.clip_geo.size.h as f32); | ||
| 106 | + | ||
| 107 | + // Our v_coords are [0, 1] inside crop. We want them to be [0, 1] inside clip_geo. | ||
| 108 | + let input_to_clip_geo = | ||
| 109 | + Mat3::from_scale(crop_size / clip_size) * Mat3::from_translation(offset / crop_size); | ||
| 110 | + | ||
| 111 | + // Revert the effect of the texture transform. | ||
| 112 | + let transform_mat = Mat3::from_translation(Vec2::new(0.5, 0.5)) | ||
| 113 | + * Mat3::from_cols_array(transform.matrix().as_ref()) | ||
| 114 | + * Mat3::from_translation(Vec2::new(-0.5, -0.5)); | ||
| 115 | + let input_to_clip_geo = input_to_clip_geo * transform_mat; | ||
| 116 | + | ||
| 117 | + let clip_geo_size = (self.clip_geo.size.w as f32, self.clip_geo.size.h as f32); | ||
| 118 | + | ||
| 119 | + [ | ||
| 120 | + Uniform::new("niri_scale", self.scale), | ||
| 121 | + Uniform::new("geo_size", clip_geo_size), | ||
| 122 | + Uniform::new("corner_radius", <[f32; 4]>::from(self.corner_radius)), | ||
| 123 | + mat3_uniform("input_to_geo", input_to_clip_geo), | ||
| 124 | + Uniform::new("noise", self.noise), | ||
| 125 | + Uniform::new("saturation", self.saturation), | ||
| 126 | + Uniform::new("bg_color", [0f32, 0., 0., 0.]), | ||
| 127 | + ] | ||
| 128 | + } | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +impl Element for FramebufferEffectElement { | ||
| 132 | + fn id(&self) -> &Id { | ||
| 133 | + &self.id | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + fn current_commit(&self) -> CommitCounter { | ||
| 137 | + self.commit | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + fn src(&self) -> Rectangle<f64, Buffer> { | ||
| 141 | + // We don't use src for drawing but we can use it to figure out how we were cropped. | ||
| 142 | + let size = self.geometry.size.to_buffer(1., Transform::Normal); | ||
| 143 | + Rectangle::from_size(size) | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> { | ||
| 147 | + self.geometry.to_physical_precise_round(scale) | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + fn is_framebuffer_effect(&self) -> bool { | ||
| 151 | + true | ||
| 152 | + } | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +impl RenderElement<GlesRenderer> for FramebufferEffectElement { | ||
| 156 | + fn capture_framebuffer( | ||
| 157 | + &self, | ||
| 158 | + frame: &mut GlesFrame<'_, '_>, | ||
| 159 | + src: Rectangle<f64, Buffer>, | ||
| 160 | + dst: Rectangle<i32, Physical>, | ||
| 161 | + cache: &UserDataMap, | ||
| 162 | + ) -> Result<(), GlesError> { | ||
| 163 | + let _span = tracy_client::span!("FramebufferEffectElement::capture_framebuffer"); | ||
| 164 | + let location = gpu_span_location!("FramebufferEffectElement::capture_framebuffer"); | ||
| 165 | + frame.with_gpu_span(location, |frame| { | ||
| 166 | + let output_rect = Rectangle::from_size(frame.output_size()); | ||
| 167 | + let transform = frame.transformation(); | ||
| 168 | + | ||
| 169 | + let mut guard = frame.renderer(); | ||
| 170 | + | ||
| 171 | + let inner = cache | ||
| 172 | + .get_or_insert::<RefCell<Inner>, _>(|| RefCell::new(Inner::new(guard.as_mut()))); | ||
| 173 | + let mut inner = inner.borrow_mut(); | ||
| 174 | + let inner = &mut *inner; | ||
| 175 | + | ||
| 176 | + inner.intermediate = None; | ||
| 177 | + | ||
| 178 | + // We want clamp-to-edge behavior for out-of-bounds pixels. However, glBlitFramebuffer | ||
| 179 | + // seems to skip out-of-bounds pixels, even though my reading of the docs suggests | ||
| 180 | + // otherwise (we use GL_LINEAR filter). So, clamp dst to the framebuffer bounds | ||
| 181 | + // ourselves. | ||
| 182 | + let clamped_dst = match dst.intersection(output_rect) { | ||
| 183 | + Some(clamped) => clamped, | ||
| 184 | + None => return Ok(()), | ||
| 185 | + }; | ||
| 186 | + let clamp_scale = clamped_dst.size.to_f64() / dst.size.to_f64(); | ||
| 187 | + | ||
| 188 | + let dst = transform.transform_rect_in(clamped_dst, &output_rect.size); | ||
| 189 | + | ||
| 190 | + // Compute size from our geometry and scale. | ||
| 191 | + // | ||
| 192 | + // The "correct" size is always dst.size since that's the pixel region we're actually | ||
| 193 | + // blitting. However, using dst.size causes two undesirable things when zooming out for | ||
| 194 | + // the overview: | ||
| 195 | + // 1. dst.size shrinks every frame, causing a texture realloaction for every fb effect | ||
| 196 | + // element every frame. | ||
| 197 | + // 2. The underlying blur visually expands. This is technically correct, since the | ||
| 198 | + // underlying contents shrink, but it's not what you visually expect: you expect the | ||
| 199 | + // blur to also shrink as the windows zoom out, to give the zooming out effect. | ||
| 200 | + // | ||
| 201 | + // Using size computed from geometry and scale solves both of those problems (even | ||
| 202 | + // though there's a bit of a cost in that zoomed-out elements still blur the entire | ||
| 203 | + // unzoomed texture size, and even though the blur ends up slightly wrong as there's two | ||
| 204 | + // layers of texture resampling, up and back down). | ||
| 205 | + // | ||
| 206 | + // Here we use src.size rather than geometry directly because src takes into account | ||
| 207 | + // cropping. | ||
| 208 | + let size = src | ||
| 209 | + .size | ||
| 210 | + .to_logical(1., Transform::Normal) | ||
| 211 | + .upscale(clamp_scale) | ||
| 212 | + .to_physical_precise_round(self.scale); | ||
| 213 | + let size = transform.transform_size(size); | ||
| 214 | + | ||
| 215 | + let size = size.to_logical(1).to_buffer(1, Transform::Normal); | ||
| 216 | + | ||
| 217 | + // Recreate framebuffer if needed. | ||
| 218 | + if inner | ||
| 219 | + .framebuffer | ||
| 220 | + .as_ref() | ||
| 221 | + .is_some_and(|fb| fb.size() != size) | ||
| 222 | + { | ||
| 223 | + inner.framebuffer = None; | ||
| 224 | + } | ||
| 225 | + let framebuffer = if let Some(fb) = &inner.framebuffer { | ||
| 226 | + fb | ||
| 227 | + } else { | ||
| 228 | + trace!("creating framebuffer texture sized {} × {}", size.w, size.h); | ||
| 229 | + let renderer = guard.as_mut(); | ||
| 230 | + let texture = renderer.create_buffer(Fourcc::Abgr8888, size)?; | ||
| 231 | + inner.framebuffer.insert(texture) | ||
| 232 | + }; | ||
| 233 | + | ||
| 234 | + // Prepare blur textures. | ||
| 235 | + let mut blur = Option::zip(inner.blur.as_mut(), self.blur_options); | ||
| 236 | + if let Some((b, options)) = &mut blur { | ||
| 237 | + let renderer = guard.as_mut(); | ||
| 238 | + if let Err(err) = b.prepare_textures( | ||
| 239 | + |fourcc, size| renderer.create_buffer(fourcc, size), | ||
| 240 | + framebuffer, | ||
| 241 | + *options, | ||
| 242 | + ) { | ||
| 243 | + warn!("error preparing blur textures: {err:?}"); | ||
| 244 | + blur = None; | ||
| 245 | + } | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + // We can't use renderer.with_context() as that will reset the GlesFrame binding that we | ||
| 249 | + // want to blit from. | ||
| 250 | + drop(guard); | ||
| 251 | + | ||
| 252 | + // Blit the framebuffer contents. | ||
| 253 | + frame.with_context(|gl| unsafe { | ||
| 254 | + while gl.GetError() != ffi::NO_ERROR {} | ||
| 255 | + | ||
| 256 | + let mut current_fbo = 0i32; | ||
| 257 | + gl.GetIntegerv(ffi::DRAW_FRAMEBUFFER_BINDING, &mut current_fbo as *mut _); | ||
| 258 | + | ||
| 259 | + // BlitFramebuffer is affected by the scissor test, we don't want that. | ||
| 260 | + gl.Disable(ffi::SCISSOR_TEST); | ||
| 261 | + | ||
| 262 | + let mut fbo = 0; | ||
| 263 | + gl.GenFramebuffers(1, &mut fbo as *mut _); | ||
| 264 | + gl.BindFramebuffer(ffi::DRAW_FRAMEBUFFER, fbo); | ||
| 265 | + | ||
| 266 | + gl.FramebufferTexture2D( | ||
| 267 | + ffi::DRAW_FRAMEBUFFER, | ||
| 268 | + ffi::COLOR_ATTACHMENT0, | ||
| 269 | + ffi::TEXTURE_2D, | ||
| 270 | + framebuffer.tex_id(), | ||
| 271 | + 0, | ||
| 272 | + ); | ||
| 273 | + | ||
| 274 | + gl.BlitFramebuffer( | ||
| 275 | + dst.loc.x, | ||
| 276 | + dst.loc.y, | ||
| 277 | + dst.loc.x + dst.size.w, | ||
| 278 | + dst.loc.y + dst.size.h, | ||
| 279 | + 0, | ||
| 280 | + 0, | ||
| 281 | + size.w, | ||
| 282 | + size.h, | ||
| 283 | + ffi::COLOR_BUFFER_BIT, | ||
| 284 | + ffi::LINEAR, | ||
| 285 | + ); | ||
| 286 | + | ||
| 287 | + // Restore state set by GlesFrame that we just modified. | ||
| 288 | + gl.BindFramebuffer(ffi::DRAW_FRAMEBUFFER, current_fbo as u32); | ||
| 289 | + gl.Enable(ffi::SCISSOR_TEST); | ||
| 290 | + | ||
| 291 | + gl.DeleteFramebuffers(1, &mut fbo as *mut _); | ||
| 292 | + | ||
| 293 | + if gl.GetError() != ffi::NO_ERROR { | ||
| 294 | + Err(GlesError::BlitError) | ||
| 295 | + } else { | ||
| 296 | + Ok(()) | ||
| 297 | + } | ||
| 298 | + })??; | ||
| 299 | + | ||
| 300 | + // If blur is off, use the unblurred texture. | ||
| 301 | + if self.blur_options.is_none() { | ||
| 302 | + inner.intermediate = Some(framebuffer.clone()); | ||
| 303 | + return Ok(()); | ||
| 304 | + } | ||
| 305 | + | ||
| 306 | + if let Some((blur, options)) = blur { | ||
| 307 | + let mut guard = frame.renderer(); | ||
| 308 | + let renderer = guard.as_mut(); | ||
| 309 | + match blur.render(renderer, framebuffer, options) { | ||
| 310 | + Ok(blurred) => inner.intermediate = Some(blurred), | ||
| 311 | + Err(err) => { | ||
| 312 | + warn!("error rendering blur: {err:?}"); | ||
| 313 | + } | ||
| 314 | + } | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + Ok(()) | ||
| 318 | + }) | ||
| 319 | + } | ||
| 320 | + | ||
| 321 | + fn draw( | ||
| 322 | + &self, | ||
| 323 | + frame: &mut GlesFrame<'_, '_>, | ||
| 324 | + src: Rectangle<f64, Buffer>, | ||
| 325 | + dst: Rectangle<i32, Physical>, | ||
| 326 | + damage: &[Rectangle<i32, Physical>], | ||
| 327 | + _opaque_regions: &[Rectangle<i32, Physical>], | ||
| 328 | + cache: Option<&UserDataMap>, | ||
| 329 | + ) -> Result<(), GlesError> { | ||
| 330 | + let Some(cache) = cache else { | ||
| 331 | + return Ok(()); | ||
| 332 | + }; | ||
| 333 | + let Some(inner) = cache.get::<RefCell<Inner>>() else { | ||
| 334 | + return Ok(()); | ||
| 335 | + }; | ||
| 336 | + let mut inner = inner.borrow_mut(); | ||
| 337 | + let inner = &mut *inner; | ||
| 338 | + | ||
| 339 | + let Some(texture) = &inner.intermediate else { | ||
| 340 | + return Ok(()); | ||
| 341 | + }; | ||
| 342 | + | ||
| 343 | + // Clamp the same way as in capture_framebuffer(). | ||
| 344 | + let output_rect = Rectangle::from_size(frame.output_size()); | ||
| 345 | + let clamped_dst = match dst.intersection(output_rect) { | ||
| 346 | + Some(clamped) => clamped, | ||
| 347 | + None => return Ok(()), | ||
| 348 | + }; | ||
| 349 | + let clamp_offset = clamped_dst.loc - dst.loc; | ||
| 350 | + | ||
| 351 | + // Filter damage by subregion, reusing the stored Vec to avoid allocation. | ||
| 352 | + let filtered = &mut inner.subregion_damage; | ||
| 353 | + filtered.clear(); | ||
| 354 | + | ||
| 355 | + if let Some(subregion) = &self.subregion { | ||
| 356 | + // Convert to subregion coordinates. | ||
| 357 | + let mut crop = src.to_logical(1., Transform::Normal, &src.size); | ||
| 358 | + crop.loc += self.geometry.loc; | ||
| 359 | + subregion.filter_damage(crop, dst, damage, filtered); | ||
| 360 | + } else { | ||
| 361 | + filtered.extend(damage.iter()); | ||
| 362 | + }; | ||
| 363 | + | ||
| 364 | + // Adjust for clamped dst. | ||
| 365 | + if clamped_dst != dst { | ||
| 366 | + let r = Rectangle::new(clamp_offset, clamped_dst.size); | ||
| 367 | + filtered.retain_mut(|d| { | ||
| 368 | + if let Some(mut crop) = d.intersection(r) { | ||
| 369 | + crop.loc -= clamp_offset; | ||
| 370 | + *d = crop; | ||
| 371 | + true | ||
| 372 | + } else { | ||
| 373 | + false | ||
| 374 | + } | ||
| 375 | + }); | ||
| 376 | + } | ||
| 377 | + | ||
| 378 | + if filtered.is_empty() { | ||
| 379 | + return Ok(()); | ||
| 380 | + } | ||
| 381 | + let damage = &filtered[..]; | ||
| 382 | + | ||
| 383 | + // Adjust src proportionally to the dst clamping. | ||
| 384 | + let src_loc = src.loc.to_logical(1., Transform::Normal, &src.size); | ||
| 385 | + let dst_to_src = src.size / dst.size.to_f64(); | ||
| 386 | + let crop = Rectangle::new( | ||
| 387 | + src_loc + clamp_offset.to_f64().upscale(dst_to_src).to_logical(1.), | ||
| 388 | + clamped_dst.size.to_f64().upscale(dst_to_src).to_logical(1.), | ||
| 389 | + ); | ||
| 390 | + | ||
| 391 | + let program = Shaders::get_from_frame(frame).postprocess_and_clip.clone(); | ||
| 392 | + let uniforms = program | ||
| 393 | + .is_some() | ||
| 394 | + .then(|| self.compute_uniforms(crop, frame.transformation())); | ||
| 395 | + let uniforms = uniforms.as_ref().map_or(&[][..], |x| &x[..]); | ||
| 396 | + | ||
| 397 | + frame.render_texture_from_to( | ||
| 398 | + texture, | ||
| 399 | + Rectangle::from_size(texture.size().to_f64()), | ||
| 400 | + clamped_dst, | ||
| 401 | + damage, | ||
| 402 | + &[], | ||
| 403 | + // The intermediate texture has the same transform as the frame. | ||
| 404 | + frame.transformation().invert(), | ||
| 405 | + 1., | ||
| 406 | + program.as_ref(), | ||
| 407 | + uniforms, | ||
| 408 | + ) | ||
| 409 | + } | ||
| 410 | +} | ||
| 411 | + | ||
| 412 | +impl<'render> RenderElement<TtyRenderer<'render>> for FramebufferEffectElement { | ||
| 413 | + fn capture_framebuffer( | ||
| 414 | + &self, | ||
| 415 | + frame: &mut TtyFrame<'_, '_, '_>, | ||
| 416 | + src: Rectangle<f64, Buffer>, | ||
| 417 | + dst: Rectangle<i32, Physical>, | ||
| 418 | + cache: &UserDataMap, | ||
| 419 | + ) -> Result<(), TtyRendererError<'render>> { | ||
| 420 | + let gles_frame = frame.as_gles_frame(); | ||
| 421 | + RenderElement::<GlesRenderer>::capture_framebuffer(&self, gles_frame, src, dst, cache)?; | ||
| 422 | + Ok(()) | ||
| 423 | + } | ||
| 424 | + | ||
| 425 | + fn draw( | ||
| 426 | + &self, | ||
| 427 | + frame: &mut TtyFrame<'_, '_, '_>, | ||
| 428 | + src: Rectangle<f64, Buffer>, | ||
| 429 | + dst: Rectangle<i32, Physical>, | ||
| 430 | + damage: &[Rectangle<i32, Physical>], | ||
| 431 | + opaque_regions: &[Rectangle<i32, Physical>], | ||
| 432 | + cache: Option<&UserDataMap>, | ||
| 433 | + ) -> Result<(), TtyRendererError<'render>> { | ||
| 434 | + let gles_frame = frame.as_gles_frame(); | ||
| 435 | + RenderElement::<GlesRenderer>::draw( | ||
| 436 | + &self, | ||
| 437 | + gles_frame, | ||
| 438 | + src, | ||
| 439 | + dst, | ||
| 440 | + damage, | ||
| 441 | + opaque_regions, | ||
| 442 | + cache, | ||
| 443 | + )?; | ||
| 444 | + Ok(()) | ||
| 445 | + } | ||
| 446 | +} | ||
| 447 | + | ||
| 448 | +impl Inner { | ||
| 449 | + fn new(renderer: &mut GlesRenderer) -> Self { | ||
| 450 | + Inner { | ||
| 451 | + framebuffer: None, | ||
| 452 | + blur: Blur::new(renderer), | ||
| 453 | + intermediate: None, | ||
| 454 | + subregion_damage: Vec::new(), | ||
| 455 | + } | ||
| 456 | + } | ||
| 457 | +} | ||
src/render_helpers/mod.rsmodified+1 -0
| @@ -33,6 +33,7 @@ pub mod clipped_surface; | |||
| 33 | pub mod damage; | 33 | pub mod damage; |
| 34 | pub mod debug; | 34 | pub mod debug; |
| 35 | pub mod effect_buffer; | 35 | pub mod effect_buffer; |
| 36 | +pub mod framebuffer_effect; | ||
| 36 | pub mod gradient_fade_texture; | 37 | pub mod gradient_fade_texture; |
| 37 | pub mod memory; | 38 | pub mod memory; |
| 38 | pub mod offscreen; | 39 | pub mod offscreen; |
src/window/mapped.rsmodified+1 -0
| @@ -697,6 +697,7 @@ impl LayoutElement for Mapped { | |||
| 697 | let should_block_out = ctx.target.should_block_out(self.rules.block_out_from); | 697 | let should_block_out = ctx.target.should_block_out(self.rules.block_out_from); |
| 698 | background_effect::render_for_tile( | 698 | background_effect::render_for_tile( |
| 699 | ctx, | 699 | ctx, |
| 700 | + None, | ||
| 700 | geometry, | 701 | geometry, |
| 701 | scale, | 702 | scale, |
| 702 | clip_to_geometry, | 703 | clip_to_geometry, |