Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

3.2 KB
Raw Blame
//! Canvas 2D, image decoding/encoding and fonts:
//! - macOS: CoreGraphics / CoreText / ImageIO
//! - Non-macOS (Linux, Windows): pure-Rust `tiny-skia` + `cosmic-text` + `image`
use jsc::{Ctx, Value};

#[cfg(target_os = "macos")]
mod canvas;
#[cfg(target_os = "macos")]
pub mod codec;
#[cfg(target_os = "macos")]
pub mod fonts;
#[cfg(target_os = "macos")]
mod js;
#[cfg(target_os = "macos")]
mod registry;
#[cfg(target_os = "macos")]
mod text;
#[cfg(target_os = "macos")]
pub mod textblock;

#[cfg(not(target_os = "macos"))]
mod portable;
#[cfg(not(target_os = "macos"))]
pub use portable::{codec, fonts, textblock};

pub use codec::Decoded;

/// Premultiplied RGBA8 pixels, rows top-down.
pub struct Bitmap<'a> {
    pub width: u32,
    pub height: u32,
    pub stride: usize,
    pub data: &'a [u8],
    /// bumps on every change (so consumers re-upload only when needed)
    pub version: u64,
}

/// Define `__native.c2d`.
pub fn install(c: Ctx, native: Value) {
    #[cfg(target_os = "macos")]
    {
        rt::on_reset(registry::reset);
        js::install(c, native);
    }
    #[cfg(not(target_os = "macos"))]
    {
        portable::install(c, native);
    }
}

/// 2D canvases and decoded images share one id space. `None` for unknown ids and for
/// zero-sized or unallocatable canvases.
pub fn with_bitmap<R>(id: u32, f: impl FnOnce(&Bitmap) -> R) -> Option<R> {
    #[cfg(target_os = "macos")]
    {
        registry::SURFACES.with(|s| {
            let s = s.borrow();
            let surf = s.get(&id)?;
            if surf.data.is_empty() {
                return None;
            }
            Some(f(&Bitmap { width: surf.width, height: surf.height, stride: surf.width as usize * 4, data: &surf.data, version: surf.version }))
        })
    }
    #[cfg(not(target_os = "macos"))]
    {
        portable::with_bitmap(id, f)
    }
}

/// Current version of a bitmap (cheap check before `with_bitmap`).
pub fn bitmap_version(id: u32) -> Option<u64> {
    #[cfg(target_os = "macos")]
    {
        registry::SURFACES.with(|s| s.borrow().get(&id).map(|s| s.version))
    }
    #[cfg(not(target_os = "macos"))]
    {
        portable::bitmap_version(id)
    }
}

/// Where the bundled font files live (`<bundle>/fonts`); set by the app before the first page.
pub fn set_font_dir(dir: std::path::PathBuf) {
    fonts::set_dir(dir);
}

/// Register the bundled fonts (process scope). Idempotent; also done lazily on first font use.
pub fn ensure_fonts() {
    fonts::register();
}

/// Encode unpremultiplied RGBA8 (rows top-down) as PNG. Empty on failure.
pub fn encode_png(w: u32, h: u32, rgba_top_down: &[u8]) -> Vec<u8> {
    codec::encode(w, h, rgba_top_down, false, codec::Format::Png).unwrap_or_default()
}

/// Decode PNG/JPEG/GIF/WebP bytes into premultiplied RGBA8 (any thread).
pub fn decode_image(bytes: &[u8]) -> Result<Decoded, String> {
    codec::decode(bytes)
}

/// Add decoded pixels to the bitmap registry (main thread); returns the id.
pub fn insert_image(img: Decoded) -> u32 {
    #[cfg(target_os = "macos")]
    {
        registry::insert_image(img.width, img.height, img.data)
    }
    #[cfg(not(target_os = "macos"))]
    {
        portable::insert_image(img.width, img.height, img.data)
    }
}