This commit is contained in:
Drakulix 2017-05-21 22:40:15 +02:00
parent b950714c03
commit 5bc641852f
8 changed files with 457 additions and 1116 deletions

View File

@ -26,6 +26,6 @@ slog-term = "~1.5"
[features]
default = ["backend_winit", "backend_libinput", "renderer_glium"]
backend_winit = ["winit", "wayland-server/dlopen", "wayland-client", "wayland-client/dlopen"]
backend_winit = ["winit", "wayland-server/dlopen", "wayland-client/dlopen"]
renderer_glium = ["glium"]
backend_libinput = ["input"]

View File

@ -1,6 +1,6 @@
extern crate gl_generator;
use gl_generator::{Registry, Api, Profile, Fallbacks};
use gl_generator::{Api, Fallbacks, Profile, Registry};
use std::env;
use std::fs::File;
@ -12,19 +12,22 @@ fn main() {
println!("cargo:rerun-if-changed=build.rs");
let mut file = File::create(&dest.join("egl_bindings.rs")).unwrap();
Registry::new(Api::Egl, (1, 5), Profile::Core, Fallbacks::All, [
"EGL_KHR_create_context",
"EGL_EXT_create_context_robustness",
"EGL_KHR_create_context_no_error",
"EGL_KHR_platform_x11",
"EGL_KHR_platform_android",
"EGL_KHR_platform_wayland",
"EGL_KHR_platform_gbm",
"EGL_EXT_platform_base",
"EGL_EXT_platform_x11",
"EGL_MESA_platform_gbm",
"EGL_EXT_platform_wayland",
"EGL_EXT_platform_device",
])
.write_bindings(gl_generator::StructGenerator, &mut file).unwrap();
Registry::new(Api::Egl,
(1, 5),
Profile::Core,
Fallbacks::All,
["EGL_KHR_create_context",
"EGL_EXT_create_context_robustness",
"EGL_KHR_create_context_no_error",
"EGL_KHR_platform_x11",
"EGL_KHR_platform_android",
"EGL_KHR_platform_wayland",
"EGL_KHR_platform_gbm",
"EGL_EXT_platform_base",
"EGL_EXT_platform_x11",
"EGL_MESA_platform_gbm",
"EGL_EXT_platform_wayland",
"EGL_EXT_platform_device"])
.write_bindings(gl_generator::StructGenerator, &mut file)
.unwrap();
}

View File

