From 40a6c3ea71e62346cb191de409f9eb71618b84a6 Mon Sep 17 00:00:00 2001 From: haroonq <29288912+haroonq@users.noreply.github.com> Date: Tue, 10 Mar 2026 23:42:17 +0000 Subject: [PATCH] Allow child classes to configure the EGLDisplay. (#9779) --- .../include/backend/platforms/PlatformEGL.h | 6 +++ .../src/opengl/platforms/PlatformEGL.cpp | 49 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/filament/backend/include/backend/platforms/PlatformEGL.h b/filament/backend/include/backend/platforms/PlatformEGL.h index 8b8f2a95c4..a9a51a52c7 100644 --- a/filament/backend/include/backend/platforms/PlatformEGL.h +++ b/filament/backend/include/backend/platforms/PlatformEGL.h @@ -159,6 +159,12 @@ protected: EGLConfig getEglConfig() const noexcept { return mEGLConfig; } EGLConfig getSuitableConfigForSwapChain(uint64_t flags, bool window, bool pbuffer) const; + // Sets the EGLDisplay to be used by this platform. This should only be called by derived + // classes before invoking createDriver. Calling it after that point will result in + // undefined behaviour. This class will take ownership of the display and call eglTerminate + // on it during shutdown. + void setEglDisplay(EGLDisplay display) noexcept; + // supported extensions detected at runtime struct { struct { diff --git a/filament/backend/src/opengl/platforms/PlatformEGL.cpp b/filament/backend/src/opengl/platforms/PlatformEGL.cpp index 76bcede161..130c613d95 100644 --- a/filament/backend/src/opengl/platforms/PlatformEGL.cpp +++ b/filament/backend/src/opengl/platforms/PlatformEGL.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -119,31 +120,41 @@ bool PlatformEGL::isOpenGL() const noexcept { PlatformEGL::ExternalImageEGL::~ExternalImageEGL() = default; +void PlatformEGL::setEglDisplay(EGLDisplay display) noexcept { + FILAMENT_CHECK_PRECONDITION(mEGLDisplay == EGL_NO_DISPLAY) + << "EGL Display has already been set."; + FILAMENT_CHECK_PRECONDITION(display != EGL_NO_DISPLAY) + << "Must specify a valid EGL Display."; + mEGLDisplay = display; +} + Driver* PlatformEGL::createDriver(void* sharedContext, const DriverConfig& driverConfig) { - mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); - assert_invariant(mEGLDisplay != EGL_NO_DISPLAY); + if (mEGLDisplay == EGL_NO_DISPLAY) { + mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); + assert_invariant(mEGLDisplay != EGL_NO_DISPLAY); - EGLint major, minor; - EGLBoolean initialized = eglInitialize(mEGLDisplay, &major, &minor); + EGLint major, minor; + EGLBoolean initialized = eglInitialize(mEGLDisplay, &major, &minor); - if (!initialized) { - EGLDeviceEXT eglDevice; - EGLint numDevices; - PFNEGLQUERYDEVICESEXTPROC const eglQueryDevicesEXT = - PFNEGLQUERYDEVICESEXTPROC(eglGetProcAddress("eglQueryDevicesEXT")); - if (eglQueryDevicesEXT != nullptr) { - eglQueryDevicesEXT(1, &eglDevice, &numDevices); - if(auto* getPlatformDisplay = reinterpret_cast( - eglGetProcAddress("eglGetPlatformDisplay"))) { - mEGLDisplay = getPlatformDisplay(EGL_PLATFORM_DEVICE_EXT, eglDevice, nullptr); - initialized = eglInitialize(mEGLDisplay, &major, &minor); + if (!initialized) { + EGLDeviceEXT eglDevice; + EGLint numDevices; + PFNEGLQUERYDEVICESEXTPROC const eglQueryDevicesEXT = + PFNEGLQUERYDEVICESEXTPROC(eglGetProcAddress("eglQueryDevicesEXT")); + if (eglQueryDevicesEXT != nullptr) { + eglQueryDevicesEXT(1, &eglDevice, &numDevices); + if(auto* getPlatformDisplay = reinterpret_cast( + eglGetProcAddress("eglGetPlatformDisplay"))) { + mEGLDisplay = getPlatformDisplay(EGL_PLATFORM_DEVICE_EXT, eglDevice, nullptr); + initialized = eglInitialize(mEGLDisplay, &major, &minor); + } } } - } - if (UTILS_UNLIKELY(!initialized)) { - LOG(ERROR) << "eglInitialize failed"; - return nullptr; + if (UTILS_UNLIKELY(!initialized)) { + LOG(ERROR) << "eglInitialize failed"; + return nullptr; + } } #if defined(FILAMENT_IMPORT_ENTRY_POINTS)