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 debug_toggles;
5pub mod dev_overlay;
6
7/// The frame background every renderer clears to (linear values; sRGB
8/// surfaces display this as rgb(75, 75, 86)). One definition so backends
9/// cannot drift.
10pub const FRAME_CLEAR_COLOR: [f32; 4] = [18.0 / 255.0, 18.0 / 255.0, 24.0 / 255.0, 1.0];
11pub mod brush_sampling;
12pub mod font_layout;
13pub mod font_source;
14pub mod geometry;
15pub mod gpos_kerning;
16pub mod graph;
17mod graph_hash;
18pub mod graph_scene;
19pub mod hit_graph;
20pub mod image_compare;
21pub mod layer_composition;
22pub mod layer_shadow;
23pub mod layer_transform;
24pub mod primitive_emit;
25pub mod raster_cache;
26pub mod render_contract;
27pub mod scene_builder;
28pub mod shape_sdf;
29pub mod software_text_raster;
30pub mod style_shared;
31pub mod text_hyphenation;
32pub mod text_measure;
33
34use std::collections::HashSet;
35
36use cranpose_core::MemoryApplier;
37use cranpose_foundation::nodes::input::PointerEvent;
38use cranpose_ui::LayoutTree;
39pub use cranpose_ui_graphics::Brush;
40use cranpose_ui_graphics::Size;
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    fn retained_visual_observation_nodes(&self) -> Option<HashSet<cranpose_core::NodeId>> {
101        None
102    }
103}
104
105/// Abstraction implemented by concrete renderer backends.
106pub trait Renderer {
107    type Scene: RenderScene;
108    type Error;
109
110    /// Installs renderer-provided app services into the target AppContext.
111    ///
112    /// AppShell calls this before the first composition pass.
113    /// Renderers that provide text measurement or other per-app services should install
114    /// them here rather than as constructor side effects.
115    fn attach_app_context_services(&mut self, _app_context: &cranpose_ui::AppContext) {}
116
117    fn scene(&self) -> &Self::Scene;
118    fn scene_mut(&mut self) -> &mut Self::Scene;
119
120    fn rebuild_scene(
121        &mut self,
122        layout_tree: &LayoutTree,
123        viewport: Size,
124    ) -> Result<(), Self::Error>;
125
126    /// Rebuilds the scene by traversing the LayoutNode tree directly via Applier.
127    ///
128    /// This is the new architecture that eliminates per-frame LayoutTree reconstruction.
129    /// Implementors must read layout state from LayoutNode.layout_state() directly.
130    fn rebuild_scene_from_applier(
131        &mut self,
132        applier: &mut cranpose_core::MemoryApplier,
133        root: cranpose_core::NodeId,
134        viewport: Size,
135    ) -> Result<(), Self::Error>;
136
137    fn update_scene_from_applier(
138        &mut self,
139        applier: &mut cranpose_core::MemoryApplier,
140        root: cranpose_core::NodeId,
141        viewport: Size,
142        dirty_nodes: &[cranpose_core::NodeId],
143    ) -> Result<(), Self::Error> {
144        let _ = dirty_nodes;
145        self.rebuild_scene_from_applier(applier, root, viewport)
146    }
147
148    fn update_visual_scene_from_applier(
149        &mut self,
150        applier: &mut cranpose_core::MemoryApplier,
151        root: cranpose_core::NodeId,
152        viewport: Size,
153        dirty_nodes: &[cranpose_core::NodeId],
154    ) -> Result<(), Self::Error> {
155        self.update_scene_from_applier(applier, root, viewport, dirty_nodes)
156    }
157
158    /// Draw a development overlay (e.g., FPS counter) on top of the scene.
159    ///
160    /// This is called after rebuild_scene when dev options are enabled.
161    /// The text is drawn directly by the renderer without affecting composition.
162    ///
163    /// Default implementation does nothing.
164    fn draw_dev_overlay(&mut self, _text: &str, _viewport: Size) {}
165
166    /// Returns whether renderer-side cache materialization needs a visible follow-up frame.
167    fn needs_frame_warmup(&self) -> bool {
168        false
169    }
170}