Skip to content

Implemented optional EGL based GL/CL interop - #22704

Open
kallaballa wants to merge 21 commits into
opencv:4.xfrom
kallaballa:replace_glx_with_egl
Open

kallaballa wants to merge 21 commits into
opencv:4.xfrom
kallaballa:replace_glx_with_egl

Conversation

@kallaballa

Copy link
Copy Markdown
Contributor

The pertaining issue: #22703

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov

Copy link
Copy Markdown
Contributor

OpenCV may use GLX for several purposes:

  • Rendering itself in highgui
  • OpenGL interop in core with cv::Mat
  • OpenGL interop for CUDA and OpenCL
  • Indirect interop in context of video codecs
    EGL integration is useful, but should be optional on CMake level.

Comment thread modules/core/CMakeLists.txt Resolved
@kallaballa

Copy link
Copy Markdown
Contributor Author

OpenCV may use GLX for several purposes:

  • Rendering itself in highgui
  • OpenGL interop in core with cv::Mat
  • OpenGL interop for CUDA and OpenCL
  • Indirect interop in context of video codecs
    EGL integration is useful, but should be optional on CMake level.

But why would you use GLX if you already have EGL (e.g. for android CL/GL interop). Could you please explain a use case where having EGL & GLX provides a benefit? Because all of those things (and more... e.g. offscreen rendering) can be done with EGL.

@asmorkalov

Copy link
Copy Markdown
Contributor

Let's say, historical reason. Some code was contributed long time ago and not developed actively. I'm not against of EGL, but prefer safe way - not destroy existing features.

@kallaballa

Copy link
Copy Markdown
Contributor Author

I see now what you mean. There is another place where GLX is used:

#include <GL/glx.h>

Anyway, I understand your point but on the other hand I wouldn't mind also replacing the CV_GL_GET_PROC_ADDRESS implementation and be done with GLX. :)

@kallaballa

Copy link
Copy Markdown
Contributor Author

Anyway... even if you don't want to replace GLX completely, this pull request still makes sense. Replacing GLX with EGL for OpenGL interop brings many advantages, like:

@kallaballa

Copy link
Copy Markdown
Contributor Author
  • EGL integration is useful, but should be optional on CMake level.

I understand now. and will implement it like that.

@kallaballa

Copy link
Copy Markdown
Contributor Author

Done de6bb16

@kallaballa kallaballa changed the title replaced glx with egl Implemented optional EGL based GL/CL interop Nov 5, 2022
@kallaballa

Copy link
Copy Markdown
Contributor Author

I improved the implementation by defining a proper OCV_OPTION OPENCV_ENABLE_EGL_INTEROP that is OFF by default and depends on WITH_OPENGL. if OPENCV_ENABLE_EGL_INTEROP is ON then the compile flag -DHAVE_EGL_INTEROP for the file modules/core/src/opengl.cpp is set which in turn enables EGL-based OpenGL-interop.

@kallaballa

Copy link
Copy Markdown
Contributor Author

I thought about it and maybe it would be a good idea to infer which window-system API (GLX or EGL) to use by probing eglGetCurrentContext() and glXGetCurrentContext() but only if HAVE_EGL_INTEROP. That way both systems can be supported.
What do you think?

@asmorkalov

Copy link
Copy Markdown
Contributor

I tried both branches on my Ubuntu 18.04 host. Both implementations work, but final executable has significantly different dependencies. I do not think that dependency from both libEGL and libGLX is good idea.

@asmorkalov

Copy link
Copy Markdown
Contributor

Also not all Linux systems provide EGL support. Please check OpenGL_EGL_FOUND beforehand: https://cmake.org/cmake/help/latest/module/FindOpenGL.html

@kallaballa

Copy link
Copy Markdown
Contributor Author

Also not all Linux systems provide EGL support. Please check OpenGL_EGL_FOUND beforehand: https://cmake.org/cmake/help/latest/module/FindOpenGL.html

Alright!

