11// Copyright 2024 the Xilem Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- use crate :: tree_arena:: { ArenaMut , ArenaMutChildren } ;
4+ use tracing:: span:: EnteredSpan ;
5+
6+ use crate :: render_root:: RenderRootState ;
7+ use crate :: tree_arena:: { ArenaMut , ArenaMutChildren , ArenaRef } ;
58use crate :: widget:: WidgetArena ;
6- use crate :: { Widget , WidgetId , WidgetState } ;
9+ use crate :: { QueryCtx , Widget , WidgetId , WidgetState } ;
710
811pub ( crate ) mod accessibility;
912pub ( crate ) mod anim;
@@ -14,6 +17,35 @@ pub(crate) mod mutate;
1417pub ( crate ) mod paint;
1518pub ( crate ) mod update;
1619
20+ #[ must_use = "Span will be immediately closed if dropped" ]
21+ pub ( crate ) fn enter_span_if (
22+ enabled : bool ,
23+ global_state : & RenderRootState ,
24+ widget : ArenaRef < ' _ , Box < dyn Widget > > ,
25+ state : ArenaRef < ' _ , WidgetState > ,
26+ ) -> Option < EnteredSpan > {
27+ if enabled {
28+ Some ( enter_span ( global_state, widget, state) )
29+ } else {
30+ None
31+ }
32+ }
33+
34+ #[ must_use = "Span will be immediately closed if dropped" ]
35+ pub ( crate ) fn enter_span (
36+ global_state : & RenderRootState ,
37+ widget : ArenaRef < ' _ , Box < dyn Widget > > ,
38+ state : ArenaRef < ' _ , WidgetState > ,
39+ ) -> EnteredSpan {
40+ let ctx = QueryCtx {
41+ global_state,
42+ widget_state : state. item ,
43+ widget_state_children : state. children ,
44+ widget_children : widget. children ,
45+ } ;
46+ widget. item . make_trace_span ( & ctx) . entered ( )
47+ }
48+
1749pub ( crate ) fn recurse_on_children (
1850 id : WidgetId ,
1951 mut widget : ArenaMut < ' _ , Box < dyn Widget > > ,
@@ -68,6 +100,12 @@ pub(crate) fn merge_state_up(arena: &mut WidgetArena, widget_id: WidgetId) {
68100/// `MASONRY_TRACE_PASSES` environment variable.
69101///
70102/// Note that passes which are bounded by depth (rather than absolute size) are never filtered out here.
103+ ///
104+ /// Ideally, we'd cache the spans, which would make a lot (but not all) of this unnecessary.
105+ /// However, each pass uses a different parent span (with the individual pass's name), so it's
106+ /// (at best) non-trivial to make that work.
107+ ///
108+ /// We could *maybe* use a global parent span called "Pass", with a name field, but that's extremely ugly.
71109pub ( crate ) struct PassTracing {
72110 pub ( crate ) update_tree : bool ,
73111 pub ( crate ) anim : bool ,
0 commit comments