Introduce snapshot concept of canvas (#36119)

Each canvas context returns snapshot instead of just raw bytes. This
allows as to hold off conversions (BGRA <-> RGBA, (un)premultiply) to
when/if they are actually needed. For example when loading snapshot into
webgl we can load both RGBA and BGRA so no conversion is really needed.

Currently whole thing is designed to be able to be extend on
https://github.com/servo/ipc-channel/pull/356, to make less copies.
Hence some commented out code.


Fixes #35759
There are tests for these changes in WPT

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
sagudev 2025-04-23 09:32:47 +02:00 committed by GitHub
parent b6967fc4c8
commit 73b778e67f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 724 additions and 251 deletions

View file

@ -22,6 +22,7 @@ malloc_size_of = { workspace = true }
serde = { workspace = true, features = ["serde_derive"] }
servo_config = { path = "../config" }
webgpu_traits = { workspace = true }
snapshot = { workspace = true }
webrender = { workspace = true }
webrender_api = { workspace = true }
wgpu-core = { workspace = true, features = ["serde", "wgsl"] }

View file

@ -10,9 +10,10 @@ use std::sync::{Arc, Mutex};
use arrayvec::ArrayVec;
use compositing_traits::{WebrenderExternalImageApi, WebrenderImageSource};
use euclid::default::Size2D;
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
use ipc_channel::ipc::IpcSender;
use log::{error, warn};
use serde::{Deserialize, Serialize};
use snapshot::{IpcSnapshot, Snapshot};
use webgpu_traits::{
ContextConfiguration, Error, PRESENTATION_BUFFER_COUNT, WebGPUContextId, WebGPUMsg,
};
@ -364,20 +365,34 @@ impl crate::WGPU {
);
}
pub(crate) fn get_image(&self, context_id: WebGPUContextId) -> IpcSharedMemory {
pub(crate) fn get_image(&self, context_id: WebGPUContextId) -> IpcSnapshot {
let webgpu_contexts = self.wgpu_image_map.lock().unwrap();
let context_data = webgpu_contexts.get(&context_id).unwrap();
let buffer_size = context_data.image_desc.buffer_size();
let size = context_data.image_desc.size().cast().cast_unit();
let data = if let Some(present_buffer) = context_data
.swap_chain
.as_ref()
.and_then(|swap_chain| swap_chain.data.as_ref())
{
IpcSharedMemory::from_bytes(present_buffer.slice())
let format = match context_data.image_desc.0.format {
ImageFormat::RGBA8 => snapshot::PixelFormat::RGBA,
ImageFormat::BGRA8 => snapshot::PixelFormat::BGRA,
_ => unimplemented!(),
};
let alpha_mode = if context_data.image_desc.0.is_opaque() {
snapshot::AlphaMode::AsOpaque {
premultiplied: false,
}
} else {
snapshot::AlphaMode::Transparent {
premultiplied: true,
}
};
Snapshot::from_vec(size, format, alpha_mode, present_buffer.slice().to_vec())
} else {
IpcSharedMemory::from_byte(0, buffer_size as usize)
Snapshot::cleared(size)
};
data
data.as_ipc()
}
pub(crate) fn update_context(