I tried both branches on my Ubuntu 18.04 host. Both implementations work, but final executable has significantly different dependencies. I do not think that dependency from both libEGL and libGLX is good idea.

I understand your intuition in this (I had the same) but since i really want this feature i went to find evidence. ;)

First i tried to find examples of libraries on my system linking to both

find /usr/lib64/ -maxdepth 1 -name "*.so" | while read lib; do
    deps="$(sudo ldd "$lib" 2>/dev/null | tr -d '\n')"; 
    echo "$deps" | grep -q -E "libGLX.so.*libEGL.so|libEGL.so.*libGLX.so"; 
    if [ $? -eq 0 ]; then 
        echo "$lib"; 
    fi; 
done

And i found the following files:

/usr/lib64/libshotwell-authenticator.so
/usr/lib64/libshotwell-plugin-common.so
/usr/lib64/libshotwell-plugin-dev-1.0.so
/usr/lib64/libpangocairo-1.0.so
/usr/lib64/liblibreofficekitgtk.so
/usr/lib64/libgailutil.so
/usr/lib64/libgdk-x11-2.0.so
/usr/lib64/libgtk-x11-2.0.so
/usr/lib64/librsvg-2.so
/usr/lib64/libgailutil-3.so
/usr/lib64/libcairomm-1.0.so
/usr/lib64/libpangomm-1.4.so
/usr/lib64/libgdkmm-2.4.so
/usr/lib64/libgtkmm-2.4.so
/usr/lib64/libgdkmm-3.0.so
/usr/lib64/libgtkmm-3.0.so
/usr/lib64/libcairomm-1.16.so
/usr/lib64/libpangomm-2.48.so
/usr/lib64/libgtk-4.so
/usr/lib64/libgtkmm-4.0.so
/usr/lib64/libglade-2.0.so
/usr/lib64/libgladeui-2.so
/usr/lib64/libadwaita-1.so
/usr/lib64/libgnome-bluetooth-ui-3.0.so
/usr/lib64/libgdk-3.so
/usr/lib64/libgtk-3.so
/usr/lib64/libgimp-2.0.so
/usr/lib64/libgimpcolor-2.0.so
/usr/lib64/libgimpconfig-2.0.so
/usr/lib64/libgimpmodule-2.0.so
/usr/lib64/libgimpui-2.0.so
/usr/lib64/libgimpwidgets-2.0.so
/usr/lib64/libgstgl-1.0.so
/usr/lib64/libcairo-gobject.so
/usr/lib64/libcairo-script-interpreter.so
/usr/lib64/libcairo.so
/usr/lib64/libQt5EglFSDeviceIntegration.so
/usr/lib64/libQt5EglFsKmsSupport.so
/usr/lib64/libgdkglext-x11-1.0.so
/usr/lib64/libgtkglext-x11-1.0.so
/usr/lib64/libavahi-ui-gtk3.so
/usr/lib64/libperf-gtk.so

So at least somebody is actively linking EGL and GLX at the same time. But what struck my eye quickly is that it's mostly GTK stack.... So i went to search in their repo and found this: https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/3540

They are basically using GLX as a fallback to EGL.

@kallaballa

kallaballa commented Nov 15, 2022 •

Copy link
Copy Markdown
Contributor Author

Half a year later firefox did the same: https://mozillagfx.wordpress.com/2021/10/30/switching-the-linux-graphics-stack-from-glx-to-egl/

Btw. I just realized I left -maxdepth 1 in my find command. There are more examples.

@asmorkalov

Copy link
Copy Markdown
Contributor

Thanks a lot for the research! Could you check if the GTK stack does lazy linkage with EGL and GLX?

@kallaballa

kallaballa commented Nov 17, 2022 •

Copy link
Copy Markdown
Contributor Author

I am on it :)

@kallaballa

kallaballa commented Nov 20, 2022 •

Copy link
Copy Markdown
Contributor Author

