euv-core 0.52.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Encodes normalized `[u0, v0, u1, v1]` coordinates inside an `(atlas_w, atlas_h)` texture into `[u16; 4]`.
///
/// # Arguments
///
/// - `u32` - Placement X origin in texels.
/// - `u32` - Placement Y origin in texels.
/// - `u32` - Placement width in texels.
/// - `u32` - Placement height in texels.
/// - `u32` - Backing atlas width in texels.
/// - `u32` - Backing atlas height in texels.
///
/// # Returns
///
/// - `[u16; 4]` - Quantized normalized UV rectangle `[u0, v0, u1, v1]` in `[0..=65535]`.
#[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]
}

/// Computes a fast deterministic 64-bit content signature over an RGBA8 pixel slice and its dimensions.
///
/// # Arguments
///
/// - `u32` - Source image width in pixels.
/// - `u32` - Source image height in pixels.
/// - `&[u8]` - Tightly packed RGBA8 pixel slice.
///
/// # Returns
///
/// - `u64` - Fast deterministic content hash for GPU image atlas caching.
#[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
}

/// Coalesces a sorted list of dirty byte spans separated by at most `max_gap_bytes` in place.
///
/// # Arguments
///
/// - `&mut Vec<DirtyBufferSpan>` - Dirty spans to merge in place.
/// - `usize` - Maximum gap in bytes between adjacent dirty spans that triggers merging.
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);
}