2015-02-16 19:29:37 +11:00
|
|
|
use Api;
|
|
|
|
use BuilderAttribs;
|
2015-06-16 18:15:31 +10:00
|
|
|
use ContextError;
|
2015-02-16 19:29:37 +11:00
|
|
|
use CreationError;
|
2015-02-19 02:49:53 +11:00
|
|
|
use GlRequest;
|
2015-04-30 21:23:37 +10:00
|
|
|
use GlContext;
|
|
|
|
use PixelFormat;
|
2015-06-23 01:58:32 +10:00
|
|
|
use Robustness;
|
2015-02-16 19:29:37 +11:00
|
|
|
|
|
|
|
use gl_common;
|
|
|
|
use libc;
|
|
|
|
|
2015-04-02 17:27:32 +11:00
|
|
|
use platform;
|
2015-02-16 19:29:37 +11:00
|
|
|
|
|
|
|
/// Object that allows you to build headless contexts.
|
|
|
|
pub struct HeadlessRendererBuilder {
|
|
|
|
attribs: BuilderAttribs<'static>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeadlessRendererBuilder {
|
|
|
|
/// Initializes a new `HeadlessRendererBuilder` with default values.
|
|
|
|
pub fn new(width: u32, height: u32) -> HeadlessRendererBuilder {
|
|
|
|
HeadlessRendererBuilder {
|
|
|
|
attribs: BuilderAttribs {
|
|
|
|
headless: true,
|
|
|
|
dimensions: Some((width, height)),
|
|
|
|
.. BuilderAttribs::new()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 02:49:53 +11:00
|
|
|
/// Sets how the backend should choose the OpenGL API and version.
|
|
|
|
pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder {
|
|
|
|
self.attribs.gl_version = request;
|
2015-02-16 19:29:37 +11:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the *debug* flag for the OpenGL context.
|
|
|
|
///
|
|
|
|
/// The default value for this flag is `cfg!(ndebug)`, which means that it's enabled
|
|
|
|
/// when you run `cargo build` and disabled when you run `cargo build --release`.
|
|
|
|
pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder {
|
|
|
|
self.attribs.gl_debug = flag;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-23 01:58:32 +10:00
|
|
|
/// Sets the robustness of the OpenGL context. See the docs of `Robustness`.
|
|
|
|
pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder {
|
|
|
|
self.attribs.gl_robustness = robustness;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-02-16 19:29:37 +11:00
|
|
|
/// Builds the headless context.
|
|
|
|
///
|
|
|
|
/// Error should be very rare and only occur in case of permission denied, incompatible system,
|
|
|
|
/// out of memory, etc.
|
|
|
|
pub fn build(self) -> Result<HeadlessContext, CreationError> {
|
2015-04-02 17:27:32 +11:00
|
|
|
platform::HeadlessContext::new(self.attribs).map(|w| HeadlessContext { context: w })
|
2015-02-16 19:29:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the headless context.
|
|
|
|
///
|
|
|
|
/// The context is build in a *strict* way. That means that if the backend couldn't give
|
|
|
|
/// you what you requested, an `Err` will be returned.
|
|
|
|
pub fn build_strict(mut self) -> Result<HeadlessContext, CreationError> {
|
|
|
|
self.attribs.strict = true;
|
|
|
|
self.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a headless OpenGL context.
|
|
|
|
pub struct HeadlessContext {
|
2015-04-02 17:27:32 +11:00
|
|
|
context: platform::HeadlessContext,
|
2015-02-16 19:29:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HeadlessContext {
|
|
|
|
/// Creates a new OpenGL context
|
|
|
|
/// Sets the context as the current context.
|
|
|
|
#[inline]
|
2015-06-16 18:15:31 +10:00
|
|
|
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
|
2015-02-16 19:29:37 +11:00
|
|
|
self.context.make_current()
|
|
|
|
}
|
2015-03-04 17:38:55 +11:00
|
|
|
|
|
|
|
/// Returns true if this context is the current one in this thread.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_current(&self) -> bool {
|
|
|
|
self.context.is_current()
|
|
|
|
}
|
2015-02-16 19:29:37 +11:00
|
|
|
|
|
|
|
/// Returns the address of an OpenGL function.
|
|
|
|
///
|
|
|
|
/// Contrary to `wglGetProcAddress`, all available OpenGL functions return an address.
|
|
|
|
#[inline]
|
|
|
|
pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
|
|
|
|
self.context.get_proc_address(addr) as *const libc::c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the API that is currently provided by this window.
|
|
|
|
///
|
|
|
|
/// See `Window::get_api` for more infos.
|
|
|
|
pub fn get_api(&self) -> Api {
|
|
|
|
self.context.get_api()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl gl_common::GlFunctionsSource for HeadlessContext {
|
|
|
|
fn get_proc_addr(&self, addr: &str) -> *const libc::c_void {
|
|
|
|
self.get_proc_address(addr)
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 21:23:37 +10:00
|
|
|
|
|
|
|
impl GlContext for HeadlessContext {
|
2015-06-16 18:15:31 +10:00
|
|
|
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.make_current()
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_current(&self) -> bool {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.is_current()
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.get_proc_address(addr)
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
2015-06-16 18:15:31 +10:00
|
|
|
fn swap_buffers(&self) -> Result<(), ContextError> {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.swap_buffers()
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_api(&self) -> Api {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.get_api()
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
2015-05-09 03:31:56 +10:00
|
|
|
self.context.get_pixel_format()
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 03:31:56 +10:00
|
|
|
|