| 1 | //! The Blitz renderer prototype: the renderer trust domain of docs/protocol.md |
| 2 | //! (validated `RenderOp`s in, layout answers and events out, pixels on |
| 3 | //! request) on blitz-dom + blitz-paint + vello_cpu. `renderer.rs` is the |
| 4 | //! platform-neutral core; `android.rs` is its JNI surface and `ffi.rs` the C |
| 5 | //! ABI the Swift glue calls. One `Renderer` per widget behind an opaque |
| 6 | //! handle, every call on the platform's main thread (the glue guarantees it), |
| 7 | //! so the document needs no lock. Panics are caught and reported, never |
| 8 | //! unwound into the host. |
| 9 | |
| 10 | mod net; |
| 11 | mod renderer; |
| 12 | mod text; |
| 13 | #[cfg(target_vendor = "apple")] |
| 14 | pub mod cg; |
| 15 | pub mod dl; |
| 16 | |
| 17 | #[cfg(target_os = "android")] |
| 18 | mod android; |
| 19 | #[cfg(not(target_os = "android"))] |
| 20 | mod ffi; |
| 21 | |
| 22 | use renderer::Renderer; |
| 23 | |
| 24 | /// Who rasterises the blitz-paint scene. `Vello` is the portable default |
| 25 | /// (vello_cpu); `CoreGraphics` (Apple only, src/cg.rs) hands the same scene to |
| 26 | /// the platform, so text and emoji come out as the system draws them; |
| 27 | /// `DisplayList` (src/dl.rs) records it for a platform executor (Android |
| 28 | /// `Canvas`) — `render()` still rasterises with vello, the glue asks for |
| 29 | /// `render_display_list()` instead. Process default from `TWB_BACKEND` |
| 30 | /// (`vello` | `cg` | `canvas`), per renderer via `twb_set_backend`. |
| 31 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 32 | pub enum Backend { |
| 33 | Vello, |
| 34 | CoreGraphics, |
| 35 | DisplayList, |
| 36 | } |
| 37 | |
| 38 | impl Backend { |
| 39 | pub fn parse(name: &str) -> Option<Backend> { |
| 40 | match name.trim().to_ascii_lowercase().as_str() { |
| 41 | "vello" | "vello_cpu" | "cpu" => Some(Backend::Vello), |
| 42 | "cg" | "coregraphics" | "native" if cfg!(target_vendor = "apple") => Some(Backend::CoreGraphics), |
| 43 | "canvas" | "dl" | "displaylist" => Some(Backend::DisplayList), |
| 44 | _ => None, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn name(self) -> &'static str { |
| 49 | match self { |
| 50 | Backend::Vello => "vello", |
| 51 | Backend::CoreGraphics => "cg", |
| 52 | Backend::DisplayList => "canvas", |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// `TWB_BACKEND` from the environment, read once per process; Vello otherwise. |
| 57 | pub fn from_env() -> Backend { |
| 58 | static DEFAULT: OnceLock<Backend> = OnceLock::new(); |
| 59 | *DEFAULT.get_or_init(|| std::env::var("TWB_BACKEND").ok().and_then(|v| Backend::parse(& |
| 60 | } |
| 61 | } |
| 62 | use serde_json::Value; |
| 63 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
| 64 | use std::sync::OnceLock; |
| 65 | |
| 66 | /// System fonts are scanned once per process; each renderer gets a clone sharing the source cache. |
| 67 | struct Fonts(blitz_dom::FontContext); |
| 68 | unsafe impl Send for Fonts {} |
| 69 | unsafe impl Sync for Fonts {} |
| 70 | static FONTS: OnceLock<Fonts> = OnceLock::new(); |
| 71 | |
| 72 | pub(crate) fn fonts() -> blitz_dom::FontContext { |
| 73 | FONTS.get_or_init(|| Fonts(new_font_context())).0.clone() |
| 74 | } |
| 75 | |
| 76 | /// One `FontContext` per process. On iOS the colour emoji font is found (`Apple |
| 77 | /// Color Emoji`, see `font_report`) but its `sbix` glyphs are Apple's proprietary |
| 78 | /// `emjc` bitmaps, which skrifa/glifo cannot decode, so emoji stay blank there; |
| 79 | /// macOS ships the same family as PNG and paints it. See the `font_probe` tests. |
| 80 | #[cfg(not(target_arch = "wasm32"))] |
| 81 | fn new_font_context() -> blitz_dom::FontContext { |
| 82 | blitz_dom::FontContext::new() |
| 83 | } |
| 84 | |
| 85 | /// Browsers expose no system fonts to wasm: one bundled face (DejaVu Sans, |
| 86 | /// Bitstream Vera licence) stands in for every generic family, so monospace |
| 87 | /// and serif text comes out sans and emoji stay blank. The web prototype is |
| 88 | /// dev tooling; a product build would ship the chat's own font set. |
| 89 | #[cfg(target_arch = "wasm32")] |
| 90 | fn new_font_context() -> blitz_dom::FontContext { |
| 91 | use parley::fontique::{Blob, Collection, CollectionOptions, GenericFamily, SourceCache}; |
| 92 | use std::sync::Arc; |
| 93 | const DEJAVU_SANS: &[u8] = include_bytes!("../assets/DejaVuSans.woff2"); |
| 94 | let mut ctx = blitz_dom::FontContext { |
| 95 | source_cache: SourceCache::new_shared(), |
| 96 | collection: Collection::new(CollectionOptions { shared: false, system_fonts: false }), |
| 97 | }; |
| 98 | let bytes = blitz_dom::decode_font_bytes(DEJAVU_SANS).into_owned(); |
| 99 | let registered = ctx.collection.register_fonts(Blob::new(Arc::new(bytes) as _), None); |
| 100 | let ids: Vec<_> = registered.iter().map(|(id, _)| *id).collect(); |
| 101 | for generic in [GenericFamily::SansSerif, GenericFamily::Serif, GenericFamily::Monospace, GenericFamily::SystemUi, GenericFamily::UiSansSerif, GenericFamily::UiMonospace] { |
| 102 | ctx.collection.append_generic_families(generic, ids.iter().copied()); |
| 103 | } |
| 104 | ctx |
| 105 | } |
| 106 | |
| 107 | /// What the renderer's font collection knows (diagnostics): family count, the |
| 108 | /// families mapped to the generic `emoji` family, and every family with "emoji" in its name. |
| 109 | #[cfg_attr(target_os = "android", allow(dead_code))] |
| 110 | pub(crate) fn font_report() -> String { |
| 111 | use parley::fontique::GenericFamily; |
| 112 | let mut fcx = fonts(); |
| 113 | let names: Vec<String> = fcx.collection.family_names().map(str::to_string).collect(); |
| 114 | let generic: Vec<String> = fcx |
| 115 | .collection |
| 116 | .generic_families(GenericFamily::Emoji) |
| 117 | .collect::<Vec<_>>() |
| 118 | .into_iter() |
| 119 | .filter_map(|id| fcx.collection.family_name(id).map(str::to_string)) |
| 120 | .collect(); |
| 121 | let emoji: Vec<&String> = names.iter().filter(|n| n.to_ascii_lowercase().contains("emoji")). |
| 122 | format!("{} families; generic emoji → {:?}; emoji families {:?}", names.len(), generic, emoji) |
| 123 | } |
| 124 | |
| 125 | /// Runs `f` on the renderer behind `handle`; a null handle or a panic inside yields `default`. |
| 126 | pub(crate) fn with_renderer<T>(handle: *mut Renderer, default: T, f: impl FnOnce(&mut Renderer) -> |
| 127 | if handle.is_null() { |
| 128 | return default; |
| 129 | } |
| 130 | let r = unsafe { &mut *handle }; |
| 131 | match catch_unwind(AssertUnwindSafe(|| f(r))) { |
| 132 | Ok(v) => v, |
| 133 | Err(e) => { |
| 134 | let msg = e.downcast_ref::<String>().cloned().or_else(|| e.downcast_ref::<&str>().map(|s |
| 135 | log::error!("renderer panic: {msg}"); |
| 136 | default |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub(crate) fn messages(out: Vec<Value>) -> String { |
| 142 | Value::Array(out).to_string() |
| 143 | } |
| 144 | |