Skip to main content

velox_engine/platform/
android.rs

1//! Google Android(`ANativeWindow` / `Choreographer` / `WindowInsetsCompat` / JNI)专属平台宿主桥接层。
2//!
3//! 通过启用 `velox-engine/android` 或 `velox-kit/android` Feature 按需引入:
4//! - [`AndroidHostBridge<V>`]:泛型封装任意 [`View`] 的 Android 宿主生命周期管理(`Surface` 尺寸、系统状态栏/挖孔屏/手势横条 `WindowInsets`、软键盘 `IME` 高度、`MotionEvent` 触控、`OnBackInvokedCallback` 返回键消费);
5//! - [`step_and_blit_android_surface`]:支持 `wgpu` (Vulkan) 硬件 Swapchain 直通或 `ANativeWindow_lock` (`WINDOW_FORMAT_RGBA_8888`) 步长对齐拷贝;
6//! - [`export_android_app!`](crate::export_android_app):一行宏为业务 [`View`] 自动生成完整 C ABI (`velox_android_app_*` / `velox_mobile_app_*`) 与 `com.velox.android.MainActivity` JNI 导出入口。
7
8use std::boxed::Box;
9
10use crate::element::View;
11use crate::platform::mobile::{CDirtyRect, MobileEmbedApp, MobileOs, TouchPhase};
12use crate::pod::{Handle, SafeAreaInsets};
13
14/// 泛型 Android JNI / NDK 宿主桥接器(将任意业务 [`View`] 挂载至 [`MobileEmbedApp`] 并桥接 Android 视图生命周期)。
15pub struct AndroidHostBridge<V: View + 'static> {
16    app: *mut MobileEmbedApp,
17    pub root_handle: Handle<V>,
18}
19
20impl<V: View + Default + 'static> AndroidHostBridge<V> {
21    /// 使用 `V::default()` 创建指定视口尺寸与系统状态栏/手势导航条安全区的 Android 应用宿主。
22    #[inline]
23    pub fn new(width: i16, height: i16, status_bar_top: i16, gesture_bar_bottom: i16) -> Self {
24        Self::with_root(
25            width,
26            height,
27            status_bar_top,
28            gesture_bar_bottom,
29            V::default(),
30        )
31    }
32}
33
34impl<V: View + 'static> AndroidHostBridge<V> {
35    /// Android `MotionEvent.ACTION_DOWN`。
36    pub const ACTION_DOWN: u8 = 0;
37    /// Android `MotionEvent.ACTION_UP`。
38    pub const ACTION_UP: u8 = 1;
39    /// Android `MotionEvent.ACTION_MOVE`。
40    pub const ACTION_MOVE: u8 = 2;
41    /// Android `MotionEvent.ACTION_CANCEL`。
42    pub const ACTION_CANCEL: u8 = 3;
43
44    /// 使用给定根视图实例 `root_view` 创建指定视口尺寸与安全区的 Android 应用宿主。
45    pub fn with_root(
46        width: i16,
47        height: i16,
48        status_bar_top: i16,
49        gesture_bar_bottom: i16,
50        root_view: V,
51    ) -> Self {
52        let safe_area = SafeAreaInsets::new(status_bar_top, 0, gesture_bar_bottom, 0);
53        let mut embed = Box::new(MobileEmbedApp::new_for_os(
54            MobileOs::Android,
55            width,
56            height,
57            safe_area,
58        ));
59        let root_handle = embed.engine.mount_root(root_view);
60        Self {
61            app: Box::into_raw(embed),
62            root_handle,
63        }
64    }
65
66    /// 设置 Android `DisplayMetrics.density` 整数倍 HiDPI 缩放因子。
67    #[inline]
68    pub fn set_dpi_scale(&mut self, scale: i16) {
69        unsafe { &mut *self.app }.set_dpi_scale(scale);
70    }
71
72    /// 响应屏幕旋转或分屏窗口大小改变(如竖屏 `390x844` 旋转为横屏 `844x390`)。
73    #[inline]
74    pub fn on_surface_changed(&mut self, width: i16, height: i16) {
75        unsafe { &mut *self.app }.resize(width, height);
76    }
77
78    /// 同步 Android `WindowInsetsCompat.Type.systemBars() | displayCutout()` 安全区内边距。
79    #[inline]
80    pub fn on_window_insets(&mut self, top: i16, right: i16, bottom: i16, left: i16) {
81        unsafe { &mut *self.app }.set_safe_area(SafeAreaInsets::new(top, right, bottom, left));
82    }
83
84    /// 同步 Android `WindowInsetsCompat.Type.ime()` 软键盘底部像素高度。
85    #[inline]
86    pub fn on_ime_inset(&mut self, ime_bottom_px: i16) {
87        unsafe { &mut *self.app }.set_keyboard_inset(ime_bottom_px);
88    }
89
90    /// 分发 Android `MotionEvent`(自动将 `ACTION_DOWN/MOVE/UP/CANCEL` 映射为 [`TouchPhase`])。
91    #[inline]
92    pub fn on_motion_event(&mut self, pointer_id: u32, action: u8, x: i16, y: i16) {
93        let phase = TouchPhase::from_android_action(action);
94        unsafe { &mut *self.app }.push_touch(pointer_id, phase, x, y);
95    }
96
97    /// 分发 Android `OnBackInvokedCallback` / `onBackPressed` 系统返回事件:
98    /// 返回 `true` 表示应用内部已消费(关闭抽屉或回退二级页),返回 `false` 表示交由宿主 `Activity` 处理。
99    #[inline]
100    pub fn on_back_pressed(&mut self) -> bool {
101        unsafe { &mut *self.app }.handle_back()
102    }
103
104    /// 响应 Android `Choreographer.FrameCallback.doFrame` 推进一帧,返回本帧脏矩形个数。
105    #[inline]
106    pub fn do_frame(&mut self, dt_ms: u16) -> usize {
107        unsafe { &mut *self.app }.step_frame(dt_ms)
108    }
109
110    /// 获取 `RGBA8888` 像素缓冲区的只读字节切片(可直接写入 `ANativeWindow_Buffer.bits`)。
111    #[inline]
112    pub fn rgba_slice(&self) -> &[u8] {
113        unsafe { &*self.app }.rgba_bytes()
114    }
115
116    /// 获取本帧脏矩形切片(供 `ANativeWindow_lock` 局部脏区域刷新使用)。
117    #[inline]
118    pub fn dirty_rects(&self) -> &[CDirtyRect] {
119        unsafe { &*self.app }.dirty_rects()
120    }
121
122    /// 访问底层 [`MobileEmbedApp`] 只读引用。
123    #[inline]
124    pub fn embed_app(&self) -> &MobileEmbedApp {
125        unsafe { &*self.app }
126    }
127
128    /// 访问底层 [`MobileEmbedApp`] 可变引用。
129    #[inline]
130    pub fn embed_app_mut(&mut self) -> &mut MobileEmbedApp {
131        unsafe { &mut *self.app }
132    }
133
134    /// 查询当前 Android 宿主实例是否启用了基于轻量 `wgpu` (Vulkan) 2D SDF 的硬件 GPU 渲染后端。
135    #[inline]
136    pub fn is_gpu_backend(&self) -> bool {
137        unsafe { &*self.app }.backend().is_gpu()
138    }
139
140    /// 取出内部裸指针供 FFI 使用(调用后不再由 `Drop` 自动释放)。
141    #[inline]
142    pub fn into_raw(mut self) -> *mut MobileEmbedApp {
143        let ptr = self.app;
144        self.app = core::ptr::null_mut();
145        ptr
146    }
147}
148
149impl<V: View + 'static> Drop for AndroidHostBridge<V> {
150    fn drop(&mut self) {
151        if !self.app.is_null() {
152            unsafe {
153                let _ = Box::from_raw(self.app);
154            }
155        }
156    }
157}
158
159#[cfg(target_os = "android")]
160#[repr(C)]
161struct ANativeWindowBuffer {
162    width: i32,
163    height: i32,
164    stride: i32,
165    format: i32,
166    bits: *mut core::ffi::c_void,
167    reserved: [u32; 6],
168}
169
170#[cfg(target_os = "android")]
171#[link(name = "android")]
172unsafe extern "C" {
173    fn ANativeWindow_fromSurface(
174        env: *mut core::ffi::c_void,
175        surface: *mut core::ffi::c_void,
176    ) -> *mut core::ffi::c_void;
177    fn ANativeWindow_release(window: *mut core::ffi::c_void);
178    fn ANativeWindow_setBuffersGeometry(
179        window: *mut core::ffi::c_void,
180        width: i32,
181        height: i32,
182        format: i32,
183    ) -> i32;
184    fn ANativeWindow_lock(
185        window: *mut core::ffi::c_void,
186        out_buffer: *mut ANativeWindowBuffer,
187        in_out_dirty_bounds: *mut core::ffi::c_void,
188    ) -> i32;
189    fn ANativeWindow_unlockAndPost(window: *mut core::ffi::c_void) -> i32;
190}
191
192/// 推进一帧 [`MobileEmbedApp`] 并将像素提交至 Android `Surface` / `ANativeWindow`。
193///
194/// 若已挂载轻量 `wgpu` (Vulkan) 2D SDF Swapchain,则在 `step_frame` 内直接完成硬件零拷贝呈现;
195/// 否则在 `aarch64` Android 环境中通过 `ANativeWindow_lock` 步长对齐写入 `RGBA_8888` 缓冲区。
196///
197/// # Safety
198/// `app_ptr` 必须为有效的 [`MobileEmbedApp`] 指针或空指针;在 `aarch64` Android 下 `env` 与 `surface` 必须为合法 JNI `JNIEnv*` 与 `jobject Surface`。
199pub unsafe fn step_and_blit_android_surface(
200    env: *mut core::ffi::c_void,
201    app_ptr: *mut MobileEmbedApp,
202    surface: *mut core::ffi::c_void,
203    dt_ms: i32,
204) -> i32 {
205    let Some(app_ref) = (unsafe { app_ptr.as_mut() }) else {
206        return 0;
207    };
208    let dirty = app_ref.step_frame(dt_ms.max(1) as u16);
209    if dirty == 0 || surface.is_null() {
210        return dirty as i32;
211    }
212    if app_ref.has_active_gpu_window() {
213        return dirty as i32;
214    }
215
216    #[cfg(target_os = "android")]
217    unsafe {
218        let win = ANativeWindow_fromSurface(env, surface);
219        if !win.is_null() {
220            let (w, h) = (
221                app_ref.physical_width() as i32,
222                app_ref.physical_height() as i32,
223            );
224            // WINDOW_FORMAT_RGBA_8888 = 1
225            let _ = ANativeWindow_setBuffersGeometry(win, w, h, 1);
226            let mut buf = ANativeWindowBuffer {
227                width: 0,
228                height: 0,
229                stride: 0,
230                format: 0,
231                bits: core::ptr::null_mut(),
232                reserved: [0; 6],
233            };
234            if ANativeWindow_lock(win, &mut buf, core::ptr::null_mut()) == 0 && !buf.bits.is_null()
235            {
236                let src = app_ref.rgba_bytes();
237                let copy_w = (w.min(buf.width).max(0) as usize) * 4;
238                let copy_h = h.min(buf.height).max(0) as usize;
239                let dst_base = buf.bits as *mut u8;
240                let dst_stride = (buf.stride.max(w) as usize) * 4;
241                let src_stride = (w as usize) * 4;
242                for row in 0..copy_h {
243                    core::ptr::copy_nonoverlapping(
244                        src.as_ptr().add(row * src_stride),
245                        dst_base.add(row * dst_stride),
246                        copy_w,
247                    );
248                }
249                let _ = ANativeWindow_unlockAndPost(win);
250            }
251            ANativeWindow_release(win);
252        }
253    }
254    #[cfg(not(target_os = "android"))]
255    let _ = env;
256
257    dirty as i32
258}
259
260/// 框架内置的标准 Android Kotlin 宿主视图组件(`VeloxSurfaceView.kt`)源码模板。
261pub const ANDROID_SURFACE_VIEW_KT: &str = r#"package com.velox.android
262
263import android.content.Context
264import android.view.Choreographer
265import android.view.MotionEvent
266import android.view.SurfaceHolder
267import android.view.SurfaceView
268import androidx.core.view.ViewCompat
269import androidx.core.view.WindowInsetsCompat
270
271/**
272 * Android 宿主视图封装(由 `velox-engine::platform::android` 自动生成,对接 `velox-kit` C ABI / JNI 接口)。
273 */
274class VeloxSurfaceView(context: Context) : SurfaceView(context), SurfaceHolder.Callback, Choreographer.FrameCallback {
275    private var nativeHandle: Long = 0L
276
277    init {
278        holder.addCallback(this)
279        ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
280            if (nativeHandle != 0L) {
281                val sysBars = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
282                val ime = insets.getInsets(WindowInsetsCompat.Type.ime())
283                nativeSetSafeArea(nativeHandle, sysBars.top, sysBars.right, sysBars.bottom, sysBars.left)
284                nativeSetKeyboardInset(nativeHandle, ime.bottom)
285            }
286            insets
287        }
288    }
289
290    override fun surfaceCreated(holder: SurfaceHolder) {
291        val frame = holder.surfaceFrame
292        nativeHandle = nativeCreate(frame.width(), frame.height(), 48, 34)
293        Choreographer.getInstance().postFrameCallback(this)
294    }
295
296    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
297        if (nativeHandle != 0L) {
298            nativeResize(nativeHandle, width, height)
299        }
300    }
301
302    override fun surfaceDestroyed(holder: SurfaceHolder) {
303        if (nativeHandle != 0L) {
304            nativeDestroy(nativeHandle)
305            nativeHandle = 0L
306        }
307    }
308
309    override fun onTouchEvent(event: MotionEvent): Boolean {
310        if (nativeHandle == 0L) return false
311        val pointerId = event.getPointerId(0)
312        nativePushTouch(nativeHandle, pointerId, event.actionMasked, event.x.toInt(), event.y.toInt())
313        return true
314    }
315
316    fun handleBackPressed(): Boolean {
317        return if (nativeHandle != 0L) nativeHandleBack(nativeHandle) else false
318    }
319
320    override fun doFrame(frameTimeNanos: Long) {
321        if (nativeHandle != 0L) {
322            nativeStepAndBlitSurface(nativeHandle, holder.surface, 16)
323            Choreographer.getInstance().postFrameCallback(this)
324        }
325    }
326
327    private external fun nativeCreate(width: Int, height: Int, safeTop: Int, safeBottom: Int): Long
328    private external fun nativeDestroy(handle: Long)
329    private external fun nativeResize(handle: Long, width: Int, height: Int)
330    private external fun nativeSetSafeArea(handle: Long, top: Int, right: Int, bottom: Int, left: Int)
331    private external fun nativeSetKeyboardInset(handle: Long, height: Int)
332    private external fun nativePushTouch(handle: Long, pointerId: Int, action: Int, x: Int, y: Int)
333    private external fun nativeHandleBack(handle: Long): Boolean
334    private external fun nativeStepAndBlitSurface(handle: Long, surface: Any, dtMs: Int): Int
335}
336"#;
337
338/// 生成加载指定原生库名 `lib_name`(如 `"android_app"`)的 Android `MainActivity.java` 宿主源码。
339pub fn generate_android_main_activity_java(lib_name: &str) -> std::string::String {
340    std::format!(
341        r#"package com.velox.android;
342
343import android.app.Activity;
344import android.os.Bundle;
345import android.view.Choreographer;
346import android.view.MotionEvent;
347import android.view.SurfaceHolder;
348import android.view.SurfaceView;
349import android.view.View;
350import android.view.Window;
351
352/**
353 * Android 原生 Activity 宿主入口(由 `velox-engine::platform::android` 自动生成,加载 `lib{lib_name}.so`)。
354 */
355public class MainActivity extends Activity
356    implements SurfaceHolder.Callback,
357               Choreographer.FrameCallback,
358               View.OnTouchListener {{
359
360    static {{
361        System.loadLibrary("{lib_name}");
362    }}
363
364    private SurfaceView surfaceView;
365    private long nativeHandle = 0L;
366    private int dpiScale = 2;
367    private int logicalWidth = 450;
368    private int logicalHeight = 800;
369
370    @Override
371    protected void onCreate(Bundle savedInstanceState) {{
372        super.onCreate(savedInstanceState);
373        requestWindowFeature(Window.FEATURE_NO_TITLE);
374        float density = getResources().getDisplayMetrics().density;
375        dpiScale = Math.max(2, Math.round(density));
376        surfaceView = new SurfaceView(this);
377        surfaceView.getHolder().addCallback(this);
378        surfaceView.setOnTouchListener(this);
379        setContentView(surfaceView);
380    }}
381
382    @Override
383    public boolean onTouch(View v, MotionEvent event) {{
384        if (nativeHandle != 0L) {{
385            int vw = Math.max(1, v.getWidth());
386            int vh = Math.max(1, v.getHeight());
387            int lx = Math.round(event.getX() * logicalWidth / (float) vw);
388            int ly = Math.round(event.getY() * logicalHeight / (float) vh);
389            int pointerId = event.getPointerId(0);
390            nativePushTouch(
391                nativeHandle,
392                pointerId,
393                event.getActionMasked(),
394                lx,
395                ly
396            );
397        }}
398        return true;
399    }}
400
401    @Override
402    public void surfaceCreated(SurfaceHolder holder) {{
403        float density = getResources().getDisplayMetrics().density;
404        dpiScale = Math.max(2, Math.round(density));
405        int vw = surfaceView.getWidth() > 0 ? surfaceView.getWidth() : 900;
406        int vh = surfaceView.getHeight() > 0 ? surfaceView.getHeight() : 1600;
407        logicalWidth = Math.max(320, vw / dpiScale);
408        logicalHeight = Math.max(320, vh / dpiScale);
409        int physW = logicalWidth * dpiScale;
410        int physH = logicalHeight * dpiScale;
411        holder.setFixedSize(physW, physH);
412        nativeHandle = nativeCreate(logicalWidth, logicalHeight, 0, 0);
413        nativeSetDpiScale(nativeHandle, dpiScale);
414        Choreographer.getInstance().postFrameCallback(this);
415    }}
416
417    @Override
418    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {{
419        if (nativeHandle != 0L) {{
420            logicalWidth = Math.max(320, width / dpiScale);
421            logicalHeight = Math.max(320, height / dpiScale);
422            nativeSetDpiScale(nativeHandle, dpiScale);
423            nativeResize(nativeHandle, logicalWidth, logicalHeight);
424        }}
425    }}
426
427    @Override
428    public void surfaceDestroyed(SurfaceHolder holder) {{
429        if (nativeHandle != 0L) {{
430            nativeDestroy(nativeHandle);
431            nativeHandle = 0L;
432        }}
433    }}
434
435    @Override
436    public void onBackPressed() {{
437        if (nativeHandle != 0L && nativeHandleBack(nativeHandle)) {{
438            return;
439        }}
440        super.onBackPressed();
441    }}
442
443    @Override
444    public void doFrame(long frameTimeNanos) {{
445        if (nativeHandle != 0L && surfaceView.getHolder().getSurface().isValid()) {{
446            nativeStepAndBlitSurface(nativeHandle, surfaceView.getHolder().getSurface(), 16);
447            Choreographer.getInstance().postFrameCallback(this);
448        }}
449    }}
450
451    public static native long nativeCreate(int width, int height, int safeTop, int safeBottom);
452    public static native void nativeDestroy(long handle);
453    public static native void nativeResize(long handle, int width, int height);
454    public static native void nativeSetDpiScale(long handle, int scale);
455    public static native void nativeSetSafeArea(long handle, int top, int right, int bottom, int left);
456    public static native void nativeSetKeyboardInset(long handle, int height);
457    public static native void nativePushTouch(long handle, int pointerId, int action, int x, int y);
458    public static native boolean nativeHandleBack(long handle);
459    public static native int nativeStepAndBlitSurface(long handle, Object surface, int dtMs);
460}}
461"#
462    )
463}
464
465/// 生成 Android `AndroidManifest.xml` 宿主清单内容。
466pub fn generate_android_manifest_xml(package_name: &str, app_label: &str) -> std::string::String {
467    std::format!(
468        r#"<?xml version="1.0" encoding="utf-8"?>
469<manifest xmlns:android="http://schemas.android.com/apk/res/android"
470    package="{package_name}"
471    android:versionCode="1"
472    android:versionName="0.1.0">
473
474    <uses-sdk android:minSdkVersion="26" android:targetSdkVersion="29" />
475
476    <application
477        android:label="{app_label}"
478        android:hasCode="true">
479        <activity
480            android:name="com.velox.android.MainActivity"
481            android:theme="@android:style/Theme.NoTitleBar"
482            android:exported="true"
483            android:configChanges="orientation|screenSize|keyboardHidden">
484            <intent-filter>
485                <action android:name="android.intent.action.MAIN" />
486                <category android:name="android.intent.category.LAUNCHER" />
487            </intent-filter>
488        </activity>
489    </application>
490</manifest>
491"#
492    )
493}
494
495/// 将框架内置的 Android 宿主胶水文件(`AndroidManifest.xml`、`VeloxSurfaceView.kt`、`MainActivity.java`)一键输出至目标目录。
496pub fn emit_android_host_project(
497    out_dir: &std::path::Path,
498    package_name: &str,
499    lib_name: &str,
500    app_label: &str,
501) -> std::io::Result<()> {
502    let java_dir = out_dir.join("src").join("com").join("velox").join("android");
503    std::fs::create_dir_all(&java_dir)?;
504    std::fs::write(
505        out_dir.join("AndroidManifest.xml"),
506        generate_android_manifest_xml(package_name, app_label),
507    )?;
508    std::fs::write(out_dir.join("VeloxSurfaceView.kt"), ANDROID_SURFACE_VIEW_KT)?;
509    std::fs::write(
510        java_dir.join("MainActivity.java"),
511        generate_android_main_activity_java(lib_name),
512    )?;
513    Ok(())
514}
515
516/// 为指定业务视图类型一键导出完整的 Android C ABI (`velox_android_app_*` / `velox_mobile_app_*`) 与 `MainActivity` JNI 符号。
517#[macro_export]
518macro_rules! export_android_app {
519    ($app_ty:ty) => {
520        #[unsafe(no_mangle)]
521        pub extern "C" fn velox_android_app_create(
522            width: i16,
523            height: i16,
524            safe_top: i16,
525            safe_bottom: i16,
526        ) -> *mut $crate::platform::mobile::MobileEmbedApp {
527            $crate::platform::android::AndroidHostBridge::<$app_ty>::new(
528                width,
529                height,
530                safe_top,
531                safe_bottom,
532            )
533            .into_raw()
534        }
535
536        #[unsafe(no_mangle)]
537        pub unsafe extern "C" fn velox_mobile_app_destroy(
538            app: *mut $crate::platform::mobile::MobileEmbedApp,
539        ) {
540            unsafe {
541                $crate::platform::mobile::ffi::destroy(app);
542            }
543        }
544
545        #[unsafe(no_mangle)]
546        pub unsafe extern "C" fn velox_mobile_app_backend_is_gpu(
547            app: *const $crate::platform::mobile::MobileEmbedApp,
548        ) -> bool {
549            unsafe { $crate::platform::mobile::ffi::backend_is_gpu(app) }
550        }
551
552        #[unsafe(no_mangle)]
553        pub unsafe extern "C" fn velox_mobile_app_resize(
554            app: *mut $crate::platform::mobile::MobileEmbedApp,
555            width: i16,
556            height: i16,
557        ) {
558            unsafe {
559                $crate::platform::mobile::ffi::resize(app, width, height);
560            }
561        }
562
563        #[unsafe(no_mangle)]
564        pub unsafe extern "C" fn velox_mobile_app_set_dpi_scale(
565            app: *mut $crate::platform::mobile::MobileEmbedApp,
566            scale: i16,
567        ) {
568            unsafe {
569                $crate::platform::mobile::ffi::set_dpi_scale(app, scale);
570            }
571        }
572
573        #[unsafe(no_mangle)]
574        pub unsafe extern "C" fn velox_mobile_app_set_safe_area(
575            app: *mut $crate::platform::mobile::MobileEmbedApp,
576            top: i16,
577            right: i16,
578            bottom: i16,
579            left: i16,
580        ) {
581            unsafe {
582                $crate::platform::mobile::ffi::set_safe_area(app, top, right, bottom, left);
583            }
584        }
585
586        #[unsafe(no_mangle)]
587        pub unsafe extern "C" fn velox_mobile_app_set_keyboard_inset(
588            app: *mut $crate::platform::mobile::MobileEmbedApp,
589            height_px: i16,
590        ) {
591            unsafe {
592                $crate::platform::mobile::ffi::set_keyboard_inset(app, height_px);
593            }
594        }
595
596        #[unsafe(no_mangle)]
597        pub unsafe extern "C" fn velox_mobile_app_push_touch(
598            app: *mut $crate::platform::mobile::MobileEmbedApp,
599            touch_id: u32,
600            phase: u8,
601            x: i16,
602            y: i16,
603        ) {
604            unsafe {
605                $crate::platform::mobile::ffi::push_touch_raw(app, touch_id, phase, x, y);
606            }
607        }
608
609        #[unsafe(no_mangle)]
610        pub unsafe extern "C" fn velox_mobile_app_handle_back(
611            app: *mut $crate::platform::mobile::MobileEmbedApp,
612        ) -> bool {
613            unsafe { $crate::platform::mobile::ffi::handle_back(app) }
614        }
615
616        #[unsafe(no_mangle)]
617        pub unsafe extern "C" fn velox_mobile_app_step_frame(
618            app: *mut $crate::platform::mobile::MobileEmbedApp,
619            dt_ms: u16,
620        ) -> usize {
621            unsafe { $crate::platform::mobile::ffi::step_frame(app, dt_ms) }
622        }
623
624        #[unsafe(no_mangle)]
625        pub unsafe extern "C" fn velox_mobile_app_framebuffer_ptr(
626            app: *const $crate::platform::mobile::MobileEmbedApp,
627        ) -> *const u8 {
628            unsafe { $crate::platform::mobile::ffi::framebuffer_ptr(app) }
629        }
630
631        #[unsafe(no_mangle)]
632        pub unsafe extern "C" fn velox_mobile_app_dirty_rects_ptr(
633            app: *const $crate::platform::mobile::MobileEmbedApp,
634        ) -> *const $crate::platform::mobile::CDirtyRect {
635            unsafe { $crate::platform::mobile::ffi::dirty_rects_ptr(app) }
636        }
637
638        #[unsafe(no_mangle)]
639        pub unsafe extern "C" fn velox_mobile_app_dirty_rects_count(
640            app: *const $crate::platform::mobile::MobileEmbedApp,
641        ) -> usize {
642            unsafe { $crate::platform::mobile::ffi::dirty_rects_count(app) }
643        }
644
645        #[unsafe(no_mangle)]
646        pub extern "system" fn Java_com_velox_android_MainActivity_nativeCreate(
647            _env: *mut core::ffi::c_void,
648            _cls: *mut core::ffi::c_void,
649            width: i32,
650            height: i32,
651            safe_top: i32,
652            safe_bottom: i32,
653        ) -> i64 {
654            velox_android_app_create(
655                width as i16,
656                height as i16,
657                safe_top as i16,
658                safe_bottom as i16,
659            ) as usize as i64
660        }
661
662        #[unsafe(no_mangle)]
663        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeDestroy(
664            _env: *mut core::ffi::c_void,
665            _cls: *mut core::ffi::c_void,
666            handle: i64,
667        ) {
668            unsafe {
669                $crate::platform::mobile::ffi::destroy(
670                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
671                );
672            }
673        }
674
675        #[unsafe(no_mangle)]
676        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeResize(
677            _env: *mut core::ffi::c_void,
678            _cls: *mut core::ffi::c_void,
679            handle: i64,
680            width: i32,
681            height: i32,
682        ) {
683            unsafe {
684                $crate::platform::mobile::ffi::resize(
685                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
686                    width as i16,
687                    height as i16,
688                );
689            }
690        }
691
692        #[unsafe(no_mangle)]
693        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeSetDpiScale(
694            _env: *mut core::ffi::c_void,
695            _cls: *mut core::ffi::c_void,
696            handle: i64,
697            scale: i32,
698        ) {
699            unsafe {
700                $crate::platform::mobile::ffi::set_dpi_scale(
701                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
702                    scale as i16,
703                );
704            }
705        }
706
707        #[unsafe(no_mangle)]
708        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeSetSafeArea(
709            _env: *mut core::ffi::c_void,
710            _cls: *mut core::ffi::c_void,
711            handle: i64,
712            top: i32,
713            right: i32,
714            bottom: i32,
715            left: i32,
716        ) {
717            unsafe {
718                $crate::platform::mobile::ffi::set_safe_area(
719                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
720                    top as i16,
721                    right as i16,
722                    bottom as i16,
723                    left as i16,
724                );
725            }
726        }
727
728        #[unsafe(no_mangle)]
729        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeSetKeyboardInset(
730            _env: *mut core::ffi::c_void,
731            _cls: *mut core::ffi::c_void,
732            handle: i64,
733            height: i32,
734        ) {
735            unsafe {
736                $crate::platform::mobile::ffi::set_keyboard_inset(
737                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
738                    height as i16,
739                );
740            }
741        }
742
743        #[unsafe(no_mangle)]
744        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativePushTouch(
745            _env: *mut core::ffi::c_void,
746            _cls: *mut core::ffi::c_void,
747            handle: i64,
748            pointer_id: i32,
749            action: i32,
750            x: i32,
751            y: i32,
752        ) {
753            let phase = $crate::platform::mobile::TouchPhase::from_android_action(action as u8);
754            unsafe {
755                $crate::platform::mobile::ffi::push_touch(
756                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
757                    pointer_id as u32,
758                    phase,
759                    x as i16,
760                    y as i16,
761                );
762            }
763        }
764
765        #[unsafe(no_mangle)]
766        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeHandleBack(
767            _env: *mut core::ffi::c_void,
768            _cls: *mut core::ffi::c_void,
769            handle: i64,
770        ) -> u8 {
771            unsafe {
772                u8::from($crate::platform::mobile::ffi::handle_back(
773                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
774                ))
775            }
776        }
777
778        #[unsafe(no_mangle)]
779        pub unsafe extern "system" fn Java_com_velox_android_MainActivity_nativeStepAndBlitSurface(
780            env: *mut core::ffi::c_void,
781            _cls: *mut core::ffi::c_void,
782            handle: i64,
783            surface: *mut core::ffi::c_void,
784            dt_ms: i32,
785        ) -> i32 {
786            unsafe {
787                $crate::platform::android::step_and_blit_android_surface(
788                    env,
789                    handle as usize as *mut $crate::platform::mobile::MobileEmbedApp,
790                    surface,
791                    dt_ms,
792                )
793            }
794        }
795    };
796}