Skip to main content

cranpose_render_common/
lib.rs

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