| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::mem; |
| 7 | use std::sync::{Arc, Mutex}; |
| 8 | |
| 9 | use anyhow::Context as _; |
| 10 | use niri_config::OutputName; |
| 11 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 12 | use smithay::backend::egl::native::EGLSurfacelessDisplay; |
| 13 | use smithay::backend::egl::{EGLContext, EGLDisplay}; |
| 14 | use smithay::backend::renderer::element::RenderElementStates; |
| 15 | use smithay::backend::renderer::gles::GlesRenderer; |
| 16 | use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; |
| 17 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 18 | use smithay::utils::Size; |
| 19 | use smithay::wayland::presentation::Refresh; |
| 20 | |
| 21 | use super::{IpcOutputMap, OutputId, RenderResult}; |
| 22 | use crate::niri::{Niri, RedrawState}; |
| 23 | use crate::render_helpers::{resources, shaders}; |
| 24 | use crate::utils::{get_monotonic_time, logical_output}; |
| 25 | |
| 26 | pub struct Headless { |
| 27 | renderer: Option<GlesRenderer>, |
| 28 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 29 | } |
| 30 | |
| 31 | impl Headless { |
| 32 | pub fn new() -> Self { |
| 33 | Self { |
| 34 | renderer: None, |
| 35 | ipc_outputs: Default::default(), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn init(&mut self, _niri: &mut Niri) {} |
| 40 | |
| 41 | pub fn add_renderer(&mut self) -> anyhow::Result<()> { |
| 42 | if self.renderer.is_some() { |
| 43 | error!("add_renderer: renderer must not already exist"); |
| 44 | return Ok(()); |
| 45 | } |
| 46 | |
| 47 | let mut renderer = unsafe { |
| 48 | let display = |
| 49 | EGLDisplay::new(EGLSurfacelessDisplay).context("error creating EGL display")?; |
| 50 | let context = EGLContext::new(&display).context("error creating EGL context")?; |
| 51 | GlesRenderer::new(context).context("error creating renderer")? |
| 52 | }; |
| 53 | |
| 54 | resources::init(&mut renderer); |
| 55 | shaders::init(&mut renderer); |
| 56 | |
| 57 | self.renderer = Some(renderer); |
| 58 | Ok(()) |
| 59 | } |
| 60 | |
| 61 | pub fn add_output(&mut self, niri: &mut Niri, n: u8, size: (u16, u16)) { |
| 62 | let connector = format!("headless-{n}"); |
| 63 | let make = "niri".to_string(); |
| 64 | let model = "headless".to_string(); |
| 65 | let serial = n.to_string(); |
| 66 | |
| 67 | let output = Output::new( |
| 68 | connector.clone(), |
| 69 | PhysicalProperties { |
| 70 | size: (0, 0).into(), |
| 71 | subpixel: Subpixel::Unknown, |
| 72 | make: make.clone(), |
| 73 | model: model.clone(), |
| 74 | serial_number: serial.clone(), |
| 75 | }, |
| 76 | ); |
| 77 | |
| 78 | let mode = Mode { |
| 79 | size: Size::from((i32::from(size.0), i32::from(size.1))), |
| 80 | refresh: 60_000, |
| 81 | }; |
| 82 | output.change_current_state(Some(mode), None, None, None); |
| 83 | output.set_preferred(mode); |
| 84 | |
| 85 | output.user_data().insert_if_missing(|| OutputName { |
| 86 | connector, |
| 87 | make: Some(make), |
| 88 | model: Some(model), |
| 89 | serial: Some(serial), |
| 90 | }); |
| 91 | |
| 92 | let physical_properties = output.physical_properties(); |
| 93 | self.ipc_outputs.lock().unwrap().insert( |
| 94 | OutputId::next(), |
| 95 | niri_ipc::Output { |
| 96 | name: output.name(), |
| 97 | make: physical_properties.make, |
| 98 | model: physical_properties.model, |
| 99 | serial: None, |
| 100 | physical_size: None, |
| 101 | modes: vec![niri_ipc::Mode { |
| 102 | width: size.0, |
| 103 | height: size.1, |
| 104 | refresh_rate: 60_000, |
| 105 | is_preferred: true, |
| 106 | }], |
| 107 | current_mode: Some(0), |
| 108 | is_custom_mode: true, |
| 109 | vrr_supported: false, |
| 110 | vrr_enabled: false, |
| 111 | logical: Some(logical_output(&output)), |
| 112 | max_bpc: None, |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | niri.add_output(output, None, false); |
| 117 | } |
| 118 | |
| 119 | pub fn seat_name(&self) -> String { |
| 120 | "headless".to_owned() |
| 121 | } |
| 122 | |
| 123 | pub fn with_primary_renderer<T>( |
| 124 | &mut self, |
| 125 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 126 | ) -> Option<T> { |
| 127 | self.renderer.as_mut().map(f) |
| 128 | } |
| 129 | |
| 130 | pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult { |
| 131 | let states = RenderElementStates::default(); |
| 132 | let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &states); |
| 133 | presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( |
| 134 | get_monotonic_time(), |
| 135 | Refresh::Unknown, |
| 136 | 0, |
| 137 | wp_presentation_feedback::Kind::empty(), |
| 138 | ); |
| 139 | |
| 140 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 141 | match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { |
| 142 | RedrawState::Idle => unreachable!(), |
| 143 | RedrawState::Queued => (), |
| 144 | RedrawState::WaitingForVBlank { .. } => unreachable!(), |
| 145 | RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(), |
| 146 | RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(), |
| 147 | } |
| 148 | |
| 149 | output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1); |
| 150 | |
| 151 | |
| 152 | |
| 153 | RenderResult::Submitted |
| 154 | } |
| 155 | |
| 156 | pub fn import_dmabuf(&mut self, _dmabuf: &Dmabuf) -> bool { |
| 157 | unimplemented!() |
| 158 | } |
| 159 | |
| 160 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 161 | self.ipc_outputs.clone() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl Default for Headless { |
| 166 | fn default() -> Self { |
| 167 | Self::new() |
| 168 | } |
| 169 | } |