I have following statement from a GTK developer: "EGL has issues with properly reporting the RGBA VIsual, which is why GTK defaults to GLX. You can often just force the RGBA Visual and it'll work, but GTK isn't doing that yet (Firefox and Chrome switched recently I think, maybe it got fixed or there's a an official way, didn't investigate yet)
in any case: GTK does pick one or the other at startup and then disallows the other."

About the exact circumstances of linking i still have to investigate and I am also going to branch out to the Firefox community and see if they have advice.

@kallaballa

Copy link
Copy Markdown
Contributor Author

Statement from a Firefox dev: "Hi! So unfortunately Mesas X11 EGL implementation still has some open issues (e.g. https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9989 ....) which is why GTK4 still falls back to GLX. Generally GTK4 and OBS-Studio might be simpler examples than FF."

@kallaballa

kallaballa commented Nov 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Also not all Linux systems provide EGL support. Please check OpenGL_EGL_FOUND beforehand: https://cmake.org/cmake/help/latest/module/FindOpenGL.html

There is no check for GLX in place either (as far as i can see). Can't hurt to add that too?

@kallaballa

kallaballa commented Nov 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Also not all Linux systems provide EGL support. Please check OpenGL_EGL_FOUND beforehand: https://cmake.org/cmake/help/latest/module/FindOpenGL.html

Done 735a0f2

There is no check for GLX in place either (as far as i can see). Can't hurt to add that too?

Done 49f64dd

@asmorkalov asmorkalov added this to the 4.11.0 milestone Jul 5, 2024
@kallaballa

Copy link
Copy Markdown
Contributor Author

I have this in use on a couple of Linux systems (OpenSuSe, Debian, Ubuntu) mostly Intel iGPU systems but also one with Nvidia GPU. Intel requires EGL while Nvidia requires GLX. On the NVidia system the Intel iGPU is available as well and works - so i can switch between them with the same binary due to the GLX fallback mechanism.
What is missing?

@asmorkalov asmorkalov modified the milestones: 4.11.0, 4.12.0 Dec 20, 2024
@kallaballa

Copy link
Copy Markdown
Contributor Author

Tested using an Intel A770 and Mesa 24.3.1 as well

@kallaballa

Copy link
Copy Markdown
Contributor Author

Resolved merge conflict. testing.

@kallaballa

Copy link
Copy Markdown
Contributor Author

Tested. Still works as expected.

@asmorkalov
asmorkalov force-pushed the replace_glx_with_egl branch from d243a30 to 3b3f415 Compare May 12, 2025 16:01
Comment thread cmake/OpenCVFindLibsGUI.cmake Outdated
if(WITH_WIN32UI OR (HAVE_QT AND QT_QTOPENGL_FOUND) OR HAVE_GTK3 OR (HAVE_GTK AND NOT HAVE_GTK3 AND HAVE_GTKGLEXT))
find_package (OpenGL QUIET)
if(OpenGL_EGL_FOUND)
set(HAVE_EGL TRUE AND OPENCV_ENABLE_EGL)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake's set doesn't support complex boolean values.

Comment thread modules/core/src/opengl.cpp Outdated
# if defined(OPENCV_ENABLE_EGL)
# include <EGL/egl.h>
# endif
# if defined(OPENCV_ENABLE_GLX)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these vars defined?

if(OPENCV_LIBVA_LINK)
ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/va_intel.cpp" "OPENCV_LIBVA_LINK=1")
endif()
if(OPENCV_ENABLE_EGL_INTEROP)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we define this flag?

@asmorkalov asmorkalov modified the milestones: 4.12.0, 4.13.0 Jun 11, 2025
@kallaballa

Copy link
Copy Markdown
Contributor Author

i fixed the issues

@asmorkalov asmorkalov modified the milestones: 4.13.0, 4.14.0 Dec 8, 2025
@asmorkalov asmorkalov modified the milestones: 4.14.0, 4.15.0 Jul 9, 2026

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🔴 Red

Verdict: Refactor before merge.

Preserve default Linux GLX interoperability and make EGL detection usable headlessly; the current option defaults can leave neither interop path enabled.

Please address this in the current PR and add focused regression coverage where applicable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants