niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·53500dc·Tahinli·1 month ago·2998 commits
| 1 | use niri_config::utils::MergeWith as _; |
| 2 | use niri_config::{Config, LayerRule}; |
| 3 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 4 | use smithay::backend::renderer::element::Kind; |
| 5 | use smithay::desktop::{LayerSurface, PopupKind, PopupManager}; |
| 6 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; |
| 7 | use smithay::wayland::compositor::{remove_pre_commit_hook, HookId}; |
| 8 | use smithay::wayland::shell::wlr_layer::{ExclusiveZone, Layer}; |
| 9 | |
| 10 | use super::ResolvedLayerRules; |
| 11 | use crate::animation::Clock; |
| 12 | use crate::layout::shadow::Shadow; |
| 13 | use crate::niri_render_elements; |
| 14 | use crate::render_helpers::background_effect::BackgroundEffectElement; |
| 15 | use crate::render_helpers::renderer::NiriRenderer; |
| 16 | use crate::render_helpers::shadow::ShadowRenderElement; |
| 17 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 18 | use crate::render_helpers::surface::push_elements_from_surface_tree; |
| 19 | use crate::render_helpers::xray::XrayPos; |
| 20 | use crate::render_helpers::{background_effect, RenderCtx}; |
| 21 | use crate::utils::{baba_is_float_offset, round_logical_in_physical}; |
| 22 | |
| 23 | #[derive(Debug)] |
| 24 | pub struct MappedLayer { |
| 25 | /// The surface itself. |
| 26 | surface: LayerSurface, |
| 27 | |
| 28 | /// Pre-commit hook that we have on all mapped layer surfaces. |
| 29 | pre_commit_hook: HookId, |
| 30 | |
| 31 | /// Up-to-date rules. |
| 32 | rules: ResolvedLayerRules, |
| 33 | |
| 34 | /// Whether to recompute layer rules on the next commit. |
| 35 | /// |
| 36 | /// Set in the pre-commit hook when the layer changes; consumed in the commit handler. |
| 37 | recompute_rules_on_commit: bool, |
| 38 | |
| 39 | /// Buffer to draw instead of the surface when it should be blocked out. |
| 40 | block_out_buffer: SolidColorBuffer, |
| 41 | |
| 42 | /// The shadow around the surface. |
| 43 | shadow: Shadow, |
| 44 | |
| 45 | /// The blur config, passed for background effect rendering. |
| 46 | blur_config: niri_config::Blur, |
| 47 | |
| 48 | /// The view size for the layer surface's output. |
| 49 | view_size: Size<f64, Logical>, |
| 50 | |
| 51 | /// Scale of the output the layer surface is on (and rounds its sizes to). |
| 52 | scale: f64, |
| 53 | |
| 54 | /// Clock for driving animations. |
| 55 | clock: Clock, |
| 56 | } |
| 57 | |
| 58 | niri_render_elements! { |
| 59 | LayerSurfaceRenderElement<R> => { |
| 60 | Wayland = WaylandSurfaceRenderElement<R>, |
| 61 | SolidColor = SolidColorRenderElement, |
| 62 | Shadow = ShadowRenderElement, |
| 63 | BackgroundEffect = BackgroundEffectElement, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl MappedLayer { |
| 68 | pub fn new( |
| 69 | surface: LayerSurface, |
| 70 | pre_commit_hook: HookId, |
| 71 | rules: ResolvedLayerRules, |
| 72 | view_size: Size<f64, Logical>, |
| 73 | scale: f64, |
| 74 | clock: Clock, |
| 75 | config: &Config, |
| 76 | ) -> Self { |
| 77 | let mut shadow_config = config.layout.shadow; |
| 78 | // Shadows for layer surfaces need to be explicitly enabled. |
| 79 | shadow_config.on = false; |
| 80 | shadow_config.merge_with(&rules.shadow); |
| 81 | |
| 82 | Self { |
| 83 | surface, |
| 84 | pre_commit_hook, |
| 85 | rules, |
| 86 | recompute_rules_on_commit: false, |
| 87 | block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), |
| 88 | view_size, |
| 89 | scale, |
| 90 | shadow: Shadow::new(shadow_config), |
| 91 | blur_config: config.blur, |
| 92 | clock, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn update_config(&mut self, config: &Config) { |
| 97 | let mut shadow_config = config.layout.shadow; |
| 98 | // Shadows for layer surfaces need to be explicitly enabled. |
| 99 | shadow_config.on = false; |
| 100 | shadow_config.merge_with(&self.rules.shadow); |
| 101 | self.shadow.update_config(shadow_config); |
| 102 | |
| 103 | self.blur_config = config.blur; |
| 104 | } |
| 105 | |
| 106 | pub fn update_shaders(&mut self) { |
| 107 | self.shadow.update_shaders(); |
| 108 | } |
| 109 | |
| 110 | pub fn update_sizes(&mut self, view_size: Size<f64, Logical>, scale: f64) { |
| 111 | self.view_size = view_size; |
| 112 | self.scale = scale; |
| 113 | } |
| 114 | |
| 115 | pub fn update_render_elements(&mut self, size: Size<f64, Logical>) { |
| 116 | // Round to physical pixels. |
| 117 | let size = size |
| 118 | .to_physical_precise_round(self.scale) |
| 119 | .to_logical(self.scale); |
| 120 | |
| 121 | self.block_out_buffer.resize(size); |
| 122 | |
| 123 | let radius = self.rules.geometry_corner_radius.unwrap_or_default(); |
| 124 | // FIXME: is_active based on keyboard focus? |
| 125 | self.shadow |
| 126 | .update_render_elements(size, true, radius, self.scale, 1.); |
| 127 | } |
| 128 | |
| 129 | pub fn are_animations_ongoing(&self) -> bool { |
| 130 | self.rules.baba_is_float |
| 131 | } |
| 132 | |
| 133 | pub fn surface(&self) -> &LayerSurface { |
| 134 | &self.surface |
| 135 | } |
| 136 | |
| 137 | pub fn rules(&self) -> &ResolvedLayerRules { |
| 138 | &self.rules |
| 139 | } |
| 140 | |
| 141 | /// Recomputes the resolved layer rules and returns whether they changed. |
| 142 | pub fn recompute_layer_rules(&mut self, rules: &[LayerRule], is_at_startup: bool) -> bool { |
| 143 | let new_rules = ResolvedLayerRules::compute(rules, &self.surface, is_at_startup); |
| 144 | if new_rules == self.rules { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | self.rules = new_rules; |
| 149 | true |
| 150 | } |
| 151 | |
| 152 | pub fn set_recompute_rules_on_commit(&mut self) { |
| 153 | self.recompute_rules_on_commit = true; |
| 154 | } |
| 155 | |
| 156 | pub fn take_recompute_rules_on_commit(&mut self) -> bool { |
| 157 | std::mem::take(&mut self.recompute_rules_on_commit) |
| 158 | } |
| 159 | |
| 160 | pub fn place_within_backdrop(&self) -> bool { |
| 161 | if !self.rules.place_within_backdrop { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | if self.surface.layer() != Layer::Background { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | let state = self.surface.cached_state(); |
| 170 | if state.exclusive_zone != ExclusiveZone::DontCare { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | true |
| 175 | } |
| 176 | |
| 177 | pub fn bob_offset(&self) -> Point<f64, Logical> { |
| 178 | if !self.rules.baba_is_float { |
| 179 | return Point::from((0., 0.)); |
| 180 | } |
| 181 | |
| 182 | let y = baba_is_float_offset(self.clock.now(), self.view_size.h); |
| 183 | let y = round_logical_in_physical(self.scale, y); |
| 184 | Point::from((0., y)) |
| 185 | } |
| 186 | |
| 187 | pub fn render_normal<R: NiriRenderer>( |
| 188 | &self, |
| 189 | mut ctx: RenderCtx<R>, |
| 190 | ns: Option<usize>, |
| 191 | location: Point<f64, Logical>, |
| 192 | xray_pos: XrayPos, |
| 193 | push: &mut dyn FnMut(LayerSurfaceRenderElement<R>), |
| 194 | ) { |
| 195 | let scale = Scale::from(self.scale); |
| 196 | let alpha = self.rules.opacity.unwrap_or(1.).clamp(0., 1.); |
| 197 | |
| 198 | let bob_offset = self.bob_offset(); |
| 199 | let location = location + bob_offset; |
| 200 | let xray_pos = xray_pos.offset(bob_offset); |
| 201 | |
| 202 | let surface = self.surface.wl_surface(); |
| 203 | |
| 204 | let should_block_out = ctx.target.should_block_out(self.rules.block_out_from); |
| 205 | if should_block_out { |
| 206 | // Round to physical pixels. |
| 207 | let location = location.to_physical_precise_round(scale).to_logical(scale); |
| 208 | |
| 209 | // FIXME: take geometry-corner-radius into account. |
| 210 | let elem = SolidColorRenderElement::from_buffer( |
| 211 | &self.block_out_buffer, |
| 212 | location, |
| 213 | alpha, |
| 214 | Kind::Unspecified, |
| 215 | ); |
| 216 | push(elem.into()); |
| 217 | } else { |
| 218 | // Layer surfaces don't have extra geometry like windows. |
| 219 | let buf_pos = location; |
| 220 | |
| 221 | push_elements_from_surface_tree( |
| 222 | ctx.renderer, |
| 223 | surface, |
| 224 | buf_pos.to_physical_precise_round(scale), |
| 225 | scale, |
| 226 | alpha, |
| 227 | Kind::ScanoutCandidate, |
| 228 | &mut |elem| push(elem.into()), |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | let location = location.to_physical_precise_round(scale).to_logical(scale); |
| 233 | self.shadow |
| 234 | .render(ctx.renderer, location, &mut |elem| push(elem.into())); |
| 235 | |
| 236 | let geometry = Rectangle::new(location, self.block_out_buffer.size()); |
| 237 | let surface_off = Point::new(0., 0.); // No geometry on layer surfaces. |
| 238 | let surface_anim_scale = Scale::from(1.); |
| 239 | let radius = self.rules.geometry_corner_radius.unwrap_or_default(); |
| 240 | background_effect::render_for_tile( |
| 241 | ctx.as_gles(), |
| 242 | ns, |
| 243 | geometry, |
| 244 | self.scale, |
| 245 | false, |
| 246 | surface, |
| 247 | surface_off, |
| 248 | surface_anim_scale, |
| 249 | self.blur_config, |
| 250 | radius, |
| 251 | self.rules.background_effect, |
| 252 | should_block_out, |
| 253 | xray_pos, |
| 254 | &mut |elem| push(elem.into()), |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | pub fn render_popups<R: NiriRenderer>( |
| 259 | &self, |
| 260 | mut ctx: RenderCtx<R>, |
| 261 | ns: Option<usize>, |
| 262 | location: Point<f64, Logical>, |
| 263 | xray_pos: XrayPos, |
| 264 | push: &mut dyn FnMut(LayerSurfaceRenderElement<R>), |
| 265 | ) { |
| 266 | if ctx.target.should_block_out(self.rules.block_out_from) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | let scale = Scale::from(self.scale); |
| 271 | let alpha = self.rules.opacity.unwrap_or(1.).clamp(0., 1.); |
| 272 | |
| 273 | let bob_offset = self.bob_offset(); |
| 274 | let location = location + bob_offset; |
| 275 | let xray_pos = xray_pos.offset(bob_offset); |
| 276 | |
| 277 | let surface = self.surface.wl_surface(); |
| 278 | for (popup, offset) in PopupManager::popups_for_surface(surface) { |
| 279 | let popup_rules = match popup { |
| 280 | PopupKind::Xdg(_) => self.rules.popups, |
| 281 | // IME popups aren't affected by rules for regular popups. |
| 282 | PopupKind::InputMethod(_) => niri_config::ResolvedPopupsRules::default(), |
| 283 | }; |
| 284 | let alpha = match popup { |
| 285 | PopupKind::Xdg(_) => alpha * popup_rules.opacity.unwrap_or(1.).clamp(0., 1.), |
| 286 | // IME popups aren't affected by the layer opacity. |
| 287 | PopupKind::InputMethod(_) => 1., |
| 288 | }; |
| 289 | |
| 290 | let surface = popup.wl_surface(); |
| 291 | let popup_geo = popup.geometry(); |
| 292 | let surface_loc = location + (offset - popup_geo.loc).to_f64(); |
| 293 | |
| 294 | push_elements_from_surface_tree( |
| 295 | ctx.renderer, |
| 296 | surface, |
| 297 | surface_loc.to_physical_precise_round(scale), |
| 298 | scale, |
| 299 | alpha, |
| 300 | Kind::ScanoutCandidate, |
| 301 | &mut |elem| push(elem.into()), |
| 302 | ); |
| 303 | |
| 304 | let geometry = Rectangle::new(location + offset.to_f64(), popup_geo.size.to_f64()); |
| 305 | let surface_off = popup_geo.loc.upscale(-1).to_f64(); |
| 306 | let surface_anim_scale = Scale::from(1.); |
| 307 | let mut effect = popup_rules.background_effect; |
| 308 | // Default xray to false for pop-ups since they're always on top of something. |
| 309 | if effect.xray.is_none() { |
| 310 | effect.xray = Some(false); |
| 311 | } |
| 312 | let xray_pos = xray_pos.offset(offset.to_f64()); |
| 313 | background_effect::render_for_tile( |
| 314 | ctx.as_gles(), |
| 315 | ns, |
| 316 | geometry, |
| 317 | self.scale, |
| 318 | false, |
| 319 | surface, |
| 320 | surface_off, |
| 321 | surface_anim_scale, |
| 322 | self.blur_config, |
| 323 | popup_rules.geometry_corner_radius.unwrap_or_default(), |
| 324 | effect, |
| 325 | false, |
| 326 | xray_pos, |
| 327 | &mut |elem| push(elem.into()), |
| 328 | ); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | impl Drop for MappedLayer { |
| 334 | fn drop(&mut self) { |
| 335 | remove_pre_commit_hook(self.surface.wl_surface(), &self.pre_commit_hook); |
| 336 | } |
| 337 | } |