1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

//! Implements the global methods required by Servo (not window/gl/compositor related).

use crate::app;
use crate::events_loop::EventsLoop;
use gleam::gl;
use glutin;
use glutin::dpi::LogicalSize;
use rust_webvr::GlWindowVRService;
use servo::compositing::windowing::EmbedderMethods;
use servo::embedder_traits::EventLoopWaker;
use servo::servo_config::{opts, pref};
use servo::webvr::VRServiceManager;
use servo::webvr_traits::WebVRMainThreadHeartbeat;
use std::cell::RefCell;
use std::rc::Rc;

pub struct EmbedderCallbacks {
    events_loop: Rc<RefCell<EventsLoop>>,
    gl: Rc<dyn gl::Gl>,
}

impl EmbedderCallbacks {
    pub fn new(events_loop: Rc<RefCell<EventsLoop>>, gl: Rc<gl::Gl>) -> EmbedderCallbacks {
        EmbedderCallbacks { events_loop, gl }
    }
}

impl EmbedderMethods for EmbedderCallbacks {
    fn create_event_loop_waker(&self) -> Box<dyn EventLoopWaker> {
        self.events_loop.borrow().create_event_loop_waker()
    }

    fn register_vr_services(
        &self,
        services: &mut VRServiceManager,
        heartbeats: &mut Vec<Box<WebVRMainThreadHeartbeat>>,
    ) {
        if !opts::get().headless {
            if pref!(dom.webvr.test) {
                warn!("Creating test VR display");
                // This is safe, because register_vr_services is called from the main thread.
                let name = String::from("Test VR Display");
                let size = opts::get().initial_window_size.to_f64();
                let size = LogicalSize::new(size.width, size.height);
                let window_builder = glutin::WindowBuilder::new()
                    .with_title(name.clone())
                    .with_dimensions(size)
                    .with_visibility(false)
                    .with_multitouch();
                let context = glutin::ContextBuilder::new()
                    .with_gl(app::gl_version())
                    .with_vsync(false) // Assume the browser vsync is the same as the test VR window vsync
                    .build_windowed(window_builder, &*self.events_loop.borrow().as_winit())
                    .expect("Failed to create window.");
                let gl = self.gl.clone();
                let (service, heartbeat) = GlWindowVRService::new(name, context, gl);

                services.register(Box::new(service));
                heartbeats.push(Box::new(heartbeat));
            }
        } else {
            // FIXME: support headless mode
        }
    }
}