Skip to content

Commit

Permalink
Fix eminent compile errors
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Dec 17, 2023
1 parent 788772d commit 1e75b95
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 185 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ members = [
"glutin_wgl_sys",
"glutin_gles2_sys",
]

[patch.crates-io]
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" }
33 changes: 23 additions & 10 deletions glutin-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ use glutin::display::{Display, DisplayApiPreference};
use glutin::platform::x11::X11GlConfigExt;
use glutin::prelude::*;

use raw_window_handle::{HasDisplayHandle, WindowHandle};
#[cfg(wgl_backend)]
use raw_window_handle::HasWindowHandle;
use raw_window_handle::{DisplayHandle, HasDisplayHandle, WindowHandle};

use raw_window_handle::{HasRawDisplayHandle, RawWindowHandle};
use winit::error::OsError;
use winit::event_loop::{EventLoopWindowTarget, OwnedDisplayHandle};
use winit::event_loop::EventLoopWindowTarget;
use winit::window::{Window, WindowBuilder};

#[cfg(glx_backend)]
Expand Down Expand Up @@ -87,16 +86,20 @@ impl DisplayBuilder {
/// **WGL:** - [`WindowBuilder`] **must** be passed in
/// [`Self::with_window_builder`] if modern OpenGL(ES) is desired,
/// otherwise only builtin functions like `glClear` will be available.
///
/// # Safety
///
/// The `Config` must not outlive the `EventLoop`.
pub fn build<T, Picker>(
mut self,
window_target: &EventLoopWindowTarget<T>,
template_builder: ConfigTemplateBuilder<WindowHandle<'_>>,
config_picker: Picker,
) -> Result<(Option<Window>, Config<OwnedDisplayHandle>), Box<dyn Error>>
) -> Result<(Option<Window>, Config<DisplayHandle<'static>>), Box<dyn Error>>
where
Picker: FnOnce(
Box<dyn Iterator<Item = Config<OwnedDisplayHandle>> + '_>,
) -> Config<OwnedDisplayHandle>,
Box<dyn Iterator<Item = Config<DisplayHandle<'static>>> + '_>,
) -> Config<DisplayHandle<'static>>,
{
// XXX with WGL backend window should be created first.
#[cfg(wgl_backend)]
Expand All @@ -111,7 +114,9 @@ impl DisplayBuilder {
#[cfg(not(wgl_backend))]
let raw_window_handle = None;

let gl_display = create_display(window_target, self.preference, raw_window_handle)?;
// SAFETY: Will not outlive the event loop.
let gl_display =
unsafe { create_display(window_target, self.preference, raw_window_handle)? };

// XXX the native window must be passed to config picker when WGL is used
// otherwise very limited OpenGL features will be supported.
Expand Down Expand Up @@ -140,11 +145,16 @@ impl DisplayBuilder {
}
}

fn create_display<T>(
/// Create the actual display.
///
/// # Safety
///
/// The `Display` must not outlive the `EventLoop`.
unsafe fn create_display<T>(
window_target: &EventLoopWindowTarget<T>,
_api_preference: ApiPreference,
_raw_window_handle: Option<WindowHandle<'_>>,
) -> Result<Display<OwnedDisplayHandle>, Box<dyn Error>> {
) -> Result<Display<DisplayHandle<'static>>, Box<dyn Error>> {
#[cfg(egl_backend)]
let _preference = DisplayApiPreference::Egl;

Expand Down Expand Up @@ -173,7 +183,10 @@ fn create_display<T>(
ApiPreference::FallbackEgl => DisplayApiPreference::WglThenEgl(_raw_window_handle),
};

Ok(Display::new(window_target.owned_display_handle(), _preference)?)
// SAFETY: Does not outlive the event loop.
let display_handle =
unsafe { DisplayHandle::borrow_raw(window_target.display_handle()?.as_raw()) };
Ok(Display::new(display_handle, _preference)?)
}

/// Finalize [`Window`] creation by applying the options from the [`Config`], be
Expand Down
2 changes: 0 additions & 2 deletions glutin-winit/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::num::NonZeroU32;

use glutin::context::PossiblyCurrentContext;
use glutin::surface::{
GlSurface, ResizeableSurface, Surface, SurfaceAttributes, SurfaceAttributesBuilder,
Expand Down
2 changes: 1 addition & 1 deletion glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use icrate::AppKit::{
NSOpenGLPFATripleBuffer, NSOpenGLPixelFormatAttribute, NSOpenGLProfileVersion3_2Core,
NSOpenGLProfileVersion4_1Core, NSOpenGLProfileVersionLegacy,
};
use objc2::rc::{Id, Shared};
use objc2::rc::Id;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};

use crate::config::{
Expand Down
9 changes: 6 additions & 3 deletions glutin/src/api/cgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ impl<D: HasDisplayHandle> ContextInner<D> {
self.raw.makeCurrentContext();

let view = &surface.ns_view;
let raw = &self.raw;
MainThreadMarker::run_on_main(|mtm| unsafe {
self.raw.setView(Some(view.get(mtm)));
raw.setView(Some(view.get(mtm)));
});

Ok(())
Expand All @@ -249,7 +250,8 @@ impl<D: HasDisplayHandle> ContextInner<D> {
}

pub(crate) fn update(&self) {
MainThreadMarker::run_on_main(|_| self.raw.update());
let raw = &self.raw;
MainThreadMarker::run_on_main(|_| raw.update());
}

pub(crate) fn flush_buffer(&self) -> Result<()> {
Expand All @@ -260,8 +262,9 @@ impl<D: HasDisplayHandle> ContextInner<D> {
}

pub(crate) fn is_view_current(&self, view: &MainThreadBound<Id<NSView>>) -> bool {
let raw = &self.raw;
MainThreadMarker::run_on_main(|mtm| {
self.raw.view(mtm).expect("context to have a current view") == *view.get(mtm)
raw.view(mtm).expect("context to have a current view") == *view.get(mtm)
})
}

Expand Down
4 changes: 2 additions & 2 deletions glutin/src/api/cgl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use core_foundation::base::TCFType;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use core_foundation::string::CFString;
use raw_window_handle::{HasDisplayHandle, HasRawDisplayHandle, HasWindowHandle, RawDisplayHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle};

use crate::config::ConfigTemplate;
use crate::display::{AsRawDisplay, DisplayFeatures, RawDisplay};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl<D> Clone for Display<D> {
impl<D: HasDisplayHandle> Display<D> {
/// Create CGL display.
pub fn new(display: D) -> Result<Self> {
match display.display_handle()?.raw_display_handle()? {
match display.display_handle()?.as_raw() {
RawDisplayHandle::AppKit(..) => Ok(Display { display: Arc::new(display) }),
_ => Err(ErrorKind::NotSupported("provided native display is not supported").into()),
}
Expand Down
25 changes: 14 additions & 11 deletions glutin/src/api/cgl/surface.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Wrapper around `NSView`.
use std::fmt;
use std::marker::PhantomData;
use std::num::NonZeroU32;

use icrate::AppKit::{NSView, NSWindow};
use icrate::Foundation::{MainThreadBound, MainThreadMarker};
use objc2::rc::Id;
use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};

use crate::config::GetGlConfig;
use crate::display::GetGlDisplay;
Expand Down Expand Up @@ -43,7 +44,7 @@ impl<D: HasDisplayHandle> Display<D> {
config: &Config<D>,
surface_attributes: SurfaceAttributes<WindowSurface<W>>,
) -> Result<Surface<D, WindowSurface<W>>> {
let native_window = match surface_attributes.ty.0.window_handle()?.raw_window_handle()? {
let native_window = match surface_attributes.ty.0.window_handle()?.as_raw() {
RawWindowHandle::AppKit(window) => window,
_ => {
return Err(
Expand All @@ -58,21 +59,23 @@ impl<D: HasDisplayHandle> Display<D> {

// SAFETY: Validity of the view and window is ensured by caller
// This function makes sure the window is non null.
let ns_view = if let Some(ns_view) = unsafe { Id::retain(native_window.ns_view.cast()) } {
let ns_view: Id<NSView> = if let Some(ns_view) =
unsafe { Id::retain(native_window.ns_view.as_ptr().cast()) }
{
ns_view
} else {
return Err(ErrorKind::NotSupported("ns_view of provided native window is nil").into());
};
let ns_view = MainThreadBound::new(ns_view, mtm);

let ns_window =
if let Some(ns_window) = unsafe { Id::retain(native_window.ns_window.cast()) } {
ns_window
} else {
let ns_window = match unsafe { ns_view.window() } {
Some(window) => window,
None => {
return Err(
ErrorKind::NotSupported("ns_window of provided native window is nil").into()
);
};
)
},
};

let ns_view = MainThreadBound::new(ns_view, mtm);
let ns_window = MainThreadBound::new(ns_window, mtm);

let surface = Surface {
Expand Down
51 changes: 19 additions & 32 deletions glutin/src/api/egl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::Deref;
use std::sync::Arc;
use std::{fmt, mem};

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

use glutin_egl_sys::egl;
use glutin_egl_sys::egl::types::{EGLConfig, EGLint};
Expand All @@ -28,6 +28,8 @@ impl<D: HasDisplayHandle> Display<D> {
&self,
template: ConfigTemplate<W>,
) -> Result<Box<dyn Iterator<Item = Config<D>> + '_>> {
use raw_window_handle::{XcbWindowHandle, XlibWindowHandle};

let mut config_attributes = Vec::<EGLint>::new();

// Add color buffer type.
Expand Down Expand Up @@ -184,7 +186,7 @@ impl<D: HasDisplayHandle> Display<D> {
}

let raw_handle =
template._native_window.map(|w| w.window_handle()?.raw_window_handle()).transpose()?;
template._native_window.map(|w| w.window_handle().map(|w| w.as_raw())).transpose()?;
let configs = found_configs
.into_iter()
.map(move |raw| {
Expand All @@ -198,11 +200,14 @@ impl<D: HasDisplayHandle> Display<D> {
// XXX This can't be done by passing visual in the EGL attributes
// when calling `eglChooseConfig` since the visual is ignored.
match raw_handle {
Some(RawWindowHandle::Xcb(xcb)) if xcb.visual_id > 0 => {
xcb.visual_id as u32 == config.native_visual()
},
Some(RawWindowHandle::Xlib(xlib)) if xlib.visual_id > 0 => {
xlib.visual_id as u32 == config.native_visual()
Some(RawWindowHandle::Xcb(XcbWindowHandle {
visual_id: Some(visual_id),
..
})) => visual_id.get() == config.native_visual(),
Some(RawWindowHandle::Xlib(XlibWindowHandle { visual_id, .. }))
if visual_id > 0 =>
{
visual_id as u32 == config.native_visual()
},
_ => true,
}
Expand Down Expand Up @@ -350,18 +355,8 @@ impl<D: HasDisplayHandle> GlConfig for Config<D> {

#[cfg(any(wayland_platform, x11_platform))]
fn supports_transparency(&self) -> Option<bool> {
use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
match self
.inner
.display
.inner
._native_display
.as_ref()?
.display_handle()
.ok()?
.raw_display_handle()
.ok()?
{
use raw_window_handle::RawDisplayHandle;
match self.inner.display.inner._native_display.as_ref()?.display_handle().ok()?.as_raw() {
#[cfg(x11_platform)]
RawDisplayHandle::Xlib(_) | RawDisplayHandle::Xcb(_) => {
self.x11_visual().map(|visual| visual.supports_transparency())
Expand Down Expand Up @@ -409,21 +404,13 @@ impl<D: HasDisplayHandle> AsRawConfig for Config<D> {
#[cfg(x11_platform)]
impl<D: HasDisplayHandle> X11GlConfigExt for Config<D> {
fn x11_visual(&self) -> Option<X11VisualInfo> {
use raw_window_handle::HasRawDisplayHandle;
match self
.inner
.display
.inner
._native_display
.as_ref()?
.display_handle()
.ok()?
.raw_display_handle()
.ok()?
{
match self.inner.display.inner._native_display.as_ref()?.display_handle().ok()?.as_raw() {
raw_window_handle::RawDisplayHandle::Xlib(display_handle) => unsafe {
let xid = self.native_visual();
X11VisualInfo::from_xid(display_handle.display as *mut _, xid as _)
X11VisualInfo::from_xid(
display_handle.display.map_or(std::ptr::null_mut(), |d| d.as_ptr() as *mut _),
xid as _,
)
},
_ => None,
}
Expand Down
Loading

0 comments on commit 1e75b95

Please sign in to comment.