Skip to main content

nativeview_codegen/abi/
jni_frame.rs

1//! The packed JNI scene frame: one `long[]` header + one `ByteBuffer[]` of slabs that
2//! `libnv_android_shell` fills per `acquireScene` and `io.github.nativeview.host.SceneReader` reads
3//! (one JNI crossing per frame). Both sides are generated from this one layout
4//! (`NvJniFrame.java`, `nv_jni_frame.generated.hpp`), so their indices cannot drift; the
5//! `LAYOUT_HASH` both carry lets the JNI build reject a stale Java half.
6
7use nativeview_abi::table::MAX_TABLE_SLOTS;
8use nativeview_abi::SceneFact;
9use nativeview_hash::xxh64;
10
11/// Frame-wide `long[]` header words (`H_*`), in order.
12pub const HEADER: &[&str] = &[
13    "schema",
14    "frame_id",
15    "node_capacity",
16    "dirty_count",
17    "moved_count",
18    "removed_count",
19    "text_len",
20    "children_len",
21    "table_count",
22    "scroll_count",
23    "scene_epoch",
24    "dirty_resource_count",
25    "paragraph_count",
26    "paragraph_run_count",
27    "paragraph_text_len",
28    "overlay_count",
29];
30/// Per-table header words (`TH_*`), repeated `MAX_TABLES` times after `HEADER`.
31pub const TABLE_HEADER: &[&str] = &[
32    "key",
33    "slot",
34    "words",
35    "row_count",
36    "dirty_count",
37    "detached_count",
38    "row_index_dir_len",
39    "row_index_page_count",
40];
41/// Frame-wide slabs (`S_*`), in order; one `S_FACT_<NAME>` slab per registered `SceneFact`
42/// (its bytes, resolved by id in the packer) follows them.
43pub const SLABS: &[&str] = &[
44    "rows",
45    "rects",
46    "text",
47    "children",
48    "dirty",
49    "moved",
50    "removed",
51    "dirty_resources",
52    "scroll_offsets",
53    "node_to_paragraph",
54    "paragraphs",
55    "paragraph_runs",
56    "paragraph_text",
57    "overlays",
58];
59/// Per-table slabs (`TS_*`), repeated `MAX_TABLES` times after `SLABS`.
60pub const TABLE_SLABS: &[&str] = &[
61    "rows",
62    "owners",
63    "dirty_owners",
64    "detached_owners",
65    "row_index_dir",
66    "row_index_pages",
67];
68
69/// One named index of a group (`H_SCENE_EPOCH = 10`).
70#[derive(Debug)]
71pub struct Slot {
72    pub name: String,
73    pub index: usize,
74}
75
76/// The rendered layout.
77#[derive(Debug)]
78pub(crate) struct JniFrameModel {
79    pub header: Vec<Slot>,
80    pub table_header: Vec<Slot>,
81    pub slabs: Vec<Slot>,
82    pub table_slabs: Vec<Slot>,
83    pub max_tables: usize,
84    pub header_len: usize,
85    pub slabs_len: usize,
86    /// Index of the first `S_FACT_*` slab (`S_FACTS`).
87    pub facts_base: usize,
88    /// `xxh64` of every group's names in order plus `max_tables`.
89    pub layout_hash: u64,
90}
91
92fn group<S: AsRef<str>>(prefix: &str, names: &[S]) -> Vec<Slot> {
93    names
94        .iter()
95        .enumerate()
96        .map(|(index, n)| Slot { name: format!("{prefix}_{}", n.as_ref().to_ascii_uppercase()), index })
97        .collect()
98}
99
100impl JniFrameModel {
101    pub fn new() -> Self {
102        let slabs: Vec<String> = SLABS
103            .iter()
104            .map(|s| (*s).to_owned())
105            .chain(SceneFact::ALL.iter().map(|f| format!("fact_{}", f.name())))
106            .collect();
107        let mut desc = String::new();
108        for g in [HEADER, TABLE_HEADER] {
109            desc.push_str(&g.join(","));
110            desc.push(';');
111        }
112        desc.push_str(&slabs.join(","));
113        desc.push(';');
114        desc.push_str(&TABLE_SLABS.join(","));
115        desc.push(';');
116        desc.push_str(&MAX_TABLE_SLOTS.to_string());
117        Self {
118            header: group("H", HEADER),
119            table_header: group("TH", TABLE_HEADER),
120            slabs: group("S", &slabs),
121            table_slabs: group("TS", TABLE_SLABS),
122            max_tables: MAX_TABLE_SLOTS,
123            header_len: HEADER.len() + MAX_TABLE_SLOTS * TABLE_HEADER.len(),
124            slabs_len: slabs.len() + MAX_TABLE_SLOTS * TABLE_SLABS.len(),
125            facts_base: SLABS.len(),
126            layout_hash: xxh64(desc.as_bytes(), 0),
127        }
128    }
129}
130
131impl Default for JniFrameModel {
132    fn default() -> Self {
133        Self::new()
134    }
135}