debug: Better frame graph
This commit is contained in:
parent
9823b42c20
commit
36cb2ac719
6 changed files with 215 additions and 72 deletions
187
src/state.rs
187
src/state.rs
|
|
@ -103,29 +103,6 @@ pub struct Common {
|
|||
pub viewporter_state: ViewporterState,
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
pub struct Egui {
|
||||
pub debug_state: smithay_egui::EguiState,
|
||||
pub active: bool,
|
||||
pub alpha: f32,
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
pub struct Fps {
|
||||
pub state: smithay_egui::EguiState,
|
||||
pub frames: VecDeque<(Instant, Duration)>,
|
||||
pub start: Instant,
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
pub struct Frame {
|
||||
start: Instant,
|
||||
duration_elements: Duration,
|
||||
duration_render: Duration,
|
||||
duration_screencopy: Duration,
|
||||
duration_displayed: Duration,
|
||||
}
|
||||
|
||||
pub enum BackendData {
|
||||
X11(X11State),
|
||||
Winit(WinitState),
|
||||
|
|
@ -455,48 +432,158 @@ impl Common {
|
|||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
impl Fps {
|
||||
const WINDOW_SIZE: usize = 1000;
|
||||
pub struct Egui {
|
||||
pub debug_state: smithay_egui::EguiState,
|
||||
pub active: bool,
|
||||
pub alpha: f32,
|
||||
}
|
||||
|
||||
pub fn start(&mut self) {
|
||||
self.start = Instant::now();
|
||||
#[cfg(feature = "debug")]
|
||||
pub struct Fps {
|
||||
pub state: smithay_egui::EguiState,
|
||||
pending_frame: Option<PendingFrame>,
|
||||
pub frames: VecDeque<Frame>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
#[derive(Debug)]
|
||||
struct PendingFrame {
|
||||
start: Instant,
|
||||
duration_elements: Option<Duration>,
|
||||
duration_render: Option<Duration>,
|
||||
duration_screencopy: Option<Duration>,
|
||||
duration_displayed: Option<Duration>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
#[derive(Debug)]
|
||||
pub struct Frame {
|
||||
pub start: Instant,
|
||||
pub duration_elements: Duration,
|
||||
pub duration_render: Duration,
|
||||
pub duration_screencopy: Option<Duration>,
|
||||
pub duration_displayed: Duration,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
fn frame_time(&self) -> Duration {
|
||||
self.duration_elements
|
||||
+ self.duration_render
|
||||
+ self.duration_screencopy.clone().unwrap_or(Duration::ZERO)
|
||||
}
|
||||
|
||||
pub fn end(&mut self) {
|
||||
let frame_time = Instant::now().duration_since(self.start);
|
||||
fn time_to_display(&self) -> Duration {
|
||||
self.duration_elements
|
||||
+ self.duration_render
|
||||
+ self.duration_screencopy.clone().unwrap_or(Duration::ZERO)
|
||||
+ self.duration_displayed
|
||||
}
|
||||
}
|
||||
|
||||
self.frames.push_back((self.start, frame_time));
|
||||
if self.frames.len() > Fps::WINDOW_SIZE {
|
||||
self.frames.pop_front();
|
||||
impl From<PendingFrame> for Frame {
|
||||
fn from(pending: PendingFrame) -> Self {
|
||||
Frame {
|
||||
start: pending.start,
|
||||
duration_elements: pending.duration_elements.unwrap_or(Duration::ZERO),
|
||||
duration_render: pending.duration_render.unwrap_or(Duration::ZERO),
|
||||
duration_screencopy: pending.duration_screencopy,
|
||||
duration_displayed: pending.duration_displayed.unwrap_or(Duration::ZERO),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
impl Fps {
|
||||
const WINDOW_SIZE: usize = 360;
|
||||
|
||||
pub fn start(&mut self) {
|
||||
self.pending_frame = Some(PendingFrame {
|
||||
start: Instant::now(),
|
||||
duration_elements: None,
|
||||
duration_render: None,
|
||||
duration_screencopy: None,
|
||||
duration_displayed: None,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn elements(&mut self) {
|
||||
if let Some(frame) = self.pending_frame.as_mut() {
|
||||
frame.duration_elements = Some(Instant::now().duration_since(frame.start));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_frametime(&self) -> &Duration {
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|(_, f)| f)
|
||||
.max()
|
||||
.unwrap_or(&Duration::ZERO)
|
||||
pub fn render(&mut self) {
|
||||
if let Some(frame) = self.pending_frame.as_mut() {
|
||||
frame.duration_render = Some(
|
||||
Instant::now().duration_since(frame.start)
|
||||
- frame.duration_elements.clone().unwrap_or(Duration::ZERO),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_frametime(&self) -> &Duration {
|
||||
pub fn screencopy(&mut self) {
|
||||
if let Some(frame) = self.pending_frame.as_mut() {
|
||||
frame.duration_screencopy = Some(
|
||||
Instant::now().duration_since(frame.start)
|
||||
- frame.duration_elements.clone().unwrap_or(Duration::ZERO)
|
||||
- frame.duration_render.clone().unwrap_or(Duration::ZERO),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn displayed(&mut self) {
|
||||
if let Some(mut frame) = self.pending_frame.take() {
|
||||
frame.duration_displayed = Some(
|
||||
Instant::now().duration_since(frame.start)
|
||||
- frame.duration_elements.clone().unwrap_or(Duration::ZERO)
|
||||
- frame.duration_render.clone().unwrap_or(Duration::ZERO)
|
||||
- frame.duration_screencopy.clone().unwrap_or(Duration::ZERO),
|
||||
);
|
||||
|
||||
self.frames.push_back(frame.into());
|
||||
if self.frames.len() > Fps::WINDOW_SIZE {
|
||||
self.frames.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_frametime(&self) -> Duration {
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|(_, f)| f)
|
||||
.map(|f| f.frame_time())
|
||||
.max()
|
||||
.unwrap_or(Duration::ZERO)
|
||||
}
|
||||
|
||||
pub fn min_frametime(&self) -> Duration {
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|f| f.frame_time())
|
||||
.min()
|
||||
.unwrap_or(&Duration::ZERO)
|
||||
.unwrap_or(Duration::ZERO)
|
||||
}
|
||||
|
||||
pub fn max_time_to_display(&self) -> Duration {
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|f| f.time_to_display())
|
||||
.max()
|
||||
.unwrap_or(Duration::ZERO)
|
||||
}
|
||||
|
||||
pub fn min_time_to_display(&self) -> Duration {
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|f| f.time_to_display())
|
||||
.min()
|
||||
.unwrap_or(Duration::ZERO)
|
||||
}
|
||||
|
||||
pub fn avg_frametime(&self) -> Duration {
|
||||
if self.frames.is_empty() {
|
||||
return Duration::ZERO;
|
||||
}
|
||||
self.frames
|
||||
.iter()
|
||||
.map(|(_, f)| f)
|
||||
.cloned()
|
||||
.sum::<Duration>()
|
||||
/ (self.frames.len() as u32)
|
||||
self.frames.iter().map(|f| f.frame_time()).sum::<Duration>() / (self.frames.len() as u32)
|
||||
}
|
||||
|
||||
pub fn avg_fps(&self) -> f64 {
|
||||
|
|
@ -504,7 +591,9 @@ impl Fps {
|
|||
return 0.0;
|
||||
}
|
||||
let secs = match (self.frames.front(), self.frames.back()) {
|
||||
(Some((start, _)), Some((end, dur))) => end.duration_since(*start) + *dur,
|
||||
(Some(Frame { start, .. }), Some(end_frame)) => {
|
||||
end_frame.start.duration_since(*start) + end_frame.frame_time()
|
||||
}
|
||||
_ => Duration::ZERO,
|
||||
}
|
||||
.as_secs_f64();
|
||||
|
|
@ -524,8 +613,8 @@ impl Default for Fps {
|
|||
state.context().set_visuals(visuals);
|
||||
state
|
||||
},
|
||||
pending_frame: None,
|
||||
frames: VecDeque::with_capacity(Fps::WINDOW_SIZE + 1),
|
||||
start: Instant::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue