servo/components/media/media_thread.rs
Martin Robinson 5465bfc2af
libservo: Move GL acclerated media setup out of RenderingContext and simplify it (#35553)
This moves the GL accelerated media setup out of `RenderingContext`
which prevents making libservo dependo on the Wayland and X11 versions
of surfman explicitly. This support is experimental and (honestly) a bit
broken. I've confirmed that this works as well as it did before the
change.

The main thing here is that the configuration, which currently needs
surfman types, moves to servoshell. In addition:

1. Instead of passing the information to the Constellation, the setup is
   stored statically. This is necessary to avoid introducing a
   dependency on `media` in `webrender_traits`. It's quite likely that
   `media` types should move to the internal embedding API to avoid
   this. This is preserved for a followup change.
2. The whole system of wrapping the media channels in an abstract type
   is removed. They could be either mpsc channels or IPC channels. This
   was never going to work because mpsc channels cannot be serialized
   and deserialized with serde. Instead this just uses IPC channels. We
   also have other ways of doing this kind of abstraction in Servo so we
   do not need another. The `mpsc` version was hard-coded to be
   disabled.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-02-20 13:52:18 +00:00

96 lines
3.3 KiB
Rust

/* 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/. */
use std::sync::{Arc, Mutex};
use std::thread;
use fnv::FnvHashMap;
use ipc_channel::ipc::{channel, IpcSender};
use log::{trace, warn};
use webrender_api::ExternalImageId;
use webrender_traits::{WebrenderExternalImageRegistry, WebrenderImageHandlerType};
/// GL player threading API entry point that lives in the
/// constellation.
use crate::{GLPlayerMsg, GLPlayerMsgForward};
/// A GLPlayerThread manages the life cycle and message demultiplexing of
/// a set of video players with GL render.
pub struct GLPlayerThread {
/// Map of live players.
players: FnvHashMap<u64, IpcSender<GLPlayerMsgForward>>,
/// List of registered webrender external images.
/// We use it to get an unique ID for new players.
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
}
impl GLPlayerThread {
pub fn new(external_images: Arc<Mutex<WebrenderExternalImageRegistry>>) -> Self {
GLPlayerThread {
players: Default::default(),
external_images,
}
}
pub fn start(
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
) -> IpcSender<GLPlayerMsg> {
let (sender, receiver) = channel().unwrap();
thread::Builder::new()
.name("GLPlayer".to_owned())
.spawn(move || {
let mut renderer = GLPlayerThread::new(external_images);
loop {
let msg = receiver.recv().unwrap();
let exit = renderer.handle_msg(msg);
if exit {
return;
}
}
})
.expect("Thread spawning failed");
sender
}
/// Handles a generic GLPlayerMsg message
#[inline]
fn handle_msg(&mut self, msg: GLPlayerMsg) -> bool {
trace!("processing {:?}", msg);
match msg {
GLPlayerMsg::RegisterPlayer(sender) => {
let id = self
.external_images
.lock()
.unwrap()
.next_id(WebrenderImageHandlerType::Media)
.0;
self.players.insert(id, sender.clone());
sender.send(GLPlayerMsgForward::PlayerId(id)).unwrap();
},
GLPlayerMsg::UnregisterPlayer(id) => {
self.external_images
.lock()
.unwrap()
.remove(&ExternalImageId(id));
if self.players.remove(&id).is_none() {
warn!("Tried to remove an unknown player");
}
},
GLPlayerMsg::Lock(id, handler_sender) => {
if let Some(sender) = self.players.get(&id) {
sender.send(GLPlayerMsgForward::Lock(handler_sender)).ok();
}
},
GLPlayerMsg::Unlock(id) => {
if let Some(sender) = self.players.get(&id) {
sender.send(GLPlayerMsgForward::Unlock()).ok();
}
},
GLPlayerMsg::Exit => return true,
}
false
}
}