use super::*;
use std::marker::PhantomData;
use winit::event_loop::EventLoop;
#[derive(Debug)]
pub struct Context<T: ContextCurrentState> {
pub(crate) context: platform_impl::Context,
pub(crate) phantom: PhantomData<T>,
}
impl<T: ContextCurrentState> Context<T> {
pub unsafe fn make_current(
self,
) -> Result<Context<PossiblyCurrent>, (Self, ContextError)> {
match self.context.make_current() {
Ok(()) => Ok(Context {
context: self.context,
phantom: PhantomData,
}),
Err(err) => Err((
Context {
context: self.context,
phantom: PhantomData,
},
err,
)),
}
}
pub unsafe fn make_not_current(
self,
) -> Result<Context<NotCurrent>, (Self, ContextError)> {
match self.context.make_not_current() {
Ok(()) => Ok(Context {
context: self.context,
phantom: PhantomData,
}),
Err(err) => Err((
Context {
context: self.context,
phantom: PhantomData,
},
err,
)),
}
}
pub unsafe fn treat_as_not_current(self) -> Context<NotCurrent> {
Context {
context: self.context,
phantom: PhantomData,
}
}
pub unsafe fn treat_as_current(self) -> Context<PossiblyCurrent> {
Context {
context: self.context,
phantom: PhantomData,
}
}
pub fn is_current(&self) -> bool {
self.context.is_current()
}
pub fn get_api(&self) -> Api {
self.context.get_api()
}
}
impl Context<PossiblyCurrent> {
pub fn get_proc_address(&self, addr: &str) -> *const () {
self.context.get_proc_address(addr)
}
}
impl<'a, T: ContextCurrentState> ContextBuilder<'a, T> {
#[cfg_attr(
not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
)),
doc = "\
[`build_surfaceless`]: os/index.html
[`build_osmesa`]: os/index.html
"
)]
#[cfg_attr(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
),
doc = "\
[`build_surfaceless`]: os/unix/trait.HeadlessContextExt.html#tymethod.build_surfaceless
[`build_osmesa`]: os/unix/trait.HeadlessContextExt.html#tymethod.build_osmesa
"
)]
pub fn build_headless<TE>(
self,
el: &EventLoop<TE>,
size: dpi::PhysicalSize,
) -> Result<Context<NotCurrent>, CreationError> {
let ContextBuilder { pf_reqs, gl_attr } = self;
let gl_attr = gl_attr.map_sharing(|ctx| &ctx.context);
platform_impl::Context::new_headless(el, &pf_reqs, &gl_attr, size).map(
|context| Context {
context,
phantom: PhantomData,
},
)
}
}
#[derive(Debug, Clone, Copy)]
pub struct PossiblyCurrent {
phantom: PhantomData<*mut ()>,
}
#[derive(Debug, Clone, Copy)]
pub enum NotCurrent {}
pub trait ContextCurrentState: std::fmt::Debug + Clone {}
impl ContextCurrentState for PossiblyCurrent {}
impl ContextCurrentState for NotCurrent {}
trait FailToCompileIfNotSendSync
where
Self: Send + Sync,
{
}
impl FailToCompileIfNotSendSync for Context<NotCurrent> {}