lgui_render_api/
contract.rs1use lgui_core::core::{PhysicalRect, Scene, ScenePrimitive, UiRect, UiScale};
2
3pub use lgui_core::application::RenderErrorStage;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum FrameReason {
7 #[default]
8 SceneChange,
9 PlatformExposure,
10 Resize,
11 Recovery,
12 Explicit,
13}
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16pub struct RendererCapabilities {
17 pub partial_redraw: bool,
18 pub retained_surface: bool,
19}
20
21#[derive(Clone, Copy, Debug)]
22pub struct FrameInfo<'a> {
23 viewport: PhysicalRect,
24 damage: &'a [PhysicalRect],
25 scale: UiScale,
26 reason: FrameReason,
27 full_redraw: bool,
28}
29
30impl<'a> FrameInfo<'a> {
31 pub fn new(
32 viewport: PhysicalRect,
33 damage: &'a [PhysicalRect],
34 scale: UiScale,
35 reason: FrameReason,
36 full_redraw: bool,
37 ) -> Self {
38 Self {
39 viewport,
40 damage,
41 scale,
42 reason,
43 full_redraw,
44 }
45 }
46
47 pub fn viewport(&self) -> PhysicalRect {
48 self.viewport
49 }
50
51 pub fn damage(&self) -> &'a [PhysicalRect] {
52 self.damage
53 }
54
55 pub fn scale(&self) -> UiScale {
56 self.scale
57 }
58
59 pub fn reason(&self) -> FrameReason {
60 self.reason
61 }
62
63 pub fn is_full_redraw(&self) -> bool {
64 self.full_redraw
65 }
66}
67
68#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
69pub struct RenderStats {
70 pub painted_rects: usize,
71 pub painted_pixels: u64,
72}
73
74impl RenderStats {
75 pub fn for_frame(frame: &FrameInfo<'_>) -> Self {
76 let rects = if frame.is_full_redraw() {
77 std::slice::from_ref(&frame.viewport)
78 } else {
79 frame.damage
80 };
81 Self {
82 painted_rects: rects.len(),
83 painted_pixels: rects.iter().fold(0_u64, |total, rect| {
84 total.saturating_add(
85 (rect.width().max(0) as u64).saturating_mul(rect.height().max(0) as u64),
86 )
87 }),
88 }
89 }
90}
91
92#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93pub enum MemoryPressure {
94 Moderate,
95 Critical,
96}
97
98pub trait SceneRenderer: 'static {
99 type Target;
100 type Error;
101
102 fn capabilities(&self) -> RendererCapabilities;
103
104 fn prepare(
105 &mut self,
106 _target: &mut Self::Target,
107 _frame: &FrameInfo<'_>,
108 ) -> Result<(), Self::Error> {
109 Ok(())
110 }
111
112 fn render(
113 &mut self,
114 target: &mut Self::Target,
115 scene: &Scene,
116 frame: &FrameInfo<'_>,
117 ) -> Result<RenderStats, Self::Error>;
118
119 fn trim(&mut self, _pressure: MemoryPressure) {}
120
121 fn memory_usage(&self) -> lgui_core::memory::CacheUsage {
122 lgui_core::memory::CacheUsage::default()
123 }
124
125 fn set_memory_budget(&mut self, _budget_bytes: usize) {}
126
127 fn trim_to(&mut self, target_bytes: usize) -> usize {
128 let before = self.memory_usage().resident_bytes();
129 self.trim(if target_bytes == 0 {
130 MemoryPressure::Critical
131 } else {
132 MemoryPressure::Moderate
133 });
134 before.saturating_sub(self.memory_usage().resident_bytes())
135 }
136
137 fn reset(&mut self) {}
138}
139
140#[derive(Clone, Copy, Debug, PartialEq, Eq)]
141pub struct ClipRegion {
142 rect: UiRect,
143}
144
145impl ClipRegion {
146 pub fn new(rect: UiRect) -> Self {
147 Self { rect }
148 }
149
150 pub fn rect(self) -> UiRect {
151 self.rect
152 }
153
154 pub fn intersects(self, command: &ScenePrimitive) -> bool {
155 command.paint_bounds().intersect(self.rect).is_some()
156 }
157
158 pub fn intersection(self, rect: UiRect) -> Option<UiRect> {
159 rect.intersect(self.rect)
160 }
161}
162
163#[cfg(test)]
164#[path = "contract_test.rs"]
165mod tests;