Skip to main content

par_term_render/cell_renderer/
surface.rs

1use super::CellRenderer;
2use crate::wgpu_conversions::VsyncModeWgpu;
3
4impl CellRenderer {
5    pub fn reconfigure_surface(&mut self) {
6        self.surface.configure(&self.device, &self.config);
7    }
8
9    /// Get the list of supported present modes for this surface
10    pub fn supported_present_modes(&self) -> &[wgpu::PresentMode] {
11        &self.supported_present_modes
12    }
13
14    /// Check if a vsync mode is supported
15    pub fn is_vsync_mode_supported(&self, mode: par_term_config::VsyncMode) -> bool {
16        self.supported_present_modes
17            .contains(&mode.to_present_mode())
18    }
19
20    /// Update the vsync mode. Returns the actual mode applied (may differ if requested mode unsupported).
21    /// Also returns whether the mode was changed.
22    pub fn update_vsync_mode(
23        &mut self,
24        mode: par_term_config::VsyncMode,
25    ) -> (par_term_config::VsyncMode, bool) {
26        let requested = mode.to_present_mode();
27        let current = self.config.present_mode;
28
29        // Determine the actual mode to use
30        let actual = if self.supported_present_modes.contains(&requested) {
31            requested
32        } else {
33            log::warn!(
34                "Requested present mode {:?} not supported, falling back to Fifo",
35                requested
36            );
37            wgpu::PresentMode::Fifo
38        };
39
40        // Only reconfigure if the mode actually changed
41        if actual != current {
42            self.config.present_mode = actual;
43            self.surface.configure(&self.device, &self.config);
44            log::info!("VSync mode changed to {:?}", actual);
45        }
46
47        // Convert back to VsyncMode for return
48        let actual_vsync = match actual {
49            wgpu::PresentMode::Immediate => par_term_config::VsyncMode::Immediate,
50            wgpu::PresentMode::Mailbox => par_term_config::VsyncMode::Mailbox,
51            wgpu::PresentMode::Fifo | wgpu::PresentMode::FifoRelaxed => {
52                par_term_config::VsyncMode::Fifo
53            }
54            _ => par_term_config::VsyncMode::Fifo,
55        };
56
57        (actual_vsync, actual != current)
58    }
59
60    /// Get the current vsync mode
61    pub fn current_vsync_mode(&self) -> par_term_config::VsyncMode {
62        match self.config.present_mode {
63            wgpu::PresentMode::Immediate => par_term_config::VsyncMode::Immediate,
64            wgpu::PresentMode::Mailbox => par_term_config::VsyncMode::Mailbox,
65            wgpu::PresentMode::Fifo | wgpu::PresentMode::FifoRelaxed => {
66                par_term_config::VsyncMode::Fifo
67            }
68            _ => par_term_config::VsyncMode::Fifo,
69        }
70    }
71}