Skip to content

Commit

Permalink
Anvil FPS texture
Browse files Browse the repository at this point in the history
Compiles, should work.

Consider best way to abstract renderes, textures.
  • Loading branch information
ids1024 committed Dec 5, 2024
1 parent 372b958 commit aab4307
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
30 changes: 23 additions & 7 deletions anvil/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,19 @@ where
pub static FPS_NUMBERS_PNG: &[u8] = include_bytes!("../resources/numbers.png");

#[cfg(feature = "debug")]
#[derive(Debug, Clone)]
pub struct FpsElement<T: Texture> {
#[derive(Debug)]
pub struct Fps {
id: Id,
fps: fps_ticker::Fps,
value: u32,
texture: T,
commit_counter: CommitCounter,
}

#[cfg(feature = "debug")]
impl<T: Texture> FpsElement<T> {
pub fn new(texture: T) -> Self {
FpsElement {
impl Fps {
pub fn new() -> Self {
Fps {
id: Id::new(),
texture,
fps: fps_ticker::Fps::default(),
value: 0,
commit_counter: CommitCounter::default(),
Expand All @@ -152,6 +150,24 @@ impl<T: Texture> FpsElement<T> {
self.commit_counter.increment();
}
}

pub fn render_element<T: Texture>(&self, texture: T) -> FpsElement<T> {
FpsElement {
id: self.id.clone(),
value: self.value,
commit_counter: self.commit_counter,
texture,
}
}
}

#[cfg(feature = "debug")]
#[derive(Debug)]
pub struct FpsElement<T: Texture> {
id: Id,
value: u32,
commit_counter: CommitCounter,
texture: T,
}

#[cfg(feature = "debug")]
Expand Down
32 changes: 25 additions & 7 deletions anvil/src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub fn run_udev() {

for backend in state.backend_data.backends.values_mut() {
for surface in backend.surfaces.values_mut() {
surface.fps_element = Some(FpsElement::new(fps_texture.clone()));
surface.fps = Fps::new();
}
}
state.backend_data.fps_texture = Some(fps_texture);
Expand Down Expand Up @@ -824,7 +824,7 @@ struct SurfaceData {
global: Option<GlobalId>,
compositor: SurfaceComposition,
#[cfg(feature = "debug")]
fps_element: Option<FpsElement<MultiTexture>>,
fps: Fps,
dmabuf_feedback: Option<SurfaceDmabufFeedback>,
}

Expand Down Expand Up @@ -1102,7 +1102,7 @@ impl AnvilState<UdevData> {
});

#[cfg(feature = "debug")]
let fps_element = self.backend_data.fps_texture.clone().map(FpsElement::new);
let fps = Fps::new();

let allocator = GbmAllocator::new(
device.gbm.clone(),
Expand Down Expand Up @@ -1189,7 +1189,7 @@ impl AnvilState<UdevData> {
global: Some(global),
compositor,
#[cfg(feature = "debug")]
fps_element,
fps,
dmabuf_feedback,
};

Expand Down Expand Up @@ -1568,6 +1568,14 @@ impl AnvilState<UdevData> {
&self.dnd_icon,
&mut self.cursor_status,
self.show_window_preview,
#[cfg(feature = "debug")]
self.backend_data.fps_texture.clone().map(|texture| {
if let Texture::Multi(texture) = texture {
texture
} else {
panic!("Invalid texture for renderer")
}
}),
),
RendererRef::Pixman(renderer) => render_surface::<_, PixmanRenderBuffer>(
surface,
Expand All @@ -1580,6 +1588,14 @@ impl AnvilState<UdevData> {
&self.dnd_icon,
&mut self.cursor_status,
self.show_window_preview,
#[cfg(feature = "debug")]
self.backend_data.fps_texture.clone().map(|texture| {
if let Texture::Pixman(texture) = texture {
texture
} else {
panic!("Invalid texture for renderer")
}
}),
),
};
let reschedule = match result {
Expand Down Expand Up @@ -1697,6 +1713,7 @@ fn render_surface<'a, R, Target>(
dnd_icon: &Option<DndIcon>,
cursor_status: &mut CursorImageStatus,
show_window_preview: bool,
#[cfg(feature = "debug")] fps_texture: Option<R::TextureId>,
) -> Result<(bool, RenderElementStates), SwapBuffersError>
where
R: Renderer
Expand Down Expand Up @@ -1779,10 +1796,11 @@ where
}

#[cfg(feature = "debug")]
if let Some(element) = surface.fps_element.as_mut() {
custom_elements.push(CustomRenderElements::Fps(element.clone()));
element.tick();
if let Some(texture) = fps_texture {
custom_elements.push(CustomRenderElements::Fps(surface.fps.render_element(texture)));
}
#[cfg(feature = "debug")]
surface.fps.tick();

let (elements, clear_color) =
output_elements(output, space, custom_elements, renderer, show_window_preview);
Expand Down
6 changes: 3 additions & 3 deletions anvil/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn run_winit() {
)
.expect("Unable to upload FPS texture");
#[cfg(feature = "debug")]
let mut fps_element = FpsElement::new(fps_texture);
let mut fps = Fps::new();

let render_node = EGLDevice::device_for_display(backend.renderer().egl_context().display())
.and_then(|device| device.try_get_render_node());
Expand Down Expand Up @@ -337,7 +337,7 @@ pub fn run_winit() {
}

#[cfg(feature = "debug")]
elements.push(CustomRenderElements::Fps(fps_element.clone()));
elements.push(CustomRenderElements::Fps(fps.render_element(fps_texture.clone())));

render_output(
&output,
Expand Down Expand Up @@ -439,6 +439,6 @@ pub fn run_winit() {
}

#[cfg(feature = "debug")]
fps_element.tick();
fps.tick();
}
}
6 changes: 3 additions & 3 deletions anvil/src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn run_x11() {
)
.expect("Unable to upload FPS texture");
#[cfg(feature = "debug")]
let mut fps_element = FpsElement::new(fps_texture);
let mut fps = Fps::new();
let output = Output::new(
OUTPUT_NAME.to_string(),
PhysicalProperties {
Expand Down Expand Up @@ -378,7 +378,7 @@ pub fn run_x11() {
}

#[cfg(feature = "debug")]
elements.push(CustomRenderElements::Fps(fps_element.clone()));
elements.push(CustomRenderElements::Fps(fps.render_element(fps_texture.clone())));

let render_res = render_output(
&output,
Expand Down Expand Up @@ -456,7 +456,7 @@ pub fn run_x11() {
}

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

0 comments on commit aab4307

Please sign in to comment.