Skip to main content

cranpose_render_common/
dev_overlay.rs

1//! Renderer-drawn dev overlay (fps counter): one shared graph builder so
2//! every backend draws the identical overlay without composition impact.
3
4use crate::graph::{
5    CachePolicy, DrawPrimitiveNode, IsolationReasons, LayerNode, PrimitiveEntry, PrimitiveNode,
6    PrimitivePhase, ProjectiveTransform, RenderGraph, RenderNode, TextPrimitiveNode,
7};
8use crate::raster_cache::LayerRasterCacheHashes;
9use cranpose_ui_graphics::{
10    Brush, Color, CornerRadii, DrawPrimitive, GraphicsLayer, Point, Rect, Size,
11};
12
13/// Cache key for the last-built overlay; rebuilding only on change keeps the
14/// overlay free when the text is stable.
15#[derive(Clone, PartialEq, Eq)]
16pub struct DevOverlayKey {
17    pub text: String,
18    pub viewport_width_bits: u32,
19    pub viewport_height_bits: u32,
20}
21
22impl DevOverlayKey {
23    pub fn new(text: &str, viewport: Size) -> Self {
24        Self {
25            text: text.to_string(),
26            viewport_width_bits: viewport.width.to_bits(),
27            viewport_height_bits: viewport.height.to_bits(),
28        }
29    }
30}
31
32/// Build the dev-overlay graph: a translucent pill with the stats text in
33/// the top-right corner, sized with fixed monospace-ish metrics so the
34/// overlay never depends on renderer text measurement.
35pub fn build_dev_overlay_graph(
36    text: &str,
37    viewport: Size,
38    node_id: cranpose_core::NodeId,
39) -> RenderGraph {
40    let padding = 8.0;
41    let font_size = 14.0;
42    let char_width = 7.0;
43    let text_width = text.len() as f32 * char_width;
44    let text_height = font_size * 1.4;
45    let x = (viewport.width - text_width - padding * 2.0).max(padding);
46    let y = padding;
47
48    let mut overlay_layer = LayerNode {
49        node_id: Some(node_id),
50        local_bounds: Rect {
51            x: 0.0,
52            y: 0.0,
53            width: text_width + padding,
54            height: text_height + padding / 2.0,
55        },
56        transform_to_parent: ProjectiveTransform::translation(x, y),
57        content_offset: Point::default(),
58        motion_context_animated: false,
59        translated_content_context: false,
60        translated_content_offset: Point::default(),
61        graphics_layer: GraphicsLayer::default(),
62        clip_to_bounds: false,
63        shadow_clip: None,
64        hit_test: None,
65        has_hit_targets: false,
66        isolation: IsolationReasons::default(),
67        cache_policy: CachePolicy::None,
68        cache_hashes: LayerRasterCacheHashes::default(),
69        cache_hashes_valid: false,
70        children: vec![
71            RenderNode::Primitive(PrimitiveEntry {
72                phase: PrimitivePhase::BeforeChildren,
73                node: PrimitiveNode::Draw(DrawPrimitiveNode {
74                    primitive: DrawPrimitive::RoundRect {
75                        rect: Rect {
76                            x: 0.0,
77                            y: 0.0,
78                            width: text_width + padding,
79                            height: text_height + padding / 2.0,
80                        },
81                        brush: Brush::Solid(Color(0.0, 0.0, 0.0, 0.7)),
82                        radii: CornerRadii::uniform(4.0),
83                    },
84                    clip: None,
85                }),
86            }),
87            RenderNode::Primitive(PrimitiveEntry {
88                phase: PrimitivePhase::AfterChildren,
89                node: PrimitiveNode::Text(Box::new(TextPrimitiveNode {
90                    node_id,
91                    rect: Rect {
92                        x: padding / 2.0,
93                        y: padding / 4.0,
94                        width: text_width,
95                        height: text_height,
96                    },
97                    text: cranpose_ui::text::AnnotatedString::from(text),
98                    text_style: cranpose_ui::TextStyle::default(),
99                    font_size,
100                    layout_options: cranpose_ui::TextLayoutOptions::default(),
101                    clip: None,
102                })),
103            }),
104        ],
105    };
106    overlay_layer.recompute_raster_cache_hashes();
107
108    let mut graph = RenderGraph::new(LayerNode {
109        node_id: None,
110        local_bounds: Rect::from_size(viewport),
111        transform_to_parent: ProjectiveTransform::identity(),
112        content_offset: Point::default(),
113        motion_context_animated: false,
114        translated_content_context: false,
115        translated_content_offset: Point::default(),
116        graphics_layer: GraphicsLayer::default(),
117        clip_to_bounds: false,
118        shadow_clip: None,
119        hit_test: None,
120        has_hit_targets: false,
121        isolation: IsolationReasons::default(),
122        cache_policy: CachePolicy::None,
123        cache_hashes: LayerRasterCacheHashes::default(),
124        cache_hashes_valid: false,
125        children: vec![RenderNode::Layer(Box::new(overlay_layer))],
126    });
127    graph.root.recompute_raster_cache_hashes();
128    graph
129}