forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
352 lines (325 loc) · 10.8 KB
/
lib.rs
File metadata and controls
352 lines (325 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use bevy::{
prelude::Entity,
render::render_resource::{Extent3d, TextureFormat},
};
use processing::prelude::*;
use crate::color::Color;
mod color;
mod error;
/// Initialize libProcessing.
///
/// SAFETY:
/// - This is called from the main thread if the platform requires it.
/// - This can only be called once.
#[unsafe(no_mangle)]
pub extern "C" fn processing_init() {
error::clear_error();
error::check(init);
}
/// Create a WebGPU surface from a native window handle.
/// Returns a window ID (entity ID) that should be used for subsequent operations.
/// Returns 0 on failure.
///
/// SAFETY:
/// - Init has been called.
/// - window_handle is a valid GLFW window pointer.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_surface_create(
window_handle: u64,
display_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> u64 {
error::clear_error();
error::check(|| surface_create(window_handle, display_handle, width, height, scale_factor))
.map(|e| e.to_bits())
.unwrap_or(0)
}
/// Destroy the surface associated with the given window ID.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_surface_destroy(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| surface_destroy(window_entity));
}
/// Update window size when resized.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_surface_resize(window_id: u64, width: u32, height: u32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| surface_resize(window_entity, width, height));
}
/// Set the background color for the given window.
///
/// SAFETY:
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_background_color(window_id: u64, color: Color) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| record_command(window_entity, DrawCommand::BackgroundColor(color.into())));
}
/// Set the background image for the given window.
///
/// SAFETY:
/// - This is called from the same thread as init.
/// - image_id is a valid ID returned from processing_image_create.
/// - The image has been fully uploaded.
#[unsafe(no_mangle)]
pub extern "C" fn processing_background_image(window_id: u64, image_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
let image_entity = Entity::from_bits(image_id);
error::check(|| record_command(window_entity, DrawCommand::BackgroundImage(image_entity)));
}
/// Begins the draw for the given window.
///
/// SAFETY:
/// - Init has been called and exit has not been called.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_begin_draw(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| begin_draw(window_entity));
}
/// Flushes recorded draw commands for the given window.
///
/// SAFETY:
/// - Init has been called and exit has not been called.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_flush(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| flush(window_entity));
}
/// Ends the draw for the given window and presents the frame.
///
/// SAFETY:
/// - Init has been called and exit has not been called.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_end_draw(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| end_draw(window_entity));
}
/// Shuts down internal resources with given exit code, but does *not* terminate the process.
///
/// SAFETY:
/// - This is called from the same thread as init.
/// - Caller ensures that update is never called again after exit.
#[unsafe(no_mangle)]
pub extern "C" fn processing_exit(exit_code: u8) {
error::clear_error();
error::check(|| exit(exit_code));
}
/// Set the fill color.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_set_fill(window_id: u64, r: f32, g: f32, b: f32, a: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
let color = bevy::color::Color::srgba(r, g, b, a);
error::check(|| record_command(window_entity, DrawCommand::Fill(color)));
}
/// Set the stroke color.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_set_stroke_color(window_id: u64, r: f32, g: f32, b: f32, a: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
let color = bevy::color::Color::srgba(r, g, b, a);
error::check(|| record_command(window_entity, DrawCommand::StrokeColor(color)));
}
/// Set the stroke weight.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_set_stroke_weight(window_id: u64, weight: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| record_command(window_entity, DrawCommand::StrokeWeight(weight)));
}
/// Disable fill for subsequent shapes.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_no_fill(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| record_command(window_entity, DrawCommand::NoFill));
}
/// Disable stroke for subsequent shapes.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_no_stroke(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| record_command(window_entity, DrawCommand::NoStroke));
}
/// Draw a rectangle.
///
/// SAFETY:
/// - Init and surface_create have been called.
/// - window_id is a valid ID returned from surface_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_rect(
window_id: u64,
x: f32,
y: f32,
w: f32,
h: f32,
tl: f32,
tr: f32,
br: f32,
bl: f32,
) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| {
record_command(
window_entity,
DrawCommand::Rect {
x,
y,
w,
h,
radii: [tl, tr, br, bl],
},
)
});
}
/// Create an image from raw pixel data.
///
/// # Safety
/// - Init has been called.
/// - data is a valid pointer to data_len bytes of RGBA pixel data.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn processing_image_create(
width: u32,
height: u32,
data: *const u8,
data_len: usize,
) -> u64 {
error::clear_error();
// SAFETY: Caller must ensure that `data` is valid for `data_len` bytes.
let data = unsafe { std::slice::from_raw_parts(data, data_len) };
error::check(|| {
let size = Extent3d {
width,
height,
depth_or_array_layers: 1,
};
image_create(size, data.to_vec(), TextureFormat::Rgba8UnormSrgb)
})
.map(|entity| entity.to_bits())
.unwrap_or(0)
}
/// Load an image from a file path.
///
/// # Safety
/// - Init has been called.
/// - path is a valid null-terminated C string.
/// - This is called from the same thread as init.
///
/// Note: This function is currently synchronous but Bevy's asset loading is async.
/// The image may not be immediately available. This needs to be improved.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn processing_image_load(path: *const std::ffi::c_char) -> u64 {
error::clear_error();
// SAFETY: Caller guarantees path is a valid C string
let c_str = unsafe { std::ffi::CStr::from_ptr(path) };
let path_str = match c_str.to_str() {
Ok(s) => s,
Err(_) => {
error::set_error("Invalid UTF-8 in image path");
return 0;
}
};
error::check(|| image_load(path_str))
.map(|entity| entity.to_bits())
.unwrap_or(0)
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_image_resize(image_id: u64, new_width: u32, new_height: u32) {
error::clear_error();
let image_entity = Entity::from_bits(image_id);
let new_size = Extent3d {
width: new_width,
height: new_height,
depth_or_array_layers: 1,
};
error::check(|| image_resize(image_entity, new_size));
}
/// Load pixels from an image into a caller-provided buffer.
///
/// # Safety
/// - Init and image_create have been called.
/// - image_id is a valid ID returned from image_create.
/// - buffer is a valid pointer to at least buffer_len Color elements.
/// - buffer_len must equal width * height of the image.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn processing_image_load_pixels(
image_id: u64,
buffer: *mut Color,
buffer_len: usize,
) {
error::clear_error();
let image_entity = Entity::from_bits(image_id);
error::check(|| {
let colors = image_load_pixels(image_entity)?;
// Validate buffer size
if colors.len() != buffer_len {
let error_msg = format!(
"Buffer size mismatch: expected {}, got {}",
colors.len(),
buffer_len
);
error::set_error(&error_msg);
return Err(error::ProcessingError::InvalidArgument(error_msg));
}
// SAFETY: Caller guarantees buffer is valid for buffer_len elements
unsafe {
let buffer_slice = std::slice::from_raw_parts_mut(buffer, buffer_len);
for (i, color) in colors.iter().enumerate() {
buffer_slice[i] = Color::from(*color);
}
}
Ok(())
});
}