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::cell::RefCell; |
| 2 | use std::collections::HashMap; |
| 3 | use std::mem; |
| 4 | use std::rc::Rc; |
| 5 | use std::sync::{Arc, Mutex}; |
| 6 | |
| 7 | use anyhow::Context as _; |
| 8 | use niri_config::{Config, OutputName}; |
| 9 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 10 | use smithay::backend::egl::EGLDevice; |
| 11 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 12 | use smithay::backend::renderer::gles::GlesRenderer; |
| 13 | use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer}; |
| 14 | use smithay::backend::winit::{self, WinitEvent, WinitGraphicsBackend}; |
| 15 | use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; |
| 16 | use smithay::reexports::calloop::LoopHandle; |
| 17 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 18 | use smithay::reexports::winit::dpi::LogicalSize; |
| 19 | use smithay::reexports::winit::platform::wayland::WindowAttributesWayland; |
| 20 | use smithay::reexports::winit::window::WindowAttributes; |
| 21 | use smithay::wayland::dmabuf::{DmabufFeedbackBuilder, DmabufGlobal}; |
| 22 | use smithay::wayland::presentation::Refresh; |
| 23 | |
| 24 | use super::{IpcOutputMap, OutputId, RenderResult}; |
| 25 | use crate::niri::{Niri, RedrawState, State}; |
| 26 | use crate::render_helpers::debug::draw_damage; |
| 27 | use crate::render_helpers::{resources, shaders, RenderCtx, RenderTarget}; |
| 28 | use crate::utils::{get_monotonic_time, logical_output}; |
| 29 | |
| 30 | pub struct Winit { |
| 31 | config: Rc<RefCell<Config>>, |
| 32 | output: Output, |
| 33 | backend: WinitGraphicsBackend<GlesRenderer>, |
| 34 | damage_tracker: OutputDamageTracker, |
| 35 | dmabuf_global: Option<DmabufGlobal>, |
| 36 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 37 | } |
| 38 | |
| 39 | impl Winit { |
| 40 | pub fn new( |
| 41 | config: Rc<RefCell<Config>>, |
| 42 | event_loop: LoopHandle<State>, |
| 43 | ) -> Result<Self, winit::Error> { |
| 44 | let _span = tracy_client::span!("Winit::new"); |
| 45 | |
| 46 | let builder = WindowAttributes::default() |
| 47 | .with_surface_size(LogicalSize::new(1280.0, 800.0)) |
| 48 | // .with_resizable(false) |
| 49 | .with_title("niri") |
| 50 | .with_platform_attributes(Box::new( |
| 51 | WindowAttributesWayland::default().with_name("niri", ""), |
| 52 | )); |
| 53 | let (backend, winit) = winit::init_from_attributes(builder)?; |
| 54 | |
| 55 | let output = Output::new( |
| 56 | "winit".to_string(), |
| 57 | PhysicalProperties { |
| 58 | size: (0, 0).into(), |
| 59 | subpixel: Subpixel::Unknown, |
| 60 | make: "Smithay".into(), |
| 61 | model: "Winit".into(), |
| 62 | serial_number: "Unknown".into(), |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | let mode = Mode { |
| 67 | size: backend.window_size(), |
| 68 | refresh: 60_000, |
| 69 | }; |
| 70 | output.change_current_state(Some(mode), None, None, None); |
| 71 | output.set_preferred(mode); |
| 72 | |
| 73 | output.user_data().insert_if_missing(|| OutputName { |
| 74 | connector: "winit".to_string(), |
| 75 | make: Some("Smithay".to_string()), |
| 76 | model: Some("Winit".to_string()), |
| 77 | serial: None, |
| 78 | }); |
| 79 | |
| 80 | let physical_properties = output.physical_properties(); |
| 81 | let ipc_outputs = Arc::new(Mutex::new(HashMap::from([( |
| 82 | OutputId::next(), |
| 83 | niri_ipc::Output { |
| 84 | name: output.name(), |
| 85 | make: physical_properties.make, |
| 86 | model: physical_properties.model, |
| 87 | serial: None, |
| 88 | physical_size: None, |
| 89 | modes: vec![niri_ipc::Mode { |
| 90 | width: backend.window_size().w.clamp(0, u16::MAX as i32) as u16, |
| 91 | height: backend.window_size().h.clamp(0, u16::MAX as i32) as u16, |
| 92 | refresh_rate: 60_000, |
| 93 | is_preferred: true, |
| 94 | }], |
| 95 | current_mode: Some(0), |
| 96 | is_custom_mode: true, |
| 97 | vrr_supported: false, |
| 98 | vrr_enabled: false, |
| 99 | logical: Some(logical_output(&output)), |
| 100 | max_bpc: None, |
| 101 | }, |
| 102 | )]))); |
| 103 | |
| 104 | let damage_tracker = OutputDamageTracker::from_output(&output); |
| 105 | |
| 106 | event_loop |
| 107 | .insert_source(winit, move |event, _, state| match event { |
| 108 | WinitEvent::Resized { size, .. } => { |
| 109 | let winit = state.backend.winit(); |
| 110 | winit.output.change_current_state( |
| 111 | Some(Mode { |
| 112 | size, |
| 113 | refresh: 60_000, |
| 114 | }), |
| 115 | None, |
| 116 | None, |
| 117 | None, |
| 118 | ); |
| 119 | |
| 120 | { |
| 121 | let mut ipc_outputs = winit.ipc_outputs.lock().unwrap(); |
| 122 | let output = ipc_outputs.values_mut().next().unwrap(); |
| 123 | let mode = &mut output.modes[0]; |
| 124 | mode.width = size.w.clamp(0, u16::MAX as i32) as u16; |
| 125 | mode.height = size.h.clamp(0, u16::MAX as i32) as u16; |
| 126 | if let Some(logical) = output.logical.as_mut() { |
| 127 | logical.width = size.w as u32; |
| 128 | logical.height = size.h as u32; |
| 129 | } |
| 130 | state.niri.ipc_outputs_changed = true; |
| 131 | } |
| 132 | |
| 133 | state.niri.output_resized(&winit.output); |
| 134 | } |
| 135 | WinitEvent::Input(event) => state.process_input_event(event), |
| 136 | WinitEvent::Focus(_) => (), |
| 137 | WinitEvent::Redraw => state.niri.queue_redraw(&state.backend.winit().output), |
| 138 | WinitEvent::CloseRequested => state.niri.stop_signal.stop(), |
| 139 | }) |
| 140 | .unwrap(); |
| 141 | |
| 142 | Ok(Self { |
| 143 | config, |
| 144 | output, |
| 145 | backend, |
| 146 | damage_tracker, |
| 147 | dmabuf_global: None, |
| 148 | ipc_outputs, |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | pub fn init(&mut self, niri: &mut Niri) { |
| 153 | let renderer = self.backend.renderer(); |
| 154 | if let Err(err) = renderer.bind_wl_display(&niri.display_handle) { |
| 155 | // wl_drm is on its way out so this is expected on most modern distros. |
| 156 | trace!("error binding legacy EGL to wl_display: {err}"); |
| 157 | } else { |
| 158 | debug!("bound legacy EGL to wl_display"); |
| 159 | } |
| 160 | |
| 161 | resources::init(renderer); |
| 162 | shaders::init(renderer); |
| 163 | |
| 164 | let config = self.config.borrow(); |
| 165 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { |
| 166 | shaders::set_custom_resize_program(renderer, Some(src)); |
| 167 | } |
| 168 | if let Some(src) = config.animations.window_close.custom_shader.as_deref() { |
| 169 | shaders::set_custom_close_program(renderer, Some(src)); |
| 170 | } |
| 171 | if let Some(src) = config.animations.window_open.custom_shader.as_deref() { |
| 172 | shaders::set_custom_open_program(renderer, Some(src)); |
| 173 | } |
| 174 | drop(config); |
| 175 | |
| 176 | niri.update_shaders(); |
| 177 | |
| 178 | self.create_dmabuf_global(niri); |
| 179 | |
| 180 | niri.add_output(self.output.clone(), None, false); |
| 181 | } |
| 182 | |
| 183 | pub fn create_dmabuf_global(&mut self, niri: &mut Niri) { |
| 184 | let renderer = self.backend.renderer(); |
| 185 | |
| 186 | let default_feedback = || { |
| 187 | let display = renderer.egl_context().display(); |
| 188 | let device = |
| 189 | EGLDevice::device_for_display(display).context("error getting EGL device")?; |
| 190 | let node = device |
| 191 | .try_get_render_node() |
| 192 | .context("error getting EGL device render node")? |
| 193 | .context("failed to query EGL device render node")?; |
| 194 | |
| 195 | let primary_formats = renderer.dmabuf_formats(); |
| 196 | DmabufFeedbackBuilder::new(node.dev_id(), primary_formats) |
| 197 | .build() |
| 198 | .context("error building dmabuf feedback") |
| 199 | }; |
| 200 | |
| 201 | // Fallback to dmabuf v3 if we failed to build feedback. |
| 202 | let dmabuf_global = match default_feedback() { |
| 203 | Ok(feedback) => niri |
| 204 | .dmabuf_state |
| 205 | .create_global_with_default_feedback::<State>(&niri.display_handle, &feedback), |
| 206 | Err(err) => { |
| 207 | debug!("failed building default dmabuf feedback, falling back to v3: {err:?}"); |
| 208 | let primary_formats = renderer.dmabuf_formats(); |
| 209 | niri.dmabuf_state |
| 210 | .create_global::<State>(&niri.display_handle, primary_formats) |
| 211 | } |
| 212 | }; |
| 213 | assert!(self.dmabuf_global.replace(dmabuf_global).is_none()); |
| 214 | } |
| 215 | |
| 216 | pub fn seat_name(&self) -> String { |
| 217 | "winit".to_owned() |
| 218 | } |
| 219 | |
| 220 | pub fn with_primary_renderer<T>( |
| 221 | &mut self, |
| 222 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 223 | ) -> Option<T> { |
| 224 | Some(f(self.backend.renderer())) |
| 225 | } |
| 226 | |
| 227 | pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult { |
| 228 | let _span = tracy_client::span!("Winit::render"); |
| 229 | |
| 230 | // Render the elements. |
| 231 | let ctx = RenderCtx { |
| 232 | renderer: self.backend.renderer(), |
| 233 | target: RenderTarget::Output, |
| 234 | xray: None, |
| 235 | }; |
| 236 | let mut elements = niri.render_to_vec(ctx, output, true); |
| 237 | |
| 238 | // Visualize the damage, if enabled. |
| 239 | if niri.debug_draw_damage { |
| 240 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 241 | draw_damage(&mut output_state.debug_damage_tracker, &mut elements); |
| 242 | } |
| 243 | |
| 244 | // Hand them over to winit. |
| 245 | let res = { |
| 246 | let (renderer, mut framebuffer) = self.backend.bind().unwrap(); |
| 247 | // FIXME: currently impossible to call due to a mutable borrow. |
| 248 | // |
| 249 | // let age = self.backend.buffer_age().unwrap(); |
| 250 | let age = 0; |
| 251 | self.damage_tracker |
| 252 | .render_output(renderer, &mut framebuffer, age, &elements, [0.; 4]) |
| 253 | .unwrap() |
| 254 | }; |
| 255 | |
| 256 | niri.update_primary_scanout_output(output, &res.states); |
| 257 | |
| 258 | let rv; |
| 259 | if let Some(damage) = res.damage { |
| 260 | if self |
| 261 | .config |
| 262 | .borrow() |
| 263 | .debug |
| 264 | .wait_for_frame_completion_before_queueing |
| 265 | { |
| 266 | let _span = tracy_client::span!("wait for completion"); |
| 267 | if let Err(err) = res.sync.wait() { |
| 268 | warn!("error waiting for frame completion: {err:?}"); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | self.backend.submit(Some(damage)).unwrap(); |
| 273 | |
| 274 | let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states); |
| 275 | presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( |
| 276 | get_monotonic_time(), |
| 277 | Refresh::Unknown, |
| 278 | 0, |
| 279 | wp_presentation_feedback::Kind::empty(), |
| 280 | ); |
| 281 | |
| 282 | rv = RenderResult::Submitted; |
| 283 | } else { |
| 284 | rv = RenderResult::NoDamage; |
| 285 | } |
| 286 | |
| 287 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 288 | match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { |
| 289 | RedrawState::Idle => unreachable!(), |
| 290 | RedrawState::Queued => (), |
| 291 | RedrawState::WaitingForVBlank { .. } => unreachable!(), |
| 292 | RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(), |
| 293 | RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(), |
| 294 | } |
| 295 | |
| 296 | output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1); |
| 297 | |
| 298 | // FIXME: this should wait until a frame callback from the host compositor, but it redraws |
| 299 | // right away instead. |
| 300 | if output_state.unfinished_animations_remain { |
| 301 | self.backend.window().request_redraw(); |
| 302 | } |
| 303 | |
| 304 | rv |
| 305 | } |
| 306 | |
| 307 | pub fn toggle_debug_tint(&mut self) { |
| 308 | let renderer = self.backend.renderer(); |
| 309 | renderer.set_debug_flags(renderer.debug_flags() ^ DebugFlags::TINT); |
| 310 | } |
| 311 | |
| 312 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool { |
| 313 | match self.backend.renderer().import_dmabuf(dmabuf, None) { |
| 314 | Ok(_texture) => true, |
| 315 | Err(err) => { |
| 316 | debug!("error importing dmabuf: {err:?}"); |
| 317 | false |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 323 | self.ipc_outputs.clone() |
| 324 | } |
| 325 | } |