Merge pull request #7385 from penpot/elenatorro-improve-image-load-performance

🔧 Improve image parsing performance
This commit is contained in:
Elena Torró
2025-09-25 17:20:49 +02:00
committed by GitHub
3 changed files with 81 additions and 48 deletions

View File

@@ -300,34 +300,6 @@ pub extern "C" fn set_children() {
}
}
#[no_mangle]
pub extern "C" fn store_image(
a1: u32,
b1: u32,
c1: u32,
d1: u32,
a2: u32,
b2: u32,
c2: u32,
d2: u32,
) {
with_state_mut!(state, {
let image_id = uuid_from_u32_quartet(a2, b2, c2, d2);
let image_bytes = mem::bytes();
if let Err(msg) = state.render_state_mut().add_image(image_id, &image_bytes) {
eprintln!("{}", msg);
}
mem::free_bytes();
});
with_state_mut!(state, {
let shape_id = uuid_from_u32_quartet(a1, b1, c1, d1);
state.update_tile_for_shape(shape_id);
});
}
#[no_mangle]
pub extern "C" fn is_image_cached(a: u32, b: u32, c: u32, d: u32) -> bool {
with_state_mut!(state, {

View File

@@ -1,6 +1,12 @@
use crate::mem;
use crate::mem::SerializableResult;
use crate::uuid::Uuid;
use crate::with_state_mut;
use crate::STATE;
use crate::{shapes::ImageFill, utils::uuid_from_u32_quartet};
const FLAG_KEEP_ASPECT_RATIO: u8 = 1 << 0;
const IMAGE_IDS_SIZE: usize = 32;
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
@@ -31,3 +37,50 @@ impl From<RawImageFillData> for ImageFill {
)
}
}
#[repr(C)]
#[derive(Clone, Debug)]
pub struct ShapeImageIds {
shape_id: Uuid,
image_id: Uuid,
}
impl From<[u8; IMAGE_IDS_SIZE]> for ShapeImageIds {
fn from(bytes: [u8; IMAGE_IDS_SIZE]) -> Self {
let shape_id = Uuid::from_bytes(bytes[0..16].try_into().unwrap());
let image_id = Uuid::from_bytes(bytes[16..32].try_into().unwrap());
ShapeImageIds { shape_id, image_id }
}
}
impl TryFrom<Vec<u8>> for ShapeImageIds {
type Error = &'static str;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let mut arr = [0u8; IMAGE_IDS_SIZE];
arr.copy_from_slice(&value);
Ok(ShapeImageIds::from(arr))
}
}
#[no_mangle]
pub extern "C" fn store_image() {
let bytes = mem::bytes();
let ids = ShapeImageIds::try_from(bytes[0..IMAGE_IDS_SIZE].to_vec()).unwrap();
let image_bytes = &bytes[IMAGE_IDS_SIZE..];
with_state_mut!(state, {
if let Err(msg) = state
.render_state_mut()
.add_image(ids.image_id, image_bytes)
{
eprintln!("{}", msg);
}
});
with_state_mut!(state, {
state.update_tile_for_shape(ids.shape_id);
});
mem::free_bytes();
}