mirror of
https://github.com/penpot/penpot.git
synced 2025-01-07 15:39:42 -05:00
Merge pull request #5352 from penpot/superalex-improve-full-rendering
🎉 Improve performance for zoom and pan with wasm render
This commit is contained in:
commit
e675ff6db5
4 changed files with 54 additions and 21 deletions
|
@ -25,6 +25,11 @@
|
||||||
(h/call internal-module "_render")
|
(h/call internal-module "_render")
|
||||||
(set! internal-frame-id nil))
|
(set! internal-frame-id nil))
|
||||||
|
|
||||||
|
(defn- render-without-cache
|
||||||
|
[_]
|
||||||
|
(h/call internal-module "_render_without_cache")
|
||||||
|
(set! internal-frame-id nil))
|
||||||
|
|
||||||
(defn cancel-render
|
(defn cancel-render
|
||||||
[]
|
[]
|
||||||
(when internal-frame-id
|
(when internal-frame-id
|
||||||
|
@ -120,13 +125,13 @@
|
||||||
;; https://rust-skia.github.io/doc/skia_safe/enum.BlendMode.html
|
;; https://rust-skia.github.io/doc/skia_safe/enum.BlendMode.html
|
||||||
(h/call internal-module "_set_shape_blend_mode" (translate-blend-mode blend-mode)))
|
(h/call internal-module "_set_shape_blend_mode" (translate-blend-mode blend-mode)))
|
||||||
|
|
||||||
(def debounce-render (fns/debounce render 100))
|
(def debounce-render-without-cache (fns/debounce render-without-cache 100))
|
||||||
|
|
||||||
(defn set-view
|
(defn set-view
|
||||||
[zoom vbox]
|
[zoom vbox]
|
||||||
(h/call internal-module "_set_view" zoom (- (:x vbox)) (- (:y vbox)))
|
(h/call internal-module "_set_view" zoom (- (:x vbox)) (- (:y vbox)))
|
||||||
(h/call internal-module "_navigate")
|
(h/call internal-module "_navigate")
|
||||||
(debounce-render))
|
(debounce-render-without-cache))
|
||||||
|
|
||||||
(defn set-objects
|
(defn set-objects
|
||||||
[objects]
|
[objects]
|
||||||
|
|
|
@ -41,7 +41,13 @@ pub extern "C" fn init(width: i32, height: i32, debug: u32) {
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn render() {
|
pub unsafe extern "C" fn render() {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
state.render_all();
|
state.render_all(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn render_without_cache() {
|
||||||
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
|
state.render_all(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use skia::gpu::{self, gl::FramebufferInfo, DirectContext};
|
use skia::gpu::{self, gl::FramebufferInfo, DirectContext};
|
||||||
|
use skia::Contains;
|
||||||
use skia_safe as skia;
|
use skia_safe as skia;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -54,6 +55,8 @@ impl GpuState {
|
||||||
pub(crate) struct CachedSurfaceImage {
|
pub(crate) struct CachedSurfaceImage {
|
||||||
pub image: Image,
|
pub image: Image,
|
||||||
pub viewbox: Viewbox,
|
pub viewbox: Viewbox,
|
||||||
|
// is_complete indicates if stored image renders the complete shape tree
|
||||||
|
pub is_complete: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct RenderState {
|
pub(crate) struct RenderState {
|
||||||
|
@ -176,14 +179,17 @@ impl RenderState {
|
||||||
self.reset_canvas();
|
self.reset_canvas();
|
||||||
if let Some(cached_surface_image) = &self.cached_surface_image {
|
if let Some(cached_surface_image) = &self.cached_surface_image {
|
||||||
// If we are drawing something bigger than the visible let's do a redraw
|
// If we are drawing something bigger than the visible let's do a redraw
|
||||||
if (viewbox.x > cached_surface_image.viewbox.x)
|
if !cached_surface_image.is_complete
|
||||||
|| (-viewbox.x + viewbox.area.width()
|
&& ((viewbox.x > cached_surface_image.viewbox.x)
|
||||||
> -cached_surface_image.viewbox.x + cached_surface_image.viewbox.area.width())
|
|| (-viewbox.x + viewbox.area.width()
|
||||||
|| (viewbox.y > cached_surface_image.viewbox.y)
|
> -cached_surface_image.viewbox.x
|
||||||
|| (-viewbox.y + viewbox.area.height()
|
+ cached_surface_image.viewbox.area.width())
|
||||||
> -cached_surface_image.viewbox.y + cached_surface_image.viewbox.area.height())
|
|| (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);
|
self.render_all(viewbox, shapes, true, debug);
|
||||||
} else {
|
} else {
|
||||||
let image = &cached_surface_image.image;
|
let image = &cached_surface_image.image;
|
||||||
let paint = skia::Paint::default();
|
let paint = skia::Paint::default();
|
||||||
|
@ -218,16 +224,20 @@ impl RenderState {
|
||||||
&mut self,
|
&mut self,
|
||||||
viewbox: &Viewbox,
|
viewbox: &Viewbox,
|
||||||
shapes: &HashMap<Uuid, Shape>,
|
shapes: &HashMap<Uuid, Shape>,
|
||||||
|
generate_cached_surface_image: bool,
|
||||||
debug: u32, // Debug flags
|
debug: u32, // Debug flags
|
||||||
) {
|
) {
|
||||||
self.reset_canvas();
|
self.reset_canvas();
|
||||||
self.scale(viewbox.zoom, viewbox.zoom);
|
self.scale(viewbox.zoom, viewbox.zoom);
|
||||||
self.translate(viewbox.x, viewbox.y);
|
self.translate(viewbox.x, viewbox.y);
|
||||||
self.render_shape_tree(&Uuid::nil(), viewbox, shapes);
|
let is_complete = self.render_shape_tree(&Uuid::nil(), viewbox, shapes);
|
||||||
self.cached_surface_image = Some(CachedSurfaceImage {
|
if generate_cached_surface_image || self.cached_surface_image.is_none() {
|
||||||
image: self.final_surface.image_snapshot(),
|
self.cached_surface_image = Some(CachedSurfaceImage {
|
||||||
viewbox: viewbox.clone(),
|
image: self.final_surface.image_snapshot(),
|
||||||
});
|
viewbox: viewbox.clone(),
|
||||||
|
is_complete,
|
||||||
|
});
|
||||||
|
}
|
||||||
if debug & debug::DEBUG_VISIBLE == debug::DEBUG_VISIBLE {
|
if debug & debug::DEBUG_VISIBLE == debug::DEBUG_VISIBLE {
|
||||||
self.render_debug(viewbox);
|
self.render_debug(viewbox);
|
||||||
}
|
}
|
||||||
|
@ -278,15 +288,22 @@ impl RenderState {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_shape_tree(&mut self, id: &Uuid, viewbox: &Viewbox, shapes: &HashMap<Uuid, Shape>) {
|
// Returns a boolean indicating if the viewbox contains the rendered shapes
|
||||||
|
fn render_shape_tree(
|
||||||
|
&mut self,
|
||||||
|
id: &Uuid,
|
||||||
|
viewbox: &Viewbox,
|
||||||
|
shapes: &HashMap<Uuid, Shape>,
|
||||||
|
) -> bool {
|
||||||
let shape = shapes.get(&id).unwrap();
|
let shape = shapes.get(&id).unwrap();
|
||||||
|
let mut is_complete = viewbox.area.contains(shape.selrect);
|
||||||
|
|
||||||
if !id.is_nil() {
|
if !id.is_nil() {
|
||||||
if !shape.selrect.intersects(viewbox.area) {
|
if !shape.selrect.intersects(viewbox.area) {
|
||||||
self.render_debug_shape(shape, false);
|
self.render_debug_shape(shape, false);
|
||||||
// TODO: This means that not all the shapes are renderer so we
|
// TODO: This means that not all the shapes are renderer so we
|
||||||
// need to call a render_all on the zoom out.
|
// need to call a render_all on the zoom out.
|
||||||
return;
|
return is_complete;
|
||||||
} else {
|
} else {
|
||||||
self.render_debug_shape(shape, true);
|
self.render_debug_shape(shape, true);
|
||||||
}
|
}
|
||||||
|
@ -303,10 +320,11 @@ impl RenderState {
|
||||||
// draw all the children shapes
|
// draw all the children shapes
|
||||||
let shape_ids = shape.children.iter();
|
let shape_ids = shape.children.iter();
|
||||||
for shape_id in shape_ids {
|
for shape_id in shape_ids {
|
||||||
self.render_shape_tree(shape_id, viewbox, shapes);
|
is_complete = self.render_shape_tree(shape_id, viewbox, shapes) && is_complete;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.final_surface.canvas().restore();
|
self.final_surface.canvas().restore();
|
||||||
self.drawing_surface.canvas().restore();
|
self.drawing_surface.canvas().restore();
|
||||||
|
return is_complete;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,9 +53,13 @@ impl<'a> State<'a> {
|
||||||
.navigate(&self.viewbox, &self.shapes, self.debug);
|
.navigate(&self.viewbox, &self.shapes, self.debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_all(&mut self) {
|
pub fn render_all(&mut self, generate_cached_surface_image: bool) {
|
||||||
self.render_state
|
self.render_state.render_all(
|
||||||
.render_all(&self.viewbox, &self.shapes, self.debug);
|
&self.viewbox,
|
||||||
|
&self.shapes,
|
||||||
|
generate_cached_surface_image,
|
||||||
|
self.debug,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_shape(&'a mut self, id: Uuid) {
|
pub fn use_shape(&'a mut self, id: Uuid) {
|
||||||
|
|
Loading…
Reference in a new issue