#![cfg_attr(not(feature = "perf_profile"), allow(dead_code))]
use std::{
sync::{Arc, atomic::Ordering},
time::Duration,
};
use super::{
FrameProfile, ProfilingHandle, detail,
model::*,
phase_slot::{Entered, PhaseSlot},
};
use crate::{app::PassReport, time::Instant};
#[derive(Clone)]
pub struct ProfileContext {
handle: ProfilingHandle,
window: Arc<str>,
pub(super) phase: Option<Arc<PhaseSlot>>,
frame_id: Option<u64>,
}
impl ProfileContext {
#[cfg(all(feature = "perf_profile", any(feature = "canvas", feature = "wgpu")))]
pub(crate) fn capture_active(&self) -> bool {
self.handle.inner.active.load(Ordering::Acquire)
}
#[cfg(all(feature = "wgpu", feature = "perf_profile"))]
pub(crate) fn frame_id(&self) -> Option<u64> {
self.frame_id
}
#[cfg(all(
feature = "perf_profile",
any(feature = "wgpu", all(feature = "dx12", target_os = "windows"))
))]
pub(crate) fn same_window(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.handle.inner, &other.handle.inner) && Arc::ptr_eq(&self.window, &other.window)
}
#[cfg(all(
feature = "perf_profile",
any(feature = "wgpu", all(feature = "dx12", target_os = "windows"))
))]
pub(crate) fn report_gpu_timestamps(&self, availability: GpuTimestampAvailability) {
self.handle.set_gpu_timestamps(&self.window, availability);
}
#[cfg(all(feature = "wgpu", feature = "perf_profile"))]
pub(crate) fn record_gpu(&self, frame_id: u64, gpu: GpuTiming) {
self.handle.record_gpu(&self.window, frame_id, gpu);
}
#[cfg(all(feature = "canvas", feature = "perf_profile"))]
pub(crate) fn canvas_text_scope(&self) -> super::canvas_text::PassScope {
super::canvas_text::PassScope::new(self.capture_active())
}
pub fn phase(&self, phase: Phase) -> PhaseGuard {
let entered = self
.phase
.as_ref()
.map(|slot| slot.enter(self.frame_id, phase))
.unwrap_or_default();
PhaseGuard {
slot: self.phase.clone(),
entered,
}
}
#[cfg(all(
feature = "canvas",
any(feature = "wgpu", all(feature = "dx12", target_os = "windows"))
))]
pub(crate) fn detail_phase(&self, phase: Phase) -> Option<PhaseGuard> {
self
.handle
.inner
.active
.load(Ordering::Acquire)
.then(|| self.phase(phase))
}
pub(crate) fn update(&self, kind: UiUpdateKind) -> UpdateGuard {
let phase = self.phase(Phase::Rebuild);
let detail = self
.handle
.inner
.detailed
.load(Ordering::Acquire)
.then(|| detail::UpdateScope::begin(self.phase.clone(), self.frame_id));
UpdateGuard {
context: self.clone(),
kind,
started: Instant::now(),
commit_started: None,
_phase: phase,
commit_phase: None,
detail,
}
}
}
#[cfg(feature = "perf_profile")]
pub(crate) struct InputGuard {
context: ProfileContext,
kind: InputKind,
received: Instant,
started: Instant,
wants_frame: bool,
_phase: PhaseGuard,
}
#[cfg(feature = "perf_profile")]
impl Drop for InputGuard {
fn drop(&mut self) {
self.context.handle.input_dispatched(
&self.context.window,
super::collector::DispatchedInput {
kind: self.kind,
frame_id: self.context.frame_id,
received: self.received,
dispatch_started: self.started,
dispatch: self.started.elapsed(),
},
self.wants_frame,
);
}
}
pub(crate) struct UpdateGuard {
context: ProfileContext,
kind: UiUpdateKind,
started: Instant,
commit_started: Option<Instant>,
_phase: PhaseGuard,
commit_phase: Option<PhaseGuard>,
detail: Option<detail::UpdateScope>,
}
impl UpdateGuard {
pub(crate) fn commit(&mut self) {
self.commit_started = Some(Instant::now());
self.commit_phase = Some(self.context.phase(Phase::Commit));
}
}
impl Drop for UpdateGuard {
fn drop(&mut self) {
let causes = self.detail.take().map(detail::UpdateScope::finish).unwrap_or_default();
self.context.handle.record(
&self.context.window,
self.started,
SampleData::UiUpdate(UiUpdateSample {
frame_id: self.context.frame_id,
kind: self.kind,
total: self.started.elapsed(),
commit: self.commit_started.map(|start| start.elapsed()).unwrap_or_default(),
causes,
}),
);
self.commit_phase.take();
}
}
pub struct PhaseGuard {
slot: Option<Arc<PhaseSlot>>,
entered: Entered,
}
impl Drop for PhaseGuard {
fn drop(&mut self) {
if let Some(slot) = &self.slot {
slot.exit(self.entered);
}
}
}
pub(crate) struct WindowProfiler {
pub(crate) context: ProfileContext,
root: bool,
layout: Duration,
layout_compute: Duration,
component_after_layout: Duration,
canvas_recording: Duration,
canvas_preparation: Duration,
pass_entry: Entered,
received: Option<Instant>,
wanted: Option<Instant>,
continuing_from: Option<Instant>,
wants_next_frame: bool,
presented_at: Option<Instant>,
work_ended: Option<Instant>,
refresh: Option<Duration>,
presents: PresentTracker,
captured: bool,
screenshot: bool,
#[cfg(feature = "perf_profile")]
allocations_start: Option<super::AllocationSnapshot>,
counters: Option<PassCounters>,
detailed: bool,
component_layouts: Vec<ComponentLayouts>,
}
impl WindowProfiler {
pub(crate) fn new() -> Self {
let handle = ProfilingHandle::new();
let phase = handle.register("main", false);
Self {
context: ProfileContext {
handle,
window: "main".into(),
phase,
frame_id: None,
},
root: true,
layout: Duration::ZERO,
layout_compute: Duration::ZERO,
component_after_layout: Duration::ZERO,
canvas_recording: Duration::ZERO,
canvas_preparation: Duration::ZERO,
pass_entry: Entered::default(),
received: None,
wanted: None,
continuing_from: None,
wants_next_frame: false,
presented_at: None,
work_ended: None,
refresh: None,
presents: PresentTracker::default(),
captured: false,
screenshot: false,
#[cfg(feature = "perf_profile")]
allocations_start: None,
counters: None,
detailed: false,
component_layouts: Vec::new(),
}
}
pub(crate) fn capturing(&self) -> bool {
self.context.handle.inner.active.load(Ordering::Acquire)
}
pub(crate) fn stamp_input(&mut self, received: Option<Instant>) {
self.received = received;
}
#[cfg(feature = "perf_profile")]
pub(crate) fn input(&mut self, kind: InputKind) -> InputGuard {
let started = Instant::now();
InputGuard {
context: self.context.clone(),
kind,
received: self.received.take().unwrap_or(started),
started,
wants_frame: true,
_phase: self.context.phase(Phase::InputDispatch),
}
}
#[cfg(feature = "perf_profile")]
pub(crate) fn end_input(&mut self, mut input: InputGuard, wants_frame: bool) {
input.wants_frame = wants_frame;
if wants_frame {
self.wanted = Some(self.wanted.map_or(input.received, |wanted| wanted.min(input.received)));
}
}
pub(crate) fn set_refresh_interval(&mut self, refresh: Option<Duration>) {
self.refresh = refresh;
}
pub(crate) fn presented(&mut self, at: Instant) {
self.presented_at = Some(at);
}
pub(crate) fn set_wants_next_frame(&mut self, wants: bool) {
self.wants_next_frame = wants;
}
pub(crate) fn attach(&mut self, parent: &Self, id: String, devtools: bool) {
if self.root {
self.context.handle.close();
} else {
self.context.handle.set_open(&self.context.window, false);
}
let handle = parent.context.handle.clone();
let phase = handle.register(&id, devtools);
self.context = ProfileContext {
handle,
window: id.into(),
phase,
frame_id: None,
};
self.root = false;
}
pub(crate) fn handle(&self) -> ProfilingHandle {
self.context.handle.clone()
}
pub(crate) fn child_id(&self, id: u64) -> String {
if self.context.window.as_ref() == "main" {
format!("w{id}")
} else {
format!("{}/w{id}", self.context.window)
}
}
pub(crate) fn set_open(&self, open: bool) {
self.context.handle.set_open(&self.context.window, open);
}
#[cfg(feature = "devtools")]
pub(crate) fn mark_devtools(&self) {
self.context.handle.mark_devtools(&self.context.window);
}
pub(crate) fn begin_pass(&mut self, frame_id: u64) -> (Instant, PhaseGuard) {
self.layout = Duration::ZERO;
self.layout_compute = Duration::ZERO;
self.component_after_layout = Duration::ZERO;
self.canvas_recording = Duration::ZERO;
self.canvas_preparation = Duration::ZERO;
self.context.frame_id = Some(frame_id);
self.captured = self.context.handle.inner.active.load(Ordering::Acquire);
self.screenshot = false;
self.counters = None;
self.detailed = self.captured && self.context.handle.inner.detailed.load(Ordering::Acquire);
if let Some(slot) = &self.context.phase {
slot.begin_spans(self.captured, self.detailed);
}
let guard = self.context.phase(Phase::PassSetup);
self.pass_entry = guard.entered;
#[cfg(feature = "perf_profile")]
{
self.allocations_start = if self.captured {
super::allocation_snapshot()
} else {
None
};
}
(Instant::now(), guard)
}
pub(crate) fn end_pass_work(&mut self) {
self.work_ended = Some(Instant::now());
}
pub(crate) fn captured(&self) -> bool {
self.captured
}
#[cfg(all(feature = "perf_profile", feature = "screenshot"))]
pub(crate) fn screenshot_target(&mut self) -> Option<crate::app::render_engine::RenderCaptureTarget> {
if !self.captured {
return None;
}
let requested = Instant::now();
let options = self.context.handle.screenshot_due(&self.context.window, requested)?;
self.screenshot = true;
let handle = self.context.handle.clone();
let window = self.context.window.clone();
let frame_id = self.context.frame_id.unwrap_or_default();
Some(crate::app::render_engine::RenderCaptureTarget::Bytes(Arc::new(
move |frame: Result<crate::app::render_engine::CapturedFrame, String>| {
let Ok(frame) = frame else {
return;
};
let scaled = super::screenshot::downscale_rgba(
frame.rgba,
frame.width,
frame.height,
options.max_width,
options.max_height,
);
handle.screenshot_taken(&window, frame_id, requested, scaled);
},
)))
}
#[cfg(feature = "perf_profile")]
pub(crate) fn pass_allocations(&self) -> Option<PassAllocations> {
let start = self.allocations_start?;
Some(super::allocation_snapshot()?.since(start))
}
pub(crate) fn set_counters(&mut self, counters: PassCounters) {
self.counters = Some(counters);
}
pub(crate) fn detailed(&self) -> bool {
self.detailed
}
pub(crate) fn hook_spans(&self) -> Option<detail::HookSpans> {
if !self.detailed {
return None;
}
let slot = self.context.phase.clone()?;
Some(detail::HookSpans::new(slot, self.context.frame_id))
}
pub(crate) fn layout(&mut self, duration: Duration) {
self.layout = duration;
}
pub(crate) fn component_after_layout(&mut self, duration: Duration) {
self.component_after_layout += duration;
}
#[cfg(feature = "canvas")]
pub(crate) fn canvas_recording(&mut self, duration: Duration) {
self.canvas_recording += duration;
}
#[cfg(feature = "canvas")]
pub(crate) fn canvas_preparation(&mut self, duration: Duration) {
self.canvas_preparation += duration;
}
pub(crate) fn component_layouts(&mut self, counts: Vec<(Option<Arc<str>>, &'static str, u32)>) {
self.component_layouts = component_layouts_of(counts);
}
pub(crate) fn finish_pass(
&mut self,
start: Instant,
report: &PassReport,
backend: &'static str,
frame: Option<&FrameProfile>,
) {
let (spans, dropped_spans) = self
.context
.phase
.as_ref()
.and_then(|slot| slot.finish_spans(self.pass_entry, report.required))
.unwrap_or_default();
self.detailed = false;
let component_layouts = std::mem::take(&mut self.component_layouts);
let presented_at = self.presented_at.take();
let work_ended = self.work_ended.take();
let frame_id = self.context.frame_id.take();
let presented = report
.rendered
.then(|| (frame_id.unwrap(), presented_at.unwrap_or_else(Instant::now)));
let present = presented.map(|(_, at)| {
let wanted = self.continuing_from.into_iter().chain(self.wanted).min();
let (interval, continuous, missed_vsyncs) = self.presents.present(at, wanted, self.refresh);
self.continuing_from = self.wants_next_frame.then_some(at);
self.wanted = None;
PresentTiming {
presented_ms: self.context.handle.millis(at),
interval,
refresh_interval: self.refresh,
continuous,
missed_vsyncs,
}
});
self.wants_next_frame = false;
if report.required {
self.context.handle.record(
&self.context.window,
start,
SampleData::Pass(PassSample {
frame_id: presented.map(|(frame_id, _)| frame_id),
rendered: report.rendered,
cached_render_list: report.used_cached_render_list,
total: work_ended.map_or_else(|| start.elapsed(), |ended| ended.saturating_duration_since(start)),
layout_update: self.layout,
layout_compute: self.layout_compute,
component_after_layout: self.component_after_layout,
layout_recalculated: report.layout_recalculated,
canvas_recording: self.canvas_recording,
canvas_preparation: self.canvas_preparation,
canvas_text: {
#[cfg(all(feature = "canvas", feature = "perf_profile"))]
{
super::canvas_text::snapshot()
}
#[cfg(not(all(feature = "canvas", feature = "perf_profile")))]
{
None
}
},
backend,
frame: frame.cloned(),
spans,
dropped_spans,
reasons: report.reasons,
present,
counters: self.counters,
screenshot: self.screenshot && report.rendered,
gpu: None,
component_layouts,
}),
);
}
self.counters = None;
self.screenshot = false;
if presented.is_some() || !report.required {
#[cfg(feature = "perf_profile")]
self.context.handle.inputs_presented(&self.context.window, presented);
self.wanted = None;
}
if !report.required {
self.continuing_from = None;
}
}
pub(crate) fn break_run(&mut self) {
self.continuing_from = None;
self.wanted = None;
}
}
fn component_layouts_of(counts: Vec<(Option<Arc<str>>, &'static str, u32)>) -> Vec<ComponentLayouts> {
let mut layouts: Vec<ComponentLayouts> = Vec::new();
for (tag, reason, nodes) in counts {
let component = tag.as_deref().map(|tag| detail::intern(tag).unwrap_or("(unnamed)"));
match layouts
.iter_mut()
.find(|layout| layout.component == component && layout.reason == reason)
{
Some(layout) => layout.nodes = layout.nodes.saturating_add(nodes),
None => layouts.push(ComponentLayouts {
component,
reason,
nodes,
}),
}
}
layouts.sort_by(|a, b| b.nodes.cmp(&a.nodes).then(a.component.cmp(&b.component)));
layouts
}
#[inline]
pub(crate) fn layout_compute<R>(
#[cfg(feature = "perf_profile")] profiler: &mut WindowProfiler,
compute: impl FnOnce() -> R,
) -> R {
#[cfg(feature = "perf_profile")]
let started = Instant::now();
#[cfg(feature = "perf_profile")]
let _phase = profiler.context.phase(Phase::LayoutCompute);
let result = compute();
#[cfg(feature = "perf_profile")]
{
profiler.layout_compute += started.elapsed();
}
result
}
impl Drop for WindowProfiler {
fn drop(&mut self) {
self.set_open(false);
if self.root {
self.context.handle.close();
}
}
}