Skip to content

Commit b4db97f

Browse files
authored
Add HTMLImageElement/ImageData as external source for copying images (#5668)
* Add `HTMLImageElement` as external source for copying images * Typo
1 parent 70155aa commit b4db97f

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ By @teoxoy in [#5901](https://github.com/gfx-rs/wgpu/pull/5901)
230230
- `MemoryHints::MemoryUsage` favors memory usage over performance. This hint is typically useful for smaller applications or UI libraries.
231231
- `MemoryHints::Manual` allows the user to specify parameters for the underlying GPU memory allocator. These parameters are subject to change.
232232
- These hints may be ignored by some backends. Currently only the Vulkan and D3D12 backends take them into account.
233+
- Add `HTMLImageElement` and `ImageData` as external source for copying images. By @Valaphee in [#5668](https://github.com/gfx-rs/wgpu/pull/5668)
233234

234235
#### Naga
235236

wgpu-hal/src/gles/queue.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,21 @@ impl super::Queue {
471471
b,
472472
);
473473
},
474+
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
475+
gl.tex_sub_image_3d_with_html_image_element(
476+
dst_target,
477+
copy.dst_base.mip_level as i32,
478+
copy.dst_base.origin.x as i32,
479+
copy.dst_base.origin.y as i32,
480+
z_offset as i32,
481+
copy.size.width as i32,
482+
copy.size.height as i32,
483+
copy.size.depth as i32,
484+
format_desc.external,
485+
format_desc.data_type,
486+
i,
487+
);
488+
},
474489
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
475490
gl.tex_sub_image_3d_with_html_video_element(
476491
dst_target,
@@ -486,6 +501,21 @@ impl super::Queue {
486501
v,
487502
);
488503
},
504+
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
505+
gl.tex_sub_image_3d_with_image_data(
506+
dst_target,
507+
copy.dst_base.mip_level as i32,
508+
copy.dst_base.origin.x as i32,
509+
copy.dst_base.origin.y as i32,
510+
z_offset as i32,
511+
copy.size.width as i32,
512+
copy.size.height as i32,
513+
copy.size.depth as i32,
514+
format_desc.external,
515+
format_desc.data_type,
516+
i,
517+
);
518+
},
489519
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
490520
gl.tex_sub_image_3d_with_html_canvas_element(
491521
dst_target,
@@ -520,6 +550,19 @@ impl super::Queue {
520550
b,
521551
);
522552
},
553+
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
554+
gl.tex_sub_image_2d_with_html_image_and_width_and_height(
555+
dst_target,
556+
copy.dst_base.mip_level as i32,
557+
copy.dst_base.origin.x as i32,
558+
copy.dst_base.origin.y as i32,
559+
copy.size.width as i32,
560+
copy.size.height as i32,
561+
format_desc.external,
562+
format_desc.data_type,
563+
i,
564+
)
565+
},
523566
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
524567
gl.tex_sub_image_2d_with_html_video_and_width_and_height(
525568
dst_target,
@@ -533,6 +576,19 @@ impl super::Queue {
533576
v,
534577
)
535578
},
579+
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
580+
gl.tex_sub_image_2d_with_image_data_and_width_and_height(
581+
dst_target,
582+
copy.dst_base.mip_level as i32,
583+
copy.dst_base.origin.x as i32,
584+
copy.dst_base.origin.y as i32,
585+
copy.size.width as i32,
586+
copy.size.height as i32,
587+
format_desc.external,
588+
format_desc.data_type,
589+
i,
590+
);
591+
},
536592
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
537593
gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(
538594
dst_target,

wgpu-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ serde = { workspace = true, features = ["derive"], optional = true }
4242
js-sys.workspace = true
4343
web-sys = { workspace = true, features = [
4444
"ImageBitmap",
45+
"ImageData",
4546
"HtmlVideoElement",
4647
"HtmlCanvasElement",
4748
"OffscreenCanvas",

wgpu-types/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6843,8 +6843,12 @@ pub struct ImageCopyExternalImage {
68436843
pub enum ExternalImageSource {
68446844
/// Copy from a previously-decoded image bitmap.
68456845
ImageBitmap(web_sys::ImageBitmap),
6846+
/// Copy from an image element.
6847+
HTMLImageElement(web_sys::HtmlImageElement),
68466848
/// Copy from a current frame of a video element.
68476849
HTMLVideoElement(web_sys::HtmlVideoElement),
6850+
/// Copy from an image.
6851+
ImageData(web_sys::ImageData),
68486852
/// Copy from a on-screen canvas.
68496853
HTMLCanvasElement(web_sys::HtmlCanvasElement),
68506854
/// Copy from a off-screen canvas.
@@ -6859,7 +6863,9 @@ impl ExternalImageSource {
68596863
pub fn width(&self) -> u32 {
68606864
match self {
68616865
ExternalImageSource::ImageBitmap(b) => b.width(),
6866+
ExternalImageSource::HTMLImageElement(i) => i.width(),
68626867
ExternalImageSource::HTMLVideoElement(v) => v.video_width(),
6868+
ExternalImageSource::ImageData(i) => i.width(),
68636869
ExternalImageSource::HTMLCanvasElement(c) => c.width(),
68646870
ExternalImageSource::OffscreenCanvas(c) => c.width(),
68656871
}
@@ -6869,7 +6875,9 @@ impl ExternalImageSource {
68696875
pub fn height(&self) -> u32 {
68706876
match self {
68716877
ExternalImageSource::ImageBitmap(b) => b.height(),
6878+
ExternalImageSource::HTMLImageElement(i) => i.height(),
68726879
ExternalImageSource::HTMLVideoElement(v) => v.video_height(),
6880+
ExternalImageSource::ImageData(i) => i.height(),
68736881
ExternalImageSource::HTMLCanvasElement(c) => c.height(),
68746882
ExternalImageSource::OffscreenCanvas(c) => c.height(),
68756883
}
@@ -6883,7 +6891,9 @@ impl std::ops::Deref for ExternalImageSource {
68836891
fn deref(&self) -> &Self::Target {
68846892
match self {
68856893
Self::ImageBitmap(b) => b,
6894+
Self::HTMLImageElement(i) => i,
68866895
Self::HTMLVideoElement(v) => v,
6896+
Self::ImageData(i) => i,
68876897
Self::HTMLCanvasElement(c) => c,
68886898
Self::OffscreenCanvas(c) => c,
68896899
}

0 commit comments

Comments
 (0)