Skip to main content

ifc_lite_processing/
prepass_styled.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Styled-item resolution helpers for the pre-pass (split from `prepass.rs`).
6//! The loop here is byte-identical to the historic inline loop in
7//! `resolve_prepass`; the sharded pre-pass fans slices of it across workers.
8
9use ifc_lite_core::EntityDecoder;
10use rustc_hash::FxHashMap;
11
12use crate::prepass::{collect_geometry_style_info, extract_style_info_from_styled_item, Span};
13use crate::style::GeometryStyleInfo;
14
15/// Pre-resolved styled maps: `(orphan id -> rgba, geometry id -> style info)`.
16pub type StyleSeeds = (FxHashMap<u32, [f32; 4]>, FxHashMap<u32, GeometryStyleInfo>);
17
18/// The styled-item classification/resolution loop of [`resolve_prepass`],
19/// exposed so the browser's SHARDED pre-pass can fan slices of the (file-
20/// ordered) styled-item span list across workers: each worker resolves its
21/// contiguous slice with this exact loop, and the host merges shard results in
22/// shard order with first-wins per geometry id — reproducing the serial
23/// resolver's file-order first-wins precedence (`collect_geometry_style_info`
24/// skips ids already present).
25pub fn resolve_styled_items_into(
26    styled_items: &[Span],
27    decoder: &mut EntityDecoder,
28    defer_attached_styles: bool,
29    orphan_styled_items: &mut FxHashMap<u32, [f32; 4]>,
30    geometry_style_index: &mut FxHashMap<u32, GeometryStyleInfo>,
31    deferred_attached_styled_spans: &mut Vec<(usize, usize)>,
32) {
33    for &(id, start, end) in styled_items {
34        let Ok(styled_item) = decoder.decode_at_with_id(id, start, end) else {
35            if defer_attached_styles {
36                // Undecodable now — let the replay try again later, matching
37                // the historic defer behaviour.
38                deferred_attached_styled_spans.push((start, end));
39            }
40            continue;
41        };
42        if styled_item.get_ref(0).is_none() {
43            // Orphan styled item (null Item) = a material appearance (#407).
44            // Always resolved up front — even in defer mode — or
45            // material-only-styled elements render default-gray (#913 §2c).
46            if let Some(info) = extract_style_info_from_styled_item(&styled_item, decoder) {
47                orphan_styled_items.insert(id, info.color);
48            }
49        } else if defer_attached_styles {
50            deferred_attached_styled_spans.push((start, end));
51        } else {
52            collect_geometry_style_info(geometry_style_index, &styled_item, decoder);
53        }
54    }
55}
56
57
58/// [`crate::prepass::flat_styles_rgba8`] with the (dominant) geometry-style
59/// source supplied as PRE-MERGED columns instead of a map — the sharded
60/// finalize path. `geom_ids` are unique (the host merged shard results
61/// first-wins) with `geom_colors` as rgba f32 quads; `resolved` carries the
62/// support resolution (indexed colours, materials, element colours) and MUST
63/// have an empty `geometry_style_index`. Output is byte-identical to the
64/// serial flatten: same precedence (geometry > indexed colour > material >
65/// element material), same id-ascending wire order, same rgba8 conversion —
66/// without ever building the 4M-entry geometry hashmap.
67pub fn flat_styles_rgba8_from_geometry_columns(
68    geom_ids: &[u32],
69    geom_colors: &[f32],
70    resolved: &crate::prepass::ResolvedPrepass,
71    decoder: &mut EntityDecoder,
72) -> (Vec<u32>, Vec<u8>) {
73    debug_assert!(resolved.geometry_style_index.is_empty());
74    // Overlay sources in serial precedence order, or_insert among themselves.
75    let mut overlay: FxHashMap<u32, [f32; 4]> = FxHashMap::default();
76    for (&geometry_id, &color) in &resolved.indexed_colour_index {
77        overlay.entry(geometry_id).or_insert(color);
78    }
79    let material_styles = crate::style::build_material_style_index(
80        &resolved.material_def_reprs,
81        &resolved.orphan_styled_items,
82        decoder,
83    );
84    for (&mat_id, &color) in crate::style::flatten_material_color_index(&material_styles).iter() {
85        overlay.entry(mat_id).or_insert(color);
86    }
87    for (&element_id, colors) in &resolved.element_material_colors {
88        if let Some(&color) = colors.first() {
89            overlay.entry(element_id).or_insert(color);
90        }
91    }
92
93    // Sort both sides by id and merge, geometry winning on ties.
94    let mut geom_order: Vec<u32> = (0..geom_ids.len() as u32).collect();
95    geom_order.sort_unstable_by_key(|&i| geom_ids[i as usize]);
96    let mut overlay_sorted: Vec<(u32, [f32; 4])> = overlay.into_iter().collect();
97    overlay_sorted.sort_unstable_by_key(|&(id, _)| id);
98
99    let mut ids: Vec<u32> = Vec::with_capacity(geom_ids.len() + overlay_sorted.len());
100    let mut rgba: Vec<u8> = Vec::with_capacity((geom_ids.len() + overlay_sorted.len()) * 4);
101    let push = |id: u32, color: [f32; 4], ids: &mut Vec<u32>, rgba: &mut Vec<u8>| {
102        ids.push(id);
103        rgba.extend_from_slice(&crate::style::Rgba::from_array(color).to_rgba8());
104    };
105    let mut oi = 0;
106    for &gi in &geom_order {
107        let id = geom_ids[gi as usize];
108        while oi < overlay_sorted.len() && overlay_sorted[oi].0 < id {
109            let (oid, oc) = overlay_sorted[oi];
110            push(oid, oc, &mut ids, &mut rgba);
111            oi += 1;
112        }
113        if oi < overlay_sorted.len() && overlay_sorted[oi].0 == id {
114            oi += 1; // geometry wins the tie, overlay entry dropped
115        }
116        let c = gi as usize * 4;
117        push(
118            id,
119            [geom_colors[c], geom_colors[c + 1], geom_colors[c + 2], geom_colors[c + 3]],
120            &mut ids,
121            &mut rgba,
122        );
123    }
124    while oi < overlay_sorted.len() {
125        let (oid, oc) = overlay_sorted[oi];
126        push(oid, oc, &mut ids, &mut rgba);
127        oi += 1;
128    }
129    (ids, rgba)
130}