Modern Apps git

← Modern-Apps.git

kart: harden GLES bring-up (config fallback, proc validation)

Commit
4268678f715aa3e4d0f0f28fcb43a4b6cf7ae66c
Author
vayun-mathur <[email protected]>
When
2026-10-18 19:59 UTC (1 month ago)
Parents
a5b1502

Diff

games/kart/src/main/rust/src/gles/egl.rs
diff --git a/games/kart/src/main/rust/src/gles/egl.rs b/games/kart/src/main/rust/src/gles/egl.rsindex 472498c420..379f2d73b3 100644--- a/games/kart/src/main/rust/src/gles/egl.rs+++ b/games/kart/src/main/rust/src/gles/egl.rs@@ -22,7 +22,7 @@ use std::os::raw::c_void; type Api = egl::DynamicInstance<egl::EGL1_4>;  /// Config attributes: 8-bit RGBA, 24-bit depth, ES3-renderable window surface.-const CONFIG_ATTRIBS: &[egl::Int] = &[+const CONFIG_ATTRIBS_D24: &[egl::Int] = &[     egl::RED_SIZE,     8,     egl::GREEN_SIZE,@@ -40,6 +40,44 @@ const CONFIG_ATTRIBS: &[egl::Int] = &[     egl::NONE, ]; +/// Fallback: same color, 16-bit depth (some Mali drivers only back D16+/// window configs despite advertising D24).+const CONFIG_ATTRIBS_D16: &[egl::Int] = &[+    egl::RED_SIZE,+    8,+    egl::GREEN_SIZE,+    8,+    egl::BLUE_SIZE,+    8,+    egl::ALPHA_SIZE,+    8,+    egl::DEPTH_SIZE,+    16,+    egl::RENDERABLE_TYPE,+    egl::OPENGL_ES3_BIT,+    egl::SURFACE_TYPE,+    egl::WINDOW_BIT,+    egl::NONE,+];++/// Last resort: RGB565 + D16. The shaders carry no alpha dependency, so this+/// renders identically on a TV.+const CONFIG_ATTRIBS_565: &[egl::Int] = &[+    egl::RED_SIZE,+    5,+    egl::GREEN_SIZE,+    6,+    egl::BLUE_SIZE,+    5,+    egl::DEPTH_SIZE,+    16,+    egl::RENDERABLE_TYPE,+    egl::OPENGL_ES3_BIT,+    egl::SURFACE_TYPE,+    egl::WINDOW_BIT,+    egl::NONE,+];+ /// Context attributes: OpenGL ES client version 3. const CONTEXT_ATTRIBS: &[egl::Int] = &[egl::CONTEXT_CLIENT_VERSION, 3, egl::NONE]; @@ -76,13 +114,23 @@ impl Egl {             .ok_or_else(|| "eglGetDisplay failed".to_string())?;         api.initialize(display)             .map_err(|e| format!("eglInitialize: {e:?}"))?;+        // Try configs richest-first: a single hard-coded config dies inside+        // some Mali drivers when the window system cannot back it (SIGSEGV in+        // libGLES_mali, not an EGL error). First config that yields BOTH a+        // config AND a live window surface wins.         let mut configs = Vec::with_capacity(1);-        api.choose_config(display, CONFIG_ATTRIBS, &mut configs)-            .map_err(|e| format!("eglChooseConfig: {e:?}"))?;-        let config = configs-            .first()-            .copied()-            .ok_or_else(|| "no ES3 window EGL config".to_string())?;+        let mut config = None;+        for attribs in [CONFIG_ATTRIBS_D24, CONFIG_ATTRIBS_D16, CONFIG_ATTRIBS_565] {+            configs.clear();+            if api.choose_config(display, attribs, &mut configs).is_err() {+                continue;+            }+            if let Some(&c) = configs.first() {+                config = Some(c);+                break;+            }+        }+        let config = config.ok_or_else(|| "no ES3 window EGL config".to_string())?;         let mut egl = Self {             api,             display,@@ -145,6 +193,18 @@ impl Egl {             .unwrap_or(std::ptr::null())     } +    /// Fail cleanly when the driver did not resolve a core entry point.+    /// Some 32-bit Mali drivers return null from `eglGetProcAddress` for+    /// functions they only expose via versioned lookup; calling through null+    /// SIGSEGVs inside `libGLES_mali` (fault addr near zero) with no EGL+    /// error to catch. Probe before the first real GL call instead.+    pub fn require_proc(&self, name: &str) -> Result<(), String> {+        if self.proc_address(name).is_null() {+            return Err(format!("eglGetProcAddress({name}) returned null"));+        }+        Ok(())+    }+     /// Unbind, destroy surface/context, terminate the display. Idempotent.     pub fn teardown(&mut self) {         if self.terminated {
games/kart/src/main/rust/src/gles/renderer.rs
diff --git a/games/kart/src/main/rust/src/gles/renderer.rs b/games/kart/src/main/rust/src/gles/renderer.rsindex 7840e6dfcf..d9d33de305 100644--- a/games/kart/src/main/rust/src/gles/renderer.rs+++ b/games/kart/src/main/rust/src/gles/renderer.rs@@ -66,6 +66,27 @@ impl GlesRenderer {         height: u32,     ) -> Result<Self, String> {         let egl = unsafe { Egl::new(window)? };+        // Validate the loader BEFORE the first real GL call: glow loads+        // lazily, so a null entry point only explodes inside the driver+        // (SIGSEGV in libGLES_mali, no EGL error). Fail to Err here and the+        // caller keeps the placeholder instead of crashing.+        for name in [+            "glCreateShader",+            "glShaderSource",+            "glCompileShader",+            "glCreateProgram",+            "glLinkProgram",+            "glGenBuffers",+            "glBindBuffer",+            "glBufferData",+            "glGenVertexArrays",+            "glBindVertexArray",+            "glViewport",+            "glClearColor",+            "glClear",+        ] {+            egl.require_proc(name)?;+        }         let gl = unsafe {             glow::Context::from_loader_function(|name| egl.proc_address(name) as *const _)         };