Skip to main content

velox_engine/
devtools.rs

1//! 内置 DevTools Inspector 调试透视层与性能遥测(100% `#![no_std]` 零堆核心)。
2//!
3//! 可在任意业务界面根容器上方一键叠放:
4//! - **盒模型透视**:实时高亮鼠标悬浮元素的边界框与坐标尺寸;
5//! - **脏矩形红框透视**:实时叠加当前帧 `DamageTracker` 合并后的脏区矩形;
6//! - **性能监控 HUD**:展示布局求解次数、L2 局部重排命中次数、Pass-4 快径次数、
7//!   `SubView` 缓存命中、Arena 峰值字节数与 `DrawOp`/`HitRegion` 数量。
8
9use crate::element::{div, text, Element, IntoElement};
10use crate::engine::VeloxEngine;
11use crate::fmt;
12use crate::pod::FrameScope;
13use crate::solver::Region;
14use crate::style::{Color, Styled};
15
16/// DevTools Inspector 开关配置。
17#[derive(Copy, Clone, PartialEq, Eq, Debug)]
18pub struct InspectorConfig {
19    pub enabled: bool,
20    pub show_box_model: bool,
21    pub show_dirty_rects: bool,
22    pub show_perf_hud: bool,
23}
24
25impl Default for InspectorConfig {
26    fn default() -> Self {
27        Self::ALL
28    }
29}
30
31impl InspectorConfig {
32    pub const OFF: Self = Self {
33        enabled: false,
34        show_box_model: false,
35        show_dirty_rects: false,
36        show_perf_hud: false,
37    };
38
39    pub const ALL: Self = Self {
40        enabled: true,
41        show_box_model: true,
42        show_dirty_rects: true,
43        show_perf_hud: true,
44    };
45}
46
47/// 单帧实时性能与布局诊断遥测快照(纯 POD、`Copy`,可在 MCU 与桌面端零分配采集)。
48#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
49pub struct FrameTelemetry {
50    pub layout_solve_count: u32,
51    pub partial_relayout_count: u32,
52    pub relayout_boundary_hits: u32,
53    pub fast_paint_pass_count: u32,
54    pub subview_cache_hits: u32,
55    pub arena_used_bytes: usize,
56    pub draw_op_count: usize,
57    pub hit_region_count: usize,
58    pub dirty_rect_count: usize,
59    pub hovered_bounds: Option<Region>,
60    pub hovered_id: Option<u32>,
61}
62
63/// 从运行中的 [`VeloxEngine`] 采集当前帧性能指标与鼠标悬浮盒模型信息。
64pub fn capture_telemetry(engine: &VeloxEngine, pointer_pos: Option<(i16, i16)>) -> FrameTelemetry {
65    let mut hovered_bounds = None;
66    let mut hovered_id = None;
67    if let Some((px, py)) = pointer_pos {
68        for hit in engine.cached_hit_regions().iter().rev() {
69            if hit.bounds.intersect(hit.clip).contains(px, py) {
70                hovered_bounds = Some(hit.bounds);
71                hovered_id = hit.id;
72                break;
73            }
74        }
75    }
76    FrameTelemetry {
77        layout_solve_count: engine.layout_solve_count(),
78        partial_relayout_count: engine.partial_relayout_count(),
79        relayout_boundary_hits: engine.relayout_boundary_hits(),
80        fast_paint_pass_count: engine.fast_paint_pass_count(),
81        subview_cache_hits: engine.subview_cache_hits(),
82        arena_used_bytes: engine.last_arena_used_bytes(),
83        draw_op_count: engine.cached_draw_ops().len(),
84        hit_region_count: engine.cached_hit_regions().len(),
85        dirty_rect_count: engine.last_damage().len(),
86        hovered_bounds,
87        hovered_id,
88    }
89}
90
91/// 构建可叠放在任意根视图顶层的 DevTools Inspector 诊断浮层(零侵入业务布局,不拦截鼠标事件)。
92pub fn build_inspector_overlay<'a>(
93    cx: &'a FrameScope,
94    telemetry: &FrameTelemetry,
95    dirty_rects: &[Region],
96    cfg: InspectorConfig,
97) -> Element<'a> {
98    if !cfg.enabled {
99        return div().w(0).h(0).into_element(cx);
100    }
101
102    let mut layer = div().overlay().pos(0, 0).w_full().h_full();
103
104    // 1. 脏矩形红框透视
105    if cfg.show_dirty_rects {
106        for &r in dirty_rects {
107            if !r.is_empty() {
108                layer = layer.child(
109                    cx,
110                    div()
111                        .pos(r.x, r.y)
112                        .w(r.w)
113                        .h(r.h)
114                        .bg(Color::rgba(239, 68, 68, 26))
115                        .border(1, Color::rgba(239, 68, 68, 210)),
116                );
117            }
118        }
119    }
120
121    // 2. 鼠标悬浮元素盒模型透视框与坐标角标
122    if cfg.show_box_model {
123        if let Some(hb) = telemetry.hovered_bounds {
124            layer = layer.child(
125                cx,
126                div()
127                    .pos(hb.x, hb.y)
128                    .w(hb.w)
129                    .h(hb.h)
130                    .bg(Color::rgba(56, 189, 248, 42))
131                    .border(1, Color::rgba(56, 189, 248, 235)),
132            );
133            let badge_y = if hb.y >= 20 { hb.y - 18 } else { hb.bottom() + 2 };
134            let label = fmt!(cx, "{}x{} @ ({},{})", hb.w, hb.h, hb.x, hb.y);
135            layer = layer.child(
136                cx,
137                div()
138                    .pos(hb.x, badge_y)
139                    .px(6)
140                    .py(2)
141                    .rounded(4)
142                    .bg(Color::rgba(15, 23, 42, 230))
143                    .child(cx, text(label).size(10).color(Color::rgb(125, 211, 252))),
144            );
145        }
146    }
147
148    // 3. 右上角实时性能监控 HUD
149    if cfg.show_perf_hud {
150        let l1 = fmt!(
151            cx,
152            "Solves:{} L2:{} Pass4:{}",
153            telemetry.layout_solve_count,
154            telemetry.partial_relayout_count,
155            telemetry.fast_paint_pass_count
156        );
157        let l2 = fmt!(
158            cx,
159            "Ops:{} Hits:{} Dirty:{}",
160            telemetry.draw_op_count,
161            telemetry.hit_region_count,
162            telemetry.dirty_rect_count
163        );
164        let l3 = fmt!(
165            cx,
166            "Arena:{}B SubCache:{}",
167            telemetry.arena_used_bytes,
168            telemetry.subview_cache_hits
169        );
170        layer = layer.child(
171            cx,
172            div()
173                .pos(8, 8)
174                .flex_col()
175                .gap(2)
176                .px(8)
177                .py(6)
178                .rounded(6)
179                .bg(Color::rgba(15, 23, 42, 220))
180                .border(1, Color::rgba(99, 102, 241, 180))
181                .child(
182                    cx,
183                    text("Velox Inspector")
184                        .size(11)
185                        .color(Color::rgb(165, 180, 252)),
186                )
187                .child(cx, text(l1).size(10).color(Color::rgb(226, 232, 240)))
188                .child(cx, text(l2).size(10).color(Color::rgb(226, 232, 240)))
189                .child(cx, text(l3).size(10).color(Color::rgb(148, 163, 184))),
190        );
191    }
192
193    layer.into_element(cx)
194}