Skip to content

Commit 107e15d

Browse files
committed
Linux Ports (All): Make EGL initialization more robust.
1 parent df06de4 commit 107e15d

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

desmume/src/frontend/posix/shared/egl_3Demu.cpp

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2024 DeSmuME team
2+
Copyright (C) 2024-2025 DeSmuME team
33
44
This file is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -45,21 +45,73 @@ static bool __egl_initOpenGL(const int requestedAPI, const int requestedProfile,
4545

4646
EGLint eglMajorVersion;
4747
EGLint eglMinorVersion;
48-
48+
EGLBoolean didDisplayInitialize = EGL_FALSE;
4949
EGLAttrib displayAttr[] = {EGL_NONE};
50+
51+
// Try to initialize EGL on a Wayland display first, which should be available on most
52+
// modern platforms.
5053
currDisplay = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_EXT, EGL_DEFAULT_DISPLAY, displayAttr);
51-
if(currDisplay == EGL_NO_DISPLAY)
54+
if (currDisplay != EGL_NO_DISPLAY)
55+
{
56+
didDisplayInitialize = eglInitialize(currDisplay, &eglMajorVersion, &eglMinorVersion);
57+
if (didDisplayInitialize == GL_TRUE)
58+
{
59+
puts("EGL: Successfully initialized with Wayland.");
60+
}
61+
}
62+
63+
// Try to initialize EGL for XCB next, which should be available for modern X11 platforms.
64+
if (didDisplayInitialize == GL_FALSE)
65+
{
5266
currDisplay = eglGetPlatformDisplay(EGL_PLATFORM_XCB_EXT, EGL_DEFAULT_DISPLAY, displayAttr);
53-
if(currDisplay == EGL_NO_DISPLAY)
67+
if (currDisplay != EGL_NO_DISPLAY)
68+
{
69+
didDisplayInitialize = eglInitialize(currDisplay, &eglMajorVersion, &eglMinorVersion);
70+
if (didDisplayInitialize == GL_TRUE)
71+
{
72+
puts("EGL: Successfully initialized with XCB.");
73+
}
74+
}
75+
}
76+
77+
// If for some reason XCB didn't work, try to initialize EGL on whatever X11 is available.
78+
if (didDisplayInitialize == GL_FALSE)
79+
{
80+
currDisplay = eglGetPlatformDisplay(EGL_PLATFORM_X11_EXT, EGL_DEFAULT_DISPLAY, displayAttr);
81+
if (currDisplay != EGL_NO_DISPLAY)
82+
{
83+
didDisplayInitialize = eglInitialize(currDisplay, &eglMajorVersion, &eglMinorVersion);
84+
if (didDisplayInitialize == GL_TRUE)
85+
{
86+
puts("EGL: Successfully initialized with generic X11.");
87+
}
88+
}
89+
}
90+
91+
// For ancient platforms that don't support eglGetPlatformDisplay(), an EGL v1.5 function,
92+
// try calling the generic eglGetDisplay() function as a last resort since it is compatible
93+
// with older versions of EGL.
94+
if (didDisplayInitialize == GL_FALSE)
95+
{
5496
currDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
55-
if(currDisplay == EGL_NO_DISPLAY)
97+
if (currDisplay != EGL_NO_DISPLAY)
98+
{
99+
didDisplayInitialize = eglInitialize(currDisplay, &eglMajorVersion, &eglMinorVersion);
100+
if (didDisplayInitialize == GL_TRUE)
101+
{
102+
puts("EGL: Successfully initialized with a generic display.");
103+
}
104+
}
105+
}
106+
107+
if (currDisplay == EGL_NO_DISPLAY)
56108
{
57-
puts("EGL: failed to obtain display handle");
109+
puts("EGL: Could not acquire a display handle to initialize EGL.");
58110
return false;
59111
}
60-
if (eglInitialize(currDisplay, &eglMajorVersion, &eglMinorVersion) == EGL_FALSE)
112+
else if (didDisplayInitialize == GL_FALSE)
61113
{
62-
puts("EGL: eglInitialize failed");
114+
puts("EGL: Could not initialize EGL with the acquired display handle.");
63115
return false;
64116
}
65117

0 commit comments

Comments
 (0)