Skip to main content

servoshell/desktop/
accelerated_gl_media.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::RefMut;
6
7use surfman::{Context, Device};
8
9#[cfg(not(any(
10    target_os = "windows",
11    all(target_os = "linux", not(target_env = "ohos"))
12)))]
13pub(crate) fn setup_gl_accelerated_media(_: RefMut<'_, Device>, _: RefMut<'_, Context>) {}
14
15#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
16pub(crate) fn setup_gl_accelerated_media(device: RefMut<'_, Device>, context: RefMut<'_, Context>) {
17    use servo::{MediaGlContext, MediaNativeDisplay, Servo};
18    use surfman::multi::connection::Connection;
19    use surfman::multi::context::Context;
20    use surfman::multi::device::Device;
21
22    let api = api(&device, &context);
23    let media_context = match (&*device, &*context) {
24        (Device::Default(Device::Default(device)), Context::Default(Context::Default(context))) => {
25            MediaGlContext::Egl(device.native_context(context).egl_context as usize)
26        },
27        (
28            Device::Default(Device::Alternate(device)),
29            Context::Default(Context::Alternate(context)),
30        ) => MediaGlContext::Egl(device.native_context(context).egl_context as usize),
31        _ => MediaGlContext::Unknown,
32    };
33
34    let media_display = match device.connection() {
35        Connection::Default(Connection::Default(connection)) => {
36            MediaNativeDisplay::Egl(connection.native_connection().0 as usize)
37        },
38        Connection::Default(Connection::Alternate(connection)) => {
39            MediaNativeDisplay::X11(connection.native_connection().x11_display as usize)
40        },
41        _ => MediaNativeDisplay::Unknown,
42    };
43
44    Servo::initialize_gl_accelerated_media(media_display, api, media_context);
45}
46
47#[cfg(target_os = "windows")]
48pub(crate) fn setup_gl_accelerated_media(device: RefMut<'_, Device>, context: RefMut<'_, Context>) {
49    use servo::{MediaGlContext, MediaNativeDisplay, Servo};
50
51    let api = api(&device, &context);
52    let context = MediaGlContext::Egl(device.native_context(&context).egl_context as usize);
53    let display = MediaNativeDisplay::Egl(device.native_device().egl_display as usize);
54    Servo::initialize_gl_accelerated_media(display, api, context);
55}
56
57#[cfg(any(
58    all(target_os = "linux", not(target_env = "ohos")),
59    target_os = "windows"
60))]
61fn api(device: &RefMut<Device>, context: &RefMut<Context>) -> servo::MediaGlApi {
62    use servo::MediaGlApi;
63    use surfman::GLApi;
64
65    let descriptor = device.context_descriptor(context);
66    let attributes = device.context_descriptor_attributes(&descriptor);
67    let major = attributes.version.major;
68    let minor = attributes.version.minor;
69    match device.connection().gl_api() {
70        GLApi::GL if major >= 3 && minor >= 2 => MediaGlApi::OpenGL3,
71        GLApi::GL => MediaGlApi::OpenGL,
72        GLApi::GLES if major > 1 => MediaGlApi::Gles2,
73        GLApi::GLES => MediaGlApi::Gles1,
74    }
75}