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,4 +1,3 @@
use softbuffer::GraphicsContext;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
@ -41,7 +40,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 buffer = Vec::new();
let mut flag = false;
@ -66,7 +66,7 @@ fn main() {
redraw(&mut buffer, width, height, flag);
// Blit the offscreen buffer to the window's client area
graphics_context.set_buffer(&buffer, width as u16, height as u16);
surface.set_buffer(&buffer, width as u16, height as u16);
}
Event::WindowEvent {