Reduce CPU usage while window is unfocused and not hovered over
Only render if window is focused, hovered, subscript is running, or coroutine is active. Closes #18
This commit is contained in:
1 parent
0066a05e13
commit
ae6a24f82c
9 files changed
+116
-30
No files matched your search
+5
-1
@@ -2,6 +2,10 @@
|
||||
|
||||
## [Unreleased] - ReleaseDate
|
||||
|
||||
### Changed
|
||||
|
||||
- Reduce CPU usage while window is unfocused and not hovered over
|
||||
|
||||
## [0.2.8] - 2025-11-07
|
||||
|
||||
### Changed
|
||||
@@ -118,8 +122,8 @@
|
||||
- First release
|
||||
|
||||
<!-- next-url -->
|
||||
[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.8...HEAD
|
||||
|
||||
[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.8...HEAD
|
||||
[0.2.8]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.7...v0.2.8
|
||||
[0.2.7]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.6...v0.2.7
|
||||
[0.2.6]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.5...v0.2.6
|
||||
|
||||
+63
-25
@@ -19,6 +19,7 @@ use winit::{
|
||||
|
||||
struct FrameOutput {
|
||||
pub render_job: RenderJob,
|
||||
pub should_continue: bool,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
@@ -39,6 +40,7 @@ pub struct App {
|
||||
gfx_context: Option<GraphicsContext>,
|
||||
state: AppState,
|
||||
tessellator: Tessellator,
|
||||
needs_reconfigure: bool,
|
||||
force_render: bool,
|
||||
current_mode: AppMode,
|
||||
}
|
||||
@@ -54,7 +56,8 @@ impl App {
|
||||
texture_manager: WrappedTextureManager::new(),
|
||||
},
|
||||
tessellator: Tessellator::default(),
|
||||
force_render: false,
|
||||
needs_reconfigure: true,
|
||||
force_render: true,
|
||||
current_mode: AppMode::Install(InstallMode::new()),
|
||||
})
|
||||
}
|
||||
@@ -92,8 +95,6 @@ impl App {
|
||||
{
|
||||
RenderJob::Skip
|
||||
} else {
|
||||
self.force_render = false;
|
||||
|
||||
let meshes = self.tessellator.convert_clipped_primitives(
|
||||
mode_output.primitives,
|
||||
font_atlas_size,
|
||||
@@ -106,7 +107,10 @@ impl App {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(FrameOutput { render_job })
|
||||
Ok(FrameOutput {
|
||||
render_job,
|
||||
should_continue: mode_output.should_continue,
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, event: AppEvent) {
|
||||
@@ -177,26 +181,50 @@ impl ApplicationHandler<GraphicsContext> for App {
|
||||
return;
|
||||
}
|
||||
|
||||
let render_job = match self.frame() {
|
||||
Ok(FrameOutput { render_job }) => render_job,
|
||||
Err(err) => {
|
||||
log::error!("{err}");
|
||||
event_loop.exit();
|
||||
return;
|
||||
if self.needs_reconfigure {
|
||||
if let Some(ref mut gfx) = self.gfx_context {
|
||||
let size = gfx.window.inner_size();
|
||||
gfx.resize(size.width, size.height);
|
||||
}
|
||||
};
|
||||
self.needs_reconfigure = false;
|
||||
// Render at least one frame after reconfigure
|
||||
self.force_render = true;
|
||||
}
|
||||
|
||||
if let Some(ref mut gfx) = self.gfx_context {
|
||||
match gfx.render(render_job) {
|
||||
Ok(_) => {}
|
||||
// Reconfigure the surface if it's lost or outdated
|
||||
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
|
||||
let size = gfx.window.inner_size();
|
||||
gfx.resize(size.width, size.height);
|
||||
self.force_render = true;
|
||||
}
|
||||
let is_focused = self.state.window.is_focused;
|
||||
let is_hovered = self.state.window.is_hovered;
|
||||
let should_render = is_focused || is_hovered || self.force_render;
|
||||
|
||||
if should_render {
|
||||
let FrameOutput {
|
||||
render_job,
|
||||
should_continue,
|
||||
} = match self.frame() {
|
||||
Ok(frame_output) => frame_output,
|
||||
Err(err) => {
|
||||
log::error!("Unable to render: {err}");
|
||||
log::error!("{err}");
|
||||
event_loop.exit();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ref mut gfx) = self.gfx_context {
|
||||
match gfx.render(render_job) {
|
||||
Ok(_) => {
|
||||
self.force_render = should_continue;
|
||||
|
||||
if is_focused || is_hovered || should_continue {
|
||||
self.state.window.request_redraw();
|
||||
}
|
||||
}
|
||||
// Reconfigure the surface if it's lost or outdated
|
||||
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
|
||||
self.needs_reconfigure = true;
|
||||
self.state.window.request_redraw();
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("Unable to render: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,11 +232,14 @@ impl ApplicationHandler<GraphicsContext> for App {
|
||||
profiling::finish_frame!();
|
||||
}
|
||||
WindowEvent::Resized(size) => {
|
||||
if let Some(ref mut gfx) = self.gfx_context {
|
||||
gfx.resize(size.width, size.height);
|
||||
self.force_render = true;
|
||||
}
|
||||
self.state.window.size = PhysicalSize::new(size.width, size.height);
|
||||
self.needs_reconfigure = true;
|
||||
}
|
||||
WindowEvent::Focused(focused) => {
|
||||
self.state.window.is_focused = focused;
|
||||
if focused {
|
||||
self.state.window.request_redraw();
|
||||
}
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
|
||||
self.state.window.scale_factor = scale_factor as f32;
|
||||
@@ -264,6 +295,13 @@ impl ApplicationHandler<GraphicsContext> for App {
|
||||
let pos = PhysicalPoint::new(position.x as f32, position.y as f32);
|
||||
self.state.set_mouse_pos(pos);
|
||||
}
|
||||
WindowEvent::CursorEntered { .. } => {
|
||||
self.state.window.is_hovered = true;
|
||||
self.state.window.request_redraw();
|
||||
}
|
||||
WindowEvent::CursorLeft { .. } => {
|
||||
self.state.window.is_hovered = false;
|
||||
}
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
let delta = match delta {
|
||||
MouseScrollDelta::LineDelta(_, y) => y,
|
||||
|
||||
+1
-3
@@ -147,8 +147,6 @@ impl GraphicsContext {
|
||||
pub fn render(&mut self, render_job: RenderJob) -> Result<(), wgpu::SurfaceError> {
|
||||
profiling::scope!("render");
|
||||
|
||||
self.window.request_redraw();
|
||||
|
||||
if !self.is_surface_configured {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -217,7 +215,7 @@ impl GraphicsContext {
|
||||
pixels_per_point,
|
||||
);
|
||||
|
||||
self.renderer.free_textures(&textures_delta)
|
||||
self.renderer.free_textures(&textures_delta);
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
@@ -73,6 +73,7 @@ impl InstallMode {
|
||||
Ok(ModeFrameOutput {
|
||||
primitives,
|
||||
can_elide: false,
|
||||
should_continue: true,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+24
-1
@@ -13,7 +13,7 @@ use crate::{
|
||||
window::WindowState,
|
||||
};
|
||||
use clap::Parser;
|
||||
use mlua::{Lua, Result as LuaResult, Table};
|
||||
use mlua::{Function, Lua, Result as LuaResult, Table, ThreadStatus};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
path::PathBuf,
|
||||
@@ -228,6 +228,29 @@ impl LuaInstance {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_running_subscripts(&self) -> bool {
|
||||
self.subscript_manager.borrow().has_running_subscripts()
|
||||
}
|
||||
|
||||
pub fn has_active_coroutine(&self) -> bool {
|
||||
self.get_coroutines().is_ok_and(|coroutines| {
|
||||
coroutines.pairs::<mlua::Thread, bool>().any(|pair| {
|
||||
pair.is_ok_and(|(thread, _)| {
|
||||
matches!(
|
||||
thread.status(),
|
||||
ThreadStatus::Resumable | ThreadStatus::Running
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn get_coroutines(&self) -> LuaResult<Table> {
|
||||
let coroutine_module: Table = self.lua.globals().get("coroutine")?;
|
||||
let list_func: Function = coroutine_module.get("_list")?;
|
||||
list_func.call::<Table>(())
|
||||
}
|
||||
|
||||
pub fn handle_event(&self, event: PoBEvent, pob_ctx: &mut PoBContext) -> LuaResult<()> {
|
||||
profiling::scope!("handle_event", format!("{}", event));
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ pub enum ModeTransition {
|
||||
pub struct ModeFrameOutput {
|
||||
pub primitives: Box<dyn Iterator<Item = ClippedPrimitive>>,
|
||||
pub can_elide: bool,
|
||||
/// Indicates that this should be redrawn again next frame even if user is not interacting with
|
||||
/// window
|
||||
pub should_continue: bool,
|
||||
}
|
||||
|
||||
pub enum AppMode {
|
||||
|
||||
@@ -68,9 +68,14 @@ impl PoBMode {
|
||||
let identical = layers_hash == self.previous_layers_hash;
|
||||
self.previous_layers_hash = layers_hash;
|
||||
|
||||
let has_active_subscript = self.lua_instance.has_running_subscripts();
|
||||
let has_active_coroutine = self.lua_instance.has_active_coroutine();
|
||||
let should_continue = has_active_subscript || has_active_coroutine;
|
||||
|
||||
Ok(ModeFrameOutput {
|
||||
primitives: self.state.layers.consume_layers(),
|
||||
can_elide: identical,
|
||||
should_continue,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,10 @@ impl SubscriptManager {
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
pub fn has_running_subscripts(&self) -> bool {
|
||||
!self.scripts.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
enum SubscriptCall {
|
||||
|
||||
@@ -13,6 +13,8 @@ pub struct WindowState {
|
||||
pub size: PhysicalSize<u32>,
|
||||
pub scale_factor: f32,
|
||||
pending_window_title: std::cell::Cell<Option<String>>,
|
||||
pub is_hovered: bool,
|
||||
pub is_focused: bool,
|
||||
}
|
||||
|
||||
impl Default for WindowState {
|
||||
@@ -23,6 +25,8 @@ impl Default for WindowState {
|
||||
scale_factor: 1.0,
|
||||
pending_window_title: std::cell::Cell::new(None),
|
||||
clipboard: None,
|
||||
is_hovered: true,
|
||||
is_focused: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,4 +77,10 @@ impl WindowState {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_redraw(&self) {
|
||||
if let Some(ref window) = self.window {
|
||||
window.request_redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user