2024-10-25 12:10:59 +02:00
|
|
|
use skia_safe as skia;
|
|
|
|
use skia_safe::gpu::{self, gl::FramebufferInfo, DirectContext};
|
2024-11-12 15:56:33 +01:00
|
|
|
use uuid::Uuid;
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-13 10:56:00 +01:00
|
|
|
use crate::shapes::Shape;
|
2024-11-12 11:09:50 +01:00
|
|
|
use crate::state::State;
|
|
|
|
|
2024-10-25 12:10:59 +02:00
|
|
|
extern "C" {
|
|
|
|
pub fn emscripten_GetProcAddress(
|
|
|
|
name: *const ::std::os::raw::c_char,
|
|
|
|
) -> *const ::std::os::raw::c_void;
|
|
|
|
}
|
|
|
|
|
2024-11-12 11:09:50 +01:00
|
|
|
pub(crate) struct GpuState {
|
2024-10-25 12:10:59 +02:00
|
|
|
pub context: DirectContext,
|
|
|
|
framebuffer_info: FramebufferInfo,
|
|
|
|
}
|
|
|
|
|
2024-11-12 11:09:50 +01:00
|
|
|
pub(crate) struct RenderState {
|
2024-10-25 12:10:59 +02:00
|
|
|
pub gpu_state: GpuState,
|
|
|
|
pub surface: skia::Surface,
|
|
|
|
}
|
|
|
|
|
2024-11-12 11:09:50 +01:00
|
|
|
impl RenderState {
|
|
|
|
pub fn new(width: i32, height: i32) -> RenderState {
|
|
|
|
let mut gpu_state = create_gpu_state();
|
|
|
|
let surface = create_surface(&mut gpu_state, width, height);
|
|
|
|
RenderState { gpu_state, surface }
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) 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 _
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This needs to be done once per WebGL context.
|
|
|
|
pub(crate) 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) };
|
|
|
|
|
|
|
|
FramebufferInfo {
|
|
|
|
fboid: fboid.try_into().unwrap(),
|
|
|
|
format: skia_safe::gpu::gl::Format::RGBA8.into(),
|
|
|
|
protected: skia_safe::gpu::Protected::No,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GpuState {
|
|
|
|
context,
|
|
|
|
framebuffer_info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the Skia surface that will be used for rendering.
|
|
|
|
pub(crate) fn create_surface(gpu_state: &mut GpuState, width: i32, height: i32) -> skia::Surface {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:00:20 +01:00
|
|
|
pub(crate) fn flush(state: &mut State) {
|
|
|
|
state
|
|
|
|
.render_state
|
|
|
|
.gpu_state
|
|
|
|
.context
|
|
|
|
.flush_and_submit_surface(&mut state.render_state.surface, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn translate(state: &mut State, dx: f32, dy: f32) {
|
|
|
|
state.render_state.surface.canvas().translate((dx, dy));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn scale(state: &mut State, sx: f32, sy: f32) {
|
|
|
|
state.render_state.surface.canvas().scale((sx, sy));
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:56:00 +01:00
|
|
|
pub(crate) fn render_shape_tree(state: &mut State, id: Uuid) {
|
2024-11-12 15:56:33 +01:00
|
|
|
let shape = state.shapes.get(&id).unwrap();
|
2024-11-13 10:56:00 +01:00
|
|
|
|
|
|
|
// This is needed so the next non-children shape does not carry this shape's transform
|
|
|
|
state.render_state.surface.canvas().save();
|
|
|
|
|
|
|
|
render_single_shape(&mut state.render_state.surface, shape);
|
|
|
|
|
|
|
|
// draw all the children shapes
|
2024-11-13 11:48:29 +01:00
|
|
|
let shape_ids = shape.children.clone();
|
2024-11-13 10:56:00 +01:00
|
|
|
for shape_id in shape_ids {
|
|
|
|
render_shape_tree(state, shape_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.render_state.surface.canvas().restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_single_shape(surface: &mut skia::Surface, shape: &Shape) {
|
2024-11-12 15:56:33 +01:00
|
|
|
let r = skia::Rect::new(
|
|
|
|
shape.selrect.x1,
|
|
|
|
shape.selrect.y1,
|
|
|
|
shape.selrect.x2,
|
|
|
|
shape.selrect.y2,
|
|
|
|
);
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-12 15:56:33 +01:00
|
|
|
// Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
|
|
|
|
let mut matrix = skia::Matrix::new_identity();
|
|
|
|
let (translate_x, translate_y) = shape.translation();
|
2024-11-13 10:56:00 +01:00
|
|
|
let (scale_x, scale_y) = shape.scale();
|
2024-11-12 15:56:33 +01:00
|
|
|
let (skew_x, skew_y) = shape.skew();
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-13 10:56:00 +01:00
|
|
|
matrix.set_all(
|
|
|
|
scale_x,
|
|
|
|
skew_x,
|
|
|
|
translate_x,
|
|
|
|
skew_y,
|
|
|
|
scale_y,
|
|
|
|
translate_y,
|
|
|
|
0.,
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
);
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-12 15:56:33 +01:00
|
|
|
let mut center = r.center();
|
|
|
|
matrix.post_translate(center);
|
|
|
|
center.negate();
|
|
|
|
matrix.pre_translate(center);
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-13 10:56:00 +01:00
|
|
|
surface.canvas().concat(&matrix);
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-13 16:39:26 +01:00
|
|
|
// TODO: use blend mode for the shape as a whole, not in each fill
|
2024-11-13 15:07:16 +01:00
|
|
|
for fill in shape.fills().rev() {
|
2024-11-13 16:39:26 +01:00
|
|
|
let mut p = fill.to_paint();
|
|
|
|
p.set_blend_mode(shape.blend_mode.into());
|
|
|
|
surface.canvas().draw_rect(r, &p);
|
2024-11-12 11:09:50 +01:00
|
|
|
}
|
|
|
|
}
|