cosmic-comp/src/debug.rs

357 lines
15 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
use std::collections::HashMap;
2023-02-13 19:05:42 +01:00
use crate::{
2024-06-07 20:07:16 +02:00
backend::kms::Timings,
2024-03-18 20:22:07 -07:00
shell::focus::target::{KeyboardFocusTarget, PointerFocusTarget},
2023-02-13 19:05:42 +01:00
state::{Common, Fps},
};
use egui::{load::SizedTexture, Color32, Vec2};
use smithay::{
2022-11-17 20:32:54 +01:00
backend::{
drm::DrmNode,
renderer::{
element::texture::TextureRenderElement,
2023-05-03 19:47:48 +02:00
gles::{GlesError, GlesTexture},
2022-11-17 20:32:54 +01:00
glow::GlowRenderer,
},
},
2024-03-18 20:22:07 -07:00
desktop::WindowSurface,
2023-02-13 19:05:42 +01:00
input::keyboard::xkb,
reexports::wayland_server::Resource,
2022-11-22 09:57:29 +01:00
utils::{Logical, Rectangle},
};
2022-11-18 17:20:52 +01:00
pub const ELEMENTS_COLOR: Color32 = Color32::from_rgb(70, 198, 115);
pub const RENDER_COLOR: Color32 = Color32::from_rgb(29, 114, 58);
2024-06-07 20:07:16 +02:00
pub const SUBMITTED_COLOR: Color32 = Color32::from_rgb(253, 178, 39);
2022-11-18 17:20:52 +01:00
pub const DISPLAY_COLOR: Color32 = Color32::from_rgb(41, 184, 209);
2024-06-07 20:07:16 +02:00
pub fn fps_ui<'a>(
gpu: Option<&DrmNode>,
2024-06-07 20:07:16 +02:00
debug_active: bool,
seats: impl Iterator<Item = &'a Seat<State>>,
2022-11-17 20:32:54 +01:00
renderer: &mut GlowRenderer,
2024-06-07 20:07:16 +02:00
state: &EguiState,
timings: &Timings,
2022-11-17 20:32:54 +01:00
area: Rectangle<i32, Logical>,
2022-01-18 19:42:56 +01:00
scale: f64,
2023-05-03 19:47:48 +02:00
) -> Result<TextureRenderElement<GlesTexture>, GlesError> {
use egui_plot::{Bar, BarChart, Legend, Plot};
2022-02-04 21:08:11 +01:00
let (max, min, avg, avg_fps) = (
2024-06-07 20:07:16 +02:00
timings.max_frametime().as_secs_f64(),
timings.min_frametime().as_secs_f64(),
timings.avg_frametime().as_secs_f64(),
timings.avg_fps(),
2022-02-04 21:08:11 +01:00
);
2022-11-18 17:20:52 +01:00
let (max_disp, min_disp) = (
2024-06-07 20:07:16 +02:00
timings.max_time_to_display().as_secs_f64(),
timings.min_time_to_display().as_secs_f64(),
2022-11-18 17:20:52 +01:00
);
let amount = avg_fps.round() as usize * 2;
2022-11-18 17:20:52 +01:00
let ((bars_elements, bars_render), (bars_screencopy, bars_displayed)): (
(Vec<Bar>, Vec<Bar>),
(Vec<Bar>, Vec<Bar>),
2024-06-07 20:07:16 +02:00
) = timings
.previous_frames
2022-02-04 21:08:11 +01:00
.iter()
.rev()
2022-11-17 20:32:54 +01:00
.take(amount)
2022-02-04 21:08:11 +01:00
.rev()
.enumerate()
2022-11-18 17:20:52 +01:00
.map(|(i, frame)| {
2024-06-07 20:07:16 +02:00
let elements_val = frame.render_duration_elements.as_secs_f64();
let render_val = frame.render_duration_draw.as_secs_f64();
let submitted_val = frame
.presentation_submitted
2022-11-18 17:20:52 +01:00
.as_ref()
.map(|val| val.as_secs_f64())
.unwrap_or(0.0);
let displayed_val = frame.duration_displayed.as_secs_f64();
2022-11-17 20:32:54 +01:00
2022-11-18 17:20:52 +01:00
let transformed_elements =
((elements_val - min_disp) / (max_disp - min_disp) * 255.0).round() as u8;
let transformed_render =
((render_val - min_disp) / (max_disp - min_disp) * 255.0).round() as u8;
let transformed_screencopy =
((screencopy_val - min_disp) / (max_disp - min_disp) * 255.0).round() as u8;
let transformed_displayed =
((displayed_val - min_disp) / (max_disp - min_disp) * 255.0).round() as u8;
(
(
Bar::new(i as f64, transformed_elements as f64).fill(ELEMENTS_COLOR),
Bar::new(i as f64, transformed_render as f64).fill(RENDER_COLOR),
),
(
Bar::new(i as f64, transformed_screencopy as f64).fill(SCREENCOPY_COLOR),
Bar::new(i as f64, transformed_displayed as f64).fill(DISPLAY_COLOR),
),
)
2022-02-04 21:08:11 +01:00
})
2022-11-18 17:20:52 +01:00
.unzip();
2022-02-04 21:08:11 +01:00
let vendors = HashMap::from([
(
"0x10de",
2024-06-07 20:07:16 +02:00
state
.with_image(renderer, "nvidia", |image, ctx| {
(image.texture_id(ctx), image.size_vec2())
})
.expect("Logo images not loaded?"),
),
(
"0x1002",
2024-06-07 20:07:16 +02:00
state
.with_image(renderer, "amd", |image, ctx| {
(image.texture_id(ctx), image.size_vec2())
})
.expect("Logo images not loaded?"),
),
(
"0x8086",
2024-06-07 20:07:16 +02:00
state
.with_image(renderer, "intel", |image, ctx| {
(image.texture_id(ctx), image.size_vec2())
})
.expect("Logo images not loaded?"),
),
]);
2024-06-07 20:07:16 +02:00
state.render(
|ctx| {
egui::Area::new("main")
.anchor(egui::Align2::LEFT_TOP, (10.0, 10.0))
.show(ctx, |ui| {
2022-11-18 17:20:52 +01:00
ui.label(format!(
"cosmic-comp version {}",
std::env!("CARGO_PKG_VERSION")
));
2022-11-17 20:32:54 +01:00
if let Some(hash) = std::option_env!("GIT_HASH").and_then(|x| x.get(0..10)) {
2022-11-22 09:57:29 +01:00
ui.label(format!(": {hash}"));
}
2024-06-07 20:07:16 +02:00
if !debug_active {
2022-11-22 09:57:29 +01:00
ui.label("Press Super+Escape for debug overlay");
} else {
2022-11-18 17:20:52 +01:00
ui.set_max_width(300.0);
ui.separator();
if let Some(gpu) = gpu {
ui.horizontal(|ui| {
let resp = ui.label(
egui::RichText::new(format!("renderD{}", gpu.minor())).code(),
);
if let Ok(vendor) = std::fs::read_to_string(format!(
"/sys/class/drm/renderD{}/device/vendor",
gpu.minor()
)) {
if let Some((texture_id, mut size)) = vendors.get(vendor.trim())
{
let factor = resp.rect.height() / size.y;
size = Vec2::from([size.x * factor, resp.rect.height()]);
ui.image(SizedTexture::new(*texture_id, size));
}
}
});
}
ui.label(egui::RichText::new(format!("FPS: {:>7.3}", avg_fps)).heading());
ui.label("Frame Times:");
ui.label(egui::RichText::new(format!("avg: {:>7.6}", avg)).code());
ui.label(egui::RichText::new(format!("min: {:>7.6}", min)).code());
ui.label(egui::RichText::new(format!("max: {:>7.6}", max)).code());
2022-11-18 17:20:52 +01:00
let elements_chart = BarChart::new(bars_elements).vertical();
let render_chart = BarChart::new(bars_render)
.stack_on(&[&elements_chart])
.vertical();
let screencopy_chart = BarChart::new(bars_screencopy)
.stack_on(&[&elements_chart, &render_chart])
.vertical();
let display_chart = BarChart::new(bars_displayed)
.stack_on(&[&elements_chart, &render_chart, &screencopy_chart])
.vertical();
Plot::new("FPS")
.legend(Legend::default())
2022-11-17 20:32:54 +01:00
.view_aspect(50.0)
.include_x(0.0)
2022-11-17 20:32:54 +01:00
.include_x(amount as f64)
.include_y(0.0)
2022-11-17 20:32:54 +01:00
.include_y(300)
.show_x(false)
.show(ui, |plot_ui| {
2022-11-18 17:20:52 +01:00
plot_ui.bar_chart(elements_chart);
plot_ui.bar_chart(render_chart);
plot_ui.bar_chart(screencopy_chart);
plot_ui.bar_chart(display_chart);
});
2023-01-30 15:53:30 +01:00
ui.separator();
ui.label(egui::RichText::new("Input States").heading());
2024-06-07 20:07:16 +02:00
for seat in seats {
2023-02-13 19:05:42 +01:00
ui.label(egui::RichText::new(format!("\t{}", seat.name())).strong());
2023-01-30 15:53:30 +01:00
if let Some(ptr) = seat.get_pointer() {
2023-02-13 19:05:42 +01:00
egui::Frame::none()
.fill(egui::Color32::DARK_GRAY)
.rounding(5.)
.inner_margin(10.)
.show(ui, |ui| {
ui.label(
egui::RichText::new(format!(
"Pos: {:?}",
ptr.current_location()
))
.code(),
);
ui.label(
egui::RichText::new(format!(
"Focus: {}",
format_pointer_focus(ptr.current_focus())
))
.code(),
);
ui.label(
egui::RichText::new(format!(
"Grabbed: {:?}",
ptr.is_grabbed()
))
.code(),
);
});
2023-01-30 15:53:30 +01:00
}
if let Some(kbd) = seat.get_keyboard() {
2023-02-13 19:05:42 +01:00
egui::Frame::none()
.fill(egui::Color32::DARK_GRAY)
.rounding(5.)
.inner_margin(10.)
.show(ui, |ui| {
let mut keysyms = format!(
"Keys: {:?}",
kbd.with_pressed_keysyms(|syms| syms
.into_iter()
.map(|k| xkb::keysym_get_name(k.modified_sym()))
.fold(String::new(), |mut list, val| {
list.push_str(&format!("{}, ", val));
list
}))
);
keysyms.truncate(keysyms.len().saturating_sub(2));
ui.label(egui::RichText::new(keysyms).code());
let mods = kbd.modifier_state();
ui.label(
egui::RichText::new(format!(
"Mods: Ctrl {} / Alt {} / Logo {} / Shift {}",
mods.ctrl, mods.alt, mods.logo, mods.shift,
))
.code(),
);
ui.label(
egui::RichText::new(format!(
"Focus: {}",
format_keyboard_focus(kbd.current_focus())
))
.code(),
);
ui.label(
egui::RichText::new(format!(
"Grabbed: {:?}",
kbd.is_grabbed()
))
.code(),
);
});
2023-01-30 15:53:30 +01:00
}
}
}
});
2022-02-04 21:08:11 +01:00
},
2022-11-17 20:32:54 +01:00
renderer,
2022-02-04 21:08:11 +01:00
area,
scale,
2022-11-17 20:32:54 +01:00
0.8,
2022-02-04 21:08:11 +01:00
)
}
2023-02-13 19:05:42 +01:00
fn format_pointer_focus(focus: Option<PointerFocusTarget>) -> String {
use PointerFocusTarget::*;
match focus {
Some(Element(x)) => match x {
x if x.is_stack() => format!(
"Stacked Window {} ({})",
2024-03-18 20:22:07 -07:00
match x.active_window().0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
x.active_window().title()
),
x if x.is_window() => format!(
"Window {} ({})",
2024-03-18 20:22:07 -07:00
match x.active_window().0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
x.active_window().title()
),
_ => unreachable!(),
},
Some(Fullscreen(x)) => format!(
"Fullscreen {} ({})",
2024-03-18 20:22:07 -07:00
match x.0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
2023-10-20 15:22:03 -07:00
x.title()
2023-02-13 19:05:42 +01:00
),
Some(LayerSurface(x)) => format!("LayerSurface {}", x.wl_surface().id().protocol_id()),
Some(Popup(x)) => format!("Popup {}", x.wl_surface().id().protocol_id()),
Some(OverrideRedirect(x)) => format!("Override Redirect {}", x.window_id()),
Some(PointerFocusTarget::ResizeFork(x)) => format!("Resize Fork {:?}", x.node),
2023-10-20 15:22:03 -07:00
Some(LockSurface(x)) => format!("LockSurface {}", x.wl_surface().id().protocol_id()),
2023-02-13 19:05:42 +01:00
None => format!("None"),
}
}
fn format_keyboard_focus(focus: Option<KeyboardFocusTarget>) -> String {
use KeyboardFocusTarget::*;
match focus {
Some(Element(x)) => match x {
x if x.is_stack() => format!(
"Stacked Window {} ({})",
2024-03-18 20:22:07 -07:00
match x.active_window().0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
x.active_window().title()
),
x if x.is_window() => format!(
"Window {} ({})",
2024-03-18 20:22:07 -07:00
match x.active_window().0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
x.active_window().title()
),
_ => unreachable!(),
},
Some(Fullscreen(x)) => format!(
"Fullscreen {} ({})",
2024-03-18 20:22:07 -07:00
match x.0.underlying_surface() {
WindowSurface::Wayland(t) => t.wl_surface().id().protocol_id(),
WindowSurface::X11(x) => x.window_id(),
2023-02-13 19:05:42 +01:00
},
2023-10-20 15:22:03 -07:00
x.title()
2023-02-13 19:05:42 +01:00
),
Some(LayerSurface(x)) => format!("LayerSurface {}", x.wl_surface().id().protocol_id()),
Some(Popup(x)) => format!("Popup {}", x.wl_surface().id().protocol_id()),
Some(Group(_)) => format!("Window Group"),
2023-10-20 15:22:03 -07:00
Some(LockSurface(x)) => format!("LockSurface {}", x.wl_surface().id().protocol_id()),
2023-02-13 19:05:42 +01:00
None => format!("None"),
}
}