diff --git a/components/canvas/lib.rs b/components/canvas/lib.rs index ba10f02636c..47628d57fbf 100644 --- a/components/canvas/lib.rs +++ b/components/canvas/lib.rs @@ -16,5 +16,7 @@ mod raqote_backend; pub mod canvas_data; pub mod canvas_paint_thread; pub mod gl_context; +mod media_mode; +pub mod media_thread; mod webgl_mode; pub mod webgl_thread; diff --git a/components/canvas/media_mode/inprocess.rs b/components/canvas/media_mode/inprocess.rs new file mode 100644 index 00000000000..0fa132efd16 --- /dev/null +++ b/components/canvas/media_mode/inprocess.rs @@ -0,0 +1,30 @@ +/* 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 crate::media_thread::GLPlayerThread; +use canvas_traits::media::{GLPlayerChan, GLPlayerMsg, GLPlayerPipeline, GLPlayerSender}; + +/// GLPlayer Threading API entry point that lives in the constellation. +pub struct GLPlayerThreads(GLPlayerSender); + +impl GLPlayerThreads { + pub fn new() -> GLPlayerThreads { + let channel = GLPlayerThread::start(); + GLPlayerThreads(channel) + } + + /// Gets the GLPlayerThread handle for each script pipeline. + pub fn pipeline(&self) -> GLPlayerPipeline { + // This mode creates a single thread, so the existing + // GLPlayerChan is just cloned. + GLPlayerPipeline(GLPlayerChan(self.0.clone())) + } + + /// Sends an exit message to close the GLPlayerThreads + pub fn exit(&self) -> Result<(), &'static str> { + self.0 + .send(GLPlayerMsg::Exit) + .map_err(|_| "Failed to send Exit message") + } +} diff --git a/components/canvas/media_mode/mod.rs b/components/canvas/media_mode/mod.rs new file mode 100644 index 00000000000..7541463b2bb --- /dev/null +++ b/components/canvas/media_mode/mod.rs @@ -0,0 +1,6 @@ +/* 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/. */ + +mod inprocess; +pub use self::inprocess::GLPlayerThreads; diff --git a/components/canvas/media_thread.rs b/components/canvas/media_thread.rs new file mode 100644 index 00000000000..7ab530adbd0 --- /dev/null +++ b/components/canvas/media_thread.rs @@ -0,0 +1,53 @@ +/* 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 canvas_traits::media::*; +use std::thread; + +/// GL player threading API entry point that lives in the +/// constellation. +/// +/// It allows to get a GLPlayerThead handle for each script pipeline. +pub use crate::media_mode::GLPlayerThreads; + +/// A GLPlayerThrx1ead manages the life cycle and message multiplexign of +/// a set of video players with GL render. +pub struct GLPlayerThread (); + +impl GLPlayerThread { + pub fn new() -> Self { + GLPlayerThread() + } + + pub fn start() -> GLPlayerSender { + let (sender, receiver) = glplayer_channel::().unwrap(); + thread::Builder::new() + .name("GLPlayerThread".to_owned()) + .spawn(move || { + let renderer = GLPlayerThread::new(); + loop { + let msg = receiver.recv().unwrap(); + let exit = renderer.handle_msg(msg); + if exit { + return; + } + } + }) + .expect("Thread spawning failed"); + + sender + } + + /// Handles a generic WebGLMsg message + #[inline] + fn handle_msg(&self, msg: GLPlayerMsg) -> bool { + trace!("processing {:?}", msg); + match msg { + GLPlayerMsg::Exit => return true, + _ => (), + } + + false + } +}