-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GLPlayerMsg and glplayer_channel in canvas_traits
GLPlayerMsg enum values are going to be the commands to send to the glplayer_thread. glplayer_channel mod is a copy of webgl_channel.
- Loading branch information
Showing
5 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,14 @@ | |||
/* 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 serde::{Deserialize, Serialize}; | |||
use std::io; | |||
|
|||
pub type GLPlayerSender<T> = ipc_channel::ipc::IpcSender<T>; | |||
pub type GLPlayerReceiver<T> = ipc_channel::ipc::IpcReceiver<T>; | |||
|
|||
pub fn glplayer_channel<T: Serialize + for<'de> Deserialize<'de>>( | |||
) -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), io::Error> { | |||
ipc_channel::ipc::channel() | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,106 @@ | |||
/* 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/. */ | |||
|
|||
//! Enum wrappers to be able to select different channel implementations at runtime. | |||
|
|||
mod ipc; | |||
mod mpsc; | |||
|
|||
use crate::media::GLPlayerMsg; | |||
use serde::{Deserialize, Serialize}; | |||
use servo_config::opts; | |||
use std::fmt; | |||
|
|||
lazy_static! { | |||
static ref IS_MULTIPROCESS: bool = { opts::multiprocess() }; | |||
} | |||
|
|||
#[derive(Deserialize, Serialize)] | |||
pub enum GLPlayerSender<T: Serialize> { | |||
Ipc(ipc::GLPlayerSender<T>), | |||
Mpsc(mpsc::GLPlayerSender<T>), | |||
} | |||
|
|||
impl<T> Clone for GLPlayerSender<T> | |||
where | |||
T: Serialize, | |||
{ | |||
fn clone(&self) -> Self { | |||
match *self { | |||
GLPlayerSender::Ipc(ref chan) => GLPlayerSender::Ipc(chan.clone()), | |||
GLPlayerSender::Mpsc(ref chan) => GLPlayerSender::Mpsc(chan.clone()), | |||
} | |||
} | |||
} | |||
|
|||
impl<T: Serialize> fmt::Debug for GLPlayerSender<T> { | |||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |||
write!(f, "GLPlayerSender(..)") | |||
} | |||
} | |||
|
|||
impl<T: Serialize> GLPlayerSender<T> { | |||
#[inline] | |||
pub fn send(&self, msg: T) -> GLPlayerSendResult { | |||
match *self { | |||
GLPlayerSender::Ipc(ref sender) => sender.send(msg).map_err(|_| ()), | |||
GLPlayerSender::Mpsc(ref sender) => sender.send(msg).map_err(|_| ()), | |||
} | |||
} | |||
} | |||
|
|||
pub type GLPlayerSendResult = Result<(), ()>; | |||
|
|||
pub enum GLPlayerReceiver<T> | |||
where | |||
T: for<'de> Deserialize<'de> + Serialize, | |||
{ | |||
Ipc(ipc::GLPlayerReceiver<T>), | |||
Mpsc(mpsc::GLPlayerReceiver<T>), | |||
} | |||
|
|||
impl<T> GLPlayerReceiver<T> | |||
where | |||
T: for<'de> Deserialize<'de> + Serialize, | |||
{ | |||
pub fn recv(&self) -> Result<T, ()> { | |||
match *self { | |||
GLPlayerReceiver::Ipc(ref receiver) => receiver.recv().map_err(|_| ()), | |||
GLPlayerReceiver::Mpsc(ref receiver) => receiver.recv().map_err(|_| ()), | |||
} | |||
} | |||
} | |||
|
|||
pub fn glplayer_channel<T>() -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), ()> | |||
where | |||
T: for<'de> Deserialize<'de> + Serialize, | |||
{ | |||
if *IS_MULTIPROCESS { | |||
ipc::glplayer_channel() | |||
.map(|(tx, rx)| (GLPlayerSender::Ipc(tx), GLPlayerReceiver::Ipc(rx))) | |||
.map_err(|_| ()) | |||
} else { | |||
mpsc::glplayer_channel() | |||
.map(|(tx, rx)| (GLPlayerSender::Mpsc(tx), GLPlayerReceiver::Mpsc(rx))) | |||
} | |||
} | |||
|
|||
#[derive(Clone, Debug, Deserialize, Serialize)] | |||
pub struct GLPlayerChan(pub GLPlayerSender<GLPlayerMsg>); | |||
|
|||
impl GLPlayerChan { | |||
#[inline] | |||
pub fn send(&self, msg: GLPlayerMsg) -> GLPlayerSendResult { | |||
self.0.send(msg) | |||
} | |||
} | |||
|
|||
#[derive(Clone, Debug, Deserialize, Serialize)] | |||
pub struct GLPlayerPipeline(pub GLPlayerChan); | |||
|
|||
impl GLPlayerPipeline { | |||
pub fn channel(&self) -> GLPlayerChan { | |||
self.0.clone() | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,58 @@ | |||
/* 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 serde::{Deserialize, Serialize}; | |||
use serde::{Deserializer, Serializer}; | |||
use std::sync::mpsc; | |||
|
|||
#[macro_use] | |||
macro_rules! unreachable_serializable { | |||
($name:ident) => { | |||
impl<T> Serialize for $name<T> { | |||
fn serialize<S: Serializer>(&self, _: S) -> Result<S::Ok, S::Error> { | |||
unreachable!(); | |||
} | |||
} | |||
|
|||
impl<'a, T> Deserialize<'a> for $name<T> { | |||
fn deserialize<D>(_: D) -> Result<$name<T>, D::Error> | |||
where | |||
D: Deserializer<'a>, | |||
{ | |||
unreachable!(); | |||
} | |||
} | |||
}; | |||
} | |||
|
|||
pub struct GLPlayerSender<T>(mpsc::Sender<T>); | |||
pub struct GLPlayerReceiver<T>(mpsc::Receiver<T>); | |||
|
|||
impl<T> Clone for GLPlayerSender<T> { | |||
fn clone(&self) -> Self { | |||
GLPlayerSender(self.0.clone()) | |||
} | |||
} | |||
|
|||
impl<T> GLPlayerSender<T> { | |||
#[inline] | |||
pub fn send(&self, data: T) -> Result<(), mpsc::SendError<T>> { | |||
self.0.send(data) | |||
} | |||
} | |||
|
|||
impl<T> GLPlayerReceiver<T> { | |||
#[inline] | |||
pub fn recv(&self) -> Result<T, mpsc::RecvError> { | |||
self.0.recv() | |||
} | |||
} | |||
|
|||
pub fn glplayer_channel<T>() -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), ()> { | |||
let (sender, receiver) = mpsc::channel(); | |||
Ok((GLPlayerSender(sender), GLPlayerReceiver(receiver))) | |||
} | |||
|
|||
unreachable_serializable!(GLPlayerReceiver); | |||
unreachable_serializable!(GLPlayerSender); |