2023-04-21 10:37:48 -07:00
|
|
|
use crate::{error::SwResultExt, util, Rect, SoftBufferError};
|
2022-12-20 08:43:26 -07:00
|
|
|
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
|
2023-04-06 00:30:59 -07:00
|
|
|
use std::{
|
|
|
|
|
cell::RefCell,
|
|
|
|
|
num::{NonZeroI32, NonZeroU32},
|
|
|
|
|
rc::Rc,
|
|
|
|
|
};
|
2022-12-19 16:07:16 -08:00
|
|
|
use wayland_client::{
|
|
|
|
|
backend::{Backend, ObjectId},
|
|
|
|
|
globals::{registry_queue_init, GlobalListContents},
|
2022-12-20 14:24:29 -08:00
|
|
|
protocol::{wl_registry, wl_shm, wl_surface},
|
2022-12-19 16:07:16 -08:00
|
|
|
Connection, Dispatch, EventQueue, Proxy, QueueHandle,
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-20 14:24:29 -08:00
|
|
|
mod buffer;
|
|
|
|
|
use buffer::WaylandBuffer;
|
|
|
|
|
|
2022-12-19 16:07:16 -08:00
|
|
|
struct State;
|
2022-01-19 21:11:20 -06:00
|
|
|
|
2023-01-06 13:08:17 -08:00
|
|
|
pub struct WaylandDisplayImpl {
|
|
|
|
|
conn: Connection,
|
2023-04-06 00:30:59 -07:00
|
|
|
event_queue: RefCell<EventQueue<State>>,
|
2022-12-19 16:07:16 -08:00
|
|
|
qh: QueueHandle<State>,
|
|
|
|
|
shm: wl_shm::WlShm,
|
|
|
|
|
}
|
2022-01-19 21:11:20 -06:00
|
|
|
|
2023-01-06 13:08:17 -08:00
|
|
|
impl WaylandDisplayImpl {
|
|
|
|
|
pub unsafe fn new(display_handle: WaylandDisplayHandle) -> Result<Self, SoftBufferError> {
|
2022-12-23 04:19:41 +01:00
|
|
|
// SAFETY: Ensured by user
|
|
|
|
|
let backend = unsafe { Backend::from_foreign_display(display_handle.display as *mut _) };
|
|
|
|
|
let conn = Connection::from_backend(backend);
|
2023-04-06 11:09:47 -07:00
|
|
|
let (globals, event_queue) =
|
|
|
|
|
registry_queue_init(&conn).swbuf_err("Failed to make round trip to server")?;
|
2022-12-19 16:07:16 -08:00
|
|
|
let qh = event_queue.handle();
|
2023-04-06 11:09:47 -07:00
|
|
|
let shm: wl_shm::WlShm = globals
|
|
|
|
|
.bind(&qh, 1..=1, ())
|
|
|
|
|
.swbuf_err("Failed to instantiate Wayland Shm")?;
|
2023-01-06 13:08:17 -08:00
|
|
|
Ok(Self {
|
|
|
|
|
conn,
|
2023-04-06 00:30:59 -07:00
|
|
|
event_queue: RefCell::new(event_queue),
|
2023-01-06 13:08:17 -08:00
|
|
|
qh,
|
|
|
|
|
shm,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WaylandImpl {
|
2023-04-06 00:30:59 -07:00
|
|
|
display: Rc<WaylandDisplayImpl>,
|
2023-01-06 13:08:17 -08:00
|
|
|
surface: wl_surface::WlSurface,
|
|
|
|
|
buffers: Option<(WaylandBuffer, WaylandBuffer)>,
|
2023-04-06 00:30:59 -07:00
|
|
|
size: Option<(NonZeroI32, NonZeroI32)>,
|
2023-01-06 13:08:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WaylandImpl {
|
|
|
|
|
pub unsafe fn new(
|
|
|
|
|
window_handle: WaylandWindowHandle,
|
2023-04-06 00:30:59 -07:00
|
|
|
display: Rc<WaylandDisplayImpl>,
|
2023-01-06 13:08:17 -08:00
|
|
|
) -> Result<Self, SoftBufferError> {
|
|
|
|
|
// SAFETY: Ensured by user
|
2023-04-06 11:09:47 -07:00
|
|
|
let surface_id = unsafe {
|
|
|
|
|
ObjectId::from_ptr(
|
|
|
|
|
wl_surface::WlSurface::interface(),
|
|
|
|
|
window_handle.surface as _,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
.swbuf_err("Failed to create proxy for surface ID.")?;
|
|
|
|
|
let surface = wl_surface::WlSurface::from_id(&display.conn, surface_id)
|
|
|
|
|
.swbuf_err("Failed to create proxy for surface ID.")?;
|
2022-12-19 16:07:16 -08:00
|
|
|
Ok(Self {
|
2023-01-06 13:08:17 -08:00
|
|
|
display,
|
2022-12-19 16:07:16 -08:00
|
|
|
surface,
|
2022-12-20 15:05:20 -08:00
|
|
|
buffers: Default::default(),
|
2023-04-06 00:30:59 -07:00
|
|
|
size: None,
|
2022-01-19 21:11:20 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 00:30:59 -07:00
|
|
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
|
|
|
|
self.size = Some(
|
|
|
|
|
(|| {
|
|
|
|
|
let width = NonZeroI32::try_from(width).ok()?;
|
|
|
|
|
let height = NonZeroI32::try_from(height).ok()?;
|
|
|
|
|
Some((width, height))
|
|
|
|
|
})()
|
|
|
|
|
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?,
|
|
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn buffer_mut(&mut self) -> Result<BufferImpl, SoftBufferError> {
|
|
|
|
|
let (width, height) = self
|
|
|
|
|
.size
|
|
|
|
|
.expect("Must set size of surface before calling `buffer_mut()`");
|
|
|
|
|
|
|
|
|
|
if let Some((_front, back)) = &mut self.buffers {
|
|
|
|
|
// Block if back buffer not released yet
|
2023-01-06 13:08:17 -08:00
|
|
|
if !back.released() {
|
2023-04-06 00:30:59 -07:00
|
|
|
let mut event_queue = self.display.event_queue.borrow_mut();
|
2023-01-06 13:08:17 -08:00
|
|
|
while !back.released() {
|
2023-04-06 00:30:59 -07:00
|
|
|
event_queue.blocking_dispatch(&mut State).map_err(|err| {
|
|
|
|
|
SoftBufferError::PlatformError(
|
|
|
|
|
Some("Wayland dispatch failure".to_string()),
|
|
|
|
|
Some(Box::new(err)),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2023-01-06 13:08:17 -08:00
|
|
|
}
|
2022-12-20 15:05:20 -08:00
|
|
|
}
|
2023-04-06 00:30:59 -07:00
|
|
|
|
|
|
|
|
// Resize, if buffer isn't large enough
|
|
|
|
|
back.resize(width.get(), height.get());
|
2022-12-20 14:24:29 -08:00
|
|
|
} else {
|
2022-12-27 15:27:36 -08:00
|
|
|
// Allocate front and back buffer
|
2023-04-06 00:30:59 -07:00
|
|
|
self.buffers = Some((
|
|
|
|
|
WaylandBuffer::new(
|
|
|
|
|
&self.display.shm,
|
|
|
|
|
width.get(),
|
|
|
|
|
height.get(),
|
|
|
|
|
&self.display.qh,
|
|
|
|
|
),
|
|
|
|
|
WaylandBuffer::new(
|
|
|
|
|
&self.display.shm,
|
|
|
|
|
width.get(),
|
|
|
|
|
height.get(),
|
|
|
|
|
&self.display.qh,
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(BufferImpl(util::BorrowStack::new(self, |buffer| {
|
|
|
|
|
Ok(unsafe { buffer.buffers.as_mut().unwrap().1.mapped_mut() })
|
|
|
|
|
})?))
|
|
|
|
|
}
|
2023-04-21 10:37:48 -07:00
|
|
|
|
|
|
|
|
fn present_with_damage(&mut self, damage: &[Rect]) -> Result<(), SoftBufferError> {
|
|
|
|
|
let _ = self
|
|
|
|
|
.display
|
|
|
|
|
.event_queue
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.dispatch_pending(&mut State);
|
|
|
|
|
|
|
|
|
|
if let Some((front, back)) = &mut self.buffers {
|
|
|
|
|
// Swap front and back buffer
|
|
|
|
|
std::mem::swap(front, back);
|
|
|
|
|
|
|
|
|
|
front.attach(&self.surface);
|
|
|
|
|
|
|
|
|
|
// Like Mesa's EGL/WSI implementation, we damage the whole buffer with `i32::MAX` if
|
|
|
|
|
// the compositor doesn't support `damage_buffer`.
|
|
|
|
|
// https://bugs.freedesktop.org/show_bug.cgi?id=78190
|
|
|
|
|
if self.surface.version() < 4 {
|
|
|
|
|
self.surface.damage(0, 0, i32::MAX, i32::MAX);
|
|
|
|
|
} else {
|
|
|
|
|
for Rect {
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
} in damage
|
|
|
|
|
{
|
|
|
|
|
// Introduced in version 4, it is an error to use this request in version 3 or lower.
|
|
|
|
|
self.surface.damage_buffer(*x, *y, *width, *height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.surface.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let _ = self.display.event_queue.borrow_mut().flush();
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-04-06 00:30:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct BufferImpl<'a>(util::BorrowStack<'a, WaylandImpl, [u32]>);
|
|
|
|
|
|
|
|
|
|
impl<'a> BufferImpl<'a> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn pixels(&self) -> &[u32] {
|
|
|
|
|
self.0.member()
|
2022-01-19 21:11:20 -06:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 00:30:59 -07:00
|
|
|
#[inline]
|
|
|
|
|
pub fn pixels_mut(&mut self) -> &mut [u32] {
|
|
|
|
|
self.0.member_mut()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 10:37:48 -07:00
|
|
|
pub fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
|
|
|
|
|
self.0.into_container().present_with_damage(damage)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 00:30:59 -07:00
|
|
|
pub fn present(self) -> Result<(), SoftBufferError> {
|
|
|
|
|
let imp = self.0.into_container();
|
|
|
|
|
let (width, height) = imp
|
|
|
|
|
.size
|
|
|
|
|
.expect("Must set size of surface before calling `present()`");
|
2023-04-21 10:37:48 -07:00
|
|
|
imp.present_with_damage(&[Rect {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
width: width.get(),
|
|
|
|
|
height: height.get(),
|
|
|
|
|
}])
|
2022-12-19 16:07:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for State {
|
|
|
|
|
fn event(
|
|
|
|
|
_: &mut State,
|
|
|
|
|
_: &wl_registry::WlRegistry,
|
|
|
|
|
_: wl_registry::Event,
|
|
|
|
|
_: &GlobalListContents,
|
|
|
|
|
_: &Connection,
|
|
|
|
|
_: &QueueHandle<State>,
|
|
|
|
|
) {
|
|
|
|
|
// Ignore globals added after initialization
|
2022-01-19 21:11:20 -06:00
|
|
|
}
|
2022-12-19 16:07:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Dispatch<wl_shm::WlShm, ()> for State {
|
|
|
|
|
fn event(
|
|
|
|
|
_: &mut State,
|
|
|
|
|
_: &wl_shm::WlShm,
|
|
|
|
|
_: wl_shm::Event,
|
|
|
|
|
_: &(),
|
|
|
|
|
_: &Connection,
|
|
|
|
|
_: &QueueHandle<State>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
}
|