wgpu_types/texture/
external_image.rs

1#[allow(unused_imports, reason = "conditionally used, including in docs")]
2use crate::{DownlevelFlags, Origin2d};
3
4/// View of an external texture that can be used to copy to a texture.
5///
6/// Corresponds to [WebGPU `GPUCopyExternalImageSourceInfo`](
7/// https://gpuweb.github.io/gpuweb/#dictdef-gpuimagecopyexternalimage).
8#[cfg(all(target_arch = "wasm32", feature = "web"))]
9#[derive(Clone, Debug)]
10pub struct CopyExternalImageSourceInfo {
11    /// The texture to be copied from. The copy source data is captured at the moment
12    /// the copy is issued.
13    pub source: ExternalImageSource,
14    /// The base texel used for copying from the external image. Together
15    /// with the `copy_size` argument to copy functions, defines the
16    /// sub-region of the image to copy.
17    ///
18    /// Relative to the top left of the image.
19    ///
20    /// Must be [`Origin2d::ZERO`] if [`DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES`] is not supported.
21    pub origin: Origin2d,
22    /// If the Y coordinate of the image should be flipped. Even if this is
23    /// true, `origin` is still relative to the top left.
24    pub flip_y: bool,
25}
26
27/// Source of an external texture copy.
28///
29/// Corresponds to the [implicit union type on WebGPU `GPUCopyExternalImageSourceInfo.source`](
30/// https://gpuweb.github.io/gpuweb/#dom-gpuimagecopyexternalimage-source).
31#[cfg(all(target_arch = "wasm32", feature = "web"))]
32#[derive(Clone, Debug)]
33pub enum ExternalImageSource {
34    /// Copy from a previously-decoded image bitmap.
35    ImageBitmap(web_sys::ImageBitmap),
36    /// Copy from an image element.
37    HTMLImageElement(web_sys::HtmlImageElement),
38    /// Copy from a current frame of a video element.
39    HTMLVideoElement(web_sys::HtmlVideoElement),
40    /// Copy from an image.
41    ImageData(web_sys::ImageData),
42    /// Copy from a on-screen canvas.
43    HTMLCanvasElement(web_sys::HtmlCanvasElement),
44    /// Copy from a off-screen canvas.
45    ///
46    /// Requires [`DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES`]
47    OffscreenCanvas(web_sys::OffscreenCanvas),
48    /// Copy from a video frame.
49    #[cfg(web_sys_unstable_apis)]
50    VideoFrame(web_sys::VideoFrame),
51}
52
53#[cfg(all(target_arch = "wasm32", feature = "web"))]
54impl ExternalImageSource {
55    /// Gets the pixel, not css, width of the source.
56    pub fn width(&self) -> u32 {
57        match self {
58            ExternalImageSource::ImageBitmap(b) => b.width(),
59            ExternalImageSource::HTMLImageElement(i) => i.width(),
60            ExternalImageSource::HTMLVideoElement(v) => v.video_width(),
61            ExternalImageSource::ImageData(i) => i.width(),
62            ExternalImageSource::HTMLCanvasElement(c) => c.width(),
63            ExternalImageSource::OffscreenCanvas(c) => c.width(),
64            #[cfg(web_sys_unstable_apis)]
65            ExternalImageSource::VideoFrame(v) => v.display_width(),
66        }
67    }
68
69    /// Gets the pixel, not css, height of the source.
70    pub fn height(&self) -> u32 {
71        match self {
72            ExternalImageSource::ImageBitmap(b) => b.height(),
73            ExternalImageSource::HTMLImageElement(i) => i.height(),
74            ExternalImageSource::HTMLVideoElement(v) => v.video_height(),
75            ExternalImageSource::ImageData(i) => i.height(),
76            ExternalImageSource::HTMLCanvasElement(c) => c.height(),
77            ExternalImageSource::OffscreenCanvas(c) => c.height(),
78            #[cfg(web_sys_unstable_apis)]
79            ExternalImageSource::VideoFrame(v) => v.display_height(),
80        }
81    }
82}
83
84#[cfg(all(target_arch = "wasm32", feature = "web"))]
85impl core::ops::Deref for ExternalImageSource {
86    type Target = js_sys::Object;
87
88    fn deref(&self) -> &Self::Target {
89        match self {
90            Self::ImageBitmap(b) => b,
91            Self::HTMLImageElement(i) => i,
92            Self::HTMLVideoElement(v) => v,
93            Self::ImageData(i) => i,
94            Self::HTMLCanvasElement(c) => c,
95            Self::OffscreenCanvas(c) => c,
96            #[cfg(web_sys_unstable_apis)]
97            Self::VideoFrame(v) => v,
98        }
99    }
100}
101
102#[cfg(all(
103    target_arch = "wasm32",
104    feature = "web",
105    feature = "fragile-send-sync-non-atomic-wasm",
106    not(target_feature = "atomics")
107))]
108unsafe impl Send for ExternalImageSource {}
109#[cfg(all(
110    target_arch = "wasm32",
111    feature = "web",
112    feature = "fragile-send-sync-non-atomic-wasm",
113    not(target_feature = "atomics")
114))]
115unsafe impl Sync for ExternalImageSource {}
116
117/// Color spaces supported on the web.
118///
119/// Corresponds to [HTML Canvas `PredefinedColorSpace`](
120/// https://html.spec.whatwg.org/multipage/canvas.html#predefinedcolorspace).
121#[derive(Copy, Clone, Debug, PartialEq, Eq)]
122#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
123#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
124pub enum PredefinedColorSpace {
125    /// sRGB color space
126    Srgb,
127    /// Display-P3 color space
128    DisplayP3,
129}
130
131/// View of a texture which can be used to copy to a texture, including
132/// color space and alpha premultiplication information.
133///
134/// Corresponds to [WebGPU `GPUCopyExternalImageDestInfo`](
135/// https://gpuweb.github.io/gpuweb/#dictdef-gpuimagecopytexturetagged).
136#[derive(Copy, Clone, Debug)]
137#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
138pub struct CopyExternalImageDestInfo<T> {
139    /// The texture to be copied to/from.
140    pub texture: T,
141    /// The target mip level of the texture.
142    pub mip_level: u32,
143    /// The base texel of the texture in the selected `mip_level`.
144    pub origin: crate::Origin3d,
145    /// The copy aspect.
146    pub aspect: crate::TextureAspect,
147    /// The color space of this texture.
148    pub color_space: PredefinedColorSpace,
149    /// The premultiplication of this texture
150    pub premultiplied_alpha: bool,
151}
152
153impl<T> CopyExternalImageDestInfo<T> {
154    /// Removes the colorspace information from the type.
155    pub fn to_untagged(self) -> crate::TexelCopyTextureInfo<T> {
156        crate::TexelCopyTextureInfo {
157            texture: self.texture,
158            mip_level: self.mip_level,
159            origin: self.origin,
160            aspect: self.aspect,
161        }
162    }
163}