niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::array; |
| 2 | use std::cell::RefCell; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use glam::{Mat3, Vec2}; |
| 6 | use niri_config::CornerRadius; |
| 7 | use smithay::backend::renderer::element::{Element, Id, RenderElement}; |
| 8 | use smithay::backend::renderer::gles::{ |
| 9 | GlesError, GlesFrame, GlesRenderer, GlesTexProgram, Uniform, |
| 10 | }; |
| 11 | use smithay::backend::renderer::utils::{CommitCounter, OpaqueRegions}; |
| 12 | use smithay::backend::renderer::Color32F; |
| 13 | use smithay::utils::user_data::UserDataMap; |
| 14 | use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 15 | |
| 16 | use crate::backend::tty::{TtyFrame, TtyRenderer, TtyRendererError}; |
| 17 | use crate::render_helpers::background_effect::RenderParams; |
| 18 | use crate::render_helpers::effect_buffer::EffectBuffer; |
| 19 | use crate::render_helpers::renderer::AsGlesFrame as _; |
| 20 | use crate::render_helpers::shaders::{mat3_uniform, Shaders}; |
| 21 | use crate::render_helpers::{RenderCtx, RenderTarget}; |
| 22 | use crate::utils::region::TransformedRegion; |
| 23 | |
| 24 | #[derive(Debug)] |
| 25 | pub struct Xray { |
| 26 | // The buffers are per-render-target to avoid constant rerendering when screencasting. |
| 27 | pub background: [Rc<RefCell<EffectBuffer>>; RenderTarget::COUNT], |
| 28 | pub backdrop: [Rc<RefCell<EffectBuffer>>; RenderTarget::COUNT], |
| 29 | pub backdrop_color: Color32F, |
| 30 | pub workspaces: Vec<(Rectangle<f64, Logical>, Color32F)>, |
| 31 | } |
| 32 | |
| 33 | /// Position for drawing xray background. |
| 34 | #[derive(Debug, Clone, Copy)] |
| 35 | pub struct XrayPos { |
| 36 | /// Position of geometry relative to the backdrop in zoomed coordinates. |
| 37 | /// |
| 38 | /// Should be upscaled by `zoom` to get position in backdrop coordinates. |
| 39 | pub pos_in_backdrop: Point<f64, Logical>, |
| 40 | |
| 41 | /// Zoom factor between backdrop coordinates and geometry. |
| 42 | pub zoom: f64, |
| 43 | } |
| 44 | |
| 45 | impl XrayPos { |
| 46 | pub fn new(pos_in_backdrop: Point<f64, Logical>, zoom: f64) -> Self { |
| 47 | Self { |
| 48 | pos_in_backdrop: pos_in_backdrop.downscale(zoom), |
| 49 | zoom, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn offset(mut self, offset: Point<f64, Logical>) -> Self { |
| 54 | self.pos_in_backdrop += offset; |
| 55 | self |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl Default for XrayPos { |
| 60 | fn default() -> Self { |
| 61 | Self { |
| 62 | pos_in_backdrop: Point::new(0., 0.), |
| 63 | zoom: 1., |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug)] |
| 69 | pub struct XrayElement { |
| 70 | buffer: Rc<RefCell<EffectBuffer>>, |
| 71 | id: Id, |
| 72 | geometry: Rectangle<f64, Logical>, |
| 73 | src: Rectangle<f64, Buffer>, |
| 74 | subregion: Option<TransformedRegion>, |
| 75 | input_to_clip_geo: Mat3, |
| 76 | clip_geo_size: Vec2, |
| 77 | corner_radius: CornerRadius, |
| 78 | scale: f32, |
| 79 | blur: bool, |
| 80 | noise: f32, |
| 81 | saturation: f32, |
| 82 | bg_color: Color32F, |
| 83 | program: Option<GlesTexProgram>, |
| 84 | } |
| 85 | |
| 86 | impl Xray { |
| 87 | pub fn new() -> Self { |
| 88 | Self { |
| 89 | background: array::from_fn(|_| Rc::new(RefCell::new(EffectBuffer::new()))), |
| 90 | backdrop: array::from_fn(|_| Rc::new(RefCell::new(EffectBuffer::new()))), |
| 91 | backdrop_color: Color32F::TRANSPARENT, |
| 92 | workspaces: Vec::new(), |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[allow(clippy::too_many_arguments)] |
| 97 | pub fn render( |
| 98 | &self, |
| 99 | ctx: RenderCtx<GlesRenderer>, |
| 100 | params: RenderParams, |
| 101 | xray_pos: XrayPos, |
| 102 | blur: bool, |
| 103 | noise: f32, |
| 104 | saturation: f32, |
| 105 | push: &mut dyn FnMut(XrayElement), |
| 106 | ) { |
| 107 | let program = Shaders::get(ctx.renderer).postprocess_and_clip.clone(); |
| 108 | |
| 109 | let zoom = xray_pos.zoom; |
| 110 | let pos_in_backdrop = xray_pos.pos_in_backdrop.upscale(zoom); |
| 111 | |
| 112 | let (clip_geo, corner_radius) = params |
| 113 | .clip |
| 114 | .unwrap_or((params.geometry, CornerRadius::default())); |
| 115 | |
| 116 | let clip_offset = clip_geo.loc - params.geometry.loc; |
| 117 | let clip_pos_in_backdrop = pos_in_backdrop + clip_offset.upscale(zoom); |
| 118 | |
| 119 | let geo_in_backdrop = Rectangle::new(pos_in_backdrop, params.geometry.size.upscale(zoom)); |
| 120 | |
| 121 | let mut backdrop = self.backdrop[ctx.target as usize].borrow_mut(); |
| 122 | let backdrop_geo = Rectangle::from_size(backdrop.logical_size()); |
| 123 | let intersection_with_backdrop = backdrop_geo.intersection(geo_in_backdrop); |
| 124 | |
| 125 | let mut skip_backdrop = intersection_with_backdrop.is_none(); |
| 126 | |
| 127 | let mut background = self.background[ctx.target as usize].borrow_mut(); |
| 128 | let prev = background.commit(); |
| 129 | if background.prepare(ctx.renderer, blur) { |
| 130 | if background.commit() != prev { |
| 131 | trace!("background damaged"); |
| 132 | } |
| 133 | |
| 134 | let clip_geo_size = Vec2::new(clip_geo.size.w as f32, clip_geo.size.h as f32); |
| 135 | let buf_size = background.logical_size(); |
| 136 | |
| 137 | for (ws_geo, bg_color) in &self.workspaces { |
| 138 | // If the background color is opaque, check if the workspace fully covers the |
| 139 | // element. In this case, we will skip the backdrop element since it's fully |
| 140 | // covered. |
| 141 | // |
| 142 | // FIXME: also implement some way to check if the background elements are fully |
| 143 | // covered in opaque regions, and not just the niri background color is opaque |
| 144 | let crop = if bg_color.is_opaque() && ws_geo.contains_rect(geo_in_backdrop) { |
| 145 | skip_backdrop = true; |
| 146 | // No need to intersect, we know it's fully covered. |
| 147 | Some(geo_in_backdrop) |
| 148 | } else { |
| 149 | ws_geo.intersection(geo_in_backdrop) |
| 150 | }; |
| 151 | |
| 152 | let Some(crop) = crop else { |
| 153 | continue; |
| 154 | }; |
| 155 | |
| 156 | // If crop contains the intersection with backdrop, then the workspace fully |
| 157 | // covers the backdrop, so we can skip the backdrop. |
| 158 | // |
| 159 | // This can happen when the overview is closed (so workspaces align left/right with |
| 160 | // the backdrop) and the window is peeking out off screen to the side. In this |
| 161 | // case, this off-screen part is on top of nothing, neither workspace nor backdrop, |
| 162 | // but since the window doesn't fully cover the workspace, the check above doesn't |
| 163 | // skip the backdrop. |
| 164 | if bg_color.is_opaque() |
| 165 | && intersection_with_backdrop |
| 166 | .is_some_and(|backdrop| crop.contains_rect(backdrop)) |
| 167 | { |
| 168 | skip_backdrop = true; |
| 169 | } |
| 170 | |
| 171 | // This can be different from zoom for surfaces that do not scale with |
| 172 | // workspaces, e.g. layer-shell top and overlay layer. |
| 173 | let ws_zoom = ws_geo.size / buf_size; |
| 174 | |
| 175 | let src = Rectangle::new(crop.loc - ws_geo.loc, crop.size).downscale(ws_zoom); |
| 176 | let src = src.to_buffer(background.scale(), Transform::Normal, &buf_size); |
| 177 | |
| 178 | let buf_size = Vec2::new(buf_size.w as f32, buf_size.h as f32); |
| 179 | let pos_against_buf = (clip_pos_in_backdrop - ws_geo.loc).downscale(ws_zoom); |
| 180 | let pos_against_buf = Vec2::new(pos_against_buf.x as f32, pos_against_buf.y as f32); |
| 181 | let ws_zoom_vec = Vec2::new(ws_zoom.x as f32, ws_zoom.y as f32); |
| 182 | let input_to_clip_geo = Mat3::from_scale(ws_zoom_vec / zoom as f32) |
| 183 | * Mat3::from_scale(buf_size / clip_geo_size) |
| 184 | * Mat3::from_translation(-pos_against_buf / buf_size); |
| 185 | |
| 186 | let mut geometry = |
| 187 | Rectangle::new(crop.loc - geo_in_backdrop.loc, crop.size).downscale(zoom); |
| 188 | geometry.loc += params.geometry.loc; |
| 189 | |
| 190 | let elem = XrayElement { |
| 191 | buffer: self.background[ctx.target as usize].clone(), |
| 192 | id: background.id().clone(), |
| 193 | geometry, |
| 194 | src, |
| 195 | subregion: params.subregion.clone(), |
| 196 | input_to_clip_geo, |
| 197 | clip_geo_size, |
| 198 | corner_radius, |
| 199 | scale: params.scale as f32, |
| 200 | blur, |
| 201 | noise, |
| 202 | saturation, |
| 203 | bg_color: *bg_color, |
| 204 | program: program.clone(), |
| 205 | }; |
| 206 | push(elem); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // If the backdrop is fully covered by opaque background, we can skip it. |
| 211 | if skip_backdrop { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | let prev = backdrop.commit(); |
| 216 | if backdrop.prepare(ctx.renderer, blur) { |
| 217 | if backdrop.commit() != prev { |
| 218 | trace!("backdrop damaged"); |
| 219 | } |
| 220 | |
| 221 | let buf_size = backdrop.logical_size(); |
| 222 | let src = geo_in_backdrop.to_buffer(backdrop.scale(), Transform::Normal, &buf_size); |
| 223 | |
| 224 | let mut clip_geo_in_backdrop = Rectangle::new(clip_offset, clip_geo.size).upscale(zoom); |
| 225 | clip_geo_in_backdrop.loc += geo_in_backdrop.loc; |
| 226 | |
| 227 | let clip_pos_in_backdrop = Vec2::new( |
| 228 | clip_geo_in_backdrop.loc.x as f32, |
| 229 | clip_geo_in_backdrop.loc.y as f32, |
| 230 | ); |
| 231 | let clip_geo_size = Vec2::new( |
| 232 | clip_geo_in_backdrop.size.w as f32, |
| 233 | clip_geo_in_backdrop.size.h as f32, |
| 234 | ); |
| 235 | |
| 236 | let buf_size = Vec2::new(buf_size.w as f32, buf_size.h as f32); |
| 237 | let input_to_clip_geo = Mat3::from_scale(buf_size / clip_geo_size) |
| 238 | * Mat3::from_translation(-clip_pos_in_backdrop / buf_size); |
| 239 | |
| 240 | let elem = XrayElement { |
| 241 | buffer: self.backdrop[ctx.target as usize].clone(), |
| 242 | id: backdrop.id().clone(), |
| 243 | geometry: params.geometry, |
| 244 | src, |
| 245 | subregion: params.subregion.clone(), |
| 246 | input_to_clip_geo, |
| 247 | clip_geo_size, |
| 248 | corner_radius: corner_radius.scaled_by(zoom as f32), |
| 249 | scale: params.scale as f32, |
| 250 | blur, |
| 251 | noise, |
| 252 | saturation, |
| 253 | bg_color: self.backdrop_color, |
| 254 | program: program.clone(), |
| 255 | }; |
| 256 | push(elem); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | impl XrayElement { |
| 262 | fn compute_uniforms(&self) -> [Uniform<'static>; 7] { |
| 263 | [ |
| 264 | Uniform::new("niri_scale", self.scale), |
| 265 | Uniform::new("geo_size", <[f32; 2]>::from(self.clip_geo_size)), |
| 266 | Uniform::new("corner_radius", <[f32; 4]>::from(self.corner_radius)), |
| 267 | mat3_uniform("input_to_geo", self.input_to_clip_geo), |
| 268 | Uniform::new("noise", self.noise), |
| 269 | Uniform::new("saturation", self.saturation), |
| 270 | Uniform::new("bg_color", self.bg_color.components()), |
| 271 | ] |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | impl Element for XrayElement { |
| 276 | fn id(&self) -> &Id { |
| 277 | &self.id |
| 278 | } |
| 279 | |
| 280 | fn current_commit(&self) -> CommitCounter { |
| 281 | self.buffer.borrow().commit() |
| 282 | } |
| 283 | |
| 284 | fn src(&self) -> Rectangle<f64, Buffer> { |
| 285 | self.src |
| 286 | } |
| 287 | |
| 288 | fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> { |
| 289 | self.geometry.to_physical_precise_round(scale) |
| 290 | } |
| 291 | |
| 292 | fn opaque_regions(&self, _scale: Scale<f64>) -> OpaqueRegions<i32, Physical> { |
| 293 | // FIXME: if bg_color alpha is 1 then compute opaque regions here taking corners into |
| 294 | // account |
| 295 | OpaqueRegions::default() |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | impl RenderElement<GlesRenderer> for XrayElement { |
| 300 | fn draw( |
| 301 | &self, |
| 302 | frame: &mut GlesFrame<'_, '_>, |
| 303 | src: Rectangle<f64, Buffer>, |
| 304 | dst: Rectangle<i32, Physical>, |
| 305 | damage: &[Rectangle<i32, Physical>], |
| 306 | _opaque_regions: &[Rectangle<i32, Physical>], |
| 307 | _cache: Option<&UserDataMap>, |
| 308 | ) -> Result<(), GlesError> { |
| 309 | let mut buffer = self.buffer.borrow_mut(); |
| 310 | let texture = match buffer.render(frame, self.blur) { |
| 311 | Ok(x) => x, |
| 312 | Err(err) => { |
| 313 | warn!("error rendering effect buffer: {err:?}"); |
| 314 | return Ok(()); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | // FIXME: avoid reallocating a fresh Vec here somehow. |
| 319 | let mut filtered_damage = Vec::new(); |
| 320 | let damage = if let Some(subregion) = &self.subregion { |
| 321 | let src_to_geo = self.geometry.size / self.src.size; |
| 322 | |
| 323 | // Compute crop in geometry coordinates. |
| 324 | let mut crop = src; |
| 325 | crop.loc -= self.src.loc; |
| 326 | crop = crop.upscale(src_to_geo); |
| 327 | let mut crop = crop.to_logical(1., Transform::Normal, &Size::default()); |
| 328 | |
| 329 | // Then convert to subregion coordinates. |
| 330 | crop.loc += self.geometry.loc; |
| 331 | |
| 332 | subregion.filter_damage(crop, dst, damage, &mut filtered_damage); |
| 333 | |
| 334 | if filtered_damage.is_empty() { |
| 335 | return Ok(()); |
| 336 | } |
| 337 | &filtered_damage[..] |
| 338 | } else { |
| 339 | damage |
| 340 | }; |
| 341 | |
| 342 | let uniforms = self.program.is_some().then(|| self.compute_uniforms()); |
| 343 | let uniforms = uniforms.as_ref().map_or(&[][..], |x| &x[..]); |
| 344 | |
| 345 | frame.render_texture_from_to( |
| 346 | &texture, |
| 347 | src, |
| 348 | dst, |
| 349 | damage, |
| 350 | // FIXME: opaque regions need to be filtered like damage. |
| 351 | &[], |
| 352 | Transform::Normal, |
| 353 | 1., |
| 354 | self.program.as_ref(), |
| 355 | uniforms, |
| 356 | ) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | impl<'render> RenderElement<TtyRenderer<'render>> for XrayElement { |
| 361 | fn draw( |
| 362 | &self, |
| 363 | frame: &mut TtyFrame<'_, '_, '_>, |
| 364 | src: Rectangle<f64, Buffer>, |
| 365 | dst: Rectangle<i32, Physical>, |
| 366 | damage: &[Rectangle<i32, Physical>], |
| 367 | opaque_regions: &[Rectangle<i32, Physical>], |
| 368 | cache: Option<&UserDataMap>, |
| 369 | ) -> Result<(), TtyRendererError<'render>> { |
| 370 | let gles_frame = frame.as_gles_frame(); |
| 371 | RenderElement::<GlesRenderer>::draw( |
| 372 | &self, |
| 373 | gles_frame, |
| 374 | src, |
| 375 | dst, |
| 376 | damage, |
| 377 | opaque_regions, |
| 378 | cache, |
| 379 | )?; |
| 380 | Ok(()) |
| 381 | } |
| 382 | } |