Skip to main content

euv_core/layout/partition/
impl.rs

1use super::*;
2
3/// Default eligibility thresholds for mixed CPU+GPU subtree partitioning.
4impl Default for SubtreePartitionConfig {
5    /// Creates default partition thresholds (`min_gpu_nodes = 512`, `max_text_wrap_ratio = 0.15`).
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `SubtreePartitionConfig`.
10    fn default() -> Self {
11        Self {
12            min_gpu_nodes: DEFAULT_MIN_GPU_SUBTREE_NODES,
13            max_text_wrap_ratio: DEFAULT_MAX_GPU_TEXT_WRAP_RATIO,
14            max_gpu_depth: DEFAULT_MAX_GPU_SUBTREE_DEPTH,
15            hardware_compute_ready: false,
16            thermal_throttled: false,
17        }
18    }
19}
20
21/// Default empty `SubtreePartitionPlanner`.
22impl Default for SubtreePartitionPlanner {
23    /// Creates a planner with default thresholds and no explicit boundaries.
24    ///
25    /// # Returns
26    ///
27    /// - `Self` - Default `SubtreePartitionPlanner`.
28    fn default() -> Self {
29        Self {
30            config: SubtreePartitionConfig::default(),
31            boundaries: HashMap::new(),
32        }
33    }
34}
35
36/// Non-panicking accessors for `SubtreePartitionEntry`.
37impl SubtreePartitionEntry {
38    /// Returns the optional GPU rejection reason for this subtree entry.
39    ///
40    /// # Returns
41    ///
42    /// - `Option<GpuSubtreeRejectReason>` - Rejection reason when assigned to CPU, or `None` for GPU subtrees.
43    #[must_use]
44    pub fn reject_reason_value(&self) -> Option<GpuSubtreeRejectReason> {
45        let Self { reject_reason, .. } = *self;
46        reject_reason
47    }
48}
49
50/// Partition planning and boundary management methods for `SubtreePartitionPlanner`.
51impl SubtreePartitionPlanner {
52    /// Creates a new planner initialized with `config`.
53    ///
54    /// # Arguments
55    ///
56    /// - `SubtreePartitionConfig` - Eligibility thresholds and device capability flags.
57    ///
58    /// # Returns
59    ///
60    /// - `Self` - Configured `SubtreePartitionPlanner`.
61    #[must_use]
62    pub fn with_config(config: SubtreePartitionConfig) -> Self {
63        Self {
64            config,
65            boundaries: HashMap::new(),
66        }
67    }
68
69    /// Registers or replaces a containment `LayoutBoundary` for its `root_node`.
70    ///
71    /// # Arguments
72    ///
73    /// - `LayoutBoundary` - Containment boundary descriptor.
74    pub fn register_boundary(&mut self, boundary: LayoutBoundary) {
75        let _: Option<LayoutBoundary> = self
76            .get_mut_boundaries()
77            .insert(boundary.get_root_node(), boundary);
78    }
79
80    /// Removes all registered explicit containment boundaries.
81    pub fn clear_boundaries(&mut self) {
82        self.get_mut_boundaries().clear();
83    }
84
85    /// Partitions `tree` into CPU chrome/prose subtrees and GPU-compute-eligible subtrees.
86    ///
87    /// # Arguments
88    ///
89    /// - `&SceneTree` - Retained scene tree to partition.
90    ///
91    /// # Returns
92    ///
93    /// - `SubtreePartitionPlan` - Complete per-subtree lane assignment plan and mixed co-execution flag.
94    #[must_use]
95    pub fn plan_tree(&self, tree: &SceneTree) -> SubtreePartitionPlan {
96        let root_id: NodeId = tree.get_root();
97        let config: SubtreePartitionConfig = self.get_config();
98        let mut candidate_roots: Vec<NodeId> = Vec::new();
99        for child_id in tree.children_iter(root_id) {
100            candidate_roots.push(child_id);
101        }
102        if candidate_roots.is_empty() {
103            candidate_roots.push(root_id);
104        }
105        let mut entries: Vec<SubtreePartitionEntry> = Vec::with_capacity(candidate_roots.len());
106        let mut cpu_node_count: usize = 1;
107        let mut gpu_node_count: usize = 0;
108        let mut cpu_subtree_count: usize = 0;
109        let mut gpu_subtree_count: usize = 0;
110        for candidate_id in candidate_roots {
111            let metrics: SubtreePartitionMetrics = measure_subtree_metrics(tree, candidate_id);
112            let boundary: Option<LayoutBoundary> =
113                self.get_boundaries().get(&candidate_id).copied();
114            let (lane, reject_reason): (SubtreeLane, Option<GpuSubtreeRejectReason>) =
115                match evaluate_subtree_gpu_eligibility(metrics, boundary, config) {
116                    Ok(assigned_lane) => (assigned_lane, None),
117                    Err(reason) => (SubtreeLane::CpuSubtree, Some(reason)),
118                };
119            match lane {
120                SubtreeLane::GpuSubtree => {
121                    gpu_subtree_count = gpu_subtree_count.saturating_add(1);
122                    gpu_node_count = gpu_node_count.saturating_add(metrics.get_total_nodes());
123                }
124                SubtreeLane::CpuSubtree => {
125                    cpu_subtree_count = cpu_subtree_count.saturating_add(1);
126                    cpu_node_count = cpu_node_count.saturating_add(metrics.get_total_nodes());
127                }
128            }
129            entries.push(SubtreePartitionEntry {
130                root_node: candidate_id,
131                lane,
132                metrics,
133                reject_reason,
134            });
135        }
136        let requires_mixed_coexec: bool = gpu_subtree_count > 0 && cpu_subtree_count > 0;
137        SubtreePartitionPlan {
138            entries,
139            cpu_node_count,
140            gpu_node_count,
141            cpu_subtree_count,
142            gpu_subtree_count,
143            requires_mixed_coexec,
144        }
145    }
146}