Skip to content

Commit

Permalink
Implement Window's GL context for Glutin
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyusa authored and ferjm committed Jul 4, 2019
1 parent 9f4f9dc commit e0a5abf
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 9 deletions.
60 changes: 59 additions & 1 deletion ports/glutin/context.rs
Expand Up @@ -2,7 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use glutin::{WindowedContext, NotCurrent, PossiblyCurrent};
use glutin::os::ContextTraitExt;
use glutin::{NotCurrent, PossiblyCurrent, WindowedContext};
use servo_media::player::context::GlContext as RawContext;
use std::os::raw;

pub enum GlContext {
Current(WindowedContext<PossiblyCurrent>),
Expand Down Expand Up @@ -71,4 +74,59 @@ impl GlContext {
GlContext::None => unreachable!(),
};
}
pub fn raw_context(&self) -> RawContext {
match self {
GlContext::Current(c) => {
let raw_handle = unsafe { c.raw_handle() };

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
use glutin::os::unix::RawHandle;

return match raw_handle {
RawHandle::Egl(handle) => RawContext::Egl(handle as usize),
RawHandle::Glx(handle) => RawContext::Glx(handle as usize),
};
}

#[cfg(not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
unimplemented!()
}
GlContext::NotCurrent(_) => {
error!("Context is not current.");
RawContext::Unknown
}
GlContext::None => unreachable!(),
}
}
pub fn egl_display(&self) -> Option<*const raw::c_void> {
match self {
GlContext::Current(c) => unsafe { c.get_egl_display() },
GlContext::NotCurrent(_) => {
error!("Context is not current.");
None
},
GlContext::None => unreachable!(),
}
}

pub fn get_api(&self) -> glutin::Api {
match self {
GlContext::Current(c) => c.get_api(),
GlContext::NotCurrent(c) => c.get_api(),
GlContext::None => unreachable!(),
}
}
}
68 changes: 60 additions & 8 deletions ports/glutin/headed_window.rs
Expand Up @@ -13,9 +13,9 @@ use gleam::gl;
use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")]
use glutin::os::macos::{ActivationPolicy, WindowBuilderExt};
use glutin::Api;
#[cfg(any(target_os = "linux", target_os = "windows"))]
use glutin::Icon;
use glutin::Api;
use glutin::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use image;
Expand All @@ -30,7 +30,7 @@ use servo::style_traits::DevicePixel;
use servo::webrender_api::{
DeviceIntPoint, DeviceIntRect, DeviceIntSize, FramebufferIntSize, ScrollLocation,
};
use servo_media::player::context as MediaPlayerCtxt;
use servo_media::player::context::{GlApi, GlContext as PlayerGLContext, NativeDisplay};
use std::cell::{Cell, RefCell};
use std::mem;
use std::rc::Rc;
Expand Down Expand Up @@ -526,16 +526,68 @@ impl WindowMethods for Window {
self.gl_context.borrow_mut().make_current();
}

fn get_gl_context(&self) -> MediaPlayerCtxt::GlContext {
MediaPlayerCtxt::GlContext::Unknown
fn get_gl_context(&self) -> PlayerGLContext {
self.gl_context.borrow().raw_context()
}

fn get_native_display(&self) -> MediaPlayerCtxt::NativeDisplay {
MediaPlayerCtxt::NativeDisplay::Unknown
fn get_native_display(&self) -> NativeDisplay {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
let native_display = {
if let Some(display) = self.gl_context.borrow().egl_display() {
NativeDisplay::Egl(display as usize)
} else {
use glutin::os::unix::WindowExt;

if let Some(display) = self.gl_context.borrow().window().get_wayland_display() {
NativeDisplay::Wayland(display as usize)
} else if let Some(display) = self.gl_context.borrow().window().get_xlib_display() {
NativeDisplay::X11(display as usize)
} else {
NativeDisplay::Unknown
}
}
};

#[cfg(not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
let native_display = NativeDisplay::Unknown;

native_display
}

fn get_gl_api(&self) -> MediaPlayerCtxt::GlApi {
MediaPlayerCtxt::GlApi::None
fn get_gl_api(&self) -> GlApi {
let api = self.gl_context.borrow().get_api();

let version = self.gl.get_string(gl::VERSION);
let version = version.trim_start_matches("OpenGL ES ");
let mut values = version.split(&['.', ' '][..]);
let major = values
.next()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(1);
let minor = values
.next()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(20);

match api {
glutin::Api::OpenGl if major >= 3 && minor >= 2 => GlApi::OpenGL3,
glutin::Api::OpenGl => GlApi::OpenGL,
glutin::Api::OpenGlEs if major > 1 => GlApi::Gles2,
glutin::Api::OpenGlEs => GlApi::Gles1,
_ => GlApi::None,
}
}
}

Expand Down

0 comments on commit e0a5abf

Please sign in to comment.