use super::*;
use std::marker::PhantomData;
use winit::event_loop::EventLoopWindowTarget;
#[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 core::ffi::c_void {
self.context.get_proc_address(addr)
}
}
#[allow(rustdoc::broken_intra_doc_links)]
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 = "\n
[`build_surfaceless()`]: crate::platform\n
[`build_osmesa()`]: crate::platform
"
)]
#[cfg_attr(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
),
doc = "\n
[`build_surfaceless()`]: platform::unix::HeadlessContextExt::build_surfaceless()\n
[`build_osmesa()`]: platform::unix::HeadlessContextExt::build_osmesa()
"
)]
pub fn build_headless<TE>(
self,
el: &EventLoopWindowTarget<TE>,
size: dpi::PhysicalSize<u32>,
) -> 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> {}