1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* 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 std::fmt;

use ipc_channel::ipc::IpcSender;
use ipc_channel::router::ROUTER;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use servo_config::opts;

use crate::webgl::WebGLMsg;

lazy_static! {
    static ref IS_MULTIPROCESS: bool = opts::multiprocess();
}

#[derive(Deserialize, Serialize)]
pub enum WebGLSender<T: Serialize> {
    Ipc(ipc::WebGLSender<T>),
    Mpsc(mpsc::WebGLSender<T>),
}

impl<T> Clone for WebGLSender<T>
where
    T: Serialize,
{
    fn clone(&self) -> Self {
        match *self {
            WebGLSender::Ipc(ref chan) => WebGLSender::Ipc(chan.clone()),
            WebGLSender::Mpsc(ref chan) => WebGLSender::Mpsc(chan.clone()),
        }
    }
}

impl<T: Serialize> fmt::Debug for WebGLSender<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "WebGLSender(..)")
    }
}

impl<T: Serialize> WebGLSender<T> {
    #[inline]
    pub fn send(&self, msg: T) -> WebGLSendResult {
        match *self {
            WebGLSender::Ipc(ref sender) => sender.send(msg).map_err(|_| WebGLSendError),
            WebGLSender::Mpsc(ref sender) => sender.send(msg).map_err(|_| WebGLSendError),
        }
    }
}

#[derive(Debug)]
pub struct WebGLSendError;
pub type WebGLSendResult = Result<(), WebGLSendError>;

#[derive(Debug)]
pub struct WebGLReceiveError;
pub type WebGLReceiveResult<T> = Result<T, WebGLReceiveError>;

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) -> WebGLReceiveResult<T> {
        match *self {
            WebGLReceiver::Ipc(ref receiver) => receiver.recv().map_err(|_| WebGLReceiveError),
            WebGLReceiver::Mpsc(ref receiver) => receiver.recv().map_err(|_| WebGLReceiveError),
        }
    }

    pub fn try_recv(&self) -> WebGLReceiveResult<T> {
        match *self {
            WebGLReceiver::Ipc(ref receiver) => receiver.try_recv().map_err(|_| WebGLReceiveError),
            WebGLReceiver::Mpsc(ref receiver) => receiver.try_recv().map_err(|_| WebGLReceiveError),
        }
    }

    pub fn into_inner(self) -> crossbeam_channel::Receiver<T>
    where
        T: Send + 'static,
    {
        match self {
            WebGLReceiver::Ipc(receiver) => {
                ROUTER.route_ipc_receiver_to_new_crossbeam_receiver(receiver)
            },
            WebGLReceiver::Mpsc(receiver) => receiver.into_inner(),
        }
    }
}

pub fn webgl_channel<T>() -> Option<(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)))
            .ok()
    } else {
        mpsc::webgl_channel()
            .map(|(tx, rx)| (WebGLSender::Mpsc(tx), WebGLReceiver::Mpsc(rx)))
            .ok()
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebGLChan(pub WebGLSender<WebGLMsg>);

impl WebGLChan {
    #[inline]
    pub fn send(&self, msg: WebGLMsg) -> WebGLSendResult {
        self.0.send(msg)
    }

    pub fn to_ipc(&self) -> IpcSender<WebGLMsg> {
        match self.0 {
            WebGLSender::Ipc(ref sender) => sender.clone(),
            WebGLSender::Mpsc(ref mpsc_sender) => {
                let (sender, receiver) =
                    ipc_channel::ipc::channel().expect("IPC Channel creation failed");
                let mpsc_sender = mpsc_sender.clone();
                ipc_channel::router::ROUTER.add_route(
                    receiver.to_opaque(),
                    Box::new(move |message| {
                        if let Ok(message) = message.to() {
                            let _ = mpsc_sender.send(message);
                        }
                    }),
                );
                sender
            },
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebGLPipeline(pub WebGLChan);

impl WebGLPipeline {
    pub fn channel(&self) -> WebGLChan {
        self.0.clone()
    }
}