|
11 | 11 | #include <EGL/egl.h> |
12 | 12 | #include <cstring> |
13 | 13 | #include <optional> |
14 | | - |
15 | | -static const char *gles3_lib[] = { |
16 | | - "libGLESv3_CM", |
17 | | - "libGLESv3", |
18 | | - nullptr |
| 14 | +typedef const char* cstr; |
| 15 | +static const cstr gles3_lib[] = { |
| 16 | + "libGLESv3_CM", |
| 17 | + "libGLESv3", |
| 18 | + nullptr |
| 19 | +}; |
| 20 | +static const cstr egl_libs[] = { |
| 21 | + "libEGL", |
| 22 | + nullptr |
19 | 23 | }; |
20 | | -static const char *vk_lib[] = { |
21 | | - "libvulkan", |
22 | | - nullptr |
| 24 | +static const cstr vk_lib[] = { |
| 25 | + "libvulkan", |
| 26 | + nullptr |
23 | 27 | }; |
24 | 28 |
|
| 29 | +namespace egl_func { |
| 30 | + PFNEGLGETDISPLAYPROC eglGetDisplay = nullptr; |
| 31 | + PFNEGLINITIALIZEPROC eglInitialize = nullptr; |
| 32 | + PFNEGLCHOOSECONFIGPROC eglChooseConfig = nullptr; |
| 33 | + PFNEGLCREATECONTEXTPROC eglCreateContext = nullptr; |
| 34 | + PFNEGLMAKECURRENTPROC eglMakeCurrent = nullptr; |
| 35 | + PFNEGLDESTROYCONTEXTPROC eglDestroyContext = nullptr; |
| 36 | + PFNEGLTERMINATEPROC eglTerminate = nullptr; |
| 37 | +} |
25 | 38 |
|
26 | | -std::string getGPUInfo() { |
27 | | - EGLDisplay eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
28 | | - if (eglDisplay == EGL_NO_DISPLAY || eglInitialize(eglDisplay, nullptr, nullptr) != EGL_TRUE) |
29 | | - return ""; |
30 | | - |
31 | | - EGLint egl_attributes[] = { |
32 | | - EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, |
33 | | - EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
34 | | - EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE |
35 | | - }; |
| 39 | +template <typename T> |
| 40 | +static void* open_lib(const T names[], const char* override) { |
| 41 | + void* handle = nullptr; |
| 42 | + int flags = RTLD_LOCAL | RTLD_NOW; |
| 43 | + if (override) { |
| 44 | + handle = dlopen(override, flags); |
| 45 | + if (handle) return handle; |
| 46 | + } |
| 47 | + for (int i = 0; names[i]; ++i) { |
| 48 | + handle = dlopen(names[i], flags); |
| 49 | + if (handle) break; |
| 50 | + } |
| 51 | + return handle; |
| 52 | +} |
36 | 53 |
|
37 | | - EGLint num_configs = 0; |
38 | | - if (eglChooseConfig(eglDisplay, egl_attributes, nullptr, 0, &num_configs) != EGL_TRUE || num_configs == 0) { |
39 | | - eglTerminate(eglDisplay); |
40 | | - return ""; |
| 54 | +static bool loadEGLFunctions(void* lib) { |
| 55 | + if (!lib) return false; |
| 56 | + egl_func::eglGetDisplay = (PFNEGLGETDISPLAYPROC)dlsym(lib, "eglGetDisplay"); |
| 57 | + egl_func::eglInitialize = (PFNEGLINITIALIZEPROC)dlsym(lib, "eglInitialize"); |
| 58 | + egl_func::eglChooseConfig = (PFNEGLCHOOSECONFIGPROC)dlsym(lib, "eglChooseConfig"); |
| 59 | + egl_func::eglCreateContext = (PFNEGLCREATECONTEXTPROC)dlsym(lib, "eglCreateContext"); |
| 60 | + egl_func::eglMakeCurrent = (PFNEGLMAKECURRENTPROC)dlsym(lib, "eglMakeCurrent"); |
| 61 | + egl_func::eglDestroyContext = (PFNEGLDESTROYCONTEXTPROC)dlsym(lib, "eglDestroyContext"); |
| 62 | + egl_func::eglTerminate = (PFNEGLTERMINATEPROC)dlsym(lib, "eglTerminate"); |
| 63 | + |
| 64 | + return egl_func::eglGetDisplay && egl_func::eglInitialize && egl_func::eglChooseConfig && |
| 65 | + egl_func::eglCreateContext && egl_func::eglMakeCurrent && |
| 66 | + egl_func::eglDestroyContext && egl_func::eglTerminate; |
| 67 | +} |
| 68 | + |
| 69 | +std::string getGPUInfo() { |
| 70 | + void* egllib = open_lib(egl_libs, nullptr); |
| 71 | + if (!loadEGLFunctions(egllib)) { |
| 72 | + if (egllib) dlclose(egllib); |
| 73 | + return std::string(); |
41 | 74 | } |
42 | 75 |
|
43 | | - EGLConfig eglConfig; |
44 | | - eglChooseConfig(eglDisplay, egl_attributes, &eglConfig, 1, &num_configs); |
| 76 | + EGLDisplay display = egl_func::eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 77 | + if (display == EGL_NO_DISPLAY) { |
| 78 | + egl_func::eglTerminate(display); |
| 79 | + dlclose(egllib); |
| 80 | + return std::string(); |
| 81 | + } |
| 82 | + if (egl_func::eglInitialize(display, nullptr, nullptr) != EGL_TRUE) { |
| 83 | + egl_func::eglTerminate(display); |
| 84 | + dlclose(egllib); |
| 85 | + return std::string(); |
| 86 | + } |
45 | 87 |
|
46 | | - const EGLint egl_context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE }; |
47 | | - EGLContext context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, egl_context_attributes); |
48 | | - if (context == EGL_NO_CONTEXT) { |
49 | | - eglTerminate(eglDisplay); |
50 | | - return ""; |
| 88 | + const EGLint attribs[] = { |
| 89 | + EGL_BLUE_SIZE, 8, |
| 90 | + EGL_GREEN_SIZE, 8, |
| 91 | + EGL_RED_SIZE, 8, |
| 92 | + EGL_ALPHA_SIZE, 8, |
| 93 | + EGL_DEPTH_SIZE, 24, |
| 94 | + EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 95 | + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 96 | + EGL_NONE |
| 97 | + }; |
| 98 | + EGLint numConfigs = 0; |
| 99 | + if (egl_func::eglChooseConfig(display, attribs, nullptr, 0, &numConfigs) != EGL_TRUE || numConfigs == 0) { |
| 100 | + egl_func::eglTerminate(display); |
| 101 | + dlclose(egllib); |
| 102 | + return std::string(); |
| 103 | + } |
| 104 | + EGLConfig config; |
| 105 | + egl_func::eglChooseConfig(display, attribs, &config, 1, &numConfigs); |
| 106 | + |
| 107 | + const EGLint ctxAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE }; |
| 108 | + EGLContext ctx = egl_func::eglCreateContext(display, config, EGL_NO_CONTEXT, ctxAttribs); |
| 109 | + if (ctx == EGL_NO_CONTEXT) { |
| 110 | + egl_func::eglTerminate(display); |
| 111 | + dlclose(egllib); |
| 112 | + return std::string(); |
51 | 113 | } |
52 | 114 |
|
53 | | - if (eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, context) != EGL_TRUE) { |
54 | | - eglDestroyContext(eglDisplay, context); |
55 | | - eglTerminate(eglDisplay); |
56 | | - return ""; |
| 115 | + if (egl_func::eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx) != EGL_TRUE) { |
| 116 | + egl_func::eglDestroyContext(display, ctx); |
| 117 | + egl_func::eglTerminate(display); |
| 118 | + dlclose(egllib); |
| 119 | + return std::string(); |
57 | 120 | } |
58 | 121 |
|
| 122 | + void* glesLib = open_lib(gles3_lib, nullptr); |
59 | 123 | std::string renderer; |
60 | | - void* lib = open_lib(gles3_lib, nullptr); |
61 | | - if (lib) { |
62 | | - GLES.glGetString = (const GLubyte * (*)( GLenum ))dlsym(lib, "glGetString"); |
63 | | - if (GLES.glGetString) { |
64 | | - renderer = (const char*)GLES.glGetString(GL_RENDERER); |
| 124 | + if (glesLib) { |
| 125 | + auto glGetString = (const GLubyte * (*)(GLenum))dlsym(glesLib, "glGetString"); |
| 126 | + if (glGetString) { |
| 127 | + renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER)); |
65 | 128 | } |
| 129 | + dlclose(glesLib); |
66 | 130 | } |
67 | 131 |
|
68 | | - eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
69 | | - eglDestroyContext(eglDisplay, context); |
70 | | - eglTerminate(eglDisplay); |
71 | | - |
72 | | - dlclose(lib); |
| 132 | + egl_func::eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 133 | + egl_func::eglDestroyContext(display, ctx); |
| 134 | + egl_func::eglTerminate(display); |
| 135 | + dlclose(egllib); |
73 | 136 |
|
74 | 137 | return renderer; |
75 | 138 | } |
|
0 commit comments