Skip to content

Commit

Permalink
feat: Avoid extra animations overhead (#321)
Browse files Browse the repository at this point in the history
* feat: Limit animations to 60fps for now

* tweak

* fix transitions
  • Loading branch information
marc2332 authored Oct 11, 2023
1 parent 71c635c commit 8e3b2ec
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/engine/src/skia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions crates/hooks/src/use_animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions crates/hooks/src/use_animation_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -193,7 +195,7 @@ impl<'a> TransitionsManager<'a> {
}
});

index += 1;
index += ANIMATION_MS;

// Wait 1ms
ticker.tick().await;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/renderer/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8e3b2ec

Please sign in to comment.