Split GraphicsContext into Context and Surface

A `Context` is created with a display handle, and a `Surface` is created
with a `&Context` and a window handle. Thus multiple windows can be
created from the same context without duplicating anything that can be
shared. This API is broadly similar to `wgpu` or `glutin`.

On Wayland, the `Context` contains the `EventQueue`, which is shared
between windows, and the `WlShm` global. On X11, `Context::new` checks
for the availability of XShm, and contains a bool representing that as
well as the `XCBConnection`. The shared context data is stored within
the window in an `Arc`.

On other platforms, the display isn't used and `Context` is empty. This
does however test that the display handle has the right type on those
platforms and fail otherwise. Previously the code didn't test that.

Closes https://github.com/rust-windowing/softbuffer/issues/37.
This commit is contained in:
Ian Douglas Scott 2023-01-06 13:08:17 -08:00
parent 3b33bbb0f5
commit 129069996e
11 changed files with 298 additions and 158 deletions

View file

@ -1,7 +1,6 @@
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use softbuffer::GraphicsContext;
use std::f64::consts::PI;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
@ -25,7 +24,8 @@ fn main() {
.unwrap();
}
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);
@ -48,7 +48,7 @@ fn main() {
};
let buffer = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
graphics_context.set_buffer(buffer.as_slice(), width as u16, height as u16);
surface.set_buffer(buffer.as_slice(), width as u16, height as u16);
}
Event::MainEventsCleared => {
window.request_redraw();