1use std::ops::Deref;
4
5use egui::*;
6use egui::epaint::*;
7use serde::*;
8
9#[repr(C)]
11pub struct EguiFfi {
12 full_output: FfiVec<u8>,
14 meshes: FfiVec<(Rect, FfiTextureId, FfiVec<u32>, FfiVec<Vertex>)>,
16 raw_input: FfiVec<u8>,
18}
19
20impl EguiFfi {
21 pub fn full_output(&self) -> FullOutput {
24 bincode::deserialize::<FullOutput2>(&self.full_output)
25 .expect("Failed to deserialize FullOutput").into()
26 }
27
28 pub fn set_full_output(&mut self, full_output: FullOutput) {
30 self.full_output = bincode::serialize(&FullOutput2::from(full_output))
31 .expect("Failed to serialize FullOutput").into();
32 }
33
34 pub fn raw_input(&self) -> RawInput {
36 bincode::deserialize(&self.raw_input)
37 .expect("Failed to deserialize RawInput")
38 }
39
40 pub fn set_raw_input(&mut self, raw_input: RawInput) {
42 self.raw_input = bincode::serialize(&raw_input)
43 .expect("Failed to serialize RawInput").into();
44 }
45
46 pub fn meshes(&self) -> Vec<(Rect, Mesh)> {
48 self.meshes.iter().map(|(clip_rect, texture_id, indices, vertices)|
49 (*clip_rect, Mesh {
50 indices: indices.to_vec(),
51 texture_id: (*texture_id).into(),
52 vertices: vertices.to_vec()
53 })).collect()
54 }
55
56 pub fn set_meshes(&mut self, meshes: impl IntoIterator<Item = (Rect, Mesh)>) {
58 self.meshes = meshes.into_iter()
59 .map(|(clip_rect, mesh)| (clip_rect, mesh.texture_id.into(), mesh.indices.into(), mesh.vertices.into()))
60 .collect::<Vec<_>>().into()
61 }
62}
63
64impl Default for EguiFfi {
65 fn default() -> Self {
66 let mut result = Self {
67 full_output: Vec::new().into(),
68 meshes: Vec::new().into(),
69 raw_input: Vec::new().into()
70 };
71
72 result.set_full_output(FullOutput::default());
73 result.set_raw_input(RawInput::default());
74
75 result
76 }
77}
78
79#[derive(Clone, Serialize, Deserialize)]
82struct FullOutput2 {
83 pub platform_output: PlatformOutput,
85 pub textures_delta: TexturesDelta,
87 pub pixels_per_point: f32
89}
90
91impl From<FullOutput2> for FullOutput {
92 fn from(value: FullOutput2) -> Self {
93 Self {
94 pixels_per_point: value.pixels_per_point,
95 platform_output: value.platform_output,
96 textures_delta: value.textures_delta,
97 ..Default::default()
98 }
99 }
100}
101
102impl From<FullOutput> for FullOutput2 {
103 fn from(value: FullOutput) -> Self {
104 Self {
105 pixels_per_point: value.pixels_per_point,
106 platform_output: value.platform_output,
107 textures_delta: value.textures_delta
108 }
109 }
110}
111
112#[derive(Copy, Clone, Debug)]
114#[repr(C)]
115struct FfiTextureId {
116 pub kind: TextureIdKind,
118 pub id: u64
120}
121
122impl From<TextureId> for FfiTextureId {
123 fn from(value: TextureId) -> Self {
124 match value {
125 TextureId::Managed(id) => Self {
126 kind: TextureIdKind::Managed,
127 id
128 },
129 TextureId::User(id) => Self {
130 kind: TextureIdKind::User,
131 id
132 }
133 }
134 }
135}
136
137impl From<FfiTextureId> for TextureId {
138 fn from(value: FfiTextureId) -> Self {
139 match value.kind {
140 TextureIdKind::Managed => TextureId::Managed(value.id),
141 TextureIdKind::User => TextureId::User(value.id),
142 }
143 }
144}
145
146#[derive(Copy, Clone, Debug)]
148#[repr(C)]
149enum TextureIdKind {
150 Managed,
152 User
154}
155
156#[derive(Debug)]
158#[repr(C)]
159struct FfiVec<T> {
160 ptr: *mut T,
162 len: usize,
164 capacity: usize,
166 on_free: unsafe extern "C" fn(&mut Self)
168}
169
170impl<T> FfiVec<T> {
171 unsafe extern "C" fn on_free(&mut self) {
177 unsafe { Vec::from_raw_parts(self.ptr, self.len, self.capacity); }
179 }
180}
181
182impl<T> From<Vec<T>> for FfiVec<T> {
183 fn from(mut value: Vec<T>) -> Self {
184 let ptr = value.as_mut_ptr();
185 let len = value.len();
186 let capacity = value.capacity();
187 std::mem::forget(value);
188 Self {
189 ptr,
190 len,
191 capacity,
192 on_free: Self::on_free
193 }
194 }
195}
196
197impl<T> Deref for FfiVec<T> {
198 type Target = [T];
199
200 fn deref(&self) -> &Self::Target {
201 unsafe {
202 std::slice::from_raw_parts(self.ptr, self.len)
203 }
204 }
205}
206
207impl<T> Drop for FfiVec<T> {
208 fn drop(&mut self) {
209 unsafe {
210 (self.on_free)(self)
211 }
212 }
213}
214
215unsafe impl<T: Send> Send for FfiVec<T> {}
216unsafe impl<T: Send> Sync for FfiVec<T> {}