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