#[derive(Clone, Debug)]
pub struct ProfilingData {
pub glyph_cache_misses: u32,
pub glyph_cache_hits: u32,
pub glyphs_drawn: u32,
pub quads_drawn: u32,
pub glyphs_rasterized: u32,
}
impl ProfilingData {
const fn cleared() -> ProfilingData {
ProfilingData {
glyph_cache_misses: 0,
glyph_cache_hits: 0,
glyphs_drawn: 0,
quads_drawn: 0,
glyphs_rasterized: 0,
}
}
}
#[cfg(feature = "profiler")]
pub use actual::*;
#[cfg(not(feature = "profiler"))]
pub use dummy::*;
#[cfg(not(feature = "profiler"))]
mod dummy {
use super::ProfilingData;
static CLEARED_DATA: ProfilingData = ProfilingData::cleared();
pub fn refresh() {}
pub(crate) fn write<F: FnOnce(&mut ProfilingData) + Copy>(_f: F) {}
pub fn read() -> ProfilingData {
CLEARED_DATA.clone()
}
}
#[cfg(feature = "profiler")]
mod actual {
use super::ProfilingData;
use std::sync::Mutex;
impl ProfilingData {
fn copy_from(&mut self, other: &ProfilingData) {
*self = other.clone();
}
fn clear(&mut self) {
*self = ProfilingData::cleared();
}
}
lazy_static::lazy_static! {
static ref FRONT: Mutex<ProfilingData> = Mutex::new(ProfilingData::cleared());
static ref BACK: Mutex<ProfilingData> = Mutex::new(ProfilingData::cleared());
}
pub fn refresh() {
if let (Ok(ref mut front), Ok(ref mut back)) = (FRONT.lock(), BACK.lock()) {
let temp = back.glyphs_rasterized;
front.copy_from(back);
back.clear();
back.glyphs_rasterized = temp;
}
}
pub(crate) fn write<F: FnOnce(&mut ProfilingData) + Copy>(f: F) {
if let Ok(ref mut instance) = BACK.lock() {
f(instance);
}
}
pub fn read() -> ProfilingData {
if let Ok(instance) = FRONT.lock() {
instance.clone()
} else {
ProfilingData::cleared()
}
}
}