mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Improve WebGL architecture.
This commit is contained in:
parent
e9cbbc58cc
commit
703962fe61
54 changed files with 3154 additions and 1426 deletions
15
components/canvas_traits/webgl_channel/ipc.rs
Normal file
15
components/canvas_traits/webgl_channel/ipc.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* 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 http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use ipc_channel;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
|
||||
pub type WebGLSender<T> = ipc_channel::ipc::IpcSender<T>;
|
||||
pub type WebGLReceiver<T> = ipc_channel::ipc::IpcReceiver<T>;
|
||||
|
||||
pub fn webgl_channel<T: Serialize + for<'de> Deserialize<'de>>()
|
||||
-> Result<(WebGLSender<T>, WebGLReceiver<T>), io::Error> {
|
||||
ipc_channel::ipc::channel()
|
||||
}
|
87
components/canvas_traits/webgl_channel/mod.rs
Normal file
87
components/canvas_traits/webgl_channel/mod.rs
Normal file
|
@ -0,0 +1,87 @@
|
|||
/* 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 http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! Enum wrappers to be able to select different channel implementations at runtime.
|
||||
|
||||
mod ipc;
|
||||
mod mpsc;
|
||||
|
||||
use ::webgl::WebGLMsg;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_config::opts;
|
||||
|
||||
lazy_static! {
|
||||
static ref IS_MULTIPROCESS: bool = {
|
||||
opts::multiprocess()
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub enum WebGLSender<T: Serialize> {
|
||||
Ipc(ipc::WebGLSender<T>),
|
||||
Mpsc(mpsc::WebGLSender<T>),
|
||||
}
|
||||
|
||||
impl<T: Serialize> WebGLSender<T> {
|
||||
#[inline]
|
||||
pub fn send(&self, msg: T) -> WebGLSendResult {
|
||||
match *self {
|
||||
WebGLSender::Ipc(ref sender) => {
|
||||
sender.send(msg).map_err(|_| ())
|
||||
},
|
||||
WebGLSender::Mpsc(ref sender) => {
|
||||
sender.send(msg).map_err(|_| ())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type WebGLSendResult = Result<(), ()>;
|
||||
|
||||
pub enum WebGLReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
|
||||
Ipc(ipc::WebGLReceiver<T>),
|
||||
Mpsc(mpsc::WebGLReceiver<T>),
|
||||
}
|
||||
|
||||
impl<T> WebGLReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
|
||||
pub fn recv(&self) -> Result<T, ()> {
|
||||
match *self {
|
||||
WebGLReceiver::Ipc(ref receiver) => {
|
||||
receiver.recv().map_err(|_| ())
|
||||
},
|
||||
WebGLReceiver::Mpsc(ref receiver) => {
|
||||
receiver.recv().map_err(|_| ())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn webgl_channel<T>() -> Result<(WebGLSender<T>, WebGLReceiver<T>), ()>
|
||||
where T: for<'de> Deserialize<'de> + Serialize {
|
||||
if *IS_MULTIPROCESS {
|
||||
ipc::webgl_channel().map(|(tx, rx)| (WebGLSender::Ipc(tx), WebGLReceiver::Ipc(rx)))
|
||||
.map_err(|_| ())
|
||||
} else {
|
||||
mpsc::webgl_channel().map(|(tx, rx)| (WebGLSender::Mpsc(tx), WebGLReceiver::Mpsc(rx)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct WebGLChan(pub WebGLSender<WebGLMsg>);
|
||||
|
||||
impl WebGLChan {
|
||||
#[inline]
|
||||
pub fn send(&self, msg: WebGLMsg) -> WebGLSendResult {
|
||||
self.0.send(msg)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct WebGLPipeline(pub WebGLChan);
|
||||
|
||||
impl WebGLPipeline {
|
||||
pub fn channel(&self) -> WebGLChan {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
51
components/canvas_traits/webgl_channel/mpsc.rs
Normal file
51
components/canvas_traits/webgl_channel/mpsc.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
/* 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 http://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!();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WebGLSender<T>(mpsc::Sender<T>);
|
||||
pub struct WebGLReceiver<T>(mpsc::Receiver<T>);
|
||||
|
||||
impl<T> WebGLSender<T> {
|
||||
#[inline]
|
||||
pub fn send(&self, data: T) -> Result<(), mpsc::SendError<T>> {
|
||||
self.0.send(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WebGLReceiver<T> {
|
||||
#[inline]
|
||||
pub fn recv(&self) -> Result<T, mpsc::RecvError> {
|
||||
self.0.recv()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn webgl_channel<T>() -> Result<(WebGLSender<T>, WebGLReceiver<T>), ()> {
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
Ok((WebGLSender(sender), WebGLReceiver(receiver)))
|
||||
}
|
||||
|
||||
unreachable_serializable!(WebGLReceiver);
|
||||
unreachable_serializable!(WebGLSender);
|
Loading…
Add table
Add a link
Reference in a new issue