mirror of
https://github.com/penpot/penpot.git
synced 2025-12-11 22:14:05 +01:00
re-render canvas when panning (rust)
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
use std::boxed::Box;
|
||||
|
||||
use skia_safe::{
|
||||
gpu::{self, gl::FramebufferInfo, DirectContext},
|
||||
Color, Paint, PaintStyle, Surface, Rect
|
||||
gpu::{self, gl::FramebufferInfo, DirectContext},
|
||||
Color, Paint, PaintStyle, Rect, Surface,
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
pub fn emscripten_GetProcAddress(
|
||||
name: *const ::std::os::raw::c_char,
|
||||
) -> *const ::std::os::raw::c_void;
|
||||
pub fn emscripten_GetProcAddress(
|
||||
name: *const ::std::os::raw::c_char,
|
||||
) -> *const ::std::os::raw::c_void;
|
||||
}
|
||||
|
||||
struct GpuState {
|
||||
context: DirectContext,
|
||||
framebuffer_info: FramebufferInfo,
|
||||
context: DirectContext,
|
||||
framebuffer_info: FramebufferInfo,
|
||||
}
|
||||
|
||||
/// This struct holds the state of the Rust application between JS calls.
|
||||
@@ -21,18 +21,18 @@ struct GpuState {
|
||||
/// It is created by [init] and passed to the other exported functions. Note that rust-skia data
|
||||
/// structures are not thread safe, so a state must not be shared between different Web Workers.
|
||||
pub struct State {
|
||||
gpu_state: GpuState,
|
||||
surface: Surface,
|
||||
gpu_state: GpuState,
|
||||
surface: Surface,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn new(gpu_state: GpuState, surface: Surface) -> Self {
|
||||
State { gpu_state, surface }
|
||||
}
|
||||
fn new(gpu_state: GpuState, surface: Surface) -> Self {
|
||||
State { gpu_state, surface }
|
||||
}
|
||||
|
||||
fn set_surface(&mut self, surface: Surface) {
|
||||
self.surface = surface;
|
||||
}
|
||||
fn set_surface(&mut self, surface: Surface) {
|
||||
self.surface = surface;
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -43,57 +43,59 @@ pub fn add(left: f32, right: f32) -> f32 {
|
||||
}
|
||||
|
||||
fn init_gl() {
|
||||
unsafe {
|
||||
gl::load_with(|addr| {
|
||||
let addr = std::ffi::CString::new(addr).unwrap();
|
||||
emscripten_GetProcAddress(addr.into_raw() as *const _) as *const _
|
||||
});
|
||||
}
|
||||
unsafe {
|
||||
gl::load_with(|addr| {
|
||||
let addr = std::ffi::CString::new(addr).unwrap();
|
||||
emscripten_GetProcAddress(addr.into_raw() as *const _) as *const _
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// This needs to be done once per WebGL context.
|
||||
fn create_gpu_state() -> GpuState {
|
||||
let interface = skia_safe::gpu::gl::Interface::new_native().unwrap();
|
||||
let context = skia_safe::gpu::direct_contexts::make_gl(interface, None).unwrap();
|
||||
let framebuffer_info = {
|
||||
let mut fboid: gl::types::GLint = 0;
|
||||
unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
|
||||
let interface = skia_safe::gpu::gl::Interface::new_native().unwrap();
|
||||
let context = skia_safe::gpu::direct_contexts::make_gl(interface, None).unwrap();
|
||||
let framebuffer_info = {
|
||||
let mut fboid: gl::types::GLint = 0;
|
||||
unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
|
||||
|
||||
FramebufferInfo {
|
||||
fboid: fboid.try_into().unwrap(),
|
||||
format: skia_safe::gpu::gl::Format::RGBA8.into(),
|
||||
protected: skia_safe::gpu::Protected::No,
|
||||
}
|
||||
};
|
||||
FramebufferInfo {
|
||||
fboid: fboid.try_into().unwrap(),
|
||||
format: skia_safe::gpu::gl::Format::RGBA8.into(),
|
||||
protected: skia_safe::gpu::Protected::No,
|
||||
}
|
||||
};
|
||||
|
||||
GpuState {
|
||||
context,
|
||||
framebuffer_info,
|
||||
}
|
||||
GpuState {
|
||||
context,
|
||||
framebuffer_info,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create the Skia surface that will be used for rendering.
|
||||
fn create_surface(gpu_state: &mut GpuState, width: i32, height: i32) -> Surface {
|
||||
let backend_render_target =
|
||||
gpu::backend_render_targets::make_gl((width, height), 1, 8, gpu_state.framebuffer_info);
|
||||
let backend_render_target =
|
||||
gpu::backend_render_targets::make_gl((width, height), 1, 8, gpu_state.framebuffer_info);
|
||||
|
||||
gpu::surfaces::wrap_backend_render_target(
|
||||
&mut gpu_state.context,
|
||||
&backend_render_target,
|
||||
skia_safe::gpu::SurfaceOrigin::BottomLeft,
|
||||
skia_safe::ColorType::RGBA8888,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
gpu::surfaces::wrap_backend_render_target(
|
||||
&mut gpu_state.context,
|
||||
&backend_render_target,
|
||||
skia_safe::gpu::SurfaceOrigin::BottomLeft,
|
||||
skia_safe::ColorType::RGBA8888,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn render_rect(surface: &mut Surface, left: f32, top: f32, right: f32, bottom: f32) {
|
||||
let mut paint = Paint::default();
|
||||
paint.set_style(PaintStyle::Fill);
|
||||
paint.set_color(Color::BLACK);
|
||||
paint.set_anti_alias(true);
|
||||
surface.canvas().draw_rect(Rect::new(left, top, right, bottom), &paint);
|
||||
let mut paint = Paint::default();
|
||||
paint.set_style(PaintStyle::Fill);
|
||||
paint.set_color(Color::BLACK);
|
||||
paint.set_anti_alias(true);
|
||||
surface
|
||||
.canvas()
|
||||
.draw_rect(Rect::new(left, top, right, bottom), &paint);
|
||||
}
|
||||
|
||||
/// This is called from JS after the WebGL context has been created.
|
||||
@@ -117,10 +119,22 @@ pub unsafe extern "C" fn resize_surface(state: *mut State, width: i32, height: i
|
||||
/// Draw a black rect at the specified coordinates.
|
||||
/// # Safety
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn draw_rect(state: *mut State, left: i32, right: i32, top: i32, bottom: i32) {
|
||||
pub unsafe extern "C" fn draw_rect(
|
||||
state: *mut State,
|
||||
left: i32,
|
||||
right: i32,
|
||||
top: i32,
|
||||
bottom: i32,
|
||||
) {
|
||||
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
|
||||
//state.surface.canvas().clear(Color::WHITE);
|
||||
render_rect(&mut state.surface, left as f32, right as f32, top as f32, bottom as f32);
|
||||
render_rect(
|
||||
&mut state.surface,
|
||||
left as f32,
|
||||
right as f32,
|
||||
top as f32,
|
||||
bottom as f32,
|
||||
);
|
||||
state
|
||||
.gpu_state
|
||||
.context
|
||||
@@ -129,7 +143,7 @@ pub unsafe extern "C" fn draw_rect(state: *mut State, left: i32, right: i32, top
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn translate(state: *mut State, dx: f32, dy: f32) {
|
||||
(*state).surface.canvas().translate((dx, dy));
|
||||
(*state).surface.canvas().translate((dx, dy));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -137,6 +151,11 @@ 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) {
|
||||
(*state).surface.canvas().reset_matrix();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
init_gl();
|
||||
}
|
||||
@@ -150,4 +169,4 @@ mod tests {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user