diff --git a/render-wasm/_build_env b/render-wasm/_build_env index d0e47b7de..886a17ba9 100644 --- a/render-wasm/_build_env +++ b/render-wasm/_build_env @@ -39,4 +39,4 @@ fi export EMCC_CFLAGS; export _CARGO_PARAMS; -export SKIA_BINARIES_URL="https://github.com/penpot/skia-binaries/releases/download/0.81.0-1/skia-binaries-24dee32a277b6c7b5357-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache.tar.gz" +export SKIA_BINARIES_URL="https://github.com/penpot/skia-binaries/releases/download/0.82.0-1/skia-binaries-24dee32a277b6c7b5357-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache.tar.gz" diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 82b4f1f2e..6e4619622 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -1,6 +1,7 @@ use skia_safe as skia; mod debug; +#[cfg(target_arch = "wasm32")] mod emscripten; mod math; mod mem; @@ -840,5 +841,6 @@ pub extern "C" fn set_grid_cells() { } fn main() { + #[cfg(target_arch = "wasm32")] init_gl!(); } diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 0aa98a2d3..df75c7279 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -4,6 +4,7 @@ use crate::uuid::Uuid; use std::collections::HashMap; use crate::view::Viewbox; +#[cfg(target_arch = "wasm32")] use crate::{run_script, run_script_int}; mod blend; @@ -33,10 +34,17 @@ const VIEWPORT_INTEREST_AREA_THRESHOLD: i32 = 1; const MAX_BLOCKING_TIME_MS: i32 = 32; const NODE_BATCH_THRESHOLD: i32 = 10; +#[cfg(target_arch = "wasm32")] fn get_time() -> i32 { run_script_int!("performance.now()") } +#[cfg(not(target_arch = "wasm32"))] +fn get_time() -> i32 { + let now = std::time::Instant::now(); + now.elapsed().as_millis() as i32 +} + pub struct NodeRenderState { pub id: Uuid, // We use this bool to keep that we've traversed all the children inside this node. @@ -485,14 +493,24 @@ impl RenderState { Ok(()) } + #[cfg(target_arch = "wasm32")] pub fn request_animation_frame(&mut self) -> i32 { run_script_int!("requestAnimationFrame(_process_animation_frame)") } + #[cfg(not(target_arch = "wasm32"))] + pub fn request_animation_frame(&mut self) -> i32 { + 0 + } + + #[cfg(target_arch = "wasm32")] pub fn cancel_animation_frame(&mut self, frame_id: i32) { run_script!(format!("cancelAnimationFrame({})", frame_id)) } + #[cfg(not(target_arch = "wasm32"))] + pub fn cancel_animation_frame(&mut self, _frame_id: i32) {} + pub fn process_animation_frame( &mut self, tree: &mut HashMap, diff --git a/render-wasm/src/render/debug.rs b/render-wasm/src/render/debug.rs index 0207b3383..0c44b7fa6 100644 --- a/render-wasm/src/render/debug.rs +++ b/render-wasm/src/render/debug.rs @@ -3,6 +3,7 @@ use skia_safe::{self as skia, Rect}; use super::{tiles, RenderState, SurfaceId}; +#[cfg(target_arch = "wasm32")] use crate::run_script; const DEBUG_SCALE: f32 = 0.2; @@ -170,19 +171,25 @@ pub fn render(render_state: &mut RenderState) { ); } +#[cfg(target_arch = "wasm32")] #[allow(dead_code)] pub fn console_debug_tile_surface(render_state: &mut RenderState, tile: tiles::Tile) { let base64_image = render_state.surfaces.base64_snapshot_tile(tile); + + #[cfg(target_arch = "wasm32")] run_script!(format!("console.log('%c ', 'font-size: 1px; background: url(data:image/png;base64,{base64_image}) no-repeat; padding: 100px; background-size: contain;')")) } +#[cfg(target_arch = "wasm32")] #[allow(dead_code)] pub fn console_debug_surface(render_state: &mut RenderState, id: SurfaceId) { let base64_image = render_state.surfaces.base64_snapshot(id); + run_script!(format!("console.log('%c ', 'font-size: 1px; background: url(data:image/png;base64,{base64_image}) no-repeat; padding: 100px; background-size: contain;')")) } #[allow(dead_code)] +#[cfg(target_arch = "wasm32")] pub fn console_debug_surface_rect(render_state: &mut RenderState, id: SurfaceId, rect: skia::Rect) { let int_rect = skia::IRect::from_ltrb( rect.left as i32, @@ -190,7 +197,9 @@ pub fn console_debug_surface_rect(render_state: &mut RenderState, id: SurfaceId, rect.right as i32, rect.bottom as i32, ); + let base64_image = render_state.surfaces.base64_snapshot_rect(id, int_rect); + if let Some(base64_image) = base64_image { run_script!(format!("console.log('%c ', 'font-size: 1px; background: url(data:image/png;base64,{base64_image}) no-repeat; padding: 100px; background-size: contain;')")) } diff --git a/render-wasm/src/shapes/modifiers.rs b/render-wasm/src/shapes/modifiers.rs index c9ef10ee7..ab4cab5b4 100644 --- a/render-wasm/src/shapes/modifiers.rs +++ b/render-wasm/src/shapes/modifiers.rs @@ -337,7 +337,7 @@ mod tests { shapes.insert(parent_id, parent.clone()); let bounds = - calculate_group_bounds(&parent, &shapes, HashMap::::new()).unwrap(); + calculate_group_bounds(&parent, &shapes, &HashMap::::new()).unwrap(); assert_eq!(bounds.width(), 3.0); assert_eq!(bounds.height(), 3.0); diff --git a/render-wasm/src/shapes/transform.rs b/render-wasm/src/shapes/transform.rs index bd495b0cc..d8b69b116 100644 --- a/render-wasm/src/shapes/transform.rs +++ b/render-wasm/src/shapes/transform.rs @@ -101,12 +101,11 @@ impl SerializableResult for TransformEntry { #[cfg(test)] mod tests { use super::*; - use crate::uuid::Uuid; #[test] fn test_serialization() { let entry = TransformEntry::new( - uuid!("550e8400-e29b-41d4-a716-446655440000"), + Uuid::new_v4(), Matrix::new_all(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0, 0.0, 1.0), ); diff --git a/render-wasm/src/uuid.rs b/render-wasm/src/uuid.rs index 2f78df686..7c2d64ec0 100644 --- a/render-wasm/src/uuid.rs +++ b/render-wasm/src/uuid.rs @@ -30,6 +30,17 @@ impl Uuid { pub fn from_u64_pair(high: u64, low: u64) -> Self { Self(ExternalUuid::from_u64_pair(high, low)) } + + #[cfg(test)] + pub fn new_v4() -> Self { + Self(ExternalUuid::new_v4()) + } +} + +impl From for Uuid { + fn from(uuid: ExternalUuid) -> Self { + Self(uuid) + } } impl fmt::Display for Uuid { diff --git a/render-wasm/test b/render-wasm/test index 752f25877..8b1a25d9d 100755 --- a/render-wasm/test +++ b/render-wasm/test @@ -1,8 +1,11 @@ #!/usr/bin/env bash _SCRIPT_DIR=$(dirname $0); - -export SKIA_BINARIES_URL="https://github.com/rust-skia/skia-binaries/releases/download/0.80.0/skia-binaries-9e7d2684a17084095aef-x86_64-unknown-linux-gnu-egl-gl-svg-textlayout-vulkan-wayland-webpd-webpe-x11.tar.gz" - pushd $_SCRIPT_DIR; -cargo test --bin render_wasm -- --show-output + +. ./_build_env + +export SKIA_BINARIES_URL="https://github.com/penpot/skia-binaries/releases/download/0.81.0-2/skia-binaries-24dee32a277b6c7b5357-aarch64-unknown-linux-gnu-gl-svg-textlayout-binary-cache.tar.gz" +export _CARGO_PARAMS="--target=aarch64-unknown-linux-gnu"; + +cargo test $_CARGO_PARAMS --bin render_wasm -- --show-output popd