wgpu_hal/dynamic/
surface.rs

1use alloc::boxed::Box;
2use core::time::Duration;
3
4use crate::{
5    DynDevice, DynFence, DynResource, DynSurfaceTexture, Surface, SurfaceConfiguration,
6    SurfaceError,
7};
8
9use super::DynResourceExt as _;
10
11#[derive(Debug)]
12pub struct DynAcquiredSurfaceTexture {
13    pub texture: Box<dyn DynSurfaceTexture>,
14    /// The presentation configuration no longer matches
15    /// the surface properties exactly, but can still be used to present
16    /// to the surface successfully.
17    pub suboptimal: bool,
18}
19
20pub trait DynSurface: DynResource {
21    unsafe fn configure(
22        &self,
23        device: &dyn DynDevice,
24        config: &SurfaceConfiguration,
25    ) -> Result<(), SurfaceError>;
26
27    unsafe fn unconfigure(&self, device: &dyn DynDevice);
28
29    unsafe fn acquire_texture(
30        &self,
31        timeout: Option<Duration>,
32        fence: &dyn DynFence,
33    ) -> Result<Option<DynAcquiredSurfaceTexture>, SurfaceError>;
34
35    unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>);
36}
37
38impl<S: Surface + DynResource> DynSurface for S {
39    unsafe fn configure(
40        &self,
41        device: &dyn DynDevice,
42        config: &SurfaceConfiguration,
43    ) -> Result<(), SurfaceError> {
44        let device = device.expect_downcast_ref();
45        unsafe { S::configure(self, device, config) }
46    }
47
48    unsafe fn unconfigure(&self, device: &dyn DynDevice) {
49        let device = device.expect_downcast_ref();
50        unsafe { S::unconfigure(self, device) }
51    }
52
53    unsafe fn acquire_texture(
54        &self,
55        timeout: Option<Duration>,
56        fence: &dyn DynFence,
57    ) -> Result<Option<DynAcquiredSurfaceTexture>, SurfaceError> {
58        let fence = fence.expect_downcast_ref();
59        unsafe { S::acquire_texture(self, timeout, fence) }.map(|acquired| {
60            acquired.map(|ast| {
61                let texture = Box::new(ast.texture);
62                let suboptimal = ast.suboptimal;
63                DynAcquiredSurfaceTexture {
64                    texture,
65                    suboptimal,
66                }
67            })
68        })
69    }
70
71    unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>) {
72        unsafe { S::discard_texture(self, texture.unbox()) }
73    }
74}