diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e34d07..585a5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 -[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 diff --git a/src/app.rs b/src/app.rs index 9217ae9..d66254b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, 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 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 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 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, diff --git a/src/gfx.rs b/src/gfx.rs index 45b7832..3ff2e4d 100644 --- a/src/gfx.rs +++ b/src/gfx.rs @@ -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); } { diff --git a/src/installer.rs b/src/installer.rs index 6108f9a..190fd2c 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -73,6 +73,7 @@ impl InstallMode { Ok(ModeFrameOutput { primitives, can_elide: false, + should_continue: true, }) } diff --git a/src/lua.rs b/src/lua.rs index 40cee44..8a6939b 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -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::().any(|pair| { + pair.is_ok_and(|(thread, _)| { + matches!( + thread.status(), + ThreadStatus::Resumable | ThreadStatus::Running + ) + }) + }) + }) + } + + fn get_coroutines(&self) -> LuaResult { + let coroutine_module: Table = self.lua.globals().get("coroutine")?; + let list_func: Function = coroutine_module.get("_list")?; + list_func.call::
(()) + } + pub fn handle_event(&self, event: PoBEvent, pob_ctx: &mut PoBContext) -> LuaResult<()> { profiling::scope!("handle_event", format!("{}", event)); diff --git a/src/mode.rs b/src/mode.rs index 6c4eb68..8a3af4a 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -34,6 +34,9 @@ pub enum ModeTransition { pub struct ModeFrameOutput { pub primitives: Box>, 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 { diff --git a/src/pob.rs b/src/pob.rs index 4a8c2c7..1ed8e1a 100644 --- a/src/pob.rs +++ b/src/pob.rs @@ -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, }) } diff --git a/src/subscript.rs b/src/subscript.rs index c45c0ad..6576479 100644 --- a/src/subscript.rs +++ b/src/subscript.rs @@ -74,6 +74,10 @@ impl SubscriptManager { results } + + pub fn has_running_subscripts(&self) -> bool { + !self.scripts.is_empty() + } } enum SubscriptCall { diff --git a/src/window.rs b/src/window.rs index 682b30f..fb107ef 100644 --- a/src/window.rs +++ b/src/window.rs @@ -13,6 +13,8 @@ pub struct WindowState { pub size: PhysicalSize, pub scale_factor: f32, pending_window_title: std::cell::Cell>, + 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(); + } + } }