Replace Backend trait with enum
Browse files at this commit5a707c879ba9b21665d2207020fe354040b807b2Parent: 3bd5a015 changed files +154 -129
- src/backend.rs +52 -5
- src/main.rs +6 -17
- src/niri.rs +2 -7
- src/tty.rs +55 -58
- src/winit.rs +39 -42
src/backend.rsmodified+52 -5
| @@ -2,15 +2,62 @@ use smithay::backend::renderer::gles::GlesRenderer; | |||
| 2 | use smithay::output::Output; | 2 | use smithay::output::Output; |
| 3 | 3 | ||
| 4 | use crate::niri::OutputRenderElements; | 4 | use crate::niri::OutputRenderElements; |
| 5 | +use crate::tty::Tty; | ||
| 6 | +use crate::winit::Winit; | ||
| 5 | use crate::Niri; | 7 | use crate::Niri; |
| 6 | 8 | ||
| 7 | -pub trait Backend { | 9 | +pub enum Backend { |
| 8 | - fn seat_name(&self) -> String; | 10 | + Tty(Tty), |
| 9 | - fn renderer(&mut self) -> &mut GlesRenderer; | 11 | + Winit(Winit), |
| 10 | - fn render( | 12 | +} |
| 13 | + | ||
| 14 | +impl Backend { | ||
| 15 | + pub fn init(&mut self, niri: &mut Niri) { | ||
| 16 | + match self { | ||
| 17 | + Backend::Tty(tty) => tty.init(niri), | ||
| 18 | + Backend::Winit(winit) => winit.init(niri), | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + pub fn seat_name(&self) -> String { | ||
| 23 | + match self { | ||
| 24 | + Backend::Tty(tty) => tty.seat_name(), | ||
| 25 | + Backend::Winit(winit) => winit.seat_name(), | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + pub fn renderer(&mut self) -> &mut GlesRenderer { | ||
| 30 | + match self { | ||
| 31 | + Backend::Tty(tty) => tty.renderer(), | ||
| 32 | + Backend::Winit(winit) => winit.renderer(), | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + pub fn render( | ||
| 11 | &mut self, | 37 | &mut self, |
| 12 | niri: &mut Niri, | 38 | niri: &mut Niri, |
| 13 | output: &Output, | 39 | output: &Output, |
| 14 | elements: &[OutputRenderElements<GlesRenderer>], | 40 | elements: &[OutputRenderElements<GlesRenderer>], |
| 15 | - ); | 41 | + ) { |
| 42 | + match self { | ||
| 43 | + Backend::Tty(tty) => tty.render(niri, output, elements), | ||
| 44 | + Backend::Winit(winit) => winit.render(niri, output, elements), | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + pub fn tty(&mut self) -> Option<&mut Tty> { | ||
| 49 | + if let Self::Tty(v) = self { | ||
| 50 | + Some(v) | ||
| 51 | + } else { | ||
| 52 | + None | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + pub fn winit(&mut self) -> Option<&mut Winit> { | ||
| 57 | + if let Self::Winit(v) = self { | ||
| 58 | + Some(v) | ||
| 59 | + } else { | ||
| 60 | + None | ||
| 61 | + } | ||
| 62 | + } | ||
| 16 | } | 63 | } |
src/main.rsmodified+6 -17
| @@ -38,8 +38,7 @@ pub struct LoopData { | |||
| 38 | niri: Niri, | 38 | niri: Niri, |
| 39 | display_handle: DisplayHandle, | 39 | display_handle: DisplayHandle, |
| 40 | 40 | ||
| 41 | - tty: Option<Tty>, | 41 | + backend: Backend, |
| 42 | - winit: Option<Winit>, | ||
| 43 | 42 | ||
| 44 | // Last so that it's dropped after the Smithay state in Niri and related state in Tty. | 43 | // Last so that it's dropped after the Smithay state in Niri and related state in Tty. |
| 45 | // Otherwise it will segfault on quit. | 44 | // Otherwise it will segfault on quit. |
| @@ -64,14 +63,10 @@ fn main() { | |||
| 64 | 63 | ||
| 65 | let has_display = env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some(); | 64 | let has_display = env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some(); |
| 66 | 65 | ||
| 67 | - let mut winit = None; | 66 | + let backend = if has_display { |
| 68 | - let mut tty = None; | 67 | + Backend::Winit(Winit::new(event_loop.handle())) |
| 69 | - let backend: &mut dyn Backend = if has_display { | ||
| 70 | - winit = Some(Winit::new(event_loop.handle())); | ||
| 71 | - winit.as_mut().unwrap() | ||
| 72 | } else { | 68 | } else { |
| 73 | - tty = Some(Tty::new(event_loop.handle())); | 69 | + Backend::Tty(Tty::new(event_loop.handle())) |
| 74 | - tty.as_mut().unwrap() | ||
| 75 | }; | 70 | }; |
| 76 | 71 | ||
| 77 | let mut display = Display::new().unwrap(); | 72 | let mut display = Display::new().unwrap(); |
| @@ -88,16 +83,10 @@ fn main() { | |||
| 88 | display_handle, | 83 | display_handle, |
| 89 | display, | 84 | display, |
| 90 | 85 | ||
| 91 | - tty, | 86 | + backend, |
| 92 | - winit, | ||
| 93 | }; | 87 | }; |
| 94 | 88 | ||
| 95 | - if let Some(tty) = data.tty.as_mut() { | 89 | + data.backend.init(&mut data.niri); |
| 96 | - tty.init(&mut data.niri); | ||
| 97 | - } | ||
| 98 | - if let Some(winit) = data.winit.as_mut() { | ||
| 99 | - winit.init(&mut data.niri); | ||
| 100 | - } | ||
| 101 | 90 | ||
| 102 | if let Some((command, args)) = cli.command.split_first() { | 91 | if let Some((command, args)) = cli.command.split_first() { |
| 103 | if let Err(err) = std::process::Command::new(command).args(args).spawn() { | 92 | if let Err(err) = std::process::Command::new(command).args(args).spawn() { |
src/niri.rsmodified+2 -7
| @@ -512,12 +512,7 @@ impl Niri { | |||
| 512 | // Timer::immediate() adds a millisecond of delay for some reason. | 512 | // Timer::immediate() adds a millisecond of delay for some reason. |
| 513 | // This should be fixed in calloop v0.11: https://github.com/Smithay/calloop/issues/142 | 513 | // This should be fixed in calloop v0.11: https://github.com/Smithay/calloop/issues/142 |
| 514 | let idle = self.event_loop.insert_idle(move |data| { | 514 | let idle = self.event_loop.insert_idle(move |data| { |
| 515 | - let backend: &mut dyn Backend = if let Some(tty) = &mut data.tty { | 515 | + data.niri.redraw(&mut data.backend, &output); |
| 516 | - tty | ||
| 517 | - } else { | ||
| 518 | - data.winit.as_mut().unwrap() | ||
| 519 | - }; | ||
| 520 | - data.niri.redraw(backend, &output); | ||
| 521 | }); | 516 | }); |
| 522 | state.queued_redraw = Some(idle); | 517 | state.queued_redraw = Some(idle); |
| 523 | } | 518 | } |
| @@ -655,7 +650,7 @@ impl Niri { | |||
| 655 | elements | 650 | elements |
| 656 | } | 651 | } |
| 657 | 652 | ||
| 658 | - fn redraw(&mut self, backend: &mut dyn Backend, output: &Output) { | 653 | + fn redraw(&mut self, backend: &mut Backend, output: &Output) { |
| 659 | let _span = tracy_client::span!("Niri::redraw"); | 654 | let _span = tracy_client::span!("Niri::redraw"); |
| 660 | 655 | ||
| 661 | let state = self.output_state.get_mut(output).unwrap(); | 656 | let state = self.output_state.get_mut(output).unwrap(); |
src/tty.rsmodified+55 -58
| @@ -30,7 +30,6 @@ use smithay::utils::DeviceFd; | |||
| 30 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; | 30 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; |
| 31 | use smithay_drm_extras::edid::EdidInfo; | 31 | use smithay_drm_extras::edid::EdidInfo; |
| 32 | 32 | ||
| 33 | -use crate::backend::Backend; | ||
| 34 | use crate::input::{BackendAction, CompositorMod}; | 33 | use crate::input::{BackendAction, CompositorMod}; |
| 35 | use crate::niri::OutputRenderElements; | 34 | use crate::niri::OutputRenderElements; |
| 36 | use crate::{LoopData, Niri}; | 35 | use crate::{LoopData, Niri}; |
| @@ -69,59 +68,6 @@ struct TtyOutputState { | |||
| 69 | crtc: crtc::Handle, | 68 | crtc: crtc::Handle, |
| 70 | } | 69 | } |
| 71 | 70 | ||
| 72 | -impl Backend for Tty { | ||
| 73 | - fn seat_name(&self) -> String { | ||
| 74 | - self.session.seat() | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - fn renderer(&mut self) -> &mut GlesRenderer { | ||
| 78 | - &mut self.output_device.as_mut().unwrap().gles | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - fn render( | ||
| 82 | - &mut self, | ||
| 83 | - niri: &mut Niri, | ||
| 84 | - output: &Output, | ||
| 85 | - elements: &[OutputRenderElements<GlesRenderer>], | ||
| 86 | - ) { | ||
| 87 | - let _span = tracy_client::span!("Tty::render"); | ||
| 88 | - | ||
| 89 | - let device = self.output_device.as_mut().unwrap(); | ||
| 90 | - let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | ||
| 91 | - let drm_compositor = device.surfaces.get_mut(&tty_state.crtc).unwrap(); | ||
| 92 | - | ||
| 93 | - match drm_compositor.render_frame::<_, _, GlesTexture>( | ||
| 94 | - &mut device.gles, | ||
| 95 | - elements, | ||
| 96 | - BACKGROUND_COLOR, | ||
| 97 | - ) { | ||
| 98 | - Ok(res) => { | ||
| 99 | - assert!(!res.needs_sync()); | ||
| 100 | - if res.damage.is_some() { | ||
| 101 | - let presentation_feedbacks = | ||
| 102 | - niri.take_presentation_feedbacks(output, &res.states); | ||
| 103 | - | ||
| 104 | - match drm_compositor.queue_frame(presentation_feedbacks) { | ||
| 105 | - Ok(()) => { | ||
| 106 | - niri.output_state | ||
| 107 | - .get_mut(output) | ||
| 108 | - .unwrap() | ||
| 109 | - .waiting_for_vblank = true | ||
| 110 | - } | ||
| 111 | - Err(err) => { | ||
| 112 | - error!("error queueing frame: {err}"); | ||
| 113 | - } | ||
| 114 | - } | ||
| 115 | - } | ||
| 116 | - } | ||
| 117 | - Err(err) => { | ||
| 118 | - // Can fail if we switched to a different TTY. | ||
| 119 | - error!("error rendering frame: {err}"); | ||
| 120 | - } | ||
| 121 | - } | ||
| 122 | - } | ||
| 123 | -} | ||
| 124 | - | ||
| 125 | impl Tty { | 71 | impl Tty { |
| 126 | pub fn new(event_loop: LoopHandle<'static, LoopData>) -> Self { | 72 | pub fn new(event_loop: LoopHandle<'static, LoopData>) -> Self { |
| 127 | let (session, notifier) = LibSeatSession::new().unwrap(); | 73 | let (session, notifier) = LibSeatSession::new().unwrap(); |
| @@ -130,7 +76,7 @@ impl Tty { | |||
| 130 | let udev_backend = UdevBackend::new(session.seat()).unwrap(); | 76 | let udev_backend = UdevBackend::new(session.seat()).unwrap(); |
| 131 | let udev_dispatcher = | 77 | let udev_dispatcher = |
| 132 | Dispatcher::new(udev_backend, move |event, _, data: &mut LoopData| { | 78 | Dispatcher::new(udev_backend, move |event, _, data: &mut LoopData| { |
| 133 | - let tty = data.tty.as_mut().unwrap(); | 79 | + let tty = data.backend.tty().unwrap(); |
| 134 | let niri = &mut data.niri; | 80 | let niri = &mut data.niri; |
| 135 | 81 | ||
| 136 | match event { | 82 | match event { |
| @@ -172,7 +118,7 @@ impl Tty { | |||
| 172 | let input_backend = LibinputInputBackend::new(libinput.clone()); | 118 | let input_backend = LibinputInputBackend::new(libinput.clone()); |
| 173 | event_loop | 119 | event_loop |
| 174 | .insert_source(input_backend, |mut event, _, data| { | 120 | .insert_source(input_backend, |mut event, _, data| { |
| 175 | - let tty = data.tty.as_mut().unwrap(); | 121 | + let tty = data.backend.tty().unwrap(); |
| 176 | let niri = &mut data.niri; | 122 | let niri = &mut data.niri; |
| 177 | 123 | ||
| 178 | niri.process_libinput_event(&mut event); | 124 | niri.process_libinput_event(&mut event); |
| @@ -207,7 +153,7 @@ impl Tty { | |||
| 207 | let udev_dispatcher_c = udev_dispatcher.clone(); | 153 | let udev_dispatcher_c = udev_dispatcher.clone(); |
| 208 | event_loop | 154 | event_loop |
| 209 | .insert_source(notifier, move |event, _, data| { | 155 | .insert_source(notifier, move |event, _, data| { |
| 210 | - let tty = data.tty.as_mut().unwrap(); | 156 | + let tty = data.backend.tty().unwrap(); |
| 211 | let niri = &mut data.niri; | 157 | let niri = &mut data.niri; |
| 212 | 158 | ||
| 213 | match event { | 159 | match event { |
| @@ -344,7 +290,7 @@ impl Tty { | |||
| 344 | let token = niri | 290 | let token = niri |
| 345 | .event_loop | 291 | .event_loop |
| 346 | .insert_source(drm_notifier, move |event, metadata, data| { | 292 | .insert_source(drm_notifier, move |event, metadata, data| { |
| 347 | - let tty = data.tty.as_mut().unwrap(); | 293 | + let tty = data.backend.tty().unwrap(); |
| 348 | match event { | 294 | match event { |
| 349 | DrmEvent::VBlank(crtc) => { | 295 | DrmEvent::VBlank(crtc) => { |
| 350 | tracy_client::Client::running() | 296 | tracy_client::Client::running() |
| @@ -593,6 +539,57 @@ impl Tty { | |||
| 593 | niri.remove_output(&output); | 539 | niri.remove_output(&output); |
| 594 | } | 540 | } |
| 595 | 541 | ||
| 542 | + pub fn seat_name(&self) -> String { | ||
| 543 | + self.session.seat() | ||
| 544 | + } | ||
| 545 | + | ||
| 546 | + pub fn renderer(&mut self) -> &mut GlesRenderer { | ||
| 547 | + &mut self.output_device.as_mut().unwrap().gles | ||
| 548 | + } | ||
| 549 | + | ||
| 550 | + pub fn render( | ||
| 551 | + &mut self, | ||
| 552 | + niri: &mut Niri, | ||
| 553 | + output: &Output, | ||
| 554 | + elements: &[OutputRenderElements<GlesRenderer>], | ||
| 555 | + ) { | ||
| 556 | + let _span = tracy_client::span!("Tty::render"); | ||
| 557 | + | ||
| 558 | + let device = self.output_device.as_mut().unwrap(); | ||
| 559 | + let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | ||
| 560 | + let drm_compositor = device.surfaces.get_mut(&tty_state.crtc).unwrap(); | ||
| 561 | + | ||
| 562 | + match drm_compositor.render_frame::<_, _, GlesTexture>( | ||
| 563 | + &mut device.gles, | ||
| 564 | + elements, | ||
| 565 | + BACKGROUND_COLOR, | ||
| 566 | + ) { | ||
| 567 | + Ok(res) => { | ||
| 568 | + assert!(!res.needs_sync()); | ||
| 569 | + if res.damage.is_some() { | ||
| 570 | + let presentation_feedbacks = | ||
| 571 | + niri.take_presentation_feedbacks(output, &res.states); | ||
| 572 | + | ||
| 573 | + match drm_compositor.queue_frame(presentation_feedbacks) { | ||
| 574 | + Ok(()) => { | ||
| 575 | + niri.output_state | ||
| 576 | + .get_mut(output) | ||
| 577 | + .unwrap() | ||
| 578 | + .waiting_for_vblank = true | ||
| 579 | + } | ||
| 580 | + Err(err) => { | ||
| 581 | + error!("error queueing frame: {err}"); | ||
| 582 | + } | ||
| 583 | + } | ||
| 584 | + } | ||
| 585 | + } | ||
| 586 | + Err(err) => { | ||
| 587 | + // Can fail if we switched to a different TTY. | ||
| 588 | + error!("error rendering frame: {err}"); | ||
| 589 | + } | ||
| 590 | + } | ||
| 591 | + } | ||
| 592 | + | ||
| 596 | fn change_vt(&mut self, vt: i32) { | 593 | fn change_vt(&mut self, vt: i32) { |
| 597 | if let Err(err) = self.session.change_vt(vt) { | 594 | if let Err(err) = self.session.change_vt(vt) { |
| 598 | error!("error changing VT: {err}"); | 595 | error!("error changing VT: {err}"); |
src/winit.rsmodified+39 -42
| @@ -12,7 +12,6 @@ use smithay::reexports::winit::dpi::LogicalSize; | |||
| 12 | use smithay::reexports::winit::window::WindowBuilder; | 12 | use smithay::reexports::winit::window::WindowBuilder; |
| 13 | use smithay::utils::Transform; | 13 | use smithay::utils::Transform; |
| 14 | 14 | ||
| 15 | -use crate::backend::Backend; | ||
| 16 | use crate::input::{BackendAction, CompositorMod}; | 15 | use crate::input::{BackendAction, CompositorMod}; |
| 17 | use crate::niri::OutputRenderElements; | 16 | use crate::niri::OutputRenderElements; |
| 18 | use crate::utils::get_monotonic_time; | 17 | use crate::utils::get_monotonic_time; |
| @@ -25,46 +24,6 @@ pub struct Winit { | |||
| 25 | damage_tracker: OutputDamageTracker, | 24 | damage_tracker: OutputDamageTracker, |
| 26 | } | 25 | } |
| 27 | 26 | ||
| 28 | -impl Backend for Winit { | ||
| 29 | - fn seat_name(&self) -> String { | ||
| 30 | - "winit".to_owned() | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - fn renderer(&mut self) -> &mut GlesRenderer { | ||
| 34 | - self.backend.renderer() | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - fn render( | ||
| 38 | - &mut self, | ||
| 39 | - niri: &mut Niri, | ||
| 40 | - output: &Output, | ||
| 41 | - elements: &[OutputRenderElements<GlesRenderer>], | ||
| 42 | - ) { | ||
| 43 | - let _span = tracy_client::span!("Winit::render"); | ||
| 44 | - | ||
| 45 | - self.backend.bind().unwrap(); | ||
| 46 | - let age = self.backend.buffer_age().unwrap(); | ||
| 47 | - let res = self | ||
| 48 | - .damage_tracker | ||
| 49 | - .render_output(self.backend.renderer(), age, elements, [0.1, 0.1, 0.1, 1.0]) | ||
| 50 | - .unwrap(); | ||
| 51 | - if let Some(damage) = res.damage { | ||
| 52 | - self.backend.submit(Some(&damage)).unwrap(); | ||
| 53 | - | ||
| 54 | - let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states); | ||
| 55 | - let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 56 | - presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( | ||
| 57 | - get_monotonic_time(), | ||
| 58 | - refresh, | ||
| 59 | - 0, | ||
| 60 | - wp_presentation_feedback::Kind::empty(), | ||
| 61 | - ); | ||
| 62 | - | ||
| 63 | - self.backend.window().request_redraw(); | ||
| 64 | - } | ||
| 65 | - } | ||
| 66 | -} | ||
| 67 | - | ||
| 68 | impl Winit { | 27 | impl Winit { |
| 69 | pub fn new(event_loop: LoopHandle<LoopData>) -> Self { | 28 | pub fn new(event_loop: LoopHandle<LoopData>) -> Self { |
| 70 | let builder = WindowBuilder::new() | 29 | let builder = WindowBuilder::new() |
| @@ -100,7 +59,7 @@ impl Winit { | |||
| 100 | let timer = Timer::immediate(); | 59 | let timer = Timer::immediate(); |
| 101 | event_loop | 60 | event_loop |
| 102 | .insert_source(timer, move |_, _, data| { | 61 | .insert_source(timer, move |_, _, data| { |
| 103 | - let winit = data.winit.as_mut().unwrap(); | 62 | + let winit = data.backend.winit().unwrap(); |
| 104 | winit.dispatch(&mut data.niri); | 63 | winit.dispatch(&mut data.niri); |
| 105 | TimeoutAction::ToDuration(Duration::from_micros(16667)) | 64 | TimeoutAction::ToDuration(Duration::from_micros(16667)) |
| 106 | }) | 65 | }) |
| @@ -178,4 +137,42 @@ impl Winit { | |||
| 178 | Ok(()) => (), | 137 | Ok(()) => (), |
| 179 | } | 138 | } |
| 180 | } | 139 | } |
| 140 | + | ||
| 141 | + pub fn seat_name(&self) -> String { | ||
| 142 | + "winit".to_owned() | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + pub fn renderer(&mut self) -> &mut GlesRenderer { | ||
| 146 | + self.backend.renderer() | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + pub fn render( | ||
| 150 | + &mut self, | ||
| 151 | + niri: &mut Niri, | ||
| 152 | + output: &Output, | ||
| 153 | + elements: &[OutputRenderElements<GlesRenderer>], | ||
| 154 | + ) { | ||
| 155 | + let _span = tracy_client::span!("Winit::render"); | ||
| 156 | + | ||
| 157 | + self.backend.bind().unwrap(); | ||
| 158 | + let age = self.backend.buffer_age().unwrap(); | ||
| 159 | + let res = self | ||
| 160 | + .damage_tracker | ||
| 161 | + .render_output(self.backend.renderer(), age, elements, [0.1, 0.1, 0.1, 1.0]) | ||
| 162 | + .unwrap(); | ||
| 163 | + if let Some(damage) = res.damage { | ||
| 164 | + self.backend.submit(Some(&damage)).unwrap(); | ||
| 165 | + | ||
| 166 | + let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states); | ||
| 167 | + let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 168 | + presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( | ||
| 169 | + get_monotonic_time(), | ||
| 170 | + refresh, | ||
| 171 | + 0, | ||
| 172 | + wp_presentation_feedback::Kind::empty(), | ||
| 173 | + ); | ||
| 174 | + | ||
| 175 | + self.backend.window().request_redraw(); | ||
| 176 | + } | ||
| 177 | + } | ||
| 181 | } | 178 | } |