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