diff --git a/Celestia/src/main/cpp/CelestiaRenderer.cpp b/Celestia/src/main/cpp/CelestiaRenderer.cpp index fd7fe659..4a409c20 100644 --- a/Celestia/src/main/cpp/CelestiaRenderer.cpp +++ b/Celestia/src/main/cpp/CelestiaRenderer.cpp @@ -101,6 +101,36 @@ JavaVM *CelestiaRenderer::jvm = nullptr; jmethodID CelestiaRenderer::flushTasksMethod = nullptr; jmethodID CelestiaRenderer::engineStartedMethod = nullptr; +static bool ChooseOpaqueConfig(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* config) +{ + EGLint configCount = 0; + // Get config count + if (!eglChooseConfig(dpy, attrib_list, nullptr, 0, &configCount) || configCount == 0) + return false; + auto configs = new EGLConfig[configCount]; + // Get all the configs + EGLint newConfigCount = 0; + if (!eglChooseConfig(dpy, attrib_list, configs, configCount, &newConfigCount) || newConfigCount == 0) { + delete[] configs; + return false; + } + + // Find the first config that has an alpha size of 0 + for (int i = 0; i < newConfigCount; i++) { + EGLint alphaSize = -1; + if (eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &alphaSize) && alphaSize == 0) { + *config = configs[i]; + delete[] configs; + return true; + } + } + + // If no config with alpha size of 0 was found, just return the first config + *config = configs[0]; + delete[] configs; + return true; +} + bool CelestiaRenderer::initialize() { if (context == EGL_NO_CONTEXT) @@ -124,7 +154,6 @@ bool CelestiaRenderer::initialize() EGL_NONE }; - EGLint numConfigs; LOG_INFO("Initializing context"); if ((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY) { @@ -140,13 +169,13 @@ bool CelestiaRenderer::initialize() if (enableMultisample) { // Try to enable multisample but fallback if not available - if (!eglChooseConfig(display, multisampleAttribs, &config, 1, &numConfigs) && !eglChooseConfig(display, attribs, &config, 1, &numConfigs)) { + if (!ChooseOpaqueConfig(display, multisampleAttribs, &config) && !ChooseOpaqueConfig(display, attribs, &config)) { LOG_ERROR("eglChooseConfig() returned error %d", eglGetError()); destroy(); return false; } } else { - if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs)) { + if (!ChooseOpaqueConfig(display, attribs, &config)) { LOG_ERROR("eglChooseConfig() returned error %d", eglGetError()); destroy(); return false;