use super::*;
#[must_use]
pub(crate) fn encode_glyph_atlas_uv(
pos_x: u32,
pos_y: u32,
width: u32,
height: u32,
atlas_w: u32,
atlas_h: u32,
) -> [u16; 4] {
let inv_w: f32 = 1.0 / (atlas_w.max(1) as f32);
let inv_h: f32 = 1.0 / (atlas_h.max(1) as f32);
let u0: u16 = ((pos_x as f32) * inv_w * 65535.0)
.round()
.clamp(0.0, 65535.0) as u16;
let v0: u16 = ((pos_y as f32) * inv_h * 65535.0)
.round()
.clamp(0.0, 65535.0) as u16;
let u1: u16 = (((pos_x.saturating_add(width)) as f32) * inv_w * 65535.0)
.round()
.clamp(0.0, 65535.0) as u16;
let v1: u16 = (((pos_y.saturating_add(height)) as f32) * inv_h * 65535.0)
.round()
.clamp(0.0, 65535.0) as u16;
[u0, v0, u1, v1]
}
#[must_use]
pub(crate) fn hash_rgba8_image_content(width: u32, height: u32, pixels: &[u8]) -> u64 {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325_u64;
let prime: u64 = 0x0000_0100_0000_01b3_u64;
hash = (hash ^ u64::from(width)).wrapping_mul(prime);
hash = (hash ^ u64::from(height)).wrapping_mul(prime);
hash = (hash ^ (pixels.len() as u64)).wrapping_mul(prime);
let len: usize = pixels.len();
if len <= 256 {
for byte_val in pixels.iter() {
hash = (hash ^ u64::from(*byte_val)).wrapping_mul(prime);
}
return hash;
}
let step: usize = (len / 64).max(4);
let mut offset: usize = 0;
while offset < len {
let word: u64 = if offset + 4 <= len {
u64::from(u32::from_le_bytes([
pixels[offset],
pixels[offset + 1],
pixels[offset + 2],
pixels[offset + 3],
]))
} else {
u64::from(pixels[offset])
};
hash = (hash ^ word).wrapping_mul(prime);
offset = offset.saturating_add(step);
}
for byte_val in &pixels[len.saturating_sub(32)..] {
hash = (hash ^ u64::from(*byte_val)).wrapping_mul(prime);
}
hash
}
pub fn coalesce_dirty_buffer_spans(spans: &mut Vec<DirtyBufferSpan>, max_gap_bytes: usize) {
if spans.len() <= 1 {
return;
}
spans.sort_by_key(|span: &DirtyBufferSpan| span.get_byte_offset());
let mut write_idx: usize = 0;
for read_idx in 1..spans.len() {
let cur: DirtyBufferSpan = spans[write_idx];
let next: DirtyBufferSpan = spans[read_idx];
let cur_end: usize = cur.get_byte_offset().saturating_add(cur.get_byte_len());
if next.get_byte_offset() <= cur_end.saturating_add(max_gap_bytes) {
let next_end: usize = next.get_byte_offset().saturating_add(next.get_byte_len());
let merged_end: usize = cur_end.max(next_end);
spans[write_idx] = DirtyBufferSpan::new(
cur.get_byte_offset(),
merged_end.saturating_sub(cur.get_byte_offset()),
);
} else {
write_idx += 1;
spans[write_idx] = next;
}
}
spans.truncate(write_idx + 1);
}