euv-core 0.48.2

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

/// Evaluates whether sparse CPU-local effect rasterization plus GPU compositing yields net positive gain over both pure GPU and pure CPU.
///
/// # Arguments
///
/// - `u32` - Physical viewport width in pixels.
/// - `u32` - Physical viewport height in pixels.
/// - `&[Rect]` - Tight physical bounding rectangles of sparse CPU-only effect regions.
/// - `f64` - Measured or estimated pure GPU frame duration in milliseconds.
/// - `f64` - Measured or estimated pure CPU software frame duration in milliseconds.
/// - `f64` - Measured or estimated CPU raster duration for the sparse effect tiles in milliseconds.
/// - `f64` - Measured or estimated GPU primitive + sparse-tile composite duration in milliseconds.
///
/// # Returns
///
/// - `HybridNetGainEstimate` - Net gain and candidate recommendation (negative/zero when coverage is `0` or too broad).
#[must_use]
pub fn estimate_hybrid_net_gain(
    viewport_w: u32,
    viewport_h: u32,
    cpu_effect_regions: &[Rect],
    gpu_full_ms: f64,
    cpu_full_ms: f64,
    cpu_tile_ms: f64,
    gpu_composite_ms: f64,
) -> HybridNetGainEstimate {
    let vp_area: f64 = ((viewport_w.max(1) as f64) * (viewport_h.max(1) as f64)).max(1.0);
    let mut region_area: f64 = 0.0;
    for rect in cpu_effect_regions.iter() {
        let rw: f64 = (rect.get_width() as f64).max(0.0);
        let rh: f64 = (rect.get_height() as f64).max(0.0);
        region_area += rw * rh;
    }
    let coverage_ratio: f64 = (region_area / vp_area).clamp(0.0, 1.0);
    let hybrid_total_ms: f64 =
        cpu_tile_ms.max(0.0) + gpu_composite_ms.max(0.0) + HYBRID_MIN_COMPOSITE_OVERHEAD_MS;
    if cpu_effect_regions.is_empty() || coverage_ratio <= 0.0 {
        let best_pure: RenderBackendCandidate = if gpu_full_ms <= cpu_full_ms {
            RenderBackendCandidate::GpuDirect
        } else {
            RenderBackendCandidate::Software
        };
        return HybridNetGainEstimate {
            cpu_effect_coverage_ratio: 0.0,
            hybrid_total_ms,
            net_gain_ms: -HYBRID_MIN_COMPOSITE_OVERHEAD_MS,
            should_use_hybrid: false,
            recommended_candidate: best_pure,
        };
    }
    if coverage_ratio > HYBRID_MAX_SPARSE_COVERAGE_RATIO {
        let best_pure: RenderBackendCandidate = if cpu_full_ms < gpu_full_ms {
            RenderBackendCandidate::Software
        } else {
            RenderBackendCandidate::GpuDirect
        };
        let penalty_ms: f64 = -(coverage_ratio - HYBRID_MAX_SPARSE_COVERAGE_RATIO)
            * gpu_full_ms.min(cpu_full_ms).max(1.0)
            - HYBRID_MIN_COMPOSITE_OVERHEAD_MS;
        return HybridNetGainEstimate {
            cpu_effect_coverage_ratio: coverage_ratio,
            hybrid_total_ms,
            net_gain_ms: penalty_ms.min(-HYBRID_MIN_COMPOSITE_OVERHEAD_MS),
            should_use_hybrid: false,
            recommended_candidate: best_pure,
        };
    }
    let best_pure_ms: f64 = gpu_full_ms.min(cpu_full_ms);
    let net_gain_ms: f64 = best_pure_ms - hybrid_total_ms;
    if net_gain_ms > 0.0 {
        HybridNetGainEstimate {
            cpu_effect_coverage_ratio: coverage_ratio,
            hybrid_total_ms,
            net_gain_ms,
            should_use_hybrid: true,
            recommended_candidate: RenderBackendCandidate::Hybrid,
        }
    } else {
        let best_pure: RenderBackendCandidate = if cpu_full_ms < gpu_full_ms {
            RenderBackendCandidate::Software
        } else {
            RenderBackendCandidate::GpuDirect
        };
        HybridNetGainEstimate {
            cpu_effect_coverage_ratio: coverage_ratio,
            hybrid_total_ms,
            net_gain_ms,
            should_use_hybrid: false,
            recommended_candidate: best_pure,
        }
    }
}