Skip to content

Commit

Permalink
introduce frame interval
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Sep 22, 2024
1 parent 7924b89 commit 6068a7f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 45 deletions.
6 changes: 4 additions & 2 deletions docs/docs/config/renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ language: 'en'

- `disable-unfocused-render` - This property disable renderer processes while Rio is unfocused.

- `max-fps` - Limits the maximum number of frames per second that rio terminal will attempt to draw. If you set as `0` then this limit will be ignored. The default on MacOS is 180 and all other platforms is 90.
- `frame-interval` - Time scheduler between frames in milliseconds per second that rio terminal will attempt to draw. If you set as `0` then this value will be ignored. The default on MacOS/Windows is 1 and all other platforms is 3.

In case you would like define 60 frames per second as target, you would need to set each frame as 1/60th of one second long, so 16.67 milliseconds.

Example:

Expand All @@ -27,5 +29,5 @@ Example:
performance = "High"
backend = "Automatic"
disable-unfocused-render = false
max-fps = 180
frame-interval = 1
```
1 change: 1 addition & 0 deletions docs/docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ language: 'en'

## Unreleased

- *breaking* `renderer.max-fps` was replaced with `renderer.frame-interval`.
- MacOS: Add dock menu.
- MacOS: Add Shell and Edit menu.
- MacOS: Support to native modal that asks if wants to close app.
Expand Down
7 changes: 2 additions & 5 deletions frontends/rioterm/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ impl ApplicationHandler<EventPayload> for Application<'_> {
}

if route_id == route.window.screen.ctx().current_route() {
if let Some(limit) = route.window.next_render {
if let Some(interval) = route.window.frame_interval {
let timer_id = TimerId::new(Topic::RenderRoute, window_id);
let event = EventPayload::new(
RioEventType::Rio(RioEvent::Render),
window_id,
);

if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(event, limit, false, timer_id);
self.scheduler.schedule(event, interval, false, timer_id);
}
} else {
route.request_redraw();
Expand Down Expand Up @@ -1022,7 +1022,6 @@ impl ApplicationHandler<EventPayload> for Application<'_> {

WindowEvent::RedrawRequested => {
route.window.winit_window.pre_present_notify();
let start = std::time::Instant::now();
match route.path {
RoutePath::Assistant => {
route.window.screen.render_assistant(&route.assistant);
Expand All @@ -1040,10 +1039,8 @@ impl ApplicationHandler<EventPayload> for Application<'_> {
.render_dialog("Do you want to leave Rio?");
}
}
let duration = start.elapsed();
// println!("Time elapsed in render() is: {:?}", duration);
// }
route.window.compute_timestamp(duration);

event_loop.set_control_flow(ControlFlow::Wait);
}
Expand Down
51 changes: 20 additions & 31 deletions frontends/rioterm/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ impl Router<'_> {
pub struct RouteWindow<'a> {
pub is_focused: bool,
pub is_occluded: bool,
pub frame_time_limit: Option<Duration>,
pub current_render_time: Duration,
pub next_render: Option<Duration>,
// pub frame_time_limit: Option<Duration>,
// pub last_timestamp: Instant,
pub frame_interval: Option<Duration>,
pub winit_window: Window,
pub screen: Screen<'a>,
#[cfg(target_os = "macos")]
Expand All @@ -338,29 +338,22 @@ impl<'a> RouteWindow<'a> {
configure_window(&self.winit_window, config);
}

pub fn compute_timestamp(&mut self, current_render_duration: Duration) {
let frame_time_limit: Duration = match self.frame_time_limit {
Some(limit) => limit,
None => return,
};

// Render took a while so move to next frame
if current_render_duration > frame_time_limit {
self.next_render = Some(frame_time_limit);
self.current_render_time = Duration::from_millis(0);
return;
}
// #[inline]
// pub fn compute_timestamp(&mut self) {
// let frame_time_limit = match self.frame_time_limit {
// Some(limit) => limit,
// None => return,
// };

self.current_render_time += current_render_duration;
// if self.last_timestamp.elapsed() > frame_time_limit {
// // move to next frame
// self.next_render = Some(frame_time_limit);
// } else {
// self.next_render = None;
// }

// If previous render duration and current are still under of frame time limit
if self.current_render_time < frame_time_limit {
self.next_render = None;
} else {
self.current_render_time = Duration::from_millis(0);
self.next_render = Some(frame_time_limit);
}
}
// self.last_timestamp = Instant::now();
// }

#[allow(clippy::too_many_arguments)]
pub fn from_target<'b>(
Expand Down Expand Up @@ -406,18 +399,14 @@ impl<'a> RouteWindow<'a> {
)
.expect("Screen not created");

let frame_time_limit = if config.renderer.max_fps == 0 {
let frame_interval = if config.renderer.frame_interval == 0 {
None
} else {
Some(Duration::from_millis(
1000 / config.renderer.max_fps.clamp(1, 1000),
))
Some(Duration::from_millis(config.renderer.frame_interval))
};

Self {
frame_time_limit,
next_render: None,
current_render_time: Duration::from_millis(0),
frame_interval,
is_focused: true,
is_occluded: false,
winit_window,
Expand Down
6 changes: 3 additions & 3 deletions rio-backend/src/config/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub fn default_cursor_interval() -> u64 {
}

#[inline]
pub fn default_max_fps() -> u64 {
pub fn default_frame_interval() -> u64 {
if cfg!(target_os = "macos") || cfg!(target_os = "windows") {
240
1
} else {
90
3
}
}

Expand Down
8 changes: 4 additions & 4 deletions rio-backend/src/config/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::default_max_fps;
use crate::config::default_frame_interval;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

Expand All @@ -10,8 +10,8 @@ pub struct Renderer {
pub backend: Backend,
#[serde(default = "bool::default", rename = "disable-unfocused-render")]
pub disable_unfocused_render: bool,
#[serde(default = "default_max_fps", rename = "max-fps")]
pub max_fps: u64,
#[serde(default = "default_frame_interval", rename = "frame-interval")]
pub frame_interval: u64,
}

#[allow(clippy::derivable_impls)]
Expand All @@ -21,7 +21,7 @@ impl Default for Renderer {
performance: Performance::default(),
backend: Backend::default(),
disable_unfocused_render: false,
max_fps: default_max_fps(),
frame_interval: default_frame_interval(),
}
}
}
Expand Down

0 comments on commit 6068a7f

Please sign in to comment.