wgpu/api/
surface_texture.rs

1use crate::*;
2
3/// Surface texture that can be rendered to.
4/// Result of a successful call to [`Surface::get_current_texture`].
5///
6/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
7/// the [`GPUCanvasContext`](https://gpuweb.github.io/gpuweb/#canvas-context) provides
8/// a texture without any additional information.
9#[derive(Debug, Clone)]
10pub struct SurfaceTexture {
11    /// Accessible view of the frame.
12    pub texture: Texture,
13    pub(crate) presented: bool,
14    pub(crate) detail: dispatch::DispatchSurfaceOutputDetail,
15}
16#[cfg(send_sync)]
17static_assertions::assert_impl_all!(SurfaceTexture: Send, Sync);
18
19crate::cmp::impl_eq_ord_hash_proxy!(SurfaceTexture => .texture.inner);
20
21impl SurfaceTexture {
22    #[cfg(custom)]
23    /// Returns custom implementation of SurfaceTexture (if custom backend and is internally T)
24    pub fn as_custom<T: crate::custom::SurfaceOutputDetailInterface>(&self) -> Option<&T> {
25        self.detail.as_custom()
26    }
27}
28
29impl Drop for SurfaceTexture {
30    fn drop(&mut self) {
31        if !self.presented && !thread_panicking() {
32            self.detail.texture_discard();
33        }
34    }
35}
36
37/// Result of a call to [`Surface::get_current_texture`].
38///
39/// See variant documentation for how to handle each case.
40#[derive(Debug)]
41pub enum CurrentSurfaceTexture {
42    /// Successfully acquired a surface texture with no issues.
43    Success(SurfaceTexture),
44    /// Successfully acquired a surface texture, but texture no longer matches the properties of the underlying surface.
45    /// It's highly recommended to call [`Surface::configure`] again for optimal performance.
46    Suboptimal(SurfaceTexture),
47    /// A timeout was encountered while trying to acquire the next frame.
48    ///
49    /// Applications should skip the current frame and try again later.
50    Timeout,
51    /// The window is occluded (e.g. minimized or behind another window).
52    ///
53    /// Applications should skip the current frame and try again once the window
54    /// is no longer occluded.
55    Occluded,
56    /// The underlying surface has changed, and therefore the surface configuration is outdated.
57    ///
58    /// Call [`Surface::configure()`] and try again.
59    Outdated,
60    /// The surface has been lost and needs to be recreated.
61    ///
62    /// If the device as a whole is lost (see [`set_device_lost_callback()`][crate::Device::set_device_lost_callback]), then
63    /// you need to recreate the device and all resources.
64    /// Otherwise, call [`Instance::create_surface()`] to recreate the surface,
65    /// then [`Surface::configure()`], and try again.
66    Lost,
67    /// A validation error inside [`Surface::get_current_texture()`] was raised
68    /// and caught by an [error scope](crate::Device::push_error_scope) or
69    /// [`on_uncaptured_error()`][crate::Device::on_uncaptured_error].
70    ///
71    /// Applications should attend to the validation error and try again.
72    Validation,
73}
74
75fn thread_panicking() -> bool {
76    cfg_if::cfg_if! {
77        if #[cfg(std)] {
78            std::thread::panicking()
79        } else if #[cfg(panic = "abort")] {
80            // If `panic = "abort"` then a thread _cannot_ be observably panicking by definition.
81            false
82        } else {
83            // TODO: This is potentially overly pessimistic; it may be appropriate to instead allow a
84            // texture to not be discarded.
85            // Alternatively, this could _also_ be a `panic!`, since we only care if the thread is panicking
86            // when the surface has not been presented.
87            compile_error!(
88                "cannot determine if a thread is panicking without either `panic = \"abort\"` or `std`"
89            );
90        }
91    }
92}