Skip to content

Truly independent web browser

LibGfx: Derive IPC codecs

Aliaksandr Kalenikb9d368ada463
diff --git a/Libraries/LibGfx/Rust/src/color.rs b/Libraries/LibGfx/Rust/src/color.rs
index fd4f1c624e7..1f4a60accac 100644
--- a/Libraries/LibGfx/Rust/src/color.rs
+++ b/Libraries/LibGfx/Rust/src/color.rs
@@ -5,8 +5,9 @@
*/
use ak::math;
+use libipc_rust::wire::{Decode, Encode};
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(transparent)]
pub struct Color(pub u32);
diff --git a/Libraries/LibGfx/Rust/src/corner_radii.rs b/Libraries/LibGfx/Rust/src/corner_radii.rs
index 1ecf8799303..2d458a61484 100644
--- a/Libraries/LibGfx/Rust/src/corner_radii.rs
+++ b/Libraries/LibGfx/Rust/src/corner_radii.rs
@@ -5,8 +5,9 @@
*/
use crate::geometry::{FloatPoint, FloatRect, IntPoint, IntRect};
+use libipc_rust::wire::{Decode, Encode};
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(C)]
pub struct CornerRadius {
pub horizontal_radius: i32,
@@ -19,7 +20,7 @@ impl CornerRadius {
}
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(C)]
pub struct CornerRadii {
pub top_left: CornerRadius,
diff --git a/Libraries/LibGfx/Rust/src/filter.rs b/Libraries/LibGfx/Rust/src/filter.rs
index 0ceb0a02290..abb4098f4b9 100644
--- a/Libraries/LibGfx/Rust/src/filter.rs
+++ b/Libraries/LibGfx/Rust/src/filter.rs
@@ -23,7 +23,7 @@ use crate::{
use libipc_rust::wire::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder};
/// Which channel of a displacement map input drives the displacement along one axis.
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum ChannelSelector {
Red,
@@ -33,16 +33,7 @@ pub enum ChannelSelector {
Alpha,
}
-impl ChannelSelector {
- pub fn from_i32(value: i32) -> Option<Self> {
- (Self::Red as i32..=Self::Alpha as i32).contains(&value).then(|| {
- // SAFETY: the enum is a dense i32 range starting at zero.
- unsafe { std::mem::transmute::<i32, Self>(value) }
- })
- }
-}
-
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum TurbulenceType {
FractalNoise,
@@ -50,19 +41,8 @@ pub enum TurbulenceType {
Turbulence,
}
-impl TurbulenceType {
- pub fn from_i32(value: i32) -> Option<Self> {
- (Self::FractalNoise as i32..=Self::Turbulence as i32)
- .contains(&value)
- .then(|| {
- // SAFETY: the enum is a dense i32 range starting at zero.
- unsafe { std::mem::transmute::<i32, Self>(value) }
- })
- }
-}
-
/// The operation tag that starts every serialized filter node.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Encode, Decode)]
#[repr(u8)]
pub enum FilterOperationType {
Arithmetic,
@@ -87,15 +67,6 @@ pub enum FilterOperationType {
Crop,
}
-impl FilterOperationType {
- pub fn from_u8(value: u8) -> Option<Self> {
- (value <= Self::Crop as u8).then(|| {
- // SAFETY: the enum is a dense u8 range starting at zero.
- unsafe { std::mem::transmute::<u8, Self>(value) }
- })
- }
-}
-
/// A 256-entry lookup table for one channel of a component transfer.
pub type ChannelTable = Box<[u8; 256]>;
@@ -707,22 +678,6 @@ fn blended_output_bounds(painted: PaintedInputs, foreground: OutputBounds, backg
}
}
-impl Encode for FilterOperationType {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&(*self as u8))
- }
-}
-
-impl Decode for FilterOperationType {
- const MIN_ENCODED_SIZE: usize = size_of::<u8>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Self::from_u8(decoder.decode()?).ok_or(DecodeError::InvalidValue("Gfx::FilterOperationType"))
- }
-}
-
impl Encode for Filter {
fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
encoder.encode(&self.operation_type())?;
diff --git a/Libraries/LibGfx/Rust/src/geometry.rs b/Libraries/LibGfx/Rust/src/geometry.rs
index 74d7fcef1a5..4020801d976 100644
--- a/Libraries/LibGfx/Rust/src/geometry.rs
+++ b/Libraries/LibGfx/Rust/src/geometry.rs
@@ -4,16 +4,17 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use libipc_rust::wire::{Decode, Encode};
use std::ops::{Add, Mul, Neg, Sub};
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(C)]
pub struct IntPoint {
pub x: i32,
pub y: i32,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Encode, Decode)]
#[repr(C)]
pub struct FloatPoint {
pub x: f32,
@@ -28,7 +29,7 @@ pub struct FloatVector3 {
pub z: f32,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(C)]
pub struct IntSize {
pub width: i32,
@@ -41,7 +42,7 @@ impl IntSize {
}
}
-#[derive(Clone, Copy, Debug, Default, PartialEq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Encode, Decode)]
#[repr(C)]
pub struct FloatSize {
pub width: f32,
@@ -54,7 +55,7 @@ impl FloatSize {
}
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Encode, Decode)]
#[repr(C)]
pub struct IntRect {
pub x: i32,
@@ -63,7 +64,7 @@ pub struct IntRect {
pub height: i32,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Encode, Decode)]
#[repr(C)]
pub struct FloatRect {
pub x: f32,
diff --git a/Libraries/LibGfx/Rust/src/ipc.rs b/Libraries/LibGfx/Rust/src/ipc.rs
index ac698c70bbb..d6a0a6dbb24 100644
--- a/Libraries/LibGfx/Rust/src/ipc.rs
+++ b/Libraries/LibGfx/Rust/src/ipc.rs
@@ -6,157 +6,9 @@
//! Codecs for the Gfx values carried by IPC.
-use crate::filter::{ChannelSelector, TurbulenceType};
-use crate::{
- Color, ColorFilterType, CompositingAndBlendingOperator, CornerRadii, CornerRadius, FloatMatrix4x4, FloatPoint,
- FloatRect, FloatSize, IntPoint, IntRect, IntSize, InterpolationColorSpace, MaskKind, ScalingMode, WindingRule,
-};
+use crate::FloatMatrix4x4;
use libipc_rust::wire::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder};
-impl Encode for IntPoint {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.x)?;
- encoder.encode(&self.y)
- }
-}
-
-impl Decode for IntPoint {
- const MIN_ENCODED_SIZE: usize = 2 * size_of::<i32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- x: decoder.decode()?,
- y: decoder.decode()?,
- })
- }
-}
-
-impl Encode for FloatPoint {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.x)?;
- encoder.encode(&self.y)
- }
-}
-
-impl Decode for FloatPoint {
- const MIN_ENCODED_SIZE: usize = 2 * size_of::<f32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- x: decoder.decode()?,
- y: decoder.decode()?,
- })
- }
-}
-
-impl Encode for IntSize {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.width)?;
- encoder.encode(&self.height)
- }
-}
-
-impl Decode for IntSize {
- const MIN_ENCODED_SIZE: usize = 2 * size_of::<i32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- width: decoder.decode()?,
- height: decoder.decode()?,
- })
- }
-}
-
-impl Encode for IntRect {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.x)?;
- encoder.encode(&self.y)?;
- encoder.encode(&self.width)?;
- encoder.encode(&self.height)
- }
-}
-
-impl Decode for IntRect {
- const MIN_ENCODED_SIZE: usize = 4 * size_of::<i32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- x: decoder.decode()?,
- y: decoder.decode()?,
- width: decoder.decode()?,
- height: decoder.decode()?,
- })
- }
-}
-
-impl Encode for FloatRect {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.x)?;
- encoder.encode(&self.y)?;
- encoder.encode(&self.width)?;
- encoder.encode(&self.height)
- }
-}
-
-impl Decode for FloatRect {
- const MIN_ENCODED_SIZE: usize = 4 * size_of::<f32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- x: decoder.decode()?,
- y: decoder.decode()?,
- width: decoder.decode()?,
- height: decoder.decode()?,
- })
- }
-}
-
-impl Encode for Color {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.0)
- }
-}
-
-impl Decode for Color {
- const MIN_ENCODED_SIZE: usize = size_of::<u32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self(decoder.decode()?))
- }
-}
-
-impl Encode for FloatSize {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.width)?;
- encoder.encode(&self.height)
- }
-}
-
-impl Decode for FloatSize {
- const MIN_ENCODED_SIZE: usize = 2 * size_of::<f32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- width: decoder.decode()?,
- height: decoder.decode()?,
- })
- }
-}
-
impl Encode for FloatMatrix4x4 {
#[inline]
fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
@@ -180,99 +32,12 @@ impl Decode for FloatMatrix4x4 {
}
}
-impl Encode for CornerRadius {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.horizontal_radius)?;
- encoder.encode(&self.vertical_radius)
- }
-}
-
-impl Decode for CornerRadius {
- const MIN_ENCODED_SIZE: usize = 2 * size_of::<i32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- horizontal_radius: decoder.decode()?,
- vertical_radius: decoder.decode()?,
- })
- }
-}
-
-impl Encode for CornerRadii {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.top_left)?;
- encoder.encode(&self.top_right)?;
- encoder.encode(&self.bottom_right)?;
- encoder.encode(&self.bottom_left)
- }
-}
-
-impl Decode for CornerRadii {
- const MIN_ENCODED_SIZE: usize = 4 * CornerRadius::MIN_ENCODED_SIZE;
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- top_left: decoder.decode()?,
- top_right: decoder.decode()?,
- bottom_right: decoder.decode()?,
- bottom_left: decoder.decode()?,
- })
- }
-}
-
-macro_rules! impl_codec_of_enum_going_as_i32 {
- ($type:ident, $type_name:literal, $from_i32:expr) => {
- impl Encode for $type {
- #[inline]
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&(*self as i32))
- }
- }
-
- impl Decode for $type {
- const MIN_ENCODED_SIZE: usize = size_of::<i32>();
-
- #[inline]
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- let from_i32: fn(i32) -> Option<Self> = $from_i32;
- from_i32(decoder.decode()?).ok_or(DecodeError::InvalidValue($type_name))
- }
- }
- };
-}
-
-impl_codec_of_enum_going_as_i32!(WindingRule, "Gfx::WindingRule", |value| match value {
- value if value == WindingRule::Nonzero as i32 => Some(WindingRule::Nonzero),
- value if value == WindingRule::EvenOdd as i32 => Some(WindingRule::EvenOdd),
- _ => None,
-});
-impl_codec_of_enum_going_as_i32!(MaskKind, "Gfx::MaskKind", |value| match value {
- value if value == MaskKind::Alpha as i32 => Some(MaskKind::Alpha),
- value if value == MaskKind::Luminance as i32 => Some(MaskKind::Luminance),
- _ => None,
-});
-impl_codec_of_enum_going_as_i32!(
- CompositingAndBlendingOperator,
- "Gfx::CompositingAndBlendingOperator",
- CompositingAndBlendingOperator::from_i32
-);
-impl_codec_of_enum_going_as_i32!(ColorFilterType, "Gfx::ColorFilterType", ColorFilterType::from_i32);
-impl_codec_of_enum_going_as_i32!(ScalingMode, "Gfx::ScalingMode", ScalingMode::from_i32);
-impl_codec_of_enum_going_as_i32!(
- InterpolationColorSpace,
- "Gfx::InterpolationColorSpace",
- InterpolationColorSpace::from_i32
-);
-impl_codec_of_enum_going_as_i32!(ChannelSelector, "Gfx::ChannelSelector", ChannelSelector::from_i32);
-impl_codec_of_enum_going_as_i32!(TurbulenceType, "Gfx::TurbulenceType", TurbulenceType::from_i32);
-
#[cfg(test)]
mod tests {
use super::*;
+ use crate::{
+ ColorFilterType, CompositingAndBlendingOperator, CornerRadii, CornerRadius, FloatSize, MaskKind, WindingRule,
+ };
fn encoded<T: Encode>(value: &T) -> Vec<u8> {
let mut encoder = Encoder::new();
@@ -346,19 +111,19 @@ mod tests {
assert_eq!(
decoded::<WindingRule>(&encoded(&2i32)),
- Err(DecodeError::InvalidValue("Gfx::WindingRule"))
+ Err(DecodeError::InvalidValue("WindingRule"))
);
assert_eq!(
decoded::<MaskKind>(&encoded(&-1i32)),
- Err(DecodeError::InvalidValue("Gfx::MaskKind"))
+ Err(DecodeError::InvalidValue("MaskKind"))
);
assert_eq!(
decoded::<CompositingAndBlendingOperator>(&encoded(&0i32)),
- Err(DecodeError::InvalidValue("Gfx::CompositingAndBlendingOperator"))
+ Err(DecodeError::InvalidValue("CompositingAndBlendingOperator"))
);
assert_eq!(
decoded::<ColorFilterType>(&encoded(&(ColorFilterType::Sepia as i32 + 1))),
- Err(DecodeError::InvalidValue("Gfx::ColorFilterType"))
+ Err(DecodeError::InvalidValue("ColorFilterType"))
);
}
}
diff --git a/Libraries/LibGfx/Rust/src/paint_enums.rs b/Libraries/LibGfx/Rust/src/paint_enums.rs
index 85d4e2ca5c0..0562eb4d9f4 100644
--- a/Libraries/LibGfx/Rust/src/paint_enums.rs
+++ b/Libraries/LibGfx/Rust/src/paint_enums.rs
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+use libipc_rust::wire::{Decode, Encode};
+
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum WindingRule {
#[default]
@@ -31,7 +33,7 @@ pub enum LineStyle {
Dashed,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum ScalingMode {
#[default]
@@ -41,7 +43,7 @@ pub enum ScalingMode {
NearestNeighbor,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum CompositingAndBlendingOperator {
#[default]
@@ -88,7 +90,7 @@ impl CompositingAndBlendingOperator {
}
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum MaskKind {
#[default]
@@ -104,7 +106,7 @@ pub enum ShouldAntiAlias {
No = 1,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum InterpolationColorSpace {
LinearRGB,
@@ -185,7 +187,7 @@ pub enum JoinStyle {
Bevel,
}
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Encode, Decode)]
#[repr(i32)]
pub enum ColorFilterType {
#[default]
@@ -198,27 +200,6 @@ pub enum ColorFilterType {
Sepia,
}
-impl ScalingMode {
- pub fn from_i32(value: i32) -> Option<Self> {
- if (Self::None as i32..=Self::NearestNeighbor as i32).contains(&value) {
- // SAFETY: the enum is a dense i32 range starting at zero.
- Some(unsafe { std::mem::transmute::<i32, Self>(value) })
- } else {
- None
- }
- }
-}
-
-impl InterpolationColorSpace {
- pub fn from_i32(value: i32) -> Option<Self> {
- match value {
- value if value == Self::LinearRGB as i32 => Some(Self::LinearRGB),
- value if value == Self::SRGB as i32 => Some(Self::SRGB),
- _ => None,
- }
- }
-}
-
impl ColorFilterType {
pub fn from_i32(value: i32) -> Option<Self> {
if (Self::Brightness as i32..=Self::Sepia as i32).contains(&value) {