1- //
2- // Created by hanji on 2025/2/9.
3- //
1+ // MobileGlues - config/gpu_utils.cpp
2+ // Copyright (c) 2025-2026 MobileGL-Dev
3+ // Licensed under the GNU Lesser General Public License v2.1:
4+ // https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
5+ // SPDX-License-Identifier: LGPL-2.1-only
6+ // End of Source File Header
47
58#include " gpu_utils.h"
69#include " ../gles/loader.h"
1215#include < cstring>
1316#include < optional>
1417typedef 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
23- };
24- static const cstr vk_lib[] = {
25- " libvulkan" ,
26- nullptr
27- };
18+ static const cstr gles3_lib[] = {" libGLESv3_CM" , " libGLESv3" , nullptr };
19+ static const cstr egl_libs[] = {" libEGL" , nullptr };
20+ static const cstr vk_lib[] = {" libvulkan" , nullptr };
2821
2922namespace 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- }
23+ PFNEGLGETDISPLAYPROC eglGetDisplay = nullptr ;
24+ PFNEGLINITIALIZEPROC eglInitialize = nullptr ;
25+ PFNEGLCHOOSECONFIGPROC eglChooseConfig = nullptr ;
26+ PFNEGLCREATECONTEXTPROC eglCreateContext = nullptr ;
27+ PFNEGLMAKECURRENTPROC eglMakeCurrent = nullptr ;
28+ PFNEGLDESTROYCONTEXTPROC eglDestroyContext = nullptr ;
29+ PFNEGLTERMINATEPROC eglTerminate = nullptr ;
30+ } // namespace egl_func
3831
3932template <typename T>
4033static void * open_lib (const T names[], const char * override ) {
@@ -62,8 +55,8 @@ static bool loadEGLFunctions(void* lib) {
6255 egl_func::eglTerminate = (PFNEGLTERMINATEPROC )dlsym (lib, " eglTerminate" );
6356
6457 return egl_func::eglGetDisplay && egl_func::eglInitialize && egl_func::eglChooseConfig &&
65- egl_func::eglCreateContext && egl_func::eglMakeCurrent &&
66- egl_func::eglDestroyContext && egl_func::eglTerminate;
58+ egl_func::eglCreateContext && egl_func::eglMakeCurrent && egl_func::eglDestroyContext &&
59+ egl_func::eglTerminate;
6760}
6861
6962std::string getGPUInfo () {
@@ -85,16 +78,21 @@ std::string getGPUInfo() {
8578 return std::string ();
8679 }
8780
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- };
81+ const EGLint attribs[] = {EGL_BLUE_SIZE ,
82+ 8 ,
83+ EGL_GREEN_SIZE ,
84+ 8 ,
85+ EGL_RED_SIZE ,
86+ 8 ,
87+ EGL_ALPHA_SIZE ,
88+ 8 ,
89+ EGL_DEPTH_SIZE ,
90+ 24 ,
91+ EGL_SURFACE_TYPE ,
92+ EGL_PBUFFER_BIT ,
93+ EGL_RENDERABLE_TYPE ,
94+ EGL_OPENGL_ES2_BIT ,
95+ EGL_NONE };
9896 EGLint numConfigs = 0 ;
9997 if (egl_func::eglChooseConfig (display, attribs, nullptr , 0 , &numConfigs) != EGL_TRUE || numConfigs == 0 ) {
10098 egl_func::eglTerminate (display);
@@ -104,7 +102,7 @@ std::string getGPUInfo() {
104102 EGLConfig config;
105103 egl_func::eglChooseConfig (display, attribs, &config, 1 , &numConfigs);
106104
107- const EGLint ctxAttribs[] = { EGL_CONTEXT_CLIENT_VERSION , 3 , EGL_NONE };
105+ const EGLint ctxAttribs[] = {EGL_CONTEXT_CLIENT_VERSION , 3 , EGL_NONE };
108106 EGLContext ctx = egl_func::eglCreateContext (display, config, EGL_NO_CONTEXT , ctxAttribs);
109107 if (ctx == EGL_NO_CONTEXT ) {
110108 egl_func::eglTerminate (display);
@@ -122,7 +120,7 @@ std::string getGPUInfo() {
122120 void * glesLib = open_lib (gles3_lib, nullptr );
123121 std::string renderer;
124122 if (glesLib) {
125- auto glGetString = (const GLubyte * (*)(GLenum))dlsym (glesLib, " glGetString" );
123+ auto glGetString = (const GLubyte* (*)(GLenum))dlsym (glesLib, " glGetString" );
126124 if (glGetString) {
127125 renderer = reinterpret_cast <const char *>(glGetString (GL_RENDERER ));
128126 }
@@ -138,23 +136,20 @@ std::string getGPUInfo() {
138136}
139137
140138int isAdreno (const char * gpu) {
141- // const char* gpu = getGPUInfo();
142- if (!gpu)
143- return 0 ;
139+ // const char* gpu = getGPUInfo();
140+ if (!gpu) return 0 ;
144141 return strstr (gpu, " Adreno" ) != nullptr ;
145142}
146143
147144int isAdreno740 (const char * gpu) {
148145 // const char* gpu = getGPUInfo();
149- if (!gpu)
150- return 0 ;
146+ if (!gpu) return 0 ;
151147 return isAdreno (gpu) && (strstr (gpu, " 740" ) != nullptr );
152148}
153149
154150int isAdreno730 (const char * gpu) {
155151 // const char* gpu = getGPUInfo();
156- if (!gpu)
157- return 0 ;
152+ if (!gpu) return 0 ;
158153 return isAdreno (gpu) && (strstr (gpu, " 730" ) != nullptr );
159154}
160155
@@ -163,41 +158,35 @@ bool checkIfANGLESupported(const char* gpu) {
163158}
164159
165160int isAdreno830 (const char * gpu) {
166- // const char* gpu = getGPUInfo();
167- if (!gpu)
168- return 0 ;
161+ // const char* gpu = getGPUInfo();
162+ if (!gpu) return 0 ;
169163 return isAdreno (gpu) && (strstr (gpu, " 830" ) != nullptr );
170164}
171165
172166static std::optional<int > hasVk12;
173167int hasVulkan12 () {
174- if (hasVk12.has_value ())
175- return hasVk12.value ();
168+ if (hasVk12.has_value ()) return hasVk12.value ();
176169 void * vulkan_lib = open_lib (vk_lib, nullptr );
177- if (!vulkan_lib)
178- return 0 ;
170+ if (!vulkan_lib) return 0 ;
179171
180172#ifndef __APPLE__
181-
173+
182174 typedef VkResult (*PFN_vkEnumerateInstanceExtensionProperties)(const char *, uint32_t *, VkExtensionProperties*);
183175 typedef VkResult (*PFN_vkCreateInstance)(const VkInstanceCreateInfo*, const VkAllocationCallbacks*, VkInstance*);
184176 typedef void (*PFN_vkDestroyInstance)(VkInstance, const VkAllocationCallbacks*);
185177 typedef VkResult (*PFN_vkEnumeratePhysicalDevices)(VkInstance, uint32_t *, VkPhysicalDevice*);
186178 typedef void (*PFN_vkGetPhysicalDeviceProperties)(VkPhysicalDevice, VkPhysicalDeviceProperties*);
187179
188180 auto vkEnumerateInstanceExtensionProperties =
189- (PFN_vkEnumerateInstanceExtensionProperties)dlsym (vulkan_lib, " vkEnumerateInstanceExtensionProperties" );
190- auto vkCreateInstance =
191- (PFN_vkCreateInstance)dlsym (vulkan_lib, " vkCreateInstance" );
192- auto vkDestroyInstance =
193- (PFN_vkDestroyInstance)dlsym (vulkan_lib, " vkDestroyInstance" );
194- auto vkEnumeratePhysicalDevices =
195- (PFN_vkEnumeratePhysicalDevices)dlsym (vulkan_lib, " vkEnumeratePhysicalDevices" );
181+ (PFN_vkEnumerateInstanceExtensionProperties)dlsym (vulkan_lib, " vkEnumerateInstanceExtensionProperties" );
182+ auto vkCreateInstance = (PFN_vkCreateInstance)dlsym (vulkan_lib, " vkCreateInstance" );
183+ auto vkDestroyInstance = (PFN_vkDestroyInstance)dlsym (vulkan_lib, " vkDestroyInstance" );
184+ auto vkEnumeratePhysicalDevices = (PFN_vkEnumeratePhysicalDevices)dlsym (vulkan_lib, " vkEnumeratePhysicalDevices" );
196185 auto vkGetPhysicalDeviceProperties =
197- (PFN_vkGetPhysicalDeviceProperties)dlsym (vulkan_lib, " vkGetPhysicalDeviceProperties" );
186+ (PFN_vkGetPhysicalDeviceProperties)dlsym (vulkan_lib, " vkGetPhysicalDeviceProperties" );
198187
199- if (!vkEnumerateInstanceExtensionProperties || !vkCreateInstance ||
200- !vkDestroyInstance || ! vkEnumeratePhysicalDevices || !vkGetPhysicalDeviceProperties) {
188+ if (!vkEnumerateInstanceExtensionProperties || !vkCreateInstance || !vkDestroyInstance ||
189+ !vkEnumeratePhysicalDevices || !vkGetPhysicalDeviceProperties) {
201190 dlclose (vulkan_lib);
202191 return 0 ;
203192 }
@@ -265,6 +254,6 @@ int hasVulkan12() {
265254 dlclose (vulkan_lib);
266255 hasVk12 = false ;
267256 return 0 ;
268-
257+
269258#endif
270259}
0 commit comments