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
587 lines (543 loc) · 18.1 KB
/
Copy pathlib.rs
File metadata and controls
587 lines (543 loc) · 18.1 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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 macOS NSWindow handle.
///
/// SAFETY:
/// - Init has been called.
/// - window_handle is a valid NSWindow pointer.
/// - This is called from the same thread as init.
#[cfg(target_os = "macos")]
#[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_macos(window_handle, width, height, scale_factor))
.map(|e| e.to_bits())
.unwrap_or(0)
}
/// Create a WebGPU surface from a Windows HWND handle.
///
/// SAFETY:
/// - Init has been called.
/// - window_handle is a valid HWND.
/// - This is called from the same thread as init.
#[cfg(target_os = "windows")]
#[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_windows(window_handle, width, height, scale_factor))
.map(|e| e.to_bits())
.unwrap_or(0)
}
/// Create a WebGPU surface from a Wayland window and display handle.
///
/// SAFETY:
/// - Init has been called.
/// - window_handle is a valid wl_surface pointer.
/// - display_handle is a valid wl_display pointer.
/// - This is called from the same thread as init.
#[cfg(all(target_os = "linux", feature = "wayland"))]
#[unsafe(no_mangle)]
pub extern "C" fn processing_surface_create_wayland(
window_handle: u64,
display_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> u64 {
error::clear_error();
error::check(|| {
surface_create_wayland(window_handle, display_handle, width, height, scale_factor)
})
.map(|e| e.to_bits())
.unwrap_or(0)
}
/// Create a WebGPU surface from an X11 window and display handle.
///
/// SAFETY:
/// - Init has been called.
/// - window_handle is a valid X11 Window ID.
/// - display_handle is a valid X11 Display pointer.
/// - This is called from the same thread as init.
#[cfg(all(target_os = "linux", feature = "x11"))]
#[unsafe(no_mangle)]
pub extern "C" fn processing_surface_create_x11(
window_handle: u64,
display_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> u64 {
error::clear_error();
error::check(|| surface_create_x11(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(|| {
graphics_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(|| {
graphics_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(|| graphics_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(|| graphics_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(|| graphics_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(|| graphics_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(|| graphics_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(|| graphics_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(|| graphics_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(|| graphics_record_command(window_entity, DrawCommand::NoStroke));
}
/// Push the current transformation matrix onto the stack.
///
/// 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_push_matrix(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::PushMatrix));
}
/// Pop the transformation matrix from the stack.
///
/// 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_pop_matrix(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::PopMatrix));
}
/// Reset the transformation matrix to identity.
///
/// 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_reset_matrix(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::ResetMatrix));
}
/// Translate the coordinate system.
///
/// 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_translate(window_id: u64, x: f32, y: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::Translate { x, y }));
}
/// Rotate the coordinate system.
///
/// 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_rotate(window_id: u64, angle: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::Rotate { angle }));
}
/// Scale the coordinate system.
///
/// 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_scale(window_id: u64, x: f32, y: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::Scale { x, y }));
}
/// Shear along the X axis.
///
/// 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_shear_x(window_id: u64, angle: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::ShearX { angle }));
}
/// Shear along the Y axis.
///
/// 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_shear_y(window_id: u64, angle: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_record_command(window_entity, DrawCommand::ShearY { angle }));
}
/// 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(|| {
graphics_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_readback(
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_readback(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(())
});
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_mode_3d(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_mode_3d(window_entity));
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_mode_2d(window_id: u64) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_mode_2d(window_entity));
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_camera_position(window_id: u64, x: f32, y: f32, z: f32) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_camera_position(window_entity, x, y, z));
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_camera_look_at(
window_id: u64,
target_x: f32,
target_y: f32,
target_z: f32,
) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_camera_look_at(window_entity, target_x, target_y, target_z));
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_perspective(
window_id: u64,
fov: f32,
aspect: f32,
near: f32,
far: f32,
) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_perspective(window_entity, fov, aspect, near, far));
}
#[unsafe(no_mangle)]
pub extern "C" fn processing_ortho(
window_id: u64,
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) {
error::clear_error();
let window_entity = Entity::from_bits(window_id);
error::check(|| graphics_ortho(window_entity, left, right, bottom, top, near, far));
}