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

Code before:
PlatformEGL::PlatformEGL() noexcept = default;

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");

Review comment (bug):
please check `getPlatformDisplay != nullptr` as well

Code after:
PlatformEGL::PlatformEGL() noexcept = default;

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");