diff --git a/mediapipe/gpu/gl_context_egl.cc b/mediapipe/gpu/gl_context_egl.cc index 952469270..37d0ed310 100644 --- a/mediapipe/gpu/gl_context_egl.cc +++ b/mediapipe/gpu/gl_context_egl.cc @@ -67,6 +67,23 @@ static void EnsureEglThreadRelease() { reinterpret_cast(0xDEADBEEF)); } +static std::array MakeEglConfig(EGLint gl_version, EGLint buffer_type) { + return { + // clang-format off + EGL_RENDERABLE_TYPE, gl_version, + // Allow rendering to pixel buffers or directly to windows. + EGL_SURFACE_TYPE, + buffer_type, + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_ALPHA_SIZE, 8, // if you need the alpha channel + EGL_DEPTH_SIZE, 16, // if you need the depth buffer + EGL_NONE + // clang-format on + }; +} + GlContext::StatusOrGlContext GlContext::Create(std::nullptr_t nullp, bool create_thread) { return Create(EGL_NO_CONTEXT, create_thread); @@ -89,35 +106,26 @@ absl::Status GlContext::CreateContextInternal(EGLContext share_context, int gl_version) { CHECK(gl_version == 2 || gl_version == 3); - const EGLint config_attr[] = { - // clang-format off - EGL_RENDERABLE_TYPE, gl_version == 3 ? EGL_OPENGL_ES3_BIT_KHR - : EGL_OPENGL_ES2_BIT, - // Allow rendering to pixel buffers or directly to windows. - EGL_SURFACE_TYPE, -#ifdef MEDIAPIPE_OMIT_EGL_WINDOW_BIT - EGL_PBUFFER_BIT, -#else - EGL_WINDOW_BIT, -#endif - EGL_RED_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_BLUE_SIZE, 8, - EGL_ALPHA_SIZE, 8, // if you need the alpha channel - EGL_DEPTH_SIZE, 16, // if you need the depth buffer - EGL_NONE - // clang-format on + EGLint gl_version_flag = gl_version == 3 ? EGL_OPENGL_ES3_BIT_KHR : EGL_OPENGL_ES2_BIT; + + std::vector> configs{ + MakeEglConfig(gl_version_flag, EGL_PBUFFER_BIT), + MakeEglConfig(gl_version_flag, EGL_WINDOW_BIT) }; - // TODO: improve config selection. EGLint num_configs; - EGLBoolean success = - eglChooseConfig(display_, config_attr, &config_, 1, &num_configs); - if (!success) { - return ::mediapipe::UnknownErrorBuilder(MEDIAPIPE_LOC) - << "eglChooseConfig() returned error " << std::showbase << std::hex - << eglGetError(); + + for (auto cfg : configs) { + EGLBoolean success = + eglChooseConfig(display_, &cfg[0], &config_, 1, &num_configs); + if (!success) { + return ::mediapipe::UnknownErrorBuilder(MEDIAPIPE_LOC) + << "eglChooseConfig() returned error " << std::showbase << std::hex + << eglGetError(); + } + if (num_configs) break; } + if (!num_configs) { return mediapipe::UnknownErrorBuilder(MEDIAPIPE_LOC) << "eglChooseConfig() returned no matching EGL configuration for "