| 1 | //! JNI surface of the Blitz renderer (org.telegram.widgetsandbox.blitz.BlitzNative). |
| 2 | //! One `Renderer` per widget behind an opaque handle; every call happens on |
| 3 | //! the Android main thread (the Kotlin side guarantees it), so the document |
| 4 | //! needs no lock. Panics are caught and reported, never unwound into the JVM. |
| 5 | |
| 6 | use crate::renderer::Renderer; |
| 7 | use crate::{fonts, messages, with_renderer}; |
| 8 | use jni::objects::{JByteBuffer, JClass, JString}; |
| 9 | use jni::sys::{jboolean, jdouble, jint, jlong, jstring}; |
| 10 | use jni::JNIEnv; |
| 11 | use serde_json::Value; |
| 12 | use std::panic::catch_unwind; |
| 13 | use std::sync::OnceLock; |
| 14 | |
| 15 | fn init_logging() { |
| 16 | static ONCE: OnceLock<()> = OnceLock::new(); |
| 17 | ONCE.get_or_init(|| { |
| 18 | android_logger::init_once(android_logger::Config::default().with_max_level(log::LevelFilter::Info).with_tag("twidget-blitz")); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | fn get_string(env: &mut JNIEnv, s: &JString) -> String { |
| 23 | env.get_string(s).map(|j| j.into()).unwrap_or_default() |
| 24 | } |
| 25 | |
| 26 | fn ret_string(env: &mut JNIEnv, s: &str) -> jstring { |
| 27 | env.new_string(s).map(|j| j.into_raw()).unwrap_or(std::ptr::null_mut()) |
| 28 | } |
| 29 | |
| 30 | #[no_mangle] |
| 31 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_create( |
| 32 | mut env: JNIEnv, _c: JClass, width: jdouble, dpr: jdouble, theme: JString, |
| 33 | ) -> jlong { |
| 34 | init_logging(); |
| 35 | let dark = get_string(&mut env, &theme) == "dark"; |
| 36 | match catch_unwind(|| Renderer::new(width, dpr, dark, fonts())) { |
| 37 | Ok(r) => Box::into_raw(Box::new(r)) as jlong, |
| 38 | Err(_) => { |
| 39 | log::error!("renderer creation panicked"); |
| 40 | 0 |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[no_mangle] |
| 46 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_destroy(_env: JNIEnv, _c: JClass, handle: jlong) { |
| 47 | if handle != 0 { |
| 48 | unsafe { drop(Box::from_raw(handle as *mut Renderer)) }; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// Host → renderer message (JSON). Returns a JSON array of renderer → host messages. |
| 53 | #[no_mangle] |
| 54 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_receive( |
| 55 | mut env: JNIEnv, _c: JClass, handle: jlong, json: JString, |
| 56 | ) -> jstring { |
| 57 | let text = get_string(&mut env, &json); |
| 58 | let out = with_renderer(handle as *mut Renderer, "[]".to_string(), |r| match serde_json::from_str::<Value>(& |
| 59 | Ok(msg) => messages(r.receive(&msg)), |
| 60 | Err(e) => messages(vec![serde_json::json!({ "type": "violation", "detail": format!("bad message: {e}") })]), |
| 61 | }); |
| 62 | ret_string(&mut env, &out) |
| 63 | } |
| 64 | |
| 65 | #[no_mangle] |
| 66 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_setWidth( |
| 67 | mut env: JNIEnv, _c: JClass, handle: jlong, width: jdouble, |
| 68 | ) -> jstring { |
| 69 | let out = with_renderer(handle as *mut Renderer, "[]".to_string(), |r| { |
| 70 | let mut out = Vec::new(); |
| 71 | r.set_width(width, &mut out); |
| 72 | messages(out) |
| 73 | }); |
| 74 | ret_string(&mut env, &out) |
| 75 | } |
| 76 | |
| 77 | #[no_mangle] |
| 78 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_setTheme( |
| 79 | mut env: JNIEnv, _c: JClass, handle: jlong, theme: JString, |
| 80 | ) -> jstring { |
| 81 | let dark = get_string(&mut env, &theme) == "dark"; |
| 82 | let out = with_renderer(handle as *mut Renderer, "[]".to_string(), |r| { |
| 83 | let mut out = Vec::new(); |
| 84 | r.set_theme(dark, &mut out); |
| 85 | messages(out) |
| 86 | }); |
| 87 | ret_string(&mut env, &out) |
| 88 | } |
| 89 | |
| 90 | /// A touch in CSS px. action: 0 down, 1 move, 2 up, 3 cancel. Returns renderer → host messages. |
| 91 | #[no_mangle] |
| 92 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_touch( |
| 93 | mut env: JNIEnv, _c: JClass, handle: jlong, action: jint, x: jdouble, y: jdouble, pointer: jint, primary: jboolean, |
| 94 | ) -> jstring { |
| 95 | let out = with_renderer(handle as *mut Renderer, "[]".to_string(), |r| messages(r.touch(action, x, y, pointer, primary != 0))); |
| 96 | ret_string(&mut env, &out) |
| 97 | } |
| 98 | |
| 99 | /// Physical size of the next render: (width << 32) | height. |
| 100 | #[no_mangle] |
| 101 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_size(_env: JNIEnv, _c: JClass, handle: jlong) -> jlong { |
| 102 | with_renderer(handle as *mut Renderer, 0, |r| { |
| 103 | let (w, h) = r.viewport_px(); |
| 104 | ((w as jlong) << 32) | (h as jlong) |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | #[no_mangle] |
| 109 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_dirty(_env: JNIEnv, _c: JClass, handle: jlong) -> jboolean { |
| 110 | with_renderer(handle as *mut Renderer, 0, |r| r.dirty as jboolean) |
| 111 | } |
| 112 | |
| 113 | /// Picks the rasteriser: "vello" (default, pixels through `render`) or "canvas" (the frame as a display |
| 114 | /// list through `displayList`, drawn by CanvasScene.kt). False for an unknown name. |
| 115 | #[no_mangle] |
| 116 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_setBackend( |
| 117 | mut env: JNIEnv, _c: JClass, handle: jlong, name: JString, |
| 118 | ) -> jboolean { |
| 119 | let Some(backend) = crate::Backend::parse(&get_string(&mut env, &name)) else { return 0 }; |
| 120 | with_renderer(handle as *mut Renderer, 0, |r| { |
| 121 | r.set_backend(backend); |
| 122 | 1 |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | /// The frame as a display list (renderer-blitz/src/dl.rs format); empty on failure. |
| 127 | #[no_mangle] |
| 128 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_displayList<'a>( |
| 129 | env: JNIEnv<'a>, _c: JClass, handle: jlong, |
| 130 | ) -> jni::objects::JByteArray<'a> { |
| 131 | let bytes = with_renderer(handle as *mut Renderer, Vec::new(), |r| r.render_display_list()); |
| 132 | env.byte_array_from_slice(&bytes).unwrap_or_else(|_| jni::objects::JByteArray::default()) |
| 133 | } |
| 134 | |
| 135 | /// The font file behind a display-list font id as a direct ByteBuffer over memory the renderer keeps |
| 136 | /// alive for the process (fontique's blob), or null. The executor builds its Font from it once per id. |
| 137 | #[no_mangle] |
| 138 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_fontBytes<'a>( |
| 139 | mut env: JNIEnv<'a>, _c: JClass, id: jlong, |
| 140 | ) -> jni::objects::JByteBuffer<'a> { |
| 141 | let Some(blob) = crate::dl::font_blob(id as u64) else { return jni::objects::JByteBuffer::default() }; |
| 142 | let data = blob.data(); |
| 143 | let (ptr, len) = (data.as_ptr() as *mut u8, data.len()); |
| 144 | // The registry holds the blob (and its Arc'd bytes) forever; the buffer must not outlive the process either. |
| 145 | std::mem::forget(blob); |
| 146 | unsafe { env.new_direct_byte_buffer(ptr, len) }.unwrap_or_else(|_| jni::objects::JByteBuffer::default()) |
| 147 | } |
| 148 | |
| 149 | /// The decoded image behind a display-list image id as premultiplied RGBA8 rows (width × height × 4), or empty. |
| 150 | #[no_mangle] |
| 151 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_imagePixels<'a>( |
| 152 | env: JNIEnv<'a>, _c: JClass, id: jlong, |
| 153 | ) -> jni::objects::JByteArray<'a> { |
| 154 | let bytes = crate::dl::image(id as u64).map(|img| crate::dl::image_rgba_premultiplied(&img)).unwrap_or_default(); |
| 155 | env.byte_array_from_slice(&bytes).unwrap_or_else(|_| jni::objects::JByteArray::default()) |
| 156 | } |
| 157 | |
| 158 | /// Paints into a direct ByteBuffer of at least width*height*4 bytes (premultiplied RGBA8). Returns the paint time in ms. |
| 159 | #[no_mangle] |
| 160 | pub extern "system" fn Java_org_telegram_widgetsandbox_blitz_BlitzNative_render( |
| 161 | env: JNIEnv, _c: JClass, handle: jlong, buffer: JByteBuffer, |
| 162 | ) -> jdouble { |
| 163 | let ptr = match env.get_direct_buffer_address(&buffer) { |
| 164 | Ok(p) => p, |
| 165 | Err(_) => return -1.0, |
| 166 | }; |
| 167 | let cap = env.get_direct_buffer_capacity(&buffer).unwrap_or(0); |
| 168 | with_renderer(handle as *mut Renderer, -1.0, |r| r.render_into(ptr, cap)) |
| 169 | } |
| 170 | |