@ -1,12 +1,15 @@
extern crate wayland_server;
extern crate smithay;
use smithay::backend::glutin;
use smithay::backend::winit;
use smithay::backend::input::InputBackend;
use smithay::shm::ShmGlobal;
use wayland_server::protocol::wl_shm;
fn main() {
// Initialize a simple backend for testing
let (mut renderer, mut input) = winit::init().unwrap();
let (_, mut event_loop) = wayland_server::create_display();
// Insert the ShmGlobal as a handler to your event loop
@ -24,9 +27,6 @@ fn main() {
state.get_handler::<ShmGlobal>(handler_id).get_token()
};
// Initialize a simple backend for testing
let (mut renderer, mut input) = glutin::init_windowed().unwrap();
// TODO render stuff
// TODO put input handling on the event loop

View File

@ -1,745 +0,0 @@
//! Implementation of backend traits for types provided by `glutin`
use backend::{SeatInternal, TouchSlotInternal};
use backend::graphics::GraphicsBackend;
use backend::graphics::opengl::{Api, OpenglGraphicsBackend, PixelFormat, SwapBuffersError};
use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState,
KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent,
PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent,
TouchMotionEvent, TouchSlot, TouchUpEvent, UnusedEvent};
use glutin::{Api as GlutinApi, MouseButton as GlutinMouseButton, MouseCursor,
PixelFormat as GlutinPixelFormat};
use glutin::{ContextError, CreationError, ElementState, Event, GlContext, HeadlessContext,
HeadlessRendererBuilder, MouseScrollDelta, Touch, TouchPhase, Window, WindowBuilder};
use nix::c_void;
use std::cmp;
use std::error::Error;
use std::fmt;
use std::rc::Rc;
/// Create a new `GlutinHeadlessRenderer` which implements the `OpenglRenderer` graphics
/// backend trait
pub fn init_headless_renderer() -> Result<GlutinHeadlessRenderer, CreationError> {
init_headless_renderer_from_builder(HeadlessRendererBuilder::new(1024, 600)
}
/// Create a new `GlutinHeadlessRenderer`, which implements the `OpenglRenderer` graphics
/// backend trait, with a given already configured `HeadlessRendererBuilder` for
/// customization
pub fn init_headless_renderer_from_builder(builder: HeadlessRendererBuilder)
-> Result<GlutinHeadlessRenderer, CreationError> {
let (w, h) = builder.dimensions;
let context = builder.build_strict()?;
Ok(GlutinHeadlessRenderer::new(context, w, h))
}
/// Create a new `GlutinWindowedRenderer`, which implements the `OpenglRenderer` graphics
/// backend trait
pub fn init_windowed_renderer() -> Result<GlutinWindowedRenderer, CreationError> {
init_windowed_renderer_from_builder(WindowBuilder::new())
}
/// Create a new `GlutinWindowedRenderer`, which implements the `OpenglRenderer` graphics
/// backend trait, with a given already configured `WindowBuilder` for customization.
pub fn init_windowed_renderer_from_builder(builder: WindowBuilder)
-> Result<GlutinWindowedRenderer, CreationError> {
let window = Rc::new(builder.build_strict()?);
Ok(GlutinWindowedRenderer::new(window))
}
/// Create a new `glutin` `Window`. Returns a `GlutinWindowedRenderer` implementing
/// the `OpenglRenderer` graphics backend trait and a `GlutinInputBackend` implementing
/// the `InputBackend` trait.
pub fn init_windowed() -> Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> {
init_windowed_from_builder(WindowBuilder::new())
}
/// Create a new `glutin` `Window` with a given already configured `WindowBuilder` for
/// customization. Returns a `GlutinWindowedRenderer` implementing
/// the `OpenglRenderer` graphics backend trait and a `GlutinInputBackend` implementing
/// the `InputBackend` trait.
pub fn init_windowed_from_builder(builder: WindowBuilder)
-> Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> {
let window = Rc::new(builder.build_strict()?);
Ok((GlutinWindowedRenderer::new(window.clone()), GlutinInputBackend::new(window)))
}
/// Headless Opengl Context created by `glutin`. Implements the `OpenglGraphicsBackend` graphics
/// backend trait.
pub struct GlutinHeadlessRenderer {
context: HeadlessContext,
w: u32,
h: u32,
}
impl GlutinHeadlessRenderer {
fn new(context: HeadlessContext, w: u32, h: u32) -> GlutinHeadlessRenderer {
GlutinHeadlessRenderer {
context: context,
w: w,
h: h,
}
}
}
impl GraphicsBackend for GlutinHeadlessRenderer {
type CursorFormat = ();
fn set_cursor_position(&mut self, _x: u32, _y: u32) -> Result<(), ()> {
// FIXME: Maybe save position? Is it of any use?
Ok(())
}
fn set_cursor_representation(&mut self, _cursor: ()) {}
}
impl OpenglGraphicsBackend for GlutinHeadlessRenderer {
#[inline]
fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
match self.context.swap_buffers() {
Ok(()) => Ok(()),
Err(ContextError::IoError(e)) => panic!("Error while swapping buffers: {:?}", e),
Err(ContextError::ContextLost) => Err(SwapBuffersError::ContextLost),
}
}
#[inline]
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
self.context.get_proc_address(symbol) as *const _
}
#[inline]
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
(self.w, self.h)
}
#[inline]
fn is_current(&self) -> bool {
self.context.is_current()
}
#[inline]
unsafe fn make_current(&self) {
self.context.make_current().unwrap();
}
fn get_api(&self) -> Api {
self.context.get_api().into()
}
fn get_pixel_format(&self) -> PixelFormat {
self.context.get_pixel_format().into()
}
}
/// Window with an active Opengl Context created by `glutin`. Implements the
/// `OpenglGraphicsBackend` graphics backend trait.
pub struct GlutinWindowedRenderer {
window: Rc<Window>,
}
impl GlutinWindowedRenderer {
fn new(window: Rc<Window>) -> GlutinWindowedRenderer {
GlutinWindowedRenderer { window: window }
}
}
impl GraphicsBackend for GlutinWindowedRenderer {
type CursorFormat = MouseCursor;
fn set_cursor_position(&mut self, x: u32, y: u32) -> Result<(), ()> {
if let Some((win_x, win_y)) = self.window.get_position() {
self.window
.set_cursor_position(win_x + x as i32, win_y + y as i32)
} else {
Err(())
}
}
fn set_cursor_representation(&mut self, cursor: MouseCursor) {
self.window.set_cursor(cursor);
}
}
impl OpenglGraphicsBackend for GlutinWindowedRenderer {
#[inline]
fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
match self.window.swap_buffers() {
Ok(()) => Ok(()),
Err(ContextError::IoError(e)) => panic!("Error while swapping buffers: {:?}", e),
Err(ContextError::ContextLost) => Err(SwapBuffersError::ContextLost),
}
}
#[inline]
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
self.window.get_proc_address(symbol) as *const _
}
#[inline]
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
let (width, height) = self.window.get_inner_size().unwrap_or((800, 600)); // TODO: 800x600 ?
let scale = self.window.hidpi_factor();
((width as f32 * scale) as u32, (height as f32 * scale) as u32)
}
#[inline]
fn is_current(&self) -> bool {
self.window.is_current()
}
#[inline]
unsafe fn make_current(&self) {
self.window.make_current().unwrap();
}
fn get_api(&self) -> Api {
self.window.get_api().into()
}
fn get_pixel_format(&self) -> PixelFormat {
self.window.get_pixel_format().into()
}
}
/// Errors that may happen when driving the event loop of `GlutinInputBackend`
#[derive(Debug)]
pub enum GlutinInputError {
/// The underlying `glutin` `Window` was closed. No further events can be processed.
///
/// See `GlutinInputBackend::process_new_events`.
WindowClosed,
}
impl Error for GlutinInputError {
fn description(&self) -> &str {
match *self {
GlutinInputError::WindowClosed => "Glutin Window was closed",
}
}
}
impl fmt::Display for GlutinInputError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
/// Abstracted event loop of a `glutin` `Window` implementing the `InputBackend` trait
///
/// You need to call `process_new_events` periodically to receive any events.
pub struct GlutinInputBackend {
window: Rc<Window>,
time_counter: u32,
key_counter: u32,
seat: Seat,
input_config: (),
handler: Option<Box<InputHandler<GlutinInputBackend> + 'static>>,
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `KeyboardKeyEvent`
pub struct GlutinKeyboardInputEvent {
time: u32,
key: u8,
count: u32,
state: ElementState,
}
impl BackendEvent for GlutinKeyboardInputEvent {
fn time(&self) -> u32 {
self.time
}
}
impl KeyboardKeyEvent for GlutinKeyboardInputEvent {
fn key_code(&self) -> u32 {
self.key as u32
}
fn state(&self) -> KeyState {
self.state.into()
}
fn count(&self) -> u32 {
self.count
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `PointerMotionAbsoluteEvent`
pub struct GlutinMouseMovedEvent {
window: Rc<Window>,
time: u32,
x: i32,
y: i32,
}
impl BackendEvent for GlutinMouseMovedEvent {
fn time(&self) -> u32 {
self.time
}
}
impl PointerMotionAbsoluteEvent for GlutinMouseMovedEvent {
fn x(&self) -> f64 {
self.x as f64
}
fn y(&self) -> f64 {
self.y as f64
}
fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.x * width as i32 /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0 as i32,
0) as u32
}
fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.y * height as i32 /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1 as i32,
0) as u32
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `PointerAxisEvent`
pub struct GlutinMouseWheelEvent {
axis: Axis,
time: u32,
delta: MouseScrollDelta,
}
impl BackendEvent for GlutinMouseWheelEvent {
fn time(&self) -> u32 {
self.time
}
}
impl PointerAxisEvent for GlutinMouseWheelEvent {
fn axis(&self) -> Axis {
self.axis
}
fn source(&self) -> AxisSource {
match self.delta {
MouseScrollDelta::LineDelta(_, _) => AxisSource::Wheel,
MouseScrollDelta::PixelDelta(_, _) => AxisSource::Continuous,
}
}
fn amount(&self) -> f64 {
match (self.axis, self.delta) {
(Axis::Horizontal, MouseScrollDelta::LineDelta(x, _)) |
(Axis::Horizontal, MouseScrollDelta::PixelDelta(x, _)) => x as f64,
(Axis::Vertical, MouseScrollDelta::LineDelta(_, y)) |
(Axis::Vertical, MouseScrollDelta::PixelDelta(_, y)) => y as f64,
}
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `PointerButtonEvent`
pub struct GlutinMouseInputEvent {
time: u32,
button: GlutinMouseButton,
state: ElementState,
}
impl BackendEvent for GlutinMouseInputEvent {
fn time(&self) -> u32 {
self.time
}
}
impl PointerButtonEvent for GlutinMouseInputEvent {
fn button(&self) -> MouseButton {
self.button.into()
}
fn state(&self) -> MouseButtonState {
self.state.into()
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `TouchDownEvent`
pub struct GlutinTouchStartedEvent {
window: Rc<Window>,
time: u32,
location: (f64, f64),
id: u64,
}
impl BackendEvent for GlutinTouchStartedEvent {
fn time(&self) -> u32 {
self.time
}
}
impl TouchDownEvent for GlutinTouchStartedEvent {
fn slot(&self) -> Option<TouchSlot> {
Some(TouchSlot::new(self.id))
}
fn x(&self) -> f64 {
self.location.0
}
fn y(&self) -> f64 {
self.location.1
}
fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.location.0 as i32 * width as i32 /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0 as i32,
0) as u32
}
fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.location.1 as i32 * height as i32 /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1 as i32,
0) as u32
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `TouchMotionEvent`
pub struct GlutinTouchMovedEvent {
window: Rc<Window>,
time: u32,
location: (f64, f64),
id: u64,
}
impl BackendEvent for GlutinTouchMovedEvent {
fn time(&self) -> u32 {
self.time
}
}
impl TouchMotionEvent for GlutinTouchMovedEvent {
fn slot(&self) -> Option<TouchSlot> {
Some(TouchSlot::new(self.id))
}
fn x(&self) -> f64 {
self.location.0
}
fn y(&self) -> f64 {
self.location.1
}
fn x_transformed(&self, width: u32) -> u32 {
self.location.0 as u32 * width /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0
}
fn y_transformed(&self, height: u32) -> u32 {
self.location.1 as u32 * height /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `TouchUpEvent`
pub struct GlutinTouchEndedEvent {
time: u32,
id: u64,
}
impl BackendEvent for GlutinTouchEndedEvent {
fn time(&self) -> u32 {
self.time
}
}
impl TouchUpEvent for GlutinTouchEndedEvent {
fn slot(&self) -> Option<TouchSlot> {
Some(TouchSlot::new(self.id))
}
}
#[derive(Clone)]
/// Glutin-Backend internal event wrapping glutin's types into a `TouchCancelEvent`
pub struct GlutinTouchCancelledEvent {
time: u32,
id: u64,
}
impl BackendEvent for GlutinTouchCancelledEvent {
fn time(&self) -> u32 {
self.time
}
}
impl TouchCancelEvent for GlutinTouchCancelledEvent {
fn slot(&self) -> Option<TouchSlot> {
Some(TouchSlot::new(self.id))
}
}
impl InputBackend for GlutinInputBackend {
type InputConfig = ();
type EventError = GlutinInputError;
type KeyboardKeyEvent = GlutinKeyboardInputEvent;
type PointerAxisEvent = GlutinMouseWheelEvent;
type PointerButtonEvent = GlutinMouseInputEvent;
type PointerMotionEvent = UnusedEvent;
type PointerMotionAbsoluteEvent = GlutinMouseMovedEvent;
type TouchDownEvent = GlutinTouchStartedEvent;
type TouchUpEvent = GlutinTouchEndedEvent;
type TouchMotionEvent = GlutinTouchMovedEvent;
type TouchCancelEvent = GlutinTouchCancelledEvent;
type TouchFrameEvent = UnusedEvent;
fn set_handler<H: InputHandler<Self> + 'static>(&mut self, mut handler: H) {
if self.handler.is_some() {
self.clear_handler();
}
handler.on_seat_created(&self.seat);
self.handler = Some(Box::new(handler));
}
fn get_handler(&mut self) -> Option<&mut InputHandler<Self>> {
self.handler
.as_mut()
.map(|handler| handler as &mut InputHandler<Self>)
}
fn clear_handler(&mut self) {
if let Some(mut handler) = self.handler.take() {
handler.on_seat_destroyed(&self.seat);
}
}
fn input_config(&mut self) -> &mut Self::InputConfig {
&mut self.input_config
}
/// Processes new events of the underlying event loop to drive the set `InputHandler`.
///
/// You need to periodically call this function to keep the underlying event loop and
/// `Window` active. Otherwise the window may no respond to user interaction and no
/// input events will be received by a set `InputHandler`.
///
/// Returns an error if the `Window` the window has been closed. Calling
/// `process_new_events` again after the `Window` has been closed is considered an
/// application error and unspecified baviour may occur.
///
/// The linked `GlutinWindowedRenderer` will error with a lost Context and should
/// not be used anymore as well.
fn dispatch_new_events(&mut self) -> Result<(), GlutinInputError> {
for event in self.window.poll_events() {
if let Some(ref mut handler) = self.handler {
match event {
Event::KeyboardInput(state, key_code, _) => {
match state {
ElementState::Pressed => self.key_counter += 1,
ElementState::Released => {
self.key_counter = self.key_counter.checked_sub(1).unwrap_or(0)
}
};
handler.on_keyboard_key(&self.seat,
GlutinKeyboardInputEvent {
time: self.time_counter,
key: key_code,
count: self.key_counter,
state: state,
})
}
Event::MouseMoved(x, y) => {
handler.on_pointer_move_absolute(&self.seat,
GlutinMouseMovedEvent {
window: self.window.clone(),
time: self.time_counter,
x: x,
y: y,
})
}
Event::MouseWheel(delta, _) => {
let event = GlutinMouseWheelEvent {
axis: Axis::Horizontal,
time: self.time_counter,
delta: delta,
};
match delta {
MouseScrollDelta::LineDelta(x, y) |
MouseScrollDelta::PixelDelta(x, y) => {
if x != 0.0 {
handler.on_pointer_axis(&self.seat, event.clone());
}
if y != 0.0 {
handler.on_pointer_axis(&self.seat, event);
}
}
}
}
Event::MouseInput(state, button) => {
handler.on_pointer_button(&self.seat,
GlutinMouseInputEvent {
time: self.time_counter,
button: button,
state: state,
})
}
Event::Touch(Touch {
phase: TouchPhase::Started,
location: (x, y),
id,
}) => {
handler.on_touch_down(&self.seat,
GlutinTouchStartedEvent {
window: self.window.clone(),
time: self.time_counter,
location: (x, y),
id: id,
})
}
Event::Touch(Touch {
phase: TouchPhase::Moved,
location: (x, y),
id,
}) => {
handler.on_touch_motion(&self.seat,
GlutinTouchMovedEvent {
window: self.window.clone(),
time: self.time_counter,
location: (x, y),
id: id,
})
}
Event::Touch(Touch {
phase: TouchPhase::Ended,
location: (x, y),
id,
}) => {
handler.on_touch_motion(&self.seat,
GlutinTouchMovedEvent {
window: self.window.clone(),
time: self.time_counter,
location: (x, y),
id: id,
});
handler.on_touch_up(&self.seat,
GlutinTouchEndedEvent {
time: self.time_counter,
id: id,
});
}
Event::Touch(Touch {
phase: TouchPhase::Cancelled,
id,
..
}) => {
handler.on_touch_cancel(&self.seat,
GlutinTouchCancelledEvent {
time: self.time_counter,
id: id,
})
}
Event::Closed => return Err(GlutinInputError::WindowClosed),
_ => {}
}
self.time_counter += 1;
}
}
Ok(())
}
}
impl GlutinInputBackend {
fn new(window: Rc<Window>) -> GlutinInputBackend {
GlutinInputBackend {
window: window,
time_counter: 0,
key_counter: 0,
seat: Seat::new(0,
SeatCapabilities {
pointer: true,
keyboard: true,
touch: true,
}),
input_config: (),
handler: None,
}
}
}
impl From<GlutinApi> for Api {
fn from(api: GlutinApi) -> Self {
match api {
GlutinApi::OpenGl => Api::OpenGl,
GlutinApi::OpenGlEs => Api::OpenGlEs,
GlutinApi::WebGl => Api::WebGl,
}
}
}
impl From<GlutinPixelFormat> for PixelFormat {
fn from(format: GlutinPixelFormat) -> Self {
PixelFormat {
hardware_accelerated: format.hardware_accelerated,
color_bits: format.color_bits,
alpha_bits: format.alpha_bits,
depth_bits: format.depth_bits,
stencil_bits: format.stencil_bits,
stereoscopy: format.stereoscopy,
double_buffer: format.double_buffer,
multisampling: format.multisampling,
srgb: format.srgb,
}
}
}
impl From<GlutinMouseButton> for MouseButton {
fn from(button: GlutinMouseButton) -> MouseButton {
match button {
GlutinMouseButton::Left => MouseButton::Left,
GlutinMouseButton::Right => MouseButton::Right,
GlutinMouseButton::Middle => MouseButton::Middle,
GlutinMouseButton::Other(num) => MouseButton::Other(num),
}
}
}
impl From<ElementState> for KeyState {
fn from(state: ElementState) -> Self {
match state {
ElementState::Pressed => KeyState::Pressed,
ElementState::Released => KeyState::Released,
}
}
}
impl From<ElementState> for MouseButtonState {
fn from(state: ElementState) -> Self {
match state {
ElementState::Pressed => MouseButtonState::Pressed,
ElementState::Released => MouseButtonState::Released,
}
}
}

View File

@ -1,27 +1,27 @@
//! Common traits and types for opengl rendering on graphics backends
//! Common traits and types for egl context creation and rendering
/// Large parts of the following file are taken from
/// https://github.com/tomaka/glutin/tree/master/src/api/egl at commit
/// `044e651edf67a2029eecc650dd42546af1501414`
/// https://github.com/tomaka/glutin/tree/044e651edf67a2029eecc650dd42546af1501414/src/api/egl/
///
/// It therefor falls under glutin's Apache 2.0 license
/// (see https://github.com/tomaka/glutin/blob/master/LICENSE)
/// It therefore falls under glutin's Apache 2.0 license
/// (see https://github.com/tomaka/glutin/tree/044e651edf67a2029eecc650dd42546af1501414/LICENSE)
use super::GraphicsBackend;
use libloading::Library;
use nix::{c_int, c_void};
use std::error::{self, Error};
use std::ffi::{CStr, CString};
use std::error::{self, Error};
use std::fmt;
use std::io;
use std::ptr;
use std::mem;
use std::ptr;
#[allow(non_camel_case_types, dead_code)]
mod ffi {
use nix::c_void;
use nix::libc::{uint64_t, int32_t, c_long};
use nix::libc::{c_long, int32_t, uint64_t};
pub type khronos_utime_nanoseconds_t = khronos_uint64_t;
pub type khronos_uint64_t = uint64_t;
@ -41,20 +41,32 @@ mod ffi {
}
}
/// Native types to create an `EGLContext` from.
/// Currently supported providers are X11, Wayland and GBM.
#[derive(Clone, Copy)]
pub enum Native {
/// X11 Display and Window objects to create an `EGLContext` upon.
X11(ffi::NativeDisplayType, ffi::NativeWindowType),
/// Wayland Display and Surface objects to create an `EGLContext` upon.
Wayland(ffi::NativeDisplayType, ffi::NativeWindowType),
/// GBM Display
Gbm(ffi::NativeDisplayType, ffi::NativeWindowType),
}
/// Error that can happen while creating an `EGLContext`
#[derive(Debug)]
pub enum CreationError {
/// I/O error from the underlying system
IoError(io::Error),
/// Operating System error
OsError(String),
/// Robustness was requested but is not supported by the graphics system
RobustnessNotSupported,
/// The requested OpenGl version is not supported by the graphics system
OpenGlVersionNotSupported,
/// There is no pixel format available that fulfills all requirements
NoAvailablePixelFormat,
/// Context creation is not supported on this system
NotSupported,
}
@ -78,13 +90,19 @@ impl error::Error for CreationError {
fn description(&self) -> &str {
match *self {
CreationError::IoError(ref err) => err.description(),
CreationError::OsError(ref text) => &text,
CreationError::RobustnessNotSupported => "You requested robustness, but it is \
not supported.",
CreationError::OpenGlVersionNotSupported => "The requested OpenGL version is not \
supported.",
CreationError::NoAvailablePixelFormat => "Couldn't find any pixel format that matches \
the criterias.",
CreationError::OsError(ref text) => text,
CreationError::RobustnessNotSupported => {
"You requested robustness, but it is \
not supported."
}
CreationError::OpenGlVersionNotSupported => {
"The requested OpenGL version is not \
supported."
}
CreationError::NoAvailablePixelFormat => {
"Couldn't find any pixel format that matches \
the criterias."
}
CreationError::NotSupported => "Context creation is not supported on the current window system",
}
}
@ -92,11 +110,12 @@ impl error::Error for CreationError {
fn cause(&self) -> Option<&error::Error> {
match *self {
CreationError::IoError(ref err) => Some(err),
_ => None
_ => None,
}
}
}
/// EGL context for rendering
pub struct EGLContext {
context: *const c_void,
display: *const c_void,
@ -106,12 +125,23 @@ pub struct EGLContext {
}
impl EGLContext {
pub unsafe fn new(native: Native, mut attributes: GlAttributes, reqs: PixelFormatRequirements) -> Result<EGLContext, CreationError> {
/// Create a new EGL context
///
/// # Unsafety
///
/// This method is marked unsafe, because the contents of `Native` cannot be verified and msy
/// contain dangeling pointers are similar unsafe content
pub unsafe fn new(native: Native, mut attributes: GlAttributes, reqs: PixelFormatRequirements)
-> Result<EGLContext, CreationError> {
let lib = Library::new("libEGL.so.1")?;
let egl = ffi::egl::Egl::load_with(|sym| {
let sym = CString::new(sym).unwrap();
unsafe { &*lib.get(sym.as_bytes()).unwrap() as *const _ }
});
let name = CString::new(sym).unwrap();
let symbol = lib.get::<*mut c_void>(name.as_bytes());
match symbol {
Ok(x) => *x as *const _,
Err(_) => ptr::null(),
}
});
// If no version is given, try OpenGLES 3.0, if available,
// fallback to 2.0 otherwise
@ -119,28 +149,28 @@ impl EGLContext {
Some((3, x)) => (3, x),
Some((2, x)) => (2, x),
None => {
attributes.version = Some((3,0));
attributes.version = Some((3, 0));
match EGLContext::new(native, attributes, reqs) {
Ok(x) => return Ok(x),
Err(_) => {
//TODO log
attributes.version = Some((2,0));
// TODO log
attributes.version = Some((2, 0));
return EGLContext::new(native, attributes, reqs);
}
}
},
Some((1,x)) => {
//TODO logging + error, 1.0 not supported
}
Some((1, _)) => {
// TODO logging + error, 1.0 not supported
unimplemented!()
},
}
Some(_) => {
//TODO logging + error, version not supported
// TODO logging + error, version not supported
unimplemented!()
}
};
// the first step is to query the list of extensions without any display, if supported
let dp_extensions = unsafe {
let dp_extensions = {
let p = egl.QueryString(ffi::egl::NO_DISPLAY, ffi::egl::EXTENSIONS as i32);
// this possibility is available only with EGL 1.5 or EGL_EXT_platform_base, otherwise
@ -149,68 +179,59 @@ impl EGLContext {
vec![]
} else {
let p = CStr::from_ptr(p);
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| String::new());
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
}
};
let has_dp_extension = |e: &str| dp_extensions.iter().find(|s| s == &e).is_some();
let has_dp_extension = |e: &str| dp_extensions.iter().any(|s| s == e);
let display = match native {
Native::X11(display, _) if has_dp_extension("EGL_KHR_platform_x11") &&
egl.GetPlatformDisplay.is_loaded() =>
{
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_X11_KHR, display as *mut _,
ptr::null()) }
},
egl.GetPlatformDisplay.is_loaded() => {
egl.GetPlatformDisplay(ffi::egl::PLATFORM_X11_KHR, display as *mut _, ptr::null())
}
Native::X11(display, _) if has_dp_extension("EGL_EXT_platform_x11") &&
egl.GetPlatformDisplayEXT.is_loaded() =>
{
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_X11_EXT, display as *mut _,
ptr::null()) }
},
egl.GetPlatformDisplayEXT.is_loaded() => {
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_X11_EXT, display as *mut _, ptr::null())
}
Native::Gbm(display, _) if has_dp_extension("EGL_KHR_platform_gbm") &&
egl.GetPlatformDisplay.is_loaded() =>
{
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_GBM_KHR, display as *mut _,
ptr::null()) }
},
egl.GetPlatformDisplay.is_loaded() => {
egl.GetPlatformDisplay(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null())
}
Native::Gbm(display, _) if has_dp_extension("EGL_MESA_platform_gbm") &&
egl.GetPlatformDisplayEXT.is_loaded() =>
{
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_GBM_KHR, display as *mut _,
ptr::null()) }
},
egl.GetPlatformDisplayEXT.is_loaded() => {
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null())
}
Native::Wayland(display, _) if has_dp_extension("EGL_KHR_platform_wayland") &&
egl.GetPlatformDisplay.is_loaded() =>
{
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_WAYLAND_KHR, display as *mut _,
ptr::null()) }
},
egl.GetPlatformDisplay.is_loaded() => {
egl.GetPlatformDisplay(ffi::egl::PLATFORM_WAYLAND_KHR,
display as *mut _,
ptr::null())
}
Native::Wayland(display, _) if has_dp_extension("EGL_EXT_platform_wayland") &&
egl.GetPlatformDisplayEXT.is_loaded() =>
{
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_WAYLAND_EXT, display as *mut _,
ptr::null()) }
},
Native::X11(display, _) | Native::Gbm(display, _) |
Native::Wayland(display, _) => {
unsafe { egl.GetDisplay(display as *mut _) }
egl.GetPlatformDisplayEXT.is_loaded() => {
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_WAYLAND_EXT,
display as *mut _,
ptr::null())
}
Native::X11(display, _) |
Native::Gbm(display, _) |
Native::Wayland(display, _) => egl.GetDisplay(display as *mut _),
};
let egl_version = unsafe {
let egl_version = {
let mut major: ffi::egl::types::EGLint = mem::uninitialized();
let mut minor: ffi::egl::types::EGLint = mem::uninitialized();
if egl.Initialize(display, &mut major, &mut minor) == 0 {
return Err(CreationError::OsError(format!("eglInitialize failed")))
return Err(CreationError::OsError(String::from("eglInitialize failed")));
}
(major, minor)
@ -219,18 +240,16 @@ impl EGLContext {
// the list of extensions supported by the client once initialized is different from the
// list of extensions obtained earlier
let extensions = if egl_version >= (1, 2) {
let p = unsafe { CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32)) };
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
let p = CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32));
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| String::new());
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
} else {
vec![]
};
if egl_version >= (1, 2) {
if egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
return Err(CreationError::OpenGlVersionNotSupported);
}
if egl_version >= (1, 2) && egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
return Err(CreationError::OpenGlVersionNotSupported);
}
let descriptor = {
@ -248,29 +267,33 @@ impl EGLContext {
match version {
(3, _) => {
if egl_version < (1, 3) { return Err(CreationError::NoAvailablePixelFormat); }
if egl_version < (1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
out.push(ffi::egl::RENDERABLE_TYPE as c_int);
out.push(ffi::egl::OPENGL_ES3_BIT as c_int);
out.push(ffi::egl::CONFORMANT as c_int);
out.push(ffi::egl::OPENGL_ES3_BIT as c_int);
},
}
(2, _) => {
if egl_version < (1, 3) { return Err(CreationError::NoAvailablePixelFormat); }
if egl_version < (1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
out.push(ffi::egl::RENDERABLE_TYPE as c_int);
out.push(ffi::egl::OPENGL_ES2_BIT as c_int);
out.push(ffi::egl::CONFORMANT as c_int);
out.push(ffi::egl::OPENGL_ES2_BIT as c_int);
},
}
(_, _) => unreachable!(),
};
if let Some(hardware_accelerated) = reqs.hardware_accelerated {
out.push(ffi::egl::CONFIG_CAVEAT as c_int);
out.push(if hardware_accelerated {
ffi::egl::NONE as c_int
} else {
ffi::egl::SLOW_CONFIG as c_int
});
ffi::egl::NONE as c_int
} else {
ffi::egl::SLOW_CONFIG as c_int
});
}
if let Some(color) = reqs.color_bits {
@ -317,8 +340,12 @@ impl EGLContext {
// calling `eglChooseConfig`
let mut config_id = mem::uninitialized();
let mut num_configs = mem::uninitialized();
if egl.ChooseConfig(display, descriptor.as_ptr(), &mut config_id, 1, &mut num_configs) == 0 {
return Err(CreationError::OsError(format!("eglChooseConfig failed")));
if egl.ChooseConfig(display,
descriptor.as_ptr(),
&mut config_id,
1,
&mut num_configs) == 0 {
return Err(CreationError::OsError(String::from("eglChooseConfig failed")));
}
if num_configs == 0 {
return Err(CreationError::NoAvailablePixelFormat);
@ -332,7 +359,7 @@ impl EGLContext {
let res = $egl.GetConfigAttrib($display, $config,
$attr as ffi::egl::types::EGLint, &mut value);
if res == 0 {
return Err(CreationError::OsError(format!("eglGetConfigAttrib failed")));
return Err(CreationError::OsError(String::from("eglGetConfigAttrib failed")));
}
value
}
@ -340,8 +367,8 @@ impl EGLContext {
};
let desc = PixelFormat {
hardware_accelerated: attrib!(egl, display, config_id, ffi::egl::CONFIG_CAVEAT)
!= ffi::egl::SLOW_CONFIG as i32,
hardware_accelerated: attrib!(egl, display, config_id, ffi::egl::CONFIG_CAVEAT) !=
ffi::egl::SLOW_CONFIG as i32,
color_bits: attrib!(egl, display, config_id, ffi::egl::RED_SIZE) as u8 +
attrib!(egl, display, config_id, ffi::egl::BLUE_SIZE) as u8 +
attrib!(egl, display, config_id, ffi::egl::GREEN_SIZE) as u8,
@ -354,18 +381,17 @@ impl EGLContext {
0 | 1 => None,
a => Some(a as u16),
},
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
};
let surface = unsafe {
let surface = {
let surface = match native {
Native::X11(_, window) => egl.CreateWindowSurface(display, config_id, window, ptr::null()),
Native::Wayland(_, window) => egl.CreateWindowSurface(display, config_id, window, ptr::null()),
Native::Gbm(_, window) => egl.CreateWindowSurface(display, config_id, window, ptr::null()),
Native::X11(_, window) | Native::Wayland(_, window) | Native::Gbm(_, window) =>
egl.CreateWindowSurface(display, config_id, window, ptr::null()),
};
if surface.is_null() {
return Err(CreationError::OsError(format!("eglCreateWindowSurface failed")))
return Err(CreationError::OsError(String::from("eglCreateWindowSurface failed")));
}
surface
};
@ -373,9 +399,10 @@ impl EGLContext {
let mut context_attributes = Vec::with_capacity(10);
let mut flags = 0;
if egl_version >= (1, 5) || extensions.iter().find(|s| s == &"EGL_KHR_create_context")
.is_some()
{
if egl_version >= (1, 5) ||
extensions
.iter()
.any(|s| s == &"EGL_KHR_create_context") {
context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32);
context_attributes.push(version.0 as i32);
context_attributes.push(ffi::egl::CONTEXT_MINOR_VERSION as i32);
@ -383,72 +410,66 @@ impl EGLContext {
// handling robustness
let supports_robustness = egl_version >= (1, 5) ||
extensions.iter()
.find(|s| s == &"EGL_EXT_create_context_robustness")
.is_some();
extensions
.iter()
.any(|s| s == "EGL_EXT_create_context_robustness");
match attributes.robustness {
Robustness::NotRobust => (),
Robustness::NoError => {
if extensions.iter().find(|s| s == &"EGL_KHR_create_context_no_error").is_some() {
if extensions
.iter()
.any(|s| s == "EGL_KHR_create_context_no_error") {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_NO_ERROR_KHR as c_int);
context_attributes.push(1);
}
},
}
Robustness::RobustNoResetNotification => {
if supports_robustness {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
as c_int);
context_attributes
.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as c_int);
context_attributes.push(ffi::egl::NO_RESET_NOTIFICATION as c_int);
flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
flags |= ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
} else {
return Err(CreationError::RobustnessNotSupported);
}
},
}
Robustness::TryRobustNoResetNotification => {
if supports_robustness {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
as c_int);
context_attributes
.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as c_int);
context_attributes.push(ffi::egl::NO_RESET_NOTIFICATION as c_int);
flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
flags |= ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
}
},
}
Robustness::RobustLoseContextOnReset => {
if supports_robustness {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
as c_int);
context_attributes
.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as c_int);
context_attributes.push(ffi::egl::LOSE_CONTEXT_ON_RESET as c_int);
flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
flags |= ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
} else {
return Err(CreationError::RobustnessNotSupported);
}
},
}
Robustness::TryRobustLoseContextOnReset => {
if supports_robustness {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
as c_int);
context_attributes
.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as c_int);
context_attributes.push(ffi::egl::LOSE_CONTEXT_ON_RESET as c_int);
flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
flags |= ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
}
},
}
}
if attributes.debug {
if egl_version >= (1, 5) {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_DEBUG as i32);
context_attributes.push(ffi::egl::TRUE as i32);
}
// TODO: using this flag sometimes generates an error
// there was a change in the specs that added this flag, so it may not be
// supported everywhere ; however it is not possible to know whether it is
// supported or not
//flags = flags | ffi::egl::CONTEXT_OPENGL_DEBUG_BIT_KHR as i32;
if attributes.debug && egl_version >= (1, 5) {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_DEBUG as i32);
context_attributes.push(ffi::egl::TRUE as i32);
}
context_attributes.push(ffi::egl::CONTEXT_FLAGS_KHR as i32);
@ -457,10 +478,11 @@ impl EGLContext {
} else if egl_version >= (1, 3) {
// robustness is not supported
match attributes.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
Robustness::RobustNoResetNotification |
Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported);
},
_ => ()
}
_ => (),
}
context_attributes.push(ffi::egl::CONTEXT_CLIENT_VERSION as i32);
@ -469,8 +491,7 @@ impl EGLContext {
context_attributes.push(ffi::egl::NONE as i32);
let context = egl.CreateContext(display, config_id, ptr::null(),
context_attributes.as_ptr());
let context = egl.CreateContext(display, config_id, ptr::null(), context_attributes.as_ptr());
if context.is_null() {
match egl.GetError() as u32 {
@ -480,57 +501,73 @@ impl EGLContext {
}
Ok(EGLContext {
context: context as *const _,
display: display as *const _,
egl: egl,
surface: surface as *const _,
pixel_format: desc,
})
context: context as *const _,
display: display as *const _,
egl: egl,
surface: surface as *const _,
pixel_format: desc,
})
}
/// Swaps buffers at the end of a frame.
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
let ret = unsafe {
self.egl.SwapBuffers(self.display as *const _, self.surface as *const _)
self.egl
.SwapBuffers(self.display as *const _, self.surface as *const _)
};
if ret == 0 {
match unsafe { self.egl.GetError() } as u32 {
ffi::egl::CONTEXT_LOST => return Err(SwapBuffersError::ContextLost),
err => panic!("eglSwapBuffers failed (eglGetError returned 0x{:x})", err)
ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost),
err => panic!("eglSwapBuffers failed (eglGetError returned 0x{:x})", err),
}
} else {
Ok(())
}
}
/// Returns the address of an OpenGL function.
///
/// Supposes that the context has been made current before this function is called.
pub unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
let addr = CString::new(symbol.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe {
self.egl.GetProcAddress(addr) as *const _
}
self.egl.GetProcAddress(addr) as *const _
}
/// Returns true if the OpenGL context is the current one in the thread.
pub fn is_current(&self) -> bool {
unsafe { self.egl.GetCurrentContext() == self.context as *const _ }
}
/// Makes the OpenGL context the current context in the current thread.
///
/// # Unsafety
///
/// This function is marked unsafe, because the context cannot be made current
/// on multiple threads.
pub unsafe fn make_current(&self) -> Result<(), SwapBuffersError> {
let ret = self.egl.MakeCurrent(self.display as *const _, self.surface as *const _, self.surface as *const _, self.context as *const _);
let ret = self.egl
.MakeCurrent(self.display as *const _,
self.surface as *const _,
self.surface as *const _,
self.context as *const _);
if ret == 0 {
match self.egl.GetError() as u32 {
ffi::egl::CONTEXT_LOST => return Err(SwapBuffersError::ContextLost),
err => panic!("eglMakeCurrent failed (eglGetError returned 0x{:x})", err)
ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost),
err => panic!("eglMakeCurrent failed (eglGetError returned 0x{:x})", err),
}
} else {
Ok(())
}
}
/// Returns the pixel format of the main framebuffer of the context.
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format
}}
}
}
unsafe impl Send for EGLContext {}
unsafe impl Sync for EGLContext {}
@ -540,8 +577,10 @@ impl Drop for EGLContext {
unsafe {
// we don't call MakeCurrent(0, 0) because we are not sure that the context
// is still the current one
self.egl.DestroyContext(self.display as *const _, self.context as *const _);
self.egl.DestroySurface(self.display as *const _, self.surface as *const _);
self.egl
.DestroyContext(self.display as *const _, self.context as *const _);
self.egl
.DestroySurface(self.display as *const _, self.surface as *const _);
self.egl.Terminate(self.display as *const _);
}
}
@ -569,41 +608,86 @@ pub enum SwapBuffersError {
AlreadySwapped,
}
/// Attributes to use when creating an OpenGL context.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlAttributes {
/// Describes the OpenGL API and version that are being requested when a context is created.
///
/// `Some(3, 0)` will request a OpenGL ES 3.0 context for example.
/// `None` means "don't care" (minimum will be 2.0).
pub version: Option<(u8, u8)>,
/// OpenGL profile to use
pub profile: Option<GlProfile>,
/// Whether to enable the debug flag of the context.
///
/// Debug contexts are usually slower but give better error reporting.
pub debug: bool,
/// How the OpenGL context should detect errors.
pub robustness: Robustness,
/// Whether to use vsync. If vsync is enabled, calling swap_buffers will block until the screen refreshes.
/// This is typically used to prevent screen tearing.
pub vsync: bool,
}
/// Specifies the tolerance of the OpenGL context to faults. If you accept raw OpenGL commands and/or raw
/// shader code from an untrusted source, you should definitely care about this.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Robustness {
/// Not everything is checked. Your application can crash if you do something wrong with your shaders.
NotRobust,
/// The driver doesn't check anything. This option is very dangerous. Please know what you're doing before
/// using it. See the GL_KHR_no_error extension.
///
/// Since this option is purely an optimisation, no error will be returned if the backend doesn't support it.
/// Instead it will automatically fall back to NotRobust.
NoError,
/// Everything is checked to avoid any crash. The driver will attempt to avoid any problem, but if a problem occurs
/// the behavior is implementation-defined. You are just guaranteed not to get a crash.
RobustNoResetNotification,
/// Same as RobustNoResetNotification but the context creation doesn't fail if it's not supported.
TryRobustNoResetNotification,
/// Everything is checked to avoid any crash. If a problem occurs, the context will enter a "context lost" state.
/// It must then be recreated.
RobustLoseContextOnReset,
/// Same as RobustLoseContextOnReset but the context creation doesn't fail if it's not supported.
TryRobustLoseContextOnReset,
}
/// Describes the requested OpenGL context profiles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlProfile {
/// Include all the immediate more functions and definitions.
Compatibility,
/// Include all the future-compatible functions and definitions.
Core,
}
/// Describes how the backend should choose a pixel format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct PixelFormatRequirements {
/// If `true`, only hardware-accelerated formats will be conisdered. If `false`, only software renderers.
/// `None` means "don't care". Default is `None`.
pub hardware_accelerated: Option<bool>,
/// Minimum number of bits for the color buffer, excluding alpha. None means "don't care". The default is `None``.
pub color_bits: Option<u8>,
/// If `true`, the color buffer must be in a floating point format. Default is `false`.
///
/// Using floating points allows you to write values outside of the `[0.0, 1.0]` range.
pub float_color_buffer: bool,
/// Minimum number of bits for the alpha in the color buffer. `None` means "don't care". The default is `None`.
pub alpha_bits: Option<u8>,
/// Minimum number of bits for the depth buffer. `None` means "don't care". The default value is `None`.
pub depth_bits: Option<u8>,
/// Minimum number of bits for the depth buffer. `None` means "don't care". The default value is `None`.
pub stencil_bits: Option<u8>,
/// If `true`, only double-buffered formats will be considered. If `false`, only single-buffer formats.
/// `None` means "don't care". The default is `None`.
pub double_buffer: Option<bool>,
/// Contains the minimum number of samples per pixel in the color, depth and stencil buffers.
/// `None` means "don't care". Default is `None`. A value of `Some(0)` indicates that multisampling must not be enabled.
pub multisampling: Option<u16>,
/// If `true`, only stereoscopic formats will be considered. If `false`, only non-stereoscopic formats.
/// The default is `false`.
pub stereoscopy: bool,
}
@ -653,6 +737,8 @@ pub trait EGLGraphicsBackend: GraphicsBackend {
/// Makes the OpenGL context the current context in the current thread.
///
/// # Unsafety
///
/// This function is marked unsafe, because the context cannot be made current
/// on multiple threads.
unsafe fn make_current(&self) -> Result<(), SwapBuffersError>;

View File

@ -328,8 +328,8 @@ impl backend::InputBackend for LibinputInputBackend {
Entry::Vacant(seat_entry) => {
let mut hasher = DefaultHasher::default();
seat_entry.key().hash(&mut hasher);
let seat = seat_entry.insert(backend::Seat::new(hasher.finish(),
new_caps));
let seat = seat_entry
.insert(backend::Seat::new(hasher.finish(), new_caps));
if let Some(ref mut handler) = self.handler {
trace!(self.logger, "Calling on_seat_created with {:?}", seat);
handler.on_seat_created(seat);

View File

@ -1,19 +1,24 @@
//! Implementation of backend traits for types provided by `winit`
use backend::{SeatInternal, TouchSlotInternal};
use backend::graphics::GraphicsBackend;
use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, Native, Robustness, PixelFormatRequirements, PixelFormat, SwapBuffersError};
use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, Native,
PixelFormat, PixelFormatRequirements, Robustness, SwapBuffersError};
use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState,
KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent,
PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent,
TouchMotionEvent, TouchSlot, TouchUpEvent, UnusedEvent};
use nix::c_void;
use wayland_client::egl as wegl;
use winit::{CreationError as WinitCreationError, ElementState, Event, EventsLoop, MouseButton as WinitMouseButton, MouseCursor, MouseScrollDelta, Touch, TouchPhase, Window, WindowBuilder, WindowEvent};
use winit::os::unix::WindowExt;
use std::cmp;
use std::error::Error;
use std::fmt;
use std::rc::Rc;
use wayland_client::egl as wegl;
use winit::{CreationError as WinitCreationError, ElementState, Event, EventsLoop,
MouseButton as WinitMouseButton, MouseCursor, MouseScrollDelta, Touch, TouchPhase, Window,
WindowBuilder, WindowEvent};
use winit::os::unix::{get_x11_xconnection, WindowExt};
/// Window with an active EGL Context created by `winit`. Implements the
/// `EGLGraphicsBackend` graphics backend trait
@ -24,7 +29,7 @@ pub struct WinitGraphicsBackend {
/// Abstracted event loop of a `winit` `Window` implementing the `InputBackend` trait
///
/// You need to call `process_new_events` periodically to receive any events.
/// You need to call `dispatch_new_events` periodically to receive any events.
pub struct WinitInputBackend {
events_loop: EventsLoop,
window: Rc<Window>,
@ -37,67 +42,78 @@ pub struct WinitInputBackend {
}
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
/// graphics backend trait
/// graphics backend trait and a corresponding `WinitInputBackend`, which implements
/// the `InputBackend` trait
pub fn init() -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
init_from_builder(WindowBuilder::new())
}
pub fn init_from_builder(builder: WindowBuilder) -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
init_from_builder_with_gl_attr(builder, GlAttributes {
version: None,
profile: None,
debug: cfg!(debug_assertions),
robustness: Robustness::TryRobustLoseContextOnReset,
vsync: true,
})
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
/// graphics backend trait, from a given `WindowBuilder` struct and a corresponding
/// `WinitInputBackend`, which implements the `InputBackend` trait
pub fn init_from_builder(builder: WindowBuilder)
-> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
init_from_builder_with_gl_attr(builder,
GlAttributes {
version: None,
profile: None,
debug: cfg!(debug_assertions),
robustness: Robustness::TryRobustLoseContextOnReset,
vsync: true,
})
}
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
/// graphics backend trait, with a given already configured `WindowBuilder` for
/// customization.
pub fn init_from_builder_with_gl_attr(builder: WindowBuilder, attributes: GlAttributes) -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
/// graphics backend trait, from a given `WindowBuilder` struct, as well as given
/// `GlAttributes` for further customization of the rendering pipeline and a
/// corresponding `WinitInputBackend`, which implements the `InputBackend` trait.
pub fn init_from_builder_with_gl_attr(builder: WindowBuilder, attributes: GlAttributes)
-> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
let events_loop = EventsLoop::new();
let window = Rc::new(builder.build(&events_loop)?);
let (native, surface) = if let (Some(display), Some(window)) = (window.get_xlib_display(), window.get_xlib_window()) {
(Native::X11(display, window), None)
} else if let (Some(display), Some(surface)) = (window.get_wayland_display(), window.get_wayland_client_surface()) {
let (native, surface) = if let (Some(conn), Some(window)) =
(get_x11_xconnection(), window.get_xlib_window()) {
(Native::X11(conn.display as *const _, window), None)
} else if let (Some(display), Some(surface)) =
(window.get_wayland_display(), window.get_wayland_client_surface()) {
let (w, h) = window.get_inner_size().unwrap();
let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32);
(Native::Wayland(display, egl_surface.ptr() as *const _), Some(egl_surface))
} else {
return Err(CreationError::NotSupported)
return Err(CreationError::NotSupported);
};
let context = unsafe { EGLContext::new(native, attributes,
PixelFormatRequirements {
hardware_accelerated: Some(true),
color_bits: Some(24),
alpha_bits: Some(8),
double_buffer: Some(true),
..Default::default()
}
)? };
let context = unsafe {
EGLContext::new(native,
attributes,
PixelFormatRequirements {
hardware_accelerated: Some(true),
color_bits: Some(24),
alpha_bits: Some(8),
..Default::default()
})?
};
Ok((WinitGraphicsBackend {
window: window.clone(),
context: context,
}, WinitInputBackend {
events_loop: events_loop,
window: window,
surface: surface,
time_counter: 0,
key_counter: 0,
seat: Seat::new(0,
SeatCapabilities {
pointer: true,
keyboard: true,
touch: true,
}
),
input_config: (),
handler: None,
}))
window: window.clone(),
context: context,
},
WinitInputBackend {
events_loop: events_loop,
window: window,
surface: surface,
time_counter: 0,
key_counter: 0,
seat: Seat::new(0,
SeatCapabilities {
pointer: true,
keyboard: true,
touch: true,
}),
input_config: (),
handler: None,
}))
}
impl GraphicsBackend for WinitGraphicsBackend {
@ -122,7 +138,9 @@ impl EGLGraphicsBackend for WinitGraphicsBackend {
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
self.window.get_inner_size_pixels().expect("Window does not exist anymore")
self.window
.get_inner_size_pixels()
.expect("Window does not exist anymore")
}
fn is_current(&self) -> bool {
@ -143,7 +161,7 @@ impl EGLGraphicsBackend for WinitGraphicsBackend {
pub enum WinitInputError {
/// The underlying `winit` `Window` was closed. No further events can be processed.
///
/// See `WinitInputBackend::process_new_events`.
/// See `WinitInputBackend::dispatch_new_events`.
WindowClosed,
}
@ -215,19 +233,13 @@ impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent {
fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.x * width as i32 /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0 as i32,
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as i32,
0) as u32
}
fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.y * height as i32 /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1 as i32,
self.window.get_inner_size_points().unwrap_or((0, height)).1 as i32,
0) as u32
}
}
@ -322,19 +334,13 @@ impl TouchDownEvent for WinitTouchStartedEvent {
fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.location.0 as i32 * width as i32 /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0 as i32,
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as i32,
0) as u32
}
fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.location.1 as i32 * height as i32 /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1 as i32,
self.window.get_inner_size_points().unwrap_or((0, height)).1 as i32,
0) as u32
}
}
@ -368,19 +374,11 @@ impl TouchMotionEvent for WinitTouchMovedEvent {
}
fn x_transformed(&self, width: u32) -> u32 {
self.location.0 as u32 * width /
self.window
.get_inner_size_points()
.unwrap_or((width, 0))
.0
self.location.0 as u32 * width / self.window.get_inner_size_points().unwrap_or((width, 0)).0
}
fn y_transformed(&self, height: u32) -> u32 {
self.location.1 as u32 * height /
self.window
.get_inner_size_points()
.unwrap_or((0, height))
.1
self.location.1 as u32 * height / self.window.get_inner_size_points().unwrap_or((0, height)).1
}
}
@ -468,7 +466,7 @@ impl InputBackend for WinitInputBackend {
/// input events will be received by a set `InputHandler`.
///
/// Returns an error if the `Window` the window has been closed. Calling
/// `process_new_events` again after the `Window` has been closed is considered an
/// `dispatch_new_events` again after the `Window` has been closed is considered an
/// application error and unspecified baviour may occur.
///
/// The linked `WinitGraphicsBackend` will error with a lost Context and should
@ -485,127 +483,126 @@ impl InputBackend for WinitInputBackend {
let surface = &self.surface;
let mut handler = self.handler.as_mut();
self.events_loop.poll_events(move |event| {
if let Some(ref mut handler) = handler {
let Event::WindowEvent{ event, window_id: _ } = event;
match event {
WindowEvent::Resized(x, y) => {
window.set_inner_size(x, y);
if let Some(wl_surface) = surface.as_ref() {
wl_surface.resize(x as i32, y as i32, 0, 0);
}
}
WindowEvent::KeyboardInput(state, key_code, _, _) => {
match state {
ElementState::Pressed => *key_counter += 1,
ElementState::Released => {
*key_counter = key_counter.checked_sub(1).unwrap_or(0)
}
};
handler.on_keyboard_key(seat,
WinitKeyboardInputEvent {
time: *time_counter,
key: key_code,
count: *key_counter,
state: state,
})
}
WindowEvent::MouseMoved(x, y) => {
handler.on_pointer_move_absolute(seat,
WinitMouseMovedEvent {
window: window.clone(),
self.events_loop
.poll_events(move |event| if let Some(ref mut handler) = handler {
let Event::WindowEvent { event, .. } = event;
match event {
WindowEvent::Resized(x, y) => {
window.set_inner_size(x, y);
if let Some(wl_surface) = surface.as_ref() {
wl_surface.resize(x as i32, y as i32, 0, 0);
}
}
WindowEvent::KeyboardInput(state, key_code, _, _) => {
match state {
ElementState::Pressed => *key_counter += 1,
ElementState::Released => {
*key_counter = key_counter.checked_sub(1).unwrap_or(0)
}
};
handler.on_keyboard_key(seat,
WinitKeyboardInputEvent {
time: *time_counter,
key: key_code,
count: *key_counter,
state: state,
})
}
WindowEvent::MouseMoved(x, y) => {
handler.on_pointer_move_absolute(seat,
WinitMouseMovedEvent {
window: window.clone(),
time: *time_counter,
x: x,
y: y,
})
}
WindowEvent::MouseWheel(delta, _) => {
let event = WinitMouseWheelEvent {
axis: Axis::Horizontal,
time: *time_counter,
delta: delta,
};
match delta {
MouseScrollDelta::LineDelta(x, y) |
MouseScrollDelta::PixelDelta(x, y) => {
if x != 0.0 {
handler.on_pointer_axis(seat, event);
}
if y != 0.0 {
handler.on_pointer_axis(seat, event);
}
}
}
}
WindowEvent::MouseInput(state, button) => {
handler.on_pointer_button(seat,
WinitMouseInputEvent {
time: *time_counter,
button: button,
state: state,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Started,
location: (x, y),
id,
}) => {
handler.on_touch_down(seat,
WinitTouchStartedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Moved,
location: (x, y),
id,
}) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Ended,
location: (x, y),
id,
}) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
});
handler.on_touch_up(seat,
WinitTouchEndedEvent {
time: *time_counter,
x: x,
y: y,
})
}
WindowEvent::MouseWheel(delta, _) => {
let event = WinitMouseWheelEvent {
axis: Axis::Horizontal,
time: *time_counter,
delta: delta,
};
match delta {
MouseScrollDelta::LineDelta(x, y) |
MouseScrollDelta::PixelDelta(x, y) => {
if x != 0.0 {
handler.on_pointer_axis(seat, event.clone());
}
if y != 0.0 {
handler.on_pointer_axis(seat, event);
}
}
}
}
WindowEvent::MouseInput(state, button) => {
handler.on_pointer_button(seat,
WinitMouseInputEvent {
time: *time_counter,
button: button,
state: state,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Started,
location: (x, y),
id,
}) => {
handler.on_touch_down(seat,
WinitTouchStartedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Moved,
location: (x, y),
id,
}) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
})
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Ended,
location: (x, y),
id,
}) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
time: *time_counter,
location: (x, y),
id: id,
});
handler.on_touch_up(seat,
WinitTouchEndedEvent {
time: *time_counter,
id: id,
});
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Cancelled,
id,
..
}) => {
handler.on_touch_cancel(seat,
WinitTouchCancelledEvent {
time: *time_counter,
id: id,
})
}
WindowEvent::Closed => *closed_ptr = true,
_ => {}
}
*time_counter += 1;
}
});
id: id,
});
}
WindowEvent::Touch(Touch {
phase: TouchPhase::Cancelled,
id,
..
}) => {
handler.on_touch_cancel(seat,
WinitTouchCancelledEvent {
time: *time_counter,
id: id,
})
}
WindowEvent::Closed => *closed_ptr = true,
_ => {}
}
*time_counter += 1;
});
}
if closed {

View File

@ -143,7 +143,7 @@ impl KbdInternal {
let mods_locked = self.state.serialize_mods(xkb::STATE_MODS_LOCKED);
let layout_locked = self.state.serialize_layout(xkb::STATE_LAYOUT_LOCKED);
return (mods_depressed, mods_latched, mods_locked, layout_locked);
(mods_depressed, mods_latched, mods_locked, layout_locked)
}
fn serialize_pressed_keys(&self) -> Vec<u8> {