mod host_config;
use std::ffi::{CStr, CString, c_char};
use euv::{
AdaptiveUploadPlan, App, DamageRegion, DeviceMotionSample, DirectGpuBatchRange,
DirectGpuFrameBatch, GpuPrimitiveInstance, ImeEvent, IosPresentTelemetry, KeyCode, KeyEvent,
MAX_REFRESH_RATE_HZ, MIN_REFRESH_RATE_HZ, MSL_UI_SHADER, MemoryPressure,
MobileFailoverAction, MobileFailoverController, MobilePresentPath, MobileSurfaceFault,
MobileUploadStrategy, Modifiers, NativeSystemFont, NetworkStatus, PixelDirtyRect, PlatformKind,
PointerEvent, RenderMode, SafeAreaInsets, SystemEvent, ThemeMode, WindowEvent, WindowId,
compile_display_list_direct_gpu_batch, damage_region_to_pixel_dirty_rects,
ios_is_simulator_target, register_native_action_bridge, reset_reactive_runtime,
select_adaptive_upload_plan,
};
use euv_example::{ShowcaseState, create_showcase_app_for_platform};
unsafe extern "C" {
fn euv_ios_invoke_native_action(
kind: *const c_char,
arg1: *const c_char,
arg2: *const c_char,
) -> i32;
}
fn ios_native_action_bridge(kind: &str, arg1: &str, arg2: &str) -> bool {
let Ok(c_kind) = CString::new(kind) else {
return false;
};
let Ok(c_a1) = CString::new(arg1) else {
return false;
};
let Ok(c_a2) = CString::new(arg2) else {
return false;
};
unsafe { euv_ios_invoke_native_action(c_kind.as_ptr(), c_a1.as_ptr(), c_a2.as_ptr()) != 0 }
}
#[repr(C)]
pub struct EuvIosDirectFrameDesc {
pub viewport_w: u32,
pub viewport_h: u32,
pub clear_rgba: [f32; 4],
pub primitives_ptr: *const GpuPrimitiveInstance,
pub primitives_len: usize,
pub ranges_ptr: *const DirectGpuBatchRange,
pub ranges_len: usize,
pub glyph_r8_ptr: *const u8,
pub glyph_r8_w: u32,
pub glyph_r8_h: u32,
pub glyph_r8_rev: u64,
pub image_rgba8_ptr: *const u8,
pub image_rgba8_w: u32,
pub image_rgba8_h: u32,
pub upload_bytes: u64,
}
pub struct EuvIosHandle {
app: App,
_state: ShowcaseState,
wid: WindowId,
width: u32,
height: u32,
direct_batch: DirectGpuFrameBatch,
upload_plan: AdaptiveUploadPlan,
uploaded_glyph_revision: u64,
present_telemetry: IosPresentTelemetry,
failover: MobileFailoverController,
msl_ui_shader_cstr: CString,
}
#[unsafe(no_mangle)]
pub extern "C" fn euv_ios_create(
phys_width: u32,
phys_height: u32,
scale: f32,
is_ipad: i32,
refresh_hz: f32,
safe_top: f32,
safe_right: f32,
safe_bottom: f32,
safe_left: f32,
) -> *mut EuvIosHandle {
register_native_action_bridge(ios_native_action_bridge);
reset_reactive_runtime();
let platform: PlatformKind = if is_ipad != 0 {
PlatformKind::IpadOs
} else {
PlatformKind::Ios
};
let Ok((mut app, state)) =
create_showcase_app_for_platform(platform, host_config::HOST_RENDER_MODE)
else {
return std::ptr::null_mut();
};
app.get_mut_renderer()
.get_mut_software()
.set_font_provider(NativeSystemFont::system());
let hz: f32 = refresh_hz.clamp(MIN_REFRESH_RATE_HZ, MAX_REFRESH_RATE_HZ);
app.update_display_refresh_rate_hz(hz);
let wid: WindowId = app.get_window_state().get_id();
let pw: u32 = phys_width.max(200);
let ph: u32 = phys_height.max(200);
let s: f32 = scale.clamp(1.0, 4.5);
app.set_exact_physical_viewport(pw, ph, s);
app.dispatch_window_event(WindowEvent::SafeAreaChanged(
wid,
SafeAreaInsets::new(
safe_top.max(0.0),
safe_right.max(0.0),
safe_bottom.max(0.0),
safe_left.max(0.0),
),
));
let preferred_path: MobilePresentPath = if host_config::HOST_RENDER_MODE == RenderMode::Software {
MobilePresentPath::Software
} else {
MobilePresentPath::GpuDirect
};
let is_sim: bool = ios_is_simulator_target();
let msl_ui_shader_cstr: CString =
CString::new(MSL_UI_SHADER).unwrap_or_else(|_| CString::new("").unwrap());
let handle = Box::new(EuvIosHandle {
app,
_state: state,
wid,
width: 0,
height: 0,
direct_batch: DirectGpuFrameBatch::default(),
upload_plan: AdaptiveUploadPlan::default(),
uploaded_glyph_revision: 0,
present_telemetry: IosPresentTelemetry::for_host(preferred_path, hz, is_sim),
failover: MobileFailoverController::for_preferred(
preferred_path,
preferred_path == MobilePresentPath::GpuDirect,
preferred_path != MobilePresentPath::Software,
),
msl_ui_shader_cstr,
});
Box::into_raw(handle)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_destroy(handle: *mut EuvIosHandle) {
if handle.is_null() {
return;
}
let boxed: Box<EuvIosHandle> = unsafe { Box::from_raw(handle) };
drop(boxed);
reset_reactive_runtime();
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_is_running(handle: *mut EuvIosHandle) -> i32 {
if handle.is_null() {
return 0;
}
let h: &EuvIosHandle = unsafe { &*handle };
if h.app.get_running() { 1 } else { 0 }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_update_refresh_rate(handle: *mut EuvIosHandle, refresh_hz: f32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.update_display_refresh_rate_hz(refresh_hz.clamp(MIN_REFRESH_RATE_HZ, MAX_REFRESH_RATE_HZ));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_ime_disabled(handle: *mut EuvIosHandle) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_window_event(WindowEvent::Ime(h.wid, ImeEvent::Disabled));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_system_lifecycle(handle: *mut EuvIosHandle, tag: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let ev: SystemEvent = match tag {
1 => SystemEvent::Appear,
2 => SystemEvent::Disappear,
3 => SystemEvent::Foreground,
4 => SystemEvent::Inactive,
5 => SystemEvent::Background,
6 => SystemEvent::Terminate,
_ => return,
};
h.app.dispatch_system_event(ev);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_focus(handle: *mut EuvIosHandle, focused: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_system_event(SystemEvent::FocusChanged(focused != 0));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_keyboard(handle: *mut EuvIosHandle, visible: i32, height: f32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_system_event(SystemEvent::KeyboardVisibilityChanged {
visible: visible != 0,
height: height.max(0.0),
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_theme(handle: *mut EuvIosHandle, is_dark: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let mode: ThemeMode = if is_dark != 0 {
ThemeMode::Dark
} else {
ThemeMode::Light
};
h.app.dispatch_system_event(SystemEvent::ThemeChanged(mode));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_memory_warning(handle: *mut EuvIosHandle) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_system_event(SystemEvent::MemoryWarning(MemoryPressure::Critical));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_low_power(handle: *mut EuvIosHandle, enabled: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_system_event(SystemEvent::LowPowerModeChanged(enabled != 0));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_locale(handle: *mut EuvIosHandle, locale: *const c_char) {
if handle.is_null() || locale.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
if let Ok(s) = unsafe { CStr::from_ptr(locale) }.to_str()
&& !s.is_empty()
{
h.app
.dispatch_system_event(SystemEvent::LocaleChanged(s.to_string()));
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_deep_link(handle: *mut EuvIosHandle, uri: *const c_char) {
if handle.is_null() || uri.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
if let Ok(s) = unsafe { CStr::from_ptr(uri) }.to_str()
&& !s.is_empty()
{
h.app
.dispatch_system_event(SystemEvent::DeepLink(s.to_string()));
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_clipboard_changed(handle: *mut EuvIosHandle) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.dispatch_system_event(SystemEvent::ClipboardChanged);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_device_motion(
handle: *mut EuvIosHandle,
gx: f32,
gy: f32,
gz: f32,
lx: f32,
ly: f32,
lz: f32,
wx: f32,
wy: f32,
wz: f32,
pitch: f32,
roll: f32,
yaw: f32,
timestamp_ns: u64,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let sample: DeviceMotionSample = DeviceMotionSample::from_vectors(
[gx, gy, gz],
[lx, ly, lz],
[wx, wy, wz],
[pitch, roll, yaw],
timestamp_ns,
true,
);
h.app
.dispatch_window_event(WindowEvent::System(SystemEvent::DeviceMotion(sample)));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_network(handle: *mut EuvIosHandle, status_tag: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let status: NetworkStatus = match status_tag {
0 => NetworkStatus::Offline,
1 => NetworkStatus::Online,
2 => NetworkStatus::Wifi,
3 => NetworkStatus::Cellular,
_ => NetworkStatus::Unknown,
};
h.app
.dispatch_system_event(SystemEvent::NetworkStatusChanged(status));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_resize(
handle: *mut EuvIosHandle,
phys_width: u32,
phys_height: u32,
scale: f32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let pw: u32 = phys_width.max(200);
let ph: u32 = phys_height.max(200);
let s: f32 = scale.clamp(1.0, 4.5);
h.app.set_exact_physical_viewport(pw, ph, s);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_set_safe_area(
handle: *mut EuvIosHandle,
top: f32,
right: f32,
bottom: f32,
left: f32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.dispatch_window_event(WindowEvent::SafeAreaChanged(
h.wid,
SafeAreaInsets::new(top.max(0.0), right.max(0.0), bottom.max(0.0), left.max(0.0)),
));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_touch_down(
handle: *mut EuvIosHandle,
pointer_id: u64,
x: f32,
y: f32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.dispatch_window_event(WindowEvent::PointerDown(
h.wid,
PointerEvent::touch(pointer_id.max(1), x, y),
));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_touch_move(
handle: *mut EuvIosHandle,
pointer_id: u64,
x: f32,
y: f32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.dispatch_window_event(WindowEvent::PointerMove(
h.wid,
PointerEvent::touch(pointer_id.max(1), x, y),
));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_touch_up(
handle: *mut EuvIosHandle,
pointer_id: u64,
x: f32,
y: f32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.dispatch_window_event(WindowEvent::PointerUp(
h.wid,
PointerEvent::touch(pointer_id.max(1), x, y),
));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_touch_cancel(handle: *mut EuvIosHandle, pointer_id: u64) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app
.dispatch_window_event(WindowEvent::PointerCancel(h.wid, pointer_id));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_touch_sync(
handle: *mut EuvIosHandle,
ids: *const u64,
count: usize,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let live: Vec<u64> = if ids.is_null() || count == 0 {
Vec::new()
} else {
let slice: &[u64] = unsafe { std::slice::from_raw_parts(ids, count.min(32)) };
slice.iter().map(|id: &u64| (*id).max(1)).collect()
};
h.app.reconcile_active_touch_ids(&live);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_text_input(handle: *mut EuvIosHandle, text: *const c_char) {
if handle.is_null() || text.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
if let Ok(s) = unsafe { CStr::from_ptr(text) }.to_str()
&& !s.is_empty()
{
h.app
.dispatch_window_event(WindowEvent::TextInput(h.wid, s.to_string()));
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_key_down(handle: *mut EuvIosHandle, key_tag: i32) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let code: KeyCode = match key_tag {
1 => KeyCode::Backspace,
2 => KeyCode::Enter,
3 => KeyCode::Tab,
4 => KeyCode::Escape,
_ => return,
};
h.app.dispatch_window_event(WindowEvent::KeyDown(
h.wid,
KeyEvent::new(code, Modifiers::default(), false),
));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_step(
handle: *mut EuvIosHandle,
dt: f32,
out_ptr: *mut *const u8,
out_w: *mut u32,
out_h: *mut u32,
out_dx: *mut u32,
out_dy: *mut u32,
out_dw: *mut u32,
out_dh: *mut u32,
) -> i32 {
if handle.is_null() {
return 0;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let Ok(stats) = h
.app
.step_game_frame(dt.clamp(1.0 / MAX_REFRESH_RATE_HZ, 0.05))
else {
return 0;
};
let prev_w: u32 = h.width;
let prev_h: u32 = h.height;
let changed: bool = stats.get_pixels_shaded() > 0 || prev_w == 0;
let mut dx: u32 = 0;
let mut dy: u32 = 0;
let mut dw: u32 = 0;
let mut dh: u32 = 0;
if let Some(fb) = h.app.get_renderer().framebuffer() {
let w: u32 = fb.get_width();
let ht: u32 = fb.get_height();
h.width = w;
h.height = ht;
if !out_ptr.is_null() {
unsafe { *out_ptr = fb.get_pixels().as_ptr() };
}
dw = w;
dh = ht;
if prev_w == w && prev_h == ht {
let dmg = h.app.last_frame_damage();
if !dmg.get_full_damage()
&& let Some(r) = dmg.resolve(w, ht)
{
let rx0: u32 = (r.get_x().floor().max(0.0) as u32).min(w);
let ry0: u32 = (r.get_y().floor().max(0.0) as u32).min(ht);
let rx1: u32 = (r.right().ceil().max(0.0) as u32).clamp(rx0, w);
let ry1: u32 = (r.bottom().ceil().max(0.0) as u32).clamp(ry0, ht);
if rx1 > rx0 && ry1 > ry0 {
dx = rx0;
dy = ry0;
dw = rx1 - rx0;
dh = ry1 - ry0;
}
}
}
} else if !out_ptr.is_null() {
unsafe { *out_ptr = std::ptr::null() };
}
if !out_w.is_null() {
unsafe { *out_w = h.width };
}
if !out_h.is_null() {
unsafe { *out_h = h.height };
}
if !out_dx.is_null() {
unsafe { *out_dx = dx };
}
if !out_dy.is_null() {
unsafe { *out_dy = dy };
}
if !out_dw.is_null() {
unsafe { *out_dw = dw };
}
if !out_dh.is_null() {
unsafe { *out_dh = dh };
}
if changed { 1 } else { 0 }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_msl_ui_shader_source(handle: *mut EuvIosHandle) -> *const c_char {
if handle.is_null() {
return std::ptr::null();
}
let h: &EuvIosHandle = unsafe { &*handle };
h.msl_ui_shader_cstr.as_ptr()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_configure_metal_capabilities(
handle: *mut EuvIosHandle,
direct_gpu_ready: i32,
upload_ready: i32,
) -> i32 {
if handle.is_null() {
return MobilePresentPath::Software as i32;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let preferred: MobilePresentPath = if host_config::HOST_RENDER_MODE == RenderMode::Software {
MobilePresentPath::Software
} else {
MobilePresentPath::GpuDirect
};
h.failover = MobileFailoverController::for_preferred(
preferred,
direct_gpu_ready != 0 && preferred == MobilePresentPath::GpuDirect,
upload_ready != 0 && preferred != MobilePresentPath::Software,
);
h.failover.set_surface_valid(true);
let active: MobilePresentPath = h.failover.get_active_path();
let use_direct: bool = active == MobilePresentPath::GpuDirect;
h.app.get_mut_renderer().set_direct_gpu_present(use_direct);
h.present_telemetry.get_mut_base().set_active_path(active);
h.present_telemetry
.get_mut_base()
.set_cpu_mirror_active(!use_direct);
h.present_telemetry.get_mut_base().set_upload_strategy(if use_direct {
MobileUploadStrategy::DirectGpu
} else {
MobileUploadStrategy::FullFrame
});
active as i32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_record_display_link(
handle: *mut EuvIosHandle,
timestamp_secs: f64,
target_timestamp_secs: f64,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.present_telemetry
.record_display_link_tick(timestamp_secs, target_timestamp_secs);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_record_metal_presented(
handle: *mut EuvIosHandle,
presented_time_secs: f64,
gpu_exec_us: u64,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.present_telemetry.set_metal_gpu_exec_us(gpu_exec_us);
h.present_telemetry
.record_metal_presented_time(presented_time_secs);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_handle_metal_fault(
handle: *mut EuvIosHandle,
fault_tag: i32,
) -> i32 {
if handle.is_null() {
return MobilePresentPath::Software as i32;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
let fault: MobileSurfaceFault = match fault_tag {
0 => MobileSurfaceFault::ContextLost,
1 => MobileSurfaceFault::SurfaceLost,
2 => MobileSurfaceFault::ShaderCompileFailed,
_ => MobileSurfaceFault::SubmitFailed,
};
let action: MobileFailoverAction = h
.present_telemetry
.record_fault_with_failover(fault, &mut h.failover);
if matches!(action, MobileFailoverAction::KeepLastFrameAndSwitch(_)) {
let next_path: MobilePresentPath = h.failover.get_active_path();
h.app
.get_mut_renderer()
.set_direct_gpu_present(next_path == MobilePresentPath::GpuDirect);
if next_path != MobilePresentPath::GpuDirect {
let _ = h
.app
.get_mut_renderer()
.capture_reference_framebuffer(DamageRegion::full());
}
}
h.failover.get_active_path() as i32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_active_present_path(handle: *mut EuvIosHandle) -> i32 {
if handle.is_null() {
return MobilePresentPath::Software as i32;
}
let h: &EuvIosHandle = unsafe { &*handle };
h.failover.get_active_path() as i32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_cpu_mirror_active(handle: *mut EuvIosHandle) -> i32 {
if handle.is_null() {
return 1;
}
let h: &EuvIosHandle = unsafe { &*handle };
if h.present_telemetry.get_base().get_cpu_mirror_active() { 1 } else { 0 }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_upload_strategy(handle: *mut EuvIosHandle) -> i32 {
if handle.is_null() {
return MobileUploadStrategy::None as i32;
}
let h: &EuvIosHandle = unsafe { &*handle };
h.present_telemetry.get_base().get_upload_strategy() as i32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_last_upload_bytes(handle: *mut EuvIosHandle) -> u64 {
if handle.is_null() {
return 0;
}
let h: &EuvIosHandle = unsafe { &*handle };
h.present_telemetry.get_base().get_upload_bytes_last_frame()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_timing_source(handle: *mut EuvIosHandle) -> i32 {
if handle.is_null() {
return 2;
}
let h: &EuvIosHandle = unsafe { &*handle };
h.present_telemetry.get_base().get_timing_source() as i32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_step_direct_gpu(
handle: *mut EuvIosHandle,
dt: f32,
force: i32,
out_desc: *mut EuvIosDirectFrameDesc,
) -> i32 {
if handle.is_null() || out_desc.is_null() {
return 0;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
if h.failover.get_active_path() != MobilePresentPath::GpuDirect {
return 0;
}
h.app.get_mut_renderer().set_direct_gpu_present(true);
let Ok(stats) = h
.app
.step_game_frame(dt.clamp(1.0 / MAX_REFRESH_RATE_HZ, 0.05))
else {
return 0;
};
let changed: bool =
stats.get_pixels_shaded() > 0 || stats.get_draw_commands() > 0 || h.width == 0 || force != 0;
if !changed {
return 0;
}
h.direct_batch = {
let renderer = h.app.get_mut_renderer();
let dl_clone = renderer.get_display_list().clone();
let font_provider: NativeSystemFont =
renderer.get_font_service().get_font_provider().clone();
let glyph_atlas = renderer.get_mut_font_service().get_mut_glyph_atlas();
compile_display_list_direct_gpu_batch(
&dl_clone,
Some(glyph_atlas),
Some(&font_provider),
h.uploaded_glyph_revision,
)
};
h.width = h.direct_batch.get_viewport_width();
h.height = h.direct_batch.get_viewport_height();
let (glyph_ptr, glyph_w, glyph_h, glyph_rev): (*const u8, u32, u32, u64) =
match h.direct_batch.glyph_r8_atlas_ref() {
Some(a) if a.get_width() > 0 && a.get_height() > 0 => (
a.get_pixels().as_ptr(),
a.get_width(),
a.get_height(),
a.get_revision(),
),
_ => (std::ptr::null(), 0, 0, h.uploaded_glyph_revision),
};
let (img_ptr, img_w, img_h): (*const u8, u32, u32) =
match h.direct_batch.image_rgba8_atlas_ref() {
Some(a) if a.get_width() > 0 && a.get_height() > 0 => {
(a.get_pixels().as_ptr(), a.get_width(), a.get_height())
}
_ => (std::ptr::null(), 0, 0),
};
unsafe {
*out_desc = EuvIosDirectFrameDesc {
viewport_w: h.direct_batch.get_viewport_width(),
viewport_h: h.direct_batch.get_viewport_height(),
clear_rgba: h.direct_batch.get_clear_rgba(),
primitives_ptr: h.direct_batch.get_primitives().as_ptr(),
primitives_len: h.direct_batch.get_primitives().len(),
ranges_ptr: h.direct_batch.get_draw_ranges().as_ptr(),
ranges_len: h.direct_batch.get_draw_ranges().len(),
glyph_r8_ptr: glyph_ptr,
glyph_r8_w: glyph_w,
glyph_r8_h: glyph_h,
glyph_r8_rev: glyph_rev,
image_rgba8_ptr: img_ptr,
image_rgba8_w: img_w,
image_rgba8_h: img_h,
upload_bytes: h.direct_batch.get_instance_upload_bytes() as u64,
};
}
1
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_commit_direct_gpu_present(
handle: *mut EuvIosHandle,
glyph_rev: u64,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
if glyph_rev > 0 {
h.uploaded_glyph_revision = glyph_rev;
}
h.failover.mark_frame_presented();
let gpu_exec_us: u64 = h.present_telemetry.get_metal_gpu_exec_us();
let presented_ns: Option<u64> = h.present_telemetry.try_get_metal_presented_time_ns();
h.present_telemetry
.record_direct_metal_present(&h.direct_batch, gpu_exec_us, presented_ns);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_step_upload_plan(
handle: *mut EuvIosHandle,
dt: f32,
force: i32,
out_ptr: *mut *const u8,
out_w: *mut u32,
out_h: *mut u32,
out_rects_ptr: *mut *const PixelDirtyRect,
out_rects_len: *mut usize,
out_strategy: *mut i32,
) -> i32 {
if handle.is_null() {
return 0;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.app.get_mut_renderer().set_direct_gpu_present(false);
let Ok(stats) = h
.app
.step_game_frame(dt.clamp(1.0 / MAX_REFRESH_RATE_HZ, 0.05))
else {
return 0;
};
let prev_w: u32 = h.width;
let prev_h: u32 = h.height;
let changed: bool = stats.get_pixels_shaded() > 0 || prev_w == 0 || force != 0;
if !changed {
return 0;
}
if h.app.get_renderer().framebuffer().is_none() {
let _ = h
.app
.get_mut_renderer()
.capture_reference_framebuffer(DamageRegion::full());
}
let Some(fb) = h.app.get_renderer().framebuffer() else {
return 0;
};
let w: u32 = fb.get_width();
let ht: u32 = fb.get_height();
h.width = w;
h.height = ht;
let dmg = h.app.last_frame_damage();
let dirty_rects: Vec<PixelDirtyRect> = damage_region_to_pixel_dirty_rects(dmg, w, ht);
let force_full: bool = force != 0 || prev_w != w || prev_h != ht || dmg.get_full_damage();
h.upload_plan = select_adaptive_upload_plan(w, ht, &dirty_rects, force_full);
if !out_ptr.is_null() {
unsafe { *out_ptr = fb.get_pixels().as_ptr() };
}
if !out_w.is_null() {
unsafe { *out_w = w };
}
if !out_h.is_null() {
unsafe { *out_h = ht };
}
if !out_rects_ptr.is_null() {
unsafe { *out_rects_ptr = h.upload_plan.get_rects().as_ptr() };
}
if !out_rects_len.is_null() {
unsafe { *out_rects_len = h.upload_plan.get_rects().len() };
}
if !out_strategy.is_null() {
unsafe { *out_strategy = h.upload_plan.get_strategy() as i32 };
}
1
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_ios_commit_fallback_present(
handle: *mut EuvIosHandle,
used_metal_upload: i32,
) {
if handle.is_null() {
return;
}
let h: &mut EuvIosHandle = unsafe { &mut *handle };
h.failover.mark_frame_presented();
if used_metal_upload != 0 {
let presented_ns: Option<u64> = h.present_telemetry.try_get_metal_presented_time_ns();
h.present_telemetry
.record_metal_upload_present(&h.upload_plan, presented_ns);
} else {
h.present_telemetry
.record_cg_software_present(&h.upload_plan);
}
}