Send fill + stops data in one call for linear fills

This commit is contained in:
Belén Albeza 2025-04-14 15:39:43 +02:00
parent 16012a3881
commit fccd1a5bd7
4 changed files with 143 additions and 62 deletions

View file

@ -16,7 +16,10 @@ mod wapi;
mod wasm;
use crate::mem::SerializableResult;
use crate::shapes::{BoolType, ConstraintH, ConstraintV, StructureEntry, TransformEntry, Type};
use crate::shapes::{
BoolType, ConstraintH, ConstraintV, StructureEntry, TransformEntry, Type,
RAW_LINEAR_FILL_DATA_SIZE, RAW_STOP_DATA_SIZE,
};
use crate::utils::uuid_from_u32_quartet;
use crate::uuid::Uuid;
use indexmap::IndexSet;
@ -255,18 +258,26 @@ pub extern "C" fn add_shape_solid_fill(raw_color: u32) {
}
#[no_mangle]
pub extern "C" fn add_shape_linear_fill(
start_x: f32,
start_y: f32,
end_x: f32,
end_y: f32,
opacity: f32,
) {
pub extern "C" fn add_shape_linear_fill() {
with_current_shape!(state, |shape: &mut Shape| {
shape.add_fill(shapes::Fill::new_linear_gradient(
(start_x, start_y),
(end_x, end_y),
opacity,
let stops_offset = RAW_LINEAR_FILL_DATA_SIZE;
let bytes = mem::bytes();
let raw_fill_data: [u8; RAW_LINEAR_FILL_DATA_SIZE] =
bytes[0..stops_offset].try_into().unwrap();
let raw_fill = shapes::RawLinearFillData::from(raw_fill_data);
let stops: Vec<shapes::RawStopData> = bytes[stops_offset..]
.chunks(RAW_STOP_DATA_SIZE)
.map(|chunk| {
let data: [u8; RAW_STOP_DATA_SIZE] = chunk.try_into().unwrap();
shapes::RawStopData::from(data)
})
.collect();
shape.add_fill(shapes::Fill::new_linear_gradient_with_stops(
raw_fill.start(),
raw_fill.end(),
raw_fill.opacity(),
stops,
));
});
}

View file

@ -3,16 +3,58 @@ use skia_safe::{self as skia, Rect};
use super::Color;
use crate::uuid::Uuid;
pub const RAW_LINEAR_FILL_DATA_SIZE: usize = 21;
#[derive(Debug)]
#[repr(C)]
pub struct RawLinearFillData {
start_x: f32,
start_y: f32,
end_x: f32,
end_y: f32,
opacity: f32,
stop_count: u8,
}
impl From<[u8; 21]> for RawLinearFillData {
fn from(bytes: [u8; 21]) -> Self {
Self {
start_x: f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
start_y: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
end_x: f32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
end_y: f32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
opacity: f32::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]),
stop_count: bytes[20],
}
}
}
impl RawLinearFillData {
pub fn start(&self) -> (f32, f32) {
(self.start_x, self.start_y)
}
pub fn end(&self) -> (f32, f32) {
(self.end_x, self.end_y)
}
pub fn opacity(&self) -> f32 {
self.opacity
}
}
pub const RAW_STOP_DATA_SIZE: usize = 5;
#[derive(Debug)]
#[repr(C)]
pub struct RawStopData {
color: [u8; 4],
color: u32,
offset: u8,
}
impl RawStopData {
pub fn color(&self) -> skia::Color {
skia::Color::from_argb(self.color[3], self.color[0], self.color[1], self.color[2])
skia::Color::from(self.color)
}
pub fn offset(&self) -> f32 {
@ -20,13 +62,21 @@ impl RawStopData {
}
pub fn from_bytes(bytes: [u8; 5]) -> Self {
let color_bytes: [u8; 4] = bytes[0..4].try_into().unwrap();
Self {
color: [bytes[0], bytes[1], bytes[2], bytes[3]],
color: u32::from_le_bytes(color_bytes),
offset: bytes[4],
}
}
}
impl From<[u8; 5]> for RawStopData {
// TODO: remove from_bytes and copy its implementation here
fn from(bytes: [u8; 5]) -> Self {
Self::from_bytes(bytes)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Gradient {
colors: Vec<Color>,
@ -124,15 +174,31 @@ pub enum Fill {
impl Fill {
pub fn new_linear_gradient(start: (f32, f32), end: (f32, f32), opacity: f32) -> Self {
Self::LinearGradient(Gradient {
Self::new_linear_gradient_with_stops(start, end, opacity, vec![])
}
pub fn new_linear_gradient_with_stops(
start: (f32, f32),
end: (f32, f32),
opacity: f32,
stops: Vec<RawStopData>,
) -> Self {
let mut gradient = Gradient {
start,
end,
opacity,
colors: vec![],
offsets: vec![],
width: 0.,
})
};
for stop in stops {
gradient.add_stop(stop.color(), stop.offset());
}
Self::LinearGradient(gradient)
}
pub fn new_radial_gradient(
start: (f32, f32),
end: (f32, f32),