Skip to main content

cranpose_render_common/
lib.rs

1//! Common rendering contracts shared between renderer backends.
2
3pub mod bounded_lru_cache;
4pub mod dev_overlay;
5
6/// The frame background every renderer clears to (linear values; sRGB
7/// surfaces display this as rgb(75, 75, 86)). One definition so backends
8/// cannot drift.
9pub const FRAME_CLEAR_COLOR: [f32; 4] = [18.0 / 255.0, 18.0 / 255.0, 24.0 / 255.0, 1.0];
10pub mod brush_sampling;
11pub mod font_layout;
12pub mod font_source;
13pub mod geometry;
14pub mod gpos_kerning;
15pub mod graph;
16mod graph_hash;
17pub mod graph_scene;
18pub mod hit_graph;
19pub mod image_compare;
20pub mod layer_composition;
21pub mod layer_shadow;
22pub mod layer_transform;
23pub mod primitive_emit;
24pub mod raster_cache;
25pub mod render_contract;
26pub mod scene_builder;
27pub mod shape_sdf;
28pub mod software_text_raster;
29pub mod style_shared;
30pub mod text_hyphenation;
31pub mod text_measure;
32
33use std::collections::HashSet;
34
35use cranpose_core::MemoryApplier;
36use cranpose_foundation::nodes::input::PointerEvent;
37use cranpose_ui::LayoutTree;
38pub use cranpose_ui_graphics::Brush;
39use cranpose_ui_graphics::Size;
40
41/// Trait implemented by hit-test targets stored inside a [`RenderScene`].
42pub trait HitTestTarget {
43    /// Dispatches a pointer event to this target's handlers.
44    fn dispatch(&self, event: PointerEvent);
45
46    /// Dispatches a pointer event using the current live node state when available.
47    ///
48    /// Render-scene hit targets may cache closures from an older scene build. Pointer
49    /// dispatch goes through this hook so implementations can resolve fresh handlers
50    /// from the current applier while still using the target's geometry snapshot.
51    fn dispatch_with_applier(&self, _applier: &mut MemoryApplier, event: PointerEvent) {
52        self.dispatch(event);
53    }
54
55    /// Returns the NodeId associated with this hit target.
56    /// Used by HitPathTracker to cache stable identity instead of geometry.
57    fn node_id(&self) -> cranpose_core::NodeId;
58
59    /// Returns the node capture path that should stay attached to this target's gesture.
60    ///
61    /// The default is just this target's own node. Renderers can override this to
62    /// include stable ancestor pointer-input nodes that must continue receiving
63    /// Move/Up/Cancel even if the original descendant target is recycled.
64    fn capture_path(&self) -> Vec<cranpose_core::NodeId> {
65        vec![self.node_id()]
66    }
67}
68
69/// Trait describing the minimal surface area required by the application
70/// shell to process pointer events and refresh the frame graph.
71pub trait RenderScene {
72    type HitTarget: HitTestTarget + Clone;
73
74    fn clear(&mut self);
75
76    /// Performs hit testing at the given coordinates.
77    /// Returns hit targets ordered by z-index (top-to-bottom).
78    fn hit_test(&self, x: f32, y: f32) -> Vec<Self::HitTarget>;
79
80    /// Returns NodeIds of all hit regions at the given coordinates.
81    /// This is a convenience method equivalent to `hit_test().map(|h| h.node_id())`.
82    fn hit_test_nodes(&self, x: f32, y: f32) -> Vec<cranpose_core::NodeId> {
83        self.hit_test(x, y)
84            .into_iter()
85            .map(|h| h.node_id())
86            .collect()
87    }
88
89    /// Finds a hit target by NodeId with fresh geometry from the current scene.
90    ///
91    /// This is the key method for HitPathTracker-style gesture handling:
92    /// - On PointerDown, we cache NodeIds (not geometry)
93    /// - On Move/Up/Cancel, we call this to get fresh HitTarget with current geometry
94    /// - Handler closures are preserved (same Rc), so internal state survives
95    ///
96    /// Returns None if the node no longer exists in the scene (e.g., removed during gesture).
97    fn find_target(&self, node_id: cranpose_core::NodeId) -> Option<Self::HitTarget>;
98
99    fn retained_visual_observation_nodes(&self) -> Option<HashSet<cranpose_core::NodeId>> {
100        None
101    }
102}
103
104/// Abstraction implemented by concrete renderer backends.
105pub trait Renderer {
106    type Scene: RenderScene;
107    type Error;
108
109    /// Installs renderer-provided app services into the target AppContext.
110    ///
111    /// AppShell calls this before the first composition pass.
112    /// Renderers that provide text measurement or other per-app services should install
113    /// them here rather than as constructor side effects.
114    fn attach_app_context_services(&mut self, _app_context: &cranpose_ui::AppContext) {}
115
116    fn scene(&self) -> &Self::Scene;
117    fn scene_mut(&mut self) -> &mut Self::Scene;
118
119    fn rebuild_scene(
120        &mut self,
121        layout_tree: &LayoutTree,
122        viewport: Size,
123    ) -> Result<(), Self::Error>;
124
125    /// Rebuilds the scene by traversing the LayoutNode tree directly via Applier.
126    ///
127    /// This is the new architecture that eliminates per-frame LayoutTree reconstruction.
128    /// Implementors must read layout state from LayoutNode.layout_state() directly.
129    fn rebuild_scene_from_applier(
130        &mut self,
131        applier: &mut cranpose_core::MemoryApplier,
132        root: cranpose_core::NodeId,
133        viewport: Size,
134    ) -> Result<(), Self::Error>;
135
136    fn update_scene_from_applier(
137        &mut self,
138        applier: &mut cranpose_core::MemoryApplier,
139        root: cranpose_core::NodeId,
140        viewport: Size,
141        dirty_nodes: &[cranpose_core::NodeId],
142    ) -> Result<(), Self::Error> {
143        let _ = dirty_nodes;
144        self.rebuild_scene_from_applier(applier, root, viewport)
145    }
146
147    fn update_visual_scene_from_applier(
148        &mut self,
149        applier: &mut cranpose_core::MemoryApplier,
150        root: cranpose_core::NodeId,
151        viewport: Size,
152        dirty_nodes: &[cranpose_core::NodeId],
153    ) -> Result<(), Self::Error> {
154        self.update_scene_from_applier(applier, root, viewport, dirty_nodes)
155    }
156
157    /// Draw a development overlay (e.g., FPS counter) on top of the scene.
158    ///
159    /// This is called after rebuild_scene when dev options are enabled.
160    /// The text is drawn directly by the renderer without affecting composition.
161    ///
162    /// Default implementation does nothing.
163    fn draw_dev_overlay(&mut self, _text: &str, _viewport: Size) {
164        // Default: no-op
165    }
166
167    /// Returns whether renderer-side cache materialization needs a visible follow-up frame.
168    fn needs_frame_warmup(&self) -> bool {
169        false
170    }
171}