Skip to content

Commit

Permalink
Propagate previously registered scopes when creating GlobalFrameView (#…
Browse files Browse the repository at this point in the history
…206)

GlobalFrameView might be created after some scopes were already
registered and frame sink won't see them without prior propagation with
GlobalProfiler::emit_scope_snapshot().

### Checklist

* [X] I have read the [Contributor Guide](../CONTRIBUTING.md)
* [X] I have read and agree to the [Code of
Conduct](../CODE_OF_CONDUCT.md)
* [X] I have added a description of my changes and why I'd like them
included in the section below

### Description of Changes

I've modified init function of GlobalFrameView so that it calls
GlobalProfiler::emit_scope_snapshot() after the frame sink is registered
so that it can see previously registered scopes.

### Related Issues

I've noticed this issue when trying to render profiler UI conditionally
with being disabled by default and in result scopes that were registered
before the first call to the `GlobalProfiler::new_frame()` were not
propagated to the frame sink that was registered afterwards.

Simple reproduction code:
```rust
let mut render_profiler_ui = false;

[..]

if render_profiler_ui {
  puffin_egui::profiler_window(&ctx);
}

[..]

render_profiler_ui |= input.key_pressed(Key::F12);
```
  • Loading branch information
insertt authored Jul 31, 2024
1 parent 3f6d4ce commit 0b0ad3f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion puffin/src/profile_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ impl Default for GlobalFrameView {
fn default() -> Self {
let view = Arc::new(parking_lot::Mutex::new(FrameView::default()));
let view_clone = view.clone();
let sink_id = crate::GlobalProfiler::lock().add_sink(Box::new(move |frame| {
let mut profiler = crate::GlobalProfiler::lock();
let sink_id = profiler.add_sink(Box::new(move |frame| {
view_clone.lock().add_frame(frame);
}));
// GlobalFrameView might be created after scope scopes were already created
// and our registered sink won't see them without prior propagation.
profiler.emit_scope_snapshot();

Self { sink_id, view }
}
}
Expand Down

0 comments on commit 0b0ad3f

Please sign in to comment.