Skip to content

Commit

Permalink
WIP: Decouple glutin from winit
Browse files Browse the repository at this point in the history
Thit commit removes direct dependency on the winit and
using raw_window_handle crate instead
  • Loading branch information
kchibisov committed Aug 18, 2022
1 parent 2f8bdfc commit 2ade3a6
Show file tree
Hide file tree
Showing 74 changed files with 5,560 additions and 8,832 deletions.
68 changes: 22 additions & 46 deletions glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,42 @@ name = "glutin"
version = "0.29.1"
authors = ["The glutin contributors", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "Cross-platform OpenGL context provider."
keywords = ["windowing", "opengl"]
keywords = ["windowing", "opengl", "egl", "glx", "wgl", "cgl"]
license = "Apache-2.0"
readme = "../README.md"
repository = "https://github.com/rust-windowing/glutin"
documentation = "https://docs.rs/glutin"
rust-version = "1.57.0"
edition = "2021"
rust-version = "1.57"

[package.metadata.docs.rs]
features = ["serde"]

[features]
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
serde = ["winit/serde"]
x11 = ["winit/x11", "glutin_glx_sys"]
wayland = ["winit/wayland", "wayland-client", "wayland-egl"]
wayland-dlopen = ["winit/wayland-dlopen"]
wayland-csd-adwaita = ["winit/wayland-csd-adwaita"]
wayland-csd-adwaita-notitle = ["winit/wayland-csd-adwaita-notitle"]
default = ["egl", "glx", "x11", "wayland", "wgl"]
egl = ["glutin_egl_sys", "glutin_gles2_sys"]
glx = ["x11", "x11-dl", "glutin_glx_sys"]
wgl = []
x11 = ["x11-dl"]
wayland = ["wayland-sys"]

[dependencies]
libloading = "0.7.3"
once_cell = "1.13"
winit = { version = "0.27.1", default-features = false }
raw-window-handle = "0.5.0"
bitflags = "1.3.2"

[target.'cfg(target_os = "windows")'.dependencies]
glutin_egl_sys = { version = "0.1.6", path = "../glutin_egl_sys", optional = true }
glutin_gles2_sys = { version = "0.1.5", path = "../glutin_gles2_sys", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
glutin_egl_sys = { version = "0.1.6", path = "../glutin_egl_sys" }
libloading = "0.7"
parking_lot = "0.12"
raw-window-handle = "0.5"

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
objc = "0.2.6"
glutin_gles2_sys = { version = "0.1.5", path = "../glutin_gles2_sys" }

[target.'cfg(target_os = "macos")'.dependencies]
cgl = "0.3"
cocoa = "0.24"
core-foundation = "0.9"

[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = [
"winnt",
"winuser",
"wingdi",
"libloaderapi",
]

[target.'cfg(target_os = "windows")'.dependencies]
libloading = "0.7"
glutin_wgl_sys = { version = "0.1.5", path = "../glutin_wgl_sys" }
glutin_egl_sys = { version = "0.1.6", path = "../glutin_egl_sys" }
parking_lot = "0.12"

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
osmesa-sys = "0.1"
wayland-client = { version = "0.29.4", features = ["dlopen"], optional = true }
wayland-egl = { version = "0.29.4", optional = true }
libloading = "0.7"
glutin_egl_sys = { version = "0.1.6", path = "../glutin_egl_sys" }
wayland-sys = { version = "0.30.0-beta.8", default-features = false, features = ["egl", "client"], optional = true }
x11-dl = { version = "2.20.0", optional = true }
glutin_glx_sys = { version = "0.1.8", path = "../glutin_glx_sys", optional = true }
parking_lot = "0.12"
log = "0.4"
glutin_egl_sys = { version = "0.1.6", path = "../glutin_egl_sys", optional = true }
glutin_gles2_sys = { version = "0.1.5", path = "../glutin_gles2_sys", optional = true }

[build-dependencies]
cfg_aliases = "0.1.1"
24 changes: 24 additions & 0 deletions glutin/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cfg_aliases::cfg_aliases;

fn main() {
// Setup alias to reduce `cfg` boilerplate.
cfg_aliases! {
// Systems.
android: { target_os = "android" },
wasm: { target_arch = "wasm32" },
macos: { target_os = "macos" },
ios: { target_os = "ios" },
apple: { any(target_os = "ios", target_os = "macos") },
free_unix: { all(unix, not(apple), not(android)) },

// Native displays.
x11_platform: { all(feature = "x11", free_unix, not(wasm)) },
wayland_platform: { all(feature = "wayland", free_unix, not(wasm)) },

// Backends.
egl_backend: { all(feature = "egl", any(windows, unix), not(apple), not(wasm)) },
glx_backend: { all(feature = "glx", x11_platform, not(wasm)) },
wgl_backend: { all(feature = "wgl", windows, not(wasm)) },
cgl_backend: { all(macos, not(wasm)) },
}
}
89 changes: 89 additions & 0 deletions glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::os::raw::c_int;
use std::sync::Arc;

use crate::config::{
Api, AsRawConfig, ColorBufferType, ConfigSurfaceTypes, ConfigTemplate, GlConfig, RawConfig,
};
use crate::display::GetGlDisplay;
use crate::private::Sealed;

use super::display::Display;

impl Display {
pub(crate) fn find_configs(
&self,
template: ConfigTemplate,
) -> Option<Box<dyn Iterator<Item = Config> + '_>> {
todo!()
}
}

#[derive(Clone)]
pub struct Config {
pub(crate) inner: Arc<ConfigInner>,
}

pub(crate) struct ConfigInner {}

impl Sealed for Config {}

impl GlConfig for Config {
fn color_buffer_type(&self) -> ColorBufferType {
todo!()
}

fn float_pixels(&self) -> bool {
todo!()
}

fn native_visual(&self) -> u32 {
todo!()
}

fn alpha_size(&self) -> u8 {
todo!()
}

fn srgb_capable(&self) -> bool {
todo!()
}

fn depth_size(&self) -> u8 {
todo!()
}

fn stencil_size(&self) -> u8 {
todo!()
}

fn sample_buffers(&self) -> u8 {
todo!()
}

fn config_surface_types(&self) -> ConfigSurfaceTypes {
todo!()
}

fn api(&self) -> Api {
todo!()
}
}

impl GetGlDisplay for Config {
type Target = Display;
fn display(&self) -> Self::Target {
todo!()
}
}

impl Config {
fn raw_attribute(&self, attr: c_int) -> c_int {
todo!()
}
}

impl AsRawConfig for Config {
fn raw_config(&self) -> RawConfig {
todo!()
}
}
183 changes: 183 additions & 0 deletions glutin/src/api/cgl/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use std::cell::Cell;
use std::ffi::{self, CStr};
use std::marker::PhantomData;
use std::rc::Rc;

use crate::context::{AsRawContext, ContextAttributes, RawContext};

use crate::config::GetGlConfig;
use crate::display::GetGlDisplay;
use crate::error::Result;
use crate::prelude::*;
use crate::private::Sealed;
use crate::surface::SurfaceTypeTrait;

use super::config::Config;
use super::display::Display;
use super::surface::Surface;

impl Display {
pub(crate) fn create_context(
&self,
config: &Config,
context_attributes: &ContextAttributes,
) -> Result<NotCurrentContext> {
todo!()
}
}

pub struct PossiblyCurrentContext {
inner: ContextInner,
// The context could be current only on the one thread.
_nosendsync: PhantomData<Rc<()>>,
}

pub struct NotCurrentContext {
inner: ContextInner,
// Only non-current context could be send between threads safely.
_nosync: PhantomData<Cell<()>>,
}

impl Sealed for PossiblyCurrentContext {}
impl Sealed for NotCurrentContext {}

impl NotCurrentContext {
fn new(inner: ContextInner) -> Self {
Self { inner, _nosync: PhantomData }
}
}

impl GetGlDisplay for NotCurrentContext {
type Target = Display;
fn display(&self) -> Self::Target {
self.inner.display.clone()
}
}

impl GetGlDisplay for PossiblyCurrentContext {
type Target = Display;
fn display(&self) -> Self::Target {
self.inner.display.clone()
}
}

impl GetGlConfig for NotCurrentContext {
type Target = Config;
fn config(&self) -> Self::Target {
self.inner.config.clone()
}
}

impl GetGlConfig for PossiblyCurrentContext {
type Target = Config;
fn config(&self) -> Self::Target {
self.inner.config.clone()
}
}

impl<T: SurfaceTypeTrait> PossiblyCurrentContextGlSurfaceAccessor<T> for PossiblyCurrentContext {
type Surface = Surface<T>;

fn make_current(&self, surface: &Self::Surface) -> Result<()> {
self.inner.make_current_draw_read(surface, surface)
}

fn make_current_draw_read(
&self,
surface_draw: &Self::Surface,
surface_read: &Self::Surface,
) -> Result<()> {
self.inner.make_current_draw_read(surface_draw, surface_read)
}
}

impl PossiblyCurrentGlContext for PossiblyCurrentContext {
type NotCurrentContext = NotCurrentContext;

fn make_not_current(self) -> Result<Self::NotCurrentContext> {
self.inner.make_not_current()?;
Ok(NotCurrentContext::new(self.inner))
}

fn update_after_resize(&self) {
self.inner.update_after_resize()
}

fn set_swap_interval(&self, interval: u16) {}

fn is_current(&self) -> bool {
todo!()
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
todo!()
}
}

impl NotCurrentGlContext for NotCurrentContext {
type PossiblyCurrentContext = PossiblyCurrentContext;

fn treat_as_current(self) -> PossiblyCurrentContext {
PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData }
}
}

impl<T: SurfaceTypeTrait> NotCurrentGlContextSurfaceAccessor<T> for NotCurrentContext {
type Surface = Surface<T>;
type PossiblyCurrentContext = PossiblyCurrentContext;

fn make_current(self, surface: &Self::Surface) -> Result<Self::PossiblyCurrentContext> {
self.inner.make_current_draw_read(surface, surface)?;
Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData })
}

fn make_current_draw_read(
self,
surface_draw: &Self::Surface,
surface_read: &Self::Surface,
) -> Result<Self::PossiblyCurrentContext> {
self.inner.make_current_draw_read(surface_draw, surface_read)?;
Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData })
}
}

impl AsRawContext for PossiblyCurrentContext {
fn raw_context(&self) -> RawContext {
todo!()
}
}

impl AsRawContext for NotCurrentContext {
fn raw_context(&self) -> RawContext {
todo!()
}
}

struct ContextInner {
display: Display,
config: Config,
}

impl ContextInner {
fn make_current_draw_read<T: SurfaceTypeTrait>(
&self,
surface_draw: &Surface<T>,
surface_read: &Surface<T>,
) -> Result<()> {
todo!()
}

fn make_not_current(&self) -> Result<()> {
todo!()
}

fn update_after_resize(&self) {
// This line is intentionally left blank.
}
}

impl Drop for ContextInner {
fn drop(&mut self) {
todo!()
}
}
Loading

0 comments on commit 2ade3a6

Please sign in to comment.