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
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2023-08-07 19:45:55 +04:00
|
|
|
use smithay::backend::renderer::damage::OutputDamageTracker;
|
|
|
|
|
use smithay::backend::renderer::gles::GlesRenderer;
|
2023-09-03 11:34:38 +04:00
|
|
|
use smithay::backend::renderer::{DebugFlags, Renderer};
|
2023-09-03 14:10:02 +04:00
|
|
|
use smithay::backend::winit::{self, WinitError, WinitEvent, WinitGraphicsBackend};
|
2023-09-21 13:48:32 +04:00
|
|
|
use smithay::output::{Mode, Output, PhysicalProperties, Scale, Subpixel};
|
2023-08-07 19:45:55 +04:00
|
|
|
use smithay::reexports::calloop::timer::{TimeoutAction, Timer};
|
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;
|
|
|
|
|
use smithay::reexports::winit::window::WindowBuilder;
|
2023-08-14 12:24:59 +04:00
|
|
|
use smithay::utils::Transform;
|
2023-09-03 15:15:55 +04:00
|
|
|
use smithay::wayland::dmabuf::DmabufFeedback;
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2023-09-14 09:33:42 +04:00
|
|
|
use crate::config::Config;
|
2023-09-30 17:13:56 +04:00
|
|
|
use crate::niri::{OutputRenderElements, RedrawState, State};
|
2023-08-16 10:59:34 +04:00
|
|
|
use crate::utils::get_monotonic_time;
|
2023-09-24 17:30:06 +04:00
|
|
|
use crate::Niri;
|
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,
|
2023-09-08 17:54:02 +04:00
|
|
|
connectors: Arc<Mutex<HashMap<String, Output>>>,
|
2023-08-07 19:44:40 +04:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
impl Winit {
|
2023-09-24 17:30:06 +04:00
|
|
|
pub fn new(config: Rc<RefCell<Config>>, event_loop: LoopHandle<State>) -> Self {
|
2023-08-13 12:46:53 +04:00
|
|
|
let builder = WindowBuilder::new()
|
|
|
|
|
.with_inner_size(LogicalSize::new(1280.0, 800.0))
|
|
|
|
|
// .with_resizable(false)
|
|
|
|
|
.with_title("niri");
|
2023-09-03 14:10:02 +04:00
|
|
|
let (backend, mut winit_event_loop) = winit::init_from_builder(builder).unwrap();
|
2023-08-09 11:03:38 +04:00
|
|
|
|
2023-09-21 13:48:32 +04:00
|
|
|
let output_config = config
|
|
|
|
|
.borrow()
|
|
|
|
|
.outputs
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|o| o.name == "winit")
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
let scale = output_config.scale.clamp(0.1, 10.);
|
|
|
|
|
let scale = scale.max(1.).round() as i32;
|
|
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
let mode = Mode {
|
|
|
|
|
size: backend.window_size().physical_size,
|
|
|
|
|
refresh: 60_000,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let output = Output::new(
|
|
|
|
|
"winit".to_string(),
|
|
|
|
|
PhysicalProperties {
|
|
|
|
|
size: (0, 0).into(),
|
|
|
|
|
subpixel: Subpixel::Unknown,
|
|
|
|
|
make: "Smithay".into(),
|
|
|
|
|
model: "Winit".into(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
output.change_current_state(
|
|
|
|
|
Some(mode),
|
|
|
|
|
Some(Transform::Flipped180),
|
2023-09-21 13:48:32 +04:00
|
|
|
Some(Scale::Integer(scale)),
|
2023-08-09 11:03:38 +04:00
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
output.set_preferred(mode);
|
|
|
|
|
|
2023-09-08 17:54:02 +04:00
|
|
|
let connectors = Arc::new(Mutex::new(HashMap::from([(
|
|
|
|
|
"winit".to_owned(),
|
|
|
|
|
output.clone(),
|
|
|
|
|
)])));
|
|
|
|
|
|
2023-08-09 11:03:38 +04:00
|
|
|
let damage_tracker = OutputDamageTracker::from_output(&output);
|
|
|
|
|
|
|
|
|
|
let timer = Timer::immediate();
|
|
|
|
|
event_loop
|
2023-09-24 17:30:06 +04:00
|
|
|
.insert_source(timer, move |_, _, state| {
|
2023-09-03 14:10:02 +04:00
|
|
|
let res = winit_event_loop.dispatch_new_events(|event| match event {
|
|
|
|
|
WinitEvent::Resized { size, .. } => {
|
2023-09-24 17:30:06 +04:00
|
|
|
let winit = state.backend.winit();
|
2023-09-03 14:10:02 +04:00
|
|
|
winit.output.change_current_state(
|
|
|
|
|
Some(Mode {
|
|
|
|
|
size,
|
|
|
|
|
refresh: 60_000,
|
|
|
|
|
}),
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
);
|
2023-09-24 17:30:06 +04:00
|
|
|
state.niri.output_resized(winit.output.clone());
|
2023-09-03 14:10:02 +04:00
|
|
|
}
|
2023-09-24 17:30:06 +04:00
|
|
|
WinitEvent::Input(event) => state.process_input_event(event),
|
2023-09-03 14:10:02 +04:00
|
|
|
WinitEvent::Focus(_) => (),
|
2023-09-24 17:30:06 +04:00
|
|
|
WinitEvent::Refresh => state
|
2023-09-03 14:10:02 +04:00
|
|
|
.niri
|
2023-09-24 17:30:06 +04:00
|
|
|
.queue_redraw(state.backend.winit().output.clone()),
|
2023-09-03 14:10:02 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// I want this to stop compiling if more errors are added.
|
|
|
|
|
#[allow(clippy::single_match)]
|
|
|
|
|
match res {
|
|
|
|
|
Err(WinitError::WindowClosed) => {
|
2023-09-24 17:30:06 +04:00
|
|
|
state.niri.stop_signal.stop();
|
|
|
|
|
state.niri.remove_output(&state.backend.winit().output);
|
2023-09-03 14:10:02 +04:00
|
|
|
}
|
|
|
|
|
Ok(()) => (),
|
|
|
|
|
}
|
2023-08-14 18:06:32 +04:00
|
|
|
TimeoutAction::ToDuration(Duration::from_micros(16667))
|
2023-08-09 11:03:38 +04:00
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
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,
|
2023-09-08 17:54:02 +04:00
|
|
|
connectors,
|
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) {
|
2023-08-15 15:16:22 +04:00
|
|
|
// For some reason, binding the display here causes damage tracker artifacts.
|
|
|
|
|
//
|
|
|
|
|
// use smithay::backend::renderer::ImportEgl;
|
|
|
|
|
//
|
|
|
|
|
// if let Err(err) = self
|
|
|
|
|
// .backend
|
|
|
|
|
// .renderer()
|
|
|
|
|
// .bind_wl_display(&niri.display_handle)
|
|
|
|
|
// {
|
|
|
|
|
// warn!("error binding renderer wl_display: {err}");
|
|
|
|
|
// }
|
2023-08-14 15:53:24 +04:00
|
|
|
niri.add_output(self.output.clone(), None);
|
2023-08-09 11:03:38 +04:00
|
|
|
}
|
2023-08-07 19:44:40 +04:00
|
|
|
|
2023-09-03 13:04:32 +04:00
|
|
|
pub fn seat_name(&self) -> String {
|
|
|
|
|
"winit".to_owned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn renderer(&mut self) -> &mut GlesRenderer {
|
|
|
|
|
self.backend.renderer()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn render(
|
|
|
|
|
&mut self,
|
|
|
|
|
niri: &mut Niri,
|
|
|
|
|
output: &Output,
|
|
|
|
|
elements: &[OutputRenderElements<GlesRenderer>],
|
2023-09-03 15:15:55 +04:00
|
|
|
) -> Option<&DmabufFeedback> {
|
2023-09-03 13:04:32 +04:00
|
|
|
let _span = tracy_client::span!("Winit::render");
|
|
|
|
|
|
|
|
|
|
self.backend.bind().unwrap();
|
|
|
|
|
let age = self.backend.buffer_age().unwrap();
|
2023-10-13 10:31:30 +04:00
|
|
|
let clear_color = niri.clear_color();
|
2023-09-03 13:04:32 +04:00
|
|
|
let res = self
|
|
|
|
|
.damage_tracker
|
2023-10-13 10:31:30 +04:00
|
|
|
.render_output(self.backend.renderer(), age, elements, clear_color)
|
2023-09-03 13:04:32 +04:00
|
|
|
.unwrap();
|
2023-10-01 19:41:42 +04:00
|
|
|
|
|
|
|
|
niri.update_primary_scanout_output(output, &res.states);
|
|
|
|
|
|
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");
|
|
|
|
|
res.sync.wait();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:04:32 +04:00
|
|
|
self.backend.submit(Some(&damage)).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &res.states);
|
2023-09-04 15:09:58 +04:00
|
|
|
let mode = output.current_mode().unwrap();
|
|
|
|
|
let refresh = Duration::from_secs_f64(1_000f64 / mode.refresh as f64);
|
2023-09-03 13:04:32 +04:00
|
|
|
presentation_feedbacks.presented::<_, smithay::utils::Monotonic>(
|
|
|
|
|
get_monotonic_time(),
|
|
|
|
|
refresh,
|
|
|
|
|
0,
|
|
|
|
|
wp_presentation_feedback::Kind::empty(),
|
|
|
|
|
);
|
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!(),
|
|
|
|
|
RedrawState::Queued(_) => (),
|
|
|
|
|
RedrawState::WaitingForVBlank { .. } => unreachable!(),
|
|
|
|
|
RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(),
|
|
|
|
|
RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(),
|
|
|
|
|
}
|
2023-09-03 13:04:32 +04:00
|
|
|
|
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-09-03 15:15:55 +04:00
|
|
|
|
|
|
|
|
None
|
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
|
|
|
|
|
|
|
|
pub fn connectors(&self) -> Arc<Mutex<HashMap<String, Output>>> {
|
|
|
|
|
self.connectors.clone()
|
|
|
|
|
}
|
2023-08-07 19:44:40 +04:00
|
|
|
}
|