0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-01 02:06:53 -05:00
penpot/render-wasm/src/render.rs

377 lines
12 KiB
Rust
Raw Normal View History

mod gpu_state;
mod options;
use skia::Contains;
2024-11-21 17:23:49 +01:00
use skia_safe as skia;
use std::collections::HashMap;
use uuid::Uuid;
2024-12-03 14:43:07 +01:00
use crate::math::Rect;
2024-12-09 15:26:51 +01:00
use crate::shapes::{draw_image_in_container, Fill, Image, Kind, Shape};
2024-11-20 15:05:16 +01:00
use crate::view::Viewbox;
2024-11-12 11:09:50 +01:00
use gpu_state::GpuState;
use options::RenderOptions;
pub(crate) struct CachedSurfaceImage {
2024-11-21 17:23:49 +01:00
pub image: Image,
pub viewbox: Viewbox,
2024-11-26 15:06:49 +01:00
has_all_shapes: bool,
}
impl CachedSurfaceImage {
fn is_dirty(&self, viewbox: &Viewbox) -> bool {
!self.has_all_shapes && !self.viewbox.area.contains(viewbox.area)
}
}
pub(crate) struct RenderState {
gpu_state: GpuState,
pub final_surface: skia::Surface,
pub drawing_surface: skia::Surface,
2024-11-20 15:05:16 +01:00
pub debug_surface: skia::Surface,
pub cached_surface_image: Option<CachedSurfaceImage>,
options: RenderOptions,
2024-11-25 17:57:01 +01:00
pub viewbox: Viewbox,
2024-12-03 14:34:07 +01:00
images: HashMap<Uuid, Image>,
}
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();
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();
RenderState {
gpu_state,
final_surface,
drawing_surface,
2024-11-20 15:05:16 +01:00
debug_surface,
cached_surface_image: None,
options: RenderOptions::default(),
2024-11-25 17:57:01 +01:00
viewbox: Viewbox::new(width as f32, height as f32),
2024-11-25 17:30:24 +01:00
images: HashMap::with_capacity(2048),
}
}
2024-12-03 14:34:07 +01:00
pub fn add_image(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
let image_data = skia::Data::new_copy(image_data);
let image = Image::from_encoded(image_data).ok_or("Error decoding image data")?;
self.images.insert(id, image);
Ok(())
}
pub fn has_image(&mut self, id: &Uuid) -> bool {
self.images.contains_key(id)
}
pub fn set_debug_flags(&mut self, debug: u32) {
self.options.debug_flags = debug;
}
2024-11-25 17:57:01 +01:00
pub fn set_dpr(&mut self, dpr: f32) {
if Some(dpr) != self.options.dpr {
self.options.dpr = Some(dpr);
self.resize(
self.viewbox.width.floor() as i32,
self.viewbox.height.floor() as i32,
);
}
}
pub fn resize(&mut self, width: i32, height: i32) {
2024-11-25 17:57:01 +01:00
let dpr_width = (width as f32 * self.options.dpr()).floor() as i32;
let dpr_height = (height as f32 * self.options.dpr()).floor() as i32;
let surface = self.gpu_state.create_target_surface(dpr_width, dpr_height);
self.final_surface = surface;
self.drawing_surface = self
.final_surface
2024-11-25 17:57:01 +01:00
.new_surface_with_dimensions((dpr_width, dpr_height))
.unwrap();
2024-11-20 15:05:16 +01:00
self.debug_surface = self
.final_surface
2024-11-25 17:57:01 +01:00
.new_surface_with_dimensions((dpr_width, dpr_height))
2024-11-20 15:05:16 +01:00
.unwrap();
2024-11-25 17:57:01 +01:00
self.viewbox.set_wh(width as f32, height as f32);
}
2024-11-14 12:08:50 +01:00
pub fn flush(&mut self) {
self.gpu_state
.context
.flush_and_submit_surface(&mut self.final_surface, None)
}
2024-11-14 12:08:50 +01:00
pub fn translate(&mut self, dx: f32, dy: f32) {
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) {
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) {
self.drawing_surface
.canvas()
2024-11-20 15:05:16 +01:00
.clear(skia::Color::TRANSPARENT)
.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)
.reset_matrix();
2024-11-12 11:09:50 +01:00
}
pub fn render_single_shape(&mut self, shape: &Shape) {
let mut transform = 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();
transform.set_all(
scale_x,
skew_x,
translate_x,
skew_y,
scale_y,
translate_y,
0.,
0.,
1.,
);
// Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
let center = shape.selrect.center();
let mut matrix = skia::Matrix::new_identity();
matrix.pre_translate(center);
matrix.pre_concat(&transform);
matrix.pre_translate(-center);
self.drawing_surface.canvas().concat(&matrix);
for fill in shape.fills().rev() {
2024-12-09 15:26:51 +01:00
self.render_fill(fill, shape.selrect, &shape.kind);
}
let mut paint = skia::Paint::default();
paint.set_blend_mode(shape.blend_mode.into());
2024-11-25 07:17:29 +01:00
paint.set_alpha_f(shape.opacity);
self.drawing_surface.draw(
&mut self.final_surface.canvas(),
(0.0, 0.0),
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest),
Some(&paint),
);
self.drawing_surface
.canvas()
.clear(skia::Color::TRANSPARENT);
}
2024-11-25 17:57:01 +01:00
pub fn navigate(&mut self, shapes: &HashMap<Uuid, Shape>) -> Result<(), String> {
2024-11-26 15:06:49 +01:00
if let Some(cached_surface_image) = self.cached_surface_image.as_ref() {
2024-11-25 17:57:01 +01:00
if cached_surface_image.is_dirty(&self.viewbox) {
self.render_all(shapes, true);
2024-11-21 17:23:49 +01:00
} else {
2024-11-25 17:57:01 +01:00
self.render_all_from_cache()?;
}
}
2024-11-26 15:06:49 +01:00
Ok(())
}
2024-11-20 15:05:16 +01:00
pub fn render_all(
&mut self,
shapes: &HashMap<Uuid, Shape>,
generate_cached_surface_image: bool,
) {
self.reset_canvas();
2024-11-25 17:57:01 +01:00
self.scale(
self.viewbox.zoom * self.options.dpr(),
self.viewbox.zoom * self.options.dpr(),
);
self.translate(self.viewbox.pan_x, self.viewbox.pan_y);
2024-11-25 17:57:01 +01:00
let is_complete = self.render_shape_tree(&Uuid::nil(), shapes);
if generate_cached_surface_image || self.cached_surface_image.is_none() {
self.cached_surface_image = Some(CachedSurfaceImage {
image: self.final_surface.image_snapshot(),
2024-11-25 17:57:01 +01:00
viewbox: self.viewbox,
2024-11-26 15:06:49 +01:00
has_all_shapes: is_complete,
});
}
if self.options.is_debug_visible() {
2024-11-25 17:57:01 +01:00
self.render_debug();
2024-11-20 15:05:16 +01:00
}
self.flush();
}
2024-12-09 15:26:51 +01:00
fn render_fill(&mut self, fill: &Fill, selrect: Rect, kind: &Kind) {
match (fill, kind) {
(Fill::Image(image_fill), kind) => {
let image = self.images.get(&image_fill.id());
if let Some(image) = image {
draw_image_in_container(
&self.drawing_surface.canvas(),
&image,
image_fill.size(),
kind,
&fill.to_paint(&selrect),
);
}
}
(_, Kind::Rect(rect)) => {
self.drawing_surface
.canvas()
.draw_rect(rect, &fill.to_paint(&selrect));
}
2024-12-09 11:45:43 +01:00
(_, Kind::Circle(rect)) => {
self.drawing_surface
.canvas()
.draw_oval(rect, &fill.to_paint(&selrect));
}
2024-12-09 15:26:51 +01:00
(_, Kind::Path(path)) => {
self.drawing_surface
.canvas()
.draw_path(&path.to_skia_path(), &fill.to_paint(&selrect));
2024-12-03 14:43:07 +01:00
}
}
}
2024-11-25 17:57:01 +01:00
fn render_all_from_cache(&mut self) -> Result<(), String> {
2024-11-26 15:06:49 +01:00
self.reset_canvas();
let cached = self
.cached_surface_image
.as_ref()
.ok_or("Uninitialized cached surface image")?;
let image = &cached.image;
let paint = skia::Paint::default();
self.final_surface.canvas().save();
self.drawing_surface.canvas().save();
2024-11-25 17:57:01 +01:00
let navigate_zoom = self.viewbox.zoom / cached.viewbox.zoom;
let navigate_x = cached.viewbox.zoom * (self.viewbox.pan_x - cached.viewbox.pan_x);
let navigate_y = cached.viewbox.zoom * (self.viewbox.pan_y - cached.viewbox.pan_y);
2024-11-26 15:06:49 +01:00
self.final_surface
.canvas()
.scale((navigate_zoom, navigate_zoom));
2024-11-25 17:57:01 +01:00
self.final_surface.canvas().translate((
navigate_x * self.options.dpr(),
navigate_y * self.options.dpr(),
));
2024-11-26 15:06:49 +01:00
self.final_surface
.canvas()
.draw_image(image.clone(), (0, 0), Some(&paint));
self.final_surface.canvas().restore();
self.drawing_surface.canvas().restore();
self.flush();
Ok(())
}
2024-11-25 17:57:01 +01:00
fn render_debug_view(&mut self) {
2024-11-20 15:05:16 +01:00
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.);
2024-11-25 17:57:01 +01:00
let mut scaled_rect = self.viewbox.area.clone();
2024-11-20 15:05:16 +01:00
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);
2024-11-20 15:05:16 +01:00
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);
2024-11-21 17:23:49 +01:00
paint.set_color(if intersected {
skia::Color::from_argb(255, 255, 255, 0)
} else {
skia::Color::from_argb(255, 0, 255, 255)
});
2024-11-20 15:05:16 +01:00
paint.set_stroke_width(1.);
2024-11-20 15:05:16 +01:00
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);
2024-11-20 15:05:16 +01:00
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
}
2024-11-25 17:57:01 +01:00
fn render_debug(&mut self) {
2024-11-20 15:05:16 +01:00
let paint = skia::Paint::default();
2024-11-25 17:57:01 +01:00
self.render_debug_view();
2024-11-20 15:05:16 +01:00
self.debug_surface.draw(
&mut self.final_surface.canvas(),
(0.0, 0.0),
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest),
Some(&paint),
);
}
// Returns a boolean indicating if the viewbox contains the rendered shapes
2024-11-25 17:57:01 +01:00
fn render_shape_tree(&mut self, id: &Uuid, shapes: &HashMap<Uuid, Shape>) -> bool {
let shape = shapes.get(&id).unwrap();
2024-11-25 17:57:01 +01:00
let mut is_complete = self.viewbox.area.contains(shape.selrect);
2024-11-20 15:05:16 +01:00
if !id.is_nil() {
if !shape.selrect.intersects(self.viewbox.area) || shape.hidden {
2024-11-20 15:05:16 +01:00
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.
2024-11-25 17:57:01 +01:00
return is_complete; // TODO return is_complete or return false??
2024-11-20 15:05:16 +01:00
} else {
self.render_debug_shape(shape, true);
}
}
// 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() {
self.render_single_shape(shape);
if shape.clip_content {
self.drawing_surface.canvas().clip_rect(
shape.selrect,
skia::ClipOp::Intersect,
true,
);
}
}
// draw all the children shapes
2024-11-20 15:05:16 +01:00
let shape_ids = shape.children.iter();
for shape_id in shape_ids {
2024-11-25 17:57:01 +01:00
is_complete = self.render_shape_tree(shape_id, shapes) && is_complete;
}
self.final_surface.canvas().restore();
self.drawing_surface.canvas().restore();
return is_complete;
}
2024-11-12 11:09:50 +01:00
}