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    VideoFrame(web_sys::VideoFrame),
50}
51
52#[cfg(all(target_arch = "wasm32", feature = "web"))]
53impl ExternalImageSource {
54    /// Gets the pixel, not css, width of the source.
55    pub fn width(&self) -> u32 {
56        match self {
57            ExternalImageSource::ImageBitmap(b) => b.width(),
58            ExternalImageSource::HTMLImageElement(i) => i.width(),
59            ExternalImageSource::HTMLVideoElement(v) => v.video_width(),
60            ExternalImageSource::ImageData(i) => i.width(),
61            ExternalImageSource::HTMLCanvasElement(c) => c.width(),
62            ExternalImageSource::OffscreenCanvas(c) => c.width(),
63            ExternalImageSource::VideoFrame(v) => v.display_width(),
64        }
65    }
66
67    /// Gets the pixel, not css, height of the source.
68    pub fn height(&self) -> u32 {
69        match self {
70            ExternalImageSource::ImageBitmap(b) => b.height(),
71            ExternalImageSource::HTMLImageElement(i) => i.height(),
72            ExternalImageSource::HTMLVideoElement(v) => v.video_height(),
73            ExternalImageSource::ImageData(i) => i.height(),
74            ExternalImageSource::HTMLCanvasElement(c) => c.height(),
75            ExternalImageSource::OffscreenCanvas(c) => c.height(),
76            ExternalImageSource::VideoFrame(v) => v.display_height(),
77        }
78    }
79}
80
81#[cfg(all(target_arch = "wasm32", feature = "web"))]
82impl core::ops::Deref for ExternalImageSource {
83    type Target = js_sys::Object;
84
85    fn deref(&self) -> &Self::Target {
86        match self {
87            Self::ImageBitmap(b) => b,
88            Self::HTMLImageElement(i) => i,
89            Self::HTMLVideoElement(v) => v,
90            Self::ImageData(i) => i,
91            Self::HTMLCanvasElement(c) => c,
92            Self::OffscreenCanvas(c) => c,
93            Self::VideoFrame(v) => v,
94        }
95    }
96}
97
98#[cfg(all(
99    target_arch = "wasm32",
100    feature = "web",
101    feature = "fragile-send-sync-non-atomic-wasm",
102    not(target_feature = "atomics")
103))]
104unsafe impl Send for ExternalImageSource {}
105#[cfg(all(
106    target_arch = "wasm32",
107    feature = "web",
108    feature = "fragile-send-sync-non-atomic-wasm",
109    not(target_feature = "atomics")
110))]
111unsafe impl Sync for ExternalImageSource {}
112
113/// Color spaces supported on the web.
114///
115/// Corresponds to [HTML Canvas `PredefinedColorSpace`](
116/// https://html.spec.whatwg.org/multipage/canvas.html#predefinedcolorspace).
117#[derive(Copy, Clone, Debug, PartialEq, Eq)]
118#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
119#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
120pub enum PredefinedColorSpace {
121    /// sRGB color space
122    Srgb,
123    /// Display-P3 color space
124    DisplayP3,
125}
126
127/// View of a texture which can be used to copy to a texture, including
128/// color space and alpha premultiplication information.
129///
130/// Corresponds to [WebGPU `GPUCopyExternalImageDestInfo`](
131/// https://gpuweb.github.io/gpuweb/#dictdef-gpuimagecopytexturetagged).
132#[derive(Copy, Clone, Debug)]
133#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
134pub struct CopyExternalImageDestInfo<T> {
135    /// The texture to be copied to/from.
136    pub texture: T,
137    /// The target mip level of the texture.
138    pub mip_level: u32,
139    /// The base texel of the texture in the selected `mip_level`.
140    pub origin: crate::Origin3d,
141    /// The copy aspect.
142    pub aspect: crate::TextureAspect,
143    /// The color space of this texture.
144    pub color_space: PredefinedColorSpace,
145    /// The premultiplication of this texture
146    pub premultiplied_alpha: bool,
147}
148
149impl<T> CopyExternalImageDestInfo<T> {
150    /// Removes the colorspace information from the type.
151    pub fn to_untagged(self) -> crate::TexelCopyTextureInfo<T> {
152        crate::TexelCopyTextureInfo {
153            texture: self.texture,
154            mip_level: self.mip_level,
155            origin: self.origin,
156            aspect: self.aspect,
157        }
158    }
159}