Repository: google/filament
File: filament/backend/src/opengl/platforms/PlatformEGL.cpp (C++)

Code before:

int PlatformEGL::getOSVersion() const noexcept {
    return 0;
}

bool PlatformEGL::isOpenGL() const noexcept {
    return false;
}

PlatformEGL::ExternalImageEGL::~ExternalImageEGL() = default;

Driver* PlatformEGL::createDriver(void* sharedContext, const DriverConfig& driverConfig) {
    static constexpr int kMaxNumEGLDevices = 32;

    EGLint major, minor;
    EGLBoolean initialized = false;

    EGLDeviceEXT eglDevices[kMaxNumEGLDevices];
    EGLint numDevices;
    PFNEGLQUERYDEVICESEXTPROC const eglQueryDevicesEXT =
        PFNEGLQUERYDEVICESEXTPROC(eglGetProcAddress("eglQueryDevicesEXT"));
    PFNEGLGETPLATFORMDISPLAYEXTPROC const getPlatformDisplay =
        PFNEGLGETPLATFORMDISPLAYEXTPROC(eglGetProcAddress("eglGetPlatformDisplay"));

    if (eglQueryDevicesEXT != nullptr) {
        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;
    }

#if defined(FILAMENT_IMPORT_ENTRY_POINTS)
    importGLESExtensionsEntryPoints();
#endif

    auto const extensions = GLUtils::split(eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
    ext.egl.ANDROID_recordable = extensions.has("EGL_ANDROID_recordable");
    ext.egl.KHR_gl_colorspace = extensions.has("EGL_KHR_gl_colorspace");
    ext.egl.KHR_create_context = extensions.has("EGL_KHR_create_context");

Review comment (bug):
You need to check the return value of this call. Otherwise `numDevices` may have an invalid value, thereby producing undefined behavior for the following loop.

Also I would initialize the variable just in case.
```
EGLint numDevices = 0;
```

Code after:

int PlatformEGL::getOSVersion() const noexcept {
    return 0;
}

bool PlatformEGL::isOpenGL() const noexcept {
    return false;
}

PlatformEGL::ExternalImageEGL::~ExternalImageEGL() = default;

Driver* PlatformEGL::createDriver(void* sharedContext, const DriverConfig& driverConfig) {
    static constexpr int kMaxNumEGLDevices = 32;

    EGLint major, minor;
    EGLBoolean initialized = false;

    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;
    }

#if defined(FILAMENT_IMPORT_ENTRY_POINTS)
    importGLESExtensionsEntryPoints();
#endif

    auto const extensions = GLUtils::split(eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
    ext.egl.ANDROID_recordable = extensions.has("EGL_ANDROID_recordable");
    ext.egl.KHR_gl_colorspace = extensions.has("EGL_KHR_gl_colorspace");