Skip to content

Commit

Permalink
Add default Unix backend
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Nov 5, 2019
1 parent 069b7e9 commit 0e262dc
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 192 deletions.
2 changes: 1 addition & 1 deletion surfman/examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn main() {

let connection = Connection::from_winit_window(&window).unwrap();
let native_widget = NativeWidget::from_winit_window(&window);
let adapter = Adapter::default().unwrap();
let adapter = connection.create_adapter().unwrap();
let mut device = Device::new(&connection, &adapter).unwrap();

let context_attributes = ContextAttributes {
Expand Down
11 changes: 0 additions & 11 deletions surfman/src/adapter.rs

This file was deleted.

6 changes: 6 additions & 0 deletions surfman/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ use crate::Error;
use winit::Window;

pub trait Connection: Sized {
type Adapter;

fn new() -> Result<Self, Error>;

fn create_adapter(&self) -> Result<Self::Adapter, Error>;
fn create_hardware_adapter(&self) -> Result<Self::Adapter, Error>;
fn create_software_adapter(&self) -> Result<Self::Adapter, Error>;

#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Self, Error>;
}
10 changes: 6 additions & 4 deletions surfman/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use crate::{ContextAttributes, ContextID, Error, GLApi, SurfaceAccess, SurfaceInfo, SurfaceType};
use crate::gl::types::{GLenum, GLuint};
use super::connection::Connection as ConnectionInterface;

use std::os::raw::c_void;

pub trait Device: Sized {
type Adapter;
pub trait Device: Sized where Self::Connection: ConnectionInterface {
type Connection;
type Context;
type ContextDescriptor;
Expand All @@ -17,9 +17,11 @@ pub trait Device: Sized {
type SurfaceTexture;

// device.rs
fn new(connection: &Self::Connection, adapter: &Self::Adapter) -> Result<Self, Error>;
fn new(connection: &Self::Connection,
adapter: &<Self::Connection as ConnectionInterface>::Adapter)
-> Result<Self, Error>;
fn connection(&self) -> Self::Connection;
fn adapter(&self) -> Self::Adapter;
fn adapter(&self) -> <Self::Connection as ConnectionInterface>::Adapter;
fn gl_api() -> GLApi;

// context.rs
Expand Down
25 changes: 0 additions & 25 deletions surfman/src/implementation/adapter.rs

This file was deleted.

18 changes: 18 additions & 0 deletions surfman/src/implementation/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@

use crate::Error;
use crate::connection::Connection as ConnectionInterface;
use super::super::adapter::Adapter;
use super::super::connection::Connection;

#[cfg(feature = "sm-winit")]
use winit::Window;

impl ConnectionInterface for Connection {
type Adapter = Adapter;

#[inline]
fn new() -> Result<Connection, Error> {
Connection::new()
}

#[inline]
fn create_adapter(&self) -> Result<Adapter, Error> {
Connection::create_adapter(self)
}

#[inline]
fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
Connection::create_hardware_adapter(self)
}

#[inline]
fn create_software_adapter(&self) -> Result<Adapter, Error> {
Connection::create_software_adapter(self)
}

#[inline]
#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Connection, Error> {
Expand Down
4 changes: 2 additions & 2 deletions surfman/src/implementation/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This is an included private module that automatically produces the implementation of the
//! `Device` trait for a backend.

use crate::connection::Connection as ConnectionInterface;
use crate::device::Device as DeviceInterface;
use crate::gl::types::{GLenum, GLuint};
use crate::{ContextAttributes, ContextID, Error, GLApi, SurfaceAccess, SurfaceInfo, SurfaceType};
Expand All @@ -15,7 +16,6 @@ use super::super::surface::{NativeWidget, Surface, SurfaceTexture};
use std::os::raw::c_void;

impl DeviceInterface for Device {
type Adapter = Adapter;
type Connection = Connection;
type Context = Context;
type ContextDescriptor = ContextDescriptor;
Expand All @@ -36,7 +36,7 @@ impl DeviceInterface for Device {
}

#[inline]
fn adapter(&self) -> Self::Adapter {
fn adapter(&self) -> <Self::Connection as ConnectionInterface>::Adapter {
Device::adapter(self)
}

Expand Down
1 change: 0 additions & 1 deletion surfman/src/implementation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
//! This is an included private module that automatically produces the implementations of the
//! various traits for a backend.

mod adapter;
mod connection;
mod device;
1 change: 0 additions & 1 deletion surfman/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use platform::default::context::{Context, ContextDescriptor};
pub use platform::default::device::Device;
pub use platform::default::surface::{NativeWidget, Surface, SurfaceDataGuard, SurfaceTexture};

pub mod adapter;
pub mod connection;
pub mod device;

Expand Down
59 changes: 3 additions & 56 deletions surfman/src/platform/generic/multi/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,10 @@
//
//! An adapter abstraction that allows the choice of backends dynamically.

use crate::Error;
use crate::adapter::Adapter as AdapterInterface;
use crate::connection::Connection as ConnectionInterface;
use crate::device::Device as DeviceInterface;

#[derive(Clone, Debug)]
pub enum Adapter<Def, Alt> where Def: DeviceInterface, Alt: DeviceInterface {
Default(Def::Adapter),
Alternate(Alt::Adapter),
}

impl<Def, Alt> Adapter<Def, Alt> where Def: DeviceInterface,
Alt: DeviceInterface,
Def::Adapter: AdapterInterface,
Alt::Adapter: AdapterInterface {
/// Returns the "best" adapter on this system.
pub fn default() -> Result<Adapter<Def, Alt>, Error> {
match <Def::Adapter>::default() {
Ok(adapter) => Ok(Adapter::Default(adapter)),
Err(_) => <Alt::Adapter>::default().map(Adapter::Alternate),
}
}

/// Returns the "best" hardware adapter on this system.
#[inline]
pub fn hardware() -> Result<Adapter<Def, Alt>, Error> {
match <Def::Adapter>::hardware() {
Ok(adapter) => Ok(Adapter::Default(adapter)),
Err(_) => <Alt::Adapter>::hardware().map(Adapter::Alternate),
}
}

/// Returns the "best" software adapter on this system.
#[inline]
pub fn software() -> Result<Adapter<Def, Alt>, Error> {
match <Def::Adapter>::software() {
Ok(adapter) => Ok(Adapter::Default(adapter)),
Err(_) => <Alt::Adapter>::software().map(Adapter::Alternate),
}
}
}

impl<Def, Alt> AdapterInterface for Adapter<Def, Alt> where Def: DeviceInterface,
Alt: DeviceInterface,
Def::Adapter: AdapterInterface,
Alt::Adapter: AdapterInterface {
#[inline]
fn default() -> Result<Adapter<Def, Alt>, Error> {
Adapter::default()
}

#[inline]
fn hardware() -> Result<Adapter<Def, Alt>, Error> {
Adapter::hardware()
}

#[inline]
fn software() -> Result<Adapter<Def, Alt>, Error> {
Adapter::software()
}
Default(<Def::Connection as ConnectionInterface>::Adapter),
Alternate(<Alt::Connection as ConnectionInterface>::Adapter),
}
51 changes: 51 additions & 0 deletions surfman/src/platform/generic/multi/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::Error;
use crate::connection::Connection as ConnectionInterface;
use crate::device::Device as DeviceInterface;
use super::adapter::Adapter;

#[cfg(feature = "sm-winit")]
use winit::Window;
Expand All @@ -31,6 +32,39 @@ impl<Def, Alt> Connection<Def, Alt> where Def: DeviceInterface,
}
}

pub fn create_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
match *self {
Connection::Default(ref connection) => {
connection.create_adapter().map(Adapter::Default)
}
Connection::Alternate(ref connection) => {
connection.create_adapter().map(Adapter::Alternate)
}
}
}

pub fn create_hardware_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
match *self {
Connection::Default(ref connection) => {
connection.create_hardware_adapter().map(Adapter::Default)
}
Connection::Alternate(ref connection) => {
connection.create_hardware_adapter().map(Adapter::Alternate)
}
}
}

pub fn create_software_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
match *self {
Connection::Default(ref connection) => {
connection.create_software_adapter().map(Adapter::Default)
}
Connection::Alternate(ref connection) => {
connection.create_software_adapter().map(Adapter::Alternate)
}
}
}

#[cfg(feature = "sm-winit")]
pub fn from_winit_window(window: &Window) -> Result<Connection<Def, Alt>, Error> {
match <Def::Connection>::from_winit_window(window) {
Expand All @@ -45,11 +79,28 @@ impl<Def, Alt> ConnectionInterface for Connection<Def, Alt>
Alt: DeviceInterface,
Def::Connection: ConnectionInterface,
Alt::Connection: ConnectionInterface {
type Adapter = Adapter<Def, Alt>;

#[inline]
fn new() -> Result<Connection<Def, Alt>, Error> {
Connection::new()
}

#[inline]
fn create_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
Connection::create_adapter(self)
}

#[inline]
fn create_hardware_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
Connection::create_hardware_adapter(self)
}

#[inline]
fn create_software_adapter(&self) -> Result<Adapter<Def, Alt>, Error> {
Connection::create_software_adapter(self)
}

#[inline]
#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Connection<Def, Alt>, Error> {
Expand Down
10 changes: 2 additions & 8 deletions surfman/src/platform/generic/multi/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! A device abstraction that allows the choice of backends dynamically.

use crate::{ContextID, Error, GLApi, SurfaceAccess, SurfaceInfo, SurfaceType};
use crate::adapter::Adapter as AdapterInterface;
use crate::connection::Connection as ConnectionInterface;
use crate::context::ContextAttributes;
use crate::device::Device as DeviceInterface;
Expand All @@ -22,8 +21,6 @@ pub enum Device<Def, Alt> where Def: DeviceInterface, Alt: DeviceInterface {

impl<Def, Alt> Device<Def, Alt> where Def: DeviceInterface,
Alt: DeviceInterface,
Def::Adapter: AdapterInterface,
Alt::Adapter: AdapterInterface,
Def::Connection: ConnectionInterface,
Alt::Connection: ConnectionInterface {
pub fn new(connection: &Connection<Def, Alt>, adapter: &Adapter<Def, Alt>)
Expand Down Expand Up @@ -63,13 +60,10 @@ impl<Def, Alt> Device<Def, Alt> where Def: DeviceInterface,
impl<Def, Alt> DeviceInterface for Device<Def, Alt>
where Def: DeviceInterface,
Alt: DeviceInterface,
Def::Adapter: AdapterInterface,
Alt::Adapter: AdapterInterface,
Def::Connection: ConnectionInterface,
Alt::Connection: ConnectionInterface,
Def::NativeWidget: Clone,
Alt::NativeWidget: Clone {
type Adapter = Adapter<Def, Alt>;
type Connection = Connection<Def, Alt>;
type Context = Context<Def, Alt>;
type ContextDescriptor = ContextDescriptor<Def, Alt>;
Expand All @@ -85,12 +79,12 @@ impl<Def, Alt> DeviceInterface for Device<Def, Alt>
}

#[inline]
fn connection(&self) -> Self::Connection {
fn connection(&self) -> Connection<Def, Alt> {
Device::connection(self)
}

#[inline]
fn adapter(&self) -> Self::Adapter {
fn adapter(&self) -> Adapter<Def, Alt> {
Device::adapter(self)
}

Expand Down
24 changes: 0 additions & 24 deletions surfman/src/platform/macos/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@
//
//! A wrapper for Core OpenGL adapters.

use crate::Error;

/// A no-op adapter.
#[derive(Clone, Debug)]
pub struct Adapter;

impl Adapter {
/// Returns the "best" adapter on this system.
#[inline]
pub fn default() -> Result<Adapter, Error> {
Ok(Adapter)
}

/// Returns the "best" hardware adapter on this system.
#[inline]
pub fn hardware() -> Result<Adapter, Error> {
Adapter::default()
}

/// Returns the "best" software adapter on this system.
///
/// The macOS backend has no software support, so this returns an error.
#[inline]
pub fn software() -> Result<Adapter, Error> {
Err(Error::NoSoftwareAdapters)
}
}
Loading

0 comments on commit 0e262dc

Please sign in to comment.