Skip to content

Commit

Permalink
Update for newest changes
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Oct 22, 2023
1 parent b2fbf23 commit 1f44e3a
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 121 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,3 @@ targets = [
"wasm32-unknown-unknown",
]

[patch.crates-io]
raw_window_handle = { package = "raw-window-handle", git = "https://github.com/rust-windowing/raw-window-handle.git", branch = "notgull/next" }
winit = { git = "https://github.com/forkgull/winit.git", branch = "rwh-0.6" }
9 changes: 4 additions & 5 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core_graphics::base::{
use core_graphics::color_space::CGColorSpace;
use core_graphics::data_provider::CGDataProvider;
use core_graphics::image::CGImage;
use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};

use cocoa::appkit::{NSView, NSViewHeightSizable, NSViewWidthSizable, NSWindow};
use cocoa::base::{id, nil};
Expand Down Expand Up @@ -36,14 +36,13 @@ pub struct CGImpl<D, W> {

impl<D: HasDisplayHandle, W: HasWindowHandle> CGImpl<D, W> {
pub(crate) fn new(window_src: W) -> Result<Self, InitError<W>> {
let raw = window_src.window_handle()?.raw_window_handle()?;
let raw = window_src.window_handle()?.as_raw();
let handle = match raw {
RawWindowHandle::AppKit(handle) => handle,
_ => return Err(InitError::Unsupported(window_src)),
};
let window = handle.ns_window as id;
let window: id = unsafe { msg_send![window, retain] };
let view = handle.ns_view as id;
let view = handle.ns_view.as_ptr() as id;
let window = unsafe { msg_send![view, window] };
let layer = CALayer::new();
unsafe {
let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view));
Expand Down
87 changes: 47 additions & 40 deletions src/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,59 @@ use drm::control::{
};
use drm::Device;

use raw_window_handle::{DrmDisplayHandle, DrmWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle};

use std::collections::HashSet;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, BorrowedFd};
use std::rc::Rc;

use crate::error::{SoftBufferError, SwResultExt};
use crate::error::{InitError, SoftBufferError, SwResultExt};

#[derive(Debug)]
pub(crate) struct KmsDisplayImpl {
pub(crate) struct KmsDisplayImpl<D: ?Sized> {
/// The underlying raw device file descriptor.
///
/// Once rwh v0.6 support is merged, this an be made safe. Until then,
/// we use this hacky workaround, since this FD's validity is guaranteed by
/// the unsafe constructor.
fd: BorrowedFd<'static>,

/// Holds a reference to the display.
_display: D,
}

impl AsFd for KmsDisplayImpl {
impl<D: ?Sized> AsFd for KmsDisplayImpl<D> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd
}
}

impl Device for KmsDisplayImpl {}
impl CtrlDevice for KmsDisplayImpl {}
impl<D: ?Sized> Device for KmsDisplayImpl<D> {}
impl<D: ?Sized> CtrlDevice for KmsDisplayImpl<D> {}

impl KmsDisplayImpl {
/// SAFETY: The underlying fd must not outlive the display.
pub(crate) unsafe fn new(handle: DrmDisplayHandle) -> Result<KmsDisplayImpl, SoftBufferError> {
let fd = handle.fd;
impl<D: HasDisplayHandle> KmsDisplayImpl<D> {
pub(crate) fn new(display: D) -> Result<Self, InitError<D>> {
let fd = match display.display_handle()?.as_raw() {
RawDisplayHandle::Drm(drm) => drm.fd,
_ => return Err(InitError::Unsupported(display)),
};
if fd == -1 {
return Err(SoftBufferError::IncompleteDisplayHandle);
return Err(SoftBufferError::IncompleteDisplayHandle.into());
}

// SAFETY: Invariants guaranteed by the user.
let fd = unsafe { BorrowedFd::borrow_raw(fd) };

Ok(KmsDisplayImpl { fd })
Ok(KmsDisplayImpl {
fd,
_display: display,
})
}
}

/// All the necessary types for the Drm/Kms backend.
#[derive(Debug)]
pub(crate) struct KmsImpl {
pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
/// The display implementation.
display: Rc<KmsDisplayImpl>,
display: Rc<KmsDisplayImpl<D>>,

/// The connectors to use.
connectors: Vec<connector::Handle>,
Expand All @@ -66,6 +71,9 @@ pub(crate) struct KmsImpl {

/// The dumb buffer we're using as a buffer.
buffer: Option<Buffers>,

/// Window handle that we are keeping around.
_window: W,
}

#[derive(Debug)]
Expand All @@ -81,7 +89,7 @@ struct Buffers {
}

/// The buffer implementation.
pub(crate) struct BufferImpl<'a> {
pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
/// The mapping of the dump buffer.
mapping: DumbMapping<'a>,

Expand All @@ -101,13 +109,16 @@ pub(crate) struct BufferImpl<'a> {
size: (NonZeroU32, NonZeroU32),

/// The display implementation.
display: &'a KmsDisplayImpl,
display: &'a KmsDisplayImpl<D>,

/// Age of the front buffer.
front_age: &'a mut u8,

/// Age of the back buffer.
back_age: &'a mut u8,

/// Window reference.
_window: PhantomData<&'a mut W>,
}

/// The combined frame buffer and dumb buffer.
Expand All @@ -123,22 +134,16 @@ struct SharedBuffer {
age: u8,
}

impl KmsImpl {
impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
/// Create a new KMS backend.
///
/// # Safety
///
/// The plane must be valid for the lifetime of the backend.
pub(crate) unsafe fn new(
window_handle: DrmWindowHandle,
display: Rc<KmsDisplayImpl>,
) -> Result<Self, SoftBufferError> {
log::trace!("new: window_handle={:X}", window_handle.plane);

pub(crate) fn new(window: W, display: Rc<KmsDisplayImpl<D>>) -> Result<Self, InitError<W>> {
// Make sure that the window handle is valid.
let plane_handle = match NonZeroU32::new(window_handle.plane) {
Some(handle) => plane::Handle::from(handle),
None => return Err(SoftBufferError::IncompleteWindowHandle),
let plane_handle = match window.window_handle()?.as_raw() {
RawWindowHandle::Drm(drm) => match NonZeroU32::new(drm.plane) {
Some(handle) => plane::Handle::from(handle),
None => return Err(SoftBufferError::IncompleteWindowHandle.into()),
},
_ => return Err(InitError::Unsupported(window)),
};

let plane_info = display
Expand Down Expand Up @@ -195,6 +200,7 @@ impl KmsImpl {
connectors,
display,
buffer: None,
_window: window,
})
}

Expand Down Expand Up @@ -232,7 +238,7 @@ impl KmsImpl {
}

/// Get a mutable reference to the buffer.
pub(crate) fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
pub(crate) fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
// Map the dumb buffer.
let set = self
.buffer
Expand Down Expand Up @@ -267,11 +273,12 @@ impl KmsImpl {
zeroes: &set.zeroes,
front_age,
back_age,
_window: PhantomData,
})
}
}

impl Drop for KmsImpl {
impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
fn drop(&mut self) {
// Map the CRTC to the information that was there before.
self.display
Expand All @@ -286,7 +293,7 @@ impl Drop for KmsImpl {
}
}

impl BufferImpl<'_> {
impl<D: ?Sized, W: ?Sized> BufferImpl<'_, D, W> {
#[inline]
pub fn pixels(&self) -> &[u32] {
// drm-rs doesn't let us have the immutable reference... so just use a bunch of zeroes.
Expand All @@ -310,7 +317,7 @@ impl BufferImpl<'_> {
.iter()
.map(|&rect| {
let err = || SoftBufferError::DamageOutOfRange { rect };
Ok(ClipRect::new(
Ok::<_, SoftBufferError>(ClipRect::new(
rect.x.try_into().map_err(|_| err())?,
rect.y.try_into().map_err(|_| err())?,
rect.x
Expand Down Expand Up @@ -375,8 +382,8 @@ impl BufferImpl<'_> {

impl SharedBuffer {
/// Create a new buffer set.
pub(crate) fn new(
display: &KmsDisplayImpl,
pub(crate) fn new<D: ?Sized>(
display: &KmsDisplayImpl<D>,
width: NonZeroU32,
height: NonZeroU32,
) -> Result<Self, SoftBufferError> {
Expand Down
24 changes: 10 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ use std::rc::Rc;
use error::InitError;
pub use error::SoftBufferError;

use raw_window_handle::{
HasDisplayHandle, HasRawDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawDisplayHandle,
RawWindowHandle,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle};

#[cfg(target_arch = "wasm32")]
pub use self::web::SurfaceExtWeb;
Expand Down Expand Up @@ -184,7 +181,7 @@ make_dispatch! {
#[cfg(wayland_platform)]
Wayland(Rc<wayland::WaylandDisplayImpl<D>>, wayland::WaylandImpl<D, W>, wayland::BufferImpl<'a, D, W>),
#[cfg(kms_platform)]
Kms(Rc<kms::KmsDisplayImpl>, kms::KmsImpl, kms::BufferImpl<'a>),
Kms(Rc<kms::KmsDisplayImpl<D>>, kms::KmsImpl<D, W>, kms::BufferImpl<'a, D, W>),
#[cfg(target_os = "windows")]
Win32(D, win32::Win32Impl<D, W>, win32::BufferImpl<'a, D, W>),
#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -213,18 +210,13 @@ impl<D: HasDisplayHandle> Context<D> {
}
}};
}
}

/// Creates a new instance of this struct, using the provided display handles
///
/// # Safety
///
/// - Ensure that the provided handle is valid for the lifetime of the Context
pub unsafe fn from_raw(raw_display_handle: RawDisplayHandle) -> Result<Self, SoftBufferError> {
#[cfg(x11_platform)]
try_init!(X11, display => x11::X11DisplayImpl::new(display).map(Rc::new));
#[cfg(wayland_platform)]
try_init!(Wayland, display => wayland::WaylandDisplayImpl::new(display).map(Rc::new));
#[cfg(kms_platform)]
try_init!(Kms, display => kms::KmsDisplayImpl::new(display).map(Rc::new));
#[cfg(target_os = "windows")]
try_init!(Win32, display => Ok(display));
#[cfg(target_os = "macos")]
Expand All @@ -234,7 +226,7 @@ impl<D: HasDisplayHandle> Context<D> {
#[cfg(target_os = "redox")]
try_init!(Orbital, display => Ok(display));

let raw = dpy.display_handle()?.raw_display_handle()?;
let raw = dpy.display_handle()?.as_raw();
Err(SoftBufferError::UnsupportedDisplayPlatform {
human_readable_display_platform_name: display_handle_type_name(&raw),
display_handle: raw,
Expand Down Expand Up @@ -270,7 +262,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Surface<D, W> {
match ($e) {
Ok(x) => x,
Err(InitError::Unsupported(window)) => {
let raw = window.window_handle()?.raw_window_handle()?;
let raw = window.window_handle()?.as_raw();
return Err(SoftBufferError::UnsupportedWindowPlatform {
human_readable_window_platform_name: window_handle_type_name(&raw),
human_readable_display_platform_name: context
Expand All @@ -293,6 +285,10 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Surface<D, W> {
ContextDispatch::Wayland(wayland_display_impl) => SurfaceDispatch::Wayland(leap!(
wayland::WaylandImpl::new(window, wayland_display_impl.clone())
)),
#[cfg(kms_platform)]
ContextDispatch::Kms(kms_display_impl) => {
SurfaceDispatch::Kms(leap!(kms::KmsImpl::new(window, kms_display_impl.clone())))
}
#[cfg(target_os = "windows")]
ContextDispatch::Win32(_) => {
SurfaceDispatch::Win32(leap!(win32::Win32Impl::new(window)))
Expand Down
8 changes: 3 additions & 5 deletions src/orbital.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::error::InitError;
use raw_window_handle::{
HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, OrbitalWindowHandle, RawWindowHandle,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, OrbitalWindowHandle, RawWindowHandle};
use std::{cmp, marker::PhantomData, num::NonZeroU32, slice, str};

use crate::{Rect, SoftBufferError};
Expand Down Expand Up @@ -67,7 +65,7 @@ pub struct OrbitalImpl<D, W> {

impl<D: HasDisplayHandle, W: HasWindowHandle> OrbitalImpl<D, W> {
pub(crate) fn new(window: W) -> Result<Self, InitError<W>> {
let raw = window.window_handle()?.raw_window_handle()?;
let raw = window.window_handle()?.as_raw();
let handle = match raw {
RawWindowHandle::Orbital(handle) => handle,
_ => return Err(InitError::Unsupported(window)),
Expand Down Expand Up @@ -95,7 +93,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> OrbitalImpl<D, W> {
}

fn window_fd(&self) -> usize {
self.handle.window as usize
self.handle.window.as_ptr() as usize
}

// Read the current width and size
Expand Down
16 changes: 8 additions & 8 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::{
error::{InitError, SwResultExt},
util, Rect, SoftBufferError,
};
use raw_window_handle::{
HasDisplayHandle, HasRawDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawDisplayHandle,
RawWindowHandle,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle};
use std::{
cell::RefCell,
num::{NonZeroI32, NonZeroU32},
Expand Down Expand Up @@ -41,13 +38,13 @@ impl<D: HasDisplayHandle + ?Sized> WaylandDisplayImpl<D> {
where
D: Sized,
{
let raw = display.display_handle()?.raw_display_handle()?;
let raw = display.display_handle()?.as_raw();
let wayland_handle = match raw {
RawDisplayHandle::Wayland(w) => w.display,
_ => return Err(InitError::Unsupported(display)),
};

let backend = unsafe { Backend::from_foreign_display(wayland_handle.cast()) };
let backend = unsafe { Backend::from_foreign_display(wayland_handle.as_ptr().cast()) };
let conn = Connection::from_backend(backend);
let (globals, event_queue) =
registry_queue_init(&conn).swbuf_err("Failed to make round trip to server")?;
Expand Down Expand Up @@ -92,14 +89,17 @@ pub struct WaylandImpl<D: ?Sized, W: ?Sized> {
impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
pub(crate) fn new(window: W, display: Rc<WaylandDisplayImpl<D>>) -> Result<Self, InitError<W>> {
// Get the raw Wayland window.
let raw = window.window_handle()?.raw_window_handle()?;
let raw = window.window_handle()?.as_raw();
let wayland_handle = match raw {
RawWindowHandle::Wayland(w) => w.surface,
_ => return Err(InitError::Unsupported(window)),
};

let surface_id = unsafe {
ObjectId::from_ptr(wl_surface::WlSurface::interface(), wayland_handle.cast())
ObjectId::from_ptr(
wl_surface::WlSurface::interface(),
wayland_handle.as_ptr().cast(),
)
}
.swbuf_err("Failed to create proxy for surface ID.")?;
let surface = wl_surface::WlSurface::from_id(display.conn(), surface_id)
Expand Down
Loading

0 comments on commit 1f44e3a

Please sign in to comment.