#[cfg(feature = "test-support")]
use std::sync::{atomic::Ordering, mpsc::Receiver};
#[cfg(feature = "test-support")]
use crate::TestImages;
use crate::{present_handle::PresentHandle, present_runtime::PresentControl};
pub struct PresentThread<'a> {
pub(crate) handle: &'a mut PresentHandle,
pub(crate) surface_epoch: &'a mut u64,
}
impl PresentThread<'_> {
fn next_surface_epoch(&mut self) -> u64 {
*self.surface_epoch = self.surface_epoch.wrapping_add(1);
*self.surface_epoch
}
pub fn replace_surface(
mut self,
surface: wgpu::Surface<'static>,
config: wgpu::SurfaceConfiguration,
) -> bool {
let surface_epoch = self.next_surface_epoch();
let sent = self.handle.send_control(PresentControl::ReplaceSurface {
surface,
config,
surface_epoch,
});
if !sent {
log::error!("[present-runtime] replace surface: runtime is gone");
}
sent
}
pub fn reconfigure(mut self, config: wgpu::SurfaceConfiguration) -> bool {
let surface_epoch = self.next_surface_epoch();
self.handle.send_control_and_wait(
move |ack| PresentControl::Reconfigure {
config,
surface_epoch,
ack,
},
"reconfigure surface",
)
}
pub fn drop_surface(mut self) -> bool {
self.next_surface_epoch();
self.handle
.send_control_and_wait(|ack| PresentControl::DropSurface { ack }, "drop surface")
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn attach_offscreen_for_tests(
mut self,
width: u32,
height: u32,
images: TestImages,
) -> bool {
let surface_epoch = self.next_surface_epoch();
self.handle.send_control_and_wait(
move |ack| PresentControl::AttachOffscreenTargetForTests {
width,
height,
images,
surface_epoch,
ack,
},
"attach offscreen target",
)
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn send_attach_offscreen_unacked_for_tests(
mut self,
width: u32,
height: u32,
images: TestImages,
) -> Option<Receiver<()>> {
let surface_epoch = self.next_surface_epoch();
self.handle
.send_control_unacked(move |ack| PresentControl::AttachOffscreenTargetForTests {
width,
height,
images,
surface_epoch,
ack,
})
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn send_reconfigure_unacked_for_tests(
self,
config: wgpu::SurfaceConfiguration,
) -> Option<Receiver<()>> {
let surface_epoch = *self.surface_epoch;
self.handle
.send_control_unacked(move |ack| PresentControl::Reconfigure {
config,
surface_epoch,
ack,
})
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn send_drop_surface_unacked_for_tests(self) -> Option<Receiver<()>> {
self.handle
.send_control_unacked(|ack| PresentControl::DropSurface { ack })
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn status_snapshot_for_tests(&self) -> (bool, u64, u64) {
let status = self.handle.status();
(
status.warmup_due(),
status.presented_frames.load(Ordering::Relaxed),
status.placeholder_frames.load(Ordering::Relaxed),
)
}
}