From 077aad9afd515a18422fdb77a724e90b2add6c7e Mon Sep 17 00:00:00 2001 From: Will S Date: Mon, 13 Dec 2021 14:57:53 +0000 Subject: [PATCH] Remove MEDIAPIPE_OMIT_EGL_WINDOW_BIT flag, and autodetect --- mediapipe/gpu/gl_context_egl.cc | 58 +++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 25 deletions(-) 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 "