diff --git a/filament/backend/src/opengl/platforms/PlatformEGL.cpp b/filament/backend/src/opengl/platforms/PlatformEGL.cpp index 1272f7fac8..24e9d6d3ae 100644 --- a/filament/backend/src/opengl/platforms/PlatformEGL.cpp +++ b/filament/backend/src/opengl/platforms/PlatformEGL.cpp @@ -120,27 +120,33 @@ bool PlatformEGL::isOpenGL() const noexcept { PlatformEGL::ExternalImageEGL::~ExternalImageEGL() = default; Driver* PlatformEGL::createDriver(void* sharedContext, const DriverConfig& driverConfig) { - mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); - assert_invariant(mEGLDisplay != EGL_NO_DISPLAY); + static constexpr int kMaxNumEGLDevices = 32; EGLint major, minor; - EGLBoolean initialized = eglInitialize(mEGLDisplay, &major, &minor); + EGLBoolean initialized = false; - 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); + PFNEGLQUERYDEVICESEXTPROC const eglQueryDevicesEXT = + PFNEGLQUERYDEVICESEXTPROC(eglGetProcAddress("eglQueryDevicesEXT")); + PFNEGLGETPLATFORMDISPLAYEXTPROC const getPlatformDisplay = + PFNEGLGETPLATFORMDISPLAYEXTPROC(eglGetProcAddress("eglGetPlatformDisplay")); + + if (eglQueryDevicesEXT != nullptr && getPlatformDisplay != nullptr) { + EGLint numDevices = 0; + EGLDeviceEXT eglDevices[kMaxNumEGLDevices]; + if (eglQueryDevicesEXT(kMaxNumEGLDevices, eglDevices, &numDevices)) { + for (int i = 0; i < numDevices && !initialized; ++i) { + mEGLDisplay = getPlatformDisplay(EGL_PLATFORM_DEVICE_EXT, eglDevices[i], nullptr); initialized = eglInitialize(mEGLDisplay, &major, &minor); } } } + if (!initialized) { + mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); + assert_invariant(mEGLDisplay != EGL_NO_DISPLAY); + initialized = eglInitialize(mEGLDisplay, &major, &minor); + } + if (UTILS_UNLIKELY(!initialized)) { LOG(ERROR) << "eglInitialize failed"; return nullptr;