cmd/ebitenmobile: add ConfigChooserWithFallback

Closes #2740
This commit is contained in:
Hajime Hoshi
2026-01-18 18:43:12 +09:00
parent c9dcb04d44
commit f0d5e9a26f
+41 -1
View File
@@ -21,13 +21,53 @@ import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;
import $Placeholder_JavaPkg$.ebitenmobileview.Ebitenmobileview;
import $Placeholder_JavaPkg$.ebitenmobileview.Renderer;
import $Placeholder_JavaPkg$.$Placeholder_PrefixLower$.EbitenView;
class ConfigChooserWithFallback implements GLSurfaceView.EGLConfigChooser {
// EGL_OPENGL_ES2_BIT is defined in EGL14, but not in EGL10.
private static final int EGL_OPENGL_ES2_BIT = 4;
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
int[] attribs = {
EGL10.EGL_RED_SIZE, 8,
EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_DEPTH_SIZE, 0,
EGL10.EGL_STENCIL_SIZE, 0,
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // often compatible with ES3.
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfig = new int[1];
if (egl.eglChooseConfig(display, attribs, configs, 1, numConfig) && numConfig[0] > 0) {
return configs[0];
}
// Try the fallback config.
attribs = new int[] {
EGL10.EGL_RED_SIZE, 5,
EGL10.EGL_GREEN_SIZE, 6,
EGL10.EGL_BLUE_SIZE, 5,
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL10.EGL_NONE
};
if (egl.eglChooseConfig(display, attribs, configs, 1, numConfig) && numConfig[0] > 0) {
return configs[0];
}
throw new IllegalStateException("No suitable EGLConfig found; RGB8888 and RGB565 formats are not supported");
}
}
class EbitenSurfaceView extends GLSurfaceView implements Renderer {
// As GLSurfaceView can be recreated, the states must be static (#3097).
static private boolean errored_ = false;
@@ -83,7 +123,7 @@ class EbitenSurfaceView extends GLSurfaceView implements Renderer {
private void initialize() {
setEGLContextClientVersion(3);
setEGLConfigChooser(8, 8, 8, 8, 0, 0);
setEGLConfigChooser(new ConfigChooserWithFallback());
setPreserveEGLContextOnPause(true);
// setRenderer must be called before Ebitenmobileview.setRenderer.
// Otherwise, setRenderMode in setExplicitRenderingMode will crash.