Skip to main content

tilo_presentation/
damage.rs

1//! Optional presentation hints; a backend without them presents whole surfaces.
2
3/// An outward-rounded rectangle in framebuffer pixels.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct DamageRect {
6    pub x: u32,
7    pub y: u32,
8    pub width: u32,
9    pub height: u32,
10}
11
12/// Hint which pixels differ from the last presented buffer. An empty hint
13/// preserves whole-surface presentation; it never permits partial buffer copies.
14#[cfg(all(feature = "gpu-damage", not(target_arch = "wasm32")))]
15pub fn set_next_present_damage(surface: &wgpu::Surface<'_>, damage: &[DamageRect]) -> bool {
16    #[cfg(all(feature = "vulkan-damage", target_os = "linux"))]
17    {
18        let regions: Vec<_> = damage
19            .iter()
20            .map(|rect| wgpu::hal::vulkan::PresentDamageRect {
21                x: rect.x,
22                y: rect.y,
23                width: rect.width,
24                height: rect.height,
25            })
26            .collect();
27        // SAFETY: borrow only this configured surface's HAL guard and use its
28        // checked hint setter. No handle is retained or destroyed, and the guard
29        // is dropped before the caller presents and takes the swapchain locks.
30        if let Some(hal) = unsafe { surface.as_hal::<wgpu::hal::vulkan::Api>() } {
31            hal.set_next_present_damage(&regions);
32            return true;
33        }
34    }
35    let _ = (surface, damage);
36    false
37}