detect_prime_egl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**************************************************************************/
  2. /* detect_prime_egl.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef GLES3_ENABLED
  31. #ifdef EGL_ENABLED
  32. #include "detect_prime_egl.h"
  33. #include "core/string/print_string.h"
  34. #include "core/string/ustring.h"
  35. #include <stdlib.h>
  36. #include <cstring>
  37. #include <sys/types.h>
  38. #include <sys/wait.h>
  39. #include <unistd.h>
  40. // To prevent shadowing warnings.
  41. #undef glGetString
  42. // Runs inside a child. Exiting will not quit the engine.
  43. void DetectPrimeEGL::create_context(EGLenum p_platform_enum) {
  44. #if defined(GLAD_ENABLED)
  45. if (!gladLoaderLoadEGL(nullptr)) {
  46. print_verbose("Unable to load EGL, GPU detection skipped.");
  47. quick_exit(1);
  48. }
  49. #endif
  50. EGLDisplay egl_display = EGL_NO_DISPLAY;
  51. if (GLAD_EGL_VERSION_1_5) {
  52. egl_display = eglGetPlatformDisplay(p_platform_enum, nullptr, nullptr);
  53. } else if (GLAD_EGL_EXT_platform_base) {
  54. #ifdef EGL_EXT_platform_base
  55. egl_display = eglGetPlatformDisplayEXT(p_platform_enum, nullptr, nullptr);
  56. #endif
  57. } else {
  58. egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  59. }
  60. EGLConfig egl_config;
  61. EGLContext egl_context = EGL_NO_CONTEXT;
  62. eglInitialize(egl_display, nullptr, nullptr);
  63. #if defined(GLAD_ENABLED)
  64. if (!gladLoaderLoadEGL(egl_display)) {
  65. print_verbose("Unable to load EGL, GPU detection skipped.");
  66. quick_exit(1);
  67. }
  68. #endif
  69. eglBindAPI(EGL_OPENGL_API);
  70. EGLint attribs[] = {
  71. EGL_RED_SIZE,
  72. 1,
  73. EGL_BLUE_SIZE,
  74. 1,
  75. EGL_GREEN_SIZE,
  76. 1,
  77. EGL_DEPTH_SIZE,
  78. 24,
  79. EGL_NONE,
  80. };
  81. EGLint config_count = 0;
  82. eglChooseConfig(egl_display, attribs, &egl_config, 1, &config_count);
  83. EGLint context_attribs[] = {
  84. EGL_CONTEXT_MAJOR_VERSION, 3,
  85. EGL_CONTEXT_MINOR_VERSION, 3,
  86. EGL_NONE
  87. };
  88. egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attribs);
  89. if (egl_context == EGL_NO_CONTEXT) {
  90. print_verbose("Unable to create an EGL context, GPU detection skipped.");
  91. quick_exit(1);
  92. }
  93. eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
  94. }
  95. int DetectPrimeEGL::detect_prime(EGLenum p_platform_enum) {
  96. pid_t p;
  97. int priorities[4] = {};
  98. String vendors[4];
  99. String renderers[4];
  100. for (int i = 0; i < 4; ++i) {
  101. vendors[i] = "Unknown";
  102. renderers[i] = "Unknown";
  103. }
  104. for (int i = 0; i < 4; ++i) {
  105. int fdset[2];
  106. if (pipe(fdset) == -1) {
  107. print_verbose("Failed to pipe(), using default GPU");
  108. return 0;
  109. }
  110. // Fork so the driver initialization can crash without taking down the engine.
  111. p = fork();
  112. if (p > 0) {
  113. // Main thread
  114. int stat_loc = 0;
  115. char string[201];
  116. string[200] = '\0';
  117. close(fdset[1]);
  118. waitpid(p, &stat_loc, 0);
  119. if (!stat_loc) {
  120. // No need to do anything complicated here. Anything less than
  121. // PIPE_BUF will be delivered in one read() call.
  122. // Leave it 'Unknown' otherwise.
  123. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  124. vendors[i] = string;
  125. renderers[i] = string + strlen(string) + 1;
  126. }
  127. }
  128. close(fdset[0]);
  129. } else {
  130. // In child, exit() here will not quit the engine.
  131. // Prevent false leak reports as we will not be properly
  132. // cleaning up these processes, and fork() makes a copy
  133. // of all globals.
  134. CoreGlobals::leak_reporting_enabled = false;
  135. char string[201];
  136. close(fdset[0]);
  137. setenv("DRI_PRIME", itos(i).utf8().ptr(), 1);
  138. create_context(p_platform_enum);
  139. PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)eglGetProcAddress("glGetString");
  140. const char *vendor = (const char *)glGetString(GL_VENDOR);
  141. const char *renderer = (const char *)glGetString(GL_RENDERER);
  142. unsigned int vendor_len = strlen(vendor) + 1;
  143. unsigned int renderer_len = strlen(renderer) + 1;
  144. if (vendor_len + renderer_len >= sizeof(string)) {
  145. renderer_len = 200 - vendor_len;
  146. }
  147. memcpy(&string, vendor, vendor_len);
  148. memcpy(&string[vendor_len], renderer, renderer_len);
  149. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  150. print_verbose("Couldn't write vendor/renderer string.");
  151. }
  152. close(fdset[1]);
  153. // The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
  154. // These objects will be freed anyway when the process finishes execution.
  155. quick_exit(0);
  156. }
  157. }
  158. int preferred = 0;
  159. int priority = 0;
  160. if (vendors[0] == vendors[1]) {
  161. print_verbose("Only one GPU found, using default.");
  162. return 0;
  163. }
  164. for (int i = 3; i >= 0; --i) {
  165. const Vendor *v = vendor_map;
  166. while (v->glxvendor) {
  167. if (v->glxvendor == vendors[i]) {
  168. priorities[i] = v->priority;
  169. if (v->priority >= priority) {
  170. priority = v->priority;
  171. preferred = i;
  172. }
  173. }
  174. ++v;
  175. }
  176. }
  177. print_verbose("Found renderers:");
  178. for (int i = 0; i < 4; ++i) {
  179. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  180. }
  181. print_verbose("Using renderer: " + renderers[preferred]);
  182. return preferred;
  183. }
  184. #endif // EGL_ENABLED
  185. #endif // GLES3_ENABLED