Skip to main content

cranpose_render_wgpu/
present_thread.rs

1#[cfg(feature = "test-support")]
2use std::sync::{atomic::Ordering, mpsc::Receiver};
3
4#[cfg(feature = "test-support")]
5use crate::TestImages;
6use crate::{present_handle::PresentHandle, present_runtime::PresentControl};
7
8/// The thread a [`WgpuRenderer`](crate::WgpuRenderer) hands its frames to,
9/// from [`WgpuRenderer::present_thread`](crate::WgpuRenderer::present_thread).
10/// The thread takes the surface's images, draws the frames into them and
11/// presents them. Every change of the surface counts a new surface epoch, so
12/// a frame built for the surface as it was is refused instead of drawn.
13pub struct PresentThread<'a> {
14    pub(crate) handle: &'a mut PresentHandle,
15    pub(crate) surface_epoch: &'a mut u64,
16}
17
18impl PresentThread<'_> {
19    fn next_surface_epoch(&mut self) -> u64 {
20        *self.surface_epoch = self.surface_epoch.wrapping_add(1);
21        *self.surface_epoch
22    }
23
24    /// Gives the thread a (re)created surface, configured as `config`,
25    /// which first shows the default background. Nothing waits for the
26    /// thread to set it up: frames published afterwards queue behind it.
27    /// False when the thread is gone.
28    pub fn replace_surface(
29        mut self,
30        surface: wgpu::Surface<'static>,
31        config: wgpu::SurfaceConfiguration,
32    ) -> bool {
33        let surface_epoch = self.next_surface_epoch();
34        let sent = self.handle.send_control(PresentControl::ReplaceSurface {
35            surface,
36            config,
37            surface_epoch,
38        });
39        if !sent {
40            log::error!("[present-runtime] replace surface: runtime is gone");
41        }
42        sent
43    }
44
45    /// Configures the thread's surface as `config` and waits until the
46    /// thread has done it, as a surface the platform hands over anew needs.
47    pub fn reconfigure(mut self, config: wgpu::SurfaceConfiguration) -> bool {
48        let surface_epoch = self.next_surface_epoch();
49        self.handle.send_control_and_wait(
50            move |ack| PresentControl::Reconfigure {
51                config,
52                surface_epoch,
53                ack,
54            },
55            "reconfigure surface",
56        )
57    }
58
59    /// Drops the thread's surface and waits until the thread has done it,
60    /// as when the window that showed it is gone: the thread draws nothing
61    /// more into it. The renderer stays for the next surface.
62    pub fn drop_surface(mut self) -> bool {
63        self.next_surface_epoch();
64        self.handle
65            .send_control_and_wait(|ack| PresentControl::DropSurface { ack }, "drop surface")
66    }
67
68    #[cfg(feature = "test-support")]
69    #[doc(hidden)]
70    pub fn attach_offscreen_for_tests(
71        mut self,
72        width: u32,
73        height: u32,
74        images: TestImages,
75    ) -> bool {
76        let surface_epoch = self.next_surface_epoch();
77        self.handle.send_control_and_wait(
78            move |ack| PresentControl::AttachOffscreenTargetForTests {
79                width,
80                height,
81                images,
82                surface_epoch,
83                ack,
84            },
85            "attach offscreen target",
86        )
87    }
88
89    #[cfg(feature = "test-support")]
90    #[doc(hidden)]
91    pub fn send_attach_offscreen_unacked_for_tests(
92        mut self,
93        width: u32,
94        height: u32,
95        images: TestImages,
96    ) -> Option<Receiver<()>> {
97        let surface_epoch = self.next_surface_epoch();
98        self.handle
99            .send_control_unacked(move |ack| PresentControl::AttachOffscreenTargetForTests {
100                width,
101                height,
102                images,
103                surface_epoch,
104                ack,
105            })
106    }
107
108    #[cfg(feature = "test-support")]
109    #[doc(hidden)]
110    pub fn send_reconfigure_unacked_for_tests(
111        self,
112        config: wgpu::SurfaceConfiguration,
113    ) -> Option<Receiver<()>> {
114        let surface_epoch = *self.surface_epoch;
115        self.handle
116            .send_control_unacked(move |ack| PresentControl::Reconfigure {
117                config,
118                surface_epoch,
119                ack,
120            })
121    }
122
123    #[cfg(feature = "test-support")]
124    #[doc(hidden)]
125    pub fn send_drop_surface_unacked_for_tests(self) -> Option<Receiver<()>> {
126        self.handle
127            .send_control_unacked(|ack| PresentControl::DropSurface { ack })
128    }
129
130    #[cfg(feature = "test-support")]
131    #[doc(hidden)]
132    pub fn status_snapshot_for_tests(&self) -> (bool, u64, u64) {
133        let status = self.handle.status();
134        (
135            status.warmup_due(),
136            status.presented_frames.load(Ordering::Relaxed),
137            status.placeholder_frames.load(Ordering::Relaxed),
138        )
139    }
140}