Skip to content

Commit

Permalink
Add HTMLImageElement/ImageData as external source for copying images (#…
Browse files Browse the repository at this point in the history
…5668)

* Add `HTMLImageElement` as external source for copying images

* Typo
  • Loading branch information
valaphee authored Aug 20, 2024
1 parent 70155aa commit b4db97f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ By @teoxoy in [#5901](https://github.com/gfx-rs/wgpu/pull/5901)
- `MemoryHints::MemoryUsage` favors memory usage over performance. This hint is typically useful for smaller applications or UI libraries.
- `MemoryHints::Manual` allows the user to specify parameters for the underlying GPU memory allocator. These parameters are subject to change.
- These hints may be ignored by some backends. Currently only the Vulkan and D3D12 backends take them into account.
- Add `HTMLImageElement` and `ImageData` as external source for copying images. By @Valaphee in [#5668](https://github.com/gfx-rs/wgpu/pull/5668)

#### Naga

Expand Down
56 changes: 56 additions & 0 deletions wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,21 @@ impl super::Queue {
b,
);
},
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
gl.tex_sub_image_3d_with_html_image_element(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
gl.tex_sub_image_3d_with_html_video_element(
dst_target,
Expand All @@ -486,6 +501,21 @@ impl super::Queue {
v,
);
},
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
gl.tex_sub_image_3d_with_image_data(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
gl.tex_sub_image_3d_with_html_canvas_element(
dst_target,
Expand Down Expand Up @@ -520,6 +550,19 @@ impl super::Queue {
b,
);
},
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
gl.tex_sub_image_2d_with_html_image_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
i,
)
},
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
gl.tex_sub_image_2d_with_html_video_and_width_and_height(
dst_target,
Expand All @@ -533,6 +576,19 @@ impl super::Queue {
v,
)
},
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
gl.tex_sub_image_2d_with_image_data_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(
dst_target,
Expand Down
1 change: 1 addition & 0 deletions wgpu-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde = { workspace = true, features = ["derive"], optional = true }
js-sys.workspace = true
web-sys = { workspace = true, features = [
"ImageBitmap",
"ImageData",
"HtmlVideoElement",
"HtmlCanvasElement",
"OffscreenCanvas",
Expand Down
10 changes: 10 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6843,8 +6843,12 @@ pub struct ImageCopyExternalImage {
pub enum ExternalImageSource {
/// Copy from a previously-decoded image bitmap.
ImageBitmap(web_sys::ImageBitmap),
/// Copy from an image element.
HTMLImageElement(web_sys::HtmlImageElement),
/// Copy from a current frame of a video element.
HTMLVideoElement(web_sys::HtmlVideoElement),
/// Copy from an image.
ImageData(web_sys::ImageData),
/// Copy from a on-screen canvas.
HTMLCanvasElement(web_sys::HtmlCanvasElement),
/// Copy from a off-screen canvas.
Expand All @@ -6859,7 +6863,9 @@ impl ExternalImageSource {
pub fn width(&self) -> u32 {
match self {
ExternalImageSource::ImageBitmap(b) => b.width(),
ExternalImageSource::HTMLImageElement(i) => i.width(),
ExternalImageSource::HTMLVideoElement(v) => v.video_width(),
ExternalImageSource::ImageData(i) => i.width(),
ExternalImageSource::HTMLCanvasElement(c) => c.width(),
ExternalImageSource::OffscreenCanvas(c) => c.width(),
}
Expand All @@ -6869,7 +6875,9 @@ impl ExternalImageSource {
pub fn height(&self) -> u32 {
match self {
ExternalImageSource::ImageBitmap(b) => b.height(),
ExternalImageSource::HTMLImageElement(i) => i.height(),
ExternalImageSource::HTMLVideoElement(v) => v.video_height(),
ExternalImageSource::ImageData(i) => i.height(),
ExternalImageSource::HTMLCanvasElement(c) => c.height(),
ExternalImageSource::OffscreenCanvas(c) => c.height(),
}
Expand All @@ -6883,7 +6891,9 @@ impl std::ops::Deref for ExternalImageSource {
fn deref(&self) -> &Self::Target {
match self {
Self::ImageBitmap(b) => b,
Self::HTMLImageElement(i) => i,
Self::HTMLVideoElement(v) => v,
Self::ImageData(i) => i,
Self::HTMLCanvasElement(c) => c,
Self::OffscreenCanvas(c) => c,
}
Expand Down

0 comments on commit b4db97f

Please sign in to comment.