euv-core 0.53.1

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::*;

/// Clamps a raw display DPI scale multiplier into `[VIEWPORT_SCALE_MIN, VIEWPORT_SCALE_MAX]`, defaulting non-finite or non-positive values to `1.0`.
///
/// # Arguments
///
/// - `f32` - Raw display scale factor reported by the OS or browser.
///
/// # Returns
///
/// - `f32` - Validated scale factor in `[0.25, 16.0]`.
#[must_use]
pub fn clamp_viewport_scale(raw_scale: f32) -> f32 {
    if raw_scale.is_finite() && raw_scale > 0.0 {
        raw_scale.clamp(VIEWPORT_SCALE_MIN, VIEWPORT_SCALE_MAX)
    } else {
        1.0
    }
}

/// Computes an exact uncapped physical pixel extent from a logical dimension and DPI scale factor.
///
/// # Arguments
///
/// - `f64` - Logical extent in CSS or point coordinates.
/// - `f32` - Display DPI scale factor.
///
/// # Returns
///
/// - `u32` - Lossless physical pixel extent (`>= 1`).
#[must_use]
pub fn compute_lossless_physical_dim(logical_extent: f64, scale_factor: f32) -> u32 {
    let safe_logical: f64 = if logical_extent.is_finite() && logical_extent > 0.0 {
        logical_extent.max(1.0)
    } else {
        1.0
    };
    let safe_scale: f64 = f64::from(clamp_viewport_scale(scale_factor));
    let raw_phys: f64 = (safe_logical * safe_scale).round();
    if !raw_phys.is_finite() || raw_phys < 1.0 {
        1
    } else if raw_phys >= f64::from(u32::MAX) {
        u32::MAX
    } else {
        raw_phys as u32
    }
}

/// Resolves balanced tile dimensions `(tile_w, tile_h)` strictly within `limits` while maximizing tile coverage and minimizing tile count.
///
/// # Arguments
///
/// - `u32` - Full viewport physical width in device pixels.
/// - `u32` - Full viewport physical height in device pixels.
/// - `SurfaceTileLimit` - Hardware maximum single-axis extent and pixel area.
///
/// # Returns
///
/// - `(u32, u32)` - Maximum tile `(width, height)` obeying both `<= max_texture_dim` and `width * height <= max_surface_area`.
#[must_use]
pub fn compute_effective_tile_dims(
    physical_width: u32,
    physical_height: u32,
    limits: SurfaceTileLimit,
) -> (u32, u32) {
    let phys_w: u32 = physical_width.max(1);
    let phys_h: u32 = physical_height.max(1);
    let max_dim: u32 = limits.get_max_texture_dim().max(MIN_SURFACE_TILE_DIM);
    let min_area: u64 =
        u64::from(MIN_SURFACE_TILE_DIM).saturating_mul(u64::from(MIN_SURFACE_TILE_DIM));
    let max_area: u64 = limits.get_max_surface_area().max(min_area);
    let mut tile_w: u32 = phys_w.min(max_dim);
    let mut tile_h: u32 = phys_h.min(max_dim);
    let area: u64 = u64::from(tile_w).saturating_mul(u64::from(tile_h));
    if area <= max_area {
        return (tile_w, tile_h);
    }
    let aspect: f64 = f64::from(tile_w) / f64::from(tile_h.max(1));
    let target_w: u32 = ((max_area as f64) * aspect)
        .sqrt()
        .floor()
        .clamp(1.0, f64::from(tile_w)) as u32;
    tile_w = target_w.max(1).min(max_dim);
    let max_h_for_w: u64 = max_area / u64::from(tile_w);
    tile_h = (max_h_for_w.min(u64::from(tile_h)) as u32).max(1);
    while u64::from(tile_w).saturating_mul(u64::from(tile_h)) > max_area {
        if tile_w >= tile_h && tile_w > 1 {
            tile_w -= 1;
        } else if tile_h > 1 {
            tile_h -= 1;
        } else {
            break;
        }
    }
    (tile_w, tile_h)
}