Skip to content

Commit

Permalink
Make it possible to rotate in webxr glwindow view
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Oct 1, 2019
1 parent 42d64d0 commit e65f1c9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 50 additions & 20 deletions ports/glutin/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ use crate::embedder::EmbedderCallbacks;
use crate::window_trait::WindowPortsMethods;
use crate::events_loop::EventsLoop;
use crate::{headed_window, headless_window};
use glutin::WindowId;
use servo::compositing::windowing::WindowEvent;
use servo::config::opts::{self, parse_url_or_filename};
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use servo::{BrowserId, Servo};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::env;
use std::mem;
use std::rc::Rc;

thread_local! {
pub static WINDOWS: RefCell<HashMap<WindowId, Rc<dyn WindowPortsMethods>>> = RefCell::new(HashMap::new());
}

pub struct App {
events_loop: Rc<RefCell<EventsLoop>>,
window: Rc<dyn WindowPortsMethods>,
servo: RefCell<Servo<dyn WindowPortsMethods>>,
browser: RefCell<Browser<dyn WindowPortsMethods>>,
event_queue: RefCell<Vec<WindowEvent>>,
Expand Down Expand Up @@ -54,10 +59,11 @@ impl App {
servo.handle_events(vec![WindowEvent::NewBrowser(get_default_url(), browser_id)]);
servo.setup_logging();

register_window(window);

let app = App {
event_queue: RefCell::new(vec![]),
events_loop,
window: window,
browser: RefCell::new(browser),
servo: RefCell::new(servo),
suspended: Cell::new(false),
Expand Down Expand Up @@ -89,26 +95,35 @@ impl App {
glutin::Event::WindowEvent {
window_id, event, ..
} => {
if Some(window_id) != self.window.id() {
warn!("Got an event from unknown window");
} else {
// Resize events need to be handled during run_forever
let cont = if let glutin::WindowEvent::Resized(_) = event {
glutin::ControlFlow::Continue
} else {
glutin::ControlFlow::Break
};
self.window.winit_event_to_servo_event(event);
return cont;
}
return WINDOWS.with(|windows| {
match windows.borrow().get(&window_id) {
None => {
warn!("Got an event from unknown window");
glutin::ControlFlow::Break
},
Some(window) => {
// Resize events need to be handled during run_forever
let cont = if let glutin::WindowEvent::Resized(_) = event {
glutin::ControlFlow::Continue
} else {
glutin::ControlFlow::Break
};
window.winit_event_to_servo_event(event);
return cont;
}
}
});
},
}
glutin::ControlFlow::Break
}

fn run_loop(self) {
loop {
if !self.window.is_animating() || self.suspended.get() {
let animating = WINDOWS.with(|windows| {
windows.borrow().iter().any(|(_, window)| window.is_animating())
});
if !animating || self.suspended.get() {
// If there's no animations running then we block on the window event loop.
self.events_loop.borrow_mut().run_forever(|e| {
let cont = self.winit_event_to_servo_event(e);
Expand Down Expand Up @@ -141,18 +156,27 @@ impl App {
let mut browser = self.browser.borrow_mut();
let mut servo = self.servo.borrow_mut();

let win_events = self.window.get_events();
// FIXME:
// As of now, we support only one browser (self.browser)
// but have multiple windows (dom.webxr.glwindow). We forward
// the events of all the windows combined to that single
// browser instance. Pressing the "a" key on the glwindow
// will send a key event to the servo window.

let mut app_events = self.get_events();
WINDOWS.with(|windows| {
for (_win_id, window) in &*windows.borrow() {
app_events.extend(window.get_events());
}
});

// FIXME: this could be handled by Servo. We don't need
// a repaint_synchronously function exposed.
let need_resize = win_events.iter().any(|e| match *e {
let need_resize = app_events.iter().any(|e| match *e {
WindowEvent::Resize => true,
_ => false,
});

let mut app_events = self.get_events();
app_events.extend(win_events);

browser.handle_window_events(app_events);

let mut servo_events = servo.get_events();
Expand Down Expand Up @@ -189,6 +213,12 @@ fn get_default_url() -> ServoUrl {
cmdline_url.or(pref_url).or(blank_url).unwrap()
}

pub fn register_window(window: Rc<dyn WindowPortsMethods>) {
WINDOWS.with(|w| {
w.borrow_mut().insert(window.id(), window);
});
}

pub fn gl_version() -> glutin::GlRequest {
if opts::get().angle {
glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (3, 0))
Expand Down
56 changes: 44 additions & 12 deletions ports/glutin/headed_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use crate::context::GlContext;
use crate::events_loop::EventsLoop;
use crate::keyutils::keyboard_event_from_winit;
use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT};
use euclid::{default::Size2D as UntypedSize2D, Point2D, Scale, Size2D, Vector2D};
use euclid::{Angle, default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D};
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::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase};
use glutin::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use image;
use keyboard_types::{Key, KeyState, KeyboardEvent};
Expand Down Expand Up @@ -68,6 +68,7 @@ pub struct Window {
animation_state: Cell<AnimationState>,
fullscreen: Cell<bool>,
gl: Rc<dyn gl::Gl>,
xr_rotation: Cell<Rotation3D<f32, UnknownUnit, UnknownUnit>>,
}

#[cfg(not(target_os = "windows"))]
Expand Down Expand Up @@ -194,6 +195,7 @@ impl Window {
inner_size: Cell::new(inner_size),
primary_monitor,
screen_size,
xr_rotation: Cell::new(Rotation3D::identity()),
};

window.present();
Expand Down Expand Up @@ -235,12 +237,36 @@ impl Window {
self.last_pressed.set(Some(event));
} else if event.key != Key::Unidentified {
self.last_pressed.set(None);
self.handle_xr_rotation(&input);
self.event_queue
.borrow_mut()
.push(WindowEvent::Keyboard(event));
}
}

fn handle_xr_rotation(&self, input: &KeyboardInput) {
if input.state != glutin::ElementState::Pressed {
return;
}
let mut x = 0.0;
let mut y = 0.0;
match input.virtual_keycode {
Some(VirtualKeyCode::Up) => x = 1.0,
Some(VirtualKeyCode::Down) => x = -1.0,
Some(VirtualKeyCode::Left) => y = 1.0,
Some(VirtualKeyCode::Right) => y = -1.0,
_ => return,
};
if input.modifiers.shift {
x = 10.0 * x;
y = 10.0 * y;
}
let x: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_x(Angle::degrees(x));
let y: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_y(Angle::degrees(y));
let rotation = self.xr_rotation.get().post_rotate(&x).post_rotate(&y);
self.xr_rotation.set(rotation);
}

/// Helper function to handle a click
fn handle_mouse(
&self,
Expand Down Expand Up @@ -394,8 +420,8 @@ impl WindowPortsMethods for Window {
self.animation_state.get() == AnimationState::Animating
}

fn id(&self) -> Option<glutin::WindowId> {
Some(self.gl_context.borrow().window().id())
fn id(&self) -> glutin::WindowId {
self.gl_context.borrow().window().id()
}

fn winit_event_to_servo_event(&self, event: glutin::WindowEvent) {
Expand Down Expand Up @@ -486,15 +512,15 @@ impl WindowPortsMethods for Window {
}

impl webxr::glwindow::GlWindow for Window {
fn make_current(&mut self) {
fn make_current(&self) {
debug!("Making window {:?} current", self.gl_context.borrow().window().id());
self.gl_context.get_mut().make_current();
self.gl_context.borrow_mut().make_current();
}

fn swap_buffers(&mut self) {
fn swap_buffers(&self) {
debug!("Swapping buffers on window {:?}", self.gl_context.borrow().window().id());
self.gl_context.get_mut().swap_buffers();
self.gl_context.get_mut().make_not_current();
self.gl_context.borrow().swap_buffers();
self.gl_context.borrow_mut().make_not_current();
}

fn size(&self) -> UntypedSize2D<gl::GLsizei> {
Expand All @@ -508,12 +534,18 @@ impl webxr::glwindow::GlWindow for Window {
Size2D::new(width * dpr, height *dpr).to_i32()
}

fn new_window(&self) -> Result<Box<dyn webxr::glwindow::GlWindow>, ()> {
Ok(Box::new(Window::new(
fn get_rotation(&self) -> Rotation3D<f32, UnknownUnit, UnknownUnit> {
self.xr_rotation.get().clone()
}

fn new_window(&self) -> Result<Rc<dyn webxr::glwindow::GlWindow>, ()> {
let window = Rc::new(Window::new(
self.inner_size.get(),
Some(self),
self.events_loop.clone(),
)))
));
app::register_window(window.clone());
Ok(window)
}
}

Expand Down
19 changes: 12 additions & 7 deletions ports/glutin/headless_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::window_trait::WindowPortsMethods;
use glutin;
use euclid::{default::Size2D as UntypedSize2D, Point2D, Scale, Size2D};
use euclid::{default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit};
use gleam::gl;
use servo::compositing::windowing::{AnimationState, WindowEvent};
use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods};
Expand Down Expand Up @@ -132,8 +132,10 @@ impl WindowPortsMethods for Window {
false
}

fn id(&self) -> Option<glutin::WindowId> {
None
fn id(&self) -> glutin::WindowId {
unsafe {
glutin::WindowId::dummy()
}
}

fn page_height(&self) -> f32 {
Expand Down Expand Up @@ -217,23 +219,26 @@ impl WindowMethods for Window {
}

impl webxr::glwindow::GlWindow for Window {
fn make_current(&mut self) {}
fn swap_buffers(&mut self) {}
fn make_current(&self) {}
fn swap_buffers(&self) {}
fn size(&self) -> UntypedSize2D<gl::GLsizei> {
let dpr = self.servo_hidpi_factor().get();
Size2D::new((self.context.width as f32 * dpr) as gl::GLsizei, (self.context.height as f32 * dpr) as gl::GLsizei)
}
fn new_window(&self) -> Result<Box<dyn webxr::glwindow::GlWindow>, ()> {
fn new_window(&self) -> Result<Rc<dyn webxr::glwindow::GlWindow>, ()> {
let width = self.context.width;
let height = self.context.height;
let share = Some(&self.context);
let context = HeadlessContext::new(width, height, share);
let gl = self.gl.clone();
Ok(Box::new(Window {
Ok(Rc::new(Window {
context,
gl,
animation_state: Cell::new(AnimationState::Idle),
fullscreen: Cell::new(false),
}))
}
fn get_rotation(&self) -> Rotation3D<f32, UnknownUnit, UnknownUnit> {
Rotation3D::identity()
}
}
2 changes: 1 addition & 1 deletion ports/glutin/window_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const LINE_HEIGHT: f32 = 38.0;

pub trait WindowPortsMethods: WindowMethods + webxr::glwindow::GlWindow {
fn get_events(&self) -> Vec<WindowEvent>;
fn id(&self) -> Option<glutin::WindowId>;
fn id(&self) -> glutin::WindowId;
fn has_events(&self) -> bool;
fn page_height(&self) -> f32;
fn get_fullscreen(&self) -> bool;
Expand Down

0 comments on commit e65f1c9

Please sign in to comment.