Skip to content

Commit

Permalink
Refactor animation timing to use lazy clocks
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Nov 24, 2024
1 parent 07ad6f0 commit d616434
Show file tree
Hide file tree
Showing 21 changed files with 532 additions and 432 deletions.
11 changes: 1 addition & 10 deletions niri-visual-tests/src/cases/gradient_angle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::f32::consts::{FRAC_PI_2, PI};
use std::sync::atomic::Ordering;
use std::time::Duration;

use niri::animation::ANIMATION_SLOWDOWN;
use niri::render_helpers::border::BorderRenderElement;
use niri_config::{Color, CornerRadius, GradientInterpolation};
use smithay::backend::renderer::element::RenderElement;
Expand Down Expand Up @@ -31,20 +29,13 @@ impl TestCase for GradientAngle {
}

fn advance_animations(&mut self, current_time: Duration) {
let mut delta = if self.prev_time.is_zero() {
let delta = if self.prev_time.is_zero() {
Duration::ZERO
} else {
current_time.saturating_sub(self.prev_time)
};
self.prev_time = current_time;

let slowdown = ANIMATION_SLOWDOWN.load(Ordering::SeqCst);
if slowdown == 0. {
delta = Duration::ZERO
} else {
delta = delta.div_f64(slowdown);
}

self.angle += delta.as_secs_f32() * PI;

if self.angle >= PI * 2. {
Expand Down
11 changes: 1 addition & 10 deletions niri-visual-tests/src/cases/gradient_area.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::f32::consts::{FRAC_PI_4, PI};
use std::sync::atomic::Ordering;
use std::time::Duration;

use niri::animation::ANIMATION_SLOWDOWN;
use niri::layout::focus_ring::FocusRing;
use niri::render_helpers::border::BorderRenderElement;
use niri_config::{Color, CornerRadius, FloatOrInt, GradientInterpolation};
Expand Down Expand Up @@ -43,20 +41,13 @@ impl TestCase for GradientArea {
}

fn advance_animations(&mut self, current_time: Duration) {
let mut delta = if self.prev_time.is_zero() {
let delta = if self.prev_time.is_zero() {
Duration::ZERO
} else {
current_time.saturating_sub(self.prev_time)
};
self.prev_time = current_time;

let slowdown = ANIMATION_SLOWDOWN.load(Ordering::SeqCst);
if slowdown == 0. {
delta = Duration::ZERO
} else {
delta = delta.div_f64(slowdown);
}

self.progress += delta.as_secs_f32() * PI;

if self.progress >= PI * 2. {
Expand Down
15 changes: 8 additions & 7 deletions niri-visual-tests/src/cases/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Layout {
let mut layout = niri::layout::Layout::with_options(clock.clone(), options);
layout.add_output(output.clone());

let start_time = clock.now();
let start_time = clock.now_unadjusted();

Self {
output,
Expand Down Expand Up @@ -207,24 +207,25 @@ impl TestCase for Layout {
self.layout.are_animations_ongoing(Some(&self.output)) || !self.steps.is_empty()
}

fn advance_animations(&mut self, current_time: Duration) {
fn advance_animations(&mut self, _current_time: Duration) {
let now_unadjusted = self.clock.now_unadjusted();
let run = self
.steps
.keys()
.copied()
.filter(|delay| self.start_time + *delay <= current_time)
.filter(|delay| self.start_time + *delay <= now_unadjusted)
.collect::<Vec<_>>();
for delay in &run {
let now = self.start_time + *delay;
self.clock.set_time_override(Some(now));
self.layout.advance_animations(now);
self.clock.set_unadjusted(now);
self.layout.advance_animations();

let f = self.steps.remove(delay).unwrap();
f(self);
}

self.clock.set_time_override(None);
self.layout.advance_animations(current_time);
self.clock.set_unadjusted(now_unadjusted);
self.layout.advance_animations();
}

fn render(
Expand Down
4 changes: 2 additions & 2 deletions niri-visual-tests/src/cases/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl TestCase for Tile {
self.tile.are_animations_ongoing()
}

fn advance_animations(&mut self, current_time: Duration) {
self.tile.advance_animations(current_time);
fn advance_animations(&mut self, _current_time: Duration) {
self.tile.advance_animations();
}

fn render(
Expand Down
14 changes: 5 additions & 9 deletions niri-visual-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
extern crate tracing;

use std::env;
use std::sync::atomic::Ordering;

use adw::prelude::{AdwApplicationWindowExt, NavigationPageExt};
use cases::Args;
use gtk::prelude::{
AdjustmentExt, ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt,
};
use gtk::prelude::{ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt};
use gtk::{gdk, gio, glib};
use niri::animation::ANIMATION_SLOWDOWN;
use smithay_view::SmithayView;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -66,20 +62,23 @@ fn on_startup(_app: &adw::Application) {

fn build_ui(app: &adw::Application) {
let stack = gtk::Stack::new();
let anim_adjustment = gtk::Adjustment::new(1., 0., 10., 0.1, 0.5, 0.);

struct S {
stack: gtk::Stack,
anim_adjustment: gtk::Adjustment,
}

impl S {
fn add<T: TestCase + 'static>(&self, make: impl Fn(Args) -> T + 'static, title: &str) {
let view = SmithayView::new(make);
let view = SmithayView::new(make, &self.anim_adjustment);
self.stack.add_titled(&view, None, title);
}
}

let s = S {
stack: stack.clone(),
anim_adjustment: anim_adjustment.clone(),
};

s.add(Window::freeform, "Freeform Window");
Expand Down Expand Up @@ -133,9 +132,6 @@ fn build_ui(app: &adw::Application) {

let content_headerbar = adw::HeaderBar::new();

let anim_adjustment = gtk::Adjustment::new(1., 0., 10., 0.1, 0.5, 0.);
anim_adjustment
.connect_value_changed(|adj| ANIMATION_SLOWDOWN.store(adj.value(), Ordering::SeqCst));
let anim_scale = gtk::Scale::new(gtk::Orientation::Horizontal, Some(&anim_adjustment));
anim_scale.set_hexpand(true);

Expand Down
33 changes: 29 additions & 4 deletions niri-visual-tests/src/smithay_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use smithay::utils::Size;

Expand All @@ -7,13 +8,13 @@ use crate::cases::{Args, TestCase};
mod imp {
use std::cell::{Cell, OnceCell, RefCell};
use std::ptr::null;
use std::time::Duration;

use anyhow::{ensure, Context};
use gtk::gdk;
use gtk::prelude::*;
use niri::animation::Clock;
use niri::render_helpers::{resources, shaders};
use niri::utils::get_monotonic_time;
use smithay::backend::egl::ffi::egl;
use smithay::backend::egl::EGLContext;
use smithay::backend::renderer::gles::GlesRenderer;
Expand All @@ -31,7 +32,7 @@ mod imp {
renderer: RefCell<Option<Result<GlesRenderer, ()>>>,
pub make_test_case: OnceCell<DynMakeTestCase>,
test_case: RefCell<Option<Box<dyn TestCase>>>,
clock: RefCell<Clock>,
pub clock: RefCell<Clock>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -127,6 +128,10 @@ mod imp {

let size = self.size.get();

let frame_clock = self.obj().frame_clock().unwrap();
let time = Duration::from_micros(frame_clock.frame_time() as u64);
self.clock.borrow_mut().set_unadjusted(time);

// Create the test case if missing.
let mut case = self.test_case.borrow_mut();
let case = case.get_or_insert_with(|| {
Expand All @@ -138,7 +143,7 @@ mod imp {
make(args)
});

case.advance_animations(get_monotonic_time());
case.advance_animations(self.clock.borrow_mut().now());

let rect: Rectangle<i32, Physical> = Rectangle::from_loc_and_size((0, 0), size);

Expand Down Expand Up @@ -238,13 +243,33 @@ glib::wrapper! {
}

impl SmithayView {
pub fn new<T: TestCase + 'static>(make_test_case: impl Fn(Args) -> T + 'static) -> Self {
pub fn new<T: TestCase + 'static>(
make_test_case: impl Fn(Args) -> T + 'static,
anim_adjustment: &gtk::Adjustment,
) -> Self {
let obj: Self = glib::Object::builder().build();

let make = move |args| Box::new(make_test_case(args)) as Box<dyn TestCase>;
let make_test_case = Box::new(make) as _;
let _ = obj.imp().make_test_case.set(make_test_case);

anim_adjustment.connect_value_changed({
let obj = obj.downgrade();
move |adj| {
if let Some(obj) = obj.upgrade() {
let mut clock = obj.imp().clock.borrow_mut();
let instantly = adj.value() == 0.0;
let rate = if instantly {
1.0
} else {
1.0 / adj.value().max(0.001)
};
clock.set_rate(rate);
clock.set_complete_instantly(instantly);
}
}
});

obj
}
}
Loading

0 comments on commit d616434

Please sign in to comment.