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;
6mod direct_mapped_cache;
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;
15mod font_tracking;
16pub mod geometry;
17pub mod gpos_kerning;
18pub mod graph;
19mod graph_hash;
20pub mod graph_scene;
21pub mod hit_graph;
22pub mod image_compare;
23pub mod layer_composition;
24pub mod layer_shadow;
25pub mod layer_transform;
26pub mod primitive_emit;
27pub mod raster_cache;
28pub mod render_contract;
29pub mod scene_builder;
30pub mod shape_sdf;
31pub mod software_text_raster;
32pub mod style_shared;
33mod text_cache_key;
34pub mod text_hyphenation;
35pub mod text_mask_gamma;
36pub mod text_measure;
37
38#[cfg(test)]
39#[path = "tests/pointer_slices.rs"]
40mod pointer_slices;
41
42use cranpose_core::{MemoryApplier, collections::map::HashSet};
43use cranpose_foundation::nodes::input::PointerEvent;
44use cranpose_ui::LayoutTree;
45pub use cranpose_ui_graphics::Brush;
46use cranpose_ui_graphics::Size;
47
48/// Trait implemented by hit-test targets stored inside a [`RenderScene`].
49pub trait HitTestTarget {
50 /// Dispatches a pointer event to this target's handlers.
51 fn dispatch(&self, event: PointerEvent);
52
53 /// Dispatches a pointer event using the current live node state when available.
54 ///
55 /// Render-scene hit targets may cache closures from an older scene build. Pointer
56 /// dispatch goes through this hook so implementations can resolve fresh handlers
57 /// from the current applier while still using the target's geometry snapshot.
58 fn dispatch_with_applier(&self, _applier: &mut MemoryApplier, event: PointerEvent) {
59 self.dispatch(event);
60 }
61
62 /// Returns the NodeId associated with this hit target.
63 /// Used by HitPathTracker to cache stable identity instead of geometry.
64 fn node_id(&self) -> cranpose_core::NodeId;
65
66 /// The pointer's appearance while it hovers this target, when the target
67 /// names one.
68 ///
69 /// The shell asks the hit list top-down and applies the first answer, so
70 /// the innermost region under the pointer decides the cursor.
71 fn pointer_icon(&self) -> Option<cranpose_ui_graphics::PointerIcon> {
72 None
73 }
74
75 /// Returns the node capture path that should stay attached to this target's gesture.
76 ///
77 /// The default is just this target's own node. Renderers can override this to
78 /// include stable ancestor pointer-input nodes that must continue receiving
79 /// Move/Up/Cancel even if the original descendant target is recycled.
80 fn capture_path(&self) -> Vec<cranpose_core::NodeId> {
81 vec![self.node_id()]
82 }
83}
84
85/// Trait describing the minimal surface area required by the application
86/// shell to process pointer events and refresh the frame graph.
87pub trait RenderScene {
88 type HitTarget: HitTestTarget + Clone;
89
90 fn clear(&mut self);
91
92 /// Performs hit testing at the given coordinates.
93 /// Returns hit targets ordered by z-index (top-to-bottom).
94 fn hit_test(&self, x: f32, y: f32) -> Vec<Self::HitTarget>;
95
96 /// The one target a press reaches when it misses every target but lands
97 /// inside a small target grown to the minimum touch size: the nearest such
98 /// target, and none when the point is outside every grown target.
99 fn hit_test_near(&self, _x: f32, _y: f32) -> Option<Self::HitTarget> {
100 None
101 }
102
103 /// Returns NodeIds of all hit regions at the given coordinates.
104 /// This is a convenience method equivalent to `hit_test().map(|h| h.node_id())`.
105 fn hit_test_nodes(&self, x: f32, y: f32) -> Vec<cranpose_core::NodeId> {
106 self.hit_test(x, y)
107 .into_iter()
108 .map(|h| h.node_id())
109 .collect()
110 }
111
112 /// Finds a hit target by NodeId with fresh geometry from the current scene.
113 ///
114 /// This is the key method for HitPathTracker-style gesture handling:
115 /// - On PointerDown, we cache NodeIds (not geometry)
116 /// - On Move/Up/Cancel, we call this to get fresh HitTarget with current geometry
117 /// - Handler closures are preserved (same Rc), so internal state survives
118 ///
119 /// Returns None if the node no longer exists in the scene (e.g., removed during gesture).
120 fn find_target(&self, node_id: cranpose_core::NodeId) -> Option<Self::HitTarget>;
121
122 /// Replaces the set with retained visual observation owners, preserving its capacity.
123 /// Returns whether the scene provides this information.
124 fn collect_retained_visual_observation_nodes(
125 &self,
126 nodes: &mut HashSet<cranpose_core::NodeId>,
127 ) -> bool {
128 nodes.clear();
129 false
130 }
131}
132
133/// Abstraction implemented by concrete renderer backends.
134pub trait Renderer {
135 type Scene: RenderScene;
136 type Error;
137
138 /// Installs renderer-provided app services into the target AppContext.
139 ///
140 /// AppShell calls this before the first composition pass.
141 /// Renderers that provide text measurement or other per-app services should install
142 /// them here rather than as constructor side effects.
143 fn attach_app_context_services(&mut self, _app_context: &cranpose_ui::AppContext) {}
144
145 fn scene(&self) -> &Self::Scene;
146 fn scene_mut(&mut self) -> &mut Self::Scene;
147
148 fn rebuild_scene(
149 &mut self,
150 layout_tree: &LayoutTree,
151 viewport: Size,
152 ) -> Result<(), Self::Error>;
153
154 /// Rebuilds the scene by traversing the LayoutNode tree directly via Applier.
155 ///
156 /// This is the new architecture that eliminates per-frame LayoutTree reconstruction.
157 /// Implementors must read layout state from LayoutNode.layout_state() directly.
158 fn rebuild_scene_from_applier(
159 &mut self,
160 applier: &mut cranpose_core::MemoryApplier,
161 root: cranpose_core::NodeId,
162 viewport: Size,
163 ) -> Result<(), Self::Error>;
164
165 fn update_scene_from_applier(
166 &mut self,
167 applier: &mut cranpose_core::MemoryApplier,
168 root: cranpose_core::NodeId,
169 viewport: Size,
170 dirty_nodes: &[cranpose_core::NodeId],
171 ) -> Result<(), Self::Error> {
172 let _ = dirty_nodes;
173 self.rebuild_scene_from_applier(applier, root, viewport)
174 }
175
176 fn update_visual_scene_from_applier(
177 &mut self,
178 applier: &mut cranpose_core::MemoryApplier,
179 root: cranpose_core::NodeId,
180 viewport: Size,
181 dirty_nodes: &[cranpose_core::NodeId],
182 ) -> Result<(), Self::Error> {
183 self.update_scene_from_applier(applier, root, viewport, dirty_nodes)
184 }
185
186 /// Draw a development overlay (e.g., FPS counter) on top of the scene.
187 ///
188 /// This is called after rebuild_scene when dev options are enabled.
189 /// The text is drawn directly by the renderer without affecting composition.
190 ///
191 /// Default implementation does nothing.
192 fn draw_dev_overlay(&mut self, _text: &str, _viewport: Size) {}
193
194 /// Replaces the developer inspector graph outside the application scene.
195 ///
196 /// Passing `None` removes the inspector. Its primitives must not participate
197 /// in application hit testing, layout, or accessibility.
198 fn set_inspector_overlay(&mut self, _graph: Option<graph::RenderGraph>) {}
199
200 /// Returns whether renderer-side cache materialization needs a visible follow-up frame.
201 fn needs_frame_warmup(&self) -> bool {
202 false
203 }
204}