mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 15:13:12 -04:00
Hey! I am not sure if this is something that's been considered before and decided against (I couldn't find any PR's or issues). This change removes the internal profiling macros in library crates and the `puffin` feature and replaces it with similar functions in the [profiling](https://github.com/aclysma/profiling) crate. This crate provides a layer of abstraction over various profiler instrumentation crates and allows library users to pick their favorite (supported) profiler. An additional benefit for puffin users is that dependencies of egui are included in the instrumentation output too (mainly wgpu which uses the profiling crate), so more details might be available when profiling. A breaking change is that instead of using the `puffin` feature on egui, users that want to profile the crate with puffin instead have to enable the `profile-with-puffin` feature on the profiling crate. Similarly they could instead choose to use `profile-with-tracy` etc. I tried to add a 'tracy' feature to egui_demo_app in order to showcase , however the /scripts/check.sh currently breaks on mutually exclusive features (which this introduces), so I decided against including it for the initial PR. I'm happy to iterate more on this if there is interest in taking this PR though. Screenshot showing the additional info for wgpu now available when using puffin 
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
//! Helpers for loading [`egui::IconData`].
|
|
|
|
use egui::IconData;
|
|
|
|
/// Helpers for working with [`IconData`].
|
|
pub trait IconDataExt {
|
|
/// Convert into [`image::RgbaImage`]
|
|
///
|
|
/// # Errors
|
|
/// If `width*height != 4 * rgba.len()`, or if the image is too big.
|
|
fn to_image(&self) -> Result<image::RgbaImage, String>;
|
|
|
|
/// Encode as PNG.
|
|
///
|
|
/// # Errors
|
|
/// The image is invalid, or the PNG encoder failed.
|
|
fn to_png_bytes(&self) -> Result<Vec<u8>, String>;
|
|
}
|
|
|
|
/// Load the contents of .png file.
|
|
///
|
|
/// # Errors
|
|
/// If this is not a valid png.
|
|
pub fn from_png_bytes(png_bytes: &[u8]) -> Result<IconData, image::ImageError> {
|
|
profiling::function_scope!();
|
|
let image = image::load_from_memory(png_bytes)?;
|
|
Ok(from_image(image))
|
|
}
|
|
|
|
fn from_image(image: image::DynamicImage) -> IconData {
|
|
let image = image.into_rgba8();
|
|
IconData {
|
|
width: image.width(),
|
|
height: image.height(),
|
|
rgba: image.into_raw(),
|
|
}
|
|
}
|
|
|
|
impl IconDataExt for IconData {
|
|
fn to_image(&self) -> Result<image::RgbaImage, String> {
|
|
profiling::function_scope!();
|
|
let Self {
|
|
rgba,
|
|
width,
|
|
height,
|
|
} = self.clone();
|
|
image::RgbaImage::from_raw(width, height, rgba).ok_or_else(|| "Invalid IconData".to_owned())
|
|
}
|
|
|
|
fn to_png_bytes(&self) -> Result<Vec<u8>, String> {
|
|
profiling::function_scope!();
|
|
let image = self.to_image()?;
|
|
let mut png_bytes: Vec<u8> = Vec::new();
|
|
image
|
|
.write_to(
|
|
&mut std::io::Cursor::new(&mut png_bytes),
|
|
image::ImageFormat::Png,
|
|
)
|
|
.map_err(|err| err.to_string())?;
|
|
Ok(png_bytes)
|
|
}
|
|
}
|