0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-09 00:28:20 -05:00
penpot/render-wasm/src/main.rs

86 lines
2.4 KiB
Rust
Raw Normal View History

pub mod render;
pub mod shapes;
use skia_safe as skia;
use render::State;
/// This is called from JS after the WebGL context has been created.
#[no_mangle]
pub extern "C" fn init(width: i32, height: i32) -> Box<render::State> {
let mut gpu_state = render::create_gpu_state();
let surface = render::create_surface(&mut gpu_state, width, height);
let state = State::new(gpu_state, surface);
Box::new(state)
}
/// This is called from JS when the window is resized.
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn resize_surface(state: *mut State, width: i32, height: i32) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
let surface = render::create_surface(&mut state.gpu_state, width, height);
state.set_surface(surface);
}
/// Draws a rect at the specified coordinates with the give ncolor
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn draw_rect(state: *mut State, x1: f32, y1: f32, x2: f32, y2: f32) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
let r = skia::Rect::new(x1, y1, x2, y2);
render::render_rect(&mut state.surface, r, skia::Color::RED);
}
#[no_mangle]
pub unsafe extern "C" fn draw_all_shapes(state: *mut State, zoom: f32, pan_x: f32, pan_y: f32) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
reset_canvas(state);
scale(state, zoom, zoom);
translate(state, pan_x, pan_y);
shapes::draw_all(state);
flush(state);
}
#[no_mangle]
pub unsafe extern "C" fn flush(state: *mut State) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
state
.gpu_state
.context
.flush_and_submit_surface(&mut state.surface, None);
}
#[no_mangle]
pub unsafe extern "C" fn translate(state: *mut State, dx: f32, dy: f32) {
(*state).surface.canvas().translate((dx, dy));
}
#[no_mangle]
pub unsafe extern "C" fn scale(state: *mut State, sx: f32, sy: f32) {
(*state).surface.canvas().scale((sx, sy));
}
#[no_mangle]
pub unsafe extern "C" fn reset_canvas(state: *mut State) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
state.surface.canvas().clear(skia_safe::Color::TRANSPARENT);
state.surface.canvas().reset_matrix();
flush(state);
}
#[no_mangle]
pub unsafe extern "C" fn shapes_buffer() -> *mut shapes::Shape {
let ptr = shapes::SHAPES_BUFFER.as_mut_ptr();
return ptr;
}
fn main() {
render::init_gl();
}