#![deny(unsafe_code)]
#![allow(clippy::type_complexity)]
mod media_thread;
use std::sync::Mutex;
use euclid::default::Size2D;
use ipc_channel::ipc::{IpcReceiver, IpcSender, channel};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use paint_api::{
ExternalImageSource, WebRenderExternalImageApi, WebRenderExternalImageHandlers,
WebRenderImageHandlerType,
};
use serde::{Deserialize, Serialize};
use servo_config::pref;
pub use servo_media::player::context::{GlApi, GlContext, NativeDisplay, PlayerGLContext};
use crate::media_thread::GLPlayerThread;
static WINDOW_GL_CONTEXT: Mutex<WindowGLContext> = Mutex::new(WindowGLContext::inactive());
#[derive(Debug, Deserialize, Serialize)]
pub enum GLPlayerMsgForward {
PlayerId(u64),
Lock(IpcSender<(u32, Size2D<i32>, usize)>),
Unlock(),
}
#[derive(Debug, Deserialize, Serialize)]
pub enum GLPlayerMsg {
RegisterPlayer(IpcSender<GLPlayerMsgForward>),
UnregisterPlayer(u64),
Lock(u64, IpcSender<(u32, Size2D<i32>, usize)>),
Unlock(u64),
Exit,
}
#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
pub struct WindowGLContext {
pub context: GlContext,
pub api: GlApi,
pub display: NativeDisplay,
pub glplayer_thread_sender: Option<IpcSender<GLPlayerMsg>>,
}
impl WindowGLContext {
pub const fn inactive() -> Self {
WindowGLContext {
context: GlContext::Unknown,
api: GlApi::None,
display: NativeDisplay::Unknown,
glplayer_thread_sender: None,
}
}
pub fn register(context: Self) {
*WINDOW_GL_CONTEXT.lock().unwrap() = context;
}
pub fn get() -> Self {
WINDOW_GL_CONTEXT.lock().unwrap().clone()
}
pub fn exit(&self) {
self.send(GLPlayerMsg::Exit);
}
#[inline]
pub fn send(&self, message: GLPlayerMsg) {
let Some(sender) = self.glplayer_thread_sender.as_ref() else {
return;
};
if let Err(error) = sender.send(message) {
warn!("Could no longer communicate with GL accelerated media threads: {error}")
}
}
pub fn initialize(display: NativeDisplay, api: GlApi, context: GlContext) {
if matches!(display, NativeDisplay::Unknown) || matches!(context, GlContext::Unknown) {
return;
}
let mut window_gl_context = WINDOW_GL_CONTEXT.lock().unwrap();
if window_gl_context.glplayer_thread_sender.is_some() {
warn!("Not going to initialize GL accelerated media playback more than once.");
return;
}
window_gl_context.context = context;
window_gl_context.display = display;
window_gl_context.api = api;
}
pub fn initialize_image_handler(external_image_handlers: &mut WebRenderExternalImageHandlers) {
if !pref!(media_glvideo_enabled) {
return;
}
let mut window_gl_context = WINDOW_GL_CONTEXT.lock().unwrap();
if window_gl_context.glplayer_thread_sender.is_some() {
warn!("Not going to initialize GL accelerated media playback more than once.");
return;
}
if matches!(window_gl_context.display, NativeDisplay::Unknown) ||
matches!(window_gl_context.context, GlContext::Unknown)
{
return;
}
let thread_sender = GLPlayerThread::start(external_image_handlers.id_manager());
let image_handler = Box::new(GLPlayerExternalImages::new(thread_sender.clone()));
external_image_handlers.set_handler(image_handler, WebRenderImageHandlerType::Media);
window_gl_context.glplayer_thread_sender = Some(thread_sender);
}
}
impl PlayerGLContext for WindowGLContext {
fn get_gl_context(&self) -> GlContext {
match self.glplayer_thread_sender {
Some(..) => self.context.clone(),
None => GlContext::Unknown,
}
}
fn get_native_display(&self) -> NativeDisplay {
match self.glplayer_thread_sender {
Some(..) => self.display.clone(),
None => NativeDisplay::Unknown,
}
}
fn get_gl_api(&self) -> GlApi {
self.api.clone()
}
}
struct GLPlayerExternalImages {
glplayer_channel: IpcSender<GLPlayerMsg>,
lock_channel: (
IpcSender<(u32, Size2D<i32>, usize)>,
IpcReceiver<(u32, Size2D<i32>, usize)>,
),
}
impl GLPlayerExternalImages {
fn new(sender: IpcSender<GLPlayerMsg>) -> Self {
Self {
glplayer_channel: sender,
lock_channel: channel().unwrap(),
}
}
}
impl WebRenderExternalImageApi for GLPlayerExternalImages {
fn lock(&mut self, id: u64) -> (ExternalImageSource<'_>, Size2D<i32>) {
self.glplayer_channel
.send(GLPlayerMsg::Lock(id, self.lock_channel.0.clone()))
.unwrap();
let (image_id, size, _gl_sync) = self.lock_channel.1.recv().unwrap();
(ExternalImageSource::NativeTexture(image_id), size)
}
fn unlock(&mut self, id: u64) {
self.glplayer_channel.send(GLPlayerMsg::Unlock(id)).unwrap();
}
}