1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-28 07:23:13 -04:00

Fill in DisplayHandle automatically on web painter just like it's done on winit (#8006)

* Closes https://github.com/emilk/egui/issues/8001
* Follow-up to https://github.com/emilk/egui/pull/7990

Also simplified/shortened the comments around display handle a bit. Lots
a repetition there made it hard to upgrade otherwise.
This commit is contained in:
Andreas Reich
2026-03-24 12:37:33 +01:00
committed by GitHub
parent b4f9cd7140
commit d985bf9b83
2 changed files with 64 additions and 63 deletions

View File

@@ -25,6 +25,24 @@ pub(crate) struct WebPainterWgpu {
needs_reconfigure: bool,
}
/// Owned web display handle that is `Send + Sync`.
///
/// `DisplayHandle` from `raw-window-handle` is `!Send`/`!Sync` because the enum
/// contains platform variants with raw pointers. On web the handle is always empty,
/// so this wrapper is safe.
#[cfg(target_arch = "wasm32")]
#[derive(Clone, Debug)]
struct WebDisplay;
#[cfg(target_arch = "wasm32")]
impl egui_wgpu::wgpu::rwh::HasDisplayHandle for WebDisplay {
fn display_handle(
&self,
) -> Result<egui_wgpu::wgpu::rwh::DisplayHandle<'_>, egui_wgpu::wgpu::rwh::HandleError> {
Ok(egui_wgpu::wgpu::rwh::DisplayHandle::web())
}
}
impl WebPainterWgpu {
pub fn render_state(&self) -> Option<RenderState> {
self.render_state.clone()
@@ -64,7 +82,17 @@ impl WebPainterWgpu {
) -> Result<Self, String> {
log::debug!("Creating wgpu painter");
let instance = options.wgpu_options.wgpu_setup.new_instance().await;
// Inject the display handle into the wgpu setup so that wgpu can create surfaces on WebGL.
let mut wgpu_options = options.wgpu_options.clone();
if let egui_wgpu::WgpuSetup::CreateNew(ref mut create_new) = wgpu_options.wgpu_setup
&& create_new.display_handle.is_none()
{
// Force WebGL, useful for quick & dirty testing:
//create_new.instance_descriptor.backends = wgpu::Backends::GL;
create_new.display_handle = Some(Box::new(WebDisplay));
}
let instance = wgpu_options.wgpu_setup.new_instance().await;
let surface = instance
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
@@ -72,7 +100,7 @@ impl WebPainterWgpu {
let depth_stencil_format = egui_wgpu::depth_format_from_bits(options.depth_buffer, 0);
let render_state = RenderState::create(
&options.wgpu_options,
&wgpu_options,
&instance,
Some(&surface),
egui_wgpu::RendererOptions {
@@ -90,7 +118,7 @@ impl WebPainterWgpu {
let surface_configuration = wgpu::SurfaceConfiguration {
format: render_state.target_format,
present_mode: options.wgpu_options.present_mode,
present_mode: wgpu_options.present_mode,
view_formats: vec![render_state.target_format],
..default_configuration
};
@@ -106,7 +134,7 @@ impl WebPainterWgpu {
surface_configuration,
depth_stencil_format,
depth_texture_view: None,
on_surface_status: Arc::clone(&options.wgpu_options.on_surface_status) as _,
on_surface_status: Arc::clone(&wgpu_options.on_surface_status) as _,
screen_capture_state: None,
capture_tx,
capture_rx,