aboutsummaryrefslogtreecommitdiffstats
path: root/components/canvas_traits/webgl_channel
diff options
context:
space:
mode:
authorAnthony Ramine <n.oxyde@gmail.com>2017-08-16 23:23:18 +0200
committerAnthony Ramine <n.oxyde@gmail.com>2017-08-16 23:23:18 +0200
commit676f2c8acf6fec8ad77d4daa51bef5bdcae101c5 (patch)
treed212209744e3b10a315dc79da754a70d7543e428 /components/canvas_traits/webgl_channel
parent4d10d39e8fe841c5fe2ac58da2daaa13c10c140e (diff)
downloadservo-676f2c8acf6fec8ad77d4daa51bef5bdcae101c5.tar.gz
servo-676f2c8acf6fec8ad77d4daa51bef5bdcae101c5.zip
Revert "Auto merge of #18114 - emilio:revert-webgl-refactor, r=nox"
This reverts commit 4d10d39e8fe841c5fe2ac58da2daaa13c10c140e, reversing changes made to ee94e2b7c0bd327abe8f9545b2a1f792f67a2bdd.
Diffstat (limited to 'components/canvas_traits/webgl_channel')
-rw-r--r--components/canvas_traits/webgl_channel/ipc.rs15
-rw-r--r--components/canvas_traits/webgl_channel/mod.rs87
-rw-r--r--components/canvas_traits/webgl_channel/mpsc.rs51
3 files changed, 153 insertions, 0 deletions
diff --git a/components/canvas_traits/webgl_channel/ipc.rs b/components/canvas_traits/webgl_channel/ipc.rs
new file mode 100644
index 00000000000..ac3020bbc7a
--- /dev/null
+++ b/components/canvas_traits/webgl_channel/ipc.rs
@@ -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()
+}
diff --git a/components/canvas_traits/webgl_channel/mod.rs b/components/canvas_traits/webgl_channel/mod.rs
new file mode 100644
index 00000000000..1ac4ce15cb1
--- /dev/null
+++ b/components/canvas_traits/webgl_channel/mod.rs
@@ -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()
+ }
+}
diff --git a/components/canvas_traits/webgl_channel/mpsc.rs b/components/canvas_traits/webgl_channel/mpsc.rs
new file mode 100644
index 00000000000..b0fe29241f3
--- /dev/null
+++ b/components/canvas_traits/webgl_channel/mpsc.rs
@@ -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);