2024-11-20 15:05:16 +01:00
|
|
|
use skia_safe as skia;
|
|
|
|
use skia::gpu::{self, gl::FramebufferInfo, DirectContext};
|
2024-11-15 14:56:21 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::shapes::Shape;
|
2024-11-20 15:05:16 +01:00
|
|
|
use crate::view::Viewbox;
|
2024-11-19 08:58:35 +01:00
|
|
|
use crate::images::Image;
|
2024-11-20 15:05:16 +01:00
|
|
|
use crate::debug;
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
struct GpuState {
|
2024-10-25 12:10:59 +02:00
|
|
|
pub context: DirectContext,
|
|
|
|
framebuffer_info: FramebufferInfo,
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
impl GpuState {
|
|
|
|
fn new() -> Self {
|
2024-11-20 15:05:16 +01:00
|
|
|
let interface = skia::gpu::gl::Interface::new_native().unwrap();
|
|
|
|
let context = skia::gpu::direct_contexts::make_gl(interface, None).unwrap();
|
2024-11-14 11:47:10 +01:00
|
|
|
let framebuffer_info = {
|
|
|
|
let mut fboid: gl::types::GLint = 0;
|
|
|
|
unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
FramebufferInfo {
|
|
|
|
fboid: fboid.try_into().unwrap(),
|
2024-11-20 15:05:16 +01:00
|
|
|
format: skia::gpu::gl::Format::RGBA8.into(),
|
|
|
|
protected: skia::gpu::Protected::No,
|
2024-11-14 11:47:10 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GpuState {
|
|
|
|
context,
|
|
|
|
framebuffer_info,
|
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
/// Create a Skia surface that will be used for rendering.
|
2024-11-14 16:21:51 +01:00
|
|
|
fn create_target_surface(&mut self, width: i32, height: i32) -> skia::Surface {
|
2024-11-14 11:47:10 +01:00
|
|
|
let backend_render_target =
|
|
|
|
gpu::backend_render_targets::make_gl((width, height), 1, 8, self.framebuffer_info);
|
|
|
|
|
|
|
|
gpu::surfaces::wrap_backend_render_target(
|
|
|
|
&mut self.context,
|
|
|
|
&backend_render_target,
|
2024-11-20 15:05:16 +01:00
|
|
|
skia::gpu::SurfaceOrigin::BottomLeft,
|
|
|
|
skia::ColorType::RGBA8888,
|
2024-11-14 11:47:10 +01:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 08:58:35 +01:00
|
|
|
pub(crate) struct CachedSurfaceImage {
|
|
|
|
pub image: Image,
|
2024-11-20 15:05:16 +01:00
|
|
|
pub viewbox: Viewbox,
|
2024-11-19 08:58:35 +01:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
pub(crate) struct RenderState {
|
|
|
|
gpu_state: GpuState,
|
2024-11-14 16:21:51 +01:00
|
|
|
pub final_surface: skia::Surface,
|
|
|
|
pub drawing_surface: skia::Surface,
|
2024-11-20 15:05:16 +01:00
|
|
|
pub debug_surface: skia::Surface,
|
2024-11-19 08:58:35 +01:00
|
|
|
pub cached_surface_image: Option<CachedSurfaceImage>,
|
2024-11-14 11:47:10 +01:00
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
impl RenderState {
|
|
|
|
pub fn new(width: i32, height: i32) -> RenderState {
|
|
|
|
// This needs to be done once per WebGL context.
|
|
|
|
let mut gpu_state = GpuState::new();
|
2024-11-14 16:21:51 +01:00
|
|
|
let mut final_surface = gpu_state.create_target_surface(width, height);
|
|
|
|
let drawing_surface = final_surface
|
|
|
|
.new_surface_with_dimensions((width, height))
|
|
|
|
.unwrap();
|
2024-11-20 15:05:16 +01:00
|
|
|
let debug_surface = final_surface
|
|
|
|
.new_surface_with_dimensions((width, height))
|
|
|
|
.unwrap();
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 16:21:51 +01:00
|
|
|
RenderState {
|
|
|
|
gpu_state,
|
|
|
|
final_surface,
|
|
|
|
drawing_surface,
|
2024-11-20 15:05:16 +01:00
|
|
|
debug_surface,
|
2024-11-19 08:58:35 +01:00
|
|
|
cached_surface_image: None,
|
2024-11-14 16:21:51 +01:00
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
pub fn resize(&mut self, width: i32, height: i32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
let surface = self.gpu_state.create_target_surface(width, height);
|
|
|
|
self.final_surface = surface;
|
|
|
|
self.drawing_surface = self
|
|
|
|
.final_surface
|
|
|
|
.new_surface_with_dimensions((width, height))
|
|
|
|
.unwrap();
|
2024-11-20 15:05:16 +01:00
|
|
|
self.debug_surface = self
|
|
|
|
.final_surface
|
|
|
|
.new_surface_with_dimensions((width, height))
|
|
|
|
.unwrap();
|
2024-11-14 11:47:10 +01:00
|
|
|
}
|
2024-11-13 10:56:00 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn flush(&mut self) {
|
|
|
|
self.gpu_state
|
|
|
|
.context
|
2024-11-14 16:21:51 +01:00
|
|
|
.flush_and_submit_surface(&mut self.final_surface, None)
|
2024-11-13 10:56:00 +01:00
|
|
|
}
|
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn translate(&mut self, dx: f32, dy: f32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface.canvas().translate((dx, dy));
|
2024-11-14 12:08:50 +01:00
|
|
|
}
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn scale(&mut self, sx: f32, sy: f32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface.canvas().scale((sx, sy));
|
2024-11-14 12:08:50 +01:00
|
|
|
}
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn reset_canvas(&mut self) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface
|
|
|
|
.canvas()
|
2024-11-20 15:05:16 +01:00
|
|
|
.clear(skia::Color::TRANSPARENT)
|
2024-11-15 11:33:26 +01:00
|
|
|
.reset_matrix();
|
|
|
|
self.final_surface
|
|
|
|
.canvas()
|
2024-11-20 15:05:16 +01:00
|
|
|
.clear(skia::Color::TRANSPARENT)
|
|
|
|
.reset_matrix();
|
|
|
|
self.debug_surface
|
|
|
|
.canvas()
|
|
|
|
.clear(skia::Color::TRANSPARENT)
|
2024-11-15 11:33:26 +01:00
|
|
|
.reset_matrix();
|
2024-11-12 11:09:50 +01:00
|
|
|
}
|
2024-11-15 14:56:21 +01:00
|
|
|
|
|
|
|
pub fn render_single_shape(&mut self, shape: &Shape) {
|
|
|
|
// 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();
|
|
|
|
let (scale_x, scale_y) = shape.scale();
|
|
|
|
let (skew_x, skew_y) = shape.skew();
|
|
|
|
|
|
|
|
matrix.set_all(
|
|
|
|
scale_x,
|
|
|
|
skew_x,
|
|
|
|
translate_x,
|
|
|
|
skew_y,
|
|
|
|
scale_y,
|
|
|
|
translate_y,
|
|
|
|
0.,
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
);
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
let mut center = shape.selrect.center();
|
2024-11-15 14:56:21 +01:00
|
|
|
matrix.post_translate(center);
|
|
|
|
center.negate();
|
|
|
|
matrix.pre_translate(center);
|
|
|
|
|
|
|
|
self.drawing_surface.canvas().concat(&matrix);
|
|
|
|
|
|
|
|
for fill in shape.fills().rev() {
|
2024-11-20 15:05:16 +01:00
|
|
|
self.drawing_surface.canvas().draw_rect(shape.selrect, &fill.to_paint());
|
2024-11-15 14:56:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut paint = skia::Paint::default();
|
|
|
|
paint.set_blend_mode(shape.blend_mode.into());
|
|
|
|
self.drawing_surface.draw(
|
|
|
|
&mut self.final_surface.canvas(),
|
|
|
|
(0.0, 0.0),
|
2024-11-15 15:13:57 +01:00
|
|
|
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest),
|
2024-11-15 14:56:21 +01:00
|
|
|
Some(&paint),
|
|
|
|
);
|
|
|
|
self.drawing_surface
|
|
|
|
.canvas()
|
|
|
|
.clear(skia::Color::TRANSPARENT);
|
|
|
|
}
|
|
|
|
|
2024-11-19 08:58:35 +01:00
|
|
|
pub fn navigate(
|
2024-11-15 14:56:21 +01:00
|
|
|
&mut self,
|
2024-11-20 15:05:16 +01:00
|
|
|
viewbox: &Viewbox,
|
2024-11-15 14:56:21 +01:00
|
|
|
shapes: &HashMap<Uuid, Shape>,
|
2024-11-20 15:05:16 +01:00
|
|
|
debug: u32,
|
2024-11-15 14:56:21 +01:00
|
|
|
) {
|
|
|
|
self.reset_canvas();
|
2024-11-19 08:58:35 +01:00
|
|
|
if let Some(cached_surface_image) = &self.cached_surface_image {
|
|
|
|
// If we are drawing something bigger than the visible let's do a redraw
|
2024-11-20 15:05:16 +01:00
|
|
|
if (viewbox.x > cached_surface_image.viewbox.x) ||
|
|
|
|
(-viewbox.x + viewbox.area.width() > -cached_surface_image.viewbox.x + cached_surface_image.viewbox.area.width()) ||
|
|
|
|
(viewbox.y > cached_surface_image.viewbox.y) ||
|
|
|
|
(-viewbox.y + viewbox.area.height() > -cached_surface_image.viewbox.y + cached_surface_image.viewbox.area.height())
|
|
|
|
{
|
|
|
|
self.render_all(viewbox, shapes, debug);
|
2024-11-19 08:58:35 +01:00
|
|
|
}
|
2024-11-20 15:05:16 +01:00
|
|
|
else
|
|
|
|
{
|
2024-11-19 08:58:35 +01:00
|
|
|
let image = &cached_surface_image.image;
|
|
|
|
let paint = skia::Paint::default();
|
|
|
|
self.final_surface.canvas().save();
|
|
|
|
self.drawing_surface.canvas().save();
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
let navigate_zoom = viewbox.zoom / cached_surface_image.viewbox.zoom;
|
|
|
|
let navigate_x = cached_surface_image.viewbox.zoom * (viewbox.x - cached_surface_image.viewbox.x);
|
|
|
|
let navigate_y = cached_surface_image.viewbox.zoom * (viewbox.y - cached_surface_image.viewbox.y);
|
2024-11-19 08:58:35 +01:00
|
|
|
|
|
|
|
self.final_surface.canvas().scale((navigate_zoom, navigate_zoom));
|
|
|
|
self.final_surface.canvas().translate((navigate_x, navigate_y));
|
|
|
|
self.final_surface.canvas().draw_image(image.clone(), (0, 0), Some(&paint));
|
|
|
|
|
|
|
|
self.final_surface.canvas().restore();
|
|
|
|
self.drawing_surface.canvas().restore();
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 14:56:21 +01:00
|
|
|
|
2024-11-19 08:58:35 +01:00
|
|
|
self.flush();
|
|
|
|
}
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
pub fn render_all(
|
2024-11-19 08:58:35 +01:00
|
|
|
&mut self,
|
2024-11-20 15:05:16 +01:00
|
|
|
viewbox: &Viewbox,
|
2024-11-19 08:58:35 +01:00
|
|
|
shapes: &HashMap<Uuid, Shape>,
|
2024-11-20 15:05:16 +01:00
|
|
|
debug: u32, // Debug flags
|
2024-11-19 08:58:35 +01:00
|
|
|
) {
|
|
|
|
self.reset_canvas();
|
2024-11-20 15:05:16 +01:00
|
|
|
self.scale(viewbox.zoom, viewbox.zoom);
|
|
|
|
self.translate(viewbox.x, viewbox.y);
|
|
|
|
self.render_shape_tree(&Uuid::nil(), viewbox, shapes);
|
2024-11-19 08:58:35 +01:00
|
|
|
self.cached_surface_image = Some(CachedSurfaceImage {
|
|
|
|
image: self.final_surface.image_snapshot(),
|
2024-11-20 15:05:16 +01:00
|
|
|
viewbox: viewbox.clone(),
|
2024-11-19 08:58:35 +01:00
|
|
|
});
|
2024-11-20 15:05:16 +01:00
|
|
|
if debug & debug::DEBUG_VISIBLE == debug::DEBUG_VISIBLE {
|
|
|
|
self.render_debug(viewbox);
|
|
|
|
}
|
2024-11-19 08:58:35 +01:00
|
|
|
|
2024-11-15 14:56:21 +01:00
|
|
|
self.flush();
|
|
|
|
}
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
fn render_debug_view(&mut self, viewbox: &Viewbox) {
|
|
|
|
let mut paint = skia::Paint::default();
|
|
|
|
paint.set_style(skia::PaintStyle::Stroke);
|
|
|
|
paint.set_color(skia::Color::from_argb(255, 255, 0, 255));
|
|
|
|
paint.set_stroke_width(1.);
|
|
|
|
let mut scaled_rect = viewbox.area.clone();
|
|
|
|
let x = 100. + scaled_rect.x() * 0.2;
|
|
|
|
let y = 100. + scaled_rect.y() * 0.2;
|
|
|
|
let width = scaled_rect.width() * 0.2;
|
|
|
|
let height = scaled_rect.height() * 0.2;
|
|
|
|
scaled_rect.set_xywh(x, y, width, height);
|
|
|
|
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_debug_shape(&mut self, shape: &Shape, intersected: bool) {
|
|
|
|
let mut paint = skia::Paint::default();
|
|
|
|
paint.set_style(skia::PaintStyle::Stroke);
|
|
|
|
paint.set_color(
|
|
|
|
if intersected {
|
|
|
|
skia::Color::from_argb(255, 255, 255, 0)
|
|
|
|
} else {
|
|
|
|
skia::Color::from_argb(255, 0, 255, 255)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
paint.set_stroke_width(1.);
|
|
|
|
let mut scaled_rect = shape.selrect.clone();
|
|
|
|
let x = 100. + scaled_rect.x() * 0.2;
|
|
|
|
let y = 100. + scaled_rect.y() * 0.2;
|
|
|
|
let width = scaled_rect.width() * 0.2;
|
|
|
|
let height = scaled_rect.height() * 0.2;
|
|
|
|
scaled_rect.set_xywh(x, y, width, height);
|
|
|
|
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_debug(&mut self, viewbox: &Viewbox) {
|
|
|
|
let paint = skia::Paint::default();
|
|
|
|
self.render_debug_view(viewbox);
|
|
|
|
self.debug_surface.draw(
|
|
|
|
&mut self.final_surface.canvas(),
|
|
|
|
(0.0, 0.0),
|
|
|
|
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest),
|
|
|
|
Some(&paint),
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_shape_tree(&mut self, id: &Uuid, viewbox: &Viewbox, shapes: &HashMap<Uuid, Shape>) {
|
2024-11-15 14:56:21 +01:00
|
|
|
let shape = shapes.get(&id).unwrap();
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
if !id.is_nil() {
|
|
|
|
if !shape.selrect.intersects(viewbox.area) {
|
|
|
|
self.render_debug_shape(shape, false);
|
|
|
|
// TODO: This means that not all the shapes are renderer so we
|
|
|
|
// need to call a render_all on the zoom out.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
self.render_debug_shape(shape, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 14:56:21 +01:00
|
|
|
// This is needed so the next non-children shape does not carry this shape's transform
|
|
|
|
self.final_surface.canvas().save();
|
|
|
|
self.drawing_surface.canvas().save();
|
|
|
|
|
2024-11-20 15:05:16 +01:00
|
|
|
if !id.is_nil() {
|
2024-11-15 14:56:21 +01:00
|
|
|
self.render_single_shape(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw all the children shapes
|
2024-11-20 15:05:16 +01:00
|
|
|
let shape_ids = shape.children.iter();
|
2024-11-15 14:56:21 +01:00
|
|
|
for shape_id in shape_ids {
|
2024-11-20 15:05:16 +01:00
|
|
|
self.render_shape_tree(shape_id, viewbox, shapes);
|
2024-11-15 14:56:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.final_surface.canvas().restore();
|
|
|
|
self.drawing_surface.canvas().restore();
|
|
|
|
}
|
2024-11-12 11:09:50 +01:00
|
|
|
}
|