nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
Documentation
//! The packed JNI scene frame: one `long[]` header + one `ByteBuffer[]` of slabs that
//! `libnv_android_shell` fills per `acquireScene` and `io.github.nativeview.host.SceneReader` reads
//! (one JNI crossing per frame). Both sides are generated from this one layout
//! (`NvJniFrame.java`, `nv_jni_frame.generated.hpp`), so their indices cannot drift; the
//! `LAYOUT_HASH` both carry lets the JNI build reject a stale Java half.

use nativeview_abi::table::MAX_TABLE_SLOTS;
use nativeview_abi::SceneFact;
use nativeview_hash::xxh64;

/// Frame-wide `long[]` header words (`H_*`), in order.
pub const HEADER: &[&str] = &[
    "schema",
    "frame_id",
    "node_capacity",
    "dirty_count",
    "moved_count",
    "removed_count",
    "text_len",
    "children_len",
    "table_count",
    "scroll_count",
    "scene_epoch",
    "dirty_resource_count",
    "paragraph_count",
    "paragraph_run_count",
    "paragraph_text_len",
    "overlay_count",
];
/// Per-table header words (`TH_*`), repeated `MAX_TABLES` times after `HEADER`.
pub const TABLE_HEADER: &[&str] = &[
    "key",
    "slot",
    "words",
    "row_count",
    "dirty_count",
    "detached_count",
    "row_index_dir_len",
    "row_index_page_count",
];
/// Frame-wide slabs (`S_*`), in order; one `S_FACT_<NAME>` slab per registered `SceneFact`
/// (its bytes, resolved by id in the packer) follows them.
pub const SLABS: &[&str] = &[
    "rows",
    "rects",
    "text",
    "children",
    "dirty",
    "moved",
    "removed",
    "dirty_resources",
    "scroll_offsets",
    "node_to_paragraph",
    "paragraphs",
    "paragraph_runs",
    "paragraph_text",
    "overlays",
];
/// Per-table slabs (`TS_*`), repeated `MAX_TABLES` times after `SLABS`.
pub const TABLE_SLABS: &[&str] = &[
    "rows",
    "owners",
    "dirty_owners",
    "detached_owners",
    "row_index_dir",
    "row_index_pages",
];

/// One named index of a group (`H_SCENE_EPOCH = 10`).
#[derive(Debug)]
pub struct Slot {
    pub name: String,
    pub index: usize,
}

/// The rendered layout.
#[derive(Debug)]
pub(crate) struct JniFrameModel {
    pub header: Vec<Slot>,
    pub table_header: Vec<Slot>,
    pub slabs: Vec<Slot>,
    pub table_slabs: Vec<Slot>,
    pub max_tables: usize,
    pub header_len: usize,
    pub slabs_len: usize,
    /// Index of the first `S_FACT_*` slab (`S_FACTS`).
    pub facts_base: usize,
    /// `xxh64` of every group's names in order plus `max_tables`.
    pub layout_hash: u64,
}

fn group<S: AsRef<str>>(prefix: &str, names: &[S]) -> Vec<Slot> {
    names
        .iter()
        .enumerate()
        .map(|(index, n)| Slot { name: format!("{prefix}_{}", n.as_ref().to_ascii_uppercase()), index })
        .collect()
}

impl JniFrameModel {
    pub fn new() -> Self {
        let slabs: Vec<String> = SLABS
            .iter()
            .map(|s| (*s).to_owned())
            .chain(SceneFact::ALL.iter().map(|f| format!("fact_{}", f.name())))
            .collect();
        let mut desc = String::new();
        for g in [HEADER, TABLE_HEADER] {
            desc.push_str(&g.join(","));
            desc.push(';');
        }
        desc.push_str(&slabs.join(","));
        desc.push(';');
        desc.push_str(&TABLE_SLABS.join(","));
        desc.push(';');
        desc.push_str(&MAX_TABLE_SLOTS.to_string());
        Self {
            header: group("H", HEADER),
            table_header: group("TH", TABLE_HEADER),
            slabs: group("S", &slabs),
            table_slabs: group("TS", TABLE_SLABS),
            max_tables: MAX_TABLE_SLOTS,
            header_len: HEADER.len() + MAX_TABLE_SLOTS * TABLE_HEADER.len(),
            slabs_len: slabs.len() + MAX_TABLE_SLOTS * TABLE_SLABS.len(),
            facts_base: SLABS.len(),
            layout_hash: xxh64(desc.as_bytes(), 0),
        }
    }
}

impl Default for JniFrameModel {
    fn default() -> Self {
        Self::new()
    }
}