euv-core 0.52.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::*;

/// Default eligibility thresholds for mixed CPU+GPU subtree partitioning.
impl Default for SubtreePartitionConfig {
    /// Creates default partition thresholds (`min_gpu_nodes = 512`, `max_text_wrap_ratio = 0.15`).
    ///
    /// # Returns
    ///
    /// - `Self` - Default `SubtreePartitionConfig`.
    fn default() -> Self {
        Self {
            min_gpu_nodes: DEFAULT_MIN_GPU_SUBTREE_NODES,
            max_text_wrap_ratio: DEFAULT_MAX_GPU_TEXT_WRAP_RATIO,
            max_gpu_depth: DEFAULT_MAX_GPU_SUBTREE_DEPTH,
            hardware_compute_ready: false,
            thermal_throttled: false,
        }
    }
}

/// Default empty `SubtreePartitionPlanner`.
impl Default for SubtreePartitionPlanner {
    /// Creates a planner with default thresholds and no explicit boundaries.
    ///
    /// # Returns
    ///
    /// - `Self` - Default `SubtreePartitionPlanner`.
    fn default() -> Self {
        Self {
            config: SubtreePartitionConfig::default(),
            boundaries: HashMap::new(),
        }
    }
}

/// Non-panicking accessors for `SubtreePartitionEntry`.
impl SubtreePartitionEntry {
    /// Returns the optional GPU rejection reason for this subtree entry.
    ///
    /// # Returns
    ///
    /// - `Option<GpuSubtreeRejectReason>` - Rejection reason when assigned to CPU, or `None` for GPU subtrees.
    #[must_use]
    pub fn reject_reason_value(&self) -> Option<GpuSubtreeRejectReason> {
        let Self { reject_reason, .. } = *self;
        reject_reason
    }
}

/// Partition planning and boundary management methods for `SubtreePartitionPlanner`.
impl SubtreePartitionPlanner {
    /// Creates a new planner initialized with `config`.
    ///
    /// # Arguments
    ///
    /// - `SubtreePartitionConfig` - Eligibility thresholds and device capability flags.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured `SubtreePartitionPlanner`.
    #[must_use]
    pub fn with_config(config: SubtreePartitionConfig) -> Self {
        Self {
            config,
            boundaries: HashMap::new(),
        }
    }

    /// Registers or replaces a containment `LayoutBoundary` for its `root_node`.
    ///
    /// # Arguments
    ///
    /// - `LayoutBoundary` - Containment boundary descriptor.
    pub fn register_boundary(&mut self, boundary: LayoutBoundary) {
        let _: Option<LayoutBoundary> = self
            .get_mut_boundaries()
            .insert(boundary.get_root_node(), boundary);
    }

    /// Removes all registered explicit containment boundaries.
    pub fn clear_boundaries(&mut self) {
        self.get_mut_boundaries().clear();
    }

    /// Partitions `tree` into CPU chrome/prose subtrees and GPU-compute-eligible subtrees.
    ///
    /// # Arguments
    ///
    /// - `&SceneTree` - Retained scene tree to partition.
    ///
    /// # Returns
    ///
    /// - `SubtreePartitionPlan` - Complete per-subtree lane assignment plan and mixed co-execution flag.
    #[must_use]
    pub fn plan_tree(&self, tree: &SceneTree) -> SubtreePartitionPlan {
        let root_id: NodeId = tree.get_root();
        let config: SubtreePartitionConfig = self.get_config();
        let mut candidate_roots: Vec<NodeId> = Vec::new();
        for child_id in tree.children_iter(root_id) {
            candidate_roots.push(child_id);
        }
        if candidate_roots.is_empty() {
            candidate_roots.push(root_id);
        }
        let mut entries: Vec<SubtreePartitionEntry> = Vec::with_capacity(candidate_roots.len());
        let mut cpu_node_count: usize = 1;
        let mut gpu_node_count: usize = 0;
        let mut cpu_subtree_count: usize = 0;
        let mut gpu_subtree_count: usize = 0;
        for candidate_id in candidate_roots {
            let metrics: SubtreePartitionMetrics = measure_subtree_metrics(tree, candidate_id);
            let boundary: Option<LayoutBoundary> =
                self.get_boundaries().get(&candidate_id).copied();
            let (lane, reject_reason): (SubtreeLane, Option<GpuSubtreeRejectReason>) =
                match evaluate_subtree_gpu_eligibility(metrics, boundary, config) {
                    Ok(assigned_lane) => (assigned_lane, None),
                    Err(reason) => (SubtreeLane::CpuSubtree, Some(reason)),
                };
            match lane {
                SubtreeLane::GpuSubtree => {
                    gpu_subtree_count = gpu_subtree_count.saturating_add(1);
                    gpu_node_count = gpu_node_count.saturating_add(metrics.get_total_nodes());
                }
                SubtreeLane::CpuSubtree => {
                    cpu_subtree_count = cpu_subtree_count.saturating_add(1);
                    cpu_node_count = cpu_node_count.saturating_add(metrics.get_total_nodes());
                }
            }
            entries.push(SubtreePartitionEntry {
                root_node: candidate_id,
                lane,
                metrics,
                reject_reason,
            });
        }
        let requires_mixed_coexec: bool = gpu_subtree_count > 0 && cpu_subtree_count > 0;
        SubtreePartitionPlan {
            entries,
            cpu_node_count,
            gpu_node_count,
            cpu_subtree_count,
            gpu_subtree_count,
            requires_mixed_coexec,
        }
    }
}