mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Move *_traits
and other shared types to shared
This is the start of the organization of types that are in their own crates in order to break dependency cycles between other crates. The idea here is that putting these packages into their own directory is the first step toward cleaning them up. They have grown organically and it is difficult to explain to new folks where to put new shared types. Many of these crates contain more than traits or don't contain traits at all. Notably, `script_traits` isn't touched because it is vendored from Gecko. Eventually this will move to `third_party`.
This commit is contained in:
parent
863529d962
commit
f4d3af296c
89 changed files with 244 additions and 226 deletions
33
components/shared/canvas/Cargo.toml
Normal file
33
components/shared/canvas/Cargo.toml
Normal file
|
@ -0,0 +1,33 @@
|
|||
[package]
|
||||
name = "canvas_traits"
|
||||
version = "0.0.1"
|
||||
authors = ["The Servo Project Developers"]
|
||||
license = "MPL-2.0"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "canvas_traits"
|
||||
path = "lib.rs"
|
||||
|
||||
[features]
|
||||
webgl_backtrace = []
|
||||
xr-profile = ["webxr-api/profile", "time"]
|
||||
|
||||
[dependencies]
|
||||
crossbeam-channel = { workspace = true }
|
||||
cssparser = { workspace = true }
|
||||
euclid = { workspace = true }
|
||||
ipc-channel = { workspace = true }
|
||||
lazy_static = { workspace = true }
|
||||
malloc_size_of = { path = "../../malloc_size_of" }
|
||||
malloc_size_of_derive = { workspace = true }
|
||||
pixels = { path = "../../pixels" }
|
||||
serde = { workspace = true }
|
||||
serde_bytes = { workspace = true }
|
||||
servo_config = { path = "../../config" }
|
||||
sparkle = { workspace = true }
|
||||
style = { path = "../../style" }
|
||||
time = { workspace = true, optional = true }
|
||||
webrender_api = { workspace = true }
|
||||
webxr-api = { git = "https://github.com/servo/webxr", features = ["ipc"] }
|
491
components/shared/canvas/canvas.rs
Normal file
491
components/shared/canvas/canvas.rs
Normal file
|
@ -0,0 +1,491 @@
|
|||
/* 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 std::default::Default;
|
||||
use std::str::FromStr;
|
||||
|
||||
use cssparser::RGBA;
|
||||
use euclid::default::{Point2D, Rect, Size2D, Transform2D};
|
||||
use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSender, IpcSharedMemory};
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_bytes::ByteBuf;
|
||||
use style::properties::style_structs::Font as FontStyleStruct;
|
||||
use webrender_api::ImageKey;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum FillRule {
|
||||
Nonzero,
|
||||
Evenodd,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub struct CanvasId(pub u64);
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub enum CanvasMsg {
|
||||
Canvas2d(Canvas2dMsg, CanvasId),
|
||||
FromLayout(FromLayoutMsg, CanvasId),
|
||||
FromScript(FromScriptMsg, CanvasId),
|
||||
Recreate(Size2D<u64>, CanvasId),
|
||||
Close(CanvasId),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct CanvasImageData {
|
||||
pub image_key: ImageKey,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub enum Canvas2dMsg {
|
||||
Arc(Point2D<f32>, f32, f32, f32, bool),
|
||||
ArcTo(Point2D<f32>, Point2D<f32>, f32),
|
||||
DrawImage(IpcSharedMemory, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
|
||||
DrawEmptyImage(Size2D<f64>, Rect<f64>, Rect<f64>),
|
||||
DrawImageInOther(CanvasId, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
|
||||
BeginPath,
|
||||
BezierCurveTo(Point2D<f32>, Point2D<f32>, Point2D<f32>),
|
||||
ClearRect(Rect<f32>),
|
||||
Clip,
|
||||
ClosePath,
|
||||
Ellipse(Point2D<f32>, f32, f32, f32, f32, f32, bool),
|
||||
Fill(FillOrStrokeStyle),
|
||||
FillText(String, f64, f64, Option<f64>, FillOrStrokeStyle, bool),
|
||||
FillRect(Rect<f32>, FillOrStrokeStyle),
|
||||
GetImageData(Rect<u64>, Size2D<u64>, IpcBytesSender),
|
||||
GetTransform(IpcSender<Transform2D<f32>>),
|
||||
IsPointInPath(f64, f64, FillRule, IpcSender<bool>),
|
||||
LineTo(Point2D<f32>),
|
||||
MoveTo(Point2D<f32>),
|
||||
PutImageData(Rect<u64>, IpcBytesReceiver),
|
||||
QuadraticCurveTo(Point2D<f32>, Point2D<f32>),
|
||||
Rect(Rect<f32>),
|
||||
RestoreContext,
|
||||
SaveContext,
|
||||
StrokeRect(Rect<f32>, FillOrStrokeStyle),
|
||||
Stroke(FillOrStrokeStyle),
|
||||
SetLineWidth(f32),
|
||||
SetLineCap(LineCapStyle),
|
||||
SetLineJoin(LineJoinStyle),
|
||||
SetMiterLimit(f32),
|
||||
SetGlobalAlpha(f32),
|
||||
SetGlobalComposition(CompositionOrBlending),
|
||||
SetTransform(Transform2D<f32>),
|
||||
SetShadowOffsetX(f64),
|
||||
SetShadowOffsetY(f64),
|
||||
SetShadowBlur(f64),
|
||||
SetShadowColor(RGBA),
|
||||
SetFont(FontStyleStruct),
|
||||
SetTextAlign(TextAlign),
|
||||
SetTextBaseline(TextBaseline),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum FromLayoutMsg {
|
||||
SendData(IpcSender<CanvasImageData>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum FromScriptMsg {
|
||||
SendPixels(IpcSender<IpcSharedMemory>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct CanvasGradientStop {
|
||||
pub offset: f64,
|
||||
pub color: RGBA,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct LinearGradientStyle {
|
||||
pub x0: f64,
|
||||
pub y0: f64,
|
||||
pub x1: f64,
|
||||
pub y1: f64,
|
||||
pub stops: Vec<CanvasGradientStop>,
|
||||
}
|
||||
|
||||
impl LinearGradientStyle {
|
||||
pub fn new(
|
||||
x0: f64,
|
||||
y0: f64,
|
||||
x1: f64,
|
||||
y1: f64,
|
||||
stops: Vec<CanvasGradientStop>,
|
||||
) -> LinearGradientStyle {
|
||||
LinearGradientStyle {
|
||||
x0: x0,
|
||||
y0: y0,
|
||||
x1: x1,
|
||||
y1: y1,
|
||||
stops: stops,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct RadialGradientStyle {
|
||||
pub x0: f64,
|
||||
pub y0: f64,
|
||||
pub r0: f64,
|
||||
pub x1: f64,
|
||||
pub y1: f64,
|
||||
pub r1: f64,
|
||||
pub stops: Vec<CanvasGradientStop>,
|
||||
}
|
||||
|
||||
impl RadialGradientStyle {
|
||||
pub fn new(
|
||||
x0: f64,
|
||||
y0: f64,
|
||||
r0: f64,
|
||||
x1: f64,
|
||||
y1: f64,
|
||||
r1: f64,
|
||||
stops: Vec<CanvasGradientStop>,
|
||||
) -> RadialGradientStyle {
|
||||
RadialGradientStyle {
|
||||
x0: x0,
|
||||
y0: y0,
|
||||
r0: r0,
|
||||
x1: x1,
|
||||
y1: y1,
|
||||
r1: r1,
|
||||
stops: stops,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SurfaceStyle {
|
||||
pub surface_data: ByteBuf,
|
||||
pub surface_size: Size2D<u32>,
|
||||
pub repeat_x: bool,
|
||||
pub repeat_y: bool,
|
||||
}
|
||||
|
||||
impl SurfaceStyle {
|
||||
pub fn new(
|
||||
surface_data: Vec<u8>,
|
||||
surface_size: Size2D<u32>,
|
||||
repeat_x: bool,
|
||||
repeat_y: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
surface_data: ByteBuf::from(surface_data),
|
||||
surface_size,
|
||||
repeat_x,
|
||||
repeat_y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum FillOrStrokeStyle {
|
||||
Color(RGBA),
|
||||
LinearGradient(LinearGradientStyle),
|
||||
RadialGradient(RadialGradientStyle),
|
||||
Surface(SurfaceStyle),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum LineCapStyle {
|
||||
Butt = 0,
|
||||
Round = 1,
|
||||
Square = 2,
|
||||
}
|
||||
|
||||
impl FromStr for LineCapStyle {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<LineCapStyle, ()> {
|
||||
match string {
|
||||
"butt" => Ok(LineCapStyle::Butt),
|
||||
"round" => Ok(LineCapStyle::Round),
|
||||
"square" => Ok(LineCapStyle::Square),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum LineJoinStyle {
|
||||
Round = 0,
|
||||
Bevel = 1,
|
||||
Miter = 2,
|
||||
}
|
||||
|
||||
impl FromStr for LineJoinStyle {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<LineJoinStyle, ()> {
|
||||
match string {
|
||||
"round" => Ok(LineJoinStyle::Round),
|
||||
"bevel" => Ok(LineJoinStyle::Bevel),
|
||||
"miter" => Ok(LineJoinStyle::Miter),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
|
||||
pub enum RepetitionStyle {
|
||||
Repeat,
|
||||
RepeatX,
|
||||
RepeatY,
|
||||
NoRepeat,
|
||||
}
|
||||
|
||||
impl FromStr for RepetitionStyle {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<RepetitionStyle, ()> {
|
||||
match string {
|
||||
"repeat" => Ok(RepetitionStyle::Repeat),
|
||||
"repeat-x" => Ok(RepetitionStyle::RepeatX),
|
||||
"repeat-y" => Ok(RepetitionStyle::RepeatY),
|
||||
"no-repeat" => Ok(RepetitionStyle::NoRepeat),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum CompositionStyle {
|
||||
SrcIn,
|
||||
SrcOut,
|
||||
SrcOver,
|
||||
SrcAtop,
|
||||
DestIn,
|
||||
DestOut,
|
||||
DestOver,
|
||||
DestAtop,
|
||||
Copy,
|
||||
Lighter,
|
||||
Xor,
|
||||
Clear,
|
||||
}
|
||||
|
||||
impl FromStr for CompositionStyle {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<CompositionStyle, ()> {
|
||||
match string {
|
||||
"source-in" => Ok(CompositionStyle::SrcIn),
|
||||
"source-out" => Ok(CompositionStyle::SrcOut),
|
||||
"source-over" => Ok(CompositionStyle::SrcOver),
|
||||
"source-atop" => Ok(CompositionStyle::SrcAtop),
|
||||
"destination-in" => Ok(CompositionStyle::DestIn),
|
||||
"destination-out" => Ok(CompositionStyle::DestOut),
|
||||
"destination-over" => Ok(CompositionStyle::DestOver),
|
||||
"destination-atop" => Ok(CompositionStyle::DestAtop),
|
||||
"copy" => Ok(CompositionStyle::Copy),
|
||||
"lighter" => Ok(CompositionStyle::Lighter),
|
||||
"xor" => Ok(CompositionStyle::Xor),
|
||||
"clear" => Ok(CompositionStyle::Clear),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CompositionStyle {
|
||||
pub fn to_str(&self) -> &str {
|
||||
match *self {
|
||||
CompositionStyle::SrcIn => "source-in",
|
||||
CompositionStyle::SrcOut => "source-out",
|
||||
CompositionStyle::SrcOver => "source-over",
|
||||
CompositionStyle::SrcAtop => "source-atop",
|
||||
CompositionStyle::DestIn => "destination-in",
|
||||
CompositionStyle::DestOut => "destination-out",
|
||||
CompositionStyle::DestOver => "destination-over",
|
||||
CompositionStyle::DestAtop => "destination-atop",
|
||||
CompositionStyle::Copy => "copy",
|
||||
CompositionStyle::Lighter => "lighter",
|
||||
CompositionStyle::Xor => "xor",
|
||||
CompositionStyle::Clear => "clear",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum BlendingStyle {
|
||||
Multiply,
|
||||
Screen,
|
||||
Overlay,
|
||||
Darken,
|
||||
Lighten,
|
||||
ColorDodge,
|
||||
ColorBurn,
|
||||
HardLight,
|
||||
SoftLight,
|
||||
Difference,
|
||||
Exclusion,
|
||||
Hue,
|
||||
Saturation,
|
||||
Color,
|
||||
Luminosity,
|
||||
}
|
||||
|
||||
impl FromStr for BlendingStyle {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<BlendingStyle, ()> {
|
||||
match string {
|
||||
"multiply" => Ok(BlendingStyle::Multiply),
|
||||
"screen" => Ok(BlendingStyle::Screen),
|
||||
"overlay" => Ok(BlendingStyle::Overlay),
|
||||
"darken" => Ok(BlendingStyle::Darken),
|
||||
"lighten" => Ok(BlendingStyle::Lighten),
|
||||
"color-dodge" => Ok(BlendingStyle::ColorDodge),
|
||||
"color-burn" => Ok(BlendingStyle::ColorBurn),
|
||||
"hard-light" => Ok(BlendingStyle::HardLight),
|
||||
"soft-light" => Ok(BlendingStyle::SoftLight),
|
||||
"difference" => Ok(BlendingStyle::Difference),
|
||||
"exclusion" => Ok(BlendingStyle::Exclusion),
|
||||
"hue" => Ok(BlendingStyle::Hue),
|
||||
"saturation" => Ok(BlendingStyle::Saturation),
|
||||
"color" => Ok(BlendingStyle::Color),
|
||||
"luminosity" => Ok(BlendingStyle::Luminosity),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlendingStyle {
|
||||
pub fn to_str(&self) -> &str {
|
||||
match *self {
|
||||
BlendingStyle::Multiply => "multiply",
|
||||
BlendingStyle::Screen => "screen",
|
||||
BlendingStyle::Overlay => "overlay",
|
||||
BlendingStyle::Darken => "darken",
|
||||
BlendingStyle::Lighten => "lighten",
|
||||
BlendingStyle::ColorDodge => "color-dodge",
|
||||
BlendingStyle::ColorBurn => "color-burn",
|
||||
BlendingStyle::HardLight => "hard-light",
|
||||
BlendingStyle::SoftLight => "soft-light",
|
||||
BlendingStyle::Difference => "difference",
|
||||
BlendingStyle::Exclusion => "exclusion",
|
||||
BlendingStyle::Hue => "hue",
|
||||
BlendingStyle::Saturation => "saturation",
|
||||
BlendingStyle::Color => "color",
|
||||
BlendingStyle::Luminosity => "luminosity",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum CompositionOrBlending {
|
||||
Composition(CompositionStyle),
|
||||
Blending(BlendingStyle),
|
||||
}
|
||||
|
||||
impl Default for CompositionOrBlending {
|
||||
fn default() -> CompositionOrBlending {
|
||||
CompositionOrBlending::Composition(CompositionStyle::SrcOver)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for CompositionOrBlending {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<CompositionOrBlending, ()> {
|
||||
if let Ok(op) = CompositionStyle::from_str(string) {
|
||||
return Ok(CompositionOrBlending::Composition(op));
|
||||
}
|
||||
|
||||
if let Ok(op) = BlendingStyle::from_str(string) {
|
||||
return Ok(CompositionOrBlending::Blending(op));
|
||||
}
|
||||
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum TextAlign {
|
||||
Start,
|
||||
End,
|
||||
Left,
|
||||
Right,
|
||||
Center,
|
||||
}
|
||||
|
||||
impl FromStr for TextAlign {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<TextAlign, ()> {
|
||||
match string {
|
||||
"start" => Ok(TextAlign::Start),
|
||||
"end" => Ok(TextAlign::End),
|
||||
"left" => Ok(TextAlign::Left),
|
||||
"right" => Ok(TextAlign::Right),
|
||||
"center" => Ok(TextAlign::Center),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TextAlign {
|
||||
fn default() -> TextAlign {
|
||||
TextAlign::Start
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum TextBaseline {
|
||||
Top,
|
||||
Hanging,
|
||||
Middle,
|
||||
Alphabetic,
|
||||
Ideographic,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
impl FromStr for TextBaseline {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<TextBaseline, ()> {
|
||||
match string {
|
||||
"top" => Ok(TextBaseline::Top),
|
||||
"hanging" => Ok(TextBaseline::Hanging),
|
||||
"middle" => Ok(TextBaseline::Middle),
|
||||
"alphabetic" => Ok(TextBaseline::Alphabetic),
|
||||
"ideographic" => Ok(TextBaseline::Ideographic),
|
||||
"bottom" => Ok(TextBaseline::Bottom),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TextBaseline {
|
||||
fn default() -> TextBaseline {
|
||||
TextBaseline::Alphabetic
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum Direction {
|
||||
Ltr,
|
||||
Rtl,
|
||||
Inherit,
|
||||
}
|
||||
|
||||
impl FromStr for Direction {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(string: &str) -> Result<Direction, ()> {
|
||||
match string {
|
||||
"ltr" => Ok(Direction::Ltr),
|
||||
"rtl" => Ok(Direction::Rtl),
|
||||
"inherit" => Ok(Direction::Inherit),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Direction {
|
||||
fn default() -> Direction {
|
||||
Direction::Inherit
|
||||
}
|
||||
}
|
26
components/shared/canvas/lib.rs
Normal file
26
components/shared/canvas/lib.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* 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/. */
|
||||
|
||||
#![crate_name = "canvas_traits"]
|
||||
#![crate_type = "rlib"]
|
||||
#![deny(unsafe_code)]
|
||||
|
||||
use crossbeam_channel::Sender;
|
||||
use euclid::default::Size2D;
|
||||
|
||||
use crate::canvas::CanvasId;
|
||||
|
||||
pub mod canvas;
|
||||
#[macro_use]
|
||||
pub mod webgl;
|
||||
mod webgl_channel;
|
||||
|
||||
pub enum ConstellationCanvasMsg {
|
||||
Create {
|
||||
id_sender: Sender<CanvasId>,
|
||||
size: Size2D<u64>,
|
||||
antialias: bool,
|
||||
},
|
||||
Exit,
|
||||
}
|
1431
components/shared/canvas/webgl.rs
Normal file
1431
components/shared/canvas/webgl.rs
Normal file
File diff suppressed because it is too large
Load diff
15
components/shared/canvas/webgl_channel/ipc.rs
Normal file
15
components/shared/canvas/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 https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::io;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
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()
|
||||
}
|
149
components/shared/canvas/webgl_channel/mod.rs
Normal file
149
components/shared/canvas/webgl_channel/mod.rs
Normal file
|
@ -0,0 +1,149 @@
|
|||
/* 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(|_| ()),
|
||||
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 try_recv(&self) -> Result<T, ()> {
|
||||
match *self {
|
||||
WebGLReceiver::Ipc(ref receiver) => receiver.try_recv().map_err(|_| ()),
|
||||
WebGLReceiver::Mpsc(ref receiver) => receiver.try_recv().map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
|
||||
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>() -> 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, 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()
|
||||
}
|
||||
}
|
62
components/shared/canvas/webgl_channel/mpsc.rs
Normal file
62
components/shared/canvas/webgl_channel/mpsc.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
/* 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, Deserializer, Serialize, Serializer};
|
||||
|
||||
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 WebGLSender<T>(crossbeam_channel::Sender<T>);
|
||||
pub struct WebGLReceiver<T>(crossbeam_channel::Receiver<T>);
|
||||
|
||||
impl<T> Clone for WebGLSender<T> {
|
||||
fn clone(&self) -> Self {
|
||||
WebGLSender(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WebGLSender<T> {
|
||||
#[inline]
|
||||
pub fn send(&self, data: T) -> Result<(), crossbeam_channel::SendError<T>> {
|
||||
self.0.send(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WebGLReceiver<T> {
|
||||
#[inline]
|
||||
pub fn recv(&self) -> Result<T, crossbeam_channel::RecvError> {
|
||||
self.0.recv()
|
||||
}
|
||||
#[inline]
|
||||
pub fn try_recv(&self) -> Result<T, crossbeam_channel::TryRecvError> {
|
||||
self.0.try_recv()
|
||||
}
|
||||
pub fn into_inner(self) -> crossbeam_channel::Receiver<T> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn webgl_channel<T>() -> Result<(WebGLSender<T>, WebGLReceiver<T>), ()> {
|
||||
let (sender, receiver) = crossbeam_channel::unbounded();
|
||||
Ok((WebGLSender(sender), WebGLReceiver(receiver)))
|
||||
}
|
||||
|
||||
unreachable_serializable!(WebGLReceiver);
|
||||
unreachable_serializable!(WebGLSender);
|
Loading…
Add table
Add a link
Reference in a new issue