cosmic-comp/src/state.rs

211 lines
5.7 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
2021-12-21 18:57:09 +01:00
use crate::{
2022-01-20 19:51:46 +01:00
backend::{kms::KmsState, winit::WinitState, x11::X11State},
2021-12-21 18:57:09 +01:00
shell::{init_shell, workspaces::Workspaces, ShellStates},
};
use smithay::{
reexports::{
calloop::LoopHandle,
wayland_server::Display,
},
2021-12-21 18:57:09 +01:00
wayland::{
data_device::{default_action_chooser, init_data_device},
output::{xdg::init_xdg_output_manager, Output},
2021-12-21 18:57:09 +01:00
seat::Seat,
shell::xdg::ToplevelSurface,
shm::init_shm_global,
},
};
2022-01-11 19:18:41 +01:00
use std::{
cell::RefCell,
rc::Rc,
sync::{atomic::AtomicBool, Arc},
time::Instant,
};
2022-01-11 19:18:41 +01:00
#[cfg(feature = "debug")]
use std::{collections::VecDeque, time::Duration};
pub struct State {
2022-01-11 17:00:04 +01:00
pub common: Common,
pub backend: BackendData,
}
pub struct Common {
pub display: Rc<RefCell<Display>>,
pub event_loop_handle: LoopHandle<'static, State>,
2021-12-21 18:57:09 +01:00
2021-12-17 17:53:01 +01:00
pub spaces: Workspaces,
2021-12-21 18:57:09 +01:00
pub shell: ShellStates,
pub pending_toplevels: Vec<ToplevelSurface>,
pub dirty_flag: Arc<AtomicBool>,
2021-12-21 18:57:09 +01:00
pub seats: Vec<Seat>,
pub last_active_seat: Seat,
2021-12-15 23:23:49 +01:00
pub start_time: Instant,
pub should_stop: bool,
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
2022-01-11 17:22:23 +01:00
pub egui: Egui,
}
#[cfg(feature = "debug")]
pub struct Egui {
pub state: smithay_egui::EguiState,
pub modifiers: smithay::wayland::seat::ModifiersState,
pub active: bool,
pub alpha: f32,
pub spaces: bool,
pub outputs: bool,
2021-12-15 23:23:49 +01:00
}
2022-01-11 19:18:41 +01:00
#[cfg(feature = "debug")]
pub struct Fps {
pub frames: VecDeque<Duration>,
pub last: Instant,
}
2021-12-15 23:23:49 +01:00
pub enum BackendData {
X11(X11State),
Winit(WinitState),
2022-01-20 19:51:46 +01:00
Kms(KmsState),
2021-12-15 23:23:49 +01:00
// TODO
// Wayland(WaylandState),
Unset,
}
impl BackendData {
2022-01-20 19:51:46 +01:00
pub fn kms(&mut self) -> &mut KmsState {
match self {
BackendData::Kms(ref mut kms_state) => kms_state,
_ => unreachable!("Called kms in non kms backend"),
}
}
2021-12-15 23:23:49 +01:00
pub fn x11(&mut self) -> &mut X11State {
match self {
BackendData::X11(ref mut x11_state) => x11_state,
_ => unreachable!("Called x11 in non x11 backend"),
}
}
pub fn winit(&mut self) -> &mut WinitState {
match self {
BackendData::Winit(ref mut winit_state) => winit_state,
_ => unreachable!("Called winit in non winit backend"),
}
}
pub fn schedule_render(&mut self, output: &Output) {
match self {
BackendData::Winit(_) => {}, // We cannot do this on the winit backend.
// Winit has a very strict render-loop and skipping frames breaks atleast the wayland winit-backend.
// Swapping with damage (which should be empty on these frames) is likely good enough anyway.
BackendData::X11(ref mut state) => state.schedule_render(output),
BackendData::Kms(ref mut state) => state.schedule_render(output),
_ => unreachable!("No backend was initialized"),
}
}
}
impl State {
pub fn new(mut display: Display, handle: LoopHandle<'static, State>) -> State {
2021-12-21 18:57:09 +01:00
init_shm_global(&mut display, vec![], None);
init_xdg_output_manager(&mut display, None);
let shell_handles = init_shell(&mut display);
let initial_seat = crate::input::add_seat(&mut display, "seat-0".into());
init_data_device(
&mut display,
|_dnd_event| { /* TODO */ },
default_action_chooser,
None,
);
State {
2022-01-11 17:00:04 +01:00
common: Common {
display: Rc::new(RefCell::new(display)),
event_loop_handle: handle,
2021-12-21 18:57:09 +01:00
2022-01-11 17:00:04 +01:00
spaces: Workspaces::new(),
shell: shell_handles,
pending_toplevels: Vec::new(),
dirty_flag: Arc::new(AtomicBool::new(false)),
2021-12-21 18:57:09 +01:00
2022-01-11 17:00:04 +01:00
seats: vec![initial_seat.clone()],
last_active_seat: initial_seat,
2021-12-15 23:23:49 +01:00
2022-01-11 17:00:04 +01:00
start_time: Instant::now(),
should_stop: false,
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
egui: Egui {
state: smithay_egui::EguiState::new(),
modifiers: Default::default(),
active: false,
alpha: 1.0,
outputs: false,
spaces: false,
2022-01-11 17:22:23 +01:00
},
2022-01-11 17:00:04 +01:00
},
2021-12-15 23:23:49 +01:00
backend: BackendData::Unset,
}
}
}
2022-01-11 19:18:41 +01:00
#[cfg(feature = "debug")]
impl Fps {
const WINDOW_SIZE: usize = 100;
2022-01-11 19:18:41 +01:00
pub fn tick(&mut self) {
let next = Instant::now();
let frame_time = next.duration_since(self.last);
2022-01-11 19:18:41 +01:00
self.frames.push_back(frame_time);
if self.frames.len() > Fps::WINDOW_SIZE {
2022-01-11 19:18:41 +01:00
self.frames.pop_front();
}
self.last = next;
}
pub fn max_frametime(&self) -> &Duration {
2022-01-11 19:18:41 +01:00
self.frames.iter().max().unwrap_or(&Duration::ZERO)
}
pub fn min_frametime(&self) -> &Duration {
2022-01-11 19:18:41 +01:00
self.frames.iter().min().unwrap_or(&Duration::ZERO)
}
pub fn avg_frametime(&self) -> Duration {
if self.frames.is_empty() {
return Duration::ZERO;
}
2022-01-11 19:18:41 +01:00
self.frames.iter().cloned().sum::<Duration>() / (self.frames.len() as u32)
}
pub fn avg_fps(&self) -> f64 {
if self.frames.is_empty() {
return 0.0;
}
2022-01-11 19:18:41 +01:00
let sum_secs = self.frames.iter().map(|d| d.as_secs_f64()).sum::<f64>();
1.0 / (sum_secs / self.frames.len() as f64)
}
}
#[cfg(feature = "debug")]
impl Default for Fps {
fn default() -> Fps {
Fps {
frames: VecDeque::with_capacity(Fps::WINDOW_SIZE + 1),
last: Instant::now(),
}
}
}
#[cfg(feature = "debug")]
2022-01-18 19:42:56 +01:00
pub fn avg_fps<'a>(iter: impl Iterator<Item = &'a Duration>) -> f64 {
let sum_secs = iter.map(|d| d.as_secs_f64()).sum::<f64>();
1.0 / (sum_secs / Fps::WINDOW_SIZE as f64)
2022-01-18 19:42:56 +01:00
}