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:
parent
3b33bbb0f5
commit
129069996e
11 changed files with 298 additions and 158 deletions
|
|
@ -3,7 +3,6 @@
|
|||
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
|
||||
mod example {
|
||||
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
|
||||
use softbuffer::GraphicsContext;
|
||||
use x11rb::{
|
||||
connection::Connection,
|
||||
protocol::{
|
||||
|
|
@ -54,13 +53,12 @@ mod example {
|
|||
|
||||
// Create a new softbuffer context.
|
||||
// SAFETY: The display and window handles outlive the context.
|
||||
let mut context = unsafe {
|
||||
GraphicsContext::from_raw(
|
||||
RawWindowHandle::Xcb(window_handle),
|
||||
RawDisplayHandle::Xcb(display_handle),
|
||||
)
|
||||
}
|
||||
.unwrap();
|
||||
let context =
|
||||
unsafe { softbuffer::Context::from_raw(RawDisplayHandle::Xcb(display_handle)) }
|
||||
.unwrap();
|
||||
let mut surface =
|
||||
unsafe { softbuffer::Surface::from_raw(&context, RawWindowHandle::Xcb(window_handle)) }
|
||||
.unwrap();
|
||||
|
||||
// Register an atom for closing the window.
|
||||
let wm_protocols_atom = conn
|
||||
|
|
@ -104,7 +102,7 @@ mod example {
|
|||
.collect::<Vec<_>>();
|
||||
|
||||
// Draw the buffer.
|
||||
context.set_buffer(&source, width, height);
|
||||
surface.set_buffer(&source, width, height);
|
||||
}
|
||||
Event::ConfigureNotify(configure_notify) => {
|
||||
width = configure_notify.width;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue