Skip to content

Commit

Permalink
Remove BuilderAttribs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Sep 21, 2015
1 parent a8d3342 commit 07640e7
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 132 deletions.
22 changes: 16 additions & 6 deletions src/api/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use events::MouseButton;
use std::collections::VecDeque;

use Api;
use BuilderAttribs;
use ContextError;
use CursorState;
use GlAttributes;
use GlContext;
use GlRequest;
use PixelFormat;
use PixelFormatRequirements;
use WindowAttributes;
use native_monitor::NativeMonitorId;

use api::egl;
Expand Down Expand Up @@ -104,15 +106,19 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
}

impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
use std::{mem, ptr};

let opengl = opengl.clone().map_sharing(|w| &w.context);

let native_window = unsafe { android_glue::get_native_window() };
if native_window.is_null() {
return Err(OsError(format!("Android's native window is null")));
}

let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder.pf_reqs, &builder.opengl,
let context = try!(EglContext::new(egl::ffi::egl::Egl, pf_reqs, &opengl,
egl::NativeDisplay::Android)
.and_then(|p| p.finish(native_window as *const _)));

Expand Down Expand Up @@ -255,9 +261,13 @@ pub struct HeadlessContext(EglContext);

impl HeadlessContext {
/// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, egl::NativeDisplay::Android));
let context = try!(context.finish_pbuffer(builder.window.dimensions.unwrap_or((800, 600)))); // TODO:
pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
let opengl = opengl.clone().map_sharing(|c| &c.0);
let context = try!(EglContext::new(egl::ffi::egl::Egl, pf_reqs, &opengl,
egl::NativeDisplay::Android));
let context = try!(context.finish_pbuffer(dimensions)); // TODO:
Ok(HeadlessContext(context))
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/api/caca/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use libc;
use api::osmesa::{OsMesaContext, OsMesaCreationError};

use Api;
use BuilderAttribs;
use ContextError;
use CreationError;
use Event;
use GlAttributes;
use GlContext;
use PixelFormat;
use PixelFormatRequirements;
use CursorState;
use MouseCursor;
use WindowAttributes;

use std::collections::VecDeque;
use std::path::Path;
Expand Down Expand Up @@ -84,8 +86,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
}

impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
let opengl = match OsMesaContext::new(builder) {
pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
let opengl = opengl.clone().map_sharing(|w| &w.opengl);

let opengl = match OsMesaContext::new(window.dimensions.unwrap_or((800, 600)), pf_reqs,
&opengl)
{
Err(OsMesaCreationError::NotSupported) => return Err(CreationError::NotSupported),
Err(OsMesaCreationError::CreationError(e)) => return Err(e),
Ok(c) => c
Expand Down
8 changes: 5 additions & 3 deletions src/api/cocoa/headless.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ContextError;
use CreationError;
use CreationError::OsError;
use BuilderAttribs;
use GlAttributes;
use GlContext;
use PixelFormatRequirements;
use libc;
use std::ptr;

Expand All @@ -27,8 +28,9 @@ pub struct HeadlessContext {
}

impl HeadlessContext {
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
let (width, height) = builder.window.dimensions.unwrap_or((1024, 768));
pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
let context = unsafe {
let attributes = [
NSOpenGLPFAAccelerated as u32,
Expand Down
17 changes: 9 additions & 8 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use CreationError::OsError;
use libc;

use Api;
use BuilderAttribs;
use ContextError;
use GlAttributes;
use GlContext;
Expand Down Expand Up @@ -269,12 +268,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> {

impl Window {
#[cfg(feature = "window")]
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
if builder.opengl.sharing.is_some() {
pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
if opengl.sharing.is_some() {
unimplemented!()
}

match builder.opengl.robustness {
match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported);
},
Expand All @@ -286,7 +287,7 @@ impl Window {
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
};

let window = match Window::create_window(&builder.window)
let window = match Window::create_window(win_attribs)
{
Some(window) => window,
None => { return Err(OsError(format!("Couldn't create NSWindow"))); },
Expand All @@ -298,13 +299,13 @@ impl Window {

// TODO: perhaps we should return error from create_context so we can
// determine the cause of failure and possibly recover?
let (context, pf) = match Window::create_context(*view, &builder.pf_reqs, &builder.opengl) {
let (context, pf) = match Window::create_context(*view, pf_reqs, opengl) {
Ok((context, pf)) => (context, pf),
Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); },
};

unsafe {
if builder.window.transparent {
if win_attribs.transparent {
let clear_col = {
let cls = Class::get("NSColor").unwrap();

Expand All @@ -320,7 +321,7 @@ impl Window {
}

app.activateIgnoringOtherApps_(YES);
if builder.window.visible {
if win_attribs.visible {
window.makeKeyAndOrderFront_(nil);
} else {
window.makeKeyWindow();
Expand Down
12 changes: 8 additions & 4 deletions src/api/osmesa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
extern crate osmesa_sys;

use Api;
use BuilderAttribs;
use ContextError;
use CreationError;
use GlAttributes;
use GlContext;
use PixelFormat;
use PixelFormatRequirements;
use Robustness;
use libc;
use std::{mem, ptr};
Expand All @@ -32,20 +33,23 @@ impl From<CreationError> for OsMesaCreationError {
}

impl OsMesaContext {
pub fn new(builder: BuilderAttribs) -> Result<OsMesaContext, OsMesaCreationError> {
pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&OsMesaContext>) -> Result<OsMesaContext, OsMesaCreationError>
{
if let Err(_) = osmesa_sys::OsMesa::try_loading() {
return Err(OsMesaCreationError::NotSupported);
}

let dimensions = builder.window.dimensions.unwrap();
if opengl.sharing.is_some() { unimplemented!() } // TODO: proper error

match builder.opengl.robustness {
match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported.into());
},
_ => ()
}

// TODO: use `pf_reqs` for the format
// TODO: check OpenGL version and return `OpenGlVersionNotSupported` if necessary

Ok(OsMesaContext {
Expand Down
1 change: 0 additions & 1 deletion src/api/win32/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use super::WindowWrapper;
use super::Context;

use Api;
use BuilderAttribs;
use CreationError;
use CreationError::OsError;
use CursorState;
Expand Down
1 change: 0 additions & 1 deletion src/api/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use GlContext;
use Api;
use PixelFormat;
use PixelFormatRequirements;
use BuilderAttribs;
use WindowAttributes;

pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
Expand Down
42 changes: 20 additions & 22 deletions src/headless.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use Api;
use BuilderAttribs;
use ContextError;
use CreationError;
use GlAttributes;
use GlRequest;
use GlContext;
use PixelFormat;
use PixelFormatRequirements;
use Robustness;
use WindowAttributes;

Expand All @@ -14,43 +15,40 @@ use libc;
use platform;

/// Object that allows you to build headless contexts.
pub struct HeadlessRendererBuilder {
attribs: BuilderAttribs<'static>,
pub struct HeadlessRendererBuilder<'a> {
dimensions: (u32, u32),
pf_reqs: PixelFormatRequirements,
opengl: GlAttributes<&'a platform::HeadlessContext>,
}

impl HeadlessRendererBuilder {
impl<'a> HeadlessRendererBuilder<'a> {
/// Initializes a new `HeadlessRendererBuilder` with default values.
pub fn new(width: u32, height: u32) -> HeadlessRendererBuilder {
pub fn new(width: u32, height: u32) -> HeadlessRendererBuilder<'a> {
HeadlessRendererBuilder {
attribs: BuilderAttribs {
headless: true,
window: WindowAttributes {
dimensions: Some((width, height)),
.. Default::default()
},
.. BuilderAttribs::new()
},
dimensions: (width, height),
pf_reqs: Default::default(),
opengl: Default::default(),
}
}

/// Sets how the backend should choose the OpenGL API and version.
pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder {
self.attribs.opengl.version = request;
pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder<'a> {
self.opengl.version = request;
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.opengl.debug = flag;
pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder<'a> {
self.opengl.debug = flag;
self
}

/// Sets the robustness of the OpenGL context. See the docs of `Robustness`.
pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder {
self.attribs.opengl.robustness = robustness;
pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder<'a> {
self.opengl.robustness = robustness;
self
}

Expand All @@ -59,15 +57,15 @@ impl HeadlessRendererBuilder {
/// 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> {
platform::HeadlessContext::new(self.attribs).map(|w| HeadlessContext { context: w })
platform::HeadlessContext::new(self.dimensions, &self.pf_reqs, &self.opengl)
.map(|w| HeadlessContext { context: w })
}

/// 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;
pub fn build_strict(self) -> Result<HeadlessContext, CreationError> {
self.build()
}
}
Expand Down
35 changes: 1 addition & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,40 +362,7 @@ pub struct PixelFormat {
pub multisampling: Option<u16>,
pub srgb: bool,
}

/// Attributes
// FIXME: remove `pub` (https://github.com/rust-lang/rust/issues/23585)
#[derive(Clone)]
#[doc(hidden)]
pub struct BuilderAttribs<'a> {
#[allow(dead_code)]
headless: bool,
strict: bool,
pf_reqs: PixelFormatRequirements,
window: WindowAttributes,
opengl: GlAttributes<&'a platform::Window>,
}

impl BuilderAttribs<'static> {
fn new() -> BuilderAttribs<'static> {
BuilderAttribs {
headless: false,
strict: false,
pf_reqs: Default::default(),
window: Default::default(),
opengl: Default::default(),
}
}
}

impl<'a> BuilderAttribs<'a> {
fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone
{
self.pf_reqs.choose_pixel_format(iter)
}
}


/// VERY UNSTABLE! Describes how the backend should choose a pixel format.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
Expand Down
Loading

0 comments on commit 07640e7

Please sign in to comment.