diff --git a/crates/voelin-android/src/app.rs b/crates/voelin-android/src/app.rs index 53904e0..c1a5b3d 100644 --- a/crates/voelin-android/src/app.rs +++ b/crates/voelin-android/src/app.rs @@ -14,7 +14,7 @@ use voelin_core::Command; use crate::foreground::Foreground; use crate::host::EngineHost; -use crate::{bridge, capture, secrets}; +use crate::{bridge, capture, egl, secrets}; /// Store setting (`bool`): the user's opt-in to local crash reports. pub const CRASH_REPORTS_SETTING: &str = "crash_reports"; @@ -73,6 +73,8 @@ fn run(app: AndroidApp) -> anyhow::Result<()> { let host = host()?; static PROCESS: Once = Once::new(); PROCESS.call_once(|| { + // Before Slint creates the first window surface. + egl::reserve_skia_extension_slots(); capture::register(); watch_voice(host); }); diff --git a/crates/voelin-android/src/egl.rs b/crates/voelin-android/src/egl.rs new file mode 100644 index 0000000..9c34514 --- /dev/null +++ b/crates/voelin-android/src/egl.rs @@ -0,0 +1,135 @@ +//! Keep EGL extension slots for Skia (Mesa-based Android, e.g. Waydroid). +//! +//! Android's libEGL gives each GL function it has no built-in entry point for +//! one of a fixed number of "extension slots" (256) the first time +//! `eglGetProcAddress` looks it up. Slint's Skia OpenGL surface first loads +//! glow's whole function table, desktop GL included, and only then lets Skia +//! assemble its GLES interface. Vendor drivers return nothing for desktop-only +//! names, but Mesa returns an entry point for every name, so under Mesa glow +//! uses up all slots. Skia then gets nothing for the extension functions it +//! needs, e.g. `glTextureBarrierNV` when the driver advertises +//! `GL_NV_texture_barrier`, and the app cannot draw ("Could not create Skia +//! Direct Context from GL interface", with "no more slots for +//! eglGetProcAddress" before it in logcat). +//! +//! Looking Skia's extension functions up once, before Slint creates its +//! surface, gives them slots first; libEGL remembers them, and the lookups +//! that later fail are glow's desktop-only ones, which nothing here uses. + +use std::ffi::{CStr, c_char, c_void}; + +use tracing::info; + +#[link(name = "EGL")] +#[allow(unsafe_code)] // FFI declaration +unsafe extern "C" { + fn eglGetProcAddress(procname: *const c_char) -> *const c_void; +} + +/// Every extension function (name with a vendor suffix) that Skia's GLES +/// interface can look up: `GET_PROC_SUFFIX` in Skia m153's +/// `src/gpu/ganesh/gl/GrGLAssembleGLESInterfaceAutogen.cpp` (the version +/// skia-safe 0.153 builds). Core GLES functions have built-in entry points and +/// need no slot. Update the list when skia-safe moves to a new Skia milestone. +const SKIA_GLES_EXTENSION_FUNCTIONS: &[&CStr] = &[ + c"glBeginQueryEXT", + c"glBindFragDataLocationEXT", + c"glBindFragDataLocationIndexedEXT", + c"glBindUniformLocationCHROMIUM", + c"glBindVertexArrayOES", + c"glBlendBarrierKHR", + c"glBlendBarrierNV", + c"glBlitFramebufferANGLE", + c"glBlitFramebufferCHROMIUM", + c"glBlitFramebufferNV", + c"glClearTexImageEXT", + c"glClearTexSubImageEXT", + c"glClientWaitSyncAPPLE", + c"glCopyBufferSubDataNV", + c"glDebugMessageCallbackKHR", + c"glDebugMessageControlKHR", + c"glDebugMessageInsertKHR", + c"glDeleteFencesNV", + c"glDeleteQueriesEXT", + c"glDeleteSyncAPPLE", + c"glDeleteVertexArraysOES", + c"glDiscardFramebufferEXT", + c"glDrawArraysInstancedANGLE", + c"glDrawArraysInstancedBaseInstanceANGLE", + c"glDrawArraysInstancedBaseInstanceEXT", + c"glDrawArraysInstancedEXT", + c"glDrawElementsInstancedANGLE", + c"glDrawElementsInstancedBaseVertexBaseInstanceANGLE", + c"glDrawElementsInstancedBaseVertexBaseInstanceEXT", + c"glDrawElementsInstancedEXT", + c"glEndQueryEXT", + c"glEndTilingQCOM", + c"glFenceSyncAPPLE", + c"glFinishFenceNV", + c"glFlushMappedBufferRangeEXT", + c"glFramebufferTexture2DMultisampleEXT", + c"glFramebufferTexture2DMultisampleIMG", + c"glGenFencesNV", + c"glGenQueriesEXT", + c"glGenVertexArraysOES", + c"glGetDebugMessageLogKHR", + c"glGetProgramBinaryOES", + c"glGetQueryObjecti64vEXT", + c"glGetQueryObjectui64vEXT", + c"glGetQueryObjectuivEXT", + c"glGetQueryivEXT", + c"glInsertEventMarkerEXT", + c"glIsSyncAPPLE", + c"glMapBufferOES", + c"glMapBufferRangeEXT", + c"glMapBufferSubDataCHROMIUM", + c"glMapTexSubImage2DCHROMIUM", + c"glMultiDrawArraysIndirectEXT", + c"glMultiDrawArraysInstancedBaseInstanceANGLE", + c"glMultiDrawElementsIndirectEXT", + c"glMultiDrawElementsInstancedBaseVertexBaseInstanceANGLE", + c"glObjectLabelKHR", + c"glPatchParameteriOES", + c"glPopDebugGroupKHR", + c"glPopGroupMarkerEXT", + c"glProgramBinaryOES", + c"glPushDebugGroupKHR", + c"glPushGroupMarkerEXT", + c"glQueryCounterEXT", + c"glRenderbufferStorageMultisampleANGLE", + c"glRenderbufferStorageMultisampleCHROMIUM", + c"glResolveMultisampleFramebufferAPPLE", + c"glSetFenceNV", + c"glStartTilingQCOM", + c"glTestFenceNV", + c"glTexBufferEXT", + c"glTexBufferOES", + c"glTexBufferRangeEXT", + c"glTexBufferRangeOES", + c"glTexStorage2DEXT", + c"glTextureBarrierNV", + c"glUnmapBufferOES", + c"glUnmapBufferSubDataCHROMIUM", + c"glUnmapTexSubImage2DCHROMIUM", + c"glVertexAttribDivisorANGLE", + c"glVertexAttribDivisorEXT", + c"glWaitSyncAPPLE", + c"glWindowRectanglesEXT", +]; + +/// Look up [`SKIA_GLES_EXTENSION_FUNCTIONS`] so libEGL keeps their slots. +/// Call once per process, before the first window is shown. +pub fn reserve_skia_extension_slots() { + let found = SKIA_GLES_EXTENSION_FUNCTIONS.iter().filter(|name| resolves(name)).count(); + info!( + "EGL: {found} of {} Skia extension functions resolved ahead of Slint's surface", + SKIA_GLES_EXTENSION_FUNCTIONS.len() + ); +} + +#[allow(unsafe_code)] // the FFI call +fn resolves(name: &CStr) -> bool { + // SAFETY: `name` is NUL-terminated; eglGetProcAddress may be the first EGL + // call of the process and only returns an address, which is not used. + !unsafe { eglGetProcAddress(name.as_ptr()) }.is_null() +} diff --git a/crates/voelin-android/src/lib.rs b/crates/voelin-android/src/lib.rs index bfa0911..117ca35 100644 --- a/crates/voelin-android/src/lib.rs +++ b/crates/voelin-android/src/lib.rs @@ -14,6 +14,8 @@ //! - `capture` (Android): screen and system-audio capture through //! MediaProjection, registered as `voelin_media` capture providers //! - `secrets` (Android): passwords encrypted with an Android Keystore key +//! - `egl` (Android): keeps EGL extension slots for Skia, without which the +//! UI cannot draw on Mesa-based Android (Waydroid) //! //! Only [`host`] and [`foreground`] build on other platforms, for tests. @@ -27,4 +29,6 @@ mod bridge; #[cfg(target_os = "android")] mod capture; #[cfg(target_os = "android")] +mod egl; +#[cfg(target_os = "android")] mod secrets; diff --git a/docs/android.md b/docs/android.md index 1e3d28a..d0ec1ef 100644 --- a/docs/android.md +++ b/docs/android.md @@ -118,6 +118,7 @@ Permissions: `INTERNET`, `ACCESS_NETWORK_STATE`, `RECORD_AUDIO`, | Engine host replay, voice service policy | unit tests (`cargo test -p voelin-android`) | tested | | External capture providers, MediaCodec buffer layouts | `voelin-media` unit tests | tested | | The app on a device: UI, voice in the background, screen sharing, watching, Keystore | manual matrix row IN3 and the Android rows | not run yet (no device or emulator in this environment) | +| Waydroid (Mesa GL) | the debug APK, `waydroid logcat` | the first start failed: Skia could not create its GL context; worked around (below), to be re-checked | Known gaps: @@ -133,3 +134,23 @@ Known gaps: - No audio focus handling yet: a phone call does not pause our audio (matrix row AN4). After a network change (Wi-Fi to mobile data) only tsclientlib's own reconnect applies; untested on a phone. + +### Mesa-based Android (Waydroid) + +Slint draws with Skia over OpenGL ES. Before Skia looks up its functions, +Slint's surface loads glow's whole GL function table through +`eglGetProcAddress`. Android's libEGL gives every function it has no built-in +entry point for one of 256 "extension slots", and Mesa answers every name, +desktop GL included, so the slots run out; Skia then gets nothing for an +extension function it needs (`glTextureBarrierNV`) and the app ends right +after start: + +``` +E libEGL : no more slots for eglGetProcAddress("glTextureBarrierNV") +I RustStdoutStderr: … Could not create Skia Direct Context from GL interface +``` + +`crates/voelin-android/src/egl.rs` looks up Skia's extension functions once +at start, before Slint's surface, so they get slots first (logcat: +`EGL: N of 83 Skia extension functions resolved`). Vendor GPU drivers on +phones return nothing for desktop-only names and are not affected.