webgpu: implement get image for webgpu canvas (#35237)

* Implement `get_image_data` for WebGPU canvas

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* Update expectations

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* Add docs

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Samson 2025-01-31 16:24:33 +01:00 committed by GitHub
parent 42b581a6f6
commit 35835af857
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 50 additions and 40 deletions

View file

@ -420,10 +420,7 @@ impl HTMLCanvasElement {
return None;
},
#[cfg(feature = "webgpu")]
Some(&CanvasContext::WebGPU(_)) => {
// TODO: add a method in GPUCanvasContext to get the pixels.
return None;
},
Some(CanvasContext::WebGPU(context)) => Some(context.get_ipc_image()),
Some(CanvasContext::Placeholder(context)) => {
let (sender, receiver) =
ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
@ -450,9 +447,8 @@ impl HTMLCanvasElement {
Some(CanvasContext::WebGL2(ref context)) => {
context.base_context().get_image_data(self.get_size())
},
//TODO: Add method get_image_data to GPUCanvasContext
#[cfg(feature = "webgpu")]
Some(CanvasContext::WebGPU(_)) => None,
Some(CanvasContext::WebGPU(ref context)) => Some(context.get_image_data()),
Some(CanvasContext::Placeholder(_)) | None => {
// Each pixel is fully-transparent black.
Some(vec![0; (self.Width() * self.Height() * 4) as usize])

View file

@ -8,7 +8,7 @@ use std::cell::RefCell;
use arrayvec::ArrayVec;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc;
use ipc_channel::ipc::{self, IpcSharedMemory};
use script_layout_interface::HTMLCanvasDataSource;
use webgpu::swapchain::WebGPUContextId;
use webgpu::wgc::id;
@ -306,6 +306,28 @@ impl GPUCanvasContext {
.replace(Some(self.texture_descriptor_for_canvas(configuration)));
}
}
/// <https://gpuweb.github.io/gpuweb/#ref-for-abstract-opdef-get-a-copy-of-the-image-contents-of-a-context%E2%91%A5>
pub(crate) fn get_ipc_image(&self) -> IpcSharedMemory {
// 1. Return a copy of the image contents of context.
if self.drawing_buffer.borrow().cleared {
IpcSharedMemory::from_byte(0, self.size().area() as usize * 4)
} else {
let (sender, receiver) = ipc::channel().unwrap();
self.channel
.0
.send(WebGPURequest::GetImage {
context_id: self.context_id,
sender,
})
.unwrap();
receiver.recv().unwrap()
}
}
pub(crate) fn get_image_data(&self) -> Vec<u8> {
self.get_ipc_image().to_vec()
}
}
impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, GPUCanvasContext> {