mirror of
https://github.com/penpot/penpot.git
synced 2025-06-15 01:02:57 +02:00
♻️ Refactor how rAF/cAF is handled (#6241)
This commit is contained in:
parent
99e64ad387
commit
304c44048f
5 changed files with 45 additions and 28 deletions
|
@ -7,6 +7,7 @@ else
|
||||||
fi
|
fi
|
||||||
|
|
||||||
EMCC_CFLAGS="--no-entry \
|
EMCC_CFLAGS="--no-entry \
|
||||||
|
--js-library src/js/wapi.js \
|
||||||
-sASSERTIONS=1 \
|
-sASSERTIONS=1 \
|
||||||
-sALLOW_TABLE_GROWTH=1 \
|
-sALLOW_TABLE_GROWTH=1 \
|
||||||
-sALLOW_MEMORY_GROWTH=1 \
|
-sALLOW_MEMORY_GROWTH=1 \
|
||||||
|
@ -33,7 +34,7 @@ else
|
||||||
# -gseparate-dwarf
|
# -gseparate-dwarf
|
||||||
# -gsplit-dwarf
|
# -gsplit-dwarf
|
||||||
# -gsource-map
|
# -gsource-map
|
||||||
EMCC_CFLAGS="-g $EMCC_CFLAGS -sMALLOC=emmalloc-debug"
|
EMCC_CFLAGS="-g $EMCC_CFLAGS -sVERBOSE=1 -sMALLOC=emmalloc-debug"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export EMCC_CFLAGS;
|
export EMCC_CFLAGS;
|
||||||
|
|
8
render-wasm/src/js/wapi.js
Normal file
8
render-wasm/src/js/wapi.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
addToLibrary({
|
||||||
|
wapi_requestAnimationFrame: function wapi_requestAnimationFrame() {
|
||||||
|
return window.requestAnimationFrame(Module._process_animation_frame);
|
||||||
|
},
|
||||||
|
wapi_cancelAnimationFrame: function wapi_cancelAnimationFrame(frameId) {
|
||||||
|
return window.cancelAnimationFrame(frameId);
|
||||||
|
}
|
||||||
|
});
|
|
@ -12,6 +12,7 @@ mod state;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod uuid;
|
mod uuid;
|
||||||
mod view;
|
mod view;
|
||||||
|
mod wapi;
|
||||||
mod wasm;
|
mod wasm;
|
||||||
|
|
||||||
use crate::mem::SerializableResult;
|
use crate::mem::SerializableResult;
|
||||||
|
|
|
@ -4,9 +4,10 @@ use crate::uuid::Uuid;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::performance;
|
use crate::performance;
|
||||||
use crate::view::Viewbox;
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use crate::{run_script, run_script_int};
|
use crate::run_script;
|
||||||
|
use crate::view::Viewbox;
|
||||||
|
use crate::wapi;
|
||||||
|
|
||||||
mod blend;
|
mod blend;
|
||||||
mod debug;
|
mod debug;
|
||||||
|
@ -433,7 +434,7 @@ impl RenderState {
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
if self.render_in_progress {
|
if self.render_in_progress {
|
||||||
if let Some(frame_id) = self.render_request_id {
|
if let Some(frame_id) = self.render_request_id {
|
||||||
self.cancel_animation_frame(frame_id);
|
wapi::cancel_animation_frame!(frame_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
performance::begin_measure!("render");
|
performance::begin_measure!("render");
|
||||||
|
@ -491,28 +492,6 @@ impl RenderState {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
pub fn request_animation_frame(&mut self) -> i32 {
|
|
||||||
#[cfg(feature = "profile-raf")]
|
|
||||||
performance::mark!("request_animation_frame");
|
|
||||||
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) {
|
|
||||||
#[cfg(feature = "profile-raf")]
|
|
||||||
performance::mark!("cancel_animation_frame");
|
|
||||||
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(
|
pub fn process_animation_frame(
|
||||||
&mut self,
|
&mut self,
|
||||||
tree: &mut HashMap<Uuid, Shape>,
|
tree: &mut HashMap<Uuid, Shape>,
|
||||||
|
@ -526,9 +505,9 @@ impl RenderState {
|
||||||
|
|
||||||
if self.render_in_progress {
|
if self.render_in_progress {
|
||||||
if let Some(frame_id) = self.render_request_id {
|
if let Some(frame_id) = self.render_request_id {
|
||||||
self.cancel_animation_frame(frame_id);
|
wapi::cancel_animation_frame!(frame_id);
|
||||||
}
|
}
|
||||||
self.render_request_id = Some(self.request_animation_frame());
|
self.render_request_id = Some(wapi::request_animation_frame!());
|
||||||
} else {
|
} else {
|
||||||
performance::end_measure!("render");
|
performance::end_measure!("render");
|
||||||
}
|
}
|
||||||
|
|
28
render-wasm/src/wapi.rs
Normal file
28
render-wasm/src/wapi.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! request_animation_frame {
|
||||||
|
() => {
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
extern "C" {
|
||||||
|
pub fn wapi_requestAnimationFrame() -> i32;
|
||||||
|
}
|
||||||
|
unsafe { wapi_requestAnimationFrame() }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! cancel_animation_frame {
|
||||||
|
($frame_id:expr) => {
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
extern "C" {
|
||||||
|
pub fn wapi_cancelAnimationFrame(frame_id: i32);
|
||||||
|
}
|
||||||
|
unsafe { wapi_cancelAnimationFrame($frame_id) }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use cancel_animation_frame;
|
||||||
|
pub use request_animation_frame;
|
Loading…
Add table
Add a link
Reference in a new issue