Skip to content

Commit

Permalink
anvil: Store fps ticker as part of FpsElement
Browse files Browse the repository at this point in the history
This removes a bit of duplication between backends. There seems to be no
need to separate these unless fps is used for other things.
  • Loading branch information
ids1024 committed Dec 7, 2024
1 parent b83a49a commit c3ffa32
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 28 deletions.
10 changes: 7 additions & 3 deletions anvil/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub static FPS_NUMBERS_PNG: &[u8] = include_bytes!("../resources/numbers.png");
#[derive(Debug, Clone)]
pub struct FpsElement<T: Texture> {
id: Id,
fps: fps_ticker::Fps,
value: u32,
texture: T,
commit_counter: CommitCounter,
Expand All @@ -137,14 +138,17 @@ impl<T: Texture> FpsElement<T> {
FpsElement {
id: Id::new(),
texture,
fps: fps_ticker::Fps::default(),
value: 0,
commit_counter: CommitCounter::default(),
}
}

pub fn update_fps(&mut self, fps: u32) {
if self.value != fps {
self.value = fps;
pub fn tick(&mut self) {
self.fps.tick();
let value = self.fps.avg().round() as u32;
if self.value != value {
self.value = value;
self.commit_counter.increment();
}
}
Expand Down
7 changes: 1 addition & 6 deletions anvil/src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,6 @@ struct SurfaceData {
global: Option<GlobalId>,
compositor: SurfaceComposition,
#[cfg(feature = "debug")]
fps: fps_ticker::Fps,
#[cfg(feature = "debug")]
fps_element: Option<FpsElement<MultiTexture>>,
dmabuf_feedback: Option<SurfaceDmabufFeedback>,
}
Expand Down Expand Up @@ -1109,8 +1107,6 @@ impl AnvilState<UdevData> {
global: Some(global),
compositor,
#[cfg(feature = "debug")]
fps: fps_ticker::Fps::default(),
#[cfg(feature = "debug")]
fps_element,
dmabuf_feedback,
};
Expand Down Expand Up @@ -1681,9 +1677,8 @@ fn render_surface<'a>(

#[cfg(feature = "debug")]
if let Some(element) = surface.fps_element.as_mut() {
element.update_fps(surface.fps.avg().round() as u32);
surface.fps.tick();
custom_elements.push(CustomRenderElements::Fps(element.clone()));
element.tick();
}

let (elements, clear_color) =
Expand Down
11 changes: 1 addition & 10 deletions anvil/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub struct WinitData {
damage_tracker: OutputDamageTracker,
dmabuf_state: (DmabufState, DmabufGlobal, Option<DmabufFeedback>),
full_redraw: u8,
#[cfg(feature = "debug")]
pub fps: fps_ticker::Fps,
}

impl DmabufHandler for AnvilState<WinitData> {
Expand Down Expand Up @@ -194,8 +192,6 @@ pub fn run_winit() {
damage_tracker,
dmabuf_state,
full_redraw: 0,
#[cfg(feature = "debug")]
fps: fps_ticker::Fps::default(),
}
};
let mut state = AnvilState::init(display, event_loop.handle(), data, true);
Expand Down Expand Up @@ -259,11 +255,6 @@ pub fn run_winit() {

pointer_element.set_status(state.cursor_status.clone());

#[cfg(feature = "debug")]
let fps = state.backend_data.fps.avg().round() as u32;
#[cfg(feature = "debug")]
fps_element.update_fps(fps);

let full_redraw = &mut state.backend_data.full_redraw;
*full_redraw = full_redraw.saturating_sub(1);
let space = &mut state.space;
Expand Down Expand Up @@ -448,6 +439,6 @@ pub fn run_winit() {
}

#[cfg(feature = "debug")]
state.backend_data.fps.tick();
fps_element.tick();
}
}
10 changes: 1 addition & 9 deletions anvil/src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ pub struct X11Data {
dmabuf_state: DmabufState,
_dmabuf_global: DmabufGlobal,
_dmabuf_default_feedback: DmabufFeedback,
#[cfg(feature = "debug")]
fps: fps_ticker::Fps,
}

impl DmabufHandler for AnvilState<X11Data> {
Expand Down Expand Up @@ -244,8 +242,6 @@ pub fn run_x11() {
dmabuf_state,
_dmabuf_global: dmabuf_global,
_dmabuf_default_feedback: dmabuf_default_feedback,
#[cfg(feature = "debug")]
fps: fps_ticker::Fps::default(),
};

let mut state = AnvilState::init(display, event_loop.handle(), data, true);
Expand Down Expand Up @@ -308,10 +304,6 @@ pub fn run_x11() {

let backend_data = &mut state.backend_data;
// We need to borrow everything we want to refer to inside the renderer callback otherwise rustc is unhappy.
#[cfg(feature = "debug")]
let fps = backend_data.fps.avg().round() as u32;
#[cfg(feature = "debug")]
fps_element.update_fps(fps);

let (buffer, age) = backend_data.surface.buffer().expect("gbm device was destroyed");
if let Err(err) = backend_data.renderer.bind(buffer) {
Expand Down Expand Up @@ -464,7 +456,7 @@ pub fn run_x11() {
}

#[cfg(feature = "debug")]
state.backend_data.fps.tick();
fps_element.tick();
window.set_cursor_visible(cursor_visible);
profiling::finish_frame!();
}
Expand Down

0 comments on commit c3ffa32

Please sign in to comment.