Skip to main content

wgpu_types/texture/
external_image.rs

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