2023-09-14 22:28:26 +04:00
|
|
|
use std::cell::RefCell;
|
2023-09-08 17:54:02 +04:00
|
|
|
use std::collections::HashMap;
|
2023-09-30 17:13:56 +04:00
|
|
|
use std::mem;
|
2023-09-14 22:28:26 +04:00
|
|
|
use std::rc::Rc;
|
2023-09-08 17:54:02 +04:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2026-04-18 22:43:11 -07:00
|
|
|
use anyhow::Context as _;
|
2024-09-03 12:13:04 +03:00
|
|
|
use niri_config::{Config, OutputName};
|
2024-01-03 11:16:47 +04:00
|
|
|
use smithay::backend::allocator::dmabuf::Dmabuf;
|
2026-04-18 22:43:11 -07:00
|
|
|
use smithay::backend::egl::EGLDevice;
|
2023-08-07 19:45:55 +04:00
|
|
|
use smithay::backend::renderer::damage::OutputDamageTracker;
|
|
|
|
|
use smithay::backend::renderer::gles::GlesRenderer;
|
2024-01-03 11:16:47 +04:00
|
|
|
use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer};
|
2023-10-17 16:53:04 +04:00
|
|
|
use smithay::backend::winit::{self, WinitEvent, WinitGraphicsBackend};
|
2024-01-16 09:42:31 +04:00
|
|
|
use smithay::output::{Mode, Output, PhysicalProperties, Subpixel};
|
2023-08-09 11:03:38 +04:00
|
|
|
use smithay::reexports::calloop::LoopHandle;
|
2023-08-16 10:59:34 +04:00
|
|
|
use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback;
|
2023-08-13 12:46:53 +04:00
|
|
|
use smithay::reexports::winit::dpi::LogicalSize;
|
2026-02-27 16:03:42 +01:00
|
|
|
use smithay::reexports::winit::platform::wayland::WindowAttributesExtWayland;
|
2024-06-21 07:53:41 +03:00
|
|
|
use smithay::reexports::winit::window::Window;
|
2026-04-18 22:43:11 -07:00
|
|
|
use smithay::wayland::dmabuf::{DmabufFeedbackBuilder, DmabufGlobal};
|
2024-12-13 10:28:25 +03:00
|
|
|
use smithay::wayland::presentation::Refresh;
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2024-07-04 13:49:33 +04:00
|
|
|
use super::{IpcOutputMap, OutputId, RenderResult};
|
2024-02-05 17:22:03 +04:00
|
|
|
use crate::niri::{Niri, RedrawState, State};
|
2024-05-03 10:25:51 +04:00
|
|
|
use crate::render_helpers::debug::draw_damage;
|
2026-01-30 08:44:32 +03:00
|
|
|
use crate::render_helpers::{resources, shaders, RenderCtx, RenderTarget};
|
2024-03-27 14:54:24 +04:00
|
|
|
use crate::utils::{get_monotonic_time, logical_output};
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
pub struct Winit {
|
2023-09-14 22:28:26 +04:00
|
|
|
config: Rc<RefCell<Config>>,
|
2023-08-09 11:03:38 +04:00
|
|
|
output: Output,
|
|
|
|
|
backend: WinitGraphicsBackend<GlesRenderer>,
|
|
|
|
|
damage_tracker: OutputDamageTracker,
|
2026-04-18 22:43:11 -07:00
|
|
|
dmabuf_global: Option<DmabufGlobal>,
|
2024-03-27 09:46:18 +04:00
|
|
|
ipc_outputs: Arc<Mutex<IpcOutputMap>>,
|
2023-08-07 19:44:40 +04:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
impl Winit {
|
2024-02-01 16:55:46 +04:00
|
|
|
pub fn new(
|
|
|
|
|
config: Rc<RefCell<Config>>,
|
|
|
|
|
event_loop: LoopHandle<State>,
|
|
|
|
|
) -> Result<Self, winit::Error> {
|
2025-11-18 21:25:35 +03:00
|
|
|
let _span = tracy_client::span!("Winit::new");
|
|
|
|
|
|
2024-06-21 07:53:41 +03:00
|
|
|
let builder = Window::default_attributes()
|
2023-08-13 12:46:53 +04:00
|
|
|
.with_inner_size(LogicalSize::new(1280.0, 800.0))
|
|
|
|
|
// .with_resizable(false)
|
2026-02-27 16:03:42 +01:00
|
|
|
.with_title("niri")
|
|
|
|
|
.with_name("niri", "");
|
2024-06-21 07:53:41 +03:00
|
|
|
let (backend, winit) = winit::init_from_attributes(builder)?;
|
2023-08-09 11:03:38 +04:00
|
|
|
|
|
|
|
|
let output = Output::new(
|
|
|
|
|
"winit".to_string(),
|
|
|
|
|
PhysicalProperties {
|
|
|
|
|
size: (0, 0).into(),
|
|
|
|
|
subpixel: Subpixel::Unknown,
|
|
|
|
|
make: "Smithay".into(),
|
|
|
|
|
model: "Winit".into(),
|
2025-08-11 09:02:28 +03:00
|
|
|
serial_number: "Unknown".into(),
|
2023-08-09 11:03:38 +04:00
|
|
|
},
|
|
|
|
|
);
|
2023-10-26 00:15:46 +04:00
|
|
|
|
|
|
|
|
let mode = Mode {
|
|
|
|
|
size: backend.window_size(),
|
|
|
|
|
refresh: 60_000,
|
|
|
|
|
};
|
2024-01-29 10:17:26 +04:00
|
|
|
output.change_current_state(Some(mode), None, None, None);
|
2023-08-09 11:03:38 +04:00
|
|
|
output.set_preferred(mode);
|
|
|
|
|
|
2024-09-03 12:13:04 +03:00
|
|
|
output.user_data().insert_if_missing(|| OutputName {
|
|
|
|
|
connector: "winit".to_string(),
|
|
|
|
|
make: Some("Smithay".to_string()),
|
|
|
|
|
model: Some("Winit".to_string()),
|
|
|
|
|
serial: None,
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-17 10:38:32 +04:00
|
|
|
let physical_properties = output.physical_properties();
|
2024-03-27 08:27:14 +04:00
|
|
|
let ipc_outputs = Arc::new(Mutex::new(HashMap::from([(
|
2024-07-04 13:49:33 +04:00
|
|
|
OutputId::next(),
|
2024-03-27 14:54:24 +04:00
|
|
|
niri_ipc::Output {
|
|
|
|
|
name: output.name(),
|
|
|
|
|
make: physical_properties.make,
|
|
|
|
|
model: physical_properties.model,
|
2024-09-03 12:13:04 +03:00
|
|
|
serial: None,
|
2024-03-27 14:54:24 +04:00
|
|
|
physical_size: None,
|
|
|
|
|
modes: vec![niri_ipc::Mode {
|
|
|
|
|
width: backend.window_size().w.clamp(0, u16::MAX as i32) as u16,
|
|
|
|
|
height: backend.window_size().h.clamp(0, u16::MAX as i32) as u16,
|
|
|
|
|
refresh_rate: 60_000,
|
|
|
|
|
is_preferred: true,
|
|
|
|
|
}],
|
|
|
|
|
current_mode: Some(0),
|
2025-10-29 07:10:38 +01:00
|
|
|
is_custom_mode: true,
|
2024-04-15 22:29:25 +04:00
|
|
|
vrr_supported: false,
|
|
|
|
|
vrr_enabled: false,
|
2024-03-27 14:54:24 +04:00
|
|
|
logical: Some(logical_output(&output)),
|
|
|
|
|
},
|
2023-09-08 17:54:02 +04:00
|
|
|
)])));
|
|
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
let damage_tracker = OutputDamageTracker::from_output(&output);
|
|
|
|
|
|
|
|
|
|
event_loop
|
2023-10-17 16:53:04 +04:00
|
|
|
.insert_source(winit, move |event, _, state| match event {
|
|
|
|
|
WinitEvent::Resized { size, .. } => {
|
|
|
|
|
let winit = state.backend.winit();
|
|
|
|
|
winit.output.change_current_state(
|
|
|
|
|
Some(Mode {
|
|
|
|
|
size,
|
|
|
|
|
refresh: 60_000,
|
|
|
|
|
}),
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
);
|
2024-01-17 10:38:32 +04:00
|
|
|
|
2024-03-27 08:27:14 +04:00
|
|
|
{
|
|
|
|
|
let mut ipc_outputs = winit.ipc_outputs.lock().unwrap();
|
2024-07-04 13:49:33 +04:00
|
|
|
let output = ipc_outputs.values_mut().next().unwrap();
|
2024-03-27 14:54:24 +04:00
|
|
|
let mode = &mut output.modes[0];
|
2024-03-27 08:27:14 +04:00
|
|
|
mode.width = size.w.clamp(0, u16::MAX as i32) as u16;
|
|
|
|
|
mode.height = size.h.clamp(0, u16::MAX as i32) as u16;
|
2024-03-27 14:54:24 +04:00
|
|
|
if let Some(logical) = output.logical.as_mut() {
|
|
|
|
|
logical.width = size.w as u32;
|
|
|
|
|
logical.height = size.h as u32;
|
|
|
|
|
}
|
2024-03-27 09:46:18 +04:00
|
|
|
state.niri.ipc_outputs_changed = true;
|
2024-03-27 08:27:14 +04:00
|
|
|
}
|
2024-01-17 10:38:32 +04:00
|
|
|
|
2024-03-23 14:16:36 +04:00
|
|
|
state.niri.output_resized(&winit.output);
|
2023-10-17 16:53:04 +04:00
|
|
|
}
|
|
|
|
|
WinitEvent::Input(event) => state.process_input_event(event),
|
|
|
|
|
WinitEvent::Focus(_) => (),
|
2024-03-23 14:16:36 +04:00
|
|
|
WinitEvent::Redraw => state.niri.queue_redraw(&state.backend.winit().output),
|
2024-01-28 16:30:29 +04:00
|
|
|
WinitEvent::CloseRequested => state.niri.stop_signal.stop(),
|
2023-08-09 11:03:38 +04:00
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2024-02-01 16:55:46 +04:00
|
|
|
Ok(Self {
|
2023-09-14 22:28:26 +04:00
|
|
|
config,
|
2023-08-07 19:44:40 +04:00
|
|
|
output,
|
2023-08-09 11:03:38 +04:00
|
|
|
backend,
|
|
|
|
|
damage_tracker,
|
2026-04-18 22:43:11 -07:00
|
|
|
dmabuf_global: None,
|
2024-01-17 10:38:32 +04:00
|
|
|
ipc_outputs,
|
2024-02-01 16:55:46 +04:00
|
|
|
})
|
2023-08-09 11:03:38 +04:00
|
|
|
}
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
pub fn init(&mut self, niri: &mut Niri) {
|
2024-02-21 21:27:44 +04:00
|
|
|
let renderer = self.backend.renderer();
|
|
|
|
|
if let Err(err) = renderer.bind_wl_display(&niri.display_handle) {
|
2023-12-31 09:46:22 +04:00
|
|
|
warn!("error binding renderer wl_display: {err}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 11:07:23 +04:00
|
|
|
resources::init(renderer);
|
2024-02-21 21:27:44 +04:00
|
|
|
shaders::init(renderer);
|
|
|
|
|
|
2024-04-21 20:10:35 +04:00
|
|
|
let config = self.config.borrow();
|
|
|
|
|
if let Some(src) = config.animations.window_resize.custom_shader.as_deref() {
|
|
|
|
|
shaders::set_custom_resize_program(renderer, Some(src));
|
|
|
|
|
}
|
2024-05-12 09:52:21 +04:00
|
|
|
if let Some(src) = config.animations.window_close.custom_shader.as_deref() {
|
|
|
|
|
shaders::set_custom_close_program(renderer, Some(src));
|
|
|
|
|
}
|
2024-05-15 19:38:29 +04:00
|
|
|
if let Some(src) = config.animations.window_open.custom_shader.as_deref() {
|
|
|
|
|
shaders::set_custom_open_program(renderer, Some(src));
|
|
|
|
|
}
|
2024-04-21 20:10:35 +04:00
|
|
|
drop(config);
|
|
|
|
|
|
2025-01-21 09:40:00 +03:00
|
|
|
niri.update_shaders();
|
2024-05-03 21:21:58 +04:00
|
|
|
|
2026-04-18 22:43:11 -07:00
|
|
|
self.create_dmabuf_global(niri);
|
|
|
|
|
|
2024-04-14 09:23:15 +04:00
|
|
|
niri.add_output(self.output.clone(), None, false);
|
2023-08-09 11:03:38 +04:00
|
|
|
}
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2026-04-18 22:43:11 -07:00
|
|
|
pub fn create_dmabuf_global(&mut self, niri: &mut Niri) {
|
|
|
|
|
let renderer = self.backend.renderer();
|
|
|
|
|
|
|
|
|
|
let default_feedback = || {
|
|
|
|
|
let display = renderer.egl_context().display();
|
|
|
|
|
let device =
|
|
|
|
|
EGLDevice::device_for_display(display).context("error getting EGL device")?;
|
|
|
|
|
let node = device
|
|
|
|
|
.try_get_render_node()
|
|
|
|
|
.context("error getting EGL device render node")?
|
|
|
|
|
.context("failed to query EGL device render node")?;
|
|
|
|
|
|
|
|
|
|
let primary_formats = renderer.dmabuf_formats();
|
|
|
|
|
DmabufFeedbackBuilder::new(node.dev_id(), primary_formats)
|
|
|
|
|
.build()
|
|
|
|
|
.context("error building dmabuf feedback")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Fallback to dmabuf v3 if we failed to build feedback.
|
|
|
|
|
let dmabuf_global = match default_feedback() {
|
|
|
|
|
Ok(feedback) => niri
|
|
|
|
|
.dmabuf_state
|
|
|
|
|
.create_global_with_default_feedback::<State>(&niri.display_handle, &feedback),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
debug!("failed building default dmabuf feedback, falling back to v3: {err:?}");
|
|
|
|
|
let primary_formats = renderer.dmabuf_formats();
|
|
|
|
|
niri.dmabuf_state
|
|
|
|
|
.create_global::<State>(&niri.display_handle, primary_formats)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
assert!(self.dmabuf_global.replace(dmabuf_global).is_none());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:04:32 +04:00
|
|
|
pub fn seat_name(&self) -> String {
|
|
|
|
|
"winit".to_owned()
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 11:33:24 +04:00
|
|
|
pub fn with_primary_renderer<T>(
|
|
|
|
|
&mut self,
|
|
|
|
|
f: impl FnOnce(&mut GlesRenderer) -> T,
|
|
|
|
|
) -> Option<T> {
|
|
|
|
|
Some(f(self.backend.renderer()))
|
2023-09-03 13:04:32 +04:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 11:28:42 +04:00
|
|
|
pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult {
|
2023-09-03 13:04:32 +04:00
|
|
|
let _span = tracy_client::span!("Winit::render");
|
|
|
|
|
|
2024-01-03 11:28:42 +04:00
|
|
|
// Render the elements.
|
2026-01-30 08:44:32 +03:00
|
|
|
let ctx = RenderCtx {
|
|
|
|
|
renderer: self.backend.renderer(),
|
|
|
|
|
target: RenderTarget::Output,
|
2026-04-14 07:33:50 +00:00
|
|
|
xray: None,
|
2026-01-30 08:44:32 +03:00
|
|
|
};
|
2026-02-06 15:00:52 +03:00
|
|
|
let mut elements = niri.render_to_vec(ctx, output, true);
|
2024-01-03 11:28:42 +04:00
|
|
|
|
2024-05-03 10:25:51 +04:00
|
|
|
// Visualize the damage, if enabled.
|
|
|
|
|
if niri.debug_draw_damage {
|
|
|
|
|
let output_state = niri.output_state.get_mut(output).unwrap();
|
|
|
|
|
draw_damage(&mut output_state.debug_damage_tracker, &mut elements);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 11:28:42 +04:00
|
|
|
// Hand them over to winit.
|
2025-03-09 22:03:14 +03:00
|
|
|
let res = {
|
|
|
|
|
let (renderer, mut framebuffer) = self.backend.bind().unwrap();
|
|
|
|
|
// FIXME: currently impossible to call due to a mutable borrow.
|
|
|
|
|
//
|
|
|
|
|
// let age = self.backend.buffer_age().unwrap();
|
|
|
|
|
let age = 0;
|
|
|
|
|
self.damage_tracker
|
|
|
|
|
.render_output(renderer, &mut framebuffer, age, &elements, [0.; 4])
|
|
|
|
|
.unwrap()
|
|
|
|
|
};
|
2023-10-01 19:41:42 +04:00
|
|
|
|
|
|
|
|
niri.update_primary_scanout_output(output, &res.states);
|
|
|
|
|
|
2023-10-13 13:04:41 +04:00
|
|
|
let rv;
|
2023-09-03 13:04:32 +04:00
|
|
|
if let Some(damage) = res.damage {
|
2023-09-14 22:28:26 +04:00
|
|
|
if self
|
|
|
|
|
.config
|
|
|
|
|
.borrow()
|
|
|
|
|
.debug
|
|
|
|
|
.wait_for_frame_completion_before_queueing
|
|
|
|
|
{
|
2023-09-14 09:33:42 +04:00
|
|
|
let _span = tracy_client::span!("wait for completion");
|
2024-04-09 19:06:13 +04:00
|
|
|
if let Err(err) = res.sync.wait() {
|
|
|
|
|
warn!("error waiting for frame completion: {err:?}");
|
|
|
|
|
}
|
2023-09-14 09:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 19:06:13 +04:00
|
|
|
self.backend.submit(Some(damage)).unwrap();
|
2023-09-03 13:04:32 +04:00
|
|
|
|
|
|
|
|
let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states);
|
|
|
|
|
presentation_feedbacks.presented::<_, smithay::utils::Monotonic>(
|
|
|
|
|
get_monotonic_time(),
|
2024-12-13 10:28:25 +03:00
|
|
|
Refresh::Unknown,
|
2023-09-03 13:04:32 +04:00
|
|
|
0,
|
|
|
|
|
wp_presentation_feedback::Kind::empty(),
|
|
|
|
|
);
|
2023-10-13 13:04:41 +04:00
|
|
|
|
|
|
|
|
rv = RenderResult::Submitted;
|
|
|
|
|
} else {
|
|
|
|
|
rv = RenderResult::NoDamage;
|
2023-09-30 17:13:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let output_state = niri.output_state.get_mut(output).unwrap();
|
|
|
|
|
match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {
|
|
|
|
|
RedrawState::Idle => unreachable!(),
|
2024-03-23 12:46:26 +04:00
|
|
|
RedrawState::Queued => (),
|
2023-09-30 17:13:56 +04:00
|
|
|
RedrawState::WaitingForVBlank { .. } => unreachable!(),
|
|
|
|
|
RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(),
|
|
|
|
|
RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(),
|
|
|
|
|
}
|
2023-09-03 13:04:32 +04:00
|
|
|
|
2024-03-02 08:10:05 +04:00
|
|
|
output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1);
|
|
|
|
|
|
|
|
|
|
// FIXME: this should wait until a frame callback from the host compositor, but it redraws
|
|
|
|
|
// right away instead.
|
2023-09-30 17:13:56 +04:00
|
|
|
if output_state.unfinished_animations_remain {
|
2023-09-03 13:04:32 +04:00
|
|
|
self.backend.window().request_redraw();
|
|
|
|
|
}
|
2023-10-13 13:04:41 +04:00
|
|
|
|
|
|
|
|
rv
|
2023-09-03 13:04:32 +04:00
|
|
|
}
|
2023-09-03 14:10:02 +04:00
|
|
|
|
|
|
|
|
pub fn toggle_debug_tint(&mut self) {
|
|
|
|
|
let renderer = self.backend.renderer();
|
|
|
|
|
renderer.set_debug_flags(renderer.debug_flags() ^ DebugFlags::TINT);
|
|
|
|
|
}
|
2023-09-08 17:54:02 +04:00
|
|
|
|
2024-02-05 17:31:11 +04:00
|
|
|
pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> bool {
|
2024-01-03 11:16:47 +04:00
|
|
|
match self.backend.renderer().import_dmabuf(dmabuf, None) {
|
2024-02-05 17:31:11 +04:00
|
|
|
Ok(_texture) => true,
|
2024-01-03 11:16:47 +04:00
|
|
|
Err(err) => {
|
|
|
|
|
debug!("error importing dmabuf: {err:?}");
|
2024-02-05 17:31:11 +04:00
|
|
|
false
|
2024-01-03 11:16:47 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 09:46:18 +04:00
|
|
|
pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> {
|
2024-01-17 10:38:32 +04:00
|
|
|
self.ipc_outputs.clone()
|
|
|
|
|
}
|
2023-08-07 19:44:40 +04:00
|
|
|
}
|