Skip to content

Commit

Permalink
Partial EGL support
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Jul 15, 2023
1 parent 7227ed9 commit 68a94f9
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 224 deletions.
16 changes: 12 additions & 4 deletions glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::{fmt, iter};

use objc2::rc::{Id, Shared};
use raw_window_handle::HasDisplayHandle;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};

use crate::config::{
Api, AsRawConfig, ColorBufferType, ConfigSurfaceTypes, ConfigTemplate, GlConfig, RawConfig,
Expand All @@ -24,9 +24,9 @@ use super::appkit::{
use super::display::Display;

impl<D: HasDisplayHandle> Display<D> {
pub(crate) fn find_configs(
pub(crate) fn find_configs<W: HasWindowHandle>(
&self,
template: ConfigTemplate,
template: ConfigTemplate<W>,
) -> Result<Box<dyn Iterator<Item = Config<D>> + '_>> {
let mut attrs = Vec::<NSOpenGLPixelFormatAttribute>::with_capacity(32);

Expand Down Expand Up @@ -127,7 +127,7 @@ impl<D: HasDisplayHandle> Display<D> {
}

/// A wrapper around NSOpenGLPixelFormat.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct Config<D> {
pub(crate) inner: Arc<ConfigInner<D>>,
}
Expand All @@ -138,6 +138,14 @@ impl<D> Clone for Config<D> {
}
}

impl<D> PartialEq for Config<D> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}

impl<D> Eq for Config<D> {}

impl<D: HasDisplayHandle> Config<D> {
fn raw_attribute(&self, attrib: NSOpenGLPixelFormatAttribute) -> i32 {
unsafe {
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 @@ -49,9 +49,9 @@ impl<D: HasDisplayHandle> GlDisplay for Display<D> {
type PixmapSurface = Surface<D, PixmapSurface>;
type WindowSurface<W: HasWindowHandle> = Surface<D, WindowSurface<W>>;

fn find_configs(
fn find_configs<W: HasWindowHandle>(
&self,
template: ConfigTemplate,
template: ConfigTemplate<W>,
) -> Result<Box<dyn Iterator<Item = Self::Config> + '_>> {
Self::find_configs(self, template)
}
Expand Down
84 changes: 61 additions & 23 deletions glutin/src/api/egl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::ops::Deref;
use std::sync::Arc;
use std::{fmt, mem};

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

use glutin_egl_sys::egl;
use glutin_egl_sys::egl::types::{EGLConfig, EGLint};
Expand All @@ -23,11 +25,11 @@ use crate::platform::x11::{X11GlConfigExt, X11VisualInfo};

use super::display::Display;

impl Display {
pub(crate) unsafe fn find_configs(
impl<D: HasDisplayHandle> Display<D> {
pub(crate) fn find_configs<W: HasWindowHandle>(
&self,
template: ConfigTemplate,
) -> Result<Box<dyn Iterator<Item = Config> + '_>> {
template: ConfigTemplate<W>,
) -> Result<Box<dyn Iterator<Item = Config<D>> + '_>> {
let mut config_attributes = Vec::<EGLint>::new();

// Add color buffer type.
Expand Down Expand Up @@ -183,6 +185,8 @@ impl Display {
found_configs.set_len(configs_number as usize);
}

let raw_handle =
template.native_window.map(|w| w.window_handle()?.raw_window_handle()).transpose()?;
let configs = found_configs
.into_iter()
.map(move |raw| {
Expand All @@ -195,7 +199,7 @@ impl Display {
//
// XXX This can't be done by passing visual in the EGL attributes
// when calling `eglChooseConfig` since the visual is ignored.
match template.native_window {
match raw_handle {
Some(RawWindowHandle::Xcb(xcb)) if xcb.visual_id > 0 => {
xcb.visual_id as u32 == config.native_visual()
},
Expand Down Expand Up @@ -223,12 +227,26 @@ impl Display {

/// A simple wrapper around `EGLConfig` that could be used with `EGLContext`
/// and `EGLSurface`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
pub(crate) inner: Arc<ConfigInner>,
#[derive(Debug)]
pub struct Config<D> {
pub(crate) inner: Arc<ConfigInner<D>>,
}

impl<D> Clone for Config<D> {
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}

impl<D> PartialEq for Config<D> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}

impl Config {
impl<D> Eq for Config<D> {}

impl<D: HasDisplayHandle> Config<D> {
/// The native visual identifier.
///
/// The interpretation of this value is platform dependant. Consult
Expand All @@ -254,7 +272,7 @@ impl Config {
}
}

impl GlConfig for Config {
impl<D: HasDisplayHandle> GlConfig for Config<D> {
fn color_buffer_type(&self) -> Option<ColorBufferType> {
unsafe {
match self.raw_attribute(egl::COLOR_BUFFER_TYPE as EGLint) as _ {
Expand Down Expand Up @@ -335,7 +353,17 @@ impl GlConfig for Config {
#[cfg(any(wayland_platform, x11_platform))]
fn supports_transparency(&self) -> Option<bool> {
use raw_window_handle::RawDisplayHandle;
match *self.inner.display.inner._native_display? {
match self
.inner
.display
.inner
._native_display
.as_ref()?
.display_handle()
.ok()?
.raw_display_handle()
.ok()?
{
#[cfg(x11_platform)]
RawDisplayHandle::Xlib(_) | RawDisplayHandle::Xcb(_) => {
self.x11_visual().map(|visual| visual.supports_transparency())
Expand Down Expand Up @@ -366,24 +394,34 @@ impl GlConfig for Config {
}
}

impl GetGlDisplay for Config {
type Target = Display;
impl<D: HasDisplayHandle> GetGlDisplay for Config<D> {
type Target = Display<D>;

fn display(&self) -> Self::Target {
Display { inner: self.inner.display.inner.clone() }
}
}

impl AsRawConfig for Config {
impl<D: HasDisplayHandle> AsRawConfig for Config<D> {
fn raw_config(&self) -> RawConfig {
RawConfig::Egl(*self.inner.raw)
}
}

#[cfg(x11_platform)]
impl X11GlConfigExt for Config {
impl<D: HasDisplayHandle> X11GlConfigExt for Config<D> {
fn x11_visual(&self) -> Option<X11VisualInfo> {
match *self.inner.display.inner._native_display? {
match self
.inner
.display
.inner
._native_display
.as_ref()?
.display_handle()
.ok()?
.raw_display_handle()
.ok()?
{
raw_window_handle::RawDisplayHandle::Xlib(display_handle) => unsafe {
let xid = self.native_visual();
X11VisualInfo::from_xid(display_handle.display as *mut _, xid as _)
Expand All @@ -393,22 +431,22 @@ impl X11GlConfigExt for Config {
}
}

impl Sealed for Config {}
impl<D: HasDisplayHandle> Sealed for Config<D> {}

pub(crate) struct ConfigInner {
display: Display,
pub(crate) struct ConfigInner<D> {
display: Display<D>,
pub(crate) raw: EglConfig,
}

impl PartialEq for ConfigInner {
impl<D> PartialEq for ConfigInner<D> {
fn eq(&self, other: &Self) -> bool {
self.raw == other.raw
}
}

impl Eq for ConfigInner {}
impl<D> Eq for ConfigInner<D> {}

impl fmt::Debug for ConfigInner {
impl<D> fmt::Debug for ConfigInner<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Config")
.field("raw", &self.raw)
Expand Down
Loading

0 comments on commit 68a94f9

Please sign in to comment.