Skip to content

Commit

Permalink
Add function to retrieve opaque EGL config
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Feb 16, 2023
1 parent aac6cb6 commit 2850fab
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions Celestia/src/main/cpp/CelestiaRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -124,7 +154,6 @@ bool CelestiaRenderer::initialize()
EGL_NONE
};

EGLint numConfigs;
LOG_INFO("Initializing context");

if ((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY) {
Expand All @@ -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;
Expand Down

0 comments on commit 2850fab

Please sign in to comment.