niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 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 | use std::time::Duration; |
| 7 | |
| 8 | use niri_config::Config; |
| 9 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 10 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 11 | use smithay::backend::renderer::gles::GlesRenderer; |
| 12 | use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer}; |
| 13 | use smithay::backend::winit::{self, WinitEvent, WinitGraphicsBackend}; |
| 14 | use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; |
| 15 | use smithay::reexports::calloop::LoopHandle; |
| 16 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 17 | use smithay::reexports::winit::dpi::LogicalSize; |
| 18 | use smithay::reexports::winit::window::Window; |
| 19 | |
| 20 | use super::{IpcOutputMap, OutputId, RenderResult}; |
| 21 | use crate::niri::{Niri, RedrawState, State}; |
| 22 | use crate::render_helpers::debug::draw_damage; |
| 23 | use crate::render_helpers::{resources, shaders, RenderTarget}; |
| 24 | use crate::utils::{get_monotonic_time, logical_output}; |
| 25 | |
| 26 | pub struct Winit { |
| 27 | config: Rc<RefCell<Config>>, |
| 28 | output: Output, |
| 29 | backend: WinitGraphicsBackend<GlesRenderer>, |
| 30 | damage_tracker: OutputDamageTracker, |
| 31 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 32 | } |
| 33 | |
| 34 | impl Winit { |
| 35 | pub fn new( |
| 36 | config: Rc<RefCell<Config>>, |
| 37 | event_loop: LoopHandle<State>, |
| 38 | ) -> Result<Self, winit::Error> { |
| 39 | let builder = Window::default_attributes() |
| 40 | .with_inner_size(LogicalSize::new(1280.0, 800.0)) |
| 41 | // .with_resizable(false) |
| 42 | .with_title("niri"); |
| 43 | let (backend, winit) = winit::init_from_attributes(builder)?; |
| 44 | |
| 45 | let output = Output::new( |
| 46 | "winit".to_string(), |
| 47 | PhysicalProperties { |
| 48 | size: (0, 0).into(), |
| 49 | subpixel: Subpixel::Unknown, |
| 50 | make: "Smithay".into(), |
| 51 | model: "Winit".into(), |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | let mode = Mode { |
| 56 | size: backend.window_size(), |
| 57 | refresh: 60_000, |
| 58 | }; |
| 59 | output.change_current_state(Some(mode), None, None, None); |
| 60 | output.set_preferred(mode); |
| 61 | |
| 62 | let physical_properties = output.physical_properties(); |
| 63 | let ipc_outputs = Arc::new(Mutex::new(HashMap::from([( |
| 64 | OutputId::next(), |
| 65 | niri_ipc::Output { |
| 66 | name: output.name(), |
| 67 | make: physical_properties.make, |
| 68 | model: physical_properties.model, |
| 69 | physical_size: None, |
| 70 | modes: vec![niri_ipc::Mode { |
| 71 | width: backend.window_size().w.clamp(0, u16::MAX as i32) as u16, |
| 72 | height: backend.window_size().h.clamp(0, u16::MAX as i32) as u16, |
| 73 | refresh_rate: 60_000, |
| 74 | is_preferred: true, |
| 75 | }], |
| 76 | current_mode: Some(0), |
| 77 | vrr_supported: false, |
| 78 | vrr_enabled: false, |
| 79 | logical: Some(logical_output(&output)), |
| 80 | }, |
| 81 | )]))); |
| 82 | |
| 83 | let damage_tracker = OutputDamageTracker::from_output(&output); |
| 84 | |
| 85 | event_loop |
| 86 | .insert_source(winit, move |event, _, state| match event { |
| 87 | WinitEvent::Resized { size, .. } => { |
| 88 | let winit = state.backend.winit(); |
| 89 | winit.output.change_current_state( |
| 90 | Some(Mode { |
| 91 | size, |
| 92 | refresh: 60_000, |
| 93 | }), |
| 94 | None, |
| 95 | None, |
| 96 | None, |
| 97 | ); |
| 98 | |
| 99 | { |
| 100 | let mut ipc_outputs = winit.ipc_outputs.lock().unwrap(); |
| 101 | let output = ipc_outputs.values_mut().next().unwrap(); |
| 102 | let mode = &mut output.modes[0]; |
| 103 | mode.width = size.w.clamp(0, u16::MAX as i32) as u16; |
| 104 | mode.height = size.h.clamp(0, u16::MAX as i32) as u16; |
| 105 | if let Some(logical) = output.logical.as_mut() { |
| 106 | logical.width = size.w as u32; |
| 107 | logical.height = size.h as u32; |
| 108 | } |
| 109 | state.niri.ipc_outputs_changed = true; |
| 110 | } |
| 111 | |
| 112 | state.niri.output_resized(&winit.output); |
| 113 | } |
| 114 | WinitEvent::Input(event) => state.process_input_event(event), |
| 115 | WinitEvent::Focus(_) => (), |
| 116 | WinitEvent::Redraw => state.niri.queue_redraw(&state.backend.winit().output), |
| 117 | WinitEvent::CloseRequested => state.niri.stop_signal.stop(), |
| 118 | }) |
| 119 | .unwrap(); |
| 120 | |
| 121 | Ok(Self { |
| 122 | config, |
| 123 | output, |
| 124 | backend, |
| 125 | damage_tracker, |
| 126 | ipc_outputs, |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | pub fn init(&mut self, niri: &mut Niri) { |
| 131 | let renderer = self.backend.renderer(); |
| 132 | if let Err(err) = renderer.bind_wl_display(&niri.display_handle) { |
| 133 | warn!("error binding renderer wl_display: {err}"); |
| 134 | } |
| 135 | |
| 136 | resources::init(renderer); |
| 137 | shaders::init(renderer); |
| 138 | |
| 139 | let config = self.config.borrow(); |
| 140 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { |
| 141 | shaders::set_custom_resize_program(renderer, Some(src)); |
| 142 | } |
| 143 | if let Some(src) = config.animations.window_close.custom_shader.as_deref() { |
| 144 | shaders::set_custom_close_program(renderer, Some(src)); |
| 145 | } |
| 146 | if let Some(src) = config.animations.window_open.custom_shader.as_deref() { |
| 147 | shaders::set_custom_open_program(renderer, Some(src)); |
| 148 | } |
| 149 | drop(config); |
| 150 | |
| 151 | niri.layout.update_shaders(); |
| 152 | |
| 153 | niri.add_output(self.output.clone(), None, false); |
| 154 | } |
| 155 | |
| 156 | pub fn seat_name(&self) -> String { |
| 157 | "winit".to_owned() |
| 158 | } |
| 159 | |
| 160 | pub fn with_primary_renderer<T>( |
| 161 | &mut self, |
| 162 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 163 | ) -> Option<T> { |
| 164 | Some(f(self.backend.renderer())) |
| 165 | } |
| 166 | |
| 167 | pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult { |
| 168 | let _span = tracy_client::span!("Winit::render"); |
| 169 | |
| 170 | // Render the elements. |
| 171 | let mut elements = niri.render::<GlesRenderer>( |
| 172 | self.backend.renderer(), |
| 173 | output, |
| 174 | true, |
| 175 | RenderTarget::Output, |
| 176 | ); |
| 177 | |
| 178 | // Visualize the damage, if enabled. |
| 179 | if niri.debug_draw_damage { |
| 180 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 181 | draw_damage(&mut output_state.debug_damage_tracker, &mut elements); |
| 182 | } |
| 183 | |
| 184 | // Hand them over to winit. |
| 185 | self.backend.bind().unwrap(); |
| 186 | let age = self.backend.buffer_age().unwrap(); |
| 187 | let res = self |
| 188 | .damage_tracker |
| 189 | .render_output(self.backend.renderer(), age, &elements, [0.; 4]) |
| 190 | .unwrap(); |
| 191 | |
| 192 | niri.update_primary_scanout_output(output, &res.states); |
| 193 | |
| 194 | let rv; |
| 195 | if let Some(damage) = res.damage { |
| 196 | if self |
| 197 | .config |
| 198 | .borrow() |
| 199 | .debug |
| 200 | .wait_for_frame_completion_before_queueing |
| 201 | { |
| 202 | let _span = tracy_client::span!("wait for completion"); |
| 203 | if let Err(err) = res.sync.wait() { |
| 204 | warn!("error waiting for frame completion: {err:?}"); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | self.backend.submit(Some(damage)).unwrap(); |
| 209 | |
| 210 | let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states); |
| 211 | let mode = output.current_mode().unwrap(); |
| 212 | let refresh = Duration::from_secs_f64(1_000f64 / mode.refresh as f64); |
| 213 | presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( |
| 214 | get_monotonic_time(), |
| 215 | refresh, |
| 216 | 0, |
| 217 | wp_presentation_feedback::Kind::empty(), |
| 218 | ); |
| 219 | |
| 220 | rv = RenderResult::Submitted; |
| 221 | } else { |
| 222 | rv = RenderResult::NoDamage; |
| 223 | } |
| 224 | |
| 225 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 226 | match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { |
| 227 | RedrawState::Idle => unreachable!(), |
| 228 | RedrawState::Queued => (), |
| 229 | RedrawState::WaitingForVBlank { .. } => unreachable!(), |
| 230 | RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(), |
| 231 | RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(), |
| 232 | } |
| 233 | |
| 234 | output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1); |
| 235 | |
| 236 | // FIXME: this should wait until a frame callback from the host compositor, but it redraws |
| 237 | // right away instead. |
| 238 | if output_state.unfinished_animations_remain { |
| 239 | self.backend.window().request_redraw(); |
| 240 | } |
| 241 | |
| 242 | rv |
| 243 | } |
| 244 | |
| 245 | pub fn toggle_debug_tint(&mut self) { |
| 246 | let renderer = self.backend.renderer(); |
| 247 | renderer.set_debug_flags(renderer.debug_flags() ^ DebugFlags::TINT); |
| 248 | } |
| 249 | |
| 250 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool { |
| 251 | match self.backend.renderer().import_dmabuf(dmabuf, None) { |
| 252 | Ok(_texture) => true, |
| 253 | Err(err) => { |
| 254 | debug!("error importing dmabuf: {err:?}"); |
| 255 | false |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 261 | self.ipc_outputs.clone() |
| 262 | } |
| 263 | } |