aboutsummaryrefslogtreecommitdiffstats
path: root/ports/glutin/headless_window.rs
blob: c24d142fda55f212da4599ff9a7a65606d1d1894 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* 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/. */

//! A headless window implementation.

use crate::window_trait::WindowPortsMethods;
use euclid::{default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector3D};
use gleam::gl;
use glutin;
use servo::compositing::windowing::{AnimationState, WindowEvent};
use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods};
use servo::servo_geometry::DeviceIndependentPixel;
use servo::style_traits::DevicePixel;
use servo::webrender_api::units::{DeviceIntRect, DeviceIntSize};
use servo_media::player::context as MediaPlayerCtxt;
use std::cell::Cell;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::cell::RefCell;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::ffi::CString;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use std::rc::Rc;

#[cfg(any(target_os = "linux", target_os = "macos"))]
struct HeadlessContext {
    width: u32,
    height: u32,
    context: osmesa_sys::OSMesaContext,
    buffer: RefCell<Vec<u32>>,
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
struct HeadlessContext {
    width: u32,
    height: u32,
}

impl HeadlessContext {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    fn new(width: u32, height: u32, share: Option<&HeadlessContext>) -> HeadlessContext {
        let mut attribs = Vec::new();

        attribs.push(osmesa_sys::OSMESA_PROFILE);
        attribs.push(osmesa_sys::OSMESA_CORE_PROFILE);
        attribs.push(osmesa_sys::OSMESA_CONTEXT_MAJOR_VERSION);
        attribs.push(3);
        attribs.push(osmesa_sys::OSMESA_CONTEXT_MINOR_VERSION);
        attribs.push(3);
        attribs.push(0);

        let share = share.map_or(ptr::null_mut(), |share| share.context as *mut _);

        let context = unsafe { osmesa_sys::OSMesaCreateContextAttribs(attribs.as_ptr(), share) };

        assert!(!context.is_null());

        HeadlessContext {
            width: width,
            height: height,
            context: context,
            buffer: RefCell::new(vec![0; (width * height) as usize]),
        }
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    fn new(width: u32, height: u32, _share: Option<&HeadlessContext>) -> HeadlessContext {
        HeadlessContext {
            width: width,
            height: height,
        }
    }

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    fn get_proc_address(s: &str) -> *const c_void {
        let c_str = CString::new(s).expect("Unable to create CString");
        unsafe { mem::transmute(osmesa_sys::OSMesaGetProcAddress(c_str.as_ptr())) }
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    fn get_proc_address(_: &str) -> *const c_void {
        ptr::null() as *const _
    }
}

pub struct Window {
    context: HeadlessContext,
    animation_state: Cell<AnimationState>,
    fullscreen: Cell<bool>,
    gl: Rc<dyn gl::Gl>,
    device_pixels_per_px: Option<f32>,
}

impl Window {
    pub fn new(
        size: Size2D<u32, DeviceIndependentPixel>,
        device_pixels_per_px: Option<f32>,
    ) -> Rc<dyn WindowPortsMethods> {
        let context = HeadlessContext::new(size.width, size.height, None);
        let gl = unsafe { gl::GlFns::load_with(|s| HeadlessContext::get_proc_address(s)) };

        // Print some information about the headless renderer that
        // can be useful in diagnosing CI failures on build machines.
        println!("{}", gl.get_string(gl::VENDOR));
        println!("{}", gl.get_string(gl::RENDERER));
        println!("{}", gl.get_string(gl::VERSION));

        let window = Window {
            context,
            gl,
            animation_state: Cell::new(AnimationState::Idle),
            fullscreen: Cell::new(false),
            device_pixels_per_px,
        };

        Rc::new(window)
    }

    fn servo_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
        match self.device_pixels_per_px {
            Some(device_pixels_per_px) => Scale::new(device_pixels_per_px),
            _ => Scale::new(1.0),
        }
    }
}

impl WindowPortsMethods for Window {
    fn get_events(&self) -> Vec<WindowEvent> {
        vec![]
    }

    fn has_events(&self) -> bool {
        false
    }

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

    fn page_height(&self) -> f32 {
        let dpr = self.servo_hidpi_factor();
        self.context.height as f32 * dpr.get()
    }

    fn set_fullscreen(&self, state: bool) {
        self.fullscreen.set(state);
    }

    fn get_fullscreen(&self) -> bool {
        return self.fullscreen.get();
    }

    fn is_animating(&self) -> bool {
        self.animation_state.get() == AnimationState::Animating
    }

    fn winit_event_to_servo_event(&self, _event: glutin::WindowEvent) {
        // Not expecting any winit events.
    }
}

impl WindowMethods for Window {
    fn gl(&self) -> Rc<dyn gl::Gl> {
        self.gl.clone()
    }

    fn get_coordinates(&self) -> EmbedderCoordinates {
        let dpr = self.servo_hidpi_factor();
        let size = (Size2D::new(self.context.width, self.context.height).to_f32() * dpr).to_i32();
        let viewport = DeviceIntRect::new(Point2D::zero(), size);
        let framebuffer = DeviceIntSize::from_untyped(size.to_untyped());
        EmbedderCoordinates {
            viewport,
            framebuffer,
            window: (size, Point2D::zero()),
            screen: size,
            screen_avail: size,
            hidpi_factor: dpr,
        }
    }

    fn present(&self) {}

    fn set_animation_state(&self, state: AnimationState) {
        self.animation_state.set(state);
    }

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    fn make_gl_context_current(&self) {
        unsafe {
            let mut buffer = self.context.buffer.borrow_mut();
            let ret = osmesa_sys::OSMesaMakeCurrent(
                self.context.context,
                buffer.as_mut_ptr() as *mut _,
                gl::UNSIGNED_BYTE,
                self.context.width as i32,
                self.context.height as i32,
            );
            assert_ne!(ret, 0);
        };
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    fn make_gl_context_current(&self) {}

    fn get_gl_context(&self) -> MediaPlayerCtxt::GlContext {
        MediaPlayerCtxt::GlContext::Unknown
    }

    fn get_native_display(&self) -> MediaPlayerCtxt::NativeDisplay {
        MediaPlayerCtxt::NativeDisplay::Unknown
    }

    fn get_gl_api(&self) -> MediaPlayerCtxt::GlApi {
        MediaPlayerCtxt::GlApi::None
    }
}

impl webxr::glwindow::GlWindow for Window {
    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<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(Rc::new(Window {
            context,
            gl,
            animation_state: Cell::new(AnimationState::Idle),
            fullscreen: Cell::new(false),
            device_pixels_per_px: self.device_pixels_per_px,
        }))
    }
    fn get_rotation(&self) -> Rotation3D<f32, UnknownUnit, UnknownUnit> {
        Rotation3D::identity()
    }

    fn get_translation(&self) -> Vector3D<f32, UnknownUnit> {
        Vector3D::zero()
    }
}