From 8e3b2ecad9678585e32af16c11b8e235f9dfade5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Esp=C3=ADn?= Date: Wed, 11 Oct 2023 21:31:04 +0200 Subject: [PATCH] feat: Avoid extra animations overhead (#321) * feat: Limit animations to 60fps for now * tweak * fix transitions --- crates/engine/src/skia.rs | 3 ++- crates/hooks/src/use_animation.rs | 6 ++++-- crates/hooks/src/use_animation_transition.rs | 7 ++++--- crates/renderer/src/window.rs | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/skia.rs b/crates/engine/src/skia.rs index 3877e9b54..e73b8784e 100644 --- a/crates/engine/src/skia.rs +++ b/crates/engine/src/skia.rs @@ -2,7 +2,8 @@ pub use skia_safe::{ font_style::{Slant, Weight, Width}, gpu::{ gl::{Format, FramebufferInfo, Interface}, - BackendRenderTarget, DirectContext, RecordingContext, SurfaceOrigin,surfaces::wrap_backend_render_target + surfaces::wrap_backend_render_target, + BackendRenderTarget, DirectContext, RecordingContext, SurfaceOrigin, }, gradient_shader::GradientShaderColors, path::ArcSize, diff --git a/crates/hooks/src/use_animation.rs b/crates/hooks/src/use_animation.rs index 672b0eb53..1caad2148 100644 --- a/crates/hooks/src/use_animation.rs +++ b/crates/hooks/src/use_animation.rs @@ -6,6 +6,8 @@ use uuid::Uuid; use crate::Animation; +const ANIMATION_MS: i32 = 16; // Assume 60 FPS for now + /// Manage the lifecyle of an [Animation]. #[derive(Clone)] pub struct AnimationManager<'a> { @@ -29,7 +31,7 @@ impl<'a> AnimationManager<'a> { // Spawn the animation that will run at 1ms speed self.cx.spawn(async move { - let mut ticker = interval(Duration::from_millis(1)); + let mut ticker = interval(Duration::from_millis(ANIMATION_MS as u64)); loop { // Stop running the animation if it was removed if *current_animation_id.current() == Some(new_id) { @@ -41,7 +43,7 @@ impl<'a> AnimationManager<'a> { // Advance one tick value.set(anim.move_value(index)); - index += 1; + index += ANIMATION_MS; // Wait 1m ticker.tick().await; diff --git a/crates/hooks/src/use_animation_transition.rs b/crates/hooks/src/use_animation_transition.rs index e456ecb48..6487e37f0 100644 --- a/crates/hooks/src/use_animation_transition.rs +++ b/crates/hooks/src/use_animation_transition.rs @@ -8,6 +8,8 @@ use uuid::Uuid; use crate::{Animation, TransitionAnimation}; +const ANIMATION_MS: i32 = 16; // Assume 60 FPS for now + /// Configure a `Transition` animation. #[derive(Clone, Debug, Copy, PartialEq)] pub enum Transition { @@ -172,7 +174,7 @@ impl<'a> TransitionsManager<'a> { // Spawn the animation that will run at 1ms speed self.cx.spawn(async move { - let mut ticker = interval(Duration::from_millis(1)); + let mut ticker = interval(Duration::from_millis(ANIMATION_MS as u64)); let mut index = 0; loop { // Stop running the animation if it's no longer selected @@ -193,7 +195,7 @@ impl<'a> TransitionsManager<'a> { } }); - index += 1; + index += ANIMATION_MS; // Wait 1ms ticker.tick().await; @@ -305,7 +307,6 @@ mod test { use std::time::Duration; use crate::{use_animation_transition, Transition, TransitionAnimation}; - use dioxus_hooks::use_effect; use freya::prelude::*; use freya_testing::launch_test; use tokio::time::sleep; diff --git a/crates/renderer/src/window.rs b/crates/renderer/src/window.rs index f0ced3b0d..29acf0e55 100644 --- a/crates/renderer/src/window.rs +++ b/crates/renderer/src/window.rs @@ -308,7 +308,7 @@ fn create_surface( ); let backend_render_target = BackendRenderTarget::new_gl(size, num_samples, stencil_size, fb_info); - wrap_backend_render_target( + wrap_backend_render_target( gr_context, &backend_render_target, SurfaceOrigin::BottomLeft,